Python code for 2.5 and 2.4?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph Turian

    #1

    Python code for 2.5 and 2.4?

    I was given code that was written for python 2.5, and uses simple
    functions like 'all' which are not present in 2.4

    I want to make the code 2.4 compatible. What is the best way to do
    this?
    If I define function 'all', then won't I break 2.5 compatability?

    Thanks,
    Joseph
  • Mike Driscoll

    #2
    Re: Python code for 2.5 and 2.4?

    On Feb 25, 2:40 pm, Joseph Turian <tur...@gmail.c omwrote:
    I was given code that was written for python 2.5, and uses simple
    functions like 'all' which are not present in 2.4
    >
    I want to make the code 2.4 compatible. What is the best way to do
    this?
    If I define function 'all', then won't I break 2.5 compatability?
    >
    Thanks,
    Joseph
    See http://docs.python.org/lib/built-in-funcs.html which shows the
    current built-ins for Python. It also shows an equivalent function for
    all() that should work in 2.4.

    You can do a check at the beginning of your file by importing the sys
    module. Something like this:

    <code>
    # untested
    import sys
    version = sys.version.spl it(' ')[0]

    if '2.5' not in version:
    # use custom all() script

    </code>

    HTH

    Mike

    Comment

    • Robert Kern

      #3
      Re: Python code for 2.5 and 2.4?

      Joseph Turian wrote:
      I was given code that was written for python 2.5, and uses simple
      functions like 'all' which are not present in 2.4
      >
      I want to make the code 2.4 compatible. What is the best way to do
      this?
      If it's a single file, put something like the following code near the top. If
      you have multiple modules, put it into a separate module, say compatibility.p y,
      and change the other modules to import these functions from there.


      import sys
      if sys.version_inf o[:2] < (2,5):
      def all(*args):
      ...
      def any(*args):
      ...
      else:
      # Only bother with this else clause and the __all__ line if you are putting
      # this in a separate file.
      import __builtin__
      all = __builtin__.all
      any = __builtin__.any

      __all__ = ['all', 'any']

      If I define function 'all', then won't I break 2.5 compatability?
      No. Defining a function named the same thing as a builtin function will not
      break anything. You just wouldn't be using the efficient implementation already
      in Python 2.5. Using the if: else: suite above lets you have both at the expense
      of some clunkiness.

      --
      Robert Kern

      "I have come to believe that the whole world is an enigma, a harmless enigma
      that is made terrible by our own mad attempt to interpret it as though it had
      an underlying truth."
      -- Umberto Eco

      Comment

      • Arnaud Delobelle

        #4
        Re: Python code for 2.5 and 2.4?

        On Feb 25, 8:47 pm, Mike Driscoll <kyoso...@gmail .comwrote:
        >
        You can do a check at the beginning of your file by importing the sys
        module. Something like this:
        >
        <code>
        # untested
        import sys
        version = sys.version.spl it(' ')[0]
        >
        if '2.5' not in version:
           # use custom all() script
        >
        </code>
        Or simply:

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

        --
        Arnaud

        Comment

        • Mike Driscoll

          #5
          Re: Python code for 2.5 and 2.4?

          Joseph,

          On 2/25/08, Joseph Turian <turian@gmail.c omwrote:
          >
          Seehttp://docs.python.org/lib/built-in-funcs.htmlwhich shows the
          current built-ins for Python. It also shows an equivalent function for
          all() that should work in 2.4.
          >
          Yes, I see that.
          >
          You can do a check at the beginning of your file by importing the sys
          module. Something like this:
          <code>
          # untested
          import sys
          version = sys.version.spl it(' ')[0]
          >
          if '2.5' not in version:
          # use custom all() script
          >
          </code>
          >
          Okay.
          I guess I was asking how do I nicely package the above into a file
          python25.py from which I can do:
          from python25 import all
          And python25 figures out the python version and returns either the
          hand-coded version or the library version.
          How can I write python25.py ?
          >
          Well one way to do this would be something like this:

          <python25.py>

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

          </python25.py>

          And combine that with the code that Robert gave you. I'm not sure of
          the exact syntax, but I would think you could do an IF statement that
          creates the custom definition and returns it if Python 2.4 or less is
          installed and return the normal version for 2.5 if it is installed.

          Mike

          Comment

          • Joseph Turian

            #6
            Re: Python code for 2.5 and 2.4?

            On 25 fév, 16:02, Robert Kern <robert.k...@gm ail.comwrote:
            Joseph Turian wrote:
            I was given code that was written for python 2.5, and uses simple
            functions like 'all' which are not present in 2.4
            >
            I want to make the code 2.4 compatible. What is the best way to do
            this?
            >
            If it's a single file, put something like the following code near the top.If
            you have multiple modules, put it into a separate module, say compatibility.p y,
            and change the other modules to import these functions from there.
            >
            import sys
            if sys.version_inf o[:2] < (2,5):
            def all(*args):
            ...
            def any(*args):
            ...
            else:
            # Only bother with this else clause and the __all__ line if you are putting
            # this in a separate file.
            import __builtin__
            all = __builtin__.all
            any = __builtin__.any
            >
            __all__ = ['all', 'any']
            >
            If I define function 'all', then won't I break 2.5 compatability?
            >
            No. Defining a function named the same thing as a builtin function will not
            break anything. You just wouldn't be using the efficient implementation already
            in Python 2.5. Using the if: else: suite above lets you have both at the expense
            of some clunkiness.
            >
            --
            Robert Kern
            >
            "I have come to believe that the whole world is an enigma, a harmless enigma
            that is made terrible by our own mad attempt to interpret it as though it had
            an underlying truth."
            -- Umberto Eco
            This is what I was looking for. Thanks!

            Comment

            • Paul Rubin

              #7
              Re: Python code for 2.5 and 2.4?

              Arnaud Delobelle <arnodel@google mail.comwrites:
              def all(iterable):
              for x in iterable:
              if not x: return False
              return True
              from itertools import imap
              def all(iterable):
              return False not in imap(bool, iterable)

              def any(iterable):
              return True in imap(bool, iterable)

              Comment

              • Arnaud Delobelle

                #8
                Re: Python code for 2.5 and 2.4?

                On Feb 25, 11:17 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                Arnaud Delobelle <arno...@google mail.comwrites:
                    def all(iterable):
                        for x in iterable:
                            if not x: return False
                        return True
                >
                from itertools import imap
                def all(iterable):
                   return False not in imap(bool, iterable)
                Ok. In that case, the following is probably faster (in fact for long
                iterables it may be equivalent to the builtin all):

                from itertools import ifilterfalse

                def all(iterable):
                for _ in ifilterfalse(No ne, iterable):
                return False
                return True


                But my point was really about not checking for the version but just
                checking for the existence of the name 'all'.

                --
                Arnaud

                Comment

                • Paul Rubin

                  #9
                  Re: Python code for 2.5 and 2.4?

                  Arnaud Delobelle <arnodel@google mail.comwrites:
                  Ok. In that case, the following is probably faster (in fact for long
                  iterables it may be equivalent to the builtin all):
                  >
                  from itertools import ifilterfalse ...
                  Nice.

                  Comment

                  • Robert Kern

                    #10
                    Unsubscribing from python-list (was Re: Python code for 2.5 and 2.4?)

                    Bhatt, Omkar wrote:
                    PLEASE TAKE ME OFF THIS LIST!
                    You can unsubscribe yourself using the link at the bottom of every message:



                    --
                    Robert Kern

                    "I have come to believe that the whole world is an enigma, a harmless enigma
                    that is made terrible by our own mad attempt to interpret it as though it had
                    an underlying truth."
                    -- Umberto Eco

                    Comment

                    Working...