Ever been annoyed by so many if/else of regex matches and searches?
Something like the following?
import re
p = re.compile('(?P<president>obama|bush|clinton)')
t = p.search('I am beating around the bush, am I?')
import matplotlib.pylab as plt | |
import importlib | |
import pandas as pd | |
import numpy as np | |
from typing import Callable, Iterator | |
import inspect | |
from types import ModuleType | |
class Required: |
import re | |
base_validation_funs = { | |
"be a": isinstance, | |
"be in": lambda val, check_val: val in check_val, | |
"be at least": lambda val, check_val: val >= check_val, | |
"be more than": lambda val, check_val: val > check_val, | |
"be no more than": lambda val, check_val: val <= check_val, | |
"be less than": lambda val, check_val: val < check_val, | |
} |
from itertools import cycle, islice | |
import re | |
ascii_alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
alpha_numerics = 'abcdefghijklmnopqrstuvwxyz0123456789' | |
vowels = 'aeiou' | |
consonants = 'bcdfghjklmnpqrstvwxyz' | |
vowels_and_consonants = (vowels, consonants) | |
from itertools import cycle, islice | |
import re | |
ascii_alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
alpha_numerics = 'abcdefghijklmnopqrstuvwxyz0123456789' | |
vowels = 'aeiou' | |
consonants = 'bcdfghjklmnpqrstvwxyz' | |
vowels_and_consonants = (vowels, consonants) | |
from functools import reduce | |
# The simple dotpath version | |
dotpath_get = lambda d, dotpath: reduce(lambda x, y: x.get(y), dotpath.split('.'), d) | |
# A generalization to any separator and any "get" method: | |
keypath_get = lambda d, keypath, sep, getmethod: reduce(lambda x, y: getattr(x, getmethod)(y), keypath.split(sep), d) |
import time | |
import re | |
p = re.compile('[\d\.]+') | |
seconds_for_unit = { | |
'd': 60 * 60 * 24, | |
'h': 60 * 60, | |
'm': 60, | |
's': 1 |
import operator | |
def _apply_op(op, d1, dflt_1, d2, dflt_2): | |
if isinstance(d2, dict): | |
out = dict() | |
for k, v1 in d1.items(): | |
v2 = d2.get(k, dflt_2) | |
out[k] = op(v1, v2) | |
for k in d2: # take care of the remainder (those keys in dict_2 that were not in dict_1) | |
if k not in out: |
Ever been annoyed by so many if/else of regex matches and searches?
Something like the following?
import re
p = re.compile('(?P<president>obama|bush|clinton)')
t = p.search('I am beating around the bush, am I?')
"""Get a `re.Pattern` instance (as given by re.compile()) with control over defaults of it's methods. | |
Useful to reduce if/else boilerplate when handling the output of search functions (match, search, etc.) | |
See [regex_search_hack.md](https://gist.github.com/thorwhalen/6c913e9be35873cea6efaf6b962fde07) for more explanatoins of the | |
use case. | |
Example; | |
>>> dflt_result = type('dflt_search_result', (), {'groupdict': lambda x: {}})() | |
>>> p = re_compile('.*(?P<president>obama|bush|clinton)', search=dflt_result, match=dflt_result) | |
>>> |