[Tutor] my pet project (long)

Andrzej Kolinski AKolinski@nriindustries.com
Mon, 4 Feb 2002 09:31:30 -0500


--0__=0ABBE1C5DFDC154E8f9e8a93df938690918c0ABBE1C5DFDC154E
Content-type: text/plain; charset=us-ascii


Hi,

I worked a bit with my game file and was able to retrieve information I
will, I imagine, be using later on to do my calculations.

import string
gameFile = open("c:\\Python22\\work\\0123x.txt","r")
for line in gameFile.readlines():
    line = string.strip(line)
    outData = string.split(line)
    gameList1 = outData[1], outData[3:5]
    gameList2 = outData[1], outData[6:8]
    print gameList1
    print gameList2
gameFile.close()

In order to get these lists of names and percentages I had to remove by
hand from the file:
- first three lines or so,
- one column with MPs.
(of course, for another game file I would have to do different weeding out
as there are two sets of results, NS and WE and the MPs column is shorter).
I tried to create some kind of results file (cPickle, shelf,...) but it
didn't work for me.

How can this be programmed?
Is this set of list good enough for future use or it has to be saved as a
file for example and how to do this?

(See attached file: 0123x.txt)(See attached file: 0116x.txt)

Thank you for your help so far and in advance as I will be asking you many,
many times to come.

         _/_/       _/     _/
    _/       _/   _/    _/
  _/_/_/_/   _/_/
 _/      _/   _/    _/
_/     _/   _/      _/

      Andrzej


                                                                                                           
                      Danny Yoo                                                                            
                      <dyoo@hkn.eecs.be        To:       Andrzej Kolinski <AKolinski@nriindustries.com>    
                      rkeley.edu>              cc:       tutor@python.org, andrzejk@idirect.com            
                      Sent by:                 Subject:  Re: [Tutor] my pet project (long)                 
                      tutor-admin@pytho                                                                    
                      n.org                                                                                
                                                                                                           
                                                                                                           
                      01/31/02 05:04 PM                                                                    
                                                                                                           
                                                                                                           




On Tue, 29 Jan 2002, Andrzej Kolinski wrote:

> I am a total newbie surrounded by Python books and tutorials. Before I
> became the one (this was not difficult :-)) I tried to learn a bit of
> C (I am glad I did not). I found Python and this mailing are the best
> things for what I dream to do.

I'm glad to hear you're enjoying things.


> This is my pet project. I would like to write a python program that
> allows me to maintain players' ranking for our bridge club.

This sounds good.  Let's see some of the things that you're planning:


> - how to get the data from the results file, i.e. how to create a
> table/file with each player's name associated with the pair's Pct
> (what type of file/string operation applies)?

You'll want to get familiar with opening files and reading whole strings
from them.  A secondary thing is to pull out individual chunks of string.

Most tutorials talk about the input and output (IO) that's involved in
reading files.  Alan's tutorial talks about opening files here:

    http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm

To pull out individual columns from a line of text, we can using a
function called string.split().  An example might help show what it does:


###
>>> sentence = "the digital information that underlies biochemistry,
cell biology, and development can be represented by a simple string of
G's, A's, T's and C's."
>>>
>>> string.split(sentence)
['the', 'digital', 'information', 'that', 'underlies', 'biochemistry,',
'cell', 'biology,', 'and', 'development', 'can', 'be', 'represented',
'by', 'a', 'simple', 'string', 'of', "G's,", "A's,", "T's", 'and', "C's."]
>>>
>>> string.split(sentence, ',')
['the digital information that underlies biochemistry', ' cell biology',
" and development can be represented by a simple string of G's", " A's",
" T's and C's."]
###

This example shows that we can break up a string into a list of smaller
strings, and we have some control over which delimiters Python decides to
split on.  By default, string.split() will split things up, treating
spaces as column boundaries.  Play around with string.split() a bit, and
you'll get the hang of it quickly.



It sounds like you also want to fiddle around with lists.

> I want to include the last 8 games in these calculations and produce a

For this case, we can say "last 8 elements of a list of games" with
something like this:

    games[-8:]

This is called an array slice, and it's very nice because it does allow us
to use negative indices, which count backwards from the end of our list.


> I want to include the last 8 games in these calculations and produce a
> current club ranking list for players that participated at least in 4
> out of the last 8 games.

This is very doable.  One way we can make this easier to write is to make
a function that tells us how many games a person has played in a list of
games:

###
def countPlayerParticipation(player, games):
    """Returns the number of times a player has participated in
    games."""
    # ... dunno what this looks like yet...
###

If we had such a function, then we can just march down a list of all the
players, and pick out the ones in which countPlayerParticipation() is
greater than 4.


This sounds like a great project!  Keep us informed of your progress, and
please feel free to ask questions as you're writing it.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



--0__=0ABBE1C5DFDC154E8f9e8a93df938690918c0ABBE1C5DFDC154E
Content-type: application/octet-stream; 
	name="0123x.txt"
Content-Disposition: attachment; filename="0123x.txt"
Content-transfer-encoding: base64

ICA2ICAgNjIuODggICQxMDUuNjQgICAJCSAgICAgRGFuaWVsYSBBbGVqc2thIC0gWm9maWEgS29u
aXR6ZXINCiAgMiAgIDYxLjE4ICAkMTAyLjc5ICAgCQkgICAgIEFuZHJ6ZWogS29saW5za2kgLSBT
dGFuaXNsYXcgUmFsY2V3aWN6DQogMTYgICA1NS45MSAgICQ5My45MyAgIAkJICAgICBBbGVrc2Fu
ZGVyIENpY2hvY2tpIC0gWmJpZ25pZXcgUGFua2lld2ljeg0KIDExICAgNTUuNDkgICAkOTMuMjIg
ICAJCSAgICAgV2llc2xhdyBTemN6ZXJib3dza2kgLSBaYmlnbmlldyBaaW9sa293c2tpDQogIDMg
ICA1NS40OCAgICQ5My4yMSAgIAkJICAgICBIZW5yeWsgUmFzdG9uIC0gVGFkZXVzeiBXaWRtb250
DQogMTMgICA1My4wMiAgICQ4OS4wNyAgICAgICAgICAgICAgICAgS3J6eXN6dG9mIEtvd2Fsc2tp
IC0gTWllY3p5c2xhdyBMYXNrb3dza2kNCiAxNCAgIDUyLjQyICAgJDg4LjA2ICAgICAgICAgICAg
ICAgICBBZGFtIFphY2hhcmEgLSBCcnVubyBaZHppZW5pY2tpDQogMTAgICA1Mi4yMSAgICQ4Ny43
MiAgICAgICAgICAgICAgICAgUmFkZWsgQ2hyYWJhbG93c2tpIC0gQW5kcnplaiBLaXRhDQogMTIg
ICA0OS4zMSAgICQ4Mi44NCAgICAgICAgICAgICAgICAgQmFyYmFyYSBGaWxpcGN6dWsgLSBKZXJ6
eSBIcnluaWV3aWVja2kNCiAgNSAgIDQ2Ljg5ICAgJDc4Ljc4ICAgICAgICAgICAgICAgICBKYW51
c3ogSGFudXMgLSBNYWxnb3J6YXRhIEx1a2FzaWFrDQogIDEgICA0NS45NiAgICQ3Ny4yMSAgICAg
ICAgICAgICAgICAgU3RhbmlzbGF3IEdydWR6aW5za2kgLSBBbmRyemVqIEhlbXBlbA0KICA3ICAg
NDQuMTMgICAkNzQuMTQgICAgICAgICAgICAgICAgIEtyeXN0eW5hIFN6YWRrb3dza2EgLSBSb2Jl
cnQgU3pjenVyZWsNCiAxNSAgIDQyLjkwICAgJDcyLjA3ICAgICAgICAgICAgICAgICBJcmVuZXVz
eiBCYWN6ZWsgLSBGcmVkIFN6b2JsaWsNCiAgOCAgIDQyLjAyICAgJDcwLjYwICAgICAgICAgICAg
ICAgICBSeXN6YXJkIExld2FuZG93c2tpIC0gWmJpZ25pZXcgU2VrdWxhDQogIDQgICA0MC4zOSAg
ICQ2Ny44NiAgICAgICAgICAgICAgICAgSnVsaWFuIEdhbGFzIC0gV2FuZGEgR2FsYXMNCiAgOSAg
IDM5LjgwICAgJDY2Ljg3ICAgICAgICAgICAgICAgICBXb2pjaWVjaCBTYW1ib3IgLSBNYXJ5bGEg
V2FjaG93aWFrDQo=

--0__=0ABBE1C5DFDC154E8f9e8a93df938690918c0ABBE1C5DFDC154E
Content-type: application/octet-stream; 
	name="0116x.txt"
Content-Disposition: attachment; filename="0116x.txt"
Content-transfer-encoding: base64

DQpUdXJuaWVqIFBhcmFtaSAoTUFYLVkpIFdlZG5lc2RheSBFdmUgU2Vzc2lvbiBKYW51YXJ5IDE2
LCAyMDAyDQoNCg0KU2NvcmVzIGFmdGVyIDI4IGJvYXJkcyAgQXZlcmFnZTogICA4NC4wICAgICAg
U2VjdGlvbiAgQSAgTm9ydGgtU291dGgNClBhaXIgICAgUGN0ICAgU2NvcmUgIFJhbmsgICBNUHMg
ICAgIA0KICA3ICAgNjYuOTYgIDExMi41MCAgIDEgICAgMC44MCAgICAgQWRhbSBLb2h1dCAtIFpi
aWduaWV3IFN0YW5raWV3aWN6DQogIDEgICA1NS4wNiAgIDkyLjUwICAgMiAgICAwLjU2ICAgICBU
YWt5IEJvaG9zc2lhbiAtIFN0YW5pc2xhdyBSYWxjZXdpY3oNCiAgMiAgIDQ4LjUxICAgODEuNTAg
IDMvNCAgIDAuMzQ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