Comments (1)
Hi, some time ago I had the same problem. I came up with a way to solve it, maybe not exactly right, but hope it helps.
Here's the code:
# Original code
def _handle_merge_additive(destination, source, key):
# Values are combined into one long collection.
if isinstance(destination[key], list) and isinstance(source[key], list):
# Extend destination if both destination and source are `list` type.
destination[key].extend(deepcopy(source[key]))
# and so on ...
# Changed code
def _handle_merge_additive(destination, source, key):
# Values are combined into one long collection.
if isinstance(destination[key], list) and isinstance(source[key], list):
# First, need to consider what type of elements are in `dst`
# If there is no element in `dst`, or the elements in `dst` are str, int and float type,
# then replace `dst` with `src`
if len(destination[key]) == 0 or isinstance(destination[key][0], (int, float, str)):
destination[key] = deepcopy(source[key])
# If the number of elements in `dst` is less than `src`,
# then traverse `dst` according to the number of elements in `dst`
elif len(destination[key]) <= len(source[key]):
for i in range(len(destination[key])):
_deepmerge(destination[key][i], source[key][i], Strategy.ADDITIVE)
# If the number of elements in `dst` is more than `src`,
# then append empty dict in `src`, until the number of elements in `src` is the same as `dst`
else:
source[key].extend([{} for x in range(len(destination[key]) - len(source[key]))])
for i in range(len(destination[key])):
_deepmerge(destination[key][i], source[key][i], Strategy.ADDITIVE)
# and so on ...
And it works for me!
Note: this modification will change the way original mergedeep handles list with int, str, float or sth not dict in it. Original mergedeep extends the list content, but changed mergedeep replaces the list content.
from mergedeep.
Related Issues (9)
- The `typesafe` merge strategy should support both replace and additive.
- Add py.typed file so types can be used for users of mergedeep HOT 5
- 1.3.4: sphinx 4.x warnings
- Additive merge for dict with list values?
- Issues with deepcopy HOT 1
- Types are not used after installation of the package HOT 1
- Credit HOT 1
- 1.3.4: `recommonmark` is deprecated
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mergedeep.