python

Why can't I do a hyphen in Django template view?

{{profile.first-name.value}} My variable is hypeh only...I wish I could do first_name, but many variables are hyphens. However, due to this problem, I can't display my variables in the template. Why? ...

Django model class methods for predefined values.

I'm working on some Django-code that has a model like this: class Status(models.Model): code = models.IntegerField() text = models.CharField(maxlength=255) There are about 10 pre-defined code/text-pairs that are stored in the database. Scattered around the codebase I see code like this: status = Status.objects.get(code=0)...

Hooking str.__getitem__ in Python

Is there a way of hooking str.__getitem__? Example: I'd like to be capable of do: >>> "this is a string"[[1,3,4]] 'hs ' passing a list to [] and get the items in that list. A more realistic example: class STR(str): pass class INT(int): pass It's easy to make that STR("a string")[1] or STR("a string")[INT(1)] return an S...

Why str has no __radd__ method in Python?

str has other methods like __rmod__, __rsub__ or __rmul__ int did have a __radd__ method Weird, and I'd like to know why. Example radd beeing called when first term HAS add method: >>> class STR(str): ... def __radd__(self, other): ... print "jeje" ... return other.__add__(self) ... >>> 'aaaaa' + STR('bbb...

In Python, I have a dictionary. How do I change the keys of this dictionary?

Let's say I have a pretty complex dictionary. {'fruit':'orange','colors':{'dark':4,'light':5}} Anyway, my objective is to scan every key in this complex multi-level dictionary. Then, append "abc" to the end of each key. So that it will be: {'fruitabc':'orange','colorsabc':{'darkabc':4,'lightabc':5}} How would you do that? ...

How to store python classes into a database with Django ?

Hi, I have two files: choices.py class SomeChoice: name = u"lorem" class AnotherChoice: name = u"ipsum" # etc... models.py from django.db import models import choices class SomeModel(models.Model): CHOICES = ( (1, choices.SomeChoice.name), (2, choices.AnotherChoice.name), # etc... ) so...

What's the pythonic way of declaring variables?

Usually declaring variables on assignment is considered a best practice in VBScript or JavaScript , for example, although it is allowed. Why does Python force you to create the variable only when you use it? Since Python is case sensitive can't it cause bugs because you misspelled a variable's name? How would you avoid such a situat...

Installing scipy with pip

It is possible to install numpy with pip using pip install numpy. Is there a similar possibility with scipy? (Doing pip install scipy does not work) Update The package scipy is now available to be installed with pip, so the question is not relevant anymore. ...

how do people normally deal with class variables in django?

I can't see any provision for this in the django docs, so how do people go about doing this. My specific case is this. I have a shopping cart, each cart instance has an invoice number field, however the invoice number is only generated if the cart goes to a paid status, so not all shopping cart instances will have an invoice number. I ...

Changing the hour with datetime.replace() in python

Given that foo is a valid datetime object in python, One can change the hour represented in a datestamp (foo) by doing something something like: foo2 = foo.replace( hour=5 ) Rather then replacing the hour with a particular value ( as is done above )..is it possible to increment the time in foo by say, 5 hours ? Something along the...

How can _meta.local_fields not match the table schema in the database?

I'm completely confused about why _meta.local_fields returns more fields than the database table contains. The User model inherits from contrib.auth.models.User. $ mysql -u user -p database Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup wit...

Python: removing duplicates from a list of lists

I have a list of lists in Python: k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]] And I want to remove duplicate elements from it. Was if it a normal list not of lists I could used set. But unfortunate that list is not hashable and can't make set of lists. Only of tuples. So I can turn all lists to tuples then use set and back to lists...

Is everything greater than None?

Is there a Python built-in datatype, besides None, for which: >>> not foo > None True where foo is a value of that type? How about Python 3? ...

cache.fetch in Django?

Does Django caching have a method similar to Rails' cache.fetch? (http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#M001023) The rails cache fetch works like: cache.fetch("my_key") { // return what I want to put in my_key if it is empty "some_value" } It's useful because it checks the cache, and returns the value ...

(django initial setup) Django installation is redirecting all traffic to django page, fix?

I'm a complete newbie to Django. I've been trying to get it working on my Ubuntu server. everytime someone my server, it redirects to the "Congratulations on your first Django-powered page." It completely ignores the index.html file in the www directory. Why is that? Is there a away to make it so that it only goes to the django page whe...

Efficient Python array with 100 million zeros?

What is an efficient way to initialize and access elements of a large array in Python? I want to create an array in Python with 100 million entries, unsigned 4-byte integers, initialized to zero. I want fast array access, preferably with contiguous memory. Strangely, NumPy arrays seem to be performing very slow. Are there alternatives ...

wxpython GUI having static Japanese text and chinese static text.

Hi All, We want to support localization of the static text (labels, button labels, etc) to Japanese and Chinese in wxpython. We want only static text within the GUI elements to be changed, hard coding of Japanese or Chinese characters in the label(static text fields) would do the work for us. Any help on how to pursue this would be help...

Django: Making ForeignKey not create a back-reference

I know that I can use ForeignKey's related_name argument to control what the back-reference's name will be. But is it possible to avoid creating a back-reference completely? (e.g., I have in Car a field ForeignKey(Person), and I don't want Person to have an attribute that leads backs to Car.) ...

Django: Can the value of ForeignKey be None?

I have a model called SimplePage in which I have this line: category = models.ForeignKey('Category', related_name='items', blank=True, null=True) I assumed this will allow me to have SimplePage instances that do not have a Category. But for some reason, when I try to create a SimplePage in the Admin with ...

Are there any free alternatives to the Ranorex library (Python, Windows)?

I am interested in the Python one. I wish to automate some GUI under Windows. What is the best open source library for that with no strings attached? Thanks. ...