pdmv-http-client 2.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ """
2
+ Other operations that spread to several
3
+ topics and intentions.
4
+ """
5
+
6
+ import pprint
7
+ import random
8
+ from copy import deepcopy
9
+
10
+
11
+ def pformat(obj) -> str:
12
+ """
13
+ Pretty print an object in console
14
+ """
15
+ return pprint.pformat(obj, width=50, compact=True)
16
+
17
+
18
+ def shuffle_pick(collection: list[dict], size: int, attribute: str = "") -> list:
19
+ """
20
+ Given a list of objects, shuffles the collection and picks
21
+ the requested attribute only for a subset of `size` elements.
22
+
23
+ Args:
24
+ collection: List of objects.
25
+ size: Subset size.
26
+ attribute: Attribute to pick from the objects, it is assumed
27
+ all objects in the collection include it. In case it
28
+ is not provided, the content of the whole object will
29
+ be returned.
30
+
31
+ Returns:
32
+ A subset of the original collection, including only the requested
33
+ attribute.
34
+
35
+ Raises:
36
+ KeyError: In case the request key is not available in the collection's
37
+ element.
38
+ """
39
+ elements = deepcopy(collection)
40
+ subset_len = min(size, len(elements))
41
+ random.shuffle(elements)
42
+ subset = elements[:subset_len]
43
+
44
+ if not attribute:
45
+ return subset
46
+
47
+ return [el[attribute] for el in subset]
rest/utils/shell.py ADDED
@@ -0,0 +1,57 @@
1
+ """
2
+ Provides some pure-functions to interact with
3
+ the OS via shell executions.
4
+ """
5
+
6
+ import platform
7
+ import subprocess
8
+ import sys
9
+ from typing import Union
10
+
11
+
12
+ def run_command(
13
+ command: Union[str, list[str]], env: dict[str, str] = {}
14
+ ) -> tuple[str, str, int]:
15
+ """
16
+ Executes a command or a list of commands
17
+ via shell.
18
+
19
+ Args:
20
+ command: Command to execute, in case a list
21
+ is provided, the result command to execute will be
22
+ concatenated.
23
+ env: Environment variables for the process
24
+ to spawn.
25
+
26
+ Returns:
27
+ tuple: Standard output, standard error and exit code.
28
+ """
29
+ full_command: str = ""
30
+
31
+ if isinstance(command, list):
32
+ full_command = ";".join(command)
33
+ else:
34
+ full_command = command
35
+
36
+ result = subprocess.run(
37
+ full_command,
38
+ shell=True,
39
+ capture_output=True,
40
+ text=True,
41
+ env=env,
42
+ )
43
+ stdout: str = result.stdout
44
+ stderr: str = result.stderr
45
+ exit_code: int = result.returncode
46
+ return stdout, stderr, exit_code
47
+
48
+
49
+ def describe_platform() -> str:
50
+ """
51
+ Retrieves an identifier for describing the current
52
+ execution environment. This is useful for including
53
+ User-Agent headers.
54
+ """
55
+ version = sys.version_info
56
+ python_version = f"{version.major}.{version.minor}.{version.micro}"
57
+ return f"(Python: {python_version}) ({platform.system()}: {platform.machine()})"