Add SimpleFrozenDict util to use as default function argument

This commit is contained in:
ines 2018-05-20 15:13:37 +02:00
parent 581d318971
commit 5768df4f09
2 changed files with 17 additions and 0 deletions

View File

@ -245,6 +245,8 @@ class Errors(object):
"the meta.json. Vector names are required to avoid issue #1660.") "the meta.json. Vector names are required to avoid issue #1660.")
E093 = ("token.ent_iob values make invalid sequence: I without B\n{seq}") E093 = ("token.ent_iob values make invalid sequence: I without B\n{seq}")
E094 = ("Error reading line {line_num} in vectors file {loc}.") E094 = ("Error reading line {line_num} in vectors file {loc}.")
E095 = ("Can't write to frozen dictionary. This is likely an internal "
"error. Are you writing to a default function argument?")
@add_codes @add_codes

View File

@ -606,3 +606,18 @@ def use_gpu(gpu_id):
def fix_random_seed(seed=0): def fix_random_seed(seed=0):
random.seed(seed) random.seed(seed)
numpy.random.seed(seed) numpy.random.seed(seed)
class SimpleFrozenDict(dict):
"""Simplified implementation of a frozen dict, mainly used as default
function or method argument (for arguments that should default to empty
dictionary). Will raise an error if user or spaCy attempts to add to dict.
"""
def __setitem__(self, key, value):
raise NotImplementedError(Errors.E095)
def pop(self, key, default=None):
raise NotImplementedError(Errors.E095)
def update(self, other):
raise NotImplementedError(Errors.E095)