38 |
assert tm.map(x=10., a=20.) == { 5 : 10, 4: 10, 1 : 20 } |
assert tm.map(x=10., a=20.) == { 5 : 10, 4: 10, 1 : 20 } |
39 |
|
|
40 |
""" |
""" |
41 |
def __init__(self, map={}): |
def __init__(self, mapping={}): |
42 |
""" |
""" |
43 |
initizlizes the mapping. map defines an initial mapping from tag to a name. |
initizlizes the mapping. map defines an initial mapping from tag to a name. |
44 |
""" |
""" |
45 |
for tag, name in map.items(): |
self.__mapping={} |
46 |
|
for tag, name in mapping.items(): |
47 |
if not isinstance(tag, int): |
if not isinstance(tag, int): |
48 |
raise TypeError("tag needs to be int") |
raise TypeError("tag needs to be int") |
49 |
if not isinstance(name, str): |
if not isinstance(name, str): |
50 |
raise TypeError("name needs to be a str.") |
raise TypeError("name needs to be a str.") |
51 |
self.__map=map |
self.__mapping[tag]=name |
52 |
def setMap(self,**kwargs): |
def setMap(self,**kwargs): |
53 |
""" |
""" |
54 |
set a new map where <name>=<tag> assigns the tag <tag> to name <name>. <tag> has to be integer. |
set a new map where <name>=<tag> assigns the tag <tag> to name <name>. <tag> has to be integer. |
58 |
for name, tag in kwargs.items(): |
for name, tag in kwargs.items(): |
59 |
if not isinstance(tag, int): |
if not isinstance(tag, int): |
60 |
raise TypeError("tag needs to be int") |
raise TypeError("tag needs to be int") |
61 |
self.__map[tag]=name |
self.__mapping[tag]=name |
62 |
def getTags(self,name=None): |
def getTags(self,name=None): |
63 |
""" |
""" |
64 |
returns a list of the tags assigned to name. If name is not present a list of tags is returned. |
returns a list of the tags assigned to name. If name is not present a list of tags is returned. |
65 |
""" |
""" |
66 |
if name == None: |
if name == None: |
67 |
out=self.__map.keys() |
out=self.__mapping.keys() |
68 |
else: |
else: |
69 |
out=[] |
out=[] |
70 |
for tag, arg in self.__map.items(): |
for tag, arg in self.__mapping.items(): |
71 |
if arg == name: out.append(tag) |
if arg == name: out.append(tag) |
72 |
return out |
return out |
73 |
def getName(self,tag=None): |
def getName(self,tag=None): |
75 |
returns the name of a tag. If tag is not present a list of names is returned. |
returns the name of a tag. If tag is not present a list of names is returned. |
76 |
""" |
""" |
77 |
if tag == None: |
if tag == None: |
78 |
return list(set(self.__map.values())) |
return list(set(self.__mapping.values())) |
79 |
else: |
else: |
80 |
return self.__map[tag] |
return self.__mapping[tag] |
81 |
|
|
82 |
def getMapping(self): |
def getMapping(self): |
83 |
""" |
""" |
84 |
returns a dictionary where the tags define the keys and the values the corresposnding names. |
returns a dictionary where the tags define the keys and the values the corresposnding names. |
85 |
""" |
""" |
86 |
return self.__map |
return self.__mapping |
87 |
|
|
88 |
def map(self,default=0,**kwargs): |
def map(self,default=0,**kwargs): |
89 |
""" |
""" |
98 |
the default is used for tags which map onto name with unspecified values |
the default is used for tags which map onto name with unspecified values |
99 |
""" |
""" |
100 |
out={} |
out={} |
101 |
for tag in self.__map: |
for tag in self.__mapping: |
102 |
if kwargs.has_key(self.__map[tag]): |
if kwargs.has_key(self.__mapping[tag]): |
103 |
out[tag]=kwargs[self.__map[tag]] |
out[tag]=kwargs[self.__mapping[tag]] |
104 |
else: |
else: |
105 |
out[tag]=default |
out[tag]=default |
106 |
return out |
return out |
117 |
""" |
""" |
118 |
passes the tag map to L{esys.escript.Domain} domain. |
passes the tag map to L{esys.escript.Domain} domain. |
119 |
""" |
""" |
120 |
for tag, name in self.__map.items(): |
for tag, name in self.__mapping.items(): |
121 |
domain.setTagMap(name,tag) |
domain.setTagMap(name,tag) |
122 |
|
|
123 |
def toDOM(self,dom): |
def toDOM(self,dom): |