import difflib
from functools import lru_cache
from itertools import combinations
arr = [
{"_id": 1, "list_word_int": (189, 114, 188, 90, 2, 68, 96, 0, 250, 168, 150, 126)},
{"_id": 2, "list_word_int": (224, 26, 56, 153, 139, 128, 126, 220, 190, 137)},
{"_id": 3, "list_word_int": (188, 241, 225, 134, 134, 30, 134, 187, 204, 227, 3)},
{"_id": 4, "list_word_int": (224, 166, 159, 236, 82, 17, 82, 21, 227, 97)},
{"_id": 5, "list_word_int": (98, 96, 38, 107, 142, 134, 13, 36, 23)},
]
@lru_cache(maxsize=2 ** 13)
def get_ratio(lst1, lst2):
return difflib.SequenceMatcher(None, lst1, lst2).ratio()
if __name__ == "__main__":
for a, b in combinations(arr, 2):
ratio = get_ratio(a["list_word_int"], b["list_word_int"])
print(
"id= ",
a["_id"],
"Сравниваемый id=",
b["_id"],
"Коэффициент похожести:",
ratio,
)
print(get_ratio.cache_info())
import difflib
import multiprocessing as mp
import os
from itertools import combinations, cycle
arr = [
{"_id": 1, "list_word_int": [189, 114, 188, 90, 2, 68, 96, 0, 250, 168, 150, 126]},
{"_id": 2, "list_word_int": [224, 26, 56, 153, 139, 128, 126, 220, 190, 137]},
{"_id": 3, "list_word_int": [188, 241, 225, 134, 134, 30, 134, 187, 204, 227, 3]},
{"_id": 4, "list_word_int": [224, 166, 159, 236, 82, 17, 82, 21, 227, 97]},
{"_id": 5, "list_word_int": [98, 96, 38, 107, 142, 134, 13, 36, 23]},
]
def target(id_, count):
pid = os.getpid()
for i, (a, b) in zip(cycle(range(count)), combinations(arr, 2)):
if i != id_:
continue
ratio = difflib.SequenceMatcher(
None, a["list_word_int"], b["list_word_int"]
).ratio()
print(f"PID: {pid} id={a['_id']} & {b['_id']} ratio={ratio}")
if __name__ == "__main__":
processes = []
for x in range(mp.cpu_count()):
p = mp.Process(target=target, args=(x, mp.cpu_count()))
p.start()
processes.append(p)
for p in processes:
p.join()
import difflib
import multiprocessing as mp
import os
from itertools import combinations
arr = [
{"_id": 1, "list_word_int": [189, 114, 188, 90, 2, 68, 96, 0, 250, 168, 150, 126]},
{"_id": 2, "list_word_int": [224, 26, 56, 153, 139, 128, 126, 220, 190, 137]},
{"_id": 3, "list_word_int": [188, 241, 225, 134, 134, 30, 134, 187, 204, 227, 3]},
{"_id": 4, "list_word_int": [224, 166, 159, 236, 82, 17, 82, 21, 227, 97]},
{"_id": 5, "list_word_int": [98, 96, 38, 107, 142, 134, 13, 36, 23]},
]
def queue_creator(q, w_count):
pid = os.getpid()
print("Created queue generator PID", pid)
for a, b in combinations(arr, 2):
q.put((a, b))
for _ in range(w_count):
q.put(("stop", None))
def worker(q):
pid = os.getpid()
print("Created worker PID", pid)
while True:
a, b = q.get()
if a == "stop":
break
ratio = difflib.SequenceMatcher(
None, a["list_word_int"], b["list_word_int"]
).ratio()
print(f"PID:{pid} {a['_id']} & {b['_id']} ratio={ratio}")
if __name__ == "__main__":
queue = mp.Queue()
# 1 воркер на генерацию комбинаций, остальные на обработку
workers_count = (mp.cpu_count() - 1) or 1
q_process = mp.Process(target=queue_creator, args=(queue, workers_count))
q_process.start()
processes = [q_process]
for x in range(workers_count):
p = mp.Process(target=worker, args=(queue,))
p.start()
processes.append(p)
for process in processes:
process.join()
In [1]: x = 5
In [2]: 2 < x < 6
Out[2]: True
In [3]: x > 2 and x < 6
Out[3]: True
In [4]: %timeit 2 < x < 6
79.2 ns ± 0.301 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [5]: %timeit x > 2 and x < 6
80 ns ± 0.429 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
main_products = Product.objects.filter(is_main=True)
all_products = Product.objects.filter(
Q(is_main=True) | Q(group_id__in=main_products.values_list("group_id", flat=True))
)
str(Review.objects.filter(Q(user_id__gt=4000) | Q(user_id__in=Review.objects.values_list('user_id', flat=True))).query)
Out[10]: 'SELECT "reviews_review"."id", "reviews_review"."user_id", "reviews_review"."email", "reviews_review"."rating", "reviews_review"."comment", "reviews_review"."datetime" FROM "reviews_review" WHERE ("reviews_review"."user_id" > 4000 OR "reviews_review"."user_id" IN (SELECT U0."user_id" AS Col1 FROM "reviews_review" U0))'
subprocess или os в связке с ping не подходит, так как вывод от ping идет в консоль программы
img(
v-if="active",
:src="require(`@/assets/img/very-long-img-name${i + 1}.gif`)",
alt="")