def timed_lru_cache(seconds: int, maxsize: int = 128) -> Callable:
def wrapper_cache(func):
func = lru_cache(maxsize=maxsize)(func)
func.lifetime = timedelta(seconds=seconds)
func.expiration = datetime.utcnow() + func.lifetime
@wraps(func)
def wrapped_func(*args, **kwargs):
if datetime.utcnow() >= func.expiration:
func.cache_clear()
func.expiration = datetime.utcnow() + func.lifetime
return func(*args, **kwargs)
return wrapped_func
return wrapper_cache
def combine(original_list, maxlen=20):
outer_list = []
accum = []
for sublist in original_list:
# if the next set of values will fit, extend the sublist
if len(accum) + len(sublist) <= maxlen:
accum.extend(sublist)
else:
# otherwise start a new sublist
outer_list.append(accum)
accum = list(sublist)
# pick up any values that are left over
if accum:
outer_list.append(accum)
return outer_list
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--path",
dest="path",
default=os.path.join(os.path.abspath(os.path.dirname(__name__)), 'data.csv')
)
def handle(self, *args, **options):
path = options.get("path")
...
call_command('my_command', path=create_test_csv_file)
task_list = [task1(), task2()]
res = await asyncio.gather(*task_list, return_exceptions=True) # res = ["result", ArithmeticError()]
res = list(map(lambda r: r if not isinstance(r, Exception) else default_value, res)) # res = ["result", None]
_, to_rar = (os.path.split(to_rar))
new_file_name = self.generate_file_name() # Генерация имени файла
path_to_temp_folder = os.path.dirname(BASE_DIR)
if not os.path.exists(f'{path_to_temp_folder}/files'):
pathlib.Path(f'{path_to_temp_folder}/files').mkdir(parents=True, exist_ok=True) # Создаю папку
wb.save(f'{path_to_temp_folder}/files/{new_file_name}') # Сохраняю в нее эксель файл
archive = self.generate_zip_name(rfi) # функция генерит имя архива (string)
to_rar = f'{path_to_temp_folder}/files' # путь к папке с файлами
_, to_rar = (os.path.split(to_rar)) # Новая строка
os.system("rar a {} {}".format(archive, to_rar)) # Вызов линуксовой либы для создания архива
folder.rar
folder
file.xlsx
"link": HOST + item.find("a", class_="js-item-slider").get('href'),
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())