Fetch all children/parents from an object/list of objects recursively!
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file get_children_parents_recursively-0.10.tar.gz.
File metadata
- Download URL: get_children_parents_recursively-0.10.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
912f18ca3021ba1be1a95dea101994e7730d97b74c33624bbc987d0af754df16
|
|
| MD5 |
bde4349de3406f14eaa46aa5998bb9b8
|
|
| BLAKE2b-256 |
7d7ee65381d24047684c993188426bb4ed046413579b4b80294e716e5342d369
|
File details
Details for the file get_children_parents_recursively-0.10-py3-none-any.whl.
File metadata
- Download URL: get_children_parents_recursively-0.10-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af373f540e4d0b9d525b65292a84b1de950246fc3cd9f174fa2917b102069664
|
|
| MD5 |
34c7729ee0e995f72d074a8881fb477e
|
|
| BLAKE2b-256 |
8174039d23749c268e8a7c1695a22b6d6a632eed00f47c4a654781d14ad76db2
|