python

Python/Django: Can a process keep running after the user sees the "Thanks. You're done!" page.

I'm trying to understand threading better. If I create a program that allows people to upload a photo and then I create a new process to resize the image in a hundred ways (taking 5 seconds or longer), and the main program returns a response HTML page to the user saying "Thanks. You're done!", can the other process still be working at th...

Iterating over a large Django queryset while the data is changing elsewhere

Iterating over a queryset, like so: class Book(models.Model): # <snip some other stuff> activity = models.PositiveIntegerField(default=0) views = models.PositiveIntegerField(default=0) def calculate_statistics(): self.activity = book.views * 4 book.save() def cron_job_calculate_all_book_statistics(): ...

MemoryLoadError when trying to run py2exe application

I am attempting to bundle up a Python application using py2exe 0.6.9 and Python 2.6.4 on Windows. While the executable runs just fine on the system I used to build it, it fails when I attempt to run it on another system: C:\Documents and Settings\Administrator\Desktop\dist>.\backend.exe install Traceback (most recent call last): File...

Adding arrays with different number of dimensions

Let's say I have a 2D Numpy array: >>> a = np.random.random((4,6)) and I want to add a 1D array to each row: >>> c = np.random.random((6,)) >>> a + c This works. Now if I try adding a 1D array to each column, I get an error: >>> b = np.random.random((4,)) >>> a + b Traceback (most recent call last): File "<stdin>", line 1, in <m...

lxml and loops to create xml rss in python

I have been using lxml to create the xml of rss feed. But I am having trouble with the tags and cant really figure out how to to add a dynamic number of elements. Given that lxml seems to just have functions as parameters of functions, I cant seem to figure out how to loop for a dynamic number of items without remaking the entire pa...

Load and Reuse Django Template Filters

Is it possible to load a django template tag/filter to use as a function in one of my template tags? I'm trying to load up some of the django.contrib.humanize filters so I can apply them to the results of some of my custom template tags. I can't seem to import them at all, and I don't want to have to rewrite any of that code. ...

QObject (QPlainTextEdit) & Multithreading issues

Im currently trying to learn Networking with Python asyncore and pyqt4. I coded a small server, which basically listens on some port, and resends all messages it recieves to the sender. Since both qts QApplication.exec_() and asyncore.loop() are functions which never return i could not start them both in one thread, so i stared asyncor...

Returning the product of a list

Is there a more concise, efficient or simply pythonic way to do the following? def product(list): p = 1 for i in list: p *= i return p EDIT: I actually find that this is marginally faster than using operator.mul: from operator import mul # from functools import reduce # python3 compatibility def with_lambda(list...

why no color output with Python?

I have a batch file which calls .Net solution to build the project. On the dos console window, the warning and error will be in different color, green and red, looks nice. When the batch file called from Python, no color at all, all in a single color. Is it possible to get the same colorful result with python? my call to the batch file i...

Python: Replacing item in a list of lists

Heres my code: data = [ [5,3,0,0,7,0,0,0,0], [6,0,0,1,9,5,0,0,0], [0,9,8,0,0,0,0,6,0], [8,0,0,0,6,0,0,0,3], [4,0,0,8,0,3,0,0,1], [7,0,0,0,2,0,0,0,6], [0,6,0,0,0,0,2,8,0], [0,0,0,4,1,9,0,0,5], [0,0,0,0,8,0,0,7,9] ] element = 4 x = 0 y = 0 data[x][y] = element I want to replace the element at coordinates 0,0 but when i print data i...

How does Python manage int and long?

Does anybody know how Python manage internally int and long types? Does it choose the right type dynamically? What is the limit for an int? I am using Python 2.6, Is is different with previous versions? How should I understand the code below? >>> print type(65535) <type 'int'> >>> print type(65536) <type 'int'> >>> print type(6553...

Django queryset that returns all unassigned fks

I have 2 models with a 1-1 relation (essentially a resource pool). For the example code, I will simply use nuts and bolts. There will be many more nuts (available resources) than bolts (which will each require 1 nut). However, if a nut can only be assigned to one bolt. The constraint is easy enough to set up with the unique=True name...

In Jinja2, how can I use macros in combination with block tags?

I'm a front end developer, and I've been trying to get a hang on using Jinja2 effectively. I want to tweak a current site so it has multiple base templates using inheritance, it fully uses block tags to substitute content and override it, and uses macros to support passing of arguments. My base template contains this code (edited for si...

Anyway of making Django CMS pages to be only accessible (site side) to only a select few?

I've installed Django CMS (http://www.django-cms.org/) and it's almost perfect. I've been chatting on the IRC #django-cms group and it's been confirmed to me that I can't have access restricted to the pages I make in Django CMS to only a select few on the site side. I know of the CMS_PERMISSIONS setting, but this seems to only restrict ...

Facebook Page details and the RESTful API?

Hi I have a list of Facebook Page urls eg... http://www.facebook.com/daftpunk http://www.facebook.com/DavidGuetta ... What's the best way to: Check if these urls are actually for Facebook Pages and not Profiles Collect details such as # of fans from these Pages Help would be very much appreciated. ...

os.walk() python: xml representation of a directory structure, recursion

Hello, So I am trying to use os.walk() to generate an XML representation of a directory structure. I seem to be getting a ton of duplicates. It properly places directories within each other and files in the right place for the first portion of the xml file; however, after it does it correctly it then continues traversing incorrectly. I a...

unicode in powershell with python? alternative shells in windows?

I want a shell that supports unicode in windows, powershell as it ships doesn't seem to. Powershell V2 (win7 x64) : PS C:\> powershell Windows PowerShell Copyright (C) 2009 Microsoft Corporation. All rights reserved. PS C:\> python Python 2.6.2 (r262:71605, Apr 14 2009, 22:46:50) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copy...

SOAP Message size it greater than allowed limit [SECURITY.MSGSIZE v 1.0]? How?

I'm trying to help a colleague run SOATest (a web services client that makes testing SOAP services easy) on a WCF web service operation, and for "big" responses, we are seeing this error: SOAP Message size it greater than allowed limit [SECURITY.MSGSIZE v 1.0] This is perplexing, as the tool is actually able to get a response from the...

how many characters are there in line in a console?

How can I find out how may character are there in a line before the end line in an interactive shell using python? (Usually 80) ...

OOP Design - In Python, is this a quality OO Design or an epic fail?

In a system that accepts orders which have payments which have gateway transactions should the objects be like this: class Order(object): ... Inside init ... self.total_in_dollars = <Dollar Amount> self.is_paid = <Boolean Value> class Payment(object): ... Inside init ... self.order = order_instan...