Skip to content

Instantly share code, notes, and snippets.

@adamv
Created November 11, 2010 17:49
Show Gist options
  • Select an option

  • Save adamv/672884 to your computer and use it in GitHub Desktop.

Select an option

Save adamv/672884 to your computer and use it in GitHub Desktop.
Find a setting/variable assignment-looking-thing.
def find_setting(source, setting, unquote=True):
re_setting = r'^\s*' + re.escape(setting) + r'\s*=\s*(.+)$'
m = re.search(re_setting, source, re.M)
if not m:
return None
value = m.group(1).strip()
if unquote:
if value.startswith("'") and value.endswith("'") or \
value.startswith('"') and value.endswith('"'):
value = value[1:-1]
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment