Skip to content

Instantly share code, notes, and snippets.

@cgibson
Created May 27, 2011 18:26
Show Gist options
  • Save cgibson/995851 to your computer and use it in GitHub Desktop.
Save cgibson/995851 to your computer and use it in GitHub Desktop.
Property loading function in python
# Prop file load
# Based off of http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch34s04.html
def load_prop(filename):
try:
f = file( filename, 'rU' )
except:
print "Error: no such property file %s" % filename
sys.exit()
d = dict()
for propline in f:
pdef = propline.strip()
if len(pdef) == 0:
continue
if pdef[0] in ( '!', '#' ):
continue
punctuation = [ pdef.find(c) for c in ':= ' ] + [ len(pdef) ]
found = min( [ pos for pos in punctuation if pos != -1 ] )
name = pdef[:found].rstrip()
value = pdef[found:].lstrip(":= ").rstrip()
d[name] = value
f.close()
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment