Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15888 > unrolled thread
| Started by | GZ <zyzhu2000@gmail.com> |
|---|---|
| First post | 2011-11-18 06:51 -0800 |
| Last post | 2011-11-20 03:23 -0800 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
Dynamically Generate Methods GZ <zyzhu2000@gmail.com> - 2011-11-18 06:51 -0800
Re: Dynamically Generate Methods Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-11-18 17:56 +0100
Re: Dynamically Generate Methods Duncan Booth <duncan.booth@invalid.invalid> - 2011-11-18 17:52 +0000
Re: Dynamically Generate Methods Ian Kelly <ian.g.kelly@gmail.com> - 2011-11-18 11:04 -0700
Re: Dynamically Generate Methods GZ <zyzhu2000@gmail.com> - 2011-11-20 03:23 -0800
| From | GZ <zyzhu2000@gmail.com> |
|---|---|
| Date | 2011-11-18 06:51 -0800 |
| Subject | Dynamically Generate Methods |
| Message-ID | <ad844f2a-8521-4ce4-8dfc-fb7e0fc505ad@q39g2000prg.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| Date | 2011-11-18 17:56 +0100 |
| Message-ID | <mailman.2836.1321635635.27778.python-list@python.org> |
| In reply to | #15888 |
GZ wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2011-11-18 17:52 +0000 |
| Message-ID | <Xns9FA1B5E1D59FEduncanbooth@127.0.0.1> |
| In reply to | #15888 |
GZ <zyzhu2000@gmail.com> wrote:
> 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?
>
Use operator.itemgetter:
>>> key_attrs = ['A', 'B']
>>> import operator
>>> get_key = operator.itemgetter(*key_attrs)
>>> d = {'A': 42, 'B': 63, 'C': 99}
>>> get_key(d)
(42, 63)
--
Duncan Booth http://kupuguy.blogspot.com
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-11-18 11:04 -0700 |
| Message-ID | <mailman.2838.1321639492.27778.python-list@python.org> |
| In reply to | #15888 |
On Fri, Nov 18, 2011 at 7:51 AM, GZ <zyzhu2000@gmail.com> wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | GZ <zyzhu2000@gmail.com> |
|---|---|
| Date | 2011-11-20 03:23 -0800 |
| Message-ID | <6c05cd6f-9adb-4f65-9da7-cd938616da41@j10g2000vbe.googlegroups.com> |
| In reply to | #15912 |
Hi All, I see. It works. Thanks, GZ On Nov 18, 12:04 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote: > On Fri, Nov 18, 2011 at 7:51 AM, GZ <zyzhu2...@gmail.com> wrote: > > 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 > >
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web