Skip to content

Commit

Permalink
Added functions to shadow re
Browse files Browse the repository at this point in the history
  • Loading branch information
smartycope committed Apr 19, 2024
1 parent 7731de5 commit 2eb5fa2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
27 changes: 27 additions & 0 deletions ezregex/EZRegex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import re
from copy import deepcopy
from functools import partial
import sys
from typing import Callable, List, Literal

from .api import api
from .generate import *
from .invert import invert
from ._dialects import dialects

# TODO: Seperate EZRegex into a "bytes" mode vs "string" mode
# TODO: consider changing addFlags to "outer" or "end" or something
# TODO: Seriously consider removing the debug functions
# TODO: in all the magic functions assert that we're not mixing dialects
Expand Down Expand Up @@ -222,6 +224,31 @@ def inverse(self, amt=1, **kwargs):
def invert(self, amt=1, **kwargs):
return self.inverse(amt, **kwargs)

# Shadowing the re functions
def search(self, string, pos=0, endpos=sys.maxsize):
return self.compile().search(string, pos, endpos)

def match(self, string, pos=0, endpos=sys.maxsize):
return self.compile().match(string, pos, endpos)

def fullmatch(self, string, pos=0, endpos=sys.maxsize):
return self.compile().fullmatch(string, pos, endpos)

def split(self, string, maxsplit=0):
return self.compile().split(string, maxsplit)

def findall(self, string, pos=0, endpos=sys.maxsize):
return self.compile().findall(string, pos, endpos)

def finditer(self, string, pos=0, endpos=sys.maxsize):
return self.compile().finditer(string, pos, endpos)

def sub(self, repl, string, count=0):
return self.compile().sub(repl, string, count)

def subn(self, repl, string, count=0):
return self.compile().subn(repl, string, count)

# Magic Functions
def __call__(self, *args, **kwargs):
""" This should be called by the user to specify the specific parameters of this instance i.e. anyof('a', 'b') """
Expand Down
13 changes: 12 additions & 1 deletion ezregex/EZRegex.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from typing import Callable
import sys
from typing import Any, Callable, Iterator
from mypy_extensions import DefaultNamedArg, VarArg
from .base.interface import InputType

Expand Down Expand Up @@ -41,6 +42,16 @@ class EZRegex:
""" "Inverts" the current Regex expression to give an example of a string it would match.
Useful for debugging purposes. """

# Shadowing the re functions
def search(self, string, pos:int=0, endpos:int=sys.maxsize) -> re.Match|None: ...
def match(self, string, pos:int=0, endpos:int=sys.maxsize) -> re.Match|None: ...
def fullmatch(self, string, pos:int=0, endpos:int=sys.maxsize) -> re.Match|None: ...
def split(self, string, maxsplit:int=0) -> list: ...
def findall(self, string, pos: int = 0, endpos: int = sys.maxsize) -> list: ...
def finditer(self, string, pos: int = 0, endpos: int = sys.maxsize) -> Iterator[re.Match]: ...
def sub(self, repl: Any | Callable[[re.Match], Any], string, count: int = 0): ...
def subn(self, repl: Any | Callable[[re.Match], Any], string, count: int = 0): ...

# Magic Functions
def __call__(self, *args, **kwargs) -> EZRegex | str:
""" This should be called by the user to specify the specific parameters of this instance i.e. anyof('a', 'b') """
Expand Down
19 changes: 19 additions & 0 deletions tests/test_EZRegex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import re
from ezregex import *


Expand Down Expand Up @@ -62,3 +63,21 @@ def test_no_parameters_to_chains():
assert match_amt(6, digit)()
assert (digit + word)()
assert digit()

def test_re_shadow_funcs():
s = r'\d(\w+)'
string = 'timmy is 6years old'
repl = replace('number {1}')

def eq(a, b):
if not ((a is None) == (b is None)) and a.span() == b.span() and a.groups() == b.groups():
raise AssertionError(f'{a} != {b}')

eq((digit + group(word)).search(string), re.compile(s).search(string))
eq((digit + group(word)).match(string), re.compile(s).match(string))
eq((digit + group(word)).fullmatch(string), re.compile(s).fullmatch(string))
eq((digit + group(word)).split(string), re.compile(s).split(string))
eq((digit + group(word)).findall(string), re.compile(s).findall(string))
eq((digit + group(word)).finditer(string), re.compile(s).finditer(string))
eq((digit + group(word)).sub(repl, string), re.compile(s).sub(repl, string))
eq((digit + group(word)).subn(repl, string), re.compile(s).subn(repl, string))

0 comments on commit 2eb5fa2

Please sign in to comment.