def __init__(self, lat, lon, name="", description="", phoneNumbers=[], urls=[], addressLines=[], emails=[]): Point.__init__(self, lat, lon, message=name) self.name = name self.description = description self.message=None self.phoneNumbers = phoneNumbers self.urls = urls self.addressLines = addressLines self.emails = emails
def test_dist_length(self): self.assertEqual(2, self.rec_elem.distribution_length()) self.rec_elem.add_point(Point(10.5, 0)) self.assertEqual(10.5, self.rec_elem.distribution_length()) for p in list(self.rec_elem.get_points()): self.rec_elem.remove_point(p) self.assertRaises(IndexError, self.rec_elem.distribution_length)
def setUp(self): self.timestamp = time.time() self.rec_type = "rec" self.ch_width = 4 self.rec_elem = RecoilElement( mo.get_element(), [Point((0, 4)), Point((1, 5)), Point((2, 10))], color="black", description="foo", name="bar", rec_type="rec", reference_density=3, channel_width=self.ch_width, modification_time=self.timestamp )
def run(): print("\nPython Special methods".upper()) print(f"{32*'-'}\n") print("# repr() of Point Class:") point = Point(20, 9) print(f"\t{repr(point)}") print("\n\n")
def test_get_item(self): p = Point(1, 2) self.assertEqual(1, p[0]) self.assertEqual(1, p["x"]) self.assertEqual(2, p[1]) self.assertEqual(2, p["y"]) self.assertRaises(ValueError, lambda: p[-1]) self.assertRaises(ValueError, lambda: p[2]) self.assertRaises(ValueError, lambda: p["z"])
def test_triangle(self): triangle = ( Point(0, 0), Point(10, 10), Point(20, 0) ) self.assertTrue(mf.point_inside_polygon(Point(10, 5), triangle)) self.assertFalse(mf.point_inside_polygon(Point(5, 5), triangle)) self.assertFalse(mf.point_inside_polygon(Point(20, 5), triangle)) self.assertFalse(mf.point_inside_polygon(Point(20, 0), triangle))
def test_bad_inputs(self): self.assertRaises(IndexError, lambda: mf.point_inside_polygon(Point(0, 0), [])) square = [ (0, 0), (0, 1), (1, 1), (1, 0) ] self.assertRaises(ValueError, lambda: mf.point_inside_polygon([], square)) self.assertRaises(ValueError, lambda: mf.point_inside_polygon([1, 2, 3], square))
def test_remove_point(self): self.rec_elem.remove_point(self.p1) self.assertEqual([self.p2, self.p3], self.rec_elem.get_points()) self.rec_elem.remove_point(self.p3) self.assertEqual([self.p2], self.rec_elem.get_points()) self.rec_elem.remove_point(Point(self.p2_args)) self.assertEqual([], self.rec_elem.get_points()) self.assertRaises( ValueError, lambda: self.rec_elem.remove_point(self.p1))
def make_step(self, board): min_dis = (board.width * board.height + 1)\ * (board.width * board.height + 1) best_point = self.last_pos for i in range(-1, 2): for j in range(-1, 2): new_pos = Point( X=(self.pos.X + i + board.width) % board.width, Y=(self.pos.Y + j + board.height) % board.height) x = new_pos.X y = new_pos.Y is_wall = board.cell(x, y) == MapCell.WALL is_same = self.last_pos == new_pos is_frigh = self.state != MonstersState.FRIGHTENED is_change = is_same and is_frigh if abs(i) == abs(j) or is_change or is_wall: continue dist = new_pos.find_dist_sq(self.target) if dist < min_dis: min_dis = dist best_point = new_pos self.last_pos = self.pos self.pos = best_point
def astar(start_point=None, end_point=None): image = read_image() if not start_point: start_point = [ int(n) for n in input( 'Please enter the start point in "x y" format without quotes: ' ).split() ] start_point = Point(*start_point, *image.getpixel(tuple(start_point))) if not end_point: end_point = [ int(n) for n in input( 'Please enter the end point in "x y" format without quotes: '). split() ] end_point = Point(*end_point, *image.getpixel(tuple(end_point))) timer = Timer() stack = Stack() stack.push(start_point) while stack.length() > 0 and stack.pop() != end_point: for point in stack.last_pop.get_neighbours(image): stack.push(point) stack.red_sort(end_point) current_point = stack.last_pop while current_point.parent: image.putpixel((current_point.x, current_point.y), (0, 255, 0)) current_point = current_point.parent image.show() timer.print()
def bfs(start_point=None, end_point=None): image = read_image() if not start_point: start_point = [ int(n) for n in input( 'Please enter the start point in "x y" format without quotes: ' ).split() ] start_point = Point(*start_point, *image.getpixel(tuple(start_point))) if not end_point: end_point = [ int(n) for n in input( 'Please enter the end point in "x y" format without quotes: ' ).split() ] end_point = Point(*end_point, *image.getpixel(tuple(end_point))) timer = Timer() stack = Stack() stack.push(start_point) while stack.length() > 0 and stack.pop() != end_point: for point in stack.last_pop.get_neighbours(image): stack.push(point) stack.distance_sort(end_point) image.putpixel((stack.last_pop.x, stack.last_pop.y), (0, 255, 0)) image.show() timer.print() print("Total numberg of stack pops:", stack.total_pop) print("Max length of stack:", stack.max_length)
def test_get_neighbours(self): ln, rn = self.rec_elem.get_neighbors(self.p1) self.assertIsNone(ln) self.assertIs(rn, self.p2) ln, rn = self.rec_elem.get_neighbors(self.p2) self.assertIs(ln, self.p1) self.assertIs(rn, self.p3) ln, rn = self.rec_elem.get_neighbors(self.p3) self.assertIs(ln, self.p2) self.assertIsNone(rn) ln, rn = self.rec_elem.get_neighbors(Point(1, 1)) self.assertIs(ln, self.p1) self.assertIs(rn, self.p3)
def _move_point(self, point, direction, n=0): new_point = Point(X=point.X, Y=point.Y) if direction == Direction.RIGHT: new_point.X = (new_point.X + 1 + n) % self.width elif direction == Direction.LEFT: new_point.X = (new_point.X - 1 - n + self.width) \ % self.width elif direction == Direction.UP: new_point.Y = (new_point.Y - 1 - n + self.height) \ % self.height else: new_point.Y = (new_point.Y + 1 + n) % self.height return new_point
def test_lt(self): self.assertLess(Point((0, 0)), Point((1, 0))) self.assertLess(Point((0, 2)), Point((1, 0))) self.assertRaises(TypeError, lambda: Point((0, 2)) < (1, 2)) self.assertRaises(TypeError, lambda: (1, 2) < Point((0, 2)))
def test_point_and_straight(self): self.assertFalse(mf.point_inside_polygon(Point(0, 0), [Point(0, 0)])) self.assertFalse(mf.point_inside_polygon(Point(0, 0), [Point(-1, 0), Point(1, 0)]))
def main(args=None): print("--- POINT TEST ---") point1 = Point() point2 = Point() point1.reset() point2.move(5, 0) print(point2.calculate_distance(point1)) assert(point2.calculate_distance(point1) == point1.calculate_distance(point2)) point1.move(3, 4) print(point1.calculate_distance(point2)) print(point1.calculate_distance(point1)) point3 = Point(3, 5) print(point3.x, point3.y) print("--- END POINT TEST ---") print("--- SECRET STRING TEST ---") secret_string = SecretString("ACME: Top Secret.", "antwerp") print(secret_string.decrypt("antwerp")) print(secret_string._SecretString__plain_string) print("--- END SECRET STRING TEST ---") print("--- POLYMORPHISM TEST ---") mp3_file = MP3File("file1.mp3") wav_file = WavFile("file2.wav") ogg_file = OggFile("file3.ogg") mp3_file.play() wav_file.play() ogg_file.play() print("--- END POLYMORPHISM TEST ---")
def test_rectangle(self): rectangle = ( Point(0, 0), Point(1, 1), Point(2, 1), Point(1, 0) ) self.assertFalse(mf.point_inside_polygon(Point(0, 0), rectangle)) self.assertFalse(mf.point_inside_polygon(Point(1, 0), rectangle)) self.assertFalse(mf.point_inside_polygon(Point(1, 1), rectangle)) self.assertTrue(mf.point_inside_polygon(Point(1.5, 1), rectangle)) self.assertTrue(mf.point_inside_polygon(Point(2, 1), rectangle)) self.assertTrue(mf.point_inside_polygon(Point(1, 0.5), rectangle)) self.assertTrue(mf.point_inside_polygon(Point(1.5, 0.8), rectangle)) self.assertFalse(mf.point_inside_polygon(Point(0, 0.25), rectangle)) self.assertFalse(mf.point_inside_polygon(Point(0.5, -0.1), rectangle)) self.assertFalse(mf.point_inside_polygon(Point(1.5, 0.25), rectangle))
def test_eq(self): # Point(x, y) should be equal to Point((x, y)) self.assertEqual(Point((0, 0)), Point(0, 0)) self.assertEqual(Point((1, 0)), Point(1, 0)) self.assertEqual(Point((0, 1)), Point(0, 1)) self.assertNotEqual(Point((1, 0)), Point(0, 0)) self.assertNotEqual(Point((0, 0)), Point(1, 1)) self.assertNotEqual(Point((1, 0)), Point(0, 1))
def test_element_simulation_settings(self): """This tests that ElementSimulation is run with the correct settings depending on how 'use_default_settings' and 'use_request_settings' have been set. """ with tempfile.TemporaryDirectory() as tmp_dir: # File paths tmp_dir = Path(tmp_dir).resolve() req_dir = tmp_dir / "req" sim_dir = req_dir / "Sample_01-foo" / \ f"{Simulation.DIRECTORY_PREFIX}01-bar" simu_file = sim_dir / "bar.simulation" # Request request = Request( req_dir, "test_req", GlobalSettings(config_dir=tmp_dir), {}, enable_logging=False) self.assertEqual(req_dir, request.directory) self.assertEqual("test_req", request.request_name) # Sample sample = request.samples.add_sample(name="foo") self.assertIs(sample, request.samples.samples[0]) self.assertIsInstance(sample, Sample) self.assertEqual("foo", sample.name) self.assertEqual(request, sample.request) self.assertEqual("Sample_01-foo", sample.directory) # Simulation sim = sample.simulations.add_simulation_file( sample, simu_file, 0) # Disable logging so the logging file handlers do not cause # an exception when the tmp dir is removed sim.close_log_files() self.assertIs(sim, sample.simulations.get_key_value(0)) self.assertIsInstance(sim.detector, Detector) self.assertIsInstance(sim.run, Run) self.assertEqual("bar", sim.name) self.assertEqual(request, sim.request) self.assertEqual(sample, sim.sample) # ElementSimulation rec_elem = RecoilElement( Element.from_string("Fe"), [Point((1, 1))], name="recoil_name") elem_sim = sim.add_element_simulation(rec_elem) self.assertIs(elem_sim, sim.element_simulations[0]) elem_sim.number_of_preions = 2 elem_sim.number_of_ions = 3 self.assertEqual(request, elem_sim.request) self.assertEqual("Fe-Default", elem_sim.get_full_name()) # Some pre-simulation checks self.assertIsNot(sim.target, request.default_target) self.assertIsNot(elem_sim, request.default_element_simulation) self.assertIsNot(sim.detector, request.default_detector) self.assertIsNot(sim.run, request.default_run) self.assertIsNot(sim.run.beam, request.default_run.beam) self.assertNotEqual( elem_sim.number_of_ions, request.default_element_simulation.number_of_ions) self.assert_expected_settings(elem_sim, request, sim)
# import sys # sys.path.append('../CourseWork/') from modules.point import Point point1 = Point(4, 12) point2 = Point(1, 1) point1.rotate(0, point2) print(str(point1))
def test_str(self): self.assertEqual("1 4.0", str(Point((1, 4.0)))) self.assertEqual("1.23 4.5624", str(Point((1.2332236, 4.562389))))
def test_calculate_new_point(self): p1 = Point(0, 1) p2 = Point(1, 2) self.assertEqual(Point(0.5, 1.5), p1.calculate_new_point(p2, 0.5)) self.assertEqual(Point(2, 3), p1.calculate_new_point(p2, 2)) self.assertEqual(Point(-1, 0), p1.calculate_new_point(p2, -1)) def rand(): return random.uniform(-100, 100) for i in range(100): x = rand() p1 = Point((rand(), rand())) p2 = Point((rand(), rand())) # To avoid dealing with floating point precision while testing, # turn the points into strings. self.assertEqual(str(p1.calculate_new_point(p2, x)), str(p2.calculate_new_point(p1, x)))
from modules.point import Point k = Point('Drohobych', 49.358012, 23.512319) print(k.is_good()) print(k.delta) print(k)