python

how to force python httplib library to use only A requests

The problem is that urllib using httplib is querying fro AAAA records what i would like to avoid. Is there a nice way to do that? >>> import socket >>> socket.gethostbyname('www.python.org') '82.94.164.162' 21:52:37.302028 IP 192.168.0.9.44992 > 192.168.0.1.53: 27463+ A? www.python.org. (32) 21:52:37.312031 IP 192.168.0.1.53 > 192.168...

Dumping a multiprocessing.Queue into a list

I wish to dump a multiprcoessing.Queue into a list. For that task I've written the following function: import Queue def dump_queue(queue): """ Empties all pending items in a queue and returns them in a list. """ result = [] # START DEBUG CODE initial_size = queue.qsize() print("Queue has %s items initially....

How make a better markdown for developer blog

I'm rebuilding my blog at http://www.elmalabarista.com/blog/. I have use in my previous version markdown and now I remember why I have almost zero code samples. Doing code samples in markdown is very fragile. I try to put some python there I can't make markdown mark it as code!. The main culprit? The syntax is markdown for code is out s...

Trace/BPT trap with Python threading module

The following code dies with Trace/BPT trap: from tvdb_api import Tvdb from threading import Thread class GrabStuff(Thread): def run(self): t = Tvdb() def main(): threads = [GrabStuff() for x in range(1)] [x.start() for x in threads] [x.join() for x in threads] if __name__ == '__main__': main() The error...

Python 'object' type and inheritance

In Python I can define a class 'foo' in the following ways: class foo: pass or class foo(object): pass What is the difference? I have tried to use the function issubclass(foo, object) to see if it returns True for both class definitions. It does not. IDLE 2.6.3 >>> class foo: pass >>> issubclass(foo, obje...

Bubble Breaker Game Solver better than greedy?

For a mental exercise I decided to try and solve the bubble breaker game found on many cell phones as well as an example here:Bubble Break Game The random (N,M,C) board consists N rows x M columns with C colors The goal is to get the highest score by picking the sequence of bubble groups that ultimately leads to the highest score A bu...

Difference between GET and FILTER in Django model layer.

What is the difference, please explain them in laymen's terms with examples. Thanks! ...

Why is subprocess.Popen not waiting until the child process terminates?

I'm having a problem with Python's subprocess.Popen method. Here's a test script which demonstrates the problem. It's being run on a Linux box. #!/usr/bin/env python import subprocess import time def run(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) return p ### START MAIN # copy some rows from a source tab...

python regular expression

Hi all, a question about python regular expression. I would like to match a div block like <div class="leftTail"><ul class="hotnews">any news stuff</ul></div> I was thinking a pattern like p = re.compile(r'<div\s+class=\"leftTail\">[^(div)]+</div>') but it seems not working properly another pattern p = re.compile(r'<div\s+class...

Django SELECT statement, Order by

Suppose I have 2 models. The 2nd model has a one-to-one relationship with the first model. I'd like to select information from the first model, but ORDER BY the 2nd model. How can I do that? class Content(models.Model): link = models.TextField(blank=True) title = models.TextField(blank=True) is_channel = models.Boole...

Why Does List Argument in Python Behave Like ByRef?

This may be for most languages in general, but I'm not sure. I'm a beginner at Python and have always worked on copies of lists in C# and VB. But in Python whenever I pass a list as an argument and enumerate through using a "for i in range," and then change the value of the list argument, the input values actually changes the original ...

A .net wrapper for Google App Engine?

Does anyone know of a .net wrapper around either python or java Google App Engine services? Any help appreciated // :) ...

Upgrade Python to 2.6 on Mac

I'd like to upgrade the default python installation (2.5.1) supplied with OS X Leopard to the latest version. Please let me know how I can achieve this. Thanks ...

Get window title with python?

I'm trying to write a python program that checks every X seconds if the 'window title' for 'last.fm' (http://www.last.fm/download) changed, if it did (or it's the first time I run the program) it should use use the string captured from the window title to search for the song's lyrics and display them to the user. I'm currently using KD...

In python, how to check if there are any duplicates in list

example 1: ['one', 'two', 'one'] should return True. example 2: ['one', 'two', 'three'] should return False. ...

python/django for loop creating database populated with 0000-9999

Basically, i've created a view to populate my database with Serial models from 0000 to 9999. below is the code i'm using for the view. def insert_serials(request): for i in range(0,10000): serial = Serial(i,False) serial.save() else: print 'The for loop is over' what is the right way to do this, and i'm getting an ...

binding local variables in python

Hi, I wonder if there is a good way to bind local variables in python. Most of my work involves cobbling together short data or text processing scripts with a series of expressions (when python permits), so defining object classes (to use as namespaces) and instantiating them seems a bit much. So what I had in mind was something like i...

How to write this "model" in Django?

Hello, I am currently using Django Users model. Very simple. However, I'd like to add one feature: Adding friends! I would like to create 2 columns in my table: UID (the ID of the User) friend_id (the ID of his friend! ...of course, this ID is also in Django's User model. The UID-friend_id combination must be unique! For example, if ...

Accuracy of stat mtime in Windows

Have an example piece of (Python) code to check if a directory has changed: import os def watch(path, fdict): """Checks a directory and children for changes""" changed = [] for root, dirs, files in os.walk(path): for f in files: abspath = os.path.abspath(os.path.join(root, f)) new_mtime = os.sta...

Does python's print function handle unicode differently now than when Dive Into Python was written?

I'm trying to work my way through some frustrating encoding issues by going back to basics. In Dive Into Python example 9.14 (here) we have this: >>> s = u'La Pe\xf1a' >>> print s Traceback (innermost last): File "<interactive input>", line 1, in ? UnicodeError: ASCII encoding error: ordinal not in range(128) >>> print s.encode('latin-...