Skip to content

Instantly share code, notes, and snippets.

Avatar

Rob Cowie robcowie

  • Recycleye
  • Leeds/London, United Kingdom
View GitHub Profile
@robcowie
robcowie / stub_event_source.py
Last active May 18, 2022
Stub event sources for consumption by stream processing code
View stub_event_source.py
# loop = asyncio.get_event_loop()
# loop.run_until_complete(event_source_async(your_handler))
import asyncio
import random
import time
def randint():
return random.randint(-100, 100)
@robcowie
robcowie / gh_dependabot.sh
Last active Oct 17, 2021
gh cli commands to work with Dependabot PRs
View gh_dependabot.sh
# Github CLI commands to work with dependabot PRs
# List dependabot PRs that need review
gh pr list -l dependencies --search "status:success review:none"
gh pr list -A app/dependabot --search "status:success review:none"
# Instruct dependabot to merge all reviewed PRs, oldest first
gh pr list \
-A app/dependabot \
--search "status:success review:approved" \
@robcowie
robcowie / foreign_keys_without_indexes.sql
Created Oct 15, 2021
Find foreign keys that do not have an index on them in Postgresql
View foreign_keys_without_indexes.sql
-- Show all foreign keys that do not have an index on them
-- Potential performance problem if the join is performed often
SELECT
tc.table_name,
kcu.column_name,
tc.constraint_name
FROM
information_schema.table_constraints AS tc
JOIN
@robcowie
robcowie / Readme.md
Created Oct 3, 2021 — forked from dornfeder/Readme.md
Git Branching Diagram Template
View Readme.md

Git Branching Diagram Template for diagrams.net

This is my own variant of a git branching diagram template based on Bryan Braun's great template.

You can use this diagram as a template to create your own git branching diagrams. Here's how:

  1. Create a new diagram with diagrams.net (formerly draw.io)
  2. Go to File > Open From > URL
  3. Insert this url (it points to the xml data below): https://gist.githubusercontent.com/dornfeder/13abff279de357f048e474d4ed6c692d/raw/f5efc838bd34429e59e38063bd348b7a86457d18/git-diagram-template.xml
  4. Customize as needed for your team.
View git-branching-diagram.md

Example Git Branching Diagram

You can use this diagram as a template to create your own git branching diagrams. Here's how:

  1. Create a new diagram with diagrams.net (formerly draw.io)
  2. Go to File > Open From > URL
  3. Insert this url (it points to the xml data below): https://gist.githubusercontent.com/bryanbraun/8c93e154a93a08794291df1fcdce6918/raw/0378fda86483def0a9680270c02907a9cbaf7615/template-data.xml
  4. Customize as needed for your team.

@robcowie
robcowie / README.md fastapi with python 3.7 dataclasses - used to create both sqlalchemy and pydantic models simultaneously
View README.md

cmdline

poetry run gunicorn testpg:app -p 8080 --preload --reload --reload-engine inotify -w 10 -k uvicorn.workers.UvicornWorker --log-level debug --access-logfile - --error-logfile - --access-logformat "SSSS - %(h)s %(l)s %(u)s %(t)s \"%(r)s\" %(s)s %(b)s \"%(f)s\" \"%(a)s"

How to quickly run postgres (using docker)

docker run --network="host" -it --rm --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -e PGDATA=/var/lib/postgresql/data/pgdata -v /tmp/pgdata2:/var/lib/postgresql/data -e POSTGRES_USER=test postgres

This command will quickly start postgres on port 5432 and create a database test with user test and password mysecretpassword

View python_dict_donfig_example.py
import logging
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'sane': {
'format': '%(levelname)-8s [%(asctime)s] %(name)s: %(message)s'
},
},
@robcowie
robcowie / versioning_trigger.sql
Last active Nov 13, 2020
Postgresql trigger to write snapshots to a history table
View versioning_trigger.sql
CREATE OR REPLACE FUNCTION versioning() RETURNS TRIGGER AS $$
DECLARE
hist_tbl text := TG_TABLE_NAME || '_history';
BEGIN
IF (TG_OP = 'UPDATE') THEN
NEW.sys_period = tstzrange(CURRENT_TIMESTAMP, NULL);
OLD.sys_period = tstzrange(lower(OLD.sys_period), CURRENT_TIMESTAMP);
EXECUTE format('INSERT INTO %I SELECT ($1).*', hist_tbl) USING OLD;
RETURN NEW;
@robcowie
robcowie / rounded_div_function.sql
Created Oct 12, 2020
Postgresql division with rounding function
View rounded_div_function.sql
-- Handle zero-division by returning null. Round result to N decimal places
CREATE OR REPLACE FUNCTION rounded_safe_div(a numeric, b numeric, dp int = 2) RETURNS numeric AS
$$ SELECT round($1::numeric / nullif($2, 0), $3) $$
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
@robcowie
robcowie / amazon_allowed_chars.py
Created Jul 22, 2020
Allowed characters for Amazon entities and keywords
View amazon_allowed_chars.py
# From https://advertising.amazon.com/API/docs/en-us/get-started/how-to-use-api#Keyword-bid-constraints-by-marketplace
# Broken decoding fixed, mostly. We've lost a few characters but they seem obscure.
# See http://www.i18nqa.com/debug/utf8-debug.html for explanation
ALLOWED_ENTITY_CHARS = {
"'",
' ',
'!',
'"',
'#',