I'm storing some configuration state in a dictionary, where a bunch of separate FFMPEG filters share a common set of parameters, and then we change/override those for each specific filter. My current code looks like this, and appears to work....
default['common']['fontfile'] = '/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf'
default['common']['rate'] = framerate
default['common']['fontcolor'] = 'yellow'
default['common']['box'] = 1
default['common']['y'] = 0 # x and y are offsets from the top left of frame
default['common']['x'] = 0
then I'm just calling another constructor with the ** operator to pull all those common keys/values into my new dict, as well as add some new k/v pairs and make some changes to the existing k/v pairs.
In this example I'm changing the values for 'x'
and 'y'
and adding 'start_number' : 0
to the dict:
default['framectr'] = {
**default['common'],
'y' : (vid_h * 0.2),
'x' : (vid_w * 0.2),
'start_number' : 0
}
So far this appears to work, but are there potential side effects here I should be worried about? I considered just making a deepcopy()
of the original/common dict and then adding/modifying the entries that way, but at least to my inexperienced eye the constructor call seems cleaner.
Thoughts?
Semantically,
default['framectr'] = {
**default['common'],
'y' : (vid_h * 0.2),
'x' : (vid_w * 0.2),
'start_number' : 0
}
is equivalent to
default['framectr'] = {
'fontfile': default['common']['fontfile'],
'rate': default['common']['rate'],
'fontcolor': default['common']['fontcolor'],
'box': default['common']['box'],
'y': default['common']['y'],
'x': default['common']['x'],
'y' : (vid_h * 0.2),
'x' : (vid_w * 0.2),
'start_number' : 0
}
Neither is safer or more dangerous than the other in terms of side effects. Given that you aren't using any mutable values in the definition of default['common']
, making a deep copy won't buy you anything.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.