smith-utils 0.4.1__tar.gz → 0.5.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.
- {smith_utils-0.4.1 → smith_utils-0.5.0}/PKG-INFO +6 -2
- {smith_utils-0.4.1 → smith_utils-0.5.0}/README.md +5 -1
- {smith_utils-0.4.1 → smith_utils-0.5.0}/pyproject.toml +1 -1
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/__init__.py +2 -1
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/cli.py +24 -0
- smith_utils-0.5.0/src/smith_utils/numeric/__init__.py +8 -0
- smith_utils-0.5.0/src/smith_utils/numeric/sampling.py +32 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_cli.py +11 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_package_exports.py +1 -0
- smith_utils-0.5.0/tests/test_sampling.py +34 -0
- smith_utils-0.4.1/src/smith_utils/numeric/__init__.py +0 -6
- {smith_utils-0.4.1 → smith_utils-0.5.0}/.gitignore +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/LICENSE +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/crypto/__init__.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/crypto/hashes.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/datetime/__init__.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/datetime/date_utils.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/file/__init__.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/file/classification.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/numeric/refinement.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/text/__init__.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/text/metrics.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/text/newlines.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/text/normalization.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/text/random_string.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/src/smith_utils/text/unicode_char_names.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_date_utils.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_file_classification.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_hashes.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_metrics.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_newlines.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_normalization.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_random_string.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_refinement.py +0 -0
- {smith_utils-0.4.1 → smith_utils-0.5.0}/tests/test_unicode_char_names.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: smith-utils
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: A utility library for data cleaning and parsing.
|
|
5
5
|
Project-URL: Homepage, https://github.com/yeiichi/smith-utils
|
|
6
6
|
Project-URL: Repository, https://github.com/yeiichi/smith-utils
|
|
@@ -70,6 +70,7 @@ Robust date parsing and formatting.
|
|
|
70
70
|
Clean and parse messy numeric data.
|
|
71
71
|
- `parse_numeric_value`: Handles custom separators, decimals, and negative formats like `(1,234.56)`.
|
|
72
72
|
- `parse_currency_value`: Alias for numeric parsing, specifically for currency strings.
|
|
73
|
+
- `sample_positive_gaussian`: Generate Gaussian samples truncated to positive values up to an approximate upper bound.
|
|
73
74
|
|
|
74
75
|
### Text Normalization & Metrics (`smith_utils.text`)
|
|
75
76
|
Standardize text and compare string similarity.
|
|
@@ -107,13 +108,15 @@ This also installs the `smith` command.
|
|
|
107
108
|
```python
|
|
108
109
|
from smith_utils import ensure_date, get_text_digest, parse_numeric_value, normalize_text, random_string
|
|
109
110
|
from smith_utils import make_unicode_char_name_records
|
|
110
|
-
from smith_utils import classify_file, get_file_digest, normalize_file_to_lf
|
|
111
|
+
from smith_utils import classify_file, get_file_digest, normalize_file_to_lf, sample_positive_gaussian
|
|
111
112
|
|
|
112
113
|
# Datetime
|
|
113
114
|
date = ensure_date("20231225") # datetime.date(2023, 12, 25)
|
|
114
115
|
|
|
115
116
|
# Numeric
|
|
116
117
|
value = parse_numeric_value("(1,250.50)") # -1250.5
|
|
118
|
+
samples = sample_positive_gaussian(mean=5.0, approx_upper_bound=10.0, trial_number=3)
|
|
119
|
+
# approx_upper_bound is treated as mean + 3 * sigma and as a truncation bound.
|
|
117
120
|
|
|
118
121
|
# Text
|
|
119
122
|
clean_text = normalize_text(" Smith Utils ") # "smith utils"
|
|
@@ -147,6 +150,7 @@ smith digest input.txt
|
|
|
147
150
|
smith normalize-text " Smith Utils "
|
|
148
151
|
smith unicode-names "Aあ"
|
|
149
152
|
smith classify-file input.pdf
|
|
153
|
+
smith positive-gaussian-samples 3 --mean 5 --approx-upper-bound 10
|
|
150
154
|
```
|
|
151
155
|
|
|
152
156
|
## Directory Structure
|
|
@@ -22,6 +22,7 @@ Robust date parsing and formatting.
|
|
|
22
22
|
Clean and parse messy numeric data.
|
|
23
23
|
- `parse_numeric_value`: Handles custom separators, decimals, and negative formats like `(1,234.56)`.
|
|
24
24
|
- `parse_currency_value`: Alias for numeric parsing, specifically for currency strings.
|
|
25
|
+
- `sample_positive_gaussian`: Generate Gaussian samples truncated to positive values up to an approximate upper bound.
|
|
25
26
|
|
|
26
27
|
### Text Normalization & Metrics (`smith_utils.text`)
|
|
27
28
|
Standardize text and compare string similarity.
|
|
@@ -59,13 +60,15 @@ This also installs the `smith` command.
|
|
|
59
60
|
```python
|
|
60
61
|
from smith_utils import ensure_date, get_text_digest, parse_numeric_value, normalize_text, random_string
|
|
61
62
|
from smith_utils import make_unicode_char_name_records
|
|
62
|
-
from smith_utils import classify_file, get_file_digest, normalize_file_to_lf
|
|
63
|
+
from smith_utils import classify_file, get_file_digest, normalize_file_to_lf, sample_positive_gaussian
|
|
63
64
|
|
|
64
65
|
# Datetime
|
|
65
66
|
date = ensure_date("20231225") # datetime.date(2023, 12, 25)
|
|
66
67
|
|
|
67
68
|
# Numeric
|
|
68
69
|
value = parse_numeric_value("(1,250.50)") # -1250.5
|
|
70
|
+
samples = sample_positive_gaussian(mean=5.0, approx_upper_bound=10.0, trial_number=3)
|
|
71
|
+
# approx_upper_bound is treated as mean + 3 * sigma and as a truncation bound.
|
|
69
72
|
|
|
70
73
|
# Text
|
|
71
74
|
clean_text = normalize_text(" Smith Utils ") # "smith utils"
|
|
@@ -99,6 +102,7 @@ smith digest input.txt
|
|
|
99
102
|
smith normalize-text " Smith Utils "
|
|
100
103
|
smith unicode-names "Aあ"
|
|
101
104
|
smith classify-file input.pdf
|
|
105
|
+
smith positive-gaussian-samples 3 --mean 5 --approx-upper-bound 10
|
|
102
106
|
```
|
|
103
107
|
|
|
104
108
|
## Directory Structure
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from .crypto import get_file_digest, get_text_digest
|
|
2
2
|
from .datetime import ensure_date, format_ordinal, parse_strict_date
|
|
3
3
|
from .file import FileClassification, classify_file
|
|
4
|
-
from .numeric import parse_currency_value, parse_numeric_value
|
|
4
|
+
from .numeric import parse_currency_value, parse_numeric_value, sample_positive_gaussian
|
|
5
5
|
from .text import (
|
|
6
6
|
CharCategory,
|
|
7
7
|
NewlineType,
|
|
@@ -41,4 +41,5 @@ __all__ = [
|
|
|
41
41
|
"parse_strict_date",
|
|
42
42
|
"random_mixed_char",
|
|
43
43
|
"random_string",
|
|
44
|
+
"sample_positive_gaussian",
|
|
44
45
|
]
|
|
@@ -12,6 +12,7 @@ from . import (
|
|
|
12
12
|
make_unicode_char_name_records,
|
|
13
13
|
normalize_text,
|
|
14
14
|
random_string,
|
|
15
|
+
sample_positive_gaussian,
|
|
15
16
|
)
|
|
16
17
|
|
|
17
18
|
|
|
@@ -67,6 +68,20 @@ def _make_parser() -> argparse.ArgumentParser:
|
|
|
67
68
|
classify_parser.add_argument("path", help="file path to classify")
|
|
68
69
|
classify_parser.set_defaults(func=_classify_file_command)
|
|
69
70
|
|
|
71
|
+
gaussian_parser = subparsers.add_parser(
|
|
72
|
+
"positive-gaussian-samples",
|
|
73
|
+
help="sample positive Gaussian values",
|
|
74
|
+
)
|
|
75
|
+
gaussian_parser.add_argument("trial_number", type=int, help="number of samples to generate")
|
|
76
|
+
gaussian_parser.add_argument("--mean", type=float, required=True, help="Gaussian mean")
|
|
77
|
+
gaussian_parser.add_argument(
|
|
78
|
+
"--approx-upper-bound",
|
|
79
|
+
type=float,
|
|
80
|
+
required=True,
|
|
81
|
+
help="approximate upper bound used as mean + 3 sigma and as a truncation bound",
|
|
82
|
+
)
|
|
83
|
+
gaussian_parser.set_defaults(func=_positive_gaussian_samples_command)
|
|
84
|
+
|
|
70
85
|
return parser
|
|
71
86
|
|
|
72
87
|
|
|
@@ -122,5 +137,14 @@ def _classify_file_command(args: argparse.Namespace) -> None:
|
|
|
122
137
|
print(f"{key}={'' if value is None else value}")
|
|
123
138
|
|
|
124
139
|
|
|
140
|
+
def _positive_gaussian_samples_command(args: argparse.Namespace) -> None:
|
|
141
|
+
for sample in sample_positive_gaussian(
|
|
142
|
+
mean=args.mean,
|
|
143
|
+
approx_upper_bound=args.approx_upper_bound,
|
|
144
|
+
trial_number=args.trial_number,
|
|
145
|
+
):
|
|
146
|
+
print(sample)
|
|
147
|
+
|
|
148
|
+
|
|
125
149
|
if __name__ == "__main__":
|
|
126
150
|
raise SystemExit(main())
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import random
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def sample_positive_gaussian(
|
|
7
|
+
mean: float,
|
|
8
|
+
approx_upper_bound: float,
|
|
9
|
+
trial_number: int,
|
|
10
|
+
) -> list[float]:
|
|
11
|
+
"""Sample positive Gaussian values bounded by an approximate upper bound.
|
|
12
|
+
|
|
13
|
+
``approx_upper_bound`` is used as ``mean + 3 * sigma`` to derive the
|
|
14
|
+
Gaussian standard deviation. It is also used as a truncation bound, so
|
|
15
|
+
returned samples are greater than zero and less than or equal to the bound.
|
|
16
|
+
"""
|
|
17
|
+
if trial_number < 0:
|
|
18
|
+
raise ValueError("trial_number must be >= 0")
|
|
19
|
+
if mean <= 0:
|
|
20
|
+
raise ValueError("mean must be positive")
|
|
21
|
+
if approx_upper_bound <= mean:
|
|
22
|
+
raise ValueError("approx_upper_bound must be greater than mean")
|
|
23
|
+
|
|
24
|
+
sigma = (approx_upper_bound - mean) / 3
|
|
25
|
+
samples: list[float] = []
|
|
26
|
+
|
|
27
|
+
while len(samples) < trial_number:
|
|
28
|
+
value = random.gauss(mean, sigma)
|
|
29
|
+
if 0 < value <= approx_upper_bound:
|
|
30
|
+
samples.append(value)
|
|
31
|
+
|
|
32
|
+
return samples
|
|
@@ -77,3 +77,14 @@ def test_classify_file_command_prints_key_value_fields(tmp_path, capsys):
|
|
|
77
77
|
assert "extension=.pdf" in output
|
|
78
78
|
assert "file_class=document" in output
|
|
79
79
|
assert "magic_type=PDF document" in output
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_positive_gaussian_samples_command_prints_one_sample_per_line(monkeypatch, capsys):
|
|
83
|
+
monkeypatch.setattr(
|
|
84
|
+
"src.smith_utils.cli.sample_positive_gaussian",
|
|
85
|
+
lambda mean, approx_upper_bound, trial_number: [1.25, 2.5],
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
assert main(["positive-gaussian-samples", "2", "--mean", "1", "--approx-upper-bound", "3"]) == 0
|
|
89
|
+
|
|
90
|
+
assert capsys.readouterr().out.splitlines() == ["1.25", "2.5"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from src.smith_utils.numeric.sampling import sample_positive_gaussian
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_sample_positive_gaussian_returns_requested_number(monkeypatch):
|
|
7
|
+
values = iter([-1.0, 12.5, 6.0, 7.5, 8.0])
|
|
8
|
+
|
|
9
|
+
monkeypatch.setattr("src.smith_utils.numeric.sampling.random.gauss", lambda mean, sigma: next(values))
|
|
10
|
+
|
|
11
|
+
assert sample_positive_gaussian(mean=5.0, approx_upper_bound=10.0, trial_number=3) == [
|
|
12
|
+
6.0,
|
|
13
|
+
7.5,
|
|
14
|
+
8.0,
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_sample_positive_gaussian_zero_trials_returns_empty_list():
|
|
19
|
+
assert sample_positive_gaussian(mean=5.0, approx_upper_bound=10.0, trial_number=0) == []
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_sample_positive_gaussian_validates_trial_number():
|
|
23
|
+
with pytest.raises(ValueError, match="trial_number must be >= 0"):
|
|
24
|
+
sample_positive_gaussian(mean=5.0, approx_upper_bound=10.0, trial_number=-1)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def test_sample_positive_gaussian_validates_positive_mean():
|
|
28
|
+
with pytest.raises(ValueError, match="mean must be positive"):
|
|
29
|
+
sample_positive_gaussian(mean=0.0, approx_upper_bound=10.0, trial_number=1)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_sample_positive_gaussian_validates_upper_bound_greater_than_mean():
|
|
33
|
+
with pytest.raises(ValueError, match="approx_upper_bound must be greater than mean"):
|
|
34
|
+
sample_positive_gaussian(mean=5.0, approx_upper_bound=5.0, trial_number=1)
|
|
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
|
|
File without changes
|