Lines Matching refs:self

37     def __init__(self):
38 self.cells = {} # {(x, y): cell, ...}
39 self.ns = dict(
40 cell = self.cellvalue,
41 cells = self.multicellvalue,
45 def cellvalue(self, x, y):
46 cell = self.getcell(x, y)
48 return cell.recalc(self.ns)
52 def multicellvalue(self, x1, y1, x2, y2):
60 seq.append(self.cellvalue(x, y))
63 def getcell(self, x, y):
64 return self.cells.get((x, y))
66 def setcell(self, x, y, cell):
69 self.cells[x, y] = cell
71 def clearcell(self, x, y):
73 del self.cells[x, y]
77 def clearcells(self, x1, y1, x2, y2):
78 for xy in self.selectcells(x1, y1, x2, y2):
79 del self.cells[xy]
81 def clearrows(self, y1, y2):
82 self.clearcells(0, y1, sys.maxsize, y2)
84 def clearcolumns(self, x1, x2):
85 self.clearcells(x1, 0, x2, sys.maxsize)
87 def selectcells(self, x1, y1, x2, y2):
92 return [(x, y) for x, y in self.cells
95 def movecells(self, x1, y1, x2, y2, dx, dy):
104 for x, y in self.cells:
105 cell = self.cells[x, y]
112 self.cells = new
114 def insertrows(self, y, n):
116 self.movecells(0, y, sys.maxsize, sys.maxsize, 0, n)
118 def deleterows(self, y1, y2):
121 self.clearrows(y1, y2)
122 self.movecells(0, y2+1, sys.maxsize, sys.maxsize, 0, y1-y2-1)
124 def insertcolumns(self, x, n):
126 self.movecells(x, 0, sys.maxsize, sys.maxsize, n, 0)
128 def deletecolumns(self, x1, x2):
131 self.clearcells(x1, x2)
132 self.movecells(x2+1, 0, sys.maxsize, sys.maxsize, x1-x2-1, 0)
134 def getsize(self):
136 for x, y in self.cells:
141 def reset(self):
142 for cell in self.cells.values():
146 def recalc(self):
147 self.reset()
148 for cell in self.cells.values():
150 cell.recalc(self.ns)
152 def display(self):
153 maxx, maxy = self.getsize()
166 for (x, y), cell in self.cells.items():
170 cell.recalc(self.ns)
202 def xml(self):
204 for (x, y), cell in self.cells.items():
214 def save(self, filename):
215 text = self.xml()
221 def load(self, filename):
223 SheetParser(self).parsefile(f)
227 def __init__(self, sheet):
228 self.sheet = sheet
230 def parsefile(self, f):
232 parser.StartElementHandler = self.startelement
233 parser.EndElementHandler = self.endelement
234 parser.CharacterDataHandler = self.data
237 def startelement(self, tag, attrs):
238 method = getattr(self, 'start_'+tag, None)
241 self.texts = []
243 def data(self, text):
244 self.texts.append(text)
246 def endelement(self, tag):
247 method = getattr(self, 'end_'+tag, None)
249 method("".join(self.texts))
251 def start_cell(self, attrs):
252 self.y = int(attrs.get("row"))
253 self.x = int(attrs.get("col"))
255 def start_value(self, attrs):
256 self.fmt = attrs.get('format')
257 self.alignment = xml2align.get(attrs.get('align'))
261 def end_int(self, text):
263 self.value = int(text)
265 self.value = None
269 def end_double(self, text):
271 self.value = float(text)
273 self.value = None
275 def end_complex(self, text):
277 self.value = complex(text)
279 self.value = None
281 def end_string(self, text):
282 self.value = text
284 def end_value(self, text):
285 if isinstance(self.value, BaseCell):
286 self.cell = self.value
287 elif isinstance(self.value, str):
288 self.cell = StringCell(self.value,
289 self.fmt or "%s",
290 self.alignment or LEFT)
292 self.cell = NumericCell(self.value,
293 self.fmt or "%s",
294 self.alignment or RIGHT)
296 def end_formula(self, text):
297 self.cell = FormulaCell(text,
298 self.fmt or "%s",
299 self.alignment or RIGHT)
301 def end_cell(self, text):
302 self.sheet.setcell(self.x, self.y, self.cell)
318 def __init__(self, value, fmt="%s", alignment=RIGHT):
321 self.value = value
322 self.fmt = fmt
323 self.alignment = alignment
325 def recalc(self, ns):
326 return self.value
328 def format(self):
330 text = self.fmt % self.value
332 text = str(self.value)
333 return text, self.alignment
335 def xml(self):
336 method = getattr(self, '_xml_' + type(self.value).__name__)
338 align2xml[self.alignment],
339 self.fmt,
342 def _xml_int(self):
343 if -2**31 <= self.value < 2**31:
344 return '<int>%s</int>' % self.value
346 return '<long>%s</long>' % self.value
348 def _xml_float(self):
349 return '<double>%r</double>' % self.value
351 def _xml_complex(self):
352 return '<complex>%r</complex>' % self.value
356 def __init__(self, text, fmt="%s", alignment=LEFT):
359 self.text = text
360 self.fmt = fmt
361 self.alignment = alignment
363 def recalc(self, ns):
364 return self.text
366 def format(self):
367 return self.text, self.alignment
369 def xml(self):
372 align2xml[self.alignment],
373 self.fmt,
374 escape(self.text))
378 def __init__(self, formula, fmt="%s", alignment=RIGHT):
380 self.formula = formula
381 self.translated = translate(self.formula)
382 self.fmt = fmt
383 self.alignment = alignment
384 self.reset()
386 def reset(self):
387 self.value = None
389 def recalc(self, ns):
390 if self.value is None:
392 self.value = eval(self.translated, ns)
396 self.value = exc.__name__
398 self.value = str(exc)
399 return self.value
401 def format(self):
403 text = self.fmt % self.value
405 text = str(self.value)
406 return text, self.alignment
408 def xml(self):
410 align2xml[self.alignment],
411 self.fmt,
412 escape(self.formula))
414 def renumber(self, x1, y1, x2, y2, dx, dy):
416 for part in re.split(r'(\w+)', self.formula):
425 return FormulaCell("".join(out), self.fmt, self.alignment)
491 def __init__(self, filename="sheet1.xml", rows=10, columns=5):
498 self.filename = filename
499 self.sheet = Sheet()
501 self.sheet.load(filename)
503 maxx, maxy = self.sheet.getsize()
507 self.root = Tk.Tk()
508 self.root.wm_title("Spreadsheet: %s" % self.filename)
509 self.beacon = Tk.Label(self.root, text="A1",
511 self.entry = Tk.Entry(self.root)
512 self.savebutton = Tk.Button(self.root, text="Save",
513 command=self.save)
514 self.cellgrid = Tk.Frame(self.root)
516 self.cellgrid.pack(side="bottom", expand=1, fill="both")
517 self.beacon.pack(side="left")
518 self.savebutton.pack(side="right")
519 self.entry.pack(side="left", expand=1, fill="x")
521 self.entry.bind("<Return>", self.return_event)
522 self.entry.bind("<Shift-Return>", self.shift_return_event)
523 self.entry.bind("<Tab>", self.tab_event)
524 self.entry.bind("<Shift-Tab>", self.shift_tab_event)
525 self.entry.bind("<Delete>", self.delete_event)
526 self.entry.bind("<Escape>", self.escape_event)
528 self.makegrid(rows, columns)
530 self.currentxy = None
531 self.cornerxy = None
532 self.setcurrent(1, 1)
534 self.sync()
536 def delete_event(self, event):
537 if self.cornerxy != self.currentxy and self.cornerxy is not None:
538 self.sheet.clearcells(*(self.currentxy + self.cornerxy))
540 self.sheet.clearcell(*self.currentxy)
541 self.sync()
542 self.entry.delete(0, 'end')
545 def escape_event(self, event):
546 x, y = self.currentxy
547 self.load_entry(x, y)
549 def load_entry(self, x, y):
550 cell = self.sheet.getcell(x, y)
557 self.entry.delete(0, 'end')
558 self.entry.insert(0, text)
559 self.entry.selection_range(0, 'end')
561 def makegrid(self, rows, columns):
566 self.rows = rows
567 self.columns = columns
568 self.gridcells = {}
570 cell = Tk.Label(self.cellgrid, relief='raised')
572 cell.bind("<ButtonPress-1>", self.selectall)
575 self.cellgrid.grid_columnconfigure(x, minsize=64)
576 cell = Tk.Label(self.cellgrid, text=colnum2name(x), relief='raised')
578 self.gridcells[x, 0] = cell
581 cell.bind("<ButtonPress-1>", self.selectcolumn)
582 cell.bind("<B1-Motion>", self.extendcolumn)
583 cell.bind("<ButtonRelease-1>", self.extendcolumn)
584 cell.bind("<Shift-Button-1>", self.extendcolumn)
587 cell = Tk.Label(self.cellgrid, text=str(y), relief='raised')
589 self.gridcells[0, y] = cell
592 cell.bind("<ButtonPress-1>", self.selectrow)
593 cell.bind("<B1-Motion>", self.extendrow)
594 cell.bind("<ButtonRelease-1>", self.extendrow)
595 cell.bind("<Shift-Button-1>", self.extendrow)
599 cell = Tk.Label(self.cellgrid, relief='sunken',
602 self.gridcells[x, y] = cell
606 cell.bind("<ButtonPress-1>", self.press)
607 cell.bind("<B1-Motion>", self.motion)
608 cell.bind("<ButtonRelease-1>", self.release)
609 cell.bind("<Shift-Button-1>", self.release)
611 def selectall(self, event):
612 self.setcurrent(1, 1)
613 self.setcorner(sys.maxsize, sys.maxsize)
615 def selectcolumn(self, event):
616 x, y = self.whichxy(event)
617 self.setcurrent(x, 1)
618 self.setcorner(x, sys.maxsize)
620 def extendcolumn(self, event):
621 x, y = self.whichxy(event)
623 self.setcurrent(self.currentxy[0], 1)
624 self.setcorner(x, sys.maxsize)
626 def selectrow(self, event):
627 x, y = self.whichxy(event)
628 self.setcurrent(1, y)
629 self.setcorner(sys.maxsize, y)
631 def extendrow(self, event):
632 x, y = self.whichxy(event)
634 self.setcurrent(1, self.currentxy[1])
635 self.setcorner(sys.maxsize, y)
637 def press(self, event):
638 x, y = self.whichxy(event)
640 self.setcurrent(x, y)
642 def motion(self, event):
643 x, y = self.whichxy(event)
645 self.setcorner(x, y)
649 def whichxy(self, event):
650 w = self.cellgrid.winfo_containing(event.x_root, event.y_root)
658 def save(self):
659 self.sheet.save(self.filename)
661 def setcurrent(self, x, y):
663 if self.currentxy is not None:
664 self.change_cell()
665 self.clearfocus()
666 self.beacon['text'] = cellname(x, y)
667 self.load_entry(x, y)
668 self.entry.focus_set()
669 self.currentxy = x, y
670 self.cornerxy = None
671 gridcell = self.gridcells.get(self.currentxy)
675 def setcorner(self, x, y):
676 if self.currentxy is None or self.currentxy == (x, y):
677 self.setcurrent(x, y)
679 self.clearfocus()
680 self.cornerxy = x, y
681 x1, y1 = self.currentxy
682 x2, y2 = self.cornerxy or self.currentxy
687 for (x, y), cell in self.gridcells.items():
690 gridcell = self.gridcells.get(self.currentxy)
693 self.setbeacon(x1, y1, x2, y2)
695 def setbeacon(self, x1, y1, x2, y2):
709 name1 = cellname(*self.currentxy)
710 name2 = cellname(*self.cornerxy)
712 self.beacon['text'] = name
715 def clearfocus(self):
716 if self.currentxy is not None:
717 x1, y1 = self.currentxy
718 x2, y2 = self.cornerxy or self.currentxy
723 for (x, y), cell in self.gridcells.items():
727 def return_event(self, event):
729 self.change_cell()
730 x, y = self.currentxy
731 self.setcurrent(x, y+1)
734 def shift_return_event(self, event):
736 self.change_cell()
737 x, y = self.currentxy
738 self.setcurrent(x, max(1, y-1))
741 def tab_event(self, event):
743 self.change_cell()
744 x, y = self.currentxy
745 self.setcurrent(x+1, y)
748 def shift_tab_event(self, event):
750 self.change_cell()
751 x, y = self.currentxy
752 self.setcurrent(max(1, x-1), y)
755 def change_cell(self):
757 x, y = self.currentxy
758 text = self.entry.get()
774 self.sheet.clearcell(x, y)
776 self.sheet.setcell(x, y, cell)
777 self.sync()
779 def sync(self):
781 self.sheet.recalc()
782 for (x, y), gridcell in self.gridcells.items():
785 cell = self.sheet.getcell(x, y)
798 "Basic non-gui self-test."