I try to raise an error if the user enter a duplicate key in a dictionary. The dictionary is in a file and the user can edit the file manually.
Example:
dico= {'root':{
'a':{'some_key':'value',...},
'b':{'some_key':'value',...},
'c':{'some_key':'value',...},
...
'a':{'some_key':'value',...},
}
}
the new key 'a' already exist...
How can I test dico and warn the user when I load dico from the file?
Python's default behavior is to silently overwrite duplicates when declaring a dictionary.
You could create your own dictionary class that would check whether an item was already in a dictionary before adding new elements and then use this. But then you would have to change your declaration of
dico
in that file to something that allows duplicates, like a list of tuples for example.Then on loading that data file, you'd parse it into your special 'subclassed' dict.
If you want to ensure that an error is raised during
dict
construction with duplicate keys, just leverage Python's native keyword argument checking:Unless I'm missing something, there is no need to subclass
dict
.You will need to have custom dict which can reject with ValueError if the key is already present.
Here is how it works.
Write a subclass of dict, override __setitem__ such that it throws an error when replacing an existing key; rewrite the file to use your new subclass's constructor instead of the default dict built-ins.
then your file will have to be written as
using tuples instead of dicts for the file import (written using the {} notation, it would use the default dict constructor, and the duplicates would disappear before the Dict constructor ever gets them!).
WRONG WAY
GO BACK
from x import dico
is not a very good idea -- you are letting USERS edit code, which you then execute blindly. You run the risk of simple typos causing a syntax error, up to malicious stuff likeimport os; os.system("rm whatever"); dico = {}
.Don't faff about with subclassing
dict
. Write your own dict-of-dicts loader. It's not that hard ... read the data file, check before each insertion whether the key already exists; if it does, log an error message with meaningful stuff like the line number and the duplicate key and its value. At the end, if there have been any errors, raise an exception. You may find that there's an existing module to do all that ... the Python supplied ConfigParser aka configparser doesn't seem to be what you want.By the way, isn't having a single 'root' key at the top level rather pointless?