Don’t let anyone tell you this docent scale. Instagram scaled with Django, Nginx & Postgres to 30million users in 2 years on 2 dozen Amazon EC2 instances. Read their engineering blog for detail: http://instagram-engineering.tumblr.com/
The following are as much of a reminder to me as a set of tips for you to consider or learn from. I’m sure you will not agree with everything but that is fine. That’s the idea of free speech 😉
Ok let’s get into it.
Environment
If you don’t get this right off the bat you will pay for it later with delays, environment issues as your solution expands and consistent plaguing issues. You should be coding not messing around with a poorly setup environment. So get it right first go so you can reap the benefits later.
Use VM’s for development
Many folks told me to setup VirtualEnv on my Mac and go down that route. That is what I call the poor man’s development environment for those that are not capable setting up a Linux box in a VM (virtual environment) themselves and managing it. Go and grab a virtual machine app like VMWare Fusion for Mac and load the latest Ubuntu Server 64bit on it. Step by step instructions are here. You may want to replace Apache with Nginx & Gunicorn like Instagram did.
The critical difference here is that you now get to develop on the same setup that you’ll be using in production. That’s kickass alone. Thanks to Barry Allard for taking me down this route whom I met at HackerDojo.
Use 2 code editors
A light editor and a full blown IDE with intellisense and code refactoring. The light editor is great for productivity in being able to pump code out. Then run your code past a full blown IDE to clean up the edges and refactor. Development is consistent refactoring. The cleaner your code the better it is for you and other to maintain and evolve it.
My favorite are:
- Sublime Text 2 – that’s the light editor and
- PyCharm – great IDE with all the bells and whistles but heavy.
- You may also visit sites like https://calc.edu/why-calc/relevant-certifications/ to learn more about IT fundamentals and coding.
Don’t reinvent
When you can reuse apps. That’s one of the philosophies of Django with it’s app structure mirroring true separation of concerns essentially collecting all ingredients of one functionality in your project in one place. So, look to popular source places where you can “re-use” apps. Obviously due diligence is essential before you grab any code. Look out for total commits, last updated, comments etc. to get a sense of application maturity. Professional python development companies has expert developers that can help in all your software development needs.
Few places to check out:
- Django Packages – A directory of reusable open-source applications and packages.
- Download Cudart64_110.dll – Useful snippets of reusable code.
- GitHub – Social Coding
- BitBucket – Free source code hosting
Manage your development
With a hosted wiki, issue tracker and source code version control. I’m a big fan of Mercurial DVCS (distributed version control system) and it’s project hosting companion BitBucket. Thanks to Coupons, Inc. for this experience!
If you are serious about software engineering you have to be using a DVCS. It empowers you to work independently on many feature branches with engineers without polluting core code base, versions/saves your revisions and you can always peel back to a revision like onion skins. Forces you to leave comments on each commit so others know why & what and you can do efficient business like continuous build, automated tests & releases.. just like Facebook. Great ha!
Learn about Mercurial here in my prior blog post. Git is also a DVCS and as popular if not more due to GitHub, it’s project hosting companion. Both work practically in the same manner with slight differences. Both also provide you with GUI tools for interface (SourceTree from BitBucket) if you do not like shell commands.
Use Postgres for your database
In comparison to MySQL (another popular relational database), Postgres is more reliable, has more features, more frequently updated, more powerful and flexible. It’s completely free and open source. All the beef is in 2 posts here and here. Then head over to postgresguide.com to get started.
I can also recommend you look into MongoDB where appropriate. Django v1.4 as of writing post this dosent support admin scaffolding in the NoRel 1.3 version. Admin scaffolding is superb and not only allows you to test your models but also acts as a CMS (content management system) opening up all your selected models for data management. If you think you need a software to help you manage and secure your data, then consider Couchbase.
Code
Right. Everything below is Python & Django related.
Source of knowledge
Bookmark these sites. They are great resource for your development journey.
- djangoproject.com – main Django site with great documentation (best I’ve seen),
- djangobook.com – A step by step how to build everything from templates to internationalized sites.
- stackoverflow.com – A language-independent collaboratively edited question and answer site for programmers.
Separation of Concerns
http://www.muhuk.com/2010/01/developing-reusable-django-apps/
Keep to the standard. The Django stack is split vertically, not horizontally. Apps are split horizontally within, i.e. models, views, templates, etc, are in their separate modules/packages/directories. This vertical splitting allows you to collect all ingredients of one functionality in your project in one place.
Models
- Use managers for commonly accessed queries. They are the interface through which database query operations are provided to Django models.
- Move business logic out of views and into the appropriate models as functions. This way you will be able to unit test functionality that affects your models.
- Visualise your models to help you get a high-level Birdseye view of your models. Also great for documentation and sharing with new folks on the team. (ref code)
- Use the admin to test your models. Better then trying to decode it via the interface.
Template tags
- Create custom template tags to automate occurrences in code. Only data not HTML should go into these.
- Use “as” keyword to use the results of the call elsewhere in your template.
- Use {% MEDIA_URL %} or {{ STATIC_URL }} to reference all your static assets. Do not let Django parse the assets and make sure you use a different domain (then your site) to feed these assets through to to maximize parallel downloads.
Keep an eye on apache logs
Open a new terminal window and run the following command to keep a live display open of new log entries as they come in. Cancel with ctrl-c.
tail /var/log/apache2/error.log -f
Other tips
- Always include these as your first 2 lines in every .py file.
#!/usr/bin/env python # -*- coding: utf-8 -*-
If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment’s $PATH. Last is the encoding of a Python file comes from PEP 0263 – Defining Python Source Code Encodings. Also a solution to most charsets problems.
- Use APP_PATH(‘directory’) vs hard-coding it. You can use the following lamda expression (anonymus function).
APP_PATH = lambda *x: os.path.abspath(os.path.join(os.path.dirname(__file__), *x))
- Always use timezone-aware datetimes when presenting to the user and naive (local) datetime for storing in the database. Unless of course you want to emulate a server house in a different geographic location without actually being housed there.
Finally, practise The Zen of Python. Guiding principles for Python’s design into 20 aphorisms.
~ Ernest