Skip navigation

Tag Archives: python

For February I tinkered with Django, arguably the most popular web framework for Python. Prior to this, the only web framework I’ve had experience with is CodeIgniter and one proprietary JSP-based framework.

In an earlier post I covered installing Django. Nothing much has changed between then and now except that the most recent version of Django, 1.5, now supports Python 3! The first for Django ever. But I’m sticking with Python 2 for the time being.

Note: I tried learning Django last August, in an afterthought project with goals loftier than what my resources could have achieved. Needless to say, I never got too far with it. It was this which prompted me to specify a mini-project I’d use to learn web frameworks (any web framework). I’m not sharing the specifics of it now but you can gather what you can in my sandbox repository.

The Admin Site

One nifty feature of Django is how it creates an admin site automatically for you. On my CodeIgniter projects, I’d pattern the users table something like this, borrowing from Linux file systems:

    -- Some project-specific fields here
    create_time TIMESTAMP NOT NULL,
    last_updater INTEGER NOT NULL,
    last_update TIMESTAMP NOT NULL,
    PRIMARY KEY (userid),
    FOREIGN KEY (last_updater) REFERENCES users (userid)
    -- Yes, apparently, MySQL allows you to foreign key recursively

If I’m really feeling up to it, or if the project requires (never had it tho), I might even implement user groups. But even with this, I can already define 23=8 types of users, which is already way more than what one project usually requires. What I’d normally do here is to designate can_exec privileges to admins. Way neater than maintaining different tables for different user types and then giving admins backdoor log-in pages—one of the naive approaches I used when I first started designing databases for apps.

Django automatically covers this for you, user permissions, groups, and all. The structure becomes a bit different though, and I find permission management way more complicated than my binary-scheme. Take a look at all possible user permissions in Django:


Look at how many that is, and realize that there is still a scroll bar!

What Django basically did is to allow you to specify add/change/delete permissions for every table in your database. Note that due to how Django enforces code modularity (see below), the number of tables in your database could easily blow-up.

Code Modularity

Perhaps one of the most confusing things I encountered as I tried to learn Django was how it distinguishes the terms “project” and “app”. Normally, I’d talk about an “app” being a “project” for some party. But in Django a project is a collection of apps. I had a hard time designing my project around these terminologies even after reading discussions at StackOverflow until I realized that perhaps a better term for “app” would’ve been “plug-in” and there’s really nothing stopping you from making your project as one big plug-in.

With the side-effect, of course, of your app being non modular and probably not well-factored. So, yes, the way Django handles things enforces the nice practice of modularity. Nice to take advantage of it, though it may be too time-taking to wrap your design around Django’s rules.

Django also enforces a form of Object-Relational Mapping (ORM). In fact, you don’t need to write a single line of actual SQL code to get your database up and running—all DB creation is done through the ORM and . This even allows you to easily switch DB back-ends. Then again, as with above, not all projects would fit well for an ORM approach.


I find Django pretty neat given that you only need minimal set-up for this whole bunch of admin stuff. But then I haven’t had much time to explore it although I’ve volunteered in a (non-work, non-paid) project that might use Django pretty extensively. Guess I’ll get more Django this month. Let’s see.

Okay. I know it’s already February and that it’s kinda late. But it’s in times like this that I say “better late than never!”. But as I’ll show, I’ve already started with my resolutions—I’m just writing about them now.

But since this is my coding blog, the resolution I will mention here will be my technical-life resolution (as opposed to the personal ones everybody makes). So, without further ado…

I resolve to learn a new technology every month. Or, at least, get deeper with one I’m already acquainted with.

Also, I’d go into programming competitions/contests again. Team requirements limit my choices but, hey, there are a lot I can do alone online, not to mention free! (Though if anyone wants to team-up with me, let’s talk.)

And, just so I’ll stick with it, I’ll write a blog post every month about what I’ve been up to. Consider this the one for January.

For my first resolution, I created this sandbox repository at GitHub. And since my GitHub account is, in itself, already my sandbox of sorts, I guess this is some sandbox-ception eh?

January, I did sockets in Python. I didn’t manage to dig as deep as I’d have wanted because the Facebook Hacker Cup kicked in earlier than I expected and that falls under my second resolution. So it had to give way. Besides, I’m already dealing with a few sockets too many at work.

Regarding my second resolution, I don’t really expect much from it. At least, I won’t get rusty from the lack of problem sets in the real world (read: industry). At most, I might score a free trip to some world finals. But that world-finals scenario is still very out of my league. I’ve been doing ACM-ICPC problems since I was in my sophomore year in College and even now I find most of them really tough and tricky. Also, after less than a year out of school, I’m bound to have some rust in my system (not that school is a sure fire way to keep you sharp though). I only decided to start practicing with my algorithms sometime in December so there isn’t really much to expect.

(Note: I started writing this post in the last hour before the first round of Hacker Cup started. It’s now over and everything fell within my expectations. There’s a lot to learn. Maybe, I’ll write about this soon.)

So, what do I think about sockets in Python? (This will be short since, as mentioned, I didn’t have much time to dig deep into this.)

As with all things Python, it looks super neat. I got a client and server up and running in just 41 lines of code combined. The biggest savings comes from the fact that socket I/O is direct in Python; you no longer have to create OutputStream and InputStream objects as the socket objects themselves have methods for sending and receiving. It’s also interesting to note that Python’s socketserver class has a serve_forever method which, as the name implies, handles requests as long as it can (the official phrase is: “until an explicit shutdown signal is received”).

Oddly, this design reminds me how I handled one of the things I did at work. Won’t go into the details but I thought that it’s neat to have one class responsible for managing one whole session: packet numbers, windowing, etc. One instance of a class maps to one client; when client goes away so does this instance (at least, I no longer care if the garbage collector marks it as garbage). Makes OO live more peacefully with concurrency.

Awesome that Python enforces this design by default. Really, the more I discover about Python, the lovelier programming gets.

‘Till I get my February experiments done! ~Chad

(You’ll find lots of this online. I’m just putting this up here for my personal reference.)

Hi there. I’m writing this with a Linux/Unix/OS X environment in mind. Basically, any OS that has a pretty powerful terminal. Don’t know how this goes for Windows…Cygwin maybe?

First off, make a virtualenv to work on and install Django. You need virtualenv and virtualenvwrapper. If you do not have them installed yet, do not despair. You can use virtualenv-burrito (and I do recommend that you use virtualenv-burrito). The readme of the GitHub repo linked is pretty self-explanatory so I won’t dwell much on that. I’ll just say that…

…if, after following the instructions for virtualenv-burrito, you still can’t invoke virtualenv commands, you might have to do

$ source /home/username/.venvburrito/

Or, better yet, just put that line in your .bashrc file (or equivalent).

And also, if you are a Python 3.x fan, Django does not work yet with Python 3.x .

So, make and work on a virtualenv by

$ mkvirtualenv env_name
$ workon env_name

While inside a virtualenv, install Django (note that, being in a virtualenv, you need to be in a virtualenv to use Django):

(env_name)$ pip install Django

Create new projects by invoking

(env_name)$ startproject project_name

This will create a directory named project_name . This directory has some Django-related files. Go to this directory. Start the server by

(env_name)$ python runserver

And your set to create your web apps!

I’ve heard of Django long ago but what really got me started with it is PyCon Philippines. Here are Daniel Greenfeld’s notes on the PyconPH Django talk by Marconi Moreto, which was my primary reference in setting up my Django installation. However, I had a few deviations and personal discoveries, hence this blog post.

Title says it all. Glad that my UP DilNet account is still up. Wonder when will it go down? Hehe.

Keynote by Daniel Greenfeld right now. Talking about Linux/Ubuntu and robotics. And Python. Of course. (9:37am)

“People recognize the passion in you” – Daniel Greenfeld, who worked for NASA, with a degree in English Literature (9:57am)

Python walkthrough by Allan Paolo Barazon (10:05am).

Python Tricks You Can’t Live Without by Audrey Roy. What’s yours? Mine is definitely higher-order functions, map (11:06am)

Django Tutorial by Marconi Moreto. I’ve long wanted to ditch PHP for Python but never got/knew how to start. This one’s of interest. (11:40am)

After lunch sessions are about to start! For the after lunch session we have…

Mr. Sony Valdez is now talking about PyGame. My most anticipated talk! (1:13pm)

Functional Programming by Mr. Malcolm Tredinnick. I know FP from Scheme as you may have guessed from earlier (2:03pm).


So, okay, the first day of PyCon did not end with Mr. Tredinnick’s talk on FP in Python but, after that, this post became too long to manage with my Galaxy Y. It became pure punishment to type. I wasn’t using the WordPress App for Android though; I can’t get it to work with DilNet’s proxy settings so I was doing it on my Droid’s browser.

Anyway, there’s little I’ve missed. The next talk was on Ansible Configuration Management by Mr. Rodnet Quillo. And then, Mr. Paolo Barazon returns onstage to talk about companies/websites that use Python. I know that a lot of companies/websites use Python but I didn’t know they were this many and this extensive!

Next, Mr. Tredinnick returned for a talk on uhhh…making maps with Python? I wasn’t very sure because it was in this part of the day that all of the week’s OTs caught up with me and I fell asleep. He seems to have been able to use some Python APIs to cartograph his own map. It would’ve been interesting to see if that can be merged with PyGame for full-scale games. Shame I fell asleep!

The closing talk was given by Mr. Bryan Veloso, of GitHub, on designing your own open-source project. Sometime recently (within the past year), slides from a GitHub employee became famous for two things: (1) incredible design, and (2) liberal use of swear words. Those slides, sans swearing, became my inspiration in creating the slides we used in our thesis presentations. Those slides were slides done right: eye-catching without being distracting, simple and yet complimenting the speaker in his points. I can say the same for the slides of Bryan Veloso. Maybe, it’s a GitHub thing?


Hey! Did you hear? PyCon is happening in the Philippines! And it happens right at my alma mater, UP Diliman (at the UP Alumni Engineers’ Centennial Hall’s Accenture Ideas Exchange Room—a.k.a. Lecture Hall), from June 30 to July 1. That’s a weekend so I have no problems with it conflicting with my work schedule.

But…registration, with the cheapest being at PhP 295, most expensive at PhP 695, requires the use of PayPal, which requires a credit card. I don’t have a credit card and, right now, I really don’t have any plans of getting one. That sucks.

Although right now, at the PhPUG Facebook Group, there are requests of having payment done through GCash. That, I think, is a better idea since how do you honestly expect students to turn up if registration requires a credit card?

I keep my fingers crossed.

EDIT (06/10/12): Update from the PhPUG Facebook group: It seems that a GCash option will be rolling out on Monday (tomorrow). Yeah!

EDIT (06/11/12): And yes, finally, PyCon Philippines is accepting GCash (since yesterday, late update :P). They also seem to support over-the-counter payments. Don’t know how that one goes though.