Skip to content

Commit 96dc00a

Browse files
committed
update & and | op support and benchmark update
added more benchmark tests and updated readme.
1 parent f104d60 commit 96dc00a

8 files changed

Lines changed: 230 additions & 39 deletions

File tree

‎README.md‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Ubuntu 12.04 LTS, Core i7-2600 3.4GHz, 16GB DDR3, Python 2.7.3
3232

3333
### Writing bulk data
3434

35+
#### Fastest
36+
3537
```python
3638
from pyexcelerate import Workbook
3739

@@ -47,6 +49,8 @@ wb.save("output.xlsx")
4749

4850
PyExcelerate also permits you to write data to ranges directly, which is faster than writing cell-by-cell.
4951

52+
#### Fastest
53+
5054
```python
5155
from pyexcelerate import Workbook
5256

@@ -59,6 +63,24 @@ wb.save("output.xlsx")
5963

6064
### Writing cell data
6165

66+
#### Faster
67+
68+
```python
69+
from datetime import datetime
70+
from pyexcelerate import Workbook
71+
72+
wb = Workbook()
73+
ws = wb.new_sheet("sheet name")
74+
ws.set_cell_value(1, 1, 15) # a number
75+
ws.set_cell_value(1, 2, 20)
76+
ws.set_cell_value(1, 3, "=SUM(A1,B1)") # a formula
77+
ws.set_cell_value(1, 4, datetime.now()) # a date
78+
wb.save("output.xlsx")
79+
80+
```
81+
82+
#### Fast
83+
6284
```python
6385
from datetime import datetime
6486
from pyexcelerate import Workbook
@@ -100,7 +122,48 @@ wb.save("output.xlsx")
100122

101123
### Styling cells
102124

103-
Styling cells causes **non-negligible** overhead. It **will** increase your execution time (up to 10x longer!). Only style cells if absolutely necessary.
125+
Styling cells causes **non-negligible** overhead. It **will** increase your execution time (up to 10x longer if done improperly!). Only style cells if absolutely necessary.
126+
127+
#### Fastest
128+
129+
```python
130+
from pyexcelerate import Workbook, Color, Style, Font, Fill
131+
from datetime import datetime
132+
133+
wb = Workbook()
134+
ws = wb.new_sheet("sheet name")
135+
ws.set_cell_value(1, 1, 1)
136+
ws.set_cell_style(1, 1, Style(font=Font(bold=True)))
137+
ws.set_cell_style(1, 1, Style(font=Font(italic=True)))
138+
ws.set_cell_style(1, 1, Style(font=Font(underline=True)))
139+
ws.set_cell_style(1, 1, Style(font=Font(strikethrough=True)))
140+
ws.set_cell_style(1, 1, Style(fill=Fill(background=Color(255,0,0,0))))
141+
ws.set_cell_value(1, 2, datetime.now())
142+
ws.set_cell_style(1, 1, Style(format=Format('mm/dd/yy')))
143+
wb.save("output.xlsx")
144+
145+
```
146+
147+
#### Faster
148+
149+
```python
150+
from pyexcelerate import Workbook, Color
151+
from datetime import datetime
152+
153+
wb = Workbook()
154+
ws = wb.new_sheet("sheet name")
155+
ws.set_cell_value(1, 1, 1)
156+
ws.get_cell_style(1, 1).font.bold = True
157+
ws.get_cell_style(1, 1).font.italic = True
158+
ws.get_cell_style(1, 1).font.underline = True
159+
ws.get_cell_style(1, 1).font.strikethrough = True
160+
ws.get_cell_style(1, 1).fill.background = Color(0, 255, 0, 0)
161+
ws.set_cell_value(1, 2, datetime.now())
162+
ws.get_cell_style(1, 1).format.format = 'mm/dd/yy'
163+
wb.save("output.xlsx")
164+
165+
```
166+
#### Fast
104167

105168
```python
106169
from pyexcelerate import Workbook, Color

‎pyexcelerate/Fill.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from .Utility import Utility
12
from . import Color
23

34
class Fill(object):
4-
def __init__(self):
5-
self.background = Color.Color()
5+
def __init__(self, background=None):
6+
self.background = (Color.Color() if not background else background)
67

78
@property
89
def is_default(self):
@@ -23,6 +24,12 @@ def get_xml_string(self):
2324
else:
2425
return "<fill><patternFill patternType=\"solid\"><fgColor rgb=\"%s\"/></patternFill></fill>" % self.background.hex
2526

27+
def __or__(self, other):
28+
return Fill(background=Utility.nonboolean_or(self.background, other.background, Color.TRANSPARENT))
29+
30+
def __and__(self, other):
31+
return Fill(background=Utility.nonboolean_and(self.background, other.background, Color.TRANSPARENT))
32+
2633
def __str__(self):
2734
return "Fill: #%s" % self.background.hex
2835

‎pyexcelerate/Font.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . import six
2+
from .Utility import Utility
23

34
class Font(object):
45
def __init__(self, bold=False, italic=False, underline=False, strikethrough=False, family='Calibri', size=11):
@@ -26,6 +27,26 @@ def get_xml_string(self):
2627
def is_default(self):
2728
return self == Font()
2829

30+
def __or__(self, other):
31+
return Font( \
32+
bold = self.bold | other.bold, \
33+
italic = self.italic | other.italic, \
34+
underline = self.underline | other.underline, \
35+
strikethrough = self.strikethrough | other.strikethrough, \
36+
family = Utility.nonboolean_or(self.family, other.family, 'Calibri'), \
37+
size = Utility.nonboolean_or(self.size, other.size, 11) \
38+
)
39+
40+
def __and__(self, other):
41+
return Font( \
42+
bold = self.bold & other.bold, \
43+
italic = self.italic & other.italic, \
44+
underline = self.underline & other.underline, \
45+
strikethrough = self.strikethrough & other.strikethrough, \
46+
family = Utility.nonboolean_and(self.family, other.family, 'Calibri'), \
47+
size = Utility.nonboolean_and(self.size, other.size, 11) \
48+
)
49+
2950
def __eq__(self, other):
3051
if other is None:
3152
return self.is_default

‎pyexcelerate/Format.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . import six
2+
from .Utility import Utility
23

34
class Format(object):
45
def __init__(self, format=None):
@@ -11,6 +12,12 @@ def __eq__(self, other):
1112
else:
1213
return self.format == other.format
1314

15+
def __or__(self, other):
16+
return Format(format=Utility.nonboolean_or(self.format, other.format, None))
17+
18+
def __and__(self, other):
19+
return Format(format=Utility.nonboolean_and(self.format, other.format, None))
20+
1421
def __hash__(self):
1522
return hash(self.format)
1623

‎pyexcelerate/Style.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
from . import six
2-
from . import Font
3-
from . import Fill
4-
from . import Format
2+
from . import Font, Fill, Format
3+
from .Utility import Utility
54

65
class Style(object):
76
_DEFAULT_FORMAT = Format.Format()
87
_DEFAULT_FILL = Fill.Fill()
98
_DEFAULT_FONT = Font.Font()
10-
def __init__(self):
11-
self._font = None
12-
self._fill = None
13-
self._format = None
9+
def __init__(self, font=None, fill=None, format=None):
10+
self._font = font
11+
self._fill = fill
12+
self._format = format
1413

1514
@property
1615
def is_default(self):
@@ -62,6 +61,12 @@ def __eq__(self, other):
6261
else:
6362
return self._to_tuple() == other._to_tuple()
6463

64+
def __or__(self, other):
65+
return Style( \
66+
font=Utility.nonboolean_or(self._font, other._font, None), \
67+
fill=Utility.nonboolean_or(self._fill, other._fill, None), \
68+
format=Utility.nonboolean_or(self._format, other._format, None))
69+
6570
def _to_tuple(self):
6671
return (self._font, self._fill, self._format)
6772

‎pyexcelerate/Utility.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Utility(object):
2+
@staticmethod
3+
def nonboolean_or(left, right, default):
4+
if left == default:
5+
return right
6+
if right == default or left == right:
7+
return left
8+
return left | right # this scenario doesn't actually make sense, but it might be implemented
9+
10+
@staticmethod
11+
def nonboolean_and(left, right, default):
12+
if left == right:
13+
return left
14+
else:
15+
return default

0 commit comments

Comments
 (0)