Example #1
0
    def name(self, **kwargs):
        """str: The attribute name """

        """ if there is a specified dataset, use the id function """
        if "dataset" in kwargs:
            return fetch(self.id(dataset=kwargs["dataset"]), self.profile.attr_type)["name"]

        return self.attr["name"]
Example #2
0
    def __init__(self, attr_id, attr_type):
        """Initializes a new Profile class.

        Args:
            attr_id (str): The ID of the selected attribute
            attr_type (str): The attribute type of the profile.

        """

        """ set id, attr (using the fetch function), and attr_type """
        self.id = attr_id
        self.attr = fetch(attr_id, attr_type)
        self.attr_type = attr_type
Example #3
0
    def top(self, **kwargs):
        """str: A text representation of a top statistic or list of statistics """

        show = kwargs.get("show")
        attr_type = kwargs.get("attr_type", self.profile.attr_type)

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

        """ set the section's attribute ID in params """
        params[attr_type] = kwargs.get("attr_id", self.attr["id"])

        """ get output key from either the value in kwargs (while removing it) or 'name' """
        col = kwargs.pop("col", "name")

        """ if the output key is not name, then add it to the params as a 'required' key """
        if col != "name":
            params["required"] = col

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

        """ set default params """
        params["limit"] = params.get("limit", 1)
        params["sort"] = params.get("sort", "desc")
        params["order"] = params.get("order", "")
        params["year"] = params.get("year", 2013)
        params["show"] = params.get("show", show)
        params["sumlevel"] = params.get("sumlevel", "all")

        """ if no required param is set, set it to the order param """
        if "required" not in params:
            params["required"] = params["order"]

        """ convert params into a url query string """
        params = urllib.urlencode(params)

        """ make the API request using the params, converting it to json and running it through the datafold util """
        try:
            r = datafold(requests.get("{}/api?{}".format(API, params)).json())
        except ValueError:
            raise Exception(params)

        """ if the output key is 'name', fetch attributes for each return and create an array of 'name' values """
        """ else create an array of the output key for each returned datapoint """
        if col == "name":
            top = [fetch(d[show], show)[col] for d in r]
        else:
            top = [d[col] for d in r]

        """ coerce all values to strings """
        top = [str(t) for t in top]

        """ if there's more than 1 value, prefix the last string with 'and' """
        if len(top) > 1:
            top[-1] = "and {}".format(top[-1])

        """ if there's only 2 values, return the list joined with a space """
        if len(top) == 2:
            return " ".join(top)

        """ otherwise, return the list joined with commans """
        return ", ".join(top)