Skip to content

Commit 73a8e97

Browse files
author
Anastomose
committed
Upstream master pull
2 parents f044e47 + 5c0aa22 commit 73a8e97

56 files changed

Lines changed: 55012 additions & 154 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Examples/Session05/codingbat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# Python > Warmup-1 > sleep_in
1010

11+
1112
def sleep_in(weekday, vacation):
1213
return not (weekday == True and vacation == False)
1314

1.39 KB
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
I Can Eat Glass:
2+
3+
And from the sublime to the ridiculous, here is a certain phrase in an assortment of languages:
4+
5+
Sanskrit: काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥
6+
7+
Sanskrit (standard transcription): kācaṃ śaknomyattum; nopahinasti mām.
8+
9+
Classical Greek: ὕαλον ϕαγεῖν δύναμαι· τοῦτο οὔ με βλάπτει.
10+
11+
Greek (monotonic): Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα.
12+
13+
Greek (polytonic): Μπορῶ νὰ φάω σπασμένα γυαλιὰ χωρὶς νὰ πάθω τίποτα.
14+
15+
Latin: Vitrum edere possum; mihi non nocet.
16+
17+
Old French: Je puis mangier del voirre. Ne me nuit.
18+
19+
French: Je peux manger du verre, ça ne me fait pas mal.
20+
21+
Provençal / Occitan: Pòdi manjar de veire, me nafrariá pas.
22+
23+
Québécois: J'peux manger d'la vitre, ça m'fa pas mal.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
hello = 'Hello '
5+
world = u'世界'
6+
7+
print hello + world
8+
9+
print u"It was nice weather today: it reached 80\u00B0"
10+
11+
print u"Maybe it will reach 90\N{degree sign}"
12+
13+
print u"It is extremely rare for it ever to reach 100° in Seattle"

Examples/Session10/latin1_test.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
An example of using latin-1 as a universal encoding
5+
6+
latin-1 is a superset of ASCII that is suitable for western european languages.
7+
8+
Is the most common, and a good default, if you need a one-byte per char encoding
9+
for European text.
10+
11+
It also has a nice property:
12+
: every byte value from 0 to 255 is avalid charactor
13+
14+
Thus you will never get an UnicodeDecodeError if
15+
you try to decode arbitrary bytes with latin-1.
16+
17+
And it can "round-trip" trhough a unicode object.
18+
19+
This can be useful is you don't know the encoding -- at least it won't break.
20+
It's also useful if you need to work with cobined text+binary data.
21+
22+
23+
24+
"""
25+
26+
# all the byte values in a bytes (str) object:
27+
all_bytes = ''.join( [chr(i) for i in range(255)] )
28+
29+
print type(all_bytes)
30+
print len(all_bytes)
31+
32+
print "Example value: 20"
33+
print ord(all_bytes[20]) == 20
34+
print "Example high value: 245"
35+
print ord(all_bytes[245]) == 245
36+
37+
# now decode it to a unicode object:
38+
try:
39+
uni = all_bytes.decode()
40+
except UnicodeDecodeError:
41+
print "OOPS: can't decode with default encoding"
42+
43+
# latin-1 works:
44+
try:
45+
all_uni = all_bytes.decode('latin-1')
46+
print "Yup -- that worked"
47+
print all_uni
48+
print "note that the ASCII subset is the same..."
49+
except UnicodeDecodeError:
50+
print "OOPS: This should have worked!!"
51+
raise
52+
53+
## now show that it round-trips:
54+
all_bytes2 = all_uni.encode('latin-1')
55+
56+
if all_bytes2 == all_bytes:
57+
print "yup -- that worked...the values are preserved on the round trip."
58+
else:
59+
print "Hey, that should have worked"
60+
61+
62+
63+
64+
65+
66+

Examples/Session10/text.utf16

1.33 KB
Binary file not shown.

Examples/Session10/text.utf32

2.65 KB
Binary file not shown.

Examples/Session10/text.utf8

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Origin (in native language) Name (in native language)
2+
Հայաստան Արամ Խաչատրյան
3+
Australia Nicole Kidman
4+
Österreich Johann Strauß
5+
Azərbaycan Vaqif Səmədoğlu
6+
Азәрбајҹан Вагиф Сәмәдоғлу
7+
Azərbaycan Heydər Əliyev
8+
Азәрбајҹан Һејдәр Әлијев
9+
België René Magritte
10+
Belgique René Magritte
11+
Belgien René Magritte
12+
বাংলা সুকুমার রায়
13+
འབྲུག་ཡུལ། མགོན་པོ་རྡོ་རྗེ།
14+
ប្រទេស​​​កម្ពុជា ព្រះ​ពុទ្ឋឃោសាចារ‌្យ​ជួន​ណាត
15+
Canada Céline Dion
16+
ᓄᓇᕗᒻᒥᐅᑦ ᓱᓴᓐ ᐊᒡᓗᒃᑲᖅ
17+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
example for what happens when you pass non-ascii unicode to a Exception
5+
"""
6+
7+
msg = u'This is an ASCII-compatible unicode message'
8+
9+
#msg = u'This is an non ASCII\N{EM DASH}compatible unicode message'
10+
11+
print "\nDo you see this message in the Exception report?\n"
12+
print msg
13+
print
14+
15+
raise ValueError(msg)
16+

Examples/Session10/unicodify.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Decorators to convert all arguments passed to a function or method to
5+
unicode or str, including default arguments
6+
7+
From: http://axialcorps.com/2014/03/20/unicode-str/
8+
9+
'''
10+
11+
12+
import sys
13+
import functools
14+
import inspect
15+
16+
def _convert_arg(arg, from_, conv, enc):
17+
'''Safely convert unicode to string or string to unicode'''
18+
return getattr(arg, conv)(encoding=enc) if isinstance(arg, from_) else arg
19+
20+
def _wrap_convert(from_type, fn, encoding=None):
21+
'''Decorate a function converting all str arguments to unicode or
22+
vice-versa'''
23+
conv = 'decode' if from_type is str else 'encode'
24+
encoding = encoding or sys.getdefaultencoding()
25+
26+
# override string defaults using partial
27+
aspec, dflts = inspect.getargspec(fn), {}
28+
if aspec.defaults:
29+
for k,v in zip(aspec.args[-len(aspec.defaults):],aspec.defaults):
30+
dflts[k] = _convert_arg(v, from_type, conv, encoding)
31+
fn = functools.partial(fn, **dflts)
32+
33+
@functools.wraps(fn.func if isinstance(fn, functools.partial) else fn)
34+
def converted(*args, **kwargs):
35+
args = [_convert_arg(a, from_type, conv, encoding) for a in args]
36+
for k,v in kwargs.iteritems():
37+
kwargs[k] = _convert_arg(v, from_type, conv, encoding)
38+
return fn(*args, **kwargs)
39+
40+
return converted
41+
42+
def unicodify(fn=None, encoding=None):
43+
'''Convert all str arguments to unicode'''
44+
if fn is None:
45+
return functools.partial(unicodify, encoding=encoding)
46+
return _wrap_convert(str, fn, encoding=encoding)
47+
48+
def stringify(fn=None, encoding=None):
49+
'''Convert all unicode arguments to str'''
50+
if fn is None:
51+
return functools.partial(stringify, encoding=encoding)
52+
return _wrap_convert(unicode, fn, encoding=encoding)
53+
54+
__all__ = ['unicodify', 'stringify']

0 commit comments

Comments
 (0)