python

Saving a Numpy array as an image

I have a matrix in the type of a Numpy array. How would I write it to disk it as an image? Any format works (png, jpeg, bmp...). One important constraint is that PIL is not present. ...

Compiling Python, curses.h not found

I'm attempting to build Python 2.6.2 from source on my Linux system. It has ncurses installed on /usr/local/, and curses.h is on /usr/local/include/ncurses. So curses.h isn't found on the include path, and those packages fail in the Python build. What's the right solution to this? Is Python supposed to include <ncurses/curses.h>? Sh...

How does the win32com python.Interpreter work?

Ok, so I'm trying to google the win32com python package and the python.Interpreter COM server. Unfortunately, python.Interpreter ends up as "python Interpreter" and not giving me any COM server results. I'm trying to make a pluggable program that has a plugin to allow python code to run, and it seems like the python.Interpreter would b...

Data storage to ease data interpolation in python

I have 20+ tables similar to table 1. where all letters represent actual values Table 1: $ / cars |<1 | 2 | 3 | 4+ <10,000 | a | b | c | d 20,000 | e | f | g | h 30,000 | i | j | k | l 40,000+ | m | n | o | p A user input could be for example, (2.4, 24594) which is a value between f, g, j, and k. my python function definition an...

How to embed a Poll in a Web Page

I want to create a simple online poll application. I have created a backend in python that handles vote tracking, poll display, results display and admin setup. However, if I wanted a third party to be able to embed the poll in their website, what would be the recommended way of doing so? I would love to be able to provide a little ja...

hasattr() vs try-except block to deal with non-existent attributes

if hasattr(obj, 'attribute'): # do somthing vs try: # access obj.attribute except AttributeError, e: # deal with AttributeError Which should be preferred and why? ...

How to implement hotlinking prevention in Google App Engine

My application is on GAE and I'm trying to figure out how to prevent hotlinking of images dynamically served (e.g. /image?id=E23432E) in Python. Please advise. ...

How do I install Python Imaging Library on Mac OS X?

Hi. I'm an extremely amateur programmer; I've done some recreational algorithmics programming, but I honestly have no idea how libraries and programming languages really fit together. I'm supposed to work on a project that requires some image processing, so I've been trying to install PIL for a while, but I haven't been able to. I went ...

Python - get static information

Hello, i have to get static information from one 'module' to another. I'm trying to write logger with information about code place from where we're logging. For example, in some file: LogObject.Log('Describe error', STATIC_INFORMATION) Static information is class name, file name and function name. I get it from this: __file__ self._...

Python's 'with' statement versus 'with .. as'

Having just pulled my hair off because of a difference, I'd like to know what the difference really is in PYthon 2.5. I had two blocks of code (dbao.getConnection() returns a MySQLdb connection). conn = dbao.getConnection() with conn: # Do stuff And with dbao.getConnection() as conn: # Do stuff I thought these would have t...

python, regular expressions, named groups and "logical or" operator

Hello. In python regular expression, named and unnamed groups are both defined with '(' and ')'. This leads to a weird behavior. Regexp "(?P<a>1)=(?P<b>2)" used with text "1=2" will find named group "a" with value "1" and named group "b" with value "2". But if i want to use "logical or" operator and concatenate multiple rules, the fo...

How can I draw automatic graphs using dot in Python on a Mac?

I am producing graphs in a Python program, and now I need to visualize them. I am using Tkinter as GUI to visualize all the other data, and I would like to have a small subwindow inside with the graph of the data. At the moment I have the data being represented in a .dot file. And then I keep graphviz open, which shows the graph. But th...

is it possible to call python methods from a C program?

I remember seeing somewhere that you could call python methods from inside C using #include "python.h" But I can't seem to find the source for this or any examples. How can I call python methods from inside a C program? ...

How to tell when a function in another class has been called

I have two Python classes, call them "C1" and "C2". Inside C1 is a function named "F1" and inside C2 is a function named "F2". Is there a way to execute F2 each time F1 is run without making a direct call to F2 from within F1? Is there some other mechanism by which to know when a function from inside another class has been called? I wel...

Prevent splitting Window when using pythoncomplete in Vim

I'm using VIM with pythoncomplete. When I'm making a completion, the current window is splitted and calltips are shown in the upper pane. I hate that! Is there a way to prevent that behavior or at least limit the size of the upper pane automaticly? ...

how to extract column from a multi-dimentional array

does anybody know how do to extract a column from a multidimentional array in python.. ...

Python: Sending a large dictionary to a server

Hi Pythonistas, I have an application that should communicate status information to a server. This information is effectively a large dictionary with string keys. The server will run a web application based on Turbogears, so the server-side method called accepts an arbitrary number of keyword arguments. In addition to the actual data,...

Chain-calling parent constructors in python

Consider this - a base class A, class B inheriting from A, class C inheriting from B. What is a generic way to call a parent class constructor in a constructor? If this still sounds too vague, here's some code. class A(object): def __init__(self): print "Constructor A was called" class B(A): def __init__(self): ...

Reading a UTF8 CSV file with Python

I am trying to read a CSV file with accented characters with Python (only French and/or Spanish characters). Based on the Python 2.5 documentation for the csvreader (http://docs.python.org/library/csv.html), I came up with the following code to read the CSV file since the csvreader supports only ASCII. def unicode_csv_reader(unicode_csv...

python-mysql : How to get interpolated query string?

In diagnosing SQL query problems, it would sometimes be useful to be able to see the query string after parameters are interpolated into it, using MySQLdb's safe interpolation. Is there a way to get that information from either a MySQL exception object or from the connection object itself? ...