[Python-checkins] cpython: Clean-up example
raymond.hettinger
python-checkins at python.org
Mon Jul 16 08:53:47 CEST 2012
http://hg.python.org/cpython/rev/cd8a7ec848e5
changeset: 78131:cd8a7ec848e5
user: Raymond Hettinger <python at rcn.com>
date: Sun Jul 15 23:53:32 2012 -0700
summary:
Clean-up example
files:
Doc/library/collections.rst | 16 +++++++++++-----
1 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -130,16 +130,22 @@
import builtins
pylookup = ChainMap(locals(), globals(), vars(builtins))
-Example of letting user specified values take precedence over environment
-variables which in turn take precedence over default values::
+Example of letting user specified command-line arguments take precedence over
+environment variables which in turn take precedence over default values::
import os, argparse
- defaults = {'color': 'red', 'user': guest}
+
+ defaults = {'color': 'red', 'user': 'guest'}
+
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user')
parser.add_argument('-c', '--color')
- user_specified = vars(parser.parse_args())
- combined = ChainMap(user_specified, os.environ, defaults)
+ namespace = parser.parse_args()
+ command_line_args = {k:v for k, v in vars(namespace).items() if v}
+
+ combined = ChainMap(command_line_args, os.environ, defaults)
+ print(combined['color'])
+ print(combined['user'])
Example patterns for using the :class:`ChainMap` class to simulate nested
contexts::
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list