python

Python Twitter library: which one?

I realize this is a bit of a lazyweb question, but I wanted to see which python library for Twitter people have had good experiences with. I've used Python Twitter Tools and like its brevity and beauty of interface, but it doesn't seem to be one of the popular ones - it's not even listed on the Twitter Libraries page. There are, howev...

KeyError when trying to save model instance. Django.

KeyError when trying to save model instance. It has to react on post_save signal than save instance... Code: from django.db.models.signals import post_save class PlaylistEntry(models.Model): playlist=models.ForeignKey(Playlist) media=models.ForeignKey(Media) order=models.PositiveIntegerField(default=9000000, editable=False...

SSH Tunnel for Python MySQLdb connection

I tried creating a SSH tunnel using ssh -L 3306:localhost:22 <hostip> Then running my python script to connect via localhost conn = MySQLdb.connect(host'localhost', port=3306, user='bob', passwd='na', db='test') However, I receive the following error (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql....

Substitute for u'string'

I saved my script in UTF-8 encoding. I changed my codepage on windows to 65001. I'm on python 2.6 Script #1 # -*- coding: utf-8 -*- print u'Español' x = raw_input() Script #2 # -*- coding: utf-8 -*- a = 'Español' a.encode('utf8') print a x = raw_input() Script #1, prints the word fine with no errors, Script #2 does error: Un...

Python: How do I convert an int to its string representation with a set number of digits?

Pretty much what it says up there. Basically, how do I get the string produced by print "%05d" % 100 ...

How to parse broken XML in Python?

A sever I can't influence sends very broken XML. Specifically, a Unicode WHITE STAR would get encoded as UTF-8 (E2 98 86) and then translated using a Latin-1 to HTML entity table. What I get is &acirc; 98 86 (9 bytes) in a file that's declared as utf-8 with no DTD. I couldn't configure W3C tidy in a way that doesn't garble this irrever...

In Python, how to add distinct items to a list of dicts from another dict?

In Python, the original dict list is as follows: orig = [{'team': 'team1', 'other': 'blah', 'abbrev': 't1'}, {'team': 'team2', 'other': 'blah', 'abbrev': 't2'}, {'team': 'team3', 'other': 'blah', 'abbrev': 't3'}, {'team': 'team1', 'other': 'blah', 'abbrev': 't1'}, {'team': 'team3', 'other': 'blah', 'abbre...

Display GAE list by most recent entry

n00b problem- I am trying to have a list show the most recent entry first. This works without the reverse(), but retrieves nothing with it in. I have heard I should try and use order_by(), but I can't seem to get that to work either. Thanks for the help! class MainHandler(webapp.RequestHandler): def get(self): que = db.Query(model...

Selenium, with Python, how to simplify scripts so that I can run them from other python scripts?

I'm having some trouble figuring out how to take out what is not necessary in a selenium strip and package it in such a way that I can call it from another script.. I am having trouble understanding what is going on with this, as I don't get where the unit testint parts are coming from... ideally if I could just separate this into a func...

Creating REST Web Services with Python

Hi guys, Is it possible to create REST Web Services, that returns JSON or XML, using Python ? Could you give me some recomandations ? Thank you. ...

How to print japanese utf-8 on console in windows?

#coding=<utf8> import os os.popen('chcp 65001') a = 'こんにちは世界' print a.decode('utf8') x = raw_input() PYTHON 2.6 on Windows 7 It will run in IDLE with no errors. However when run from the console, it errors and flashes very quickly and I can't read the error message. How can it be done in windows console? By the way, doing this wit...

MySQL error: 2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0"

I'm having an issue connecting to my local MySQL database using Python's MySQLdb library. The script has been working well previously, but I will occasionally get the MySQL error in the title. There seems to be no explanation for when the error occurs, and the script is always run from the same machine with the same arguments. The MySQ...

Newline showing up on screen but not in email

I have a list (errors) that I both print to the screen and send in the body of an email. But first I separate the elements of the list with a newline character: "\n".join(errors) I then print it to the console and send it as an email. On the console it appears delimited by newlines: Error generating report Another error In the emai...

Does this web app require a task queue?

Background I have a web app that will create an image from user input. The image creation could take up to a couple seconds. Problem If I let the server thread, that is handling the request/response also generate the image, that is going to tie up a thread for a couple seconds, and possibly bog down my server, affect performance, kill...

Image bit manipulation in Python

Hello SO, I have an application that receives a pointer to JPEG data from a camera API wrapped with ctypes, converts it to a wx.Image, and displays the images as a movie. One of required features is to set two of the components of a pixel equal to the third. E.g, my pixel in RGB format is (100,200,255), I want to set the the R and B va...

How to import a class from a module from a package dynamically?

This is probably really simple, but I don't understand it. The following works: foo.py class Foo: pass bar.py module = __import__('foo') foo = module.__dict__['Foo'] Afterwards, foo is the class foo.Foo as expected. Yet, if I put a.py into a package, it stops working: qux/__init__.py (empty file) qux/foo.py class Foo: ...

Behavior of Python exec() differs depending on the where it is called from

I have a Python script 'runme.py' that I am trying to execute from 'callerX.py' below. I am using exec(open(filename).read()) to accomplish this task. The script being executed contains a simple class that attempts to call the 'time()' function both from the global namespace & inside a function. In all of the examples below, we are exec...

Convert list of Fractions to floats in Python

I have a list of fractions, such as: data = ['24/221 ', '25/221 ', '24/221 ', '25/221 ', '25/221 ', '30/221 ', '31/221 ', '31/221 ', '31/221 ', '31/221 ', '30/221 ', '30/221 ', '33/221 '] How would I go about converting these to floats, e.g. data = ['0.10 ', '0.11 ', '0.10 ', '0.11 ', '0.13 ', '0.14 ', '0.14 ', '0.14 ', '0.14 ', '0.14...

How to display utf-8 in windows console

I'm using Python 2.6 on Windows 7 I borrowed some code from here: http://stackoverflow.com/questions/5419/python-unicode-and-the-windows-console My goal is to be able to display uft-8 strings in the windows console. Apparantly in python 2.6, the sys.setdefaultencoding() is no longer supported However, I wrote reload(sys) ...

I can`t decide what to select: ASP.NET MVC 2 (C#) or Django (Python)?

I`m learning programming languages. And I decide that I need to lear a new web framework. I have 2 candidates: Django or ASP.NET MVC 2. Can you say me the difference between them and what is so interesting? ...