Skip to content

Instantly share code, notes, and snippets.

View pouyamn's full-sized avatar

Pouya MN pouyamn

View GitHub Profile
@pouyamn
pouyamn / bobp-python.md
Created January 16, 2019 14:32 — forked from sloria/bobp-python.md
A "Best of the Best Practices" (BOBP) guide to developing in Python.

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@pouyamn
pouyamn / BIT.py
Created May 31, 2019 13:14
Algorithms / Binary Indexed Tree
# Python implementation of Binary Indexed Tree
# sum of elements and update both cost O(log(n))
# Returns sum of arr[0..index]. This function assumes
# that the array is preprocessed and partial sums of
# array elements are stored in BITree[].
def getsum(BITTree,i):
s = 0 #initialize result
# index in BITree[] is 1 more than the index in arr[]
@pouyamn
pouyamn / Data structures in Python.md
Last active May 31, 2019 18:20
Data structures in Python

Lists (Dynamic Array)

Operation Example Class Notes
Index l[i] O(1)
Store l[i] = 0 O(1)
Length len(l) O(1)
Append l.append(5) O(1) mostly: ICS-46 covers details
Pop l.pop() O(1) same as l.pop(-1), popping at end

Sorting

Bubble Sort: Iterate through entire list while comparing pairs and swap positions based on their values until all elements sorted.

O(n²), but fast if list is almost sorted.

Insertion Sort: Iterates through unsorted list while building a sorted list. For each value encountered in unsorted list, find appropriate place in sorted list and insert it.

O(n²).

Deploying database for multiple docker app stacks

When there are multiple apps on a server using same database, e.g. postgres, there are several deployment options:

  1. Each app can have a database container for each own
  2. There could be a single database container and a shared network for all apps
  3. using a single docker-compose.yml for all apps having one database container,
  4. database can be installed on local host with different users for each apps

When using single DB, i.e. options 2-4, you have to decied for using same creditentials for all apps, or make DB users per apps. The former requires less work but may cause interference and debug can be more complicated, the latter is easier to implement but less error prune. I suggest to have shared user for every type of apps. If you have 5 instances of app A with different databases, and 3 instances of app B, you can make 2 shared users for each. Of course shared database means you have to know how your apps interact with database and you are respo

@pouyamn
pouyamn / instanses_and_classes.md
Created August 2, 2019 10:06
Yet another understanding Python classes and instanses, using a simulated interpreter approach [WIP]

Yet another understanding Python classes and instanses, using a simulated interpreter approach

class Foo:
  bar = "class attribute"
  
  def __init__(self):
    self.ham="instance attribute"
  
  def baz(self):
    self.baz = "changed instance attribute"
@pouyamn
pouyamn / g2j
Created October 14, 2019 05:12 — forked from omidp/g2j
postgresql persian to gregorian function
select g2j(now())
CREATE OR REPLACE FUNCTION g2j(in_date timestamp with time zone)
RETURNS character varying AS
$BODY$
DECLARE
y smallint;
aday smallint;
amonth smallint;
ayear smallint;