Re: [Tutor] printing out a box of O's
- Original Message - From: "Kevin" <[EMAIL PROTECTED]> To: Sent: Tuesday, March 01, 2005 12:35 AM Subject: [Tutor] printing out a box of O's > there a better way to do > this: > > j = 'O' > for i in j*10: > print i * 100 Its not bad, but the for loop could be 'simplified' to: for i in range(10): print j*100 its not any shorter and probably doesn't run much faster but its a lot more readable because its the conventional Python idiom for coding fixed length loops. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and a web image map
> I have been doing Python for a bit now but I am trying to make a clickable > map of the world on a web page that gives me the latitude and longitude of a > location selected. I have done little with HTML beyond forms and have done > no Java script. Is this a problem Python can solve or is this a HTML / Java > script issue. Personally I'd go with JavaScript on this one. It has a built-in knowledge of the web browser interface and document which makes extracting the mouse location etc much easier. Its possible in Python but IMHO is easier in JavaScript on the client. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: How to read unicode strings from a binary file and display them as plain ascii?
R. Alan Monroe wrote: I started writing a program to parse the headers of truetype fonts to examine their family info. But I can't manage to print out the strings without the zero bytes in between each character (they display as a black block labeled 'NUL' in Scite's output pane) I tried: stuff = f.read(nlength) stuff = unicode(stuff, 'utf-8') If there are embeded 0's in the string, it won't be utf8, it could be utf16 or 32. Try: unicode(stuff, 'utf-16') or stuff.decode('utf-16') print type(stuff), 'stuff', stuff.encode() This prints: stuff [NUL]C[NUL]o[NUL]p[NUL]y[NUL]r[NUL]i[NUL]g[NUL] I don't understand what you tried to accomplish here. Apparently I'm missing something simple, but I don't know what. Try the other encodings. It probably is utf-16. Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Edonkey Automatic Search Program?!
. , wrote: Hi, I just want to know whether this program can be programmed by python or not. p2p program like edonkey is very very complicated (I think so..) but, is searching program for edonkey complicated too? Should the search program be connected to edonkey? (I think so..) The Edonkey Automatic Search Program should be like... i input some words and the prgram automatically download files which are related to words by days, hours. I'm looking forward to see your replys... P2P complicated? Please! Check http://www.freedom-to-tinker.com/tinyp2p.html out. ;) Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to read unicode strings from a binary file and display them as plain ascii?
I started writing a program to parse the headers of truetype fonts to examine their family info. But I can't manage to print out the strings without the zero bytes in between each character (they display as a black block labeled 'NUL' in Scite's output pane) I tried: stuff = f.read(nlength) stuff = unicode(stuff, 'utf-8') print type(stuff), 'stuff', stuff.encode() This prints: stuff [NUL]C[NUL]o[NUL]p[NUL]y[NUL]r[NUL]i[NUL]g[NUL] Apparently I'm missing something simple, but I don't know what. Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Criticism / Suggestions
Hello, So I think that I've 'completed' my first real Python program, and I would appreciate any constructive criticism you all could offer. The program deals with a question that my Dad asked me awhile ago, which was "If twelve people want to divide into teams of two and play (golf) against each other, can a series of rounds be constructed such that each player is teammates with each other player only once, and play against each other as opponents no more then 3 times" ( that last bit might or might not be 'optimal'. My program: 1. Defines a round as a list, for example [1,2,3,4,5,6,7,8,9,10,11,12], meaning player 1 & player 2 vs. player 3 & player 4, etc. I have generated all such possible rounds ( I think ). 2. Defines a tournament iterator object, which uses two functions, checkTeammates and checkOpponents, to build a tournament satisfying the above criteria. Like I mentioned before, this is my first fairly complex program, and is also my first real use of things like exceptions, objects, and list comprehensions. Basically I would like to know weather or not I used these structures properly, weather or not my syntax is good, and if there are any places with potential for improvement. ( This version is somewhat slow, but is much faster than previous versions that I have written. ) I've tried to have as many comments as possible to help readability. Code aside, my algorithm may or may not be the best. Feel free to suggest improvements. The code is located at http://rafb.net/paste/results/lrd5DG32.html. Thanks for any thoughts! Bill ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and a web image map
> Save the above as an HTM and click, it should give you the x,y co-ords > for the browser window excluding scrolbars etc. It is possible to do this with Python, since a server-side HTML ISMAP will send its coordinates off as part of the request. There are some notes here: http://www.algonet.se/~ug/html+pycgi/img.html The coordinates we get back are in terms of the pixels of the image, so translating to a latitidue/longitude system will probably take some more work. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] printing out a box of O's
for y in range(10): for x in range(10): print "O", print '\n' Or - for y in range(10): print "O"*10 On Mon, 28 Feb 2005 18:35:08 -0600, Kevin <[EMAIL PROTECTED]> wrote: > I just started getting in to python and for taking a look at the for > loop. I want to print out a box > of O's 10o chars long by 10 lines long this is what I came up with. Is > there a better way to do > this: > > j = 'O' > for i in j*10: > print i * 100 > > Thanks > > Kevin > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] printing out a box of O's
I just started getting in to python and for taking a look at the for loop. I want to print out a box of O's 10o chars long by 10 lines long this is what I came up with. Is there a better way to do this: j = 'O' for i in j*10: print i * 100 Thanks Kevin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
I attempting to control xfmedia, http://spuriousinterrupt.org/projects/xfmedia/ , via it's remote from python and I can not get a connection. s = socket.fromfd('/tmp/xfmedia_remote.1001.0', socket.AF_UNIX, socket.SOCK_STREAM) i get this error ", line 1, in ? f = open('/tmp/xfmedia_remote.1001.0') Traceback (most recent call last): File "", line 1, in ? IOError: [Errno 6] No such device or address: '/tmp/xfmedia_remote.1001.0' yet ls shows the file exists, xfmedia is working fine. I have not done this before, but I think you need to create a _new_ socket for your end, and then connect to the other socket. Does that make more sense? _ Don't just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Python and a web image map
Liam, Thanks for the code chunk and the advice. Java script here I come. John Ertl -Original Message- From: Liam Clarke [mailto:[EMAIL PROTECTED] Sent: Monday, February 28, 2005 13:27 To: Tutor Tutor Subject: Re: [Tutor] Python and a web image map I would say it's best done as a Javascript thing. function goFunc(e){ x = e.clientX y = e.clientY alert("X=" + x + " Y=" + y) } window.onload = function(e){document.onclick = goFunc;}; Javascript or Python? Save the above as an HTM and click, it should give you the x,y co-ords for the browser window excluding scrolbars etc. Shouldn't be too hard to create a window that matches the size of your map, and convert window co-ords into image co-ords. Good luck with that, though, Javascript is a funny beast, but there's some good tutorials out there. Regards, Liam Clarke On Mon, 28 Feb 2005 12:00:09 -0800, Ertl, John <[EMAIL PROTECTED]> wrote: > > > All, > > I have been doing Python for a bit now but I am trying to make a clickable > map of the world on a web page that gives me the latitude and longitude of a > location selected. I have done little with HTML beyond forms and have done > no Java script. Is this a problem Python can solve or is this a HTML / Java > script issue. > > Thanks, > > John Ertl > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and a web image map
I would say it's best done as a Javascript thing. function goFunc(e){ x = e.clientX y = e.clientY alert("X=" + x + " Y=" + y) } window.onload = function(e){document.onclick = goFunc;}; Javascript or Python? Save the above as an HTM and click, it should give you the x,y co-ords for the browser window excluding scrolbars etc. Shouldn't be too hard to create a window that matches the size of your map, and convert window co-ords into image co-ords. Good luck with that, though, Javascript is a funny beast, but there's some good tutorials out there. Regards, Liam Clarke On Mon, 28 Feb 2005 12:00:09 -0800, Ertl, John <[EMAIL PROTECTED]> wrote: > > > All, > > I have been doing Python for a bit now but I am trying to make a clickable > map of the world on a web page that gives me the latitude and longitude of a > location selected. I have done little with HTML beyond forms and have done > no Java script. Is this a problem Python can solve or is this a HTML / Java > script issue. > > Thanks, > > John Ertl > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
> As an added bonus, you can also create a system environment variable > called PATHEXT and set it to .py and you won't even have to type the .py Well, well, well, you live and learn! :-) Thanks for that neat tip. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable name based on variables (expansion?)
> # I have some lists > GameLogic.varList0=[1,1,1,1] > GameLogic.varList1=[1,1,1,1] > GameLogic.varList3=[1,1,1,1] Pythonically: GameLogic.varLists = [[1,1,1,1], [1,1,1,1], [1,1,1,1]] > # But I want the assignment > # to be based on variables > LIST=1 > POSITION=2 > > GameLogic.varList$LIST[$POSITION]=0 GameLogic.varlists[LIST][POSITION] = 0 > # I've also tried variations of eval(), single ticks, > # back ticks, quotes, etc... but I just can't seem > # to get the syntax right. You could use eval but its nasty and should be avoided if possible. Its usually better to eitrher use lists or dictionaries. > # if I hardcode things, but I'd rather use variables > # since I will eventually have 20 lists with at least > # 10 positions in each list. In that case using variables will be messy, far better to use a single variable which is a container. If for some reason your lists cannot be contigious then you should opt for a dictionary and manipulate the key names: GameLogic.varLists = {'list0' : [1,1,1,1], 'list1' : [1,1,1,1], 'list3' : [1,1,1,1]} key = 'list' + str(LIST) GameLogic.varlists[key][POSITION] = 0 HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Edonkey Automatic Search Program?!
You sure could write a client for the eDonkey p2p protocol using Python, the original BitTorrent protocol and client was written in Python. You can download the source somewhere on www.bittorrent.org. But yeah, good luck with that. Regards, Liam Clarke On Mon, 28 Feb 2005 19:48:00 +, . , <[EMAIL PROTECTED]> wrote: > Hi, > > I just want to know whether this program can be programmed by python or not. > > p2p program like edonkey is very very complicated (I think so..) > > but, is searching program for edonkey complicated too? > > Should the search program be connected to edonkey? (I think so..) > > The Edonkey Automatic Search Program should be like... > > i input some words and the prgram automatically download files > > which are related to words by days, hours. > > I'm looking forward to see your replys... > > > Cheers! :) > > _ > Express yourself instantly with MSN Messenger! Download today it's FREE! > http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/ > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python and a web image map
All, I have been doing Python for a bit now but I am trying to make a clickable map of the world on a web page that gives me the latitude and longitude of a location selected. I have done little with HTML beyond forms and have done no Java script. Is this a problem Python can solve or is this a HTML / Java script issue. Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Edonkey Automatic Search Program?!
Hi, I just want to know whether this program can be programmed by python or not. p2p program like edonkey is very very complicated (I think so..) but, is searching program for edonkey complicated too? Should the search program be connected to edonkey? (I think so..) The Edonkey Automatic Search Program should be like... i input some words and the prgram automatically download files which are related to words by days, hours. I'm looking forward to see your replys... Cheers! :) _ Express yourself instantly with MSN Messenger! Download today it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.argv[1: ] help
Hi, Thanks a lot to everyone that replied. I was missing the %* in the following line, in the File associations I just had upto the "%1". Adding %* cured my problem. Python.File="C:\Python24\python.exe" "%1" %* Sorry for the typos in some of my examples, every keyboard I've tried appears to have the same fault on it! Anyway thanks a lot again, Richard G. Smith, Jeff wrote: Richard, I have no problems running your example. It would be helpful in the future ot let us know which version and variant of Python you are running. I am using the canonical (as oppose to ActiveState) Python 2.4. From the command prompt, type assoc .py and you should see .py=Python.File Then type ftype Python.File which should return Python.File="C:\Python24\python.exe" "%1" %* If the last one isn't correct (with approriate path and assoc type associations) then you can correct it with ftype ASSOCTYPE=PATHSTUFF As an added bonus, you can also create a system environment variable called PATHEXT and set it to .py and you won't even have to type the .py to execute the script. I added all the following to my PATHEXT: .py;.pyw;.pys;.pyo;.pyc While you're at it, you should also check the assoc/ftype for .pyw as .pyw=Python.NoConFile Python.NoConFile="C:\Python24\pythonw.exe" "%1" %* Good luck, Jeff -Original Message- From: Richard gelling [mailto:[EMAIL PROTECTED] Sent: Sunday, February 27, 2005 1:41 PM To: tutor@python.org Subject: Re: [Tutor] sys.argv[1: ] help Hi, It is actually associated with just 'python', changed it to associate with 'pythonw' and I got nothing on the same example not even the [], so I am assuming that 'python' is the correct one? Liam Clarke wrote: Yeah, right click on a .py and check if it's associated with pythonw or python.exe GL, Liam Clarke On Sun, 27 Feb 2005 18:28:18 +, Richard gelling <[EMAIL PROTECTED]> wrote: Hi, Yes, I use both Wndows XP and Linux( at work ) . I left that in by mistake I am actually just typing in arg1,py a b c at the windows XP command prompt Sorry for the confusion. Liam Clarke wrote: Are you using XP still? I've never seen this before - ./arg1.py a b c But anyhoo, I tried out just 'c:\python23\foo.py' as opposed to 'c:\python23\python foo.py' and while foo.py will run, it doesn't echo to the console, as on my machine running a .py file runs it through pythonw.exe - I'd check it out for your machine, it's probably the same. You'd need to change the association to python.exe, but that would mean that you always got a DOS box for every Python script you ran, which is annoying with GUIs. Erm, if you don't want to type in python each time, either change the association or create a batch file called x or a or something that runs Python and stick it in a directory that's in your PATH system variable. Only problem with that is passing command line variables ...might just be better to type python Good Luck, Liam Clarke On Sun, 27 Feb 2005 17:55:54 +, Richard gelling <[EMAIL PROTECTED]> wrote: Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: Richard, if you try to print sys.argv[1:] when sys.argv only contain sys.argv[0] then you are bound to get an empty list returned, [] . Im not sure I understand the problem you think you've got but here's what happens with sys.argv for me, and it's correct. [argl.py] $ cat argl.py #!/usr/bin/python import sys print sys.argv[1:] ./argl.py [] ./argl.py a b c ['a', 'b', 'c'] Is that what your getting ? Sorry for the late response, I tried all of the the suggestions, including correcting my typo of print sys[1:] and tried print sys,argv[1:], this does now work as long as I run 'python test.py fred joe' it returns all the arguments. If I try just test.py all I get is '[]' . Is there something wrong with my environmental variables in Windows XP, I would like to be able to just use the file name rather than having to type python each time. Any help would be gratefully received. Richard G. ___ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] gensuitemodule?
http://www.python.org/doc/2.3.5/mac/module-gensuitemodule.html -Original Message- From: Mike Hall [mailto:[EMAIL PROTECTED] Sent: Friday, February 25, 2005 7:19 PM To: tutor@python.org Subject: [Tutor] gensuitemodule? I'm seeing it used in a Python/Applescript tutorial, though am unclear on it's exact purpose or usage. Can someone fill me in? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] sys.argv[1: ] help
Richard, I have no problems running your example. It would be helpful in the future ot let us know which version and variant of Python you are running. I am using the canonical (as oppose to ActiveState) Python 2.4. >From the command prompt, type assoc .py and you should see .py=Python.File Then type ftype Python.File which should return Python.File="C:\Python24\python.exe" "%1" %* If the last one isn't correct (with approriate path and assoc type associations) then you can correct it with ftype ASSOCTYPE=PATHSTUFF As an added bonus, you can also create a system environment variable called PATHEXT and set it to .py and you won't even have to type the .py to execute the script. I added all the following to my PATHEXT: .py;.pyw;.pys;.pyo;.pyc While you're at it, you should also check the assoc/ftype for .pyw as .pyw=Python.NoConFile Python.NoConFile="C:\Python24\pythonw.exe" "%1" %* Good luck, Jeff -Original Message- From: Richard gelling [mailto:[EMAIL PROTECTED] Sent: Sunday, February 27, 2005 1:41 PM To: tutor@python.org Subject: Re: [Tutor] sys.argv[1: ] help Hi, It is actually associated with just 'python', changed it to associate with 'pythonw' and I got nothing on the same example not even the [], so I am assuming that 'python' is the correct one? Liam Clarke wrote: >Yeah, right click on a .py and check if it's associated with pythonw or >python.exe > >GL, > >Liam Clarke > > >On Sun, 27 Feb 2005 18:28:18 +, Richard gelling ><[EMAIL PROTECTED]> wrote: > > >>Hi, >>Yes, I use both Wndows XP and Linux( at work ) . I left that in by >>mistake I am actually just typing in >> >>arg1,py a b c >> >>at the windows XP command prompt >> >>Sorry for the confusion. >> >> >>Liam Clarke wrote: >> >> >> >>>Are you using XP still? I've never seen this before - >>> >>> >>> >>> ./arg1.py a b c >>>But anyhoo, I tried out just >>>'c:\python23\foo.py' >>>as opposed to >>>'c:\python23\python foo.py' and >>>while foo.py will run, it doesn't echo to the console, as on my >>>machine running a .py file runs it through pythonw.exe - I'd check it >>>out for your machine, it's probably the same. You'd need to change >>>the association to python.exe, but that would mean that you always >>>got a DOS box for every Python script you ran, which is annoying with >>>GUIs. >>> >>>Erm, if you don't want to type in python each time, either change the >>>association or create a batch file called x or a or something that >>>runs Python and stick it in a directory that's in your PATH system >>>variable. Only problem with that is passing command line >>>variables >>> >>>...might just be better to type python >>> >>>Good Luck, >>> >>>Liam Clarke >>> >>>On Sun, 27 Feb 2005 17:55:54 +, Richard gelling >>><[EMAIL PROTECTED]> wrote: >>> >>> >>> >>> Hi, No What I get if I was to type in ./arg1.py a b c All I get is [] If i type at the command prompt python arg1.py a b c I get ['a','b','c'] as expected All the other programs and examples I have typed in work fine just by typing in the file name, I don't have to preced the file name with python, only this example. I hope this makes it clearer Richard G. Nick Lunt wrote: >Richard, > >if you try to print sys.argv[1:] when sys.argv only contain >sys.argv[0] then you are bound to get an empty list returned, [] . > >Im not sure I understand the problem you think you've got but >here's what happens with sys.argv for me, and it's correct. > >[argl.py] > >$ cat argl.py >#!/usr/bin/python > >import sys >print sys.argv[1:] > > >./argl.py >[] > >./argl.py a b c >['a', 'b', 'c'] > >Is that what your getting ? > > > > > > > > > > >>Sorry for the late response, I tried all of the the suggestions, >>including correcting my typo of print sys[1:] and tried print >>sys,argv[1:], this does now work as long as I run 'python test.py >>fred joe' it returns all the arguments. If I try just test.py all >>I get is '[]' . Is there something wrong with my environmental >>variables in Windows XP, I would like to be able to just use the >>file name rather than having to type python each time. Any help >>would be gratefully received. >> >>Richard G. ___ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] puzzling traceback -- what to do with it?
On Mon, 28 Feb 2005 02:02:22 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: > Hi all, > > I just ran a program of mine and got the traceback: > > >>> > Traceback (most recent call last): >File "C:\PYTHON24\lib\idlelib\rpc.py", line 233, in asyncqueue > self.putmessage((seq, request)) >File "C:\PYTHON24\lib\idlelib\rpc.py", line 333, in putmessage > raise IOError > IOError > > This stumps me, as I've almost no idea what rpc.py, putmessage, and > asyncqueue are. (A quick glance at the code made me realize my > code-reading skills and knowledge of IDLE aren't up to tracking this > down on my own.) > > Furthermore, the code that produced this runs just fine via other > methods (running from within SciTe, the python command line, etc.) > And, sometimes, but not always, closing the offending code, running > something else, and then trying again with the code that caused the > traceback makes it work. > > I'm thinking IDLE bug, but also that it would be a good idea to > solicit opinions/expertise here before running off screaming BUG to > the IDLE folks :-) > Looks like an IDLE bug to me. Either it's a bug with them, or you're interfering with something they do; either way it looks like those guys are likely gonna have to help you out. The fact that the errors are in idlelib leads me to this conclusion - unless you're importing idlelib into your program, an error there is an error with idle. Having used IDLE (maybe) once or twice, you should take this with a grain of salt, but asking them about this error seems to be a good bet. > Any suggestions for what the problem might be, or how to narrow it > down before reporting? I would just give them a link to the smallest bit of source code you can get to reproduce this problem, and an exact list of steps for how to repeat it consistently. Also tell them your OS, python version, and IDLE version. I'm also assuming you already googled the traceback and searched their mailing list to see if it's a well-known problem. Peace Bill Mill bill.mill at gmail.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable name based on variables (expansion?)
John, On Mon, 28 Feb 2005 06:05:44 -0800 (PST), John Christian <[EMAIL PROTECTED]> wrote: > a python 2.3 noob asks: > > # I have some lists > GameLogic.varList0=[1,1,1,1] > GameLogic.varList1=[1,1,1,1] > GameLogic.varList3=[1,1,1,1] > > # I want to change specific list elements > GameLogic.varList0[2]=0 > print GameLogic.varList0 > [1,1,0,1] > > # But I want the assignment > # to be based on variables > LIST=1 > POSITION=2 > > GameLogic.varList$LIST[$POSITION]=0 > Most likely you want to have a 2-dimensional list. Like so: #note the lists inside a list GameLogic.varMatrix=[[1,1,1,1], [1,1,1,1], [1,1,1,1]] Then, to get to the second element in the first list, do: GameLogic.varMatrix[0][1] Or the third element of the second list: GameLogic.varMatrix[1][2] In general, using zero-based counting of rows and columns, accessing the array is done with: GameLogic.varMatrix[row][column] so access like you have above is just: GameLogic.varMatrix[LIST][POSITION] Assuming that LIST and POSITION are zero-based counts. Also note that all-CAPS variables in python are traditionally used only for constants. This is a pretty strong tradition which, if you break, will confuse anyone trying to read your code. Variables are traditionally either camel case or all lower-case with underscores. > # But the variable assignment above does not work. > # Python complains of a syntax error. > # I've also tried variations of eval(), single ticks, > # back ticks, quotes, etc... but I just can't seem > # to get the syntax right. > # > # Can someone please provide a working example? > the $ is not a syntactic character in python. Single quotes simply delimit strings in python. Back ticks are equivalent to the str() function which creates strings (or is it repr()? I can't remember; it's generally bad form to use back ticks anyway). Double quotes are the same as single quotes. Please read the tutorial at http://docs.python.org/tut/tut.html . Peace Bill Mill bill.mill at gmail.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] variable name based on variables (expansion?)
John Christian wrote: # But I want the assignment # to be based on variables LIST=1 POSITION=2 GameLogic.varList$LIST[$POSITION]=0 >>> help(getattr) Help on built-in function getattr: getattr(...) getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. from http://docs.python.org/lib/built-in-funcs.html *getattr*( object, name[, default]) Return the value of the named attributed of object. name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. For example, |getattr(x, 'foobar')| is equivalent to |x.foobar|. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised. #--- LIST = 1 POSITION = 2 getattr(GameLogic, 'varList'+str(LIST))[POSITION] = 0 __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] variable name based on variables (expansion?)
a python 2.3 noob asks: # I have some lists GameLogic.varList0=[1,1,1,1] GameLogic.varList1=[1,1,1,1] GameLogic.varList3=[1,1,1,1] # I want to change specific list elements GameLogic.varList0[2]=0 print GameLogic.varList0 [1,1,0,1] # But I want the assignment # to be based on variables LIST=1 POSITION=2 GameLogic.varList$LIST[$POSITION]=0 # But the variable assignment above does not work. # Python complains of a syntax error. # I've also tried variations of eval(), single ticks, # back ticks, quotes, etc... but I just can't seem # to get the syntax right. # # Can someone please provide a working example? # In case it matters, here are additional details: # My python script imports a module named GameLogic. # My script is called fresh every time it's used; # therefore, some variables must be stored in locations # such as GameLogic.varA, GameLogic.varB, etc... so # they are available for later use. It all works fine # if I hardcode things, but I'd rather use variables # since I will eventually have 20 lists with at least # 10 positions in each list. TIA! -potus98 __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor