1. Cloning a git repository without history

    Large git repositories tend to owe a very large portion of their size to commit history. Most of the time, however, when cloning a large project for local usage, history is of no interest.

    Take the django project, for example. To get the latest development version, you have to clone a repository of no less than 158 megabytes. If you could only grab just the latest code...

    git clone git://github.com/django/django.git --depth 1

    The --depth flag does the trick, bringing the number down to 61 megabytes.

  2. Avoiding circular Dependencies in Flask

    flask is a compact python web framework that combines simplicity and speed of development with a design that can scale when complexity grows.

    flask documentation, however, is a little too eager to show off this simplicity, and all the basic examples introduce practices that won't work when your application grows from tiny to small.


    In its most basic form, a flask application lives inside a single file, a file that looks like this:

    app = Flask(__name__)
    db = SQLAlchemy(app)
    class Thing(db.Model):
        id = db.Column(db.Integer, primary_key = True)
    def index():
        return 'Hello World!'

    Note that both views and models (via db and route()) depend on the app object.

    When you attempt to move them into their own, separate files, you'll discover that there's a circular dependency in both cases: views will need to import app to use route(), and app will want to import views to set up those routes. The same will happen with models, app and db objects needing each other.

    Breaking the cycle

    The db mutual dependency is easily solved by postponing the initialization of the db object:

    # models.py
    db = SQLAlchemy() # no `app` paramater
    # app.py
    from models import db
    db.init_app(app) # delayed initialization

    Easy enough, no hacks involved. Same goes for the route dependency:

    # views.py
    blueprint = Blueprint('views', __name__)
    def index():
        return 'Hello World!'
    # app.py
    from views import blueprint

    Closing thoughts

    The flask documentation should be very clear about the right design pattern for these cases. It's not. The amount of work required to do it right is so minimal that I don't really see why this is not mentioned early on.

    As I said above, I think flask is too proud of its simplicity. Too proud to show you even the slightest level of complexity as you learn the framework.

  3. Goodbye GNU cut

    GNU cut is one of those small programs that embody the Unix philosophy. cut does exactly one thing, and is ready to work with pipes. From a modern perspective, however, cut is simply too primitive.

    The shortcomings of cut drive people to use grep, sed or awk for inline field selection, which is theoretically cut's responsibility. These regex-powered tools, however, are meant for more complex operations, and require advanced know-how and special syntax.

    So I wrote sel.

    sel serves the same purpose as cut, with notably improved syntax and better, more flexible features. It's a zero-googling field selection and table transformation tool, a strict superset of cut that doesn't require a DSL to operate.

    Goodbye, cut.

Page 1 / 5