modular 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- modular/__init__.py +5 -0
- modular/tables/__init__.py +7 -0
- modular/tables/converters.py +102 -0
- modular-0.1.0.dist-info/METADATA +92 -0
- modular-0.1.0.dist-info/RECORD +8 -0
- modular-0.1.0.dist-info/WHEEL +5 -0
- modular-0.1.0.dist-info/licenses/LICENSE +21 -0
- modular-0.1.0.dist-info/top_level.txt +1 -0
modular/__init__.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Table conversion functions for various formats.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from typing import Union, List, Dict, Any
|
|
6
|
+
import csv
|
|
7
|
+
from io import StringIO
|
|
8
|
+
|
|
9
|
+
def _normalize_data(data: Union[List[List[Any]], List[Dict[str, Any]]]) -> tuple[List[List[str]], List[str]]:
|
|
10
|
+
"""
|
|
11
|
+
Normalize input data to a consistent format.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
data: List of lists or list of dictionaries
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
Tuple of (rows, headers)
|
|
18
|
+
"""
|
|
19
|
+
if not data:
|
|
20
|
+
return [], []
|
|
21
|
+
|
|
22
|
+
if isinstance(data[0], dict):
|
|
23
|
+
headers = list(data[0].keys())
|
|
24
|
+
rows = [[str(row.get(header, '')) for header in headers] for row in data]
|
|
25
|
+
else:
|
|
26
|
+
rows = [[str(cell) for cell in row] for row in data]
|
|
27
|
+
headers = [f"Column {i+1}" for i in range(len(rows[0]))] if rows else []
|
|
28
|
+
|
|
29
|
+
return rows, headers
|
|
30
|
+
|
|
31
|
+
def to_delimited(data: Union[List[List[Any]], List[Dict[str, Any]]],
|
|
32
|
+
delimiter: str = ',') -> str:
|
|
33
|
+
"""
|
|
34
|
+
Convert data to a delimited string format.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
data: List of lists or list of dictionaries
|
|
38
|
+
delimiter: Character to use as delimiter (default: ',')
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
String containing the delimited data
|
|
42
|
+
"""
|
|
43
|
+
rows, headers = _normalize_data(data)
|
|
44
|
+
output = StringIO()
|
|
45
|
+
writer = csv.writer(output, delimiter=delimiter)
|
|
46
|
+
writer.writerow(headers)
|
|
47
|
+
writer.writerows(rows)
|
|
48
|
+
return output.getvalue()
|
|
49
|
+
|
|
50
|
+
def to_markdown(data: Union[List[List[Any]], List[Dict[str, Any]]]) -> str:
|
|
51
|
+
"""
|
|
52
|
+
Convert data to Markdown table format.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
data: List of lists or list of dictionaries
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
String containing the Markdown table
|
|
59
|
+
"""
|
|
60
|
+
rows, headers = _normalize_data(data)
|
|
61
|
+
if not rows:
|
|
62
|
+
return ""
|
|
63
|
+
|
|
64
|
+
# Create header row
|
|
65
|
+
markdown = "| " + " | ".join(headers) + " |\n"
|
|
66
|
+
# Create separator row
|
|
67
|
+
markdown += "| " + " | ".join(["---"] * len(headers)) + " |\n"
|
|
68
|
+
# Add data rows
|
|
69
|
+
for row in rows:
|
|
70
|
+
markdown += "| " + " | ".join(row) + " |\n"
|
|
71
|
+
|
|
72
|
+
return markdown
|
|
73
|
+
|
|
74
|
+
def to_rst(data: Union[List[List[Any]], List[Dict[str, Any]]]) -> str:
|
|
75
|
+
"""
|
|
76
|
+
Convert data to reStructuredText table format.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
data: List of lists or list of dictionaries
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
String containing the reStructuredText table
|
|
83
|
+
"""
|
|
84
|
+
rows, headers = _normalize_data(data)
|
|
85
|
+
if not rows:
|
|
86
|
+
return ""
|
|
87
|
+
|
|
88
|
+
# Calculate column widths
|
|
89
|
+
col_widths = [max(len(str(cell)) for cell in col)
|
|
90
|
+
for col in zip(headers, *rows)]
|
|
91
|
+
|
|
92
|
+
# Create header row
|
|
93
|
+
rst = "+" + "+".join("-" * (width + 2) for width in col_widths) + "+\n"
|
|
94
|
+
rst += "|" + "|".join(f" {header:<{width}} " for header, width in zip(headers, col_widths)) + "|\n"
|
|
95
|
+
rst += "+" + "+".join("=" * (width + 2) for width in col_widths) + "+\n"
|
|
96
|
+
|
|
97
|
+
# Add data rows
|
|
98
|
+
for row in rows:
|
|
99
|
+
rst += "|" + "|".join(f" {cell:<{width}} " for cell, width in zip(row, col_widths)) + "|\n"
|
|
100
|
+
rst += "+" + "+".join("-" * (width + 2) for width in col_widths) + "+\n"
|
|
101
|
+
|
|
102
|
+
return rst
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: modular
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A modular package for all sorts of things where you want choices
|
|
5
|
+
Author-email: Jeffrey Spies <code@jeffspies.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/yourusername/modular
|
|
7
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/modular/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.7
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# Modular
|
|
17
|
+
|
|
18
|
+
A Python package for various data processing utilities with a focus on modularity and choice.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install modular
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
### Tables Module
|
|
29
|
+
|
|
30
|
+
The `tables` module provides functions to convert data into various table formats.
|
|
31
|
+
|
|
32
|
+
#### Supported Formats:
|
|
33
|
+
- Delimited (CSV, TSV, etc.)
|
|
34
|
+
- Markdown
|
|
35
|
+
- reStructuredText
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Converting Data to Different Formats
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from modular.tables import to_delimited, to_markdown, to_rst
|
|
43
|
+
|
|
44
|
+
# Example data as a list of dictionaries
|
|
45
|
+
data_dict = [
|
|
46
|
+
{"name": "Alice", "age": 30, "city": "New York"},
|
|
47
|
+
{"name": "Bob", "age": 25, "city": "Los Angeles"},
|
|
48
|
+
{"name": "Charlie", "age": 35, "city": "Chicago"}
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
# Convert to CSV
|
|
52
|
+
csv_output = to_delimited(data_dict)
|
|
53
|
+
print(csv_output)
|
|
54
|
+
|
|
55
|
+
# Convert to TSV
|
|
56
|
+
tsv_output = to_delimited(data_dict, delimiter='\t')
|
|
57
|
+
print(tsv_output)
|
|
58
|
+
|
|
59
|
+
# Convert to Markdown
|
|
60
|
+
md_output = to_markdown(data_dict)
|
|
61
|
+
print(md_output)
|
|
62
|
+
|
|
63
|
+
# Convert to reStructuredText
|
|
64
|
+
rst_output = to_rst(data_dict)
|
|
65
|
+
print(rst_output)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
You can also use lists of lists:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
data_list = [
|
|
72
|
+
["Alice", 30, "New York"],
|
|
73
|
+
["Bob", 25, "Los Angeles"],
|
|
74
|
+
["Charlie", 35, "Chicago"]
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
# Convert to any format as shown above
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Development
|
|
81
|
+
|
|
82
|
+
To install the package in development mode:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
git clone https://github.com/yourusername/modular.git
|
|
86
|
+
cd modular
|
|
87
|
+
pip install -e .
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
modular/__init__.py,sha256=KOWahwN7vyyMUktkN7YyQbmVdMFO2ZyS9Nxrj1kqWr4,90
|
|
2
|
+
modular/tables/__init__.py,sha256=S3XW4hQxc1PBpabJAwCu_lc5yGtWkWSoVOJusMsJLc8,174
|
|
3
|
+
modular/tables/converters.py,sha256=2SP1e8bDxyMrod8VeE28d18YYhjjRAyq5q0lNms1dME,3173
|
|
4
|
+
modular-0.1.0.dist-info/licenses/LICENSE,sha256=7f8kYSFH8lmD7FiRcmdEzaF1l9fRFLfqCk9LawWFeu8,1090
|
|
5
|
+
modular-0.1.0.dist-info/METADATA,sha256=eC86ZljmIAnopydeqPnmZ4H-9BB17tjITW2DbhvJjAw,2047
|
|
6
|
+
modular-0.1.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
7
|
+
modular-0.1.0.dist-info/top_level.txt,sha256=NYsXuaXaxphCF3Hrqg9P6w0CnOIsgyT2lxYlLoew070,8
|
|
8
|
+
modular-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jeffrey Spies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
modular
|