python

Common elements between two lists not using sets in Python

I want count the same elements of two lists. Lists can have duplicate elements, so I can't convert this to sets and use & operator. a=[2,2,1,1] b=[1,1,3,3] set(a) & set(b) work a & b don't work It is possible to do it withoud set and dictonary? ...

Error Installing Pyhdf on Mac

Hi, I am working on porting an existing windows application to mac OS X and I am stuck. I am not able to install pyhdf on my mac. [OS X 10.5.8]. I have HDF4 installed through macports (Is this a problem?). $sudo python setup.py install running install running build running config_fc running build_src building extension "pyhdf._hdfext"...

add methods in subclasses within the super class constructor

I want to add methods (more specifically: method aliases) automatically to Python subclasses. If the subclass defines a method named 'get' I want to add a method alias 'GET' to the dictionary of the subclass. To not repeat myself I'd like to define this modifation routine in the base class. But if I check in the base class __init__ met...

finding the missing values in a range using any scripting language - perl, python or shell script

I got stuck in one problem of finding the missing values in a range and the range is also variable for the successive rows. input 673 673 673 676 676 680 2667 2667 2668 2670 2671 2674 output should be like this 674 675 677 678 679 2669 2672 2673 This is just one part and the row values can be more also If you need any clarification,...

Python indentation in "empty lines"

Which is preferred ("." indicating whitespace)? A) def foo(): x = 1 y = 2 .... if True: bar() B) def foo(): x = 1 y = 2 if True: bar() My intuition would be B (that's also what vim does for me), but I see people using A) all the time. Is it just because most of the editors out there are bro...

how to create application(exe) from python script for linux

hi expert, i'm newbie for python programming, i'm having a .py file, now what shall i do so i can create an application from .py file and it can be istall and run in any linux pc, i try to packaging it but its just create .tar file where i need python to run it, is it any to do so, thanks ...

Passing parameter to base class constructor or using instance variable?

All classes derived from a certain base class have to define an attribute called "path". In the sense of duck typing I could rely upon definition in the subclasses: class Base: pass # no "path" variable here def Sub(Base): def __init__(self): self.path = "something/" Another possiblity would be to use the base class c...

New at Python: GLPK not building properly / Python ImportError

This is a beginner question, and a follow-up to this one, where I was pointed to GLPK. I'm trying to get PyGLPK, a Python binding for the GNU Linear Programming Kit up and running, but no matter what I do, I can't seem to build and install GLPK so that Python finds it correctly. This comes after running ./configure, make, and sudo make...

Apache + Mod_wsgi returning 502 Bad Gateway!

Hi folks, I'm serving Django with mod_wsgi and Apache... unfortunately requests are returning 502 Bad Gateway error messages... Received a invalid response HttpResponse('OK') is affected by this render_to_response('...') is not! any ideas?!? ...

efficient list mapping in python

Hi everyone, I have the following input: input = [(dog, dog, cat, mouse), (cat, ruby, python, mouse)] and trying to have the following output: outputlist = [[0, 0, 1, 2], [1, 3, 4, 2]] outputmapping = {0:dog, 1:cat, 2:mouse, 3:ruby, 4:python, 5:mouse} Any tips on how to handle given with scalability in mind (var input can get rea...

How to create a CFuncType in Python

I need to pass a callback function that is CFuncType (ctypes.CFUNCTYPE or ctypes.PYFUNCTYPE...). How can I cast a python function to CFuncType or how can I create a CFuncType function in python. ...

Python BOM error in Ascii file

I have a wierd annoying problem with Python 2.6 I trying to run this file (and the other), on my Embedded Linux ARM board. http://svn.tuxisalive.com/software_suite_v3/smart-core/smart-server/trunk/TDSService.py I get this error File "tuxhttpserver.py", line 1 SyntaxError: encoding problem: with BOM I know that error is about ...

How to speed up read method call of object returned by urllib2.urlopen

I have following code. req = urllib2.Request(url,'',txheaders) f = urllib2.urlopen(req) data = f.read() f.close() In the above code, the read function takes 1-2 minutes when response is of 58KB. How can I make this faster. ...

Webfaction apache + mod_wsgi + django configuration issue

A problem that I stumbled upon recently, and, even though I solved it, I would like to hear your opinion of what correct/simple/adopted solution would be. I'm developing website using Django + python. When I run it on local machine with "python manage.py runserver", local address is http://127.0.0.1:8000/ by default. However, on produc...

How do you handle options that can't be used together (using OptionParser)?

My Python script (for todo lists) is started from the command line like this: todo [options] <command> [command-options] Some options can not be used together, for example todo add --pos=3 --end "Ask Stackoverflow" would specify both the third position and the end of the list. Likewise todo list --brief --informative would con...

DSA module in Python

Does anyone know of a pure DSA module in Python for signing messages? ...

Words doesn't starts with numbers

I have a string "one two 9three 52eight four", so I only want to get "one two four", because "three" starts with "9" and "eight" starts with "52". I tried: "(?!\d)\w+" but it's still taking the "three" and "eight". I don't want it. ...

Does dict.update affect a function's argspec?

import inspect class Test: def test(self, p, d={}): d.update(p) return d print inspect.getargspec(getattr(Test, 'test'))[3] print Test().test({'1':True}) print inspect.getargspec(getattr(Test, 'test'))[3] I would expect the argspec for Test.test not to change but because of dict.update it does. Why? ...

Python: does it make sense to refactor this check into its own method?

I'm still learning python. I just wrote this method to determine if a player has won a game of tic-tac-toe yet, given a board state like: '[['o','x','x'],['x','o','-'],['x','o','o']]' def hasWon(board): players = ['x', 'o'] for player in players: for row in board: if row.count(player) == 3: return player top, m...

Replacement for htmllib module in Python 3.0

I want to use the htmllib module but it's been removed from Python 3.0. Does anyone know what's the replacement for this module? ...