Skip to content

Commit 83de865

Browse files
committed
Black
1 parent bb39748 commit 83de865

File tree

3 files changed

+74
-52
lines changed

3 files changed

+74
-52
lines changed

ifcgraph.py

+33-37
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77

88
class IfcEntity:
9-
def __init__(self, data):
10-
self.ifctype = data['ifc_type']
11-
self.id = data['id']
12-
13-
def __str__(self):
14-
return "#" + str(self.id) + "_" + self.ifctype
15-
__repr__ = __str__
9+
def __init__(self, data):
10+
self.ifctype = data["ifc_type"]
11+
self.id = data["id"]
1612

13+
def __str__(self):
14+
return "#" + str(self.id) + "_" + self.ifctype
1715

16+
__repr__ = __str__
1817

19-
def get_cmap(n, name='hsv'):
20-
'''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
21-
RGB color; the keyword argument name must be a standard mpl colormap name.'''
18+
19+
def get_cmap(n, name="hsv"):
20+
"""Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
21+
RGB color; the keyword argument name must be a standard mpl colormap name."""
2222
return plt.cm.get_cmap(name, n)
2323

2424

@@ -32,14 +32,14 @@ def get_cmap(n, name='hsv'):
3232
types_color = {}
3333

3434
for e in ents.values():
35-
for r in e['attributes'][1]:
36-
G.add_edge(e['id'], r)
37-
color_map.append('red')
38-
ifctypes.add(e['ifc_type'])
39-
ifctypes.add(ents[r]['ifc_type'])
40-
35+
for r in e["attributes"][1]:
36+
G.add_edge(e["id"], r)
37+
color_map.append("red")
38+
ifctypes.add(e["ifc_type"])
39+
ifctypes.add(ents[r]["ifc_type"])
40+
4141

42-
cmap = get_cmap(len(ifctypes),'rainbow')
42+
cmap = get_cmap(len(ifctypes), "rainbow")
4343

4444
colors = [cmap(i) for i in range(len(ifctypes))]
4545

@@ -49,14 +49,14 @@ def get_cmap(n, name='hsv'):
4949

5050
i = 0
5151
for eid in G.nodes():
52-
t = ents[eid]['ifc_type']
53-
# print(t)
54-
if t in type_color_mapping.keys():
55-
cols.append(type_color_mapping[t])
56-
else:
57-
type_color_mapping[t] = colors[i]
58-
i = i + 1
59-
cols.append(type_color_mapping[t])
52+
t = ents[eid]["ifc_type"]
53+
# print(t)
54+
if t in type_color_mapping.keys():
55+
cols.append(type_color_mapping[t])
56+
else:
57+
type_color_mapping[t] = colors[i]
58+
i = i + 1
59+
cols.append(type_color_mapping[t])
6060

6161

6262
# k controls the distance between the nodes and varies between 0 and 1
@@ -70,27 +70,23 @@ def get_cmap(n, name='hsv'):
7070

7171
c_map = [cmap(i) for i in range(len(nodes))]
7272

73-
nc = nx.draw_networkx_nodes(G, pos,nodelist=nodes, node_color=cols,node_size=200)
73+
nc = nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=cols, node_size=200)
7474

7575
# edges
76-
elarge = [(u, v) for (u, v, d) in G.edges(data=True) ]
77-
nx.draw_networkx_edges(G, pos,edgelist=elarge, width=1)
76+
elarge = [(u, v) for (u, v, d) in G.edges(data=True)]
77+
nx.draw_networkx_edges(G, pos, edgelist=elarge, width=1)
7878

7979
nx.draw_networkx_labels(G, pos, font_size=8, font_family="sans-serif")
8080

8181

82-
red_patch = mpatches.Patch(color='red', label='The red data')
83-
blue_patch = mpatches.Patch(color='blue', label='The blue data')
82+
red_patch = mpatches.Patch(color="red", label="The red data")
83+
blue_patch = mpatches.Patch(color="blue", label="The blue data")
8484

8585
patches = []
86-
for k,v in type_color_mapping.items():
87-
patches.append(mpatches.Patch(color=v, label=k))
86+
for k, v in type_color_mapping.items():
87+
patches.append(mpatches.Patch(color=v, label=k))
8888

8989

90-
plt.legend(handles=patches,fontsize=8)
90+
plt.legend(handles=patches, fontsize=8)
9191
plt.axis("off")
9292
plt.show()
93-
94-
95-
96-

main.py

+36-13
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ def __init__(self, filecontent, exception):
2828

2929
def asdict(self, with_message=True):
3030
return {
31-
"type": "unexpected_token"
32-
if isinstance(self.exception, UnexpectedToken)
33-
else "unexpected_character",
31+
"type": (
32+
"unexpected_token"
33+
if isinstance(self.exception, UnexpectedToken)
34+
else "unexpected_character"
35+
),
3436
"lineno": self.exception.line,
3537
"column": self.exception.column,
3638
"found_type": self.exception.token.type.lower(),
@@ -244,16 +246,19 @@ def enumeration(self, s):
244246

245247
@dataclass
246248
class entity_instance:
247-
id : int
248-
type : str
249+
id: int
250+
type: str
249251
attributes: tuple
250252
lines: tuple
253+
251254
def __getitem__(self, k):
252255
# compatibility with dict
253256
return getattr(self, k)
257+
254258
def __repr__(self):
255259
return f'#{self.id}={self.type}({",".join(map(str, self.attributes))})'
256260

261+
257262
def create_step_entity(entity_tree):
258263
entity = {}
259264
t = T(visit_tokens=True).transform(entity_tree)
@@ -317,7 +322,14 @@ def make_header_ent(ast):
317322
return ents
318323

319324

320-
def parse(*, filename=None, filecontent=None, with_progress=False, with_tree=True, with_header=False):
325+
def parse(
326+
*,
327+
filename=None,
328+
filecontent=None,
329+
with_progress=False,
330+
with_tree=True,
331+
with_header=False,
332+
):
321333
if filename:
322334
assert not filecontent
323335
filecontent = builtins.open(filename, encoding=None).read()
@@ -384,13 +396,14 @@ class file:
384396
"""
385397
A somewhat compatible interface (but very limited) to ifcopenshell.file
386398
"""
399+
387400
def __init__(self, parse_outcomes):
388401
self.header_, self.data_ = parse_outcomes
389-
402+
390403
@property
391404
def schema_identifier(self) -> str:
392-
return self.header_['FILE_SCHEMA'][0][0]
393-
405+
return self.header_["FILE_SCHEMA"][0][0]
406+
394407
@property
395408
def schema(self) -> str:
396409
"""General IFC schema version: IFC2X3, IFC4, IFC4X3."""
@@ -403,7 +416,10 @@ def schema(self) -> str:
403416
((p, match.group(p)) for p in prefixes),
404417
)
405418
)
406-
return "".join("".join(map(str, t)) if t[1] else "" for t in zip(prefixes, version_tuple[0:2]))
419+
return "".join(
420+
"".join(map(str, t)) if t[1] else ""
421+
for t in zip(prefixes, version_tuple[0:2])
422+
)
407423

408424
@property
409425
def schema_version(self) -> tuple[int, int, int, int]:
@@ -417,14 +433,14 @@ def schema_version(self) -> tuple[int, int, int, int]:
417433
number = re.search(prefix + r"(\d)", schema)
418434
version.append(int(number.group(1)) if number else 0)
419435
return tuple(version)
420-
436+
421437
@property
422438
def header(self):
423439
return types.SimpleNamespace(**{k.lower(): v for k, v in self.header_.items()})
424440

425441
def __getitem__(self, key: numbers.Integral) -> entity_instance:
426442
return self.by_id(key)
427-
443+
428444
def by_id(self, id: int) -> entity_instance:
429445
"""Return an IFC entity instance filtered by IFC ID.
430446
@@ -447,11 +463,18 @@ def by_type(self, type: str) -> list[entity_instance]:
447463
:rtype: list[entity_instance]
448464
"""
449465
type_lc = type.lower()
450-
return list(filter(lambda ent: ent.type.lower() == type_lc, itertools.chain.from_iterable(self.data_.values())))
466+
return list(
467+
filter(
468+
lambda ent: ent.type.lower() == type_lc,
469+
itertools.chain.from_iterable(self.data_.values()),
470+
)
471+
)
472+
451473

452474
def open(fn) -> file:
453475
return file(parse(filename=fn, with_tree=True, with_header=True))
454476

477+
455478
if __name__ == "__main__":
456479
args = [x for x in sys.argv[1:] if not x.startswith("-")]
457480
flags = [x for x in sys.argv[1:] if x.startswith("-")]

test_parser.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
from main import parse, ValidationError
55
from contextlib import nullcontext
66

7+
78
def create_context(fn):
89
if "fail_" in fn:
910
return pytest.raises(ValidationError)
1011
else:
1112
return nullcontext()
1213

14+
1315
@pytest.mark.parametrize("file", glob.glob("fixtures/*.ifc"))
1416
def test_file_with_tree(file):
15-
with create_context(file):
17+
with create_context(file):
1618
parse(filename=file, with_tree=True)
1719

20+
1821
@pytest.mark.parametrize("file", glob.glob("fixtures/*.ifc"))
1922
def test_file_without_tree(file):
20-
with create_context(file):
23+
with create_context(file):
2124
parse(filename=file, with_tree=False)

0 commit comments

Comments
 (0)