473,606 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A __getattr__ for class methods?

I'm trying to implement a bunch of class methods in an ORM object in
order to provide functionality similar to Rails' ActiveRecord. This
means that if I have an SQL table mapped to the class "Person" with
columns name, city, and email, I can have class methods such as:

Person.find_by_ name
Person.find_by_ city_and_name
Person.find_by_ name_and_city_a nd_email

I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_ city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, or am I trying to mirror a functionality that Python simply does
not have?

Feb 8 '06 #1
5 6849
Dylan Moreland wrote:
I'm trying to implement a bunch of class methods in an ORM object in
order to provide functionality similar to Rails' ActiveRecord. This
means that if I have an SQL table mapped to the class "Person" with
columns name, city, and email, I can have class methods such as:

Person.find_by_ name
Person.find_by_ city_and_name
Person.find_by_ name_and_city_a nd_email

I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_ city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, ...


Sure, define __getattr__ on the type of the class i.e., the metaclass, just as
you define it on a class to provide default-attribute-lookup to its instances:
class A(object): ... class __metaclass__(t ype):
... def __getattr__(cls , attr):
... return "%s.%s" % (cls.__name__, attr)
... A.somefunc 'A.somefunc' A.someotherfunc 'A.someotherfun c'


HTH

Michael

Feb 8 '06 #2
Dylan Moreland wrote:
I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_ city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, or am I trying to mirror a functionality that Python simply does
not have?


I don't get it? __getattr__ is exactly what you are looking for:

http://docs.python.org/ref/attribute...s.html#l2h-206

__getattr__( self, name)

Called when an attribute lookup has not found the attribute in the usual
places (i.e. it is not an instance attribute nor is it found in the
class tree for self). name is the attribute name. This method should
return the (computed) attribute value or raise an AttributeError exception.

HtH, Roland
Feb 8 '06 #3
Michael Spencer wrote:
Dylan Moreland wrote:
I'm trying to implement a bunch of class methods in an ORM object in
order to provide functionality similar to Rails' ActiveRecord. This
means that if I have an SQL table mapped to the class "Person" with
columns name, city, and email, I can have class methods such as:

Person.find_by_ name
Person.find_by_ city_and_name
Person.find_by_ name_and_city_a nd_email

I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_ city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, ...


Sure, define __getattr__ on the type of the class i.e., the metaclass, just as
you define it on a class to provide default-attribute-lookup to its instances:
>>> class A(object): ... class __metaclass__(t ype):
... def __getattr__(cls , attr):
... return "%s.%s" % (cls.__name__, attr)
... >>> A.somefunc 'A.somefunc' >>> A.someotherfunc 'A.someotherfun c' >>>


HTH

Michael


Thanks! I only recently realized that I would have to learn metaclasses
in order to make this work, and I'm still a bit unclear on their
properties.

Feb 8 '06 #4
Dylan Moreland wrote:
I'm trying to implement a bunch of class methods in an ORM object in
order to provide functionality similar to Rails' ActiveRecord. This
means that if I have an SQL table mapped to the class "Person" with
columns name, city, and email, I can have class methods such as:

Person.find_by_ name
Person.find_by_ city_and_name
Person.find_by_ name_and_city_a nd_email

I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_ city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, or am I trying to mirror a functionality that Python simply does
not have?

I'm not sure that the way you tackled this is the good approach: while
it's quite flexible as far as the search criteria go, it'll require less
than obvious code to match keywords (field names) and values, will lead
to somewhat verbose syntax (especially with many criteria), and the
syntax itself is unforgiving (every new search field requires at least 5
additional characters on top of the field name itself), brittle (you'll
have to do an extensive validation of your method name and fields unless
you want everything to break, and Python doesn't support your syntax)
and not really pythonic.

Since you seem to know ActiveRecord, you're probably familiar with the
way AR does this task: with a hash of column_name, value pairs. The
syntax is quite straightforward especially due to the fact that any "key
=> value" comma-separated pairs sequence generates a hash, without
requiring explicit hash boundaries ( { and } ). The syntax is extremely
straightforward since it relies on the language's syntax itself, and the
correctness of the grammar is checked by the compiler/interpreter itself
since no custom syntax is built. This construct is therefore quite
solid, on top of being obvious to a Rubyist.

Now, you're in luck, because Python has even better than that: **kwargs,
the optional keyword arguments.

As you probably know, Python has a good support for keyword args,
allowing you to fill your arguments out of order (and leave the 3th
argument at it's default value while specifying the value of the 5th
argument). But **kwargs goes beyond the regular explicit keyword
arguments: when specified, **kwargs is a dict populated with the
implicit keyword arguments (keyword:value pairs), the ones you haven't
specified in the argument tuple of your method.

This means that if I define a function as

def foo(bar=0, **kwargs):
print kwargs

Then calling foo() will print an empty dict
As will calling foo(3) or foo(bar=3), because the explicit "bar" keyword
argument is used.

Now if I call foo(something=5 , somethingelse=" woohoo"), kwargs will
evaluate to

{"somethingelse ":"woohoo","som ething":5}

This means that all you have to do is replace your method definition with
def find_by(cls, **kwargs): pass
and in the method itself iterate over the key:value pairs of kwargs to
automagically get both the field names and the values upon which your
search shall be performed.

Calling the method would then look something like:
Person.find_by( name="thenameyo ulookfor", city="somecity" )

the syntax is fairly obvious and pythonic, has a low verbosity, and
Python itself will do the parsing and the grammatical validation of your
method calls for you.
Feb 8 '06 #5
Oh, and I wondered too: is your goal to build an ORM, or do you just
need an ORM?

Cause if it's the latter then Python does already have some fairly good
ORMs such as SQLAlchemy or PyDO2, you don't *need* to create yours.
Feb 8 '06 #6

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

Similar topics

0
6583
by: Gigi | last post by:
Hi, In the Python documentation regarding __getattribute__ (more attribute access for new style classes) it is mentioned that if __getattribute__ is defined __getattr__ will never be called (unless called explicitely). Here is the exact citation: """ The following methods only apply to new-style classes. __getattribute__( self, name)
13
3487
by: Pelmen | last post by:
How can I get rid of recursive call __getattr__ inside this method, if i need to use method or property of the class?
6
4501
by: Erik Johnson | last post by:
Maybe I just don't know the right special function, but what I am wanting to do is write something akin to a __getattr__ function so that when you try to call an object method that doesn't exist, it get's intercepted *along with it's argument*, in the same manner as __getattr__ intercepts attributes references for attributes that don't exist. This doesn't quite work: >>> class Foo:
2
2295
by: mwojc | last post by:
Hi! My class with implemented __getattr__ and __setattr__ methods cannot be pickled because of the Error: ====================================================================== ERROR: testPickle (__main__.TestDeffnet2WithBiases) ---------------------------------------------------------------------- Traceback (most recent call last): File "deffnet.py", line 246, in testPickle cPickle.dump(self.denet, file)
0
8016
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8306
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3937
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.