Skip to content

Commit 551dabf

Browse files
committed
🗓 Dec 21, 2024 9:18:22 AM
🐙 update find_replace to use byte pattern 🤖 types added/updated 🧪 tests added/updated
1 parent 73fb105 commit 551dabf

File tree

7 files changed

+43
-14
lines changed

7 files changed

+43
-14
lines changed

chepy/core.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __str__(self):
139139
logging.exception(
140140
"\n\nCannot print current state. Either chain with "
141141
"another method, or use one of the output methods "
142-
"Example: .o, .out, .state or .out\n\n"
142+
"Example: .o, .out, or .state\n\n"
143143
)
144144
return ""
145145

chepy/modules/dataformat.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def to_int(
478478
if isinstance(self.state, bytes):
479479
self.state = int.from_bytes(self.state, byteorder)
480480
elif isinstance(self.state, str):
481-
self.state = int(self.state, base)
481+
self.state = int(self.state, int(base))
482482
return self
483483

484484
@ChepyDecorators.call_stack
@@ -1494,6 +1494,20 @@ def substitute(self, x: str, y: str) -> DataFormatT:
14941494
self.state = s.translate(o)
14951495
return self
14961496

1497+
@ChepyDecorators.call_stack
1498+
def remove(self, pattern: Union[str, bytes] = b"") -> DataFormatT:
1499+
"""Remove is identifcal to find_replace, except it removes the identified
1500+
pattern with an empty value.
1501+
1502+
Args:
1503+
pattern (Union[str, bytes], optional): Replace non-printable characters with this. Defaults to ''.
1504+
1505+
Returns:
1506+
Chepy: The Chepy object.
1507+
"""
1508+
self.find_replace(pattern, b"")
1509+
return self
1510+
14971511
@ChepyDecorators.call_stack
14981512
def remove_nonprintable(self, replace_with: Union[str, bytes] = b"") -> DataFormatT:
14991513
"""Remove non-printable characters from string.

chepy/modules/dataformat.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class DataFormat(ChepyCore):
7575
def length(self: DataFormatT) -> DataFormatT: ...
7676
def to_leetcode(self: DataFormatT, replace_space: str=...) -> DataFormatT: ...
7777
def substitute(self: DataFormatT, x: str=..., y: str=...) -> DataFormatT: ...
78+
def remove(self: DataFormatT, pattern: Union[str, bytes] = ...): ...
7879
def remove_nonprintable(self: DataFormatT, replace_with: Union[str, bytes] = ...): ...
7980
def to_base92(self: DataFormatT) -> DataFormatT: ...
8081
def from_base92(self: DataFormatT) -> DataFormatT: ...

chepy/modules/utils.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ def strip_non_printable(self) -> UtilsT:
509509
return self
510510

511511
@ChepyDecorators.call_stack
512-
def find_replace(self, pattern: str, repl: str, ignore_case=True) -> UtilsT:
512+
def find_replace(
513+
self, pattern: Union[bytes, str], repl: Union[bytes, str], ignore_case=True
514+
) -> UtilsT:
513515
"""Replace matched pattern with repln
514516
515517
Args:
@@ -527,7 +529,12 @@ def find_replace(self, pattern: str, repl: str, ignore_case=True) -> UtilsT:
527529
flags = 0
528530
if ignore_case:
529531
flags = re.IGNORECASE
530-
self.state = re.sub(pattern, repl, self._convert_to_str(), flags=flags)
532+
self.state = re.sub(
533+
self._to_bytes(pattern),
534+
self._to_bytes(repl),
535+
self._convert_to_bytes(),
536+
flags=flags,
537+
)
531538
return self
532539

533540
@ChepyDecorators.call_stack

chepy/modules/utils.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Utils(ChepyCore):
2929
def strip_ansi(self: UtilsT) -> UtilsT: ...
3030
def strip(self: UtilsT, pattern: str, ignore_case: bool=...) -> UtilsT: ...
3131
def strip_non_printable(self: UtilsT) -> UtilsT: ...
32-
def find_replace(self: UtilsT, pattern: str, repl: str, ignore_case: bool=...) -> UtilsT: ...
32+
def find_replace(self: UtilsT, pattern: Union[bytes, str], repl: Union[bytes, str], ignore_case: bool=...) -> UtilsT: ...
3333
def escape_string(self: UtilsT) -> UtilsT: ...
3434
def unescape_string(self: UtilsT) -> UtilsT: ...
3535
def color_hex_to_rgb(self: UtilsT) -> UtilsT: ...

docs/conf.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616

1717
sys.path.insert(0, os.path.abspath("."))
1818

19+
1920
def setup(app):
20-
app.add_config_value('recommonmark_config', {
21-
'enable_eval_rst': True,
22-
}, True)
21+
app.add_config_value(
22+
"recommonmark_config",
23+
{
24+
"enable_eval_rst": True,
25+
},
26+
True,
27+
)
2328
app.add_transform(AutoStructify)
2429

2530

2631
# -- Project information -----------------------------------------------------
2732

2833
project = "Chepy"
29-
copyright = "2019, @securisec"
34+
copyright = "2025, @securisec"
3035
author = "@securisec"
3136

32-
master_doc = 'index'
37+
master_doc = "index"
3338

3439

3540
# -- General configuration ---------------------------------------------------
@@ -66,7 +71,8 @@ def setup(app):
6671
autodoc_member_order = "groupwise"
6772

6873
source_suffix = {
69-
'.rst': 'restructuredtext',
70-
'.txt': 'markdown',
71-
'.md': 'markdown',
72-
}
74+
".rst": "restructuredtext",
75+
".txt": "markdown",
76+
".md": "markdown",
77+
}
78+

tests/test_utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def test_string_non_printable():
187187

188188
def test_find_replace():
189189
assert Chepy("some some data").find_replace(r"some\s", "data").o == b"datadatadata"
190+
assert Chepy("some some data").remove(r"some\s").o == b"data"
190191

191192

192193
def test_escape_string():

0 commit comments

Comments
 (0)