print
по умолчанию разделяет аргументы пробелом. Чтобы это предотвратить, добавьте параметр sep=''
.print(2, '\n', 3, sep='')
>>> print(2, 3, 4, 5, 6, sep='\n')
2
3
4
5
6
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
with open(path_in, 'w+') as read_file:
read_file.write('something')
mapping = str.maketrans(
"f,dult`;pbqrkvyjghcnea[wxio]sm'.z",
"абвгдеёжзийклмнопрстуфхцчшщъыьэюя",
)
calc = {
'квадрат': {
'площадь': lambda **kwargs: bottom ** 2,
'периметр':lambda **kwargs: bottom * 4,
},
'прямоугольник': {
'площадь': lambda **kwargs: bottom * height,
'периметр':lambda **kwargs: (bottom + height) * 2,
},
'треугольник': {
'площадь': lambda **kwargs: bottom * height / 2,
}
}
def ask(question, options=None, expected=str):
if options:
question = '{} ({}): '.format(question, '|'.join(options))
answer = None
while (options and answer not in options) or (type(answer) is not expected):
answer = input(question)
if expected is str:
answer = answer.lower().translate(mapping)
candidates = set(option for option in options if option.startswith(answer))
if len(candidates) == 1:
answer = candidates.pop()
else:
try:
answer = expected(answer)
except ValueError:
pass
return answer
if __name__ == '__main__':
figure = ask('Какую фигуру необходимо рассчитать?', set(calc))
parameter = ask('Какой параметр требуется вычислить?', set(calc[figure]))
bottom = ask('Введите длину основания: ', expected=float)
if figure == 'квадрат':
height = bottom
else:
height = ask('Введите высоту: ', expected=float)
result = calc[figure][parameter](bottom=bottom, height=height)
print(f'{parameter.title()} {figure}а: {result:.3f}')
>>> import xml.etree.ElementTree as ET
>>> root = ET.fromstring('<img src="some.jpg"></img>')
>>> root
<Element 'img' at 0x104bd5458>
>>> root.get('src')
'some.jpg'
>>> root.tag
'img'
>>> def some_magic(patt, str_in):
... import re
... patt = patt.replace('%s', '(.*)')
... res = re.match(patt, str_in)
... return res.groups()
...
>>>
>>> print(some_magic('<%s=\"%s\">', '<img src="some.jpg">'))
('img src', 'some.jpg')
def http_method(urls):
source = ["http://", "https://"]
my_list = []
for data in source:
my_list += [data + urls]
print(my_list)
def http_method(urls):
source = ["http://", "https://"]
return [data + urls for data in source]