Attachment 'uni.py'
Download 1 # -*- coding: utf-8 -*-
2
3 class Text:
4
5 def __init__(self, text):
6 self.text = text
7
8 def __unicode__(self, encoding='utf-8', errors='strict'):
9 print 'converting to unicode'
10 if isinstance(self.text, unicode):
11 return self.text
12 return unicode(self.text, encoding, errors)
13
14 def __str__(self):
15 print 'converting to string'
16 if isinstance(self.text, str):
17 return self.text
18 return self.text.encode('utf-8', 'strict')
19
20 print 'init with ascii'
21 t = Text('ASCII')
22 assert unicode(t) == u'ASCII'
23
24 print 'init with utf-8'
25 t = Text('יוניקוד')
26 assert unicode(t) == u'יוניקוד'
27
28 print 'init with unicode'
29 t = Text(u'יוניקוד')
30 assert unicode(t) == u'יוניקוד'
31
32 print 'insert string into string'
33 t = Text('ASCII')
34 assert '%s' % t == 'ASCII'
35
36 print 'insert utf-8 string into string'
37 t = Text('יוניקוד')
38 assert '%s' % t == 'יוניקוד'
39
40 print 'insert unicode into string'
41 t = Text(u'יוניקוד')
42 assert '%s' % t == 'יוניקוד'
43
44 print 'insert string into u"unicode"'
45 t = Text('ASCII')
46 assert u'%s' % t == u'ASCII'
47
48 print 'insert utf-8 string into u"unicode"'
49 t = Text('יוניקוד')
50 assert u'%s' % unicode(t) == u'יוניקוד'
51
52 print 'insert unicode into u"unicode"'
53 t = Text(u'יוניקוד')
54 assert u'%s' % unicode(t) == u'יוניקוד'
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.