Skip to content

Instantly share code, notes, and snippets.

View vubon's full-sized avatar
🎯
Focusing

Vubon vubon

🎯
Focusing
View GitHub Profile
@vubon
vubon / models.py
Last active January 29, 2022 06:01
Django BRIN index in PostgreSQL Database
from django.contrib.postgres.indexes import BrinIndex
from django.db import models
class SampleData(models.Model):
created_at = models.DateTimeField()
class Meta:
"""
You must need to use PostgreSQL DB unless you can't use BrinIndex
"""
@vubon
vubon / dispatch_table.py
Created January 8, 2019 18:16
Dispatch Table example with O(n) and O(1) complexity
"""
## ==== Never create O(n), if you can O(1) ===== #
“A dispatch table is a table of pointers to functions or methods.”
Link: https://en.wikipedia.org/wiki/Dispatch_table?fbclid=IwAR392A7zZELtQxij7w5iHurjdKJxuYbdyOc6_iVBh7pqvr4oeEUr0xLUNpk
"""
import datetime
def monday_task():
@vubon
vubon / pyenv.sh
Created January 28, 2019 07:07
CentOS 7 , Install Pyenv
yum install -y gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel sqlite-devel bzip2-devel
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> .zshrc
echo 'eval "$(pyenv init -)"' >> .zshrc
echo 'eval "$(pyenv virtualenv-init -)"' >> .zshrc
exec $SHELL - l
pyenv install 3.6.0
pyenv global 3.6.0
@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}
@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 / 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 / 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 / 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-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
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 = [
....