def test_iterators(self): pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] shuffle(pairs) od = OrderedDict(pairs) self.assertEqual(list(od), [t[0] for t in pairs]) self.assertEqual(od.keys()[:], [t[0] for t in pairs]) self.assertEqual(od.values()[:], [t[1] for t in pairs]) self.assertEqual(od.items()[:], pairs) self.assertEqual(list(od.iterkeys()), [t[0] for t in pairs]) self.assertEqual(list(od.itervalues()), [t[1] for t in pairs]) self.assertEqual(list(od.iteritems()), pairs) self.assertEqual(list(reversed(od)), [t[0] for t in reversed(pairs)])
def main(srcdir, outfn, templateloc, verbose=True): from jinja2 import Environment, FileSystemLoader if verbose: print_ = lambda *args, **kwargs: print(*args, **kwargs) else: print_ = lambda *args, **kwargs: None #Prepare the jinja2 templating environment env = Environment(loader=FileSystemLoader(templateloc)) def prefix(a_list, pre): return [pre+'{0}'.format(an_element) for an_element in a_list] def postfix(a_list, post): return ['{0}'.format(an_element)+post for an_element in a_list] def surround(a_list, pre, post): return [pre+'{0}'.format(an_element)+post for an_element in a_list] env.filters['prefix'] = prefix env.filters['postfix'] = postfix env.filters['surround'] = surround erfa_c_in = env.get_template('core.c.templ') erfa_py_in = env.get_template('core.py.templ') #Extract all the ERFA function names from erfa.h if os.path.isdir(srcdir): erfahfn = os.path.join(srcdir, 'erfa.h') multifilserc = True else: erfahfn = os.path.join(os.path.split(srcdir)[0], 'erfa.h') multifilserc = False with open(erfahfn, "r") as f: erfa_h = f.read() funcs = OrderedDict() section_subsection_functions = re.findall('/\* (\w*)/(\w*) \*/\n(.*?)\n\n', erfa_h, flags=re.DOTALL|re.MULTILINE) for section, subsection, functions in section_subsection_functions: print_("{0}.{1}".format(section, subsection)) if section == "Astronomy": func_names = re.findall(' (\w+)\(.*?\);', functions, flags=re.DOTALL) for name in func_names: print_("{0}.{1}.{2}...".format(section, subsection, name)) if multifilserc: # easy because it just looks in the file itself funcs[name] = Function(name, srcdir) else: # Have to tell it to look for a declaration matching # the start of the header declaration, otherwise it # might find a *call* of the function instead of the # definition for line in functions.split('\n'): if name in line: # [:-1] is to remove trailing semicolon, and # splitting on '(' is because the header and # C files don't necessarily have to match # argument names and line-breaking or # whitespace match_line = line[:-1].split('(')[0] funcs[name] = Function(name, srcdir, match_line) break else: raise ValueError("A name for a C file wasn't " "found in the string that " "spawned it. This should be " "impossible!") funcs = list(funcs.values()) #Extract all the ERFA constants from erfam.h erfamhfn = os.path.join(srcdir, 'erfam.h') with open(erfamhfn, 'r') as f: erfa_m_h = f.read() constants = [] for chunk in erfa_m_h.split("\n\n"): result = re.findall("#define (ERFA_\w+?) (.+?)$", chunk, flags=re.DOTALL|re.MULTILINE) if result: doc = re.findall("/\* (.+?) \*/\n", chunk, flags=re.DOTALL) for (name, value) in result: constants.append(Constant(name, value, doc)) print_("Rendering template") erfa_c = erfa_c_in.render(funcs=funcs) erfa_py = erfa_py_in.render(funcs=funcs, constants=constants) if outfn is not None: outfn_c = os.path.splitext(outfn)[0] + ".c" print_("Saving to", outfn, 'and', outfn_c) with open(outfn, "w") as f: f.write(erfa_py) with open(outfn_c, "w") as f: f.write(erfa_c) print_("Done!") return erfa_c, erfa_py, funcs
def main(srcdir, outfn, templateloc, verbose=True): from jinja2 import Environment, FileSystemLoader if verbose: print_ = lambda *args, **kwargs: print(*args, **kwargs) else: print_ = lambda *args, **kwargs: None #Prepare the jinja2 templating environment env = Environment(loader=FileSystemLoader(templateloc)) def prefix(a_list, pre): return [pre + '{0}'.format(an_element) for an_element in a_list] def postfix(a_list, post): return ['{0}'.format(an_element) + post for an_element in a_list] def surround(a_list, pre, post): return [pre + '{0}'.format(an_element) + post for an_element in a_list] env.filters['prefix'] = prefix env.filters['postfix'] = postfix env.filters['surround'] = surround erfa_c_in = env.get_template('core.c.templ') erfa_py_in = env.get_template('core.py.templ') #Extract all the ERFA function names from erfa.h if os.path.isdir(srcdir): erfahfn = os.path.join(srcdir, 'erfa.h') multifilserc = True else: erfahfn = os.path.join(os.path.split(srcdir)[0], 'erfa.h') multifilserc = False with open(erfahfn, "r") as f: erfa_h = f.read() funcs = OrderedDict() section_subsection_functions = re.findall('/\* (\w*)/(\w*) \*/\n(.*?)\n\n', erfa_h, flags=re.DOTALL | re.MULTILINE) for section, subsection, functions in section_subsection_functions: print_("{0}.{1}".format(section, subsection)) if section == "Astronomy": func_names = re.findall(' (\w+)\(.*?\);', functions, flags=re.DOTALL) for name in func_names: print_("{0}.{1}.{2}...".format(section, subsection, name)) if multifilserc: # easy because it just looks in the file itself funcs[name] = Function(name, srcdir) else: # Have to tell it to look for a declaration matching # the start of the header declaration, otherwise it # might find a *call* of the function instead of the # definition for line in functions.split('\n'): if name in line: # [:-1] is to remove trailing semicolon, and # splitting on '(' is because the header and # C files don't necessarily have to match # argument names and line-breaking or # whitespace match_line = line[:-1].split('(')[0] funcs[name] = Function(name, srcdir, match_line) break else: raise ValueError("A name for a C file wasn't " "found in the string that " "spawned it. This should be " "impossible!") funcs = list(funcs.values()) #Extract all the ERFA constants from erfam.h erfamhfn = os.path.join(srcdir, 'erfam.h') with open(erfamhfn, 'r') as f: erfa_m_h = f.read() constants = [] for chunk in erfa_m_h.split("\n\n"): result = re.findall("#define (ERFA_\w+?) (.+?)$", chunk, flags=re.DOTALL | re.MULTILINE) if result: doc = re.findall("/\* (.+?) \*/\n", chunk, flags=re.DOTALL) for (name, value) in result: constants.append(Constant(name, value, doc)) print_("Rendering template") erfa_c = erfa_c_in.render(funcs=funcs) erfa_py = erfa_py_in.render(funcs=funcs, constants=constants) if outfn is not None: outfn_c = os.path.splitext(outfn)[0] + ".c" print_("Saving to", outfn, 'and', outfn_c) with open(outfn, "w") as f: f.write(erfa_py) with open(outfn_c, "w") as f: f.write(erfa_c) print_("Done!") return erfa_c, erfa_py, funcs