pygenutils 16.1.3__tar.gz → 16.2.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-16.1.3/pygenutils.egg-info → pygenutils-16.2.1}/PKG-INFO +10 -3
- {pygenutils-16.1.3 → pygenutils-16.2.1}/README.md +7 -1
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/__init__.py +1 -1
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/strings/string_handler.py +30 -6
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/strings/text_formatters.py +56 -35
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/time_formatters.py +167 -72
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/time_utils.py +16 -7
- {pygenutils-16.1.3 → pygenutils-16.2.1/pygenutils.egg-info}/PKG-INFO +10 -3
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils.egg-info/requires.txt +2 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pyproject.toml +2 -2
- {pygenutils-16.1.3 → pygenutils-16.2.1}/LICENSE +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/MANIFEST.in +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/arrays_and_lists/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/arrays_and_lists/conversions.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/arrays_and_lists/data_manipulation.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/arrays_and_lists/maths.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/arrays_and_lists/patterns.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/audio_and_video/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/audio_and_video/audio_and_video_manipulation.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/audio_and_video/merge_audio_and_video.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/audio_and_video/merge_audio_or_video.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/audio_and_video/trim_media.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/dictionaries/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/dictionaries/dict_handler.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/dictionaries/dict_operators.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/number_bases/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/number_bases/base_converters.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/number_bases/binary_operations.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/number_bases/bitwise_operators.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/number_bases/mathematical_utils.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/operative_systems/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/operative_systems/os_operations.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/sets_and_intervals/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/sets_and_intervals/interval_handler.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/sets_and_intervals/sets_handler.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/strings/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/__init__.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/calendar_utils.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/countdown.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/date_and_time_maths.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/date_and_time_utils.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/program_snippet_exec_timers.py +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils.egg-info/SOURCES.txt +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils.egg-info/dependency_links.txt +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils.egg-info/top_level.txt +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/setup.cfg +0 -0
- {pygenutils-16.1.3 → pygenutils-16.2.1}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pygenutils
|
|
3
|
-
Version: 16.1
|
|
3
|
+
Version: 16.2.1
|
|
4
4
|
Summary: A comprehensive Python utility library for general-purpose and specialised tasks
|
|
5
5
|
Author-email: Jon Ander Gabantxo <jagabantxo@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -47,9 +47,10 @@ Requires-Dist: numpy<2.0.0,>=1.21.0
|
|
|
47
47
|
Requires-Dist: pandas<2.0.0,>=1.3.0
|
|
48
48
|
Requires-Dist: filewise>=3.11.0
|
|
49
49
|
Requires-Dist: paramlib>=3.4.3
|
|
50
|
-
Requires-Dist: arrow>=1.2.0
|
|
51
50
|
Provides-Extra: xarray
|
|
52
51
|
Requires-Dist: xarray>=2024.2.0; extra == "xarray"
|
|
52
|
+
Provides-Extra: arrow
|
|
53
|
+
Requires-Dist: arrow>=1.2.0; extra == "arrow"
|
|
53
54
|
Provides-Extra: dev
|
|
54
55
|
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
55
56
|
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
@@ -117,11 +118,17 @@ This package has minimal external dependencies and is designed to be lightweight
|
|
|
117
118
|
**For regular users** who want to use the package in their projects:
|
|
118
119
|
|
|
119
120
|
```bash
|
|
120
|
-
pip install pygenutils
|
|
121
|
+
pip install pygenutils # Core functionality
|
|
122
|
+
pip install pygenutils[arrow] # With arrow support (optional)
|
|
121
123
|
```
|
|
122
124
|
|
|
123
125
|
This automatically installs `pygenutils` and all its dependencies with version constraints.
|
|
124
126
|
|
|
127
|
+
**How It Works Now**:
|
|
128
|
+
|
|
129
|
+
- `pip install pygenutils` - Core functionality without arrow dependency
|
|
130
|
+
- `pip install pygenutils[arrow]` - With arrow support for enhanced time handling features
|
|
131
|
+
|
|
125
132
|
### Package Updates
|
|
126
133
|
|
|
127
134
|
To stay up-to-date with the latest version of this package, simply run:
|
|
@@ -56,11 +56,17 @@ This package has minimal external dependencies and is designed to be lightweight
|
|
|
56
56
|
**For regular users** who want to use the package in their projects:
|
|
57
57
|
|
|
58
58
|
```bash
|
|
59
|
-
pip install pygenutils
|
|
59
|
+
pip install pygenutils # Core functionality
|
|
60
|
+
pip install pygenutils[arrow] # With arrow support (optional)
|
|
60
61
|
```
|
|
61
62
|
|
|
62
63
|
This automatically installs `pygenutils` and all its dependencies with version constraints.
|
|
63
64
|
|
|
65
|
+
**How It Works Now**:
|
|
66
|
+
|
|
67
|
+
- `pip install pygenutils` - Core functionality without arrow dependency
|
|
68
|
+
- `pip install pygenutils[arrow]` - With arrow support for enhanced time handling features
|
|
69
|
+
|
|
64
70
|
### Package Updates
|
|
65
71
|
|
|
66
72
|
To stay up-to-date with the latest version of this package, simply run:
|
|
@@ -109,7 +109,14 @@ def find_substring_index(string,
|
|
|
109
109
|
find_whole_words,
|
|
110
110
|
all_matches)
|
|
111
111
|
else:
|
|
112
|
-
|
|
112
|
+
if return_match_index == "lo":
|
|
113
|
+
return string.find(substring)
|
|
114
|
+
elif return_match_index == "hi":
|
|
115
|
+
return string.rfind(substring)
|
|
116
|
+
else: # return_match_index == "both"
|
|
117
|
+
first_index = string.find(substring)
|
|
118
|
+
last_index = string.rfind(substring)
|
|
119
|
+
return [first_index, last_index]
|
|
113
120
|
|
|
114
121
|
|
|
115
122
|
elif get_type_str(string) in ["list", "ndarray", "tuple"]:
|
|
@@ -118,8 +125,18 @@ def find_substring_index(string,
|
|
|
118
125
|
# Simple search without advanced features
|
|
119
126
|
if not advanced_search:
|
|
120
127
|
if get_type_str(string) == "ndarray":
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
if return_match_index == "lo":
|
|
129
|
+
match_indices = char.find(string, substring, start=start, end=end)
|
|
130
|
+
return [idx for idx in match_indices if idx != -1]
|
|
131
|
+
elif return_match_index == "hi":
|
|
132
|
+
match_indices = char.rfind(string, substring, start=start, end=end)
|
|
133
|
+
return [idx for idx in match_indices if idx != -1]
|
|
134
|
+
else: # return_match_index == "both"
|
|
135
|
+
first_indices = char.find(string, substring, start=start, end=end)
|
|
136
|
+
last_indices = char.rfind(string, substring, start=start, end=end)
|
|
137
|
+
first_filtered = [idx for idx in first_indices if idx != -1]
|
|
138
|
+
last_filtered = [idx for idx in last_indices if idx != -1]
|
|
139
|
+
return [first_filtered, last_filtered]
|
|
123
140
|
|
|
124
141
|
else:
|
|
125
142
|
if end is None:
|
|
@@ -144,7 +161,14 @@ def find_substring_index(string,
|
|
|
144
161
|
substring = flatten_list(substring)
|
|
145
162
|
|
|
146
163
|
if not advanced_search:
|
|
147
|
-
|
|
164
|
+
if return_match_index == "lo":
|
|
165
|
+
return char.find(string, substring, start=start, end=end)
|
|
166
|
+
elif return_match_index == "hi":
|
|
167
|
+
return char.rfind(string, substring, start=start, end=end)
|
|
168
|
+
else: # return_match_index == "both"
|
|
169
|
+
first_indices = char.find(string, substring, start=start, end=end)
|
|
170
|
+
last_indices = char.rfind(string, substring, start=start, end=end)
|
|
171
|
+
return [first_indices, last_indices]
|
|
148
172
|
else:
|
|
149
173
|
match_indices = _advanced_pattern_searcher(string, substring,
|
|
150
174
|
return_match_index,
|
|
@@ -739,7 +763,7 @@ def strip(string, strip_option='strip', chars=None):
|
|
|
739
763
|
|
|
740
764
|
Parameters
|
|
741
765
|
----------
|
|
742
|
-
strip_option: {'strip', 'lstrip', '
|
|
766
|
+
strip_option: {'strip', 'lstrip', 'rstrip'} or None
|
|
743
767
|
Location of the white spaces or substring to strip.
|
|
744
768
|
Default option is the widely used 'strip'.
|
|
745
769
|
|
|
@@ -833,4 +857,4 @@ REPLACE_ACTIONS = {
|
|
|
833
857
|
"ndarray": lambda s, sb2find, sb2replace, _ : char.replace(s, sb2find, sb2replace),
|
|
834
858
|
"dataframe": lambda s, sb2find, sb2replace, _ : DataFrame.replace(s, sb2find, sb2replace),
|
|
835
859
|
"series": lambda s, sb2find, sb2replace, _ : Series.replace(s, sb2find, sb2replace),
|
|
836
|
-
}
|
|
860
|
+
}
|
|
@@ -163,13 +163,13 @@ def print_percent_string(string2format, arg_obj):
|
|
|
163
163
|
# String font effects #
|
|
164
164
|
#---------------------#
|
|
165
165
|
|
|
166
|
-
def string_underliner(string, underline_char="-"):
|
|
166
|
+
def string_underliner(string, underline_char="-", width=None):
|
|
167
167
|
"""
|
|
168
168
|
Underlines a given string with the specified character.
|
|
169
169
|
|
|
170
170
|
If the string contains multiple lines, each line will be underlined
|
|
171
171
|
individually with the same character, keeping the length of the underline
|
|
172
|
-
consistent with each line's length.
|
|
172
|
+
consistent with each line's length (or the specified width).
|
|
173
173
|
|
|
174
174
|
Parameters
|
|
175
175
|
----------
|
|
@@ -179,24 +179,40 @@ def string_underliner(string, underline_char="-"):
|
|
|
179
179
|
underline_char : str, optional
|
|
180
180
|
The character used to underline the string.
|
|
181
181
|
Defaults to a dash ("-").
|
|
182
|
+
|
|
183
|
+
width : int, optional
|
|
184
|
+
The total width for centering the string. If None, uses the natural
|
|
185
|
+
length of each line. If specified, the string will be centered within
|
|
186
|
+
this width using the underline character as padding.
|
|
182
187
|
|
|
183
188
|
Returns
|
|
184
189
|
-------
|
|
185
190
|
str_underlined : str
|
|
186
191
|
The original string with an underline applied to each line.
|
|
187
192
|
For multiline strings, each line is individually underlined.
|
|
193
|
+
If width is specified, each line is centered within that width.
|
|
188
194
|
|
|
189
|
-
|
|
190
|
-
|
|
195
|
+
Examples
|
|
196
|
+
--------
|
|
191
197
|
For a single-line string:
|
|
192
198
|
|
|
193
199
|
>>> string_underliner("Hello", "*")
|
|
194
200
|
'Hello\n*****'
|
|
195
201
|
|
|
202
|
+
For a single-line string with width:
|
|
203
|
+
|
|
204
|
+
>>> string_underliner("Hello", "*", width=15)
|
|
205
|
+
'*****Hello*****\n***************'
|
|
206
|
+
|
|
196
207
|
For a multiline string:
|
|
197
208
|
|
|
198
209
|
>>> string_underliner("Hello\nWorld", "*")
|
|
199
210
|
'Hello\n*****\nWorld\n*****'
|
|
211
|
+
|
|
212
|
+
For a multiline string with width:
|
|
213
|
+
|
|
214
|
+
>>> string_underliner("Hello\nWorld", "*", width=15)
|
|
215
|
+
'*****Hello*****\n***************\n*****World*****\n***************'
|
|
200
216
|
"""
|
|
201
217
|
|
|
202
218
|
# Check if the string contains newlines, indicating a multiline string
|
|
@@ -210,17 +226,32 @@ def string_underliner(string, underline_char="-"):
|
|
|
210
226
|
# Build the underlined string by iterating over each line
|
|
211
227
|
str_underlined = ""
|
|
212
228
|
for word in word_list:
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
229
|
+
if width is not None:
|
|
230
|
+
# Center the word within the specified width
|
|
231
|
+
centered_word = word.center(width, underline_char)
|
|
232
|
+
underline = underline_char * width
|
|
233
|
+
else:
|
|
234
|
+
# Use natural length
|
|
235
|
+
centered_word = word
|
|
236
|
+
underline = underline_char * len(word)
|
|
237
|
+
|
|
238
|
+
str_underlined += f"{centered_word}\n{underline}\n"
|
|
216
239
|
|
|
217
240
|
# Remove the last newline character to avoid an extra empty line
|
|
218
241
|
str_underlined = str_underlined.rstrip(newline_char)
|
|
219
242
|
|
|
220
243
|
else:
|
|
221
|
-
# For a single-line string
|
|
222
|
-
|
|
223
|
-
|
|
244
|
+
# For a single-line string
|
|
245
|
+
if width is not None:
|
|
246
|
+
# Center the string within the specified width
|
|
247
|
+
centered_string = string.center(width, underline_char)
|
|
248
|
+
underline = underline_char * width
|
|
249
|
+
else:
|
|
250
|
+
# Use natural length
|
|
251
|
+
centered_string = string
|
|
252
|
+
underline = underline_char * len(string)
|
|
253
|
+
|
|
254
|
+
str_underlined = f"{centered_string}\n{underline}"
|
|
224
255
|
|
|
225
256
|
return str_underlined
|
|
226
257
|
|
|
@@ -316,7 +347,7 @@ def format_table(nested_dict,
|
|
|
316
347
|
|
|
317
348
|
# Build the header string
|
|
318
349
|
header_row = column_delimiter + \
|
|
319
|
-
column_delimiter.join(
|
|
350
|
+
column_delimiter.join(header.center(column_widths[header])
|
|
320
351
|
for header in headers) + \
|
|
321
352
|
column_delimiter
|
|
322
353
|
|
|
@@ -330,13 +361,13 @@ def format_table(nested_dict,
|
|
|
330
361
|
content_rows = []
|
|
331
362
|
for idx, subdict in nested_dict.items():
|
|
332
363
|
if display_index:
|
|
333
|
-
row = [
|
|
364
|
+
row = [str(idx).center(column_widths[index_header])]
|
|
334
365
|
else:
|
|
335
366
|
row = []
|
|
336
367
|
for key in keys:
|
|
337
368
|
original_key = list(subdict.keys())[keys.index(key)]
|
|
338
369
|
value = subdict.get(original_key, "")
|
|
339
|
-
row.append(
|
|
370
|
+
row.append(str(value).center(column_widths[key]))
|
|
340
371
|
content_rows.append(column_delimiter + column_delimiter.join(row) + column_delimiter)
|
|
341
372
|
|
|
342
373
|
# Combine all parts
|
|
@@ -404,8 +435,7 @@ def format_table_from_list(dict_list,
|
|
|
404
435
|
If display_index = True, set the number for the index to start from.
|
|
405
436
|
Default value is 1.
|
|
406
437
|
column_delimiter : str
|
|
407
|
-
Character to delimit the columns, applying both for the header and the content.
|
|
408
|
-
|
|
438
|
+
Character to delimit the columns, applying both for the header and the content.
|
|
409
439
|
|
|
410
440
|
Raises
|
|
411
441
|
------
|
|
@@ -459,7 +489,7 @@ def format_table_from_list(dict_list,
|
|
|
459
489
|
|
|
460
490
|
# Build the header string
|
|
461
491
|
header_row = column_delimiter + \
|
|
462
|
-
column_delimiter.join(
|
|
492
|
+
column_delimiter.join(header.center(column_widths[header])
|
|
463
493
|
for header in headers) + \
|
|
464
494
|
column_delimiter
|
|
465
495
|
|
|
@@ -474,13 +504,13 @@ def format_table_from_list(dict_list,
|
|
|
474
504
|
content_rows = []
|
|
475
505
|
for idx, subdict in enumerate(dict_list, start=custom_start_index):
|
|
476
506
|
if display_index:
|
|
477
|
-
row = [
|
|
507
|
+
row = [str(idx).center(column_widths[index_header])]
|
|
478
508
|
else:
|
|
479
509
|
row = []
|
|
480
510
|
for key in keys:
|
|
481
511
|
original_key = list(subdict.keys())[keys.index(key)]
|
|
482
512
|
value = subdict.get(original_key, "")
|
|
483
|
-
row.append(
|
|
513
|
+
row.append(str(value).center(column_widths[key]))
|
|
484
514
|
content_rows.append(column_delimiter + column_delimiter.join(row) + column_delimiter)
|
|
485
515
|
|
|
486
516
|
# Combine all parts
|
|
@@ -488,16 +518,10 @@ def format_table_from_list(dict_list,
|
|
|
488
518
|
return table
|
|
489
519
|
|
|
490
520
|
# # Example usage
|
|
491
|
-
# # dict_list = [
|
|
492
|
-
# # {'key1': 'val11', 'key2': 'val12'},
|
|
493
|
-
# # {'key1': 'val21', 'key2': 'val22'},
|
|
494
|
-
# # {'key1': 'val31', 'key2': 'val32'}
|
|
495
|
-
# # ]
|
|
496
|
-
|
|
497
521
|
# dict_list = [
|
|
498
|
-
# {'
|
|
499
|
-
# {'
|
|
500
|
-
# {'
|
|
522
|
+
# {'key1': 'val11', 'key2': 'val12'},
|
|
523
|
+
# {'key1': 'val21', 'key2': 'val22'},
|
|
524
|
+
# {'key1': 'val31', 'key2': 'val32'}
|
|
501
525
|
# ]
|
|
502
526
|
|
|
503
527
|
# # Print the table with the index
|
|
@@ -510,7 +534,7 @@ def format_table_from_list(dict_list,
|
|
|
510
534
|
|
|
511
535
|
# # Print the table with custom headers
|
|
512
536
|
# custom_keys = ['Futbola', 'Esku pilota edo pala']
|
|
513
|
-
# print(format_table_from_list(dict_list, keys=custom_keys, display_index=
|
|
537
|
+
# print(format_table_from_list(dict_list, keys=custom_keys, display_index=False))
|
|
514
538
|
|
|
515
539
|
|
|
516
540
|
def format_table_from_lists(keys, values,
|
|
@@ -594,7 +618,7 @@ def format_table_from_lists(keys, values,
|
|
|
594
618
|
headers = keys
|
|
595
619
|
|
|
596
620
|
# Build the header string
|
|
597
|
-
header_row = column_delimiter + column_delimiter.join(
|
|
621
|
+
header_row = column_delimiter + column_delimiter.join(header.center(column_widths[header])
|
|
598
622
|
for header in headers) + column_delimiter
|
|
599
623
|
|
|
600
624
|
# Build the header underline string
|
|
@@ -607,11 +631,11 @@ def format_table_from_lists(keys, values,
|
|
|
607
631
|
content_rows = []
|
|
608
632
|
for idx, row in enumerate(rows, start=custom_start_index):
|
|
609
633
|
if display_index:
|
|
610
|
-
row_content = [
|
|
634
|
+
row_content = [str(idx).center(column_widths[index_header])]
|
|
611
635
|
else:
|
|
612
636
|
row_content = []
|
|
613
637
|
for key, value in zip(keys, row):
|
|
614
|
-
row_content.append(
|
|
638
|
+
row_content.append(str(value).center(column_widths[key]))
|
|
615
639
|
content_rows.append(column_delimiter + column_delimiter.join(row_content) + column_delimiter)
|
|
616
640
|
|
|
617
641
|
# Combine all parts
|
|
@@ -642,8 +666,5 @@ MAIN_INPUT_DTYPE_LIST_STRFMT = ["list", "ndarray", "tuple"]
|
|
|
642
666
|
# Error strings #
|
|
643
667
|
TYPE_ERROR_STR1 = "Check the iterable type passed to the instance."
|
|
644
668
|
TYPE_ERROR_STR2 = "Argument must be of type 'str' only."
|
|
645
|
-
|
|
646
669
|
INDEX_ERROR_STR = "Not all indices were referenced in the string to format."
|
|
647
|
-
|
|
648
|
-
SYNTAX_ERROR_STR = "One or more arguments in the formatting object "\
|
|
649
|
-
"has strings with unclosed quotes."
|
|
670
|
+
SYNTAX_ERROR_STR = "One or more arguments in the formatting object has strings with unclosed quotes."
|
|
@@ -10,7 +10,6 @@ import time
|
|
|
10
10
|
from datetime import datetime
|
|
11
11
|
|
|
12
12
|
# Third-party modules #
|
|
13
|
-
import arrow
|
|
14
13
|
import numpy as np
|
|
15
14
|
import pandas as pd
|
|
16
15
|
from dateutil import parser
|
|
@@ -113,13 +112,42 @@ def _validate_unit(unit, module):
|
|
|
113
112
|
raise ValueError("Unsupported date unit for pandas.Timestamp objects. "
|
|
114
113
|
f"Choose one from {PANDAS_DATE_UNIT_LIST}.")
|
|
115
114
|
|
|
115
|
+
# %% UTILITY FUNCTIONS
|
|
116
|
+
|
|
117
|
+
def _arrow_get_with_import(*args, **kwargs):
|
|
118
|
+
"""
|
|
119
|
+
Helper function to import arrow and call arrow.get() with lazy import.
|
|
120
|
+
|
|
121
|
+
Parameters
|
|
122
|
+
----------
|
|
123
|
+
*args : Any
|
|
124
|
+
Arguments to pass to arrow.get()
|
|
125
|
+
**kwargs : Any
|
|
126
|
+
Keyword arguments to pass to arrow.get()
|
|
127
|
+
|
|
128
|
+
Returns
|
|
129
|
+
-------
|
|
130
|
+
arrow.Arrow
|
|
131
|
+
The result of arrow.get(*args, **kwargs)
|
|
132
|
+
|
|
133
|
+
Raises
|
|
134
|
+
------
|
|
135
|
+
ImportError
|
|
136
|
+
If arrow package is not installed
|
|
137
|
+
"""
|
|
138
|
+
try:
|
|
139
|
+
import arrow
|
|
140
|
+
except ImportError:
|
|
141
|
+
raise ImportError("arrow package is required for arrow operations. Install with: pip install arrow")
|
|
142
|
+
|
|
143
|
+
return arrow.get(*args, **kwargs)
|
|
116
144
|
|
|
117
145
|
# %% SIMPLE DATA PARSING
|
|
118
146
|
|
|
119
147
|
# Input format: str #
|
|
120
148
|
#-------------------#
|
|
121
149
|
|
|
122
|
-
def parse_dt_string(datetime_str, dt_fmt_str=None, module="datetime", unit="ns"):
|
|
150
|
+
def parse_dt_string(datetime_str, dt_fmt_str=None, module="datetime", unit="ns", dayfirst=False, yearfirst=False):
|
|
123
151
|
"""
|
|
124
152
|
Convert a time string or object to a date/time object using a specified library.
|
|
125
153
|
|
|
@@ -153,6 +181,15 @@ def parse_dt_string(datetime_str, dt_fmt_str=None, module="datetime", unit="ns")
|
|
|
153
181
|
and 'us' for NumPy.
|
|
154
182
|
Then, in order to maintain compatibility, the largest common time unit 'us'
|
|
155
183
|
has been defined as default in this function.
|
|
184
|
+
dayfirst : bool, default False
|
|
185
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
186
|
+
If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.
|
|
187
|
+
Only applies when module is 'pandas'.
|
|
188
|
+
yearfirst : bool, default False
|
|
189
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
190
|
+
If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
|
|
191
|
+
If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
|
|
192
|
+
Only applies when module is 'pandas'.
|
|
156
193
|
|
|
157
194
|
Returns
|
|
158
195
|
-------
|
|
@@ -187,13 +224,13 @@ def parse_dt_string(datetime_str, dt_fmt_str=None, module="datetime", unit="ns")
|
|
|
187
224
|
if module == "pandas":
|
|
188
225
|
# If datetime_str is a string and looks like a numeric timestamp, use unit
|
|
189
226
|
if isinstance(datetime_str, str) and (datetime_str.isdigit() or (datetime_str.replace('.', '', 1).isdigit() and datetime_str.count('.') <= 1)):
|
|
190
|
-
datetime_obj = pd.to_datetime(datetime_str, unit=unit)
|
|
227
|
+
datetime_obj = pd.to_datetime(datetime_str, unit=unit, dayfirst=dayfirst, yearfirst=yearfirst)
|
|
191
228
|
else:
|
|
192
229
|
# Otherwise use format if provided, or let pandas infer the format
|
|
193
230
|
if dt_fmt_str is None:
|
|
194
|
-
datetime_obj = pd.to_datetime(datetime_str)
|
|
231
|
+
datetime_obj = pd.to_datetime(datetime_str, dayfirst=dayfirst, yearfirst=yearfirst)
|
|
195
232
|
else:
|
|
196
|
-
datetime_obj = parse_func(datetime_str, dt_fmt_str, unit)
|
|
233
|
+
datetime_obj = parse_func(datetime_str, dt_fmt_str, unit, dayfirst, yearfirst)
|
|
197
234
|
else:
|
|
198
235
|
# For other modules, use the parse_func as defined
|
|
199
236
|
datetime_obj = parse_func(datetime_str, dt_fmt_str, unit) if module == "numpy" else parse_func(datetime_str, dt_fmt_str)
|
|
@@ -215,7 +252,9 @@ def parse_float_dt(datetime_float,
|
|
|
215
252
|
origin="unix",
|
|
216
253
|
unit="us",
|
|
217
254
|
dt_fmt_str=None,
|
|
218
|
-
module="datetime"
|
|
255
|
+
module="datetime",
|
|
256
|
+
dayfirst=False,
|
|
257
|
+
yearfirst=False):
|
|
219
258
|
"""
|
|
220
259
|
Converts an integer or float time to a date/time object.
|
|
221
260
|
It also converts to a string representation if requested.
|
|
@@ -247,6 +286,15 @@ def parse_float_dt(datetime_float,
|
|
|
247
286
|
module : {"datetime", "time", "pandas", "numpy", "arrow", "str"}, default 'datetime'.
|
|
248
287
|
The module or class used to parse the floated time.
|
|
249
288
|
If 'numpy', datetime_float represents an offset from the Unix epoch start.
|
|
289
|
+
dayfirst : bool, default False
|
|
290
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
291
|
+
If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.
|
|
292
|
+
Only applies when module is 'pandas'.
|
|
293
|
+
yearfirst : bool, default False
|
|
294
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
295
|
+
If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
|
|
296
|
+
If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
|
|
297
|
+
Only applies when module is 'pandas'.
|
|
250
298
|
|
|
251
299
|
Returns
|
|
252
300
|
-------
|
|
@@ -285,9 +333,11 @@ def parse_float_dt(datetime_float,
|
|
|
285
333
|
origin,
|
|
286
334
|
dt_fmt_str,
|
|
287
335
|
unit,
|
|
288
|
-
module
|
|
336
|
+
module,
|
|
337
|
+
dayfirst,
|
|
338
|
+
yearfirst)
|
|
289
339
|
else:
|
|
290
|
-
return _float_dt_parser(datetime_float, module, unit)
|
|
340
|
+
return _float_dt_parser(datetime_float, module, unit, dayfirst, yearfirst)
|
|
291
341
|
|
|
292
342
|
|
|
293
343
|
# Auxiliary functions #
|
|
@@ -298,7 +348,9 @@ def _parse_float_to_string(floated_time,
|
|
|
298
348
|
origin,
|
|
299
349
|
dt_fmt_str,
|
|
300
350
|
unit,
|
|
301
|
-
module
|
|
351
|
+
module,
|
|
352
|
+
dayfirst,
|
|
353
|
+
yearfirst):
|
|
302
354
|
"""
|
|
303
355
|
Converts a floated time to its string representation.
|
|
304
356
|
|
|
@@ -317,6 +369,15 @@ def _parse_float_to_string(floated_time,
|
|
|
317
369
|
Time unit for `floated_time` if `origin='unix'` and `module` in {'numpy', 'pandas'}.
|
|
318
370
|
module : {"datetime", "time", "pandas", "numpy", "arrow"}
|
|
319
371
|
Module used for parsing.
|
|
372
|
+
dayfirst : bool, default False
|
|
373
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
374
|
+
If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.
|
|
375
|
+
Only applies when module is 'pandas'.
|
|
376
|
+
yearfirst : bool, default False
|
|
377
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
378
|
+
If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
|
|
379
|
+
If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
|
|
380
|
+
Only applies when module is 'pandas'.
|
|
320
381
|
|
|
321
382
|
Returns
|
|
322
383
|
-------
|
|
@@ -332,18 +393,18 @@ def _parse_float_to_string(floated_time,
|
|
|
332
393
|
if frac_precision is not None:
|
|
333
394
|
if frac_precision <= 6:
|
|
334
395
|
dt_seconds = round(floated_time)
|
|
335
|
-
dt_obj = _float_dt_parser(dt_seconds, module, unit)
|
|
396
|
+
dt_obj = _float_dt_parser(dt_seconds, module, unit, dayfirst, yearfirst)
|
|
336
397
|
dt_str = dt_obj.strftime(dt_fmt_str)
|
|
337
398
|
elif 7 <= frac_precision <= 9:
|
|
338
399
|
return get_nano_datetime(floated_time, module)
|
|
339
400
|
# Keep the original precision #
|
|
340
401
|
else:
|
|
341
|
-
dt_str = _float_dt_parser(floated_time, module, unit).strftime(dt_fmt_str)
|
|
402
|
+
dt_str = _float_dt_parser(floated_time, module, unit, dayfirst, yearfirst).strftime(dt_fmt_str)
|
|
342
403
|
|
|
343
404
|
return dt_str
|
|
344
405
|
|
|
345
406
|
|
|
346
|
-
def _float_dt_parser(floated_time, module, unit):
|
|
407
|
+
def _float_dt_parser(floated_time, module, unit, dayfirst, yearfirst):
|
|
347
408
|
"""
|
|
348
409
|
Parses a floated time into a date/time object.
|
|
349
410
|
|
|
@@ -355,6 +416,15 @@ def _float_dt_parser(floated_time, module, unit):
|
|
|
355
416
|
Module used for parsing.
|
|
356
417
|
unit : str, optional
|
|
357
418
|
Time unit for `floated_time` if `module` in {'numpy', 'pandas'}.
|
|
419
|
+
dayfirst : bool, default False
|
|
420
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
421
|
+
If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.
|
|
422
|
+
Only applies when module is 'pandas'.
|
|
423
|
+
yearfirst : bool, default False
|
|
424
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
425
|
+
If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
|
|
426
|
+
If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
|
|
427
|
+
Only applies when module is 'pandas'.
|
|
358
428
|
|
|
359
429
|
Returns
|
|
360
430
|
-------
|
|
@@ -375,7 +445,7 @@ def _float_dt_parser(floated_time, module, unit):
|
|
|
375
445
|
# Calculate datetime object #
|
|
376
446
|
#############################
|
|
377
447
|
|
|
378
|
-
datetime_obj = FLOATED_TIME_PARSING_DICT.get(module)(floated_time, unit)
|
|
448
|
+
datetime_obj = FLOATED_TIME_PARSING_DICT.get(module)(floated_time, unit, dayfirst, yearfirst)
|
|
379
449
|
return datetime_obj
|
|
380
450
|
|
|
381
451
|
|
|
@@ -454,7 +524,9 @@ def dt_obj_converter(datetime_obj,
|
|
|
454
524
|
unit="s",
|
|
455
525
|
float_class="d",
|
|
456
526
|
int_class="int",
|
|
457
|
-
dt_fmt_str=None
|
|
527
|
+
dt_fmt_str=None,
|
|
528
|
+
dayfirst=False,
|
|
529
|
+
yearfirst=False):
|
|
458
530
|
|
|
459
531
|
"""
|
|
460
532
|
Convert a date/time object to another, including float and string representation.
|
|
@@ -486,6 +558,15 @@ def dt_obj_converter(datetime_obj,
|
|
|
486
558
|
The integer precision class. Default is `"int"` (signed integer type).
|
|
487
559
|
dt_fmt_str : str
|
|
488
560
|
Format string to convert the date/time object to a string.
|
|
561
|
+
dayfirst : bool, default False
|
|
562
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
563
|
+
If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.
|
|
564
|
+
Only applies when converting to pandas objects.
|
|
565
|
+
yearfirst : bool, default False
|
|
566
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
567
|
+
If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
|
|
568
|
+
If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
|
|
569
|
+
Only applies when converting to pandas objects.
|
|
489
570
|
|
|
490
571
|
Returns
|
|
491
572
|
-------
|
|
@@ -559,7 +640,7 @@ def dt_obj_converter(datetime_obj,
|
|
|
559
640
|
conversion_dict = CONVERSION_OPT_DICT[obj_type]
|
|
560
641
|
return perform_conversion(conversion_dict, datetime_obj, unit=unit,
|
|
561
642
|
float_class=float_class, int_class=int_class,
|
|
562
|
-
dt_fmt_str=dt_fmt_str)
|
|
643
|
+
dt_fmt_str=dt_fmt_str, dayfirst=dayfirst, yearfirst=yearfirst)
|
|
563
644
|
|
|
564
645
|
|
|
565
646
|
# Auxiliary functions #
|
|
@@ -569,7 +650,7 @@ def dt_obj_converter(datetime_obj,
|
|
|
569
650
|
#-#-#-#-#-#-#-#-#-#-#-#-#-
|
|
570
651
|
|
|
571
652
|
# Scalar complex data #
|
|
572
|
-
def _total_dt_unit(datetime_obj, unit, float_class, int_class):
|
|
653
|
+
def _total_dt_unit(datetime_obj, unit, float_class=None, int_class=None):
|
|
573
654
|
"""
|
|
574
655
|
Convert a datetime object into total time based on the specified unit
|
|
575
656
|
(e.g., seconds, microseconds, nanoseconds).
|
|
@@ -756,7 +837,7 @@ def _to_string(dt_obj, unit, dt_fmt_str):
|
|
|
756
837
|
return str(dt_obj)
|
|
757
838
|
|
|
758
839
|
|
|
759
|
-
def _to_float(dt_obj, unit, float_class):
|
|
840
|
+
def _to_float(dt_obj, unit=None, float_class=None):
|
|
760
841
|
"""
|
|
761
842
|
Convert a datetime object to a float representing the total time in the specified unit.
|
|
762
843
|
|
|
@@ -776,7 +857,7 @@ def _to_float(dt_obj, unit, float_class):
|
|
|
776
857
|
"""
|
|
777
858
|
obj_type = get_type_str(dt_obj, lowercase=True)
|
|
778
859
|
if obj_type == "datetime64":
|
|
779
|
-
return dt_obj.astype(f"timedelta64[{unit}]").astype(float_class)
|
|
860
|
+
return dt_obj.astype(f"timedelta64[{unit}]").astype(float_class) if unit and float_class else dt_obj.astype(float)
|
|
780
861
|
elif obj_type == "time": # datetime.time
|
|
781
862
|
return __time_component_to_float(dt_obj)
|
|
782
863
|
if hasattr(dt_obj, 'timestamp'):
|
|
@@ -805,7 +886,7 @@ def __time_component_to_float(t):
|
|
|
805
886
|
return time_component_float
|
|
806
887
|
|
|
807
888
|
|
|
808
|
-
def _to_datetime(dt_obj, unit):
|
|
889
|
+
def _to_datetime(dt_obj, unit=None):
|
|
809
890
|
"""
|
|
810
891
|
Convert a given datetime-like object or each value in DataFrame/Series to a
|
|
811
892
|
standard Python datetime object.
|
|
@@ -845,7 +926,7 @@ def _to_datetime(dt_obj, unit):
|
|
|
845
926
|
else:
|
|
846
927
|
if obj_type == "datetime64":
|
|
847
928
|
# If unit is 'ns' (nanoseconds), use the auxiliary conversion function
|
|
848
|
-
unit = get_datetime_object_unit(dt_obj)
|
|
929
|
+
unit = unit or get_datetime_object_unit(dt_obj)
|
|
849
930
|
if unit == "ns":
|
|
850
931
|
return _to_datetime_aux(dt_obj, unit)
|
|
851
932
|
else:
|
|
@@ -876,7 +957,7 @@ def _to_datetime_aux(dt_obj, unit):
|
|
|
876
957
|
return pd.to_datetime(dt_obj, unit=unit).to_pydatetime()
|
|
877
958
|
|
|
878
959
|
|
|
879
|
-
def _to_time_struct(dt_obj):
|
|
960
|
+
def _to_time_struct(dt_obj, unit=None):
|
|
880
961
|
"""
|
|
881
962
|
Convert a datetime-like object to a time.struct_time object.
|
|
882
963
|
|
|
@@ -884,15 +965,17 @@ def _to_time_struct(dt_obj):
|
|
|
884
965
|
----------
|
|
885
966
|
dt_obj : datetime-like
|
|
886
967
|
The object to be converted to time.struct_time.
|
|
968
|
+
unit : str, optional
|
|
969
|
+
The unit for conversion (if needed).
|
|
887
970
|
|
|
888
971
|
Returns
|
|
889
972
|
-------
|
|
890
973
|
time.struct_time
|
|
891
974
|
The converted time.struct_time object.
|
|
892
975
|
"""
|
|
893
|
-
return _to_datetime(dt_obj).timetuple()
|
|
976
|
+
return _to_datetime(dt_obj, unit).timetuple()
|
|
894
977
|
|
|
895
|
-
def _to_pandas(dt_obj, unit):
|
|
978
|
+
def _to_pandas(dt_obj, unit, dayfirst=False, yearfirst=False):
|
|
896
979
|
"""
|
|
897
980
|
Convert a datetime-like object to a pandas Timestamp object with the specified unit.
|
|
898
981
|
|
|
@@ -902,15 +985,22 @@ def _to_pandas(dt_obj, unit):
|
|
|
902
985
|
The object to be converted to a pandas Timestamp.
|
|
903
986
|
unit : str, optional
|
|
904
987
|
The unit for conversion.
|
|
988
|
+
dayfirst : bool, default False
|
|
989
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
990
|
+
If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10.
|
|
991
|
+
yearfirst : bool, default False
|
|
992
|
+
Specify a date parse order if datetime_str is str or is list-like.
|
|
993
|
+
If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12.
|
|
994
|
+
If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil).
|
|
905
995
|
|
|
906
996
|
Returns
|
|
907
997
|
-------
|
|
908
998
|
pd.Timestamp
|
|
909
999
|
The converted pandas Timestamp object.
|
|
910
1000
|
"""
|
|
911
|
-
return pd.to_datetime(_to_datetime(dt_obj), unit=unit)
|
|
1001
|
+
return pd.to_datetime(_to_datetime(dt_obj), unit=unit, dayfirst=dayfirst, yearfirst=yearfirst)
|
|
912
1002
|
|
|
913
|
-
def _to_numpy(dt_obj, unit):
|
|
1003
|
+
def _to_numpy(dt_obj, unit=None):
|
|
914
1004
|
"""
|
|
915
1005
|
Convert a datetime-like object to a NumPy datetime64 object with the specified unit.
|
|
916
1006
|
|
|
@@ -926,7 +1016,7 @@ def _to_numpy(dt_obj, unit):
|
|
|
926
1016
|
np.datetime64
|
|
927
1017
|
The converted NumPy datetime64 object.
|
|
928
1018
|
"""
|
|
929
|
-
dt_obj = _to_datetime(dt_obj)
|
|
1019
|
+
dt_obj = _to_datetime(dt_obj, unit)
|
|
930
1020
|
return np.datetime64(_tzinfo_remover(dt_obj), unit)
|
|
931
1021
|
|
|
932
1022
|
def _to_arrow(dt_obj):
|
|
@@ -943,6 +1033,11 @@ def _to_arrow(dt_obj):
|
|
|
943
1033
|
arrow.Arrow
|
|
944
1034
|
The converted Arrow object.
|
|
945
1035
|
"""
|
|
1036
|
+
try:
|
|
1037
|
+
import arrow
|
|
1038
|
+
except ImportError:
|
|
1039
|
+
raise ImportError("arrow package is required for arrow conversion. Install with: pip install arrow")
|
|
1040
|
+
|
|
946
1041
|
dt_obj = _to_datetime(dt_obj)
|
|
947
1042
|
return arrow.get(dt_obj)
|
|
948
1043
|
|
|
@@ -965,20 +1060,20 @@ _INT_CLASS_LIST = [np.int8, np.int16, "i", np.float32, "int", np.int64]
|
|
|
965
1060
|
TIME_STR_PARSING_DICT = {
|
|
966
1061
|
"datetime" : lambda datetime_str, dt_fmt_str: datetime.strptime(datetime_str, dt_fmt_str),
|
|
967
1062
|
"dateutil" : lambda datetime_str, dt_fmt_str: parser.parse(datetime_str, dt_fmt_str),
|
|
968
|
-
"pandas" : lambda datetime_str, dt_fmt_str,
|
|
1063
|
+
"pandas" : lambda datetime_str, dt_fmt_str, unit, dayfirst=False, yearfirst=False : pd.to_datetime(datetime_str, format=dt_fmt_str, dayfirst=dayfirst, yearfirst=yearfirst),
|
|
969
1064
|
"numpy" : lambda datetime_str, _, unit : np.datetime64(datetime_str, unit),
|
|
970
|
-
"arrow" : lambda datetime_str, dt_fmt_str:
|
|
1065
|
+
"arrow" : lambda datetime_str, dt_fmt_str: _arrow_get_with_import(datetime_str, dt_fmt_str)
|
|
971
1066
|
}
|
|
972
1067
|
|
|
973
1068
|
# Floated #
|
|
974
1069
|
#-#-#-#-#-#
|
|
975
1070
|
|
|
976
1071
|
FLOATED_TIME_PARSING_DICT = {
|
|
977
|
-
"datetime" : lambda floated_time,
|
|
978
|
-
"time" : lambda floated_time : datetime(*tuple(time.localtime(floated_time))[:6]),
|
|
979
|
-
"pandas" : lambda floated_time, unit : pd.to_datetime(floated_time, unit=unit),
|
|
980
|
-
"numpy" : lambda floated_time, unit : np.datetime64(floated_time, unit),
|
|
981
|
-
"arrow" : lambda floated_time,
|
|
1072
|
+
"datetime" : lambda floated_time, unit, dayfirst=False, yearfirst=False : datetime.fromtimestamp(floated_time),
|
|
1073
|
+
"time" : lambda floated_time, unit, dayfirst=False, yearfirst=False : datetime(*tuple(time.localtime(floated_time))[:6]),
|
|
1074
|
+
"pandas" : lambda floated_time, unit, dayfirst=False, yearfirst=False : pd.to_datetime(floated_time, unit=unit, dayfirst=dayfirst, yearfirst=yearfirst),
|
|
1075
|
+
"numpy" : lambda floated_time, unit, dayfirst=False, yearfirst=False : np.datetime64(floated_time, unit),
|
|
1076
|
+
"arrow" : lambda floated_time, unit, dayfirst=False, yearfirst=False : _arrow_get_with_import(floated_time)
|
|
982
1077
|
}
|
|
983
1078
|
|
|
984
1079
|
# Complex data #
|
|
@@ -986,63 +1081,63 @@ FLOATED_TIME_PARSING_DICT = {
|
|
|
986
1081
|
|
|
987
1082
|
# To other objects #
|
|
988
1083
|
DT_OBJ_CONVERSION_DICT = {
|
|
989
|
-
"float" : lambda dt_obj,
|
|
990
|
-
"time" : lambda dt_obj,
|
|
991
|
-
"pandas" : lambda dt_obj, unit,
|
|
992
|
-
"numpy" : lambda dt_obj, unit,
|
|
993
|
-
"arrow" : lambda dt_obj,
|
|
994
|
-
"str" : lambda dt_obj,
|
|
1084
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : dt_obj.timestamp(),
|
|
1085
|
+
"time" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : dt_obj.timetuple(),
|
|
1086
|
+
"pandas" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_pandas(_tzinfo_remover(dt_obj), unit, dayfirst, yearfirst),
|
|
1087
|
+
"numpy" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_numpy(_tzinfo_remover(dt_obj), unit),
|
|
1088
|
+
"arrow" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_arrow(dt_obj),
|
|
1089
|
+
"str" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_string(dt_obj, unit, dt_fmt_str)
|
|
995
1090
|
}
|
|
996
1091
|
|
|
997
1092
|
DT64_OBJ_CONVERSION_DICT = {
|
|
998
|
-
"float" : lambda dt_obj, unit,
|
|
999
|
-
"datetime" : lambda dt_obj, unit : _to_datetime(dt_obj, unit),
|
|
1000
|
-
"time" : lambda dt_obj,
|
|
1001
|
-
"pandas" : lambda dt_obj, unit : _to_pandas(dt_obj, unit),
|
|
1002
|
-
"arrow" : lambda dt_obj,
|
|
1003
|
-
"str" : lambda dt_obj,
|
|
1093
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_float(dt_obj, unit, float_class),
|
|
1094
|
+
"datetime" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_datetime(dt_obj, unit),
|
|
1095
|
+
"time" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_time_struct(dt_obj),
|
|
1096
|
+
"pandas" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_pandas(dt_obj, unit, dayfirst, yearfirst),
|
|
1097
|
+
"arrow" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_arrow(dt_obj),
|
|
1098
|
+
"str" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_string(_to_datetime(dt_obj), unit, dt_fmt_str)
|
|
1004
1099
|
}
|
|
1005
1100
|
|
|
1006
1101
|
DT_TIME_OBJ_CONVERSION_DICT = {
|
|
1007
|
-
"float" : lambda dt_obj,
|
|
1008
|
-
"datetime" : lambda dt_obj,
|
|
1009
|
-
"time" : lambda dt_obj,
|
|
1010
|
-
"pandas" : lambda dt_obj,
|
|
1011
|
-
"numpy" : lambda dt_obj,
|
|
1012
|
-
"arrow" : lambda dt_obj,
|
|
1013
|
-
"str" : lambda dt_obj,
|
|
1102
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_float(dt_obj, unit, float_class),
|
|
1103
|
+
"datetime" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_datetime(dt_obj, unit),
|
|
1104
|
+
"time" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_time_struct(dt_obj),
|
|
1105
|
+
"pandas" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_pandas(_to_datetime(dt_obj), unit, dayfirst, yearfirst),
|
|
1106
|
+
"numpy" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_numpy(_to_datetime(dt_obj), unit),
|
|
1107
|
+
"arrow" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_arrow(dt_obj),
|
|
1108
|
+
"str" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_string(dt_obj, unit, dt_fmt_str)
|
|
1014
1109
|
}
|
|
1015
1110
|
|
|
1016
1111
|
TIMESTAMP_OBJ_CONVERSION_DICT = {
|
|
1017
|
-
"float" : lambda dt_obj,
|
|
1018
|
-
"datetime" : lambda dt_obj, unit,
|
|
1019
|
-
"time" : lambda dt_obj,
|
|
1020
|
-
"numpy" : lambda dt_obj,
|
|
1021
|
-
"arrow" : lambda dt_obj,
|
|
1022
|
-
"str" : lambda dt_obj,
|
|
1112
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_float(dt_obj, unit, float_class),
|
|
1113
|
+
"datetime" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_datetime(dt_obj, unit),
|
|
1114
|
+
"time" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_time_struct(dt_obj),
|
|
1115
|
+
"numpy" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : dt_obj.to_numpy(),
|
|
1116
|
+
"arrow" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_arrow(dt_obj),
|
|
1117
|
+
"str" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_string(dt_obj, unit, dt_fmt_str)
|
|
1023
1118
|
}
|
|
1024
1119
|
|
|
1025
1120
|
ARROW_OBJ_CONVERSION_DICT = {
|
|
1026
|
-
"float" : lambda dt_obj,
|
|
1027
|
-
"datetime" : lambda dt_obj, unit : _to_datetime(dt_obj, unit),
|
|
1028
|
-
"time" : lambda dt_obj,
|
|
1029
|
-
"pandas" : lambda dt_obj, unit,
|
|
1030
|
-
"numpy" : lambda dt_obj, unit,
|
|
1031
|
-
"str" : lambda dt_obj,
|
|
1121
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_float(dt_obj, unit, float_class),
|
|
1122
|
+
"datetime" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_datetime(dt_obj, unit),
|
|
1123
|
+
"time" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_time_struct(dt_obj),
|
|
1124
|
+
"pandas" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_pandas(dt_obj, unit, dayfirst, yearfirst),
|
|
1125
|
+
"numpy" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_numpy(dt_obj, unit),
|
|
1126
|
+
"str" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_string(dt_obj, unit, dt_fmt_str)
|
|
1032
1127
|
}
|
|
1033
1128
|
|
|
1034
1129
|
TIME_STT_OBJ_CONVERSION_DICT = {
|
|
1035
|
-
"float" : lambda dt_obj,
|
|
1036
|
-
"datetime" : lambda dt_obj, unit : _to_datetime(dt_obj, unit),
|
|
1037
|
-
"pandas" : lambda dt_obj, unit : pd.Timestamp(*dt_obj[:6], unit=unit),
|
|
1038
|
-
"numpy" : lambda dt_obj, unit : np.datetime64(datetime(*dt_obj[:6]), unit),
|
|
1039
|
-
"arrow" : lambda dt_obj,
|
|
1130
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_float(dt_obj, unit, float_class),
|
|
1131
|
+
"datetime" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_datetime(dt_obj, unit),
|
|
1132
|
+
"pandas" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : pd.Timestamp(*dt_obj[:6], unit=unit),
|
|
1133
|
+
"numpy" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : np.datetime64(datetime(*dt_obj[:6]), unit),
|
|
1134
|
+
"arrow" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_arrow(dt_obj),
|
|
1040
1135
|
}
|
|
1041
1136
|
|
|
1042
1137
|
_DT_LIKE_OBJ_CONVERSION_DICT = {
|
|
1043
|
-
"float" : lambda dt_obj, unit,
|
|
1044
|
-
"pandas" : lambda dt_obj, unit,
|
|
1045
|
-
"str" : lambda dt_obj, unit, dt_fmt_str : _to_string(dt_obj,
|
|
1138
|
+
"float" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _total_dt_unit(dt_obj, unit, float_class, int_class),
|
|
1139
|
+
"pandas" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_datetime(dt_obj, unit),
|
|
1140
|
+
"str" : lambda dt_obj, unit=None, float_class=None, int_class=None, dt_fmt_str=None, dayfirst=False, yearfirst=False : _to_string(dt_obj, unit, dt_fmt_str)
|
|
1046
1141
|
}
|
|
1047
1142
|
|
|
1048
1143
|
# Enumerate all possibilities #
|
|
@@ -14,7 +14,6 @@ import time
|
|
|
14
14
|
from datetime import datetime, timedelta
|
|
15
15
|
|
|
16
16
|
# Third-party modules #
|
|
17
|
-
import arrow
|
|
18
17
|
import numpy as np
|
|
19
18
|
import pandas as pd
|
|
20
19
|
|
|
@@ -124,6 +123,10 @@ def _convert_floated_time_to_datetime(floated_time, module):
|
|
|
124
123
|
elif module == "numpy":
|
|
125
124
|
dt = pd.Timestamp.fromtimestamp(seconds).to_pydatetime() # Convert via pandas for compatibility
|
|
126
125
|
elif module == "arrow":
|
|
126
|
+
try:
|
|
127
|
+
import arrow
|
|
128
|
+
except ImportError:
|
|
129
|
+
raise ImportError("arrow package is required for arrow module conversion. Install with: pip install arrow")
|
|
127
130
|
dt = arrow.get(seconds).datetime
|
|
128
131
|
elif module == "time":
|
|
129
132
|
dt = datetime.fromtimestamp(seconds) # time module uses same as datetime
|
|
@@ -232,7 +235,8 @@ def datetime_obj_converter(datetime_obj,
|
|
|
232
235
|
return datetime_obj.to_pydatetime()
|
|
233
236
|
if isinstance(datetime_obj, np.datetime64):
|
|
234
237
|
return pd.Timestamp(datetime_obj).to_pydatetime()
|
|
235
|
-
if
|
|
238
|
+
# Check if the object is an Arrow object using string comparison to avoid import
|
|
239
|
+
if obj_type == "arrow":
|
|
236
240
|
return datetime_obj.datetime
|
|
237
241
|
raise ValueError(f"Cannot convert {obj_type} to datetime")
|
|
238
242
|
|
|
@@ -244,7 +248,7 @@ def datetime_obj_converter(datetime_obj,
|
|
|
244
248
|
return pd.Timestamp(datetime_obj)
|
|
245
249
|
if isinstance(datetime_obj, np.datetime64):
|
|
246
250
|
return pd.Timestamp(datetime_obj, unit=unit)
|
|
247
|
-
if
|
|
251
|
+
if obj_type == "arrow":
|
|
248
252
|
return pd.Timestamp(datetime_obj.datetime)
|
|
249
253
|
raise ValueError(f"Cannot convert {obj_type} to Timestamp")
|
|
250
254
|
|
|
@@ -254,13 +258,18 @@ def datetime_obj_converter(datetime_obj,
|
|
|
254
258
|
return datetime_obj.astype(f'datetime64[{unit}]')
|
|
255
259
|
if isinstance(datetime_obj, (datetime, pd.Timestamp)):
|
|
256
260
|
return np.datetime64(datetime_obj, unit)
|
|
257
|
-
if
|
|
261
|
+
if obj_type == "arrow":
|
|
258
262
|
return np.datetime64(datetime_obj.datetime, unit)
|
|
259
263
|
raise ValueError(f"Cannot convert {obj_type} to datetime64")
|
|
260
264
|
|
|
261
265
|
# Convert to arrow if requested
|
|
262
266
|
if convert_to == "arrow":
|
|
263
|
-
|
|
267
|
+
try:
|
|
268
|
+
import arrow
|
|
269
|
+
except ImportError:
|
|
270
|
+
raise ImportError("arrow package is required for arrow conversion. Install with: pip install arrow")
|
|
271
|
+
|
|
272
|
+
if obj_type == "arrow":
|
|
264
273
|
return datetime_obj
|
|
265
274
|
if isinstance(datetime_obj, datetime):
|
|
266
275
|
return arrow.get(datetime_obj)
|
|
@@ -279,7 +288,7 @@ def datetime_obj_converter(datetime_obj,
|
|
|
279
288
|
# For numpy datetime64 objects
|
|
280
289
|
timestamp = datetime_obj.astype(f'datetime64[{unit}]').astype(float_class)
|
|
281
290
|
return timestamp
|
|
282
|
-
elif
|
|
291
|
+
elif obj_type == "arrow":
|
|
283
292
|
timestamp = datetime_obj.float_timestamp
|
|
284
293
|
else:
|
|
285
294
|
raise ValueError(f"Cannot convert {obj_type} to float")
|
|
@@ -303,7 +312,7 @@ def datetime_obj_converter(datetime_obj,
|
|
|
303
312
|
# For numpy datetime64 objects
|
|
304
313
|
timestamp = datetime_obj.astype(f'datetime64[{unit}]').astype(int_class)
|
|
305
314
|
return timestamp
|
|
306
|
-
elif
|
|
315
|
+
elif obj_type == "arrow":
|
|
307
316
|
timestamp = int(datetime_obj.float_timestamp)
|
|
308
317
|
else:
|
|
309
318
|
raise ValueError(f"Cannot convert {obj_type} to int")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pygenutils
|
|
3
|
-
Version: 16.1
|
|
3
|
+
Version: 16.2.1
|
|
4
4
|
Summary: A comprehensive Python utility library for general-purpose and specialised tasks
|
|
5
5
|
Author-email: Jon Ander Gabantxo <jagabantxo@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -47,9 +47,10 @@ Requires-Dist: numpy<2.0.0,>=1.21.0
|
|
|
47
47
|
Requires-Dist: pandas<2.0.0,>=1.3.0
|
|
48
48
|
Requires-Dist: filewise>=3.11.0
|
|
49
49
|
Requires-Dist: paramlib>=3.4.3
|
|
50
|
-
Requires-Dist: arrow>=1.2.0
|
|
51
50
|
Provides-Extra: xarray
|
|
52
51
|
Requires-Dist: xarray>=2024.2.0; extra == "xarray"
|
|
52
|
+
Provides-Extra: arrow
|
|
53
|
+
Requires-Dist: arrow>=1.2.0; extra == "arrow"
|
|
53
54
|
Provides-Extra: dev
|
|
54
55
|
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
55
56
|
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
@@ -117,11 +118,17 @@ This package has minimal external dependencies and is designed to be lightweight
|
|
|
117
118
|
**For regular users** who want to use the package in their projects:
|
|
118
119
|
|
|
119
120
|
```bash
|
|
120
|
-
pip install pygenutils
|
|
121
|
+
pip install pygenutils # Core functionality
|
|
122
|
+
pip install pygenutils[arrow] # With arrow support (optional)
|
|
121
123
|
```
|
|
122
124
|
|
|
123
125
|
This automatically installs `pygenutils` and all its dependencies with version constraints.
|
|
124
126
|
|
|
127
|
+
**How It Works Now**:
|
|
128
|
+
|
|
129
|
+
- `pip install pygenutils` - Core functionality without arrow dependency
|
|
130
|
+
- `pip install pygenutils[arrow]` - With arrow support for enhanced time handling features
|
|
131
|
+
|
|
125
132
|
### Package Updates
|
|
126
133
|
|
|
127
134
|
To stay up-to-date with the latest version of this package, simply run:
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pygenutils"
|
|
7
|
-
version = "16.1
|
|
7
|
+
version = "16.2.1"
|
|
8
8
|
license = {file = "LICENSE"}
|
|
9
9
|
description = "A comprehensive Python utility library for general-purpose and specialised tasks"
|
|
10
10
|
keywords = ["utilities", "python", "general-purpose", "file handling", "string processing"]
|
|
@@ -31,11 +31,11 @@ dependencies = [
|
|
|
31
31
|
"pandas>=1.3.0,<2.0.0",
|
|
32
32
|
"filewise>=3.11.0",
|
|
33
33
|
"paramlib>=3.4.3",
|
|
34
|
-
"arrow>=1.2.0",
|
|
35
34
|
]
|
|
36
35
|
|
|
37
36
|
[project.optional-dependencies]
|
|
38
37
|
xarray = ["xarray>=2024.2.0"]
|
|
38
|
+
arrow = ["arrow>=1.2.0"]
|
|
39
39
|
dev = [
|
|
40
40
|
"pytest>=7.0.0",
|
|
41
41
|
"pytest-cov>=4.0.0",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/audio_and_video/audio_and_video_manipulation.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{pygenutils-16.1.3 → pygenutils-16.2.1}/pygenutils/time_handling/program_snippet_exec_timers.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|