identifier class enforcement

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.
 
M

Mel Wilson

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(" ", "_"))

As far as I know, strings are immutable, so you have to set
a str descendant up using `str.__new__`.


Regards. Mel.
 
J

Jeff Epler

I'd write Identifier as a function:
def Identifier(s):
return s.strip().replace(" ", "_")
unless it's important that Identifier be a class. If it is, use a str
subclass and the __new__ method as suggested by another poster.

In Class, I'd use a property to call a setter function when name is
modified:
class Class(object): #subclass of object required for property to work
def set_name(self, newname):
self.__name = Identifier(newname)
def get_name(self):
return self.__name
name = property(get, set)

def __init__(self, name):
self.name = name

The setting of self.name in __init__ or anywhere else in the program
will go through the setter function, set_name, enforcing the requirement
that the name be an Identifier.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA/FxXJd01MZaTXX0RAhBmAJ9fZawKI2GV8a3vnAis7dgk84Y/ewCfUuHg
pJHyulTFhBC/+HBxuaQxJXA=
=Nquj
-----END PGP SIGNATURE-----
 
C

Christopher T King

As far as I know, strings are immutable, so you have to set
a str descendant up using `str.__new__`.

I've had to do this before. Using __new__, what you want to do is:

class Identifier(str):
def __new__(cls, s = ""):
return str.__new__(cls, s.strip().replace(" ", "_"))

As Mel pointed out, strings are immutable, so you have to set their value
before they are created (i.e. create them with a value).

You can enforce this in Class using properties:

class Class(object): # must be new-style class for this to work
def getname(self):
return self._name

def setname(self,v):
if not isinstance(v,Identifier):
raise TypeError, 'name must be of type Identifier!'
self._name = v

name = property(setname,getname)

Now, any assignment to or retrieval from Class.name will transparently go
through Class.getname() and Class.setname().

However, as mentioned by Jeff, a function Identifier() might be a bit
neater than a class. In this case, you can force name to be of type
Identifier by defining setname() like this:

def setname(self,v):
self._name = Identifier(v)

This will also work if you use the class approach.

Depending on your application, it may be better just to use setname() and
getname() directly, and forgo use of Class.name. Some think this is
better OO methodology all around, while others think properties are
cleaner in certain instances. I'll let you be the judge ;)

Hope this helps.
 
C

Christopher T King

You can enforce this in Class using properties:

In Class, I'd use a property to call a setter function when name is
modified:

Whoops, didn't see that part of your post, Jeff -- sorry!
That's what I get for using Pine as my newsreader :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top