On 1/8/2010 3:12 AM, Garry Bettle wrote:
This is what I've come up with.  Sorry, python is something I touch on
occasionally:  must do more!

As the races are output, I build a dictionary of key=FixtureName and
value=RaceTimes:

RaceTime = marketResp.market.displayTime.time()
cRaceTime = RaceTime.strftime("%H%M")
FixtureName = MarketSummary.marketName.strip()
MarketName = marketResp.market.name
if FixtureName not in FixtureList:
     FixtureList[FixtureName] = cRaceTime
else:
     FixtureList[FixtureName]+= " " + cRaceTime

And then, when I want the summary to be printed:

for fixture in FixtureList:
     print fixture.ljust(6), FixtureList[fixture]

It works, but have I done it the "python" way?

In general, you should keep all data in object format and convert it to string just before printing it (except for data that is inherently a text). You can have FixtureList as a dict mapping FixtureName to a list of datetime objects.

Tips: you may want to see defaultdict from the collections module.

>>> import collections
>>> from datetime import datetime
>>> a = collections.defaultdict(list)
>>> fmt = '%Y-%m-%d %H%M'
>>> a['Sund'].append(datetime.strptime('2010-01-07 1103', fmt))
>>> a['Sheff'].append(datetime.strptime('2010-01-07 1111', fmt))
>>> a['Sund'].append(datetime.strptime('2010-01-07 1119', fmt))
>>> a
defaultdict(<class 'list'>, {'Sund': [datetime.datetime(2010, 1, 7, 11, 3), datetime.datetime(2010, 1, 7, 11, 19)], 'Sheff': [datetime.datetime(2010, 1, 7, 11, 11)]})

It's much more versatile than keeping the time as a string; though for quick and dirty scripts keeping everything as a string may not be so much of a problem

Can't I unpack both
the key and value from FixtureList?

Assuming that FixtureList is a dict (why name it List?), you can use:

#python 2.0
for fixture, ractimes in FixtureList.iteritems():
    print fixture, racetimes

#python 3.0
for fixture, ractimes in FixtureList.items():
    print(fixture, racetimes)


Another thing I'd like to change is the ljust().  I believe it's
depreciated later in 3.0+, so I should really find an alternative.

No it isn't. What is deprecated is the 'string' module, in favor of "string method".

i.e. instead of:

import string
s = 'abc'
t = string.ljust(s, 10)

you should use

s = 'abc'
t = s.ljust(10)


And the last tips: http://www.python.org/dev/peps/pep-0008/

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to