Finding the name of an object's source file
Friedrich Rentsch
anthra.norell at bluewin.ch
Tue Jun 6 10:48:49 EDT 2017
On 06/06/2017 03:52 PM, Matt Wheeler wrote:
> On Tue, 6 Jun 2017 at 11:20 Peter Otten <__peter__ at web.de> wrote:
>
>>>>> import os
>>>>> inspect.getsourcefile(os.path.split)
>> '/usr/lib/python3.4/posixpath.py'
>>
>> And so much more fun than scanning the documentation :)
>>
> Alternatively, without using inspect, we can get around `Object.__module__`
> being a string by importing it as a string:
>
>>>> import importlib, os
>>>> importlib.import_module(os.path.split.__module__).__file__
> '/Users/matt/.pyenv/versions/3.6.0/lib/python3.6/posixpath.py'
Stupendous! Thanks both of you. I tried the Peter's inspect-based method
on a hierarchical assembly of objects:
>>> def sources (S, indent = 0):
print indent * '\t' + '%-60s%s' % (S.__class__,
inspect.getsourcefile (S.__class__))
if isinstance (S, WS._Association):
for s in S:
sources (s, indent + 1)
>>> sources (M[1][1])
<class 'workshops.Chain'> /home/fr/python/util/workshops.py
<class 'workshops.Merger'> /home/fr/python/util/workshops.py
<class 'position.TF_Position_Journalizer'>
/home/fr/python/finance/position.py
positions_initializer.Positions_Initializer
/home/fr/python/finance/positions_initializer.py
position.Position_Activity /home/fr/python/finance/position.py
position.normalizer /home/fr/python/finance/position.py
position.split_adjuster /home/fr/python/finance/position.py
position.Journalizer /home/fr/python/finance/position.py
current_work.report_unmerger /home/fr/temp/current_work.py
<class 'position.Report_Writer'> /home/fr/python/finance/position.py
position.report_name_sender /home/fr/python/finance/position.py
position.Summary_Report /home/fr/python/finance/position.py
workshops.File_Writer /home/fr/python/util/workshops.py
Wonderful! Awesome! Matt's solution works well too.
Thanks a million
Frederic
More information about the Python-list
mailing list