A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
| # Send POST request with customize header | |
| curl --header "Content-Type: application/json" --header "authToken: <token_string>" --data @file.txt https://api.abc.com/endpoint |
| # Send GET request to github api to get OAuth token. | |
| curl --user "<username>" --data '{"scopes": ["gist"], "note": "Demo Oauth token"}' https://api.github.com/authorizations | |
| # Use OAuth token to access resource | |
| curl https://api.github.com/gitsts/starred?access_token=<token_string> | |
| # or use OAuth token on request header | |
| curl --header "Authorization: token <token_string>" https://api.github.com/gists/starred | |
| # List all authorizations | |
| curl --user "<username>" https://api.github.com/authorizations |
| # define a decorator simplest as possible | |
| def identify(f): | |
| return f | |
| # Use decorator | |
| def foo(): | |
| return 'fuzz' | |
| # the same as | |
| foo = identify(foo) |
| import functools | |
| import inspect | |
| # decorator | |
| def check_role(f): | |
| # Use this decorator to keep update some attributes of f such as doc string, name ... | |
| @functools.wraps(f) | |
| def wrapper(*args, **kwargs): | |
| # make arguments as a dict, this avoid to check argument which is position or keyword argument | |
| func_args = inspect.getcallargs(f, *args, **kwargs) |
| # Define TV class, notice class is an object too | |
| class TV(object): | |
| def __init__(self, size) | |
| self.size = size | |
| # This is an instance method and it always bound to specific instance of TV class | |
| def get_size(self): | |
| return self.size | |
| # Try to call class method | |
| TV.get_size(TV(40)) |
| -- Define a function that increment one value | |
| inc x = x + 1 | |
| -- Call function | |
| inc |
| -- define function type signature | |
| inc :: Inc -> Inc | |
| -- define function equation | |
| inc x = x + 1 |
| -- Example module in haskell | |
| -- Mr ABC, Sept 2016 | |
| -- | |
| -- Define a simple module | |
| module Simple | |
| where | |
| -- calculates the arithmetic of two numbers | |
| arithmetic :: Fractional a => a -> a -> a | |
| arithmetic x y = (x + y) / 2 |
| -- define max function accept two number and return greater number | |
| max :: Ord a => a -> a -> a | |
| -- single line function equation | |
| max x y = if x >= y then x else y | |
| -- multiple line function equation (Guard style -> this is idiomatic haskell) | |
| max x y | x >= y = x | |
| | otherwise = y | |
| -- signum function (Ord a, Num a) meant type variable a can belong both type class Ord and Num | |
| signum :: (Ord a, Num a) => a -> Int |