-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_literal.py
More file actions
48 lines (27 loc) · 863 Bytes
/
python_literal.py
File metadata and controls
48 lines (27 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
"""
Example of how to save data as python literals in a py file
"""
outfilename = "add_book_data.pyliteral"
# get the data from the py file
from add_book_data import AddressBook
# save it as python literals:
outfile = open(outfilename, 'w')
outfile.write(str(AddressBook))
outfile.close()
## see if we can re-load it
data = open(outfilename, 'r').read()
AddressBook2 = eval(data)
if AddressBook2 == AddressBook:
print "they are the same"
## try again with the pretty print version:
import pprint
outfilename = "add_book_data.pyliteral_pretty"
outfile = open(outfilename, 'w')
outfile.write(pprint.pformat(AddressBook))
outfile.close()
## see if we can re-load it
data = open(outfilename, 'r').read()
AddressBook2 = eval(data)
if AddressBook2 == AddressBook:
print "pretty printed version is the same as well"