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: -
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 5 1393
Gerardo Herzig wrote:
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:
-
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
Gerardo Herzig wrote:
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. -
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
--
BOFH excuse #321:
Scheduled global CPU outage
Gerardo Herzig wrote:
>
>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.
> - 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.
>
>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.
Haha, no, the actual methods do other kind of things.
Thanks Björn!!!
Cheers.
Gerardo
On Sep 21, 12:26 am, Gerardo Herzig <gher...@fmed.uba.arwrote:
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)())
--
Paul Hankin gh*****@fmed.uba.ar wrote:
Haha, no, the actual methods do other kind of things.
Thanks Björn!!!
Okay, so I hoped. Glad to be of help.
Regards,
Björn
--
BOFH excuse #233:
TCP/IP UDP alarm threshold is set too low. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Istvan Albert |
last post by:
Paul McGuire wrote:
> Please reconsider the "def f() :" construct. Instead of
> invoking a special punctuation character, it uses context and placement,
> with familiar old 's, to infuse the...
|
by: Jay O'Connor |
last post by:
Is there a good way to import python files without executing their content?
I'm trying some relfection based stuff and I want to be able to import a
module dynamically to check it's contents...
|
by: clr |
last post by:
I like to stamp trace logs with the name of the executing Class and Method.
I can get the Class Name using GetType.Name and I can get a list of every
Method in the class using...
|
by: Mythran |
last post by:
I wrote some code that is supposed to enumerate through the specified file's
win32 resources and return a string-array of all icon names. When it runs,
it returns a string-array with a bunch of...
|
by: ahart |
last post by:
I'm pretty new to python and am trying to write a fairly small
application to learn more about the language. I'm noticing some
unexpected behavior in using lists in some classes to hold child...
|
by: Jon Slaughter |
last post by:
I was wondering if maybe allowing "fields" for methods. The reason is to
encapsulate the data that is mainly used by the method and to prevent the
need of having to create new variables every time...
|
by: HMS Surprise |
last post by:
Seems to me that one should be able to put the names of several
functions in a list and then have the list executed. But it seems the
output of the functions is hidden, only their return value is...
|
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= |
last post by:
I am executing an AJAX page method that is a long running task. After
starting the first method, I execute a second page method to retrieve the
status of the task. It works fine in an empty web...
|
by: Karlo Lozovina |
last post by:
This is what I'm trying to do (create a list using list comprehesion, then
insert new element at the beginning of that list):
result = .insert(0, 'something')
But instead of expected results,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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...
| |