Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clearer alternative to loading_fn #40

Closed
mivanit opened this issue Jul 24, 2023 · 4 comments · Fixed by #52
Closed

clearer alternative to loading_fn #40

mivanit opened this issue Jul 24, 2023 · 4 comments · Fixed by #52
Labels
enhancement New feature or request good first issue Good for newcomers sweep Assigns Sweep to an issue or pull request.

Comments

@mivanit
Copy link
Owner

mivanit commented Jul 24, 2023

serializeable_field() currently takes a loading_fn parameter, which, due to a dumb choice by me ages ago, takes directly the dict representing the whole object instead of the actual field we care about. this makes code a bit messy. It was originally to allow for extracting data from other fields in the object, but this is bad practice. Currently, the syntax looks like:

@serializable_dataclass
class MyClass(SerializableDataclass):
  some_field: int = serializable_field(
    serialization_fn = lambda x : str(x),
    loading_fn = lambda x : int(x["some_field"])
  )

Changing the behavior of the existing function would probably be a bad idea, so I think the best course of action is to create a new parameter, perhaps deserialization_fn, which allows us to do

deserialization_fn = lambda x : int(x)

This should be a change of just a few lines in muutils.json_serialize.serializable_dataclass

@mivanit mivanit changed the title alternative to loading_fn clearer alternative to loading_fn Jul 24, 2023
@mivanit mivanit added enhancement New feature or request good first issue Good for newcomers labels Jul 24, 2023
@mivanit mivanit added the sweep Assigns Sweep to an issue or pull request. label Aug 3, 2023
@sweep-ai

This comment was marked as outdated.

@kevinlu1248

This comment was marked as outdated.

@kevinlu1248

This comment was marked as outdated.

@kevinlu1248

This comment was marked as outdated.

@mivanit mivanit mentioned this issue Jun 21, 2024
mivanit added a commit that referenced this issue Jun 21, 2024
mivanit added a commit that referenced this issue Jun 21, 2024
- add support and CI checks for python `>=3.8` (Previously only `3.10, 3.11` were supported)
  - this mostly consisted of messing with a lot of type annotations and adding `from __future__ import annotations` in a number of places
- added `validate_type` function
  - make `SerializableDatclass` actually do type validation
- improvements to `SerializableDataclass` and related functions
  - much better docs and comments
  - move `SerializableField` and `serializable_field` to their own file
  - resolve #40 by adding `deserialize_fn` to `SerializableField`
  - moved `array_safe_eq` and `dc_eq` to `muutils.json_serialize.util`
  - added `array_b64_meta` mode to `muutils.json_serialize.array`, works pretty much the same as existing `array_hex_meta` but should take less space
- added `ErrorMode` enum 
  - special `.process()` method which takes a message (and optionally classes for warnings and errors, and an `except_from` parameter) and decides whether to throw warning, exception, or do nothing
  - switched error mode handling to this across the package
- switch to ruff for formatting and linting
- add cli interface to `muutils.sys_info`
- added `sanitize_name` function with `sanitize_fname` and `sanitize_identifier` as special cases
  - see `muutils.misc`
- big changes and improvements to CI/makefile
  - `uv` now used to install deps in CI, separate job for checking `poetry.lock` consistency
  - makefile recipes reworked, and major changes to internals. `make help` for more info
  - added many comments to the makefile
- many other small fixes and changes, including a large number of new tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers sweep Assigns Sweep to an issue or pull request.
Projects
None yet
2 participants