Skip to content

Commit

Permalink
Implementováno ukládání naklikaných souřadnic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Doležal committed Apr 27, 2018
1 parent c784eb0 commit 892e81a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 23 deletions.
69 changes: 60 additions & 9 deletions fr2fd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import re
import time, random
import collections
import contextlib

import sympy

Expand All @@ -39,6 +40,8 @@

REGEX_NEW_LINE = re.compile(r"""(?:\r\n|[\r\n])""")

DEFAULT_FILE_SAVE_POINTS = "data.txt"

# ====== FUNKCE A TŘÍDY ======

replaceNewLine = lambda input_string: REGEX_NEW_LINE.sub(r"""\\n """, input_string)
Expand All @@ -49,7 +52,9 @@

class CoordsGetter:

def __init__(self):
def __init__(self, verbose=True):
self.verbose = verbose

self.width = 600+1
self.width_scale = 50 # počet pixelů na jednotku na ose x
self.height = 400+1
Expand All @@ -69,10 +74,12 @@ def __init__(self):
self.canvas.pack()

# Nastavení událostí na tlačítka
# TODO: <Escape>: clean, <S>: save
# TODO: V pravém horním rohu vykresvat aktuální platné souřadnice kurzoru myši.
self.canvas.bind("<Button-1>", self.pushCoord)
self.window.bind('<BackSpace>', self.popCoord)
self.window.bind('<Return>', self.close)
self.window.protocol('WM_DELETE_WINDOW', self.close)
self.window.bind('<Return>', self.saveAndClose)
self.window.protocol('WM_DELETE_WINDOW', self.saveAndClose)

# Vykreslení souřadné osy
self.drawAxes()
Expand Down Expand Up @@ -100,23 +107,46 @@ def pushCoord(self, event):
# Definiční obor i obor hodnot je od nuly do nekonečna a žádné dva body nesmí sdílet x-ovou souřadnici
if coord.x >= 0.0 and coord.y >= 0.0 and (not self.coords or coord.x > self.coords[-1].x):
self.coords.append(coord)
print("Pushed", coord)
if self.verbose:
print("Pushed", coord)

# Vykreslení křížku
self.drawCross(canvas_coord, tag=coord)

def popCoord(self, event=None):
if self.coords:
coord = self.coords.pop()
print("Popped", coord)
if self.verbose:
print("Popped", coord)

self.canvas.delete(coord)
else:
print("Nothing to pop.")
if self.verbose:
print("Nothing to pop.")

def save(self, event=None):
"""
Uloží ve formátu pro TinyGp
"""
filename = DEFAULT_FILE_SAVE_POINTS

NVAR = 1
NRAND = 100
MINRAND = -5
MAXRAND = 5

with smartOpen(filename, "w") as fp:
print(NVAR, NRAND, MINRAND, MAXRAND, len(self.coords), file=fp)
for coord in self.coords:
print("", coord.x, coord.y, file=fp)

def close(self, event=None):
self.window.destroy()

def saveAndClose(self, event=None):
self.save(event)
self.close(event)

def drawCross(self, coord, tag=None, cross_length = 3):
"""
Vykreslení křížku
Expand Down Expand Up @@ -219,9 +249,25 @@ def preparePrng(prng=None, seed=None):

return prng

@contextlib.contextmanager
def smartOpen(filename, mode="r"):
if filename == "-":
if "r" in mode:
fp = sys.stdin
else:
fp = sys.stdout
else:
fp = open(filename, mode)

try:
yield fp
finally:
if filename != "-":
fp.close()

def loadCoords(filename):
coords = []
with open(filename, 'r') as fp:
with smartOpen(filename) as fp:
x_coords = set()
for line in fp: # Načítají se body
values = line.split()
Expand Down Expand Up @@ -339,8 +385,13 @@ def main():
nargs="?",
default=None,
type=str,
help=("Cesta k souboru s body, jenž mají být interpolovány."),
metavar="data.txt",
help=("Cesta k souboru s body, jenž mají být interpolovány. "
"Je-li zadáno \"-\", čte se ze stdin. "
"Není-li zadán soubor s body, pak se souřadnice získají od uživatele naklikáním do grafu. "
"Levé tlačítko myši <LMB> přidá bod, <BackSpace> odebere poslední přidaný, <Enter> uloží a ukončí. "
"Body jsou uloženy do souboru \"%s\". Existuje-li, přepíše se." % DEFAULT_FILE_SAVE_POINTS
),
metavar=DEFAULT_FILE_SAVE_POINTS,
)
arguments = parser.parse_args()

Expand Down
35 changes: 21 additions & 14 deletions tests/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@
NFITCASES = 60

@contextlib.contextmanager
def smartOpenToWrite(filename=None):
if filename is not None:
fp = open(filename, 'w')
else:
fp = sys.stdout

try:
yield fp
finally:
if fp is not sys.stdout:
fp.close()

def generateTest(f, nfitcases=NFITCASES, filename=None, start=0, step=0.1):
with smartOpenToWrite(filename) as fp:
def smartOpen(filename, mode="r"):
if filename == "-":
if "r" in mode:
fp = sys.stdin
else:
fp = sys.stdout
else:
fp = open(filename, mode)

try:
yield fp
finally:
if filename != "-":
fp.close()

def generateTest(f, nfitcases=NFITCASES, filename="-", start=0, step=0.1):
"""
Uloží ve formátu pro TinyGp
"""

with smartOpen(filename, "w") as fp:
print(NVAR, NRAND, MINRAND, MAXRAND, nfitcases+10, file=fp)

x = start
Expand Down

0 comments on commit 892e81a

Please sign in to comment.