Skip to content

Commit

Permalink
Fixed speed formatting for different activity types (#22)
Browse files Browse the repository at this point in the history
* Fixed speed formatting for different activity types

* Reformat with  black & fix lint issues

---------

Co-authored-by: Bartlomiej Wilczynski <[email protected]>
  • Loading branch information
futureshape and bwilczynski authored May 6, 2023
1 parent c0a756e commit bf3b5c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
select = BLK,C,E,F,W
ignore = E203,W503
max-line-length = 88
max-line-length = 119
2 changes: 1 addition & 1 deletion strava/commands/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _as_table(result):


def _format_summary_activity(activity):
def format_name(name):
def format_name(name, activity):
return format_activity_name(name, activity)

formatters = {"name": format_name, **_SUMMARY_ACTIVITY_FORMATTERS}
Expand Down
4 changes: 2 additions & 2 deletions strava/commands/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _format_activity(result, output=None):


def _as_table(activity):
def format_name(name):
def format_name(name, activity=None):
activity_name = format_activity_name(name, activity)
activity_description = activity.get("description")
return (
Expand All @@ -69,7 +69,7 @@ def format_heartrate_with_emoji(heartrate):
return f"{click.style(emoji.RED_HEART, fg='red')} {format_heartrate(heartrate)}"

def format_speed_with_emoji(speed):
return f"{click.style(emoji.RUNNING_SHOE, fg='yellow')} {format_speed(speed)}"
return f"{click.style(emoji.RUNNING_SHOE, fg='yellow')} {format_speed(speed, activity)}"

def format_elevation_with_emoji(elevation):
difference = round(elevation)
Expand Down
42 changes: 31 additions & 11 deletions strava/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
KM_TO_MI = 0.6213712


def format_seconds(seconds):
def format_seconds(seconds, activity=None):
if seconds > 3600:
mins = math.floor(seconds / 60)
return f"{math.floor(mins / 60):.0f}h {mins % 60:.0f}m"
else:
return f"{math.floor(seconds / 60):02.0f}:{seconds % 60:02.0f}"


def format_date(date):
def format_date(date, activity=None):
utc_date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
return utc_date.replace(tzinfo=timezone.utc).astimezone()


def format_distance(distance):
def format_distance(distance, activity=None):
distance = math.floor(distance / 10) / 100
suffix = "km"
if strava.settings.IMPERIAL_UNITS:
Expand All @@ -33,20 +33,40 @@ def format_distance(distance):
return f"{distance:.2f} {suffix}"


def format_speed(speed):
def format_speed(speed, activity):
activity_types_with_speed = [
"alpineski",
"backcountryski",
"ebikeride",
"handcycle",
"nordicski",
"ride",
"rollerski",
"skateboard",
"snowboard",
"virtualride",
]

suffix = "km"
postfix = "kph"
if strava.settings.IMPERIAL_UNITS:
suffix = "mi"
postfix = "mph"
speed = speed * KM_TO_MI

return f"{format_seconds(1000 / speed)} /{suffix}" if speed > 0 else None
if activity.get("type").lower() in activity_types_with_speed:
return f"{speed * 3.6:.1f} {postfix}" if speed > 0 else None
else:
return (
f"{format_seconds(1000 / speed, activity)} /{suffix}" if speed > 0 else None
)


def format_heartrate(heartrate):
def format_heartrate(heartrate, activity=None):
return f"{heartrate:.0f} bpm"


def format_activity_type(activity_type):
def format_activity_type(activity_type, activity=None):
type_emojis = {
"run": emoji.PERSON_RUNNING,
"walk": emoji.PERSON_WALKING,
Expand All @@ -57,7 +77,7 @@ def format_activity_type(activity_type):
return type_emojis.get(activity_type.lower(), "")


def format_elevation(elevation):
def format_elevation(elevation, activity=None):
return f"{round(elevation)} m"


Expand All @@ -68,18 +88,18 @@ def humanize(word):
return word


def noop_formatter(value):
def noop_formatter(value, activity=None):
return value


def format_activity_name(name, activity):
def format_activity_name(name, activity=None):
activity_type = format_activity_type(activity.get("type"))
is_race = activity.get("workout_type", 0) == 1
return f"{activity_type} {click.style(name, bold=is_race)}"


def apply_formatters(activity, formatters):
return {
k: formatter(activity[k]) if k in activity else N_A
k: formatter(activity[k], activity) if k in activity else N_A
for k, formatter in formatters.items()
}

0 comments on commit bf3b5c1

Please sign in to comment.