473,385 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 980
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Elaine Jackson | last post by:
Two quick newbie questions: 1) Does Python have passing-by-reference? 2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the Python "copy" module (if I understand...
10
by: Snake | last post by:
Hello guys, I have a quick question about pointers. When I say int *p; *p = 40; cout << *p; why does that produce and error message(something about fault address)? Isnt that I created a...
15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
8
by: Jm | last post by:
Hi All Ive asked this question in ms.public.dotnet.framework.interop but havent got a reply so im hoping someone may be able to help here. I am a bit of a newbie to vbnet and am having trouble...
2
by: pinkfog | last post by:
Hello,all I m just a newbie to c,and now i m learning quick sorting,i dont very undertand this algorrithm,can someone kind to explain it to me? the code: <quote> void quick_sort(int a ,int low...
2
by: vozzek | last post by:
Hi all, I have a really quick syntax problem I can't seem to hammer out... In my script, I am trying to set a variable based upon an entry from my SQL table. Here's two different lines of code...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
0
by: Steve Holden | last post by:
Karthik Krishnan wrote: Apart from the syntax error Rob pointed out (use of "=" instead of "==" as a comparison operator) the output you show makes it seem possible you are entering the command...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.