Skip to content

Instantly share code, notes, and snippets.

View robcowie's full-sized avatar

Rob Cowie robcowie

  • Recycleye
  • Leeds/London, United Kingdom
View GitHub Profile
@robcowie
robcowie / postgresql_management_queries.sql
Last active May 22, 2021 16:28
Useful Postgresql Management Queries
-----------------------------------------------------------------------------------------------------------------------------------
-- USEFUL POSTGRESQL MANAGEMENT QUERIES --
-- See https://medium.com/compass-true-north/dealing-with-significant-postgres-database-bloat-what-are-your-options-a6c1814a03a5 --
-----------------------------------------------------------------------------------------------------------------------------------
-- SHOW RUNNING QUERIES
SELECT
state,
pid,
@robcowie
robcowie / fix_unicode.py
Created March 22, 2020 15:41
Fix unicode. Python.
# -*- coding: utf-8 -*-
"""OLD AND UNTESTED"""
import unicodedata
def fix_bad_unicode(text):
u"""
Something you will find all over the place, in real-world text, is text
that's mistakenly encoded as utf-8, decoded in some ugly format like
@robcowie
robcowie / pg_vacuuming_queries.md
Last active February 18, 2020 13:43
PostgreSQL vacuuming monitoring queries

Postgresql Vacuuming

To monitor progress of autovacuum

SELECT
    heap_blks_scanned/cast(heap_blks_total as numeric) * 100 as heap_blks_percent,
    progress.*,
    activity.query
FROM
@robcowie
robcowie / marketplace.py
Created November 4, 2019 15:15
Amazon marketplaces with api endpoints
"""Amazon Marketplace and Region Info.
* [MWS docs](https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html)
* [Advertising API docs](https://advertising.amazon.com/API/docs/en-us/get-started/how-to-use-api)
"""
from dataclasses import dataclass
@dataclass
@robcowie
robcowie / circleci.yml
Created October 25, 2019 16:41
Example circleci config for running tests with a postgresql DB
version: 2.1
jobs:
test-cf-audit-report-data:
docker:
- image: circleci/python:3.7.1
- image: circleci/postgres:11.4-alpine-ram
environment:
POSTGRES_USER: user
POSTGRES_DB: test
@robcowie
robcowie / install_lsp_sublime.md
Last active August 29, 2022 19:19
Install and configure the python language server for Sublime Text 3 (using the LSP plugin)

Install and configure Python LSP & Sublime Text 3

Install the langauge server

This installs the python server provided by Palantir.

Microsoft also provide the C# server which requires the .net runtime.

pipenv install 'python-language-server[all]'
@robcowie
robcowie / sqlalchemy_create_statement.py
Last active August 30, 2019 09:41
Print create SQL statement for tales and indices defined with SQLAlchemy
from sqlalchemy import create_engine, Table, MetaData
from sqlalchemy.schema import CreateIndex, CreateTable
metadata = MetaData()
engine = create_engine('')
def create_table_statement(connection, table):
stmts = []
@robcowie
robcowie / postgresql_notes.md
Created April 6, 2019 15:32
Notes and tips for using Postgresql

PostgreSQL Notes

Column Names

All identifiers are coerced to lower-case so column names are effectively case-insensitive. To avoid this, quote the identifier with double quotes.

SELECT "myColA" FROM "camelCaseTable";
@robcowie
robcowie / config_arg_parser.py
Created February 18, 2019 10:00
ArgumentParser and argparse Action that can pull args from a yaml config file
# -*- coding: utf-8 -*-
"""
Part of the undertime app https://gitlab.com/anarcat/undertime by Antoine Beaupré.
AGPLv3 licence (https://gitlab.com/anarcat/undertime/blob/master/LICENSE)
"""
import argparse
import os
@robcowie
robcowie / ec2_operator.py
Created February 11, 2019 20:02
EC2 Airflow Operator
# -*- coding: utf-8 -*-
"""
NOTE THIS IS UNTESTED AS IT WAS NOT REQUIRED.
See:
- https://github.com/apache/airflow/blob/master/airflow/contrib/hooks/aws_hook.py
- https://github.com/apache/airflow/blob/master/airflow/contrib/operators/ecs_operator.py
- https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.ServiceResource.create_instances
- https://stackabuse.com/automating-aws-ec2-management-with-python-and-boto3/