imio.pyutils 1.1.3__tar.gz → 1.2.0__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.
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/CHANGES.rst +22 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/Makefile +2 -2
- {imio_pyutils-1.1.3/imio.pyutils.egg-info → imio_pyutils-1.2.0}/PKG-INFO +38 -2
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/batching.py +7 -1
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/system.py +87 -15
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/tests.py +82 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/utils.py +77 -28
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0/imio.pyutils.egg-info}/PKG-INFO +38 -2
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio.pyutils.egg-info/requires.txt +2 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/setup.py +5 -1
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/MANIFEST.in +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/README.rst +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/__init__.py +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/__init__.py +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/bs.py +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio/pyutils/postgres.py +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio.pyutils.egg-info/SOURCES.txt +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio.pyutils.egg-info/dependency_links.txt +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio.pyutils.egg-info/namespace_packages.txt +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio.pyutils.egg-info/not-zip-safe +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/imio.pyutils.egg-info/top_level.txt +0 -0
- {imio_pyutils-1.1.3 → imio_pyutils-1.2.0}/setup.cfg +0 -0
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
Changelog
|
|
2
2
|
=========
|
|
3
3
|
|
|
4
|
+
1.2.0 (2026-01-16)
|
|
5
|
+
------------------
|
|
6
|
+
|
|
7
|
+
- Add missing `psutil` dependency
|
|
8
|
+
[laulaz]
|
|
9
|
+
- Tested with python 3.12, 3.13.
|
|
10
|
+
[sgeulette]
|
|
11
|
+
- Improved runCommand
|
|
12
|
+
[sgeulette]
|
|
13
|
+
- Improved `batching.batch_delete_files`
|
|
14
|
+
[sgeulette]
|
|
15
|
+
- Improved `system.post_request`
|
|
16
|
+
[sgeulette]
|
|
17
|
+
- Added `utils.shortuid_encode_id` and `utils.shortuid_decode_id` to encode/decode uid with shortuid.
|
|
18
|
+
[sgeulette]
|
|
19
|
+
|
|
20
|
+
1.1.4 (2025-06-06)
|
|
21
|
+
------------------
|
|
22
|
+
|
|
23
|
+
- Fixed `IndexError` in `utils.get_ordinal_clusters` when `numbers=[]`.
|
|
24
|
+
[gbastien]
|
|
25
|
+
|
|
4
26
|
1.1.3 (2025-02-05)
|
|
5
27
|
------------------
|
|
6
28
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
# The original Makefile can be found on https://github.com/IMIO/scripts-buildout
|
|
5
5
|
|
|
6
6
|
SHELL=/bin/bash
|
|
7
|
-
pythons=2.7 3.10
|
|
7
|
+
pythons=2.7 3.10 3.12 3.13
|
|
8
8
|
|
|
9
9
|
ifeq (, $(shell which pyenv))
|
|
10
10
|
$(error "pyenv command not found! Aborting")
|
|
@@ -26,7 +26,7 @@ help:
|
|
|
26
26
|
bin/pip: .python-version ## Setups environment
|
|
27
27
|
virtualenv .
|
|
28
28
|
./bin/pip install --upgrade pip
|
|
29
|
-
./bin/pip install
|
|
29
|
+
./bin/pip install pdbp
|
|
30
30
|
./bin/pip install -e .
|
|
31
31
|
|
|
32
32
|
.PHONY: setup
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: imio.pyutils
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Some python useful methods
|
|
5
5
|
Home-page: https://github.com/imio/imio.pyutils/
|
|
6
6
|
Author: IMIO
|
|
@@ -15,10 +15,24 @@ Classifier: Operating System :: OS Independent
|
|
|
15
15
|
Classifier: Programming Language :: Python
|
|
16
16
|
Classifier: Programming Language :: Python :: 2.7
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
20
|
Requires-Dist: setuptools
|
|
19
21
|
Requires-Dist: future
|
|
22
|
+
Requires-Dist: psutil
|
|
20
23
|
Requires-Dist: requests
|
|
24
|
+
Requires-Dist: shortuuid>=0.5.0
|
|
21
25
|
Requires-Dist: six>=1.16.0
|
|
26
|
+
Dynamic: author
|
|
27
|
+
Dynamic: author-email
|
|
28
|
+
Dynamic: classifier
|
|
29
|
+
Dynamic: description
|
|
30
|
+
Dynamic: home-page
|
|
31
|
+
Dynamic: keywords
|
|
32
|
+
Dynamic: license
|
|
33
|
+
Dynamic: project-url
|
|
34
|
+
Dynamic: requires-dist
|
|
35
|
+
Dynamic: summary
|
|
22
36
|
|
|
23
37
|
.. image:: https://github.com/IMIO/imio.pyutils/actions/workflows/main.yml/badge.svg?branch=master
|
|
24
38
|
:target: https://github.com/IMIO/imio.pyutils/actions/workflows/main.yml
|
|
@@ -83,6 +97,28 @@ Can be run with: `bin/python -m unittest discover`
|
|
|
83
97
|
Changelog
|
|
84
98
|
=========
|
|
85
99
|
|
|
100
|
+
1.2.0 (2026-01-16)
|
|
101
|
+
------------------
|
|
102
|
+
|
|
103
|
+
- Add missing `psutil` dependency
|
|
104
|
+
[laulaz]
|
|
105
|
+
- Tested with python 3.12, 3.13.
|
|
106
|
+
[sgeulette]
|
|
107
|
+
- Improved runCommand
|
|
108
|
+
[sgeulette]
|
|
109
|
+
- Improved `batching.batch_delete_files`
|
|
110
|
+
[sgeulette]
|
|
111
|
+
- Improved `system.post_request`
|
|
112
|
+
[sgeulette]
|
|
113
|
+
- Added `utils.shortuid_encode_id` and `utils.shortuid_decode_id` to encode/decode uid with shortuid.
|
|
114
|
+
[sgeulette]
|
|
115
|
+
|
|
116
|
+
1.1.4 (2025-06-06)
|
|
117
|
+
------------------
|
|
118
|
+
|
|
119
|
+
- Fixed `IndexError` in `utils.get_ordinal_clusters` when `numbers=[]`.
|
|
120
|
+
[gbastien]
|
|
121
|
+
|
|
86
122
|
1.1.3 (2025-02-05)
|
|
87
123
|
------------------
|
|
88
124
|
|
|
@@ -19,7 +19,7 @@ logger = logging.getLogger("imio.pyutils")
|
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
# 7) when all the items are treated, we can delete the dictionary file
|
|
22
|
-
def batch_delete_files(batch_keys, config, rename=True):
|
|
22
|
+
def batch_delete_files(batch_keys, config, rename=True, log=False):
|
|
23
23
|
"""Deletes the file containing the batched keys.
|
|
24
24
|
|
|
25
25
|
:param batch_keys: the treated keys set
|
|
@@ -27,6 +27,7 @@ def batch_delete_files(batch_keys, config, rename=True):
|
|
|
27
27
|
'lc': loop_count, 'pf': infile, 'cf': config_file, 'kc': keys_count, 'lk': last_key,
|
|
28
28
|
'ldk': last_dump_key, 'fr'; first_run_bool}
|
|
29
29
|
:param rename: do not delete but rename
|
|
30
|
+
:param log: if True, log action
|
|
30
31
|
"""
|
|
31
32
|
if batch_keys is None:
|
|
32
33
|
return
|
|
@@ -36,7 +37,12 @@ def batch_delete_files(batch_keys, config, rename=True):
|
|
|
36
37
|
for filename in files:
|
|
37
38
|
if rename:
|
|
38
39
|
os.rename(filename, "{}.{}".format(filename, datetime.now().strftime("%Y%m%d-%H%M%S")))
|
|
40
|
+
msg = 'BATCHING ended: renamed file "%s" to "%s.%s"' % (filename, filename,
|
|
41
|
+
datetime.now().strftime("%Y%m%d-%H%M%S"))
|
|
39
42
|
else:
|
|
40
43
|
os.remove(filename)
|
|
44
|
+
msg = '"BATCHING ended: deleted file "%s""' % filename
|
|
45
|
+
if log:
|
|
46
|
+
logger.info(msg)
|
|
41
47
|
except Exception as error:
|
|
42
48
|
logger.exception("Error while renaming/deleting the file %s: %s", filename, error)
|
|
@@ -12,6 +12,7 @@ from six.moves import range
|
|
|
12
12
|
|
|
13
13
|
import datetime # do not import datetime datetime because load_var must eval datetime.datetime(2024, 12, 19, 11, 34)
|
|
14
14
|
import hashlib
|
|
15
|
+
import logging
|
|
15
16
|
import os
|
|
16
17
|
import re
|
|
17
18
|
import requests
|
|
@@ -194,7 +195,15 @@ def read_dir(dirpath, with_path=False, only_folders=False, only_files=False, to_
|
|
|
194
195
|
|
|
195
196
|
|
|
196
197
|
def read_recursive_dir(root_dir, rel_dir, with_folder=False, with_full_path=False, exclude_patterns=[]):
|
|
197
|
-
"""Read the dir and return files
|
|
198
|
+
"""Read the dir and return files.
|
|
199
|
+
|
|
200
|
+
:param root_dir: the root directory
|
|
201
|
+
:param rel_dir: the relative directory to root_dir
|
|
202
|
+
:param with_folder: if True, include folder names in the result
|
|
203
|
+
:param with_full_path: if True, return full paths, else relative to root_dir
|
|
204
|
+
:param exclude_patterns: list of regex patterns to exclude files or folders matching them
|
|
205
|
+
:return: list of files
|
|
206
|
+
"""
|
|
198
207
|
files = []
|
|
199
208
|
full_dir = os.path.join(root_dir, rel_dir)
|
|
200
209
|
for filename in os.listdir(full_dir):
|
|
@@ -286,12 +295,14 @@ def runCommand(cmd, outfile=None, append=True):
|
|
|
286
295
|
|
|
287
296
|
os.system(cmd + ' >_cmd_pv.out 2>_cmd_pv.err ;echo "RET_CODE=$?" >> _cmd_pv.out')
|
|
288
297
|
stdout = stderr = []
|
|
298
|
+
ret_code = 1
|
|
289
299
|
try:
|
|
290
300
|
if os.path.exists("_cmd_pv.out"):
|
|
291
301
|
ofile = open("_cmd_pv.out", "r")
|
|
292
302
|
stdout = ofile.readlines()
|
|
293
303
|
ofile.close()
|
|
294
304
|
os.remove("_cmd_pv.out")
|
|
305
|
+
ret_code = get_ret_code(stdout.pop())
|
|
295
306
|
else:
|
|
296
307
|
error("File %s does not exist" % "_cmd_pv.out")
|
|
297
308
|
except IOError:
|
|
@@ -306,7 +317,7 @@ def runCommand(cmd, outfile=None, append=True):
|
|
|
306
317
|
error("File %s does not exist" % "_cmd_pv.err")
|
|
307
318
|
except IOError:
|
|
308
319
|
error("Cannot open %s file" % "_cmd_pv.err")
|
|
309
|
-
return stdout, stderr,
|
|
320
|
+
return stdout, stderr, ret_code
|
|
310
321
|
|
|
311
322
|
|
|
312
323
|
def full_path(path, filename):
|
|
@@ -410,7 +421,7 @@ def create_temporary_file(initial_file, file_name):
|
|
|
410
421
|
|
|
411
422
|
def get_git_tag(path, last=False):
|
|
412
423
|
if last: # from all branches
|
|
413
|
-
cmd = "git --git-dir={0}/.git describe --tags `git --git-dir={0}/.git rev-list --tags
|
|
424
|
+
cmd = "git --git-dir={0}/.git describe --tags `git --git-dir={0}/.git rev-list --tags --max-count=1`".format(
|
|
414
425
|
path
|
|
415
426
|
)
|
|
416
427
|
else: # current branch only
|
|
@@ -434,26 +445,87 @@ def get_temporary_filename(file_name):
|
|
|
434
445
|
return temp_filename
|
|
435
446
|
|
|
436
447
|
|
|
437
|
-
def post_request(url, data=None, json=None, headers=None
|
|
448
|
+
def post_request(url, data=None, json=None, headers=None, files=None, return_json=False, logger=None,
|
|
449
|
+
clean_files_for_logging=True):
|
|
438
450
|
"""Post data to url.
|
|
439
451
|
|
|
440
452
|
:param url: the url to post to
|
|
441
|
-
:param data: a
|
|
453
|
+
:param data: a data struct to consider
|
|
442
454
|
:param json: a json serializable object
|
|
443
|
-
:param headers: headers to use
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
455
|
+
:param headers: headers to use
|
|
456
|
+
:param files: files to upload (dict or list of tuples)
|
|
457
|
+
:param return_json: response as json
|
|
458
|
+
:param logger: current logger, otherwise a default one is created
|
|
459
|
+
:param clean_files_for_logging: cleans files content for logging if True
|
|
460
|
+
:return: the response object
|
|
461
|
+
"""
|
|
462
|
+
if json is not None and (data is not None or files is not None):
|
|
463
|
+
raise ValueError("Cannot use both json and data or files parameters")
|
|
464
|
+
if logger is None:
|
|
465
|
+
logger = logging.getLogger("imio.pyutils")
|
|
466
|
+
kwargs = {}
|
|
467
|
+
|
|
468
|
+
if files:
|
|
469
|
+
kwargs["files"] = files
|
|
470
|
+
if headers:
|
|
471
|
+
# Exclude Content-Type with multipart/form-data
|
|
472
|
+
kwargs["headers"] = {k: v for k, v in headers.items() if k.lower() != "content-type"}
|
|
473
|
+
if "headers" not in kwargs:
|
|
474
|
+
kwargs["headers"] = headers or (
|
|
475
|
+
{"Content-Type": "application/json"} if json else {"Content-Type": "application/x-www-form-urlencoded"}
|
|
476
|
+
)
|
|
449
477
|
if json:
|
|
450
478
|
kwargs["json"] = json
|
|
451
479
|
else:
|
|
452
480
|
kwargs["data"] = data
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
481
|
+
|
|
482
|
+
try:
|
|
483
|
+
with requests.post(url, **kwargs) as response:
|
|
484
|
+
if not response.ok:
|
|
485
|
+
if files and clean_files_for_logging:
|
|
486
|
+
cleaned_files = []
|
|
487
|
+
# clean only if item is of this format ("files", (filename, file_content))
|
|
488
|
+
for item in kwargs["files"]:
|
|
489
|
+
if isinstance(item, tuple) and len(item) == 2 and isinstance(item[1], tuple):
|
|
490
|
+
if len(item[1]) == 2:
|
|
491
|
+
cleaned_files.append((item[0], (item[1][0], len(item[1][1]))))
|
|
492
|
+
elif len(item[1]) == 3:
|
|
493
|
+
cleaned_files.append((item[0], (item[1][0], len(item[1][1]), item[1][2])))
|
|
494
|
+
else:
|
|
495
|
+
cleaned_files.append(item)
|
|
496
|
+
else:
|
|
497
|
+
cleaned_files.append(item)
|
|
498
|
+
kwargs["files"] = cleaned_files
|
|
499
|
+
if response.status_code in (401, 403):
|
|
500
|
+
logger.error("Authentication error while posting data to '%s': %s (status %d)"
|
|
501
|
+
% (url, response.text, response.status_code))
|
|
502
|
+
else:
|
|
503
|
+
logger.error("Error while posting data '%s' to '%s': %s" % (kwargs, url, response.text))
|
|
504
|
+
elif return_json:
|
|
505
|
+
try:
|
|
506
|
+
return response.json()
|
|
507
|
+
except ValueError as e:
|
|
508
|
+
logger.error("Error parsing JSON response from '%s': %s" % (url, str(e)))
|
|
509
|
+
return response
|
|
510
|
+
else:
|
|
511
|
+
return response
|
|
512
|
+
return response
|
|
513
|
+
except requests.ConnectionError as e:
|
|
514
|
+
msg = "Connection error while posting data to '{}': {}".format(url, str(e))
|
|
515
|
+
logger.error(msg)
|
|
516
|
+
mock_response = requests.Response()
|
|
517
|
+
mock_response.status_code = 503 # service unavailable
|
|
518
|
+
mock_response._content = "{'error': '%s'}" % msg
|
|
519
|
+
mock_response.url = url
|
|
520
|
+
return mock_response
|
|
521
|
+
except Exception as e:
|
|
522
|
+
msg = "Unexpected error while posting data to '{}': {}".format(url, str(e))
|
|
523
|
+
logger.error(msg)
|
|
524
|
+
mock_response = requests.Response()
|
|
525
|
+
mock_response.status_code = 500 # Internal server error
|
|
526
|
+
mock_response._content = "{'error': '%s'}" % msg
|
|
527
|
+
mock_response.url = url
|
|
528
|
+
return mock_response
|
|
457
529
|
|
|
458
530
|
|
|
459
531
|
def process_memory():
|
|
@@ -22,11 +22,14 @@ from imio.pyutils.utils import one_of_dict_values
|
|
|
22
22
|
from imio.pyutils.utils import radix_like_starting_1
|
|
23
23
|
from imio.pyutils.utils import replace_in_list
|
|
24
24
|
from imio.pyutils.utils import safe_encode
|
|
25
|
+
from imio.pyutils.utils import shortuid_decode_id
|
|
26
|
+
from imio.pyutils.utils import shortuid_encode_id
|
|
25
27
|
from imio.pyutils.utils import sort_by_indexes
|
|
26
28
|
|
|
27
29
|
import os
|
|
28
30
|
import types
|
|
29
31
|
import unittest
|
|
32
|
+
import uuid
|
|
30
33
|
|
|
31
34
|
|
|
32
35
|
class TestUtils(unittest.TestCase):
|
|
@@ -89,6 +92,10 @@ class TestUtils(unittest.TestCase):
|
|
|
89
92
|
self.assertListEqual(
|
|
90
93
|
get_ordinal_clusters([100, 200, 250, 400, 700], as_str=False), [[100, 200], [250], [400], [700]]
|
|
91
94
|
)
|
|
95
|
+
self.assertEqual(get_ordinal_clusters([]), '')
|
|
96
|
+
self.assertEqual(get_ordinal_clusters([], as_str=False), [])
|
|
97
|
+
self.assertEqual(get_ordinal_clusters(), '')
|
|
98
|
+
self.assertEqual(get_ordinal_clusters(as_str=False), [])
|
|
92
99
|
|
|
93
100
|
def test_insert_in_ordered_dict(self):
|
|
94
101
|
dic = OrderedDict([("a", 1), ("b", 2)])
|
|
@@ -183,6 +190,81 @@ class TestUtils(unittest.TestCase):
|
|
|
183
190
|
self.assertEqual(safe_encode(b"xx"), "xx")
|
|
184
191
|
self.assertEqual(safe_encode(5), 5)
|
|
185
192
|
|
|
193
|
+
def test_shortuid_decode_id(self):
|
|
194
|
+
# Test with empty string
|
|
195
|
+
self.assertEqual(shortuid_decode_id(""), "")
|
|
196
|
+
|
|
197
|
+
# Test with valid encoded ID
|
|
198
|
+
test_uuid = "f40682caafc045b4b81973bd82ea9ab6"
|
|
199
|
+
encoded = shortuid_encode_id(test_uuid)
|
|
200
|
+
decoded = shortuid_decode_id(encoded)
|
|
201
|
+
self.assertEqual(decoded, test_uuid)
|
|
202
|
+
|
|
203
|
+
# Test decode without separator in encoded string
|
|
204
|
+
encoded_no_sep = shortuid_encode_id(test_uuid, separator="")
|
|
205
|
+
decoded = shortuid_decode_id(encoded_no_sep, separator="")
|
|
206
|
+
self.assertEqual(decoded, test_uuid)
|
|
207
|
+
|
|
208
|
+
# Test decode with custom separator
|
|
209
|
+
encoded_custom_sep = shortuid_encode_id(test_uuid, separator="|")
|
|
210
|
+
decoded = shortuid_decode_id(encoded_custom_sep, separator="|")
|
|
211
|
+
self.assertEqual(decoded, test_uuid)
|
|
212
|
+
|
|
213
|
+
# Test with whitespace (should be stripped)
|
|
214
|
+
encoded_with_space = " " + shortuid_encode_id(test_uuid) + " "
|
|
215
|
+
decoded = shortuid_decode_id(encoded_with_space)
|
|
216
|
+
self.assertEqual(decoded, test_uuid)
|
|
217
|
+
|
|
218
|
+
# Test with invalid input
|
|
219
|
+
invalid_decoded = shortuid_decode_id("invalid-short-id")
|
|
220
|
+
self.assertIsNone(invalid_decoded)
|
|
221
|
+
|
|
222
|
+
# Test roundtrip with multiple UUIDs
|
|
223
|
+
for _ in range(5):
|
|
224
|
+
test_uid = uuid.uuid4().hex
|
|
225
|
+
encoded = shortuid_encode_id(test_uid)
|
|
226
|
+
decoded = shortuid_decode_id(encoded)
|
|
227
|
+
self.assertEqual(decoded, test_uid)
|
|
228
|
+
|
|
229
|
+
def test_shortuid_encode_id(self):
|
|
230
|
+
# Test with empty string
|
|
231
|
+
self.assertEqual(shortuid_encode_id(""), "")
|
|
232
|
+
|
|
233
|
+
# Test with valid UUID
|
|
234
|
+
test_uuid = "f40682caafc045b4b81973bd82ea9ab6"
|
|
235
|
+
encoded = shortuid_encode_id(test_uuid)
|
|
236
|
+
# Check that encoded is not empty
|
|
237
|
+
self.assertTrue(encoded)
|
|
238
|
+
# Check that it contains separators
|
|
239
|
+
self.assertIn("-", encoded)
|
|
240
|
+
# Check the format (default block_size=5)
|
|
241
|
+
parts = encoded.split("-")
|
|
242
|
+
self.assertTrue(all(len(part) <= 5 for part in parts))
|
|
243
|
+
|
|
244
|
+
# Test without separator
|
|
245
|
+
encoded_no_sep = shortuid_encode_id(test_uuid, separator="")
|
|
246
|
+
self.assertNotIn("-", encoded_no_sep)
|
|
247
|
+
|
|
248
|
+
# Test with custom separator
|
|
249
|
+
encoded_custom_sep = shortuid_encode_id(test_uuid, separator="|")
|
|
250
|
+
self.assertIn("|", encoded_custom_sep)
|
|
251
|
+
self.assertNotIn("-", encoded_custom_sep)
|
|
252
|
+
|
|
253
|
+
# Test with custom block_size
|
|
254
|
+
encoded_block_3 = shortuid_encode_id(test_uuid, separator="-", block_size=3)
|
|
255
|
+
parts = encoded_block_3.split("-")
|
|
256
|
+
self.assertTrue(all(len(part) <= 3 for part in parts))
|
|
257
|
+
|
|
258
|
+
# Test with block_size=0 (no chunking)
|
|
259
|
+
encoded_no_chunk = shortuid_encode_id(test_uuid, separator="-", block_size=0)
|
|
260
|
+
self.assertNotIn("-", encoded_no_chunk)
|
|
261
|
+
|
|
262
|
+
# Test roundtrip consistency
|
|
263
|
+
generated_uuid = uuid.uuid4().hex
|
|
264
|
+
encoded = shortuid_encode_id(generated_uuid)
|
|
265
|
+
decoded = shortuid_decode_id(encoded)
|
|
266
|
+
self.assertEqual(decoded, generated_uuid)
|
|
267
|
+
|
|
186
268
|
def test_sort_by_indexes(self):
|
|
187
269
|
lst = ["a", "b", "c", "d", "e", "f", "g"]
|
|
188
270
|
indexes = [1, 3, 5, 2, 4, 6, 6]
|
|
@@ -18,8 +18,14 @@ from six.moves import zip
|
|
|
18
18
|
import copy
|
|
19
19
|
import itertools
|
|
20
20
|
import logging
|
|
21
|
+
import shortuuid
|
|
21
22
|
import time
|
|
22
23
|
import timeit
|
|
24
|
+
import uuid
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Initialize shortuuid (default alphabet)
|
|
28
|
+
s_uuid = shortuuid.ShortUUID()
|
|
23
29
|
|
|
24
30
|
|
|
25
31
|
def add_key_if_value(dic, key, value, strict=False):
|
|
@@ -60,6 +66,13 @@ def append(lst, value):
|
|
|
60
66
|
return value
|
|
61
67
|
|
|
62
68
|
|
|
69
|
+
def display_offset_number(number, offset):
|
|
70
|
+
"""Display a number with an offset. For example when p_number=123 and offset=100, it returns '1.23'"""
|
|
71
|
+
if number % offset == 0:
|
|
72
|
+
return str(int(number / offset))
|
|
73
|
+
return "{}.{}".format(number // offset, number % offset)
|
|
74
|
+
|
|
75
|
+
|
|
63
76
|
def ftimed(f, nb=100, fmt="{:.7f}"):
|
|
64
77
|
duration, ret = timed(f, nb=nb)
|
|
65
78
|
return fmt.format(duration), ret
|
|
@@ -80,13 +93,6 @@ def get_clusters(numbers=[], separator=", "):
|
|
|
80
93
|
return separator.join(res)
|
|
81
94
|
|
|
82
95
|
|
|
83
|
-
def display_offset_number(number, offset):
|
|
84
|
-
"""Display a number with an offset. For example when p_number=123 and offset=100, it returns '1.23'"""
|
|
85
|
-
if number % offset == 0:
|
|
86
|
-
return str(int(number / offset))
|
|
87
|
-
return "{}.{}".format(number // offset, number % offset)
|
|
88
|
-
|
|
89
|
-
|
|
90
96
|
def get_ordinal_clusters(
|
|
91
97
|
numbers=[], cluster_format="{0}-{1}", single_cluster_format="{0}", separator=", ", offset=100, as_str=True
|
|
92
98
|
):
|
|
@@ -106,14 +112,15 @@ def get_ordinal_clusters(
|
|
|
106
112
|
|
|
107
113
|
# Initialize the first group
|
|
108
114
|
clusters = []
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
if numbers:
|
|
116
|
+
current_cluster = [numbers[0]]
|
|
117
|
+
for num in numbers[1:]:
|
|
118
|
+
if _is_in_cluster(num, current_cluster, offset):
|
|
119
|
+
current_cluster.append(num)
|
|
120
|
+
else: # we'll start a new cluster
|
|
121
|
+
clusters.append(current_cluster)
|
|
122
|
+
current_cluster = [num]
|
|
123
|
+
clusters.append(current_cluster) # Add the last one
|
|
117
124
|
|
|
118
125
|
if not as_str:
|
|
119
126
|
return clusters
|
|
@@ -193,6 +200,19 @@ def letters_sequence(nth, letters="abcdefghijklmnopqrstuvwxyz"):
|
|
|
193
200
|
return res
|
|
194
201
|
|
|
195
202
|
|
|
203
|
+
def listify(value, force=False):
|
|
204
|
+
"""Ensure given value is a list-like iterable.
|
|
205
|
+
|
|
206
|
+
:param value: the value to turn into a list if not already the case
|
|
207
|
+
:param force: if value is a tuple, returned as a list
|
|
208
|
+
"""
|
|
209
|
+
if isinstance(value, string_types):
|
|
210
|
+
value = [value]
|
|
211
|
+
if force and not isinstance(value, list):
|
|
212
|
+
value = list(value)
|
|
213
|
+
return value
|
|
214
|
+
|
|
215
|
+
|
|
196
216
|
def merge_dicts(dicts, as_dict=True):
|
|
197
217
|
"""Merge dicts, extending values of each dicts,
|
|
198
218
|
useful for example when the value is a list.
|
|
@@ -308,6 +328,48 @@ def setup_logger(logger, replace=logging.StreamHandler, level=20):
|
|
|
308
328
|
logger.setLevel(level)
|
|
309
329
|
|
|
310
330
|
|
|
331
|
+
def shortuid_decode_id(short_id, separator="-"):
|
|
332
|
+
"""Get original UID from segmented ShortUUID.
|
|
333
|
+
|
|
334
|
+
:param short_id: string, short ID (with or without separators)
|
|
335
|
+
:param separator: string, separator character
|
|
336
|
+
:return: string, original UID
|
|
337
|
+
"""
|
|
338
|
+
if not short_id:
|
|
339
|
+
return ""
|
|
340
|
+
|
|
341
|
+
clean_id = short_id.strip()
|
|
342
|
+
if separator:
|
|
343
|
+
clean_id = clean_id.replace(separator, "")
|
|
344
|
+
|
|
345
|
+
try:
|
|
346
|
+
u = s_uuid.decode(clean_id)
|
|
347
|
+
return u.hex
|
|
348
|
+
except Exception:
|
|
349
|
+
return None
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def shortuid_encode_id(uid, separator="-", block_size=5):
|
|
353
|
+
"""Transform UID in segmented ShortUUID.
|
|
354
|
+
|
|
355
|
+
:param uid: string, original UID
|
|
356
|
+
:param separator: string, separator character
|
|
357
|
+
:param block_size: int, block size length
|
|
358
|
+
:return: string, shortened UID
|
|
359
|
+
"""
|
|
360
|
+
if not uid:
|
|
361
|
+
return ""
|
|
362
|
+
|
|
363
|
+
u = uuid.UUID(str(uid).strip())
|
|
364
|
+
short_id = s_uuid.encode(u)
|
|
365
|
+
|
|
366
|
+
if separator and block_size > 0:
|
|
367
|
+
chunks = [short_id[i:i + block_size] for i in range(0, len(short_id), block_size)]
|
|
368
|
+
return separator.join(chunks)
|
|
369
|
+
|
|
370
|
+
return short_id
|
|
371
|
+
|
|
372
|
+
|
|
311
373
|
def sort_by_indexes(lst, indexes, reverse=False):
|
|
312
374
|
"""Sort a list following a second list containing the order"""
|
|
313
375
|
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: x[0], reverse=reverse)]
|
|
@@ -346,16 +408,3 @@ def time_elapsed(start, cond=True, msg=u"", dec=3, min=0.0):
|
|
|
346
408
|
def time_start():
|
|
347
409
|
"""To be used with time_elapsed."""
|
|
348
410
|
return timeit.default_timer()
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
def listify(value, force=False):
|
|
352
|
-
"""Ensure given value is a list-like iterable.
|
|
353
|
-
|
|
354
|
-
:param value: the value to turn into a list if not already the case
|
|
355
|
-
:param force: if value is a tuple, returned as a list
|
|
356
|
-
"""
|
|
357
|
-
if isinstance(value, string_types):
|
|
358
|
-
value = [value]
|
|
359
|
-
if force and not isinstance(value, list):
|
|
360
|
-
value = list(value)
|
|
361
|
-
return value
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: imio.pyutils
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Some python useful methods
|
|
5
5
|
Home-page: https://github.com/imio/imio.pyutils/
|
|
6
6
|
Author: IMIO
|
|
@@ -15,10 +15,24 @@ Classifier: Operating System :: OS Independent
|
|
|
15
15
|
Classifier: Programming Language :: Python
|
|
16
16
|
Classifier: Programming Language :: Python :: 2.7
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
20
|
Requires-Dist: setuptools
|
|
19
21
|
Requires-Dist: future
|
|
22
|
+
Requires-Dist: psutil
|
|
20
23
|
Requires-Dist: requests
|
|
24
|
+
Requires-Dist: shortuuid>=0.5.0
|
|
21
25
|
Requires-Dist: six>=1.16.0
|
|
26
|
+
Dynamic: author
|
|
27
|
+
Dynamic: author-email
|
|
28
|
+
Dynamic: classifier
|
|
29
|
+
Dynamic: description
|
|
30
|
+
Dynamic: home-page
|
|
31
|
+
Dynamic: keywords
|
|
32
|
+
Dynamic: license
|
|
33
|
+
Dynamic: project-url
|
|
34
|
+
Dynamic: requires-dist
|
|
35
|
+
Dynamic: summary
|
|
22
36
|
|
|
23
37
|
.. image:: https://github.com/IMIO/imio.pyutils/actions/workflows/main.yml/badge.svg?branch=master
|
|
24
38
|
:target: https://github.com/IMIO/imio.pyutils/actions/workflows/main.yml
|
|
@@ -83,6 +97,28 @@ Can be run with: `bin/python -m unittest discover`
|
|
|
83
97
|
Changelog
|
|
84
98
|
=========
|
|
85
99
|
|
|
100
|
+
1.2.0 (2026-01-16)
|
|
101
|
+
------------------
|
|
102
|
+
|
|
103
|
+
- Add missing `psutil` dependency
|
|
104
|
+
[laulaz]
|
|
105
|
+
- Tested with python 3.12, 3.13.
|
|
106
|
+
[sgeulette]
|
|
107
|
+
- Improved runCommand
|
|
108
|
+
[sgeulette]
|
|
109
|
+
- Improved `batching.batch_delete_files`
|
|
110
|
+
[sgeulette]
|
|
111
|
+
- Improved `system.post_request`
|
|
112
|
+
[sgeulette]
|
|
113
|
+
- Added `utils.shortuid_encode_id` and `utils.shortuid_decode_id` to encode/decode uid with shortuid.
|
|
114
|
+
[sgeulette]
|
|
115
|
+
|
|
116
|
+
1.1.4 (2025-06-06)
|
|
117
|
+
------------------
|
|
118
|
+
|
|
119
|
+
- Fixed `IndexError` in `utils.get_ordinal_clusters` when `numbers=[]`.
|
|
120
|
+
[gbastien]
|
|
121
|
+
|
|
86
122
|
1.1.3 (2025-02-05)
|
|
87
123
|
------------------
|
|
88
124
|
|
|
@@ -2,7 +2,7 @@ from setuptools import find_packages
|
|
|
2
2
|
from setuptools import setup
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
version = "1.
|
|
5
|
+
version = "1.2.0"
|
|
6
6
|
|
|
7
7
|
setup(
|
|
8
8
|
name="imio.pyutils",
|
|
@@ -18,6 +18,8 @@ setup(
|
|
|
18
18
|
"Programming Language :: Python",
|
|
19
19
|
"Programming Language :: Python :: 2.7",
|
|
20
20
|
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
21
23
|
],
|
|
22
24
|
keywords="Python IMIO",
|
|
23
25
|
author="IMIO",
|
|
@@ -38,7 +40,9 @@ setup(
|
|
|
38
40
|
install_requires=[
|
|
39
41
|
"setuptools",
|
|
40
42
|
"future",
|
|
43
|
+
"psutil",
|
|
41
44
|
"requests",
|
|
45
|
+
"shortuuid>=0.5.0",
|
|
42
46
|
"six>=1.16.0",
|
|
43
47
|
# -*- Extra requirements: -*-
|
|
44
48
|
],
|
|
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
|