quick newbie syntax question

Larry Bates larry.bates at vitalEsafe.com
Mon Oct 20 16:21:43 EDT 2008


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



More information about the Python-list mailing list