python

How to serve any file type with Python's BaseHTTPRequestHandler.

Consider the following example: import string,cgi,time from os import curdir, sep from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer class MyHandler(BaseHTTPRequestHandler): def do_GET(self): try: if self.path.endswith(".html"): f = open(curdir + sep + self.path) #self.path has /test....

Elegant way to abstract multiple function calls?

Example: >>> def write_to_terminal(fmt, *args): ... print fmt % args >>> LOG = logging.getLogger(__name__) >>> info = multicall(write_to_terminal, LOG.info) >>> debug = multicall(write_debug_to_terminal, LOG.debug) >>> ... >>> info('Hello %s', 'guido') # display in terminal *and* log the message Is there an elegant way to write mu...

wavelet plot with python libraries

I know that scipy has some signal processing tools for wavelets in scipy.signal.wavelets and a chart can be drawn using matplotlib, but it seems I can't get it right. I have tried plotting a daub wavelet against a linspace, but it's not what I am looking for. I am highly unskilled about wavelets and math in general . :) ...

Convert a string to integer with decimal in Python

I have a string in the format: 'nn.nnnnn' in Python, and I'd like to convert it to an integer. Direct conversion fails: >>> s = '23.45678' >>> i = int(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '23.45678' I can convert it to a decimal by using: >>> ...

Reusable library to get human readable version of file size?

There are various snippets on the web that would give you a function to return human readable size from bytes size: >>> human_readable(2048) '2 bytes' >>> But is there a Python library that provides this? ...

Is there a Python language specification?

Is there anything in Python akin to Java's JLS or C#'s spec? ...

python strings / match case

I have a CSV file which has the following format: id,case1,case2,case3 123,null,X,Y 342,X,X,Y 456,null,null,null 789,null,null,X above is the sample data that could be in that file. So for each line I need to know which of the case is not null. Is there an easy way to find the which case(s) are not null instead of splitting the st...

Django file upload input validation and security

I'm creating a very simple django upload application but I want to make it as secure as possible. This is app is going to be completely one way, IE. anybody who uploads a file will never have to retrieve it. So far I've done the following: Disallow certain file extensions (.php, .html, .py, .rb, .pl, .cgi, .htaccess, etc) Set a maxim...

Matrix from Python to MATLAB

I'm working with Python and MATLAB right now and I have a 2D array in Python that I need to write to a file and then be able to read it into MATLAB as a matrix. Any ideas on how to do this? Thanks! ...

Finding partial strings in a list of strings - python

I am trying to check if a user is a member of an Active Directory group, and I have this: ldap.set_option(ldap.OPT_REFERRALS, 0) try: con = ldap.initialize(LDAP_URL) con.simple_bind_s(userid+"@"+ad_settings.AD_DNS_NAME, password) ADUser = con.search_ext_s(ad_settings.AD_SEARCH_DN, ldap.SCOPE_SUBTREE, \ "sAMAccountName...

Alternative Python imaging libraries on Google App Engine?

I am thinking about uploading images to Google App Engine, but I need to brighten parts of the image. I am not sure if the App Engine imagine API will be sufficient. I consider to try an overlay with a white image and partial opacity. However, if that does not yield the desired results, would there be another Python imaging library tha...

Get __name__ of calling function's module in Python

Suppose myapp/foo.py contains: def info(msg): caller_name = ???? print '[%s] %s' % (caller_name, msg) And myapp/bar.py contains: import foo foo.info('Hello') # => [myapp.foo] Hello I want caller_name to be set to the __name__ attribute of the calling functions' module (which is 'myapp.foo') in this case. How can this be don...

Sending SIGINT to a subprocess of python

I've got a python script managing a gdb process on windows, and I need to be able to send a SIGINT to the spawned process in order to halt the target process (managed by gdb) It appears that there is only SIGTERM available in win32, but clearly if I run gdb from the console and Ctrl+C, it thinks it's receiving a SIGINT. Is there a wa...

Find module name of the originating exception in Python

Example: >>> try: ... myapp.foo.doSomething() ... except Exception, e: ... print 'Thrown from:', modname(e) Thrown from: myapp.util.url In the above example, the exception was actually thrown at myapp/util/url.py module. Is there a way to get the __name__ of that module? My intention is to use this in logging.getLogger functio...

Does get_or_create() have to save right away? (Django)

I need to use something like get_or_create() but the problem is that I have a lot of fields and I don't want to set defaults (which don't make sense anyway), and if I don't set defaults it returns an error, because it saves the object right away apparently. I can set the fields to null=True, but I don't want null fields. Is there any o...

where does django install in ubuntu

I am looking for the init.py file for django. I tried whereis and find, but I get a lot of dirs. ...

what next after 'dive into python'

I've been meaning to learn another language than java. So I started to poke around with python. I've gone over 'dive into python' so I have a decent knowledge about python now. where do you suggest I go from here? I dont want to go through another advanced book again and would like to use the python knowledge towards building 'someth...

Comparing and updating array values in Python

I'm developing a Sirius XM radio desktop player in Python, in which I want the ability to display a table of all the channels and what is currently playing on each of them. This channel data is obtained from their website as a JSON string. I'm looking for the best data structure that would allow the cleanest way to compare and update t...

Override namespace in python

Hi all, Say there is a folder '/home/user/temp/a40bd22344'. The name is completely random and changes in every iteration. I need to be able to import this folder in python using fixed name, say 'project'. I know I can add this folder to sys.path to enable import lookup, but is there a way to replace 'a40bd22344' with 'project'? May b...

Pylons/Routes rewrite POST or GET to fancy URL

The behavior I propose: A user loads up my "search" page, www.site.com/search, types their query into a form, clicks submit, and then ends up at www.site.com/search/the+query instead of www.site.com/search?q=the+query. I've gone through a lot of the Pylons documentation already and just finished reading the Routes documentation and am w...