Is it possible to sort the units of python "units"?

I use the Python "units" package (http://pypi.python.org/pypi/units/) and I ran into some problems when trying to sort them. I tried to weld it to the simplest case, to try to figure out what was going on. Here is my simple test:

from units import unit, named_unit
from units.predefined import define_units
from units.compatibility import compatible
from units.registry import REGISTRY

a = unit('m')
a_p = pickle.dumps(a)
a_up = pickle.loads(a_p)

logging.info(repr(unit('m')))
logging.info(repr(a))
logging.info(repr(a_up))

logging.info(a.is_si())
logging.info(a_up.is_si())

logging.info( compatible(a,a_up) )
logging.info(a(10) + a_up(10))

The result that I see at startup is:

LeafUnit('m', True)
LeafUnit('m', True)
LeafUnit('m', True)
True
True
False
IncompatibleUnitsError

I would understand if their herbs broke, if not for the fact that repr () returns the same results for them. What am I missing?

It uses the v0.04 unit package, and the Google App Engine 1.4 SDK 1

+3
source share
2 answers

, , , , , , equivlent.

, , , , , units.compatibility.compatible , , LeafUnit __eq__, ( python ).

, , ( ..), . , , , , (, , )

- unit.abstract.AbstractUnit __eq__:

AbstractUnit.__eq__ = lambda self, other: repr(self)==repr(other)

, , , , . () "".

+2

, pickle , , __reduce__() copy_reg.dispatch_table

import copy_reg

from units import LeafUnit

def leafunit_reduce(self):
    return LeafUnit, (self.specifier, self.is_si())

copy_reg.pickle(LeafUnit, leafunit_reduce)
0

Source: https://habr.com/ru/post/1783171/


All Articles