Esempio n. 1
0
    def percent(self, **kwargs):
        """str: 2 columns divided by one another """

        attr_type = kwargs.get("attr_type", self.attr_type)
        attr_id = kwargs.get("attr_id", self.attr["id"])

        r = {"num": 1, "den": 1}
        for t in r.keys():
            key = kwargs.get(t)

            params = {}
            params["limit"] = 1
            params["year"] = kwargs.get("year", "latest")
            params = param_format(params)
            t_type = kwargs.get("{}_type".format(t), attr_type)
            params["show"] = kwargs.get("show", t_type)
            params[t_type] = kwargs.get("{}_id".format(t), attr_id)
            params["exclude"] = kwargs.get("exclude", kwargs.get("{}_exclude".format(t), ""))

            if "top:" in key:

                params["col"], params["force"] = key.split(":")[1].split(",")
                r["{}_key".format(t)] = params["col"]
                r[t] = self.top(**params)["data"][0]

            elif "var:" in key:

                keys = key.split(":")[1].split(",")
                if len(keys) == 2:
                    keys.append(None)
                ns, col, row = keys
                r["{}_key".format(t)] = col
                r[t] = self.var(namespace=ns, key=col, row=row, format="raw")

            elif "," in key:

                num, den = key.split(",")
                subparams = {}
                subparams["num"] = num
                subparams["den"] = den
                subparams["data_only"] = True
                subparams["num_id"] = params[t_type]
                subparams["den_id"] = params[t_type]
                r["{}_key".format(t)] = None
                r[t] = self.percent(**subparams)

            else:

                params["required"] = key
                r["{}_key".format(t)] = key

                # convert request arguments into a url query string
                query = RequestEncodingMixin._encode_params(params)
                url = u"{}/api?{}".format(API, query)

                try:
                    r[t] = datafold(requests.get(url).json())
                except ValueError:
                    app.logger.info("STAT ERROR: {}".format(url))
                    return "N/A"

                if len(r[t]) == 0:
                    return "N/A"
                r[t] = r[t][0][key]

            if r[t] in [None, "N/A"]:
                return "N/A"

        diff = kwargs.get("diff", False)
        text = kwargs.get("text", False)
        if text and text in TEXTCOMPARATORS:
            r["num"] = float(num_format(r["num"], r["num_key"], False, suffix=False).replace(",", ""))
            r["den"] = float(num_format(r["den"], r["den_key"], False, suffix=False).replace(",", ""))

        if r["num"] == 0 or r["den"] == 0:
            val = 0
        elif diff:
            val = r["num"] - r["den"]
        else:
            val = float(r["num"])/float(r["den"])

        if kwargs.get("invert", False):
            val = 1 - val

        if kwargs.get("data_only", False):
            return val

        if text and text in TEXTCOMPARATORS:
            text = TEXTCOMPARATORS[text]
            if diff:
                if val > 0:
                    return text[0]
                elif val < 0:
                    return text[1]
                else:
                    return text[2]
            else:
                if val > 1:
                    return text[0]
                elif val < 1:
                    return text[1]
                else:
                    return text[2]
        elif diff or kwargs.get("ratio", False):
            return num_format(abs(val))
        else:
            return "{}%".format(num_format(val * 100))
Esempio n. 2
0
    def top(self, **kwargs):
        """str: A text representation of a top statistic or list of statistics """

        attr_type = kwargs.get("attr_type", self.attr_type)
        dataset = kwargs.get("dataset", False)
        moe = kwargs.pop("moe", False)
        truncate = int(kwargs.pop("truncate", 0))

        # create a params dict to use in the URL request
        params = {}

        # set the section's attribute ID in params
        attr_id = kwargs.get("attr_id", False)
        if attr_type != self.attr_type and attr_id and "top" in attr_id:
            l, o = attr_id.split(":")
            attr_id = self.top(**{
                "col": "id",
                "order": o,
                "sort": "desc",
                "limit": l[3:],
                "show": attr_type
            })["value"]

        child = kwargs.get("child", False)
        if attr_id == False:
            if child:
                kwargs["prefix"] = kwargs.get("prefix", True)
                where = self.children(**kwargs)
                if kwargs["prefix"] is True:
                    pre = kwargs.pop("prefix")
                if len(where) > 0:
                    params["where"] = "{}:{}".format(self.attr_type, where)
                    attr_id = ""
        if attr_id == False:
            attr_id = self.id(**kwargs)
        params[attr_type] = attr_id

        # get output key from either the value in kwargs (while removing it) or 'name'
        col = kwargs.pop("col", "name")
        data_only = kwargs.pop("data_only", False)
        if child:
            kwargs["sumlevel"] = self.sumlevel(**kwargs)

        for k in ["attr_type", "attr_id", "child", "dataset"]:
            if k in kwargs:
                del kwargs[k]

        # add the remaining kwargs into the params dict
        params = dict(params.items()+kwargs.items())

        # set default params
        params["year"] = params.get("year", "latest")
        params["limit"] = params.get("limit", 1)
        params["show"] = params.get("show", attr_type)
        params["sumlevel"] = params.get("sumlevel", "all")
        if "sumlevel" in params["sumlevel"]:
            params["sumlevel"] = params["sumlevel"].replace("sumlevel", self.sumlevel())
        if "naics_level" in params and "sumlevel" in params["naics_level"]:
            params["naics_level"] = params["naics_level"].replace("sumlevel", self.sumlevel())
        if "soc_level" in params and "sumlevel" in params["soc_level"]:
            params["soc_level"] = params["soc_level"].replace("sumlevel", self.sumlevel())
        params = param_format(params)

        if "force" not in params and params["required"] == "":
            col_maps = COLMAP.keys()
            col_maps += ["-".join(c) for c in list(combinations(col_maps, 2))]

            # extra allowed values for 'col'
            col_maps += ["id", "name", "ratio"]

            if col not in col_maps:
                params["required"] = col
            elif "order" in params:
                params["required"] = params["order"]

        if moe and "force" not in params:
            params["required"] += ",{}".format(moe)

        # make the API request using the params
        return stat(params, col=col, dataset=dataset, data_only=data_only, moe=moe, truncate=truncate)
Esempio n. 3
0
    def __init__(self, params, highlight=False, profile=False, select=False, slug=False):
        """Initializes a new Viz class.

        Args:
            config (str): The YAML configuration file as one long string.
            profile (Profile): The Profile class instance this Section will be a part of.

        """

        self.highlight = params.pop("highlight", highlight)
        self.profile = profile.attr
        self.select = select
        self.profile_type = profile.attr_type
        self.className = params.pop("class", False)
        self.slug = slug

        # force the data of params into a list
        data = params.pop("data") if isinstance(params["data"], list) else [params.pop("data")]

        # remove sumlevel if it exists
        sumlevel = params.pop("sumlevel", None)

        # loop through each data and append to self.data
        self.data = []
        for d in data:

            # create a new dict containing the 'split' and 'static' params
            data_obj = {
                "map": d.pop("map", None),
                "split": d.pop("split", None),
                "static": d.pop("static", None),
                "share": d.pop("share", None)
            }

            # Set fallback API params
            d = param_format(d)

            # create the data URL
            p = RequestEncodingMixin._encode_params(d)
            data_obj["url"] = "{}/api/?{}".format(API, p)

            # store the params in the return dict
            data_obj["params"] = d

            # self.data.append(data_obj)

            if "limit" in d and "year" in d and d["year"] == "all":
                for year in year_cache[requests.get(data_obj["url"].replace("/api/", "/api/logic/")).json()["tables"][0]["table"]]:
                    new_obj = copy.deepcopy(data_obj)
                    year = str(int(year))
                    new_obj["url"] = new_obj["url"].replace("year=all", "year={}".format(year))
                    new_obj["params"]["year"] = year
                    self.data.append(new_obj)
            else:
                # append the data dict to self.data
                self.data.append(data_obj)

        self.attrs = []
        if "attrs" in params:
            # force the attrs of params into a list
            attrs = params.pop("attrs") if isinstance(params["attrs"], list) else [params.pop("attrs")]
            # loop through each data and append to self.data
            self.attrs = [{"type": a, "url": "{}/attrs/{}/".format(API, a)} for a in attrs]

        # set self.config to the params
        self.config = params

        if "mouse" in params:
            if params["mouse"] == "NO":
                self.config["mouse"] = False
            else:
                self.config["mouse"] = True

        # set the tooltip config using the function
        self.config["tooltip"] = params.pop("tooltip", {})
        self.config["tooltip"]["value"] = self.tooltip()

        # set default depth to zero
        self.config["depth"] = int(params["depth"]) if "depth" in params else 0

        # set default text to "name"
        self.config["text"] = params["text"] if "text" in params else "name"
Esempio n. 4
0
    def top(self, **kwargs):
        """str: A text representation of a top statistic or list of statistics """

        attr_type = kwargs.get("attr_type", self.attr_type)
        dataset = kwargs.get("dataset", False)
        moe = kwargs.pop("moe", False)
        truncate = int(kwargs.pop("truncate", 0))

        # create a params dict to use in the URL request
        params = {}

        # set the section's attribute ID in params
        attr_id = kwargs.get("attr_id", False)
        if attr_type != self.attr_type and attr_id and "top" in attr_id:
            l, o = attr_id.split(":")
            attr_id = self.top(**{
                "col": "id",
                "order": o,
                "sort": "desc",
                "limit": l[3:],
                "show": attr_type
            })["value"]

        child = kwargs.get("child", False)
        if attr_id == False:
            if child:
                kwargs["prefix"] = kwargs.get("prefix", True)
                where = self.children(**kwargs)
                if kwargs["prefix"] is True:
                    pre = kwargs.pop("prefix")
                if len(where) > 0:
                    params["where"] = "{}:{}".format(self.attr_type, where)
                    attr_id = ""
        if attr_id == False:
            attr_id = self.id(**kwargs)
        params[attr_type] = attr_id

        # get output key from either the value in kwargs (while removing it) or 'name'
        col = kwargs.pop("col", "name")
        data_only = kwargs.pop("data_only", False)
        if child:
            kwargs["sumlevel"] = self.sumlevel(**kwargs)

        for k in ["attr_type", "attr_id", "child", "dataset"]:
            if k in kwargs:
                del kwargs[k]

        # add the remaining kwargs into the params dict
        params = dict(params.items()+kwargs.items())

        # set default params
        params["limit"] = params.get("limit", 1)
        params["show"] = params.get("show", attr_type)
        params["sumlevel"] = params.get("sumlevel", "all")
        if "sumlevel" in params["sumlevel"]:
            params["sumlevel"] = params["sumlevel"].replace("sumlevel", self.sumlevel())
        if "naics_level" in params and "sumlevel" in params["naics_level"]:
            params["naics_level"] = params["naics_level"].replace("sumlevel", self.sumlevel())
        if "soc_level" in params and "sumlevel" in params["soc_level"]:
            params["soc_level"] = params["soc_level"].replace("sumlevel", self.sumlevel())
        params = param_format(params)

        if "force" not in params and params["required"] == "":
            col_maps = COLMAP.keys()
            col_maps += ["-".join(c) for c in list(combinations(col_maps, 2))]

            # extra allowed values for 'col'
            col_maps += ["id", "name", "ratio"]

            if col not in col_maps:
                params["required"] = col
            elif "order" in params:
                params["required"] = params["order"]

        if moe and "force" not in params:
            params["required"] += ",{}".format(moe)

        # make the API request using the params
        return stat(params, col=col, dataset=dataset, data_only=data_only, moe=moe, truncate=truncate)
Esempio n. 5
0
    def percent(self, **kwargs):
        """str: 2 columns divided by one another """

        attr_type = kwargs.get("attr_type", self.attr_type)
        attr_id = kwargs.get("attr_id", self.attr["id"])

        r = {"num": 1, "den": 1}
        for t in r.keys():
            key = kwargs.get(t)

            params = {}
            params["limit"] = 1
            params["year"] = kwargs.get("year", "latest")
            params = param_format(params)
            t_type = kwargs.get("{}_type".format(t), attr_type)
            params["show"] = kwargs.get("show", t_type)
            params[t_type] = kwargs.get("{}_id".format(t), attr_id)
            params["exclude"] = kwargs.get("exclude", kwargs.get("{}_exclude".format(t), ""))

            if "top:" in key:

                params["col"], params["force"] = key.split(":")[1].split(",")
                r["{}_key".format(t)] = params["col"]
                r[t] = self.top(**params)["data"][0]

            elif "var:" in key:

                keys = key.split(":")[1].split(",")
                if len(keys) == 2:
                    keys.append(None)
                ns, col, row = keys
                r["{}_key".format(t)] = col
                r[t] = self.var(namespace=ns, key=col, row=row, format="raw")

            elif "," in key:

                num, den = key.split(",")
                subparams = {}
                subparams["num"] = num
                subparams["den"] = den
                subparams["data_only"] = True
                subparams["num_id"] = params[t_type]
                subparams["den_id"] = params[t_type]
                r["{}_key".format(t)] = None
                r[t] = self.percent(**subparams)

            else:

                params["required"] = key
                r["{}_key".format(t)] = key

                # convert request arguments into a url query string
                query = RequestEncodingMixin._encode_params(params)
                url = u"{}/api?{}".format(API, query)

                try:
                    r[t] = datafold(requests.get(url).json())
                except ValueError:
                    app.logger.info("STAT ERROR: {}".format(url))
                    return "N/A"

                if len(r[t]) == 0:
                    return "N/A"
                r[t] = r[t][0][key]

            if r[t] in [None, "N/A"]:
                return "N/A"

        diff = kwargs.get("diff", False)
        text = kwargs.get("text", False)
        if text and text in TEXTCOMPARATORS:
            r["num"] = float(num_format(r["num"], r["num_key"], False, suffix=False).replace(",", ""))
            r["den"] = float(num_format(r["den"], r["den_key"], False, suffix=False).replace(",", ""))

        if r["num"] == 0 or r["den"] == 0:
            val = 0
        elif diff:
            val = r["num"] - r["den"]
        else:
            val = float(r["num"])/float(r["den"])

        if kwargs.get("invert", False):
            val = 1 - val

        if kwargs.get("data_only", False):
            return val

        if text and text in TEXTCOMPARATORS:
            text = TEXTCOMPARATORS[text]
            if diff:
                if val > 0:
                    return text[0]
                elif val < 0:
                    return text[1]
                else:
                    return text[2]
            else:
                if val > 1:
                    return text[0]
                elif val < 1:
                    return text[1]
                else:
                    return text[2]
        elif diff or kwargs.get("ratio", False):
            return num_format(abs(val))
        else:
            return "{}%".format(num_format(val * 100))
Esempio n. 6
0
    def __init__(self,
                 params,
                 highlight=False,
                 profile=False,
                 select=False,
                 slug=False,
                 topic=False):
        """Initializes a new Viz class.

        Args:
            config (str): The YAML configuration file as one long string.
            profile (Profile): The Profile class instance this Section will be a part of.

        """

        self.highlight = params.pop("highlight", highlight)
        self.profile = profile.attr
        self.select = select
        self.profile_type = profile.attr_type
        self.className = params.pop("class", False)
        self.slug = slug

        # force the data of params into a list
        data = params.pop("data") if isinstance(
            params["data"], list) else [params.pop("data")]
        data = [d for d in data if self.allowed_levels(d)]

        # remove sumlevel if it exists
        sumlevel = params.pop("sumlevel", None)

        # loop through each data and append to self.data
        self.data = []
        for d in data:

            # create a new dict containing the 'split' and 'static' params
            data_obj = {
                "join": d.pop("join", None),
                "divide": d.pop("divide", None),
                "map": d.pop("map", None),
                "merge": d.pop("merge", None),
                "split": d.pop("split", None),
                "static": d.pop("static", None),
                "share": d.pop("share", None),
                "sum": d.pop("sum", None),
            }

            # Set fallback API params
            d = param_format(d)

            # create the data URL
            p = RequestEncodingMixin._encode_params(d)
            join = "join/" if data_obj["join"] else ""
            data_obj["url"] = "{}/api/{}?{}".format(API, join, p)

            # store the params in the return dict
            data_obj["params"] = d

            # self.data.append(data_obj)

            if "limit" in d and "year" in d and d["year"] == "all":
                for year in year_cache[requests.get(data_obj["url"].replace(
                        "/api/", "/api/logic/")).json()["tables"][0]["table"]]:
                    new_obj = copy.deepcopy(data_obj)
                    year = str(int(year))
                    new_obj["url"] = new_obj["url"].replace(
                        "year=all", "year={}".format(year))
                    new_obj["params"]["year"] = year
                    self.data.append(new_obj)
            else:
                # append the data dict to self.data
                self.data.append(data_obj)

        self.attrs = []
        if "attrs" in params:
            # force the attrs of params into a list
            attrs = params.pop("attrs") if isinstance(
                params["attrs"], list) else [params.pop("attrs")]
            # loop through each data and append to self.data
            self.attrs = [{
                "type": a,
                "url": "{}/attrs/{}/".format(API, a)
            } for a in attrs]

        # set self.config to the params
        self.config = params

        if "mouse" in params:
            if params["mouse"] == "NO":
                self.config["mouse"] = False
            else:
                self.config["mouse"] = True

        # set the tooltip config using the function
        self.config["tooltip"] = params.pop("tooltip", {})
        self.config["tooltip"]["value"] = self.tooltip()
        tooltipValues = []
        if "year" not in tooltipValues:
            tooltipValues.append("year")
        for value in self.config["tooltip"]["value"]:
            tooltipValues.append(value)
            if value in COLLECTIONYEARS:
                tooltipValues.append("{}_collection".format(value))
        self.config["tooltip"]["value"] = tooltipValues

        # set default depth to zero
        self.config["depth"] = int(params["depth"]) if "depth" in params else 0

        # set default text to "name"
        self.config["text"] = params["text"] if "text" in params else "name"

        if topic and "cart" in topic:
            self.cart = topic["cart"]
        else:
            self.cart = False
Esempio n. 7
0
    def __init__(self, params, highlight=False, profile=False):
        """Initializes a new Viz class.

        Args:
            config (str): The YAML configuration file as one long string.
            profile (Profile): The Profile class instance this Section will be a part of.

        """

        self.highlight = params.pop("highlight", highlight)
        self.profile = profile.attr
        self.profile_type = profile.attr_type

        # force the data of params into a list
        data = params.pop("data") if isinstance(
            params["data"], list) else [params.pop("data")]

        # remove sumlevel if it exists
        sumlevel = params.pop("sumlevel", None)

        # loop through each data and append to self.data
        self.data = []
        for d in data:

            # create a new dict containing the 'split' and 'static' params
            data_obj = {
                "map": d.pop("map", None),
                "split": d.pop("split", None),
                "static": d.pop("static", None),
                "share": d.pop("share", None)
            }

            # Set fallback API params
            d = param_format(d)

            # create the data URL
            p = RequestEncodingMixin._encode_params(d)
            data_obj["url"] = "{}/api/?{}".format(API, p)

            # store the params in the return dict
            data_obj["params"] = d

            # append the data dict to self.data
            self.data.append(data_obj)

        self.attrs = []
        if "attrs" in params:
            # force the attrs of params into a list
            attrs = params.pop("attrs") if isinstance(
                params["attrs"], list) else [params.pop("attrs")]
            # loop through each data and append to self.data
            self.attrs = [{
                "type": a,
                "url": "{}/attrs/{}/".format(API, a)
            } for a in attrs]

        # set self.config to the params
        self.config = params

        if "mouse" in params:
            if params["mouse"] == "NO":
                self.config["mouse"] = False
            else:
                self.config["mouse"] = True

        # set the tooltip config using the function
        self.config["tooltip"] = params.pop("tooltip", {})
        self.config["tooltip"]["value"] = self.tooltip()

        # set default depth to zero
        self.config["depth"] = int(params["depth"]) if "depth" in params else 0

        # set default text to "name"
        self.config["text"] = params["text"] if "text" in params else "name"