16 ############################################################ |
18 ############################################################ |
17 ## Classes implementing resolvers for various compund types |
19 ## Classes implementing resolvers for various compund types |
18 ############################################################ |
20 ############################################################ |
19 |
21 |
20 |
22 |
21 class BaseResolver(object): |
23 class BaseResolver: |
22 """ |
24 """ |
23 Base class of the resolver class tree. |
25 Base class of the resolver class tree. |
24 """ |
26 """ |
25 def resolve(self, var, attribute): |
27 def resolve(self, var, attribute): |
26 """ |
28 """ |
48 if not names and hasattr(var, "__members__"): |
50 if not names and hasattr(var, "__members__"): |
49 names = var.__members__ |
51 names = var.__members__ |
50 |
52 |
51 d = {} |
53 d = {} |
52 for name in names: |
54 for name in names: |
53 try: |
55 with contextlib.suppress(Exception): |
54 attribute = getattr(var, name) |
56 attribute = getattr(var, name) |
55 d[name] = attribute |
57 d[name] = attribute |
56 except Exception: # secok |
|
57 pass # if we can't get it, simply ignore it |
|
58 |
58 |
59 return d |
59 return d |
60 |
60 |
61 |
61 |
62 ############################################################ |
62 ############################################################ |
82 if not names and hasattr(var, "__members__"): |
82 if not names and hasattr(var, "__members__"): |
83 names = var.__members__ |
83 names = var.__members__ |
84 |
84 |
85 d = {} |
85 d = {} |
86 for name in names: |
86 for name in names: |
87 try: |
87 with contextlib.suppress(Exception): |
88 attribute = getattr(var, name) |
88 attribute = getattr(var, name) |
89 d[name] = attribute |
89 d[name] = attribute |
90 except Exception: # secok |
|
91 pass # if we can't get it, simply ignore it |
|
92 |
90 |
93 yield -1, d |
91 yield -1, d |
94 while True: |
92 while True: |
95 yield -2, {} |
93 yield -2, {} |
96 |
94 |
262 @param attribute id of the value to extract |
260 @param attribute id of the value to extract |
263 @type str |
261 @type str |
264 @return value of the attribute |
262 @return value of the attribute |
265 @rtype any |
263 @rtype any |
266 """ |
264 """ |
267 return super(DictViewResolver, self).resolve(list(var), attribute) |
265 return super().resolve(list(var), attribute) |
268 |
266 |
269 def getDictionary(self, var): |
267 def getDictionary(self, var): |
270 """ |
268 """ |
271 Public method to get the attributes of a variable as a dictionary. |
269 Public method to get the attributes of a variable as a dictionary. |
272 |
270 |
273 @param var variable to be converted |
271 @param var variable to be converted |
274 @type any |
272 @type any |
275 @return dictionary containing the variable attributes |
273 @return dictionary containing the variable attributes |
276 @rtype dict |
274 @rtype dict |
277 """ |
275 """ |
278 return super(DictViewResolver, self).getDictionary(list(var)) |
276 return super().getDictionary(list(var)) |
279 |
277 |
280 |
278 |
281 ############################################################ |
279 ############################################################ |
282 ## Resolver for Sets and Frozensets |
280 ## Resolver for Sets and Frozensets |
283 ############################################################ |
281 ############################################################ |
487 return var[attribute] |
485 return var[attribute] |
488 except Exception: |
486 except Exception: |
489 return getattr(var, attribute, None) |
487 return getattr(var, attribute, None) |
490 |
488 |
491 expectedID = int(attribute.split(" (ID:")[-1][:-1]) |
489 expectedID = int(attribute.split(" (ID:")[-1][:-1]) |
492 for key in var.keys(): |
490 for key in var: |
493 if id(key) == expectedID: |
491 if id(key) == expectedID: |
494 return var.getlist(key) |
492 return var.getlist(key) |
495 |
493 |
496 return None |
494 return None |
497 |
495 |
651 (dict, dictResolver), |
649 (dict, dictResolver), |
652 (set, setResolver), |
650 (set, setResolver), |
653 (frozenset, setResolver), |
651 (frozenset, setResolver), |
654 ] |
652 ] |
655 |
653 |
656 try: |
654 with contextlib.suppress(Exception): |
657 _TypeMap.append((long, None)) # __IGNORE_WARNING__ |
655 _TypeMap.append((long, None)) # __IGNORE_WARNING__ |
658 except Exception: # secok |
656 |
659 pass # not available on all Python versions |
657 with contextlib.suppress(ImportError): |
660 |
|
661 try: |
|
662 import array |
658 import array |
663 _TypeMap.append((array.array, arrayResolver)) |
659 _TypeMap.append((array.array, arrayResolver)) |
664 except ImportError: |
660 # array.array may not be available |
665 pass # array.array may not be available |
661 |
666 |
662 with contextlib.suppress(ImportError): |
667 try: |
|
668 import numpy |
663 import numpy |
669 _TypeMap.append((numpy.ndarray, ndarrayResolver)) |
664 _TypeMap.append((numpy.ndarray, ndarrayResolver)) |
670 except ImportError: |
665 # numpy may not be installed |
671 pass # numpy may not be installed |
666 |
672 |
667 with contextlib.suppress(ImportError): |
673 try: |
|
674 from django.utils.datastructures import MultiValueDict |
668 from django.utils.datastructures import MultiValueDict |
675 # it should go before dict |
669 # it should go before dict |
676 _TypeMap.insert(0, (MultiValueDict, multiValueDictResolver)) |
670 _TypeMap.insert(0, (MultiValueDict, multiValueDictResolver)) |
677 except ImportError: |
671 # django may not be installed |
678 pass # django may not be installed |
672 |
679 |
673 with contextlib.suppress(ImportError): |
680 try: |
|
681 from collections.abc import ItemsView, KeysView, ValuesView |
674 from collections.abc import ItemsView, KeysView, ValuesView |
682 _TypeMap.append((ItemsView, dictViewResolver)) |
675 _TypeMap.append((ItemsView, dictViewResolver)) |
683 _TypeMap.append((KeysView, dictViewResolver)) |
676 _TypeMap.append((KeysView, dictViewResolver)) |
684 _TypeMap.append((ValuesView, dictViewResolver)) |
677 _TypeMap.append((ValuesView, dictViewResolver)) |
685 except ImportError: |
678 # not available on all Python versions |
686 pass # not available on all Python versions |
|
687 |
679 |
688 |
680 |
689 def getType(obj): |
681 def getType(obj): |
690 """ |
682 """ |
691 Public method to get the type information for an object. |
683 Public method to get the type information for an object. |