import re
REPLACEMENT = {
'ноль': '0',
'один': '1',
'два': '2',
'три': '3',
'четыре': '4',
'пять': '5',
'шесть': '6',
'семь': '7',
'восемь': '8',
'девять': '9'
}
PHONE_REGEX = re.compile('(\+)?\d{10,11}')
def parse_phones(file_path):
parsed = []
unparsed = []
with open(file_path, 'r') as file:
for line in file:
name, phone, *_ = line.split(':')
name = name.strip()
phone = phone.strip()
for key, value in REPLACEMENT.items():
phone = phone.replace(key, value)
if PHONE_REGEX.match(phone):
phone_len = len(phone)
if phone_len == 10:
phone = '+7' + phone
elif phone_len == 11:
phone = '+7' + phone[1:]
parsed.append((name, phone))
else:
unparsed.append(line)
return parsed, unparsed
import sys
import logging
LOG = logging.getLogger(__name__)
def handle_exception_with_locals(func):
def wrapped(*args, **kwargs):
locals = {}
def tracer(frame, event, arg):
""" Эта функция будет инспектировать код,
который будет выполнятся после того,
как она будет установлена профайлером.
Профайлер срабатывает только на события
retrun и call (при этом exception интерпретируется как return)
"""
nonlocal locals
if event == 'return':
locals = frame.f_locals.copy()
sys.setprofile(tracer)
try:
res = func(*args, **kwargs)
except Exception as e:
log_pattern = ('{}(*{}, **{}) failed with {}. ' +
'Local function variables: {}; ' +
'Global variables: {};')
LOG.error(log_pattern.format(
func.__name__, repr(args), repr(kwargs),
repr(e), repr(locals), repr(globals()))
)
raise
finally:
sys.setprofile(None)
return res
return wrapped
@handle_exception_with_locals
def test(a, b):
a = 16
b = 7
c = 9
raise ValueError('Oops!')
test(1, 2)
result = Context.query.join(
Request, Request.context_id == Context.id_
).join(
Point, Point.request_id == Request.id_
).group_by(Context.id_).with_entities(
Context.field1, ...
func.count(Point.id_).label('points_count')
)
import logging
LOG = logging.getLogger(__name__)
def handle_silently(function):
def wrapped(*args, **kwargs):
result = None
try:
result = function(*args, **kwargs)
except Exception as e:
LOG.error(
'{}(*{}, **{}) failed with exception {}'.format(
function.__name__, repr(args), repr(kwargs), repr(e)
)
)
return result
return wrapped
@handle_silently
def some_function(test):
return test / 0
def decorators_fabric(handler_function):
def decorator(function):
def wrapped(*args, **kwargs):
handler_function(function.__name__, repr(args), repr(kwargs))
return function(*args, **kwargs)
return wrapped
return decorator
@decorators_fabric(print)
def my_test_function(arg1, arg2):
return arg1 * arg2
import re
replacement = ''
string_to_replace = '\xa0 test data '
strip_re = re.compile(r'(^\s+)|(\s+$)')
result = strip_re.sub(replacement , string_to_replace)