pbi-parsers 0.7.21__py3-none-any.whl → 0.8.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pbi_parsers/__init__.py CHANGED
@@ -1,6 +1,6 @@
1
1
  from . import dax, pq
2
2
 
3
- __version__ = "0.7.21"
3
+ __version__ = "0.8.0"
4
4
 
5
5
 
6
6
  __all__ = [
@@ -1,4 +1,5 @@
1
- from typing import TYPE_CHECKING, Any
1
+ from collections.abc import Callable
2
+ from typing import TYPE_CHECKING, Any, TypeVar
2
3
 
3
4
  from pbi_parsers.dax.tokens import TokenType
4
5
 
@@ -6,6 +7,9 @@ if TYPE_CHECKING:
6
7
  from pbi_parsers.dax.parser import Parser
7
8
 
8
9
 
10
+ T = TypeVar("T", bound="Expression")
11
+
12
+
9
13
  class Expression:
10
14
  pre_comments: list[Any] = []
11
15
  post_comments: list[Any] = []
@@ -23,6 +27,37 @@ class Expression:
23
27
  msg = "This method should be implemented by subclasses."
24
28
  raise NotImplementedError(msg)
25
29
 
30
+ def find(self, cls_type: type[T] | tuple[type[T], ...], attributes: Callable[[T], bool] | None = None) -> T:
31
+ attributes = attributes or (lambda _x: True)
32
+
33
+ if isinstance(self, cls_type) and attributes(self):
34
+ return self
35
+
36
+ for child in self.children():
37
+ try:
38
+ return child.find(cls_type, attributes)
39
+ except ValueError:
40
+ continue
41
+
42
+ msg = f"Matching {cls_type} not found in expression tree."
43
+ raise ValueError(msg)
44
+
45
+ def find_all(
46
+ self,
47
+ cls_type: type[T] | tuple[type[T], ...],
48
+ attributes: Callable[[T], bool] | None = None,
49
+ ) -> list[T]:
50
+ attributes = attributes or (lambda _x: True)
51
+ results: list[T] = []
52
+
53
+ if isinstance(self, cls_type) and attributes(self):
54
+ results.append(self)
55
+
56
+ for child in self.children():
57
+ results.extend(child.find_all(cls_type, attributes))
58
+
59
+ return results
60
+
26
61
  @classmethod
27
62
  def match(cls, parser: "Parser") -> "Expression | None":
28
63
  """Attempt to match the current tokens to this expression type.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pbi_parsers
3
- Version: 0.7.21
3
+ Version: 0.8.0
4
4
  Summary: Power BI lexer, parsers, and formatters for DAX and M (Power Query) languages
5
5
  Project-URL: Homepage, https://github.com/douglassimonsen/pbi_parsers
6
6
  Project-URL: Documentation, https://douglassimonsen.github.io/pbi_parsers/
@@ -1,4 +1,4 @@
1
- pbi_parsers/__init__.py,sha256=XYRPT6QSy184iWUMvcvZjM8YxFQb7VBbETk4M3-OF3M,92
1
+ pbi_parsers/__init__.py,sha256=cFx9LLUu1MM9UFcbhuA1oauYK8JiPDwU0ohEoJnrI_I,91
2
2
  pbi_parsers/base/__init__.py,sha256=U7QpzFFD9A4wK3ZHin6xg5fPgTca0y5KC-O7nrZ-flM,115
3
3
  pbi_parsers/base/lexer.py,sha256=5iOkdYzJ9wGwz7I4rDlc7slrRNUPzi0oFFkDxN1d62M,4180
4
4
  pbi_parsers/base/tokens.py,sha256=slIVl4673xXomqHMgrn1ApHs8YRvcC2yQgs1zfSpe1U,2220
@@ -10,7 +10,7 @@ pbi_parsers/dax/parser.py,sha256=QLKrIBcxZ26TGhTHpeKcTGEHEHUDLC6IgpxxrdJzdek,182
10
10
  pbi_parsers/dax/tokens.py,sha256=nY1laCbL8vwALpJ4jcd8k4lAscqOwqdw3dFuj4_KKVk,1234
11
11
  pbi_parsers/dax/utils.py,sha256=OURPa-b6Ldn0_KKXPdLIPA3Zdc12OfbbFd2X5SocCek,4402
12
12
  pbi_parsers/dax/exprs/__init__.py,sha256=OUfiXzZYp5HkTPE9x595MMxpsgG1IvsED8p8spAKZuk,3432
13
- pbi_parsers/dax/exprs/_base.py,sha256=KIlP2AxtZVJGy49ANZfotYnBKt9-EDKOuTx2k48k9cc,1634
13
+ pbi_parsers/dax/exprs/_base.py,sha256=bMHLICgAUOqAKl_S9d6V8kk62cqM1jynUY2-gBJlEcs,2732
14
14
  pbi_parsers/dax/exprs/_utils.py,sha256=BxxRCtsqpL9t330ZfBnkm1lq7B_ejAoG89Ot2bLYJig,2123
15
15
  pbi_parsers/dax/exprs/add_sub.py,sha256=O-1DGXr4F4WWG1PyL1xra_pFR0ZxFo-_y-e6SioHrQc,2241
16
16
  pbi_parsers/dax/exprs/add_sub_unary.py,sha256=OhLF_AFxatvbI7X_Tj3_wQ0gjsnmuwkMkmcCzsECsK4,2003
@@ -73,7 +73,7 @@ pbi_parsers/pq/exprs/statement.py,sha256=JSg48pGAU3Ka2pt4lzVsYlVOqGeF_ARGm8Ajf0l
73
73
  pbi_parsers/pq/exprs/try_expr.py,sha256=UcnqfA-t9S1LVrKqeNUT8n4JJcO-ZQZoJrxAdjJ-GMA,1692
74
74
  pbi_parsers/pq/exprs/type_expr.py,sha256=hH5ubrIJaxwQsopNJHUZ4ByS1rHEgv2Tf8ocYqSukXM,2570
75
75
  pbi_parsers/pq/exprs/variable.py,sha256=wp4t0QHIGA264sXnWp7XVe1H8MJzMIOaoLNBQe-dfNk,1602
76
- pbi_parsers-0.7.21.dist-info/METADATA,sha256=EbXAfjLz-llL3r1V_cmYmX2aLZ-3Djh77b2Dsa96v50,2907
77
- pbi_parsers-0.7.21.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
78
- pbi_parsers-0.7.21.dist-info/licenses/LICENSE,sha256=Sn0IfXOE4B0iL9lZXmGmRuTGyJeCtefxcfws0bLjp2g,1072
79
- pbi_parsers-0.7.21.dist-info/RECORD,,
76
+ pbi_parsers-0.8.0.dist-info/METADATA,sha256=o3D15LUYr8IlrK-TNjptPgLFlSvwHF7BqmkXfOPFJtA,2906
77
+ pbi_parsers-0.8.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
78
+ pbi_parsers-0.8.0.dist-info/licenses/LICENSE,sha256=Sn0IfXOE4B0iL9lZXmGmRuTGyJeCtefxcfws0bLjp2g,1072
79
+ pbi_parsers-0.8.0.dist-info/RECORD,,