Skip to content

Instantly share code, notes, and snippets.

@mbakeranalecta
Created January 27, 2015 21:25
Show Gist options
  • Save mbakeranalecta/4e9a9c6cfc681ea6661a to your computer and use it in GitHub Desktop.
Save mbakeranalecta/4e9a9c6cfc681ea6661a to your computer and use it in GitHub Desktop.
Resolving variables in a text string in Python
self.defines = {'HOME': self.spfe_dirs['home'],
'SPFEOT_HOME': self.spfe_dirs['spfe_ot_home'],
'CONTENT_SET_BUILD_DIR': self.content_set_build_dir,
'CONTENT_SET_OUTPUT_DIR': self.content_set_output_dir,
'CONTENT_SET_BUILD_ROOT_DIR': self.content_set_build_root_dir
}
def _resolve_defines(self, string):
defines_pattern = re.compile('\$\{([^}]*)\}')
resolved = re.sub(defines_pattern, self._replace_defines, string)
return resolved
def _replace_defines(self, match):
try:
return self.defines[match.group(1)]
except KeyError:
Exception("Invalid define ${" + match.group(1) + "}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment