Created
July 25, 2018 13:19
-
-
Save krikit/9aaa60d24d977f92cefdafdd425dd523 to your computer and use it in GitHub Desktop.
code template for Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
code template for Python 3 | |
__author__ = 'Your Name (your.email@address.info)' | |
__copyright__ = 'No copyright, just copyleft!' | |
""" | |
########### | |
# imports # | |
########### | |
import argparse | |
import logging | |
import sys | |
######### | |
# types # | |
######### | |
class MyClass(object): | |
""" | |
base class is always object. | |
only class names are pascal case and all the others are snake case. there is no camel case. | |
""" | |
def __init__(self): | |
super().__init__() # call super class's initialization function | |
self.your_member = 'members are lower snake cases' | |
self._my_member = 'private members start with underscore' | |
def your_method(self, first_arg: int, second_arg: str): | |
""" | |
methods are lower snake cases | |
Google style docstrings: http://www.sphinx-doc.org/en/master/ext/example_google.html | |
Args: | |
first_arg (int): function arguments are lower snake cases | |
second_arg (str): do not align arguments vertically with spaces. | |
it causes unnecessary code changes when arguments are added. | |
Returns: | |
value1 | |
value2 | |
""" | |
self._my_method() | |
return first_arg, second_arg | |
def _my_method(self): | |
""" | |
private methods start with underscore | |
""" | |
pass | |
@classmethod | |
def everybodys_method(cls): | |
""" | |
class methods are same as instance methods | |
""" | |
pass | |
############# | |
# constants # | |
############# | |
YOUR_CONSTANT = 'constants are upper snake cases' | |
_MY_CONSTANT = 'private constants start with underscore' | |
############# | |
# functions # | |
############# | |
def run(args): # pylint: disable=unused-argument | |
""" | |
actual function which is doing some task | |
Args: | |
args: program arguments | |
""" | |
for line_num, line in enumerate(sys.stdin, start=1): | |
line = line.rstrip('\r\n') | |
logging.debug('%d: %s', line_num, line) | |
print(line) | |
######## | |
# main # | |
######## | |
def main(): | |
""" | |
main function processes only argument parsing | |
""" | |
parser = argparse.ArgumentParser(description='cat') | |
parser.add_argument('--input', help='input file <default: stdin>', metavar='FILE') | |
parser.add_argument('--output', help='output file <default: stdout>', metavar='FILE') | |
parser.add_argument('--debug', help='enable debug', action='store_true') | |
args = parser.parse_args() | |
if args.input: | |
sys.stdin = open(args.input, 'r', encoding='UTF-8') | |
if args.output: | |
sys.stdout = open(args.output, 'w', encoding='UTF-8') | |
if args.debug: | |
logging.basicConfig(level=logging.DEBUG) | |
else: | |
logging.basicConfig(level=logging.INFO) | |
run(args) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment