python

python for in control structure

I am a php programmer trying to understand python's for in syntax I get the basic for in for i in range(0,5): in php would be for ($i = 0; $i < 5; $i++){ but what does this do for x, y in z: and what would be the translation to php? This is the full code i am translating to php: def preProcess(self): """ plan for the arra...

How to identify whether a variable is a class or an object

I am working at a bit lower level writing a small framework for creating test fixtures for my project in Python. In this I want to find out whether a particular variable is an instance of a certain class or a class itself and if it is a class, I want to know if it is a subclass of a certain class defined by my framework. How do I do it? ...

How to get the arch string that distutils uses for builds?

When I build a c extension using python setup.py build, the result is created under a directory named build/lib.linux-x86_64-2.6/ where the part after lib. changes by the OS, CPU and Python version. Is there a way I can access the appropriate string for my current architecture from python? Hopefully in a way that is guaranteed to mat...

how to match UserProperty() with StringProperty()

i want to match StringProperty() with UserProperty & i cant change property so how i can achieve it? Please help me out.................. ...

NameError: name 'self' is not defined

Why such structure class A: def __init__(self, a): self.a = a def p(self, b=self.a): print b gives an error NameError: name 'self' is not defined? ...

Speeding up computations with numpy matrices

I have two matrices. Both are filled with zeros and ones. One is a big one (3000 x 2000 elements), and the other is smaller ( 20 x 20 ) elements. I am doing something like: newMatrix = (size of bigMatrix), filled with zeros l = (a constant) for y in xrange(0, len(bigMatrix[0])): for x in xrange(0, len(bigMatrix)): for b in...

How to install mysql connector

I have downloaded mysqlDb, and while installing it I am getting errors like: C:\Documents and Settings\naresh\Desktop\MySQL-python-1.2.3c1>setup.py build Traceback (most recent call last): File "C:\Documents and Settings\naresh\Desktop\MySQL-python-1.2.3c1 \setup.py",line15, in metadata, options = get_config() File "C:\Document...

Initialize project layout in python ?

Suppose a programmer has the following problem: he wants to start a new python project. He needs a basic layout of boilerplate stuff, like test directory, source directory, setuptools script etc.. How does he create all this stuff and layout with a single command ? For example, paster (as suggested in one of the answers, provides you th...

Pythonic way to select first variable that evaluates to True

I have some variables and I want to select the first one that evaluates to True, or else return a default value. For instance I have a, b, and c. My existing code: result = a if a else (b if b else (c if c else default)) Another approach I was considering: result = ([v for v in (a, b, c) if v] + [default])[0] But they both feel me...

Fast Graphics with XServer

I am working on embedded linux platform with limited system resources. I want to do fullscreen slideshow with simple transistions (like slide in-out, fade in-out ). I tried PyGtk+GTK+Cairo but its very slow, when I animate GTK image controls I get just two or three frames per second. But smplayer is playing video at good speed! I did ...

replace the NaN value zero after an operation with arrays

hi all, how can I replace the NaN value in an array, zero if an operation is performed such that as a result instead of the NaN value is zero operations as 0 / 0 = NaN can be replaced by 0 ...

How to set User-Agent in python-twitter ?

Hi, i'm writing a small script to tweet messages from the monitoring systems. The only issue i ran into so far is that i can't set the User-Agent correctly, all tweets show up as "from API" which ain't a huge deal but i wonder what I'm doing wrong. An example to reproduce this behavior: import sys import twitter USERNAME="twitteruser...

communicate with a process in utf-8 on a cp1252 consoless

I need to control a program by sending commands in utf-8 encoding to its standard input. For this I run the program using subprocess.Popen(): proc = Popen("myexecutable.exe", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) proc.stdin.write(u'ééé'.encode('utf_8')) If I run this from a cygwin utf-8 console, it works. If I run it from ...

Python: Can you make this __eq__ easy to understand?

Hello SO, I have another question for you. I have a python class with a list 'metainfo'. This list contains variable names that my class might contain. I wrote a __eq__ method that returns True if the both self and other have the same variables from metainfo and those variables have the same value. Here is my implementation: def __eq...

Python regular expression matching a multiline block of text but not replacing it

Ok so i have this piece of code: def findNReplaceRegExp(file_name, regexp, replaceString, verbose=True, confirmationNeeded=True): '''Replaces the oldString with the replaceString in the file given,\ returns the number of replaces ''' # initialize local variables cregexp = re.compile(regexp, re.MULTILINE | re.DOTALL) somet...

Error codes returned by urllib/urllib2 and the actual page

Hi guys, the normal behavior of urllib/urllib2 is if an error code is sent in the header of the response (i.e 404) an Exception is raised. How do you look for specific errors i.e (40x, or 50x) based on the different errors, do different things. Also, how do you read the actual data being returned HTML/JSON etc (The data usually has err...

Recognising guitar input tone from mic

Possible Duplicate: Recognising tone of the audio Hi I need to recognize what tone is being played on my mic which inputs in the pc from the mic, with python. What should I use? ...

make operations excluding the diagonal of an array

as I can perform operations on arrays so that does nothing on the diagonal is calculated such that all but the diagonal array ([[0., 1.37, 1., 1.37, 1., 1.37, 1.] [1.37, 0. , 1.37, 1.73, 2.37, 1.73, 1.37] [1. , 1.37, 0. , 1.37, 2. , 2.37, 2. ] [1.37, 1.73, 1.37, 0. , 1.37, 1.73, 2.37] [1. , 2.37, 2...

PyAudio.open, how to use?

I'm trying to make a pyaudio input stream but can't figure out how to make it. What I did is: a = pyaudio.PyAudio() Then tried to call a.open() but I don't know the arguments I should type in. It asks me to check Stream.init for a reference, but I don't know what a PA_MANAGER is and the documentation isn't useful at all about it. ...

Running methods on different cores on python

Is there any easy way to make 2 methods, let's say MethodA() and MethodB() run in 2 different cores? I don't mean 2 different threads. I'm running in Windows, but I'd like to know if it is possible to be platform independent. edit: And what about http://docs.python.org/dev/library/multiprocessing.html and parallel python ? ...