Package: get-children-parents-recursively
A function to fetch all children/parents from an object/list of objects recursively$pipinstallget-children-parents-recursivelyfromget_children_parents_recursivelyimportget_all_children_parents_recursivelyimportpsutil#lets use psutil as an exampleitem=psutil.Process(15328)#'chrome.exe'test1=get_all_children_parents_recursively(item,children_parents_method="children",delete_duplicates=False,flatten_all=False,normalize_children_list=False,)test1Out[3]:[{'item':psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21'),'children':[]},{'item':psutil.Process(pid=15328,name='chrome.exe',status='running',started='23:49:19'),'children':[psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21')]}]test2=get_all_children_parents_recursively(item,children_parents_method="children",delete_duplicates=True,flatten_all=False,normalize_children_list=False,)test2Out[4]:[{'item':psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),'children':[]},{'item':psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21'),'children':[]},{'item':psutil.Process(pid=15328,name='chrome.exe',status='running',started='23:49:19'),'children':[psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21')]}]test3=get_all_children_parents_recursively(item,children_parents_method="children",delete_duplicates=True,flatten_all=True,normalize_children_list=False,)test3Out[5]:[psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21'),psutil.Process(pid=15328,name='chrome.exe',status='running',started='23:49:19')]test3=get_all_children_parents_recursively(item,children_parents_method="children",delete_duplicates=False,flatten_all=True,normalize_children_list=False,)test3Out[10]:[psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21'),psutil.Process(pid=15328,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21')]test4=get_all_children_parents_recursively(item,children_parents_method="children",delete_duplicates=True,flatten_all=False,normalize_children_list=True,)test4Out[6]:[{'item':psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),'children':[None,None,None,None,None,None]},{'item':psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),'children':[None,None,None,None,None,None]},{'item':psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),'children':[None,None,None,None,None,None]},{'item':psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),'children':[None,None,None,None,None,None]},{'item':psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),'children':[None,None,None,None,None,None]},{'item':psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21'),'children':[None,None,None,None,None,None]},{'item':psutil.Process(pid=15328,name='chrome.exe',status='running',started='23:49:19'),'children':[psutil.Process(pid=3660,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=9664,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=16444,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=14144,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=18376,name='chrome.exe',status='running',started='23:49:19'),psutil.Process(pid=7160,name='chrome.exe',status='running',started='23:49:21')]}]#useful for pandasfroma_pandas_ex_plode_toolimportpd_add_explode_toolsimportpandasaspdpd_add_explode_tools()df=pd.DataFrame(test4)df=pd.concat([df.item,df.children.s_explode_lists_and_tuples()],axis=1)dfOut[7]:item...children_50psutil.Process(pid=3660,name='chrome.exe',st......None1psutil.Process(pid=9664,name='chrome.exe',st......None2psutil.Process(pid=16444,name='chrome.exe',s......None3psutil.Process(pid=14144,name='chrome.exe',s......None4psutil.Process(pid=18376,name='chrome.exe',s......None5psutil.Process(pid=7160,name='chrome.exe',st......None6psutil.Process(pid=15328,name='chrome.exe',s......psutil.Process(pid=7160,name='chrome.exe',st...importrequestsimportbs4soup=bs4.BeautifulSoup(requests.get("http://www.github.com").content,"lxml")test5=get_all_children_parents_recursively(soup.find_all("body")[0],children_parents_method="find_parents",delete_duplicates=True,flatten_all=False,normalize_children_list=False,)item:<!DOCTYPEhtml><htmldata-a11y-animated-images="sfind_parents:[]item:<htmldata-a11y-animated-images="system"lang="en"find_parents:[<!DOCTYPEhtml><htmldata-a11y-animated-images="item:<bodyclass="logged-out env-production page-responfind_parents:[<htmldata-a11y-animated-images="system"lang="enParameters:item:Anyanyobjectthatcanberecursivelyiteratechildren_parents_method:strnameofthemethodthatgetsthechildren/parentsitem(default="children")delete_duplicates:bool=TrueUsefulifyouuseflatten_all=Truedefault=True)flatten_all:bool=FalseReturnsallitemsasaflattenedlistdefault=False)normalize_children_list:bool=FalseUsefulifyoudon't want to flatten the output and want the same length foreverychildrenlist(e.g.forcreatingaDataFrame)(default=False)Returns:readyitems:list