Пример #1
0
 def list_top(data: pd.Series):
     *head, last = sk.pipe(
         data.sort_values(ascending=False).index[:top],
         sk.map(mundi.region),
         sk.map("{0.name}".format),
     )
     head = ", ".join(head)
     return _(" and ").join([head, last])
Пример #2
0
def parse_args(args):
    """
    Parse the "Args" section of a google docstring.
    """
    out = sk.pipe(args.splitlines(), sk.map(str.lstrip),
                  sk.partition_by(ARG.fullmatch), sk.partition(2), dict)
    return {k[0].rstrip(":"): "\n".join(v) for k, v in out.items()}
Пример #3
0
def parse_docstring(obj: Union[str, callable]) -> Dict[str, str]:
    """
    Parse a docstring in google format.

    Args:
        obj:
            Docstring or function

    Returns:
        A parsed docstring
    """
    if not isinstance(obj, str):
        doc = obj.__doc__
        if doc is None:
            raise ValueError("object does not have docstring")
    else:
        doc = obj
    lines = doc.splitlines()
    indent = len(lines[-1])
    lines = sk.pipe(
        lines,
        sk.map(_[indent:]),
        sk.partition_by(SECTION.fullmatch),
        sk.cons(("", )),
        sk.partition(2),
        dict,
    )
    return {
        k[0].strip(":"): "\n".join(filter(None, v))
        for k, v in lines.items()
    }
Пример #4
0
    def ask(self, parent_region="BR", where=st.sidebar):
        st = where
        scenario_kind = st.selectbox(_("Select scenario"), list(REGIONS_TYPES[parent_region]))
        scenario = REGIONS_TYPES[parent_region][scenario_kind]
        regions = self.__datahandler.get_regions(**scenario["query"])

        if "filter" in scenario:
            filtering = scenario["filter"]
            format_func = filtering.pop("format_func", None)
            if format_func is not None:
                function = format_func

                def format_func(x):
                    if x == "all":
                        return _("All")
                    return function(x)

            field, message = filtering.popitem()
            groups = sk.group_by(lambda x: getattr(x, field), regions)

            key = st.selectbox(message, ["all", *groups], format_func=format_func)
            if key != "all":
                regions = groups[key]

        message = _("Columns")
        columns = st.multiselect(message, COLUMNS, default=COLUMNS_DEFAULT)

        message = _("Isolation scores")
        kwargs = {"default": TARGETS_DEFAULT, "format_func": lambda x: f"{x}%"}
        targets = st.multiselect(message, TARGETS, **kwargs)

        message = _("Show values for the given days")
        days = st.multiselect(message, DAYS, default=DAYS_DEFAULT)
        if any(not isinstance(d, int) for d in days):
            day_max = sk.pipe(
                days,
                sk.remove(lambda d: isinstance(d, int)),
                sk.map(lambda d: int(NUMBER.search(d).groups()[0])),
                max,
            )
            days = list(range(1, day_max + 1))

        message = _("Transpose data")
        transpose = st.checkbox(message, value=False)

        self.__datahandler.user_inputs = {
            "parent_region": parent_region,
            "regions": regions,
            "columns": columns,
            "targets": targets,
            "days": days,
            "scenario": scenario,
            "transpose": transpose,
            "disease": "covid-19"
        }
Пример #5
0
def sidebar(parent_region="BR", where=st.sidebar):
    """
    Collect input parameters for the app to run.

    Returns:
        Dictionary with the following keys:
            parent_region, regions, columns, targets, days
    """
    st = where
    kind = st.selectbox(_("Select scenario"), list(REGIONS_TYPES[parent_region]))
    scenario = REGIONS_TYPES[parent_region][kind]
    regions = get_regions(**scenario["query"])

    if "filter" in scenario:
        filtering = scenario["filter"]
        format_func = filtering.pop("format_func", None)
        if format_func is not None:
            fn = format_func

            def format_func(x):
                if x == "all":
                    return _("All")
                return fn(x)

        field, msg = filtering.popitem()
        groups = sk.group_by(lambda x: getattr(x, field), regions)

        key = st.selectbox(msg, ["all", *groups], format_func=format_func)
        if key != "all":
            regions = groups[key]

    msg = _("Columns")
    columns = st.multiselect(msg, COLUMNS, default=COLUMNS_DEFAULT)

    msg = _("Isolation scores")
    kwargs = {"default": TARGETS_DEFAULT, "format_func": lambda x: f"{x}%"}
    targets = st.multiselect(msg, TARGETS, **kwargs)

    msg = _("Show values for the given days")
    days = st.multiselect(msg, DAYS, default=DAYS_DEFAULT)
    if any(not isinstance(d, int) for d in days):
        day_max = sk.pipe(
            days,
            sk.remove(lambda d: isinstance(d, int)),
            sk.map(lambda d: int(NUMBER.search(d).groups()[0])),
            max,
        )
        days = list(range(1, day_max + 1))

    msg = _("Transpose data")
    transpose = st.checkbox(msg, value=False)

    return {
        "parent_region": parent_region,
        "regions": regions,
        "columns": columns,
        "targets": targets,
        "days": days,
        "scenario": scenario,
        "transpose": transpose,
    }