Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, April 26, 2018

Things in Django I constantly forget

There are some things in Django I constantly forget!

Getting the friendly value for a enum value


You have model:

class MyModel(models.Model)
    thing = models.IntegerField(choices=SOME_CHOICES)

You want the value for thing:
happy_value = my_instance.get_thing_valuedisplay()


Use the free jupyter shell that comes with django extensions

pip3 install jupyter
or
python -m pip install jupyter
then
python manage.py shell_plus --notebook

Wednesday, January 07, 2015

Things about python that I constantly forget (hate)


SyntaxError: Non-ASCII character '\xe2' in file blah.py on line 67, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Gaaaaah!

Put at top of file:
# -*- coding: utf-8 -*-

Wednesday, March 19, 2014

Why I love Python

Reason #1

monkeys.sort(key=lambda monkey: monkey.height)


Reason #1.2

monkeys.sort(key=lambda monkey: (monkey.height, monkey.age))

Reason #1.3

monkeys.sort(key=lambda monkey: (monkey.height, monkey.age), reverse=True)