This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
base_werkzeug_app.py | |
==================== | |
Example of a simple app written using werkzeug library. | |
""" | |
import json | |
import psycopg2 | |
from werkzeug.exceptions import HTTPException |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
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 | | |
+---------+ +-------------------+ +-------------------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
OlderNewer