Skip to content

Commit 6f9d171

Browse files
committed
Add ability to specify the tree for uproot requests
1 parent 87735de commit 6f9d171

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

servicex/func_adl/func_adl_dataset.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
Iterable,
4242
Dict,
4343
)
44-
from pydantic import typing
4544
from qastle import python_ast_to_text_ast
4645

4746
from func_adl import EventDataset
@@ -99,29 +98,25 @@ def __init__(
9998
def set_provided_qastle(self, qastle: str):
10099
self.provided_qastle = qastle
101100

102-
def clone_with_new_ast(self, new_ast: ast.AST, new_type: typing.Any):
101+
def __deepcopy__(self, memo):
103102
"""
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
113107
"""
114-
clone = copy.copy(self)
108+
cls = self.__class__
109+
obj = cls.__new__(cls)
110+
111+
memo[id(self)] = obj
112+
115113
for attr, value in vars(self).items():
116114
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)
120116
else:
121-
setattr(clone, attr, copy.deepcopy(value))
117+
setattr(obj, attr, copy.deepcopy(value, memo))
122118

123-
clone._item_type = new_type
124-
return clone
119+
return obj
125120

126121
def SelectMany(
127122
self, func: Union[str, ast.Lambda, Callable[[T], Iterable[S]]]
@@ -216,6 +211,26 @@ def QMetaData(self, metadata: Dict[str, Any]) -> FuncADLDataset[T]:
216211
"""
217212
return super().QMetaData(metadata)
218213

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+
219234
def generate_qastle(self, a: ast.AST) -> str:
220235
r"""Generate the qastle from the ast of the query.
221236

0 commit comments

Comments
 (0)