Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b2269bac
Commit
b2269bac
authored
Jul 15, 2012
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean-up example
parent
4866266b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
5 deletions
+11
-5
Doc/library/collections.rst
Doc/library/collections.rst
+11
-5
No files found.
Doc/library/collections.rst
View file @
b2269bac
...
...
@@ -130,16 +130,22 @@ Example of simulating Python's internal lookup chain::
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::
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment