def test_lt(self):
        train_1 = Speed(mile__hour=5)
        train_2 = Speed(mile__hour=10)

        self.assertTrue(
            train_1 < train_2
        )
    def test_abbreviations(self):
        train_1 = Speed(mph=4)
        train_2 = Speed(mile__hour=4)

        self.assertEqual(
            train_1,
            train_2
        )
    def test_equals(self):
        train_1 = Speed(mile__hour=10)
        train_2 = Speed(mile__hour=10)

        self.assertEqual(
            train_1,
            train_2,
        )
    def test_sub(self):
        train_1 = Speed(mile__hour=10)
        train_2 = Speed(mile__hour=5)

        expected_value = Speed(mile__hour=5)
        actual_value = train_1 - train_2

        self.assertEqual(expected_value, actual_value)
    def test_addition(self):
        train_1 = Speed(mile__hour=10)
        train_2 = Speed(mile__hour=5)

        actual_value = train_1 + train_2
        expected_value = Speed(mile__hour=15)

        self.assertEqual(actual_value, expected_value)
    def test_set_unit(self):
        speed = Speed(mi__hr=10)
        speed.unit = 'm__s'

        expected_value = 4.4704
        actual_value = speed.value

        self.assertAlmostEqual(
            expected_value,
            actual_value,
        )
示例#7
0
    def test_div(self):
        train_1 = Speed(mile__hour=10)
        divider = 2

        actual_value = train_1 / divider
        expected_value = Speed(mile__hour=5)

        self.assertEqual(
            actual_value,
            expected_value,
        )
    def test_mul(self):
        train_1 = Speed(mile__hour=10)
        multiplier = 2

        actual_value = multiplier * train_1
        expected_value = Speed(mile__hour=20)

        self.assertEqual(
            actual_value,
            expected_value,
        )
    def test_set_unit(self):
        speed = Speed(mi__hr=10)
        speed.unit = 'm__s'

        expected_value = 4.4704
        actual_value = speed.value

        self.assertAlmostEqual(
            expected_value,
            actual_value,
        )
    def test_set_unit(self):
        speed = Speed(mi__hr=10)
        speed.unit = 'm__s'

        expected_value =  Decimal('4.4704')
        actual_value = speed.value

        self.assertEqual(
            expected_value,
            actual_value,
        )
    def test_iadd(self):
        train_1 = Speed(mile__hour=10)
        train_2 = Speed(mile__hour=5)

        actual_value = train_1
        actual_value += train_2
        expected_value = Speed(mile__hour=15)

        self.assertEqual(
            actual_value,
            expected_value,
        )
    def test_different_units_addition(self):
        train = Speed(mile__hour=10)
        increase = Speed(km__day=2)

        two_km_day_in_mph = 0.0517809327

        expected_speed = Speed(mile__hour=10 + two_km_day_in_mph)
        actual_speed = train + increase

        self.assertAlmostEqual(
            expected_speed.standard,
            actual_speed.standard,
        )
    def test_attrconversion_nonstandard(self):
        miles_per_hour = Speed(mi__hr=22.3694)
        kilometers_per_minute = 0.599748864

        self.assertAlmostEqual(kilometers_per_minute,
                               miles_per_hour.km__min,
                               places=3)
示例#14
0
def calculate_calories_burned(activity: activity.Activity, distance: Distance,
                              bodyweight: Mass, elevation_gain: Distance,
                              duration: timedelta) -> int:
    """
    Calculates the number of calories (rounded to the nearest integer) burned during an activity.

    The number of calories burned is calculated by taking the VO2 in ml/min/kg, and converting that to
    L/min (multiply by athlete bodyweight in kg and divide by 1000), then multiplying that number by 5
    kCalories/min, and finally multiplying that by the number of minutes.  The result is then rounded
    to the nearest integer.

    Args:
        activity (Activity):
        distance (Distance):
        bodyweight (Mass):
        elevation_gate (Distance):
        duration (timedelta):

    Returns:
        int: The number of calories burned performing the activity
    """
    if distance.ft == 0:
        return 0

    if duration.total_seconds() == 0:
        return 0

    speed = Speed(mph=distance.mi / (duration.total_seconds() / 3600))
    grade = elevation_gain.ft / distance.ft
    vo2_est = vo2.get_VO2(activity, speed, grade)
    # Calculate VO2 in Liters/minute
    vo2_l_min = (vo2_est.get_value() * bodyweight.kg) / 1000

    return int(round(vo2_l_min * 5 * duration.total_seconds() / 60, 0))
    def test_attrconversion(self):
        meters_per_second = Speed(meter__second=10)
        miles_per_hour = 22.3694

        self.assertAlmostEqual(miles_per_hour,
                               meters_per_second.mi__hr,
                               places=3)
    def test_attrconversion_nonstandard(self):
        miles_per_hour = Speed(mi__hr=22.3694)
        kilometers_per_minute =  Decimal('0.600000995')

        self.assertAlmostEqual(
            kilometers_per_minute,
            miles_per_hour.km__min,
            places=9
        )
    def test_aliases(self):
        speed = Speed(mph=10)

        expected_kph =  Decimal('16.09344')
        actual_kph = speed.kph

        self.assertEqual(
            expected_kph,
            actual_kph,
        )
    def test_aliases(self):
        speed = Speed(mph=10)

        expected_kph = 16.09344
        actual_kph = speed.kph

        self.assertAlmostEqual(
            expected_kph,
            actual_kph,
        )
    def test_bool_false(self):
        train_1 = Speed(mile__hour=0)

        self.assertFalse(train_1)
import pytest
from domain import activity, vo2
from measurement.measures import Speed

vo2_inst_testdata = [
    (0, 0, Speed(mph=0), 0, 3.5),  # Should get resting constant value of 3.5
    (1, 1, Speed(kph=6), 0, 103.5),
    (vo2.O2_COST_HORIZ_RUN, vo2.O2_COST_VERT_RUN, Speed(kph=6), 0, 23.5),
    (vo2.O2_COST_HORIZ_WALK, vo2.O2_COST_VERT_WALK, Speed(mph=2), 0.15,
     23.348576),
]


@pytest.mark.parametrize("o2_cost_horiz,o2_cost_vert,speed,grade,expected",
                         vo2_inst_testdata)
def test_vo2_instantiation(o2_cost_horiz, o2_cost_vert, speed, grade,
                           expected):
    vo2_est = vo2.VO2(o2_cost_horiz=o2_cost_horiz,
                      o2_cost_vert=o2_cost_vert,
                      speed=speed,
                      grade=grade)

    assert expected == vo2_est.get_value()


vo2_fact_testdata = [
    (activity.Run(), Speed(kph=6), 0, 23.5),
    (activity.Walk(), Speed(mph=2), 0.15, 23.348576),
]

 def test_string_formatting(self):
     speed = Speed(m__s=1)
     self.assertEqual(speed.unit_label, u"m/s")
     self.assertEqual("1.0 m/s", "{:.1f %u}".format(speed))
     self.assertEqual("1.0 m/s", "{}".format(speed))
     self.assertEqual("Speed(m__s=1.0)", repr(speed))
    def test_bool_true(self):
        train_1 = Speed(mile__hour=5)

        self.assertTrue(train_1)
示例#23
0
def _api_activity_blocks(url, activity):
    activity_sub = {
        'id': activity['id'],
        'timestamp': format_date(activity['start_date'], format='long'),
        'name': activity['name'],
        'description': activity['description'],
        'athlete.id': activity['athlete']['id'],
        'athlete.firstname': activity['athlete']['firstname'],
        'athlete.lastname': activity['athlete']['lastname'],
        'url': url,
    }
    blocks = [{
        "type": "section",
        "text": {
            "type":
            "mrkdwn",
            "text":
            "<%(url)s|*%(name)s*> by <https://www.strava.com/athletes/%(athlete.id)s|%(athlete.firstname)s %(athlete.lastname)s>, %(timestamp)s\n%(description)s"
            % activity_sub,
        },
        "accessory": {
            "type": "image",
            "image_url": generate_url(activity),
            "alt_text": "route map",
        },
    }]

    fields = []
    if activity.get('distance', None):
        fields.append({
            "type":
            "mrkdwn",
            "text":
            "*Distance:* %smi" % round(Distance(m=activity['distance']).mi, 2),
        })

    if activity.get('total_elevation_gain', None):
        fields.append({
            "type":
            "mrkdwn",
            "text":
            "*Elevation:* %sft" %
            round(Distance(m=activity['total_elevation_gain']).ft, 0),
        })

    if activity.get('average_speed', None):
        fields.append({
            "type":
            "mrkdwn",
            "text":
            "*Speed:* %smph" %
            round(Speed(m__s=activity['average_speed']).mph, 0),
        })

    if fields:
        blocks.append({"type": "divider"})
        blocks.append({"type": "section", "fields": fields})

    try:
        primary_image = activity['photos']['primary']['urls']['600']
    except (KeyError, TypeError):
        primary_image = None
    if primary_image:
        blocks.append({
            "type": "image",
            "image_url": primary_image,
            "alt_text": "Cover Photo"
        })
    return blocks