Skip to content

Commit cb5cf02

Browse files
committed
Fix bug where inherited not being passed; avoid classmethods
1 parent 0ce72f9 commit cb5cf02

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

pandas/formats/format.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,10 @@ class CSSToExcelConverter(object):
17791779
CSS declarations understood to be the containing scope for the
17801780
CSS processed by :meth:`__call__`.
17811781
"""
1782+
# NB: Most of the methods here could be classmethods, as only __init__
1783+
# and __call__ make use of instance attributes. We leave them as
1784+
# instancemethods so that users can easily experiment with extensions
1785+
# without monkey-patching.
17821786

17831787
def __init__(self, inherited=None):
17841788
if inherited is not None:
@@ -1799,7 +1803,7 @@ def __call__(self, declarations_str):
17991803
e.g. "font-weight: bold; background: blue"
18001804
"""
18011805
# TODO: memoize?
1802-
properties = self.compute_css(declarations_str)
1806+
properties = self.compute_css(declarations_str, self.inherited)
18031807
return self.build_xlstyle(properties)
18041808

18051809
def build_xlstyle(self, props):
@@ -2000,9 +2004,8 @@ def font_size_to_pt(self, val, em_pt=None):
20002004
val *= mul
20012005
return val
20022006

2003-
@classmethod
2004-
def compute_css(cls, declarations_str, inherited=None):
2005-
props = dict(cls.atomize(cls.parse(declarations_str)))
2007+
def compute_css(self, declarations_str, inherited=None):
2008+
props = dict(self.atomize(self.parse(declarations_str)))
20062009
if inherited is None:
20072010
inherited = {}
20082011

@@ -2032,12 +2035,11 @@ def compute_css(cls, declarations_str, inherited=None):
20322035
# 4. TODO: resolve other relative styles (e.g. ?)
20332036
return props
20342037

2035-
@classmethod
2036-
def atomize(cls, declarations):
2038+
def atomize(self, declarations):
20372039
for prop, value in declarations:
20382040
attr = 'expand_' + prop.replace('-', '_')
20392041
try:
2040-
expand = getattr(cls, attr)
2042+
expand = getattr(self, attr)
20412043
except AttributeError:
20422044
yield prop, value
20432045
else:
@@ -2053,16 +2055,15 @@ def atomize(cls, declarations):
20532055
DIRECTIONS = ('top', 'right', 'bottom', 'left')
20542056

20552057
def _direction_expander(prop_fmt):
2056-
@classmethod
2057-
def expand(cls, prop, value):
2058+
def expand(self, prop, value):
20582059
tokens = value.split()
20592060
try:
2060-
mapping = cls.DIRECTION_SHORTHANDS[len(tokens)]
2061+
mapping = self.DIRECTION_SHORTHANDS[len(tokens)]
20612062
except KeyError:
20622063
warnings.warn('Could not expand "%s: %s"' % (prop, value),
20632064
CSSParseWarning)
20642065
return
2065-
for key, idx in zip(cls.DIRECTIONS, mapping):
2066+
for key, idx in zip(self.DIRECTIONS, mapping):
20662067
yield prop_fmt % key, tokens[idx]
20672068

20682069
return expand
@@ -2073,8 +2074,7 @@ def expand(cls, prop, value):
20732074
expand_margin = _direction_expander('margin-%s')
20742075
expand_padding = _direction_expander('padding-%s')
20752076

2076-
@classmethod
2077-
def parse(cls, declarations_str):
2077+
def parse(self, declarations_str):
20782078
"""Generates (prop, value) pairs from declarations
20792079
20802080
In a future version may generate parsed tokens from tinycss/tinycss2

0 commit comments

Comments
 (0)