Pythonic Code - jordy33/turbogears_tutorial GitHub Wiki

Pythonic Code

A common neologism in the Python community is pythonic, which can have a wide range of meanings related to program style. To say that code is pythonic is to say that it uses Python idioms well, that it is natural or shows fluency in the language. Likewise, to say of an interface or language feature that it is pythonic is to say that it works well with Python idioms, that its use meshes well with the rest of the language.

The ten commandments (recite every day before start coding):

  1. Always Use Camel Notation and Classes begins with capital letter.
  2. Always use classes to implement. Stand alone defs are prohibited. Create class methods instead.
  3. Centralize code. Locate classes under lib directory in turbogears. Don't repeat classes with the same functionality.
  4. Define in app_globals (located under lib) Global variables. Use constants wisely, like directory paths, environment variables, constants, etc.
  5. Think globally. Use Localization and internationalization. Primary language is english. Record all dates in UTC and render locally using moments library
  6. Create a test controller and test templates to leave test code there. Never mix test code with original created code. That will prevent to clean test code from your final code.
  7. Use turbo gears shell console to test code before implementation. (look sqlalchemy wiki here)
  8. Avoid to use try-except-else. You can use ONLY in very odd situations (like exec or eval).
  9. Table Fields,Class variables and methods should use representative names. Avoid to use letter names like: a,b,c,d,flag,myvar,thevariable,zz,counter. The variable,method or field must represent the use or function like: totalNumberOfViews, alarmCounter, userName.
  10. Avoid to use ifs ifs ifs. Instead create a dictionary with classes. example:
selector = {"everyday" : EveryDayClass(), "monday" : MondayClass(), "sunday" : SundayClass() }
toDo=selector[passedValue]
toDo.run()