473,327 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Metaclasses for class mangling after definition.

Hi everybody,

I am writing a base class for <<entity>> stereotype classes, to implement
controlled access to the state and expose some of the access logic (in order
to dinamically build javascript code for client-side checks, for example).

Currently the idiom needed to subclass it is something like:

class Group(Entity):
_interface = Entity._extend_interface(
id = Direct(state_field='grp_id',
filters=(ReadOnlyFilter(),),
doc="The univocal database id for the group."),

name = Direct(
filters=(
StringFilter(),
MaxLenFilter(max_len=40,
message="The group name can be at most "
"%(max_len)s characters long."),),
doc = "A descriptive name for the group."),
)

_state = Entity._extend_state(
grp_id=None,
name=None,
)

# ... class definition ...

make_entity(Group)

The make_entity() call compiles some dinamically generated code to produce
the properties required to access the class. But adding such line after each
entity definition bothers me.

I don't know much about metaclasses: could they help me to automatically
perform such mangling on the classes "at compile time" (or at least once in
the class lifetime)? Hints about that?

What make_entity does is here down. Basically each Field instance (Direct is
a Field subclass) in the _interface dictionary (a class member) create a
property object on the same class that will perform the access for the class
instances.

def make_entity(entity):
"""Complete the Entity subclass building its attributes."""
for (field_name, field) in entity._interface.iteritems():
if not hasattr(entity, field_name):
field.field_name = field_name
field.create_accessors(entity)

Thank you in advance

Daniele

Jul 18 '05 #1
0 1077

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
3
by: Mike C. Fletcher | last post by:
Slides from my PyGTA presentation on Tuesday, focusing mostly on why/where you would want to use meta-classes, are available in PDF format: ...
2
by: Santiago Aguiar | last post by:
Im trying to use AOP techniques on python but, even if I understand the idea behind metaclasses, I cant find a way to use AOP in a dynamic way. What im trying to do is to weave an aspect to a...
27
by: Michele Simionato | last post by:
> Hello, my name is Skip and I am metaclass-unaware. I've been programming in > Python for about ten years and I have yet to write a metaclass. At first I > thought it was just that metaclasses...
4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
8
by: Jan-Ole Esleben | last post by:
Hi! I am new to this list, and maybe this is a stupid question, but I can't seem to find _any_ kind of answer anywhere. What I want to do is the following: I want to insert a class variable...
4
by: Lawrence Oluyede | last post by:
I've never used metaclasses in real life before and while searching through the online Cookbook I found this gorgeous example: "Wrapping method calls (meta-class example)"...
2
by: Brian van den Broek | last post by:
Hi all, I've the following code snippet that puzzles me: class Base(object): __v, u = "Base v", "Base u" def __init__(self): print self.__v, self.u class Derived(Base):
1
by: David Hirschfield | last post by:
Is it possible for one class definition to have multiple metaclasses? I don't think it is, but I'm just checking to make sure. From what I know, you can only define "__metaclass__" to set the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.