Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97555239
config.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Jan 5, 05:52
Size
2 KB
Mime Type
text/x-python
Expires
Tue, Jan 7, 05:52 (2 d)
Engine
blob
Format
Raw Data
Handle
23425661
Attached To
rSTICAZZI yearII_reports
config.py
View Options
from
decimal
import
Decimal
def
parseInt
(
x
):
value
=
None
try
:
value
=
int
(
x
)
except
:
value
=
None
return
value
def
parseDecimal
(
x
):
value
=
None
try
:
value
=
Decimal
(
x
)
except
:
value
=
None
return
value
# A config parseable class must implement:
# __str__(self)
# __load__(s), not a class method
class
ConfigEntry
:
def
__init__
(
self
,
key
,
value
):
self
.
key
=
key
self
.
value
=
value
def
toString
(
self
,
delim
=
'='
):
return
"
%s
%s
%s
"
%
(
self
.
key
,
delim
,
str
(
self
.
value
))
def
load
(
line
,
sep
,
typeset
):
v
=
line
.
replace
(
' '
,
''
)
.
split
(
sep
)
if
len
(
v
)
!=
2
:
print
(
v
)
return
None
value
=
parseInt
(
v
[
1
])
if
value
is
None
:
value
=
parseDecimal
(
v
[
1
])
if
value
is
None
:
for
T
in
typeset
:
loader
=
getattr
(
T
,
'__load__'
,
None
)
if
loader
is
not
None
and
callable
(
loader
):
value
=
loader
(
v
[
1
])
if
value
is
None
:
value
=
v
[
1
]
#string
return
ConfigEntry
(
v
[
0
],
value
)
class
Config
:
def
__init__
(
self
,
sep
=
'='
,
com
=
'%'
,
initmap
=
[]):
self
.
map
=
initmap
self
.
sep
=
sep
self
.
com
=
com
def
load
(
self
,
filename
,
typeset
=
[]):
self
.
map
=
[]
reader
=
open
(
filename
,
'r'
)
for
line
in
reader
:
if
line
:
#eof
entry
=
None
if
line
.
startswith
(
self
.
com
):
entry
=
line
[
1
:
-
1
]
else
:
entry
=
ConfigEntry
.
load
(
line
[:
-
1
],
self
.
sep
,
typeset
)
if
entry
is
not
None
:
self
.
map
.
append
(
entry
)
else
:
print
(
"Line
\"
"
,
line
[:
-
1
],
"
\"
format not valid, skipping it"
)
reader
.
close
()
def
save
(
self
,
filename
):
writer
=
open
(
filename
,
'w'
)
writer
.
write
(
str
(
self
))
writer
.
close
()
def
__str__
(
self
):
out
=
""
for
entry
in
self
.
map
:
if
isinstance
(
entry
,
str
):
out
+=
(
"
%s%s
\n
"
%
(
self
.
com
,
entry
))
elif
isinstance
(
entry
,
ConfigEntry
):
out
+=
(
"
%s
\n
"
%
entry
.
toString
(
self
.
sep
))
return
out
def
__getitem__
(
self
,
key
):
for
entry
in
self
.
map
:
if
isinstance
(
entry
,
ConfigEntry
)
and
entry
.
key
==
key
:
return
entry
.
value
def
__setitem__
(
self
,
key
,
value
):
for
entry
in
self
.
map
:
if
isinstance
(
entry
,
ConfigEntry
)
and
entry
.
key
==
key
:
entry
.
value
=
value
Event Timeline
Log In to Comment