summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2017-02-21 20:13:03 -0800
committerEli Bendersky <eliben@gmail.com>2017-02-21 20:13:03 -0800
commit599a495ac6efe6da9e64aa343e68a33d6a3ed5a3 (patch)
treefbe6324ae766f3c8f9e1bb74d4e95e1283d125b9 /examples
parent4771ceba9a72bfe732d06115b7b48937e4de39c0 (diff)
downloadpycparser-599a495ac6efe6da9e64aa343e68a33d6a3ed5a3.tar.gz
Tweak serialize_ast sample to use `with` statements
Diffstat (limited to 'examples')
-rw-r--r--examples/serialize_ast.py14
1 files changed, 6 insertions, 8 deletions
diff --git a/examples/serialize_ast.py b/examples/serialize_ast.py
index 8add03b..7a71f9b 100644
--- a/examples/serialize_ast.py
+++ b/examples/serialize_ast.py
@@ -26,13 +26,11 @@ ast = parser.parse(text)
# space saving, it needs Pickle's protocol version >= 2.
# The default version is 3 for python 3.x and 1 for python 2.7.
# You can always select the highest available protocol with the -1 argument.
-#
-f = open('ast', 'wb')
-pickle.dump(ast, f, protocol=-1)
-f.close()
-f = open('ast', 'rb')
-ast = pickle.load(f)
-f.close()
+with open('ast', 'wb') as f:
+ pickle.dump(ast, f, protocol=-1)
-ast.show()
+# Deserialize.
+with open('ast', 'rb') as f:
+ ast = pickle.load(f)
+ ast.show()