F
Florian Preknya
I work on building a metamodel from an UML diagram (serialized as xmi).
There I have a class called Class that represents a class from the UML
model. The Class has a name member and ininitially it was a string. What I
want to do is to enfoce the class name be an identifier (for example to
exclude white spaces etc.).
I defined an Identifier class, having the builtin str as base class. I have
2 problems:
1. The string is not initialized with the s.strip().replace(" ", "_")
expression.
2. I can at any time change the Class.name member type from client code,
so my enforcement with the Identifier class is gone.
I know Python is very weak typed, but can there is a solution for my second
problem ? Or can I model the problem in some other way ?
class Identifier(str):
def __init__(self, s = ""):
str.__init__(self, s.strip().replace(" ", "_"))
class Class:
def __init__(self, name = ""):
self.name = Identifier(name)
# ....
c = Class(" Circle ")
print type(c.name)
print c.name # will print " Circle " and not "Circle" as I
expected
c.name = "Bubu" # I change the type from Identifier to string
print type(c.name)
print c.name
Thanks,
Florian.
There I have a class called Class that represents a class from the UML
model. The Class has a name member and ininitially it was a string. What I
want to do is to enfoce the class name be an identifier (for example to
exclude white spaces etc.).
I defined an Identifier class, having the builtin str as base class. I have
2 problems:
1. The string is not initialized with the s.strip().replace(" ", "_")
expression.
2. I can at any time change the Class.name member type from client code,
so my enforcement with the Identifier class is gone.
I know Python is very weak typed, but can there is a solution for my second
problem ? Or can I model the problem in some other way ?
class Identifier(str):
def __init__(self, s = ""):
str.__init__(self, s.strip().replace(" ", "_"))
class Class:
def __init__(self, name = ""):
self.name = Identifier(name)
# ....
c = Class(" Circle ")
print type(c.name)
print c.name # will print " Circle " and not "Circle" as I
expected
c.name = "Bubu" # I change the type from Identifier to string
print type(c.name)
print c.name
Thanks,
Florian.