Guest User

Untitled

a guest
Nov 27th, 2017
371
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. ast.literal_eval()
  72.  
  73. [section]
  74. option=["item1","item2","item3"]
  75.  
  76. import ConfigParser
  77. import ast
  78.  
  79. my_list = ast.literal_eval(config.get("section", "option"))
  80. print(type(my_list))
  81. print(my_list)
  82.  
  83. <type'list'>
  84. ["item1","item2","item3"]
  85.  
  86. [global]
  87. spys = richard.sorge@cccp.gov, mata.hari@deutschland.gov
  88.  
  89. SPYS = [e.strip() for e in parser.get('global', 'spys').split(',')]
  90.  
  91. ['richard.sorge@cccp.gov', 'mata.hari@deutschland.gov']
  92.  
  93. [sect]
  94. alist = a
  95. b
  96. c
  97.  
  98. l = config.get('sect', 'alist').split('n')
  99.  
  100. nlist = 1
  101. 2
  102. 3
  103.  
  104. nl = config.get('sect', 'alist').split('n')
  105. l = [int(nl) for x in nl]
  106.  
  107. def get(self, section, option):
  108. """ Get a parameter
  109. if the returning value is a list, convert string value to a python list"""
  110. value = SafeConfigParser.get(self, section, option)
  111. if (value[0] == "[") and (value[-1] == "]"):
  112. return eval(value)
  113. else:
  114. return value
  115.  
  116. class Section
  117. bar = foo
  118. class Section2
  119. bar2 = baz
  120. class Section3
  121. barList=[ item1, item2 ]
  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'
Add Comment
Please, Sign In to add comment