Others
Decorators
Existing decorators in python
@cache
Cache result of functions.
@cache
def some_func() -> int:
...You can also see the cached info
print(some_func.cache_info())@atexit.register
It will call this decorated function when script terminates, for whatever reason.
import atexit
@atexit.register
def exit_handler() -> None:
print('something')You can also unregister these functions
Walrus Operator
Transform this
to this
This operator firts evaluates the expression or function and than assign it to the variable.
Thousand separator
Aligning output strings
Formatting datetime
Formatting decimals
Showing "var name" in print
Getting Memory Variable Use
Random Functions
shuffle
Shuffles a list in place.
random
Returns a random number between 0 and 1.
randint
Returns a random int number within a specified range [beginning, end].
randrange
Works the same as randint, but with a specified range [beginning, end).
It also let's you provide a step, randrange(beginning, end, step).
choice
Selects a random element from a list.
choices
Selects a specified k number of random elements from a list. (By default 1 element)
You can also provide a weight parameter, which will be a tuple | list of float that will be applied to the elements. (Must be in the same order as the element's list)
Returns a list.
sample
Does the same as choices, but it only retuns unique elements. (Each will only appear once)
This means k must be greater or equal to the length of the provided list.
If the element is repeated inside the provided list, IT CAN repeat.
seed
It will "save" the state of the other random functions, so that, they will reproduce the same result if necessary.
Last updated