Flattens everything - lists,tuples,dicts,np,pd... no requirements / MIT
Project description
Install it:
pip install flatten-everything
Import it:
from flatten_everything import flatten_everything, ProtectedDict,ProtectedList,ProtectedTuple,ProtectedSet
Use it:
#Let's create some random data
import numpy as np
from collections import UserDict, OrderedDict
import pandas as pd
a = np.array([2, 3, 4], dtype=np.uint32)
b = np.array([5, 6, 7], dtype=np.uint32)
data = { # https://www.geeksforgeeks.org/converting-nested-json-structures-to-pandas-dataframes/
"company": "XYZ pvt ltd",
"location": "London",
"info": {
"president": "Rakesh Kapoor",
"contacts": {"email": "contact@xyz.com", "tel": "9876543210"},
},
}
data2 = [ # https://www.geeksforgeeks.org/converting-nested-json-structures-to-pandas-dataframes/
{
"id": "001",
"company": "XYZ pvt ltd",
"location": "London",
"info": {
"president": "Rakesh Kapoor",
"contacts": {"email": "contact@xyz.com", "tel": "9876543210"},
},
},
{
"id": "002",
"company": "PQR Associates",
"location": "Abu Dhabi",
"info": {
"president": "Neelam Subramaniyam",
"contacts": {"email": "contact@pqr.com", "tel": "8876443210"},
},
},
]
dataf = pd.json_normalize(data)
dicti2 = {
"name": {"name": "John", "age": "27", "sex": "Male"},
"Peter2": {"name": "Marie", "age": "22", "sex": "Female"},
"sdfsdf": {"name": "Luna", "age": "24", "sex": "Female"},
"Peter": {"name": "Peter", "age": "29", "sex": "Male"},
}
dicti = {
"name": {"name": "John", "age": "27", "sex": "Male"},
"Peter2": {"name": "Marie", "age": "22", "sex": "Female"},
"sdfsdf": {"name": "Luna", "age": "24", "sex": "Female"},
"Peter": {"name": Peter", "age": "29", "sex": "Male"},
}
odi = OrderedDict(dicti2)
ada = UserDict(dicti2)
testlistnotprotected1 = {
"mytest": [
["dd", "xxaa"],
[[[[(data, 111, {33, 44, 22})]]], [data2]],
dataf,
{"blabla": data2},
"stads",
(444, 4),
21.2,
["sda", "sadfrs"],
("bababa", 44, [111, 111, 111]),
dicti,
a,
b,
["colkey"],
odi,
ada,
np.arange(27).reshape((3, 3, 3)),
]
}
flattened1 = list((flatten_everything(testlistnotprotected1)))
#output:
['dd',
'xxaa',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
111,
33,
44,
22,
'001',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
'002',
'PQR Associates',
'Abu Dhabi',
'Neelam Subramaniyam',
'contact@pqr.com',
'8876443210',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
'001',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
'002',
'PQR Associates',
'Abu Dhabi',
'Neelam Subramaniyam',
'contact@pqr.com',
'8876443210',
'stads',
444,
4,
21.2,
'sda',
'sadfrs',
'bababa',
44,
111,
111,
111,
'John',
'27',
'Male',
'Marie',
'22',
'Female',
'Luna',
'24',
'Female',
'Peter',
'29',
'Male',
2,
3,
4,
5,
6,
7,
'colkey',
'John',
'27',
'Male',
'Marie',
'22',
'Female',
'Luna',
'24',
'Female',
'Peter',
'29',
'Male',
'name',
'Peter2',
'sdfsdf',
'Peter',
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26]
#You can also protect lists, dicts, tuples and sets from getting flattened
testlistprotected2 = {
"mytest": [
["dd", "xxaa"],
[[[[(data, 111, {33, 44, 22})]]], [data2]],
dataf,
ProtectedDict({"blabla": data2}),
"stads",
(444, 4),
21.2,
ProtectedList(["sda", "sadfrs"]),
ProtectedTuple(("bababa", 44, [111, 111, 111])),
dicti,
a,
b,
["colkey"],
odi,
ada,
np.arange(27).reshape((3, 3, 3)),
]
}
flattened2 = list((flatten_everything(testlistprotected2)))
#output:
['dd',
'xxaa',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
111,
33,
44,
22,
'001',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
'002',
'PQR Associates',
'Abu Dhabi',
'Neelam Subramaniyam',
'contact@pqr.com',
'8876443210',
'XYZ pvt ltd',
'London',
'Rakesh Kapoor',
'contact@xyz.com',
'9876543210',
{'blabla': [{'id': '001',
'company': 'XYZ pvt ltd',
'location': 'London',
'info': {'president': 'Rakesh Kapoor',
'contacts': {'email': 'contact@xyz.com', 'tel': '9876543210'}}},
{'id': '002',
'company': 'PQR Associates',
'location': 'Abu Dhabi',
'info': {'president': 'Neelam Subramaniyam',
'contacts': {'email': 'contact@pqr.com', 'tel': '8876443210'}}}]},
'stads',
444,
4,
21.2,
['sda', 'sadfrs'],
('bababa', 44, [111, 111, 111]),
'John',
'27',
'Male',
'Marie',
'22',
'Female',
'Luna',
'24',
'Female',
'Peter',
'29',
'Male',
2,
3,
4,
5,
6,
7,
'colkey',
'John',
'27',
'Male',
'Marie',
'22',
'Female',
'Luna',
'24',
'Female',
'Peter',
'29',
'Male',
'name',
'Peter2',
'sdfsdf',
'Peter',
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flatten_everything-0.2.tar.gz
(4.1 kB
view hashes)
Built Distribution
Close
Hashes for flatten_everything-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cb817331b046c0287334e80b4dd70b45585b10dc66f813eafb2a9141db9c57d |
|
MD5 | 0e8d7666c63f0903256db29cb25cc0ef |
|
BLAKE2b-256 | ec94f3a1192c172914f748ae89098e90e12bf817743ebe18372535b0c5fd6b42 |