dt-counter 0.1.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.
- dt_counter-0.1.0/LICENSE +8 -0
- dt_counter-0.1.0/PKG-INFO +46 -0
- dt_counter-0.1.0/README.md +27 -0
- dt_counter-0.1.0/pyproject.toml +30 -0
- dt_counter-0.1.0/setup.cfg +4 -0
- dt_counter-0.1.0/src/dt_counter/__init__.py +7 -0
- dt_counter-0.1.0/src/dt_counter/core.py +63 -0
- dt_counter-0.1.0/src/dt_counter/py.typed +0 -0
- dt_counter-0.1.0/src/dt_counter.egg-info/PKG-INFO +46 -0
- dt_counter-0.1.0/src/dt_counter.egg-info/SOURCES.txt +12 -0
- dt_counter-0.1.0/src/dt_counter.egg-info/dependency_links.txt +1 -0
- dt_counter-0.1.0/src/dt_counter.egg-info/requires.txt +5 -0
- dt_counter-0.1.0/src/dt_counter.egg-info/top_level.txt +1 -0
- dt_counter-0.1.0/tests/test_dt_counter.py +49 -0
dt_counter-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Muhammad Ashhad Mahdi
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dt-counter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Count and extract ints, floats, letters and special characters in strings and lists.
|
|
5
|
+
Author-email: Muhammad Ashhad Mahdi <xorvixsensei@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/xorvixsensei/dt-counter
|
|
8
|
+
Keywords: string,parsing,datatype,counter,text
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
16
|
+
Requires-Dist: build; extra == "dev"
|
|
17
|
+
Requires-Dist: twine; extra == "dev"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# dt-counter
|
|
21
|
+
|
|
22
|
+
Count and extract data types in a string or list.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install dt-counter
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import dt_counter as dt
|
|
30
|
+
|
|
31
|
+
s = "ahxue3747" + "#!2838sjwj"
|
|
32
|
+
dt.count(int, s) # 2 (3747 and 2838)
|
|
33
|
+
dt.count(int, s, mode="chars") # 8 (single digits)
|
|
34
|
+
dt.extract(float, "costs 3.50!") # [3.5]
|
|
35
|
+
dt.count(dt.SPECIAL, s) # 2
|
|
36
|
+
dt.count_all("Order #42 costs 3.50!") # dict of every category
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Categories: `int`, `float`, `str`, `bool` (lists), plus `dt.ALPHA`, `dt.UPPER`,
|
|
40
|
+
`dt.LOWER`, `dt.DIGIT`, `dt.SPECIAL`, `dt.SPACE`.
|
|
41
|
+
|
|
42
|
+
Rules: every character belongs to one token, so `3.50` is one float (not ints
|
|
43
|
+
plus a special). `1.2.3` is ints and dots, not a float. `-5` is a signed int.
|
|
44
|
+
No thousands separators or exponents.
|
|
45
|
+
|
|
46
|
+
License: MIT
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# dt-counter
|
|
2
|
+
|
|
3
|
+
Count and extract data types in a string or list.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install dt-counter
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import dt_counter as dt
|
|
11
|
+
|
|
12
|
+
s = "ahxue3747" + "#!2838sjwj"
|
|
13
|
+
dt.count(int, s) # 2 (3747 and 2838)
|
|
14
|
+
dt.count(int, s, mode="chars") # 8 (single digits)
|
|
15
|
+
dt.extract(float, "costs 3.50!") # [3.5]
|
|
16
|
+
dt.count(dt.SPECIAL, s) # 2
|
|
17
|
+
dt.count_all("Order #42 costs 3.50!") # dict of every category
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Categories: `int`, `float`, `str`, `bool` (lists), plus `dt.ALPHA`, `dt.UPPER`,
|
|
21
|
+
`dt.LOWER`, `dt.DIGIT`, `dt.SPECIAL`, `dt.SPACE`.
|
|
22
|
+
|
|
23
|
+
Rules: every character belongs to one token, so `3.50` is one float (not ints
|
|
24
|
+
plus a special). `1.2.3` is ints and dots, not a float. `-5` is a signed int.
|
|
25
|
+
No thousands separators or exponents.
|
|
26
|
+
|
|
27
|
+
License: MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dt-counter"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Count and extract ints, floats, letters and special characters in strings and lists."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "Muhammad Ashhad Mahdi", email = "xorvixsensei@gmail.com" }]
|
|
13
|
+
keywords = ["string", "parsing", "datatype", "counter", "text"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
dependencies = []
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
dev = ["pytest>=7", "build", "twine"]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/xorvixsensei/dt-counter"
|
|
25
|
+
|
|
26
|
+
[tool.setuptools.packages.find]
|
|
27
|
+
where = ["src"]
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""dt_counter: count and extract data types (ints, floats, letters, symbols) in strings and lists."""
|
|
2
|
+
from .core import (ALPHA, DIGIT, LOWER, SPACE, SPECIAL, UPPER,
|
|
3
|
+
count, count_all, extract)
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
__all__ = ["count", "extract", "count_all", "ALPHA", "UPPER", "LOWER",
|
|
7
|
+
"DIGIT", "SPECIAL", "SPACE", "__version__"]
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Core tokenizer and counting functions."""
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
ALPHA, UPPER, LOWER, DIGIT, SPECIAL, SPACE = "alpha", "upper", "lower", "digit", "special", "space"
|
|
5
|
+
|
|
6
|
+
_TOKEN = re.compile(r"""
|
|
7
|
+
(?P<float>(?<!\d\.)(?:(?<![\w.])[-+])?\d+\.\d+(?!\.\d)) # 3.50, -2.5 (not 1.2.3)
|
|
8
|
+
| (?P<int>(?:(?<![\w.])[-+])?\d+) # 42, -5 (not the - in 5-3)
|
|
9
|
+
| (?P<alpha>[^\W\d_]+) # words
|
|
10
|
+
| (?P<space>\s)
|
|
11
|
+
| (?P<special>.) # leftovers
|
|
12
|
+
""", re.X | re.S)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _tokens(text):
|
|
16
|
+
return [(m.lastgroup, m.group()) for m in _TOKEN.finditer(text)]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _chars(kind, text):
|
|
20
|
+
out = []
|
|
21
|
+
for name, tok in _tokens(text):
|
|
22
|
+
if name in ("int", "float"):
|
|
23
|
+
if kind == DIGIT:
|
|
24
|
+
out += [c for c in tok if c.isdigit()] # sign and '.' belong to the number
|
|
25
|
+
elif name == "alpha":
|
|
26
|
+
if kind == ALPHA:
|
|
27
|
+
out += list(tok)
|
|
28
|
+
elif kind == UPPER:
|
|
29
|
+
out += [c for c in tok if c.isupper()]
|
|
30
|
+
elif kind == LOWER:
|
|
31
|
+
out += [c for c in tok if c.islower()]
|
|
32
|
+
elif name == kind:
|
|
33
|
+
out.append(tok)
|
|
34
|
+
return out
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def extract(kind, data, mode="tokens"):
|
|
38
|
+
if kind in (ALPHA, UPPER, LOWER, DIGIT, SPECIAL, SPACE):
|
|
39
|
+
texts = [data] if isinstance(data, str) else [x for x in data if isinstance(x, str)]
|
|
40
|
+
return [c for t in texts for c in _chars(kind, t)]
|
|
41
|
+
if isinstance(data, str):
|
|
42
|
+
if kind is int:
|
|
43
|
+
if mode == "chars":
|
|
44
|
+
return [int(c) for n, t in _tokens(data) if n == "int" for c in t if c.isdigit()]
|
|
45
|
+
return [int(t) for n, t in _tokens(data) if n == "int"]
|
|
46
|
+
if kind is float:
|
|
47
|
+
return [float(t) for n, t in _tokens(data) if n == "float"]
|
|
48
|
+
if kind is str:
|
|
49
|
+
return [t for n, t in _tokens(data) if n == "alpha"]
|
|
50
|
+
raise ValueError(f"unsupported kind: {kind!r}")
|
|
51
|
+
if kind in (int, float, str, bool, type(None)):
|
|
52
|
+
return [x for x in data if type(x) is kind]
|
|
53
|
+
raise ValueError(f"unsupported kind: {kind!r}")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def count(kind, data, mode="tokens"):
|
|
57
|
+
return len(extract(kind, data, mode))
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def count_all(data):
|
|
61
|
+
kinds = {"int": int, "float": float, "str": str, "alpha": ALPHA, "upper": UPPER,
|
|
62
|
+
"lower": LOWER, "digit": DIGIT, "special": SPECIAL, "space": SPACE}
|
|
63
|
+
return {name: count(k, data) for name, k in kinds.items()}
|
|
File without changes
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dt-counter
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Count and extract ints, floats, letters and special characters in strings and lists.
|
|
5
|
+
Author-email: Muhammad Ashhad Mahdi <xorvixsensei@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/xorvixsensei/dt-counter
|
|
8
|
+
Keywords: string,parsing,datatype,counter,text
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
16
|
+
Requires-Dist: build; extra == "dev"
|
|
17
|
+
Requires-Dist: twine; extra == "dev"
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# dt-counter
|
|
21
|
+
|
|
22
|
+
Count and extract data types in a string or list.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install dt-counter
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import dt_counter as dt
|
|
30
|
+
|
|
31
|
+
s = "ahxue3747" + "#!2838sjwj"
|
|
32
|
+
dt.count(int, s) # 2 (3747 and 2838)
|
|
33
|
+
dt.count(int, s, mode="chars") # 8 (single digits)
|
|
34
|
+
dt.extract(float, "costs 3.50!") # [3.5]
|
|
35
|
+
dt.count(dt.SPECIAL, s) # 2
|
|
36
|
+
dt.count_all("Order #42 costs 3.50!") # dict of every category
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Categories: `int`, `float`, `str`, `bool` (lists), plus `dt.ALPHA`, `dt.UPPER`,
|
|
40
|
+
`dt.LOWER`, `dt.DIGIT`, `dt.SPECIAL`, `dt.SPACE`.
|
|
41
|
+
|
|
42
|
+
Rules: every character belongs to one token, so `3.50` is one float (not ints
|
|
43
|
+
plus a special). `1.2.3` is ints and dots, not a float. `-5` is a signed int.
|
|
44
|
+
No thousands separators or exponents.
|
|
45
|
+
|
|
46
|
+
License: MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/dt_counter/__init__.py
|
|
5
|
+
src/dt_counter/core.py
|
|
6
|
+
src/dt_counter/py.typed
|
|
7
|
+
src/dt_counter.egg-info/PKG-INFO
|
|
8
|
+
src/dt_counter.egg-info/SOURCES.txt
|
|
9
|
+
src/dt_counter.egg-info/dependency_links.txt
|
|
10
|
+
src/dt_counter.egg-info/requires.txt
|
|
11
|
+
src/dt_counter.egg-info/top_level.txt
|
|
12
|
+
tests/test_dt_counter.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dt_counter
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import dt_counter as dt
|
|
3
|
+
|
|
4
|
+
S = "ahxue3747" + "#!2838sjwj"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_example_string():
|
|
8
|
+
assert dt.count(int, S) == 2
|
|
9
|
+
assert dt.count(int, S, mode="chars") == 8
|
|
10
|
+
assert dt.count(dt.SPECIAL, S) == 2
|
|
11
|
+
assert dt.count(dt.ALPHA, S) == 9
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_float_is_single_token():
|
|
15
|
+
assert dt.extract(float, "costs 3.50!") == [3.5]
|
|
16
|
+
assert dt.extract(int, "costs 3.50!") == []
|
|
17
|
+
assert dt.extract(dt.SPECIAL, "costs 3.50!") == ["!"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_version_is_not_float():
|
|
21
|
+
assert dt.extract(int, "1.2.3") == [1, 2, 3]
|
|
22
|
+
assert dt.count(float, "1.2.3") == 0
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_signs():
|
|
26
|
+
assert dt.extract(int, "-5 and 5-3") == [-5, 5, 3]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_sentence_period():
|
|
30
|
+
assert dt.extract(float, "pi=3.14.") == [3.14]
|
|
31
|
+
assert dt.extract(dt.SPECIAL, "pi=3.14.") == ["=", "."]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_lists_use_real_types():
|
|
35
|
+
items = [1, 2.5, "hi", True, 7, "x!", None, 3.0]
|
|
36
|
+
assert dt.count(int, items) == 2
|
|
37
|
+
assert dt.count(float, items) == 2
|
|
38
|
+
assert dt.count(bool, items) == 1
|
|
39
|
+
assert dt.count(dt.SPECIAL, items) == 1
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_count_all_keys():
|
|
43
|
+
assert set(dt.count_all("a1 2.5!")) == {
|
|
44
|
+
"int", "float", "str", "alpha", "upper", "lower", "digit", "special", "space"}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_unsupported_kind():
|
|
48
|
+
with pytest.raises(ValueError):
|
|
49
|
+
dt.count(dict, S)
|