Skip to content
Myr

Added the pocoo styleguide to the documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent e84ca8e · Browse files · 2 files+201 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+201 added−0 removed

  • other:+201 added−0 removed

2 files+201 added−0 removed

modified:docs/contents.rst.inc+1 added
42 unmodified lines
43 htmlfaq44 security45 unicode46 extensiondev47 styleguide48 upgrading49 changelog50 license
added:docs/styleguide.rst+200 added
1Pocoo Styleguide2================34The Pocoo styleguide is the styleguide for all Pocoo Projects, including5Flask. This styleguide is a requirement for Patches to Flask and a6recommendation for Flask extensions.78In general the Pocoo Styleguide closely follows :pep:`8` with some small9differences and extensions.1011General Layout12--------------1314Indentation:15 4 real spaces. No tabs, no exceptions.1617Maximum line length:18 79 characters with a soft limit for 84 if absolutely necessary. Try19 to avoid too nested code by cleverly placing `break`, `continue` and20 `return` statements.2122Continuing long statements:23 To continue a statement you can use backslashes in which case you should24 align the next line with the last dot or equal sign, or indent four25 spaces::2627 this_is_a_very_long(function_call, 'with many parameters') \28 .that_returns_an_object_with_an_attribute2930 MyModel.query.filter(MyModel.scalar > 120) \31 .order_by(MyModel.name.desc()) \32 .limit(10)3334 If you break in a statement with parentheses or brances, align to the35 braces::3637 this_is_a_very_long(function_call, 'with many parameters',38 23, 42, 'and even more')3940 For lists or tuples with many items, break immediately after the41 opening brace::4243 items = [44 'this is the first', 'set of items', 'with more items',45 'to come in this line', 'like this'46 ]4748Blank lines:49 Top level functions and classes are separated by two lines, everything50 else by one. Do not use too many blank lines to separate logical51 segments in code. Example::5253 def hello(name):54 print 'Hello %s!' % name555657 def goodbye(name):58 print 'See you %s.' % name596061 class MyClass(object):62 """This is a simple docstring"""6364 def __init__(self, name):65 self.name = name6667 def get_annoying_name(self):68 return self.name.upper() + '!!!!111'6970Expressions and Statements71--------------------------7273General whitespace rules:74 - Whitespace is absend for unary operators that are not works75 (eg: ``-``, ``~`` etc.) as well on the inner side of parentheses.76 - Whitespace is placed between binary operators.7778 Good::7980 exp = -1.0581 value = (item_value / item_count) * offset / exp82 value = my_list[index]83 value = my_dict['key']8485 Bad::8687 exp = - 1.0588 value = ( item_value / item_count ) * offset / exp89 value = (item_value/item_count)*offset/exp90 value=( item_value/item_count ) * offset/exp91 value = my_list[ index ]92 value = my_dict ['key']9394Yoda statements are a nogo:95 Never compare constant with variable, always variable with constant:9697 Good::9899 if method == 'md5':100 pass101102 Bad::103104 if 'md5' == method:105 pass106107Comparisons:108 - against arbitary types: ``==`` and ``!=``109 - against singletones with ``is`` and ``is not`` (eg: ``foo is not110 None``)111 - never compare something with `True` or `False` (for example never112 do ``foo == False``, do ``not foo`` instead)113114Negated containment checks:115 use ``foo not in bar`` instead of ``not foo in bar``116117Instance checks:118 ``isinstance(a, C)`` instead of ``type(A) is C``, but try to avoid119 instance checks in general. Check for features.120121122Naming Conventions123------------------124125- Class names: ``CamelCase``, with acronyms kept uppercase (``HTTPWriter``126 and not ``HttpWriter``)127- Variable names: ``lowercase_with_underscores``128- Method and functin names: ``lowercase_with_underscores``129- Constants: ``UPPERCASE_WITH_UNDERSCORES``130- precompiled regular expressions: ``name_re``131132Protected members are prefixed with a single underscore. Double133underscores are reserved for mixin classes.134135On classes with keywords, trailing underscores are appended. Clashes with136builtins are allowed and **must not** be resolved by appending an137underline to the variable name. If the function needs to access a138shadowed builtin, rebind the builtin to a different name instead.139140Function and method arguments:141 - class methods: ``cls`` as first parameter142 - instance methods: ``self`` as first parameter143 - lambdas for properties might have the first parameter replaced144 with ``x`` like in ``display_name = property(lambda x: x.real_name145 or x.username)``146147148Docstrings149----------150151Docstring conventions:152 All docstrings are formatted with reStructuredText as understood by153 Sphinx. Depending on the number of lines in the docstring, they are154 layed out differently. If it's just one line, the closing tripple155 quote is on the same line as the opening, otherwise the text is on156 the same line as the opening quote and the tripple quote that closes157 the string on its own line::158159 def foo():160 """This is a simple docstring"""161162163 def bar():164 """This is a longer docstring with so much information in there165 that it spans three lines. In this case the closing tripple quote166 is on its own line.167 """168169Module header:170 The module header consists of an utf-8 encoding declaration (if non171 ASCII letters are used, but it is recommended all the time) and a172 standard docstring::173174 # -*- coding: utf-8 -*-175 """176 package.module177 ~~~~~~~~~~~~~~178179 A brief description goes here.180181 :copyright: (c) YEAR by AUTHOR.182 :license: LICENSE_NAME, see LICENSE_FILE for more details.183 """184185 Please keep in mind that proper copyrights and license files are a186 requirement for approved Flask extensions.187188189Comments190--------191192Rules for comments are similar to docstrings. Both are formatted with193reStructuredText. If a comment is used to document an attribute, put a194colon after the opening pound sign (``#``)::195196 class User(object):197 #: the name of the user as unicode string198 name = Column(String)199 #: the sha1 hash of the password + inline salt200 pw_hash = Column(String)