python

Testing time sensitive applications in Python

I've written an auction system in Django. I want to write unit tests but the application is time sensitive (e.g. the amount advertisers are charged is a function of how long their ad has been active on a website). What's a good approach for testing this type of application? Here's one possible solution: a DateFactory class which provi...

Python timedelta in years

I need to check if some number of years have been since some date. Currently I've got timedelta from datetime module and I don't know how to convert it to years. ...

Amazon S3 permissions

Trying to understand S3...How do you limit access to a file you upload to S3? For example, from a web application, each user has files they can upload, but how do you limit access so only that user has access to that file? It seems like the query string authentication requires an expiration date and that won't work for me, is there ano...

Inverting a string in Python

I was looking for a way to print a string backwards, and after a quick search on google, I found this method: Suppose 'a' is a string variable. This will return the 'a' string backwards: a[::-1] Can anyone explain how that works? ...

Python speed testing - Time Difference - milliseconds

What is the proper way to compare 2 times in Python in order to speed test a section of code? I tried reading the API docs. I'm not sure I understand the timedelta thing. So far I have this code: from datetime import datetime tstart = datetime.now() print t1 # code to speed test tend = datetime.now() print t2 # what am I missing? # ...

Python non-greedy regexes

How do I make a python regex like "(.*)" such that, given "a (b) c (d) e" python matches "b" instead of "b) c (d"? I know that I can use "[^)]" instead of ".", but I'm looking for a more general solution that keeps my regex a little cleaner. Is there any way to tell python "hey, match this as soon as possible"? ...

Python Collections.DefaultDict Sort + Output Top X Custom Class Object

Problem: I need to output the TOP X Contributors determined by the amount of messages posted. Data: I have a collection of the messages posted. This is not a Database/SQL question by the sample query below just give an overview of the code. tweetsSQL = db.GqlQuery("SELECT * FROM TweetModel ORDER BY date_created DESC") My Model: clas...

mod_wsgi force reload modules

Is there a way to have mod_wsgi reload all modules (maybe in a particular directory) on each load? While working on the code, it's very annoying to restart apache every time something is changed. The only option I've found so far is to put modname = reload(modname) below every import.. but that's also really annoying since it means I'm ...

Why won't you switch to Python 3.x?

I ask this for deployable reasons. As in, if I write a solution in python, I feel bound to write to 2.x due to the lack of adoption of python 3. This is a major daily concern of mine, and I want to figure out what's going on. For many of the python-based questions here, people are giving solutions that simply do not work in python 3.x. ...

Some internals of Django auth middleware

In the django.contrib.auth middleware I see the code: class AuthenticationMiddleware(object): def process_request(self, request): assert hasattr(request, 'session'), "requires session middleware" request.__class__.user = LazyUser() return None Please avdise me why such a form request._ class _.user = L...

Programattically taking screenshots in windows without the application noticing

Duplicate of: http://stackoverflow.com/questions/774925/detect-when-users-take-screenshots-of-my-program There are various ways to take screenshots of a running application in Windows. However, I hear that an application can be tailored such that it can notice when a screenshot is being taken of it, through some windows event handlers p...

Attributes not available when overwriting __init__?

I'm trying to overwrite a __init__ method, but when I call the super method the attributes created in that method are not available. I can see that it's not an inheritance problem since class B still has the attributes available. I think the code sample will explain it better :-) Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4...

nice html reports for pyunit

Do you know a tool for creating nice html reports for pyunit? ...

Blocks of code in python

Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python? What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs? I have so far understood the lambda thing; it is only one-line, but maybe it comes...

Current working directory no longer inherited from calling process from python 2.5 onwards?

I updated my python version on windows 2003 server from 2.4 to 2.5. In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this: import sub1 as long as the calling script main.py that lives in c:\application was started like this: c:\application\subdir>python ..\main.py But in 2.5 it no longer works...

Estimating zip size/creation time.

I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. Resources to be zipped are often > 1GB and not necessarily compression-friendly. How do I efficiently estimate its creation time / size? ...

Time taken by an import in Python

I want to know how much time an import takes for both built-in as well as user defined modules. ...

Riddle: The Square Puzzle

Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect to some simple "traverse rules" and reach n...

delete *.pyc continuation

As a follow-up to this question, I have a new question: What is happening internally in os.remove(module_name) and del sys.modules["module_name"]? I need an urgent help for this.Please help me out. ...

What is the most Pythonic way to provide a fall-back value in an assignment?

In Perl, it's often nice to be able to assign an object, but specify some fall-back value if the variable being assigned from is 'undef'. For instance: my $x = undef; my $y = 2; my $a = $x || $y; After this, $a == 2 Is there a concise way to achieve this in Python if the value x is None, or would a full-on ... if x is not None ...