Skip to content

Commit af5aeb3

Browse files
committed
reused cached excel fonts to avoid max font bug w/ rec2excel
1 parent e85c93b commit af5aeb3

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/matplotlib/mlab.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,13 @@ def toval(self, x):
23502350
def fromstr(self, s):
23512351
return s
23522352

2353+
2354+
def __hash__(self):
2355+
"""
2356+
override the hash function of any of the formatters, so that we don't create duplicate excel format styles
2357+
"""
2358+
return hash(self.__class__)
2359+
23532360
class FormatString(FormatObj):
23542361
def tostr(self, x):
23552362
val = repr(x)
@@ -2378,6 +2385,9 @@ def __init__(self, precision=4, scale=1.):
23782385
self.precision = precision
23792386
self.scale = scale
23802387

2388+
def __hash__(self):
2389+
return hash((self.__class__, self.precision, self.scale))
2390+
23812391
def toval(self, x):
23822392
if x is not None:
23832393
x = x * self.scale
@@ -2425,6 +2435,10 @@ class FormatDate(FormatObj):
24252435
def __init__(self, fmt):
24262436
self.fmt = fmt
24272437

2438+
def __hash__(self):
2439+
return hash((self.__class__, self.fmt))
2440+
2441+
24282442
def toval(self, x):
24292443
if x is None: return 'None'
24302444
return x.strftime(self.fmt)

lib/mpl_toolkits/exceltools.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Some io tools for excel -- requires xlwt
3-
3+
l
44
Example usage:
55
66
import matplotlib.mlab as mlab
@@ -33,9 +33,16 @@ def xlformat_factory(format):
3333
copy the format, perform any overrides, and attach an xlstyle instance
3434
copied format is returned
3535
"""
36-
format = copy.deepcopy(format)
3736

37+
#if we have created an excel format already using this format,
38+
#don't recreate it; mlab.FormatObj override has to make objs with
39+
#the same props hash to the same value
40+
key = hash(format)
41+
fmt_ = xlformat_factory.created_formats.get(key)
42+
if fmt_ is not None:
43+
return fmt_
3844

45+
format = copy.deepcopy(format)
3946

4047
xlstyle = excel.XFStyle()
4148
if isinstance(format, mlab.FormatPercent):
@@ -55,8 +62,12 @@ def xlformat_factory(format):
5562

5663
format.xlstyle = xlstyle
5764

65+
xlformat_factory.created_formats[ key ] = format
66+
5867
return format
5968

69+
xlformat_factory.created_formats = {}
70+
6071
def rec2excel(r, ws, formatd=None, rownum=0, colnum=0, nanstr='NaN', infstr='Inf'):
6172
"""
6273
save record array r to excel xlwt worksheet ws

0 commit comments

Comments
 (0)