import random
def get_randoms(n, avg, min_val, max_val, repeats=20):
result = [avg] * n
for i in range(repeats):
i1, i2 = random.sample(range(n), 2) # выбираем 2 случайных индекса
v1, v2 = (result[i1], result[i2])
max_delta = min(v1 - min_val, max_val - v2)
rand_delta = random.randint(0, max_delta)
v1 -= rand_delta
v2 += rand_delta
result[i1] = v1
result[i2] = v2
return result
randoms = get_randoms(5, 89, 60, 100)
print(randoms, sum(randoms) / 5) # [95, 71, 98, 92, 89] 89.0
randoms = get_randoms(5, 89, 60, 100, 50)
print(randoms, sum(randoms) / 5) # [95, 97, 75, 81, 97] 89.0
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os
from lxml import etree
FILES_DIR = 'Путь/До/Папки/С/Файлами'
def main():
parser = etree.HTMLParser()
for fname in os.listdir(FILES_DIR):
path = os.path.join(FILES_DIR, fname)
tree = etree.parse(path, parser)
root = tree.getroot()
el = root.find('.//h3[@class="header-text"]')
if el is not None:
header = el.text
if not header:
print(fname, 'Пустой заголовок')
continue
new_fname = header.strip() + '.html'
new_path = os.path.join(FILES_DIR, new_fname)
try:
os.rename(path, new_path)
except Exception as e:
print(fname, 'Ошибка при переименовании')
continue
print(fname, '-->', new_fname)
else:
print(fname, 'Заголовок не найден')
if __name__ == '__main__':
main()
raw_input('Enter для закрытия...'.encode('cp866')) # Да, я использую windows :(