Skip to content

Instantly share code, notes, and snippets.

@waylan
waylan / service_year.py
Created February 22, 2021 21:10
A Python object which represents a year that runs from Sept to Aug.
import datetime
class ServiceYear():
'''
A year which runs from Sept to Aug.
Accepts a year in any format which can be converted to an integer.
>>> year = ServiceYear(2021)
@waylan
waylan / pd_service_year.py
Last active February 22, 2021 19:14
A couple functions which use Pandas to work with a year which runs from Sept to Aug. Each function only wraps a single line of Pandas calls, so their value is limited. But, remembering how to accomplish the things they do is valuable.
import pandas
def get_service_year(date):
'''
Return service year for given date.
The service year runs from Sept to Aug and Sept-Dec are in the service year of
the following calendar year. In other words, the date 2020-09 will return 2021.
Accepts date as an ISO formatted string (for example YYYY-MM), a datetime.date
@waylan
waylan / example.yml
Last active November 12, 2020 02:44
A custom YAML tag for referencing environment variables in YAML files. The content of the env var will be resolved to one of YAML's implicit types (bool, float, int, timestamp, null, value). The content of the env var is always assumed to be a Scalar node, so sequence and mappings are ignored and simply passed through as a string.
foo: !ENV FOO
bar: !ENV [BAR]
baz: !ENV [BAZ, ~]
fbb: !ENV [FOO, BAR, BAZ, ~]
@waylan
waylan / example.yml
Last active October 8, 2021 13:17
Proof of concept for yaml inherit tag. Unfortunately, defining the tag is ugly. This was based on the code in [this comment](https://gist.github.com/joshbode/569627ced3076931b02f#gistcomment-2309157).
# Unforunately, the tag can't be a standalone item by itelf.
# After all, the following document is invalid:
# foo
# a: bar
# Therefore the tag needs to fit in with the structure of the document.
# I have used a null key and then discard it in the loader
# so that it is not in the final output.
null: !inherit parent.yml
@waylan
waylan / rawhtmltokens.py
Created September 16, 2020 18:41
Some test tools I used when refactoring Python-Markdown's HTML parser. Preserved here for future reference.
import markdown
src = """
<div><p markdown="1">Hello _World!_</p></div>
"""
md = markdown.Markdown(extensions=['md_in_html'])
print('Doc: ', '\n'.join(md.preprocessors['html_block'].run(src.split('\n'))))
print('Stash: ', md.htmlStash.rawHtmlBlocks)
md.htmlStash.reset()
print('Ouput: ', '\n---------------------\n' + md.convert(src) + '\n---------------------')
@waylan
waylan / github_notifier.py
Created August 25, 2020 01:11
GitHub Notifier for BitBar
#!/usr/local/bin/python3
# <bitbar.title>GitHub Notifier</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Waylan Limberg</bitbar.author>
# <bitbar.author.github>waylan</bitbar.author.github>
# <bitbar.desc>Display a list of unread GitHub Notifications.</bitbar.desc>
# <bitbar.image>https://github.com/primer/octicons</bitbar.image>
# <bitbar.dependencies>python,agithub</bitbar.dependencies>
# <bitbar.abouturl>http://url-to-about.com/</bitbar.abouturl>
@waylan
waylan / md2md.py
Created June 19, 2020 18:31
Markdown to Markdown using Mistune (experimental and incomplete)
from mistune import create_markdown
from mistune.renderers import BaseRenderer
import re
ESCAPE_CHAR = re.compile(r'(?<!\\)([\\`*_()\[\]#+-])')
UL_BULLET = re.compile(r'(?<=^)(\*)( +)', re.MULTILINE)
def indent(text, level, tab_length=4):
''' Indent block of text by level '''
@waylan
waylan / test_html_parser.py
Created March 26, 2019 00:09
A simple test HTMLParser to see how the HTMLParser for python works.
try:
import HTMLParser as parser
except ImportError:
from html import parser
class TestParser(parser.HTMLParser):
def handle_starttag(self, tag, attrs):
print ("STAG:", tag)
@waylan
waylan / date_range.py
Last active December 28, 2018 21:17
Use Pandas to get a range of start/end dates for all weeks of year
import pandas as pd
# "W-SUN" for Monday to Sunday week. Use "W-SAT" for Sunday to Saturday...
date_range = pd.period_range('1/1/2019', '12/31/2019', freq='W-SUN')
"""
>>> date_range
PeriodIndex(['2018-12-31/2019-01-06', '2019-01-07/2019-01-13',
'2019-01-14/2019-01-20', '2019-01-21/2019-01-27',
...
@waylan
waylan / applescript.py
Created December 2, 2018 01:44
Call Applescripts from Python
#!/usr/bin/python
# From http://www.leancrew.com/all-this/2013/03/combining-python-and-applescript/
# Updated for Python 3
import subprocess
def asrun(ascript):
"Run the given AppleScript and return the standard output and error."