Checking the boolean value of a collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Bizzarri

    Checking the boolean value of a collection

    Hi all.

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

    def isInUseByOutgoi ngRegistrations (self, archivefolder):
    for instance in self.findActive OutgoingRegistr ationInstances( ):
    if instance.forbid ToClose(archive folder):
    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
    Where we talk about coding, Dungeons and Dragons, and stuff.


  • Diez B. Roggisch

    #2
    Re: Checking the boolean value of a collection

    Marco Bizzarri schrieb:
    Hi all.
    >
    In many parts of my code I've the following schema of code:
    >
    def isInUseByOutgoi ngRegistrations (self, archivefolder):
    for instance in self.findActive OutgoingRegistr ationInstances( ):
    if instance.forbid ToClose(archive folder):
    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.fo rbitToClose(arc hivefolder) for instance in
    self.findActive OutgoingRegistr ationInstances( ))

    You should also consider using PEP8 style naming.


    Diez

    Comment

    • Marco Bizzarri

      #3
      Re: Checking the boolean value of a collection

      On Fri, Sep 12, 2008 at 4:09 PM, Diez B. Roggisch <deets@nospam.w eb.dewrote:
      Marco Bizzarri schrieb:
      >>
      >Hi all.
      >>
      >In many parts of my code I've the following schema of code:
      >>
      > def isInUseByOutgoi ngRegistrations (self, archivefolder):
      > for instance in self.findActive OutgoingRegistr ationInstances( ):
      > if instance.forbid ToClose(archive folder):
      > 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.fo rbitToClose(arc hivefolder) for instance in
      self.findActive OutgoingRegistr ationInstances( ))
      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...


      --
      Marco Bizzarri
      Where we talk about coding, Dungeons and Dragons, and stuff.


      Comment

      • Diez B. Roggisch

        #4
        Re: Checking the boolean value of a collection

        >if any(instance.fo rbitToClose(arc hivefolder) for instance in
        >self.findActiv eOutgoingRegist rationInstances ())
        >
        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__.an y = 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

        Comment

        • Fredrik Lundh

          #5
          Re: Checking the boolean value of a collection

          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>

          Comment

          • Marco Bizzarri

            #6
            Re: Checking the boolean value of a collection

            On Fri, Sep 12, 2008 at 4:44 PM, Diez B. Roggisch <deets@nospam.w eb.dewrote:
            >>if any(instance.fo rbitToClose(arc hivefolder) for instance in
            >>self.findActi veOutgoingRegis trationInstance s())
            >>
            >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__.an y = 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
            --

            >
            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
            Where we talk about coding, Dungeons and Dragons, and stuff.


            Comment

            • Marco Bizzarri

              #7
              Re: Checking the boolean value of a collection

              On Fri, Sep 12, 2008 at 4:44 PM, Diez B. Roggisch <deets@nospam.w eb.dewrote:
              >>if any(instance.fo rbitToClose(arc hivefolder) for instance in
              >>self.findActi veOutgoingRegis trationInstance s())
              >>
              >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__.an y = 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
              --

              >
              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
              Where we talk about coding, Dungeons and Dragons, and stuff.


              Comment

              • Fredrik Lundh

                #8
                Re: Checking the boolean value of a collection

                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>

                Comment

                • D'Arcy J.M. Cain

                  #9
                  Re: Checking the boolean value of a collection

                  On Fri, 12 Sep 2008 17:11:47 +0200
                  Fredrik Lundh <fredrik@python ware.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 <darcy@druid.ne t | 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.

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: Checking the boolean value of a collection

                    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>

                    Comment

                    • Bruno Desthuilliers

                      #11
                      Re: Checking the boolean value of a collection

                      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.

                      Comment

                      • bearophileHUGS@lycos.com

                        #12
                        Re: Checking the boolean value of a collection

                        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.findAc tiveOutgoingReg istrationInstan cesPlusSomeOthe rThings())

                        Bye,
                        bearophile

                        Comment

                        • Marco Bizzarri

                          #13
                          Re: Checking the boolean value of a collection

                          On Fri, Sep 12, 2008 at 6:07 PM, Bruno Desthuilliers
                          <bruno.42.desth uilliers@websit eburo.invalidwr ote:
                          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
                          Where we talk about coding, Dungeons and Dragons, and stuff.


                          Comment

                          • Marco Bizzarri

                            #14
                            Re: Checking the boolean value of a collection

                            On Fri, Sep 12, 2008 at 4:09 PM, Diez B. Roggisch <deets@nospam.w eb.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(archi vefolder) for instance in
                            self.core.activ e_outgoing_regi stration_instan ces()])

                            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(cor e)

                            if specification.t rue_for(folder) :
                            ...

                            Any thought about this?

                            Regards
                            Marco


                            --
                            Marco Bizzarri
                            Where we talk about coding, Dungeons and Dragons, and stuff.


                            Comment

                            • Fredrik Lundh

                              #15
                              Re: Checking the boolean value of a collection

                              Marco Bizzarri wrote:
                              class FolderInUse:
                              >
                              def true_for(self, archivefolder):
                              return any([instance.forbid _to_close(archi vefolder) for instance in
                              self.core.activ e_outgoing_regi stration_instan ces()])
                              >
                              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>

                              Comment

                              Working...