Skip to content

Instantly share code, notes, and snippets.

@jvanasco
jvanasco / zone_updates.py
Last active May 10, 2024 17:21
Cloudflare Zone Updates - migrate an IP address across your account, and enable CT monitoring
"""
Cloudflare Zone Updates - migrate an IP address across your account, and enable CT monitoring
Usage:
This expects the Cloudflare object to be configured with an API token
as-is, this can be run if the token is exported into the environment
otherwise, the CF object must be manually created and use an api token
*api keys will not work*
I ran into a ephemeral non-repeatable issues on the CT updates on some requests.
@jvanasco
jvanasco / replace_domain.py
Last active June 27, 2024 21:24
replace acme-dns random domains with custom domains.
"""
This is deprecated.
It is now maintained as part of peter_sslers:
* https://github.com/aptise/peter_sslers/blob/main/tools/replace_domain.py
"""
from __future__ import print_function
import os
import sqlite3
import sys
@jvanasco
jvanasco / utcnow.py
Created August 21, 2020 21:36
making sure we send the right timestamp...
import sqlalchemy.types
from sqlalchemy.sql import expression
from sqlalchemy.ext.compiler import compiles
class utcnow(expression.FunctionElement):
type = sqlalchemy.types.DateTime()
@compiles(utcnow)
def utcnow__default(element, compiler, **kw):
@jvanasco
jvanasco / compiler.py
Created May 26, 2020 20:35
_bind_processors
@util.memoized_property
def _bind_processors(self):
return dict(
(key, value)
for key, value in (
(
self.bind_names[bindparam],
bindparam.type._cached_bind_processor(self.dialect)
if not bindparam._expanding_in_types
else tuple(
@jvanasco
jvanasco / test_invalidate.py
Last active January 16, 2020 18:45
sqlalchemy invalidation test
from __future__ import print_function
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# Use this file to build your own SSCCE
# SSCCE = Short, Self Contained, Correct (Compatible) Example
# see http://sscce.org/
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@jvanasco
jvanasco / encodings_test.py
Created May 8, 2019 15:11
a test harness showing an edge case scenario with bytes and string encoding from a python2 to python3 port.
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
This test harness showcases an odd scenario when providing compatibility
with Python2 and Python3 data.
The input to a function is a URL, which in Python2 might have been:
url_unicode = u'http://➡.ws/♥'
url_string = 'http://\xe2\x9e\xa1.ws/\xe2\x99\xa5'
@jvanasco
jvanasco / amp_img_upgrade.py
Created October 19, 2018 22:37
after too much testing - including writing custom html parsers and checking cpython generated bytecode - this is the fastest general-purpose way to upgrade an html doc's images into amp i could come up with. if you expect a lot of images in a document, it would be faster to write a second function which ensure unique `imgs` and eliminates duplic…
import re
RE_img = re.compile("""<img[^>]+>""", re.I)
def upgrade(
html,
img_layout=None, # STRING
img_fallback=None, # STRING
img_noscript=None, # BOOL
img_noloading=None, # BOOL
img_default_height=None, # must be a STRING, never an INT
@jvanasco
jvanasco / wellformed_parser.py
Created October 18, 2018 23:07
wellformed parser - utility for parsing html tags as fast as possible.
def extract_tag_inner(tag):
"""
extracts the inner part of a tag - dropping brackets, tagname, trailing slash
:arg string tag: a html tag of the formats:
<TAG_NAME TAG_ATTRIBUTES>
<TAG_NAME TAG_ATTRIBUTES/>
<TAG_NAME TAG_ATTRIBUTES />
:returns string: the inner part of a tag
@jvanasco
jvanasco / shame.py
Created July 3, 2018 23:24
shameful python
def foo():
# i am awful for doing this.
# this function has a nested function, which can not accept arguments
# the inner function will be called twice by a 3rd party library
iteration = [0, ] # use a list as a shameful hack to get around scoping issues
def bar():
iteration[0] += 1 # increment the list index, use that for comparison
if iteration[0] > 1:
@jvanasco
jvanasco / dogpile.py
Created June 20, 2018 07:43
dogpile and raw redis
"""
The following are some code snippets on how i optimized a system using Dogpile cache into native Redis, while still leveraging dogpile.
This is not fully functional code, it's just snippets to explain and help the next person:
General Overview:
* The cached data is a computed value of multiple permissions for an A2B relation.
* The Redis key is `'calculated-a|%s' % id_a`.
* The payload is stored in a field, via `id_b`.