partition = 'boot'
out="""
# mtd0: 00500000 00020000 "boot"
# mtd1: 00500000 00020000 "recovery"
# mtd2: 00140000 00020000 "misc"
# mtd3: 00060000 00020000 "splash"
# mtd4: 0aa00000 00020000 "system"
# mtd5: 05d00000 00020000 "cache"
# mtd6: 0a6a0000 00020000 "userdata"
# mtd7: 01400000 00020000 "cust"
"""
for line in out.splitlines():
if partition in line:
print(line.split()[1][:-1])
break
In [25]:
mtd0
for line in out.splitlines():
if partition in line:
print(line[2:6])
break
a = '{один|два|три}{текст_2{{один|два|три}|{один|два|три}}|{один|два|три}}текст_1'
res = re.search('({.+})({.+})', a)
print(res.group(1))
print(res.group(2))
In [8]:
{один|два|три}
{текст_2{{один|два|три}|{один|два|три}}|{один|два|три}}
class Test:
def creat_role(self,ctx):
print("ctreate_role")
loading_bar(self.update_mute)(ctx)
# self.update_mute(ctx)
# @loading_bar
def update_mute(self, ctx):
print(f"update_mute {ctx}")
def loading_bar(func):
print("loading_bar")
def decorator(*args, **kwargs):
print("decorator")
func(*args, **kwargs)
for i in range(1, 6):
print("finish")
return decorator
loading_bar(self.update_mute)(ctx)
, а # self.update_mute(ctx)
# @loading_bar
раскомментировать text = 'Lorem ipsum dolor sit amet, 1234567890123456789012345678901234567890, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
from textwrap import wrap
def justify(line, width):
gap_width, max_replace = divmod(width - len(line) + line.count(' '), line.count(' '))
return line.replace(' ', ' ' * gap_width).replace(' ' * gap_width, ' ' * (gap_width + 1), max_replace)
def lines_formatter(text, width):
lines = wrap(text, width, break_long_words=False)
for i, line in enumerate(lines[:-1]):
if len(line) <= width and line.count(' '):
lines[i] = justify(line, width).rstrip()
return '\n'.join(lines)
print(lines_formatter(text, 80))
In [17]:
Lorem ipsum dolor sit amet, 1234567890123456789012345678901234567890,
consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua.
print(lines_formatter(text, 35))
In [18]:
Lorem ipsum dolor sit amet,
1234567890123456789012345678901234567890,
consectetur adipisicing elit, sed
do eiusmod tempor incididunt ut
labore et dolore magna aliqua.
def sumelem(seq, step):
for i, elem in enumerate(seq,1):
if i%step:
yield elem
else:
yield elem
yield sum(seq[i-step:i])
print(*sumelem(a, 5))
Расскажите о своем мнении, как сделать код лучше?
alphabet = 'abcdefghijklmnopqrstuvwxyz'
text = "The sunset sets at twelve o'clock."
def alphabet_position(text):
text = ' '.join(char for char in text.lower() if char.isalpha())
return text.translate({ord(c): str(i) for i,c in enumerate(alphabet,1)})
print(alphabet_position(text))
In [4]:
20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11
У меня есть своя функция для округления чисел... В строках для форматирования функции не работают
balance = 1.237
s = f"Balance: {my_round(balance)}"
print(s)
from binascii import hexlify
url = "..."
querystring = {..., ...,}
headers = {...}
content = querystring['content'].encode('utf-16-be')
querystring['content'] = hexlify(content).decode('utf-8')
responce = request.....
from binascii import hexlify
querystring = {'content': 'Привет'}
content = querystring['content'].encode('utf-16-be')
querystring['content'] = hexlify(content).decode('utf-8')
print(querystring)
In [4]:
{'content': '041f04400438043204350442'}
x = [0.0799, 0.07991, 0.07998, 0.08, 0.080574, 0.081, 0.082, 0.08405, 0.085]
raznica = (0.00001, 0.00009, 0.00405)
prev = x[0]
new_x = [prev]
for delta in raznica:
while True:
next = prev + delta
if next in x:
new_x.append(next)
prev = next
else: break
print(new_x)
In [5]:
[0.0799, 0.07991, 0.08, 0.08405]
finished_sandwiches = [dish for dish in sandwich_orders if dish != 'pastrami']
print(finished_sandwiches )
In [1]:
['bokaldilo', 'arepa', 'kebab']
while 'pastrami' in sandwich_orders:
sandwich_orders.pop(sandwich_orders.index('pastrami'))
print(sandwich_orders)
In [2]:
['bokaldilo', 'arepa', 'kebab']