quick newbie syntax question

R

Robocop

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.
 
C

Chris Rebert

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
 
R

Robocop

oops! Sorry about that, i should have just copied my code directly.
I actually did specify an int in range:
The code is currently failing due to the syntax in the filter,
particularly the section "date = year'-'month'-'i"
 
L

Larry Bates

Robocop said:
oops! Sorry about that, i should have just copied my code directly.
I actually did specify an int in range:

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
 
R

Robocop

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!
Robocop said:
oops!   Sorry about that, i should have just copied my code directly.
I actually did specify an int in range:
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
 
S

Steven D'Aprano

oops! Sorry about that, i should have just copied my code directly. I
actually did specify an int in range:

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top