469,280 Members | 1,868 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

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.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.
Oct 20 '08 #1
5 875
On Mon, Oct 20, 2008 at 12:08 PM, Robocop <bt*****@physics.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.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
Oct 20 '08 #2
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"
Oct 20 '08 #3
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
Oct 20 '08 #4
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...@vitalEsafe.comwrote:

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


Oct 20 '08 #5
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
Oct 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Elaine Jackson | last post: by
10 posts views Thread by Snake | last post: by
15 posts views Thread by Christopher Benson-Manica | last post: by
2 posts views Thread by pinkfog | last post: by
2 posts views Thread by vozzek | last post: by
16 posts views Thread by Raxit | last post: by
reply views Thread by Steve Holden | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.