Issue #18457: Fixed saving of formulas and complex numbers in Tools/demo/ss1.py.
Useed context managers for file I/O. Removed out-of-dated code and misleading comments.
This commit is contained in:
parent
b5c9dfdab3
commit
ec7ddd9028
|
@ -229,6 +229,9 @@ Documentation
|
||||||
Tools/Demos
|
Tools/Demos
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
- Issue #18457: Fixed saving of formulas and complex numbers in
|
||||||
|
Tools/demo/ss1.py.
|
||||||
|
|
||||||
- Issue #18449: Make Tools/demo/ss1.py work again on Python 3. Patch by
|
- Issue #18449: Make Tools/demo/ss1.py work again on Python 3. Patch by
|
||||||
Févry Thibault.
|
Févry Thibault.
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ SS1 -- a spreadsheet-like application.
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import html
|
|
||||||
from xml.parsers import expat
|
from xml.parsers import expat
|
||||||
|
from xml.sax.saxutils import escape
|
||||||
|
|
||||||
LEFT, CENTER, RIGHT = "LEFT", "CENTER", "RIGHT"
|
LEFT, CENTER, RIGHT = "LEFT", "CENTER", "RIGHT"
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ class Sheet:
|
||||||
if hasattr(cell, 'xml'):
|
if hasattr(cell, 'xml'):
|
||||||
cellxml = cell.xml()
|
cellxml = cell.xml()
|
||||||
else:
|
else:
|
||||||
cellxml = '<value>%s</value>' % html.escape(cell)
|
cellxml = '<value>%s</value>' % escape(cell)
|
||||||
out.append('<cell row="%s" col="%s">\n %s\n</cell>' %
|
out.append('<cell row="%s" col="%s">\n %s\n</cell>' %
|
||||||
(y, x, cellxml))
|
(y, x, cellxml))
|
||||||
out.append('</spreadsheet>')
|
out.append('</spreadsheet>')
|
||||||
|
@ -213,16 +213,14 @@ class Sheet:
|
||||||
|
|
||||||
def save(self, filename):
|
def save(self, filename):
|
||||||
text = self.xml()
|
text = self.xml()
|
||||||
f = open(filename, "w")
|
with open(filename, "w", encoding='utf-8') as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
if text and not text.endswith('\n'):
|
if text and not text.endswith('\n'):
|
||||||
f.write('\n')
|
f.write('\n')
|
||||||
f.close()
|
|
||||||
|
|
||||||
def load(self, filename):
|
def load(self, filename):
|
||||||
f = open(filename, 'rb')
|
with open(filename, 'rb') as f:
|
||||||
SheetParser(self).parsefile(f)
|
SheetParser(self).parsefile(f)
|
||||||
f.close()
|
|
||||||
|
|
||||||
class SheetParser:
|
class SheetParser:
|
||||||
|
|
||||||
|
@ -239,13 +237,10 @@ class SheetParser:
|
||||||
def startelement(self, tag, attrs):
|
def startelement(self, tag, attrs):
|
||||||
method = getattr(self, 'start_'+tag, None)
|
method = getattr(self, 'start_'+tag, None)
|
||||||
if method:
|
if method:
|
||||||
for key, value in attrs.items():
|
|
||||||
attrs[key] = str(value) # XXX Convert Unicode to 8-bit
|
|
||||||
method(attrs)
|
method(attrs)
|
||||||
self.texts = []
|
self.texts = []
|
||||||
|
|
||||||
def data(self, text):
|
def data(self, text):
|
||||||
text = str(text) # XXX Convert Unicode to 8-bit
|
|
||||||
self.texts.append(text)
|
self.texts.append(text)
|
||||||
|
|
||||||
def endelement(self, tag):
|
def endelement(self, tag):
|
||||||
|
@ -269,11 +264,7 @@ class SheetParser:
|
||||||
except:
|
except:
|
||||||
self.value = None
|
self.value = None
|
||||||
|
|
||||||
def end_long(self, text):
|
end_long = end_int
|
||||||
try:
|
|
||||||
self.value = int(text)
|
|
||||||
except:
|
|
||||||
self.value = None
|
|
||||||
|
|
||||||
def end_double(self, text):
|
def end_double(self, text):
|
||||||
try:
|
try:
|
||||||
|
@ -288,10 +279,7 @@ class SheetParser:
|
||||||
self.value = None
|
self.value = None
|
||||||
|
|
||||||
def end_string(self, text):
|
def end_string(self, text):
|
||||||
try:
|
self.value = text
|
||||||
self.value = text
|
|
||||||
except:
|
|
||||||
self.value = None
|
|
||||||
|
|
||||||
def end_value(self, text):
|
def end_value(self, text):
|
||||||
if isinstance(self.value, BaseCell):
|
if isinstance(self.value, BaseCell):
|
||||||
|
@ -328,7 +316,7 @@ class BaseCell:
|
||||||
class NumericCell(BaseCell):
|
class NumericCell(BaseCell):
|
||||||
|
|
||||||
def __init__(self, value, fmt="%s", alignment=RIGHT):
|
def __init__(self, value, fmt="%s", alignment=RIGHT):
|
||||||
assert isinstance(value, (int, int, float, complex))
|
assert isinstance(value, (int, float, complex))
|
||||||
assert alignment in (LEFT, CENTER, RIGHT)
|
assert alignment in (LEFT, CENTER, RIGHT)
|
||||||
self.value = value
|
self.value = value
|
||||||
self.fmt = fmt
|
self.fmt = fmt
|
||||||
|
@ -355,21 +343,18 @@ class NumericCell(BaseCell):
|
||||||
if -2**31 <= self.value < 2**31:
|
if -2**31 <= self.value < 2**31:
|
||||||
return '<int>%s</int>' % self.value
|
return '<int>%s</int>' % self.value
|
||||||
else:
|
else:
|
||||||
return self._xml_long()
|
return '<long>%s</long>' % self.value
|
||||||
|
|
||||||
def _xml_long(self):
|
|
||||||
return '<long>%s</long>' % self.value
|
|
||||||
|
|
||||||
def _xml_float(self):
|
def _xml_float(self):
|
||||||
return '<double>%s</double>' % repr(self.value)
|
return '<double>%r</double>' % self.value
|
||||||
|
|
||||||
def _xml_complex(self):
|
def _xml_complex(self):
|
||||||
return '<complex>%s</double>' % repr(self.value)
|
return '<complex>%r</complex>' % self.value
|
||||||
|
|
||||||
class StringCell(BaseCell):
|
class StringCell(BaseCell):
|
||||||
|
|
||||||
def __init__(self, text, fmt="%s", alignment=LEFT):
|
def __init__(self, text, fmt="%s", alignment=LEFT):
|
||||||
assert isinstance(text, (str, str))
|
assert isinstance(text, str)
|
||||||
assert alignment in (LEFT, CENTER, RIGHT)
|
assert alignment in (LEFT, CENTER, RIGHT)
|
||||||
self.text = text
|
self.text = text
|
||||||
self.fmt = fmt
|
self.fmt = fmt
|
||||||
|
@ -386,7 +371,7 @@ class StringCell(BaseCell):
|
||||||
return s % (
|
return s % (
|
||||||
align2xml[self.alignment],
|
align2xml[self.alignment],
|
||||||
self.fmt,
|
self.fmt,
|
||||||
html.escape(self.text))
|
escape(self.text))
|
||||||
|
|
||||||
class FormulaCell(BaseCell):
|
class FormulaCell(BaseCell):
|
||||||
|
|
||||||
|
@ -404,7 +389,6 @@ class FormulaCell(BaseCell):
|
||||||
def recalc(self, ns):
|
def recalc(self, ns):
|
||||||
if self.value is None:
|
if self.value is None:
|
||||||
try:
|
try:
|
||||||
# A hack to evaluate expressions using true division
|
|
||||||
self.value = eval(self.translated, ns)
|
self.value = eval(self.translated, ns)
|
||||||
except:
|
except:
|
||||||
exc = sys.exc_info()[0]
|
exc = sys.exc_info()[0]
|
||||||
|
@ -425,7 +409,7 @@ class FormulaCell(BaseCell):
|
||||||
return '<formula align="%s" format="%s">%s</formula>' % (
|
return '<formula align="%s" format="%s">%s</formula>' % (
|
||||||
align2xml[self.alignment],
|
align2xml[self.alignment],
|
||||||
self.fmt,
|
self.fmt,
|
||||||
self.formula)
|
escape(self.formula))
|
||||||
|
|
||||||
def renumber(self, x1, y1, x2, y2, dx, dy):
|
def renumber(self, x1, y1, x2, y2, dx, dy):
|
||||||
out = []
|
out = []
|
||||||
|
@ -776,7 +760,7 @@ class SheetGUI:
|
||||||
if text.startswith('='):
|
if text.startswith('='):
|
||||||
cell = FormulaCell(text[1:])
|
cell = FormulaCell(text[1:])
|
||||||
else:
|
else:
|
||||||
for cls in int, int, float, complex:
|
for cls in int, float, complex:
|
||||||
try:
|
try:
|
||||||
value = cls(text)
|
value = cls(text)
|
||||||
except:
|
except:
|
||||||
|
|
Loading…
Reference in New Issue