PDA

View Full Version : Moved --> Geek Forum: Python


Immortal Wombat
24-03-2003, 22:34:07
All snake-related discussion, go to the inevitable copysnake thread.

I can't seem to do global variables. They either stay constant at the number I set them, or aren't global properly... :cry:

I want a value to change depending on user input.

Roughly speaking:

CAVELIGHT = 0

def Room1()
go = raw_input("> ")
if(go == "LIGHT"):
CAVELIGHT = 1
if(CAVELIGHT == 1):
print("blah blah")


def Room2():
go = raw_input("> ")
if(CAVELIGHT == 1):
print("foo")


Room1()
Room2()


I get the "blah blah" fine: the variable holds value, then it resets to 0 before Room2, and I don't get a "foo"

Anyone who didn't understand that can legally shoot me under President Bush's new anti-geek legislation.

Debaser
24-03-2003, 22:36:23
:shoot:

Sean
24-03-2003, 23:30:34
def Room1():
global CAVELIGHT
go = raw_input("> ")
if(go == "LIGHT"):
CAVELIGHT = 1
if(CAVELIGHT == 1):
print("blah blah")

Alternatively:
def Room1():
import __main__
go = raw_input("> ")
if(go == "LIGHT"):
__main__.CAVELIGHT = 1
if(__main__.CAVELIGHT == 1):
print("blah blah")

Sean
24-03-2003, 23:38:46
The second is worse, I think, unless you need to have a local CAVELIGHT and a global CAVELIGHT.

Sir Penguin
25-03-2003, 01:18:31
I always wondered what the global keyword did. The help() description for it is terrible.

Alternatively:


import string
class Room:
def __init__(self):
self.cavelight = False
def setLight(self):
go = string.lower( raw_input("> ") )
if go == "light": self.cavelight = True
if self.cavelight: print "blah blah"

rooms = []
for i in (0,1):
rooms.append( Room() )
rooms[i].setLight()


Only modified to make it work with the rest of the code and actually do what you want. :)

SP

Sean
25-03-2003, 01:42:32
You show-off with your instances in lists and then your iteration over them.

Sir Penguin
25-03-2003, 01:57:12
Of course, it would be a stupid way of doing things. A static class member for the cavelight boolean would be better if you wanted the rooms to be able to communicate.


import string
class Room:
cavelight = False

def light(self):
"light all rooms ==> self.light()"
Room.cavelight = True
def isLit(self):
"print some crap if rooms are lit ==> self.isLit()"
if Room.cavelight: print "blah " * 2

def move(self): pass
def get(self): pass
def foo(self): pass
# ...

room = Room()
while 1:
go = string.strip( string.lower( raw_input("> ") ) )
validMoves = ("light","move","get","foo")
if go in validMoves: exec "room.%s()" %go
else: continue
otherStuff()


Or something.

SP

Koshko
25-03-2003, 02:14:28
heh

Funkodrom
25-03-2003, 09:55:05
The lights are off in the geek forum and you are trying to write some code to switch them on?

Immortal Wombat
25-03-2003, 19:50:40
close. But thanks Sean and SP.

Yes, my way of doing it will be very stupid: slow, clumsy and unadaptable. Maybe if I feel like it once I've finished I'll rewrite it better, but it'll do for now. :)

Sir Penguin
25-03-2003, 19:57:02
I know what you mean. I've re-written the code for a project I'm working on in Python three times already, and I'm ready to do it a fourth time. :)

SP

Immortal Wombat
26-03-2003, 18:25:02
Sean's first example doesn't work. I'm still not getting a "foo" or equivalent in the first example. The CAVELIGHT value doesn't seem to carry across functions :confused:

Luckily the second one does. Yay.

Sean
26-03-2003, 18:46:42
You have to get the global CAVELIGHT in the Room2 function as well, I think.

Sean
26-03-2003, 19:19:08
No, this should work fine:CAVELIGHT = 0

def Room1():
global CAVELIGHT
go = raw_input("> ")
if(go == "LIGHT"):
CAVELIGHT = 1
if(CAVELIGHT == 1):
print("blah blah")

def Room2():
go = raw_input("> ")
if(CAVELIGHT == 1):
print("foo")

Room1()
Room2()Remember that Room2 requires to enter something, even if you just tap Return.

Sir Penguin
26-03-2003, 19:42:18
Works for me.

SP

Immortal Wombat
26-03-2003, 20:45:09
Hmmm, yes it does work. How very odd. I'll use that approach then, since I understand that one better.

And to make SP happy, I've defined each object as a class so they can hold their locations and names and stuff.

Sir Penguin
26-03-2003, 20:55:27
:love:

If you're working in IDLE, the shell window has a tendency to accrete data over multiple executions of the script, which can lead to dozens upon dozens of bizarre errors which can be fixed just by restarting IDLE. That might be why #1 didn't work the first time?

SP

Sean
26-03-2003, 21:04:21
Originally posted by Immortal Wombat
And to make SP happy, I've defined each object as a class so they can hold their locations and names and stuff.
Remember about inheritance and polymorphism! Classes aren’t just really handy namespaces.

Immortal Wombat
27-03-2003, 18:21:12
And by "remember" you mean "learn"

Sir Penguin
27-03-2003, 21:11:10
Learning is only remembering the future!

SP

Darkstar
27-03-2003, 21:23:48
No... remembering the future is called precognition.

Sir Penguin
27-03-2003, 21:58:04
Not in the future!

SP

Immortal Wombat
16-04-2003, 20:14:59
Well I'm finished. :)

I also managed to inadvertantly compile one of the earlier versions into a .pyc file, but I can't seem to replicate the process and create me an executable version. :confused:

Sean
16-04-2003, 22:28:13
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.108.htp

And post the code here!

Sir Penguin
16-04-2003, 23:06:38
And compile it into a win32 executable and make lots of money off of it!

SP

Immortal Wombat
17-04-2003, 01:03:35
yay!

Well I'm going to do exactly the opposite of what both of you suggest. No code here, but no money either. So nobody's happy.

Immortal Wombat
17-04-2003, 01:21:50
oh, we can't upload zips. How annoying.

Ok, file here (http://www.freewebz.com/wombat/GCM.zip).

There are currently two ways of winning. A fairly easy way, and a fairly torturous way (though with far fewer extended leaps of logic than the HHGTTG one...).

If you want to attempt the long game, simply avoid the temptation to walk out the main exit when it becomes possible. In future versions, I'll make it possible to choose which version of the game you want to play at the very beginning.

Immortal Wombat
17-04-2003, 01:23:20
Oh crap. Forgot to randomise the door codes. :o

Sir Penguin
17-04-2003, 01:38:36
:hmm:

SP

Immortal Wombat
17-04-2003, 13:57:57
If you downloaded it with about 15 minutes of my post, then yeah, it was a bit screwed. Every "command not recognised" put you back to the start. It should be ok now

Scabrous Birdseed
17-04-2003, 16:02:09
When I click the link it redirects back to this thread. :hmm:

Sir Penguin
17-04-2003, 16:08:02
You have to muck around with copying and pasting it.

SP

Immortal Wombat
18-04-2003, 12:57:18
I assume one need Python installed to be able to run a *.pyc file?

And I need "Freeze" to make it into a standalone exe?

Sean
18-04-2003, 13:22:20
Yes. Kinda. It’s awkward.

http://www.cwi.nl/ftp/jack/freeze/

Immortal Wombat
18-04-2003, 14:17:00
Cannot determine your Windows or System directories
Please add them to your PATH if .dlls are not found
The win32api module is not available - modules listed
in the registry will not be found.
opts: []
args: ['GCM.cfg']
Initializing
Traceback (most recent call last):
File "C:\Python22\thing\Builder.py", line 502, in ?
main(opts, args)
File "C:\Python22\thing\Builder.py", line 473, in main
target = makeTarget(cfg, section)
File "C:\Python22\thing\Builder.py", line 416, in makeTarget
return dispatch[cfg.get(section, 'type')](cfg, section, optcnvrts)
File "C:\Python22\thing\Builder.py", line 98, in __init__
Target.__init__(self, cfg, sectnm, cnvrts)
File "C:\Python22\thing\Builder.py", line 34, in __init__
print "Initializing", self.__name__
AttributeError: PYZTarget instance has no attribute '__name__'

Sean
18-04-2003, 14:55:44
Talk to whom? BUILDER
>>Good morning Mr Builder. You look happy this morning...
>>Well I'm not. I want cake! And stay away from my daughter
[Game ends.]Examine what? TABLE
That object isn't here.
It looks to me like a medium-sized, medium-height, medium-practical
a stupid metal ridge all round the top so it abrades your wrists..
:confused: :hmm:.

Immortal Wombat
18-04-2003, 20:29:57
Dontcha love <strike>beta</strike> alpha testing :D

-bugs fixed

Sean
18-04-2003, 20:45:32
If I had the code, I could fix the bugs myself. Or at least cheat and just read what happens.

Immortal Wombat
18-04-2003, 20:49:13
Its not worth the bother to be honest...

Sir Penguin
19-04-2003, 03:23:09
Hehe, Python Universe Builder (http://py-universe.sourceforge.net/).

py2exe (http://starship.python.net/crew/theller/py2exe/) can also be used to convert Python to win32 binary format. I've never used it, so I don't know how it works. The page has links to similar tools.

SP

Sir Penguin
20-04-2003, 11:47:15
I just checked out py2exe. It seems to work well. I mean, aside from the fact that it takes almost 5 megs for it to be able to run a tool that displays 54 card images (the script is 1.07 KB, and the images are < 45 KB; the rest is taken up by the libraries and python dll). Almost 4 MB of that is from the Tkinter, re, and zlib modules and their related libs (yeah, I used the 61KB zlib module to compress 64 KB of images down to 45 KB... it was cool before I knew zlib.pyd was that big), and almost 3 MB of that is from Tkinter and tcl/tk. The python22.dll file, all you need if you're not using any external modules, is less than a meg. It all compressed down to a bit less than 2 MB. The binary for the script is 129 KB with the -O2 flag set, and 188 KB with no optimisation.

http://www.csc.uvic.ca/~nrqm/papyrus/showimgs-noversion.zip

SP

Immortal Wombat
20-04-2003, 12:28:52
I beginning to dislike Python as anything more than a private plaything.


C:\Python22>python programs\GCMsetup.py py2exe
Traceback (most recent call last):
File "programs\GCMsetup.py", line 10, in ?
script="GCM.py"
File "C:\PYTHON22\distutils\core.py", line 101, in setup
_setup_distribution = dist = klass(attrs)
File "C:\PYTHON22\distutils\dist.py", line 130, in __init__
setattr(self, method_name, getattr(self.metadata, method_name))
AttributeError: DistributionMetadata instance has no attribute 'get___doc__'

Sir Penguin
20-04-2003, 12:37:35
It's probably not a language you want to write for-distribution Windows apps in if you know C or something.

Are you sure you got the pathnames right? I have problems when I don't keep everything in the same directory.

SP

Immortal Wombat
20-04-2003, 12:38:53
GCM.py is in the same directory as GCMsetup.py and that is being called properly.

Sir Penguin
20-04-2003, 12:44:22
In situations like this, absolute pathnames can be the solution to all your problems. Nobody knows why, and it may not even have anything to do with the error message, but that's the way things are. Of couse, the Python people could have just mucked up and you'll never know. I hate it when they do that.

SP

Sir Penguin
20-04-2003, 12:46:44
Oh, hehe. It's supposed to be scripts="GCM.py", not script="GCM.py". I think. Do you mind posting your GCMsetup.py file? I've just got 3 lines in mine.

SP

Immortal Wombat
20-04-2003, 12:54:23
:cry: I've tried scripts and script; ["GCM.py"] and "GCM.py"; with an without a trailing comma and every combination of them all.

Sir Penguin
20-04-2003, 13:07:28
This is what works for me:

from distutils.core import setup
import py2exe
setup(name="showimgs",scripts=["showimgs.py"],)


with C:\Python22\Papyrus\images>python setup.py py2exe -O2 -w

SP

Immortal Wombat
20-04-2003, 13:44:09
Urgh. I can't get the bastard to work. The .py file is here (http://www.notanyfuckingmoreitsnot).
If you could attempt to convert it, I would be eternally indebted to you. :) Otherwise just have fun cheating and reading ahead.

Sir Penguin
20-04-2003, 14:04:36
It worked fine for me. I put GCM.py and GCMsetup.py in my \Python22 directory, changed scripts=["showimgs.py"] to scripts=["GCM.PY"], and ran python GCMsetup.py py2exe. It produced the proper output: http://www.csc.uvic.ca/~nrqm/GCM.zip

I think it's great that your nearly-1500 lines of code compiled unoptimised into a binary about half the size of my nearly-40 lines of code optimised.

SP

Sean
20-04-2003, 14:21:20
That would be Tcl and Tk, surely?

Sir Penguin
20-04-2003, 14:25:32
It must be. I don't know much about binaries and libraries (in general, let alone in Windows), but I assumed it would be dynamically linked (or its Windows equivalent) to the tcl/tk libraries that have to be distributed alongside the binary.

SP

Immortal Wombat
20-04-2003, 15:20:23
:bounce: Thankyou SP :bounce: