anymerge¶
Anymerge is a library for merging dataclasses, TypedDicts, and Pydantic models seamlessly with custom reducers.
Examples¶
import operator
from typing import Annotated
from anymerge import Reducer, merge
from pydantic import BaseModel
class State(BaseModel):
replace: str # (1)!
total: Annotated[int, Reducer(operator.add)] # (2)!
original_state = State(
replace="original",
total=0,
)
new_state = State(
replace="replace",
total=1,
)
final = merge(original, new_state)
print(final)
#> State(replace='replace', total=1)
- By default, the field value is replaced with the value from the new state.
- By annotating the field with a
Reducer
, you can specify a custom reducer function for the field. In this example, thetotal
field uses theoperator.add
function as its reducer. This means that when merging, the values of thetotal
field from both states will be added together instead of the default behavior of replacing the value.
Installation¶
License¶
anymerge
is distributed under the terms of the MIT ⧉ license.