-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Configuration_File_Fallback_Values.py
More file actions
59 lines (39 loc) · 1.74 KB
/
Python_Configuration_File_Fallback_Values.py
File metadata and controls
59 lines (39 loc) · 1.74 KB
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
49
50
51
52
53
54
55
56
57
58
59
# Python Configuration File Parser
# configparser — Configuration file parser
# This module provides the ConfigParser class which implements a basic configuration language which provides a structure similar to what’s found in
# Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.
#
# Fallback Values.
# As with a dictionary, you can use a section’s get() method to provide fallback values:
#
topsecret.get('Port')
# OUTPUT: '50022'
topsecret.get('CompressionLevel')
# OUTPUT: '9'
topsecret.get('Cipher')
topsecret.get('Cipher', '3des-cbc')
# OUTPUT: '3des-cbc'
#
# Please note that default values have precedence over fallback values.
# For instance, in our example the 'CompressionLevel' key was specified only in the 'DEFAULT' section.
# If we try to get it from the section 'topsecret.server.com', we will always get the default, even if we specify a fallback:
#
topsecret.get('CompressionLevel', '3')
# OUTPUT: '9'
#
# One more thing to be aware of is that the parser-level get() method provides a custom, more complex interface, maintained for backwards compatibility.
# When using this method, a fallback value can be provided via the fallback keyword-only argument:
#
config.get('bitbucket.org', 'monster',
fallback='No such things as monsters')
# OUTPUT: 'No such things as monsters'
#
# The same fallback argument can be used with the getint(), getfloat() and getboolean() methods, for example:
#
'BatchMode' in topsecret
# OUTPUT: 'False'
topsecret.getboolean('BatchMode', fallback=True)
# OUTPUT: 'True'
config['DEFAULT']['BatchMode'] = 'no'
topsecret.getboolean('BatchMode', fallback=True)
# OUTPUT: 'False'