Showing posts with label programming. Show all posts
Showing posts with label programming. 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

Thursday, January 08, 2015

MySQL - create database and user quickly

I'm not a MySQL fan. I don't use it enough to remember some basic things.

mysql> create database mydb;
mysql> create user 'myuser'@'localhost' IDENTIFIED BY '*******';

mysql> grant all privileges on mydb.* to 'myuser'@'localhost';

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)

Tuesday, October 13, 2009

log4net

I keep forgetting how to switch on log4net (using app.config)

I always end up wasting time on the apache docs, and searching the net to find this

Step 0:
Ensure that the first declaration of
"private static readonly log4net.ILog Log =log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);"
Occurs in your main assembly - if the first declaration is in linked assemblies, it won't load the setting correctly.


Step 1: Edit AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Step 2: Edit App.config
Important! The configSections bit has to be at the top of your config file. So if you've got an existing one, don't just add it on to the end!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

Step 3: