Skip to content

Instantly share code, notes, and snippets.

@ansaso
ansaso / fish_json_complete.fish
Last active November 15, 2021 16:44
Fish completion from json
#########################
#### fish completion ####
#########################
# in /path/to/fish/completions/{mycommand}.fish
# store json completion for command as environment variable
# to be passed down to python script
set -x json_completion (cat "path/to/mycommand.json")
@ansaso
ansaso / DefaultList.py
Last active February 28, 2021 17:00
pyhton's collections.DefaultDict but for lists. Quick mockup
class DefaultList(list):
def __init__(self, list_, default=None):
super().__init__(list_)
self.default = default
def __getitem__(self, index):
if isinstance(index, slice):
start = index.start
stop = index.stop
step = index.step
@ansaso
ansaso / alfred-quicklook.py
Last active December 24, 2020 21:43
alfred-quicklook python boilerplate
from __future__ import print_function
from subprocess import Popen, PIPE
import json
import os
def extra_pane(json, bin_path=os.getenv('BIN_PATH')):
p = Popen((bin_path), stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate(json)
if err:
raise OSError(err)
@ansaso
ansaso / google_calendar_color_map.md
Last active April 29, 2024 14:59
google calendar color mapping. name (string) to id (integer).

intro

As of 2020-07-27 The default google calendar api response does not include the user visible names:

google calendar color choice.

Below is a json map for all the default colors. Please note:

  • colorId empty for event with default color
  • different colorId for the same color in Calendar and Event (ex. Tomato: 11 and 3)
  • there is no consistent logic in the enumeration of the colors
@ansaso
ansaso / df_info_object.py
Last active August 23, 2020 19:21
pandas df.info() as object instead of print
def log_df(df:pd.DataFrame, name: str='') -> str:
return f'''
___________________
# DATAFRAME: {name}
___________
## COLUMNS
size: {df.size}
shape: {df.shape}
nans:
{df.isna().sum()}
@ansaso
ansaso / useful_regex.py
Last active August 19, 2020 11:37
useful regex
class usefulRegex:
def toggle(kwrd: str, match: str) -> str:
return r'(?:\W|\A)' + re.escape(kwrd) + r'[=: \'\"]+(' + re.escape(match) + r')'
def keyword(kwrd: str) -> str:
return r'(?:\W|\A)' + re.escape(kwrd) + r'[=: \'\"]+(?:\'(.+)\'|(\".+\")|(\S+))'
def value(kwrd: str, match: str=r'([\d.]+)') -> str:
return r'(?:\A|\W)' + re.escape(kwrd) + r'[ =:]*?' + re.escape(match)
@ansaso
ansaso / filter_empty_kwargs.py
Last active August 11, 2020 23:21
python locals() with explicit keywords hack
# This is a nice hack I've used a bit in order to write more
# explicit code in Python. It works for functions inside and outside
# of classes
def filter_kwargs(locals, *, exception_keys: Tuple = ('self', 'cls'), exception_values = (None,)):
''' filter out empty kwargs and pack the rest in a dictionary.
works the same as supplying **kwargs, except it is more explicit.
python's built in locals() function has to be supplied as the first argument'''
@ansaso
ansaso / re.itersub.py
Last active June 19, 2020 08:33
python regex re.sub with iterable substitution argument
import re
def sub_iter(reg: str, subs: Iterable, string: str) -> str:
'''
Perform an action equivalent to re.sub chronologically for every
match where the string substituted in is an equivalent item in an
iterable.
reg: regular expression
subs: iterable of chronological sub items. (ideally a generator)
@ansaso
ansaso / sqlite3_with_wrapper.py
Created May 14, 2020 13:14
enable the opening and closing of python's sqlite3 using with syntax
# small wrap of sqlite3 for python in order to control
# the opening and closing of the database using python's
# with syntax. Makes for cleaner code
from sqlite3 import Cursor, connect
class sqlite:
def __init__(self, db: str, *args, **kwargs) -> None:
self.db = db
self.conn = None