executing list of methods (and collecting results)

G

Gerardo Herzig

Hi all. Im in this situation: I want to perform several kind of
(validating) methods to a given value.
Lets say i have a class named Number, and the following methods:
is_really_a_number(),
is_even(),
is_greater_than_zero(),
and so on. All of them returning booleans.

I want the collect_validators() method is to execute any of the above
methods, and collect their names as items of a list (wich will be the
collect_validators() return value).

My first approach is:

Code:
def collect_validators(self):
    v_dict = { 'is_really_a_number': is_really_a_number,
                      'is_even': is_even,
                      'is_greater_than_zero', is_greater_than_zero
                   }

       for name, meth in v_dict.items():
          result = meth()
          if result: yield name

I wondering if is this a good pattern to apply, i like the way it looks
like, at least to me it looks `natural', but...im calling every method
twice here? One in v_dict and again on the dict iteration?

Any suggestion will be great!

Thanks!
Gerardo
 
J

James Stroud

Gerardo said:
Hi all. Im in this situation: I want to perform several kind of
(validating) methods to a given value.
Lets say i have a class named Number, and the following methods:
is_really_a_number(),
is_even(),
is_greater_than_zero(),
and so on. All of them returning booleans.

I want the collect_validators() method is to execute any of the above
methods, and collect their names as items of a list (wich will be the
collect_validators() return value).

My first approach is:

Code:
def collect_validators(self):
v_dict = { 'is_really_a_number': is_really_a_number,
'is_even': is_even,
'is_greater_than_zero', is_greater_than_zero
}

for name, meth in v_dict.items():
result = meth()
if result: yield name

I wondering if is this a good pattern to apply, i like the way it looks
like, at least to me it looks `natural', but...im calling every method
twice here? One in v_dict and again on the dict iteration?

Any suggestion will be great!

Thanks!
Gerardo

You are not calling every method twice. You are painstakingly typing out
their names twice. But fortunately, functions and methods have a
__name__ attribute which makes a this typing redundant. I would give
your class instances a _validators attribute which is a list of
validating methods, then make the generator like this:

def collect_validators(self):
for v in self._validators:
if v():
yield v.__name__

James
 
B

Bjoern Schliessmann

Gerardo said:
I want the collect_validators() method is to execute any of the
above methods, and collect their names as items of a list (wich
will be the collect_validators() return value).

(inside class definition -- untested)
validators = {"is a number": is_really_a_number,
"is even": is_even,
"is greater than zero": is_greater_than_zero}

def collect_validators(self):
return [desc for desc, func in self.validators.items() if func()]
My first approach is:

.... no method, but a generator. Executing it will give you a
generator object instead of a result list.
Code:
def collect_validators(self):
v_dict = { 'is_really_a_number': is_really_a_number,
'is_even': is_even,
'is_greater_than_zero', is_greater_than_zero
}

for name, meth in v_dict.items():
result = meth()
if result: yield name

I wondering if is this a good pattern to apply, i like the way it
looks like, at least to me it looks `natural',

IMHO, it doesn't look natural. It depends on what you want to
achieve. This generator will need to be iterated over until it
is "exhausted".
but...im calling every method twice here?

No. Methods are only called if you apply the function call
operator, "()".

BTW, I hope you don't really want to test a number to be greater
than zero, or even, by using an own method, respectively, just to
test this.

Regards,


Björn
 
G

gherzig

Gerardo said:
I want the collect_validators() method is to execute any of the
above methods, and collect their names as items of a list (wich
will be the collect_validators() return value).

(inside class definition -- untested)
validators = {"is a number": is_really_a_number,
"is even": is_even,
"is greater than zero": is_greater_than_zero}

def collect_validators(self):
return [desc for desc, func in self.validators.items() if func()]
Excelent!!!
My first approach is:

... no method, but a generator. Executing it will give you a
generator object instead of a result list.
Code:
def collect_validators(self):
v_dict = { 'is_really_a_number': is_really_a_number,
'is_even': is_even,
'is_greater_than_zero', is_greater_than_zero
}

for name, meth in v_dict.items():
result = meth()
if result: yield name

I wondering if is this a good pattern to apply, i like the way it
looks like, at least to me it looks `natural',

IMHO, it doesn't look natural. It depends on what you want to
achieve. This generator will need to be iterated over until it
is "exhausted".
Im having some fun doing a mail filter. A master thread will fire several
threads (each one returning a list with the matched validators) and
collect the results of each one of them.
No. Methods are only called if you apply the function call
operator, "()".

BTW, I hope you don't really want to test a number to be greater
than zero, or even, by using an own method, respectively, just to
test this.
Haha, no, the actual methods do other kind of things.
Thanks Björn!!!

Cheers.
Gerardo
 
P

Paul Hankin

def collect_validators(self):
v_dict = { 'is_really_a_number': is_really_a_number,
'is_even': is_even,
'is_greater_than_zero', is_greater_than_zero
}

for name, meth in v_dict.items():
result = meth()
if result: yield name

Are these validators actually methods rather than functions? If so,
you should write something like this:

def collect_validators(self):
methods = ['is_really_a_number', 'is_even',
'is_greater_than_zero']
return (m for m in methods if getattr(self, m)())
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top