Skip to content

Instantly share code, notes, and snippets.

View brunokim's full-sized avatar
🌌

Bruno Kim Medeiros Cesar brunokim

🌌
View GitHub Profile
@brunokim
brunokim / bbb.csv
Last active January 13, 2023 21:02
Exemplo de votos de duplas no BBB 23
Fred Key MC Guimê
Amanda 250 120 10
Bruno 50 150 200
Tina 150 110 170
from dataclasses import dataclass
@dataclass
class Ref:
"""Representação de uma variável.
Se ela não possuir um valor ainda, é dita livre; se não, ela está ligada."""
name: str
value: any = None
docker run \
--memory 100m --memory-swap 100m \
-e FILENAME=big_file_1m.txt \
-e MAX_MEMORY_OCCUPANCY=0.66 \
-e REPORT_INTERVAL=1 \
big_file_counter | gzip -c > output.log.gz
approx usage kernel usage total cache total rss total swap inactive file working set limit
9.16 MiB 600.00 KiB 2.03 MiB 6.48 MiB 0.00 B 1.80 MiB 7.36 MiB 100.00 MiB
40.56 MiB 656.00 KiB 2.03 MiB 37.87 MiB 0.00 B 1.80 MiB 38.76 MiB 100.00 MiB
def merge_files(filenames):
# Abre um arquivo temporário de forma segura
fd, tempname = tempfile.mkstemp(text=True)
try:
target = os.fdopen(fd, mode='w')
# Abre todos os arquivos temporários.
# FileState é um wrapper que guarda qual foi a última chave/valor lida
files = [FileState(filename) for filename in filenames]
[f.open() for f in files]
def memory_working_set() -> int:
usage = memory_usage()
inactive = memory_stats()['inactive_file']
return usage - inactive
def memory_usage() -> int:
with open('/sys/fs/cgroup/memory/memory.memsw.usage_in_bytes') as f:
return int(f.read())
def memory_limit() -> int:
with open('/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes') as f:
return int(f.read())
def get_counter_size(c: Counter) -> int:
size = sys.getsizeof(c)
size += sum(sys.getsizeof(key) for key in c)
return size
import gc
import sys
def get_transitive_size(obj):
stack = [obj]
size = 0
seen = set()
while stack:
o = stack.pop()
if isinstance(o, type):
def create_file(num_elements: int, size: int, filename: str) -> int:
with open(filename, 'w') as f:
total = 0
while total < size:
value = random.randrange(num_elements)
total += f.write(f'{value}\n')
return total
from collections import Counter
c = Counter()
with open('big_file.txt') as f:
for line in f:
c[line[:-1]] += 1 # Remove newline from line
for key, count in c.most_common():
print(f'{key} - {count}')