Skip to content

Commit

Permalink
feat: Content.form_with_key_path()
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Nov 20, 2024
1 parent 4db7973 commit 0dd0e73
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/awkward/contents/bitmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> BitMaskedFo
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> BitMaskedForm:
return self.form_cls(
self._mask.form,
self._content._form_with_key_path((*path, None)),
self._valid_when,
self._lsb_order,
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
9 changes: 9 additions & 0 deletions src/awkward/contents/bytemaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> ByteMaskedF
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> ByteMaskedForm:
return self.form_cls(
self._mask.form,
self._content._form_with_key_path((*path, None)),
self._valid_when,
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
6 changes: 6 additions & 0 deletions src/awkward/contents/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ def _form_with_key(
) -> Form:
raise NotImplementedError

def form_with_key_path(self, root: (str | None) = ()) -> Form:
return self._form_with_key_path(root)

def _form_with_key_path(self, path: (str | None)) -> Form:
raise NotImplementedError

@property
def form_cls(self) -> type[Form]:
raise NotImplementedError
Expand Down
3 changes: 3 additions & 0 deletions src/awkward/contents/emptyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def simplified(cls, *, parameters=None, backend=None):
def _form_with_key(self, getkey: Callable[[Content], str | None]) -> EmptyForm:
return self.form_cls(form_key=getkey(self))

def _form_with_key_path(self, path: (str | None)) -> EmptyForm:
return self.form_cls(form_key=repr(path))

def _to_buffers(
self,
form: Form,
Expand Down
8 changes: 8 additions & 0 deletions src/awkward/contents/indexedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> IndexedForm
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> IndexedForm:
return self.form_cls(
self._index.form,
self._content._form_with_key_path((*path, None)),
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
8 changes: 8 additions & 0 deletions src/awkward/contents/indexedoptionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ def _form_with_key(
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> IndexedOptionForm:
return self.form_cls(
self._index.form,
self._content._form_with_key_path((*path, None)),
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
9 changes: 9 additions & 0 deletions src/awkward/contents/listarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> ListForm:
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> ListForm:
return self.form_cls(
self._starts.form,
self._stops.form,
self._content._form_with_key_path((*path, None)),
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
8 changes: 8 additions & 0 deletions src/awkward/contents/listoffsetarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> ListOffsetF
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> ListOffsetForm:
return self.form_cls(
self._offsets.form,
self._content._form_with_key_path((*path, None)),
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
8 changes: 8 additions & 0 deletions src/awkward/contents/numpyarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> NumpyForm:
form_key=getkey(self),
)

def _form_with_key_path(self, path: (str | None)) -> NumpyForm:
return self.form_cls(
ak.types.numpytype.dtype_to_primitive(self._data.dtype),
self.inner_shape,
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
18 changes: 18 additions & 0 deletions src/awkward/contents/recordarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,24 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> RecordForm:
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> RecordForm:
if self._fields is None:
contents = [
x._form_with_key_path((*path, i)) for i, x in enumerate(self._contents)
]
else:
contents = [
x._form_with_key_path((*path, k))
for k, x in zip(self._fields, self._contents)
]

return self.form_cls(
contents,
self._fields,
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
8 changes: 8 additions & 0 deletions src/awkward/contents/regulararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> RegularForm
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> RegularForm:
return self.form_cls(
self._content._form_with_key_path((*path, None)),
self._size,
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
9 changes: 9 additions & 0 deletions src/awkward/contents/unionarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> UnionForm:
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> UnionForm:
return self.form_cls(
self._tags.form,
self._index.form,
[x._form_with_key_path((*path, i)) for i, x in enumerate(self._contents)],
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down
7 changes: 7 additions & 0 deletions src/awkward/contents/unmaskedarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ def _form_with_key(self, getkey: Callable[[Content], str | None]) -> UnmaskedFor
form_key=form_key,
)

def _form_with_key_path(self, path: (str | None)) -> UnmaskedForm:
return self.form_cls(
self._content._form_with_key_path((*path, None)),
parameters=self._parameters,
form_key=repr(path),
)

def _to_buffers(
self,
form: Form,
Expand Down

0 comments on commit 0dd0e73

Please sign in to comment.