1. Python One-liner Games

    I took a break from the intense book schedule this weekend (don’t worry everything is on schedule). Was reading about Commodore’s one-liner maze when it struck me.

    I wondered - why not make some one-liner games in Python? And just to make it insanely hard, I would try to make it fit a tweet i.e. 140 characters. Let’s see what we could manage.

    NOTE: All the one-liners below work only in Python 3. Depending on your platform, you might want to change the code by replacing python with python3.

    Maze

    Game recording

    python -c "while 1:import random;print(random.choice('|| __'), end='')"
    

    Create an infinite maze with this deceptively short one-liner. It is quite easy to understand too. The while loop is infinite. The import statement had to move inside the loop but Python takes care not to re-import it each time.

    A random character is picked from one of the maze drawing characters and printed. An alternate version below has better maze drawing characters. But this version used characters which were displayable on the Windows shell (and possibly portable to other operating systems).

    Prettier Version

    python -c "while 1:import random;print(random.choice('╱╲'), end='')"
    

    This version ...

    Continue Reading →
  2. Edge v2.0 - More Edgy Ideas

    Edge v2.0 has just been announced. Since the first release, there have been numerous feature requests and pull requests. I am happy to share that we have been able to incorporate most of the planned features and fixes, including:

    • Email-based logins!: Most websites don’t need usernames. Furthermore, the users have to come up with a unique and memorable username. But their email address is already unique and memorable, hence more widely used for logins these days.

    • User profiles!: In 90% of the user based applications you will need to create a user profile. So we have added one with nice default fields like a user avatar image, bio etc. They are extendable too.

    • Python 2.7 support!: Supporting Python 2.7 was a much requested change. Back-porting needed not just syntactic changes but some tricky Unicode management as well.

    • Less Bootstrappish: After browsing through several examples of well designed bootstrap-based sites, several design improvements have been added like see-through navbar that changes on scroll, full cover image etc.

    • Environment specific Settings and Requirements files: This was a convention most Django developers follow so the environment specific files have been split out.

    • Authentication Workflows: In addition to signup, login ...

    Continue Reading →
  3. Django Design Patterns and Best Practices Book - Coming Soon

    Here is the big news I hinted at in my last Pycon talk. I have been working on a book titled “Django Design Patterns and Best Practices”. Most of the early drafts have been reviewed and it is slated for publishing in March.

    You can pre-order it here right now! Click here if you like to be notified when it will be published.

    Some of the highlights of this book:

    • Guides advanced beginners to web application fundamentals and patterns
    • Updated for Django 1.7 (upcoming 1.8 features also mentioned)
    • Covers the creation of a fun project - a social network for superheroes
    • Uses engaging story elements to illustrate real world challenges
    • A short read at less than 200 pages

    Why Patterns?

    Most Python programmers I know, rarely use a Gang of Four pattern like the Strategy Pattern or the Iterator Pattern. It makes sense, too. After all, they were designed for object-oriented programming languages like C++ and Smalltalk, while Python has better higher-order abstractions. Many of these patterns are not even required in Python.

    But while working in Django, we come across patterns at a higher level of abstraction. They could be anything from implementing models to store user profile ...

    Continue Reading →
  4. Introducing Edge - a Modern Django Project Template

    A newer version has been announced

    Have you ever begun creating a web app only to abandon it while setting it up? I have done it more times than I would have liked.

    Many programming environments involve so much ritual to setup that we tend to forget what we set out to create. Recently, I had to setup an Android development environment from scratch. Despite downloading the latest version of the IDE, I waited until several additional updates downloaded to patch it to the latest version. The trend of packages getting more granular and updates being made several times a day is here to stay.

    It is like if you have an idea for a painting then you have to go shopping for all the art-supplies first. But the moment you step out, you have lost your muse. Scientists call this encoding specificity or as a famous paper calls it “Walking through doorways causes forgetting.”

    Django Projects

    Of course, in Django I use startproject which builds an excellent but minimal project structure. But once I start coding I inevitably tend to install packages like the django braces. Most of my project use Bootstrap, so I end up downloading the bootstrap ...

    Continue Reading →
  5. Recreating the “Building a Blog in Django” Screencast

    Few days back, Django 1.7 release candidate 1 was announced. I have never been so excited about a release in recent memory. It has a new app loading framework and schema migrations baked right in!

    It is also the significant release that I had been waiting for to update my original Django screencast. With more than 77 thousand views, it is by far the most popular Django screencast on YouTube.

    Back in 2012, when I was working in Django, there were a lot of comparisons between Rails and Django. One of the biggest selling points of Ruby on Rails was their official 15-minute blog screencast. It was fascinating to watch an expert Rails developer build an entire site from scratch especially how deftly they use TextMate, which could nearly read his mind.

    Armed with a Linux terminal and Emacs, I set out to create a similar screencast for anyone interested in Django. Live coding was something that I had not tried before. I remember starting at 11 pm one night, hoping to complete my recording by midnight.

    But by the time I finished a recording without any major goofups it was 5 am in the morning. So with barely ...

    Continue Reading →
  6. Understanding Test Driven Development with Django

    Test-driven Development (TDD) has been getting a lot of attention these days. While I understand the importance of testing, I was sceptical of Test-Driven Development for a long time. I mean, why not Development-Driven Testing or Develop Then Test Later? I thought figuring out the tests before one can write even a single line of code would be impossible.

    I was so wrong.

    Test-driven Development will help you immensely in the long run. We will soon see how. We will approach TDD from a sceptical viewpoint and then try to create a simple URL shortener like bit.ly using TDD. I will conclude with my evaluation of the pros and cons of the technique; to which you may directly jump to as well.

    What is TDD?

    Test-driven development (TDD) is a form of software development where you first write the test, run the test (which will fail first) and then write the minimum code needed to make the test pass. We will elaborate on these steps in detail later but essentially this is the process that gets repeated.

    This might sound counter-intuitive. Why do we need to write tests when we know that we have not written any code and ...

    Continue Reading →
  7. An Easy Guide to Install Python or Pip on Windows

    I enjoy studying other people’s development environments. They give me plenty of insights into their aesthetic choices and their productivity shortcuts. But I have seen a lot of horrible environments cobbled together by flaky batch files that are one update away from blowing up, often leaving no option but to reinstall everything from scratch. Unfortunately, they have all been Windows environments.

    Recently, I tried to install Python and pip on a Windows laptop. Though I have installed Python on Windows XP and Windows Servers for several years till 2010; there have been a lot of changes both in the Windows world such as Powershell and the Python world. So there is a lot of confusing information out there for the Python beginner. And then there is Python 3. Sigh!

    Suffice to say I wanted to share what I learnt in a video aimed at the Python beginner or anyone struggling to install Python on Windows. The video and the high-level transcript follows:

    What is covered?

    • Installing Python 2 or Python 3 on Windows 7
    • Installing pip from PowerShell
    • Setting up a virtualenv
    • Installing via pip

    Screencast

    How to install Python / Pip on Windows 7 (or 8)

    1. Download the MSI installer ...

    Continue Reading →
  8. Binary Clock - Django Nibbles

    Django can be used to build great websites. But it is also really good for solving small problems quickly. Introducing a new series called “Django Nibbles”, to help you learn an aspect of Django say Templates through a short and simple problem. You can either solve the problem yourself or follow my step-by-step solution.


    Q. Create a page that displays a binary clock showing the current time (when the page was loaded).

    For instance, if the time is “23:55:02” then we should see:

         
         
         
         
    

    Each column represents a binary digit when read from top to bottom. For more details, read the Binary Clock wiki page.

    Django Feature: Views

    Time Given: 1 Hour

    Go ahead. Try it yourself before reading further!


    A. We will be using Python 3 (not 2.7) and Django 1.6 for solving this. Both are the latest versions at the time of writing.

    Setting up the project

    This section can be skipped if you know the basics of setting up a project in Django (which is simpler in Django 1.6)

    On Linux command-line (indicated by the ‘$’ prompt), enter the following to create the binclock project:

    $ cd ~/projects
    $ django-admin.py startproject binclock
    $ cd binclock
    

    Getting ...

    Continue Reading →
  9. Real-time Applications and will Django adapt to it?

    While talking about Django at PyCon India this year, the most common question I got was whether it supports Single Page applications or an event driven architecture. I was quick to point out that such realtime applications are usually a handled by separate services at the backend (running on a different port?) and a JavaScript MVC framework on the front. Sometimes, Django supported by a REST library like Django REST or Piston is used as the backend too.

    But the more I thought about it, those questions felt more and more important. Then, I had a chance to read about Meteor. I tried some of their tutorials and they were fantastic. I was so impressed at that point that I felt this framework had the power to change web development in a big way. I was even tempted to write an article similar to Why Meteor will kill Ruby on Rails for Django. But then, better sense prevailed and I dug deeper.

    So I wrote this post to really understand the Real-time web application phenomena and pen my thoughts around it. It will look at how a traditional web framework like Django might tackle it or whether it really can ...

    Continue Reading →
  10. Building a Hacker News clone in Django - Part 4 (AJAX and Mixin)

    You are reading a post from a four-part tutorial series

    Yes, all good things do come to an end. It gets even better when the ending is good. Steel Rumors was a project to help Django beginners progress to the next level from basic tutorials. It had elements which would be useful for most practical sites like user registrations and making CRUD views.

    Honestly, I don’t like most video tutorials myself as they need a considerable amount of time to watch. However, if they come with a full transcript then I can skim through the text and decide if it contains tips which are worth watching on video. Sometimes, the additional commentary explaining the context is well worth the watch.

    So, I set out to create Steel Rumors as something that everyone including me would enjoy watching. But it turns out creating the transcript is much harder than recording a quick video. In fact, it gets monotonous at times (I really sympathise those who work in Medical Transcription!).

    The videos also get difficult to record in a longer, complex project such as this. I don’t edit out any mistakes or ...

    Continue Reading →

Page 1 / 17 »