From 4722fddbbe2b5906e2b8e032094300d598aad03b Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 13 May 2023 04:42:51 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- examples/core_animation.py | 7 ++-- .../core_boolean_fuzzy_cut_emmenthaler.py | 4 +- examples/core_boolean_sewed_shapes.py | 3 +- examples/core_display_activate_manipulator.py | 4 +- examples/core_display_callbacks.py | 2 +- examples/core_display_clip_planes.py | 2 +- ...core_display_material_compound_subshape.py | 2 +- examples/core_display_point_cloud.py | 19 +++++---- examples/core_display_signal_slots.py | 6 +-- examples/core_geometry_airfoil.py | 2 +- examples/core_geometry_geomplate.py | 15 +++---- examples/core_geometry_medial_axis_offset.py | 6 +-- examples/core_geometry_nurbs_converter.py | 8 +--- .../core_geometry_oriented_bounding_box.py | 5 ++- examples/core_geometry_quaternion.py | 4 +- examples/core_geometry_recognize_feature.py | 2 +- examples/core_geometry_splinecage.py | 17 ++++---- examples/core_geometry_surface_from_curves.py | 6 +-- examples/core_mesh_gmsh.py | 12 ++---- examples/core_modeling_bool_demo.py | 16 +++----- examples/core_modeling_sprocket.py | 31 +++----------- examples/core_parallel_slicer.py | 10 ++--- examples/core_shape_properties.py | 6 +-- examples/core_tesselation_vertices_list.py | 6 +-- examples/core_topology_face.py | 2 +- examples/core_topology_heightmap.py | 19 +++------ examples/core_topology_local_ops.py | 2 +- ...re_topology_uv_to_cartesian_coordinates.py | 3 +- examples/core_visualization_glsl.py | 2 +- ...e_visualization_graphic3d_custom_opengl.py | 9 ++--- .../core_visualization_overpaint_viewer.py | 40 ++++++------------- examples/core_webgl_threejs_random_toruses.py | 4 +- examples/core_webgl_x3dom_random_boxes.py | 2 +- run_examples_as_tests.py | 4 +- 34 files changed, 102 insertions(+), 180 deletions(-) diff --git a/examples/core_animation.py b/examples/core_animation.py index 75eb9d8..bfaf67e 100644 --- a/examples/core_animation.py +++ b/examples/core_animation.py @@ -32,8 +32,7 @@ def build_shape(): boxshp = BRepPrimAPI_MakeBox(50.0, 50.0, 50.0).Shape() - ais_boxshp = display.DisplayShape(boxshp, update=True)[0] - return ais_boxshp + return display.DisplayShape(boxshp, update=True)[0] def rotating_cube_1_axis(event=None): @@ -44,7 +43,7 @@ def rotating_cube_1_axis(event=None): angle = 0.0 tA = time.time() n_rotations = 200 - for i in range(n_rotations): + for _ in range(n_rotations): aCubeTrsf.SetRotation(ax1, angle) aCubeToploc = TopLoc_Location(aCubeTrsf) display.Context.SetLocation(ais_boxshp, aCubeToploc) @@ -63,7 +62,7 @@ def rotating_cube_2_axis(event=None): angle = 0.0 tA = time.time() n_rotations = 200 - for i in range(n_rotations): + for _ in range(n_rotations): a_cube_trsf.SetRotation(ax1, angle) a_cube_trsf2.SetRotation(ax2, angle) aCubeToploc = TopLoc_Location(a_cube_trsf * a_cube_trsf2) diff --git a/examples/core_boolean_fuzzy_cut_emmenthaler.py b/examples/core_boolean_fuzzy_cut_emmenthaler.py index 9c855da..b3f9f6b 100644 --- a/examples/core_boolean_fuzzy_cut_emmenthaler.py +++ b/examples/core_boolean_fuzzy_cut_emmenthaler.py @@ -32,12 +32,12 @@ def random_pnt(): - x, y, z = [random.uniform(0, 1) for i in range(3)] + x, y, z = [random.uniform(0, 1) for _ in range(3)] return gp_Pnt(x, y, z) def random_vec(): - x, y, z = [random.uniform(-1, 1) for i in range(3)] + x, y, z = [random.uniform(-1, 1) for _ in range(3)] return gp_Vec(x, y, z) diff --git a/examples/core_boolean_sewed_shapes.py b/examples/core_boolean_sewed_shapes.py index 7fc0620..e893cc1 100644 --- a/examples/core_boolean_sewed_shapes.py +++ b/examples/core_boolean_sewed_shapes.py @@ -31,8 +31,7 @@ def MakeSolidFromShell(shell): ms = BRepBuilderAPI_MakeSolid() ms.Add(topods.Shell(shell)) - solid = ms.Solid() - return solid + return ms.Solid() def make_face_from_4_points(pnt1, pnt2, pnt3, pnt4): diff --git a/examples/core_display_activate_manipulator.py b/examples/core_display_activate_manipulator.py index 714dc05..5c240c9 100644 --- a/examples/core_display_activate_manipulator.py +++ b/examples/core_display_activate_manipulator.py @@ -76,10 +76,10 @@ def createLayout(self): def show_layer(self): if self.show_layer_button.isChecked(): self.layer.show() - self.display.FitAll() else: self.layer.hide() - self.display.FitAll() + + self.display.FitAll() def activate_manipulator(self): if self.activate_manip_button.isChecked(): diff --git a/examples/core_display_callbacks.py b/examples/core_display_callbacks.py index 4d72726..b86fc15 100644 --- a/examples/core_display_callbacks.py +++ b/examples/core_display_callbacks.py @@ -28,7 +28,7 @@ def print_xy_click(shp, *kwargs): def compute_bbox(shp, *kwargs): - print("Compute bbox for %s " % shp) + print(f"Compute bbox for {shp} ") for shape in shp: bbox = Bnd_Box() brepbndlib_Add(shape, bbox) diff --git a/examples/core_display_clip_planes.py b/examples/core_display_clip_planes.py index a16cc8d..19befff 100644 --- a/examples/core_display_clip_planes.py +++ b/examples/core_display_clip_planes.py @@ -69,7 +69,7 @@ def disable_clip_plane(event=None): def animate_translate_clip_plane(event=None): plane_definition = clip_plane_1.ToPlane() # it's a gp_Pln h = 0.2 - for i in range(100): + for _ in range(100): plane_definition.Translate(gp_Vec(0.0, 0.0, h)) clip_plane_1.SetEquation(plane_definition) display.Context.UpdateCurrentViewer() diff --git a/examples/core_display_material_compound_subshape.py b/examples/core_display_material_compound_subshape.py index 09eb5b4..1ec9218 100644 --- a/examples/core_display_material_compound_subshape.py +++ b/examples/core_display_material_compound_subshape.py @@ -33,7 +33,7 @@ s = BRepPrimAPI_MakeCylinder(radius, 200).Shape() delta_x = 0.0 solids = [] -for mat in available_materials: +for _ in available_materials: s2 = translate_shp(s, gp_Vec(delta_x, 0.0, 0.0)) delta_x += 2 * radius + 1.0 solids.append(s2) diff --git a/examples/core_display_point_cloud.py b/examples/core_display_point_cloud.py index c82d8f1..33dbf4b 100644 --- a/examples/core_display_point_cloud.py +++ b/examples/core_display_point_cloud.py @@ -45,13 +45,12 @@ def pcd_get_number_of_vertices(pcd_filename): POINTS 397 DATA ascii """ - f = open(pcd_filename, "r") - # read 8 lines - for i in range(8): - f.readline() - # the 9th line holds the number of points - number_of_points = int(f.readline().split()[1]) - f.close() + with open(pcd_filename, "r") as f: + # read 8 lines + for _ in range(8): + f.readline() + # the 9th line holds the number of points + number_of_points = int(f.readline().split()[1]) return number_of_points @@ -59,7 +58,7 @@ def random_points(event=None): n_points = 500000 # first, create a set of 1000 points points_3d = Graphic3d_ArrayOfPoints(n_points) - for idx in range(n_points): + for _ in range(n_points): x = random.uniform(-50, 50) y = random.uniform(-50, 50) z = random.uniform(-50, 50) @@ -86,9 +85,9 @@ def bunny(event=None): # fedd it with vertices fp = open(pcd_file_name, "r") # read 11 lines to skip header - for i in range(10): + for _ in range(10): fp.readline() - for i in range(nbr_of_vertices): + for _ in range(nbr_of_vertices): line = fp.readline() x, y, z = map(float, line.split()) pc.AddVertex(x, y, z) diff --git a/examples/core_display_signal_slots.py b/examples/core_display_signal_slots.py index f752bfe..736050f 100644 --- a/examples/core_display_signal_slots.py +++ b/examples/core_display_signal_slots.py @@ -33,8 +33,7 @@ def get_occ_viewer(): widgets = app.topLevelWidgets() for wi in widgets: if hasattr(wi, "_menus"): # OCC.Display.SimpleGui.MainWindow - viewer = wi.findChild(qtViewer3d, "qt_viewer_3d") - return viewer + return wi.findChild(qtViewer3d, "qt_viewer_3d") def on_select(shapes): @@ -75,8 +74,7 @@ def also_on_select(shapes): def location_from_vector(x, y, z): trsf = gp_Trsf() trsf.SetTranslation(gp_Vec(x, y, z)) - loc = TopLoc_Location(trsf) - return loc + return TopLoc_Location(trsf) cube = BRepPrimAPI_MakeBox(100, 100, 100).Shape() diff --git a/examples/core_geometry_airfoil.py b/examples/core_geometry_airfoil.py index 664dffc..4c1c895 100644 --- a/examples/core_geometry_airfoil.py +++ b/examples/core_geometry_airfoil.py @@ -49,7 +49,7 @@ def __init__(self, chord, span, profile): def make_shape(self): # 1 - retrieve the data from the UIUC airfoil data page foil_dat_url = ( - "http://m-selig.ae.illinois.edu/ads/coord_seligFmt/%s.dat" % self.profile + f"http://m-selig.ae.illinois.edu/ads/coord_seligFmt/{self.profile}.dat" ) # explicitly tell to not use ssl verification ssl._create_default_https_context = ssl._create_unverified_context diff --git a/examples/core_geometry_geomplate.py b/examples/core_geometry_geomplate.py index 4529466..b624501 100644 --- a/examples/core_geometry_geomplate.py +++ b/examples/core_geometry_geomplate.py @@ -85,22 +85,20 @@ def make_n_sided(edges, points, continuity=GeomAbs_C0): for pt in points: n_sided.Add(pt) n_sided.Build() - face = n_sided.Face() - return face + return n_sided.Face() def make_closed_polygon(*args): poly = BRepBuilderAPI_MakePolygon() for pt in args: - if isinstance(pt, list) or isinstance(pt, tuple): + if isinstance(pt, (list, tuple)): for i in pt: poly.Add(i) else: poly.Add(pt) poly.Build() poly.Close() - result = poly.Wire() - return result + return poly.Wire() def geom_plate(event=None): @@ -111,7 +109,7 @@ def geom_plate(event=None): p4 = gp_Pnt(0, 0, 10) p5 = gp_Pnt(5, 5, 5) poly = make_closed_polygon([p1, p2, p3, p4]) - edges = [i for i in TopologyExplorer(poly).edges()] + edges = list(TopologyExplorer(poly).edges()) face = make_n_sided(edges, [p5]) display.DisplayShape(edges) display.DisplayShape(make_vertex(p5)) @@ -244,7 +242,7 @@ def solve_radius(event=None): for i in (0.1, 0.5, 1.5, 2.0, 3.0, 0.2): rcs = RadiusConstrainedSurface(display, poly, p5, i) rcs.solve() - print("Goal: %s radius: %s" % (i, rcs.curr_radius)) + print(f"Goal: {i} radius: {rcs.curr_radius}") time.sleep(0.1) @@ -267,8 +265,7 @@ def build_geom_plate(edges): plate = GeomPlate_MakeApprox(srf, 0.01, 10, 5, 0.01, 0, GeomAbs_C0) uMin, uMax, vMin, vMax = srf.Bounds() - face = make_face(plate.Surface(), uMin, uMax, vMin, vMax, 1e-6) - return face + return make_face(plate.Surface(), uMin, uMax, vMin, vMax, 1e-6) def build_curve_network(event=None): diff --git a/examples/core_geometry_medial_axis_offset.py b/examples/core_geometry_medial_axis_offset.py index 60d4d0b..04d2114 100644 --- a/examples/core_geometry_medial_axis_offset.py +++ b/examples/core_geometry_medial_axis_offset.py @@ -34,8 +34,7 @@ def boolean_cut(shapeToCutFrom, cuttingShape): cut = BRepAlgoAPI_Cut(shapeToCutFrom, cuttingShape) - shp = cut.Shape() - return shp + return cut.Shape() def make_face_to_contour_from(): @@ -63,8 +62,7 @@ def make_face_to_contour_from(): f = make_face(w1) w2 = make_wire(e7, e8, e9, e10) f2 = make_face(w2) - f3 = boolean_cut(f, f2) - return f3 + return boolean_cut(f, f2) def create_offsets(face, nr_of_counters, distance_between_contours): diff --git a/examples/core_geometry_nurbs_converter.py b/examples/core_geometry_nurbs_converter.py index 441ca82..9c6a710 100644 --- a/examples/core_geometry_nurbs_converter.py +++ b/examples/core_geometry_nurbs_converter.py @@ -34,16 +34,13 @@ expl = TopologyExplorer(converted_shape) -# loop over faces -fc_idx = 1 - -for face in expl.faces(): +for fc_idx, face in enumerate(expl.faces(), start=1): print("=== Face %i ===" % fc_idx) surf = BRepAdaptor_Surface(face, True) surf_type = surf.GetType() # check each of the is a BSpline surface # it should be, since we used the nurbs converter before - if not surf_type == GeomAbs_BSplineSurface: + if surf_type != GeomAbs_BSplineSurface: raise AssertionError("the face was not converted to a GeomAbs_BSplineSurface") # get the nurbs bsrf = surf.BSpline() @@ -78,4 +75,3 @@ p = poles.Value(i + 1, j + 1) print(p.X(), p.Y(), p.Z(), end=" ") print() - fc_idx += 1 diff --git a/examples/core_geometry_oriented_bounding_box.py b/examples/core_geometry_oriented_bounding_box.py index 8ce5ab9..541ef9c 100644 --- a/examples/core_geometry_oriented_bounding_box.py +++ b/examples/core_geometry_oriented_bounding_box.py @@ -48,8 +48,9 @@ def convert_bnd_to_shape(the_box): axes.SetLocation( gp_Pnt(point.XYZ() - x_vec * half_x - y_vec * half_y - z_vec * half_z) ) - box = BRepPrimAPI_MakeBox(axes, 2.0 * half_x, 2.0 * half_y, 2.0 * half_z).Shape() - return box + return BRepPrimAPI_MakeBox( + axes, 2.0 * half_x, 2.0 * half_y, 2.0 * half_z + ).Shape() # compute the oriented bounding box of a point cloud diff --git a/examples/core_geometry_quaternion.py b/examples/core_geometry_quaternion.py index e2e95de..e0b01cb 100644 --- a/examples/core_geometry_quaternion.py +++ b/examples/core_geometry_quaternion.py @@ -78,9 +78,7 @@ def frange(start, end=None, inc=None): L = [] while 1: next_ = start + len(L) * inc - if inc > 0 and next_ >= end: - break - elif inc < 0 and next_ <= end: + if inc > 0 and next_ >= end or inc < 0 and next_ <= end: break L.append(next_) diff --git a/examples/core_geometry_recognize_feature.py b/examples/core_geometry_recognize_feature.py index 4ec1035..cc2daa8 100644 --- a/examples/core_geometry_recognize_feature.py +++ b/examples/core_geometry_recognize_feature.py @@ -44,7 +44,7 @@ def recognize_face(a_face): if a plane, returns the normal if a cylinder, returns the radius """ - if not type(a_face) is TopoDS_Face: + if type(a_face) is not TopoDS_Face: print("Please hit the 'G' key to switch to face selection mode") return False surf = BRepAdaptor_Surface(a_face, True) diff --git a/examples/core_geometry_splinecage.py b/examples/core_geometry_splinecage.py index 563bd6f..e5eb433 100644 --- a/examples/core_geometry_splinecage.py +++ b/examples/core_geometry_splinecage.py @@ -42,10 +42,12 @@ def random_color(): def length_from_edge(edg): curve_adapt = BRepAdaptor_Curve(edg) - length = GCPnts_AbscissaPoint().Length( - curve_adapt, curve_adapt.FirstParameter(), curve_adapt.LastParameter(), 1e-6 + return GCPnts_AbscissaPoint().Length( + curve_adapt, + curve_adapt.FirstParameter(), + curve_adapt.LastParameter(), + 1e-6, ) - return length def divide_edge_by_nr_of_points(edg, n_pts): @@ -105,7 +107,7 @@ def build_curve_network(event=None, enforce_tangency=True): root_compound_shape = read_step_file("../assets/models/splinecage.stp") topology_explorer = TopologyExplorer(root_compound_shape) - tangent_constraint_faces = [f for f in topology_explorer.faces()] + tangent_constraint_faces = list(topology_explorer.faces()) # loop through the imported faces # associate the length of each of the faces edges to the corresponding face @@ -115,12 +117,11 @@ def build_curve_network(event=None, enforce_tangency=True): # loop through the imported curves, avoiding the imported faces # when we've got these filtered out, we retrieved the geometry to build the surface from - filtered_edges = [ - e - for e in topology_explorer._loop_topo( + filtered_edges = list( + topology_explorer._loop_topo( TopAbs_EDGE, root_compound_shape, TopAbs_FACE ) - ] + ) filtered_length = {} for e in filtered_edges: diff --git a/examples/core_geometry_surface_from_curves.py b/examples/core_geometry_surface_from_curves.py index b462626..1221e31 100644 --- a/examples/core_geometry_surface_from_curves.py +++ b/examples/core_geometry_surface_from_curves.py @@ -41,8 +41,7 @@ def surface_from_curves(): @param display: """ # First spline - array = [] - array.append(gp_Pnt(-4, 0, 2)) + array = [gp_Pnt(-4, 0, 2)] array.append(gp_Pnt(-7, 2, 2)) array.append(gp_Pnt(-6, 3, 1)) array.append(gp_Pnt(-4, 3, -1)) @@ -52,8 +51,7 @@ def surface_from_curves(): SPL1 = GeomAPI_PointsToBSpline(pt_list1).Curve() # Second spline - a2 = [] - a2.append(gp_Pnt(-4, 0, 2)) + a2 = [gp_Pnt(-4, 0, 2)] a2.append(gp_Pnt(-2, 2, 0)) a2.append(gp_Pnt(2, 3, -1)) a2.append(gp_Pnt(3, 7, -2)) diff --git a/examples/core_mesh_gmsh.py b/examples/core_mesh_gmsh.py index 1bf5567..d1f0ac5 100644 --- a/examples/core_mesh_gmsh.py +++ b/examples/core_mesh_gmsh.py @@ -44,18 +44,14 @@ def mesh_shape(a_topods_shape): a() = ShapeFromFile("shape.brep"); """ - gmsh_geo_file = open("shape.geo", "w") - gmsh_geo_file.write(gmsh_geo_file_content) - gmsh_geo_file.close() - + with open("shape.geo", "w") as gmsh_geo_file: + gmsh_geo_file.write(gmsh_geo_file_content) # call gmsh gmsh_success = os.system("gmsh shape.geo -2 -o shape.stl -format stl") - # load the stl file if gmsh_success != 0 and os.path.isfile("shape.stl"): return read_stl_file("shape.stl") - else: - print("Be sure gmsh is in your PATH") - sys.exit() + print("Be sure gmsh is in your PATH") + sys.exit() # First example, a simple torus diff --git a/examples/core_modeling_bool_demo.py b/examples/core_modeling_bool_demo.py index 4108cf8..b8dcb4e 100644 --- a/examples/core_modeling_bool_demo.py +++ b/examples/core_modeling_bool_demo.py @@ -49,10 +49,9 @@ def generate_shape(): sphere_radius = 1.0 sphere_angle = atan(0.5) sphere_origin = gp_Ax2(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)) - sphere = BRepPrimAPI_MakeSphere( + return BRepPrimAPI_MakeSphere( sphere_origin, sphere_radius, -sphere_angle, sphere_angle ).Shape() - return sphere def add_feature(base): @@ -63,8 +62,7 @@ def add_feature(base): feature_maker.Init(base, feature_origin) feature_maker.Build() feature_maker.Perform(feature_diameter / 2.0) - shape = feature_maker.Shape() - return shape + return feature_maker.Shape() def boolean_cut(base): @@ -99,8 +97,7 @@ def boolean_fuse(base): ring_radius = 0.25 torus_radius = 1.0 - ring_radius torus = BRepPrimAPI_MakeTorus(torus_radius, ring_radius).Shape() - fuse = BRepAlgoAPI_Fuse(base, torus).Shape() - return fuse + return BRepAlgoAPI_Fuse(base, torus).Shape() def revolved_cut(base): @@ -142,9 +139,7 @@ def revolved_cut(base): move.SetTranslation(gp_Pnt(0, 0, 0), gp_Pnt(0, 0, sin(0.5))) moved_shape = BRepBuilderAPI_Transform(revolved_shape, move, False).Shape() - # Remove the revolved shape - cut = BRepAlgoAPI_Cut(base, moved_shape).Shape() - return cut + return BRepAlgoAPI_Cut(base, moved_shape).Shape() def generate_demo(): @@ -152,8 +147,7 @@ def generate_demo(): featured_shape = add_feature(basic_shape) cut_shape = boolean_cut(featured_shape) fused_shape = boolean_fuse(cut_shape) - revolved_shape = revolved_cut(fused_shape) - return revolved_shape + return revolved_cut(fused_shape) if __name__ == "__main__": diff --git a/examples/core_modeling_sprocket.py b/examples/core_modeling_sprocket.py index b595061..9ea8f4e 100644 --- a/examples/core_modeling_sprocket.py +++ b/examples/core_modeling_sprocket.py @@ -284,9 +284,7 @@ def round_tooth(wedge): # Cut the wedge using the first and second cutting shape cut_1 = BRepAlgoAPI_Cut(wedge, rounding_cut_1).Shape() - cut_2 = BRepAlgoAPI_Cut(cut_1, rounding_cut_2).Shape() - - return cut_2 + return BRepAlgoAPI_Cut(cut_1, rounding_cut_2).Shape() def clone_tooth(base_shape): @@ -373,26 +371,10 @@ def cut_out(base): inter_3 = Geom2dAPI_InterCurveCurve(geom_inner, geom_right) inter_4 = Geom2dAPI_InterCurveCurve(geom_inner, geom_left) - if inter_1.Point(1).X() > 0: - p1 = inter_1.Point(1) - else: - p1 = inter_1.Point(2) - - if inter_2.Point(1).X() > 0: - p2 = inter_2.Point(1) - else: - p2 = inter_2.Point(2) - - if inter_3.Point(1).X() > 0: - p3 = inter_3.Point(1) - else: - p3 = inter_3.Point(2) - - if inter_4.Point(1).X() > 0: - p4 = inter_4.Point(1) - else: - p4 = inter_4.Point(2) - + p1 = inter_1.Point(1) if inter_1.Point(1).X() > 0 else inter_1.Point(2) + p2 = inter_2.Point(1) if inter_2.Point(1).X() > 0 else inter_2.Point(2) + p3 = inter_3.Point(1) if inter_3.Point(1).X() > 0 else inter_3.Point(2) + p4 = inter_4.Point(1) if inter_4.Point(1).X() > 0 else inter_4.Point(2) trimmed_outer = GCE2d_MakeArcOfCircle(outer, p1, p2).Value() trimmed_inner = GCE2d_MakeArcOfCircle(inner, p4, p3).Value() @@ -447,8 +429,7 @@ def build_sprocket(): basic_disk = clone_tooth(rounded_wedge) cut_disc = center_hole(basic_disk) mountable_disc = mounting_holes(cut_disc) - sprocket = cut_out(mountable_disc) - return sprocket + return cut_out(mountable_disc) sprocket_model = build_sprocket() diff --git a/examples/core_parallel_slicer.py b/examples/core_parallel_slicer.py index 9ab616b..aa741d2 100644 --- a/examples/core_parallel_slicer.py +++ b/examples/core_parallel_slicer.py @@ -85,16 +85,16 @@ def get_z_coords_for_n_procs(n_slices, n_procs): for i in range(1, n_procs + 1): if i == 1: slices.append(z_slices[: i * n]) - _str_slices.append(":" + str(i * n) + " ") + _str_slices.append(f":{str(i * n)} ") elif i == n_procs: # does a little extra work if the number of slices # isn't divisible by n_procs slices.append(z_slices[(i - 1) * n :]) - _str_slices.append(str((i - 1) * n) + ": ") + _str_slices.append(f"{str((i - 1) * n)}: ") print("last slice", len(z_slices[(i - 1) * n :])) else: slices.append(z_slices[(i - 1) * n : i * n]) - _str_slices.append(" %s:%s " % ((i - 1) * n, i * n)) + _str_slices.append(f" {(i - 1) * n}:{i * n} ") print( "the z-index array is sliced over %s processors like this: \n %s" % (n_procs, _str_slices) @@ -127,7 +127,7 @@ def arguments(n_slices, n_procs): else: P = multiprocessing.Pool(n_procs) _results = P.map(vectorized_slicer, arguments(n_slice, i)) - print("slicing took %s seconds for %s processors" % (time.time() - tA, i)) + print(f"slicing took {time.time() - tA} seconds for {i} processors") sys.exit() print("\n\n\n done slicing on %i cores \n\n\n" % nprocs) @@ -143,7 +143,7 @@ def arguments(n_slices, n_procs): # update viewer when all is added: display.Repaint() total_time = time.time() - init_time - print("%s necessary to perform slice with %s processor(s)." % (total_time, n_procs)) + print(f"{total_time} necessary to perform slice with {n_procs} processor(s).") start_display() diff --git a/examples/core_shape_properties.py b/examples/core_shape_properties.py index f2da945..5808c97 100644 --- a/examples/core_shape_properties.py +++ b/examples/core_shape_properties.py @@ -37,7 +37,7 @@ def cube_inertia_properties(): cog = props.CentreOfMass() matrix_of_inertia = props.MatrixOfInertia() # Display inertia properties - print("Cube mass = %s" % mass) + print(f"Cube mass = {mass}") cog_x, cog_y, cog_z = cog.Coord() print("Center of mass: x = %f;y = %f;z = %f;" % (cog_x, cog_y, cog_z)) print("Matrix of inertia", matrix_of_inertia) @@ -50,12 +50,10 @@ def shape_faces_surface(): # then loop over faces t = TopologyExplorer(the_shape) props = GProp_GProps() - shp_idx = 1 - for face in t.faces(): + for shp_idx, face in enumerate(t.faces(), start=1): brepgprop_SurfaceProperties(face, props) face_surf = props.Mass() print("Surface for face nbr %i : %f" % (shp_idx, face_surf)) - shp_idx += 1 if __name__ == "__main__": diff --git a/examples/core_tesselation_vertices_list.py b/examples/core_tesselation_vertices_list.py index ff79773..c629df9 100644 --- a/examples/core_tesselation_vertices_list.py +++ b/examples/core_tesselation_vertices_list.py @@ -47,12 +47,12 @@ # get normals normals = tess.GetNormalsAsTuple() number_of_normals = len(normals) -if not number_of_normals == number_of_vertices: +if number_of_normals != number_of_vertices: raise AssertionError("wrong number of normals returned by the tessellator") # if HAVE_NUMPY, we try to reshape the tuple so that it is of # a ndarray such as [[x1, y1, z1], [x2, y2, z2], ...] # if HAVE_NUMPY: - vertices = np.array(vertices_position).reshape(int(number_of_vertices / 3), 3) - normals = np.array(normals).reshape(int(number_of_vertices / 3), 3) + vertices = np.array(vertices_position).reshape(number_of_vertices // 3, 3) + normals = np.array(normals).reshape(number_of_vertices // 3, 3) diff --git a/examples/core_topology_face.py b/examples/core_topology_face.py index 413d8b9..89da955 100644 --- a/examples/core_topology_face.py +++ b/examples/core_topology_face.py @@ -110,7 +110,7 @@ def face(): ## line1 = Geom2d_Line(P12d, gp_Dir2d((0.2 - 0.9), (0.7 - 0.1))) line2 = Geom2d_Line(P22d, gp_Dir2d((0.02 - 0.2), (0.1 - 0.7))) - line3 = Geom2d_Line(P32d, gp_Dir2d((0.9 - 0.02), (0.1 - 0.1))) + line3 = Geom2d_Line(P32d, gp_Dir2d(0.9 - 0.02, 0.0)) ## ##//Edges are on the BSpline surface Edge1 = BRepBuilderAPI_MakeEdge( diff --git a/examples/core_topology_heightmap.py b/examples/core_topology_heightmap.py index 52b6681..38abb11 100644 --- a/examples/core_topology_heightmap.py +++ b/examples/core_topology_heightmap.py @@ -67,25 +67,17 @@ def heightmap_from_equation(f, x_min=-1, x_max=1, y_min=-1, y_max=1): n = 100 # initialize x axis step_x = (x_max - x_min) / n - x_ = [] - for i in range(n): - x_.append(x_min + i * step_x) + x_ = [x_min + i * step_x for i in range(n)] # initialize y axis step_y = (y_max - y_min) / n - y_ = [] - for i in range(n): - y_.append(y_min + i * step_y) + y_ = [y_min + i * step_y for i in range(n)] # compute z array = TColgp_Array2OfPnt(1, len(x_), 1, len(y_)) - i = 1 - for x in x_: - j = 1 - for y in y_: + for i, x in enumerate(x_, start=1): + for j, y in enumerate(y_, start=1): z = f(x, y) point_to_add = gp_Pnt(x, y, z) array.SetValue(i, j, point_to_add) - j += 1 - i += 1 print("bspline surface creation") bspl_surface = GeomAPI_PointsToBSplineSurface( array, 3, 8, GeomAbs_C2, 0.001 @@ -99,8 +91,7 @@ def boundary_curve_from_2_points(p1, p2): w0 = BRepBuilderAPI_MakeWire(e0).Wire() # boundary for filling adap = BRepAdaptor_CompCurve(w0) - boundary = GeomFill_SimpleBound(adap, 1e-6, 1e-6) - return boundary + return GeomFill_SimpleBound(adap, 1e-6, 1e-6) def heightmap_from_image(event=None): diff --git a/examples/core_topology_local_ops.py b/examples/core_topology_local_ops.py index 6331b17..cf949ec 100644 --- a/examples/core_topology_local_ops.py +++ b/examples/core_topology_local_ops.py @@ -127,7 +127,7 @@ def brepfeat_prism(event=None): box = BRepPrimAPI_MakeBox(400, 250, 300).Shape() faces = TopologyExplorer(box).faces() - for i in range(5): + for _ in range(5): face = next(faces) srf = BRep_Tool_Surface(face) diff --git a/examples/core_topology_uv_to_cartesian_coordinates.py b/examples/core_topology_uv_to_cartesian_coordinates.py index 0525c84..593cd9d 100644 --- a/examples/core_topology_uv_to_cartesian_coordinates.py +++ b/examples/core_topology_uv_to_cartesian_coordinates.py @@ -42,8 +42,7 @@ def build_surf(): array.SetValue(1, 2, p4) array.SetValue(2, 2, p5) array.SetValue(3, 2, p6) - bspl_surf = GeomAPI_PointsToBSplineSurface(array, 3, 8, GeomAbs_C2, 0.001).Surface() - return bspl_surf + return GeomAPI_PointsToBSplineSurface(array, 3, 8, GeomAbs_C2, 0.001).Surface() def build_points_network(bspl_srf): diff --git a/examples/core_visualization_glsl.py b/examples/core_visualization_glsl.py index f1cc1d7..f7f498c 100644 --- a/examples/core_visualization_glsl.py +++ b/examples/core_visualization_glsl.py @@ -64,7 +64,7 @@ raise AssertionError("no valid shader program found") # when a shader fails to compile, raise an AssertionError -assert not aspect.ShaderProgram() is None, "no shader program is null" +assert aspect.ShaderProgram() is not None, "no shader program is null" # redisplay the sphere, when the shader was attached to its AIS_Shape aspect display.Context.Redisplay(anIO, True) diff --git a/examples/core_visualization_graphic3d_custom_opengl.py b/examples/core_visualization_graphic3d_custom_opengl.py index a910c23..e1f5feb 100644 --- a/examples/core_visualization_graphic3d_custom_opengl.py +++ b/examples/core_visualization_graphic3d_custom_opengl.py @@ -25,16 +25,13 @@ def generate_points(spread, n): try: import numpy as np - arr = np.random.uniform(-spread / 2.0, spread / 2.0, (n, 3)) - for i in arr: - yield i + yield from np.random.uniform(-spread / 2.0, spread / 2.0, (n, 3)) except ImportError: n_ = n / 100 warnings.warn( - "Numpy could not be imported... this example will run very SLOW" - "drawing {} rather than {} lines".format(n_, n) + f"Numpy could not be imported... this example will run very SLOWdrawing {n_} rather than {n} lines" ) - for i in range(n_): + for _ in range(n_): a = random.uniform(-spread / 2.0, spread / 2.0) b = random.uniform(-spread / 2.0, spread / 2.0) c = random.uniform(-spread / 2.0, spread / 2.0) diff --git a/examples/core_visualization_overpaint_viewer.py b/examples/core_visualization_overpaint_viewer.py index af57d24..13cc137 100644 --- a/examples/core_visualization_overpaint_viewer.py +++ b/examples/core_visualization_overpaint_viewer.py @@ -322,40 +322,32 @@ def mouseMoveEvent(self, event): modifiers = event.modifiers() # rotate - if buttons == QtCore.Qt.LeftButton and not modifiers == QtCore.Qt.ShiftModifier: + if ( + buttons == QtCore.Qt.LeftButton + and modifiers != QtCore.Qt.ShiftModifier + ): self.current_action = ON_DYN_ROT - # dynamic zoom elif ( buttons == QtCore.Qt.RightButton - and not modifiers == QtCore.Qt.ShiftModifier + and modifiers != QtCore.Qt.ShiftModifier ): self.current_action = ON_DYN_ZOOM - # dynamic panning elif buttons == QtCore.Qt.MidButton: self.current_action = ON_DYN_PAN - # zoom window, overpaints rectangle - elif buttons == QtCore.Qt.RightButton and modifiers == QtCore.Qt.ShiftModifier: + elif buttons == QtCore.Qt.RightButton: self.current_action = ON_ZOOM_AREA - # select area - elif buttons == QtCore.Qt.LeftButton and modifiers == QtCore.Qt.ShiftModifier: + elif buttons == QtCore.Qt.LeftButton: self.current_action = ON_SELECT_AREA self.update() def wheelEvent(self, event): - if self._have_pyqt5: - delta = event.angleDelta().y() - else: - delta = event.delta() - - if delta > 0: - self.zoom_factor = 1.3 - else: - self.zoom_factor = 0.7 + delta = event.angleDelta().y() if self._have_pyqt5 else event.delta() + self.zoom_factor = 1.3 if delta > 0 else 0.7 self.current_action = ON_ZOOM_FACTOR self.point_on_mouse_move = event @@ -373,10 +365,7 @@ def on_zoom_area(self): dx, dy = self.delta_mouse_event_pos tolerance = 2 - if abs(dx) <= tolerance and abs(dy) <= tolerance: - # zooming at a near nil value can segfault the opengl viewer - pass - else: + if abs(dx) > tolerance or abs(dy) > tolerance: if not self.is_right_mouse_button_surpressed: coords = [ self.point_on_mouse_press[0], @@ -467,8 +456,6 @@ def _dispatch_camera_command_actions(self): False otherwise """ - perform_action = False - if self.current_action: print("handling camera action:", self.current_action) @@ -485,7 +472,7 @@ def _dispatch_camera_command_actions(self): finally: self.current_action = None - return perform_action + return False def paintEvent(self, event): """handles all actions that redraw the viewport @@ -595,10 +582,7 @@ def drawBox(self, painter): dx, dy = self.delta_mouse_event_pos - if abs(dx) <= tolerance and abs(dy) <= tolerance: - pass - - else: + if abs(dx) > tolerance or abs(dy) > tolerance: rect = QtCore.QRect( self.point_on_mouse_press[0], self.point_on_mouse_press[1], -dx, -dy ) diff --git a/examples/core_webgl_threejs_random_toruses.py b/examples/core_webgl_threejs_random_toruses.py index 651f972..58edb8a 100644 --- a/examples/core_webgl_threejs_random_toruses.py +++ b/examples/core_webgl_threejs_random_toruses.py @@ -30,8 +30,7 @@ my_ren = threejs_renderer.ThreejsRenderer() n_toruses = 100 -idx = 0 -for i in range(n_toruses): +for idx, _ in enumerate(range(n_toruses)): torus_shp = BRepPrimAPI_MakeTorus( 10 + random.random() * 10, random.random() * 10 ).Shape() @@ -49,5 +48,4 @@ trans_torus, export_edges=True, color=rnd_color, transparency=random.random() ) print("%i%%" % (idx * 100 / n_toruses), end="") - idx += 1 my_ren.render() diff --git a/examples/core_webgl_x3dom_random_boxes.py b/examples/core_webgl_x3dom_random_boxes.py index 7fde73d..6504878 100644 --- a/examples/core_webgl_x3dom_random_boxes.py +++ b/examples/core_webgl_x3dom_random_boxes.py @@ -27,7 +27,7 @@ my_ren = x3dom_renderer.X3DomRenderer() -for i in range(100): +for _ in range(100): box_shp = BRepPrimAPI_MakeBox( random.random() * 20, random.random() * 20, random.random() * 20 ).Shape() diff --git a/run_examples_as_tests.py b/run_examples_as_tests.py index 446537e..870f411 100644 --- a/run_examples_as_tests.py +++ b/run_examples_as_tests.py @@ -27,7 +27,7 @@ def worker(example_name): # += operation is not atomic, so we need to get a lock: - print("running %s ..." % example_name, end="") + print(f"running {example_name} ...", end="") try: subprocess.check_output( [sys.executable, example_name], @@ -37,7 +37,7 @@ def worker(example_name): print("[passed]") return True except subprocess.CalledProcessError as cpe: - print("%s" % cpe.output) + print(f"{cpe.output}") print("[failed]") return False