pygenutils 15.11.1__tar.gz
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.
- pygenutils-15.11.1/LICENSE +21 -0
- pygenutils-15.11.1/PKG-INFO +203 -0
- pygenutils-15.11.1/README.md +157 -0
- pygenutils-15.11.1/pygenutils/__init__.py +18 -0
- pygenutils-15.11.1/pygenutils/arrays_and_lists/__init__.py +12 -0
- pygenutils-15.11.1/pygenutils/arrays_and_lists/conversions.py +265 -0
- pygenutils-15.11.1/pygenutils/arrays_and_lists/data_manipulation.py +590 -0
- pygenutils-15.11.1/pygenutils/arrays_and_lists/maths.py +123 -0
- pygenutils-15.11.1/pygenutils/arrays_and_lists/patterns.py +478 -0
- pygenutils-15.11.1/pygenutils/audio_and_video/__init__.py +12 -0
- pygenutils-15.11.1/pygenutils/audio_and_video/audio_and_video_manipulation.py +399 -0
- pygenutils-15.11.1/pygenutils/audio_and_video/merge_audio_and_video.py +73 -0
- pygenutils-15.11.1/pygenutils/audio_and_video/merge_audio_or_video.py +54 -0
- pygenutils-15.11.1/pygenutils/audio_and_video/trim_media.py +55 -0
- pygenutils-15.11.1/pygenutils/dictionaries/__init__.py +10 -0
- pygenutils-15.11.1/pygenutils/dictionaries/dict_handler.py +127 -0
- pygenutils-15.11.1/pygenutils/dictionaries/dict_operators.py +105 -0
- pygenutils-15.11.1/pygenutils/number_bases/__init__.py +11 -0
- pygenutils-15.11.1/pygenutils/number_bases/base_converters.py +363 -0
- pygenutils-15.11.1/pygenutils/number_bases/binary_operations.py +183 -0
- pygenutils-15.11.1/pygenutils/number_bases/bitwise_operators.py +137 -0
- pygenutils-15.11.1/pygenutils/operative_systems/__init__.py +9 -0
- pygenutils-15.11.1/pygenutils/operative_systems/os_operations.py +386 -0
- pygenutils-15.11.1/pygenutils/sets_and_intervals/__init__.py +10 -0
- pygenutils-15.11.1/pygenutils/sets_and_intervals/interval_handler.py +251 -0
- pygenutils-15.11.1/pygenutils/sets_and_intervals/sets_handler.py +110 -0
- pygenutils-15.11.1/pygenutils/strings/__init__.py +10 -0
- pygenutils-15.11.1/pygenutils/strings/string_handler.py +830 -0
- pygenutils-15.11.1/pygenutils/strings/text_formatters.py +640 -0
- pygenutils-15.11.1/pygenutils/time_handling/__init__.py +14 -0
- pygenutils-15.11.1/pygenutils/time_handling/calendar_utils.py +341 -0
- pygenutils-15.11.1/pygenutils/time_handling/countdown.py +132 -0
- pygenutils-15.11.1/pygenutils/time_handling/date_and_time_maths.py +801 -0
- pygenutils-15.11.1/pygenutils/time_handling/date_and_time_utils.py +900 -0
- pygenutils-15.11.1/pygenutils/time_handling/program_snippet_exec_timers.py +250 -0
- pygenutils-15.11.1/pygenutils/time_handling/time_formatters.py +1053 -0
- pygenutils-15.11.1/pygenutils/time_handling/time_utils.py +112 -0
- pygenutils-15.11.1/pygenutils.egg-info/PKG-INFO +203 -0
- pygenutils-15.11.1/pygenutils.egg-info/SOURCES.txt +43 -0
- pygenutils-15.11.1/pygenutils.egg-info/dependency_links.txt +1 -0
- pygenutils-15.11.1/pygenutils.egg-info/requires.txt +6 -0
- pygenutils-15.11.1/pygenutils.egg-info/top_level.txt +1 -0
- pygenutils-15.11.1/pyproject.toml +3 -0
- pygenutils-15.11.1/setup.cfg +4 -0
- pygenutils-15.11.1/setup.py +73 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Jon Ander Gabantxo
|
|
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,203 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pygenutils
|
|
3
|
+
Version: 15.11.1
|
|
4
|
+
Summary: A comprehensive Python utility library for general-purpose and specialised tasks
|
|
5
|
+
Home-page: https://github.com/EusDancerDev/pygenutils
|
|
6
|
+
Author: Jon Ander Gabantxo
|
|
7
|
+
Author-email: jagabantxo@gmail.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Project-URL: Bug Reports, https://github.com/EusDancerDev/pygenutils/issues
|
|
10
|
+
Project-URL: Source, https://github.com/EusDancerDev/pygenutils
|
|
11
|
+
Project-URL: Documentation, https://github.com/EusDancerDev/pygenutils#readme
|
|
12
|
+
Keywords: utilities,tools,python
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Operating System :: OS Independent
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
22
|
+
Classifier: Topic :: Utilities
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: more_itertools>=10.0.0
|
|
28
|
+
Requires-Dist: numpy<2.0.0,>=1.21.0
|
|
29
|
+
Requires-Dist: pandas<2.0.0,>=1.3.0
|
|
30
|
+
Requires-Dist: xarray>=0.20.0
|
|
31
|
+
Requires-Dist: filewise>=3.7.0
|
|
32
|
+
Requires-Dist: paramlib>=3.4.0
|
|
33
|
+
Dynamic: author
|
|
34
|
+
Dynamic: author-email
|
|
35
|
+
Dynamic: classifier
|
|
36
|
+
Dynamic: description
|
|
37
|
+
Dynamic: description-content-type
|
|
38
|
+
Dynamic: home-page
|
|
39
|
+
Dynamic: keywords
|
|
40
|
+
Dynamic: license
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
Dynamic: project-url
|
|
43
|
+
Dynamic: requires-dist
|
|
44
|
+
Dynamic: requires-python
|
|
45
|
+
Dynamic: summary
|
|
46
|
+
|
|
47
|
+
# pygenutils
|
|
48
|
+
|
|
49
|
+
**pygenutils** is a comprehensive Python utilities package designed to provide a wide range of general-purpose tools for data manipulation, string handling, time operations, and system interactions. It serves as a collection of commonly needed utilities for Python development.
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
- **String Operations**:
|
|
54
|
+
- Advanced string manipulation
|
|
55
|
+
- Text formatting and parsing
|
|
56
|
+
- String pattern matching
|
|
57
|
+
- Text transformation utilities
|
|
58
|
+
- **Time Handling**:
|
|
59
|
+
- Date and time calculations
|
|
60
|
+
- Calendar operations
|
|
61
|
+
- Time formatting
|
|
62
|
+
- Program execution timing
|
|
63
|
+
- Countdown functionality
|
|
64
|
+
- **Data Structures**:
|
|
65
|
+
- Array and list manipulation
|
|
66
|
+
- Data pattern matching
|
|
67
|
+
- Mathematical operations
|
|
68
|
+
- Data conversion utilities
|
|
69
|
+
- **System Operations**:
|
|
70
|
+
- Operating system interactions
|
|
71
|
+
- Process management
|
|
72
|
+
- System information retrieval
|
|
73
|
+
- **Audio and Video**:
|
|
74
|
+
- Media file handling
|
|
75
|
+
- Format conversion utilities
|
|
76
|
+
- **Number Systems**:
|
|
77
|
+
- Base conversion utilities
|
|
78
|
+
- Number system operations
|
|
79
|
+
- **Set Operations**:
|
|
80
|
+
- Interval handling
|
|
81
|
+
- Set manipulation
|
|
82
|
+
- **Dictionary Operations**:
|
|
83
|
+
- Dictionary manipulation
|
|
84
|
+
- Key-value operations
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Installation Guide
|
|
89
|
+
|
|
90
|
+
### Dependency Notice
|
|
91
|
+
|
|
92
|
+
This package has minimal external dependencies and is designed to be lightweight. Most functionality works with Python's standard library.
|
|
93
|
+
|
|
94
|
+
### Installation Instructions
|
|
95
|
+
|
|
96
|
+
Install the package using pip:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
pip install pygenutils
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Package Updates
|
|
103
|
+
|
|
104
|
+
To stay up-to-date with the latest version of this package, simply run:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
pip install --upgrade pygenutils
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## Project Structure
|
|
113
|
+
|
|
114
|
+
The package is organised into several specialised modules:
|
|
115
|
+
|
|
116
|
+
- **strings/**: String manipulation utilities
|
|
117
|
+
- `string_handler.py`: Core string operations
|
|
118
|
+
- `text_formatters.py`: Text formatting utilities
|
|
119
|
+
|
|
120
|
+
- **time_handling/**: Time and date operations
|
|
121
|
+
- `date_and_time_utils.py`: Date/time calculations
|
|
122
|
+
- `time_formatters.py`: Time formatting
|
|
123
|
+
- `calendar_utils.py`: Calendar operations
|
|
124
|
+
- `program_snippet_exec_timers.py`: Execution timing
|
|
125
|
+
- `countdown.py`: Countdown functionality
|
|
126
|
+
|
|
127
|
+
- **arrays_and_lists/**: Data structure operations
|
|
128
|
+
- `data_manipulation.py`: Data handling
|
|
129
|
+
- `patterns.py`: Pattern matching
|
|
130
|
+
- `conversions.py`: Data conversion
|
|
131
|
+
- `maths.py`: Mathematical operations
|
|
132
|
+
|
|
133
|
+
- **operative_systems/**: System operations
|
|
134
|
+
- System interaction utilities
|
|
135
|
+
- Process management tools
|
|
136
|
+
|
|
137
|
+
- **audio_and_video/**: Media handling
|
|
138
|
+
- Media file operations
|
|
139
|
+
- Format conversion
|
|
140
|
+
|
|
141
|
+
- **number_bases/**: Number system operations
|
|
142
|
+
- Base conversion utilities
|
|
143
|
+
|
|
144
|
+
- **sets_and_intervals/**: Set operations
|
|
145
|
+
- Interval handling
|
|
146
|
+
- Set manipulation
|
|
147
|
+
|
|
148
|
+
- **dictionaries/**: Dictionary operations
|
|
149
|
+
- Dictionary manipulation tools
|
|
150
|
+
|
|
151
|
+
For detailed version history and changes, please refer to:
|
|
152
|
+
|
|
153
|
+
- `CHANGELOG.md`: Comprehensive list of changes for each version
|
|
154
|
+
- `VERSIONING.md`: Versioning policy and guidelines
|
|
155
|
+
|
|
156
|
+
## Usage Examples
|
|
157
|
+
|
|
158
|
+
### String Operations
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
from pygenutils.strings import string_handler, text_formatters
|
|
162
|
+
|
|
163
|
+
# String manipulation
|
|
164
|
+
modified_str = string_handler.modify_obj_specs("example.txt", "new")
|
|
165
|
+
formatted_text = text_formatters.print_format_string("Hello, {name}!", name="World")
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Time Operations
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
from pygenutils.time_handling import date_and_time_utils, time_formatters
|
|
172
|
+
|
|
173
|
+
# Date calculations
|
|
174
|
+
next_week = date_and_time_utils.add_days_to_date("2024-03-20", 7)
|
|
175
|
+
formatted_time = time_formatters.format_time_string("14:30:00", "HH:MM")
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Array Operations
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from pygenutils.arrays_and_lists import data_manipulation, patterns
|
|
182
|
+
|
|
183
|
+
# Data manipulation
|
|
184
|
+
processed_data = data_manipulation.process_array([1, 2, 3, 4, 5])
|
|
185
|
+
matched_pattern = patterns.find_pattern([1, 2, 3, 1, 2, 3], [1, 2, 3])
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Best Practices
|
|
189
|
+
|
|
190
|
+
1. **Module Organisation**:
|
|
191
|
+
- Use appropriate sub-packages for related functionality
|
|
192
|
+
- Keep utility functions focused and single-purpose
|
|
193
|
+
- Document all public functions and classes
|
|
194
|
+
|
|
195
|
+
2. **Performance Considerations**:
|
|
196
|
+
- Optimise frequently used operations
|
|
197
|
+
- Use appropriate data structures
|
|
198
|
+
- Consider memory usage for large operations
|
|
199
|
+
|
|
200
|
+
3. **Error Handling**:
|
|
201
|
+
- Implement proper error checking
|
|
202
|
+
- Provide meaningful error messages
|
|
203
|
+
- Handle edge cases appropriately
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# pygenutils
|
|
2
|
+
|
|
3
|
+
**pygenutils** is a comprehensive Python utilities package designed to provide a wide range of general-purpose tools for data manipulation, string handling, time operations, and system interactions. It serves as a collection of commonly needed utilities for Python development.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **String Operations**:
|
|
8
|
+
- Advanced string manipulation
|
|
9
|
+
- Text formatting and parsing
|
|
10
|
+
- String pattern matching
|
|
11
|
+
- Text transformation utilities
|
|
12
|
+
- **Time Handling**:
|
|
13
|
+
- Date and time calculations
|
|
14
|
+
- Calendar operations
|
|
15
|
+
- Time formatting
|
|
16
|
+
- Program execution timing
|
|
17
|
+
- Countdown functionality
|
|
18
|
+
- **Data Structures**:
|
|
19
|
+
- Array and list manipulation
|
|
20
|
+
- Data pattern matching
|
|
21
|
+
- Mathematical operations
|
|
22
|
+
- Data conversion utilities
|
|
23
|
+
- **System Operations**:
|
|
24
|
+
- Operating system interactions
|
|
25
|
+
- Process management
|
|
26
|
+
- System information retrieval
|
|
27
|
+
- **Audio and Video**:
|
|
28
|
+
- Media file handling
|
|
29
|
+
- Format conversion utilities
|
|
30
|
+
- **Number Systems**:
|
|
31
|
+
- Base conversion utilities
|
|
32
|
+
- Number system operations
|
|
33
|
+
- **Set Operations**:
|
|
34
|
+
- Interval handling
|
|
35
|
+
- Set manipulation
|
|
36
|
+
- **Dictionary Operations**:
|
|
37
|
+
- Dictionary manipulation
|
|
38
|
+
- Key-value operations
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Installation Guide
|
|
43
|
+
|
|
44
|
+
### Dependency Notice
|
|
45
|
+
|
|
46
|
+
This package has minimal external dependencies and is designed to be lightweight. Most functionality works with Python's standard library.
|
|
47
|
+
|
|
48
|
+
### Installation Instructions
|
|
49
|
+
|
|
50
|
+
Install the package using pip:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install pygenutils
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Package Updates
|
|
57
|
+
|
|
58
|
+
To stay up-to-date with the latest version of this package, simply run:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install --upgrade pygenutils
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Project Structure
|
|
67
|
+
|
|
68
|
+
The package is organised into several specialised modules:
|
|
69
|
+
|
|
70
|
+
- **strings/**: String manipulation utilities
|
|
71
|
+
- `string_handler.py`: Core string operations
|
|
72
|
+
- `text_formatters.py`: Text formatting utilities
|
|
73
|
+
|
|
74
|
+
- **time_handling/**: Time and date operations
|
|
75
|
+
- `date_and_time_utils.py`: Date/time calculations
|
|
76
|
+
- `time_formatters.py`: Time formatting
|
|
77
|
+
- `calendar_utils.py`: Calendar operations
|
|
78
|
+
- `program_snippet_exec_timers.py`: Execution timing
|
|
79
|
+
- `countdown.py`: Countdown functionality
|
|
80
|
+
|
|
81
|
+
- **arrays_and_lists/**: Data structure operations
|
|
82
|
+
- `data_manipulation.py`: Data handling
|
|
83
|
+
- `patterns.py`: Pattern matching
|
|
84
|
+
- `conversions.py`: Data conversion
|
|
85
|
+
- `maths.py`: Mathematical operations
|
|
86
|
+
|
|
87
|
+
- **operative_systems/**: System operations
|
|
88
|
+
- System interaction utilities
|
|
89
|
+
- Process management tools
|
|
90
|
+
|
|
91
|
+
- **audio_and_video/**: Media handling
|
|
92
|
+
- Media file operations
|
|
93
|
+
- Format conversion
|
|
94
|
+
|
|
95
|
+
- **number_bases/**: Number system operations
|
|
96
|
+
- Base conversion utilities
|
|
97
|
+
|
|
98
|
+
- **sets_and_intervals/**: Set operations
|
|
99
|
+
- Interval handling
|
|
100
|
+
- Set manipulation
|
|
101
|
+
|
|
102
|
+
- **dictionaries/**: Dictionary operations
|
|
103
|
+
- Dictionary manipulation tools
|
|
104
|
+
|
|
105
|
+
For detailed version history and changes, please refer to:
|
|
106
|
+
|
|
107
|
+
- `CHANGELOG.md`: Comprehensive list of changes for each version
|
|
108
|
+
- `VERSIONING.md`: Versioning policy and guidelines
|
|
109
|
+
|
|
110
|
+
## Usage Examples
|
|
111
|
+
|
|
112
|
+
### String Operations
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from pygenutils.strings import string_handler, text_formatters
|
|
116
|
+
|
|
117
|
+
# String manipulation
|
|
118
|
+
modified_str = string_handler.modify_obj_specs("example.txt", "new")
|
|
119
|
+
formatted_text = text_formatters.print_format_string("Hello, {name}!", name="World")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Time Operations
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from pygenutils.time_handling import date_and_time_utils, time_formatters
|
|
126
|
+
|
|
127
|
+
# Date calculations
|
|
128
|
+
next_week = date_and_time_utils.add_days_to_date("2024-03-20", 7)
|
|
129
|
+
formatted_time = time_formatters.format_time_string("14:30:00", "HH:MM")
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Array Operations
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from pygenutils.arrays_and_lists import data_manipulation, patterns
|
|
136
|
+
|
|
137
|
+
# Data manipulation
|
|
138
|
+
processed_data = data_manipulation.process_array([1, 2, 3, 4, 5])
|
|
139
|
+
matched_pattern = patterns.find_pattern([1, 2, 3, 1, 2, 3], [1, 2, 3])
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Best Practices
|
|
143
|
+
|
|
144
|
+
1. **Module Organisation**:
|
|
145
|
+
- Use appropriate sub-packages for related functionality
|
|
146
|
+
- Keep utility functions focused and single-purpose
|
|
147
|
+
- Document all public functions and classes
|
|
148
|
+
|
|
149
|
+
2. **Performance Considerations**:
|
|
150
|
+
- Optimise frequently used operations
|
|
151
|
+
- Use appropriate data structures
|
|
152
|
+
- Consider memory usage for large operations
|
|
153
|
+
|
|
154
|
+
3. **Error Handling**:
|
|
155
|
+
- Implement proper error checking
|
|
156
|
+
- Provide meaningful error messages
|
|
157
|
+
- Handle edge cases appropriately
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# pygenutils/__init__.py
|
|
5
|
+
|
|
6
|
+
__version__ = "15.11.1"
|
|
7
|
+
|
|
8
|
+
# Define what should be available when using 'from pygenutils import *'
|
|
9
|
+
__all__ = [
|
|
10
|
+
'arrays_and_lists',
|
|
11
|
+
'audio_and_video',
|
|
12
|
+
'dictionaries',
|
|
13
|
+
'number_bases',
|
|
14
|
+
'operative_systems',
|
|
15
|
+
'sets_and_intervals',
|
|
16
|
+
'strings',
|
|
17
|
+
'time_handling',
|
|
18
|
+
]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
# pygenutils/arrays_and_lists/__init__.py
|
|
5
|
+
|
|
6
|
+
# Define what should be available when using 'from pygenutils.arrays_and_lists import *'
|
|
7
|
+
__all__ = [
|
|
8
|
+
'conversions',
|
|
9
|
+
'data_manipulation',
|
|
10
|
+
'maths',
|
|
11
|
+
'patterns'
|
|
12
|
+
]
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
#----------------#
|
|
5
|
+
# Import modules #
|
|
6
|
+
#----------------#
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
#------------------------#
|
|
11
|
+
# Import project modules #
|
|
12
|
+
#------------------------#
|
|
13
|
+
|
|
14
|
+
from filewise.general.introspection_utils import get_type_str
|
|
15
|
+
|
|
16
|
+
#------------------#
|
|
17
|
+
# Define functions #
|
|
18
|
+
#------------------#
|
|
19
|
+
|
|
20
|
+
# Data types #
|
|
21
|
+
#------------#
|
|
22
|
+
|
|
23
|
+
def convert_data_type(obj_data, old_type, new_type, colnames=None, convert_to_list=False):
|
|
24
|
+
"""
|
|
25
|
+
Function that converts the original data type of the values in a given object
|
|
26
|
+
(numpy array, pandas DataFrame/Series) to the desired one.
|
|
27
|
+
If the new data type is the same as the original, the function returns
|
|
28
|
+
the object unchanged, and prints a message showing the latter.
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
obj_data : pandas.DataFrame, pandas.Series, numpy.ndarray, or list
|
|
33
|
+
Object containing the data to be converted.
|
|
34
|
+
old_type : str
|
|
35
|
+
Current type of the object's values.
|
|
36
|
+
new_type : str
|
|
37
|
+
Type to which the data should be converted.
|
|
38
|
+
colnames : str, list of str, or '__all_columns__', optional
|
|
39
|
+
Column(s) to apply conversion in case of pandas DataFrame.
|
|
40
|
+
If '__all_columns__', conversion will be applied to all columns.
|
|
41
|
+
Not applicable for pandas Series or numpy arrays.
|
|
42
|
+
convert_to_list : bool, optional
|
|
43
|
+
If True, converts the result to a list before returning.
|
|
44
|
+
|
|
45
|
+
Returns
|
|
46
|
+
-------
|
|
47
|
+
obj_data : pandas.DataFrame, pandas.Series, numpy.ndarray, or list
|
|
48
|
+
Object with the converted data type, or unchanged if no conversion was made.
|
|
49
|
+
|
|
50
|
+
Raises
|
|
51
|
+
------
|
|
52
|
+
TypeError
|
|
53
|
+
If the conversion to the new type cannot be done or if the object type is invalid.
|
|
54
|
+
KeyError
|
|
55
|
+
If specified columns are not found in pandas DataFrame.
|
|
56
|
+
"""
|
|
57
|
+
# Get input object's type
|
|
58
|
+
obj_type = get_type_str(obj_data)
|
|
59
|
+
|
|
60
|
+
# Handle pandas DataFrames
|
|
61
|
+
if obj_type == "DataFrame":
|
|
62
|
+
if colnames is None:
|
|
63
|
+
raise ValueError("Please specify 'colnames' for pandas DataFrame.")
|
|
64
|
+
if colnames == '__all_columns__': # apply to all columns
|
|
65
|
+
colnames = obj_data.columns
|
|
66
|
+
elif isinstance(colnames, str):
|
|
67
|
+
colnames = [colnames] # convert to list for consistency
|
|
68
|
+
elif isinstance(colnames, list):
|
|
69
|
+
pass
|
|
70
|
+
else:
|
|
71
|
+
raise TypeError("'colnames' must be a string, list of strings, or '__all_columns__'.")
|
|
72
|
+
|
|
73
|
+
# Find missing columns
|
|
74
|
+
missing_cols = [col for col in colnames if col not in obj_data.columns]
|
|
75
|
+
if missing_cols:
|
|
76
|
+
raise KeyError(f"The following columns were not found: {missing_cols}")
|
|
77
|
+
|
|
78
|
+
# Apply conversion
|
|
79
|
+
data_converted = obj_data.copy()
|
|
80
|
+
for col in colnames:
|
|
81
|
+
if obj_data[col].dtype == old_type:
|
|
82
|
+
try:
|
|
83
|
+
data_converted[col] = obj_data[col].astype(new_type)
|
|
84
|
+
except:
|
|
85
|
+
raise TypeError(f"Cannot convert column '{col}' to type '{new_type}'.")
|
|
86
|
+
else:
|
|
87
|
+
print(f"Column '{col}' data type unchanged.")
|
|
88
|
+
|
|
89
|
+
return data_converted
|
|
90
|
+
|
|
91
|
+
# Handle pandas Series
|
|
92
|
+
elif obj_type == "Series":
|
|
93
|
+
if obj_data.dtype == old_type:
|
|
94
|
+
try:
|
|
95
|
+
return obj_data.astype(new_type)
|
|
96
|
+
except:
|
|
97
|
+
raise TypeError(f"Cannot convert Series to type '{new_type}'.")
|
|
98
|
+
else:
|
|
99
|
+
print("Series data type unchanged.")
|
|
100
|
+
return obj_data
|
|
101
|
+
|
|
102
|
+
# Handle numpy arrays and lists
|
|
103
|
+
elif obj_type in ["ndarray", "list"]:
|
|
104
|
+
try:
|
|
105
|
+
obj_data = np.array(obj_data) # convert to numpy array if it's not already
|
|
106
|
+
if obj_data.dtype == old_type:
|
|
107
|
+
try:
|
|
108
|
+
data_converted = obj_data.astype(new_type)
|
|
109
|
+
except:
|
|
110
|
+
raise TypeError(f"Cannot convert array to type '{new_type}'.")
|
|
111
|
+
if convert_to_list:
|
|
112
|
+
return list(data_converted)
|
|
113
|
+
return data_converted
|
|
114
|
+
else:
|
|
115
|
+
print("Array data type unchanged.")
|
|
116
|
+
if convert_to_list:
|
|
117
|
+
return list(obj_data)
|
|
118
|
+
return obj_data
|
|
119
|
+
except Exception as e:
|
|
120
|
+
raise TypeError(f"Error occurred during conversion: {e}")
|
|
121
|
+
|
|
122
|
+
# Raise TypeError if the object type is not supported
|
|
123
|
+
else:
|
|
124
|
+
raise TypeError("Unsupported object type. "
|
|
125
|
+
"Expected pandas DataFrame/Series, numpy array, or list.")
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def combine_arrays(array_of_lists):
|
|
129
|
+
"""
|
|
130
|
+
Combine a list of NumPy arrays or lists into a single NumPy array.
|
|
131
|
+
|
|
132
|
+
This function takes a list of NumPy arrays (or lists) and combines them
|
|
133
|
+
into a single NumPy array. It supports arrays with up to 3 dimensions.
|
|
134
|
+
If the arrays have inhomogeneous lengths, it uses `np.hstack` to flatten
|
|
135
|
+
and concatenate the arrays.
|
|
136
|
+
|
|
137
|
+
Parameters
|
|
138
|
+
----------
|
|
139
|
+
array_of_lists : list
|
|
140
|
+
A list of NumPy arrays or lists to be combined.
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
-------
|
|
144
|
+
array : numpy.ndarray
|
|
145
|
+
A single NumPy array formed by combining the input arrays.
|
|
146
|
+
|
|
147
|
+
Raises
|
|
148
|
+
------
|
|
149
|
+
ValueError
|
|
150
|
+
- If the arrays in the list have more than 3 dimensions.
|
|
151
|
+
- If the shapes of the arrays are inconsistent and cannot be combined.
|
|
152
|
+
|
|
153
|
+
Example
|
|
154
|
+
-------
|
|
155
|
+
>>> import numpy as np
|
|
156
|
+
>>> array1 = np.array([[1, 2], [3, 4]])
|
|
157
|
+
>>> array2 = np.array([[5, 6], [7, 8]])
|
|
158
|
+
>>> array_of_lists = [array1, array2]
|
|
159
|
+
>>> result = combine_arrays(array_of_lists)
|
|
160
|
+
>>> print(result)
|
|
161
|
+
[[1 2]
|
|
162
|
+
[3 4]
|
|
163
|
+
[5 6]
|
|
164
|
+
[7 8]]
|
|
165
|
+
|
|
166
|
+
Notes
|
|
167
|
+
-----
|
|
168
|
+
- If the arrays have different shapes, they are concatenated and flattened
|
|
169
|
+
using `np.hstack`.
|
|
170
|
+
- This function assumes that the input contains valid NumPy arrays or lists.
|
|
171
|
+
"""
|
|
172
|
+
# Get the list of unique dimensions of the arrays #
|
|
173
|
+
dim_list = np.unique([arr.ndim for arr in array_of_lists])
|
|
174
|
+
ld = len(dim_list)
|
|
175
|
+
|
|
176
|
+
# If all arrays/lists are of the same dimension #
|
|
177
|
+
if ld == 1:
|
|
178
|
+
dims = dim_list[0]
|
|
179
|
+
|
|
180
|
+
if dims == 2:
|
|
181
|
+
array = np.vstack(array_of_lists)
|
|
182
|
+
elif dims == 3:
|
|
183
|
+
array = np.stack(array_of_lists)
|
|
184
|
+
else:
|
|
185
|
+
raise ValueError("Cannot handle arrays with dimensions greater than 3.")
|
|
186
|
+
|
|
187
|
+
# If the arrays/lists have inconsistent dimensions #
|
|
188
|
+
else:
|
|
189
|
+
array = np.hstack(array_of_lists)
|
|
190
|
+
|
|
191
|
+
return array
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def flatten_to_string(obj, delim=" ", add_final_space=False):
|
|
195
|
+
"""
|
|
196
|
+
Flatten the content of a list, NumPy array, or pandas DataFrame/Series
|
|
197
|
+
into a single string, where elements are separated by a specified delimiter.
|
|
198
|
+
|
|
199
|
+
This method takes an input object (list, NumPy array, pandas DataFrame, or Series),
|
|
200
|
+
flattens it (if needed), converts all elements to strings, and joins them into
|
|
201
|
+
a single string. Optionally, a final delimiter can be added to the end of the string.
|
|
202
|
+
|
|
203
|
+
Parameters
|
|
204
|
+
----------
|
|
205
|
+
obj : list, numpy.ndarray, pandas.DataFrame, or pandas.Series
|
|
206
|
+
The input object containing data to be flattened and converted to a string.
|
|
207
|
+
delim : str, optional
|
|
208
|
+
The delimiter to use for separating elements in the resulting string.
|
|
209
|
+
By default, a space character (' ') is used.
|
|
210
|
+
add_final_space : bool, optional
|
|
211
|
+
If True, adds a delimiter (or space) at the end of the string.
|
|
212
|
+
Default is False.
|
|
213
|
+
|
|
214
|
+
Returns
|
|
215
|
+
-------
|
|
216
|
+
str
|
|
217
|
+
A single string containing all elements of the input object,
|
|
218
|
+
separated by the specified delimiter.
|
|
219
|
+
|
|
220
|
+
Raises
|
|
221
|
+
------
|
|
222
|
+
TypeError
|
|
223
|
+
If the input object is not a list, numpy array, pandas DataFrame, or Series.
|
|
224
|
+
|
|
225
|
+
Example
|
|
226
|
+
-------
|
|
227
|
+
>>> import numpy as np
|
|
228
|
+
>>> arr = np.array([[1, 2], [3, 4]])
|
|
229
|
+
>>> flatten_to_string(arr, delim=',', add_final_space=True)
|
|
230
|
+
'1,2,3,4,'
|
|
231
|
+
|
|
232
|
+
Notes
|
|
233
|
+
-----
|
|
234
|
+
This method is particularly useful for converting arrays or lists of file names
|
|
235
|
+
into a single string to pass as arguments to shell commands or other processes
|
|
236
|
+
that require string input.
|
|
237
|
+
"""
|
|
238
|
+
# Get input object type
|
|
239
|
+
obj_type = get_type_str(obj)
|
|
240
|
+
|
|
241
|
+
# Validate input type #
|
|
242
|
+
if obj_type not in ["list", "ndarray", "DataFrame", "Series"]:
|
|
243
|
+
raise TypeError("'flatten_to_string' supports lists, NumPy arrays, and pandas DataFrames/Series.")
|
|
244
|
+
|
|
245
|
+
# Convert pandas DataFrame/Series to NumPy array #
|
|
246
|
+
if obj_type not in ["DataFrame", "Series"]:
|
|
247
|
+
obj_val_array = obj.values
|
|
248
|
+
else:
|
|
249
|
+
obj_val_array = np.array(obj) # Ensure it's a NumPy array if it's a list
|
|
250
|
+
|
|
251
|
+
# Flatten the array if it has more than one dimension #
|
|
252
|
+
if hasattr(obj_val_array, "flatten"):
|
|
253
|
+
obj_val_array = obj_val_array.flatten()
|
|
254
|
+
|
|
255
|
+
# Convert all elements to strings #
|
|
256
|
+
obj_list = [str(el) for el in obj_val_array]
|
|
257
|
+
|
|
258
|
+
# Join all elements into a single string #
|
|
259
|
+
allobj_string = delim.join(obj_list)
|
|
260
|
+
|
|
261
|
+
# Optionally add a final delimiter/space #
|
|
262
|
+
if add_final_space:
|
|
263
|
+
allobj_string += delim
|
|
264
|
+
|
|
265
|
+
return allobj_string
|