python

py2app error: "'module' object has no attribute 'symlink'"

I'm trying to pack my Python app with py2app. I'm running the setup.py I created, and I get this error: Traceback (most recent call last): File "C:\Python26\lib\site-packages\py2app-0.3.6-py2.6.egg\py2app\build_app.py ", line 548, in _run self.run_normal() File "C:\Python26\lib\site-packages\py2app-0.3.6-py2.6.egg\py2app\build_a...

How do I see stdout when running Django tests?

When I run tests with ./manage.py test, whatever I send to the standard output through print doesn't show. When tests fail, I see an "stdout" block per failed test, so I guess Django traps it (but doesn't show it when tests pass). ...

Creating multiple Python modules in different directories that share a portion of the package structure.

I'm working on a Django project that contains a single application. The application will be released under the GPL so I want to develop it separately from the project - a personal site using the app. I'm attempting to use a package structure based on my domain name for both the project and the app, and that's where I'm running into pro...

How do I use PIL with Tkinter?

I'm missing something at a very basic level when it comes to loading an image using PIL and displaying it in a window created by Tkinter. The simplest form of what I'm trying to do is: import Tkinter as TK from PIL import Image, ImageTk im = Image.open("C:\\tinycat.jpg") tkIm = ImageTk.PhotoImage(im) tkIm.pack() TK.mainloop() When I ...

Comma separated lists in django templates

If fruits is the list ['apples', 'oranges', 'pears'], is there a quick way using django template tags to produce "apples, oranges, and pears"? I know it's not difficult to do this using a loop and {% if counter.last %} statements, but because I'm going to use this repeatedly I think I'm going to have to learn how to write custom tags f...

Is 'for x in array' always result in sorted x? [Python/NumPy]

For arrays and lists in Python and Numpy are the following lines equivalent: itemlist = [] for j in range(len(myarray)): item = myarray[j] itemlist.append(item) and: itemlist = [] for item in myarray: itemlist.append(item) I'm interested in the order of itemlist. In a few examples that I have tried they are identical, b...

Grouping dates in Django

My question is almost exactly the same as this post, except that I'm using Python and Django instead of PHP. The task is to take: id date 1 2009-01-01 10:15:23 2 2009-01-01 13:21:29 3 2009-01-02 01:03:13 4 2009-01-03 12:20:19 5 2009-01-03 13:01:06 And output: 2009-01-01 1 2 2009-01-02 3 2009-01-03 4 5 I ...

Help with MySQL LOAD DATA INFILE

I want to load a CSV file that looks like this: Acct. No.,1-15 Days,16-30 Days,31-60 Days,61-90 Days,91-120 Days,Beyond 120 Days 2314134101,898.89,8372.16,5584.23,7744.41,9846.54,2896.25 2414134128,5457.61,7488.26,9594.02,6234.78,273.7,2356.13 2513918869,2059.59,7578.59,9395.51,7159.15,5827.48,3041.62 1687950783,4846.85,8364.22,9892.55,...

math.sin incorrect result

>>> import math >>> math.sin(68) -0.897927680689 sin(68)=0.927(3dp) Any ideas about why I am getting this result? Thanks. ...

Cron job python Google App Engine

I want to Add a scheduled task to fetch a URL via cron job using google app engine. I am continuously getting the failure. I am just fetching www.google.com. Why The url fetch is failed??? Am I missing something??? ...

Python differences: a+= [b] vs a = a + [b] ...

Edited: Why is there a different output in these two cases? class foo: bar = [] def __init__(self,x): self.bar += [x] class foo2: bar = [] def __init__(self,x): self.bar = self.bar + [x] f = foo(11) g = foo(12) print f.bar print g.bar f = foo2(11) g = foo2(12) print f.bar print g.bar ...

How do I set sys.excepthook to invoke pdb globally in python?

From Python docs: sys.excepthook(type, value, traceback)¶ This function prints out a given traceback and exception to sys.stderr. When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this ...

Choose the filename of an uploaded file with Django

I'm uploading images (represented by a FileField) and I need to rename those files when they are uploaded. I want them to be formated like that: "%d-%d-%s.%s" % (width, height, md5hash, original_extension) I've read the documentation but I don't know if I need to write my own FileSystemStorage class or my own FileField class or ... ? ...

How to implement a state-space tree?

I'm trying to solve a knapsack like problem from MIT OCW. It's problem set 5. I need use branch and bound algorithm to find the optimal states. So I need implement a state-space tree. I understand the idea of this algorithm, but I find it's not so easy to implement. If I find a node that with it, the budget is not enough, I should stop...

Python: binary/hex string conversion?

I have a string that has both binary and string characters and I would like to convert it to binary first, then to hex. The string is as below: <81>^Q<81>"^Q^@^[)^G ^Q^A^S^A^V^@<83>^Cd<80><99>}^@N^@^@^A^@^@^@^@^@^@^@j How do I go about converting this string in Python so that the output in hex format is similar to this below? 242080...

What does % do to strings in Python?

I have failed to find documentation for the operator % as it is used on strings in Python. Does someone know where that documentation is? ...

Python Multiprocessing exit error

Hey everyone I am seeing this when I press Ctrl-C to exit my app Error in atexit._run_exitfuncs: Traceback (most recent call last): File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs func(*targs, **kargs) File "/usr/lib/python2.6/multiprocessing/util.py", line 269, in _exit_function p.join() File "/usr/lib/pyt...

Is it necessary or useful to inherit from python's object in Python 3.x?

In older python version when you create a class in python, it can inherit from object which is as far I understand a special built-in python element that allow your object to be a new-style object. What about newer version (> 3.0 and 2.6)? I googled about the class object but I get so much result (for an obvious reasons). Any hint? Tha...

surprising time shift for python call

I'm using the following code in Python 2.5.1 to generate a UTC timestamp from a string representation of a date: time.mktime(time.strptime("2009-06-16", "%Y-%m-%d")) The general result is: 1245103200 (16.6.2009 0:00 UTC or 15.6.09 22:00:00, if you're in my time zone). But now, I found that on some computers running Windows XP, this ...

Does 'a+' mode allow random access to files, on all systems?

According to the documentation of open function 'a' means appending, which on some Unix systems means that all writes append to the end of the file regardless of the current seek position. Will 'a+' allow random writes to any position in the file on all systems? ...