Пример #1
0
    def create_js_file(self, substitutions, cb_before_creation=None):
        """
        Creates a js file from a template

        Parameters
        ----------
        name : string
        The name of the javascript file without the '.js' extension

        substitutions : dict
        A dict of the substitutions to make in the template
        file in order to produce the final js

        cb_before_creation : function
        (Optional) A callback function to be called right before substitution.
        It should accept the following arguments:
        (test_framework_object, name_of_js_file, substitutions_dict)
        and it returns the edited substitutions map
        """
        name = self.running_scenario()
        print("Creating {}.js".format(name))
        scenario_dir = os.path.join(self.tests_dir, "scenarios", name)
        with open(os.path.join(scenario_dir, 'template.js'), 'r') as f:
            data = f.read()
        tmpl = Template(data)
        if cb_before_creation:
            substitutions = cb_before_creation(self, name, substitutions)
        s = tmpl.substitute(substitutions)
        write_js("{}.js".format(name), s, len(self.accounts))
Пример #2
0
    def create_js_file(self, substitutions):
        """
        Creates a js file from a template

        Parameters
        ----------
        name : string
        The name of the javascript file without the '.js' extension

        substitutions : dict
        A dict of the substitutions to make in the template
        file in order to produce the final js

        cb_before_creation : function
        (Optional) A callback function to be called right before substitution.
        It should accept the following arguments:
        (test_framework_object, name_of_js_file, substitutions_dict)
        and it returns the edited substitutions map
        """
        name = self.running_scenario()
        print("Creating {}.js".format(name))
        scenario_dir = os.path.join(self.tests_dir, "scenarios", name)
        with open(
                os.path.join(scenario_dir, 'template.js'),
                'r'
        ) as f:
            data = f.read()
        tmpl = Template(data)
	#print substitutions
        s = tmpl.substitute(substitutions)
        write_js("{}.js".format(name), s, len(self.accounts))
Пример #3
0
    def create_js_file(
            self,
            substitutions,
            specific_name=None,
            cb_before_creation=None
    ):
        """
        Creates a js file from a template called template.js found in the same
        directory as the scenario. Alternatively if specific_name is given then
        it creates a js file from that template.

        Parameters
        ----------
        substitutions : dict
        A dict of the substitutions to make in the template
        file in order to produce the final js

        specific_name : string
        (Optional) If given then the generic template.js file is not chosen but
        instead a file of the specific given name ending with _template.js is
        used.

        cb_before_creation : function
        (Optional) A callback function to be called right before substitution.
        It should accept the following arguments:
        (test_framework_object, name_of_js_file, substitutions_dict)
        and it returns the edited substitutions map
        """
        scenario_name = self.running_scenario()
        name = specific_name if specific_name else self.running_scenario()
        scenario_dir = os.path.join(self.tests_dir, "scenarios", scenario_name)
        fullpath = os.path.join(
            scenario_dir,
            name + '_template.js' if specific_name else 'template.js'
        )
        print("Creating {}.js".format(name))
        with open(fullpath, 'r') as f:
            data = f.read()
        tmpl = Template(data)
        if cb_before_creation:
            substitutions = cb_before_creation(self, name, substitutions)
        s = tmpl.substitute(substitutions)
        write_js("{}.js".format(name), s, len(self.accounts))
Пример #4
0
                k = "IMPACT"
            elif any(s in f for s in chemical):
                k = "CHEMICAL"
            elif any(s in f for s in weapon):
                k = "WEAPON"
            elif any(s in f for s in electric):
                k = "ELECTRIC"
            elif any(s in f for s in canine):
                print("WOOP")
                k = "CANINE"
            else:
                pass
                # print("No category for", f)

            if k in mapped[label]:
                mapped[label][k] += 1
            else:
                mapped[label][k] = 1

    # HACK: re-structuring for Highcharts.
    keys = mapped["OTHER"].keys()
    by_response = {k: [] for k in keys}
    for k in keys:
        for label in mapped:
            if k in mapped[label]:
                by_response[k].append(mapped[label][k])
            else:
                by_response[k].append(0)

    write_js("uof", [("by_response", by_response)])
Пример #5
0
        except KeyError as e:
            # FIXME: Add manually?
            print("Skipping", query)

    # Calculate the per 1 million averages:
    for state, pops in STATE_POPS.items():
        for year, total in totals_by_year[state].items():
            # If we don't have data on the current year yet, fall back to last
            # year.
            pop = pops.get(year, pops[str(TODAY.year - 1)])
            totals_by_year[state][year] = str(round(total / pop, 3))

    # Calculate the per 1 million averages by race:
    for race, pop in RACE_POPS.items():
        if race in RACE_POPS:
            total = RACE_POPS[race]
            totals_by_race[race] = round(totals_by_race[race] / total, 3)

    # Required for HighCharts.
    cats = ["Asian", "Black", "Hispanic", "Native American", "White"]
    for state, history in by_weapon_race.items():
        for weapon in history:
            by_weapon_race[state][weapon] = [history[weapon][k] for k in cats]

    write_js(
        "ois",
        [("by_weapon_race", by_weapon_race), ("by_race", by_state_race),
         ("by_county", by_fips), ("by_1_mil", totals_by_year),
         ("by_1_mil_race", totals_by_race)],
    )