3.141.202.187 |    

Navigation

Google Advertisement

Diese Funktion liest eine CFG Datei aus und wandelt die Keys/Value Paare in die entsprechenden Python Daten Typen um.\\\\ Beispiel einer CFG Datei
; Home directroy
home_directory = /home/mschulz

# Beispiel einer Tuple (auch das ist ein Kommentar)
tuple = ( 't1', 't2', [ 'l1', 'l2', 3 ], 4 )

# Alter
age = 36

# Gewicht
weight = 87.3

# Maennlich
male = True

# Beispiel einer Liste
list = [ 'd1', 'd2', ( 't1', 't2', 3 ), 4 ]

; Beispiel eines Dictionarys
dict = { 1: 'Berlin', 2: 'Frankfurt (Oder)', 3: 'Dresden', 4: [ 'l1', 'l2', 3 ] }


config
  1. def config( file ):
  2.     """config --- Read a config file in cfg format.
  3.  
  4.     (dict) dict config( (string) file )"""
  5.  
  6.     import re
  7.  
  8.     """_checktype --- Help function to get the right data type.
  9.  
  10.     (bool) bool _checktype( (string) Type, (string) String )"""
  11.     def _checktype( Type, String ):
  12.         if Type == 'int':
  13.             regexp = re.compile('^\d+$')
  14.         elif Type == 'float':
  15.             regexp = re.compile('^\d+\.\d+$')
  16.         elif Type == 'bool':
  17.             regexp = re.compile('^(True|False)$')
  18.         elif Type == 'tuple':
  19.             regexp = re.compile('^\(.*\)$')
  20.         elif Type == 'dict':
  21.             regexp = re.compile('^\{.*\}$')
  22.         elif Type == 'list':
  23.             regexp = re.compile('^\[.*\]$')
  24.  
  25.         if regexp.match( str(String) ) != None:
  26.             return True
  27.         else:
  28.             return False
  29.  
  30.     conf = {} # empty dictionary
  31.  
  32.     # Open the config file and loop all lines.
  33.     fh = open( file )
  34.     for line in fh:
  35.  
  36.         # Remove spaces from the begin and end of the line.
  37.         line = line.strip()
  38.  
  39.         # Ignore comment lines.
  40.         if not line or line.startswith('#') or line.startswith(';'):
  41.             continue
  42.  
  43.         # Define the regexp object to split keys(1)/value(2) pairs. 
  44.         match = re.match( r'(.*?)\s*=\s*(.*)$', line )
  45.         assert match, "Syntax error in following line: %s" % line
  46.  
  47.         # String is the default data type.
  48.         value = str(match.group(2))
  49.  
  50.         # Convert the value into the right data type.
  51.         if _checktype( 'int', value ):
  52.             value = int(eval(value))   # integer found
  53.         if _checktype( 'float', value ):
  54.             value = float(eval(value)) # float found
  55.         if _checktype( 'tuple', value ):
  56.             value = tuple(eval(value)) # tuple found
  57.         if _checktype( 'dict', value ):
  58.             value = dict(eval(value))  # dictionary found
  59.         if _checktype( 'list', value ):
  60.             value = list(eval(value))  # list found
  61.         if _checktype( 'bool', value ):
  62.             value = bool(eval(value))  # bolean found
  63.  
  64.         # Save Keys/Value pairs into dictionary 'conf'.
  65.         conf[match.group(1)] = value
  66.  
  67.     # Close file and return the dictionary.
  68.     fh.close()
  69.     return conf
Parsed in 0.002 seconds at 970.35 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 1 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)