python

In Python, is the idiom "from Module import ClassName" typical?

Since I prefer small files, I typically place a single "public" class per Python module. I name the module with the same name as the class it contains. So for example, the class ToolSet would be defined in ToolSet.py. Within a package, if another module needs to instanciate an object of class ToolSet, I use: from ToolSet import ToolSet...

Python: check if an object is a list or tuple (but not string)

This is what I normally do in order to ascertain that the input is a list/tuple - but not a str. Because many times I stumbled upon bugs where a function passes a str object by mistake, and the target function does for x in lst assuming that lst is actually a list or tuple. assert isinstance(lst, (list, tuple)) My question is: is ther...

Pivoting SQLite table, setwise like SQL should be

I have some data. 224,000 rows of it, in a SQLite database. I want to extract time series information from it to feed a data visualisation tool. Essentially, each row in the db is an event that has (among other things not strictly relevant) a time-date group in seconds since the epoch and a name responsible for it. I want to extract how ...

Calling a function using ctypes with pointers to structs

Hi! I am trying to call a C function sitting in a shared object from python, and using ctypes seems to be the best way of accomplishing this. I have to pass derived types to this function, with the following function prototype: int MyFunc (Config *config, int *argc, char **argv ) The Config struct is defined as typedef struct{ char ...

Python zlib not decodable when returned by an http response

I'm using Amazon S3 to serve static files. When the Content-Type is just 'text/css' and I haven't compressed the file, it is returned ok. If I try to zlib.compress() the contents that will be returned and change Content-Encoding to 'gzip', the browser cannot decode the result. In Chrome, the error is Error 330 net::ERR_CONTENT_DECODING...

mysqldb build error

i remember installing Python + Django + MySQL + MySQLdb on my 32-bit Mac with Leopard 10.5.7. I tried the same procedure with Mac Snow Leopard. But have unfortunately ran into a lot of errors... i dont know but something weird is happening. Please look at the error log: Amit-Vermas-MacBook:mysql-python-1.2.2 amitverma$ python setup.py b...

What Python-only HTTP/1.1 web servers are available?

There is CherryPy. Are there any others? ...

buildout deployment strategies

So I am applying zc.buildout to an existing django project. I am wondering about deploying it now. How do I achieve the sandbox effect on a production server? ...

Codec Errors in Python

Does anyone know the name of a codec that can translate any random assortment of bytes into a string? I have been getting the following error after encoding, encrypting, and decoding a string in tkinter.Text. UnicodeDecodeError: 'utf8' codec can't decode byte 0x99 in position 151: unexpected code byte Code used to generate the error f...

Using try vs if in python

Is there a rationale to decide which one of try or if constructs to use, when testing variable to have a value? For example, there is a function that returns either a list or doesn't return a value. I want to check result before processing it. Which of the following would be more preferable and why? result = function(); if (result): ...

What is the range of values a float can have in Python?

What are its smallest and biggest values in python? ...

Problem with variable scoping in Python

This problem is partly due to my lack of completely understanding scoping in python, so I'll need to review that. Either way, here is a seriously trivial piece of code that keeps crashing on my Django test app. Here's a snippet: @login_required def someview(request): try: usergroup = request.user.groups.all()[0].name except: Http...

Python long multiplication

I'm in need of an algorithm faster than the current normal python long multiplication , I tried to find a decent karatsuba implementation , but I can't. def main(): a=long(raw_input()) if(a<0): a=a*-1 a=((a*(a+1)/2)-1) print(-a) else: a=(a*(a+1))/2 print(a) main() As you see , it's nothing complica...

Need a zip of Python 2.6 for windows

Not the source codes, thats the only thing i seem to find. I can't install py2.6 because it would overtake 2.5 and cause mayor mess in my pc. ...

Python universal database interface?

Does there exist, or is there an intention to create, a universal database frontend for Python like Perl's DBI? I am aware of Python's DB-API, but all the separate packages are leaving me somewhat aggravated. ...

A Friendship relationship question.

Hi, I am having difficulties with listing this type of data. Scenario is as follows: user1 add's a friend called user2 user2 confirms that user1 is his friend what should happen is user2 and user1 see's each others name in their friends list. What's happening now is I am able to add user2 to user1 friends list but user1 cannot see us...

Similarity Between Users Based On Votes

lets say i have a set of users, a set of songs, and a set of votes on each song: =========== =========== ======= User Song Vote =========== =========== ======= user1 song1 [score] user1 song2 [score] user1 song3 [score] user2 song1 [score] user2 song2 [score] user...

Subprocess Popen and PIPE in Python

The following code prints an empty line as an output which is false. The problem is not in the permissions, since I tested the command with 777 permissions for the pdf -file. How can you fix the command to give a right output? import subprocess from subprocess import PIPE, Popen output = Popen(['pdftotext', '/home/aal/Desktop/lkn_pdf/ap...

Documenting module/class/function bodies in python sphinx docs

Is there a way with Sphinx documentation to output a function or class body (the code itself) with the autodoc feature? I'm using autodoc to much success. In addition to the docstrings getting pulled in to the documentation I want like a link to click for each function where it will show you the source... is that possible? This is about...

python help display regular expression result

Hi, I am doing simple regular expressions in python I am trying the re.split but things like ['\r\n', '\r\n'] are coming instead of the answer. Can someone please tell me how to display the actual text please? I tried this statement: t_html = re.split("<[a-zA-Z0-9\s\w\W]*>[a-zA-Z0-9\s\w\W]*</[a-zA-Z0-9\s\w\W]*>" ,s) THanks ...