quick newbie syntax question

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

    quick newbie syntax question

    Is it possible to do something like this syntactically:

    year = '2008'
    month = '09'
    limit = '31'
    for i in range(1,limit):
    temp = Table.objects.f ilter(date = year'-'month'-'i) <----screwed
    up syntax
    ...do something with temp
    return

    I know that the syntax within the filter statement is wrong. Is it
    even possible to do something like that? Any help is always
    appreciated.
  • Chris Rebert

    #2
    Re: quick newbie syntax question

    On Mon, Oct 20, 2008 at 12:08 PM, Robocop <bthayre@physic s.ucsd.eduwrote :
    Is it possible to do something like this syntactically:
    >
    year = '2008'
    month = '09'
    limit = '31'
    for i in range(1,limit):
    This previous line will fail. range() takes numbers, not strings.
    Change 'limit' to an int.
    temp = Table.objects.f ilter(date = year'-'month'-'i) <----screwed
    I believe you're looking for the string formatting syntax, to wit:

    temp = Table.objects.f ilter( date="%s-%s-%s" % (year, month, i) )

    Note that you can let 'year' and 'month' be integers now, as they will
    be converted to strings for you automatically.

    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    up syntax
    ...do something with temp
    return
    >
    I know that the syntax within the filter statement is wrong. Is it
    even possible to do something like that? Any help is always
    appreciated.
    --

    >

    Comment

    • Robocop

      #3
      Re: quick newbie syntax question

      oops! Sorry about that, i should have just copied my code directly.
      I actually did specify an int in range:
      year = '2008'
      month = '09'
      limit = '31'
      for i in range(1,int(lim it)):
      The code is currently failing due to the syntax in the filter,
      particularly the section "date = year'-'month'-'i"

      Comment

      • Larry Bates

        #4
        Re: quick newbie syntax question

        Robocop wrote:
        oops! Sorry about that, i should have just copied my code directly.
        I actually did specify an int in range:
        >>year = '2008'
        >>month = '09'
        >>limit = '31'
        >>for i in range(1,int(lim it)):
        >
        The code is currently failing due to the syntax in the filter,
        particularly the section "date = year'-'month'-'i"
        I believe you want something more like

        date = "%s-%s-%s" % (year, month, i)

        but then again I can't quite figure out what is is that you are wanting to do
        (Note: September doesn't have 31 days).

        Where did Table object come from and what does the table.objects.f ilter method
        do and what are the appropriate arguments to that method? If it was "my"
        method, and I wanted to use it this way, I would think about changing it so that
        it accepted a filtering function and returned only those objects that passed the
        filtering function:

        def myFilter(obj):
        return (obj.date >= '2008-09-01' and obj.date <= '2008-09-30')

        class objects(object) :
        def __init__(self):
        self.objects = []

        def filter(filterFu nc):
        return [x for x in self.objects if filterFunc(x)]


        Then

        temp = Table.objects.f ilter(myFilter)

        temp is a list of objects you can act on.

        -Larry

        Comment

        • Robocop

          #5
          Re: quick newbie syntax question

          date = "%s-%s-%s" % (year, month, i) is exactly what i'd like to do.

          The Table object will just be a mysql table, and the filter function
          will yield a list filtered for those dates.
          For my purposes the limit variable will not be static, depending on
          which day of the month it is i will only want it to iterate up to that
          date in the month (i use 31 here as an example as i would want it to
          iterate through the 30th of september). Thanks for the input!
          On Oct 20, 1:21 pm, Larry Bates <larry.ba...@vi talEsafe.comwro te:

          Robocop wrote:
          oops!   Sorry about that, i should have just copied my code directly.
          I actually did specify an int in range:
          >year = '2008'
          >month = '09'
          >limit = '31'
          >for i in range(1,int(lim it)):
          >
          The code is currently failing due to the syntax in the filter,
          particularly the section "date = year'-'month'-'i"
          >
          I believe you want something more like
          >
          date = "%s-%s-%s" % (year, month, i)
          >
          but then again I can't quite figure out what is is that you are wanting to do
          (Note: September doesn't have 31 days).
          >
          Where did Table object come from and what does the table.objects.f ilter method
          do and what are the appropriate arguments to that method?  If it was "my"
          method, and I wanted to use it this way, I would think about changing it so that
          it accepted a filtering function and returned only those objects that passed the
          filtering function:
          >
          def myFilter(obj):
               return (obj.date >= '2008-09-01' and obj.date <= '2008-09-30')
          >
          class objects(object) :
               def __init__(self):
                   self.objects = []
          >
               def filter(filterFu nc):
                   return [x for x in self.objects if filterFunc(x)]
          >
          Then
          >
          temp = Table.objects.f ilter(myFilter)
          >
          temp is a list of objects you can act on.
          >
          -Larry


          Comment

          • Steven D'Aprano

            #6
            Re: quick newbie syntax question

            On Mon, 20 Oct 2008 12:24:14 -0700, Robocop wrote:
            oops! Sorry about that, i should have just copied my code directly. I
            actually did specify an int in range:
            year = '2008'
            month = '09'
            limit = '31'
            for i in range(1,int(lim it)):
            >
            The code is currently failing due to the syntax in the filter,
            particularly the section "date = year'-'month'-'i"
            Name binding ("date = something") is not an expression and must be on a
            line of its own. So you can't do something like:

            function(y=x+1, 2, 3)

            (Except of course for function default values, which is not quite the
            same thing.)


            --
            Steven

            Comment

            Working...