Skip to content

Instantly share code, notes, and snippets.

View vubon's full-sized avatar
🎯
Focusing

Vubon vubon

🎯
Focusing
View GitHub Profile
@vubon
vubon / performance.py
Created February 18, 2022 03:39
Get usage memory & execution time
import tracemalloc
from time import perf_counter
def performance(func):
def wrapper(*args, **kwargs):
tracemalloc.start()
start_time = perf_counter()
func(*args, **kwargs)
current, peak = tracemalloc.get_traced_memory()
finish_time = perf_counter()
@vubon
vubon / logger.py
Created July 16, 2020 10:07
Python logging example with Time Rotating File Handler
import os
import logging
import logging.config
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOGGER = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
@vubon
vubon / Common_PostgreSQL.sql
Last active April 26, 2021 04:24
Few RAW SQL
-- Get Database Size
select pg_size_pretty(pg_database_size('<DB Name>'));
-- Get Table Size
select pg_size_pretty(pg_table_size('<Table SIze>'));
-- Delete a small table
delete from table_name;
-- Delete a big table
TRUNCATE TABLE table_name;
-- Count row of a table
from functools import partial
from django.db.models import signals
class WhoDidMiddleware(object):
"""
his class represent as catch request user and mark the user into that model.
Usage:
MIDDLEWARE = [
....
@vubon
vubon / property-5.py
Created September 14, 2019 14:10
Python Property function part 5
class Account:
""" A basic account information store class """
def __init__(self, bank_account: str, balance: float):
self.bank_account = bank_account
self.balance = balance
self._account_information = dict(bank_account=self.bank_account, balance=self.balance)
# Just declare the @property decorator and solve those problems.
@property
@vubon
vubon / property-4.py
Created September 14, 2019 14:08
Python Property function part 4
# .......
# Just declare the @property decorator and solve those problems.
@property
def account_information(self) -> dict:
"""
Get account information
:return: {'bank_account': '0004-0067894712', 'balance': 1000.5}
:rtype: dict
"""
return dict(bank_account=self.bank_account, balance=self.balance)
@vubon
vubon / property-3.py
Created September 14, 2019 14:06
Python Property function part 3
class Account:
""" A basic account information store class """
def __init__(self, bank_account: str, balance: float):
self.bank_account = bank_account
self.balance = balance
# self.account_information = dict(bank_account=self.bank_account, balance=self.balance)
def account_information(self) -> dict:
"""
@vubon
vubon / property2.py
Created September 14, 2019 14:04
Python Property function part 2
# Fund transfer process
account.balance = account.balance - 500.25
# Now call the account_information attribute
print("Calling the account_information attribute")
print(f"Account information: {account.account_information}")
# calling the balance attribute
print(f"Current balance: {account.balance}")
# Output
@vubon
vubon / property1.py
Created September 14, 2019 14:00
Python Property function part 1
class Account:
""" A basic class that store bank account information """
def __init__(self, bank_account: str, balance: float):
self.bank_account = bank_account
self.balance = balance
self.account_information = dict(bank_account=self.bank_account, balance=self.balance)
def __str__(self):
return f"Bank Account: {self.bank_account} - balance: {self.balance}"
@vubon
vubon / pg_backup.sh
Last active May 25, 2020 21:03
PostgreSQL Database Backup shell script
BACKUP_DIR=<back up location>
DAYS_TO_KEEP=14
FILE_SUFFIX=_db_suffix.sql
DATABASE=<Databae Name>
USER=<Database username >
FILE=`date +"%Y%m%d%H%M%S"`${FILE_SUFFIX}
OUTPUT_FILE=${BACKUP_DIR}/${FILE}