距离字符串的dateutil.parser.parse()?

时间:2010-10-15 15:35:10

标签: python

任何人都知道python包是距离字符串的dateutil吗?如果出现类似以下内容的东西会很棒:

>>> 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

关键的不是转换,而是能够传递代表某种测量的字符串,并且在不知道字符串中的测量值的情况下返回另一个您正在选择的测量值的浮点数。 / p>

什么吗?

3 个答案:

答案 0 :(得分:4)

我是为你写的,但你可以随意扩展它:

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')

一些例子:

>>> 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

编辑:添加了setter

答案 1 :(得分:3)

如果你知道你需要哪些单位(而且不需要查找数百个不同的单位),你可以很容易地自己实现。这是一个例子:

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

为您处理字符串读取。然后,您可以使用任何单位转换库。

答案 2 :(得分:2)

以下模块之一是否符合您的需求?

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(来源文档)

quantitiesdocumentation