pattern = ("Поезд {number} отправляется в "
"{departure_time} с {platform} платформы, {way} пути.")
def function(pattern):
train = {
"number": 123,
"departure_time": "13:40",
"platform": 2,
"way": 3,
}
return pattern.format(**train)
function(pattern)
>>>
Поезд 123 отправляется в 13:40 с 2 платформы, 3 пути.
def line_break(limit, step):
def get_chars():
for i in range(1, (limit+1)):
yield f'{i} ' if i % step != 0 else f'{i}\n'
return ''.join(get_chars()).strip()
print(line_break(14, 4))
1 2 3 4
5 6 7 8
9 10 11 12
13 14
print(line_break(25, 5))
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
import json
with open('questions.json') as file:
file_parse = json.load(file)
for key in file_parse:
print(f'{key:<10}', *file_parse[key].keys())
Transport 100 200 300
Animals 100 200 300
Meal 100 200 300
data = [
{'ip':'192.168.0.2', 'model':'DES-3200-26', 'uptime': '3d 12:03:05', 'uplink state': '25: up\n26: up', 'uplink err': '0\n11', 'uplink mcast': '24560\n113'},
{'ip':'192.168.0.3', 'model':'DES-3200-52', 'uptime': '1d 04:00:15', 'uplink state': '49: up\n50: up\n51: down\n52: down', 'uplink err': '10\n1133\n0\n0', 'uplink mcast': '5497812\n3145\n0\n0'},
]
def set_data_for_PrettyTable(data):
field_names = data[0].keys()
rows = []
for row in data:
rows.append(row.values())
return field_names, rows
field_names, rows = set_data_for_PrettyTable(data)
table = PrettyTable()
table.hrules = 1
table.field_names = field_names
table.add_rows(rows)
table.align["uplink state"] = "l"
print(table)
+-------------+-------------+-------------+--------------+------------+--------------+
| ip | model | uptime | uplink state | uplink err | uplink mcast |
+-------------+-------------+-------------+--------------+------------+--------------+
| 192.168.0.2 | DES-3200-26 | 3d 12:03:05 | 25: up | 0 | 24560 |
| | | | 26: up | 11 | 113 |
+-------------+-------------+-------------+--------------+------------+--------------+
| 192.168.0.3 | DES-3200-52 | 1d 04:00:15 | 49: up | 10 | 5497812 |
| | | | 50: up | 1133 | 3145 |
| | | | 51: down | 0 | 0 |
| | | | 52: down | 0 | 0 |
+-------------+-------------+-------------+--------------+------------+--------------+
from prettytable import PrettyTable
def data_for_table():
with open ('w.txt', encoding='utf-8') as file:
names, days, salaries, pays = [], [], [], []
total = 0
work_days, *employees = file.read().splitlines()
work_days=int(work_days.strip())
for elem in employees:
if elem.strip().isalpha():
name = f"{elem.strip():<30}"
names.append(name)
else:
paid_days, salary = elem.strip().split()
pay = float(salary)/work_days*int(paid_days)
total += pay
days_str = f"{paid_days:>3}"
salary_str = f"{float(salary):>10.2f}"
pay_str = f"{float(pay):>10.2f}"
days.append(days_str)
salaries.append(salary_str)
pays.append(pay_str)
rows = list(zip(names, days, salaries, pays))
total_str = f"{float(total):>10.2f}"
return rows, total_str
def table_with_footer(field_names, table_rows, table_title='', footer_field_name='', footer_field_value=''):
pt_table = PrettyTable(padding_width=0)
pt_table.field_names = field_names
pt_table.add_rows(table_rows)
if table_title:
pt_table.title = table_title
# Здесь используем костыль (хак?), чтобы сделать подвал таблицы (с итогами)
# table footer
if footer_field_name and footer_field_value:
bottom_line = pt_table.get_string().splitlines()[-1]
width_field_name, width_field_value = [len(line) for line in bottom_line[1:-1].rsplit('+', 1)]
footer_fields = [f'{footer_field_name:<{width_field_name}}', f'{footer_field_value:>{width_field_value}}']
footer_row = f'|{"|".join(footer_fields)}|'
table_footer = (f'{footer_row}\n'
f'{bottom_line}')
else:
table_footer = ''
# Table with title and footer
table = (f"{pt_table.get_string()}\n"
f"{table_footer}")
return table
table_rows, total_value = data_for_table()
t_title = 'Зарплатная ведомость'
f_names = ["Фамилия", "Дни", "Оклад", "Зарплата"]
t_rows = table_rows
footer_f_name = "Итого"
footer_f_value = total_value
print(table_with_footer(f_names, t_rows))
+------------------------------+---+----------+----------+
| Фамилия |Дни| Оклад | Зарплата |
+------------------------------+---+----------+----------+
|Петров | 15| 6300.00| 3937.50|
|Иванов | 24| 5600.00| 5600.00|
|Сидоров | 19| 4350.00| 3443.75|
|Николаев | 15| 4800.00| 3000.00|
|Малышев | 21| 4350.00| 3806.25|
|Соколов | 12| 7800.00| 3900.00|
+------------------------------+---+----------+----------+
print(table_with_footer(f_names, t_rows, t_title, footer_f_name, footer_f_value))
+--------------------------------------------------------+
| Зарплатная ведомость |
+------------------------------+---+----------+----------+
| Фамилия |Дни| Оклад | Зарплата |
+------------------------------+---+----------+----------+
|Петров | 15| 6300.00| 3937.50|
|Иванов | 24| 5600.00| 5600.00|
|Сидоров | 19| 4350.00| 3443.75|
|Николаев | 15| 4800.00| 3000.00|
|Малышев | 21| 4350.00| 3806.25|
|Соколов | 12| 7800.00| 3900.00|
+------------------------------+---+----------+----------+
|Итого | 23687.50|
+------------------------------+---+----------+----------+
print(table_with_footer(f_names, t_rows, footer_field_name=footer_f_name, footer_field_value=footer_f_value))
+------------------------------+---+----------+----------+
| Фамилия |Дни| Оклад | Зарплата |
+------------------------------+---+----------+----------+
|Петров | 15| 6300.00| 3937.50|
|Иванов | 24| 5600.00| 5600.00|
|Сидоров | 19| 4350.00| 3443.75|
|Николаев | 15| 4800.00| 3000.00|
|Малышев | 21| 4350.00| 3806.25|
|Соколов | 12| 7800.00| 3900.00|
+------------------------------+---+----------+----------+
|Итого | 23687.50|
+------------------------------+---+----------+----------+
[sg.Button("Generate:", key = "GENERATE"), sg.Text(random.randint(int(x), int(y)), size=(4,1), key = 'OUTPUT')],
[sg.Button("Generate:", key = "GENERATE"), sg.Text(random.randint(int(x), int(y)), key = 'OUTPUT', auto_size_text=False)],