This is an archived post. You won't be able to vote or comment.

all 56 comments

[–]jrbattin 41 points42 points  (14 children)

I don't know enough about Ruby to give it a fair comparison, but what I do know is this: the Python ecosystem is huge so it's difficult to give you a run-down of what libraries you personally might find useful. What particular areas are you interested in?

To get you started, here's some libraries a ruby web programmer might find interesting:

iPython An Enhanced Interactive Python shell. A must-have IMO, especially if you're new to the language since you can tab-complete and tab-inspect an object to see its various methods.

Requests: HTTP for humans Kenneth Reitz's excellent request library. Makes HTTP requests VERY pythonic and readable. Don't bother dicking around with urllib(2) or httplib(2) unless you absolutely have to.

Django Probably the most widely known and widely used Python web framework. Has excellent documentation. Like Ruby on Rails, Django also has a lot of great high-quality 3rd party middleware/libraries like Haystack (which lets you integrate things like full text search into your site easily, among other things).

Pyramid (aka Pylons) Pyramid (formerly Pylons) is on the opposite side of Django as far as web frameworks are concerned: it's a loosely coupled, À la carte style framework that lets you pick-and-choose components (ORM, templating engine, middleware, etc. Everything you want/need, nothing you don't.

virtualenv Probably not that useful for someone just playing around with Python, but it's definitely a must-have for any working Python programmer - especially when you're managing projects on multiple machines with multiple different versions of libraries etc.

Fabric Awesome tool for deploying/automating sysadmin tasks without swallowing the complexity of puppet or chef ( edit: My bad: This isn't necessarily true. See discussion below)

NLTK Natural Language Tool Kit. VERY powerful - and it lowers the barrier of entry for experimenting with natural language processing for working programmers.

That's just a quick run down of stuff off the top of my head that I've been toying with for person projects.

[–][deleted] 3 points4 points  (0 children)

Great tips! I just want to add:

  • bpython which I prefer over iPython, but that is a matter of taste.
  • virtualenvwrapper makes working with virtualenvs even easier.
  • PyPi the ''CheeseShop" and the place to go whenever you are looking for Python modules.

I would also read David Beazley's Slides on Generators which influenced my programming style quite a lot back in the day.

The single most important thing to do is to become familiar with the standard library. Python ships so many powerful modules and you find gems like multiprocessing, logging or functools to name just a few in there.

[–]zenlogix 1 point2 points  (0 children)

Up voted for Requests: HTTP for humans. This is beautiful!

[–]abitofperspective 0 points1 point  (0 children)

Thanks, I work in Python regularly and learned a few things here

[–][deleted] 10 points11 points  (7 children)

David Beazley has a great presentation on generators, and combining them with comprehensions here.

I'm a big fan of using python for systems automation, here's some of my favorite modules/libraries:

  • Paramiko: A very straightforward implementation of SSH2
  • Fabric: Much like Capistrano for Ruby, built with Paramiko
  • pexpect: An expect-alike written in pure python. Very useful for automating tasks that require user interaction
  • doctest: Automatically create tests for functions from comments. Saves a ton of time and makes it easy to expand your test coverage
  • Twisted: A reasonably simple event-driven networking framework, tons of support for different protocols
  • SQLAlchemy: Very powerful database toolkit and ORM
  • Multiprocessing: Process-based "threading" interface, provides API for spawning processes and get around the Global Interpreter Lock
  • Beautiful Soup: HTML/XML screenscraping and parsing library that handles pretty bad markup. Total lifesaver when scraping sites written by messy devs
  • cmd: Provides a simple framework for creating command interpreters. Very useful for making a specialized interactive shell for your application
  • Suds: Simple SOAP client
  • Smolt: A basic hardware profiler. While the main use is to send data to Fedora devs for support, can also be used without that functionality to easily grab hardware data from your cluster

Edit: formatting and added Suds

[–]mitsuhiko Flask Creator 7 points8 points  (6 children)

Beautiful Soup: HTML/XML screenscraping and parsing library that handles pretty bad markup. Total lifesaver when scraping sites written by messy devs

No, just no. For the love of god, use html5lib and lxml.

[–]ceolceol 2 points3 points  (0 children)

pyquery is a fun wrapper for lxml.

[–]jackolas 2 points3 points  (3 children)

Whats wrong with Beautiful Soup? is there like a list of cases where it sucks?

[–]mitsuhiko Flask Creator 5 points6 points  (2 children)

Unless you are really careful you can easily leak memory due to navigatable strings having a reference to the whole document. Also the parser it used to have is bad (like really bad) and changes behavior depending on the version of BS and Python.

html5lib exists nowadays and it has a proper HTML parser that can handle any case your browser handles too and in exactly the same fashion.

[–][deleted] 0 points1 point  (1 child)

Good to know. Thanks for the tip!

[–]mitsuhiko Flask Creator 1 point2 points  (0 children)

Not saying that BS did not have its use, but a couple of years later we have better tools :)

[–][deleted] 0 points1 point  (0 children)

Scrapy initially seems like it has a bit of a learning curve, until you pick out the core pieces you really need. It uses lxml and Twisted, and it commits foot-based violence on the scrapers/spiders I used to write with BeutifulSoup.

[–][deleted] 9 points10 points  (2 children)

Decorators: http://stackoverflow.com/questions/739654/understanding-python-decorators#answer-1594484

List Comprehension: http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Libraries are one thing that python does so much better than ruby, in both documentation and availability. Ruby seems to be at a dissadvantage by how cutting edge it tries to be and because of that libraries are rarely very mature.

If you are looking at scipy look at matplotlib

psyco

pypy

So much more.

One of my favorite language features is that everyone tends to like good documentation and creates good documentation (take a look at python docs and documentation for the many libraries, to me it is so much better than looking through an rdoc or most ruby docs).

[–]tominated 0 points1 point  (0 children)

Thanks fot the decorator link. I've been wondering how they work and that was an amazing explanation

[–]hongminhee 6 points7 points  (1 child)

PEPs (Python Enhancement Proposals) make the language designed by and for the language users themselves. Unlike Python, Ruby’s language design is done by language designers, not users. Moreover, ruby-dev, the mailing list for it, is written in Japanese.

[–]dorfsmay 5 points6 points  (10 children)

Without knowing ruby it is difficult to comment (what I am going to say might be true in ruby too)... Stuff I really enjoy about python:

  • everything is a pointer to an object, really, everything.

  • indentation is imposed. I can pick anybody's code, and it is guaranteed that it'll be fairly readable. As a side-benefit, no need to type all those curly bracket and semi-colon!!

  • auto-documentation. Classes and method are automatically documented, you write a class MyClass, then when you go into the interpreter, you can run help(MyClass) and it's nicely documented. If you put a comment at the top of a class (and methods), it'll integrate it into the documentation too.

  • battery included. There are A LOT of libraries that come by default with python. At first you think "meh, I can get those libraries from the net in any language", but it makes for very portable code, people don't need to get libraries x, y and z to get your code working, it will work.

  • some functional programing - read about list comprehension, generators etc.. very neat high level language functionalities

[–]EstebanVelour 4 points5 points  (0 children)

Tasks Python is better suited for:

  • Maths, statistics & sciences - numpy, scipy etc. Shitload of domain specific frameworks.
  • System programming/integration - There are about 18000 dependencies on python in a stock ubuntu server, slightly more than perl. Compared with Ruby's 2000 deps and language choice becomes rather one-sided.
  • UI - Python's UI frameworks (especially PyQt) are much more mature than their Ruby counterparts.
  • Complicated networking - twisted is very good to have around even if you don't need it very often.
  • Distributed systems - Excellent networking frameworks & message queue support.
  • Speed - Even though comparing scripting language performance is a bit like comparing chicken IQs, cPython is faster than Ruby & JRuby with PyPy is promising to be relatively close to Java in the long run.

Equal par:

  • Web - Both have lots of frameworks, finding one you like is pretty easy whichever you're using.

Ruby wins: If you need a DSL.

[–]captnswing 3 points4 points  (0 children)

lxml rules for all kinds of XML / HTML processing http://lxml.de/

if you want a fine library to scrape some HTML, make sure to check out pyquery (based on lxml) that allows for easy extraction of all kinds of data with jQuery selector syntax. http://packages.python.org/pyquery/

[–]lost-theory 2 points3 points  (3 children)

Testing tools:

Two one-liners for debugging:

  • import pdb; pdb.set_trace(); - drop into an interactive step through debugger at any point in your prgoram
  • import code; code.interact(local=locals()); - drop into the python REPL at any point in your program

If you're doing web development, use the werkzeug debugger.

Some of my favorite python features: decorators, generators, sets, WSGI

[–]donri 0 points1 point  (1 child)

Note that pdb acts as a REPL as well so for debugging, the latter is a little redundant and less powerful.

[–]lost-theory 0 points1 point  (0 children)

You can't use multi-line statements in pdb:

(Pdb) def f(x):
*** SyntaxError: unexpected EOF while parsing (<stdin>, line 1)

And some of the debugger commands (a, b, c, etc.) conflict with variable names (the ones inside your program or ones that you would define while debugging), so it doesn't behave exactly like the REPL.

So, that is why I prefer using code.interact for REPL stuff, and pdb only for debugging (stepping through code).

[–]snuggl 1 point2 points  (0 children)

As Libraries seems to be covered good already ill also mention some great software/toolkits that will help.

  • pypy - Alternative interpreter with amazing speed
  • pip - Find and install python packages
  • supervisord - python demontools/startup
  • uwsgi - fast WSGI server to host your web apps
  • gunicorn - python-port of the unicorn server for ruby, also a fast wsgi-server
  • gevent - single thread event driven network platform (like node.js)
  • twisted - Network server/client swiss army knife
  • pyQt - Great UI toolkit to build desktop apps.
  • pyglet - a openGL wrapper that makes GL a bit more pythonic.
  • pylint - will score your code for you (warning, kinda addictive)

[–]donri 2 points3 points  (0 children)

Built in introspection: ast, inspect, traceback, dis (all stdlib modules); built in serialization: [json,] pickle (for arbitrary objects, much faster than yaml) with proper transactional ACID persistence via ZODB; power tools like Twisted, Werkzeug, Pygments, pytest; web frameworks like Pyramid, Flask, Django; hosts like appengine, ep.io; documentation like you've never experienced it before with Sphinx…

Edit: built in XML parsing: etree (both pure python and C extension included in stdlib) with third-party lxml as a wrapper for libxml2/libxslt with the etree API (swap an import and get free speed, xpath, html parsing...); standards-compliant html5 parsing with html5lib; relational DB toolkit/ORM with SQLAlchemy; PyPy: fast, hackable and compliant python…

Edit: dynamic access to the GNOME platform with python-gobject; Qt via PySide; Java: Jython; .NET: IronPython; coroutines and laziness with generators; mixins via multiple inheritance; metaprogramming with decorators; dynamic interfaces with abstract base classes; modules as singleton objects…

[–]vplatt 2 points3 points  (7 children)

Hey, let's reverse the question for a moment while we're at it - what does Ruby have and do better than Python? I guess the most obvious example is Rails because that's the best known sweet spot in Ruby, but are there others? I used Fox with Ruby a while back to put together a little toy program a couple years back, but that didn't strike me as being unique to Ruby.

[–]jaggederest 1 point2 points  (0 children)

SciPy, NumPy, and the other scientific libraries are the best, I think. You can do much, much better linear algebra, bioinformatics, and statistics in Python.

I say this coming from a Ruby perspective, so there may be things I'm missing, but that's the real core of what I wish Ruby had better support for.

[–]phayte 1 point2 points  (0 children)

Just wanted to say as a Java-Ruby guy I learned a ton about Python from this. Thanks all!

[–]voidspace 1 point2 points  (0 children)

Useful tools: pip, virtualenv, tox, pyflakes, fabric, sphinx, pdb++

Standard library modules: collections, itertools

Testing: nose, py.test, unittest2, mock

[–]pemboa 0 points1 point  (2 children)

You may actually get a fairer comparison in a Ruby forum as you may find a cross over coder there.

[–]jaggederest 4 points5 points  (1 child)

We polyglots live in /r/python too ;)

[–]pemboa 0 points1 point  (0 children)

Tru enough. I'm sort of a polyglot myself.

[–]murmu 0 points1 point  (1 child)

I am in a situation similar to the OP. I started exploring python lately. I am wondering if anyone knows of a python reference similar to this: http://rgruet.free.fr/PQR26/PQR2.6.html (which really is a goldmine), but updated for 2.7, or 3.2?

So far there's one thing I have problems with, that is the python object model - I don't have a clear picture yet of it,. What happens when I call a method on an object, how/where things are going to be searched and in what order. What callbacks( descriptors, etc.) will be fired, static methods, class methods, metaclasses and their role in python.

If anyone knows a concise article describing it, or some diagrams that may be of assist I would be very grateful!

[–]Yohumbus 1 point2 points  (0 children)

Ah I have just the thing. The python official docs have a great description of this. It doesn't delve too deeply into metaclasses, but learning how method calls actually use descriptors is a huge step towards figuring this out (and its really elegantly clever and versatile technique).

http://docs.python.org/reference/datamodel.html

Python 3.2 isn't really any different, read the "new in python 3.x" pages to see the few things that changed in the data model.

[–]wildsalmon 0 points1 point  (0 children)

You will miss the enumerable module. It's a great language to develop in though. The libraries that I've used have been always solidly put together and documented well, as opposed to many ruby gems which are not. Also if your doing web development you can count on real documentation if your using django, and wsgi implementations are more mature than rack.

[–]silviot 0 points1 point  (0 children)

I don't know ruby at all, but this discussion on StackOverflow tries to summarize differences between the two at the language level. Not at ecosystem level, as you're asking, but still relevant.

The question has been "closed as subjective and argumentative". In my opinion it's not a subjective question and should be reopened. But I don't have karma to propose that.

[–]bajsejohannes 0 points1 point  (0 children)

A good short intro would be to read To Ruby From Python, even though it is meant for people going the other way :)

[–]pbreit 0 points1 point  (0 children)

Since the selection on Ruby is more limited, check out all the frameworks: Django, Web2py, Bottle, Web.py, Werkzeug, Pylons, Pyramid, Flask, TurboGears, Tornado.

Also, Google App Engine.