From 8eccbe529375458a6d127d33e27f64a188eaf170 Mon Sep 17 00:00:00 2001 From: Hernan Grecco Date: Thu, 17 Nov 2016 23:17:20 -0300 Subject: [PATCH] Fixed compatibility in Python2 --- pint/compat/meta.py | 25 +++++++++++++++++++++++++ pint/registry.py | 19 ++----------------- 2 files changed, 27 insertions(+), 17 deletions(-) create mode 100644 pint/compat/meta.py diff --git a/pint/compat/meta.py b/pint/compat/meta.py new file mode 100644 index 0000000..0036870 --- /dev/null +++ b/pint/compat/meta.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" + pint.compat.meta + ~~~~~~~~~~~~~~~~ + + Compatibility layer. + + :copyright: 2016 by Pint Authors, see AUTHORS for more details. + :license: BSD, see LICENSE for more details. +""" + + +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + # This requires a bit of explanation: the basic idea is to make a dummy + # metaclass for one level of class instantiation that replaces itself with + # the actual metaclass. + + # Taken from six + + class metaclass(meta): + + def __new__(cls, name, this_bases, d): + return meta(name, bases, d) + return type.__new__(metaclass, 'temporary_class', (), {}) \ No newline at end of file diff --git a/pint/registry.py b/pint/registry.py index a2bae61..efb2665 100644 --- a/pint/registry.py +++ b/pint/registry.py @@ -51,7 +51,7 @@ from .util import (logger, pi_theorem, solve_dependencies, ParserHelper, find_shortest_path, UnitsContainer, _is_dim, to_units_container, SourceIterator) -from .compat import tokenizer, string_types +from .compat import tokenizer, string_types, meta from .definitions import (Definition, UnitDefinition, PrefixDefinition, DimensionDefinition) from .converters import ScaleConverter @@ -64,21 +64,6 @@ from . import systems _BLOCK_RE = re.compile(r' |\(') -def _with_metaclass(meta, *bases): - """Create a base class with a metaclass.""" - # This requires a bit of explanation: the basic idea is to make a dummy - # metaclass for one level of class instantiation that replaces itself with - # the actual metaclass. - - # Taken from six - - class metaclass(meta): - - def __new__(cls, name, this_bases, d): - return meta(name, bases, d) - return type.__new__(metaclass, 'temporary_class', (), {}) - - class _Meta(type): """This is just to call after_init at the right time instead of asking the developer to do it when subclassing. @@ -90,7 +75,7 @@ class _Meta(type): return obj -class BaseRegistry(_with_metaclass(_Meta)): +class BaseRegistry(meta.with_metaclass(_Meta)): """Base class for all registries. Capabilities: