Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby eye for the Python guy

Ruby eye for the Python guy

A lightning talk I gave about Python as a reasonably new Rubyist back in 2007.

Samuel Cochran

July 01, 2007
Tweet

More Decks by Samuel Cochran

Other Decks in Programming

Transcript

  1. >>> import this The Zen of Python, by Tim Peters

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
  2. >>> import this The Zen of Python, by Tim Peters

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! WTF?!
  3. >>> import this The Zen of Python, by Tim Peters

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
  4. DSLs describe MyClass do it ‘adds two and two’ do

    sum = MyClass.add(2, 2) sum.should == 4 end end
  5. >> x = Proc.new { 5 } >> x() No

    Method Error: undefined method “x” >> x.call => 5 >> x[] => 5 Special cases
  6. class Animal: def __init__(self, name): self.name = name def talk(self):

    raise NotImplementedError("Don’t know how") class Cat(Animal): def talk(self): return 'Meow!' class Dog(Animal): def talk(self): return 'Woof! Woof!' animals = [Cat('Missy'), Cat('Mr. Mistoffelees'), Dog('Lassie')] ! for animal in animals: print animal.name + ': ' + animal.talk()
  7. import web ! urls = ( '/(.*)', 'hello' ) app

    = web.application(urls, globals()) ! class hello: def GET(self, name): if not name: name = 'World' return 'Hello, ' + name + '!' ! if __name__ == "__main__": app.run()
  8. from django.db import models ! class Musician(models.Model): first_name = models.CharField(maxlength=50)

    last_name = models.CharField(maxlength=50) instrument = models.CharField(maxlength=100) ! class Album(models.Model): artist = models.ForeignKey(Musician) name = models.CharField(maxlength=100) release_date = models.DateField() num_stars = models.IntegerField()
  9. RuntimeError (Called id for nil, which would mistakenly be 4

    -- if you really wanted the id of nil, use object_id): app/controllers/operators_controller.rb:22:in `apply_scope' .bundle/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `send' .bundle/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `evaluate_method' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:186:in `call' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:635:in `run_before_filters' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:615:in `call_filters' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:638:in `run_before_filters' app/controllers/application_controller.rb:85:in `scope_by_current_merchant' .bundle/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `send' .bundle/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `evaluate_method' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:186:in `call' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:635:in `run_before_filters' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:615:in `call_filters' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:638:in `run_before_filters' app/controllers/application_controller.rb:74:in `scope_by_current_portfolio' .bundle/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `send' .bundle/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `evaluate_method' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:186:in `call' .bundle/gems/actionpack-2.3.5/lib/action_controller/filters.rb:635:in `run_before_filters' ... /Users/sj26/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15/lib/phusion_passenger/spawn_manager.rb:145:in `spawn_application' /Users/sj26/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15/lib/phusion_passenger/spawn_manager.rb:278:in `handle_spawn_application' /Users/sj26/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15/lib/phusion_passenger/abstract_server.rb:352:in `__send__' /Users/sj26/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15/lib/phusion_passenger/abstract_server.rb:352:in `main_loop' /Users/sj26/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.15/lib/phusion_passenger/abstract_server.rb:196:in `start_synchronously'
  10. Traceback (most recent call last): 
 File "/usr/lib/python2.5/site-packages/django/core/management/commands/runserver.py", line 48

    in inner_run 
 self.validate(display_num_errors=True) 
 File "/usr/lib/python2.5/site-packages/django/core/management/base.py", line 122, in validate 
 num_errors = get_validation_error(s, app) 
 File "/usr/lib/python2.5/site-packages/django/core/management/validation.py", line 28, in get_validation_errors 
 for (app_name, error) in get_app_errors().items: 
 File "/usr/lib/python2.5/site-packages/django/db/models/loading.py", line 128, in get_app_errors 
 self._populate() 
 File "/usr/lib/python2.5/site-packages/django/db/models/loading.py", line 57, in _populate 
 self.load_app(app_name, True) 
 File "/usr/lib/python2.5/site-packages/django/db/models/loading.py", line 72, in load_app 
 mod = __import__(app_name, {}, {}, ['models'}) 
 File "/home/joseph/mysite/../mysite/recipes/models.py", line 8, in <module> 
 class Recipes(models.Model): 
 File "/home/joseph/mysite/../mysite/recipes/models.py", line 18, in Recipes 
 favorites = models.ManytoManyField(User) 
 AttributeError: 'module' object has no attribute 'ManytoManyField'