Posts tagged python
Integrating Mongoengine into Pylons is really quick and painless if you know where to put stuff.
Add your DB settings to your development.ini file
mongodb.host = host[,replHost2]
mongodb.port = port
mongodb.db = db_name
mongodb.username = uname
mongodb.password = pass
Connect to MongoDB in config/environment.ini
At the top, include Mongoengine
from mongoengine import connect
And at the bottom, just before we return, include the following line:
# Connect with Mongoengine
connect(config['mongodb.db'], host=config['mongodb.host'], port=int(config['mongodb.port']), username=config['mongodb.username'], password=config['mongodb.password'])
Add your models
Now we can create our models just like in the Mongoengine documentation.
Over the last 5 months, I've had the opportunity to develop web-apps using 5 different Python web frameworks. Those frameworks are Django, Pylons, Turbogears, Plone/Zope and web.py. Each has their own flavor, and could be useful for different projects. If you're thinking about starting a web project, and you already know the virtues of Python*, then it's good to know the pros and cons of some of the major frameworks.
In short, Django is a really phenomenal framework. Sorry to jump on the bandwagon, but getting up to speed was really painless. On the other hand, I didn't feel like the structure of things was going to seriously hamper expansion/scalability anytime in the very near future. It's a MVC (technically a MVT) framework, like most modern web frameworks out there. This keeps things nice, neat and organized, so long as you keep with convention. Basically, they start you out with a bunch of "buckets" and say "ok, your data layer goes here, your logic goes here, and your presentation layer goes here." You don't have to follow suite, but that is why you're using a framework, right? I used Django for the back-end at PeoplePatcher.
Pros: Easily the most popular Python web framework. This is great if you want to outsource development, or get community help on an issue you're having. The pros over at #django on freenode.net (irc client required) are a very friendly and helpful bunch. Anybody that says community size/quality is over-rated hasn't run into enough difficult problems yet!
The framework itself fits it's motto "For perfectionists with deadlines." There is a huge library of community-built "apps" that you can easily toss into your application to extend its functionality. The code's flow is logical and simple, and keeps tight scopes on everything. An answer to the question "why" is almost never more than one jump away. Gone are the days of tracing function calls through 9 files to find a root cause. Development is fast and painless.
Cons: I question how well the system will scale if your site really explodes with growth, but honestly, you're not there yet, and if you do get there it's a damn good problem to have. I'd honestly rather have a better templating system such as Mako or Genshi. That's sort of the Achilles Heel of Django, but it's not a deal-breaker.
Pylons feels the up-and-comer that's aiming to do things right. It has nowhere near the community support that Django has, nor the library/"app" support. It's going to take you longer to develop an app using Pylons, but my feeling is that it is more of an "industrial-strength" framework for when you want to build an app that's rock-solid. The cost is that you'll be writing a lot more code yourself, but it's going to be built on top of a great foundation. The Pylons framework has a very dedicated core team that is constantly keeping the platform on the cutting edge. It's full-stack, and they unapologetically keep the components used current and relevant. I have no doubt that they will be the first to incorporate newer, better stack components as they stabilize and become production-ready (such as Tornado or Twisted). This is what I used to develop the back-end to Socialgraph.
Pros: Cutting-edge, using the latest open-source components, philosophies, and techniques. It just hit v1.0 recently, and is a rock-solid choice for production development. If you have a big, mean, complicated web-app that you want control over, Pylons is the perfect foundation to build it on top of.
Cons: Prepare to write more code yourself. It has a lot less structure for plugins/apps and copy-paste than Django, so somebody else's solution may take a lot more to adapt to yours. After all, you're building the application-level framework yourself. If you're not afraid to get your hands messy with code and fix your own problems, I feel like Pylons is going to afford you a longer run-way than Django for really gnarly web-apps.
Turbogears has the philosophy that they'll take the best of open web frameworks and components and stick them together. It's a similar philosophy to Pylons, but it feels a little less polished. On the other hand, they're not afraid to provide more functionality than Pylons, which is a plus and a minus. I feel like it's a solid attempt, but it's not really filling a market need. Need a ready-to-rock framework? Why would you pick Turbogears over Django? It's a false sense of accompishment. It's basically Pylons, with even more functionality, but no game-plan as to how to go any further. You're starting off at the same point as Django, but without any community support or pre-written apps. If you really needed the degree of customization offered by Pylons/Turbogears to begin with, why not just start with Pylons, and add in what you need. Turbogears has a lot of potential, but it's not there yet for production work.
Pros: Super-simple to get a basic web-app up and running. Clean MVC delineation.
Cons: Feels like a "me too" approach to Pylons, but with more stuff, and less production focus. It delivers on what it promises, but the question is "do you need what it delivers?" Probably not. If you want batteries included, go with Django. If you want a production-ready, easy-to-use stack, go with Pylons. Turbogears is still a niche framework, that I wouldn't really recommend for when you just want to take care of business needs.
web.py is the bare-bones of the web frameworks, and that's how it likes it. It doesn't waste your time with any cruft, or MVC, or telling you how to write your damn apps. In all reality, it's little more than simple hooks into a web-server that you can tie into your Python program. This framework is not for the faint of heart. If you don't know what you're doing, it will let you shoot yourself in the foot, and laugh in your face about it. On the other hand, not all web-apps fit nicely into the MVC paradigm. What if you're building a web-server that doesn't even have a presentation layer? web.py is great for web-enabling your own code, without fitting your square peg into the round hole of traditional web-app design. I used this heavily when I was working with Oyster.com.
Pros: Fast, light-weight, and simple. You don't have to read a ton of documentation in order to understand how the whole system works. You can basically start writing your program your way, and every time you want some web or DB functionality, you can do a quick search in the web.py documentation for how to hook it in.
Cons: It will let you develop yourself a flaming pile of shit, and it certainly won't be web.py's fault. If you don't have software design experience, this is not the framework for you. Another note is that there are other frameworks out there that take a similar philosophy, that are a bit more modern, and higher performance (ie, Tornado or Twisted).
Depends upon your size/ambition.
- Quick up-and-running? Django. I'd recommend this to 80% of people out there. It will have you running in no time, and will allow you to scale out functionality quickly, and with minimal effort.
- Serious web-app? Look into Pylons. It will grow with you better than Django, but you're going to have to put more effort into getting there. Still a well-designed system, and it makes doing a quality job easy.
- Hardcore, mean and nasty platform? Need some serious firepower and your own architecture? Check out a low-touch framework like web.py, Tornado, or Twisted. Be prepared to roll up your sleeves and put some serious blood, sweat, and tears into your new project. I wouldn't recommend this for a part-time project unless you have a long development timeline in mind.
A great list of a ton more Python web frameworks is kept up-to-date on the official Python website.
*note: No knock to Ruby and RoR there, they're just not my thing.
I had a mean streak of motivation in me this weekend, and cranked out a quick web-app, start-to-finish.
It's called PeoplePatcher, and the premise is that it allows you to conference call two of your friends together, whether or not they want to talk with each other. It's perfect for getting irritated friends or a feuding couple speaking again.
All you do is give the two numbers that need to be connected, and PeoplePatcher takes care of the rest. The friends will never know who requested the call for them, they will both just get calls that toss them into a phone conference with each other.
In case you're wondering, the web-app was built on top of Django (with a little jQuery sprinkled on for flavor), and the phone integration is courtesy of Twilio.
Go check it out and patch some relationships up!
Socialgraph is an open-source project I just launched over the weekend that aims to be a server-to-client relationship visualization platform.
Rather than me trying to explain the whole thing, a picture is worth a thousand words, and an interactive demo is worth a million.
Seriously. Check it out.
Architecture vs Agile
C++ can be really quite useful and fast for developing architecture-oriented software with clear interfaces. It is not good for on the fly tweaks and experimentation. New functionality requires new layers, from an object perspective. This is not always trivial. The rigid nature of the language also seems to encourage rotten code. Simple changes require significant effort to do right, or….. you could just hack a few lines and get it "good enough." Guess what happens when you're under the gun, time-wise?
Python can get a bit more chaotic if you're a sloppy coder. It enforces far less than strongly casted languages. On the flip-side, if you are disciplined enough to keep your code well-organized, it offers orders of magnitude more flexibility. It's so easy to do things "the right way" that it becomes the default. Have to rip out a class and refactor it into thirds? 20 minutes and done. Problem solved, and the code evolves with your vision of the system, rather than turning into a stagnant reminder of the vision from a year ago.
Yes, I know, Python's been around forever. I've even dabbled with it a bit over the last few years. To be honest though, I never really gave it a fair chance. By that, I mean I had never used it to tackle a large enough problem that it's ease-of-use would shine.
I've been working on a dynamic content generator for Oyster.com over the last two weeks, and Python has shined in spades. From database access, to string manipulation, to error handling, Python is a just plain better experience than C++.
Even though I knew higher level languages, C/C++ (yes, I realize they're different) have been by languages of choice for years. They were lean, fast, and powerful in the hands of a skilled coder. It doesn't help that my professional background is mostly embedded. When I moved to the web world with Oyster.com, our backend servers were built in C++. Quite a nice transition to me. Different domain, but familiar tools. Now that I'm getting quite comfortable with the world of web requests, database queries, and the like, C++ seems a bit archaic.
As a die-hard lean-coding nut, C++ still has a place in my heart, but I view it now more as a special case tool, no longer my default go-to. I'm hooked on interpreted languages, and love that I can get programming tasks finished in literally one-fifth the time, and with a more stable result. Handling all of the nuts and bolts, ala C++, is really asking for trouble once the string and pointer handling get tricky. It's not always the languages fault, but the fact that so much control is available make premature optimization quite tempting on a first pass write.
With Python there's plenty of room to chose best-case algorithms, but the simple fact that it's an interpretted language pretty much screams "Use our libraries first! Your version of a built-in will probably suck!" Problem solved. It's no longer a free-form clay sculpture, but something more resembling Legos. That's a good thing when you want to get things done fast.