python

FastCGI C++ vs. A Script Language (PHP/Python/Perl)

What are the ups and downs of using FastCGI C++ vs. PHP/Python/Perl to do the same job. Any performance or design pitfalls or using one over the other? Even your opinions are welcome. (Tell me why one or the other rocks, or one or the other sucks). ...

How to hash a large object (dataset) in Python?

I would like to calculate a hash of a Python class containing a dataset for Machine Learning. The hash is meant to be used for caching, so I was thinking of md5 or sha1. The problem is that most of the data is stored in NumPy arrays; these do not provide a __hash__() member. Currently I do a pickle.dumps() for each member and calculate a...

Can I encrypt email and decrypt it back using python default library set?

Ofcourse similar questions have been asked in stackoverflow but I dont want to use any third party library like Crypto or something. So I need to generate a code based on user email and decrypt the code to generate the email back. Can I do it in python? ...

Django: Redirect to previous page after login

Hey, I'm trying to build a simple website with login functionality very similar to the one here on SO. The user should be able to browse the site as an anonymous user and there will be a login link on every page. When clicking on the login link the user will be taken to the login form. After a succesfull login the user should be taken b...

Python equivalent of Perl's while (<>) {...}?

I write a lot of little scripts that process files on a line-by-line basis. In Perl, I use while (<>) { do stuff; } This is handy because it doesn't care where the input comes from (a file or stdin). In Python I use this if len(sys.argv) == 2: # there's a command line argument sys.stdin = file(sys.argv[1]) for line in sys.st...

Where is the phpMailer php class equivalent for Python?

Hi guys, i'm new with python.. Actually, i'm trying to send featured email with python: html body, text alternative body, and attachment. So, i've found this tutorial and adapted it with the gmail authentication (tutorial found here) The code i have atm, is that: def createhtmlmail (html, text, subject): """Create a mime-message that ...

Django m2m queries, distinct Users for a m2m relationship of a Model

Hi, I have a model Model with a m2m field : user = .. fk user ... watchers = models.ManyToManyField(User, related_name="boardShot_watchers", null=True) How do i select all distinct Users involved in this watchers relationship for all my entries of type Model ? I dont think there is an ORM way to access to intermediary M2M tabl...

How to output list of floats to a binary file in Python

I have a list of floating-point values in Python: floats = [3.14, 2.7, 0.0, -1.0, 1.1] I would like to write these values out to a binary file using IEEE 32-bit encoding. What is the best way to do this in Python? My list actually contains about 200 MB of data, so something "not too slow" would be best. Since there are 5 values, I ju...

Decoding problems in Django and lxml

I have a strange problem with lxml when using the deployed version of my Django application. I use lxml to parse another HTML page which I fetch from my server. This works perfectly well on my development server on my own computer, but for some reason it gives me UnicodeDecodeError on the server. ('utf8', "\x85why hello there!", 0, 1,...

Printing XML into HTML with python

I have a TextEdit widget in PyQt that I use to print out a log in HTML. I use HTML so I can separate entries into color categories (red for error, yellow for debug, blue for message, etc), but this creates a problem. Most of the debug messages are XML. When I use appendHtml on the widget, it strips out all the tags. How can I pretty pri...

Parsing files with Python

What type of Python objects should I use to parse files with a specific syntax? Also what sort of loop should be followed to make it through the file. Should one pass be sufficient? Two, three? ...

Creating an interactive shell for .NET apps and embed scripting languages like python/iron python into it

I was learning python using the tutorial that comes with the standard python installation. One of the benefits that the author states about python is "maybe you’ve written a program that could use an extension language, and you don’t want to design and implement a whole new language for your application" - My question is how would i go a...

Python - Threading and a While True Loop

I have a thread that appends rows to self.output and a loop that runs until self.done is True (or the max execution time is reached). Is there a more efficient way to do this other than using a while loop that constantly checks to see if it's done. The while loop causes the CPU to spike to 100% while it's running.. time.clock() while ...

Find all IPs on an HTML Page

I want to get an HTML page with python and then print out all the IPs from it. I will define an IP as the following: x.x.x.x:y Where: x = a number between 0 and 256. y = a number with < 7 digits. Thanks. ...

Django Manager Chaining

I was wondering if it was possible (and, if so, how) to chain together multiple managers to produce a query set that is affected by both of the individual managers. I'll explain the specific example that I'm working on: I have multiple abstract model classes that I use to provide small, specific functionality to other models. Two of t...

cumulative normal distribution in Python

Hi, I am looking for a function in Numpy or Scipy (or any rigorous Python library) that will give me the cumulative normal distribution function in Python. This is for Black Scholes option pricing. I can program it myself but it is a (decent) approximation and I'd like to test if there's something (even) better as as I still get a few d...

distributed/faster python unit tests

I have a lot of python unit tests for a project and it's getting to the point where it takes a long time to run them. I don't want to add more because I know they're going to make things slower. How do people solve this problem? Is there any easy way to distribute the test execution over a cluster? ...

Any gotchas using unicode_literals in Python 2.6?

We've already gotten our code base running under Python 2.6. In order to prepare for Python 3.0, we've started adding: from __future__ import unicode_literals into our .py files (as we modify them). I'm wondering if anyone else has been doing this and has run into any non-obvious gotchas (perhaps after spending a lot of time debugg...

How to work with threads in pygtk

I have a problem with threads in pygtk. My application consist of a program that downloads pictures off the internet and then displays it with pygtk. The problem is that in order to do this and keep the GUI responsive, I need to use threads. So I got into a callback after the user clicked on the button "Download pictures" and I call th...

Python regex for finding contents of mediawiki markup links

If I have some xml containing things like the following mediawiki markup: " ...collected in the 12th century, of which [[Alexander the Great]] was the hero, and in which he was represented, somewhat like the British [[King Arthur|Arthur]]" what would be the appropriate arguments to something like: re.findall([[__?__]], ar...