-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
46 lines (35 loc) · 1.54 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def whereis(object, inthe=None):
"""
Print a markdown hyperlink to the source code of `object`.
Parameters
==========
object | python object
The class or function you are looking for.
inthe | string, optional
The kind of place you want to look in: ['source','repo','technotes']
Comments
========
No error handling whatsoever. Not extensively tested.
"""
# Locate the module that contains the desired object, and break its name into pieces:
modulename = object.__module__
pieces = str.split(modulename,'.')
objectname = object.__name__
# Form the URL, and a useful markdown representation of it:
if inthe is None: inthe = 'source'
if inthe == 'source':
URL = 'https://github.com/'+pieces[0]+'/'+pieces[1]+'_'+pieces[2] \
+ '/blob/master/python/'+pieces[0]+'/'+pieces[1]+'/'+pieces[2]+'/'+pieces[3]+'.py'
link = '['+modulename+']('+URL+')'
elif inthe == 'repo':
URL = 'https://github.com/search?l=Python&q=user%3Alsst+'+objectname+'&type=Code'
link = '[searching for `'+objectname+'` in the `lsst` repo]('+URL+')'
elif inthe == 'technotes':
URL = 'https://github.com/search?l=reStructuredText&q=user%3Alsst-dm+'+objectname+'&type=Code'
link = '[searching for `'+objectname+'` in the `lsst-dm` technotes]('+URL+')'
else:
raise ValueError("unrecognized kwarg "+inthe)
from IPython.display import display, Markdown
display(Markdown(link))
print(link)
return