Slashdot Mirror


TurboGears: Python on Rails?

gcantallopsr writes "If you liked Ruby on Rails and its 15m intro video (.mov) you will probably like TurboGears and its 20 minute wiki tutorial. (.mov) It shows you the development of a simple wiki in just 20 minutes, and there is a text version of the tutorial. TurboGears uses Python, SQLObject, CherryPy, Kid, MochiKit and some extra pythonic glue to help you to (in their own words) 'Create a database-driven, ready-to-extend application in minutes. All with designer friendly templates, easy AJAX on the browser side and on the server side, not a single SQL query in sight with code that is as natural as writing a function.'"

28 of 279 comments (clear)

  1. 20 mins vs 15 mins!! by VC · · Score: 3, Funny

    How do the python crowd expect to get taken seriously when implementing a wiki takes an whopping 125% as long in python as in ruby!!!!!!????

    (oh wait, they did ajax as well.. ;-)

  2. no sql? by ambilio · · Score: 3, Insightful
    ...not a single SQL query in sight...

    Why is this an advantage?

    1. Re:no sql? by quigonn · · Score: 5, Informative

      It shows a high level of abstraction when you access the DB by simply loading/persisting objects instead of having to handle queries, result sets, records and all the other low-level stuff. And abstraction (in this case) is good, as it helps the developer concentrate on the relevant parts of the program.

      --
      A monkey is doing the real work for me.
    2. Re:no sql? by wootest · · Score: 5, Informative

      Other people have answered in-depth, but the short answer is:

      a) You do not need to worry about which vendor's dialect of SQL syntax you're using - provided you know how to create and populate the tables in any database system, you can switch at the drop of a hat if you need or want to.

      b) Provided the layers are stable, it protects you from SQL query injections. The abstraction layer does the escaping for you.

      c) Abstracted queries makes queries 'just another function/method call', and you get ordinary data structures back. This in combination with a) and b) and a competent framework (Rails, Django, TurboGears, Cake, Trax, WebObjects) makes coding much quicker as you don't have to keep the semantics of SQL and your database in mind - just the model itself.

      There are *many* nuances to this, but the above three are some of the most pertinent ones. Peruse the other comments if you want to get in-depth.

    3. Re:no sql? by mixmasterjake · · Score: 4, Informative

      Persistance layers are cool for a lot of reasons. Though, it definitely takes some getting used to for those of us who have developed a lot of SQL driven applications.

      Don't let anyone fool you into thinking that a persistance layer will be less development work for you - I have found this to be untrue. I can use automated tools to get myself 80% of the way there. For anything substantial, though, it always seem to wind up being more work as I figure out how to configure & trick the persistance layer into giving me my data in the most efficient way. This can be frustrating when you know how to accomplish the same thing in 5 seconds using plain SQL. Maybe it's just me?

      But, if you do manage to get over the hump, the benefit is that your business logic layer is very clean with no DB code whatsoever. If you use it properly, you can get 100% separation between these layers.

      If you're using a strongly typed language, you get the added bonus of compile-time checks for certain illegal assigment errors. You don't have to fumble around with things like converting dates into SQL. You don't have to check for SQL injection. Lots of other little things.

      --
      TODO: come up with a clever sig
    4. Re:no sql? by holle2 · · Score: 3, Insightful
      It shows a high level of abstraction when you access the DB by simply loading/persisting objects [...]

      Last time we used an object-to-sql mapper was quite some time ago, so my info might be outdated:

      We attempted to create objects (in Python) to store a lot of attributes ( > 30 ). The design explicitly asked for a "flat" database.
      All this happened inside Zope/Plone, so we tried out the Archetypes, which come with a hand attributes (or better PropertySheet) to SQL mapper. But the code was that ugly, it created a single SQL insert/update statement for each property (attribute) even if it did not change.
      This resulted in an extremely long running "save" operation that you could simply throw the code out of the project ... . The data is now stored in the Zopes own ZODB and gets archived after a while in a SQL DB.
      For archiving purposes a hand-crafted SQL statement is used that runs lightning fast :-) .

      On the other hand, I have seen Java code querying an SQL DB using the Oracles TopLink product to abstract the underlying database. The code was so gross, you simply felt the urge to go and wash yourself after looking at it.

      The result of this experience is:
      • If you want to concentrate on your object oriented approach, stick to databases that can handle objects, like the ZODB (usable w/o Zope if you like).
      • If you have to access mass-data from legacy systems (like e.g. master product data), you have to program SQL, since nothing is faster than that. Make sure the results can be converted into something like a list of dictionaries so handling of the results is more natural in an object oriented manner.
      • Every good programmer should be able to switch between programming languages and be able to access data stored in legacy systems as well. Sometimes the data comes only via an ODBC connector from an old AS/400, but if that is the source of the master data, you have to live with it.
      • SQL databases are not the primary choice to store objects and/or tree like structures. They were designed it the "good ole' times" when everybody though that relational databases were cool. Sometimes it can be much more useful to store a tree in an object persistent storage and put the node data into an SQL DB (for performance).
      • For debugging: If you use SQL code in your project you can actually print out the statements before they get send over to the database so you can simply copy them from your logfile (you have a logfile, do you ?) and run them manually to see if they produce the outcome you want. This is often more useful than enabling query logging on the server since digging through thousands of lines of log-messages can be rather time consuming.
      • For designing: You can build your queries step by step in a "Query Analyzer" using some example data and then embed the results into your code (including the exchange of fixed teststring with variable names).
      • For performance: You might know that you have to run a query 3000 times in a row with different variables (e.g. insert into ...) but you object mapper does not know that fact. Thus you can use one prepared SQL statement and fill in the variables upon execution. This saves a hell lot of time (depending on your database system, some systems are slow either way).


      Ok, I got my flame shield up, let 'em come ...
  3. My Experience by HogynCymraeg · · Score: 3, Informative

    I've used Python/SQLObject/CherryPy on a project before. It's very quick to code something useful. SQLObject will change the way you think about how you integrate DBs to web applications. All in all, it's well worth checking out.

  4. Video software by The+OPTiCIAN · · Score: 3, Insightful

    What software do people use for making these neat videos? (I realise this is bordering on off-topic :) )

    --


    Believe with me, my saplings.
    1. Re:Video software by jrockway · · Score: 3, Informative

      Since it was made on a mac, my guess is Snapz Pro X.

      http://www.ambrosiasw.com/utilities/snapzprox/

      --
      My other car is first.
    2. Re:Video software by mykdavies · · Score: 4, Informative

      What software do people use for making these neat videos?

      vnc2swf

      And to bring us (nearly) back on topic, the latest version is written in python!

      --
      The world has changed and we all have become metal men.
  5. SQLObject rocks! by SimHacker · · Score: 5, Informative

    One of the nicest features of SQLObject is that it insulates you from the peculiararities of the database's SQL syntax, so you don't need to put any SQL code directly into the Python code (but you can if you need to for efficiency or if you're willing to write non-portable code).

    The SQL database abstraction layer is an important feature of SQLObject, that Ruby on Rails doesn't currently support -- you have to write database dependent SQL code mixed in with your Ruby code.

    SQLObject lets you write generic SQL queries with normal Pythonic expressions and operators, which are automatically translated into the database dependent SQL syntax by the database driver. So you don't have to change any of your Python code to port it to a different database, and you don't have to mix together two different notations, or quote a bunch of SQL strings in your Python code. It's a much more "pythonic" way of database programming than raw SQL.

    The great thing is that it's so convenient and the syntax is so simple, that you can use the interactive Python shell to browse and test out and edit your database. It's trivial enough to type in some Python code on the keyboard that loops over the results of a query, performs some complex logic, and validates and edits a bunch of rows in the database. Much more powerful and easier to use than anything you can do with raw SQL.

    -Don

    --
    Take a look and feel free: http://www.PieMenu.com
    1. Re:SQLObject rocks! by Osty · · Score: 3, Informative

      The SQL database abstraction layer is an important feature of SQLObject, that Ruby on Rails doesn't currently support -- you have to write database dependent SQL code mixed in with your Ruby code.

      There's your problem -- you're mixing SQL code in with your app code. Bad developer! Bad! You're introducing SQL injection holes (I don't care how well you think you're filtering your input, chances are you missed something, especially if you're trying to do so in a DBMS-agnostic way). While an abstraction layer like SQLObject may be better in that regard because it's main focus is interfacing with SQL and thus should be more focused on catching these kinds of bugs, if it's mixing in dynamic SQL queries there's a good chance it also has SQL injection holes.

      There's a reason why stored procedures are important, and it doesn't have anything to do with perceived performance increases from not having to re-parse the same SQL code every call (that's definitely a benefit, but it's minor). In this case, stored procedures shelter you from SQL injection attacks (assuming, of course, that your stored procedures aren't just dumb "take a string and 'exec' it as SQL code" queries). You may not agree with the people that tell you to use stored procedures to keep your business logic near the data, or that using stored procedures allows you to easily expose only the permissions required (GRANT EXECUTE on the stored proc, rather than having to figure out what tables need INSERT permissions, which ones need SELECT permissions, etc for each app that uses this database), but I don't see how you could argue their protection against injection. Yes, parameterized queries allow you to write dynamic SQL code with some level of injection protection, but that still leaves you with the downside of having SQL mixed into your code.

      It's trivial enough to type in some Python code on the keyboard that loops over the results of a query, performs some complex logic, and validates and edits a bunch of rows in the database. Much more powerful and easier to use than anything you can do with raw SQL.

      I call BS. First off, SQL is a set-based language. Very rarely do you need to loop over a result set (if you find yourself looping in SQL code, you're not thinking hard enough). Whatever "loop and operate" action you'd take with Python can be done quicker and more efficiently with SQL code than with app code. Second, for Python code to loop over a result set means it has to get that result set. If your database is not local to your web server (bad idea!), that means network I/O costs. You can and should avoid that cost by processing the data first at the SQL Server (hey, stored procedures again! Why return 100,000 rows that you're going to process down to 1,000 when you could just return the 1,000 processed rows in the first place? Why return anything at all when you're going to insert/update data based on what's in a different table?). Finally, while it may be "easier" for a developer proficient in Python and not the SQL dialect used by your chosen DBMS, that's a cop-out. As so many people are so fond of saying, you should use the right tool for the job. In this case, you're trying to use the hammer of Python to pound a screw, when you'd be better off with the screwdriver of SQL. Sure, you may be able to pound that screw into the wood, but it'd be quicker and easier to do it in SQL. Maybe you don't want to learn SQL, in which case you probably shouldn't be working with a database. You have a responsibility to learn how to use the tools you're going to apply to a problem.

      I've intentionally ignored the problem of database portability, because a) you should be using stored procedures, which means you'll want to port them yourself anyway for maximum benefit, b) you should be using a proper DBI layer such that you just have to tell it, "I'm using Oracle now instead of Postgres, do the right thing", and c) because you're using stored procedures, you won't be switching to a DBMS that doesn't support the (*cough*MySQL*cough*) (and yes, I know MySQL "will" support them, but that's still some way off in non-beta form).

    2. Re:SQLObject rocks! by Trejkaz · · Score: 4, Informative

      There's a reason why stored procedures are important, and it doesn't have anything to do with perceived performance increases from not having to re-parse the same SQL code every call (that's definitely a benefit, but it's minor). In this case, stored procedures shelter you from SQL injection attacks

      Whereas this is true, it's also true that using "?" parameters for any user-entered values can solve the SQL injection problem just as well.

      --
      Karma: It's all a bunch of tree-huggin' hippy crap!
    3. Re:SQLObject rocks! by njyoder · · Score: 3, Insightful

      if it's mixing in dynamic SQL queries there's a good chance it also has SQL injection holes.

      I'll take that "if" to mean you haven't read the documentation and didn't actually watch the video. Stop frothing at the mouth about SQL injection holes. If you had bothered to watch the fucking video (which you didn't), you'd notice it has specific mechanisms to deal with them.

      Also, its main purpose isn't to "catch bugs", it's to make things easier on the programmer. Abstraction toolkits like this are good. God forbid someone make things easier.

      I call BS. First off, SQL is a set-based language. Very rarely do you need to loop over a result set (if you find yourself looping in SQL code, you're not thinking hard enough). Whatever "loop and operate" action you'd take with Python can be done quicker and more efficiently with SQL code than with app code.

      And I call BS on your BS, because you didn't actually read what you were responding to. I'm beginning to notice a pattern here, first you start frothing at the mouth based on speculation from not having RTFV (video), then you do it for not reading the comment you're responding to. They specifically used the descriptive words "powerful" and "easier" not "faster." The issue isn't effeciency, no one is claiming that TurboGears produces the most effecient code. The whole purpose of TurboGears is to make things easier on the programmer, which is what this does. Designing the logic within python, which actually does allow you to do quite a bit more in terms of 'business logic', makes things easier.

      Finally, while it may be "easier" for a developer proficient in Python and not the SQL dialect used by your chosen DBMS, that's a cop-out. As so many people are so fond of saying, you should use the right tool for the job

      No, even for someone very proficient in SQL it's still easier in Python due to these bindings. Of course, not having actually read anything about TurboGears (e.g. the documentation) and engaging and rampant speculation and all, you wouldn't know that.

      And they ARE using the right tool for the right job, you should follow your own damn advice. TurboGears is designed for jobs where EASE is a priority over effeciency. GUESS WHAT, effeciency isn't always the #1 priority, genius.

      You speak boldly, but you can't read worth shit, excercise critical thinking skills nor even follow your own advice.

      I've intentionally ignored the problem of database portability, because a) you should be using stored procedures, which means you'll want to port them yourself anyway for maximum benefit, b) you should be using a proper DBI layer such that you just have to tell it, "I'm using Oracle now instead of Postgres, do the right thing", and c) because you're using stored procedures, you won't be switching to a DBMS that doesn't support the

      A isn't even a "reason", it's just a circular statement. "You'll want to do it because you'll want to."
      B isn't valid either, which leads me to believe that you're not actually a database programmer, since DBI layers don't just magically translate from one proprietarism to another. If you DO use an sql stored procedure, you're forced to stick to strict standards, otherwise the DBI layer becomes useless.
      C is just a lame excuse to bash MySQL, it's not even a real reason. Keep on frothing there, buddy.

  6. Scaling up, and scaling down by DavidNWelton · · Score: 5, Insightful

    Ok, so some of these tools are not suitable for running Amazon. But guess what - most people are not running Amazon! A lot of people don't have the development resources that amazon has, either, so what they are really looking for is a sweet spot that lets them get going quickly, and will grow within reason.

    I'm still mulling it over and working on it, but I talk some about "scaling down" in this article:

    http://dedasys.com/articles/scalable_systems.html

    You're right of course that you don't want stuff that falls over the first time traffic spikes a bit, but you absolutely must have something that you can use to produce a functional product. You can have the fanciest, most scalable system out there, but if you spend two months twiddling with XML config files, things just aren't going to work out.

  7. OpenDoc Cookbook by Graymalkin · · Score: 3, Insightful

    I'm definitely not an expert in Python, in fact I've only ever given it a cursory look. However that tutorial was damn impressive. Obviously he had some prior knowlege of Python and using TurboGears but it is really not all that difficult to build something like a Wiki using that framework. As far as web work I've slowly become disenchanted with PHP. It's a good language to be sure but it's simplicity is short-lived. As you want to do more complex things you end up having to work around PHP more than you get to benefit from it. A large web project in PHP ends up structured like a project of similar size in Perl or Python. Between TurboGears on Python and Ruby on Rails it looks like I have some reading to do.

    --
    I'm a loner Dottie, a Rebel.
  8. Re:Does it scale? by Scarblac · · Score: 4, Insightful

    if you want to do something for your professional career, don't waste your time with those kind of frameworks.

    If you want to do something for your professional career, get familiar with as varied a collection of tools as you can. Know the pros and cons of each. Actually test their performance, make toy projects, steal ideas and patterns. Be opinionated, but prepared to honestly choose the best tool for the job you're given, and to explain why it is the best, to suits and to techies. A few hours getting to know something new is never wasted.

    --
    I believe posters are recognized by their sig. So I made one.
  9. Tried it, too bitty by Boffy · · Score: 3, Informative

    Tried TurboGears, but the fact that it's a glue was way too appparent. I then moved on to trying Django and fell in love. All the stuff TurboGears can do Django can too, but natively.

    1. Re:Tried it, too bitty by aleph · · Score: 3, Informative

      Still downloading the movie from TurboGears (tried streaming, but well.. That wasn't happening too well :)), but I have been playing with Django a bit the past few days.

      It does look kind of cool, the admin interface is good for _simple_ objects, but seems to start to fall down when you want to construct interesting relationships between the objects, and I haven't started to look into doing custom (or using generic) create/update templates yet.

      That being said I'm still suffering a bit of paradigm lag since i've mostly been dealing with Zope/Plone for the past couple of years, and while that platform isn't without it's problems, it does have a lot of nice features you begin to take for granted.

      I also came across areas where Django would silently fail to do things in the admin interface with no sign of an error anywhere (it wasn't even attempting to insert the new object in the db, nor raising an exception anywhere). That kind of behaviour makes me a bit nervous :p

  10. Small Wikis by xihr · · Score: 3, Interesting

    If you're up for small Wikis, there's always HeyHeyWickie, a Python Wiki in under 4K lines of code (using EmPy and docutils).

  11. Twisted by jcr · · Score: 4, Informative

    Also worth checking out: Twisted. I haven't had occasion to use it myself, but people I know swear by it.

    -jcr

    --
    The only title of honor that a tyrant can grant is "Enemy of the State."
  12. Ruby functional? Not. by archeopterix · · Score: 3, Insightful
    Because, like it or not, functional programming has been trying to hit the mainstream for decades without result because functional programming is not as easy and straightforward as imperative programming.
    Ruby is no more functional than Python, even by the most twisted definition of 'functional'. Yup, it uses closures (aka blocks) and that is how far it goes towards functional. For a language to be functional it is essential to restrict side effects (haskell with monads, ocaml with 'mutable'), or at least have some good support for no-side-effects programming: currying, higher-order functions, the "let x = expr1 in expr2" (aka non-destructive assignment) construct and so on. Both Python and Ruby make routine use of destructive assignment, which is the epithome of side effects.
  13. Re:Rails everywhere. by arevos · · Score: 4, Informative
    To me it seems like a silly exercise to replicate rails in python or what have you. Ruby is easy to pick up and a nice language to boot. Why bother really? Just learn ruby and get on board.

    Whilst Rails is an excellent framework for web applications, I've found that it gets exponentially more difficult to work with when your database structures grow more complex than interconnected lists. I recently designed a double-booked accounts program in Rails, and whilst most of the code was simple to design, I quickly got bogged down in handling the accounts-tree and the double-booking of financial records. Here the documentation ran out, and I was forced to go through the Rails source to discover a solution to my problem, which turned out to be less than optimum.

    Secondly, whilst I have done a fair bit of work in Ruby, I can't help but prefer Python. If there's little difference between Rails and, say, Turbogears, Django or Subway, then surely it comes down to personal preference. Python web frameworks appear to take a more piecemeal approach than Rail, which can provide a more flexible solution in certain situations.

    Can't say I much like SQLObject's syntax, though; but CherryPy seems rather elegant.

  14. Yeah, why use SQL halfway? by TheLink · · Score: 3, Insightful

    Yeah, that's the part that gets me wondering. People keep talking about hiding away the SQL. But if they don't want SQL, then why are they using an SQL RDBMS in the first place?

    Shouldn't they be using something else then? Otherwise they'll get the drawbacks of using an SQL database but fewer of the advantages. What happens if performance in a particular area is not good enough?

    Say you want to store a session in a database and you want it to expire after X seconds of inactivity.

    A simplistic method would be to update the session row each time there's activity. But this would cause lots of writes which would be slower in most proper databases (those that actually write to disk for writes). An alternative would be the databases equivalent of "update sessiontable set lastactive=now() where sessionid='$sessionid' and lastactive+'1 second' <= now()".

    With this, you could have thousands of hits per second but only 1 forced write to disk per second due to that query.

    How would you achieve this when you're so abstracted away from the SQL database? And it might look strange to others when you try to do the same thing N layers above the database. I'm sure there are better examples.

    Those sort of queries are likely to look different on different RDBMSes. You could make a function that looks the same, but someone still is going to have to write the SQL for portability (and sometimes bad luck, it's not possible - DB doesn't support functions or that sort of function). Also, if only the program's session module does that stuff, then what's so bad about leaving the SQL in there? At least then there'll be some context to understand the SQL (and whether it's wrong or right ;) ), rather than it being neatly stored with 1000 other nonrelated SQL statements (as functions or otherwise).

    Sure it's ugly. But if people want it all so elegant and clean maybe they should write _everything_ in some version of Lisp, and not interface with the rest of the ugly real world.

    --
  15. Re:Perl on rails? by holy+zarquon's+singi · · Score: 4, Informative
    --
    "...we should just trust our president in every decision that he makes and we should just support that." B.Spears 2003
  16. Question by g2devi · · Score: 3, Interesting

    > For anything substantial, though, it always seem to wind up
    > being more work as I figure out how to configure & trick the
    > persistance layer into giving me my data in the most efficient
    > way. This can be frustrating when you know how to accomplish
    > the same thing in 5 seconds using plain SQL. Maybe it's just me?

    That raises a question about these persistence layers. Most of the tables we create use an "always insert, never update or delete" model so we can keep track of who made the change, when the change was made, and by whom.

    You have to code, normalize, index, and select from these tables in a particular way otherwise you'd end up with extremely inefficient code. I just don't see how a persistance layer can handle this.

    Another thing, one thing I've learned is that for enterprise applications, data outlives the application and in many cases, it predates the application. In essense, the persistence layer will need to be retrofitted into the application. Top-down GUIs like the ones presented for Rails appear to be of no use. We already know the exact representation of the data so Rails will need to work from the bottom up by default.

    As a side note, I don't see why people are so afraid of a little SQL. Sure it takes time to learn how to master efficient SQL coding, but it's well worth it. It's the most efficient way to deal with most data, and because it's a declarative language, it's often more intuitive than the imperative language you're working in. It's also supported by most languages, so it's universal. If you're using a strict model-view-controller paradigm, database access will be restricted to the model and recast into data objects that can be used by the view and controller. If done correctly, you can rewrite the data code of several applications every day of the week without affecting the view and the controller (i.e. the bulk of your code). From what I've seen, that clarity of what exactly is happening and how complex things are mapped really helps with maintenance and optimization. From trying to maintain persistense layers, I'm left trying to figure out "what on earth is the persistense layer trying to do that is causing so much trouble".

  17. Re:Rails everywhere. by Ian+Bicking · · Score: 5, Informative
    To me it seems like a silly exercise to replicate rails in python or what have you.
    1. TurboGears is not a Rails clone.
    2. Most parts of TurboGears existed before Rails: CherryPy, SQLObject, FormEncode (and Python of course).
    3. Kid is most closely related to Zope Page Templates (from the Python world), not anything from Rails.
    4. MochiKit has a certain relationship to Prototype (the Javascript library from Rails), and is compatible with it. However, it's not that the author particularly likes Prototype.

    Rails has taught us some important lessons, but they aren't really technical lessons:

    1. We shouldn't sit around and say "oh, those poor people using PHP/Java/etc, too bad they don't know about what you can do using X". Instead we should talk more loudly and insistently about the advantages of our platforms. If you do it right people will pay attention.
    2. We haven't concentrated enough on full-stack integration. We've been overvaluing decoupled pick-and-choose components. Full-stack integration doesn't have to mean coupling -- it can just be a matter of presentation, and making sure tools are complimentary. Not all of the Python frameworks are coming at it from this direction, but TurboGears very much is.
    3. Things like screencasts are nice.

    After looking at various pieces of Rails, these lessons have stood out to me, but the particular technology in Rails has not. Sure, there are some good ideas, but nothing radical, and there's good ideas everywhere waiting to be mined. We're not beneath mining other people's ideas, but it does not follow that the result is merely a "replication" in part or in whole.

    As for Ruby: I think the two languages are largely equivalent in terms of what you can do. I would not say the same about PHP or Java. As for Rails specifically, I think it is only ahead of Python options in the second derivative. With conscious players the second derivative doesn't mean a whole lot.

  18. Re:Does it scale? by swillden · · Score: 3, Funny

    A few hours getting to know something new is never wasted.

    I once spent a couple of hours looking at VBScript.

    I think I came away knowing less about good programming than before, *and* I was out two hours.

    --
    Note to ACs: I usually delete AC replies without reading them. If you want to talk to me, log in.