Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Section]
- bar=foo
- [Section 2]
- bar2= baz
- [Section 3]
- barList={
- item1,
- item2
- }
- [Section 3]
- barList=item1,item2
- [Foo]
- fibs: [1,1,2,3,5,8,13]
- >>> json.loads(config.get("Foo","fibs"))
- [1, 1, 2, 3, 5, 8, 13]
- [Bar]
- files_to_check = [
- "/path/to/file1",
- "/path/to/file2",
- "/path/to/another file with space in the name"
- ]
- [paths]
- path1 = /some/path/
- path2 = /another/path/
- ...
- path_items = config.items( "paths" )
- for key, path in path_items:
- #do something with path
- ;test.ini
- [hello]
- barlist =
- item1
- item2
- "nitem1nitem2"
- def aslist_cronly(value):
- if isinstance(value, string_types):
- value = filter(None, [x.strip() for x in value.splitlines()])
- return list(value)
- def aslist(value, flatten=True):
- """ Return a list of strings, separating the input based on newlines
- and, if flatten=True (the default), also split on spaces within
- each line."""
- values = aslist_cronly(value)
- if not flatten:
- return values
- result = []
- for value in values:
- subvalues = value.split()
- result.extend(subvalues)
- return result
- class MyConfigParser(ConfigParser):
- def getlist(self,section,option):
- value = self.get(section,option)
- return list(filter(None, (x.strip() for x in value.splitlines())))
- def getlistint(self,section,option):
- return [int(x) for x in self.getlist(section,option)]
- [global]
- spys = richard.sorge@cccp.gov, mata.hari@deutschland.gov
- SPYS = [e.strip() for e in parser.get('global', 'spys').split(',')]
- ['richard.sorge@cccp.gov', 'mata.hari@deutschland.gov']
- ast.literal_eval()
- [section]
- option=["item1","item2","item3"]
- import ConfigParser
- import ast
- my_list = ast.literal_eval(config.get("section", "option"))
- print(type(my_list))
- print(my_list)
- <type'list'>
- ["item1","item2","item3"]
- def get(self, section, option):
- """ Get a parameter
- if the returning value is a list, convert string value to a python list"""
- value = SafeConfigParser.get(self, section, option)
- if (value[0] == "[") and (value[-1] == "]"):
- return eval(value)
- else:
- return value
- class Section
- bar = foo
- class Section2
- bar2 = baz
- class Section3
- barList=[ item1, item2 ]
- [sect]
- alist = a
- b
- c
- l = config.get('sect', 'alist').split('n')
- nlist = 1
- 2
- 3
- nl = config.get('sect', 'alist').split('n')
- l = [int(nl) for x in nl]
- import ConfigParser
- import os
- class Parser(object):
- """attributes may need additional manipulation"""
- def __init__(self, section):
- """section to retun all options on, formatted as an object
- transforms all comma-delimited options to lists
- comma-delimited lists with colons are transformed to dicts
- dicts will have values expressed as lists, no matter the length
- """
- c = ConfigParser.RawConfigParser()
- c.read(os.path.join(os.path.dirname(__file__), 'config.cfg'))
- self.section_name = section
- self.__dict__.update({k:v for k, v in c.items(section)})
- #transform all ',' into lists, all ':' into dicts
- for key, value in self.__dict__.items():
- if value.find(':') > 0:
- #dict
- vals = value.split(',')
- dicts = [{k:v} for k, v in [d.split(':') for d in vals]]
- merged = {}
- for d in dicts:
- for k, v in d.items():
- merged.setdefault(k, []).append(v)
- self.__dict__[key] = merged
- elif value.find(',') > 0:
- #list
- self.__dict__[key] = value.split(',')
- [server]
- credentials=username:admin,password:$3<r3t
- loggingdirs=/tmp/logs,~/logs,/var/lib/www/logs
- timeoutwait=15
- >>> import config
- >>> my_server = config.Parser('server')
- >>> my_server.credentials
- {'username': ['admin'], 'password', ['$3<r3t']}
- >>> my_server.loggingdirs:
- ['/tmp/logs', '~/logs', '/var/lib/www/logs']
- >>> my_server.timeoutwait
- '15'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement