Created
December 19, 2018 03:01
-
-
Save mulderu/7e44e4fe154841bebf93ce640a81b5d7 to your computer and use it in GitHub Desktop.
python utility sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loadLines(path, rmSharp=False): | |
""" line by line reader : output : array """ | |
fp = open(path, "r") | |
alls = fp.read().split("\n")[:-1] | |
if (rmSharp): | |
lines = [] | |
for line in alls: | |
line = line.strip() | |
if line.startswith('#'): | |
continue | |
else: | |
lines.append(line) | |
return lines | |
else: | |
return alls | |
def loadOptionLines(path): | |
""" line by line reader : output : array """ | |
fp = open(path, "r") | |
alls = fp.read().split("\n")[:-1] | |
lines = [] | |
for line in alls: | |
line = line.strip() | |
if line == '' or line.startswith('#'): | |
continue | |
else: | |
lines.append(line) | |
return lines | |
def load_options(path): | |
""" line by line config : name=value pair """ | |
options = dict() | |
with open(path, 'r') as fp: | |
lines = fp.readlines() | |
for line in lines: | |
line = line.strip() | |
if line == '' or line.startswith('#'): | |
continue | |
key, value = line.split('=') | |
options[key.strip()] = value.strip() | |
return options | |
def parse_info_config(path): | |
""" Parses the header and name value pairs """ | |
file = open(path, 'r') | |
lines = file.read().split('\n') | |
lines = [x for x in lines if x and not x.startswith('#')] | |
lines = [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespaces | |
module_defs = [] | |
for line in lines: | |
if line.startswith('['): # This marks the start of a new block | |
module_defs.append({}) | |
module_defs[-1]['type'] = line[1:-1].rstrip() | |
else: | |
key, value = line.split("=") | |
value = value.strip() | |
module_defs[-1][key.rstrip()] = value.strip() | |
return module_defs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment