mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 10:47:13 +00:00
feat: restructure docs into "chapters" (#12)
* feat(docker, k8s): create containers folder and kubernetes notes
This commit is contained in:
parent
b1cb858508
commit
2725e3cb70
92 changed files with 777 additions and 367 deletions
215
docs/languages/python/modules/argparse.md
Normal file
215
docs/languages/python/modules/argparse.md
Normal file
|
@ -0,0 +1,215 @@
|
|||
# Argpasrse Module
|
||||
|
||||
## Creating a parser
|
||||
|
||||
```py
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="description", allow_abbrev=True)
|
||||
```
|
||||
|
||||
**Note**: All parameters should be passed as keyword arguments.
|
||||
|
||||
- `prog`: The name of the program (default: `sys.argv[0]`)
|
||||
- `usage`: The string describing the program usage (default: generated from arguments added to parser)
|
||||
- `description`: Text to display before the argument help (default: none)
|
||||
- `epilog`: Text to display after the argument help (default: none)
|
||||
- `parents`: A list of ArgumentParser objects whose arguments should also be included
|
||||
- `formatter_class`: A class for customizing the help output
|
||||
- `prefix_chars`: The set of characters that prefix optional arguments (default: ‘-‘)
|
||||
- `fromfile_prefix_chars`: The set of characters that prefix files from which additional arguments should be read (default: None)
|
||||
- `argument_default`: The global default value for arguments (default: None)
|
||||
- `conflict_handler`: The strategy for resolving conflicting optionals (usually unnecessary)
|
||||
- `add_help`: Add a -h/--help option to the parser (default: True)
|
||||
- `allow_abbrev`: Allows long options to be abbreviated if the abbreviation is unambiguous. (default: True)
|
||||
|
||||
## [Adding Arguments](https://docs.python.org/3/library/argparse.html#the-add-argument-method)
|
||||
|
||||
```py
|
||||
ArgumentParser.add_argument("name_or_flags", nargs="...", action="...")
|
||||
```
|
||||
|
||||
**Note**: All parameters should be passed as keyword arguments.
|
||||
|
||||
- `name or flags`: Either a name or a list of option strings, e.g. `foo` or `-f`, `--foo`.
|
||||
- `action`: The basic type of action to be taken when this argument is encountered at the command line.
|
||||
- `nargs`: The number of command-line arguments that should be consumed.
|
||||
- `const`: A constant value required by some action and nargs selections.
|
||||
- `default`: The value produced if the argument is absent from the command line.
|
||||
- `type`: The type to which the command-line argument should be converted to.
|
||||
- `choices`: A container of the allowable values for the argument.
|
||||
- `required`: Whether or not the command-line option may be omitted (optionals only).
|
||||
- `help`: A brief description of what the argument does.
|
||||
- `metavar`: A name for the argument in usage messages.
|
||||
- `dest`: The name of the attribute to be added to the object returned by `parse_args()`.
|
||||
|
||||
### Actions
|
||||
|
||||
`store`: This just stores the argument's value. This is the default action.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo')
|
||||
>>> parser.parse_args('--foo 1'.split())
|
||||
Namespace(foo='1')
|
||||
```
|
||||
|
||||
`store_const`: This stores the value specified by the const keyword argument. The `store_const` action is most commonly used with optional arguments that specify some sort of flag.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', action='store_const', const=42)
|
||||
>>> parser.parse_args(['--foo'])
|
||||
Namespace(foo=42)
|
||||
```
|
||||
|
||||
`store_true` and `store_false`: These are special cases of `store_const` used for storing the values True and False respectively. In addition, they create default values of False and True respectively.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', action='store_true')
|
||||
>>> parser.add_argument('--bar', action='store_false')
|
||||
>>> parser.add_argument('--baz', action='store_false')
|
||||
>>> parser.parse_args('--foo --bar'.split())
|
||||
Namespace(foo=True, bar=False, baz=True)
|
||||
```
|
||||
|
||||
`append`: This stores a list, and appends each argument value to the list. This is useful to allow an option to be specified multiple times. Example usage:
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', action='append')
|
||||
>>> parser.parse_args('--foo 1 --foo 2'.split())
|
||||
Namespace(foo=['1', '2'])
|
||||
```
|
||||
|
||||
`append_const`: This stores a list, and appends the value specified by the const keyword argument to the list. (Note that the const keyword argument defaults to None.) The `append_const` action is typically useful when multiple arguments need to store constants to the same list. For example:
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--str', dest='types', action='append_const', const=str)
|
||||
>>> parser.add_argument('--int', dest='types', action='append_const', const=int)
|
||||
>>> parser.parse_args('--str --int'.split())
|
||||
Namespace(types=[<class 'str'>, <class 'int'>])
|
||||
```
|
||||
|
||||
`count`: This counts the number of times a keyword argument occurs. For example, this is useful for increasing verbosity levels:
|
||||
**Note**: the default will be None unless explicitly set to 0.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--verbose', '-v', action='count', default=0)
|
||||
>>> parser.parse_args(['-vvv'])
|
||||
Namespace(verbose=3)
|
||||
```
|
||||
|
||||
`help`: This prints a complete help message for all the options in the current parser and then exits. By default a help action is automatically added to the parser.
|
||||
|
||||
`version`: This expects a version= keyword argument in the add_argument() call, and prints version information and exits when invoked:
|
||||
|
||||
```py
|
||||
>>> import argparse
|
||||
>>> parser = argparse.ArgumentParser(prog='PROG')
|
||||
>>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')
|
||||
>>> parser.parse_args(['--version'])
|
||||
PROG 2.0
|
||||
```
|
||||
|
||||
`extend`: This stores a list, and extends each argument value to the list. Example usage:
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
|
||||
>>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
|
||||
Namespace(foo=['f1', 'f2', 'f3', 'f4'])
|
||||
```
|
||||
|
||||
### Nargs
|
||||
|
||||
ArgumentParser objects usually associate a single command-line argument with a single action to be taken.
|
||||
The `nargs` keyword argument associates a different number of command-line arguments with a single action.
|
||||
|
||||
**Note**: If the nargs keyword argument is not provided, the number of arguments consumed is determined by the action.
|
||||
|
||||
`N` (an integer): N arguments from the command line will be gathered together into a list.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', nargs=2)
|
||||
>>> parser.add_argument('bar', nargs=1)
|
||||
>>> parser.parse_args('c --foo a b'.split())
|
||||
Namespace(bar=['c'], foo=['a', 'b'])
|
||||
```
|
||||
|
||||
**Note**: `nargs=1` produces a list of one item. This is different from the default, in which the item is produced by itself.
|
||||
|
||||
`?`: One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from default will be produced.
|
||||
|
||||
For optional arguments, there is an additional case: the option string is present but not followed by a command-line argument. In this case the value from const will be produced.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', nargs='?', const='c', default='d')
|
||||
>>> parser.add_argument('bar', nargs='?', default='d')
|
||||
>>> parser.parse_args(['XX', '--foo', 'YY'])
|
||||
Namespace(bar='XX', foo='YY')
|
||||
>>> parser.parse_args(['XX', '--foo'])
|
||||
Namespace(bar='XX', foo='c')
|
||||
>>> parser.parse_args([])
|
||||
Namespace(bar='d', foo='d')
|
||||
```
|
||||
|
||||
`*`: All command-line arguments present are gathered into a list. Note that it generally doesn't make much sense to have more than one positional argument with `nargs='*'`, but multiple optional arguments with `nargs='*'` is possible.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser()
|
||||
>>> parser.add_argument('--foo', nargs='*')
|
||||
>>> parser.add_argument('--bar', nargs='*')
|
||||
>>> parser.add_argument('baz', nargs='*')
|
||||
>>> parser.parse_args('a b --foo x y --bar 1 2'.split())
|
||||
Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])
|
||||
```
|
||||
|
||||
`+`: All command-line args present are gathered into a list. Additionally, an error message will be generated if there wasn't at least one command-line argument present.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser(prog='PROG')
|
||||
>>> parser.add_argument('foo', nargs='+')
|
||||
>>> parser.parse_args(['a', 'b'])
|
||||
Namespace(foo=['a', 'b'])
|
||||
>>> parser.parse_args([])
|
||||
usage: PROG [-h] foo [foo ...]
|
||||
PROG: error: the following arguments are required: foo
|
||||
```
|
||||
|
||||
`argparse.REMAINDER`: All the remaining command-line arguments are gathered into a list. This is commonly useful for command line utilities that dispatch to other command line utilities.
|
||||
|
||||
```py
|
||||
>>> parser = argparse.ArgumentParser(prog='PROG')
|
||||
>>> parser.add_argument('--foo')
|
||||
>>> parser.add_argument('command')
|
||||
>>> parser.add_argument('args', nargs=argparse.REMAINDER)
|
||||
>>> print(parser.parse_args('--foo B cmd --arg1 XX ZZ'.split()))
|
||||
Namespace(args=['--arg1', 'XX', 'ZZ'], command='cmd', foo='B')
|
||||
```
|
||||
|
||||
## Parsing Arguments
|
||||
|
||||
```py
|
||||
# Convert argument strings to objects and assign them as attributes of the namespace. Return the populated namespace.
|
||||
ArgumentParser.parse_args(args=None, namespace=None)
|
||||
|
||||
# assign attributes to an already existing object, rather than a new Namespace object
|
||||
class C:
|
||||
pass
|
||||
|
||||
c = C()
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--foo')
|
||||
parser.parse_args(args=['--foo', 'BAR'], namespace=c)
|
||||
c.foo # BAR
|
||||
|
||||
# return a dict instead of a Namespace
|
||||
args = parser.parse_args(['--foo', 'BAR'])
|
||||
vars(args) # {'foo': 'BAR'}
|
||||
```
|
78
docs/languages/python/modules/collections.md
Normal file
78
docs/languages/python/modules/collections.md
Normal file
|
@ -0,0 +1,78 @@
|
|||
|
||||
# Collections Module
|
||||
|
||||
``` py
|
||||
# COUNTER ()
|
||||
# subclass dictionary for counting hash-capable objects
|
||||
from collections import Counter
|
||||
Counter (sequence) # -> Counter object
|
||||
# {item: num included in sequence, ...}
|
||||
|
||||
var = Counter (sequence)
|
||||
var.most_common (n) # produce list of most common elements (most common n)
|
||||
sum (var.values ()) # total of all counts
|
||||
var.clear () #reset all counts
|
||||
list (var) # list unique items
|
||||
set (var) # convert to a set
|
||||
dict (var) # convert to regular dictionary
|
||||
var.items () # convert to a list of pairs (element, count)
|
||||
Counter (dict (list_of_pairs)) # convert from a list of pairs
|
||||
var.most_common [: - n-1: -1] # n less common elements
|
||||
var + = Counter () # remove zero and negative counts
|
||||
|
||||
|
||||
# DEFAULTDICT ()
|
||||
# dictionary-like object that takes a default type as its first argument
|
||||
# defaultdict will never raise a KeyError exception.
|
||||
# non-existent keys return a default value (default_factory)
|
||||
from collections import defaultdict
|
||||
var = defaultdict (default_factory)
|
||||
var.popitem () # remove and return first element
|
||||
var.popitem (last = True) # remove and return last item
|
||||
|
||||
|
||||
# OREDERDDICT ()
|
||||
# subclass dictionary that "remembers" the order in which the contents are entered
|
||||
# Normal dictionaries have random order
|
||||
name_dict = OrderedDict ()
|
||||
# OrderedDict with same elements but different order are considered different
|
||||
|
||||
|
||||
# USERDICT ()
|
||||
# pure implementation in pythondi a map that works like a normal dictionary.
|
||||
# Designated to create subclasses
|
||||
UserDict.data # recipient of UserDict content
|
||||
|
||||
|
||||
# NAMEDTUPLE ()
|
||||
# each namedtuple is represented by its own class
|
||||
from collections import namedtuple
|
||||
NomeClasse = namedtuple (NomeClasse, parameters_separated_from_space)
|
||||
var = ClassName (parameters)
|
||||
var.attribute # access to attributes
|
||||
var [index] # access to attributes
|
||||
var._fields # access to attribute list
|
||||
var = class._make (iterable) # transformain namedtuple
|
||||
var._asdict () # Return OrderedDict object starting from namedtuple
|
||||
|
||||
|
||||
# DEQUE ()
|
||||
# double ended queue (pronounced "deck")
|
||||
# list editable on both "sides"
|
||||
from collections import deque
|
||||
var = deque (iterable, maxlen = num) # -> deque object
|
||||
var.append (item) # add item to the bottom
|
||||
var.appendleft (item) # add item to the beginning
|
||||
var.clear () # remove all elements
|
||||
var.extend (iterable) # add iterable to the bottom
|
||||
var.extendleft (iterable) # add iterable to the beginning '
|
||||
var.insert (index, item) # insert index position
|
||||
var.index (item, start, stop) # returns position of item
|
||||
var.count (item)
|
||||
var.pop ()
|
||||
var.popleft ()
|
||||
var.remove (value)
|
||||
var.reverse () # reverse element order
|
||||
var.rotate (n) # move the elements of n steps (dx if n> 0, sx if n <0)
|
||||
var.sort ()
|
||||
```
|
83
docs/languages/python/modules/csv.md
Normal file
83
docs/languages/python/modules/csv.md
Normal file
|
@ -0,0 +1,83 @@
|
|||
|
||||
# CSV Module
|
||||
|
||||
``` python
|
||||
# iterate lines of csvfile
|
||||
.reader (csvfile, dialect, ** fmtparams) -> reader object
|
||||
|
||||
# READER METHODS
|
||||
.__ next __ () # returns next iterable object line as a list or dictionary
|
||||
|
||||
# READER ATTRIBUTES
|
||||
dialect # read-only description of the dialec used
|
||||
line_num # number of lines from the beginning of the iterator
|
||||
fieldnames
|
||||
|
||||
# convert data to delimited strings
|
||||
# csvfile must support .write ()
|
||||
#type None converted to empty string (simplify SQL NULL dump)
|
||||
.writer (csvfile, dialect, ** fmtparams) -> writer object
|
||||
|
||||
# WRITER METHODS
|
||||
# row must be iterable of strings or numbers or of dictionaries
|
||||
.writerow (row) # write row formatted according to the current dialect
|
||||
.writerows (rows) # write all elements in rows formatted according to the current dialect. rows is iterable of row
|
||||
|
||||
# CSV METHODS
|
||||
# associate dialect to name (name must be string)
|
||||
.register_dialect (name, dialect, ** fmtparams)
|
||||
|
||||
# delete the dialect associated with name
|
||||
.unregister_dialect ()
|
||||
|
||||
# returns the dialect associated with name
|
||||
.get_dialect (name)
|
||||
|
||||
# list of dialects associated with name
|
||||
.list_dialect (name)
|
||||
|
||||
# returns (if empty) or sets the limit of the csv field
|
||||
.field_size_limit (new_limit)
|
||||
|
||||
'''
|
||||
csvfile - iterable object returning a string on each __next __ () call
|
||||
if csv is a file it must be opened with newline = '' (universal newline)
|
||||
dialect - specify the dialect of csv (Excel, ...) (OPTIONAL)
|
||||
|
||||
fmtparams --override formatting parameters (OPTIONAL) https://docs.python.org/3/library/csv.html#csv-fmt-params
|
||||
'''
|
||||
|
||||
# object operating as a reader but maps the info in each row into an OrderedDict whose keys are optional and passed through fieldnames
|
||||
class csv.Dictreader (f, fieldnames = None, restket = none, restval = None, dialect, * args, ** kwargs)
|
||||
'''
|
||||
f - files to read
|
||||
fieldnames --sequence, defines the names of the csv fields. if omitted use the first line of f
|
||||
restval, restkey --se len (row)> fieldnames excess data stored in restval and restkey
|
||||
|
||||
additional parameters passed to the underlying reader instance
|
||||
'''
|
||||
|
||||
class csv.DictWriter (f, fieldnames, restval = '', extrasaction, dialect, * args, ** kwargs)
|
||||
'''
|
||||
f - files to read
|
||||
fieldnames --sequence, defines the names of the csv fields. (NECESSARY)
|
||||
restval --se len (row)> fieldnames excess data stored in restval and restkey
|
||||
extrasaction - if the dictionary passed to writerow () contains key not present in fieldnames extrasaction decides action to be taken (raise cause valueError, ignore ignores additional keys)
|
||||
|
||||
additional parameters passed to the underlying writer instance
|
||||
'''
|
||||
|
||||
# DICTREADER METHODS
|
||||
.writeheader () # write a header line of fields as specified by fieldnames
|
||||
|
||||
# class used to infer the format of the CSV
|
||||
class csv.Sniffer
|
||||
.sniff (sample, delimiters = None) #parse the sample and return a Dialect class. delimiter is a sequence of possible box delimiters
|
||||
.has_header (sample) -> bool # True if first row is a series of column headings
|
||||
|
||||
#CONSTANTS
|
||||
csv.QUOTE_ALL # instructs writer to quote ("") all fields
|
||||
csv.QUOTE_MINIMAL # instructs write to quote only fields containing special characters such as delimiter, quote char ...
|
||||
csv.QUOTE_NONNUMERIC # instructs the writer to quote all non-numeric fields
|
||||
csv.QUOTE_NONE # instructs write to never quote fields
|
||||
```
|
70
docs/languages/python/modules/ftplib.md
Normal file
70
docs/languages/python/modules/ftplib.md
Normal file
|
@ -0,0 +1,70 @@
|
|||
# Ftplib Module
|
||||
|
||||
## FTP CLASSES
|
||||
|
||||
```py
|
||||
ftplib.FTP(host="", user="", password="", acct="")
|
||||
# if HOST => connect(host)
|
||||
# if USER => login(user, password, acct)
|
||||
|
||||
|
||||
ftplib.FTP_TLS(host="", user="", password="", acct="")
|
||||
```
|
||||
|
||||
## EXCEPTIONS
|
||||
|
||||
```py
|
||||
ftplib.error_reply # unexpected error from server
|
||||
ftplib.error_temp # temporary error (response codes 400-499)
|
||||
ftplib.error_perm # permanent error (response codes 500-599)
|
||||
ftplib.error_proto # error not in ftp specs
|
||||
ftplib.all_errors # tuple of all exceptions
|
||||
```
|
||||
|
||||
## FTP OBJECTS
|
||||
|
||||
```py
|
||||
# method on text files: -lines
|
||||
# method on binary files: -binary
|
||||
|
||||
# CONNECTION
|
||||
FTP.connect(host="", port=0) # used once per instance
|
||||
# DON'T CALL if host was supplied at instance creation
|
||||
|
||||
FTP.getwelcome() # return welcome message
|
||||
|
||||
FTP.login(user='anonymous', password='', acct='')
|
||||
# called once per instance after connection is established
|
||||
# DEFAULT PASSWORD: anonymous@
|
||||
# DON'T CALL if host was supplied at instance creation
|
||||
FTP.sendcmd(cmd) # send command string and return response
|
||||
FTP.voidcmd(cmd) # send command string and return nothing if successful
|
||||
# FILE TRANSFER
|
||||
FTP.abort() # abort in progress file transfer (can fail)
|
||||
|
||||
FTTP.transfercmd(cmd, rest=None) # returns socket for connection
|
||||
# CMD active mode: send EPRT or PORT command and CMD and accept connection
|
||||
# CMD passive mode: send EPSV or PASV and start transfer command
|
||||
|
||||
FTP.retrbinary(cmd, callback, blocksize=8192, rest=None) # retrieve file in binary mode
|
||||
# CMD: appropriate RETR command ('RETR filename')
|
||||
# CALLBACK: func called on every block of data received
|
||||
|
||||
FTP.rertlines(cmd, callback=None)
|
||||
# retrieve file or dir list in ASCII transfer mode
|
||||
# CMD: appropriate RETR, LSIT (list and info of files), NLST (list of file names)
|
||||
# DEFAULT CALLBACK: sys.stdout
|
||||
|
||||
FTP.set_pasv(value) # set passive mode if value is true, otherwise disable it
|
||||
# passive mode on by default
|
||||
|
||||
FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None) # store file in binary mode
|
||||
# CMD: appropriate STOR command ('STOR filename')
|
||||
# FP: {file object in binary mode} read until EOF in blocks of blocksize
|
||||
# CALLBACK: func called on each block after sending
|
||||
|
||||
FTP.storlines(cmd, fp, callback=None) # store file in ASCII transfer mode
|
||||
# CMD: appropriate STOR command ('STOR filename')
|
||||
# FP: {file object} read until EOF
|
||||
# CALLBACK: func called on each block after sending
|
||||
```
|
72
docs/languages/python/modules/itertools.md
Normal file
72
docs/languages/python/modules/itertools.md
Normal file
|
@ -0,0 +1,72 @@
|
|||
# Itertools Module
|
||||
|
||||
``` py
|
||||
# accumulate ([1,2,3,4,5]) -> 1, 3 (1 + 2), 6 (1 + 2 + 3), 10 (1 + 2 + 3 + 6), 15 (1+ 2 + 3 + 4 + 5)
|
||||
# accumulate (iter, func (,)) -> iter [0], func (iter [0] + iter [1]) + func (prev + iter [2]), ...
|
||||
accumulate (iterable, func (_, _))
|
||||
|
||||
# iterator returns elements from the first iterable,
|
||||
# then proceeds to the next until the end of the iterables
|
||||
# does not work if there is only one iterable
|
||||
chain (* iterable)
|
||||
|
||||
# concatenates elements of the single iterable even if it contains sequences
|
||||
chain.from_iterable (iterable)
|
||||
|
||||
# returns sequences of length r starting from the iterable
|
||||
# items treated as unique based on their value
|
||||
combinations (iterable, r)
|
||||
|
||||
# # returns sequences of length r starting from the iterable allowing the repetition of the elements
|
||||
combinations_with_replacement (iterable, r)
|
||||
|
||||
# iterator filters date elements returning only those that have
|
||||
# a corresponding element in selectors that is true
|
||||
compress (data, selectors)
|
||||
|
||||
count (start, step)
|
||||
|
||||
# iterator returning values in infinite sequence
|
||||
cycle (iterable)
|
||||
|
||||
# iterator discards elements of the iterable as long as the predicate is true
|
||||
dropwhile (predicate, iterable)
|
||||
|
||||
# iterator returning values if predicate is false
|
||||
filterfalse (predicate, iterable)
|
||||
|
||||
# iterator returns tuple (key, group)
|
||||
# key is the grouping criterion
|
||||
# group is a generator returning group members
|
||||
groupby (iterable, key = None)
|
||||
|
||||
# iterator returns slices of the iterable
|
||||
isslice (iterable, stop)
|
||||
isslice (iterable, start, stop, step)
|
||||
|
||||
# returns all permutations of length r of the iterable
|
||||
permutations (iterable, r = None)
|
||||
|
||||
# Cartesian product of iterables
|
||||
# loops iterables in order of input
|
||||
# [product ('ABCD', 'xy') -> Ax Ay Bx By Cx Cy Dx Dy]
|
||||
# [product ('ABCD', repeat = 2) -> AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD]
|
||||
product (* iterable, repetitions = 1)
|
||||
|
||||
# returns an object infinite times if repetition is not specified
|
||||
repeat (object, repetitions)
|
||||
|
||||
# iterator compute func (iterable)
|
||||
# used if iterable is pre-zipped sequence (seq of tuples grouping elements)
|
||||
starmap (func, iterable)
|
||||
|
||||
# iterator returning values from iterable as long as predicate is true
|
||||
takewhile (predicate, iterable)
|
||||
|
||||
# returns n independent iterators from the single iterable
|
||||
tee (iterable, n = 2)
|
||||
|
||||
# produces an iterator that aggregates elements from each iterable
|
||||
# if the iterables have different lengths the missing values are filled according to fillervalue
|
||||
zip_longest (* iterable, fillvalue = None)
|
||||
```
|
110
docs/languages/python/modules/json.md
Normal file
110
docs/languages/python/modules/json.md
Normal file
|
@ -0,0 +1,110 @@
|
|||
# JSON Module
|
||||
|
||||
## JSON Format
|
||||
|
||||
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
|
||||
It is easy for humans to read and write.
|
||||
It is easy for machines to parse and generate.
|
||||
|
||||
JSON is built on two structures:
|
||||
|
||||
- A collection of name/value pairs.
|
||||
- An ordered list of values.
|
||||
|
||||
An OBJECT is an unordered set of name/value pairs.
|
||||
An object begins with `{` (left brace) and ends with `}` (right brace).
|
||||
Each name is followed by `:` (colon) and the name/value pairs are separated by `,` (comma).
|
||||
|
||||
An ARRAY is an ordered collection of values.
|
||||
An array begins with `[` (left bracket) and ends with `]` (right bracket).
|
||||
Values are separated by `,` (comma).
|
||||
|
||||
A VALUE can be a string in double quotes, or a number,
|
||||
or true or false or null, or an object or an array.
|
||||
These structures can be nested.
|
||||
|
||||
A STRING is a sequence of zero or more Unicode characters,
|
||||
wrapped in double quotes, using backslash escapes.
|
||||
A CHARACTER is represented as a single character string.
|
||||
A STRING is very much like a C or Java string.
|
||||
A NUMBER is very much like a C or Java number,
|
||||
except that the octal and hexadecimal formats are not used.
|
||||
|
||||
WHITESPACE can be inserted between any pair of tokens.
|
||||
|
||||
## Usage
|
||||
|
||||
```python
|
||||
|
||||
# serialize obj as JSON formatted stream to fp
|
||||
json.dump(obj, fp, cls=None, indent=None, separators=None, sort_keys=False)
|
||||
# CLS: {custom JSONEncoder} -- specifies custom encoder to be used
|
||||
# INDENT: {int > 0, string} -- array elements, object members pretty-printed with indent level
|
||||
# SEPARATORS: {tuple} -- (item_separator, key_separator)
|
||||
# [default: (', ', ': ') if indent=None, (',', ':') otherwise],
|
||||
# specify (',', ':') to eliminate whitespace
|
||||
# SORT_KEYS: {bool} -- if True dict sorted by key
|
||||
|
||||
# serialize obj as JSON formatted string
|
||||
json.dumps(obj, cls=None, indent=None, separators=None, sort_keys=False)
|
||||
# CLS: {custom JSONEncoder} -- specifies custom encoder to be used
|
||||
# INDENT: {int > 0, string} -- array elements, object members pretty-printed with indent level
|
||||
# SEPARATORS: {tuple} -- (item_separator, key_separator)
|
||||
# [default: (', ', ': ') if indent=None, (',', ':') otherwise],
|
||||
# specify (',', ':') to eliminate whitespace
|
||||
# SORT_KEYS: {bool} -- if True dict sorted by key
|
||||
|
||||
# deserialize fp to python object
|
||||
json.load(fp, cls=None)
|
||||
# CLS: {custom JSONEncoder} -- specifies custom decoder to be used
|
||||
|
||||
# deserialize s (string, bytes or bytearray containing JSON doc) to python object
|
||||
json.loads(s, cls=None)
|
||||
# CLS: {custom JSONEncoder} -- specifies custom decoder to be used
|
||||
```
|
||||
|
||||
## Default Decoder (`json.JSONDecoder()`)
|
||||
|
||||
Conversions (JSON -> Python):
|
||||
|
||||
- object -> dict
|
||||
- array -> list
|
||||
- string -> str
|
||||
- number (int) -> int
|
||||
- number (real) -> float
|
||||
- true -> True
|
||||
- false -> False
|
||||
- null -> None
|
||||
|
||||
## Default Encoder (`json.JSONEncoder()`)
|
||||
|
||||
Conversions (Python -> Json):
|
||||
|
||||
- dict -> object
|
||||
- list, tuple -> array
|
||||
- str -> string
|
||||
- int, float, Enums -> number
|
||||
- True -> true
|
||||
- False -> false
|
||||
- None -> null
|
||||
|
||||
## Extending JSONEncoder (Example)
|
||||
|
||||
```python
|
||||
import json
|
||||
|
||||
class ComplexEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, complex):
|
||||
return [obj.real, obj.image]
|
||||
# Let the base class default method raise the TypeError
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
```
|
||||
|
||||
## Retrieving Data from json dict
|
||||
|
||||
```python
|
||||
data = json.loads(json)
|
||||
data["key"] # retrieve the value associated with the key
|
||||
data["outer key"]["nested key"] # nested key value retrieval
|
||||
```
|
85
docs/languages/python/modules/logging.md
Normal file
85
docs/languages/python/modules/logging.md
Normal file
|
@ -0,0 +1,85 @@
|
|||
# Logging Module
|
||||
|
||||
## Configuration
|
||||
|
||||
```python
|
||||
# basic configuration for the logging system
|
||||
logging.basicConfig(filename="relpath", level=logging.LOG_LEVEL, format=f"message format", **kwargs)
|
||||
# DATEFMT: Use the specified date/time format, as accepted by time.strftime().
|
||||
|
||||
# create a logger with a name (useful for having multiple loggers)
|
||||
logger = logging.getLogger(name="logger name")
|
||||
logger.level # LOG_LEVEL for this logger
|
||||
|
||||
# disable all logging calls of severity level and below
|
||||
# alternative to basicConfig(level=logging.LOG_LEVEL)
|
||||
logging.disable(level=LOG_LEVEL)
|
||||
```
|
||||
|
||||
### Format (`basicConfig(format="")`)
|
||||
|
||||
| Attribute name | Format | Description |
|
||||
|----------------|-------------------|-------------------------------------------------------------------------------------------|
|
||||
| asctime | `%(asctime)s` | Human-readable time when the LogRecord was created. Modified by `basicConfig(datefmt="")` |
|
||||
| created | `%(created)f` | Time when the LogRecord was created (as returned by `time.time()`). |
|
||||
| filename | `%(filename)s` | Filename portion of pathname. |
|
||||
| funcName | `%(funcName)s` | Name of function containing the logging call. |
|
||||
| levelname | `%(levelname)s` | Text logging level for the message. |
|
||||
| levelno | `%(levelno)s` | Numeric logging level for the message. |
|
||||
| lineno | `%(lineno)d` | Source line number where the logging call was issued (if available). |
|
||||
| message | `%(message)s` | The logged message, computed as `msg % args`. |
|
||||
| module | `%(module)s` | Module (name portion of filename). |
|
||||
| msecs | `%(msecs)d` | Millisecond portion of the time when the LogRecord was created. |
|
||||
| name | `%(name)s` | Name of the logger used to log the call. |
|
||||
| pathname | `%(pathname)s` | Full pathname of the source file where the logging call was issued (if available). |
|
||||
| process | `%(process)d` | Process ID (if available). |
|
||||
| processName | `%(processName)s` | Process name (if available). |
|
||||
| thread | `%(thread)d` | Thread ID (if available). |
|
||||
| threadName | `%(threadName)s` | Thread name (if available). |
|
||||
|
||||
### Datefmt (`basicConfig(datefmt="")`)
|
||||
|
||||
| Directive | Meaning |
|
||||
|-----------|------------------------------------------------------------------------------------------------------------------------------|
|
||||
| `%a` | Locale's abbreviated weekday name. |
|
||||
| `%A` | Locale's full weekday name. |
|
||||
| `%b` | Locale's abbreviated month name. |
|
||||
| `%B` | Locale's full month name. |
|
||||
| `%c` | Locale's appropriate date and time representation. |
|
||||
| `%d` | Day of the month as a decimal number [01,31]. |
|
||||
| `%H` | Hour (24-hour clock) as a decimal number [00,23]. |
|
||||
| `%I` | Hour (12-hour clock) as a decimal number [01,12]. |
|
||||
| `%j` | Day of the year as a decimal number [001,366]. |
|
||||
| `%m` | Month as a decimal number [01,12]. |
|
||||
| `%M` | Minute as a decimal number [00,59]. |
|
||||
| `%p` | Locale's equivalent of either AM or PM. |
|
||||
| `%S` | Second as a decimal number [00,61]. |
|
||||
| `%U` | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. |
|
||||
| `%w` | Weekday as a decimal number [0(Sunday),6]. |
|
||||
| `%W` | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. |
|
||||
| `%x` | Locale's appropriate date representation. |
|
||||
| `%X` | Locale's appropriate time representation. |
|
||||
| `%y` | Year without century as a decimal number [00,99]. |
|
||||
| `%Y` | Year with century as a decimal number. |
|
||||
| `%z` | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM [-23:59, +23:59]. |
|
||||
| `%Z` | Time zone name (no characters if no time zone exists). |
|
||||
| `%%` | A literal '%' character. |
|
||||
|
||||
## Logs
|
||||
|
||||
Log Levels (Low To High):
|
||||
|
||||
- default: `0`
|
||||
- debug: `10`
|
||||
- info: `20`
|
||||
- warning: `30`
|
||||
- error: `40`
|
||||
- critical: `50`
|
||||
|
||||
```python
|
||||
logging.debug(msg) # Logs a message with level DEBUG on the root logger
|
||||
logging.info(msg) # Logs a message with level INFO on the root logger
|
||||
logging.warning(msg) # Logs a message with level WARNING on the root logger
|
||||
logging.error(msg) # Logs a message with level ERROR on the root logger
|
||||
logging.critical(msg) # Logs a message with level CRITICAL on the root logger
|
||||
```
|
52
docs/languages/python/modules/shutil.md
Normal file
52
docs/languages/python/modules/shutil.md
Normal file
|
@ -0,0 +1,52 @@
|
|||
# Shutil Module
|
||||
|
||||
High-level file operations
|
||||
|
||||
```python
|
||||
# copy file src to fil dst, return dst in most efficient way
|
||||
shutil.copyfile(src, dst)
|
||||
# dst MUST be complete target name
|
||||
# if dst already exists it will be overwritten
|
||||
|
||||
# copy file src to directory dst, return path to new file
|
||||
shutil.copy(src, dst)
|
||||
|
||||
# Recursively copy entire dir-tree rooted at src to directory named dst
|
||||
# return the destination directory
|
||||
shutil.copytree(src, dst, dirs_exist_ok=False)
|
||||
# DIRS_EXIST_OK: {bool} -- dictates whether to raise an exception in case dst
|
||||
# or any missing parent directory already exists
|
||||
|
||||
# delete an entire directory tree
|
||||
shutil.rmtree(path, ignore_errors=False, onerror=None)
|
||||
# IGNORE_ERROR: {bool} -- if true errors (failed removals) will be ignored
|
||||
# ON_ERROR: handler for removal errors (if ignore_errors=False or omitted)
|
||||
|
||||
# recursively move file or directory (src) to dst, return dst
|
||||
shutil.move(src, dst)
|
||||
# if the destination is an existing directory, then src is moved inside that directory.
|
||||
# if the destination already exists but is not a directory,
|
||||
# it may be overwritten depending on os.rename() semantics
|
||||
# used to rename files
|
||||
|
||||
# change owner user and/or group of the given path
|
||||
shutil.chown(path, user=None, group=None)
|
||||
# user can be a system user name or a uid; the same applies to group.
|
||||
# At least one argument is required
|
||||
|
||||
# create archive file and return its name
|
||||
shutil.make_archive(base_name, format, [root_dir, base_dir])
|
||||
# BASE_NAME: {string} -- name of the archive, including path, excluding extension
|
||||
# FROMAT: {zip, tar, gztar, bztar, xztar} -- archive format
|
||||
# ROOT_DIR: {path} -- root directory of archive (location of archive)
|
||||
# BASE_DIR: {path} -- directory where the archiviation starts
|
||||
|
||||
# unpack an archive
|
||||
shutil.unpack_archive(filename, [extract_dir, format])
|
||||
# FILENAME: full path of archive
|
||||
# EXTRACT_DIR: {path} -- directory to unpack into
|
||||
# FORMAT: {zip, tar, gztar, bztar, xztar} -- archive format
|
||||
|
||||
# return disk usage statistics as Namedtuple w/ attributes total, used, free
|
||||
shutil.disk_usage(path)
|
||||
```
|
43
docs/languages/python/modules/smtplib.md
Normal file
43
docs/languages/python/modules/smtplib.md
Normal file
|
@ -0,0 +1,43 @@
|
|||
# SMTPlib Module
|
||||
|
||||
```python
|
||||
import smtplib
|
||||
|
||||
# SMTP instance that encapsulates a SMTP connection
|
||||
# If the optional host and port parameters are given, the SMTP connect() method is called with those parameters during initialization.
|
||||
s = smtplib.SMTP(host="host_smtp_address", port="smtp_service_port", **kwargs)
|
||||
|
||||
s = smtplib.SMTP_SSL(host="host_smtp_address", port="smtp_service_port", **kwargs)
|
||||
# An SMTP_SSL instance behaves exactly the same as instances of SMTP.
|
||||
# SMTP_SSL should be used for situations where SSL is required from the beginning of the connection
|
||||
# and using starttls() is not appropriate.
|
||||
# If host is not specified, the local host is used.
|
||||
# If port is zero, the standard SMTP-over-SSL port (465) is used.
|
||||
|
||||
SMTP.connect(host='localhost', port=0)
|
||||
#Connect to a host on a given port. The defaults are to connect to the local host at the standard SMTP port (25). If the hostname ends with a colon (':') followed by a number, that suffix will be stripped off and the number interpreted as the port number to use. This method is automatically invoked by the constructor if a host is specified during instantiation. Returns a 2-tuple of the response code and message sent by the server in its connection response.
|
||||
|
||||
SMTP.verify(address) # Check the validity of an address on this server using SMTP VRFY
|
||||
|
||||
SMTP.login(user="full_user_mail", password="user_password") # Log-in on an SMTP server that requires authentication
|
||||
|
||||
SMTP.SMTPHeloError # The server didn't reply properly to the HELO greeting
|
||||
SMTP.SMTPAuthenticationError # The server didn't accept the username/password combination.
|
||||
SMTP.SMTPNotSupportedError # The AUTH command is not supported by the server.
|
||||
SMTP.SMTPException # No suitable authentication method was found.
|
||||
|
||||
SMTP.starttls(keyfile=None, certfile=None, **kwargs) # Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP commands that follow will be encrypted
|
||||
# from_addr & to_addrs are used to construct the message envelope used by the transport agents. sendmail does not modify the message headers in any way.
|
||||
# msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified.
|
||||
SMTP.sendmail(from_addr, to_addrs, msg, **kwargs)
|
||||
# from_addr: {string} -- RFC 822 from-address string
|
||||
# ro_addrs: {string, list of strings} -- list of RFC 822 to-address strings
|
||||
# msg: {string} -- message string
|
||||
|
||||
# This is a convenience method for calling sendmail() with the message represented by an email.message.Message object.
|
||||
SMTP.send_message(msg, from_addr=None, to_addrs=None, **kwargs)
|
||||
# from_addr: {string} -- RFC 822 from-address string
|
||||
# ro_addrs: {string, list of strings} -- list of RFC 822 to-address strings
|
||||
# msg: {email.message.Message object} -- message string
|
||||
SMTP.quit() # Terminate the SMTP session and close the connection. Return the result of the SMTP QUIT command
|
||||
```
|
31
docs/languages/python/modules/socket.md
Normal file
31
docs/languages/python/modules/socket.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
# Socket Module
|
||||
|
||||
## Definition
|
||||
|
||||
A network socket is an internal endpoint for sending or receiving data within a node on a computer network.
|
||||
|
||||
In practice, socket usually refers to a socket in an Internet Protocol (IP) network, in particular for the **Transmission Control Protocol (TCP)**, which is a protocol for *one-to-one* connections.
|
||||
In this context, sockets are assumed to be associated with a specific socket address, namely the **IP address** and a **port number** for the local node, and there is a corresponding socket address at the foreign node (other node), which itself has an associated socket, used by the foreign process. Associating a socket with a socket address is called *binding*.
|
||||
|
||||
## Socket Creation & Connection
|
||||
|
||||
```python
|
||||
import socket
|
||||
|
||||
# socket over the internet, socket is a stream of data
|
||||
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
socket.connect = (("URL", port: int)) # connect to socket
|
||||
socket.close() # close connection
|
||||
```
|
||||
|
||||
## Making HTTP Requests
|
||||
|
||||
```python
|
||||
import socket
|
||||
HTTP_Method = "GET http://url/resource HTTP/version\n\n".encode() # set HTTP request (encoded string from UTF-8 to bytes)
|
||||
socket.send(HTTP_Method) # make HTTP request
|
||||
|
||||
data = socket.recv(buffer_size) # receive data from socket
|
||||
decoded = data.decode() # decode data (from bytes to UTF-8)
|
||||
```
|
96
docs/languages/python/modules/sqlite.md
Normal file
96
docs/languages/python/modules/sqlite.md
Normal file
|
@ -0,0 +1,96 @@
|
|||
# sqlite3 Module
|
||||
|
||||
## Connecting To The Database
|
||||
|
||||
To use the module, you must first create a Connection object that represents the database.
|
||||
|
||||
```python
|
||||
import sqlite3
|
||||
connection = sqlite3.connect("file.db")
|
||||
```
|
||||
|
||||
Once you have a `Connection`, you can create a `Cursor` object and call its `execute()` method to perform SQL commands.
|
||||
|
||||
```python
|
||||
cursor = connection.cursor()
|
||||
|
||||
cursor.execute(sql)
|
||||
executemany(sql, seq_of_parameters) # Executes an SQL command against all parameter sequences or mappings found in the sequence seq_of_parameters.
|
||||
|
||||
cursor.close() # close the cursor now
|
||||
# ProgrammingError exception will be raised if any operation is attempted with the cursor.
|
||||
```
|
||||
|
||||
The data saved is persistent and is available in subsequent sessions.
|
||||
|
||||
### Query Construction
|
||||
|
||||
Usually your SQL operations will need to use values from Python variables.
|
||||
You shouldn't assemble your query using Python's string operations because doing so is insecure:
|
||||
it makes your program vulnerable to an [SQL injection attack](https://en.wikipedia.org/wiki/SQL_injection)
|
||||
|
||||
Put `?` as a placeholder wherever you want to use a value, and then provide a _tuple of values_ as the second argument to the cursor's `execute()` method.
|
||||
|
||||
```python
|
||||
# Never do this -- insecure!
|
||||
c.execute("SELECT * FROM stocks WHERE symbol = value")
|
||||
|
||||
# Do this instead
|
||||
t = ('RHAT',)
|
||||
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
|
||||
print(c.fetchone())
|
||||
|
||||
# Larger example that inserts many records at a time
|
||||
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
|
||||
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
|
||||
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
|
||||
]
|
||||
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
|
||||
```
|
||||
|
||||
### Writing Operations to Disk
|
||||
|
||||
```python
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("SQL")
|
||||
connection.commit()
|
||||
```
|
||||
|
||||
### Multiple SQL Instructions
|
||||
|
||||
```python
|
||||
connection = sqlite3.connect("file.db")
|
||||
cur = con.cursor()
|
||||
cur.executescript("""
|
||||
QUERY_1;
|
||||
QUERY_2;
|
||||
...
|
||||
QUERY_N;
|
||||
""")
|
||||
|
||||
con.close()
|
||||
```
|
||||
|
||||
### Retrieving Records
|
||||
|
||||
```python
|
||||
# Fetches the next row of a query result set, returning a single sequence.
|
||||
# Returns None when no more data is available.
|
||||
cursor.fetchone()
|
||||
|
||||
# Fetches all (remaining) rows of a query result, returning a list.
|
||||
# An empty list is returned when no rows are available.
|
||||
cursor.fetchall()
|
||||
|
||||
# Fetches the next set of rows of a query result, returning a list.
|
||||
# An empty list is returned when no more rows are available.
|
||||
fetchmany(size=cursor.arraysize)
|
||||
```
|
||||
|
||||
The number of rows to fetch per call is specified by the `size` parameter. If it is not given, the cursor's `arraysize` determines the number of rows to be fetched.
|
||||
The method should try to fetch as many rows as indicated by the size parameter.
|
||||
If this is not possible due to the specified number of rows not being available, fewer rows may be returned.
|
||||
|
||||
Note there are performance considerations involved with the size parameter.
|
||||
For optimal performance, it is usually best to use the arraysize attribute.
|
||||
If the size parameter is used, then it is best for it to retain the same value from one `fetchmany()` call to the next.
|
64
docs/languages/python/modules/time-datetime.md
Normal file
64
docs/languages/python/modules/time-datetime.md
Normal file
|
@ -0,0 +1,64 @@
|
|||
# Time & Datetime
|
||||
|
||||
## Time
|
||||
|
||||
```py
|
||||
# epoch: elapsed time in seconds (in UNIX starts from 01-010-1970)
|
||||
import time # UNIX time
|
||||
variable = time.time () # returns the time (in seconds) elapsed since 01-01-1970
|
||||
variable = time.ctime (epochseconds) # transform epoch into date
|
||||
|
||||
var = time.perf_counter () # returns the current running time
|
||||
# execution time = start time - end time
|
||||
```
|
||||
|
||||
### time.srtfrime() format
|
||||
|
||||
| Format | Data |
|
||||
|--------|------------------------------------------------------------------------------------------------------------|
|
||||
| `%a` | Locale's abbreviated weekday name. |
|
||||
| `%A` | Locale's full weekday name. |
|
||||
| `%b` | Locale's abbreviated month name. |
|
||||
| `%B` | Locale's full month name. |
|
||||
| `%c` | Locale's appropriate date and time representation. |
|
||||
| `%d` | Day of the month as a decimal number `[01,31]`. |
|
||||
| `%H` | Hour (24-hour clock) as a decimal number `[00,23]`. |
|
||||
| `%I` | Hour (12-hour clock) as a decimal number `[01,12]`. |
|
||||
| `%j` | Day of the year as a decimal number `[001,366]`. |
|
||||
| `%m` | Month as a decimal number `[01,12]`. |
|
||||
| `%M` | Minute as a decimal number `[00,59]`. |
|
||||
| `%p` | Locale's equivalent of either AM or PM. |
|
||||
| `%S` | Second as a decimal number `[00,61]`. |
|
||||
| `%U` | Week number of the year (Sunday as the first day of the week) as a decimal number `[00,53]`. |
|
||||
| `%w` | Weekday as a decimal number `[0(Sunday),6]`. |
|
||||
| `%W` | Week number of the year (Monday as the first day of the week) as a decimal number `[00,53]`. |
|
||||
| `%x` | Locale's appropriate date representation. |
|
||||
| `%X` | Locale's appropriate time representation. |
|
||||
| `%y` | Year without century as a decimal number `[00,99]`. |
|
||||
| `%Y` | Year with century as a decimal number. |
|
||||
| `%z` | Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM |
|
||||
| `%Z` | Time zone name (no characters if no time zone exists). |
|
||||
| `%%` | A literal `%` character. |
|
||||
|
||||
## Datetime
|
||||
|
||||
```py
|
||||
import datetime
|
||||
today = datetime.date.today () # returns current date
|
||||
today = datetime.datetime.today () # returns the current date and time
|
||||
|
||||
# formatting example
|
||||
print ('Current Date: {} - {} - {}' .format (today.day, today.month, today.year))
|
||||
print ('Current Time: {}: {}. {}' .format (today.hour, today.minute, today.second))
|
||||
|
||||
var_1 = datetime.date (year, month, day) # create date object
|
||||
var_2 = datetime.time (hour, minute, second, micro-second) # create time object
|
||||
dt = datetime.combine (var_1, var_2) # combine date and time objects into one object
|
||||
|
||||
date_1 = datetime.date ('year', 'month', 'day')
|
||||
date_2 = date_1.replace (year = 'new_year')
|
||||
|
||||
#DATETIME ARITHMETIC
|
||||
date_1 - date_2 # -> datetime.timedelta (num_of_days)
|
||||
datetime.timedelta # duration expressing the difference between two date, time or datetime objects
|
||||
```
|
64
docs/languages/python/modules/unittest.md
Normal file
64
docs/languages/python/modules/unittest.md
Normal file
|
@ -0,0 +1,64 @@
|
|||
# Unittest Module
|
||||
|
||||
```py
|
||||
import unittest
|
||||
import module_under_test
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
def test_1(self):
|
||||
self.assert*(output, expected_output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
```
|
||||
|
||||
## TestCase Class
|
||||
|
||||
Instances of the `TestCase` class represent the logical test units in the unittest universe. This class is intended to be used as a base class, with specific tests being implemented by concrete subclasses. This class implements the interface needed by the test runner to allow it to drive the tests, and methods that the test code can use to check for and report various kinds of failure.
|
||||
|
||||
### Assert Methods
|
||||
|
||||
| Method | Checks that |
|
||||
|-----------------------------|------------------------|
|
||||
| `assertEqual(a, b)` | `a == b` |
|
||||
| `assertNotEqual(a, b)` | `a != b` |
|
||||
| `assertTrue(x)` | `bool(x) is True` |
|
||||
| `assertFalse(x)` | `bool(x) is False` |
|
||||
| `assertIs(a, b)` | `a is b` |
|
||||
| `assertIsNot(a, b)` | `a is not b` |
|
||||
| `assertIsNone(x)` | `x is None` |
|
||||
| `assertIsNotNone(x)` | `x is not None` |
|
||||
| `assertIn(a, b)` | `a in b` |
|
||||
| `assertNotIn(a, b)` | `a not in b` |
|
||||
| `assertIsInstance(a, b)` | `isinstance(a, b)` |
|
||||
| `assertNotIsInstance(a, b)` | `not isinstance(a, b)` |
|
||||
|
||||
| Method | Checks that |
|
||||
|-------------------------------------------------|---------------------------------------------------------------------|
|
||||
| `assertRaises(exc, fun, *args, **kwds)` | `fun(*args, **kwds)` raises *exc* |
|
||||
| `assertRaisesRegex(exc, r, fun, *args, **kwds)` | `fun(*args, **kwds)` raises *exc* and the message matches regex `r` |
|
||||
| `assertWarns(warn, fun, *args, **kwds)` | `fun(*args, **kwds)` raises warn |
|
||||
| `assertWarnsRegex(warn, r, fun, *args, **kwds)` | `fun(*args, **kwds)` raises warn and the message matches regex *r* |
|
||||
| `assertLogs(logger, level)` | The with block logs on logger with minimum level |
|
||||
|
||||
| Method | Checks that |
|
||||
|------------------------------|-------------------------------------------------------------------------------|
|
||||
| `assertAlmostEqual(a, b)` | `round(a-b, 7) == 0` |
|
||||
| `assertNotAlmostEqual(a, b)` | `round(a-b, 7) != 0` |
|
||||
| `assertGreater(a, b)` | `a > b` |
|
||||
| `assertGreaterEqual(a, b)` | `a >= b` |
|
||||
| `assertLess(a, b)` | `a < b` |
|
||||
| `assertLessEqual(a, b)` | `a <= b` |
|
||||
| `assertRegex(s, r)` | `r.search(s)` |
|
||||
| `assertNotRegex(s, r)` | `not r.search(s)` |
|
||||
| `assertCountEqual(a, b)` | a and b have the same elements in the same number, regardless of their order. |
|
||||
|
||||
| Method | Used to compare |
|
||||
|------------------------------|--------------------|
|
||||
| `assertMultiLineEqual(a, b)` | strings |
|
||||
| `assertSequenceEqual(a, b)` | sequences |
|
||||
| `assertListEqual(a, b)` | lists |
|
||||
| `assertTupleEqual(a, b)` | tuples |
|
||||
| `assertSetEqual(a, b)` | sets or frozensets |
|
||||
| `assertDictEqual(a, b)` | dicts |
|
Loading…
Add table
Add a link
Reference in a new issue