Skip to content

Instantly share code, notes, and snippets.

@bencharb
bencharb / query_messages_from_person.sql
Last active July 17, 2017 06:07
Apple Messages query messages from person
/*
QUERY Apple Messages sqlite database
at ~/Library/Messages/chat.db
*/
select
datetime(m.date + strftime('%s','2001-01-01'), 'unixepoch', 'localtime') as dt
, m.text
, c.chat_identifier
@bencharb
bencharb / kill_ooo_alerts.js
Last active December 19, 2016 20:44
Kill Outlook OOO/WFH Alerts
// Utilities
var td_mill = {
second: 1000
}
td_mill.minute = td_mill.second * 60;
td_mill.hour = td_mill.minute * 60;
td_mill.day = td_mill.hour * 24;
@bencharb
bencharb / parse_boa_statements.py
Last active December 10, 2023 00:53
parse bank of america pdf statements
'''
Transform Bank of America PDF text into tables.
Example:
python parse_boa_statements.py test/joint_dump.txt test/pers_dump.txt --output test/combined.csv
Example of original PDF text (copy/pasted from PDF):
MISTER NAME | Account # 999 99999 9999 | November 24, 2015 to December 24, 2015
Your checking account
Page 3 of 4
@bencharb
bencharb / diskcache.py
Last active April 17, 2016 05:22
expirable disk cache
import os
import datetime
import time
import shove
SEC = 1
MIN = SEC * 60
HOUR = MIN * 60
DAY = HOUR * 24
@bencharb
bencharb / overwrite_file.py
Created March 26, 2016 11:51
safe overwrite file
'''
Move a file to a backup, if the file exists. If context manager statement fails, then restore the backup.
'''
class FakeException(Exception):
pass
def create_backup_path(path, backup_ext='.bak'):
backup_path = path
while backup_path == path or os.path.exists(backup_path):
@bencharb
bencharb / find_jinja_variables.py
Created February 24, 2016 18:51
find jinja variables
import string
import re
import jinja2
def split_many(astring, *split_on_chars, **kwargs):
"""
Split a string on many values.
:param split_on_chars: string to split
:type split_on_chars: basestring
@bencharb
bencharb / find_jinja_variables.py
Created February 24, 2016 18:51
find jinja variables
import string
import re
import jinja2
acceptable_punc = set('#$._-')
all_punctuation = set(string.punctuation)
ignore_punc = all_punctuation.difference(acceptable_punc)
@bencharb
bencharb / get_dependencies.py
Created February 24, 2016 06:05
get basic graph dependencies
def get_dependencies(node, edges_list, depth=0):
if depth > len(edges_list):
raise Exception('Circular dependency somewhere.')
seen_nodes = set()
for from_node, to_node in edges_list:
if from_node == node:
seen_nodes.add(from_node)
if to_node in seen_nodes:
continue
yield to_node
@bencharb
bencharb / iterable_paths.py
Last active February 22, 2016 20:41
iterable paths
import itertools
import collections
def is_non_string_sequence(seq):
return isinstance(seq, collections.Sequence) and not isinstance(seq, basestring)
def is_dict(d):
return isinstance(d, collections.Mapping) or hasattr(d, 'iteritems') and not isinstance(d, basestring)
def iterable_paths(o, level=0):
if is_dict(o):
seqs = []
for k, v in o.iteritems():
@bencharb
bencharb / merge_collections.py
Last active February 7, 2016 08:42
Merge and sort dict collections and json files
import collections
import operator
import json
def merge_dicts(dicts=None, create_key_func=None):
""" Efficiently sort and merge different dictionary collections """
sort_keys = collections.defaultdict(list)
for dct_ix, dcts in enumerate(dicts):
for record_ix, record in enumerate(dcts):
sortkey = create_key_func(record)