In pondering optimization of code, I was wondering which was more expensive in python:
if x:
d = 1
else:
d = 2
or
d = 2
if x:
d = 1
Any thoughts? I like the reduced line count in the second but wondered if reassignment was more costly than the condition switching.
...
I'm currently returning 401 Unauthorized whenever I encounter a validation failure in my Django/Piston based REST API application.
Having had a look at the HTTP Status Code Registry
I'm not convinced that this is an appropriate code for a validation failure, what do y'all recommend?
400 Bad Request
401 Unauthorized
403 ...
How to use "in" statement to check is item in the list or not. If I use:
{% for picture in pictures %}
{% if picture in article.pictures %}
<input type="checkbox" checked="true" name="picture" value="{{ picture.key }}" />
{% else %}
<input type="checkbox" name="picture" value="{{ picture.key }}" />
{% endif %}
...
I've got a nasty feeling this is something to do with using 32-bit Python and/or 64-bit Mercurial, but I'm new to Macs and I don't fully understand the background.
I'm running Django and I've got Mercurial and Python 2.6 installed (in both cases, the default versions from Macports). I've set 32-bit Python as an environment variable:
ex...
Hello, I'm making a game with python and OpenGL. I was making it with pygame but I realised quick enough that it was too slow. I've made a class which I want to reasonably act like the Surface class for pygame, where Surface objects can be blitted to other Surface objects. I'm wishing to use framebuffers for rendering textures to texture...
Hi,
If I want to be able to test my application against a empty MySQL database each time my application's testsuite is run, how can I start up a server as a non-root user which refers to a empty (not saved anywhere, or in saved to /tmp) MySQL database?
My application is in Python, and I'm using unittest on Ubuntu 9.10.
...
I have the following code in django.template:
class Template(object):
def __init__(self, template_string, origin=None, name='<Unknown Template>'):
try:
template_string = smart_unicode(template_string)
except UnicodeDecodeError:
raise TemplateEncodingError("Templates can only be constructed fro...
I have a Decimal('3.9') as part of an object, and wish to encode this to a JSON string which should look like {'x': 3.9}. I don't care about precision on the client side, so a float is fine.
Is there a good way to serialize this? JSONDecoder doesn't accept Decimal objects, and converting to a float beforehand yields {'x': 3.899999999999...
the next is my code,it can print 'xxx', but run wrong at last:
def a(object):
print 'xxx'
@a
def b():
return 'bbb'
b()
In your answers, please try to use code examples rather than text, because my English is not very good. Thank you.
...
I have code
c = get_object_or_404(Category, slug=category_slug)
products = c.product_set.all()
page_title = c.name
Code doesn't work with error
'Category' object has no attribute 'product_set'
What wrong? I can't found any description about "product_set" on django.org
...
b=','.join([1,2,3,4,5])
print b
In your answers, please try to use code examples rather than text, because my English is not very good. Thank you.
...
I am trying to make a GUI in Tkinter and am wondering how to refresh a window, namely if I fill in a rectangle, I want the GUI to delete it a specified time later. How would I go about doing this? Documentation on Tkinter seems to be thin...
...
Hi all,
I'm using Turbogears 2.1 and repoze.who/what and am having trouble figuring out how to extend the basic authentication functionality. I am essentially attempting to require users to activate their account via an emailed link before they can login. If they try to login without activating their account, I want to display an approp...
I have A method for format the ouput as json.
My keyword_filter will be pass in this this format:
<QueryDict: {u'customer_type': [u'ABC'], u'tag': [u'2']}>
<QueryDict: {u'customer_type': [u'TDO'], u'tag': [u'3']}>
<QueryDict: {u'customer_type': [u'FRI'], u'tag': [u'2,3']}>
In fact this I got from request.GET (keyword_filter=request.GE...
Hi
I have a list of shapely polygons in Python. To find out which polygon touch is easy, using the .touches() method. However, I need something that returns true only when the polygons share more than one point (in other words shares a border). Let me illustrate:
IPython 0.10 -- An enhanced Interactive Python.
In [1]: from shapely.geo...
Hi,
I am developing an application suite where users will need to connect to a server and depending on their account type they will be given some services. The server will run Linux. Can you please suggest me some user management API which I can use to develop the server program? By user management I mean user authentication and other r...
I'm looking a way to automate schema migration for such databases like MongoDB or CouchDB.
Preferably, this instument should be written in python, but any other language is ok.
...
In general, where does program execution resume after an exception has been thrown and caught? Does it resume following the line of code where the exception was thrown, or does it resume following where it's caught? Also, is this behavior consistent across most programming languages?
...
I'm developing a Python project for dealing with computer simulations, and I'm also developing a GUI for it. (The core logic itself does not require a GUI.) The GUI toolkit I use for is wxPython, but I think my question is general enough not to depend on it.
The way that the GUI currently works is that it starts the core logic package (...
This is probably simple, but I can't figure this out:
I need regex expression which would extract following records (Each record may span multiple lines and delimited by one or more blank lines):
TextTextTextTextTextTextText
TextTextTextTextTextTextTextTextText
(one or more blank lines)
TextTextTextTextText
TextTextText
TextTextTex...