2

Anyone know a python package that's the dateutil of distance strings? It would be great if something was out there that worked something like the following:

>>> from awesome_dist_module import Distance
>>> d = Distance("100 ft")
>>> d.meters
30.48
>>> d = Distance("100 feet")
>>> d.meters
30.48
>>> d.miles
0.0189393939

The key thing isn't the conversions so much as being able to pass in a string that represents some kind of measurement and get back a float for another measurement of you're choosing without knowing what measurement is in the string.

Anything?

Eric Palakovich Carr
  • 22,701
  • 8
  • 49
  • 54
  • A couple of packages to check out in this question: http://stackoverflow.com/questions/2125076/unit-conversion-in-python – Mark Ransom Oct 15 '10 at 15:38
  • You're likely to get a lot of people looking at this question who think you're referring to something like Levenshtein distance or related algorithms (and people ignoring it who might otherwise have an answer). Possibly change the title, or just defer to the previously asked question. – Nick Bastin Oct 15 '10 at 16:50

3 Answers3

3

I wrote this for you, but you can expand it as you like:

class Distance(object):

    METER = 1
    FOOT = 0.3048
    MILE = 1609.344
    INCH = 0.0254
    UNITS = {'meters': METER,
         'mts': METER,
         'mt': METER,
         'feet': FOOT,
         'foot': FOOT,
         'ft': FOOT,
         'miles': MILE,
         'mls': MILE,
         'ml': MILE,
         'inch': INCH,
         }
    def __init__(self, s):
        self.number, unit = s.split()
        self._convert(unit)

    def _convert(self, unit):
        self.number = float(self.number)
        if self.UNITS[unit] != 1:
            self.number *= self.UNITS[unit]

    @ property
    def meters(self):
        return self.number

    @ meters.setter
    def meters(self, v):
        self.number = float(v)

    @ property
    def miles(self):
        return self.number / self.MILE

    @ miles.setter
    def miles(self, v):
        self.number = v
        self._convert('miles')

    @ property
    def feet(self):
        return self.number / self.FOOT

    @ feet.setter
    def feet(self, v):
        self.number = v
        self._convert('feet')

    @ property
    def inch(self):
        return self.number / self.INCH

    @ inch.setter
    def inch(self, v):
        self.number = v
        self._convert('inch')

Some examples:

>>> d = Distance('1302.09029321 mts')
>>> d.meters
1302.09029321
>>> d.feet
4271.949780872703
>>> d.inch
51263.39737047244
>>> d.miles
0.8090813978925575
>>> d.miles = 1
>>> d.meters
1609.344
>>> d.feet
5280.0
>>> d.inch = .0002
>>> d.inch
0.00019999999999999998
>>> d.feet
1.6666666666666664e-05
>>> d.meters
5.08e-06
>>> d.miles
3.156565656565656e-09
>>> d.feet = 1
>>> d.meters
0.3048
>>> d.miles
0.0001893939393939394

EDIT: Added setters

rubik
  • 8,814
  • 9
  • 58
  • 88
2

If you know which units you'll need (and don't need to look up hundreds of different units) you can implement this yourself fairly easily. Here's an example:

class Distance():

    METER   = 1
    FOOT    = 0.3048

    UNITS = { "feet" : FOOT,
              "ft"   : FOOT,
              "foot" : FOOT,
              "meters" : METER,
              "mts"    : METER,
              "meter"  : METER,
              "mt"     : METER }

    def __init__(self, string):

        number, unit = string.split()
        number = long(number)

        self.value = number * Distance.UNITS[unit]

if __name__ == "__main__":
    d = Distance("1500 ft")
    print d.value

That takes care of the string reading for you. You can then use any unit conversion library.

salezica
  • 74,081
  • 25
  • 105
  • 166
1

Is one of the following modules close to your needs?

units

>>> from units import unit
>>> metre = unit('m')
>>> second = unit('s')
>>> print(metre(10) / second(2))
5 m / s
>>> print(metre(10) ** 3)
1000 m * m * m

magnitude

>>> from magnitude import mg
>>> print mg(10, 'm/s') ** 2
100.0000 m2 / s2 
>>> print (mg(10, 'm') * 2 / (10, 'm/s2')).sqrt()
1.4142 s 
>>> tsq = mg(10, 'm') * 2 / (10, 'm/s2')
>>> print tsq ** 0.5
1.4142 s
>>> print mg(1, "lightyear") / mg(1, "c")
>>> 31557600.0000 s
>>> y = mg(1, "lightyear") / (1, "c") 
>>> print y.ounit("year")
1.0000 year
>>> print y.ounit('day')
>>> print yd
365.2500 day
>>> power = mg(100, 'kg') * (2, 'gravity') * (5, 'km/h')
>>> print power
2724.0694 m2 kg / s3 
>>> print power.ounit('mW')
2724069.4444 mW
>>> print power.ounit('kW')
2.7241 kW

Unum (documentation in the source)

quantities (documentation)

tzot
  • 92,761
  • 29
  • 141
  • 204
  • [`dateutil.parser.parse()`](https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.parse) is a function that tries to correctly parse arbitrary strings into the date and time they represent. `parse()` is (as the name implies...) not related to manipulating the dates. The libraries you linked to are strictly about manipulating units. – Boris Verkhovskiy Jan 27 '21 at 22:32