Mercurial > p > roundup > code
annotate roundup/anypy/strings.py @ 6476:10a8a6bc4667
Change spacing for file addition; larger comment block; top label
Add padding to file input not the close button. This groups the clear
button with its file input.
Comment block becomes larger.
In table layout, make header and data align to top of cell.
Add padding to in table headers, makes the rows easier to scan.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 28 Aug 2021 21:43:11 -0400 |
| parents | 81990ac0b013 |
| children | 82f870433b18 |
| rev | line source |
|---|---|
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
1 # Roundup represents text internally using the native Python str type. |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
2 # In Python 3, these are Unicode strings. In Python 2, these are |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
3 # encoded using UTF-8, and the Python 2 unicode type is only used in a |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
4 # few places, generally for interacting with external modules |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
5 # requiring that type to be used. |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
6 |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
7 import sys |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
8 import io |
|
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
9 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
10 _py3 = sys.version_info[0] > 2 |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
11 |
|
5418
55f09ca366c4
Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5417
diff
changeset
|
12 if _py3: |
|
55f09ca366c4
Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5417
diff
changeset
|
13 StringIO = io.StringIO |
|
55f09ca366c4
Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5417
diff
changeset
|
14 else: |
|
55f09ca366c4
Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5417
diff
changeset
|
15 StringIO = io.BytesIO |
|
55f09ca366c4
Python 3 preparation: StringIO.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5417
diff
changeset
|
16 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
17 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
18 def b2s(b): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
19 """Convert a UTF-8 encoded bytes object to the internal string format.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
20 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
21 return b.decode('utf-8') |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
22 else: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
23 return b |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
24 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
25 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
26 def s2b(s): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
27 """Convert a string object to UTF-8 encoded bytes.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
28 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
29 return s.encode('utf-8') |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
30 else: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
31 return s |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
32 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
33 |
|
5492
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
34 def bs2b(s): |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
35 """Convert a string object or UTF-8 encoded bytes to UTF-8 encoded bytes. |
|
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
36 """ |
|
5492
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
37 if _py3: |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
38 if isinstance(s, bytes): |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
39 return s |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
40 else: |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
41 return s.encode('utf-8') |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
42 else: |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
43 return s |
|
6b0c542642be
blobfiles now always stores/returns bytes
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5418
diff
changeset
|
44 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
45 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
46 def s2u(s, errors='strict'): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
47 """Convert a string object to a Unicode string.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
48 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
49 return s |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
50 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
51 return unicode(s, 'utf-8', errors) # noqa: 821 |
|
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
52 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
53 |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
54 def u2s(u): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
55 """Convert a Unicode string to the internal string format.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
56 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
57 return u |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
58 else: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
59 return u.encode('utf-8') |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
60 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
61 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
62 def us2u(s, errors='strict'): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
63 """Convert a string or Unicode string to a Unicode string.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
64 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
65 return s |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
66 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
67 if isinstance(s, unicode): # noqa: 821 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
68 return s |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
69 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
70 return unicode(s, 'utf-8', errors) # noqa: 821 |
|
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
71 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
72 |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
73 def us2s(u): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
74 """Convert a string or Unicode string to the internal string format.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
75 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
76 return u |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
77 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
78 if isinstance(u, unicode): # noqa: 821 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
79 return u.encode('utf-8') |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
80 else: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
81 return u |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
82 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
83 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
84 def uany2s(u): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
85 """Convert a Unicode string or other object to the internal string format. |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
86 |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
87 Objects that are not Unicode strings are passed to str().""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
88 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
89 return str(u) |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
90 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
91 if isinstance(u, unicode): # noqa: 821 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
92 return u.encode('utf-8') |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
93 else: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
94 return str(u) |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
95 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
96 |
|
5416
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
97 def is_us(s): |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
98 """Return whether an object is a string or Unicode string.""" |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
99 if _py3: |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
100 return isinstance(s, str) |
|
56c9bcdea47f
Python 3 preparation: unicode.
Joseph Myers <jsm@polyomino.org.uk>
parents:
diff
changeset
|
101 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
102 return isinstance(s, str) or isinstance(s, unicode) # noqa: 821 |
|
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
103 |
|
5417
c749d6795bc2
Python 3 preparation: unichr.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5416
diff
changeset
|
104 |
|
c749d6795bc2
Python 3 preparation: unichr.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5416
diff
changeset
|
105 def uchr(c): |
|
c749d6795bc2
Python 3 preparation: unichr.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5416
diff
changeset
|
106 """Return the Unicode string containing the given character.""" |
|
c749d6795bc2
Python 3 preparation: unichr.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5416
diff
changeset
|
107 if _py3: |
|
c749d6795bc2
Python 3 preparation: unichr.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5416
diff
changeset
|
108 return chr(c) |
|
c749d6795bc2
Python 3 preparation: unichr.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5416
diff
changeset
|
109 else: |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
110 return unichr(c) # noqa: 821 |
|
5525
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
111 |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
112 # CSV files used for export and import represent strings in the style |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
113 # used by repr in Python 2; this means that each byte of the UTF-8 |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
114 # representation is represented by a \x escape if not a printable |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
115 # ASCII character. When such a representation is interpreted by eval |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
116 # in Python 3, the effect is that the Unicode characters in the |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
117 # resulting string correspond to UTF-8 bytes, so encoding the string |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
118 # as ISO-8859-1 produces the correct byte-string which must then be |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
119 # decoded as UTF-8 to produce the correct Unicode string. The same |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
120 # representations are also used for journal storage in RDBMS |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
121 # databases, so that the database can be compatible between Python 2 |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
122 # and Python 3. |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
123 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
124 |
|
5525
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
125 def repr_export(v): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
126 """Return a Python-2-style representation of a value for export to CSV.""" |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
127 if _py3: |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
128 if isinstance(v, str): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
129 return repr(s2b(v))[1:] |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
130 elif isinstance(v, dict): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
131 repr_vals = [] |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
132 for key, value in sorted(v.items()): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
133 repr_vals.append('%s: %s' % (repr_export(key), |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
134 repr_export(value))) |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
135 return '{%s}' % ', '.join(repr_vals) |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
136 else: |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
137 return repr(v) |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
138 else: |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
139 return repr(v) |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
140 |
|
6048
81990ac0b013
flak8: move import; whitespace changes; ignore undefined
John Rouillard <rouilj@ieee.org>
parents:
5525
diff
changeset
|
141 |
|
5525
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
142 def eval_import(s): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
143 """Evaluate a Python-2-style value imported from a CSV file.""" |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
144 if _py3: |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
145 v = eval(s) |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
146 if isinstance(v, str): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
147 return v.encode('iso-8859-1').decode('utf-8') |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
148 elif isinstance(v, dict): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
149 v_mod = {} |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
150 for key, value in v.items(): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
151 if isinstance(key, str): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
152 key = key.encode('iso-8859-1').decode('utf-8') |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
153 if isinstance(value, str): |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
154 value = value.encode('iso-8859-1').decode('utf-8') |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
155 v_mod[key] = value |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
156 return v_mod |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
157 else: |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
158 return v |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
159 else: |
|
bb7865241f8a
Make CSV import/export compatible across Python versions (also RDBMS journals) (issue 2550976, issue 2550975).
Joseph Myers <jsm@polyomino.org.uk>
parents:
5492
diff
changeset
|
160 return eval(s) |
