quick newbie syntax question

Discussion in 'Python' started by Robocop, Oct 20, 2008.

  1. Robocop

    Robocop Guest

    Is it possible to do something like this syntactically:

    year = '2008'
    month = '09'
    limit = '31'
    for i in range(1,limit):
    temp = Table.objects.filter(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.
    Robocop, Oct 20, 2008
    #1
    1. Advertising

  2. Robocop

    Chris Rebert Guest

    On Mon, Oct 20, 2008 at 12:08 PM, Robocop <> wrote:
    > 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.filter(date = year'-'month'-'i) <----screwed


    I believe you're looking for the string formatting syntax, to wit:

    temp = Table.objects.filter( 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...
    http://rebertia.com

    > 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.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Chris Rebert, Oct 20, 2008
    #2
    1. Advertising

  3. Robocop

    Robocop Guest

    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(limit)):


    The code is currently failing due to the syntax in the filter,
    particularly the section "date = year'-'month'-'i"
    Robocop, Oct 20, 2008
    #3
  4. Robocop

    Larry Bates Guest

    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(limit)):

    >
    > 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.filter 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(filterFunc):
    return [x for x in self.objects if filterFunc(x)]


    Then

    temp = Table.objects.filter(myFilter)

    temp is a list of objects you can act on.

    -Larry
    Larry Bates, Oct 20, 2008
    #4
  5. Robocop

    Robocop Guest

    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 <> wrote:


    > 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(limit)):

    >
    > > 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.filter 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(filterFunc):
    >          return [x for x in self.objects if filterFunc(x)]
    >
    > Then
    >
    > temp = Table.objects.filter(myFilter)
    >
    > temp is a list of objects you can act on.
    >
    > -Larry
    Robocop, Oct 20, 2008
    #5
  6. 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(limit)):

    >
    > 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
    Steven D'Aprano, Oct 20, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wizard
    Replies:
    2
    Views:
    356
    Saravana
    Jan 3, 2005
  2. JKop
    Replies:
    11
    Views:
    852
  3. Ian Davies

    quick syntax question

    Ian Davies, Jun 17, 2006, in forum: HTML
    Replies:
    9
    Views:
    354
    Toby Inkster
    Jun 18, 2006
  4. Miles

    Quick Questions on Syntax

    Miles, May 26, 2008, in forum: Java
    Replies:
    10
    Views:
    493
    Arne Vajhøj
    May 27, 2008
  5. Johannes Bauer

    Quick nested loop syntax?

    Johannes Bauer, Nov 19, 2008, in forum: Python
    Replies:
    3
    Views:
    336
    alex23
    Nov 20, 2008
Loading...

Share This Page