Category Archives: Meh Practices

Meh Practices Nerd Patterns

Django management commands and verbosity

Ren and Stimpy

[update: This post has been corrected, thanks to my commenters for your feedback]

Every Django management command gets the verbosity option for free. You may recognize this:

optional arguments:
  -h, --help            show this help message and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output

We rarely use it because doing so usually means lots of if statements scattered through our code to support this. If you’re writing quick n’ dirty code, this may look familiar in your management commands:


if options.get('verbosity') == 3:
    print('hi')

In a recent Django project, I came up with a few lines of boilerplate to support the verbosity option, assuming you’re using also the logging library and not relying on print:


import logging
class Command(BaseCommand):
    def handle(self, *args, **options):
        verbosity = options.get('verbosity')
        if verbosity == 0:
            logging.getLogger('my_command').setLevel(logging.WARN)
        elif verbosity == 1:  # default
            logging.getLogger('my_command').setLevel(logging.INFO)
        elif verbosity > 1:
            logging.getLogger('my_command').setLevel(logging.DEBUG)
        if verbosity > 2:
            logging.getLogger().setLevel(logging.DEBUG)

github.com/texas/tx_mixed_beverages/blob/master/mixed_beverages/apps/receipts/management/commands/geocode.py

So what does this do?

At the default verbosity, 1, I display INFO logging statements from my command. Increasing verbosity to 2, I also display DEBUG logs from my command. And going all the way to verbosity 3, I also enable all logging statements that reach the root logger.

Go forth and log!

Meh Practices Nerd

Autobuilding Sphinx Docs With inotify

So tonight I decided I’ve been putting it off far enough, and started my first Sphinx documentation project. Read The Docs’s Getting Started guide wasn’t bad, but it really bothered me that there wasn’t a good way to autobuild the docs.

I ended up just adding this to the makefile:

# If you don't have inotify-tools, install it first: `apt-get install inotify-tools`
watch:
    @while true; do \
    inotifywait --recursive -e modify -e move -e create -e delete \
    --exclude $(BUILDDIR) . && $(MAKE) --silent html; done

This blog post by JKM is often cited as a way to do it too, but the install_requires section of watchdog’s setup.py was longer than I felt like sitting through.

There’s a promising project, sphinx-autobuild, but the readme currently says “sphinx-autobuild relies on a not-yet-released version of python-livereload” and it also uses watchdog; so forget that. If I end up spending a lot of time writing docs, it’s worth getting this up and running though because it comes with LiveReload.

Meh Practices pics plz

Winter Tips

Here are some winter tips from a Texan who was in Chicago that one time:

  • Don’t layer. If it’s below 40 degrees F (4 degrees C), forget layers. Just wear a T-shirt and your good winter coat. That is seriously all you need. Don’t have a coat warm enough? Well, then you should probably layer. Would you rather put on and peel off a dozen layers as you transition from indoors to outdoors and back indoors; or just throw on a coat?
  • Forget gloves. Put your hands in your pockets.
  • I lied, get gloves.My point is: don’t depend on them (unless you’re biking, but that’s another story). Be prepared to lose your gloves. Ideally, the gloves you buy have clasps, hooks, and/or velcro so you don’t lose them. I actually have a glove/coat combo where I can peel off my gloves and stick them onto a velcro strip on the coat. Ski jackets usually have good glove storage.
  • Learn how to put on and take off your gloves with your teeth.¬†True story: If you’re walking down the street eating your ice cream and you need to answer your phone, you don’t want to have to put down your ice cream to mess with a glove.
  • Your nose will run. Don’t use tissues. I always carry a washcloth for this. Bicyclist will know that winter gloves are designed to have a patch for wiping your nose.
  • No hats. All you need are ear warmers. Even on bike. I got these foldable ear wamers on woot a loooong time ago: http://www.woot.com/offers/eargrips-behind-the-head-fleece-ear-warmer-2-pack-dec-15 You can fold them for easy storage, and you can also carry them by wrapping them on your arm, freeing up valuable pocket space. Besides, hats mess up your hair.
  • Do use scarves and hoods. Hoods are useful for dealing with wind, and scarves can be equivalent to wearing a whole layer. Learn the different way of tying a scarf. You can tie a scarf different ways to deal with different degrees.
  • Pants: it doesn’t matter, and you don’t need long underwear. You can wear the same pants you wore in summer. Same goes for socks and shoes. Your jacket will cut the wind and cover the important bits. I don’t know what magic girls use to walk around in leg warmers. Maybe it’s the Uggs.

And there you have it. With that wardrobe, I was biking around Chicago all winter in 5 degree F weather. The only modification I made for biking was I had to invest in some serious gloves. Another modification you might want is if you’re going to be standing still outdoors for a long time (why?), then you should add layers.

Finish Writing Me Plz Life=Boring Meh Practices

GTA IV 1.0.7 bootstrap guide

There are lots of websites and youtube videos all trying to tell you how to mod GTA IV. Unfortunately, they’re almost all out of date, or youtube videos.

I’m using steam installed Grand Theft Auto IV 1.0.7

A decent guide: http://www.gta-modding.com/iv/tutorials

Prepping the Game

Install This:

Xliveless (v0.999b7 md5: F7FD7512F6AC8959CDDA6A2B2E014C68)

http://www.gtaforums.com/?showtopic=388658

SparkIV (v0.7.0b1)

The original tool, currently trying OpenIV below instead of this.

http://www.gtaforums.com/?showtopic=384933

 OpenIV (v1.5.0)

Seems to be much newer than SparkIV. Still testing it out. It actually installs (as opposed to SparkIV, which you just unzip and run) and is more difficult to set up that it needs to be. Some mods require OpenIV.

http://openiv.com/

Installing a simple mod

Most mods will tell you exactly what to do in their readme.txt. I’ll only give installation highlights.

No Intro Screen

Who has time to look at intro screens? This mod is easy to install and well worth it. Just copy loadingscreens_pc.dat to common/data. It’s plaintext so you can see what changes it makes.

http://www.gta4-mods.com/misc/no-intro-logos-f1814

Rockstar Social Club

I couldn’t find a way to disable it. I added these to my hosts file anyways:

127.0.0.1 rsgames.hs.llnwd.net
127.0.0.1 mls.rockstargames.com

At least the time saved taking off the intro screens make up for having to click through the Social Club screens.

Installing some intermediate mods

scripthook.dll (v0.5.1 md5: 7260B388AAC8329C3CF615084AA7DB83)

You may need to dig around for this download; the download link in the homepage is dead. Sometimes it’s redistributed inside a mod. Just copy it into root GTAIV directory.

homepage: http://www.gtaforums.com/index.php?showtopic=390582

inGame Trainer (v1.9.0)

This is the trainer you see most often in youtube videos. You can change the weather, teleport, change your wanted level, enable god mode, and enable cheats. It’s not an open sandbox game without a little god mode.

homepage: http://www.gtaforums.com/index.php?showtopic=393676

CarSpawner In-Game Menu

The thing about this mod is that for some reason, the readme is a word doc. Don’t worry though, the text on the homepage is the exact same as what the readme says. This mod defaults to taking over F1 to open the gui, just like inGameTrainer, so you’ll need to edit at least one of them.

http://www.gtaforums.com/index.php?showtopic=399468

Installing an advanced mod

Iron Man IV (v1.2)

See the readme.txt. It’s an involved process but detailed nicely. Remember to make backups!

homepage: http://www.gta4-mods.com/player/iron-man-iv-v11-f21888

Other noteworth mods:

  • iCEnhancer

Other hacks

commandline.txt

http://www.gamefaqs.com/pc/952150-grand-theft-auto-iv/answers?qid=20326

Meh Practices Nerd

A few virtualenv tips

If you’re doing any development in Python, odds are you’re using virtualenv. And if you’re using virtualenv, odds are you’re using virtualenvwrapper. I came up with a few tricks to make my life easier, and I thought I ought to share them. And as a disclaimer: although I am obsessed with discovering best practices, these are definitely not. So I’m adding a new category called “Meh Practices”.

Set an $IPYTHONDIR environment variable.

According to the IPython documentation, IPython stores settings in a profile, and it looks for that profile in $IPYTHONDIR. So in my global virtualenv postactivate, I have:

export IPYTHONDIR=$VIRTUAL_ENV/.ipython

Now why would anyone want a separate profile for every virtualenv? I do this so every virtual’s IPython gets its own history database. I also used to run IPython 0.10.X and 0.11.X side by side, and the two had incompatible profile formats. All my tools support the new IPython so this isn’t an issue for me anymore, but I still do it for the separate history.

Maintain the same IPython configuration for every profile

Because now I’m running around with N IPython profiles with N configurations when I really only want 1 configuration, I symlink all the profile configurations from a master one I keep in source control. The gist of it looks like:

DST=$IPYTHONDIR/profile_default
SRC=$HOME/.dotfiles/.ipython/ipython_config.py
mkdir -p $DST
cd $DST
ln -s -f $SRC

But my actual one currently looks like this and I run it in my global postmkvirtualenv, which now looks like:

IPYTHONDIR=$VIRTUAL_ENV/.ipython
mkipythonconfig

Where mkipythonconfig is the snippet above.

Add an alias to maintain your environment variables

I got really sick of cdvirtualenv then vi bin/postactivate, so in my postactivate, I have this line:

alias vivirtualenv="$VISUAL $VIRTUAL_ENV/bin/postactivate"

in my virtualenv’s postactivate. Now when I need to edit my postactivate, it’s a few tab complete-able keystrokes away. I stole the naming convention from visudo, and I tried naming it vipostactivate, but I think vivirtualenv is just so catchy. Now if your immediately reaction is to make a comment telling me I should just use autoenv; too bad, I already thought about that. Right now, issue#28 is preventing me from adopting autoenv. I also can’t go around adding .env to the .gitignore of collaborated projects. And there’s one project I know of where the Procfile for foreman is not in the project root. #everythingisterrible

Use setvirtualenvproject

I don’t take advantage of virtualenvwrapper’s mkproject, nor do I really know what to do with them, but there is one thing I do use from that… setvirtualenvproject. I never use mkproject because I rarely name my projects and virtualenvs the same, and I haven’t bothered to set up any project templates. But I am lazy, and I like how if you tell virtualenv that your current working directory is the project root using setvirtualenvproject, every time you workon it, you’ll automatically cd into it. But what if you don’t want that to happen? You can jump back to the directory you were just in with cd -. For me, the extra time it takes to go back is more than made up by all the time saved not having to cd in.

So putting everything together, I have a makefile for bootstrapping a new install so virtualenvwrapper and IPython are set up the way I like. It may seem like I have to do a lot of work to get up and running, but I’ve got it down to one make now.