python

easiest way to pause a command-line python program?

Let's say I have a python program that is spitting out lines of text, such as: while 1: print "This is a line" What's the easiest way to allow one to press a key on the keyboard to pause the loop, then to resume if pressed again---but if nothing is pressed it should just continue on automatically? I'm hoping I don't have to go into...

python + mongo issue

Hello, I'm playing around with mongodb (documentation isn't very complete): tmpQuery = collection.find({"title_full": "kdsljfklsadfklj"}) print tmpQuery[0]['title_full'] That just echo's "no such item for Cursor instance", what is an if statement to determine if the variable tmpQuery has a valid result set and not empty? ...

String to datetime

Hello, I saved a datetime.datetime.now() as a string. Now I have a string value, i.e. 2010-10-08 14:26:01.220000 How can I convert this string to Oct 8th 2010 ? Thanks ...

Replace numeric character references in XML document using Python

Hi, I am struggling with the following issue: I have an XML string that contains the following tag and I want to convert this, using cElementTree, to a valid XML document: <tag>#55296;#57136;#55296;#57149;#55296;#57139;#55296;#57136;#55296;#57151;#55296; #57154;#55296;#57136;</tag> but each # sign is preceded by a & sign and hence t...

[Python] Open file into array, search for string and return value

Alright, I've been working on this for a while and cannot get it. I'm making a method that accepts a filename, and a pattern. E.g findPattern(fname, pat) Then the goal is to look for that pattern, say the string "apple" within the text file that is opened, and return it's location by [line, beginning character index] I'm new to python...

What icons are available to use when displaying a notification with libnotify?

I'm using the libnotify library to display a notification in Ubuntu. I would ideally like to display a battery of some sort (since my app is a battery meter). The types of icons I can use are: a URI specifying the icon file name (e.g. file://path/to/my-icon.png) a 'stock' icon name. One that would succeed in a call to gtk_icontheme_...

[Python] Extract content from a file with mime multipart

Hi, I have a file that contain a tiff image and a document xml in a multipart mime document. I would extract the image from this file. How I can get it? I have this code, but it requires an infinite time to extract it, if I have a big file (for example 30Mb), so this is unuseful. f=open("content_file.txt","rb") msg = email.message_fr...

json.dumps(pickle.dumps(u'å')) raises UnicodeDecodeError

Is this a bug? >>> import json >>> import cPickle >>> json.dumps(cPickle.dumps(u'å')) Traceback (most recent call last): File "<console>", line 1, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 230, in dumps return _default_encoder.encode(obj) File "/opt/loca...

getting keyboard back from tkinter

I'm working on a graphical app, and at the start of the run I'd like to ask the user a single configuration question. The graphical framework (Panda3D) has ugly default dialog boxes, so I'd like to use something like tkInter to provide a modal dialog. I tried this: import Tkinter import tkMessageBox root = Tkinter.Tk() # hide the root ...

anyone know how to NOT slice the Queryset with the Django Pagination object?

My django-sorting results are pathetic. Won't sort the data in the entire Queryset because djangos pagination model slices it. Any know a way around this? More details: http://code.google.com/p/django-pagination/issues/detail?id=37 ...

BDD in Google App Engine (Python)

I have seen some mention of some form of TDD for Python with Google App Engine, however I've not really seen a discussion of a BDD approach. Is someone familiar with how to string this together properly with GAE? I'm hopeful that things may be in a better position for this now than they were from notes and articles I saw from about a y...

How to make a light query for a many to many relationship in Google Apps Engine?

How to make a light query for a many to many relationship? Users has many Lists the ListUser is the model that links them Currently I'm doing like this but there are a lot of get queries to get all this data. lists = [] for list in user.lists: lists.append(list.list) Now I got this: list_users = user.lists.fetch(1000) # proble...

How importing works. Why imported modules not inheriting other imported modules

Hello, I just "thought" I understood how importing and modules work but obviously I need more schooling. Here is an example program (just a test case of somerthing I'm doing that is much bigger in scope and scale) and a module: quick.py import gtk from quick_window import * w.show_all() gtk.main() quick_window.py w = gtk.Window()...

python global object cache

Hello Little question concerning app architecture: I have a python script, running as a daemon. Inside i have many objects, all inheriting from one class (let's name it 'entity') I have also one main object, let it be 'topsys' Entities are identified by pair (id, type (= class, roughly)), and they are connected in many wicked ways. ...

what is a quick way to delete all elements from a list that do not satisfy a constraint?

I have a list of strings. I have a function that given a string returns 0 or 1. How can I delete all strings in the list for which the function returns 0? ...

Python's print function that flushes the buffer when it's called?

Possible Duplicates: How to flush output of Python print? unbuffered stdout in python (as in python -u) from within the program I have the following code to flushing out the output buffer. print 'return 1' sys.stdout.flush() Can I setup the print function so that it automatically flushes the buffer when it's called? ...

python: quickest way to split a file into two files randomly

python: what is the quickest way to split a file into two files, each file having half of the number of lines in the original file, such that the lines in each of the two files are random? for example: if the file is 1 2 3 4 5 6 7 8 9 10 it could be split into: 3 2 10 9 1 4 6 8 5 7 ...

Why the connect failed for ipv6 at python?

Why the connect failed for ipv6 ?? # python >>> import socket >>> s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) >>> sa = ('2000::1',2000,0,0) >>> s.connect(sa) >>> sa = ('fe80::21b:78ff:fe30:7c6', 2000, 0, 0) >>> s.connect(sa) Traceback (most recent call last): File "<stdin>", l...

pymongo (python+mongodb) drop collection/gridfs?

Hello, Anyone know the commands to drop a collection of documents and also drop a gridfs database? ...

Number of regex matches

I'm using the finditer-function in the re module to match some things and everything is working. Now I need to find out how many matches I've got, is it possible without looping through the iterator twice? (one to find out the count and then the real iteration) Edit: As requested, some code: imageMatches = re.finditer("<img src\=\"(?...