[2.5.1] Building loop with some exceptions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gilles Ganault

    [2.5.1] Building loop with some exceptions?

    Hello

    I need to call a URL through a loop that starts at 01 and ends at 99,
    but some of the steps must be ignored:

    =====
    url = "http://www.acme.com/list?code="
    p = re.compile("^(\ d+)\t(.+)$")

    for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
    f = urllib.urlopen( url + i)
    page = f.read()
    f.close()

    for line in page:
    m = p.search(line)
    if m:
    code = m.group(1)
    label = m.group(2)

    print "Code=" + code + " # Label=" + label
    =====

    I'm clueless at what the Python way is to do this :-/

    Thank you for any help.
  • Aaron Brady

    #2
    Re: Building loop with some exceptions?

    On Nov 4, 1:20 pm, Gilles Ganault <nos...@nospam. comwrote:
    Hello
    >
    I need to call a URL through a loop that starts at 01 and ends at 99,
    but some of the steps must be ignored:
    >
    =====
    url = "http://www.acme.com/list?code="
    p = re.compile("^(\ d+)\t(.+)$")
    >
    for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
    sorted( list( set( domain ) - set( exceptions ) ) )

    Set subtraction.

    Comment

    • Gilles Ganault

      #3
      Re: Building loop with some exceptions?

      On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
      <castironpi@gma il.comwrote:
      >for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
      >
      >sorted( list( set( domain ) - set( exceptions ) ) )
      >
      >Set subtraction.
      Thanks a lot but... I don't know what the above means :-/

      Comment

      • Matimus

        #4
        Re: Building loop with some exceptions?

        On Nov 4, 11:20 am, Gilles Ganault <nos...@nospam. comwrote:
        Hello
        >
        I need to call a URL through a loop that starts at 01 and ends at 99,
        but some of the steps must be ignored:
        >
        =====
        url = "http://www.acme.com/list?code="
        p = re.compile("^(\ d+)\t(.+)$")
        >
        for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
                f = urllib.urlopen( url + i)
                page = f.read()
                f.close()
        >
                for line in page:
                        m = p.search(line)
                        if m:
                                code = m.group(1)
                                label = m.group(2)
        >
                                print "Code=" + code + " # Label=" + label
        =====
        >
        I'm clueless at what the Python way is to do this :-/
        >
        Thank you for any help.
        I would just do something like this (not tested):

        url_tmpl = "http://www.acme.com/list?code=%02d"
        p = re.compile("^(\ d+)\t(.+)$")
        EXCLUDED_VALUES = 4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89

        for i in xrange(1,100):
        if i in EXCLUDED_VALUES :
        continue
        f = urllib.urlopen( url_tmpl % i)
        try:
        for line in f:
        m = p.search(line)
        if m:
        code = m.group(1)
        label = m.group(2)

        print "Code=", code, "# Label=", label
        finally:
        f.close()

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: Building loop with some exceptions?

          Gilles Ganault:
          Thanks a lot but... I don't know what the above means :-/
          set(iterable) just builds a set, then you use the really usual set
          semantics.

          Anyway, maybe you may find this more easy to understand:

          refused_indexes = set([4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89])
          for i in xrange(1, 100):
          if i not in refused_indexes :
          print i

          Note: never put a zero before an integer number (because it will give
          you troubles, defining octal numbers).

          Bye,
          bearophile

          Comment

          • Aaron Brady

            #6
            Re: Building loop with some exceptions?

            On Nov 4, 1:26 pm, Gilles Ganault <nos...@nospam. comwrote:
            On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
            >
            <castiro...@gma il.comwrote:
            for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
            >
            sorted( list( set( domain ) - set( exceptions ) ) )
            >
            Set subtraction.
            >
            Thanks a lot but... I don't know what the above means :-/
            This example produces the numbers 0..9, except 3, 5, and 9.
            >>sorted( list( set( range( 10 ) ) - set( [ 3, 5, 9 ] ) ) )
            [0, 1, 2, 4, 6, 7, 8]

            Comment

            • Gilles Ganault

              #7
              Re: Building loop with some exceptions?

              On Tue, 4 Nov 2008 12:10:28 -0800 (PST), Matimus <mccredie@gmail .com>
              wrote:
              >I would just do something like this (not tested):
              Thanks a lot guys :-) Worked first time.

              I just have the usual issue with ASCII/Unicode:

              =======
              cursor.execute( sql)
              UnicodeDecodeEr ror: 'ascii' codec can't decode byte 0xe9 in position
              53: ordinal
              not in range(128)
              =======

              Is there a way to get rid of this error once and for all?

              Thank you.

              Comment

              • Steven D'Aprano

                #8
                Re: [2.5.1] Building loop with some exceptions?

                On Tue, 04 Nov 2008 20:20:20 +0100, Gilles Ganault wrote:
                Hello
                >
                I need to call a URL through a loop that starts at 01 and ends at 99,
                but some of the steps must be ignored:
                ....
                I'm clueless at what the Python way is to do this :-/
                Perhaps you should start by doing the Python tutorial, so you'll be less
                clueless of simple things like how to do a loop.

                Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...




                for i in range(1, 100):
                if i in (4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89):
                continue
                do_rest_of_proc essing


                --
                Steven

                Comment

                • MRAB

                  #9
                  Re: Building loop with some exceptions?

                  On Nov 4, 10:02 pm, Aaron Brady <castiro...@gma il.comwrote:
                  On Nov 4, 1:26 pm, Gilles Ganault <nos...@nospam. comwrote:
                  >
                  On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
                  >
                  <castiro...@gma il.comwrote:
                  >for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
                  >
                  >sorted( list( set( domain ) - set( exceptions ) ) )
                  >
                  >Set subtraction.
                  >
                  Thanks a lot but... I don't know what the above means :-/
                  >
                  This example produces the numbers 0..9, except 3, 5, and 9.
                  >
                  >sorted( list( set( range( 10 ) ) - set( [ 3, 5, 9 ] ) ) )
                  >
                  [0, 1, 2, 4, 6, 7, 8]
                  FYI, 'set' accepts an iterable, so you don't need 'list':
                  >>sorted(set(ra nge(10)) - set([3, 5, 9]))
                  [0, 1, 2, 4, 6, 7, 8]

                  Comment

                  • alex23

                    #10
                    Re: Building loop with some exceptions?

                    On Nov 5, 5:20 am, Gilles Ganault <nos...@nospam. comwrote:
                    I need to call a URL through a loop that starts at 01 and ends at 99,
                    but some of the steps must be ignored:
                    exclusions = [04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89]
                    for i in (x for x in xrange(1,100) if x not in exclusions):
                    ....

                    Comment

                    • Gilles Ganault

                      #11
                      Re: [2.5.1] Building loop with some exceptions?

                      On 04 Nov 2008 22:34:49 GMT, Steven D'Aprano
                      <steve@REMOVE-THIS-cybersource.com .auwrote:
                      >for i in range(1, 100):
                      if i in (4, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89):
                      continue
                      do_rest_of_proc essing
                      Thanks for the sample.

                      Comment

                      • Gilles Ganault

                        #12
                        Re: Building loop with some exceptions?

                        On Tue, 4 Nov 2008 15:55:06 -0800 (PST), alex23 <wuwei23@gmail. com>
                        wrote:
                        exclusions = [04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89]
                        for i in (x for x in xrange(1,100) if x not in exclusions):
                        Thanks guys.

                        Comment

                        Working...