python

Best practice for Python Assert

Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, 'x is less then zero' and better or worse then if x < 0: raise Exception, 'x is less then zero' Also, is there anyway to set a business rule like if x < 0 raise err...

How to check for NaN in python?

float('nan') results in a thingy simply called nan. But how do I check for it? Should be very easy, but i cannot find it. ...

How to extract nested tables from HTML?

Hi everybody. I have an HTML file (encoded in utf-8). I open it with codecs.open(). The file architecture is: <html> // header <body> // some text <table> // some rows with cells here // some cells contains tables </table> // maybe some text here <table> // a form and other stuff </table> // probably some more...

Numpy: Should I use newaxis or None?

In numpy one can use the 'newaxis' object in the slicing syntax to create an axis of length one, e.g.: import numpy as np print np.zeros((3,5))[:,np.newaxis,:].shape # shape will be (3,1,5) The documentation states that one can also use None instead of newaxis, the effect is exactly the same. Is there any reason to choose one over th...

Publish feeds using Django

I'm publishing a feed from a Django application. I subclassed django.contrib.syndication.feeds.Feed, everything works fine, except the date that doesn't get published on the feed. Here's the method I've created on my Feed class def item_pubdate(self, item): return item.date this method never gets called.... ...

How can I script the creation of a movie from a set of images?

I managed to get a set of images loaded using Python. I'd like my script to take this series of images (in whatever format I need them), and create a video from them. The big limit in all this is that I am looking for something easy and simple to install. Ideally, using the standard OS X installation procedure: download .dmg click m...

Why doesn't anyone care about this MySQLdb bug? is it a bug?

TL;DR: I've supplied a patch for a bug I found and I've got 0 feedback on it. I'm wondering if it's a bug at all. This is not a rant. Please read this and if you may be affected by it check the fix. I have found and reported this MySQLdb bug some weeks ago (edit: 6 weeks ago), sent a patch, posted it on a couple of ORM's forums, mailed ...

is there a multiple format specifier in Python?

I have a data table 44 columns wide that I need to write to file. I don't want to write: outfile.write("%i,%f,%f,$f ... )\n" % (i, a,b,c ...)) In Fortran you can specify multiple format specifiers easily: write (*,"(3f8.3)") a,b,c Is there a similar capability in Python? ...

Equivalent Javascript Functions for Python's urllib.quote() and urllib.unquote()

Hello, Are there any equivalent Javascript functions for Python's urllib.quote() and urllib.unquote()? The closest I've come across are escape(), encodeURI(), and encodeURIComponent() (and their corresponding un-encoding functions), but they don't encode/decode the same set of special characters as far as I can tell. Thanks, Cameron ...

Using Python's list index() method on a list of tuples or objects?

Python's list type has an index() method that takes one parameter and returns the index of the first item in the list matching the parameter. For instance: >>> some_list = ["apple", "pear", "banana", "grape"] >>> some_list.index("pear") 1 >>> some_list.index("grape") 3 Is there a graceful (idiomatic) way to extend this to lists of co...

How to execute a process remotely using python.

I want to connect too and execute a process on a remote server using python. I want to be able to get the return code and stderr(if any) of the process. Has anyone ever done anything like this before. I have done it with ssh, but I want to do it from python script. Cheers. ...

Get file creation time with Python on Mac

Python's os.path.getctime on the Mac (and under Unix in general) does not give the date when a file was created but "the time of the last change" (according to the docs at least). On the other hand in the Finder I can see the real file creation time so this information is kept by HFS+. Do you have any suggestions on how to obtain the fi...

using pyodbc on linux to insert unicode or utf-8 chars in a nvarchar mssql field

I am using Ubuntu 9.04 I have installed the following package versions: unixodbc and unixodbc-dev: 2.2.11-16build3 tdsodbc: 0.82-4 libsybdb5: 0.82-4 freetds-common and freetds-dev: 0.82-4 I have configured /etc/unixodbc.ini like this: [FreeTDS] Description = TDS driver (Sybase/MS SQL) Driver = /usr/lib/odbc/libt...

Custom simple Python HTTP server not serving css files.

Hello, I had found written in python, a very simple http server, it's do_get method looks like this: def do_GET(self): try: self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers(); filepath = self.path print filepath, USTAW['rootwww'] ...

How to I reload global vars on every page refresh in DJango

Here is my problem. DJango continues to store all the global objects after the first run of a script. For instance, an object you instantiate in views.py globally will be there until you restart the app server. This is fine unless your object is tied to some outside resource that may time out. Now the way I was thinking to correct wa...

Strip all non-numeric characters (except for ".") from a string in Python

I've got a pretty good working snippit of code, but I was wondering if anyone has any better suggestions on how to do this: val = ''.join([c for c in val if c in '1234567890.']) What would you do? ...

How can I get an accurate absolute url from get_absolute_url with an included urls.py in Django?

I've building a app right now that I'm trying to keep properly decoupled from the other apps in my Django project (feel free to lecture me on keeping Django apps decoupled, I'd be happy to learn more any/all the time). My problem is this: The get_ absolute_url() method I've written is returning a relative path based on my view. I think ...

How to save a Python interactive session?

I find myself frequently using Python's interpreter to work with databases, files, etc -- basically a lot of manual formatting of semi-structured data. I don't properly save and clean up the useful bits as often as I would like. Is there a way to save my input into the shell (db connections, variable assignments, little for loops and b...

Python Opencv Ubuntu not creating Windows

I have a strange problem with opencv running on an Ubuntu. I installed OpenCV from the apt sources. And most of the Examples work fine. But in my programs, which are working with Mac OS, no windows are created. The following code is showing a window and an image in this on my Mac but not on my Ubuntu powered machine import time from o...

Advanced Python programming book like "Effective C++"?

There are a lot of books out there for new programmers getting into Python. However, are there any books for people who are experienced both in programming and to some degree in Python that introduces advanced topics, subtleties, gotchas, and best practices in Python? I'm thinking in terms of something like Effective C++ or Effective Ja...