python

Should I use a class in this: Reading a XML file using lxml.

Hi everyone. This question is in continuation to my previous question, in which I asked about passing around an ElementTree. I need to read the XML files only and to solve this, I decided to create a global ElementTree and then parse it wherever required. My question is: Is this an acceptable practice? I heard global variables are ba...

Python - How to find a correlation between two vectors?

Given two vectors X and Y, I have to find their correlation, i.e. their linear dependence/independence. Both vectors have equal dimension. The result should be a floating point number from [-1.0 .. 1.0]. Example: X=[-1, 2, 0] Y=[ 4, 2, -0.3] Find y = cor(X,Y) such that y belongs to [-1.0 .. 1.0]. It should be a simple construct...

numpy calling sse2 via ctypes

Hello, In brief, I am trying to call into a shared library from python, more specifically, from numpy. The shared library is implemented in C using sse2 instructions. Enabling optimisation, i.e. building the library with -O2 or –O1, I am facing strange segfaults when calling into the shared library via ctypes. Disabling optimisation (-O...

How to convert string "0671" or "0x45" into integer form with 0 and 0x in the beginning.

I wanted to make my own encryption algorithm and decryption algorithm , encryption algorithm works fine and converts ascii value of the characters into alternate hexadecimal and octal representations. But when I tried decryption, problem occured as it return int('0671') = 671, as 0671 is string type in the following code. Is there a meth...

Python and object/class attrs - what's going on?

Can someone explain why Python does the following? >>> class Foo(object): ... bar = [] ... >>> a = Foo() >>> b = Foo() >>> a.bar.append(1) >>> b.bar [1] >>> a.bar = 1 >>> a.bar 1 >>> b.bar [1] >>> a.bar = [] >>> a.bar [] >>> b.bar [1] >>> del a.bar >>> a.bar [1] It's rather confusing! ...

Image Gurus: Optimize my Python PNG transparency function

I need to replace all the white(ish) pixels in a PNG image with alpha transparency. I'm using Python in AppEngine and so do not have access to libraries like PIL, imagemagick etc. AppEngine does have an image library, but is pitched mainly at image resizing. I found the excellent little pyPNG module and managed to knock up a little fun...

how to profile my code??

i want to how to profile my code... i have gone through the docs , but as there were no example codes given i could not get anything from it. i have a large code and it is taking so much time hence want to profile and increase its speed. i havent written my code in method , there are few in between but not completely. i dont have any mai...

unittest in python: ignore an import from the code I want to test

I have a python program that imports pythoncom (and uses pythoncom.CoCreateInstance from it). I want to create a unittest for the program logic without it importing pythoncom (so I can run the test on Linux as well). What options are there? Can I do it without modifying the system under test? What I found so far: sys.modules["pythonco...

python: iif or (x ? a : b)

Possible Duplicate: Python Ternary Operator If Python would support the (x ? a : b) syntax from C/C++, I would write: print paid ? ("paid: " + str(paid) + " €") : "not paid" I really don't want to have an if-check and two independent prints here (because that is only an example above, in my code, it looks much more complica...

When I m starting to develop Java Applications What is required to do?

Possible Duplicate: Learning Java? Hi, I'm basically a Python programmer and I want to develop a java based application. What basic requirements do I need to develop? ...

How does Qt works (exactly)?

When you write an application using Qt, it can be run in different operating systems. And - correct me if I'm wrong - you don't need to have Qt already installed in all of the plataforms you execute your application. How exactly this approach works? Does Qt compiles to the desired platform, does it bundle some "dlls" (libs), how does it...

Convert a GTK python script to C

The following script will take a screenshot on a Gnome desktop. import gtk.gdk w = gtk.gdk.get_default_root_window() sz = w.get_size() pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False, 8, sz[0], sz[1]) pb = pb.get_from_drawable(w, w.get_colormap(), 0, 0, 0, 0, sz[0], sz[1]) if (pb != None): pb.save("screenshot.png", "png") prin...

Convert hex to utf in Python

Hi.. I have a hex string and i want to convert it utf8 to insert mysql. (my database is utf8) hex_string = 'kitap ara\xfet\xfdrmas\xfd' .. .. .. result='kitap araştırması' How can i do that? Best regards. ...

Django: how to create sites dynamically?

Hi, I need to create an application for the company where I can create sites dynamically. For example, I need an admin interface (Django's admin is enough) where I can setup a new site and add some settings to it. Each site must hold a domain (domains can be manually added to apache conf, but if Django can handle it too would be awesome...

How do I override file.write() under Python 3?

Below works on Python 2.6 but on Python 3.x it doesn't: old_file_write = file.write class file(): def write(self, d): if isinstance(d, types.bytes): self.buffer.write(d) else: old_file_write(d) # ... some code I cannot change or do not want to change f = open("x") f.write("...") f.write(b"....

Can this code be further optimized??

i understand that the code given below will not be compltely understood unless i explain my whole of previous and next lines of code. But this is part of the code which is causing so much of delay in my project and want to optimize this. i want to know which code part is faulty and how could this be replaced. i guess,few can say that use...

I suspect I have multiple version of Python 2.6 installed on Mac OS X 10.6.3; how do I set which one Terminal should launch?

When I enter in python in Terminal it loads up Python 2.6.2. However there are folders by the name of Python 2.6 in different places on my drive. I'm not sure if that's because Python 2.6 has been installed in different places or because Python just likes to have lots of folers in different places. If there are multiple installations, I...

Join a list of lists together into 1 list in Python

Possible Duplicate: Flatten (an irregular) list of lists in Python Hay All. I have a list which consists of many lists, here is an example [ [Obj, Obj, Obj, Obj], [Obj], [Obj], [ [Obj,Obj], [Obj,Obj,Obj] ] ] Is there a way to join all these items together as 1 list, so the output will be...

Simple wrapping of C code with cython

Hi, I have a number of C functions, and I would like to call them from python. cython seems to be the way to go, but I can't really find an example of how exactly this is done. My C function looks like this: void calculate_daily ( char *db_name, int grid_id, int year, double *dtmp, double *dtmn, double *dtmx, ...

Why is autoconf ac_python_devel requiring libssl, libcrypto?

I am writing an autoconf script for a library that has some Python-based test programs. I am trying to add a conditional in the script that tests for Python devel stuff and turns these tests off or on. I'm trying to use autoconf archive's AC_PYTHON_DEVEL macro to test for python. I'm calling it like this: AC_PYTHON_DEVEL([>='2.3']) ...