Skip to content

Instantly share code, notes, and snippets.

View mzpqnxow's full-sized avatar
🙈
Look at all this free stuff!

AG mzpqnxow

🙈
Look at all this free stuff!
View GitHub Profile
@mzpqnxow
mzpqnxow / description.md
Created March 13, 2021 14:52 — forked from mangecoeur/description.md
Pandas PostgresSQL support for loading to DB using fast COPY FROM method

This small subclass of the Pandas sqlalchemy-based SQL support for reading/storing tables uses the Postgres-specific "COPY FROM" method to insert large amounts of data to the database. It is much faster that using INSERT. To acheive this, the table is created in the normal way using sqlalchemy but no data is inserted. Instead the data is saved to a temporary CSV file (using Pandas' mature CSV support) then read back to Postgres using Psychopg2 support for COPY FROM STDIN.

@mzpqnxow
mzpqnxow / sql.py
Created March 9, 2021 01:29 — forked from jorisvandenbossche/sql.py
Patched version of pandas.io.sql to support PostgreSQL
"""
Patched version to support PostgreSQL
(original version: https://github.com/pydata/pandas/blob/v0.13.1/pandas/io/sql.py)
Adapted functions are:
- added _write_postgresql
- updated table_exist
- updated get_sqltype
- updated get_schema
@mzpqnxow
mzpqnxow / map-cipher-suites.py
Last active September 12, 2021 00:31
Generate a mapping for IANA, OpenSSL and the int16 representation of SSL/TLS cipher-suites
#!/usr/bin/env python3
#
# Parse https://testssl.sh/openssl-iana.mapping.html or a copy/paste of
# the content from a browser, drop it in a line-based JSON file
#
# This lets you quickly translate from IANA<->OpenSSL<->int16 which is
# useful for unusual people :>
#
# Requirements: lxml, pandas>=1.2
# If Pandas is too old, you will get:
@mzpqnxow
mzpqnxow / sqlalchemy_masklen.py
Created February 9, 2021 14:35
PostgreSQL with SQLALchemy: INET/CIDR masklen function
#!/usr/bin/env python3
"""Small function to use PostgreSQL MASKLEN() with SQLALchemy
If you work regularly with SQLAlchemy and know a cleaner way to do this, please
leave a comment on this Gist! Thanks
(C) 2019, mzpqnxow, BSD 3-Clause
"""
from typing import Union
@mzpqnxow
mzpqnxow / bulkupsert.py
Created February 5, 2021 19:05 — forked from luke/bulkupsert.py
I needed to upsert (insert or update) bajillions of records into postgresql. After trying various libs including upsert (which was slow as hell) I ended up doing a bit of research and trying 3 different methods. This one won. While I'm manually building the sql string no user data is passed in. Its loaded via the copy from statement as CSV. Call…
import logging
import cStringIO
import csv
DEBUG = False
def data2csv(data):
si = cStringIO.StringIO()
cw = csv.writer(si, delimiter='\t',lineterminator="\n")
for row in data:
@mzpqnxow
mzpqnxow / lzmalogger.py
Last active January 5, 2021 19:57
Python LzmaRotatingFileHandler: Simple extension of RotatingFileHandler that using LZMA compression after rolling a log file
import logging.handlers
import lzma
import os
"""
This can be done a few different ways. You can also implement it as a standalone
function, instnatiate a standard logging.handlers.RotatingFileHandler, and then
assign your own rotator() function to to the `rotator` attribute of the instance
I think it's more convenient encapsulating it like this, though
-AG
@mzpqnxow
mzpqnxow / gist:328118cc427342b1bb0305e10ba2e572
Created November 26, 2020 17:24
name_value_consolidate_to_true_dict.py
def kv_pair_list_to_map(self, obj, name_key, value_key='value'):
"""Return a dictionary from a list of fixed name/value keyed dicts
This handles the common "xml-style" storage of values in JSON
It also merges into lists as appropriate
Example Usage (for below input)
===============================
map_obj = kv_pair_list_to_map(obj, 'var_name', value_key='value')
@mzpqnxow
mzpqnxow / crackdn.py
Created November 5, 2020 09:27
Crack a distinguished name string in Python
def crackdn(dn_str):
"""Crack a DN string into a dict
'C=US, ST=New Jersey, L=Newark, O=The Dump, Inc., CN=www.thedump.newark'
... Becomes ...
{
"C": "US",
"ST": "New Jersey",
@mzpqnxow
mzpqnxow / jsonencoder.py
Created October 23, 2020 23:20
JSON encoder for standard Python json package
import codecs
import json
import datetime
from uuid import UUID
class UniversalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime.datetime, datetime.date, datetime.time)):
return obj.isoformat()
@mzpqnxow
mzpqnxow / dnspython_json.py
Last active February 8, 2021 00:03
JSON encode/serialize dnspython Answer objects and print as a string, native Python objects, or to disk
# I have made this into a small package, please see https://github.com/mzpqnxow/dnspyjson