Skip to content

Instantly share code, notes, and snippets.

View mccarthysean's full-sized avatar

Sean McCarthy mccarthysean

View GitHub Profile
@plockaby
plockaby / database.py
Last active January 7, 2024 15:49
Python Flask Connection Pool
import logging
import psycopg2
from psycopg2.extras import RealDictCursor
from psycopg2.extensions import TRANSACTION_STATUS_UNKNOWN, TRANSACTION_STATUS_IDLE
from flask import g
import threading
import tenacity
import uuid
import pwd
import os
@nickoala
nickoala / gpspipe.md
Last active March 11, 2023 18:47
Log GPSD Outputs and Extract Lat/Lon

Log GPSD Outputs and Extract Lat/Lon

$ gpspipe -w             # log to stdout
$ gpspipe -w -o abc.log  # log to a file

To auto-log on startup, insert the following line to the file /etc/rc.local:

@thomas15v
thomas15v / docker.compose.yml
Last active June 22, 2022 09:41
traefik.io nginx example
version: '2'
services:
nginx:
image: nginx:alpine
restart: always
labels:
- "traefik.enable=true"
- 'traefik.frontend.rule=Host:www.website.com'
- "traefik.port=80"
volumes:
@Havoc24k
Havoc24k / sync.py
Last active September 29, 2023 08:55
Import tables from one PostgreSQL database to another using the INFORMATION_SCHEMA
#!/usr/bin/python3.6
"""Sync PostgreSQL."""
import psycopg2
import sys
from psycopg2.extras import RealDictCursor, execute_values
"""
Usage:
@dariodip
dariodip / Dockerfile
Created September 21, 2017 09:45
Cython in a Docker container
FROM python:3.6
WORKDIR /app
ADD . /app
RUN pip install -r requirements.txt
RUN python setup.py build_ext --inplace
ENTRYPOINT ["python"]
CMD ["app.py"]
@ellisvalentiner
ellisvalentiner / bulk-insert.py
Last active January 10, 2023 19:03
Recipe for (fast) bulk insert from python Pandas DataFrame to Postgres database
#!/usr/bin/env/python
import psycopg2
import os
from io import StringIO
import pandas as pd
# Get a database connection
dsn = os.environ.get('DB_DSN') # Use ENV vars: keep it secret, keep it safe
conn = psycopg2.connect(dsn)
@gordol
gordol / example.py
Created August 23, 2016 23:31
Complex Flask-Admin View with custom filters and bulk action
class FilterLowercase(BaseMongoEngineFilter):
def apply(self, query, value):
flt = {'%s__icontains' % self.column: value}
return query.filter(**flt)
def operation(self):
return gettext('contains')
class FilterLowercaseNot(BaseMongoEngineFilter):
def apply(self, query, value):
@sysradium
sysradium / flask_logging.py
Created February 25, 2016 22:12
Logging Flask errors to console to use it with docker and gunicorn
import logging
import flask
app = flask.Flask(__name__)
@app.before_first_request
def setup_logging():
if not app.debug:
# In production mode, add log handler to sys.stderr.
app.logger.addHandler(logging.StreamHandler())
@pklaus
pklaus / README.md
Last active July 6, 2023 13:50
Generating Random MAC Addresses with Python

The mini-tool has a CLI-Interface with the following options:

  • Unicast or Multicast? Default: Unicast
  • Locally Administered or Globally Unique? Default: Locally Administered
  • Prescribe specific OUI (overwrites the above two)

TODO

  • Add an option to generate a number of MACs without collisions.
@mrjoes
mrjoes / ckedit.py
Created March 18, 2013 19:04
Flask-Admin and CKEditor WYSIWYG textarea integration. Basically, all you have to do: 1. Create new wtforms widget which will emit 'ckeditor' class 2. Make new wtforms field which will use this widget 3. Create new jinja2 template, which includes ckeditor javascript 4. Tell flask-admin to use new field and new template
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext import admin, wtf
from flask.ext.admin.contrib import sqlamodel
app = Flask(__name__)
app.config['SECRET_KEY'] = '123456790'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite'
db = SQLAlchemy(app)