python

Pass array of structs from Python to C

[Update: Problem solved! See bottom of the post] I need to allow python developers to pass an array of packed data (in this case vertices) into my API, which is a series of C++ interfaces exposed manually through the Python C API. My initial impression with this is to use the ctypes Structure class to allow for an interface like this: ...

In Python using Tkinter, what is the difference between root.destroy() and root.quit()?

In Python using Tkinter, what is the difference between root.destroy and root.quit when closing the root window? Is one prefered over the other? Does one release resources that the other doesn't? ...

Generating and applying diffs in python

Is there an 'out-of-the-box' way in python to generate a list of differences between two texts, and then applying this diff to one file to obtain the other, later? I want to keep the revision history of a text, but I don't want to save the entire text for each revision if there is just a single edited line. I looked at difflib, but I c...

Python JSON New York Times API

Hi Everyone, Really new to Python and getting data from the web, so here it goes. I have been able to pull data from the NYT api and parse the JSON output into a CSV file. However, depending on my search, I may get the following error when I attempt to write a row to the CSV. UnicodeEncodeError: 'charmap' codec can't encode charac...

Python class syntax - is this a good idea?

I'm tempted to define my Python classes like this: class MyClass(object): """my docstring""" msg = None a_variable = None some_dict = {} def __init__(self, msg): self.msg = msg Is declaring the object variables (msg, a_variable, etc) at the top, like Java good or bad or indifferent? I know it's unnecessar...

Uploading a HTML file to Google App Engine - getting 405

I'm trying to do a simple echo app using Python. I want to submit a file with a POST form and echo it back (an HTML file). Here's the handlers section of the YAML I'm using: handlers: - url: /statics static_dir: statics - url: .* script: main.py It's basically the hello world example in main.py and I added a directory to host m...

How does assignment of a function as a class attribute become a method in Python?

>>> class A(object): pass >>> def func(cls): pass >>> A.func = func >>> A.func <unbound method A.func> How does this assignment create a method? It seems unintuitive that assignment does the following for classes: Turn functions into unbound instance methods Turn functions wrapped in classmethod() into class methods (actually, this i...

When using Parallel Python, is there any way to tell on which machine the job has run?

I have written a simple program using parallel python, and all works well. However, mainly for curiosities sake, I would like to know on which machine each task ran, and how long it took. Is there any way to programmatically get this information for the job that is returned? ...

Encoding detection library in python

This is somehow related to my question here. I process tons of texts (in HTML and XML mainly) fetched via HTTP. I'm looking for a library in python that can do smart encoding detection based on different strategies and convert texts to unicode using best possible character encoding guess. I found that chardet does auto-detection extrem...

Why am I getting the error "cannot import name Scanner" when I try to use the mwclient module for Python?

I'm using Python 2.5.2 (because mwclient still only works for 2.x). I've copied the mwclient folder into the /usr/lib/python2.5/site-packages/mwclient folder, and when I run a program that imports mwclient I get this: Traceback (most recent call last): File "get_wiki.py", line 2, in <module> import mwclient File "/usr/lib/pyth...

GAE Task Queue oddness

I have been testing the taskqueue with mixed success. Currently I am using the default queue, in default settings ect ect.... I have a test url setup which inserts about 8 tasks into the queue. With short order, all 8 are completed properly. So far so good. The problem comes up when I re-load that url twice under say a minute. Now watc...

How to limit Python heap size?

I sometimes write Python programs which are very difficult to determine how much memory it will use before execution. As such, I sometimes invoke a Python program that tries to allocate massive amounts of RAM causing the kernel to heavily swap and degrade the performance of other running processes. Because of this, I wish to restrict ho...

Find maximum signed short integer in python

How do I get the maximum signed short integer in Python (i.e. SHRT_MAX in C's limits.h)? I want to normalize samples from a single channel of a *.wav file, so instead of a bunch of 16-bit signed integers, I want a bunch of floats between 1 and -1. Here's what I've got (the pertinent code is in the normalized_samples() function): def s...

Python - Spaces in Filenames

Possible Duplicate: How to escape os.system() calls in Python? Is there a Python method of making filenames safe (ie. putting \ infront of spaces and escaping ( , ), symbols) programatically in Python? ...

How to convert a numeric string with place-value commas into an integer?

In Python, what is a clean and elegant way to convert strings like "1,374" or "21,000,000" to int values like 1374 or 21000000? ...

How can I have an encrypted input in Python?

I am writing a Python program that requires a user to input their gmail usernames and passwords. When the user types in their password, I want the characters to be displayed as asterisks. Is this possible for a command-line program? ...

How to create comparison tables in Python

I want to generate comparison tables like this by using Python. Can you suggest any library to do this? ...

cx_Oracle, generators, and threads in Python

Hi all, What is the behavior of cx_Oracle cursors when the connection object is used by different threads? How would generators affect this behavior? Specifically... Edit: The original example function was incorrect; a generator was being returned by a sub function, yield wasn't used directly in the loop. This clarifies when finally...

Export list as .txt (Python)

My Python module has a list that contains all the data I want to save as a .txt file somewhere. The list contains several tuples like so: list = [ ('one', 'two', 'three'), ('four', 'five', 'six')] How do I print the list so each tuple item is separated by a tab and each tuple is separated by a newline? Thanks ...

Multiple value checks using 'in' operator (Python)

if 'string1' in line: ... ... works as expected but what if I need to check multiple strings like so: if 'string1' or 'string2' or 'string3' in line: ... ... doesn't seem to work. ...