Skip to content

Commit a822d4d

Browse files
committed
Get rid of unused imports.
1 parent b128a1e commit a822d4d

File tree

8 files changed

+547
-266
lines changed

8 files changed

+547
-266
lines changed

pyjviz/fstriplestore.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# import ipdb
21
import os
32
import os.path
43
import textwrap

pyjviz/nb_utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import graphviz
22
import IPython.display
3-
import rdflib
43

5-
from . import viz
64

75
def show_method_chain(dot_code):
86
gvz = graphviz.Source(dot_code)
@@ -12,15 +10,15 @@ def show_method_chain(dot_code):
1210
# and note that no temp files in current dirctory are visible
1311
if 1:
1412
print("gvz:", type(gvz))
15-
#print(dir(gvz))
13+
# print(dir(gvz))
1614
IPython.display.display_png(gvz)
1715
else:
18-
gvz.render(format = 'png')
16+
gvz.render(format="png")
1917
print("gvz:", type(gvz))
20-
#print("filename:", gvz.filename)
21-
#image = IPython.display.Image(filename = gvz.filename + '.png')
18+
# print("filename:", gvz.filename)
19+
# image = IPython.display.Image(filename = gvz.filename + '.png')
2220
image = gvz.view()
2321
print("image:", type(image), image[:20])
24-
#scale = 0.3
25-
#image = image.resize(( int(image.width * scale), int(image.height * scale)))
22+
# scale = 0.3
23+
# image = image.resize(( int(image.width * scale), int(image.height * scale)))
2624
IPython.display.display_png(image)

pyjviz/nested_call.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1-
#import ipdb
2-
import sys, contextlib
1+
import sys
32
from . import obj_tracking
43
from . import obj_utils
54
from . import wb_stack
65
from . import fstriplestore
76
from . import rdf_node
87

8+
99
class profile_objs:
1010
def __init__(self):
1111
self.collected_ids = set()
1212

1313
def collect_obj_ids(self, frame, event, arg):
1414
trs = obj_tracking.tracking_store
1515

16-
#print(frame.f_code.co_name)
1716
gs = frame.f_globals
18-
#global_ids = [trs.get_obj_uuid(id(gs.get(x))) for x in frame.f_code.co_names if x in gs]
19-
global_ids = [(trs.get_uuid(id(gs.get(x))), trs.get_last_obj_state_uri(id(gs.get(x)))) for x in frame.f_code.co_names if x in gs]
17+
global_ids = [
18+
(
19+
trs.get_uuid(id(gs.get(x))),
20+
trs.get_last_obj_state_uri(id(gs.get(x))),
21+
)
22+
for x in frame.f_code.co_names
23+
if x in gs
24+
]
2025
self.collected_ids.update(global_ids)
2126

2227
ls = frame.f_locals
23-
#local_ids = [trs.get_obj_uuid(id(ls.get(x))) for x in frame.f_code.co_varnames if x in ls]
24-
local_ids = [(trs.get_uuid(id(ls.get(x))),trs.get_last_obj_state_uri(id(ls.get(x)))) for x in frame.f_code.co_varnames if x in ls]
28+
local_ids = [
29+
(
30+
trs.get_uuid(id(ls.get(x))),
31+
trs.get_last_obj_state_uri(id(ls.get(x))),
32+
)
33+
for x in frame.f_code.co_varnames
34+
if x in ls
35+
]
2536
self.collected_ids.update(local_ids)
2637

2738
sys.setprofile(self.collect_obj_ids)
@@ -39,7 +50,10 @@ def dump_nested_call_refs(self, nested_call_uri):
3950

4051
for _, ref_obj_state_uri in self.collected_ids:
4152
if ref_obj_state_uri:
42-
ts.dump_triple(nested_call_uri, "<nested-call-ref>", ref_obj_state_uri)
53+
ts.dump_triple(
54+
nested_call_uri, "<nested-call-ref>", ref_obj_state_uri
55+
)
56+
4357

4458
class NestedCall(rdf_node.RDFNode):
4559
"""
@@ -50,38 +64,43 @@ class NestedCall(rdf_node.RDFNode):
5064
```
5165
5266
Corresponding NestedCall objects are:
53-
67+
5468
```python
5569
NestedCall(arg_name = 'date_as_obj', arg_func = lambda x: pd.to_datetime(x.date_string))
5670
NestedCall(arg_name = 'description', arg_func = lambda x: x.description.lower)
5771
```
5872
59-
During method handling (`assign` in example above, see MethodCall.handle_start_method_call) the arguments which are isfunction(arg) == True will be converted to NestedCall object.
73+
During method handling (`assign` in example above, see MethodCall.handle_start_method_call) the arguments which are isfunction(arg) == True will be converted to NestedCall object.
6074
The code then proceed and causes controlled call of `nested_call_func` via __call__ implementation. Results are saved as self.ret and later used by MethodCall.handle_end_method_call
6175
"""
76+
6277
def __init__(self, arg_name, arg_func):
63-
super().__init__(rdf_type = "NestedCall", label = f"nested_call({arg_name})")
78+
super().__init__(
79+
rdf_type="NestedCall", label=f"nested_call({arg_name})"
80+
)
6481
rdfl = fstriplestore.triple_store
6582
parent_uri = wb_stack.wb_stack.stack_entries__[-1].uri
6683
rdfl.dump_triple(self.uri, "<part-of>", parent_uri)
67-
68-
#ipdb.set_trace()
84+
85+
# ipdb.set_trace()
6986
self.arg_name = arg_name
7087
self.arg_func = arg_func
7188
self.ret = None
72-
89+
7390
def __call__(self, *args, **kwargs):
7491
ts = fstriplestore.triple_store
7592
print("NestedCall called")
7693

7794
ctx = profile_objs()
78-
#ctx = contextlib.nullcontext()
95+
# ctx = contextlib.nullcontext()
7996
with ctx:
8097
self.ret = self.arg_func(*args, **kwargs)
8198

8299
ctx.dump_nested_call_refs(self.uri)
83100

84-
ret_t_obj, obj_found = obj_tracking.tracking_store.get_tracking_obj(self.ret)
101+
ret_t_obj, obj_found = obj_tracking.tracking_store.get_tracking_obj(
102+
self.ret
103+
)
85104
if not obj_found:
86105
ret_t_obj = obj_utils.dump_obj_state(self.ret)
87106
return self.ret

pyjviz/obj_tracking.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,44 @@
22
import uuid
33
from . import fstriplestore
44

5+
56
def obj_del_cb(ref):
67
print("obj deleted", ref)
7-
8+
9+
810
class TrackingObj:
911
def __init__(self, obj):
1012
self.obj_wref = weakref.ref(obj, obj_del_cb)
1113
self.uuid = uuid.uuid4()
1214
self.pyid = id(obj)
1315
self.last_version_num = 0
1416
self.last_obj_state_uri = None
15-
17+
1618
self.uri = f"<Obj#{self.uuid}>"
1719

1820
fstriplestore.triple_store.dump_triple(self.uri, "rdf:type", "<Obj>")
19-
fstriplestore.triple_store.dump_triple(self.uri, "<obj-type>", f'"{type(obj).__name__}"')
20-
fstriplestore.triple_store.dump_triple(self.uri, "<obj-uuid>", f'"{self.uuid}"')
21-
fstriplestore.triple_store.dump_triple(self.uri, "<obj-pyid>", f'{self.pyid}')
22-
21+
fstriplestore.triple_store.dump_triple(
22+
self.uri, "<obj-type>", f'"{type(obj).__name__}"'
23+
)
24+
fstriplestore.triple_store.dump_triple(
25+
self.uri, "<obj-uuid>", f'"{self.uuid}"'
26+
)
27+
fstriplestore.triple_store.dump_triple(
28+
self.uri, "<obj-pyid>", f"{self.pyid}"
29+
)
30+
2331
def is_alive(self):
2432
return not self.obj_wref() is None
2533

2634
def incr_version(self):
2735
ret = self.last_version_num
2836
self.last_version_num += 1
2937
return ret
30-
38+
39+
3140
class TrackingStore:
3241
def __init__(self):
33-
self.tracking_objs = {} # id(obj) -> TrackingObj
42+
self.tracking_objs = {} # id(obj) -> TrackingObj
3443

3544
def get_uuid(self, obj_pyid):
3645
t_obj = self.tracking_objs.get(obj_pyid)
@@ -39,12 +48,12 @@ def get_uuid(self, obj_pyid):
3948
def get_last_obj_state_uri(self, obj_pyid):
4049
t_obj = self.tracking_objs.get(obj_pyid)
4150
return t_obj.last_obj_state_uri if t_obj and t_obj.is_alive() else None
42-
51+
4352
def find_tracking_obj(self, obj):
44-
t_obj, obj_found = self.get_tracking_obj(obj, add_missing = False)
53+
t_obj, obj_found = self.get_tracking_obj(obj, add_missing=False)
4554
return t_obj
46-
47-
def get_tracking_obj(self, obj, add_missing = True):
55+
56+
def get_tracking_obj(self, obj, add_missing=True):
4857
obj_found = False
4958
obj_pyid = id(obj)
5059
tracking_obj = None
@@ -59,4 +68,5 @@ def get_tracking_obj(self, obj, add_missing = True):
5968

6069
return tracking_obj, obj_found
6170

71+
6272
tracking_store = TrackingStore()

pyjviz/obj_utils.py

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,106 @@
1-
#import ipdb
21
import textwrap
32
import pandas as pd
4-
import io, base64
3+
import io
4+
import base64
55

66
from . import fstriplestore
77
from . import obj_tracking
88
from . import wb_stack
99

1010
random_id = 0
11+
12+
1113
def dump_obj_state(obj):
1214
caller_stack_entry = wb_stack.wb_stack.get_parent_of_current_entry()
13-
stack_entry = wb_stack.wb_stack.get_top()
15+
stack_entry = wb_stack.wb_stack.get_top()
1416
t_obj, obj_found = obj_tracking.tracking_store.get_tracking_obj(obj)
1517

1618
global random_id
17-
obj_state_uri = f"<ObjState#{random_id}>"; random_id += 1
18-
fstriplestore.triple_store.dump_triple(obj_state_uri, "rdf:type", "<ObjState>")
19+
obj_state_uri = f"<ObjState#{random_id}>"
20+
random_id += 1
21+
fstriplestore.triple_store.dump_triple(
22+
obj_state_uri, "rdf:type", "<ObjState>"
23+
)
1924
fstriplestore.triple_store.dump_triple(obj_state_uri, "<obj>", t_obj.uri)
20-
fstriplestore.triple_store.dump_triple(obj_state_uri, "<part-of>", caller_stack_entry.uri)
21-
fstriplestore.triple_store.dump_triple(obj_state_uri, "<version>", f'"{t_obj.last_version_num}"')
25+
fstriplestore.triple_store.dump_triple(
26+
obj_state_uri, "<part-of>", caller_stack_entry.uri
27+
)
28+
fstriplestore.triple_store.dump_triple(
29+
obj_state_uri, "<version>", f'"{t_obj.last_version_num}"'
30+
)
2231
t_obj.last_obj_state_uri = obj_state_uri
2332
t_obj.last_version_num += 1
2433

25-
dump_obj_state_cc(obj_state_uri, obj, output_type = 'head')
34+
dump_obj_state_cc(obj_state_uri, obj, output_type="head")
2635

2736
return t_obj
28-
29-
def dump_obj_state_cc(obj_state_uri, obj, output_type = 'head'):
37+
38+
39+
def dump_obj_state_cc(obj_state_uri, obj, output_type="head"):
3040
if isinstance(obj, pd.DataFrame):
3141
dump_DataFrame_obj_state_cc(obj_state_uri, obj, output_type)
3242
elif isinstance(obj, pd.Series):
3343
dump_Series_obj_state_cc(obj_state_uri, obj, output_type)
3444
else:
3545
raise Exception(f"unknown obj type at {obj_state_uri}")
3646

47+
3748
def dump_DataFrame_obj_state_cc(obj_state_uri, df, output_type):
3849
ts = fstriplestore.triple_store
3950
global random_id
40-
obj_state_cc_uri = f"<ObjStateCC#{random_id}>"; random_id += 1
51+
obj_state_cc_uri = f"<ObjStateCC#{random_id}>"
52+
random_id += 1
4153

4254
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<ObjStateCC>")
4355
ts.dump_triple(obj_state_cc_uri, "<obj-state>", obj_state_uri)
4456

45-
if output_type == 'head':
57+
if output_type == "head":
4658
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCGlance>")
4759
ts.dump_triple(obj_state_cc_uri, "<shape>", f'"{df.shape}"')
48-
df_head_html = df.head(10).applymap(lambda x: textwrap.shorten(str(x), 50)).to_html().replace("<", "&lt;").replace(">", "&gt;").replace('"', "&quot;").replace("\n", "&#10;")
60+
df_head_html = (
61+
df.head(10)
62+
.applymap(lambda x: textwrap.shorten(str(x), 50))
63+
.to_html()
64+
.replace("<", "&lt;")
65+
.replace(">", "&gt;")
66+
.replace('"', "&quot;")
67+
.replace("\n", "&#10;")
68+
)
4969
ts.dump_triple(obj_state_cc_uri, "<df-head>", '"' + df_head_html + '"')
50-
elif output_type == 'plot':
70+
elif output_type == "plot":
5171
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>")
5272
ts.dump_triple(obj_state_cc_uri, "<shape>", f'"{df.shape}"')
5373
out_fd = io.BytesIO()
5474
fig = df.plot().get_figure()
5575
fig.savefig(out_fd)
56-
#ipdb.set_trace()
57-
im_s = base64.b64encode(out_fd.getvalue()).decode('ascii')
58-
ts.dump_triple(obj_state_cc_uri, '<plot-im>', '"' + im_s + '"')
76+
# ipdb.set_trace()
77+
im_s = base64.b64encode(out_fd.getvalue()).decode("ascii")
78+
ts.dump_triple(obj_state_cc_uri, "<plot-im>", '"' + im_s + '"')
5979
else:
6080
raise Exception(f"unknown output_type: {output_type}")
6181

6282

6383
def dump_Series_obj_state_cc(obj_state_uri, s, output_type):
6484
ts = fstriplestore.triple_store
6585
global random_id
66-
obj_state_cc_uri = f"<ObjStateCC#{random_id}>"; random_id += 1
86+
obj_state_cc_uri = f"<ObjStateCC#{random_id}>"
87+
random_id += 1
6788

6889
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<ObjStateCC>")
6990
ts.dump_triple(obj_state_cc_uri, "<obj-state>", obj_state_uri)
7091

71-
if output_type == 'head':
92+
if output_type == "head":
7293
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCGlance>")
7394
ts.dump_triple(obj_state_cc_uri, "<shape>", f"{len(s)}")
7495
ts.dump_triple(obj_state_cc_uri, "<df-head>", '"NONE"')
75-
elif output_type == 'plot':
76-
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>")
96+
elif output_type == "plot":
97+
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>")
7798
ts.dump_triple(obj_state_cc_uri, "<shape>", f"{len(s)}")
7899
out_fd = io.BytesIO()
79100
fig = s.plot().get_figure()
80101
fig.savefig(out_fd)
81-
#ipdb.set_trace()
82-
im_s = base64.b64encode(out_fd.getvalue()).decode('ascii')
83-
ts.dump_triple(obj_state_cc_uri, '<plot-im>', '"' + im_s + '"')
102+
# ipdb.set_trace()
103+
im_s = base64.b64encode(out_fd.getvalue()).decode("ascii")
104+
ts.dump_triple(obj_state_cc_uri, "<plot-im>", '"' + im_s + '"')
84105
else:
85106
raise Exception(f"unknown output_type: {output_type}")
86-

0 commit comments

Comments
 (0)