Advertisement
Guest User

Untitled

a guest
Feb 28th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. [Section]
  2. bar=foo
  3. [Section 2]
  4. bar2= baz
  5.  
  6. [Section 3]
  7. barList={
  8. item1,
  9. item2
  10. }
  11.  
  12. [Section 3]
  13. barList=item1,item2
  14.  
  15. [Foo]
  16. fibs: [1,1,2,3,5,8,13]
  17.  
  18. >>> json.loads(config.get("Foo","fibs"))
  19. [1, 1, 2, 3, 5, 8, 13]
  20.  
  21. [Bar]
  22. files_to_check = [
  23. "/path/to/file1",
  24. "/path/to/file2",
  25. "/path/to/another file with space in the name"
  26. ]
  27.  
  28. [paths]
  29. path1 = /some/path/
  30. path2 = /another/path/
  31. ...
  32.  
  33. path_items = config.items( "paths" )
  34. for key, path in path_items:
  35. #do something with path
  36.  
  37. ;test.ini
  38. [hello]
  39. barlist =
  40. item1
  41. item2
  42.  
  43. "nitem1nitem2"
  44.  
  45. def aslist_cronly(value):
  46. if isinstance(value, string_types):
  47. value = filter(None, [x.strip() for x in value.splitlines()])
  48. return list(value)
  49.  
  50. def aslist(value, flatten=True):
  51. """ Return a list of strings, separating the input based on newlines
  52. and, if flatten=True (the default), also split on spaces within
  53. each line."""
  54. values = aslist_cronly(value)
  55. if not flatten:
  56. return values
  57. result = []
  58. for value in values:
  59. subvalues = value.split()
  60. result.extend(subvalues)
  61. return result
  62.  
  63. class MyConfigParser(ConfigParser):
  64. def getlist(self,section,option):
  65. value = self.get(section,option)
  66. return list(filter(None, (x.strip() for x in value.splitlines())))
  67.  
  68. def getlistint(self,section,option):
  69. return [int(x) for x in self.getlist(section,option)]
  70.  
  71. [global]
  72. spys = richard.sorge@cccp.gov, mata.hari@deutschland.gov
  73.  
  74. SPYS = [e.strip() for e in parser.get('global', 'spys').split(',')]
  75.  
  76. ['richard.sorge@cccp.gov', 'mata.hari@deutschland.gov']
  77.  
  78. ast.literal_eval()
  79.  
  80. [section]
  81. option=["item1","item2","item3"]
  82.  
  83. import ConfigParser
  84. import ast
  85.  
  86. my_list = ast.literal_eval(config.get("section", "option"))
  87. print(type(my_list))
  88. print(my_list)
  89.  
  90. <type'list'>
  91. ["item1","item2","item3"]
  92.  
  93. def get(self, section, option):
  94. """ Get a parameter
  95. if the returning value is a list, convert string value to a python list"""
  96. value = SafeConfigParser.get(self, section, option)
  97. if (value[0] == "[") and (value[-1] == "]"):
  98. return eval(value)
  99. else:
  100. return value
  101.  
  102. class Section
  103. bar = foo
  104. class Section2
  105. bar2 = baz
  106. class Section3
  107. barList=[ item1, item2 ]
  108.  
  109. [sect]
  110. alist = a
  111. b
  112. c
  113.  
  114. l = config.get('sect', 'alist').split('n')
  115.  
  116. nlist = 1
  117. 2
  118. 3
  119.  
  120. nl = config.get('sect', 'alist').split('n')
  121. l = [int(nl) for x in nl]
  122.  
  123. import ConfigParser
  124. import os
  125.  
  126. class Parser(object):
  127. """attributes may need additional manipulation"""
  128. def __init__(self, section):
  129. """section to retun all options on, formatted as an object
  130. transforms all comma-delimited options to lists
  131. comma-delimited lists with colons are transformed to dicts
  132. dicts will have values expressed as lists, no matter the length
  133. """
  134. c = ConfigParser.RawConfigParser()
  135. c.read(os.path.join(os.path.dirname(__file__), 'config.cfg'))
  136.  
  137. self.section_name = section
  138.  
  139. self.__dict__.update({k:v for k, v in c.items(section)})
  140.  
  141. #transform all ',' into lists, all ':' into dicts
  142. for key, value in self.__dict__.items():
  143. if value.find(':') > 0:
  144. #dict
  145. vals = value.split(',')
  146. dicts = [{k:v} for k, v in [d.split(':') for d in vals]]
  147. merged = {}
  148. for d in dicts:
  149. for k, v in d.items():
  150. merged.setdefault(k, []).append(v)
  151. self.__dict__[key] = merged
  152. elif value.find(',') > 0:
  153. #list
  154. self.__dict__[key] = value.split(',')
  155.  
  156. [server]
  157. credentials=username:admin,password:$3<r3t
  158. loggingdirs=/tmp/logs,~/logs,/var/lib/www/logs
  159. timeoutwait=15
  160.  
  161. >>> import config
  162. >>> my_server = config.Parser('server')
  163. >>> my_server.credentials
  164. {'username': ['admin'], 'password', ['$3<r3t']}
  165. >>> my_server.loggingdirs:
  166. ['/tmp/logs', '~/logs', '/var/lib/www/logs']
  167. >>> my_server.timeoutwait
  168. '15'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement