Skip to content

Commit

Permalink
🗓 Jul 28, 2022 12:30:03 AM
Browse files Browse the repository at this point in the history
🧪 tests added/updated
💚 build steps added/updated
🔍 refactor to/from binary and octal
🔥 rename unicode_chars_by_name to search_perl_unicode_props
🔥 unify all en/decoing functions to encode and decode
  • Loading branch information
securisec committed Jul 28, 2022
1 parent 32c0a8c commit 25ffaa6
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 918 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: Test with pytest
run: |
pytest --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
pytest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
coverage report -m
- name: Test plugins osx
Expand All @@ -53,7 +53,7 @@ jobs:
sed -iE '/chepy/d' chepy/chepy_plugins/requirements.txt
pip install -r chepy/chepy_plugins/requirements.txt
pip install .
pytest --disable-pytest-warnings tests_plugins/
pytest -v --disable-pytest-warnings tests_plugins/
python -c "from chepy import Chepy"
- name: Test plugins ubuntu
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


test:
pytest --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
pytest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/

test-all: test
pytest --disable-pytest-warnings tests_plugins/
pytest -v --disable-pytest-warnings tests_plugins/
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
✔ 🔥 combine all encoding/decoding methods @project(New ideas)
✔ translate function that takes a string the converts the state @project(New ideas)
✔ split by newline @project(New ideas)
✔ string to leetcode @project(New ideas)
Expand Down
56 changes: 34 additions & 22 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,67 +825,79 @@ def from_decimal(self, delimiter: str = " ", join_by: str = "") -> DataFormatT:
return self

@ChepyDecorators.call_stack
def to_binary(self) -> DataFormatT:
def to_binary(self, join_by: str = " ") -> DataFormatT:
"""Convert string characters to binary
Args:
join_by (str, optional): join_by. Defaults to " ".
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("abc").to_binary().o
["01100001", "01100010", "01100011"]
"01100001 01100010 01100011"
"""
self.state = list(format(ord(s), "08b") for s in list(self._convert_to_str()))
self.state = join_by.join(
list(format(ord(s), "08b") for s in list(self._convert_to_str()))
)
return self

@ChepyDecorators.call_stack
def from_binary(self) -> DataFormatT:
def from_binary(self, delimiter: str = " ") -> DataFormatT:
"""Convert a list of binary numbers to string
Args:
delimiter (str, optional): Delimiter. Defaults to " ".
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy(["01100001", "01100010", "01100011"]).from_binary().o
[
"a",
"b",
"c",
]
"""
if isinstance(self.state, list):
self.state = list(chr(int(s, 2)) for s in self.state)
else:
n = int(self._convert_to_str(), 2)
self.state = n.to_bytes((n.bit_length() + 7) // 8, "big")
"abc"
"""
n = int("".join(self._convert_to_str().split(delimiter)), 2)
self.state = n.to_bytes((n.bit_length() + 7) // 8, "big")
return self

@ChepyDecorators.call_stack
def to_octal(self) -> DataFormatT:
def to_octal(self, join_by: str = " ") -> DataFormatT:
"""Convert string characters to octal
Args:
join_by (str, optional): Join by. Defaults to "".
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("abㅎ").to_octal().o
["141", "142", "30516"]
"141 142 30516"
"""
self.state = list(format(ord(s), "0o") for s in list(self._convert_to_str()))
self.state = join_by.join(
list(format(ord(s), "0o") for s in list(self._convert_to_str()))
)
return self

@ChepyDecorators.call_stack
def from_octal(self) -> DataFormatT:
def from_octal(self, delimiter: str = None, join_by: str = "") -> DataFormatT:
"""Convert a list of octal numbers to string
Args:
delimiter (str, optional): Delimiter. Defaults to None.
join_by (str, optional): Join by. Defaults to "".
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy(["141", "142", "30516"]).from_octal().o
["a", "b", "ㅎ"]
>>> Chepy("141 142").from_octal().o
"ab"
"""
self.state = list(chr(int(str(s), 8)) for s in self.state)
self.state = join_by.join(
list(chr(int(str(x), 8)) for x in self._convert_to_str().split(delimiter))
)
return self

@ChepyDecorators.call_stack
Expand Down
12 changes: 6 additions & 6 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..core import ChepyCore
from typing import Any, TypeVar, Union
from typing import Any, Literal, TypeVar, Union

yaml: Any
DataFormatT = TypeVar('DataFormatT', bound='DataFormat')
Expand Down Expand Up @@ -30,7 +30,7 @@ class DataFormat(ChepyCore):
def from_bytes(self: DataFormatT) -> DataFormatT: ...
def base64_encode(self: DataFormatT, custom: str=...) -> DataFormatT: ...
def base64_decode(self: DataFormatT, custom: str=..., fix_padding: bool=...) -> DataFormatT: ...
def decode_bytes(self: DataFormatT, errors: str=...) -> DataFormatT: ...
def decode_bytes(self: DataFormatT, errors: Literal['ignore', 'backslashreplace', 'replace']=...) -> DataFormatT: ...
def to_hex(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def from_hex(self: DataFormatT, delimiter: str=...) -> DataFormatT: ...
def hex_to_int(self: DataFormatT) -> DataFormatT: ...
Expand All @@ -53,10 +53,10 @@ class DataFormat(ChepyCore):
def from_charcode(self: DataFormatT, delimiter: str=..., join_by: str=..., base: int=...) -> DataFormatT: ...
def to_decimal(self: DataFormatT) -> DataFormatT: ...
def from_decimal(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def to_binary(self: DataFormatT) -> DataFormatT: ...
def from_binary(self: DataFormatT) -> DataFormatT: ...
def to_octal(self: DataFormatT) -> DataFormatT: ...
def from_octal(self: DataFormatT) -> DataFormatT: ...
def to_binary(self: DataFormatT, join_by: str=...) -> DataFormatT: ...
def from_binary(self: DataFormatT, delimiter: str=...) -> DataFormatT: ...
def to_octal(self: DataFormatT, join_by: str=...) -> DataFormatT: ...
def from_octal(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def to_html_entity(self: DataFormatT) -> DataFormatT: ...
def from_html_entity(self: DataFormatT) -> DataFormatT: ...
def to_punycode(self: DataFormatT) -> DataFormatT: ...
Expand Down

0 comments on commit 25ffaa6

Please sign in to comment.