Skip to content

Instantly share code, notes, and snippets.

Avatar

Jonathan Vanasco jvanasco

View GitHub Profile
@jvanasco
jvanasco / replace_domain.py
Created Sep 4, 2020
replace acme-dns random domains with custom domains.
View replace_domain.py
from __future__ import print_function
import os
import sqlite3
import sys
import re
_args = sys.argv
try:
if len(_args) != 3:
@jvanasco
jvanasco / utcnow.py
Created Aug 21, 2020
making sure we send the right timestamp...
View utcnow.py
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
_bind_processors
View compiler.py
@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 Jan 16, 2020
sqlalchemy invalidation test
View test_invalidate.py
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 / sqlassist.py
Created Jan 27, 2012
Supporting multiple database connections & Reflecting Tables in pyramid with sqlalchemy .7
View sqlassist.py
r"""
sqlassist
~~~~~~~~~
v0.3
sections of code taken from :
- Mike Orr's package 'sqlahelper'
- Mike Bayer's blog post 'Django-style Database Routers in SQLAlchemy'
@jvanasco
jvanasco / encodings_test.py
Created May 8, 2019
a test harness showing an edge case scenario with bytes and string encoding from a python2 to python3 port.
View encodings_test.py
# -*- 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 / pyramid-interfaces.py
Created Feb 19, 2014
better ISession interface
View pyramid-interfaces.py
class ISession(IDict):
...
...
...
id = Attribute('String `id`')
cookie_name = Attribute('String `cookie_name`')
cookie_value_in = Attribute('String `cookie_value_in`')
cookie_value_out = Attribute('String `cookie_value_in`')
@jvanasco
jvanasco / amp_img_upgrade.py
Created Oct 19, 2018
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…
View amp_img_upgrade.py
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 Oct 18, 2018
wellformed parser - utility for parsing html tags as fast as possible.
View wellformed_parser.py
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 Jul 3, 2018
shameful python
View shame.py
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:
You can’t perform that action at this time.