Skip to content

Instantly share code, notes, and snippets.

View gordol's full-sized avatar

Gordo Lowrey gordol

View GitHub Profile
@gordol
gordol / zha_lutron_aurora_blueprint.yaml
Created November 29, 2022 04:20 — forked from bjpetit/zha_lutron_aurora_blueprint.yaml
Blueprint for Lutron Aurora automations in ZHA
blueprint:
name: ZHA - Lutron Aurora Dimmer v1.3
description: 'Control lights with a Lutron Aurora Dimmer
Pressing in the dimmer button will toggle between turning lights on
to full brightness, and turning the lights off.
Rotating the dimmer will increase and decrease the light brightness.
Adjust the sensitivity if updates from the dimmer are being sent too quickly
'
domain: automation
@gordol
gordol / CaseClass.py
Created March 22, 2021 21:54 — forked from breeko/CaseClass.py
Scala's case class built out for Python
def CaseClass(case_class_name, **kwargs):
types = {}
for k, v in kwargs.items():
if type(v) is not type:
raise ValueError("{} is not a type".format(v))
types[k] = v
class CaseClassImpl():
initialized = False
@gordol
gordol / test.sql
Created November 4, 2020 22:31
Corrupt data landing in s3 via aws_s3.query_export_to_s3 extension
-- When using aws_s3.query_export_to_s3, on Aurora Postgres, version 11, data is reliably and predictably corrupted at certain positions of the payloads that land in s3.
-- It seems that when data falls over a certain byte length, it is reset and resent mid-stream to s3.
-- We have been able to reproduce this positively, as well as reproduce a negative as well; ensuring that data falls at powers of 2 on even byte lengths seems to prevent this issue from occurring.
-- As the length of data increases, as does the frequency of the corruption. For small data sets, it may never surface.
-- You will see upon running this SQL that it occurs once in 100,000 records with 128+9 byte length records, twice for 256+9, and 4 times for 512+9, and so-on, where each record looks like {"A":""}\n where \n is a new line control character, amounting to 9 extra bytes on top of the value in the JSON objects.
-- these queries will result in broken json in s3
select * from aws_s3.query_export_to_s3(
'select row_to_json(test) fr
@gordol
gordol / flatten.py
Last active October 9, 2019 00:50
Example code that flattens an arbitrarily nested array of integers into a flat array of integers.
#!/usr/bin/env python3
import unittest
def flattenIntArray(arr):
'''Yield flattened elements from input array'''
for element in arr:
try:
yield from flattenIntArray(element)
@gordol
gordol / prime-challenge-numpy.py
Last active September 28, 2019 07:22
A simple little module to complete the prime number challenge. Includes 4 tests. This variant is using NumPy. To run the tests, run: `python -m unittest prime-challenge-numpy`
#!/usr/bin/env python
import unittest
from sympy import sieve
import numpy as np
def generatePrimes(num_primes):
"""
Returns an array of primes.
@gordol
gordol / prime-challenge.py
Last active September 28, 2019 06:02
A simple little module to complete the prime number challenge. Includes 4 tests. This could be made to be much much faster if we used PyPy and NumPy, but the challenge was to utilize the standard library only. To run the tests, run: `python -m unittest prime-challenge`
#!/usr/bin/env python
import itertools
import unittest
def primeGenerator():
"""
Yields an array of primes.
"""
@gordol
gordol / udp-punch.py
Created October 4, 2017 11:02
Automated Bidirectional UDP NAT Traversal via SSH Wizardry
#!/usr/bin/env python3
"""
This is a 100% self-contained script to facilitate automated creation of bidirectional UDP pinholes.
It should work as a non-root user assuming you use a high port number.
All that is necessary is an SSH server with a Python environment.
This script is ran on the client, and then the client runs it on the server dynamically.
No permanent changes are made to the server.
Basically we just open UDP connection from both ends, to the same port, and then exchange some tokens to verify connectivity.
ModelView
---------
# field1 (->Model1), field2 (->Model2) filtered by field1 values
column_filter_by = ('field2')
form_widget_args = {
'field2': {
'data-filter-by': 'field1',
}
}
@gordol
gordol / pjl_generator.py
Created September 14, 2016 02:23
Generates commands to configure network settings on Brother Label Printers
#!/usr/bin/env python
def str_to_hex(string):
return "-".join("{:02x}".format(ord(c)) for c in str(string))
def chunks(s, n):
for start in range(0, len(s), n):
yield s[start:start+n]
delimiter = '%-12345X@PJL'
@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):