By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,612 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,612 IT Pros & Developers. It's quick & easy.

identifier class enforcement

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <cd**********@nebula.dnttm.ro>,
"Florian Preknya" <bo**@coco.ro> wrote:
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.
Jul 18 '05 #2

P: n/a
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-----

Jul 18 '05 #3

P: n/a
On Mon, 19 Jul 2004, Mel Wilson wrote:
In article <cd**********@nebula.dnttm.ro>,
"Florian Preknya" <bo**@coco.ro> wrote:
1. The string is not initialized with the s.strip().replace(" ", "_")
expression.

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__`.


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).
2. I can at any time change the Class.name member type from client
code, so my enforcement with the Identifier class is gone.


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.

Jul 18 '05 #4

P: n/a
On Mon, 19 Jul 2004, Christopher T King wrote:
You can enforce this in Class using properties:
Previously, Jeff Epler wrote:
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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.