Skip to content

Commit 60a28b9

Browse files
committed
Graceful failure when CmapDict cannot find global vars
1 parent 92d3a39 commit 60a28b9

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

proplot/styletools.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,17 +1835,23 @@ def __init__(self, kwargs):
18351835
if not isinstance(key, str):
18361836
raise KeyError(f'Invalid key {key}. Must be string.')
18371837
self.__setitem__(key, value, sort=False)
1838-
for record in (cmaps, cycles):
1839-
record[:] = sorted(record)
1838+
try:
1839+
for record in (cmaps, cycles):
1840+
record[:] = sorted(record)
1841+
except NameError:
1842+
pass
18401843

18411844
def __delitem__(self, key):
18421845
"""Delete the item from the list records."""
18431846
super().__delitem__(self, key)
1844-
for record in (cmaps, cycles):
1845-
try:
1846-
record.remove(key)
1847-
except ValueError:
1848-
pass
1847+
try:
1848+
for record in (cmaps, cycles):
1849+
try:
1850+
record.remove(key)
1851+
except ValueError:
1852+
pass
1853+
except NameError:
1854+
pass
18491855

18501856
def __getitem__(self, key):
18511857
"""Retrieve the colormap associated with the sanitized key name. The
@@ -1904,10 +1910,13 @@ def __setitem__(self, key, item, sort=True):
19041910
'matplotlib.colors.LinearSegmentedColormap.'
19051911
)
19061912
key = self._sanitize_key(key, mirror=False)
1907-
record = cycles if isinstance(item, ListedColormap) else cmaps
1908-
record.append(key)
1909-
if sort:
1910-
record[:] = sorted(record)
1913+
try:
1914+
record = cycles if isinstance(item, ListedColormap) else cmaps
1915+
record.append(key)
1916+
if sort:
1917+
record[:] = sorted(record)
1918+
except NameError:
1919+
pass
19111920
return super().__setitem__(key, item)
19121921

19131922
def __contains__(self, item):
@@ -1950,11 +1959,14 @@ def get(self, key, *args):
19501959
def pop(self, key, *args):
19511960
"""Pop the sanitized colormap name."""
19521961
key = self._sanitize_key(key, mirror=True)
1953-
for record in (cmaps, cycles):
1954-
try:
1955-
record.remove(key)
1956-
except ValueError:
1957-
pass
1962+
try:
1963+
for record in (cmaps, cycles):
1964+
try:
1965+
record.remove(key)
1966+
except ValueError:
1967+
pass
1968+
except NameError:
1969+
pass
19581970
return super().pop(key, *args)
19591971

19601972
def update(self, *args, **kwargs):

0 commit comments

Comments
 (0)