Python! Is! Truly! Amazing!

E

Erik Bethke

Hello Everyone,

I have to say:

Python! Is! Truly! Amazing!

So I started with python about a month ago and put in 24 hours across
three weekends.

My first project was to recreate a simple maze-let thingie that I
remember as my first program way back on the Vic-20. That was a quick
snap and a pleasure.

Since then I have settled down into working on a Mahjong game. I have
uploaded a current version at my personal website:

www.erikbethke.com/Python/mahjong.zip

also note screenshots:

www.erikbethke.com/Python/screenshot01.jpg
www.erikbethke.com/Python/screenshot02.pg
www.erikbethke.com/Python/screenshot03.pg
www.erikbethke.com/Python/screenshot04.pg
www.erikbethke.com/Python/screenshot05.pg

So I have been using Python 2.3.4 as that is what was compatible with
the excellent PyGame last month. After reading the posts here I am
using wing IDE, and over all I like it... some minor issues and I am
sure if I RTFM I would have these handled. I will be buying a bunch of
licenses for us.

I also used this project to learn a little about XML, so I used XML to
fully describe the UI, the tilesets and the tile layouts and have it
able to go back and forth between 3 different tilesets, and backgounds.
The XML is crappy as you can see I am experimenting with putting some
data in attributes and elsewhere in elements.

So now I have it laying out the tiles, tile picking correctly, and I
was even able to write many new little feature-ettes in wickedly fast
single edit-run passes such as this to highlight the currently
available matches:

def findMatchingTiles(self):
matchList = []
visibleTiles = []
for i, c in enumerate ( self.tiles ):
if c.state != 'clear':
visibleTiles.append( c )

for i, c in enumerate ( visibleTiles ):
if self.testForClear( c ):
for j, d in enumerate ( visibleTiles ):
if d != c and d.tileNum == c.tileNum and self.testForClear( d ):
matchList.append( [c, d] )
return matchList

def showHint(self):
matchList = self.findMatchingTiles()
for i, c in enumerate( matchList ):
c[0].state='clicked'
c[1].state='clicked'
self.draw()


I have NEVER experienced this kind of programming joy. I am a 33
year-old professional game developer (www.gopetslive.com) and have been
in the industry for 11 years, and have been programming since 4th grade
like all the rest of you guys. And I am not saying all this to brag,
no seriously I am delirious in joy right now. I have a bad cold and
went to sleep at 1am last night and woke at 5am just to get back to
playing with Python!

I have been looking into Python to see if it is a good answer for
getting a lot of new fresh mini-game and UI work done for our project
GoPets, and clearly it is. I haven't yet explored the server-side
but I will. The Mahjong game in the future will take its tile art
dynamically from the portraits of the users that are your friends and
the people your GoPet has visited recently.

Anyways, I am now a super gushing fan-boy. I have worked my way up
from a scripter working in crappy proprietary languages to a c++
programmer, to now biz guy. But when I walked away from programming I
gave it a grim farewell, c++ work is good, but so much mind goes into
it to make progree compared to other creative uses of the mind. But
Python rocks, it makes programming very fun again and now I finding
myself coming home earlier so I can get to work on Python and I have an
entire heap of new projects I wanted to get done.

Anyways, the Mahjong game code is quite crappy. No effort has been
made to optimize it, nor have I handled any sorts of errors whatsoever.
I know it sucks. But I posted the code there anyways in case any one
else is just coming along and wants some more example code.

I also have no real UI going on. Hit R to randomize the tiles, M to
try to setup a solvable game (but my algorithm for this is not very
effective yet), and H for hints on tiles that can be matched.

Other than the tile art anyone can do what the feel with it.

Thank you all for posting here, you have already solved dozens and
dozens of questions for me with a little help of google groups.
Truly thank you.

-Erik
 
E

Erik Bethke

Oh yeah, and farmer, no I didn't yet get back to making an exe by
fixing the problem with pygame2exe i just got disctracted getting other
things done so fast!

-Erik
www.gopetslive.com
 
N

Nelson Minar

Erik Bethke said:
I have NEVER experienced this kind of programming joy.

Yep, I feel the same way since learning Python. It's really a
productive and pleasant language.

Congratulations on all your game successes!
 
M

Miklós P

Erik Bethke said:
Hello Everyone,

I have to say:

Python! Is! Truly! Amazing!

So I started with python about a month ago and put in 24 hours across
three weekends. ....

Truly thank you.

-Erik

I enjoyed to read about your enthusiasm about Python you have recently
discovered. :)
After checking out your personal and business site, I'm sure you'll be even
more pleased when Python will be an essential component in your business
and, say, GoPets take on a bit of snake-like inner working. :)

Best,
Miklós
---
Jegenye 2001 Bt.
Egyedi szoftverkészítés, tanácsadás | Custom software development,
consulting
Magyarul: http://jegenye2001.parkhosting.com
In English: http://jegenye2001.parkhosting.com/en
 
K

Kamilche

Erik said:
Anyways, I am now a super gushing fan-boy. I have worked my way up
from a scripter working in crappy proprietary languages to a c++
programmer, to now biz guy. But when I walked away from programming I
gave it a grim farewell, c++ work is good, but so much mind goes into
it to make progree compared to other creative uses of the mind. But
Python rocks, it makes programming very fun again and now I finding
myself coming home earlier so I can get to work on Python and I have an
entire heap of new projects I wanted to get done.

Yeah, it is really a great language. For me, it matches my internal idea
of 'pseudocode' to a T. Executable shorthand - does it get any better
than that?

--Kamilche
 
R

Ron Garret

[email protected] (Aahz) said:
Taking this more seriously than it deserves, I've tried poking at Lisp a
couple of times -- each time, I walk away shaking my head in disgust.
Lisp just ain't as *READABLE* as Python.

Readability is in the eye of the beholder, but this is not the place to
argue this.

But this topic does bring up a legitimate question: I have a bunch of
code that generates HTML using PRINT statements. I need to convert all
this code to return strings rather than actually printing them (so I can
use the results to populate templates). In Lisp I could do this:

(with-output-to-string (s)
(let ( (*standard-output* s) )
(call-html-generating-code)
s))

Is there an equivalent Python trick to capture a function call's output
as a string?

Thanks,
rg
 
M

Mark McEahern

Ron said:
But this topic does bring up a legitimate question: I have a bunch of
code that generates HTML using PRINT statements. I need to convert
all this code to return strings rather than actually printing them (so
I can use the results to populate templates). In Lisp I could do this:

(with-output-to-string (s)
(let ( (*standard-output* s) )
(call-html-generating-code)
s))

Is there an equivalent Python trick to capture a function call's
output as a string?
Just to make sure I understand, I'm going to restate your question:

Is there a way to capture stdout?

The answer: Sure, replace it with something file-like:

// m
 
R

Ron Garret

Mark McEahern said:
Just to make sure I understand, I'm going to restate your question:

Is there a way to capture stdout?

The answer: Sure, replace it with something file-like:


// m

That's exactly what I was looking for. Thanks!

rg
 
J

JanC

Ron Garret schreef:
But this topic does bring up a legitimate question: I have a bunch of
code that generates HTML using PRINT statements. I need to convert all
this code to return strings rather than actually printing them (so I can
use the results to populate templates). In Lisp I could do this:

(with-output-to-string (s)
(let ( (*standard-output* s) )
(call-html-generating-code)
s))

Is there an equivalent Python trick to capture a function call's output
as a string?

Something like this:

py> import cStringIO
py> import sys
py>
py> def foo():
.... print "test"
....
py> f = cStringIO.StringIO()
py> sys.stdout = f
py> foo()
py> s = f.getvalue()
py> sys.stdout = sys.__stdout__
py> f.close()
py> print s.capitalize()
Test
 
J

Just

JanC said:
Something like this:

py> import cStringIO
py> import sys
py>
py> def foo():
... print "test"
...
py> f = cStringIO.StringIO()
py> sys.stdout = f
py> foo()
py> s = f.getvalue()
py> sys.stdout = sys.__stdout__

You should always save stdout instead of using __stdout__. It may not be
the same! I don't think anyone should *ever* use __stdout__ except when
debugging. See also:

http://mail.python.org/pipermail/python-dev/2000-October/010144.html
py> f.close()
py> print s.capitalize()
Test

Just
 
S

Simo Melenius

Ron Garret said:
(with-output-to-string (s)
(let ( (*standard-output* s) )
(call-html-generating-code)
s))

Is there an equivalent Python trick to capture a function call's output
as a string?

I've sometimes replaced sys.stdout (and/or sys.stderr) to
capture/redirect debugging information in existing code that has
unwisely just "print"ed error and warning messages, instead of using
sys.stderr or error logging modules.

py> def with_output_to_string (func):
.... try:
.... sys.stdout = StringIO.StringIO ()
.... func ()
.... return sys.stdout.getvalue ()
.... finally:
.... sys.stdout = sys.__stdout__
....
py> def printing_code ():
.... print "Foobar"
....
py> with_output_to_string (printing_code)
'Foobar\n'
py>


br,
S
 
J

Just

Simo Melenius said:
I've sometimes replaced sys.stdout (and/or sys.stderr) to
capture/redirect debugging information in existing code that has
unwisely just "print"ed error and warning messages, instead of using
sys.stderr or error logging modules.

py> def with_output_to_string (func):
... try:
... sys.stdout = StringIO.StringIO ()
... func ()
... return sys.stdout.getvalue ()
... finally:
... sys.stdout = sys.__stdout__

Aargh, I can't believe how widespread this idiom is :-(. See my other
reply in this thread: DON'T use sys.__stdout__. Ever.

Just
 
V

Ville Vainio

jfj> There were functional and non-functional programming
jfj> languages (the first being *much* simpler to
jfj> implement). There is a *reason* people chose C over
jfj> lisp. It's not that we were all blind and didn't see the
jfj> amazingness of lisp. Procedural languages are simply better,
jfj> and I'm not replying to this flamewar.

You did already ;). Lisp is not a functional programming language, if
that was the case it would be even less popular than it is now. Lisp
had it's heyday, while pure FP languages didn't even have that much.

Hey, it's 2005, I don't think we've used up our yearly Lisp flamewar
quota yet.
 
C

Christopher Koppler

jfj> There were functional and non-functional programming
jfj> languages (the first being *much* simpler to
jfj> implement). There is a *reason* people chose C over
jfj> lisp. It's not that we were all blind and didn't see the
jfj> amazingness of lisp. Procedural languages are simply better,
jfj> and I'm not replying to this flamewar.

You did already ;). Lisp is not a functional programming language, if
that was the case it would be even less popular than it is now. Lisp
had it's heyday, while pure FP languages didn't even have that much.

Hey, it's 2005, I don't think we've used up our yearly Lisp flamewar
quota yet.

Lisp will strike back! Someday, it will be popular again! When all else is
dust, Lisp will rule the skies!

In the meantime, let's do Python.

--
Christopher

root@creation # python
Python inf (day 1)
[PyPy inf (Pythonix inf)] on pynuxinf
Type "help", "copyright", "credits" or "license" for more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top