Skip to content

Commit

Permalink
Add options to hide name and firstlast columns
Browse files Browse the repository at this point in the history
  • Loading branch information
liskin committed Sep 10, 2021
1 parent 49208e3 commit 25e2093
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
10 changes: 8 additions & 2 deletions src/strava_gear/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@
'--report', type=click.Choice(reports.keys()),
default='bikes', show_default=True,
help="Type of report")
def main(rules, csv, strava_database, report):
@click.option(
'--show-name/--hide-name', default=True, show_default=True,
help="Show long component names")
@click.option(
'--show-first-last/--hide-first-last', default=True, show_default=True,
help="Show first/last usage of components")
def main(rules, csv, strava_database, report, show_name, show_first_last):
if csv:
aliases, activities = {}, read_input_csv(csv)
else:
aliases, activities = read_strava_offline()
rules = read_rules(rules, aliases=aliases)
res = apply_rules(rules, activities)
print(reports[report](res))
print(reports[report](res, show_name=show_name, show_first_last=show_first_last))


if __name__ == "__main__":
Expand Down
60 changes: 33 additions & 27 deletions src/strava_gear/report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
from functools import partial
from typing import Dict
from typing import Iterable
from typing import Iterator

from tabulate import tabulate

Expand All @@ -11,16 +11,28 @@
from .data import Result


def report_components(res: Result) -> str:
components: Iterable[Component] = sorted(res.components, key=lambda c: c.firstlast)
return tabulate(
[[c.ident, c.name, c.distance / 1000, c.time / 3600, c.firstlast] for c in components],
headers=["id", "name", "km", "hour", "first … last"],
floatfmt=".1f",
)
def report(f, res: Result, show_name: bool = True, show_first_last: bool = True) -> str:
def cols(d: Dict) -> Dict:
if not show_name:
del d["name"]
if not show_first_last:
del d["first … last"]
return d
return tabulate([cols(d) for d in f(res)], headers="keys", floatfmt=".1f")


def report_bikes(res: Result) -> str:
def report_components(res: Result) -> Iterator[Dict]:
for c in sorted(res.components, key=lambda c: c.firstlast):
yield {
"id": c.ident,
"name": c.name,
"km": c.distance / 1000,
"hour": c.time / 3600,
"first … last": c.firstlast,
}


def report_bikes(res: Result, show_names: bool = True, show_firstlasts: bool = True) -> Iterator[Dict]:
bikes_firstlasts = bikes_firstlast(res)

def sort_key(c: Component):
Expand All @@ -29,24 +41,18 @@ def sort_key(c: Component):

return bikes_firstlasts[b], b, t

def cols(c: Component):
for c in sorted((c for c in res.components if c.assignment), key=sort_key):
assert c.assignment
b, t = c.assignment
return [
res.bike_names.get(b, b),
t,
c.ident,
c.name,
c.distance / 1000,
c.time / 3600,
c.firstlast
]

return tabulate(
[cols(c) for c in sorted((c for c in res.components if c.assignment), key=sort_key)],
headers=["bike", "role", "id", "name", "km", "hour", "first … last"],
floatfmt=".1f",
)
yield {
"bike": res.bike_names.get(b, b),
"role": t,
"id": c.ident,
"name": c.name,
"km": c.distance / 1000,
"hour": c.time / 3600,
"first … last": c.firstlast,
}


def bikes_firstlast(res: Result) -> Dict[BikeId, FirstLast]:
Expand All @@ -59,6 +65,6 @@ def bikes_firstlast(res: Result) -> Dict[BikeId, FirstLast]:


reports = {
'components': partial(report_components),
'bikes': report_bikes,
'components': partial(report, report_components),
'bikes': partial(report, report_bikes),
}

0 comments on commit 25e2093

Please sign in to comment.