Created
May 11, 2021 21:46
-
-
Save Bruno02468/6f50eebc84aeb354b170f637421ff2e0 to your computer and use it in GitHub Desktop.
gerador de sumário pro lucas janela
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# sumario.py | |
# autor: bruno borges paschoalinoto | |
# nenhum direito reservado | |
# escrito para o esplêndido pipi de @LucasGiannella | |
import sys, os, re | |
class Picture(object): | |
def __init__(self, obj, inst, loc, when, bg, rep): | |
self.obj = obj | |
self.inst = inst | |
self.loc = loc | |
self.when = when | |
self.bg = bg | |
self.rep = rep | |
def __str__(self): | |
return ( | |
f"{self.obj} #{self.inst} {self.loc} {self.when} com o fundo {self.bg}" | |
f" - repetição #{self.rep}" | |
) | |
def norep(self): | |
return ( | |
f"{self.obj} #{self.inst} {self.loc} {self.when} com o fundo {self.bg}" | |
) | |
@classmethod | |
def from_fn(cls, fn, angry = False): | |
pat = re.compile(r"^([^_]+)(\d+)_(In|Out)_(D|N)_(\w)_(\d+).jpe?g") | |
match = pat.search(fn) | |
if not match: | |
if angry: | |
raise ValueError(f"Nome de arquivo não deu match: {fn}") | |
return None | |
else: | |
obj = match.group(1) | |
inst = int(match.group(2)) | |
loc = "dentro de casa" if match.group(3) == "In" else "fora de casa" | |
when = "de dia" if match.group(4) == "D" else "de noite" | |
bg = match.group(5) | |
rep = int(match.group(6)) | |
return cls(obj, inst, loc, when, bg, rep) | |
def illum(self): | |
return f"{self.loc} e {self.when}" | |
def make_objs(fnl, angry = False): | |
dicio = {} | |
for fn in fnl: | |
pic = Picture.from_fn(fn, angry) | |
if pic is None: | |
continue | |
if pic.obj not in dicio: | |
dicio[pic.obj] = [] | |
dicio[pic.obj].append(pic) | |
return dicio | |
def lenum(l): | |
l = list(l) | |
l.sort() | |
return "{}, e {}".format(", ".join(l[:-1]), l[-1]) | |
def plural(p): | |
p = p.lower() | |
excecoes = {} | |
if p in excecoes: | |
return excecoes[p] | |
else: | |
return p + "s" | |
def summary(dirpath, angry = False): | |
fnl = os.listdir(dirpath) | |
dicio = make_objs(fnl, angry) | |
nomes = list(dicio.keys()) | |
pls = lenum(map(plural, nomes)) | |
print(f"Temos {len(nomes)} objetos: {pls}.\n") | |
print("Detalhes para cada objeto:\n") | |
for name, pics in dicio.items(): | |
num_objs = len(set([pic.inst for pic in pics])) | |
bgs = set([pic.bg for pic in pics]) | |
illums = set([pic.illum() for pic in pics]) | |
tot = len(pics) | |
reps = {} | |
for pic in pics: | |
nr = pic.norep() | |
if nr not in reps: | |
reps[nr] = 0 | |
reps[nr] += 1 | |
nreps = list(reps.values()) | |
m, n = (min(nreps), max(nreps)) | |
repcnt = n if m == n else f"de {m} a {n}" | |
print(f"==> {name}s: {tot} amostras") | |
print(f" => {num_objs} objetos diferentes") | |
print(f" => {len(bgs)} variações de fundo: {lenum(bgs)}") | |
print(f" => {len(illums)} condições de iluminação:") | |
for illum in illums: | |
print(f" => {illum}") | |
print(f" => repetições: {repcnt} por combinação") | |
print("") | |
def main(argc, argv): | |
if argc < 2: | |
print("Especifique uma pasta.") | |
return 1 | |
_, dirpath = argv[:2] | |
summary(dirpath, True) | |
return 0 | |
if __name__ == "__main__": | |
from sys import argv, exit | |
exit(main(len(argv), argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment