Skip to content

Instantly share code, notes, and snippets.

@ndrwpvlv
ndrwpvlv / parentheses_check.py
Created August 8, 2021 22:38
Fast O(n) check for balanced parentheses in Python
def calc_seq(seq):
rep = {'(': 1, ')': -1, }
seq_sum = 0
for s in seq:
seq_sum += rep[s]
if seq_sum < 0:
return False
if seq_sum > 0:
return False
return True
@ndrwpvlv
ndrwpvlv / ZipTimedRotatingFileHandler_27.py
Created August 5, 2021 20:34
ZipTimedRotatingFileHandler_27.py
import os
import zipfile
from logging.handlers import TimedRotatingFileHandler
class ZipTimedRotatingFileHandler(TimedRotatingFileHandler):
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):
super(ZipTimedRotatingFileHandler, self).__init__(filename, when, interval, backupCount, encoding, delay, utc)
@ndrwpvlv
ndrwpvlv / ZipTimedRotatingFileHandler.py
Created August 4, 2021 15:06
ZipTimedRotatingFileHandler from python logging
import os
import zipfile
from logging.handlers import TimedRotatingFileHandler
class ZipTimedRotatingFileHandler(TimedRotatingFileHandler):
"""
ZipTimedRotatingFileHandler is class based on TimedRotatingFileHandler for saving old logs files
to zip-archive instead of themself deleting
@ndrwpvlv
ndrwpvlv / brutest.py
Last active July 29, 2021 22:14
Test of strings creation speed
import string
import itertools
import time
def brute(count_symb, lenght_max = 6, fill = 'a'):
s = string.ascii_letters
for s in itertools.product(s, repeat=count_symb):
yield '{}{}'.format(''.join(s), (lenght_max - count_symb) * fill if count_symb < lenght_max else '')
def brute_2(count = 10000):
@ndrwpvlv
ndrwpvlv / timench_intersection_test.py
Last active July 18, 2021 22:31
Python timetest of intersections
import random
from timench import Timench
# Setup
a3 = [v for v in range(0, 3)]
b3 = [v for v in range(0, 3)]
a1000 = [v for v in range(0, 1000)]
b1000 = [v for v in range(0, 1000)]
ar3 = [random.randint(0, 3) for _ in range(0, 3)]
br3 = [random.randint(0, 3) for _ in range(0, 3)]
@ndrwpvlv
ndrwpvlv / list_replace.py
Created July 14, 2021 12:58
Python. Function for replace element of elements by value in combined nestred structure of list, sets and tuples
def list_replace(lst, value_search, value_replace):
res = []
for item in lst:
if isinstance(item, (list, set, tuple)):
res.append(list_replace(item, value_search, value_replace))
else:
res.append(item if item != value_search else value_replace)
return type(lst)(res)
# Example
@ndrwpvlv
ndrwpvlv / example_flatlist.py
Created July 7, 2021 23:51
Compare of list flatten time
from timench import Timench
# Setup
lst_1 = [[i, i + 1] for i in range(100)]
lst_2 = [[i, i + 1] for i in range(1000)]
lst_3 = [[i, i + 1] for i in range(10000)]
tmnch = Timench()