-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flax_field and tree_map_with_path
- Loading branch information
1 parent
a29eaa4
commit 0ee0ae8
Showing
8 changed files
with
42 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import field | ||
from typing import Any | ||
|
||
__all__ = ['flax_field'] | ||
|
||
|
||
def flax_field() -> Any: | ||
return field(init=False, default=None, kw_only=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from typing import Any, TypeVar | ||
|
||
__all__ = ['tree_map_with_path'] | ||
|
||
|
||
T = TypeVar('T') | ||
def tree_map_with_path(structure: Any, | ||
transform: Callable[[T, tuple[str, ...]], T] | ||
) -> Any: | ||
def f(structure: Any, | ||
transform: Callable[[T, tuple[str, ...]], T], | ||
path: tuple[str, ...] | ||
) -> Any: | ||
if isinstance(structure, dict): | ||
out_structure = {} | ||
for key, value in structure.items(): | ||
out_structure[key] = f(value, transform, (*path, key)) | ||
return out_structure | ||
return transform(structure, path) | ||
return f(structure, transform, ()) |