Browse Source

corrections, remove itpog

olivier.massot 8 years ago
parent
commit
b22599bc21

+ 1 - 1
pypog/geometry_objects.py

@@ -8,7 +8,7 @@ import math
 
 try:
     inf = math.inf
-except AttributeError:
+except ImportError:
     # backward compatibility for python < 3.5
     inf = float("inf")
 

+ 9 - 0
pypog/grid_objects.py

@@ -5,6 +5,7 @@
 '''
 from pypog.geometry_objects import BaseGeometry, FHexGeometry, SquareGeometry, \
     BoundingRect, HexGeometry
+from pypog.painter_objects import LinePainter
 
 
 class BaseGrid(object):
@@ -120,6 +121,14 @@ class BaseGrid(object):
     def rotate(self, *args):
         return self.geometry.rotate(*args, br=self.br)
 
+    # painting
+    def _compare_cells(self, x1, y1, x2, y2):
+        return True
+
+    # pathfinding
+    def _movingcost(self, from_x, from_y, to_x, to_y):
+        return 1
+
 class SquareGrid(BaseGrid):
     """ Square grid object """
     geometry = SquareGeometry

+ 2 - 3
pypog/painter_objects.py

@@ -162,8 +162,7 @@ class PaintPotPainter(BasePainter):
         BasePainter.__init__(self, *args)
         self._comp_pointer = (lambda x: False)
 
-    def start(self, x0, y0, comp_pointer):
-        self._comp_pointer = comp_pointer
+    def start(self, x0, y0):
         BasePainter.start(self, x0, y0)
 
     def update(self, *args):
@@ -184,7 +183,7 @@ class PaintPotPainter(BasePainter):
         buffer = set(self._grid.neighbours(*self._origin))
         while buffer:
             pos = buffer.pop()
-            if self._comp_pointer(*self._origin, *pos):
+            if self._grid._compare_cells(*self._origin, *pos):
                 current_selection.add(pos)
                 buffer |= (set(self._grid.neighbours(*pos)) - current_selection)
 

+ 4 - 5
pypog/pathfinding.py

@@ -30,15 +30,14 @@
 
     ** By Cro-Ki l@b, 2017 **
 '''
-from pypog.gridlib import _HexGrid
-
+from pypog.geometry_objects import HexGeometry
 
 def distance(coord1, coord2):
     """distance between 1 and 2"""
     x1, y1 = coord1
-    xu1, yu1, zu1 = _HexGrid.cv_off_cube(x1, y1)
+    xu1, yu1, zu1 = HexGeometry.cv_off_cube(x1, y1)
     x2, y2 = coord2
-    xu2, yu2, zu2 = _HexGrid.cv_off_cube(x2, y2)
+    xu2, yu2, zu2 = HexGeometry.cv_off_cube(x2, y2)
     return max(abs(xu1 - xu2), abs(yu1 - yu2), abs(zu1 - zu2))
 
 def square_distance(coord1, coord2):
@@ -75,7 +74,7 @@ class Node():
         """distance (en cases) entre deux coordonnees"""
         x1, y1 = coord1
         x2, y2 = coord2
-        return _HexGrid.distance_off(x1, y1, x2, y2)
+        return HexGeometry.distance_off(x1, y1, x2, y2)
 
 def _default_moving_cost_function(from_coord, to_coord):
     return 1

+ 0 - 199
tests/itpog.py

@@ -1,199 +0,0 @@
-'''
-Tipog tests
-
-Usage:
-  itpog [-v] <filename> [-o <output>]
-
-Options:
-  -v --verbose       Verbose print
-  -o --output        Register the results in <output> file (.json)
-  -h --help          Show this screen.
-  --version          Show version.
-'''
-
-import cProfile
-from importlib import import_module
-import json
-import os
-import timeit
-
-from docopt import docopt
-import yaml
-
-__version__ = "0.2"
-
-class ItpogDataError(IOError):
-    pass
-
-class ItPog(object):
-    def __init__(self, ipfile, verbose=False, output=None, outfilename=None):
-        try:
-            with open(ipfile, "r") as f:
-                data = yaml.load(f)
-        except FileNotFoundError:
-            raise FileNotFoundError("no file named '{}'".format(ipfile))
-        except yaml.scanner.ScannerError:
-            raise ItpogDataError("unreadable yaml file '{}'".format(ipfile))
-
-        if not data:
-            raise ItpogDataError("file '{}' is empty".format(ipfile))
-
-        self.ipfile = ipfile
-        self.ipdata = data
-
-        self.verbose = verbose
-        self.output = output
-        self.outfilename = outfilename
-        self.out_result = []
-
-    @classmethod
-    def run_file(cls, *args, **kwargs):
-        """ convenient method to load a file and run the tests
-        returns an ItPog object"""
-        try:
-            itpog = cls(*args, **kwargs)
-            itpog.run()
-        except Exception as e:
-            print("{}: {}".format(e.__class__.__name__, str(e)))
-        return itpog
-
-    def run(self):
-        """ run the jobs as programmed in the given file """
-        for name, imp in self.ipdata["imports"].items():
-            try:
-                globals()[name] = import_module(imp)
-            except (ModuleNotFoundError, ImportError):
-                raise ItpogDataError("unable to import '{}'".format(imp))
-        try:
-            jobs = self.ipdata["jobs"]
-        except KeyError:
-            raise ItpogDataError("missing 'jobs' entry")
-
-        for function_name, job in jobs.items():
-            try:
-                self._run_job(function_name, job)
-            except Exception as e:
-                print("{}: {}".format(e.__class__.__name__, str(e)))
-
-        if self.output:
-            if not self.outfilename:
-                self.outfilename = "{}_result.json".format(os.path.splitext(self.ipfile)[0])
-
-            outfilepath = os.path.join(".", self.outfilename)
-            try:
-                os.remove(outfilepath)
-            except FileNotFoundError:
-                pass
-            with open(outfilepath, "w+") as f:
-                json.dump(self.out_result, f)
-
-        print("** End of the tests")
-
-    def _run_job(self, function_name, job):
-            try:
-                function = eval(function_name)
-            except NameError:
-                raise NameError("unknown function ('{}')".format(function_name))
-
-            if self.verbose:
-                print("** Test function '{}'".format(function_name))
-
-            try:
-                args_lst = job["args"]
-            except KeyError:
-                args_lst = [[]]
-
-            try:
-                validator_str = job["validator"]
-                try:
-                    validator = eval(validator_str)
-                    if self.verbose:
-                        print("> validator: '{}'".format(validator_str))
-                except NameError:
-                    raise ItpogDataError("unknown function as validator ('{}')".format(validator_str))
-
-            except (TypeError, KeyError, SyntaxError):
-                validator = None
-
-            for args in args_lst:
-                call_str = "{}(*{})".format(function_name, args)
-
-                print(">> {}".format(call_str))
-
-                exectime = self.ittime(call_str)
-                print("\t> Run in {} ms.".format(exectime))
-
-                if self.verbose:
-                    self.profile(call_str)
-
-                if validator:
-                    valid = self.validate(function, validator, args)
-                    print("\t> Validated: {}".format(valid))
-
-                if self.output:
-                    last_result = {"call": call_str, "result": function(*args), "exectime": exectime}
-                    try:
-                        last_result.update(job["infos"])
-                    except KeyError:
-                        pass
-                    self.out_result.append(last_result)
-
-            if self.verbose:
-                print("------------------------------")
-
-
-    @staticmethod
-    def profile(_call):
-        """ Use the cProfile module to measure the execution time of each call
-            _call has to be a string
-        """
-        cProfile.run(_call, sort='nfl')
-
-    @staticmethod
-    def ittime(_call):
-        """ returns the execution time in milli-seconds
-            _call has to be a string
-            (ex: 'time.sleep(1)', which will return 1000)
-        """
-        number, t = 1, 0
-        while t < 10 ** 8:
-            t = timeit.timeit(lambda: eval(_call), number=number)
-            if t >= 0.1:
-                return 1000 * t / number
-            number *= 10
-        else:
-            return -1
-
-    @staticmethod
-    def validate(fct, validator, args=[], kwargs={}):
-        """ compare the results of 'fct' and 'validator' methods,
-        with the same 'args' and 'kwargs' arguments """
-        result = fct(*args, **kwargs)
-        attended = validator(*args, **kwargs)
-        return result == attended
-
-    @staticmethod
-    def generate_sample(filename="itpog_sample_file.yml"):
-        """ generate a sample yaml configuration file """
-        data = {"imports": {"time": "time", "math": "math"},
-                "jobs": {
-                          "time.sleep": {"infos": {"note": "x second attended"},
-                                    "args": [[0.1], [1]],
-                                    "validator": ""},
-                          "math.pow":  {"infos": {},
-                                    "args": [[1, 2], [3, 2], [3, 3], [4, 2]],
-                                    "validator": "lambda x, y: x**y"}
-                         }
-                }
-        with open(os.path.join(".", filename), "w+") as f:
-            yaml.dump(data, f)
-
-
-if __name__ == "__main__":
-
-    sysargs = docopt(__doc__, version=__version__)
-
-    itpog = ItPog.run_file(sysargs["<filename>"], sysargs["--verbose"], sysargs["--output"], sysargs["<output>"])
-
-
-

+ 0 - 18
tests/itpog_tests/hollow_rectangles.yml

@@ -1,18 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.hollow_rectangle:
-    infos: {'cell_shape': 61}
-    args:
-    - [0, 0, 1, 1]
-    - [0, 0, 10, 10]
-    - [0, 0, 100, 100]
-    validator:
-  geometry.hollow_rectangle:
-    infos: {'cell_shape': 4}
-    args:
-    - [0, 0, 1, 1]
-    - [0, 0, 10, 10]
-    - [0, 0, 100, 100]
-    validator:

File diff suppressed because it is too large
+ 0 - 0
tests/itpog_tests/hollow_rectangles_result.json


+ 0 - 18
tests/itpog_tests/lines.yml

@@ -1,18 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.fhex2_line:
-    infos: {'cell_shape': 61}
-    args:
-    - [0, 0, 1, 1]
-    - [0, 0, 10, 10]
-    - [0, 0, 100, 100]
-    validator:
-  geometry.squ2_line:
-    infos: {'cell_shape': 4}
-    args:
-    - [0, 0, 1, 1]
-    - [0, 0, 10, 10]
-    - [0, 0, 100, 100]
-    validator:

File diff suppressed because it is too large
+ 0 - 0
tests/itpog_tests/lines_result.json


+ 0 - 14
tests/itpog_tests/neighbours.yml

@@ -1,14 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.fhex2_neighbours:
-    infos: {'cell_shape': 61}
-    args:
-    - [0, 0]
-    validator:
-  geometry.squ2_neighbours:
-    infos: {'cell_shape': 4}
-    args:
-    - [0, 0]
-    validator:

+ 0 - 1
tests/itpog_tests/neighbours_result.json

@@ -1 +0,0 @@
-[{"call": "geometry.fhex2_neighbours(*[0, 0])", "result": [[0, -1], [1, -1], [1, 0], [0, 1], [-1, 0], [-1, -1]], "exectime": 0.014875455914448195, "cell_shape": 61}, {"call": "geometry.squ2_neighbours(*[0, 0])", "result": [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]], "exectime": 0.015270075426026498, "cell_shape": 4}]

+ 0 - 18
tests/itpog_tests/pivots.yml

@@ -1,18 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.fhex2_pivot:
-    infos: {'cell_shape': 61}
-    args:
-    - [[0, 0], [(1, 1)], 1]
-    - [[0, 0], [(i, i) for i in range(1, 11)], 1]
-    - [[0, 0], [(i, i) for i in range(1, 101)], 1]
-    validator:
-  geometry.squ2_pivot:
-    infos: {'cell_shape': 4}
-    args:
-    - [[0, 0], [(1, 1)], 1]
-    - [[0, 0], [(i, i) for i in range(1, 11)], 1]
-    - [[0, 0], [(i, i) for i in range(1, 101)], 1]
-    validator:

+ 0 - 1
tests/itpog_tests/pivots_result.json

@@ -1 +0,0 @@
-[]

+ 0 - 18
tests/itpog_tests/rectangles.yml

@@ -1,18 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.rectangle:
-    infos: {'cell_shape': 61}
-    args:
-    - [0, 0, 1, 1]
-    - [0, 0, 10, 10]
-    - [0, 0, 100, 100]
-    validator:
-  geometry.rectangle:
-    infos: {'cell_shape': 4}
-    args:
-    - [0, 0, 1, 1]
-    - [0, 0, 10, 10]
-    - [0, 0, 100, 100]
-    validator:

File diff suppressed because it is too large
+ 0 - 0
tests/itpog_tests/rectangles_result.json


+ 0 - 18
tests/itpog_tests/triangles.yml

@@ -1,18 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.fhex2_triangle:
-    infos: {'cell_shape': 61}
-    args:
-    - [0, 0, 1, 1, 2]
-    - [0, 0, 10, 10, 2]
-    - [0, 0, 100, 100, 2]
-    validator:
-  geometry.squ2_triangle:
-    infos: {'cell_shape': 4}
-    args:
-    - [0, 0, 1, 1, 2]
-    - [0, 0, 10, 10, 2]
-    - [0, 0, 100, 100, 2]
-    validator:

File diff suppressed because it is too large
+ 0 - 0
tests/itpog_tests/triangles_result.json


+ 0 - 18
tests/itpog_tests/zone.yml

@@ -1,18 +0,0 @@
-imports:
-  geometry: pypog.geometry
-  
-jobs:
-  geometry.zone:
-    infos: {'cell_shape': 61}
-    args:
-    - [61, 0, 0, 1]
-    - [61, 0, 0, 10]
-    - [61, 0, 0, 100]
-    validator:
-  geometry.zone:
-    infos: {'cell_shape': 4}
-    args:
-    - [4, 0, 0, 1]
-    - [4, 0, 0, 10]
-    - [4, 0, 0, 100]
-    validator:

+ 2 - 2
tests/test_geometry.py

@@ -8,7 +8,7 @@ from math import inf
 import unittest
 
 from pypog.geometry_objects import FHexGeometry, SquareGeometry, BaseGeometry, \
-    BoundingRect, IBoundingRect
+    BoundingRect
 
 
 class Test(unittest.TestCase):
@@ -29,7 +29,7 @@ class Test(unittest.TestCase):
         self.assertTrue((5, 5) in br)
         self.assertFalse((15, 15) in br)
 
-        ibr = IBoundingRect()
+        ibr = BoundingRect()
         self.assertTrue((5, 5) in ibr)
         self.assertTrue((15, 15) in ibr)
         self.assertTrue((10000, 10000) in ibr)

Some files were not shown because too many files changed in this diff