skariel / webalchemy

Modern web development with Python
MIT License
346 stars 21 forks source link

Added docstrings, comments, and 'pythonized' the code a little #143

Closed joaoventura closed 10 years ago

joaoventura commented 10 years ago

This is my effort to add some comments and documentation to the server.py file as I understand the source-code so far..

I have also moved functions to the top of the file (it is usually considered a python best practice), and created a new method on the WebSocketHandler to handle local document initialization. The initialization code was inside the "on_message" method, and I've moved it outside as the other "on_message handlers" are.

In general, minimal code per function is considered a good practice. In my own projects I try to keep functions with less than 20-25 lines, and an entire file with less than 200-250 lines of pythonic code. I find it to be very readable that way. The server.py has now the double, so it means that some things should move (PrivateDataStore should be elsewhere, the handlers could be in an own module, etc.)

Finally, I have also "pythonized" some of the code, but did not change any functionalities. Things like:

if not to_session_ids:
    lst = self.sharedhandlers.keys()
else:
    lst = to_session_ids

were changed to one-liners (with more explicit variable names):

session_ids = to_session_ids if (to_session_ids) else self.sharedhandlers.keys()

I've tested with the Hello World and it is working as usual, so I am almost sure I did not break anything.. :)

skariel commented 10 years ago

Thanks, it's much better. I'll try to follow the guide-lines from now on :)