Checking the boolean value of a collection

M

Marco Bizzarri

Hi all.

In many parts of my code I've the following schema of code:

def isInUseByOutgoingRegistrations(self, archivefolder):
for instance in self.findActiveOutgoingRegistrationInstances():
if instance.forbidToClose(archivefolder):
return True
return False

Before devising my own solution for this kind of problem, I wonder if
there is a common solution for the problem. I'm looking for a
python2.3 solution.

Regards
Marco
 
D

Diez B. Roggisch

Marco said:
Hi all.

In many parts of my code I've the following schema of code:

def isInUseByOutgoingRegistrations(self, archivefolder):
for instance in self.findActiveOutgoingRegistrationInstances():
if instance.forbidToClose(archivefolder):
return True
return False

Before devising my own solution for this kind of problem, I wonder if
there is a common solution for the problem. I'm looking for a
python2.3 solution.

if any(instance.forbitToClose(archivefolder) for instance in
self.findActiveOutgoingRegistrationInstances())

You should also consider using PEP8 style naming.


Diez
 
M

Marco Bizzarri

if any(instance.forbitToClose(archivefolder) for instance in
self.findActiveOutgoingRegistrationInstances())

Can you clarify where I can find "any"? It seems to me I'm unable to find it...

You should also consider using PEP8 style naming.

I knew that someone would have said that to me :).

I'm doing that... slowly. I'm trying to fix naming conventions as I
had to work on my code...
 
D

Diez B. Roggisch

if any(instance.forbitToClose(archivefolder) for instance in
Can you clarify where I can find "any"? It seems to me I'm unable to find it...

It's part of python2.5.

If you don't have that, you can write it your own and stuff it into
__builtins__:
.... for item in iterable:
.... if item:
.... return True
.... return False
....
.... __builtins__.any = any


You might also want to add all, the companion of any:

.... for item in iterable:
.... if not item:
.... return False
.... return True
....

Diez
 
F

Fredrik Lundh

Marco said:
Can you clarify where I can find "any"? It seems to me I'm
> unable to find it...

it's a 2.5 addition. to use this in a "future-compatible" way in 2.3,
you can add

try:
any
except NameError:
def any(iterable):
for element in iterable:
if element:
return True
return False

to the top of the file (or to some suitable support library).

2.5 also provides an "all" function, which can be emulated as:

try:
all
except NameError:
def all(iterable):
for element in iterable:
if not element:
return False
return True

</F>
 
M

Marco Bizzarri

It's part of python2.5.

If you don't have that, you can write it your own and stuff it into
__builtins__:

... for item in iterable:
... if item:
... return True
... return False
...
... __builtins__.any = any


You might also want to add all, the companion of any:


... for item in iterable:
... if not item:
... return False
... return True
...

Diez

Thanks for the clarification, Diez! Indeed, I tried python2.3 and
python2.4, and of course not python2.5 ;)

I would like to make this available to the whole project. I suspect I
could put it in the package __init__.py... in that way, the
__builtins__ namespace should have it... am I right?
 
M

Marco Bizzarri

It's part of python2.5.

If you don't have that, you can write it your own and stuff it into
__builtins__:

... for item in iterable:
... if item:
... return True
... return False
...
... __builtins__.any = any


You might also want to add all, the companion of any:


... for item in iterable:
... if not item:
... return False
... return True
...

Diez

I'm afraid this have another problem for me...


emmebi@janitor:/var/local/zope28/porting/Products/PAFlow$ python2.3
Python 2.3.5 (#2, Oct 18 2006, 23:04:45)
[GCC 4.1.2 20061015 (prerelease) (Debian 4.1.1-16.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def any(iterable): pass ....
any(x for x in [1, 2, 3])
File "<stdin>", line 1
any(x for x in [1, 2, 3])
^
SyntaxError: invalid syntax

I mean, I'm afraid I can't use an expression like that without
building a list... not at least in python2.3

Regards
Marco
 
F

Fredrik Lundh

Marco said:
I would like to make this available to the whole project. I suspect I
could put it in the package __init__.py... in that way, the
__builtins__ namespace should have it... am I right?

the __init__ module for package "foo" defines the contents of the "foo"
module; it doesn't add anything to the builtin namespace.

Diez made a typo in his post, btw. To add your own builtins, you should
add them to the "__builtin__" module (no plural s):

import __builtin__

try:
any
except NameError:
def any(iterable):
for element in iterable:
if element:
return True
return False
__builtin__.any = any

try:
all
except NameError:
def all(iterable):
for element in iterable:
if not element:
return False
return True
__builtin__.all = all

The "__builtins__" object is an implementation detail, and shouldn't be
accessed directly. And I hope I don't need to point out that adding
custom builtins nillywilly is a bad idea...

</F>
 
D

D'Arcy J.M. Cain

The "__builtins__" object is an implementation detail, and shouldn't be
accessed directly. And I hope I don't need to point out that adding
custom builtins nillywilly is a bad idea...

Is there ever any advantage to having something as a builtin rather
than as a regular user method? What difference does it make to the
running script? I can see that adding "bar" from module "foo" to
"__builtins__" means that you can use "bar()" instead of "foo.bar()".
Is that the only benefit?
 
F

Fredrik Lundh

D'Arcy J.M. Cain said:
Is there ever any advantage to having something as a builtin rather
than as a regular user method? What difference does it make to the
running script? I can see that adding "bar" from module "foo" to
"__builtins__" means that you can use "bar()" instead of "foo.bar()".
Is that the only benefit?

basically, yes. in this case, it does make some sense to patch any/all
into __builtin__, since they are builtins in a later version.

</F>
 
B

bearophileHUGS

B

Bruno Desthuilliers

Marco Bizzarri a écrit :
(snip)
I'm afraid this have another problem for me...


emmebi@janitor:/var/local/zope28/porting/Products/PAFlow$ python2.3
Python 2.3.5 (#2, Oct 18 2006, 23:04:45)
[GCC 4.1.2 20061015 (prerelease) (Debian 4.1.1-16.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def any(iterable): pass ...
any(x for x in [1, 2, 3])
File "<stdin>", line 1
any(x for x in [1, 2, 3])
^
SyntaxError: invalid syntax
any([x for x in [1, 2, 3]])

I mean, I'm afraid I can't use an expression like that without
building a list... not at least in python2.3

Err... a list being an iterable, you just need any([1, 2, 3]).

But this wont be enough to solve your real use case, indeed.
 
M

Marco Bizzarri

Marco Bizzarri a écrit :
(snip)
I'm afraid this have another problem for me...


emmebi@janitor:/var/local/zope28/porting/Products/PAFlow$ python2.3
Python 2.3.5 (#2, Oct 18 2006, 23:04:45)
[GCC 4.1.2 20061015 (prerelease) (Debian 4.1.1-16.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
def any(iterable): pass
...

any(x for x in [1, 2, 3])

File "<stdin>", line 1
any(x for x in [1, 2, 3])
^
SyntaxError: invalid syntax
any([x for x in [1, 2, 3]])

I mean, I'm afraid I can't use an expression like that without
building a list... not at least in python2.3

Err... a list being an iterable, you just need any([1, 2, 3]).

Ehm, yes, of course... I was trying just to show from a command line
what were my results.
But this wont be enough to solve your real use case, indeed.


Yes, that's the point.
 
M

Marco Bizzarri

You should also consider using PEP8 style naming.


Diez


class FolderInUse:

def __init__(self, core):
self.core = core

def true_for(self, archivefolder):
return any([instance.forbid_to_close(archivefolder) for instance in
self.core.active_outgoing_registration_instances()])

Is this any better? The true_for name does not satisfy me a lot...
maybe because it is too similar to True. Anyway, I'm trying a good
naming so that code is readable, like:

specification = FolderInUse(core)

if specification.true_for(folder):
...

Any thought about this?

Regards
Marco
 
F

Fredrik Lundh

Marco said:
class FolderInUse:
>
def true_for(self, archivefolder):
return any([instance.forbid_to_close(archivefolder) for instance in
self.core.active_outgoing_registration_instances()])

Is this any better? The true_for name does not satisfy me a lot...

well, "true_for" is indeed pretty inscrutable, but I'm not sure that
would be the first thing I'd complain about in that verbose mess...

(when you pick method names, keep in mind that the reader will see the
context, the instance, and the arguments at the same time as they see
the name. there's no need to use complete sentences; pick short short
descriptive names instead.)

</F>
 
T

Terry Reedy

Marco said:
You should also consider using PEP8 style naming.


Diez


class FolderInUse:

def __init__(self, core):
self.core = core

def true_for(self, archivefolder):
return any([instance.forbid_to_close(archivefolder) for instance in
self.core.active_outgoing_registration_instances()])

Is this any better?

Yes. Now I can read it to suggest shorter names (I agree with FL here).
I would consider 'outgoing' for 'a_o_r_i' and perhaps 'no_close' or
'stay_open' or suggestions below for 'f_t_c'
The true_for name does not satisfy me a lot...
maybe because it is too similar to True.

Does one of 'locked', 'forbidden', 'untouchable' express the essence of
the condition being tested? I would consider using the same adjective
to name the test on instances and the collection of instances, or maybe
'x' and 'any_x'.


Anyway, I'm trying a good
naming so that code is readable, like:

specification = FolderInUse(core)

if specification.true_for(folder):
...

Any thought about this?

tjr
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top