python

how to get a certain number of elements from the database django

I have a simply class class BlogPost(models.Model): title = models.CharField(max_length=150) ... timestamp = models.DateTimeField() How do I get the last five items from the database. I tried to do like this: posts = BlogPost.objects.<any code> Excuse me for bad English ...

Transaction on GAE entity property

I am trying to create a vote function that increases the class URL.votes +1 when clicked. This is a two part question: How do you pull the entity key? (I think you need the key to distinguish which vote property is being modified?) How do you then write the 'a href' for the link to perform the vote? Thanks! Models: class URL(db.M...

unsigned char* image to Python

hello All, I was able to generate python bindings for a camera library using SWIG and I am able to capture and save image using the library's inbuilt functions. I am trying to obtain data from the camera into Python Image Library format, the library provides functions to return camera data as unsigned char* . Does anyone know how to c...

Fastest way to take a screenshot with python on windows

What's the fastest way to take a screenshot on windows? PIL.ImageGrab is rather slow.. it takes between 4-5 seconds to take 30 screenshots of the same small window. Taking screenshots of the whole desktop is even slower. ...

Localization of GUI built with Glade and Python (Gtk)

Hi, I have made an application using Glade and Python and I would like to make several localizations. I know how to localize strings that are in the Python code, I just encapsule all the strings that are supposed to be localized with _() and than specify the translation of the string in a .po file. But how do I tell a string that is bu...

Perform commands over ssh with Python

I'm writing a script to automate some command line commands in Python. At the moment I'm doing calls thus: cmd = "some unix command" retcode = subprocess.call(cmd,shell=True) However I need to run some commands on a remote machine. Manually, I would log in using ssh and then run the commands. How would I automate this in Python? I nee...

How to avoid NotImplementedError "Only tempfile.TemporaryFile is available for use" in django on Google App Engine?

I'm using django 1.1 on Google App Engine through use_library. No django gae helper, django-nonrel or similar tools are used here. Django handles urls routing, forms validation etc., but I'm using pure appengine models. In one of my django's forms there is a FileField, which from time to time seems to call django.core.files.uploadedfile...

django modeladmin list_display

I'm trying to play with the Django's official tutorial. Specifically the modeladmin list_display: http://docs.djangoproject.com/en/1.2/intro/tutorial02/#customize-the-admin-change-list How can I add a column that displays the number of choices for each poll in the list? Thanks! ...

How can I get 'urlpatterns = __import__(<string_name>)' to work like a normal import statement?

I'm trying to create an import statement that's pluggable with other projects. This statement is located in urls.py So this works: from forum.urls import urlpatterns # Base Class: <type 'list'> But this doesn't work: from settings import ROOT_URLCONF as project_urls urlpatterns = __import__(project_urls) # Base Class: <type ...

Does urllib2.urlopen() cache stuff?

They didn't mention this in python documentation. And recently I'm testing a website simply refreshing the site using urllib2.urlopen() to extract certain content, I notice sometimes when I update the site urllib2.urlopen() seems not get the newly added content. So I wonder it does cache stuff somewhere, right? ...

Why my program freezing while I am listening socket

So, here is some code: obj.HOST = "" obj.PORT = int(port.get()) # it's 100% correct PORT number obj.srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) obj.srv.bind((obj.HOST, obj.PORT)) obj.srv.listen(1) obj.sock, obj.addr = obj.srv.accept() class Client(threading.Thread): def __init__(self,from_): if from_.ip.get() =...

oauth on appengine: access issue

I am having some difficulty accessing resources through OAuth on AppEngine. My client application (on Linux using python-oauth) is able to retrieve a valid "access token" but when I try to access a protected resource (e.g. user = oauth.get_current_user()) , I get a oauth.OAuthRequestError exception thrown. headers: {'Content-Length': ...

python -> multiprocessing module

Hi all, Here's what I am trying to accomplish - I have about a million files which I need to parse & append the parsed content to a single file. Since a single process takes ages, this option is out. Not using threads in Python as it essentially comes to running a single process (due to GIL). Hence using multiprocessing module. i.e. s...

Is there a standard way to make sure a python script will be interpreted by python2 and not python3?

Is there a standard way to make sure a python script will be interpreted by python2 and not python3? On my distro, I can use #!/usr/bin/env python2 as the shebang, but it seems not all distros ship "python2". I could explicitly call a specific version (eg. 2.6) of python, but that would rule out people who don't have that version. It se...

sqlite remove non utf-8 characters

I have an sqlite db that has some crazy ascii characters in it and I would like to remove them, but I have no idea how to go about doing it. I googled some stuff and found some people saying to use REGEXP with mysql, but that threw an error saying REGEXP wasn't recognized. Here is the error I get: sqlalchemy.exc.OperationalError: (Ope...

Google App Engine: Webtest simulating logged in user and administrator.

Hello, from the webtest documentation I learn that: The best way to simulate authentication is if your application looks in environ['REMOTE_USER'] to see if someone is authenticated. Then you can simply set that value, like: app.get('/secret', extra_environ=dict(REMOTE_USER='bob')) I am trying to do the same thing but in...

json in python format

{"required_items":[ { "filename":"abcd", "no":"3" }, { "filename":"abc", "no":"2" } ]} I am not getting the code of the JSON format in Python - I want to insert t...

python configparser

How can I parse tags with no value on ini file with python configparser module? For example, I have the below ini and I need to parse rb. On some ini the rb have integer value and on some hasn't at all like below. How can I do that with configparser without getting a valueerror? I use getint function [section] person=name id=000 rb= ...

Pythonic Object Instantiation question

I have a Sleep class (object) and within that class a sleepInSeconds() method. Is there any difference between doing this: wait = Sleep() wait.sleepInSeconds(10) And doing this: wait = Sleep().sleepInSeconds(10) or Sleep().sleepInSeconds(10) Which is more correct (or 'Pythonic'), what kinds of problems could I run into with on...

Python - Dynamic Nested List

Hi. So I'm trying to generate a nested list in Python based on a width and a height. This is what I have so far: width = 4 height = 5 row = [None]*width map = [row]*height Now, this obviously isn't quite right. When printed it looks fine: [[None, None, None, None], [None, None, None, None], [None, None, None, None],...