Version 1.0
Copyright © 2023 Lowell D. Thomas
python-ini
… powered by
Python APG
Main Page
Related Pages
Packages
Packages
Package Functions
All
b
d
e
f
h
i
k
l
m
n
r
s
t
u
v
w
Functions
Variables
d
e
f
h
i
k
r
s
t
u
v
w
Classes
Class List
Class Index
Class Members
All
Functions
Variables
Files
File List
•
All
Classes
Namespaces
Files
Functions
Variables
Pages
examples
defaults.py
Go to the documentation of this file.
1
''' @file examples/defaults.py
2
@brief A demonstration of specifying default values for missing section or key names.
3
'''
4
5
import
sys
6
import
os
7
# add the current working directory to the path
8
# DO NOT MOVE THE FOLLOWING STATEMENT
9
# if using autopep8 formatter, for example, set argument '--ignore=E402'
10
sys.path.append(os.getcwd())
11
from
python_ini.ini_file
import
IniFile
12
13
title =
'''This example demonstrates the use of default values for specific keys.
14
Some requests are for section names and keys that do not exist in the INI file.
15
In these cases default values are specified. Note that default values are passed
16
through exactly as specified and may not have the same characteristics of valid
17
key values.
18
'''
19
print()
20
print(title)
21
22
ini_file =
'''
23
[SECTION.1]
24
number = 100
25
name = "Sam Johnson"
26
false-flag = false
27
[SECTION.1.1]
28
number = 200
29
flag
30
'''
31
# parse the file in single-value mode
32
ini =
IniFile
()
33
ini.parse(fstr=ini_file)
34
if(ini.errors):
35
print(ini.display_errors())
36
37
# request an anonymous key, but none exist
38
key =
'non-existant'
39
values = ini.get_values(key, default=100)
40
print(
'request an anonymous key that does not exist'
)
41
print(key, end=
': '
)
42
print(values)
43
44
# default is not an ordinarily valid key value
45
print(
'\nrequest with default that is not an ordinarily valid value'
)
46
key =
'non-existant'
47
values = ini.get_values(key, default={
'invalid'
: 100})
48
print(key, end=
': '
)
49
print(values)
50
51
# a couple of valid values
52
section =
'SECTION.1'
53
key =
'number'
54
values = ini.get_section_values(section, key, default=300)
55
print(
'\nrequest a couple of valid section/key values'
)
56
print(section, end=
', '
)
57
print(key, end=
': '
)
58
print(values)
59
section =
'SECTION.1.1'
60
key =
'number'
61
values = ini.get_section_values(section, key, default=300)
62
print(section, end=
', '
)
63
print(key, end=
': '
)
64
print(values)
65
66
# section doen't exist
67
section =
'SECTION.2'
68
key =
'number'
69
values = ini.get_section_values(section, key, default=300)
70
print(
'\nsection doesn\'t exist'
)
71
print(section, end=
', '
)
72
print(key, end=
': '
)
73
print(values)
74
75
# key doen't exist
76
section =
'SECTION.1'
77
key =
'flag'
78
values = ini.get_section_values(section, key, default=
False
)
79
print(
'\nkey doesn\'t exist'
)
80
print(section, end=
', '
)
81
print(key, end=
': '
)
82
print(values)
python_ini.ini_file.IniFile
Definition:
ini_file.py:27
python_ini.ini_file
Definition:
ini_file.py:1
Generated by
1.9.1
Python APG, Version 1.0, is licensed under the
2-Clause BSD License
,
an
Open Source Initiative
Approved License.