Skip to content

Commit

Permalink
v1.6.6 (#170)
Browse files Browse the repository at this point in the history
* fix: fix die_all

* improves: v1.6.6
  • Loading branch information
jlsneto authored Mar 29, 2022
1 parent cc5a9f5 commit 22b7e0b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cereja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions cereja/_requests/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SOFTWARE.
"""
import threading
import time
from urllib import request as urllib_req
import json
import io
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cereja/config/__init__.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 10 additions & 3 deletions cereja/config/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
SOFTWARE.
"""

from cereja.system.unicode import Unicode
from ..system.unicode import Unicode
import string

ENG_CONTRACTIONS = {
Expand Down Expand Up @@ -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'}
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cereja/display/_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 4 additions & 8 deletions cereja/file/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions cereja/utils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.:
Expand All @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions tests/testsutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 22b7e0b

Please sign in to comment.