Skip to content

Instantly share code, notes, and snippets.

View Rustam-Z's full-sized avatar
🚀
Cracking...

Rustam Zokirov Rustam-Z

🚀
Cracking...
View GitHub Profile
@Rustam-Z
Rustam-Z / timer.py
Last active August 9, 2021 10:57
A decorator that prints how long a function took to run.
import time
from functools import wraps
def timer(func):
"""A decorator that prints how long a function took to run."""
@wraps(func)
def wrapper(*args, **kwargs):
# When wrapper() is called, get the current time.
t_start = time.time()
@Rustam-Z
Rustam-Z / counter.py
Created August 9, 2021 10:37
Counting how many times function was called
def counter(func):
def wrapper(*args, **kwargs):
wrapper.count += 1
# Call the function being decorated and return the result
return func(*args, **kwargs)
wrapper.count = 0
# Return the new decorated function
return wrapper
# Decorate foo() with the counter() decorator
def print_return_type(func):
# Define wrapper(), the decorated function
def wrapper(*args, **kwargs):
# Call the function being decorated
result = func(*args, **kwargs)
print('{}() returned type {}'.format(
func.__name__, type(result)
))
return result
# Return the decorated function
@Rustam-Z
Rustam-Z / doc_string.py
Last active August 9, 2021 10:44
How to write docstrning of Python function.
# Docstring - Google style
def function(arg_1, arg_2=42, some_function):
"""Descriptioin of what this function does.
Args:
arg_1 (str): Description of arg_1.
(The string to search).
arg_2 (int, optional): Write optional when
argument has default value.
some_function (callable): The function we want a tooltip for.
int call(int rnum){
// rnum is parameter
printf("the num is %d", rnum);
}
int num = 20;
call(num);
// num is argument
@Rustam-Z
Rustam-Z / infinite_sequence.py
Created August 9, 2021 04:53
Makes the infinite sequence using generator.
def infinite_sequence():
"""Makes the infinite sequence with Python generator.
"""
num = 0
while True:
yield num
num += 1