Пример #1
0
 def __init__(self,
              printable_subset: Optional[Iterable[str]] = None) -> None:
     self.omit_types: Set[str] = set()
     self.omit_wrappers: Set[str] = set()
     self._rxtypes = RxTypeSet()
     self._char_sets = CharSets(self._rxtypes)
     self._rxwrappers = RxWrapperSet(self._char_sets, printable_subset)
Пример #2
0
 def test_create_wrapper_defaults_invalid_char_set(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     with self.assertRaises(TypeError):
         wrappers.create_wrapper("test",
                                 "re",
                                 display_value="X",
                                 is_char_set=True)
Пример #3
0
    def test_create_wrapper_full(self):
        data = {
            "name": "test",
            "rxtype_name": "re",
            "child_types": ["alpha_lower", "digit"],
            "child_count": 3,
            "is_modifiable": False,
            "strip_child_mods": True,
            "uniform_child_types": True,
            "display_function_name": "or",
            "compile_function_name": "or",
        }
        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        wrapper = wrappers.create_wrapper(**data)
        mock_node = MockNode(children=[
            MockNode(_d("f"), _d("f")),
            MockNode(_d("j"), _d("j")),
        ])

        self.assertEqual(wrapper.name, data["name"])
        self.assertEqual(wrapper.rxtype.name, data["rxtype_name"])
        self.assertEqual(wrapper.child_types, data["child_types"])
        self.assertEqual(wrapper.child_count, data["child_count"])
        self.assertFalse(wrapper.is_modifiable)
        self.assertTrue(wrapper.strip_child_mods)
        self.assertTrue(wrapper.uniform_child_types)
        self.assertEqual(wrapper.display_function(mock_node), d_or(mock_node))
        self.assertIn(wrapper.compile_function(mock_node), ["f", "j"])
Пример #4
0
    def test_all(self):
        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        wrappers._wrappers[self.river_wrapper.name] = self.river_wrapper
        wrappers._wrappers[self.candle_wrapper.name] = self.candle_wrapper
        result = wrappers.all()

        self.assertEqual(len(result), 2)
        self.assertSetEqual(set(result),
                            set([self.river_wrapper, self.candle_wrapper]))
Пример #5
0
    def test_create_wrapper_defaults(self):
        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        wrapper = wrappers.create_wrapper("test", "re", display_value="X")

        self.assertEqual(wrapper.child_types, None)
        self.assertEqual(wrapper.child_count, 0)
        self.assertTrue(wrapper.is_modifiable)
        self.assertFalse(wrapper.strip_child_mods)
        self.assertFalse(wrapper.uniform_child_types)
Пример #6
0
 def test_create_wrapper_format_char_set_name_with_value(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     data = {
         "name": "digit",
         "rxtype_name": "digit",
         "is_char_set": True,
         "char_value": "5",
     }
     wrapper = wrappers.create_wrapper(**data)
     self.assertEqual(wrapper.name, "digit(5)")
Пример #7
0
    def test_create_wrapper_compile_function_from_display_value_only(self):
        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        data = {
            "name": "test",
            "display_value": "X",
            "rxtype_name": "re",
        }

        wrapper = wrappers.create_wrapper(**data)
        self.assertEqual(wrapper.compile_function(None), data["display_value"])
Пример #8
0
    def test_create_wrapper_defaults_func_name(self):
        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        set_wrapper = wrappers.create_wrapper("set", "re")
        mock_node = MockNode(children=[
            MockNode(_d("a"), _d("a"), rxtype=RxType("test")),
        ], )

        self.assertEqual(set_wrapper.display_function(mock_node),
                         d_set(mock_node))
        self.assertEqual(set_wrapper.compile_function(mock_node),
                         c_set(mock_node))
Пример #9
0
    def test_get_wrapper_func_name(self):
        data = {
            "0+": "zero_plus",
            "0/1": "zero_one",
            "1+": "one_plus",
            "!set": "nset",
        }

        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        for name in data.keys():
            result = wrappers.get_wrapper_func_name(name)
            self.assertEqual(result, data[name])
Пример #10
0
    def test_create_wrapper_explicit_compile_function_name(self):
        wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
        data = {
            "name": "empty!term",
            "display_value": r"\B",
            "rxtype_name": "cset*",
            "compile_function_name": "empty",
        }

        wrapper = wrappers.create_wrapper(**data)
        c_func = rxwrapper_functions["compile"][data["compile_function_name"]]
        self.assertEqual(wrapper.compile_function(None), c_func(None))
Пример #11
0
 def test_getitem(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     wrappers._wrappers[self.river_wrapper.name] = self.river_wrapper
     self.assertEqual(wrappers[self.river_wrapper.name], self.river_wrapper)
Пример #12
0
 def setUp(self):
     self.rxtypes = RxTypeSet()
     self.char_sets = CharSets(self.rxtypes)
     self.rxwrappers = RxWrapperSet(self.char_sets)
Пример #13
0
 def test_init_wrappers_printable_subset(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     wrappers.init_wrappers(printable_subset=["alpha_upper", "digit"])
     # print(wrappers._char_sets._char_sets)
     # print(wrappers._wrappers)
     self.assertTrue(False)
Пример #14
0
 def test_init_wrappers(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     wrappers.init_wrappers()
     # print(wrappers._char_sets._char_sets)
     # print(wrappers._wrappers)
     self.assertTrue(False)
Пример #15
0
 def test_create_wrapper_defaults_invalid_function(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     with self.assertRaises(TypeError):
         wrappers.create_wrapper("test", "re")
Пример #16
0
 def test_add(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     wrappers.add(self.river_wrapper)
     self.assertEqual(wrappers[self.river_wrapper.name], self.river_wrapper)
Пример #17
0
 def test_wrapper_is_type(self):
     wrappers = RxWrapperSet(self.char_sets, init_wrappers=False)
     wrappers._wrappers[self.river_wrapper.name] = self.river_wrapper
     result = wrappers.wrapper_is_type("river", "water")
     self.assertTrue(result)
Пример #18
0
class RxNodeFactory:
    def __init__(self,
                 printable_subset: Optional[Iterable[str]] = None) -> None:
        self.omit_types: Set[str] = set()
        self.omit_wrappers: Set[str] = set()
        self._rxtypes = RxTypeSet()
        self._char_sets = CharSets(self._rxtypes)
        self._rxwrappers = RxWrapperSet(self._char_sets, printable_subset)

    def set_omit(
        self,
        types: Optional[Union[str, Iterable[str]]] = None,
        wrappers: Optional[Union[str, Iterable[str]]] = None,
    ) -> None:

        if types and not isinstance(types, set):
            types = set(types)
        self.omit_types = types or set()

        if wrappers and not isinstance(wrappers, set):
            wrappers = set(wrappers)
        self.omit_wrappers = wrappers or set()

    def clear_omit(self, types: bool = False, wrappers: bool = False) -> None:
        if not (types and wrappers):
            self.clear_omit(types=True, wrappers=True)

        if types:
            self.omit_types.clear()
        if wrappers:
            self.omit_wrappers.clear()

    def parse_rxspec(self, rxspec: RxSpec) -> NodeSpec:
        if not isinstance(rxspec, list):
            rxspec = [rxspec]

        node_spec = {"rw_name": rxspec[0]}
        for spec in rxspec[1:]:
            if self._rxwrappers.wrapper_is_type(first_nested(spec), "mod"):
                node_spec["modifier"] = self.parse_rxspec(spec)

            else:
                node_spec["children"] = [
                    self.parse_rxspec(child) for child in spec
                ]

        return node_spec

    def make_node(
        self,
        rw_name: Optional[str] = None,
        children: Union[List[NodeSpec], int] = RAND,
        modifier: Optional[Union[NodeSpec, int]] = None,
        rxwrapper: Optional[RxWrapper] = None,
        is_child: bool = False,
        strict_type_match: bool = False,
    ) -> RxNode:
        """
        children format: [{'rw_name': regex_wrapper_name, 'children': [<children>]})]
        modifier format: {'rw_name': <modifier_name>, 'children': <children>, 'modifier': <modifier>}
        """

        if not rxwrapper:
            if not rw_name:
                raise ValueError("must provide regex wrapper object or name")

            rxwrapper = self._rxwrappers[rw_name]

        child_nodes: List[RxNode] = []
        if rxwrapper.child_count != 0:
            if children == RAND:
                child_types: List[str] = list(
                    filter(
                        lambda type_name: not self._rxtypes.is_one_of(
                            type_name, self.omit_types),
                        rxwrapper.child_types,
                    ))

                if rxwrapper.uniform_child_types:
                    child_types = sample(rxwrapper.child_types, 1)
                child_nodes = [
                    self.make_random_node(choice(child_types), is_child=True)
                    for i in range(rxwrapper.get_child_count())
                ]
            else:
                for child in children:
                    child_nodes.append(self.make_node(**child, is_child=True))

        node: RxNode = RxNode(self._char_sets, rxwrapper, child_nodes,
                              is_child)
        if rxwrapper.is_modifiable:
            if modifier == RAND:
                # print("- ", node.name)
                # print("- ", rxwrapper.name)
                # print("- ", rxwrapper.rxtype)
                # print("- ", rxwrapper.rxtype.is_type_name("mod"))

                # if wrapper is not a modifier, build a modifier. Otherwise, build mod-modifier.
                mod_type: str = ("mmod" if rxwrapper.rxtype.is_type_name("mod")
                                 else "mod")
                # print("- ", mod_type)
                # if mod_type (to make) is mod, then don't build an mmod
                # omit_types += ["mmod"] if mod_type == "mod" else []
                # print(">> ", omit_types)
                if mod_type not in self.omit_types:
                    modifier_node: RxNode = self.make_random_node(
                        mod_type, strict_typing=True)
                    node.set_modifier(modifier_node)
                # print("-- ", modifier)
            elif modifier:
                modifier_node = self.make_node(**modifier)
                node.set_modifier(modifier_node)

        return node

    def make_random_node(
        self,
        type_name: str = "re",
        is_child: bool = False,
        prob_modifier: float = P_MODIFIER,
        strict_typing: bool = False,
    ) -> RxNode:
        rxtype: RxType = self._rxtypes[type_name]

        # filter RxWrapper.wrappers with items that match rxtype
        filtered_wrappers: List[RxWrapper] = list(
            filter(
                lambda rxwrapper: rxwrapper.rxtype.is_type(
                    rxtype, strict=strict_typing),
                self._rxwrappers.all(),
            ))

        # filter out types specified for omission in node generation
        for omit in self.omit_types:
            omit_type: RxType = self._rxtypes[omit]
            filtered_wrappers = list(
                filter(
                    lambda rxwrapper: not rxwrapper.rxtype.is_type(omit_type),
                    filtered_wrappers,
                ))

        # filter out characters if is root node and suppression parameter specified
        if not is_child and SUPPRESS_ROOT_CHARS:
            filtered_wrappers = list(
                filter(
                    lambda rxwrapper: not rxwrapper.rxtype.is_type(
                        self._rxtypes["printable"]),
                    filtered_wrappers,
                ))

        # filter out wrappers specified for omission in node generation
        for omit in self.omit_wrappers:
            filtered_wrappers = list(
                filter(lambda rxwrapper: rxwrapper.name != omit,
                       filtered_wrappers))

        rxwrapper: RxWrapper = choice(filtered_wrappers)
        modifier: Optional[int] = None
        if rxwrapper.is_modifiable and random() < prob_modifier:
            modifier = RAND

        return self.make_node(
            rxwrapper=rxwrapper,
            modifier=modifier,
            is_child=is_child,
        )