python

Subclassing Decimal in Python

I want to use Decimal class in my Python program for doing financial calculations. Decimals to not work with floats - they need explicit conversion to strings first. So i decided to subclass Decimal to be able to work with floats without explicit conversions. m_Decimal.py: # -*- coding: utf-8 -*- import decimal Decimal = decimal.Decim...

Python XMLRPC: Handling arbitrary exceptions on client-side

I'm trying to pass arbitrary exceptions from a XMLRPC server to a client (both Python scripts, exception types are defined on both sides). There's an exemplary client-side implementation at ActiveState Recipes which parses the returned "faultString", compares it with a list of known exceptions and, if found, raises that exception (instea...

Pythonize Me: how to manage caller context variables in Python? (Python/Django)

I'm trying to refactor a fairly hefty view function in Django. There are too many variables floating around and it's a huge function. Ideally, I want to modularize the view into logical functions. However, I have to pass the function context around to get easy access to the variables. For example, def complex_view(request, slug): ...

Pass session information from php to python securely? (in agile)

I have a sign up process that is in a legacy framework and we are trying to switch to a new framework...in fact a different language. So let's say that there are 3 steps in the sign up process and each of those 3 steps has it's own file(step1.php, step2.php, step3.php). Now if I want to change page2.php to a python file I will still need...

Regex match even number of letters

Sorry if this is redundant. I need to match an expression in python with regular expressions that only matches even number of letter occurances. For example: # Even number of A's AAA # no match AA # match fsfaAAasdf #match sAfA # match sdAAewAsA # match AeAiA # no match EDIT: Sorry for confusion. You are right - even number of A's ...

Good backend for downloading nzb files to seperate directories

I'm laying out a download management that will segregate each users downloads, separate watched directories each download to their own folders, can't see each others queues, etc. I wanted to use Hellanzb with xml-rpc, however it does not seem to allow me to set separate download directories for each file. I want to avoid trying to gues...

django, location based searches

Excuse my ignorance, I am not even sure of the correct term for this. What I want to do is search by city and state or zip code in my django application and also include results within certain distances of the location (25, 50, 75 miles). I am guessing you probably need to convert the city and state or zip code to lat and long and then...

PyQt & unittest - Testing signal and slots

I have a pyqt application that I'm writing unit tests for, and it relies heavily on signals and slots. To properly test it, I have to check that the correct signals are sent. What is the best way to do this? I see that the Qt library has a QSignalSpy, but I can't find any reference to this in PyQt. The only option I can think of is to m...

What's the best way to implement a webapp to teach programming with?

In the near future, I'm going to be doing a series of short workshops for teens aged 13-17, who may not have any programming experience at all, showing them the basics of programming and webapps. The workshops are short, so it really has to stick to the absolute basics. The apps will be written in Python, for App Engine. Edit: I guess I...

How to catch an exception in python and get a reference to the exception, WITHOUT knowing the type?

I'm wondering how I can catch any raised object (i.e. a type that does not extend Exception), and still get a reference to it. I came across the desire to do this when using Jython. When calling a Java method, if that method raises an exception, it will not extend Python's Exception class, so a block like this will not catch it: try:...

'Invalid Key' error from Paramiko

I'm trying to set up Fabric for deploying my Python web application and Paramiko is barfing on my private RSA key. I had been using my key successfully for 6 months, so I know it's good. In case having a passphrase was the problem, I just made a new key with no passphrase and still get the error. Help? ...

PEP8 - 80 Characters - Big Integers

This is somehow related to question about big strings and PEP8. How can I make my script that has the following line PEP8 compliant ("Maximum Line Length" rule)? pub_key = { 'e': 3226833362680126101036263622033066816222202666130162062116461326212012222403311326222666622610430466620224662364142L, 'n': 22642100386104124846282622610...

Tab Completion in Python Command Line Interface - how to catch Tab events

I'm writing a little CLI in Python (as an extension to Mercurial) and would like to support tab-completion. Specifically, I would like catch tabs in the prompt and show a list of matching options (just like bash). Example: Enter section name: ext*TAB* extensions extras The problem is I'm not sure how to catch the Tab ev...

i have '__contains__' ,why error

class a(object): def a(self): return True __contains__=a b=a() print 2 in b#why error thanks ...

Getting String from A TextCtrl Box

I cannot seem to grasp how I would get the strings from a text Ctrl box. I am very new to this, so any help is greatly appricated. Here is the practic code: import wx class citPanel(wx.Panel): def __init__(self, parent, id): wx.Panel.__init__(self, parent, id) wx.StaticText(self, -1, "Choose put you would like:"...

Is it possible to run function in a subprocess without threading or writing a separate file/script.

import subprocess def my_function(x): return x + 100 output = subprocess.Popen(my_function, 1) #I would like to pass the function object and its arguments print output #desired output: 101 I have only found documentation on opening subprocesses using separate scripts. Does anyone know how to pass function objects or even an easy...

record output sound in python

i want to programatically record sound coming out of my laptop in python. i found PyAudio and came up with the following program that accomplishes the task: import pyaudio, wave, sys chunk = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = sys.argv[1] p = pyaudio.PyAudio() channel_map ...

Creating waitable objects in Python.

I more or less know how to use select() to take a list of sockets, and only return the ones that are ready to read/write something. The project I'm working on now has a class called 'user'. Each 'user' object contains its own socket. What I would like to do is pass a list of users to a select(), and get back a list of only the users w...

Pylons "global name 'c' is not defined"

hiyas. i had setup Pylons v0.9.7, and create project. and using genshi. and i coding to easy test code , but that code dont working. code: member.py coding: utf-8 import logging import foo.model from foo.lib.base import * log = logging.getLogger(__name__) class MemberController(BaseController): def index(self): c.tit...

Google App Engine singletons (Python)

The standard way of doing singletons in Python is class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instance However, this doesn't work on App Engine, since there are may ...