News for hackers. Stuff that matters.
Re:What if SMTP and Sinatra Had a Baby? by LeafStorm on Saturday November 26, 2011 @10:40PM (#3281580)
The title ("SMTP and Sinatra") made me expect something written in Ruby. Also:

    def subscribe(self, route=r'subscribe-(?P<name>[^@]*)@.*'):
No. No. No. No. No. No. No. Default arguments are not for specifying metadata about your functions. That's what decorators are for. And for the love of Guido, why are you accessing per-request information from instance variables? It seems to me that you would much rather be programming this in Ruby.
Re:What if SMTP and Sinatra Had a Baby? by BenjaminCoe on Saturday November 26, 2011 @10:44PM (#3281594)
Lol, it's dogmatism like that which gives the Python community such a great reputation.

I was shooting for the most concise syntax I could get. Why Python and not Ruby? I wanted to use Python's standard smtpd library, and their great libraries for dealing with RFC2822 emails.

Oh, in regards to the instance variables. It uses a forking based paradigm to handle requests, so the instance variables are not maintained from request to request.

Re:What if SMTP and Sinatra Had a Baby? by jrockway on Saturday November 26, 2011 @10:53PM (#3281617)
The things he's talking about have nothing to do with Python. If you have a per-application instance of a class, you can't use instance data for request-specific things. The reason is because the per-application class is per-application, not per-request. (If you clone the per-application class for each request, then it's OK, but still a little weird. Any operation that needs state to be initialized and thrown away is exactly why you create an instance of a class.)

Really, anything that changes instance data outside the constructor should worry you. And actually, Python's habit of requiring you to initialize attributes in __init__ is kind of annoying; stuff like Perl's Moose is much cleaner.

Re:What if SMTP and Sinatra Had a Baby? by LeafStorm on Saturday November 26, 2011 @10:53PM (#3281615)
> I was shooting for the most concise syntax I could get.

Yes, but default arguments are simply not intended for that. Decorators are. You wouldn't use default arguments as method metadata in Ruby, would you? Going for the "most concise syntax" in all cases gets you Perl, and Python is most definitely not Perl.

Re:What if SMTP and Sinatra Had a Baby? by BenjaminCoe on Saturday November 26, 2011 @11:00PM (#3281637)
You could definitely twist my arm and get me to change the approach, mentally substitute:

def subscribe(self, route=r'subscribe-(?P<name>[^@])@.'):

with:

@route('subscribe-(?P<name>[^@])@.'')

def subscribe(self):

and we can stop arguing.

I'm sticking to my guns on the instance variables, it's cleaner.

Re:What if SMTP and Sinatra Had a Baby? by jrockway on Saturday November 26, 2011 @10:56PM (#3281625)
In Perl, all arguments are undef (similar to None) by default, which is pretty much the only thing you should be defaulting things to in Python. So actually, Perl makes it hard to shoot yourself in the foot, while Python has syntax for making it easy.

(We have an internal library at work that does something like:

    def __init__(self, ..., foo={}):
        self.foo = foo
Guess what happens when you change some_instance.foo. Yup, all the other instances change too!)
Re:What if SMTP and Sinatra Had a Baby? by LeafStorm on Saturday November 26, 2011 @11:02PM (#3281643)
That's really not the point I was going for. I mean, we can debate which language's style is better all day, but regardless of which approach to default arguments is better Mr. Coe is still using the ones in Python improperly.
Re:What if SMTP and Sinatra Had a Baby? by BenjaminCoe on Saturday November 26, 2011 @11:28PM (#3281691)
I attempted to make amends one comment below this. I will switch the library to using a decorator based approach -- it's as clean, and there's no reason for me to break convention (I'll lash myself a few times to be safe too ;)

With regards to instance variables:

On a request, the main process forks and a new instance of the route object is instantiated. Trust me, instance variables won't leak between requests:

This is what happens when a request comes in, the parameters to process_message are described here: http://docs.python.org/library/smtpd.html

    @pytoad_decorator()
    def process_message(self, peer, mailfrom, rcpttos, message_data):
        for RouteClass in self.routes:
            route = RouteClass(peer_ip=peer[0])
            route.route(message_data)
Re:What if SMTP and Sinatra Had a Baby? by fakeempire on Saturday November 26, 2011 @11:19PM (#3281675)
Excuse my ignorance, but why would that change all the instances in this case? I don't understand. Would you mind explaining more?
Re:What if SMTP and Sinatra Had a Baby? by pyre on Sunday November 27, 2011 @01:11AM (#3281846)

      def __init__(self, ..., foo={}):
        self.foo = foo
In this case, 'foo={}' is evaluated once, and returns a pointer to a dict(). After that, foo is always pointing to the same dict, not to a newly created empty dict.

Run this in a Python interpreter:

  class PhoneBook(object):
    def __init__(self, names={}):
      self.names = names

  phonebook1 = PhoneBook()
  phonebook2 = PhoneBook()

  phonebook1.names['Bob'] = '+1 555 5555'

  print phonebook2.names['Bob']
The only thing that is really safe to put in the defaults are immutable values.
Re:What if SMTP and Sinatra Had a Baby? by mike-cardwell on Sunday November 27, 2011 @07:30AM (#3282306)
FYI, RFC2822 was obsoleted by RFC5322, three years ago.
Re:What if SMTP and Sinatra Had a Baby? by BenjaminCoe on Sunday November 27, 2011 @12:29PM (#3282818)
I have switched to using decorators, based on your suggestion.

https://github.com/bcoe/smtproutes/issues/1