|
41 | 41 | Iterable, |
42 | 42 | Dict, |
43 | 43 | ) |
44 | | -from pydantic import typing |
45 | 44 | from qastle import python_ast_to_text_ast |
46 | 45 |
|
47 | 46 | from func_adl import EventDataset |
@@ -99,29 +98,25 @@ def __init__( |
99 | 98 | def set_provided_qastle(self, qastle: str): |
100 | 99 | self.provided_qastle = qastle |
101 | 100 |
|
102 | | - def clone_with_new_ast(self, new_ast: ast.AST, new_type: typing.Any): |
| 101 | + def __deepcopy__(self, memo): |
103 | 102 | """ |
104 | | - Override the method from ObjectStream - We need to be careful because the query |
105 | | - cache is a tinyDB database that holds an open file pointer. We are not allowed |
106 | | - to clone an open file handle, so for this property we will copy by reference |
107 | | - and share it between the clones. Turns out ast class is also picky about copies, |
108 | | - so we set that explicitly. |
109 | | -
|
110 | | - :param new_ast: |
111 | | - :param new_type: |
112 | | - :return: |
| 103 | + Customize deepcopy behavior for this class. |
| 104 | + We need to be careful because the query cache is a tinyDB database that holds an |
| 105 | + open file pointer. We are not allowed to clone an open file handle, so for this |
| 106 | + property we will copy by reference and share it between the clones |
113 | 107 | """ |
114 | | - clone = copy.copy(self) |
| 108 | + cls = self.__class__ |
| 109 | + obj = cls.__new__(cls) |
| 110 | + |
| 111 | + memo[id(self)] = obj |
| 112 | + |
115 | 113 | for attr, value in vars(self).items(): |
116 | 114 | if type(value) == QueryCache: |
117 | | - setattr(clone, attr, value) |
118 | | - elif attr == "_q_ast": |
119 | | - setattr(clone, attr, new_ast) |
| 115 | + setattr(obj, attr, value) |
120 | 116 | else: |
121 | | - setattr(clone, attr, copy.deepcopy(value)) |
| 117 | + setattr(obj, attr, copy.deepcopy(value, memo)) |
122 | 118 |
|
123 | | - clone._item_type = new_type |
124 | | - return clone |
| 119 | + return obj |
125 | 120 |
|
126 | 121 | def SelectMany( |
127 | 122 | self, func: Union[str, ast.Lambda, Callable[[T], Iterable[S]]] |
@@ -216,6 +211,26 @@ def QMetaData(self, metadata: Dict[str, Any]) -> FuncADLDataset[T]: |
216 | 211 | """ |
217 | 212 | return super().QMetaData(metadata) |
218 | 213 |
|
| 214 | + def set_tree(self, tree_name: str) -> FuncADLDataset[T]: |
| 215 | + r"""Set the tree name for the query. |
| 216 | + Args: |
| 217 | + tree_name (str): Name of the tree to use for the query |
| 218 | +
|
| 219 | + Returns: |
| 220 | + The Dataset with the tree appended to the first call object |
| 221 | + """ |
| 222 | + ast_clone = copy.copy(self.query_ast) |
| 223 | + for attr, value in vars(self.query_ast).items(): |
| 224 | + if type(value) == QueryCache or type(value) == FuncADLDataset: |
| 225 | + setattr(ast_clone, attr, value) |
| 226 | + else: |
| 227 | + setattr(ast_clone, attr, copy.deepcopy(value)) |
| 228 | + |
| 229 | + clone = self.clone_with_new_ast(ast_clone, self._item_type) |
| 230 | + clone._q_ast.args[0].args.append(ast.Str(s="bogus.root")) |
| 231 | + clone._q_ast.args[0].args.append(ast.Str(s=tree_name)) |
| 232 | + return clone |
| 233 | + |
219 | 234 | def generate_qastle(self, a: ast.AST) -> str: |
220 | 235 | r"""Generate the qastle from the ast of the query. |
221 | 236 |
|
|
0 commit comments