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
Metadata
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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| get_children_parents_recursively-0.10.tar.gz | 6.8 kB | Without provenance | Details for get_children_parents_recursively-0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| get_children_parents_recursively-0.10-py3-none-any.whl | Python 3 | none | any | Details for get_children_parents_recursively-0.10-py3-none-any.whl |
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
|
Security for get-children-parents-recursively 0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.