I've got some Perl code like this:
my $match = $matches{$key}
? "$matches{$key} was a match!"
: "There was no match."
How is this best rewritten in Python? I'm trying to avoid getting a KeyError.
...
I have a function that occasionally hangs. Normally I would set an alarm, but I'm in Windows and it's unavailable. Is there a simple way around this, or should I just create a thread that calls time.sleep()?
Ended up going with a thread. Only trick was using os._exit instead of sys.exit
import os
import time
import threading
clas...
class Domains(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length= 60)
user = models.ManyToManyField("Users", blank=True, null=True)
def __unicode__(self):
return self.name
class Groups(models.Model):
domain = models.ForeignKey(Domains)
name = models.CharField(m...
Empirically, it seems that Python's default list sorter, when passed a list of tuples, will sort by the first element in each tuple. Is that correct? If not, what's the right way to sort a list of tuples by their first elements?
...
I am working on a program that (among other things) reads a CSV file in (it gets stored as an array of dicts in the form [{col1:data1a,col2:data2a},{col1:data1b,col2:data2b}] ). For each row, as part of other processing, I need to remap those keys to user entered values, which are provided in another dict so they can be used as parameter...
Businessmen typically want a web application developed. They are aware of .net or J2EE by names, without much knowledge about either.
Altho' Rails and Django offer for a much better and faster development stack, it is a big task to convince businessmen to use these platforms.
The task begins with introducing Django (or Rails), quoting ...
My website is written in Python and currently runs under mod_python with Apache. Lately I've had to put in a few ugly hacks that make me think it might be worth converting the site to mod_wsgi. But I've gotten used to using some of mod_python's utility classes, especially FieldStorage and Session (and sometimes Cookie), and from a scan o...
I've been using matplotlib in python for some time now and I've finally gotten around to asking this question about an issue on my mac. When a plot shows up (after the plot() command, draw(), or show()), I have all the functionality I could want; I can move, zoom, etc. that I didn't do in the code.
When I go to save a figure with the vi...
I am developing a Python module with several source files, each with its own test class derived from unittest right in the source. Consider the directory structure:
dirFoo\
test.py
dirBar\
__init__.py
Foo.py
Bar.py
To test either Foo.py or Bar.py, I would add this at the end of the Foo.py and Bar.py so...
What is the quickest way to HTTP GET in Python if I know the Content will be a string? I am searching the docs for a quick one-liner like:
contents = url.get("http://example.com/foo/bar")
But all I can find using Google are httplib and urllib - and I am unable to find a shortcut in those libraries.
Does standard Python 2.5 have a sho...
I am writing a python platform for the simulation of distributed sensor swarms. The idea being that the end user can write a custom Node consisting of the SensorNode behaviour (communication, logging, etc) as well as implementing a number of different sensors.
The example below briefly demonstrates the concept.
#prewritten
class Sensor...
Is it possible to change the Windows command prompt working directory via Python script?
e.g.
>> cd
>> c:\windows\system32
>> make_decision_change_dir.py
>> cd
>> c:\windows
I have tried a few things which don't work:
import os
os.chdir(path)
import os, subprocess
subprocess.Popen("chdir /D \"%s\"" %path, shell=True)
import ...
I have a Python module with nothing but regular global functions. I need to call it from another business-domain scripting environment that can only call out to C DLLs. Is there anyway to build my Python modules so that to other code it can be called like a standard C function that's exported from a DLL? This is for a Windows environment...
I am finding it difficult to use MySQL with Python in my windows system.
I am currently using Python 2.6. I have tried to compile MySQL-python-1.2.3b1 (which is supposed to work for Python 2.6 ?) source code using the provided setup scripts. The setup script runs and it doesn't report any error but it doesn't generate _mysql module.
I...
I have genereated image by PIL.
How can I save it to string in memory?
Image.save() method requires file.
I'd like to have number of such images stored in dictionary.
...
I read dive into python for Python 2.x but is there any e-books for Python 3.x?
...
Currently I'm using a very ugly approach based on a regex for finding links and taking them apart.
I'm unhappy with the code, so I'm asking for nicer solutions, preferably using only the stdlib.
Edit
The task at hand has 2 parts:
Find all distributions that match a certain criteria (like prefix of the name).
Find all versions availa...
How can I detect in my python script if its being run by the debug interpreter (ie python_d.exe rather than python.exe)? I need to change the paths to some dlls that I pass to an extension.
eg Id like to do something like this at the start of my python script:
#get paths to graphics dlls
if debug_build:
d3d9Path = "bin\\debug\\di...
While working on a really only-for-fun project I encountered some problem.
There is a 2D world populated with Round Balls, Pointy Triangles and Skinny Lines (and other wildlife too, maybe). They all are subclasses of WorldCreatures. They can move inside this world. When they meet each other, a Collision happens.
The thing I'd like to d...
I'm new to XML-RPC and I would like to know if there is any good tutorial to use XML-RPC with the Last.fm API.
Is it possible to call the API methods using the xmlrpclib module like in the following example?
import xmlrpclib
myserver = xmlrpclib.ServerProxy('http://ws.audioscrobbler.com/2.0/')
...