python

How to include annotation in JSON string?

I've got a view that returns a list of shipments encoded as JSON... def get_new_shipments(request): # ... shipments = Shipment.objects.filter(filter).exclude(**exclude).order_by(order) \ .annotate(num_bids=Count('bids'), min_bid=Min('bids__amount'), max_bid=Max('bids__amount')) return json_response(shipments) def js...

python twisted : retrieve a deferred's execution time

I would like to know how long a Deferred takes to execute, from the time the first callback is fired to the final result. Any ideas on how to do that, possibly in a non-invasive manner ( meaning no modification on any of the callback functions in order to track the execution time ) ? ...

Bloomberg Server API and Ruby/Python

Im looking to write a new application in ruby/python which uses a feed from bloomberg and am stuck trying to find any documentation for using (or even setting up) Bloomberg Server API with either of these languages. Does anyone have any good links to tutorials for this or maybe some boilerplate code to get set up? Or is it best to just ...

MySQL syntax error using python to add column to a table

The code i have is: for key in keys: cursor.execute(""" ALTER TABLE segment_table ADD %s VARCHAR(40) """, key) I get a error telling me my syntax is wrong. When I replace the %s with a actual string the syntax error goes away. for key in keys: cursor.execute(""" ALT...

How can "k in d" be False, but "k in d.keys()" be True?

I have some python code that's throwing a KeyError exception. So far I haven't been able to reproduce outside of the operating environment, so I can't post a reduced test case here. The code that's raising the exception is iterating through a loop like this: for k in d.keys(): if condition: del d[k] The del[k] line throw...

A question about Django and Google Apps

Just wondering about this, is it possible to use Django with the Google Apps API's? I have a small organization that uses Google Apps Education Edition. I was thinking about making a small intranet using Django, and I would love if the first page they saw when they logged in had a few widgets with their email, calendar, maybe docs. I loo...

Add new keys to a dictionary while incrementing existing values

I am processing a CSV file and counting the unique values of column 4. So far I have coded this three ways. One uses "if key in dictionary", the second traps the KeyError and the third uses "DefaultDictionary". For example (where x[3] is the value from the file and "a" is a dictionary): First way: if x[3] in a: a[x[3]] += 1 else: ...

Suggestions for next language to learn

I figured its time to start learning another language, but I can't decide between Ruby and Python (I'd like to start using Rails or Django). Which one do you think would be both most helpful and fun to learn, and if possible, could you give me some sites/links that would help me get started? If you think there's another language that wo...

Search app in Django

I am building search app using django & sphinx. I got the setup working but when I search I get irrelevant results. Here is what I do - # this is in my trial_data Model search = SphinxSearch( index = 'trial_data trial_datastemmed', weights = {'name': 100,}, mode = 'SPH_MATCH_A...

wxPython CheckListBox with HTML

I'm using wxPython to create a GUI app. Right now I'm using a wx.CheckListBox to display options with check boxes, but I'd like the text in the CheckListBox to be formatted using HTML. What's the best way to go about this? ...

how to reference compiled python code in IronPython?

we need to reference .py code from C#. This was solved using IronPython 2.6 The issue arises from the fact that .py code uses 'import customlib' which is a library compiled into customlib.pyc IronPython gives error: IronPython.Runtime.Exceptions.ImportException: No module named customlib attempted solution: in python code add referen...

How can I re-use a sqlalchemy ORM model on multiple databases and schemas?

I have a SQLAlchemy ORM model that currently looks a bit like this: Base = declarative_base() class Database(Base): __tablename__ = "databases" __table_args__ = ( saschema.PrimaryKeyConstraint('db', 'role'), { 'schema' : 'defines', }, ) db = Column(String, nullable=False) role = Column(String, nullabl...

Django Tutorial - TemplateDoesNotExist at /polls/

I have tried to get this to work a million times. I have lef it alone for a week and come back. I have Googled and read every post pertaining to this. I have let insecure morons belittle in messages groups without ever finding the answer. I just want this to work. When I am following along in part three of the the Django tutorial, I get ...

Efficient way to count unique elements in array in numpy/scipy in Python

I have a scipy array, e.g. a = array([[0, 0, 1], [1, 1, 1], [1, 1, 1], [1, 0, 1]]) I want to count the number of occurrences of each unique element in the array. For example, for the above array a, I want to get out that there is 1 occurrence of [0, 0, 1], 2 occurrences of [1, 1, 1] and 1 occurrence of [1, 0, 1]. One way I thought of...

Is there a way to really pickle compiled regular expressions in python?

I have a python console application that contains 300+ regular expressions. The set of regular expressions is fixed for each release. When users run the app, the entire set of regular expressions will be applied anywhere from once (a very short job) to thousands of times (a long job). I would like to speed up the shorter jobs by compi...

"Caching" attributes of classes in Python

I'm writing a class in python and I have an attribute that will take a relatively long time to compute, so I only want to do it once. Also, it will not be needed by every instance of the class, so I don't want to do it by default in __init__. I'm new to Python, but not to programming. I can come up with a way to do this pretty easil...

Google app engine regular expression

I'm working on a Google appengine project and I've encountered a quandary. The following should (if the regex's are normal) redirect everything which does not contain the word "test" to the MainPage class, and the rest to the TestPage class. application = webapp.WSGIApplication( [ ...

Add a python script at runtime

Hello, I am beginning python and working on a project, and one thing I would like to be able to do is download and run a script dynamically at runtime. The general idea is to be able to connect to a server, download a python script on demand, and run that script that was just downloaded without having to restart the program or hard cod...

How to store a return value in a variable and use print to display the value returned.

Modify the main function to call getAction right after the win.getMouse() call inside of the while loop. Store the return value in a variable named action, and use print to display the value returned. This is my code so far. What I'm having trouble with is how to return the value in a variable named action, and then use print to displa...

Python/Tkinter window events and properties

I've been searching for information on the following Tkinter window features without success. Platform is Windows, Python 2.7. At the end of this post is code that can be used to explore Tkinter window events. How can one detect window minimize/maximize events? The event object returned by binding to a window's event does contain any ...