Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hidra
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
steinbac
hidra
Commits
0046f7f2
Commit
0046f7f2
authored
9 years ago
by
Manuela Kuhn
Browse files
Options
Downloads
Patches
Plain Diff
Added test for configParster
parent
92acd3fb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
test/configParser/example.cfg
+7
-0
7 additions, 0 deletions
test/configParser/example.cfg
test/configParser/readConfig.py
+32
-0
32 additions, 0 deletions
test/configParser/readConfig.py
test/configParser/writeConfig.py
+22
-0
22 additions, 0 deletions
test/configParser/writeConfig.py
with
61 additions
and
0 deletions
test/configParser/example.cfg
0 → 100644
+
7
−
0
View file @
0046f7f2
an_int
=
15
a_bool
=
true
a_float
=
3.1415
baz
=
fun
bar
=
Python
foo
=
%(bar)s is %(baz)s!
This diff is collapsed.
Click to expand it.
test/configParser/readConfig.py
0 → 100644
+
32
−
0
View file @
0046f7f2
import
ConfigParser
# is needed because configParser always needs a section name
# the used config file consists of key-value pairs only
# source: http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788
class
FakeSecHead
(
object
):
def
__init__
(
self
,
fp
):
self
.
fp
=
fp
self
.
sechead
=
'
[asection]
\n
'
def
readline
(
self
):
if
self
.
sechead
:
try
:
return
self
.
sechead
finally
:
self
.
sechead
=
None
else
:
return
self
.
fp
.
readline
()
config
=
ConfigParser
.
ConfigParser
()
config
.
readfp
(
FakeSecHead
(
open
(
'
example.cfg
'
)))
# Set the third, optional argument of get to 1 if you wish to use raw mode.
print
config
.
get
(
'
asection
'
,
'
foo
'
,
0
)
# -> "Python is fun!"
print
config
.
get
(
'
asection
'
,
'
foo
'
,
1
)
# -> "%(bar)s is %(baz)s!"
# The optional fourth argument is a dict with members that will take
# precedence in interpolation.
print
config
.
get
(
'
asection
'
,
'
foo
'
,
0
,
{
'
bar
'
:
'
Documentation
'
,
'
baz
'
:
'
evil
'
})
This diff is collapsed.
Click to expand it.
test/configParser/writeConfig.py
0 → 100644
+
22
−
0
View file @
0046f7f2
import
ConfigParser
config
=
ConfigParser
.
RawConfigParser
()
# When adding sections or items, add them in the reverse order of
# how you want them to be displayed in the actual file.
# In addition, please note that using RawConfigParser's and the raw
# mode of ConfigParser's respective set functions, you can assign
# non-string values to keys internally, but will receive an error
# when attempting to write to a file or when you get it in non-raw
# mode. SafeConfigParser does not allow such assignments to take place.
config
.
add_section
(
'
Section1
'
)
config
.
set
(
'
Section1
'
,
'
an_int
'
,
'
15
'
)
config
.
set
(
'
Section1
'
,
'
a_bool
'
,
'
true
'
)
config
.
set
(
'
Section1
'
,
'
a_float
'
,
'
3.1415
'
)
config
.
set
(
'
Section1
'
,
'
baz
'
,
'
fun
'
)
config
.
set
(
'
Section1
'
,
'
bar
'
,
'
Python
'
)
config
.
set
(
'
Section1
'
,
'
foo
'
,
'
%(bar)s is %(baz)s!
'
)
# Writing our configuration file to 'example.cfg'
with
open
(
'
example.cfg
'
,
'
wb
'
)
as
configfile
:
config
.
write
(
configfile
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment