Through trying to explain the Monty Hall problem to a friend during class yesterday, we ended up coding it in Python to prove that if you always swap, you will win 2/3 times. We came up with this:
import random as r
#iterations = int(raw_input("How many iterations? >> "))
iterations = 100000
doors = ["goat", "goat", "car"]
wins = 0.0
...
I've heard that coroutines are a good way to structure games (e.g., PEP 342: "Coroutines are a natural way of expressing many algorithms, such as simulations, games...") but I'm having a hard time wrapping my head around how this would actually be done.
I see from this article that coroutines can represent states in a state machine whic...
I've just started working with pygame and I'm trying to make a semi-transparent sprite, and the sprite's source file is a non-transparent bitmap file loaded from the disk. I don't want to edit the source image if I can help it. I'm sure there's a way to do this with pygame code, but Google is of no help to me.
...
I have this Perl snippet from a script that I am translating into Python. I have no idea what the "s!" operator is doing; some sort of regex substitution. Unfortunately searching Google or Stackoverflow for operators like that doesn't yield many helpful results.
$var =~ s!<foo>.+?</foo>!!;
$var =~ s!;!/!g;
What is each line doing? I...
I'm writing a pythonic web API wrapper with a class like this
import httplib2
import urllib
class apiWrapper:
def __init__(self):
self.http = httplib2.Http()
def _http(self, url, method, dict):
'''
Im using this wrapper arround the http object
all the time inside the class
'''
params = urllib.url...
How to escape HTML with characters like – in Python?
...
Python is not a pretty good language in defining a set of commands to run. Bash is. But Bash does not run naively on Windows.
Background: I am trying to build a set of programs - with established dependency relationships between them - on mac/win/linux. Something like macports but should work on all the three platforms listed.
This beg...
I've created a small PyQt based utility in Python that creates PNG graphs using matplotlib when a user clicks a button. Everything works well during the first few clicks, however each time an image is created, the application's memory footprint grows about 120 MB, eventually crashing Python altogether.
How can I recover this memory afte...
Quite a simple question, but I'm a python noobie. How do we remove all non-numeric characters from a string in Python?
How can I do that?
...
Python came pre-installed on my macbook and I have been slowly getting acquainted with the langauge. However, it seems that my configuration of the re library is incorrect, or I simply misunderstand something and things are amiss. Whenever I run a python script with "import re", I recieve the following error:
Traceback (most recent ca...
for filename in os.listdir("."):
for line in open(filename).xreadlines():
if "foo" in line:
print line
So this is a simple python equivalent of cat filename | grep foo. However, I would like the equivalent of cat filename | grep -B 5 -C 5 foo, how should the above code be modified?
...
I'm becoming acquainted with python and am creating problems in order to help myself learn the ins and outs of the language. My next problem comes as follows:
I have copied and pasted a huge slew of text from the internet, but the copy and paste added several new lines to break up the huge string. I wish to programatically remove all ...
Is there a cross-platform library function that would collapse a multiline string into a single-line string with no repeating spaces?
I've come up with some snip below, but I wonder if there is a standard function which I could just import which is perhaps even optimized in C?
def collapse(input):
import re
rn = re.compile(r'(\...
I try logging exceptions in Python 2.5, but I can't do it. All formatting functions do something else than what I want.
I came up with this:
def logexception(type, value, traceback):
print traceback.format_exception(type, value, traceback)
sys.excepthook = logexception
but it bails out with an argument error when called, though ac...
I want to try python purple but I don't have debian. Is there a way to get it to run on either windows or a different linux?
...
By default, when you call ElementTree.parse(someXMLfile) the Python ElementTree library prefixes every parsed node with it's namespace URI in Clark's Notation:
{http://example.org/namespace/spec}mynode
This makes accessing specific nodes by name a huge pain later in the code.
I've read through the docs on ElementTree and namespa...
Without using VisIt's python library visit-writer.py, how can I import my data into VisIt? I'm aware of many alternative file formats but I can't find a simple library that takes my arrays of data and just writes it as a file. Specifically, my data consists of adaptive mesh boxes (really, voxels) and so I figure is best represented by an...
I'm trying to write a batch process that can take an MP3 file and mute certain parts of it, ideally in Python or Java.
Take this example: Given a 2 minute MP3, I want to mute the time between 1:20 and 1:30. When saved back to a file, the rest of the MP3 will play normally -- only that portion will be silent.
Any advice for setting this...
i know that there is a documentation page on qt web site but i was wondering if that's possible to get dynamically through python.
...
I write two python module, and simplify them as:
# a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
# b.py
import a
def hi():
print "hi"
When I run a.py, I get
AttributeError: 'module' object has no attribute 'hi'
What does the error mean?
What can I do with it?
...