diff --git a/cereja/__init__.py b/cereja/__init__.py index 8de18ad..65b6eee 100644 --- a/cereja/__init__.py +++ b/cereja/__init__.py @@ -48,7 +48,7 @@ from ._requests import request -VERSION = "1.6.5.final.0" +VERSION = "1.6.6.final.0" __version__ = get_version_pep440_compliant(VERSION) diff --git a/cereja/_requests/_http.py b/cereja/_requests/_http.py index 0eaf629..4c53123 100644 --- a/cereja/_requests/_http.py +++ b/cereja/_requests/_http.py @@ -20,6 +20,7 @@ SOFTWARE. """ import threading +import time from urllib import request as urllib_req import json import io @@ -107,20 +108,22 @@ class HttpResponse: CHUNK_SIZE = 1024 * 1024 * 1 # 1MB def __init__(self, request: 'HttpRequest', save_on_path=None, timeout=None): - self._request = request - self._save_on_path = save_on_path - self._headers = {} - self._total_completed = '0.0%' - self._timeout = timeout self._finished = False + self._timeout = timeout self._code = None self._status = None self._data = None + self._headers = {} + self._request = request + self._save_on_path = save_on_path + self._total_completed = '0.0%' self._th_request = threading.Thread(target=self.__request) self._th_request.start() + while not self.headers: + time.sleep(0.1) # await headers def __request(self): - # TODO: send to HttpRequest + # TODO: send to HttpRequest ? try: with urllib_req.urlopen(self._request.urllib_req, timeout=self._timeout) as req_file: self._code = req_file.status diff --git a/cereja/config/__init__.py b/cereja/config/__init__.py index 4b6c792..d27b3f8 100644 --- a/cereja/config/__init__.py +++ b/cereja/config/__init__.py @@ -1,2 +1,2 @@ from .conf import * -from ._constants import ENG_CONTRACTIONS, PUNCTUATION, VALID_LANGUAGE_CHAR, LANGUAGES, STOP_WORDS +from ._constants import ENG_CONTRACTIONS, PUNCTUATION, VALID_LANGUAGE_CHAR, LANGUAGES, STOP_WORDS, DATA_UNIT_MAP diff --git a/cereja/config/_constants.py b/cereja/config/_constants.py index d568e65..a736091 100644 --- a/cereja/config/_constants.py +++ b/cereja/config/_constants.py @@ -21,7 +21,7 @@ SOFTWARE. """ -from cereja.system.unicode import Unicode +from ..system.unicode import Unicode import string ENG_CONTRACTIONS = { @@ -145,8 +145,8 @@ } PUNCTUATION = [',', '!', '#', '$', '%', "'", '*', '+', '-', '.', '/', '?', '@', '\\', '^', '_', '~'] STOP_WORDS = { - 'english': ['is', 'are', 'am', 'at', 'a', 'an', 'of', 'the'], - 'portuguese': ['em', 'do', 'da', 'de', 'pro', 'pra', 'no', 'dos', 'na', 'os', 'à', 'pela', 'nas', 'num',\ + 'english': ['is', 'are', 'am', 'at', 'a', 'an', 'of', 'the'], + 'portuguese': ['em', 'do', 'da', 'de', 'pro', 'pra', 'no', 'dos', 'na', 'os', 'à', 'pela', 'nas', 'num', 'das', 'numa', 'nos', 'às', 'pelas', 'as', 'uns', 'umas', 'lhe', 'é', 'o', 'a'] } LANGUAGES = {'english', 'portuguese'} @@ -155,3 +155,10 @@ VALID_LANGUAGE_CHAR.update(string.punctuation) VALID_LANGUAGE_CHAR.update(PUNCTUATION) VALID_LANGUAGE_CHAR.update(' ') + +DATA_UNIT_MAP = {"B": 1.e0, + "KB": 1.e3, + "MB": 1.e6, + "GB": 1.e9, + "TB": 1.e12 + } diff --git a/cereja/display/_display.py b/cereja/display/_display.py index 6972a19..af97a8a 100644 --- a/cereja/display/_display.py +++ b/cereja/display/_display.py @@ -621,7 +621,7 @@ def completed(self): @staticmethod def die_all(): - for progress in Progress._progresses: + for progress in Progress._progresses.values(): progress.stop() def _parse_states(self): diff --git a/cereja/file/_io.py b/cereja/file/_io.py index a0b6e39..0e5c6ab 100644 --- a/cereja/file/_io.py +++ b/cereja/file/_io.py @@ -10,10 +10,11 @@ from typing import Any, List, Union, Type, TextIO, Iterable, Tuple, KeysView, ItemsView, ValuesView import json -import cereja from .._requests import request from zipfile import ZipFile, ZIP_DEFLATED +from ..display import Progress +from ..config import DATA_UNIT_MAP from ..system import Path, mkdir from ..array import get_cols, flatten, get_shape from ..utils import is_sequence, clipboard @@ -26,12 +27,7 @@ class _IFileIO(metaclass=ABCMeta): - _size_map = {"B": 1.e0, - "KB": 1.e3, - "MB": 1.e6, - "GB": 1.e9, - "TB": 1.e12 - } + _size_map = DATA_UNIT_MAP _dont_read = [".pyc"] _ignore_dir = [".git"] _date_format = "%Y-%m-%d %H:%M:%S" @@ -767,7 +763,7 @@ def unzip(cls, file_path, save_on: str = None, members: List = None, k=None, is_ members = members or sample(myzip.namelist(), k, is_random) if save_on: mkdir(save_on) - myzip.extractall(save_on, members=cereja.Progress.prog(list(members), 'Extracting')) + myzip.extractall(save_on, members=Progress.prog(list(members), name='Extracting')) def _save_fp(self, fp: Union[TextIO, BytesIO]) -> None: raise NotImplementedError diff --git a/cereja/utils/_utils.py b/cereja/utils/_utils.py index 2919131..a574133 100644 --- a/cereja/utils/_utils.py +++ b/cereja/utils/_utils.py @@ -126,7 +126,7 @@ def clipboard() -> str: return _get_tkinter().clipboard_get() -def truncate(text: Union[str, bytes], k=4): +def truncate(text: Union[str, bytes], k=15): """ Truncate text. eg.: @@ -138,10 +138,11 @@ def truncate(text: Union[str, bytes], k=4): @param k: natural numbers, default is 4 """ assert isinstance(text, (str, bytes)), TypeError(f"{type(text)} isn't valid. Expected str or bytes") - if k > len(text) or k < 0: + if k > len(text) or k <= 4: return text - trunc_chars = '...' if isinstance(text, str) else b'...' - return text[:k] + trunc_chars + n = int((k - 4) / 2) # k is the max length of text, 4 is the length of truncate symbol + trunc_chars = '....' if isinstance(text, str) else b'....' + return text[:n] + trunc_chars + text[-n:] def obj_repr(obj_, attr_limit=10, val_limit=3, show_methods=False, show_private=False, deep=3): diff --git a/tests/testsutils.py b/tests/testsutils.py index dc21054..5c9e436 100644 --- a/tests/testsutils.py +++ b/tests/testsutils.py @@ -137,10 +137,11 @@ def test_time_format(self): pass def test_truncate(self): - self.assertEqual(utils.truncate("Cereja is fun.", k=3), 'Cer...') - self.assertEqual(utils.truncate(b"Cereja is fun.", k=3), b'Cer...') + self.assertEqual(utils.truncate("Cereja is fun.", k=3), "Cereja is fun.") + self.assertEqual(utils.truncate(b"Cereja is fun.", k=3), b"Cereja is fun.") self.assertEqual(utils.truncate("Cereja is fun.", k=-1), "Cereja is fun.") self.assertEqual(utils.truncate("Cereja is fun.", k=1000), "Cereja is fun.") + self.assertEqual(utils.truncate("Cereja is fun.", k=10), "Cer....un.") self.assertRaises(AssertionError, utils.truncate, 1123)