python

Proper way to assert type of variable in Python

In using a function, I wish to ensure that the type of the variables are as expected. How to do it right? Here is an example fake function trying to do just this before going on with its role: def my_print(begin, text, end): """Print 'text' in UPPER between 'begin' and 'end' in lower """ for i in (begin, text, end): ...

Creating a method that is simultaneously an instance and class method

In Python, I'd like to be able to create a function that behaves both as a class function and an instance method, but with the ability to change behaviors. The use case for this is for a set of serializable objects and types. As an example: >>> class Thing(object): #... >>> Thing.to_json() 'A' >>> Thing().to_json() 'B' I know that...

Find full path of python interpreter?

How do I find the full path of the currently running Python interpreter? ...

conversion of html to pdf in python

I need to convert HTML documents into PDF format. Can u help me? ...

How to define and use Python generators appropriately

I want to define a generator from a list that will output the elements one at a time, then use this generator object in an appropriate manner. a = ["Hello", "world", "!"] b = (x for x in a) c = next(b, None) while c != None: print c, c = next(b, None) Is there anything wrong or improvable with the while approach here? Is ther...

What are the common patterns in web programming?

I have been trying to write my first big web app (more than one cgi file) and as I kept moving forward with the rough prototype, paralelly trying to predict more tasks, this is the todo that got accumulated (In no particular order). * Validations and input sanitizations * Object versioning (to avoid edit conflicts. I dont want hard lock...

Scipy Negative Distance? What?

I have a input file which are all floating point numbers to 4 decimal place. i.e. 13359 0.0000 0.0000 0.0001 0.0001 0.0002` 0.0003 0.0007 ... (the first is the id). My class uses the loadVectorsFromFile method which multiplies it by 10000 and then int() these numbers. On top of that, I also loop through each ...

Histogram in Matplotlib with input file

I wish to make a Histogram in Matplotlib from an input file containing the raw data (.txt). I am facing issues in referring to the input file. I guess it should be a rather small program. Any Matplotlib gurus, any help ? I am not asking for the code, some inputs should put me on the right way ! ...

Eclipse + PyDev: Eclipse telling me that this is an invalid import?

I recently installed twython, a really sleek and awesome twitter API wrapper for Python. I installed it and it works fine from the interpreter, but when I try to import it via Eclipse, it says that twython is an invalid import. How do I "tell" eclipse where twython is so that it will let me import and use it? ...

MySQLdb not INSERTING, _mysql does fine.

Okay, I log onto the MySQL command-line client as root. I then open or otherwise run a python app using the MySQLdb module as root. When I check the results using python (IDLE), everything looks fine. When I use the MySQL command-line client, no INSERT has occurred. If I change things around to _mysql instead of MySQLdb, everything works...

how to find whether a string in a another string..

a='1234;5' print a.index('s') thr error is : > "D:\Python25\pythonw.exe" "D:\zjm_code\kml\a.py" Traceback (most recent call last): File "D:\zjm_code\kml\a.py", line 4, in <module> print a.index('s') ValueError: substring not found thanks ...

How to localize static content in database with Django

My app has tables for languages and countries (actually django-countries at the moment, but open for suggestions). The tables are populated when I initialize the database and remain static after that. What would be the ideal localization mechanism for the contents of these tables, so that I can show the country and language names to us...

Matplotlib: plotting discrete values

I am trying to plot the following ! from numpy import * from pylab import * import random for x in range(1,500): y = random.randint(1,25000) print(x,y) plot(x,y) show() However, I keep getting a blank graph (?). Just to make sure that the program logic is correct I added the code print(x,y), just the confirm that (x,...

Cant compile uwsgi under centos (fc10) with python2.6

it`s normally build with python 2.5 BUT i need 2.6! 2.6 normally installed in /opt/python26 and successfully run as python2.6 in console. but python2.6 uwsgiconfig.py --build give me *** uWSGI linking *** /usr/bin/ld: cannot find -lpython2.6 collect2: ld returned 1 exit status HELP! ...

Setting A Generator As The Argument Of A PyQt4 Signal

I want to use a generator as the argument passed by a PyQt4 signal, and I am not sure as to the cleanest way. I could just do something like elementChosen=QtCore.pyqtSignal(type((i for i in xrange (i)))), but this just looks ugly. Any suggestions? ...

Find next lower item in a sorted list

Hey guys, let's say I have a sorted list of Floats. Now I'd like to get the index of the next lower item of a given value. The usual for-loop aprroach has a complexity of O(n). Since the list is sorted there must be a way to get the index with O(log n). My O(n) approach: index=0 for i,value in enumerate(mylist): if value>compareVa...

How do I write this Django model in SQL?

I want to create a new column. How do I write this in SQL? class mytable(models.Model): created_at = models.DateTimeField(auto_now_add=True) ...

strip only html anchor tags.

i have following code that strip all tags. now i want to strip only anchor tags. x = re.compile(r'<[^<]*?/?>') how to modify so that only anchor tags stripped. ...

Getting a specific bit value in a byte string

There is a byte at a specific index in a byte string which represents eight flags; one flag per bit in the byte. If a flag is set, its corresponding bit is 1, otherwise its 0. For example, if I've got b'\x21' the flags would be 0001 0101 # Three flags are set at indexes 0, 2 and 4 # and the others are not set What w...

Non-ASCII character Syntax Error in Python while calling URL.

Hi experts, How can data can be sent to the server? For example, I have retrieve MAC address, so I want send to the server (e.g. http://211.21.24.43:8080/data?mac=00-0C-F1-56-98-AD) I found this snippet on the Internet: from urllib2 import Request, urlopen from binascii import b2a_base64 def b64open(url, postdata): req = Request(...