Skip to content

Commit f285fb6

Browse files
author
Robin Oval
committed
check types with collections
1 parent 8e9776e commit f285fb6

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/compas/datastructures/mesh/_mesh.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pickle
66
import json
7+
import collections
78

89
from copy import deepcopy
910
from ast import literal_eval
@@ -644,17 +645,17 @@ def from_vertices_and_faces(cls, vertices, faces):
644645
"""
645646
mesh = cls()
646647

647-
if type(vertices) == list:
648+
if isinstance(vertices, collections.MutableSequence):
648649
for x, y, z in iter(vertices):
649650
mesh.add_vertex(x=x, y=y, z=z)
650-
elif type(vertices) == dict:
651+
elif isinstance(vertices, collections.Mapping):
651652
for key, xyz in vertices.items():
652653
mesh.add_vertex(key = key, attr_dict = {i: j for i, j in zip(['x', 'y', 'z'], xyz)})
653654

654-
if type(faces) == list:
655+
if isinstance(faces, collections.MutableSequence):
655656
for face in iter(faces):
656657
mesh.add_face(face)
657-
elif type(faces) == dict:
658+
elif isinstance(faces, collections.Mapping):
658659
for fkey, vertices in faces.items():
659660
mesh.add_face(vertices, fkey)
660661

@@ -3178,19 +3179,19 @@ def plot(self,
31783179

31793180
# # mesh.add_face([a, b, c, d, e, f, g, h])
31803181

3181-
for k in mesh.faces():
3182-
print(k, mesh.is_face_on_boundary(k))
3182+
# for k in mesh.faces():
3183+
# print(k, mesh.is_face_on_boundary(k))
31833184

31843185

3185-
print(list(mesh.edges(True)))
3186+
# print(list(mesh.edges(True)))
31863187

31873188

3188-
plotter = MeshPlotter(mesh)
3189+
# plotter = MeshPlotter(mesh)
31893190

3190-
plotter.draw_vertices()
3191-
plotter.draw_edges()
3192-
plotter.draw_faces(text='key')
3193-
plotter.show()
3191+
# plotter.draw_vertices()
3192+
# plotter.draw_edges()
3193+
# plotter.draw_faces(text='key')
3194+
# plotter.show()
31943195

31953196
# print(mesh.get_vertices_attribute('x'))
31963197
# print(mesh.get_vertices_attributes('xy'))
@@ -3213,8 +3214,27 @@ def plot(self,
32133214
mesh = Mesh.from_vertices_and_faces(vertices, faces)
32143215

32153216
plotter = MeshPlotter(mesh)
3216-
plotter.draw_vertices()
3217+
plotter.draw_vertices(text='key')
32173218
plotter.draw_edges()
32183219
plotter.draw_faces(text='key')
32193220
plotter.show()
32203221

3222+
vertices = [
3223+
[0, 0, 0],
3224+
[1, 1, 0],
3225+
[1, -1, 0],
3226+
[-1, -1, 0],
3227+
[-1, 1, 0]
3228+
]
3229+
faces = [
3230+
[0, 2, 1],
3231+
[0, 4, 3]
3232+
]
3233+
3234+
mesh = Mesh.from_vertices_and_faces(vertices, faces)
3235+
3236+
plotter = MeshPlotter(mesh)
3237+
plotter.draw_vertices(text='key')
3238+
plotter.draw_edges()
3239+
plotter.draw_faces(text='key')
3240+
plotter.show()

src/compas/datastructures/network/_network.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pickle
66
import pprint
77
import json
8+
import collections
89

910
from copy import deepcopy
1011
from ast import literal_eval
@@ -462,10 +463,10 @@ def from_vertices_and_edges(cls, vertices, edges):
462463
"""
463464
network = cls()
464465

465-
if type(vertices) == list:
466+
if isinstance(vertices, collections.MutableSequence):
466467
for x, y, z in vertices:
467468
network.add_vertex(x=x, y=y, z=z)
468-
if type(vertices) == dict:
469+
elif isinstance(vertices, collections.Mapping):
469470
for key, xyz in vertices.items():
470471
network.add_vertex(key = key, attr_dict = {i: j for i, j in zip(['x', 'y', 'z'], xyz)})
471472

@@ -1356,3 +1357,16 @@ def plot(self,
13561357
plotter.draw_vertices(text='key', radius=0.2)
13571358
plotter.draw_edges()
13581359
plotter.show()
1360+
1361+
vertices = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]]
1362+
edges = [(0, 1), (1, 2)]
1363+
1364+
network = Network.from_vertices_and_edges(vertices, edges)
1365+
print(network)
1366+
1367+
plotter = NetworkPlotter(network, figsize=(10, 7))
1368+
plotter.defaults['vertex.fontsize'] = 8
1369+
plotter.draw_vertices(text='key', radius=0.2)
1370+
plotter.draw_edges()
1371+
plotter.show()
1372+

0 commit comments

Comments
 (0)