Skip to content

Commit d0c6c9d

Browse files
committed
update object module docs
1 parent 5180acd commit d0c6c9d

File tree

22 files changed

+212
-158
lines changed

22 files changed

+212
-158
lines changed

pyformlang/objects/base_epsilon.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" General epsilon representation """
1+
"""General epsilon representation."""
22

33
from typing import Any
44

@@ -9,26 +9,28 @@
99

1010

1111
class BaseEpsilon(BaseTerminal):
12-
""" An epsilon transition
12+
"""An epsilon transition.
1313
1414
Examples
1515
--------
16-
1716
>>> epsilon = Epsilon()
18-
1917
"""
2018

2119
def __init__(self) -> None:
20+
"""Initializes the epsilon terminal."""
2221
super().__init__("epsilon")
2322

2423
def __eq__(self, other: Any) -> bool:
24+
"""Check if the epsilon is equal to the given object."""
2525
return isinstance(other, BaseEpsilon) \
2626
or not isinstance(other, FormalObject) and other in EPSILON_SYMBOLS
2727

2828
def __hash__(self) -> int:
29+
"""Gets the hash of the epsilon symbol."""
2930
return super().__hash__()
3031

3132
def __repr__(self) -> str:
33+
"""Gets the string representation of the epsilon symbol."""
3234
return "epsilon"
3335

3436
def _is_equal_to(self, other: FormalObject) -> bool:

pyformlang/objects/base_terminal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
""" General terminal representation """
1+
"""General terminal representation."""
22

33
from abc import abstractmethod
44

55
from .formal_object import FormalObject
66

77

88
class BaseTerminal(FormalObject):
9-
""" General terminal representation """
9+
"""General terminal representation."""
1010

1111
@abstractmethod
1212
def __repr__(self):
13+
"""Gets the string representation of the terminal."""
1314
raise NotImplementedError
1415

1516
def _is_equal_to(self, other: FormalObject) -> bool:
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
""" An object in a CFG (Variable and Terminal)"""
1+
"""An object in a CFG (Variable and Terminal)."""
22

33
from abc import abstractmethod
44

55
from ..formal_object import FormalObject
66

77

88
class CFGObject(FormalObject):
9-
""" An object in a CFG
9+
"""An object in a CFG.
1010
1111
Parameters
1212
-----------
13-
value : any
14-
The value of the object
13+
value:
14+
The value of the object.
1515
"""
1616

1717
@abstractmethod
1818
def to_text(self) -> str:
19-
""" Turns the object into a text format """
19+
"""Turns the object into a text format."""
2020
raise NotImplementedError
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
""" An epsilon terminal """
1+
"""An epsilon terminal in CFG."""
22

33
from .terminal import Terminal
44
from ..base_epsilon import BaseEpsilon
55

66

77
class Epsilon(BaseEpsilon, Terminal):
8-
""" An epsilon terminal """
8+
"""An epsilon terminal in CFG."""

pyformlang/objects/cfg_objects/production.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" A production or rule of a CFG """
1+
"""A production or rule of a CFG."""
22

33
from typing import List, Set, Any
44

@@ -9,14 +9,14 @@
99

1010

1111
class Production:
12-
""" A production or rule of a CFG
12+
"""A production or rule of a CFG.
1313
1414
Parameters
1515
----------
16-
head : :class:`~pyformlang.cfg.Variable`
17-
The head of the production
18-
body : iterable of :class:`~pyformlang.cfg.CFGObject`
19-
The body of the production
16+
head:
17+
The head of the production.
18+
body:
19+
The body of the production.
2020
"""
2121

2222
__slots__ = ["_body", "_head", "_hash"]
@@ -25,6 +25,7 @@ def __init__(self,
2525
head: Variable,
2626
body: List[CFGObject],
2727
filtering: bool = True) -> None:
28+
"""Initializes the production."""
2829
if filtering:
2930
self._body = [x for x in body if not isinstance(x, Epsilon)]
3031
else:
@@ -34,48 +35,48 @@ def __init__(self,
3435

3536
@property
3637
def head(self) -> Variable:
37-
"""Gets the head variable"""
38+
"""Gets the head variable of the production."""
3839
return self._head
3940

4041
@property
4142
def body(self) -> List[CFGObject]:
42-
"""Gets the body objects"""
43+
"""Gets the body objects of the production."""
4344
return self._body
4445

4546
@property
4647
def variables(self) -> Set[Variable]:
47-
"""Gets variables used in the production"""
48+
"""Gets variables used in the production."""
4849
return {self.head} | {object for object in self.body
4950
if isinstance(object, Variable)}
5051

5152
@property
5253
def terminals(self) -> Set[Terminal]:
53-
"""Gets terminals used in the production"""
54+
"""Gets terminals used in the production."""
5455
return {object for object in self.body
5556
if isinstance(object, Terminal) and object != Epsilon()}
5657

5758
def __eq__(self, other: Any) -> bool:
59+
"""Checks if current production is equal to the given object."""
5860
if not isinstance(other, Production):
5961
return False
6062
return self.head == other.head and self.body == other.body
6163

6264
def __hash__(self) -> int:
65+
"""Gets the hash of the production."""
6366
if self._hash is None:
6467
self._hash = sum(map(hash, self._body)) + hash(self._head)
6568
return self._hash
6669

6770
def __repr__(self) -> str:
71+
"""Gets the string representation of the production."""
6872
return str(self.head) + " -> " + " ".join([str(x) for x in self.body])
6973

7074
def is_normal_form(self) -> bool:
71-
"""
72-
Tells is the production is in Chomsky Normal Form
75+
"""Tells if the production is in Chomsky Normal Form.
7376
7477
Returns
7578
-------
76-
is_normal_form : bool
77-
If the production is in CNF
78-
79+
Whether the production is in CNF.
7980
"""
8081
if len(self._body) == 2:
8182
return (isinstance(self._body[0], Variable) and

pyformlang/objects/cfg_objects/terminal.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
""" A terminal in a CFG """
1+
"""A terminal in a CFG."""
22

33
from .cfg_object import CFGObject
44
from ..base_terminal import BaseTerminal
55

66

77
class Terminal(BaseTerminal, CFGObject):
8-
""" A terminal in a CFG """
8+
"""A terminal in a CFG.
9+
10+
Parameters
11+
----------
12+
value:
13+
A value of the terminal.
14+
"""
915

1016
def __repr__(self) -> str:
17+
"""Gets the string representation of the terminal."""
1118
return f"Terminal({self})"
1219

1320
def to_text(self) -> str:
21+
"""Gets a formatted string representing the terminal."""
1422
text = str(self._value)
1523
if text and text[0].isupper():
1624
return '"TER:' + text + '"'

pyformlang/objects/cfg_objects/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Utility for cfg object creation """
1+
"""Utility for CFG object creation."""
22

33
from typing import Hashable
44

@@ -8,14 +8,14 @@
88

99

1010
def to_variable(given: Hashable) -> Variable:
11-
""" Transformation into a variable """
11+
"""Transforms the given object into a variable."""
1212
if isinstance(given, Variable):
1313
return given
1414
return Variable(given)
1515

1616

1717
def to_terminal(given: Hashable) -> Terminal:
18-
""" Transformation into a terminal """
18+
"""Transforms the given object into a terminal."""
1919
if given == Epsilon():
2020
return Epsilon()
2121
if isinstance(given, Terminal):

pyformlang/objects/cfg_objects/variable.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" A variable in a CFG """
1+
"""A variable in a CFG."""
22

33
from string import ascii_uppercase
44

@@ -7,18 +7,20 @@
77

88

99
class Variable(CFGObject):
10-
""" An variable in a CFG
10+
"""An variable in a CFG.
1111
1212
Parameters
1313
-----------
14-
value : any
15-
The value of the variable
14+
value:
15+
The value of the variable.
1616
"""
1717

1818
def __repr__(self) -> str:
19+
"""Gets the string representation of the variable."""
1920
return f"Variable({self})"
2021

2122
def to_text(self) -> str:
23+
"""Gets a formatted string representing the variable."""
2224
text = str(self._value)
2325
if text and text[0] not in ascii_uppercase:
2426
return '"VAR:' + text + '"'
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
"""
2-
Represents an epsilon transition
3-
"""
1+
"""An epsilon symbol in finite automaton."""
42

53
from .symbol import Symbol
64
from ..base_epsilon import BaseEpsilon
75

86

97
class Epsilon(BaseEpsilon, Symbol):
10-
""" An epsilon transition
8+
"""An epsilon symbol in finite automaton.
119
1210
Examples
1311
--------
14-
1512
>>> epsilon = Epsilon()
16-
1713
"""
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
"""
2-
Represents an object of a finite state automaton
3-
"""
1+
"""Representation of an object of a finite state automaton."""
42

53
from abc import abstractmethod
64

75
from ..formal_object import FormalObject
86

97

108
class FiniteAutomatonObject(FormalObject):
11-
""" Represents an object in a finite state automaton
9+
"""Representation of an object of a finite state automaton.
1210
1311
Parameters
1412
----------
15-
value: any
16-
The value of the object
13+
value:
14+
The value of the object.
1715
"""
1816

1917
@abstractmethod
2018
def __repr__(self) -> str:
19+
"""Gets a string representation of the object."""
2120
raise NotImplementedError

0 commit comments

Comments
 (0)