def parse_chunks(self, classes=['python'], language='python'): """Parse `self.chunks` with attributes matching `classes`. Parameters ========== classes : string or list of strings Classes to match against those in `self.chunks` language : string Programming language of code blocks to be parsed See also ======== FSIC.parser.chunk.parse() FSIC.parser.code.translate() FSIC.parser.ini.read_string() """ # Remove duplicates chunks = list(sorted(list(set(self.chunks)))) # Parse chunks to extract metadata from FSIC.parser.chunk import parse blocks = [parse(c) for c in chunks] # Filter by classes if type(classes) is not list: classes = [classes] blocks = [b for b in blocks if len(set(b['classes']) & set(classes))] # Extract code code_blocks = [b['code'] for b in blocks] code = '\n'.join(code_blocks) # Parse if language == 'python': from FSIC.parser.code import translate code = translate(code) elif language == 'ini': from FSIC.parser.ini import read_string code = read_string(code) else: raise ValueError( 'Unrecognised language argument \'%s\'' % (language)) # Return return code
def parse_chunks(self, classes=['python'], language='python'): """Parse `self.chunks` with attributes matching `classes`. Parameters ========== classes : string or list of strings Classes to match against those in `self.chunks` language : string Programming language of code blocks to be parsed See also ======== FSIC.parser.chunk.parse() FSIC.parser.code.translate() FSIC.parser.ini.read_string() """ # Remove duplicates chunks = list(sorted(list(set(self.chunks)))) # Parse chunks to extract metadata from FSIC.parser.chunk import parse blocks = [parse(c) for c in chunks] # Filter by classes if type(classes) is not list: classes = [classes] blocks = [b for b in blocks if len(set(b['classes']) & set(classes))] # Extract code code_blocks = [b['code'] for b in blocks] code = '\n'.join(code_blocks) # Parse if language == 'python': from FSIC.parser.code import translate code = translate(code) elif language == 'ini': from FSIC.parser.ini import read_string code = read_string(code) else: raise ValueError('Unrecognised language argument \'%s\'' % (language)) # Return return code
def update_data(self, data): """Store the contents of `data`. Parameters ========== data : pandas DataFrame Data to store; one column per variable, with name matching that of the intended model variable Returns ======= N/A Notes ===== The index of the DataFrame must match the index of the model variables. It is not necessary for all periods in the model to be covered by the DataFrame index. See also ======== FSIC.parser.code.translate() """ # Generate a set of statements for execution from FSIC.parser.code import translate expression = [] for c in data.columns: # Translate the statement into one compatible with the model class e = ''.join([ translate(c, period='') + '[data.index]', ' = data[\'', c, '\']']) e = e.replace('[]', '') expression.append(e) # Combine into a single string and call expression = '\n'.join(expression) exec(expression)
def update_data(self, data): """Store the contents of `data`. Parameters ========== data : pandas DataFrame Data to store; one column per variable, with name matching that of the intended model variable Returns ======= N/A Notes ===== The index of the DataFrame must match the index of the model variables. It is not necessary for all periods in the model to be covered by the DataFrame index. See also ======== FSIC.parser.code.translate() """ # Generate a set of statements for execution from FSIC.parser.code import translate expression = [] for c in data.columns: # Translate the statement into one compatible with the model class e = "".join([translate(c, period="") + "[data.index]", " = data['", c, "']"]) e = e.replace("[]", "") expression.append(e) # Combine into a single string and call expression = "\n".join(expression) exec(expression)