MCPcopy Create free account
hub / github.com/pytorch/executorch / _ResourceFiles

Class _ResourceFiles

exir/_serialize/_flatbuffer.py:123–151  ·  view source on GitHub ↗

Manages a collection of python resources that will be written to files.

Source from the content-addressed store, hash-verified

121
122
123class _ResourceFiles:
124 """Manages a collection of python resources that will be written to files."""
125
126 def __init__(self, resource_names: Sequence[str]) -> None:
127 """Load the resources with the provided names."""
128 # Map each name to its contents.
129 self._files: Dict[str, bytes] = {}
130 for name in resource_names:
131 self._files[name] = importlib.resources.read_binary(__package__, name)
132
133 def patch_files(self, patch_fn: Callable[[bytes], bytes]) -> None:
134 """Uses the provided patching function to update the contents of all
135 files. `patch_fn` takes the current contents of a file as input and
136 returns the new contents.
137 """
138 for name in self._files.keys():
139 self._files[name] = patch_fn(self._files[name])
140
141 def get(self, name: str) -> bytes:
142 """Returns the current contents of the named file."""
143 return self._files[name]
144
145 def write_to(self, out_dir: str) -> None:
146 """Writes the files to the specified directory. File names are based on
147 the original resource names.
148 """
149 for name, data in self._files.items():
150 with open(os.path.join(out_dir, name), "wb") as fp:
151 fp.write(data)
152
153
154@dataclass

Callers 2

make_resource_filesMethod · 0.90
_prepare_schemaFunction · 0.85

Calls

no outgoing calls

Tested by 1

make_resource_filesMethod · 0.72