Skip to content

Instantly share code, notes, and snippets.

View wellic's full-sized avatar

Valerii Savchenko wellic

View GitHub Profile
@wellic
wellic / db_utils.py
Created October 22, 2021 23:14 — forked from obeattie/db_utils.py
Exposes SQLAlchemy's sessions and transactions as context managers (so they will be managed automatically inside blocks), and also provides a transaction decorator, which wraps an entire function in a transaction
"""Utilities for managing database sessions."""
from __future__ import with_statement
import contextlib
import functools
@contextlib.contextmanager
def temp_session(session_cls, **kwargs):
"""Quick and dirty context manager that provides a temporary Session object
to the nested block. The session is always closed at the end of the block.
@wellic
wellic / sqlalchemy_nested_example.py
Created October 22, 2021 22:42 — forked from snorfalorpagus/sqlalchemy_nested_example.py
SQLAlchemy nested transactions
"""
This script demonstrates the use of nested transactions in SQLAlchemy, including
a workaround for an issue with the SQLite backend.
References:
http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#using-savepoint
http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#serializable-isolation-savepoints-transactional-ddl
"""
from sqlalchemy import Column, String, Integer
@wellic
wellic / rownum.sql
Created October 8, 2021 12:19 — forked from tototoshi/rownum.sql
Grouped LIMIT in PostgreSQL: show the first N rows for each group
-- http://stackoverflow.com/questions/1124603/grouped-limit-in-postgresql-show-the-first-n-rows-for-each-group
-- http://www.postgresql.jp/document/9.2/html/tutorial-window.html
CREATE TABLE empsalary (
depname varchar(10) not null
, empno integer not null
, salary integer not null
);
INSERT INTO empsalary (depname, empno, salary) VALUES ('develop', 11, 5200);
@wellic
wellic / 00-setup.sql
Created October 7, 2021 09:39 — forked from apneadiving/00-setup.sql
Tree structure query with PostgreSQL
-- Create tables
CREATE TABLE groups (id serial NOT NULL, name character varying NOT NULL);
CREATE TABLE network_group_members (pid integer, cid integer NOT NULL);
-- Insert the groups
INSERT INTO groups (name) VALUES ('GROUP 1'),('GROUP 2'),('GROUP 3'),('GROUP 4'),('GROUP 5'),('GROUP 6'),('GROUP 7'),('GROUP 8'),('GROUP 9'),('GROUP 10'),('GROUP 11'),('GROUP 12'),('GROUP 13');
-- Build the "Network"
INSERT INTO network_group_members(pid,cid) VALUES (1,2),(1,3),(1,4),(2,5),(5,6),(5,7),(7,8),(3,9),(4,10),(4,11),(11,12),(12,13);
@wellic
wellic / localstack.md
Created October 6, 2021 14:13 — forked from lobster1234/localstack.md
Working with localstack on command line

Starting localstack

C02STG51GTFM:localstack mpandit$ make infra
. .venv/bin/activate; exec localstack/mock/infra.py
Starting local dev environment. CTRL-C to quit.
Starting local Elasticsearch (port 4571)...
Starting mock ES service (port 4578)...
Starting mock S3 server (port 4572)...
Starting mock SNS server (port 4575)...
@wellic
wellic / docker-compose.yml
Last active August 4, 2023 09:59
httpx proxy to *.docker
version: '2'
services:
http-proxy:
image: nginxproxy/nginx-proxy
container_name: http-proxy
privileged: true
environment:
# - DNS_IP=127.0.0.1
@wellic
wellic / uuid_regex.py
Created July 6, 2021 09:08 — forked from kgriffs/uuid_regex.py
UUID regular expressions (regex) with usage examples in Python
import re
# RFC 4122 states that the characters should be output as lowercase, but
# that input is case-insensitive. When validating input strings,
# include re.I or re.IGNORECASE per below:
def _create_pattern(version):
return re.compile(
(
'[a-f0-9]{8}-' +
@wellic
wellic / Dockerfile
Last active February 26, 2021 10:41
Console debug from docker
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND="noninteractive"
WORKDIR /tmp
RUN apt-get update \
&& apt-get install -y software-properties-common \
&& add-apt-repository ppa:ondrej/php \
&& add-apt-repository ppa:ondrej/apache2 \
@wellic
wellic / getter_and_setter.py
Created December 21, 2020 10:22 — forked from luhn/getter_and_setter.py
Using getters and setters with SQLAlchemy
class Table(Base):
id = Column(Integer, primary_key=True)
_name = Column('name', String(24))
@property
def name(self):
return self._name;
@name.setter
def name(self, value):
@wellic
wellic / deadsnakes-python38-ubuntu-bionic.md
Created October 13, 2020 06:28
Deadsnakes python 3.8 on Ubuntu 18.04 LTS

Deadsnakes python 3.8.0 on Ubuntu 18.04 LTS

The deadsnakes PPA make the latest stable versions of python available on LTS distributions. I now find it preferable to installing from source, whether from download or using pyenv.

The following was tested on a stock Ubuntu 18.04.3 LTS desktop with python 2.7.15 and 3.68 as the shipping system python versions. The pip3 binary was install using the stock python3-pip package, updated with pip3 install --upgrade pip.

One of the key reasons this works for me is that I've been aggressive about individually virtualizing project environments with venv.

First, add the deadsnakes PPA to apt: