Inside the Head of PyDanny

Hi, I'm Daniel Roy Greenfeld, and welcome to my blog. I write about Python, Django, and much more.

Tag: Howto

Implemementing Manual Schema with Django REST Framework

BitCoin and Two Scoops of Django!

This is what will hopefully be the first in a series of reference articles for using Core API libraries with Django REST Framework (DRF).

This is an extraction from an existing production view running Django 1.11/2.0 on Python 3.6. The original code did something else, but ...

Read more ...


Why Doesn't Python Have Switch/Case?

Aliens

Unlike every other programming language I've used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping:

def numbers_to_strings(argument):
    switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
    return switcher.get(argument, "nothing")

This code is analogous to:

function(argument){
    switch ...

Read more ...


Setting up LaTeX on Mac OS X

These are my notes for getting LaTeX running on Mac OS X with the components and fonts I want. Which is handy when you want to generate PDFs from Sphinx. At some point I want to replace this with a Docker container similar https://github.com/blang/latex-docker, albeit with ...

Read more ...


Building Conda Packages for Multiple Operating Systems

On the Cookiecutter project, recently we added conda to the open source packaging systems we officially support (You can find Cookiecutter on PyPI, homebrew, and apparently some Linux distros).

Creating a conda recipe from a PyPI package

Prequisites:

Read more ...


Adding Django form instance attributes

Sometimes in the clean(), clean_FOO or save() methods of a Django form, you need to have additional form instance attributes available. A sample case for this is having user_id available. This is a simple example of how to do it in Class-Based Views.

Assuming this form:

from django import forms ...

Read more ...


Filepicker.io and South

I've heard good things about filepicker.io, which is a service that makes file uploading a much better experience. Unfortunately, the Django package for filepicker.io doesn't work with South. When I try to create a migration using the filepicker.io field using code like the following...

# products ...

Read more ...


Generating NCX files with Python

With the help of fellow Python developer Matt Harrison's excellent Ebook Formatting: KF8, Mobi & EPUB, we managed to create pretty decent looking Kindle and ePub versions of Two Scoops of Django.

One of many things we did was focus on providing an excellent table of contents. Of course ...

Read more ...


Exceptions as Decorator Arguments

I wanted to see if I could have an exception as a decorator argument. Here is what I came up with:

import functools

class MyGoof(Exception):
    pass

def pass_goof(exception):
    def decorator(test_func):
        @functools.wraps(test_func)
        def wrapper(*args, **kwargs):
            try:
                return test_func(*args, **kwargs)
            except exception as e:
                return ...

Read more ...


The Easy Form Views Pattern Controversy

In the summer of 2010 Frank Wiles of Revsys exposed me to what I later called the "Easy Form Views" pattern when creating Django form function views. I used this technique in a variety of places, including Django Packages and the documentation for django-uni-form (which is rebooted as django-crispy-forms). At ...

Read more ...


Stay with the Django CBV defaults!

One virtue of Django Class Based Views (CBVs) is that they come with pretty good default settings. The virtue of this is you can really pare your code down in size and complexity.

For example, here is an implementation of CBVs based on a straight-forward Django model , stuffage.models.Stuff ...

Read more ...


Case Study: URL Design for petcheatsheets.com

Backstory: On Saturday, November 17, 2012 Audrey Roy and I decided to participate in the Petcentric hackathon, a Los Angeles area Pet-themed product/coding contest held at Amplify. We arrived a bit late, but armed with Audrey's idea of creating a pet based reference sheet for owners, pet sitters ...

Read more ...


Django GetOrCreateView

Today I decided to use the Django class based view (CBV) CreateView, but I wanted to avoid duplications and submit to the view from the front page of a site. The reason was I needed a simple newsletter signup form. This is what I cooked up and should work for ...

Read more ...


Installing Pycairo on Mountain Lion

Pycairo is the binding for the cairo graphics library. It's also not something you can get running with a simple pip install py2cairo. After many hours of working the search engines and dancing to the configure/make/make install melody, I figured out an answer that worked for me ...

Read more ...


Python dictionary as a class

A long time ago, circa 1999, when I was working in a certain procedural language I found a library that added objects to the language. It did so by playing interesting tricks with key/value structures, which in Python are called dictionaries. In 2005, as a new Python user, I ...

Read more ...


Attaching custom exceptions to functions and classes

Having too many custom exceptions on a project can be a pain, but a few choices ones are really nice. The problem is that in complex libraries having to import both functions and exceptions becomes a drag. To mitigate having to remember to import custom exceptions, this is a handy ...

Read more ...


Django Update View without slug in the url

Today I wanted to use the Django Class Based View (CBV) UpdateView but without a slug identifier in the URL. For example, instead of /profiles/pydanny/ I would go to /my-crazy-profile/. Also, I needed to force authentication.

I've done this with Django functional views a few times times, but ...

Read more ...


Simple HTTP Basic Auth Wall

I have a client who wanted their entire unlaunched public content site quickly but temporarily blocked for a short period of time. He wanted a universal password so he could send the site to reviewers, done quickly, and nothing else. In a few days the site will launch, and even ...

Read more ...


Django Class Based View: email form with CAPTCHA

Yesterday I showed how to implement a simple email form for Django using Class Based Views. Today I'm going to extend yesterday's work to use the excellent RECAPTCHA service to help reduce spam content.

This version requires pip installing the following into your virtualenv.

  • pip install django-crispy-forms so ...

Read more ...


Simple Django email form using CBV

Here's a simple FormView Class Based Views for Django. Here is a sample of how to do one as a simple email form. There is no CAPTCHA in this example, that's the topic of a future blog post.

This version requires the following packages pip installed into your ...

Read more ...


Parsing MongoDB URI

Rather than hard-code the configuration into a Python based settings file, when using a PaaS such as Heroku you want to pick up the MongoDB URI from the system settings. Here's what I do:

# get the dynamic elements from the MongoURI
import os
import re
r = r'^mongodb ...

Read more ...


Content Copyright © 2012-2018 Daniel Greenfeld. Proudly harnessed by Mountain, powered by Flask, and rendered by Frozen Flask, all of which take great advantage of Python.