473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Checking the boolean value of a collection

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

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 12 '08 #1
15 1026
Marco Bizzarri schrieb:
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
Sep 12 '08 #2
On Fri, Sep 12, 2008 at 4:09 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
Marco Bizzarri schrieb:
>>
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())
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...
Diez
--
http://mail.python.org/mailman/listinfo/python-list


--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 12 '08 #3
>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...
It's part of python2.5.

If you don't have that, you can write it your own and stuff it into
__builtins__:
>>def any(iterable):
.... for item in iterable:
.... if item:
.... return True
.... return False
....
.... __builtins__.any = any
You might also want to add all, the companion of any:

>>def all(iterable):
.... for item in iterable:
.... if not item:
.... return False
.... return True
....

Diez
Sep 12 '08 #4
Marco Bizzarri wrote:
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>

Sep 12 '08 #5
On Fri, Sep 12, 2008 at 4:44 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
>>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...

It's part of python2.5.

If you don't have that, you can write it your own and stuff it into
__builtins__:
>>>def any(iterable):
... for item in iterable:
... if item:
... return True
... return False
...
... __builtins__.any = any
You might also want to add all, the companion of any:

>>>def all(iterable):
... for item in iterable:
... if not item:
... return False
... return True
...

Diez
--
http://mail.python.org/mailman/listinfo/python-list
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?

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 12 '08 #6
On Fri, Sep 12, 2008 at 4:44 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
>>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...

It's part of python2.5.

If you don't have that, you can write it your own and stuff it into
__builtins__:
>>>def any(iterable):
... for item in iterable:
... if item:
... return True
... return False
...
... __builtins__.any = any
You might also want to add all, the companion of any:

>>>def all(iterable):
... for item in iterable:
... if not item:
... return False
... return True
...

Diez
--
http://mail.python.org/mailman/listinfo/python-list
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

Regards
Marco

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 12 '08 #7
Marco Bizzarri wrote:
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>

Sep 12 '08 #8
On Fri, 12 Sep 2008 17:11:47 +0200
Fredrik Lundh <fr*****@pythonware.comwrote:
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?

--
D'Arcy J.M. Cain <da***@druid.net | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
Sep 12 '08 #9
D'Arcy J.M. Cain wrote:
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>

Sep 12 '08 #10
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.

Sep 12 '08 #11
Marco Bizzarri:
>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
Note that you don't need a list comp there:
>>any([x for x in [1, 2, 3]])
True
>>any([1, 2, 3])
True

so this may suffice:

any(self.findActiveOutgoingRegistrationInstancesPl usSomeOtherThings())

Bye,
bearophile
Sep 12 '08 #12
On Fri, Sep 12, 2008 at 6:07 PM, Bruno Desthuilliers
<br********************@websiteburo.invalidwrote :
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.

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 13 '08 #13
On Fri, Sep 12, 2008 at 4:09 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
>
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
--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 13 '08 #14
Marco Bizzarri wrote:
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>

Sep 13 '08 #15


Marco Bizzarri wrote:
On Fri, Sep 12, 2008 at 4:09 PM, Diez B. Roggisch <de***@nospam.web.dewrote:
>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

Sep 13 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anders Borum | last post by:
Hello! I would like to request a new method on the XsltArgumentList class, allowing developers to check the presense of a key/value pair. If you try to add a key/value pair that has already been...
4
by: Rich | last post by:
I have a access form that is connected to linked sql table via odbc. I have some fields that I dont want to allow nulls when data is entered via the form. I can set the null checking on the SQL...
6
by: No_Spam | last post by:
I'm calling a function in .NET (ASP/VB) this way: If Not myFunction(var1, var2) Then .... End If myFunction is declared as: - Function myFunction(var1 as String, var2 as String) As Boolean...
7
by: Blake Weaver | last post by:
Subject pretty much says it. I'm testing my Collection to check and see if an item has already been added, thus avoiding duplicates. I don't even know what value to check it against... some sort of...
0
by: ECathell | last post by:
Ok this may be a silly question...but here goes... I need to extend the boolean type. the reason for this is I have a base collection that has boolean properties. Then my inherited class has...
2
by: Brett Romero | last post by:
I convert a collection to DataSet, reference the first table and throw that into a DataTable. The DataTable is used as a DataSource for a DataGrid. The DataGrid has an associated GridTableSyle. ...
5
by: Skip | last post by:
Hi All, I am trying to check a fields value (boolean in this case) in the ItemUpdated event. The following does not work. I am very new to .NET (VB6 guy) so sorry if this is way off. If...
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.