table-stream 0.2.8__tar.gz → 0.2.9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {table_stream-0.2.8 → table_stream-0.2.9}/PKG-INFO +1 -1
- {table_stream-0.2.8 → table_stream-0.2.9}/pyproject.toml +1 -1
- {table_stream-0.2.8 → table_stream-0.2.9}/table_stream/__init__.py +184 -22
- {table_stream-0.2.8 → table_stream-0.2.9}/table_stream.egg-info/PKG-INFO +1 -1
- {table_stream-0.2.8 → table_stream-0.2.9}/README.md +0 -0
- {table_stream-0.2.8 → table_stream-0.2.9}/setup.cfg +0 -0
- {table_stream-0.2.8 → table_stream-0.2.9}/table_stream.egg-info/SOURCES.txt +0 -0
- {table_stream-0.2.8 → table_stream-0.2.9}/table_stream.egg-info/dependency_links.txt +0 -0
- {table_stream-0.2.8 → table_stream-0.2.9}/table_stream.egg-info/requires.txt +0 -0
- {table_stream-0.2.8 → table_stream-0.2.9}/table_stream.egg-info/top_level.txt +0 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
|
-
#from collections.abc import Iterable
|
|
3
2
|
|
|
4
|
-
from abc import
|
|
3
|
+
# from collections.abc import Iterable
|
|
4
|
+
|
|
5
|
+
from abc import abstractmethod, ABC # ABCMeta
|
|
5
6
|
from typing import (
|
|
6
7
|
Any,
|
|
7
8
|
Callable,
|
|
@@ -26,10 +27,11 @@ from zipfile import ZipFile
|
|
|
26
27
|
|
|
27
28
|
import json
|
|
28
29
|
from pandas import DataFrame
|
|
29
|
-
|
|
30
|
+
|
|
31
|
+
# from pandas import Series
|
|
30
32
|
import pandas as pd
|
|
31
33
|
|
|
32
|
-
__version__ = "0.2.8"
|
|
34
|
+
__version__ = version = "0.2.8"
|
|
33
35
|
|
|
34
36
|
T = TypeVar("T")
|
|
35
37
|
K = TypeVar("K")
|
|
@@ -644,7 +646,9 @@ class HashMapDict[K, T](HashMap):
|
|
|
644
646
|
|
|
645
647
|
def to_map_str(self) -> dict[str, Any]:
|
|
646
648
|
final = dict()
|
|
647
|
-
|
|
649
|
+
_header = self.header()
|
|
650
|
+
|
|
651
|
+
for i in _header:
|
|
648
652
|
final[str(i)] = str(self.get_value(i))
|
|
649
653
|
return final
|
|
650
654
|
|
|
@@ -895,7 +899,9 @@ class Listener[NOTIFIER](ABC):
|
|
|
895
899
|
|
|
896
900
|
def _read_notifier(self, type_notifier: type[NOTIFIER]) -> NOTIFIER:
|
|
897
901
|
if self.type_notifier is None:
|
|
898
|
-
raise ValueError(
|
|
902
|
+
raise ValueError(
|
|
903
|
+
f"Nenhum notificador foi definido em ... {__class__.__name__}()"
|
|
904
|
+
)
|
|
899
905
|
return self._watched[type_notifier]
|
|
900
906
|
|
|
901
907
|
def read(self) -> NOTIFIER:
|
|
@@ -3039,12 +3045,15 @@ def create_table_from_file(
|
|
|
3039
3045
|
md5 = load_file_hash(file)
|
|
3040
3046
|
|
|
3041
3047
|
tb: TableData[HeaderMetaData] = create_void_table_files()
|
|
3048
|
+
row = RowH(0)
|
|
3042
3049
|
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3050
|
+
row.set(HeaderMetaData.FILE_NAME, file.name())
|
|
3051
|
+
row.set(HeaderMetaData.FILE_EXTENSION, file.extension() or ".nan")
|
|
3052
|
+
row.set(HeaderMetaData.FILE_PATH, file.absolute())
|
|
3053
|
+
row.set(HeaderMetaData.PARENT_DIR, file.parent().absolute())
|
|
3054
|
+
row.set(HeaderMetaData.HASH_FILE, md5)
|
|
3055
|
+
|
|
3056
|
+
tb.add_row(row)
|
|
3048
3057
|
return tb
|
|
3049
3058
|
|
|
3050
3059
|
|
|
@@ -3212,9 +3221,16 @@ class DataStorage(Observable[pd.DataFrame]):
|
|
|
3212
3221
|
|
|
3213
3222
|
|
|
3214
3223
|
class DataReader(object):
|
|
3224
|
+
"""
|
|
3225
|
+
Faz a leitura de uma planilha e retorna um Map de DataFrame()
|
|
3226
|
+
|
|
3227
|
+
"""
|
|
3215
3228
|
|
|
3216
|
-
def __init__(self, store: DataStorage) -> None:
|
|
3229
|
+
def __init__(self, store: DataStorage | None = None) -> None:
|
|
3217
3230
|
super().__init__()
|
|
3231
|
+
if store is None:
|
|
3232
|
+
store = DataStorage()
|
|
3233
|
+
|
|
3218
3234
|
self._store = store
|
|
3219
3235
|
|
|
3220
3236
|
def read(self, path: File) -> pd.DataFrame:
|
|
@@ -3233,9 +3249,9 @@ class DataSave(Observer[pd.DataFrame]):
|
|
|
3233
3249
|
Salvar o DataFrame() em um arquivo sempre receber uma notificação.
|
|
3234
3250
|
"""
|
|
3235
3251
|
|
|
3236
|
-
def __init__(self, path_sheet: File) -> None:
|
|
3252
|
+
def __init__(self, path_sheet: File | None = None) -> None:
|
|
3237
3253
|
super().__init__()
|
|
3238
|
-
self.path_sheet: File = path_sheet
|
|
3254
|
+
self.path_sheet: File | None = path_sheet
|
|
3239
3255
|
self.csv_options: CsvOptions = CsvOptions()
|
|
3240
3256
|
self.debug: bool = True
|
|
3241
3257
|
|
|
@@ -3246,6 +3262,9 @@ class DataSave(Observer[pd.DataFrame]):
|
|
|
3246
3262
|
|
|
3247
3263
|
def update(self, value: pd.DataFrame) -> None:
|
|
3248
3264
|
|
|
3265
|
+
if self.path_sheet is None:
|
|
3266
|
+
raise ValueError(f"{__class__.__name__} o arquivo de destino é nulo.")
|
|
3267
|
+
|
|
3249
3268
|
self._show(f"Salvando ... {self.path_sheet.absolute()}")
|
|
3250
3269
|
|
|
3251
3270
|
if self.path_sheet.is_csv():
|
|
@@ -3282,6 +3301,7 @@ class SplitterData(Notifier):
|
|
|
3282
3301
|
def split(self, df: pd.DataFrame, output: Directory) -> None:
|
|
3283
3302
|
if df.empty:
|
|
3284
3303
|
return None
|
|
3304
|
+
df = df.astype(str)
|
|
3285
3305
|
|
|
3286
3306
|
self.output_dir = output
|
|
3287
3307
|
values_split = Set(df[self.col_split].drop_duplicates().values.tolist())
|
|
@@ -3306,16 +3326,42 @@ class SplitterData(Notifier):
|
|
|
3306
3326
|
return super().notify_listeners()
|
|
3307
3327
|
|
|
3308
3328
|
|
|
3309
|
-
class
|
|
3329
|
+
class ListenerSplitter(Listener[SplitterData]):
|
|
3310
3330
|
|
|
3311
3331
|
def __init__(self) -> None:
|
|
3312
3332
|
super().__init__()
|
|
3313
|
-
self.saver = DataSave(
|
|
3333
|
+
self.saver = DataSave()
|
|
3334
|
+
|
|
3335
|
+
def set_csv_options(self, opt: CsvOptions):
|
|
3336
|
+
self.saver.csv_options = opt
|
|
3314
3337
|
|
|
3315
3338
|
def receiver_notifier(self) -> None:
|
|
3316
3339
|
sp = self.read()
|
|
3317
3340
|
self.update(sp)
|
|
3318
3341
|
|
|
3342
|
+
def create_export_files(self, d: Directory, filename: str) -> Set[File]:
|
|
3343
|
+
dir_excel = d.concat("EXCEL", True)
|
|
3344
|
+
dir_csv = d.concat("CSV", True)
|
|
3345
|
+
|
|
3346
|
+
path_excel = dir_excel.join_file(f"{filename}.xlsx")
|
|
3347
|
+
path_csv = dir_csv.join_file(f"{filename}.csv")
|
|
3348
|
+
return Set([path_excel, path_csv])
|
|
3349
|
+
|
|
3350
|
+
def _run_export(self, df: pd.DataFrame, files: Set[File]) -> None:
|
|
3351
|
+
"""
|
|
3352
|
+
Exporta o mesmo DataFrame() para varios caminhos/arquivos
|
|
3353
|
+
|
|
3354
|
+
util para exportar csv/excel
|
|
3355
|
+
"""
|
|
3356
|
+
|
|
3357
|
+
if files.empty():
|
|
3358
|
+
return None
|
|
3359
|
+
|
|
3360
|
+
f: File
|
|
3361
|
+
for f in files:
|
|
3362
|
+
self.saver.path_sheet = f
|
|
3363
|
+
self.saver.update(df)
|
|
3364
|
+
|
|
3319
3365
|
def update(self, value: SplitterData) -> None:
|
|
3320
3366
|
if value.output_dir is None:
|
|
3321
3367
|
return
|
|
@@ -3325,12 +3371,128 @@ class DataExport(Listener):
|
|
|
3325
3371
|
d = value.output_dir
|
|
3326
3372
|
d.mkdir()
|
|
3327
3373
|
|
|
3328
|
-
|
|
3374
|
+
final_filename: str = str(value.current_file_name)
|
|
3329
3375
|
|
|
3330
3376
|
if value.current_prefix is not None:
|
|
3331
|
-
|
|
3377
|
+
final_filename = f"{value.current_prefix}-{final_filename}"
|
|
3378
|
+
|
|
3379
|
+
print(f"Progresso: [{value.done}/{value.total_export}]")
|
|
3380
|
+
|
|
3381
|
+
files = self.create_export_files(d, final_filename)
|
|
3382
|
+
df = value.storage.get_data()
|
|
3383
|
+
self._run_export(df, files)
|
|
3384
|
+
|
|
3385
|
+
|
|
3386
|
+
class ListenerSplitterQgiz(ListenerSplitter):
|
|
3387
|
+
|
|
3388
|
+
def __init__(self) -> None:
|
|
3389
|
+
super().__init__()
|
|
3390
|
+
|
|
3391
|
+
def create_export_files(self, d: Directory, filename: str) -> Set[File]:
|
|
3392
|
+
sp = self.read()
|
|
3393
|
+
current_data = sp.storage.get_data()
|
|
3394
|
+
cols = ArrayList(current_data.columns.to_list())
|
|
3395
|
+
|
|
3396
|
+
dir_excel = d.concat("EXCEL", True)
|
|
3397
|
+
dir_csv = d.concat("CSV", True)
|
|
3398
|
+
|
|
3399
|
+
col_ano = "numano"
|
|
3400
|
+
col_mes = "nummes"
|
|
3401
|
+
|
|
3402
|
+
if cols.contains(col_ano):
|
|
3403
|
+
ano = str(current_data[col_ano].values.tolist()[0])
|
|
3404
|
+
|
|
3405
|
+
dir_excel = dir_excel.concat(ano, True)
|
|
3406
|
+
dir_csv = dir_csv.concat(ano, True)
|
|
3407
|
+
|
|
3408
|
+
if cols.contains(col_mes):
|
|
3409
|
+
mes = str(current_data[col_mes].values.tolist()[0])
|
|
3410
|
+
|
|
3411
|
+
dir_excel = dir_excel.concat(mes, True)
|
|
3412
|
+
dir_csv = dir_csv.concat(mes, True)
|
|
3413
|
+
|
|
3414
|
+
return Set.from_list(
|
|
3415
|
+
[
|
|
3416
|
+
dir_excel.join_file(f"{filename}.xlsx"),
|
|
3417
|
+
dir_csv.join_file(f"{filename}.csv"),
|
|
3418
|
+
]
|
|
3419
|
+
)
|
|
3420
|
+
|
|
3421
|
+
|
|
3422
|
+
class ListenerStorage(Observer[pd.DataFrame]):
|
|
3423
|
+
|
|
3424
|
+
def __init__(
|
|
3425
|
+
self,
|
|
3426
|
+
output_dir: Directory,
|
|
3427
|
+
col_name: str,
|
|
3428
|
+
*,
|
|
3429
|
+
cols_subdirs: ArrayList[str] = None,
|
|
3430
|
+
) -> None:
|
|
3431
|
+
super().__init__()
|
|
3432
|
+
if cols_subdirs is None:
|
|
3433
|
+
cols_subdirs = Set[str]()
|
|
3434
|
+
self.cols_subdirs: ArrayList[str] = cols_subdirs
|
|
3435
|
+
self.output_dir: Directory = output_dir
|
|
3436
|
+
self.col_name: str = col_name
|
|
3437
|
+
|
|
3438
|
+
def get_current_export_dir(self, df: pd.DataFrame) -> Directory:
|
|
3439
|
+
if self.cols_subdirs.empty():
|
|
3440
|
+
return self.output_dir
|
|
3441
|
+
|
|
3442
|
+
concat_dirs = ArrayList()
|
|
3443
|
+
|
|
3444
|
+
for c in self.cols_subdirs:
|
|
3445
|
+
first = df[c].astype(str).values.tolist()[0]
|
|
3446
|
+
concat_dirs.append(first)
|
|
3447
|
+
|
|
3448
|
+
final_dir = Directory(self.output_dir.absolute())
|
|
3449
|
+
for item in concat_dirs:
|
|
3450
|
+
final_dir = final_dir.concat(item)
|
|
3451
|
+
|
|
3452
|
+
final_dir.mkdir()
|
|
3453
|
+
return final_dir
|
|
3454
|
+
|
|
3455
|
+
def update(self, value: pd.DataFrame) -> None:
|
|
3456
|
+
|
|
3457
|
+
path_dir = self.get_current_export_dir(value)
|
|
3458
|
+
filename = value[self.col_name].astype(str).values.tolist()[0]
|
|
3459
|
+
|
|
3460
|
+
csv_file = path_dir.concat("CSV", True).join_file(f"{filename}.csv")
|
|
3461
|
+
excel_file = path_dir.concat("EXCEL", True).join_file(f"{filename}.xlsx")
|
|
3462
|
+
|
|
3463
|
+
saver = DataSave(excel_file)
|
|
3464
|
+
saver.update(value)
|
|
3465
|
+
|
|
3466
|
+
saver.path_sheet = csv_file
|
|
3467
|
+
saver.update(value)
|
|
3468
|
+
|
|
3469
|
+
|
|
3470
|
+
def fmt_dataframe_circuit(df: pd.DataFrame) -> pd.DataFrame:
|
|
3471
|
+
rmnull = ["vlr_coordenada_longitude"]
|
|
3472
|
+
select = [
|
|
3473
|
+
"numano",
|
|
3474
|
+
"nummes",
|
|
3475
|
+
"numliv",
|
|
3476
|
+
"codlcd",
|
|
3477
|
+
"numrota",
|
|
3478
|
+
"numcta",
|
|
3479
|
+
"numcdc",
|
|
3480
|
+
"vlr_coordenada_latitude",
|
|
3481
|
+
"vlr_coordenada_longitude",
|
|
3482
|
+
]
|
|
3483
|
+
|
|
3484
|
+
df.dropna(subset=rmnull, inplace=True)
|
|
3485
|
+
df = df[select]
|
|
3486
|
+
return df
|
|
3487
|
+
|
|
3488
|
+
|
|
3489
|
+
def split_dataframe(df: pd.DataFrame, col_split: str, *, storage: DataStorage) -> None:
|
|
3490
|
+
df = fmt_dataframe_circuit(df)
|
|
3491
|
+
values_split = Set(df[col_split].drop_duplicates().values.tolist())
|
|
3492
|
+
|
|
3493
|
+
total = len(values_split)
|
|
3494
|
+
for num, item in enumerate(values_split):
|
|
3495
|
+
print(f"Progresso: {num+1}/{total}", end=" ")
|
|
3332
3496
|
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
self.saver.path_sheet = path
|
|
3336
|
-
self.saver.update(value.storage.get_data())
|
|
3497
|
+
current = df[df[col_split] == item]
|
|
3498
|
+
storage.set_data(current)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|