Skip to main content

A function to fetch all children/parents from an object/list of objects recursively

$pip install get-children-parents-recursively

from get_children_parents_recursively import get_all_children_parents_recursively

import psutil #lets use psutil as an example



item = 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,

)



test1

Out[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,

)



test2

Out[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,

)



test3

Out[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,

)

test3

Out[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,

)







test4

Out[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 pandas

   from a_pandas_ex_plode_tool import pd_add_explode_tools

	import pandas as pd

	pd_add_explode_tools()

	df = pd.DataFrame(test4)

	df = pd.concat([df.item, df.children.s_explode_lists_and_tuples()], axis=1)

   df

	Out[7]:

													item  ...                                         children_5

	0  psutil.Process(pid=3660, name='chrome.exe', st...  ...                                               None

	1  psutil.Process(pid=9664, name='chrome.exe', st...  ...                                               None

	2  psutil.Process(pid=16444, name='chrome.exe', s...  ...                                               None

	3  psutil.Process(pid=14144, name='chrome.exe', s...  ...                                               None

	4  psutil.Process(pid=18376, name='chrome.exe', s...  ...                                               None

	5  psutil.Process(pid=7160, name='chrome.exe', st...  ...                                               None

	6  psutil.Process(pid=15328, name='chrome.exe', s...  ...  psutil.Process(pid=7160, name='chrome.exe', st...





import requests

import bs4

soup = 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        :  <!DOCTYPE html> <html data-a11y-animated-images="s

find_parents:  []

item        :  <html data-a11y-animated-images="system" lang="en"

find_parents:  [<!DOCTYPE html> <html data-a11y-animated-images="

item        :  <body class="logged-out env-production page-respon

find_parents:  [<html data-a11y-animated-images="system" lang="en



	Parameters:

		item:Any

			any object that can be recursively iterate

		children_parents_method:str

			name of the method that gets the children/parents item

			(default="children")

		delete_duplicates:bool=True

			Useful if you use flatten_all=True

			default=True)



		flatten_all:bool=False

			Returns all items as a flattened list

			default=False)



		normalize_children_list:bool=False

			Useful if you don't want to flatten the output and want the same length for

			every children list (e.g. for creating a DataFrame)

			(default=False)

	Returns:

		readyitems:list

Release files for get-children-parents-recursively 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for get-children-parents-recursively 0.10
File Size Uploaded
get_children_parents_recursively-0.10.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for get-children-parents-recursively 0.10
File Interpreter ABI Platform
get_children_parents_recursively-0.10-py3-none-any.whl Python 3 none any Details

Total release size: 15.4 kB

Release files / get_children_parents_recursively-0.10.tar.gz

Download URL get_children_parents_recursively-0.10.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
912f18ca3021ba1be1a95dea101994e7730d97b74c33624bbc987d0af754df16
BLAKE2b-256 checksum
How to use checksums
7d7ee65381d24047684c993188426bb4ed046413579b4b80294e716e5342d369
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / get_children_parents_recursively-0.10-py3-none-any.whl

Download URL get_children_parents_recursively-0.10-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
af373f540e4d0b9d525b65292a84b1de950246fc3cd9f174fa2917b102069664
BLAKE2b-256 checksum
How to use checksums
8174039d23749c268e8a7c1695a22b6d6a632eed00f47c4a654781d14ad76db2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page