Skip to content

Instantly share code, notes, and snippets.

View vskrachkov's full-sized avatar

Viacheslav Krachkov vskrachkov

View GitHub Profile
@vskrachkov
vskrachkov / base_werkzeug_app.py
Created February 4, 2017 16:00
Basic example of Werkzeug application
"""
base_werkzeug_app.py
====================
Example of a simple app written using werkzeug library.
"""
import json
import psycopg2
from werkzeug.exceptions import HTTPException
@vskrachkov
vskrachkov / property_example.py
Created February 5, 2017 13:49
Usage of built-in class `property`
"""
class_property.py
=================
Shows how to use python's built-in class 'property' for implementation of
encapsulation by pythonic way.
Use 'python3 -i class_property' for testing Man class interfaces.
Instance of this class will be already initialized as 'man' variable.
@vskrachkov
vskrachkov / sort.py
Created February 5, 2017 13:57
Examples of bible sort, selection sort, insertion sort and merging sort.
def bubble_sort(a_list):
"""Realizes a bubble sort algorithm.
Time complexity: O(n^2)
"""
for one_pass in range(len(a_list)-1):
for i in range(len(a_list)-1):
if a_list[i] > a_list[i+1]:
a_list[i], a_list[i+1] = a_list[i+1], a_list[i]
return a_list
select t.table_schema, t.table_name,
'TRUNCATE public.' || t.table_name || ' CASCADE;', 'insert into public.'
|| t.table_name || '(' || string_agg(c.column_name, ',') || ') select ' || string_agg(c.column_name, ',') || ' from '
|| 'main.' || substring(t.table_name from 6) || ';'
from information_schema.columns c
join information_schema.tables t on t.table_name = c.table_name and t.table_schema = 'public'
where t.table_schema in ('public') and t.table_type = 'BASE TABLE'
GROUP BY t.table_schema, t.table_name;
SELECT
n.nspname as "schema"
,t.relname as "table"
,c.relname as "index"
,i.indisunique AS "is_unique"
,array_to_string(array_agg(a.attname), ', ') as "columns"
,pg_get_indexdef(i.indexrelid) || ';' as "ddl"
,'DROP INDEX ' || n.nspname || '.' || c.relname || ';' as "delete_ddl"
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
SELECT
tc.constraint_name, tc.table_name, kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
/******************************************************************************
Current example realize many-to-many relationship between product and
customer's purchase order on it in PostgreSQL DBMS.
+~~~~~~~~~+ +~~~~~~~~~~~~~~~~~~~+ +~~~~~~~~~~~~~~~~~~~+
|*product*| | *order_items* | | *purchase_order* |
+~~~~~~~~~+ +~~~~~~~~~~~~~~~~~~~+ +~~~~~~~~~~~~~~~~~~~+
| id | _____ | product_id | __ | id |
+---------+ +-------------------+ / +-------------------+
| name | | purchase_order_id | _/ | customer_address |
+---------+ +-------------------+ +-------------------+
SELECT 'SELECT setval(''main_' || c.relname || ''', nextval(''' || n.nspname || '.' || c.relname || ''') + 20);' FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'S' and n.nspname = 'main';
def discover(package_name):
"""Function that discovers packages for django.
For example, if you want store your models in different files you must
call this function in __init__ file of the models package.
Django app structure:
some_app/
__init__.py
models/
my_model.py
def register_all(app_label):
"""Function for django admin.
Register all app models in django admin.
"""
from django.apps import apps
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered
app_models = apps.get_app_config(app_label).get_models()
for model in app_models: