Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Defines the VarKey class""" 

2from .small_classes import Count, qty 

3from .repr_conventions import ReprMixin 

4 

5 

6class VarKey(ReprMixin): # pylint:disable=too-many-instance-attributes 

7 """An object to correspond to each 'variable name'. 

8 

9 Arguments 

10 --------- 

11 name : str, VarKey, or Monomial 

12 Name of this Variable, or object to derive this Variable from. 

13 

14 **descr : 

15 Any additional attributes, which become the descr attribute (a dict). 

16 

17 Returns 

18 ------- 

19 VarKey with the given name and descr. 

20 """ 

21 unique_id = Count().next 

22 subscripts = ("lineage", "idx") 

23 

24 def __init__(self, name=None, **descr): 

25 # NOTE: Python arg handling guarantees 'name' won't appear in descr 

26 self.descr = descr 

27 self.descr["name"] = name or "\\fbox{%s}" % VarKey.unique_id() 

28 unitrepr = self.unitrepr or self.units 

29 if unitrepr in ["", "-", None]: # dimensionless 

30 self.descr["units"] = None 

31 self.descr["unitrepr"] = "-" 

32 else: 

33 self.descr["units"] = qty(unitrepr) 

34 self.descr["unitrepr"] = unitrepr 

35 

36 self.key = self 

37 fullstr = self.str_without(["modelnums", "vec"]) 

38 self.eqstr = fullstr + str(self.lineage) + self.unitrepr 

39 self.hashvalue = hash(self.eqstr) 

40 self.keys = set((self.name, fullstr)) 

41 

42 if "idx" in self.descr: 

43 if "veckey" not in self.descr: 

44 vecdescr = self.descr.copy() 

45 del vecdescr["idx"] 

46 self.veckey = VarKey(**vecdescr) 

47 else: 

48 self.keys.add(self.veckey) 

49 self.keys.add(self.str_without(["idx", "modelnums"])) 

50 

51 def __getstate__(self): 

52 "Stores varkey as its metadata dictionary, removing functions" 

53 state = self.descr.copy() 

54 for key, value in state.items(): 

55 if getattr(value, "__call__", None): 

56 state[key] = "unpickleable function %s" % value 

57 return state 

58 

59 def __setstate__(self, state): 

60 "Restores varkey from its metadata dictionary" 

61 self.__init__(**state) 

62 

63 def str_without(self, excluded=()): 

64 "Returns string without certain fields (such as 'lineage')." 

65 name = self.name 

66 if "lineage" not in excluded and self.lineage: 

67 namespace = self.lineagestr("modelnums" not in excluded).split(".") 

68 backscan = 0 

69 for ex in excluded: 

70 if ex[0:7] == ":MAGIC:": 

71 to_replace = ex[7:].split(".") 

72 replaced = 0 

73 for modelname in to_replace: 

74 if not namespace or namespace[0] != modelname: 

75 break 

76 replaced += 1 

77 namespace = namespace[1:] 

78 if len(to_replace) > replaced: 

79 namespace.insert(0, "."*(len(to_replace)-replaced)) 

80 backscan = replaced - 1 

81 if "unnecessary lineage" in excluded: 

82 if self.necessarylineage: 

83 namespace = namespace[-self.necessarylineage:] 

84 else: 

85 namespace = None 

86 if namespace: 

87 name = ".".join(namespace) + "." + name 

88 if "idx" not in excluded: 

89 if self.idx: 

90 name += "[%s]" % ",".join(map(str, self.idx)) 

91 elif "vec" not in excluded and self.shape: 

92 name += "[:]" 

93 return name 

94 

95 __repr__ = str_without 

96 

97 # pylint: disable=multiple-statements 

98 def __hash__(self): return self.hashvalue 

99 def __getattr__(self, attr): return self.descr.get(attr, None) 

100 

101 @property 

102 def models(self): 

103 "Returns a tuple of just the names of models in self.lineage" 

104 return list(zip(*self.lineage))[0] 

105 

106 def latex(self, excluded=()): 

107 "Returns latex representation." 

108 name = self.name 

109 if "vec" not in excluded and "idx" not in excluded and self.shape: 

110 name = "\\vec{%s}" % name 

111 if "idx" not in excluded and self.idx: 

112 name = "{%s}_{%s}" % (name, ",".join(map(str, self.idx))) 

113 if ("lineage" not in excluded and self.lineage 

114 and ("unnecessary lineage" not in excluded 

115 or self.necessarylineage)): 

116 name = "{%s}_{%s}" % (name, 

117 self.lineagestr("modelnums" not in excluded)) 

118 return name 

119 

120 def __eq__(self, other): 

121 if not hasattr(other, "descr"): 

122 return False 

123 return self.eqstr == other.eqstr