summaryrefslogtreecommitdiff
path: root/pint/registry.py
blob: ad1e2c76cfbbc33cb1c0ba458abf916c5e67c0c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
# -*- coding: utf-8 -*-
"""
    pint.registry
    ~~~~~~~~~~~~~

    Defines the Registry, a class to contain units and their relations.

    The module actually defines 5 registries with different capabilites:

    - BaseRegistry: Basic unit definition and querying.
                    Conversion between multiplicative units.

    - NonMultiplicativeRegistry: Conversion between non multiplicative (offset) units.
                                 (e.g. Temperature)
      * Inherits from BaseRegistry

    - ContextRegisty: Conversion between units with different dimenstions according
                      to previously established relations (contexts).
                      (e.g. in the spectroscopy, conversion between frequency and energy is possible)
      * Inherits from BaseRegistry

    - SystemRegistry: Group unit and changing of base units.
                      (e.g. in MKS, meter, kilogram and second are base units.)

      * Inherits from BaseRegistry

    - UnitRegistry: Combine all previous capabilities, it is exposed by Pint.

    :copyright: 2016 by Pint Authors, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

from __future__ import division, unicode_literals, print_function, absolute_import

import os
import re
import math
import functools
import itertools
import pkg_resources
from decimal import Decimal
from fractions import Fraction
from contextlib import contextmanager, closing
from io import open, StringIO
from collections import defaultdict
from tokenize import NUMBER, NAME

from . import registry_helpers
from .context import Context, ContextChain
from .util import (logger, pi_theorem, solve_dependencies, ParserHelper,
                   string_preprocessor, find_connected_nodes,
                   find_shortest_path, UnitsContainer, _is_dim,
                   to_units_container, SourceIterator)

from .compat import tokenizer, string_types, meta
from .definitions import (Definition, UnitDefinition, PrefixDefinition,
                          DimensionDefinition)
from .converters import ScaleConverter
from .errors import (DimensionalityError, UndefinedUnitError,
                     DefinitionSyntaxError, RedefinitionError)

from .pint_eval import build_eval_tree
from . import systems

_BLOCK_RE = re.compile(r' |\(')


class _Meta(type):
    """This is just to call after_init at the right time
    instead of asking the developer to do it when subclassing.
    """

    def __call__(self, *args, **kwargs):
        obj = super(_Meta, self).__call__(*args, **kwargs)
        obj._after_init()
        return obj

class RegistryCache():
    """Cache to speed up unit registries"""

    def __init__(self):
        #: Maps dimensionality (UnitsContainer) to Units (str)
        self.dimensional_equivalents = dict()

        #: Maps dimensionality (UnitsContainer) to Dimensionality (UnitsContainer)
        self.root_units = dict()

        #: Maps dimensionality (UnitsContainer) to Units (UnitsContainer)
        self.dimensionality = dict()

        #: Cache the unit name associated to user input. ('mV' -> 'millivolt')
        self.parse_unit = dict()

class BaseRegistry(meta.with_metaclass(_Meta)):
    """Base class for all registries.

    Capabilities:
    - Register units, prefixes, and dimensions, and their relations.
    - Convert between units.
    - Find dimensionality of a unit.
    - Parse units with prefix and/or suffix.
    - Parse expressions.
    - Parse a definition file.
    - Allow extending the definition file parser by registering @ directives.

    :param filename: path of the units definition file to load or line iterable object.
                     Empty to load the default definition file.
                     None to leave the UnitRegistry empty.
    :type filename: str | None
    :param force_ndarray: convert any input, scalar or not to a numpy.ndarray.
    :param on_redefinition: action to take in case a unit is redefined.
                            'warn', 'raise', 'ignore'
    :type on_redefinition: str
    :param auto_reduce_dimensions: If True, reduce dimensionality on appropriate operations.
    """

    #: Map context prefix to function
    #: type: Dict[str, (SourceIterator -> None)]
    _parsers = None

    #: List to be used in addition of units when dir(registry) is called.
    #: Also used for autocompletion in IPython.
    _dir = ['Quantity', 'Unit', 'Measurement',
            'define', 'load_definitions',
            'get_name', 'get_symbol', 'get_dimensionality',
            'get_base_units', 'get_root_units',
            'parse_unit_name', 'parse_units', 'parse_expression',
            'convert']

    def __init__(self, filename='', force_ndarray=False, on_redefinition='warn', auto_reduce_dimensions=False):

        self._register_parsers()

        from .unit import build_unit_class
        self.Unit = build_unit_class(self)

        from .quantity import build_quantity_class
        self.Quantity = build_quantity_class(self, force_ndarray)

        from .measurement import build_measurement_class
        self.Measurement = build_measurement_class(self, force_ndarray)

        self._filename = filename

        #: Action to take in case a unit is redefined. 'warn', 'raise', 'ignore'
        self._on_redefinition = on_redefinition

        #: Determines if dimensionality should be reduced on appropriate operations.
        self.auto_reduce_dimensions = auto_reduce_dimensions

        #: Map between name (string) and value (string) of defaults stored in the definitions file.
        self._defaults = {}

        #: Map dimension name (string) to its definition (DimensionDefinition).
        self._dimensions = {}

        #: Map unit name (string) to its definition (UnitDefinition).
        #: Might contain prefixed units.
        self._units = {}

        #: Map unit name in lower case (string) to a set of unit names with the right case.
        #: Does not contain prefixed units.
        #: e.g: 'hz' - > set('Hz', )
        self._units_casei = defaultdict(set)

        #: Map prefix name (string) to its definition (PrefixDefinition).
        self._prefixes = {'': PrefixDefinition('', '', (), 1)}

        #: Map suffix name (string) to canonical , and unit alias to canonical unit name
        self._suffixes = {'': None, 's': ''}

        #: Map contexts to RegistryCache
        self._cache = RegistryCache()

        self._initialized = False

    def _after_init(self):
        """This should be called after all __init__
        """
        if self._filename == '':
            self.load_definitions('default_en.txt', True)
        elif self._filename is not None:
            self.load_definitions(self._filename)

        self.define(UnitDefinition('pi', 'π', (), ScaleConverter(math.pi)))

        self._build_cache()
        self._initialized = True

    def _register_parsers(self):
        self._register_parser('@defaults', self._parse_defaults)

    def _parse_defaults(self, ifile):
        """Loader for a @default section.

        :type ifile: SourceITerator
        """
        next(ifile)
        for lineno, part in ifile.block_iter():
            k, v = part.split('=')
            self._defaults[k.strip()] = v.strip()

    def __getattr__(self, item):
        if item[0] == '_':
            return super(BaseRegistry, self).__getattribute__(item)
        return self.Unit(item)

    def __getitem__(self, item):
        logger.warning('Calling the getitem method from a UnitRegistry is deprecated. '
                       'use `parse_expression` method or use the registry as a callable.')
        return self.parse_expression(item)

    def __dir__(self):
        return list(self._units.keys()) + self._dir

    @property
    def default_format(self):
        """Default formatting string for quantities.
        """
        return self.Quantity.default_format

    @default_format.setter
    def default_format(self, value):
        self.Unit.default_format = value
        self.Quantity.default_format = value

    def define(self, definition):
        """Add unit to the registry.

        :param definition: a dimension, unit or prefix definition.
        :type definition: str | Definition
        """

        if isinstance(definition, string_types):
            for line in definition.split('\n'):
                self._define(Definition.from_string(line))
        else:
            self._define(definition)

    def _define(self, definition):
        """Add unit to the registry.

        This method defines only multiplicative units, converting any other type
        to `delta_` units.

        :param definition: a dimension, unit or prefix definition.
        :type definition: Definition
        :return: Definition instance, case sensitive unit dict, case insensitive unit dict.
        :rtype: Definition, dict, dict
        """

        if isinstance(definition, DimensionDefinition):
            d, di = self._dimensions, None

        elif isinstance(definition, UnitDefinition):
            d, di = self._units, self._units_casei

            # For a base units, we need to define the related dimension
            # (making sure there is only one to define)
            if definition.is_base:
                for dimension in definition.reference.keys():
                    if dimension in self._dimensions:
                        if dimension != '[]':
                            raise DefinitionSyntaxError('only one unit per dimension can be a base unit.')
                        continue

                    self.define(DimensionDefinition(dimension, '', (), None, is_base=True))

        elif isinstance(definition, PrefixDefinition):
            d, di = self._prefixes, None

        else:
            raise TypeError('{} is not a valid definition.'.format(definition))

        # define "delta_" units for units with an offset
        if getattr(definition.converter, "offset", 0.0) != 0.0:

            if definition.name.startswith('['):
                d_name = '[delta_' + definition.name[1:]
            else:
                d_name = 'delta_' + definition.name

            if definition.symbol:
                d_symbol = 'Δ' + definition.symbol
            else:
                d_symbol = None

            d_aliases = tuple('Δ' + alias for alias in definition.aliases)

            d_reference = UnitsContainer(dict((ref, value)
                                         for ref, value in definition.reference.items()))

            d_def = UnitDefinition(d_name, d_symbol, d_aliases,
                                   ScaleConverter(definition.converter.scale),
                                   d_reference, definition.is_base)
        else:
            d_def = definition

        self._define_adder(d_def, d, di)

        return definition, d, di

    def _define_adder(self, definition, unit_dict, casei_unit_dict):
        """Helper function to store a definition in the internal dictionaries.
        It stores the definition under its name, symbol and aliases.
        """
        self._define_single_adder(definition.name, definition, unit_dict, casei_unit_dict)

        if definition.has_symbol:
            self._define_single_adder(definition.symbol, definition, unit_dict, casei_unit_dict)

        for alias in definition.aliases:
            if ' ' in alias:
                logger.warn('Alias cannot contain a space: ' + alias)

            self._define_single_adder(alias, definition, unit_dict, casei_unit_dict)

    def _define_single_adder(self, key, value, unit_dict, casei_unit_dict):
        """Helper function to store a definition in the internal dictionaries.

        It warns or raise error on redefinition.
        """
        if key in unit_dict:
            if self._on_redefinition == 'raise':
                raise RedefinitionError(key, type(value))
            elif self._on_redefinition == 'warn':
                logger.warning("Redefining '%s' (%s)" % (key, type(value)))

        unit_dict[key] = value
        if casei_unit_dict is not None:
            casei_unit_dict[key.lower()].add(key)

    def _register_parser(self, prefix, parserfunc):
        """Register a loader for a given @ directive..

        :param prefix: string identifying the section (e.g. @context)
        :param parserfunc: A function that is able to parse a Definition section.
        :type parserfunc: SourceIterator -> None
        """
        if self._parsers is None:
            self._parsers = dict()

        if prefix and prefix[0] == '@':
            self._parsers[prefix] = parserfunc
        else:
            raise ValueError("Prefix directives must start with '@'")

    def load_definitions(self, file, is_resource=False):
        """Add units and prefixes defined in a definition text file.

        :param file: can be a filename or a line iterable.
        :param is_resource: used to indicate that the file is a resource file
                            and therefore should be loaded from the package.
        """
        # Permit both filenames and line-iterables
        if isinstance(file, string_types):
            try:
                if is_resource:
                    with closing(pkg_resources.resource_stream(__name__, file)) as fp:
                        rbytes = fp.read()
                    return self.load_definitions(StringIO(rbytes.decode('utf-8')), is_resource)
                else:
                    with open(file, encoding='utf-8') as fp:
                        return self.load_definitions(fp, is_resource)
            except (RedefinitionError, DefinitionSyntaxError) as e:
                if e.filename is None:
                    e.filename = file
                raise e
            except Exception as e:
                msg = getattr(e, 'message', '') or str(e)
                raise ValueError('While opening {}\n{}'.format(file, msg))

        ifile = SourceIterator(file)
        for no, line in ifile:
            if line and line[0] == '@':
                if line.startswith('@import'):
                    if is_resource:
                        path = line[7:].strip()
                    else:
                        try:
                            path = os.path.dirname(file.name)
                        except AttributeError:
                            path = os.getcwd()
                        path = os.path.join(path, os.path.normpath(line[7:].strip()))
                    self.load_definitions(path, is_resource)
                else:
                    parts = _BLOCK_RE.split(line)

                    loader = self._parsers.get(parts[0], None) if self._parsers else None

                    if loader is None:
                        raise DefinitionSyntaxError('Unknown directive %s' % line, lineno=no)

                    try:
                        loader(ifile)
                    except DefinitionSyntaxError as ex:
                        if ex.lineno is None:
                            ex.lineno = no
                        raise ex
            else:
                try:
                    self.define(Definition.from_string(line))
                except DefinitionSyntaxError as ex:
                    if ex.lineno is None:
                        ex.lineno = no
                    raise ex
                except Exception as ex:
                    logger.error("In line {}, cannot add '{}' {}".format(no, line, ex))

    def _build_cache(self):
        """Build a cache of dimensionality and base units.
        """
        self._cache = RegistryCache()
        root_units = self._cache.root_units
        dimensionality = self._cache.dimensionality
        dimensional_equivalents = self._cache.dimensional_equivalents

        deps = dict((name, set(definition.reference.keys() if definition.reference else {}))
                    for name, definition in self._units.items())

        for unit_names in solve_dependencies(deps):
            for unit_name in unit_names:
                if '[' in unit_name:
                    continue
                parsed_names = tuple(self.parse_unit_name(unit_name))
                _prefix = None
                if parsed_names:
                    _prefix, base_name, _suffix = parsed_names[0]
                else:
                    base_name = unit_name
                prefixed = True if _prefix else False
                try:
                    uc = ParserHelper.from_word(base_name)

                    bu = self._get_root_units(uc)
                    di = self._get_dimensionality(uc)

                    root_units[uc] = bu
                    dimensionality[uc] = di

                    if not prefixed:
                        if di not in dimensional_equivalents:
                            dimensional_equivalents[di] = set()

                        dimensional_equivalents[di].add(self._units[base_name]._name)

                except Exception as e:
                    logger.warning('Could not resolve {0}: {1!r}'.format(unit_name, e))

    def _dedup_candidates(self, candidates):
        """Given a list of unit triplets (prefix, name, suffix),
        remove those with different names but equal value.

            e.g. ('kilo', 'gram', '') and ('', 'kilogram', '')
        """
        candidates = tuple(candidates)
        if len(candidates) < 2:
            return candidates

        unique = [candidates[0]]
        for c in candidates[2:]:
            for u in unique:
                if c == u:
                    break
            else:
                unique.append(c)

        return tuple(unique)

    def get_name(self, name_or_alias, case_sensitive=True):
        """Return the canonical name of a unit.
        """

        if name_or_alias == 'dimensionless':
            return ''

        try:
            return self._units[name_or_alias]._name
        except KeyError:
            pass

        candidates = self._dedup_candidates(self.parse_unit_name(name_or_alias, case_sensitive))
        if not candidates:
            raise UndefinedUnitError(name_or_alias)
        elif len(candidates) == 1:
            prefix, unit_name, _ = candidates[0]
        else:
            logger.warning('Parsing {} yield multiple results. '
                           'Options are: {}'.format(name_or_alias, candidates))
            prefix, unit_name, _ = candidates[0]

        if prefix:
            name = prefix + unit_name
            symbol = self.get_symbol(name)
            prefix_def = self._prefixes[prefix]
            self._units[name] = UnitDefinition(name, symbol, (), prefix_def.converter,
                                               UnitsContainer({unit_name: 1}))
            return prefix + unit_name

        return unit_name

    def get_symbol(self, name_or_alias):
        """Return the preferred alias for a unit
        """
        candidates = self._dedup_candidates(self.parse_unit_name(name_or_alias))
        if not candidates:
            raise UndefinedUnitError(name_or_alias)
        elif len(candidates) == 1:
            prefix, unit_name, _ = candidates[0]
        else:
            logger.warning('Parsing {0} yield multiple results. '
                           'Options are: {1!r}'.format(name_or_alias, candidates))
            prefix, unit_name, _ = candidates[0]

        return self._prefixes[prefix].symbol + self._units[unit_name].symbol

    def _get_symbol(self, name):
        return self._units[name].symbol

    def get_dimensionality(self, input_units):
        """Convert unit or dict of units or dimensions to a dict of base dimensions
        dimensions

        :param input_units:
        :return: dimensionality
        """
        input_units = to_units_container(input_units)

        return self._get_dimensionality(input_units)

    def _get_dimensionality(self, input_units):
        """ Convert a UnitsContainer to base dimensions.

        :param input_units:
        :return: dimensionality
        """
        if not input_units:
            return UnitsContainer()

        cache = self._cache.dimensionality

        if input_units in cache:
            return cache[input_units]

        accumulator = defaultdict(float)
        self._get_dimensionality_recurse(input_units, 1.0, accumulator)

        if '[]' in accumulator:
            del accumulator['[]']

        dims = UnitsContainer(dict((k, v) for k, v in accumulator.items()
                                   if v != 0.0))

        cache[input_units] = dims

        return dims

    def _get_dimensionality_recurse(self, ref, exp, accumulator):
        for key in ref:
            exp2 = exp*ref[key]
            if _is_dim(key):
                reg = self._dimensions[key]
                if reg.is_base:
                    accumulator[key] += exp2
                elif reg.reference is not None:
                    self._get_dimensionality_recurse(reg.reference, exp2, accumulator)
            else:
                reg = self._units[self.get_name(key)]
                if reg.reference is not None:
                    self._get_dimensionality_recurse(reg.reference, exp2, accumulator)

    def _get_dimensionality_ratio(self, unit1, unit2):
        """ Get the exponential ratio between two units, i.e. solve unit2 = unit1**x for x.
        :param unit1: first unit
        :type unit1: UnitsContainer compatible (str, Unit, UnitsContainer, dict)
        :param unit2: second unit
        :type unit2: UnitsContainer compatible (str, Unit, UnitsContainer, dict)
        :returns: exponential proportionality or None if the units cannot be converted
        """
        #shortcut in case of equal units
        if unit1 == unit2:
            return 1

        dim1, dim2 = (self.get_dimensionality(unit) for unit in (unit1, unit2))
        if not dim1 or not dim2 or dim1.keys() != dim2.keys(): #not comparable
            return None

        ratios = (dim2[key]/val for key, val in dim1.items())
        first = next(ratios)
        if all(r == first for r in ratios): #all are same, we're good
            return first
        return None

    def get_root_units(self, input_units, check_nonmult=True):
        """Convert unit or dict of units to the root units.

        If any unit is non multiplicative and check_converter is True,
        then None is returned as the multiplicative factor.

        :param input_units: units
        :type input_units: UnitsContainer or str
        :param check_nonmult: if True, None will be returned as the
                              multiplicative factor if a non-multiplicative
                              units is found in the final Units.
        :return: multiplicative factor, base units
        """
        input_units = to_units_container(input_units)

        f, units = self._get_root_units(input_units, check_nonmult)

        return f, self.Unit(units)

    def _get_root_units(self, input_units, check_nonmult=True):
        """Convert unit or dict of units to the root units.

        If any unit is non multiplicative and check_converter is True,
        then None is returned as the multiplicative factor.

        :param input_units: units
        :type input_units: UnitsContainer or dict
        :param check_nonmult: if True, None will be returned as the
                              multiplicative factor if a non-multiplicative
                              units is found in the final Units.
        :return: multiplicative factor, base units
        """
        if not input_units:
            return 1., UnitsContainer()

        cache = self._cache.root_units

        # The cache is only done for check_nonmult=True
        if check_nonmult and input_units in cache:
            return cache[input_units]

        accumulators = [1., defaultdict(float)]
        self._get_root_units_recurse(input_units, 1.0, accumulators)

        factor = accumulators[0]
        units = UnitsContainer(dict((k, v) for k, v in accumulators[1].items()
                                    if v != 0.))

        # Check if any of the final units is non multiplicative and return None instead.
        if check_nonmult:
            for unit in units.keys():
                if not self._units[unit].converter.is_multiplicative:
                    return None, units

        if check_nonmult:
            cache[input_units] = factor, units

        return factor, units

    def get_base_units(self, input_units, check_nonmult=True, system=None):
        """Convert unit or dict of units to the base units.

        If any unit is non multiplicative and check_converter is True,
        then None is returned as the multiplicative factor.

        :param input_units: units
        :type input_units: UnitsContainer or str
        :param check_nonmult: if True, None will be returned as the
                              multiplicative factor if a non-multiplicative
                              units is found in the final Units.
        :return: multiplicative factor, base units
        """

        return self.get_root_units(input_units, check_nonmult)

    def _get_root_units_recurse(self, ref, exp, accumulators):
        for key in sorted(ref):
            exp2 = exp*ref[key]
            key = self.get_name(key)
            reg = self._units[key]
            if reg.is_base:
                accumulators[1][key] += exp2
            else:
                accumulators[0] *= reg._converter.scale ** exp2
                if reg.reference is not None:
                    self._get_root_units_recurse(reg.reference, exp2,
                                                 accumulators)

    def get_compatible_units(self, input_units, group_or_system=None):
        """
        """
        input_units = to_units_container(input_units)

        equiv = self._get_compatible_units(input_units, group_or_system)

        return frozenset(self.Unit(eq) for eq in equiv)

    def _get_compatible_units(self, input_units, group_or_system):
        """
        """
        if not input_units:
            return frozenset()

        src_dim = self._get_dimensionality(input_units)

        ret = self._cache.dimensional_equivalents[src_dim]

        return ret

    def convert(self, value, src, dst, inplace=False):
        """Convert value from some source to destination units.

        :param value: value
        :param src: source units.
        :type src: Quantity or str
        :param dst: destination units.
        :type dst: Quantity or str

        :return: converted value
        """
        src = to_units_container(src, self)

        dst = to_units_container(dst, self)

        if src == dst:
            return value

        return self._convert(value, src, dst, inplace)

    def _convert(self, value, src, dst, inplace=False, check_dimensionality=True):
        """Convert value from some source to destination units.

        :param value: value
        :param src: source units.
        :type src: UnitsContainer
        :param dst: destination units.
        :type dst: UnitsContainer

        :return: converted value
        """

        if check_dimensionality:

            src_dim = self._get_dimensionality(src)
            dst_dim = self._get_dimensionality(dst)

            # If the source and destination dimensionality are different,
            # then the conversion cannot be performed.
            if src_dim != dst_dim:
                raise DimensionalityError(src, dst, src_dim, dst_dim)

        # Here src and dst have only multiplicative units left. Thus we can
        # convert with a factor.
        factor, units = self._get_root_units(src / dst)

        # factor is type float and if our magnitude is type Decimal then
        # must first convert to Decimal before we can '*' the values
        if isinstance(value, Decimal):
            factor = Decimal(str(factor))
        elif isinstance(value, Fraction):
            factor = Fraction(str(factor))

        if inplace:
            value *= factor
        else:
            value = value * factor

        return value

    def parse_unit_name(self, unit_name, case_sensitive=True):
        """Parse a unit to identify prefix, unit name and suffix
        by walking the list of prefix and suffix.

        :rtype: (str, str, str)
        """
        stw = unit_name.startswith
        edw = unit_name.endswith
        for suffix, prefix in itertools.product(self._suffixes, self._prefixes):
            if stw(prefix) and edw(suffix):
                name = unit_name[len(prefix):]
                if suffix:
                    name = name[:-len(suffix)]
                    if len(name) == 1:
                        continue
                if case_sensitive:
                    if name in self._units:
                        yield (self._prefixes[prefix].name,
                               self._units[name].name,
                               self._suffixes[suffix])
                else:
                    for real_name in self._units_casei.get(name.lower(), ()):
                        yield (self._prefixes[prefix].name,
                               self._units[real_name].name,
                               self._suffixes[suffix])

    def parse_units(self, input_string, as_delta=None):
        """Parse a units expression and returns a UnitContainer with
        the canonical names.

        The expression can only contain products, ratios and powers of units.

        :param as_delta: if the expression has multiple units, the parser will
                         interpret non multiplicative units as their `delta_` counterparts.

        :raises:
            :class:`pint.UndefinedUnitError` if a unit is not in the registry
            :class:`ValueError` if the expression is invalid.
        """
        units = self._parse_units(input_string, as_delta)
        return self.Unit(units)

    def _parse_units(self, input_string, as_delta=None):
        """
        """
        if as_delta is None:
            as_delta = True

        cache = self._cache.parse_unit

        if as_delta and input_string in cache:
            return cache[input_string]

        if not input_string:
            return UnitsContainer()

        # Sanitize input_string with whitespaces.
        input_string = input_string.strip()

        units = ParserHelper.from_string(input_string)
        if units.scale != 1:
            raise ValueError('Unit expression cannot have a scaling factor.')

        ret = {}
        many = len(units) > 1
        for name in units:
            cname = self.get_name(name)
            value = units[name]
            if not cname:
                continue
            if as_delta and (many or (not many and value != 1)):
                definition = self._units[cname]
                if not definition.is_multiplicative:
                    cname = 'delta_' + cname
            ret[cname] = value

        ret = UnitsContainer(ret)

        if as_delta:
            cache[input_string] = ret

        return ret

    def _eval_token(self, token, case_sensitive=True, use_decimal=False, **values):
        token_type = token[0]
        token_text = token[1]
        if token_type == NAME:
            if token_text == 'pi':
                return self.Quantity(math.pi)
            elif token_text == 'dimensionless':
                return 1 * self.dimensionless
            elif token_text in values:
                return self.Quantity(values[token_text])
            else:
                return self.Quantity(1, UnitsContainer({self.get_name(token_text,
                                                                      case_sensitive=case_sensitive) : 1}))
        elif token_type == NUMBER:
            return ParserHelper.eval_token(token, use_decimal=use_decimal)
        else:
            raise Exception('unknown token type')

    def parse_expression(self, input_string, case_sensitive=True, use_decimal=False, **values):
        """Parse a mathematical expression including units and return a quantity object.

        Numerical constants can be specified as keyword arguments and will take precedence
        over the names defined in the registry.
        """

        if not input_string:
            return self.Quantity(1)

        input_string = string_preprocessor(input_string)
        gen = tokenizer(input_string)

        return build_eval_tree(gen).evaluate(lambda x: self._eval_token(x,
                                                                        case_sensitive=case_sensitive,
                                                                        use_decimal=use_decimal,
                                                                        **values))

    __call__ = parse_expression


class NonMultiplicativeRegistry(BaseRegistry):
    """Handle of non multiplicative units (e.g. Temperature).

    Capabilities:
    - Register non-multiplicative units and their relations.
    - Convert between non-multiplicative units.

    :param default_as_delta: If True, non-multiplicative units are interpreted as
                             their *delta* counterparts in multiplications.
    :param autoconvert_offset_to_baseunit: If True, non-multiplicative units are
                                           converted to base units in multiplications.
    """

    def __init__(self, default_as_delta=True, autoconvert_offset_to_baseunit=False, **kwargs):
        super(NonMultiplicativeRegistry, self).__init__(**kwargs)

        #: When performing a multiplication of units, interpret
        #: non-multiplicative units as their *delta* counterparts.
        self.default_as_delta = default_as_delta

        # Determines if quantities with offset units are converted to their
        # base units on multiplication and division.
        self.autoconvert_offset_to_baseunit = autoconvert_offset_to_baseunit

    def _parse_units(self, input_string, as_delta=None):
        """
        """
        if as_delta is None:
            as_delta = self.default_as_delta

        return super(NonMultiplicativeRegistry, self)._parse_units(input_string, as_delta)

    def _define(self, definition):
        """Add unit to the registry.

        In addition to what is done by the BaseRegistry,
        registers also non-multiplicative units.

        :param definition: a dimension, unit or prefix definition.
        :type definition: str | Definition
        :return: Definition instance, case sensitive unit dict, case insensitive unit dict.
        :rtype: Definition, dict, dict
        """

        definition, d, di = super(NonMultiplicativeRegistry, self)._define(definition)

        # define additional units for units with an offset
        if getattr(definition.converter, "offset", 0.0) != 0.0:
            self._define_adder(definition, d, di)

        return definition, d, di

    def _is_multiplicative(self, u):
        if u in self._units:
            return self._units[u].is_multiplicative

        # If the unit is not in the registry might be because it is not
        # registered with its prefixed version.
        # TODO: Might be better to register them.
        l = self._dedup_candidates(self.parse_unit_name(u))
        try:
            u = l[0][1]
            return self._units[u].is_multiplicative
        except KeyError:
            raise UndefinedUnitError(u)

    def _validate_and_extract(self, units):

        nonmult_units = [(u, e) for u, e in units.items()
                         if not self._is_multiplicative(u)]

        # Let's validate source offset units
        if len(nonmult_units) > 1:
            # More than one src offset unit is not allowed
            raise ValueError('more than one offset unit.')

        elif len(nonmult_units) == 1:
            # A single src offset unit is present. Extract it
            # But check that:
            # - the exponent is 1
            # - is not used in multiplicative context
            nonmult_unit, exponent = nonmult_units.pop()

            if exponent != 1:
                raise ValueError('offset units in higher order.')

            if len(units) > 1 and not self.autoconvert_offset_to_baseunit:
                raise ValueError('offset unit used in multiplicative context.')

            return nonmult_unit

        return None

    def _convert(self, value, src, dst, inplace=False):
        """Convert value from some source to destination units.

        In addition to what is done by the BaseRegistry,
        converts between non-multiplicative units.

        :param value: value
        :param src: source units.
        :type src: UnitsContainer
        :param dst: destination units.
        :type dst: UnitsContainer

        :return: converted value
        """

        # Conversion needs to consider if non-multiplicative (AKA offset
        # units) are involved. Conversion is only possible if src and dst
        # have at most one offset unit per dimension. Other rules are applied
        # by validate and extract.
        try:
            src_offset_unit = self._validate_and_extract(src)
        except ValueError as ex:
            raise DimensionalityError(src, dst, extra_msg=' - In source units, %s ' % ex)

        try:
            dst_offset_unit = self._validate_and_extract(dst)
        except ValueError as ex:
            raise DimensionalityError(src, dst, extra_msg=' - In destination units, %s ' % ex)

        if not (src_offset_unit or dst_offset_unit):
            return super(NonMultiplicativeRegistry, self)._convert(value, src, dst, inplace)

        src_dim = self._get_dimensionality(src)
        dst_dim = self._get_dimensionality(dst)

        # If the source and destination dimensionality are different,
        # then the conversion cannot be performed.
        if src_dim != dst_dim:
            raise DimensionalityError(src, dst, src_dim, dst_dim)

        # clean src from offset units by converting to reference
        if src_offset_unit:
            value = self._units[src_offset_unit].converter.to_reference(value, inplace)

        src = src.remove([src_offset_unit])

        # clean dst units from offset units
        dst = dst.remove([dst_offset_unit])

        # Convert non multiplicative units to the dst.
        value = super(NonMultiplicativeRegistry, self)._convert(value, src, dst, inplace, False)

        # Finally convert to offset units specified in destination
        if dst_offset_unit:
            value = self._units[dst_offset_unit].converter.from_reference(value, inplace)

        return value


class ContextRegistry(BaseRegistry):
    """Handle of Contexts.

    Conversion between units with different dimenstions according
    to previously established relations (contexts).
    (e.g. in the spectroscopy, conversion between frequency and energy is possible)

    Capabilities:
    - Register contexts.
    - Enable and disable contexts.
    - Parse @context directive.

    """

    def __init__(self, **kwargs):
        super(ContextRegistry, self).__init__(**kwargs)

        #: Map context name (string) or abbreviation to context.
        self._contexts = {}

        #: Stores active contexts.
        self._active_ctx = ContextChain()

        #: Map context chain to cache
        self._caches = { self._active_ctx: self._cache }

    def _register_parsers(self):
        super(ContextRegistry, self)._register_parsers()
        self._register_parser('@context', self._parse_context)

    def _parse_context(self, ifile):
        try:
            self.add_context(Context.from_lines(ifile.block_iter(),
                                                self.get_dimensionality))
        except KeyError as e:
            raise DefinitionSyntaxError('unknown dimension {} in context'.format(str(e)))

    def add_context(self, context):
        """Add a context object to the registry.

        The context will be accessible by its name and aliases.

        Notice that this method will NOT enable the context. Use `enable_contexts`.
        """
        if context.name in self._contexts:
            logger.warning('The name %s was already registered for another context.',
                           context.name)
        self._contexts[context.name] = context
        for alias in context.aliases:
            if alias in self._contexts:
                logger.warning('The name %s was already registered for another context',
                               context.name)
            self._contexts[alias] = context

    def remove_context(self, name_or_alias):
        """Remove a context from the registry and return it.

        Notice that this methods will not disable the context. Use `disable_contexts`.
        """
        context = self._contexts[name_or_alias]

        del self._contexts[context.name]
        for alias in context.aliases:
            del self._contexts[alias]

        return context

    def _build_cache(self):
        """"""
        cache = self._caches.get(self._active_ctx)
        if cache is None:
            super()._build_cache()
            self._caches[self._active_ctx] = self._cache
        else:
            self._cache = cache

    def enable_contexts(self, *names_or_contexts, **kwargs):
        """Enable contexts provided by name or by object.

        :param names_or_contexts: sequence of the contexts or contexts names/alias
        :param kwargs: keyword arguments for the context
        """

        # If present, copy the defaults from the containing contexts
        if self._active_ctx.defaults:
            kwargs = dict(self._active_ctx.defaults, **kwargs)

        # For each name, we first find the corresponding context
        ctxs = list((self._contexts[name] if isinstance(name, string_types) else name)
                    for name in names_or_contexts)

        # Check if the contexts have been checked first, if not we make sure
        # that dimensions are expressed in terms of base dimensions.
        for ctx in ctxs:
            if getattr(ctx, '_checked', False):
                continue
            for (src, dst), func in ctx.funcs.items():
                src_ = self._get_dimensionality(src)
                dst_ = self._get_dimensionality(dst)
                if src != src_ or dst != dst_:
                    ctx.remove_transformation(src, dst)
                    ctx.add_transformation(src_, dst_, func)
            ctx._checked = True

        # and create a new one with the new defaults.
        ctxs = tuple(Context.from_context(ctx, **kwargs)
                     for ctx in ctxs)

        # Finally we add them to the active context.
        self._active_ctx.insert_contexts(*ctxs)
        self._build_cache()

    def disable_contexts(self, n=None):
        """Disable the last n enabled contexts.
        """
        if n is None:
            n = len(self._contexts)
        self._active_ctx.remove_contexts(n)
        self._build_cache()

    @contextmanager
    def context(self, *names, **kwargs):
        """Used as a context manager, this function enables to activate a context
        which is removed after usage.

        :param names: name of the context.
        :param kwargs: keyword arguments for the contexts.

        Context are called by their name::

            >>> with ureg.context('one'):
            ...     pass

        If the context has an argument, you can specify its value as a keyword
        argument::

            >>> with ureg.context('one', n=1):
            ...     pass

        Multiple contexts can be entered in single call:

            >>> with ureg.context('one', 'two', n=1):
            ...     pass

        or nested allowing you to give different values to the same keyword argument::

            >>> with ureg.context('one', n=1):
            ...     with ureg.context('two', n=2):
            ...         pass

        A nested context inherits the defaults from the containing context::

            >>> with ureg.context('one', n=1):
            ...     with ureg.context('two'): # Here n takes the value of the upper context
            ...         pass

        """

        # Enable the contexts.
        self.enable_contexts(*names, **kwargs)

        try:
            # After adding the context and rebuilding the graph, the registry
            # is ready to use.
            yield self
        finally:
            # Upon leaving the with statement,
            # the added contexts are removed from the active one.
            self.disable_contexts(len(names))

    def with_context(self, name, **kw):
        """Decorator to wrap a function call in a Pint context.

        Use it to ensure that a certain context is active when
        calling a function::

            >>> @ureg.with_context('sp')
            ... def my_cool_fun(wavelenght):
            ...     print('This wavelength is equivalent to: %s', wavelength.to('terahertz'))


        :param names: name of the context.
        :param kwargs: keyword arguments for the contexts.
        :return: the wrapped function.
        """
        def decorator(func):
            assigned = tuple(attr for attr in functools.WRAPPER_ASSIGNMENTS if hasattr(func, attr))
            updated = tuple(attr for attr in functools.WRAPPER_UPDATES if hasattr(func, attr))

            @functools.wraps(func, assigned=assigned, updated=updated)
            def wrapper(*values, **kwargs):
                with self.context(name, **kw):
                    return func(*values, **kwargs)

            return wrapper

        return decorator

    def _convert(self, value, src, dst, inplace=False):
        """Convert value from some source to destination units.

        In addition to what is done by the BaseRegistry,
        converts between units with different dimensions by following
        transformation rules defined in the context.

        :param value: value
        :param src: source units.
        :type src: UnitsContainer
        :param dst: destination units.
        :type dst: UnitsContainer

        :return: converted value
        """

        # If there is an active context, we look for a path connecting source and
        # destination dimensionality. If it exists, we transform the source value
        # by applying sequentially each transformation of the path.
        if self._active_ctx:

            src_dim = self._get_dimensionality(src)
            dst_dim = self._get_dimensionality(dst)

            path = find_shortest_path(self._active_ctx.graph, src_dim, dst_dim)
            if path:
                src = self.Quantity(value, src)
                for a, b in zip(path[:-1], path[1:]):
                    src = self._active_ctx.transform(a, b, self, src)

                value, src = src._magnitude, src._units

        return super(ContextRegistry, self)._convert(value, src, dst, inplace)

    def _get_compatible_units(self, input_units, group_or_system):
        """
        """

        src_dim = self._get_dimensionality(input_units)

        ret = super(ContextRegistry, self)._get_compatible_units(input_units, group_or_system)

        if self._active_ctx:
            nodes = find_connected_nodes(self._active_ctx.graph, src_dim)
            if nodes:
                for node in nodes:
                    ret |= self._cache.dimensional_equivalents[node]

        return ret


class SystemRegistry(BaseRegistry):
    """Handle of Systems and Groups.

    Conversion between units with different dimenstions according
    to previously established relations (contexts).
    (e.g. in the spectroscopy, conversion between frequency and energy is possible)

    Capabilities:
    - Register systems and groups.
    - List systems
    - Get or get the default system.
    - Parse @system and @group directive.

    """

    def __init__(self, system=None, **kwargs):
        super(SystemRegistry, self).__init__(**kwargs)

        #: Map system name to system.
        #: :type: dict[ str | System]
        self._systems = {}

        #: Maps dimensionality (UnitsContainer) to Dimensionality (UnitsContainer)
        self._base_units_cache = dict()

        #: Map group name to group.
        #: :type: dict[ str | Group]
        self._groups = {}
        self.Group = systems.build_group_class(self)
        self._groups['root'] = self.Group('root')
        self.System = systems.build_system_class(self)

        self._default_system = system

    def _after_init(self):
        """After init function

        Create default group.
        Add all orphan units to it.
        Set default system.
        """
        super(SystemRegistry, self)._after_init()

        #: Copy units not defined in any group to the default group
        if 'group' in self._defaults:
            grp = self.get_group(self._defaults['group'], True)
            group_units = frozenset(
                [
                    member
                    for group in self._groups.values()
                    if group.name != "root"
                    for member in group.members
                ]
            )
            all_units = self.get_group("root", False).members
            grp.add_units(*(all_units - group_units))

        #: System name to be used by default.
        self._default_system = self._default_system or self._defaults.get('system', None)

    def _register_parsers(self):
        super(SystemRegistry, self)._register_parsers()
        self._register_parser('@group', self._parse_group)
        self._register_parser('@system', self._parse_system)

    def _parse_group(self, ifile):
        self.Group.from_lines(ifile.block_iter(), self.define)

    def _parse_system(self, ifile):
        self.System.from_lines(ifile.block_iter(), self.get_root_units)

    def get_group(self, name, create_if_needed=True):
        """Return a Group.

        :param name: Name of the group to be
        :param create_if_needed: Create a group if not Found. If False, raise an Exception.
        :return: Group
        """
        if name in self._groups:
            return self._groups[name]

        if not create_if_needed:
            raise ValueError('Unkown group %s' % name)

        return self.Group(name)

    @property
    def sys(self):
        return systems.Lister(self._systems)

    @property
    def default_system(self):
        return self._default_system

    @default_system.setter
    def default_system(self, name):
        if name:
            if name not in self._systems:
                raise ValueError('Unknown system %s' % name)

            self._base_units_cache = {}

        self._default_system = name

    def get_system(self, name, create_if_needed=True):
        """Return a Group.

        :param name: Name of the group to be
        :param create_if_needed: Create a group if not Found. If False, raise an Exception.
        :return: System
        """
        if name in self._systems:
            return self._systems[name]

        if not create_if_needed:
            raise ValueError('Unkown system %s' % name)

        return self.System(name)

    def _define(self, definition):

        # In addition to the what is done by the BaseRegistry,
        # this adds all units to the `root` group.

        definition, d, di = super(SystemRegistry, self)._define(definition)

        if isinstance(definition, UnitDefinition):
            # We add all units to the root group
            self.get_group('root').add_units(definition.name)

        return definition, d, di

    def get_base_units(self, input_units, check_nonmult=True, system=None):
        """Convert unit or dict of units to the base units.

        If any unit is non multiplicative and check_converter is True,
        then None is returned as the multiplicative factor.

        Unlike BaseRegistry, in this registry root_units might be different
        from base_units

        :param input_units: units
        :type input_units: UnitsContainer or str
        :param check_nonmult: if True, None will be returned as the
                              multiplicative factor if a non-multiplicative
                              units is found in the final Units.
        :return: multiplicative factor, base units
        """

        input_units = to_units_container(input_units)

        f, units = self._get_base_units(input_units, check_nonmult, system)

        return f, self.Unit(units)

    def _get_base_units(self, input_units, check_nonmult=True, system=None):

        if system is None:
            system = self._default_system

        # The cache is only done for check_nonmult=True and the current system.
        if check_nonmult and system == self._default_system and input_units in self._base_units_cache:
            return self._base_units_cache[input_units]

        factor, units = self.get_root_units(input_units, check_nonmult)

        if not system:
            return factor, units

        # This will not be necessary after integration with the registry
        # as it has a UnitsContainer intermediate
        units = to_units_container(units, self)

        destination_units = UnitsContainer()

        bu = self.get_system(system, False).base_units

        for unit, value in units.items():
            if unit in bu:
                new_unit = bu[unit]
                new_unit = to_units_container(new_unit, self)
                destination_units *= new_unit ** value
            else:
                destination_units *= UnitsContainer({unit: value})

        base_factor = self.convert(factor, units, destination_units)

        if check_nonmult:
            self._base_units_cache[input_units] = base_factor, destination_units

        return base_factor, destination_units

    def _get_compatible_units(self, input_units, group_or_system):
        """
        """

        if group_or_system is None:
            group_or_system = self._default_system

        ret = super(SystemRegistry, self)._get_compatible_units(input_units, group_or_system)

        if group_or_system:
            if group_or_system in self._systems:
                members = self._systems[group_or_system].members
            elif group_or_system in self._groups:
                members = self._groups[group_or_system].members
            else:
                raise ValueError("Unknown Group o System with name '%s'" % group_or_system)
            return frozenset(ret.intersection(members))

        return ret


class UnitRegistry(SystemRegistry, ContextRegistry, NonMultiplicativeRegistry):
    """The unit registry stores the definitions and relationships between units.

    :param filename: path of the units definition file to load or line-iterable object.
                     Empty to load the default definition file.
                     None to leave the UnitRegistry empty.
    :param force_ndarray: convert any input, scalar or not to a numpy.ndarray.
    :param default_as_delta: In the context of a multiplication of units, interpret
                             non-multiplicative units as their *delta* counterparts.
    :param autoconvert_offset_to_baseunit: If True converts offset units in quantites are
                                           converted to their base units in multiplicative
                                           context. If False no conversion happens.
    :param on_redefinition: action to take in case a unit is redefined.
                            'warn', 'raise', 'ignore'
    :type on_redefinition: str
    :param auto_reduce_dimensions: If True, reduce dimensionality on appropriate operations.
    """

    def __init__(self, filename='', force_ndarray=False, default_as_delta=True,
                 autoconvert_offset_to_baseunit=False,
                 on_redefinition='warn', system=None,
                 auto_reduce_dimensions=False):

        super(UnitRegistry, self).__init__(filename=filename, force_ndarray=force_ndarray,
                                           on_redefinition=on_redefinition,
                                           default_as_delta=default_as_delta,
                                           autoconvert_offset_to_baseunit=autoconvert_offset_to_baseunit,
                                           system=system,
                                           auto_reduce_dimensions=auto_reduce_dimensions)

    def pi_theorem(self, quantities):
        """Builds dimensionless quantities using the Buckingham π theorem
        :param quantities: mapping between variable name and units
        :type quantities: dict
        :return: a list of dimensionless quantities expressed as dicts
        """
        return pi_theorem(quantities, self)

    def setup_matplotlib(self, enable=True):
        """Set up handlers for matplotlib's unit support.
        :param enable: whether support should be enabled or disabled
        :type enable: bool
        """
        # Delays importing matplotlib until it's actually requested
        from .matplotlib import setup_matplotlib_handlers
        setup_matplotlib_handlers(self, enable)

    wraps = registry_helpers.wraps

    check = registry_helpers.check


class LazyRegistry(object):

    def __init__(self, args=None, kwargs=None):
        self.__dict__['params'] = args or (), kwargs or {}

    def __init(self):
        args, kwargs = self.__dict__['params']
        kwargs['on_redefinition'] = 'raise'
        self.__class__ = UnitRegistry
        self.__init__(*args, **kwargs)
        self._after_init()

    def __getattr__(self, item):
        if item == '_on_redefinition':
            return 'raise'
        self.__init()
        return getattr(self, item)

    def __setattr__(self, key, value):
        if key == '__class__':
            super(LazyRegistry, self).__setattr__(key, value)
        else:
            self.__init()
            setattr(self, key, value)

    def __getitem__(self, item):
        self.__init()
        return self[item]

    def __call__(self, *args, **kwargs):
        self.__init()
        return self(*args, **kwargs)