Skip to content

Instantly share code, notes, and snippets.

@GeyseR
Forked from dcramer/args.py
Last active September 1, 2020 10:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeyseR/2b92f6665fda9c4ef47b51292a5669d0 to your computer and use it in GitHub Desktop.
Save GeyseR/2b92f6665fda9c4ef47b51292a5669d0 to your computer and use it in GitHub Desktop.
Python Standards(that I would change and enforce if I could)
# dont do this
some_function(foo, bar,
baz)
# dont do this
some_function(foo, bar
baz)
# do this
some_function(
foo, bar, baz)
# you can move close parenthesis on new line
# if all positional args placed on separate lines
# so allow this
some_function(
foo,
bar,
baz,
)
# or this
some_function(
foo, bar, # not enough space here
baz)
# but not this
some_function(
foo,
bar,
baz)
# do this
foo(
'bar',
keyword='argument',
)
# dont do this
foo(hello='world', biz='baz',
foo='bar')
# do this
foo(
hello='world',
biz='baz',
foo='bar',
)
# func declarations:
# dont do this
def foo(
arg1,
arg2,
arg3,
arg=default
):
pass
# do this
def foo(arg1, arg2,
arg3, arg=default):
pass
# dont do this
def my_func():
"""lol this is bad"""
# dont do this
def my_func():
"lol this is bad"
# dont do this
def my_func():
"""lol this is bad
but worse
"""
# cramer's version
def my_func():
"""
lol this is readable
no matter how many lines it is
"""
# sort sections (split each group by empty line):
# future imports
# stdlib
# framework dependencies (django, flask, etc.)
# third party modules (installed from pypi or other sources)
# first party modules (from current project) and local modules
# example
from __future__ import absolute_import
from datetime import date
from django.db import models
import requests
from other_project_app.models import SomeModel
from ..utils import some_util_func
from .models import AppModel
# sort multiple imports from one module alphabetically
from django.db.models import Max, Min
# surround multiple import by parenthesis if your need to break them to separate line
from django.db.models import (
Count, F, Max,
Min, F
)
# import specific module items should be after whole module imports
import os
from os import path
# dont do this
foo = {
'foo': 'bar',
'baz': 'biz'}
# do this
foo = {
'foo': 'bar',
'baz': 'biz', # always have a trailing comma
}
# dont do this
foo = ('bar', 'baz', 'biz'
'lol', 'world')
# do this
foo = (
'bar',
'baz',
'biz',
)
# allow this
foo = (
'bar', 'baz',
'biz'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment