Dynamically Generate Methods

G

GZ

Hi,

I have a class Record and a list key_attrs that specifies the names of
all attributes that correspond to a primary key.

I can write a function like this to get the primary key:

def get_key(instance_of_record):
return tuple(instance_of_record.__dict__[k] for k in key_attrs)

However, since key_attrs are determined at the beginning of the
program while get_key() will be called over and over again, I am
wondering if there is a way to dynamically generate a get_ley method
with the key attributes expanded to avoid the list comprehension/
generator.

For example, if key_attrs=['A','B'], I want the generated function to
be equivalent to the following:

def get_key(instance_of_record):
return (instance_of_record['A'],instance_of_record['B'] )

I realize I can use eval or exec to do this. But is there any other
way to do this?

Thanks,
gz
 
J

Jean-Michel Pichavant

GZ said:
Hi,

I have a class Record and a list key_attrs that specifies the names of
all attributes that correspond to a primary key.

I can write a function like this to get the primary key:

def get_key(instance_of_record):
return tuple(instance_of_record.__dict__[k] for k in key_attrs)

However, since key_attrs are determined at the beginning of the
program while get_key() will be called over and over again, I am
wondering if there is a way to dynamically generate a get_ley method
with the key attributes expanded to avoid the list comprehension/
generator.

For example, if key_attrs=['A','B'], I want the generated function to
be equivalent to the following:

def get_key(instance_of_record):
return (instance_of_record['A'],instance_of_record['B'] )

I realize I can use eval or exec to do this. But is there any other
way to do this?

Thanks,
gz
Hi,

you may want to do something like

class Record(object):
PRIMARY_KEY = []
def __init__(self):
for key in self.PRIMARY_KEY:
setattr(self, key, None)

def getPrimaryKeyValues(self):
return [ getattr(self, key) for key in self.PRIMARY_KEY]


class FruitRecord(Record):
PRIMARY_KEY = ['fruit_id', 'fruit_name']



JM

PS : there's a high chance that a python module already exists to access
your database with python objects.
 
I

Ian Kelly

Hi,

I have a class Record and a list key_attrs that specifies the names of
all attributes that correspond to a primary key.

I can write a function like this to get the primary key:

def get_key(instance_of_record):
return tuple(instance_of_record.__dict__[k] for k in key_attrs)

However, since key_attrs are determined at the beginning of the
program while get_key() will be called over and over again, I am
wondering if there is a way to dynamically generate a get_ley method
with the key attributes expanded to avoid the list comprehension/
generator.

(Accidentally sent this to the OP only)

This is exactly what the attrgetter factory function produces.

from operator import attrgetter
get_key = attrgetter(*key_attrs)

But if your attribute names are variable and arbitrary, I strongly
recommend you store them in a dict instead. Setting them as instance
attributes risks that they might conflict with the regular attributes
and methods on your objects.

Cheers,
Ian
 
G

GZ

Hi All,

I see. It works.

Thanks,
GZ

I have a class Record and a list key_attrs that specifies the names of
all attributes that correspond to a primary key.
I can write a function like this to get the primary key:
def get_key(instance_of_record):
  return tuple(instance_of_record.__dict__[k] for k in key_attrs)
However, since key_attrs are determined at the beginning of the
program while get_key() will be called over and over again, I am
wondering if there is a way to dynamically generate a get_ley method
with the key attributes expanded to avoid the list comprehension/
generator.

(Accidentally sent this to the OP only)

This is exactly what the attrgetter factory function produces.

from operator import attrgetter
get_key = attrgetter(*key_attrs)

But if your attribute names are variable and arbitrary, I strongly
recommend you store them in a dict instead.  Setting them as instance
attributes risks that they might conflict with the regular attributes
and methods on your objects.

Cheers,
Ian
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top