Example #1
0
def define_base_si_units():
    """Define the basic SI units.

    >>> define_base_si_units()
    >>> unit('m').is_si()
    True
    """
    # meter, gram, second, ampere, kelvin, mole, candela
    for sym in ["m", "g", "s", "A", "K", "mol", "cd"]:
        LeafUnit(sym, is_si=True)

    scaled_unit('tonne', 'kg', 1000) # == 1Mg.
Example #2
0
def define_base_si_units():
    """Define the basic SI units.
    
    >>> define_base_si_units()
    >>> unit('m').is_si()
    True
    """
    # meter, gram, second, ampere, kelvin, mole, candela
    for sym in ["m", "g", "s", "A", "K", "mol", "cd"]:
        LeafUnit(sym, is_si=True)
    
    scaled_unit('tonne', 'kg', 1000) # == 1Mg. 
Example #3
0
def define_time_units():
    """Define some common time units.

    >>> from units.compatibility import within_epsilon
    >>> define_base_si_units()
    >>> define_time_units()
    >>> hour = unit('h')
    >>> hour.is_si()
    False
    >>> from units.quantity import Quantity
    >>> half_hour = Quantity(0.5, hour)
    >>> few_secs = Quantity(60.0, unit('s'))
    >>> sum = half_hour + few_secs

    >>> mins = unit('min')
    >>> thirty_one = Quantity(31, mins)
    >>> within_epsilon(thirty_one, sum)
    True
    """
    assert unit('s').is_si() # Ensure SI units already defined.

    scaled_unit('min', 's', 60.)
    scaled_unit('h', 'min', 60.)
    scaled_unit('day', 'h', 24.)
    scaled_unit('wk', 'day', 7.)
Example #4
0
def define_time_units():
    """Define some common time units.
    
    >>> from units.compatibility import within_epsilon
    >>> define_base_si_units()
    >>> define_time_units()
    >>> hour = unit('h')
    >>> hour.is_si()
    False
    >>> from units.quantity import Quantity
    >>> half_hour = Quantity(0.5, hour)
    >>> few_secs = Quantity(60.0, unit('s'))
    >>> sum = half_hour + few_secs
    
    >>> mins = unit('min')
    >>> thirty_one = Quantity(31, mins)
    >>> within_epsilon(thirty_one, sum)
    True
    """
    assert unit("s").is_si()  # Ensure SI units already defined.

    scaled_unit("min", "s", 60.0, name="minute")
    scaled_unit("h", "min", 60.0, name="hour")
    scaled_unit("day", "h", 24.0, symbal="d", name="day")
    scaled_unit("wk", "day", 7.0, name="week")
Example #5
0
def define_time_units():
    """Define some common time units.
    
    >>> from units.compatibility import within_epsilon
    >>> define_base_si_units()
    >>> define_time_units()
    >>> hour = unit('h')
    >>> hour.is_si()
    False
    >>> from units.quantity import Quantity
    >>> half_hour = Quantity(0.5, hour)
    >>> few_secs = Quantity(60.0, unit('s'))
    >>> sum = half_hour + few_secs
    
    >>> mins = unit('min')
    >>> thirty_one = Quantity(31, mins)
    >>> within_epsilon(thirty_one, sum)
    True
    """
    assert unit('s').is_si() # Ensure SI units already defined.
    
    scaled_unit('min', 's', 60.)
    scaled_unit('h', 'min', 60.)
    scaled_unit('day', 'h', 24.)
    scaled_unit('wk', 'day', 7.)
Example #6
0
def define_base_si_units():
    """Define the basic SI units.
    
    >>> define_base_si_units()
    >>> unit('m').is_si()
    True
    """
    # Grabed from http://en.wikipedia.org/wiki/Conversion_of_units
    # TODO: Are these names differnet in another language?
    LeafUnit("m", name="metre", is_si=True)  # Length, area, volume
    LeafUnit("g", name="gram", is_si=True)  # Mass, Density
    LeafUnit("s", name="second", is_si=True)  # Time
    LeafUnit("A", name="ampere", is_si=True)  # Electric Current
    LeafUnit("K", name="kelvin", is_si=True)  # Temperature
    LeafUnit("cd", name="candela", is_si=True)  # Luminous intensity
    LeafUnit("mol", name="mole", is_si=True)

    scaled_unit("tonne", "kg", 1000)  # == 1Mg.
Example #7
0
    def __init__(self, codata):
        #SI units
        self.si_mass = unit('kg')
        self.si_charge = unit('C')
        self.si_energy = unit('J')
        self.si_action = unit('J') * unit('s')
        self.si_time = unit('s')
        self.si_frequency = unit('Hz')
        #farad_per_meter = unit('F') / unit('m')

        self.codata = codata

        #Define basic atomic units constants
        self.mass = scaled_unit('mass_au', *codata['electron mass'])
        self.charge = scaled_unit('charge_au', *codata['atomic unit of charge'])
        self.electrostatic_constant = named_unit('eps0_au', ['F'], ['m'],
                codata['atomic unit of permittivity'][1])
        self.hbar = named_unit('action_au', ['J'], ['s'],
                codata['atomic unit of action'][1])
        self.alpha = codata['fine-structure constant'][1]
        self.time = scaled_unit('time_au', *codata['atomic unit of time'])

        #Derived units
        self.speed = scaled_unit("speed_au", *codata['atomic unit of velocity'])
        self.lightspeed = self.speed(codata['speed of light in vacuum'][1])
        self.length = scaled_unit('length_au', 'm',
                (self.electrostatic_constant * self.hbar**2 /
                    (self.mass * self.charge**2)).squeeze())
        self.energy = scaled_unit('energy_au', 'J',
                (self.hbar**2 / (self.mass * self.length**2)).squeeze())
        self.electric_field_strength = named_unit('efield_au', ['V'], ['m'],
            (self.charge / (self.electrostatic_constant * self.length**2)).squeeze())
        self.frequency = scaled_unit('frequency_au', 'Hz', 1.0/self.time.squeeze())

        #Other units
        self.electron_volt = scaled_unit('eV', *codata['electron volt'])
        self.femtosecond = scaled_unit('fs', 's', self.si_time.squeeze()*1e-15)
Example #8
0
def define_volumes():
    """Define some common kitchen volumes.


    >>> from units.quantity import Quantity
    >>> define_base_si_units()
    >>> define_volumes()
    >>> one_litre = Quantity(520, unit('mL')) + Quantity(2, unit('cups'))
    >>> one_litre == Quantity(1, unit('L'))
    True
    """
    # Dangerous unit, 3L gives a long int.
    assert unit('m').is_si()
    NamedComposedUnit("L", unit("dm") ** 3, is_si=True)

    scaled_unit('tsp', 'mL', 5)
    scaled_unit('tbsp', 'mL', 15)
    scaled_unit('cups', 'mL', 240)
Example #9
0
def define_volumes():
    """Define some common kitchen volumes.
    
    
    >>> from units.quantity import Quantity
    >>> define_base_si_units()
    >>> define_volumes()
    >>> one_litre = Quantity(520, unit('mL')) + Quantity(2, unit('cups'))
    >>> one_litre == Quantity(1, unit('L'))
    True
    """
    # Dangerous unit, 3L gives a long int.
    assert unit("m").is_si()
    NamedComposedUnit("L", unit("dm") ** 3, is_si=True)

    scaled_unit("tsp", "mL", 5.0)
    scaled_unit("tbsp", "mL", 15.0)
    scaled_unit("cups", "mL", 240.0)
Example #10
0
def define_ridiculous_units():
    """Define some silly units.
    
    >>> define_units()
    >>> from units.quantity import Quantity
    >>> Quantity(1, unit('keg')) / Quantity(1, unit('bottle'))
    140.8450704225352
    """
    
    scaled_unit('firkin', 'lb', 90)
    scaled_unit('fortnight', 'day', 14)
    
    scaled_unit('smoot', 'cm', 170)
    
    scaled_unit('hiroshima', 'J', 6.3 * 10 ** 13)
        
    scaled_unit('bottle', 'mL', 355)
    scaled_unit('keg', 'L', 50)    

        
Example #11
0
def define_computer_units():
    """Define some units for technology.

    >>> define_units()
    >>> unit('GiB')(200) > unit('GB')(200) # bastard marketers
    True
    """

    NamedComposedUnit('flop', unit('operation') / unit('s'), is_si=True)
    scaled_unit('B', 'bit', 8, is_si=True)  # byte
    scaled_unit('KiB', 'B', 1024)
    scaled_unit('MiB', 'KiB', 1024)
    scaled_unit('GiB', 'MiB', 1024)
    scaled_unit('TiB', 'GiB', 1024)
    scaled_unit('PiB', 'TiB', 1024)
Example #12
0
def define_ridiculous_units():
    """Define some silly units.

    >>> define_units()
    >>> from units.quantity import Quantity
    >>> Quantity(1, unit('keg')) / Quantity(1, unit('bottle'))
    140.8450704225352
    """

    scaled_unit('firkin', 'lb', 90)
    scaled_unit('fortnight', 'day', 14)

    scaled_unit('smoot', 'cm', 170)

    scaled_unit('hiroshima', 'J', 6.3 * 10 ** 13)

    scaled_unit('bottle', 'mL', 355)
    scaled_unit('keg', 'L', 50)
Example #13
0
def define_ridiculous_units():
    """Define some silly units.
    
    >>> define_units()
    >>> from units.quantity import Quantity
    >>> Quantity(1, unit('keg')) / Quantity(1, unit('bottle'))
    140.8450704225352
    """

    scaled_unit("firkin", "lb", 90.0, name="firkin")
    scaled_unit("fortnight", "day", 14.0, name="fortnight")

    scaled_unit("smoot", "cm", 170.0, name="smoot")

    scaled_unit("hiroshima", "J", 6.3 * 10.0 ** 13.0, name="hiroshima")

    scaled_unit("bottle", "mL", 355.0, name="bottle")
    scaled_unit("keg", "L", 50.0, name="keg")
Example #14
0
def define_computer_units():
    """Define some units for technology.
    
    >>> define_units()
    >>> unit('GiB')(200) > unit('GB')(200) # bastard marketers
    True
    """

    NamedComposedUnit("flop", unit("operation") / unit("s"), name="flop", is_si=True)
    scaled_unit("B", "bit", 8.0, name="byte", is_si=True)
    scaled_unit("KiB", "B", 1024.0, name="kilobyte")
    scaled_unit("MiB", "KiB", 1024.0, name="megabyte")
    scaled_unit("GiB", "MiB", 1024.0, name="gigabyte")
    scaled_unit("TiB", "GiB", 1024.0, name="terabyte")
    scaled_unit("PiB", "TiB", 1024.0, name="petabyte")
Example #15
0
def define_astronomical_units():
    """Define some astronomical units."""
    scaled_unit("ly", "m", 9460730472580800, name="light-year")
    scaled_unit("AU", "m", 149597870691, name="Astronomical unit")
    scaled_unit("pc", "m", 3.08568025 * 10 ** 16, name="parsec", is_si=True)
Example #16
0
def define_imperial_units():
    """Define some common imperial units."""

    assert unit("m").is_si()  # Ensure SI units already defined

    # scaled_unit measures
    scaled_unit("inch", "cm", 2.54, name="inch")
    scaled_unit("in", "cm", 2.54, name="inch")  # 'in' is a python keyword
    scaled_unit("ft", "inch", 12.0, name="foot")
    scaled_unit("yd", "ft", 3.0, name="yard")
    scaled_unit("fm", "ft", 6.0, name="fathom")
    scaled_unit("rd", "yd", 5.5, name="rod")
    scaled_unit("fur", "rd", 40.0, name="furlong")
    scaled_unit("mi", "fur", 8.0, name="mile")
    scaled_unit("lea", "mi", 3.0, name="leage")

    # nautical scaled_unit measures
    scaled_unit("NM", "m", 1852.0, name="nautical mile")
    scaled_unit("cable", "NM", 0.1, name="cable length")

    # chain measure
    scaled_unit("li", "inch", 7.92, name="link")
    scaled_unit("ch", "li", 100.0, name="chain")

    # area measure
    NamedComposedUnit("acre", ComposedUnit([unit("rd")] * 2, [], 160.0), name="acre")

    # liquid measures
    NamedComposedUnit("pt", ComposedUnit([unit("inch")] * 3, [], 28.875), name="pint")

    scaled_unit("gi", "pt", 0.25, name="gills")
    scaled_unit("qt", "pt", 2.0, name="quarts")
    scaled_unit("gal", "qt", 4.0, name="gallons")

    scaled_unit("fl oz", "pt", 1.0 / 16.0, name="fluid ounce")
    scaled_unit("fl dr", "fl oz", 1.0 / 8.0, name="fluid drachm")
    scaled_unit("minim", "fl dr", 1.0 / 60.0, name="minim")

    # weight
    scaled_unit("oz", "g", 28.375, name="ounce")
    scaled_unit("lb", "oz", 16.0, name="pound")
    scaled_unit("ton", "lb", 2000.0, name="ton")
    scaled_unit("grain", "lb", 1.0 / 7000.0, name="grain")
    scaled_unit("dr", "lb", 1.0 / 256.0, name="dram")
    scaled_unit("cwt", "lb", 100.0, name="hundredweight")

    scaled_unit("dwt", "grain", 24.0, name="pennyweight")
    scaled_unit("oz t", "dwt", 20.0, name="ounce troy")
    scaled_unit("lb t", "oz t", 12.0, name="pound troy")

    # power
    scaled_unit("hpl", "W", 746.9999, name="mechanical")
    scaled_unit("hpm", "W", 735.49875, name="metric horsepower")
    scaled_unit("hpe", "W", 746.0, name="electric horsepower")

    # energy
    scaled_unit("BTU", "J", 1055.056, name="ISO BTU", is_si=True)
Example #17
0
"""Define the scalar units used by vehicle measurements."""
from __future__ import absolute_import

import sys
from units import unit, scaled_unit, named_unit
import units.predefined

units.predefined.define_units()


Percentage = unit("%")
Meter = unit("m")
Kilometer = scaled_unit("km", "m", 1000)
Hour = unit("h")
KilometersPerHour = unit("km") / unit("h")
RotationsPerMinute = unit("rotations") / unit("m")
Litre = unit("L")
Degree = unit("deg")
NewtonMeter = named_unit("Nm", ["N", "m"], [])
MetersPerSecondSquared = unit("m") / (pow(unit("s"), 2))
Undefined = unit("undef")
Example #18
0
def define_astronomical_units():
    """Define some astronomical units."""
    scaled_unit('ly', 'm', 9460730472580800) # light-year
    scaled_unit('AU', 'm', 149597870691) # Astronomical unit
    scaled_unit('pc', 'm', 3.08568025 * 10 ** 16, is_si=True) # parsec
Example #19
0
def home(request):

    from_date = request.GET.get('from_date')
    to_date = request.GET.get('to_date')

    activities = []
      
    if from_date or to_date:
        if from_date:
            from_datetime = datetime.strptime(from_date + " 00:00:00", "%d/%m/%Y %H:%M:%S")
        if to_date:
            to_datetime = datetime.strptime(to_date + " 23:59:59", "%d/%m/%Y %H:%M:%S")

        page = request.GET.get('page')

        # get access token
        social = request.user.social_auth.get(provider='strava')
        token = social.extra_data['access_token']

        # get activity details
        client = Client()
        client.access_token = token

        if from_date and to_date:
            query = client.get_activities(before=to_datetime, after=from_datetime)
        elif from_date: 
            query = client.get_activities(after=from_datetime)
        else: # to_date
            query = client.get_activities(before=to_datetime)

        km = scaled_unit('km', 'm', 1000) # define a new unit

        for activity in query:
            if from_date and not to_date:
                index = 0
            else:
                index = len(activities)

            activities.insert(index,
                { 
                    "id": activity.id,
                    "name": activity.name,
                    "distance": km(activity.distance),
                    "type": activity.type,
                    "link": "https://www.strava.com/activities/{}".format(activity.id),
                    "date": activity.start_date_local,
                }
            )

        paginator = Paginator(activities, 20) # Show n results per page
        try:
            activities = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            activities = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            activities = paginator.page(paginator.num_pages)

    return render(request, 'core/home.html', 
                            context={
                                'activities': activities,
                                'from_date': from_date,
                                'to_date': to_date,
                            })
Example #20
0
def define_computer_units():
    """Define some units for technology.
    
    >>> define_units()
    >>> unit('GiB')(200) > unit('GB')(200) # bastard marketers
    True
    """

    NamedComposedUnit('flop', unit('operation') / unit('s'), is_si=True)
    scaled_unit('B', 'bit', 8, is_si=True)  # byte
    scaled_unit('KiB', 'B', 1024)
    scaled_unit('MiB', 'KiB', 1024)
    scaled_unit('GiB', 'MiB', 1024)
    scaled_unit('TiB', 'GiB', 1024)
    scaled_unit('PiB', 'TiB', 1024)
Example #21
0
def define_astronomical_units():
    """Define some astronomical units."""
    scaled_unit('ly', 'm', 9460730472580800) # light-year
    scaled_unit('AU', 'm', 149597870691) # Astronomical unit
    scaled_unit('pc', 'm', 3.08568025 * 10 ** 16, is_si=True) # parsec
Example #22
0
def define_imperial_units():
    """Define some common imperial units."""
    
    assert unit('m').is_si() # Ensure SI units already defined
    
    # scaled_unit measures
    scaled_unit('inch', 'cm', 2.54) # 'in' is a python keyword
    scaled_unit('ft', 'inch', 12) # foot
    scaled_unit('yd', 'ft', 3) # yard
    scaled_unit('fathom', 'ft', 6) 
    scaled_unit('rd', 'yd', 5.5) # rod
    scaled_unit('fur', 'rd', 40) # furlong
    scaled_unit('mi', 'fur', 8) # mile
    scaled_unit('league', 'mi', 3)

    # nautical scaled_unit measures
    scaled_unit('NM', 'm', 1852) # Nautical mile
    scaled_unit('cable', 'NM', 0.1)
    
    # chain measure
    scaled_unit('li', 'inch', 7.92) # link
    scaled_unit('ch', 'li', 100) # chain
    
    # area measure
    NamedComposedUnit('acre',
                      ComposedUnit([unit('rd'), unit('rd')],
                                   [],
                                   160))
                                   
    # liquid measures
    NamedComposedUnit('pt', 
                      ComposedUnit([unit('inch')] * 3,
                                   [],
                                   28.875)) # pint
    
    scaled_unit('gi', 'pt', 0.25) # gills
    scaled_unit('qt', 'pt', 2) # quarts
    scaled_unit('gal', 'qt', 4) # gallons

    scaled_unit('fl oz', 'pt', 1.0 / 16)
    scaled_unit('fl dr', 'fl oz', 1.0 / 8)
    scaled_unit('minim', 'fl dr', 1.0 / 60)
    
    # weight
    
    scaled_unit('oz', 'g', 28.375)
    scaled_unit('lb', 'oz', 16)
    scaled_unit('ton', 'lb', 2000)
    scaled_unit('grain', 'lb', 1.0 / 7000)
    scaled_unit('dr', 'lb', 1.0 / 256) # dram
    scaled_unit('cwt', 'lb', 100) # hundredweight
    
    scaled_unit('dwt', 'grain', 24) # pennyweight
    scaled_unit('oz t', 'dwt', 20) # ounce troy
    scaled_unit('lb t', 'oz t', 12) # pound troy
    
    # power
    scaled_unit('hpl', 'W', 746.9999) # mechanical
    scaled_unit('hpm', 'W', 735.49875) # metric horsepower
    scaled_unit('hpe', 'W', 746) # electric horsepower.
    
    # energy
    scaled_unit('BTU', 'J', 1055.056, is_si=True) # ISO BTU
Example #23
0
import sys
from units import unit, scaled_unit, named_unit
import units.predefined

units.predefined.define_units()

## @var Percentage
# The percentage unit.
Percentage = unit('%')
## @var Meter
# The meter unit.
Meter = unit('m')
## @var Kilometer
# The Kilometer unit.
Kilometer = scaled_unit('m', 'km', 1000)
## @var Hour
# The Hour unit.
Hour = unit('h')
## @var KilometersPerHour
# The KilometersPerHour unit.
KilometersPerHour = unit('km') / unit('h')
## @var RotationsPerMinute
# The RotationsPerMinute unit.
RotationsPerMinute = unit('rotations') / unit('m')
## @var Litre
# The Litre unit.
Litre = unit('L')
## @var Degree
# The Degree unit.
Degree = unit('deg')
Example #24
0
"""Define the scalar units used by vehicle measurements."""

import sys
from units import unit, scaled_unit, named_unit
import units.predefined

units.predefined.define_units()

Percentage = unit('%')
Meter = unit('m')
Kilometer = scaled_unit('km', 'm', 1000)
Hour = unit('h')
KilometersPerHour = unit('km') / unit('h')
RotationsPerMinute = unit('rotations') / unit('m')
Litre = unit('L')
Degree = unit('deg')
NewtonMeter = named_unit("Nm", ["N", "m"], [])
MetersPerSecondSquared = unit('m') / (pow(unit('s'), 2))
Undefined = unit('undef')
Example #25
0
def define_imperial_units():
    """Define some common imperial units."""

    assert unit('m').is_si() # Ensure SI units already defined

    # scaled_unit measures
    scaled_unit('inch', 'cm', 2.54) # 'in' is a python keyword
    scaled_unit('ft', 'inch', 12) # foot
    scaled_unit('yd', 'ft', 3) # yard
    scaled_unit('fathom', 'ft', 6)
    scaled_unit('rd', 'yd', 5.5) # rod
    scaled_unit('fur', 'rd', 40) # furlong
    scaled_unit('mi', 'fur', 8) # mile
    scaled_unit('league', 'mi', 3)

    # nautical scaled_unit measures
    scaled_unit('NM', 'm', 1852) # Nautical mile
    scaled_unit('cable', 'NM', 0.1)

    # chain measure
    scaled_unit('li', 'inch', 7.92) # link
    scaled_unit('ch', 'li', 100) # chain

    # area measure
    NamedComposedUnit('acre',
                      ComposedUnit([unit('rd'), unit('rd')],
                                   [],
                                   160))

    # liquid measures
    NamedComposedUnit('pt',
                      ComposedUnit([unit('inch')] * 3,
                                   [],
                                   28.875)) # pint

    scaled_unit('gi', 'pt', 0.25) # gills
    scaled_unit('qt', 'pt', 2) # quarts
    scaled_unit('gal', 'qt', 4) # gallons

    scaled_unit('fl oz', 'pt', 1.0 / 16)
    scaled_unit('fl dr', 'fl oz', 1.0 / 8)
    scaled_unit('minim', 'fl dr', 1.0 / 60)

    # weight

    scaled_unit('oz', 'g', 28.375)
    scaled_unit('lb', 'oz', 16)
    scaled_unit('ton', 'lb', 2000)
    scaled_unit('grain', 'lb', 1.0 / 7000)
    scaled_unit('dr', 'lb', 1.0 / 256) # dram
    scaled_unit('cwt', 'lb', 100) # hundredweight

    scaled_unit('dwt', 'grain', 24) # pennyweight
    scaled_unit('oz t', 'dwt', 20) # ounce troy
    scaled_unit('lb t', 'oz t', 12) # pound troy

    # power
    scaled_unit('hpl', 'W', 746.9999) # mechanical
    scaled_unit('hpm', 'W', 735.49875) # metric horsepower
    scaled_unit('hpe', 'W', 746) # electric horsepower.

    # energy
    scaled_unit('BTU', 'J', 1055.056, is_si=True) # ISO BTU