Re: Computing win/loss records in Python

2012-08-26 Thread Hans Mulder
On 26/08/12 04:42:59, Steven W. Orr wrote:
> On 8/25/2012 10:20 PM, Christopher McComas wrote:
>> Greetings,
>>
>> I have code that I run via Django that grabs the results from various
>> sports from formatted text files. The script iterates over every line
>> in the formatted text files, finds the team in the Postgres database
>> updates their w/l record depending on the outcome on that line, saves
>> the team's row in the db, and then moves on to the next line in the file.
>>
>> I'm trying to get away from Django for this project, I want to run the
>> files, get the W/L results and output a formatted text file with the
>> teams and their W/L records. What's confusing me I guess how to store
>> the data/results as the wins and losses tally up. We're talking
>> hundreds of teams, thousands of games, but a quick example would be:
>>
>> Marshall
>> Ohio State
>> Kentucky
>> Indiana
>>
>> Marshall,24,Ohio State,48,
>> Kentucky,14,Indiana,10,
>> Marshall,10,Indiana,7,
>> Ohio State,28,Kentucky,10
>>
>> That's just a quick example, I can handle seperating the data in the
>> lines, figuring it all out, I just am unsure of how to keep a running
>> total of a team's record. I would do "for line in file:" then on the
>> first line I see that Marshall lost so they would have 1, Ohio State
>> won so they'd have 1 win. It'd go to the next line Kentucky 1 win,
>> Indiana 1 loss, then on the 3rd line, Marshall got a win so they'd
>> have 1 win, but it would have to remember that loss from line 1...
>>
>> Does this make sense?
>>
>> Thanks,
> 
> win_count = defaultdict(int)
> loss_count = defaultdict(int)
> 
> items = line.split(',')
> if items[1] > items[3]:
> windex = 0
> lossdex = 2
> else:
> windex = 2
> lossdex = 0
> win_count[windex] += 1
> loss_count[lossdex] += 1

I think you meant:

win_count[items[windex]] += 1
loss_count[items[windex]] += 1


I think it would be more readable to do:

from collections import defaultdict

win_count = defaultdict(int)
loss_count = defaultdict(int)

items = line.split(',')
if int(items[1]) > int(items[3]):
winner = items[0]
loser = items[2]
else:
winner = items[2]
loser = items[0]
win_count[winner] += 1
loss_count[loser] += 1

It looks like you're afraid of wasting RAM by needlessly
copying strings.  However, this fear is unfounded: Python
doesn't copy strings, unless you tell it to do so explictly.
An assignment like "winner = items[0]" doesn't copy the string;
it merely creates a new reference to the existing string.


Hope this helps,

-- HansM
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Computing win/loss records in Python

2012-08-25 Thread Ben Finney
Christopher McComas  writes:

> I have code that I run via Django that grabs the results from various
> sports from formatted text files. The script iterates over every line
> in the formatted text files, finds the team in the Postgres database
> updates their w/l record depending on the outcome on that line, saves
> the team's row in the db, and then moves on to the next line in the
> file.

It seems that you already have a PostgreSQL database storing this data.

> I'm trying to get away from Django for this project

That existing database can be accessed without Django. You could talk
directly using the ‘psycopg2’ library, but you don't have to go that
far.

I would recommend you use SQLAlchemy as a good and flexible way to
access existing databases (or make new ones) in a Pythonic manner
http://www.sqlalchemy.org/>. If you are using a free-software
operating system, you will likely already have packages available to
install SQLAlchemy from your operating system's package repositories.

-- 
 \ “True greatness is measured by how much freedom you give to |
  `\  others, not by how much you can coerce others to do what you |
_o__)   want.” —Larry Wall |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Computing win/loss records in Python

2012-08-25 Thread Steven D'Aprano
On Sat, 25 Aug 2012 22:20:05 -0400, Christopher McComas wrote:

> Marshall,24,Ohio State,48,
> Kentucky,14,Indiana,10,
> Marshall,10,Indiana,7,
> Ohio State,28,Kentucky,10
> 
> That's just a quick example, I can handle seperating the data in the
> lines, figuring it all out, I just am unsure of how to keep a running
> total of a team's record. I would do "for line in file:" then on the
> first line I see that Marshall lost so they would have 1, Ohio State won
> so they'd have 1 win. It'd go to the next line Kentucky 1 win, Indiana 1
> loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but
> it would have to remember that loss from line 1...

There are many ways to do this. Here's one: we keep three sets of data, 
wins, losses and ties.

wins = {}
losses = {}
ties = {}
for line in open("datafile.txt"):
line = line.strip()  # get rid of leading and trailing whitespace
line = line.rstrip(',')  # and any trailing comma
teamA, scoreA, teamB, scoreB = line.split(',')  # split on commas
teamA = teamA.strip().title()  # normalise the case
teamB = teamB.strip().title()
scoreA = int(scoreA)
scoreB = int(scoreB)
if scoreA == scoreB:
# Handle a draw.
ties[teamA] = ties.get(teamA, 0) + 1
ties[teamB] = ties.get(teamB, 0) + 1
else:
if scoreA > scoreB:
winner = teamA
loser = teamB
else:
winner = teamB
loser = teamA
wins[winner] = wins.get(winner, 0) + 1
losses[loser] = losses.get(loser, 0) + 1


Once you've done that, you can check the win/loss score of any team:

name = 'Marshall'
w = wins.get(name, 0)
l = losses.get(name, 0)
d = ties.get(name, 0)
total = w+l+d
print(
  "Team %s played %d games, won %d, lost %d and tied %d."
  % (name, total, w, l, d)
  )


If you want to store these results permanently, you need to write them 
out to file. You can roll your own, but a simpler way might be to use one 
of the pickle, json, csv or plistlib modules to do it.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Computing win/loss records in Python

2012-08-25 Thread Steven D'Aprano
On Sat, 25 Aug 2012 22:42:59 -0400, Steven W. Orr wrote:

> win_count = defaultdict(int)
> loss_count = defaultdict(int)

When I try that, I get "NameError: name 'defaultdict' is not defined."

I think it is rather unfair on beginners to show them code that almost, 
but not quite, works, and expect them to somehow work out what this 
mysterious "defaultdict" is.

The answer is probably to do this first:

from collections import defaultdict


> items = line.split(',')
> if items[1] > items[3]:
>  windex = 0
>  lossdex = 2

That's not going to work, because you are doing string comparisons 
instead of numeric comparisons. Consider:

Kentucky,6,Indiana,59

'6' > '59' and you will wrongly count that as a win to Kentucky.


> else:
>  windex = 2
>  lossdex = 0
> win_count[windex] += 1
> loss_count[lossdex] += 1

And that certainly won't work, because all you are doing is counting how 
many times the first team beats the second, instead of counting how many 
times each team wins.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Computing win/loss records in Python

2012-08-25 Thread Steven W. Orr

On 8/25/2012 10:20 PM, Christopher McComas wrote:

Greetings,

I have code that I run via Django that grabs the results from various sports 
from formatted text files. The script iterates over every line in the formatted 
text files, finds the team in the Postgres database updates their w/l record 
depending on the outcome on that line, saves the team's row in the db, and then 
moves on to the next line in the file.

I'm trying to get away from Django for this project, I want to run the files, 
get the W/L results and output a formatted text file with the teams and their 
W/L records. What's confusing me I guess how to store the data/results as the 
wins and losses tally up. We're talking hundreds of teams, thousands of games, 
but a quick example would be:

Marshall
Ohio State
Kentucky
Indiana

Marshall,24,Ohio State,48,
Kentucky,14,Indiana,10,
Marshall,10,Indiana,7,
Ohio State,28,Kentucky,10

That's just a quick example, I can handle seperating the data in the lines, figuring it 
all out, I just am unsure of how to keep a running total of a team's record. I would do 
"for line in file:" then on the first line I see that Marshall lost so they 
would have 1, Ohio State won so they'd have 1 win. It'd go to the next line Kentucky 1 
win, Indiana 1 loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but 
it would have to remember that loss from line 1...

Does this make sense?

Thanks,


win_count = defaultdict(int)
loss_count = defaultdict(int)

items = line.split(',')
if items[1] > items[3]:
windex = 0
lossdex = 2
else:
windex = 2
lossdex = 0
win_count[windex] += 1
loss_count[lossdex] += 1

Zat help?


--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: Computing win/loss records in Python

2012-08-25 Thread Rodrick Brown
On Aug 25, 2012, at 10:22 PM, Christopher McComas
 wrote:

> Greetings,
>
> I have code that I run via Django that grabs the results from various sports 
> from formatted text files. The script iterates over every line in the 
> formatted text files, finds the team in the Postgres database updates their 
> w/l record depending on the outcome on that line, saves the team's row in the 
> db, and then moves on to the next line in the file.
>
> I'm trying to get away from Django for this project, I want to run the files, 
> get the W/L results and output a formatted text file with the teams and their 
> W/L records. What's confusing me I guess how to store the data/results as the 
> wins and losses tally up. We're talking hundreds of teams, thousands of 
> games, but a quick example would be:
>
> Marshall
> Ohio State
> Kentucky
> Indiana
>
> Marshall,24,Ohio State,48,
> Kentucky,14,Indiana,10,
> Marshall,10,Indiana,7,
> Ohio State,28,Kentucky,10
>
> That's just a quick example, I can handle seperating the data in the lines, 
> figuring it all out, I just am unsure of how to keep a running total of a 
> team's record. I would do "for line in file:" then on the first line I see 
> that Marshall lost so they would have 1, Ohio State won so they'd have 1 win. 
> It'd go to the next line Kentucky 1 win, Indiana 1 loss, then on the 3rd 
> line, Marshall got a win so they'd have 1 win, but it would have to remember 
> that loss from line 1...
>
> Does this make sense?

Yes, use a RDBMS, SQLite may be the best fit for your use case its
quick and has low overhead.


>
> Thanks,
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list