python

regex that matches a string that contains some text

I need a regex that matches re.compile('userpage') href="www.example.com?u=userpage&as=233&p=1" href="www.example.com?u=userpage&as=233&p=2" I want to get all urls that have u=userpage and p=1 How can I modify the regex above to find both u=userpage and p=1? ...

in a loop, only add to a dictionary or list or tuple if doesn't contains the key

I am looping in python and want to add a key to a dictionary only if it isn't already in the collection. How can I do this? mydic = {} for x in range(100): ?? ...

Python ctypes not loading dynamic library on Mac OS X

I have a C++ library repeater.so that I can load from Python in Linux the following way: import numpy as np repeater = np.ctypeslib.load_library('librepeater.so', '.') However, when I compile the same library on Mac OS X (Snow Leopard, 32 bit) and get repeater.dylib, and then run the following in Py...

if loop: x not in VS not x in

This is a strange question but here goes: I've noticed that both of these work the same: if x not in list and if not x in list. Is there some sort of difference between the two in certain cases? Is there a reason for having both, or is it just because it's more natural for some people to write one or the other. Which one am I more ...

Python using doctest on the mainline

Hello i was wondering if it is possible and if so how? to do doctests or something similar from the mainline, instead of testing a function as is described in the doctest docs i.e. """ >>> Hello World """ if __name__ == "__main__": print "Hello" import doctest doctest.testmod() This is part of being able to test students...

blogger (python) API - How to query none draft blog entry

I would like to retrieve through the python blogger API the list of published (not draft) blog entry from my blog. What parameter do I need to add to specify draft=false. query = gdata.blogger.client.Query() query.max_results = 3 feed = bc.GetPosts(blog_id=blog_id, query=query) ...

pure python socket module

The socket module in python wraps the _socket module which is the C implementation stuff. As well, socket.socket will take a _sock parameter that must implement the _socket interface. In some regards _sock must be an actual instance of the underlying socket type from _socket since the C code does type checking (unlike pure python). Gi...

Using xterm to open a new console: How to while the current console is printing, to print on the new console also.

I'm using python right now. I have a thread that represents my entire program. I want to open another console window using os.system(xterm&) as a thread which works. The only thing is, is it possible to print to the new window while the other thread is printing to the older window? import sys import os def my_fork(): child_pid ...

Why is this variable being changed?

tokens_raw = {"foo": "bar"} tokens_raw_old = { } while not tokens_raw == tokens_raw_old: tokens_raw_old = tokens_raw # while loop that modifies tokens_raw goes here; # tokens_raw_old is never referenced print tokens_raw_old == tokens_raw This outputs True after the first time for some reason. tokens_raw_old has the same...

__decorated__ for python decorators

As of 2.4 (2.6 for classes), python allows you to decorate a function with another function: def d(func): return func @d def test(first): pass It's a convenient syntactic sugar. You can do all sorts of neat stuff with decorators without making a mess. However, if you want to find out the original function that got decorated you hav...

looking python multiplayer game server project

looking for a python multiplayer game server project. just trying to learn more and i love to game ...

Komodo Edit auto-complete won't find a Python module.

I am using Komodo edit on a Python file on Windows. When I type import s it successfully lists all the importable files starting with s, including one of my modules in one of my directories. When I type import t it lists all the importable files starting with t, EXCLUDING one of my modules in the same directory. Even though Komodo can...

How should I read the input data?

For example, I have the following input data: (( 12 3 ) 42 ) I want to treat each integer value of the input data. This is an example of the general input data presentation. Just for additional information: Such presentation is corresponding to the binary tree with marked leaves: /\ /\ 42 12 3 ...

Font width returns incorrect values

The following code should return the width in pixels of the character 'a' in the default font; it doesn't: import pygtk gtk.require20() import gtk t = gtk.TextView() print t.get_style().get_font().width("w") # Always returns -6 ...

SQLAlchemy - ObjectDeletedError: Instance '<Class at...>' has been deleted. Help.

I'm having some issues with deleting rows from a database and then adding new ones. Here's the code: for positionid in form_result['responsibilities']: inputdata = form_result['responsibilities'][positionid] self.__deleterow(dbmyaccount.Responsibilities, session['authed']['userid']) for resp in (i.strip() for i in inputdata...

Tkinter Label Widget with Image update?

My question is similar to here, I would like to be able to swap out an image on a Tkinter label, but I'm not sure how to do it, except for replacing the widget itself. Currently, I can display and image like so: import Tkinter as tk import ImageTk root = tk.Tk() img = ImageTk.PhotoImage(Image.open(path)) panel = tk.Label(root, image =...

How to control/call another python script within one python script? (Communicate between scripts)

I'm working on one GUI program, and was gonna add a long running task into one event, but I found this would make the whole program freeze a lot, so considering other people's advice I would make the GUI only responsible for starting, stopping and monitoring and make the long running task run as a separate script. The only way I know to ...

Google App Engine - Naked Domain Path Redirect in Python

I'm working on a site, colorurl.com, and I need users to be able to type in colorurl.com/00ff00 (or some variation of that), and see the correct page. However, with the naked domain issue, users who type in colorurl.com/somepath will instead be redirected to www.colorurl.com/. Is there a way to detect this in python, and then redirect t...

How to extract a given frame from a .gif animation in Python

I'm trying to figure out how to extract a given frame from an animated-gif, possibly in PIL, in Python. I'm not able to easily dig this up, and I'm guessing it would take some knowledge of the gif format, something that is not readily understandable to me. Is there any straightforward way to accomplish this? Do I need to do some custom p...

Shed some light on working with pipes and subprocesses in Python?

Hi Everyone I'm wrestling with the concepts behind subprocesses and pipes, and working with them in a Python context. If anybody could shed some light on these questions it would really help me out. Say I have a pipeline set up as follows createText.py | processText.py | cat processText.py is receiving data through stdin, but how i...