nmr 0.9.0__tar.gz → 0.10.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.
- nmr-0.10.0/PKG-INFO +148 -0
- nmr-0.10.0/README.md +127 -0
- nmr-0.10.0/nmr/.DS_Store +0 -0
- {nmr-0.9.0 → nmr-0.10.0}/nmr/__init__.py +7 -9
- nmr-0.10.0/nmr/__main__.py +90 -0
- nmr-0.10.0/nmr/_main.py +94 -0
- nmr-0.10.0/nmr/category.py +104 -0
- {nmr-0.9.0 → nmr-0.10.0}/nmr/count_words.py +4 -3
- nmr-0.10.0/nmr/nmr.py +27 -0
- nmr-0.10.0/nmr/pack_numbers.py +206 -0
- nmr-0.10.0/nmr/radixes.py +19 -0
- nmr-0.10.0/nmr/type_namer.py +69 -0
- nmr-0.10.0/nmr/types/__init__.py +37 -0
- nmr-0.10.0/nmr/types/chess.py +89 -0
- nmr-0.10.0/nmr/types/fraction.py +40 -0
- nmr-0.10.0/nmr/types/integer.py +17 -0
- nmr-0.10.0/nmr/types/ip_address.py +14 -0
- nmr-0.10.0/nmr/types/lat_long.py +50 -0
- nmr-0.10.0/nmr/types/phone_number.py +48 -0
- nmr-0.10.0/nmr/types/sem_ver.py +29 -0
- nmr-0.10.0/nmr/types/time/constants.py +87 -0
- nmr-0.10.0/nmr/types/time/formats.py +60 -0
- nmr-0.10.0/nmr/types/time/time.py +123 -0
- nmr-0.10.0/nmr/types/uuid.py +23 -0
- nmr-0.10.0/nmr/words.py +138 -0
- nmr-0.10.0/pyproject.toml +50 -0
- nmr-0.9.0/LICENSE +0 -24
- nmr-0.9.0/PKG-INFO +0 -23
- nmr-0.9.0/nmr/__main__.py +0 -63
- nmr-0.9.0/nmr/_main.py +0 -92
- nmr-0.9.0/nmr/nmr.py +0 -125
- nmr-0.9.0/nmr/types/__init__.py +0 -32
- nmr-0.9.0/nmr/types/_type.py +0 -29
- nmr-0.9.0/nmr/types/fraction.py +0 -75
- nmr-0.9.0/nmr/types/hex.py +0 -13
- nmr-0.9.0/nmr/types/integer.py +0 -11
- nmr-0.9.0/nmr/types/ip_address.py +0 -14
- nmr-0.9.0/nmr/types/lat_long.py +0 -48
- nmr-0.9.0/nmr/types/sem_ver.py +0 -30
- nmr-0.9.0/nmr/types/times.py +0 -109
- nmr-0.9.0/nmr/types/type_groups.py +0 -50
- nmr-0.9.0/nmr/types/uuid.py +0 -20
- nmr-0.9.0/pyproject.toml +0 -34
- nmr-0.9.0/setup.py +0 -35
- /nmr-0.9.0/README.rst → /nmr-0.10.0/nmr/types/time/__init__.py +0 -0
nmr-0.10.0/PKG-INFO
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nmr
|
|
3
|
+
Version: 0.10.0
|
|
4
|
+
Summary: 🌐 Name all canonical things 🌐
|
|
5
|
+
Author: Tom Ritchford
|
|
6
|
+
Author-email: Tom Ritchford <tom@swirly.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Requires-Dist: dtyper>=2.0.0,<3
|
|
15
|
+
Requires-Dist: lat-lon-parser>=1.3.0,<2
|
|
16
|
+
Requires-Dist: typer>=0.7.0,<0.8
|
|
17
|
+
Requires-Dist: chess>=1.9.3,<2
|
|
18
|
+
Requires-Dist: semver>=3.0.2,<4
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
🔢 `nmr`: name all canonical things 🔢
|
|
23
|
+
|
|
24
|
+
Give each canonical thing a unique English name using non-repeating words from a
|
|
25
|
+
short list of common, short English words... or use a word list of your choice.
|
|
26
|
+
|
|
27
|
+
The word list is here: https://github.com/rec/nmr/blob/main/words.txt
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
Canonical things include but are not limited to:
|
|
31
|
+
|
|
32
|
+
* numbers
|
|
33
|
+
* fractions
|
|
34
|
+
* times, dates, time intervals
|
|
35
|
+
* lat/long positions on the earth
|
|
36
|
+
* IP addresses
|
|
37
|
+
* UUIDs
|
|
38
|
+
|
|
39
|
+
Other unimplemented possibilities:
|
|
40
|
+
|
|
41
|
+
* Phone numbers
|
|
42
|
+
* Periodic table
|
|
43
|
+
* Chemical compounds(?) (probably not worth the huge effort)
|
|
44
|
+
* Pieces of music! (my original motivating example)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
Shorter or simpler things should generally be represented by shorter names.
|
|
48
|
+
|
|
49
|
+
Extending this to your own domain is easy and you can either add it to this
|
|
50
|
+
codebase or write your own code and easily plug in in.
|
|
51
|
+
|
|
52
|
+
Installs both a module named `nmr` and an executable called `nmr.py`
|
|
53
|
+
|
|
54
|
+
# EXAMPLE
|
|
55
|
+
|
|
56
|
+
``` python
|
|
57
|
+
import nmr
|
|
58
|
+
|
|
59
|
+
assert nmr(0) == ['the']
|
|
60
|
+
assert nmr(2718281828) == ['the', 'race', 'tie', 'hook']
|
|
61
|
+
|
|
62
|
+
for i in range(-2, 3):
|
|
63
|
+
print(i, ':', *nmr(i))
|
|
64
|
+
|
|
65
|
+
# Prints
|
|
66
|
+
# -2 : to
|
|
67
|
+
# -1 : of
|
|
68
|
+
# 0 : the
|
|
69
|
+
# 1 : and
|
|
70
|
+
# 2 : a
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
# HOW IT WORKS
|
|
74
|
+
|
|
75
|
+
All of this involves the same trick over and over again - counting using positional notation:
|
|
76
|
+
https://en.wikipedia.org/wiki/Positional_notation
|
|
77
|
+
|
|
78
|
+
By default `nmr` has 1628 different words, which you can think of as 1628 different
|
|
79
|
+
digits.
|
|
80
|
+
|
|
81
|
+
There's a wrinkle that makes things harder: the author decided that repeating
|
|
82
|
+
words was unaesthetic, so counting is a little trickier, but you don't have to
|
|
83
|
+
understand how it's done.
|
|
84
|
+
|
|
85
|
+
With repeating words eliminated, 1628 is the minimum total number of words
|
|
86
|
+
needed to be able to represent all 64-bit integers with at most six words.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
Conversion runs as follows.
|
|
90
|
+
|
|
91
|
+
First you take your type, whatever it is, and figure out a way to represent it as a
|
|
92
|
+
number in positional notation, where perhaps the digits have different values.
|
|
93
|
+
|
|
94
|
+
Suppose you're wanting to represent the time of the day, down to milliseconds.
|
|
95
|
+
|
|
96
|
+
You represent it as a four digit number: hours, minutes, seconds, milliseconds
|
|
97
|
+
|
|
98
|
+
Next, you take the positional number, and evaluate it into a "type-relative number",
|
|
99
|
+
a number that makes sense only within this type.
|
|
100
|
+
|
|
101
|
+
For example, to represent the time 15:11:55.823, we'd evaluate the number
|
|
102
|
+
`((((15 * 60) + 11) * 60 + 55) * 1000 + 823)` to get the type relative number,
|
|
103
|
+
but this number might mean something completely different for say, lat-long.
|
|
104
|
+
|
|
105
|
+
Now, each type also gets its own fixed "type identifier number" and that gets combined
|
|
106
|
+
with the type-relative number to give a new number, the `nmr` number which is unique
|
|
107
|
+
over all types.
|
|
108
|
+
|
|
109
|
+
Then that `nmr` number is encoded into a non-repeating sequence of words from the
|
|
110
|
+
word list, which is the `nmr` name.
|
|
111
|
+
|
|
112
|
+
----
|
|
113
|
+
|
|
114
|
+
In the reverse direction, a `nmr` name is given, and it's converted back into an `nmr`
|
|
115
|
+
number, then split into the type identifier number and type-relative number.
|
|
116
|
+
|
|
117
|
+
The type identifier number is used to look up which type is being decoded, and then
|
|
118
|
+
the type-relative number is decoded into the positional number, and finally
|
|
119
|
+
back into the original type.
|
|
120
|
+
|
|
121
|
+
----
|
|
122
|
+
|
|
123
|
+
You are guaranteed that for each original canonical thing, T, there is a unique
|
|
124
|
+
canonical name, and that decoding that name will always reproduce exactly the
|
|
125
|
+
same thing.
|
|
126
|
+
|
|
127
|
+
It's mathematically
|
|
128
|
+
an [injection](https://en.wikipedia.org/wiki/Bijection,_injection_and_surjection).
|
|
129
|
+
|
|
130
|
+
On the other hand, if you start with a random name and try to decode, a data type
|
|
131
|
+
isn't required to give any guarantees at all.
|
|
132
|
+
|
|
133
|
+
You aren't even guaranteed that the decoding will return anything - some names just
|
|
134
|
+
won't correspond even to a known type. If a name can be decoded to a known type,
|
|
135
|
+
that type decoder might still not be able to come up with a type. Or, if it comes
|
|
136
|
+
up with a type, that type might have a different canonical name.
|
|
137
|
+
|
|
138
|
+
This is all good because it makes it really easy to whip up an encoder/decoder, but for
|
|
139
|
+
the basic encoders included with the program, we can offer a better guarantee: that we
|
|
140
|
+
can always decode any any type-relative number and return an instance of the type, even
|
|
141
|
+
if we weren't given a canonical name.
|
|
142
|
+
|
|
143
|
+
Separately, we add a "type wrapping" feature to the main program so that unknown type
|
|
144
|
+
identifier numbers just wrap repeated over known type identifiers - again, this will
|
|
145
|
+
never be a canonical name.
|
|
146
|
+
|
|
147
|
+
With those features, it means you can type random names into the program, and see what
|
|
148
|
+
sort of thing you get.
|
nmr-0.10.0/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
🔢 `nmr`: name all canonical things 🔢
|
|
2
|
+
|
|
3
|
+
Give each canonical thing a unique English name using non-repeating words from a
|
|
4
|
+
short list of common, short English words... or use a word list of your choice.
|
|
5
|
+
|
|
6
|
+
The word list is here: https://github.com/rec/nmr/blob/main/words.txt
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Canonical things include but are not limited to:
|
|
10
|
+
|
|
11
|
+
* numbers
|
|
12
|
+
* fractions
|
|
13
|
+
* times, dates, time intervals
|
|
14
|
+
* lat/long positions on the earth
|
|
15
|
+
* IP addresses
|
|
16
|
+
* UUIDs
|
|
17
|
+
|
|
18
|
+
Other unimplemented possibilities:
|
|
19
|
+
|
|
20
|
+
* Phone numbers
|
|
21
|
+
* Periodic table
|
|
22
|
+
* Chemical compounds(?) (probably not worth the huge effort)
|
|
23
|
+
* Pieces of music! (my original motivating example)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Shorter or simpler things should generally be represented by shorter names.
|
|
27
|
+
|
|
28
|
+
Extending this to your own domain is easy and you can either add it to this
|
|
29
|
+
codebase or write your own code and easily plug in in.
|
|
30
|
+
|
|
31
|
+
Installs both a module named `nmr` and an executable called `nmr.py`
|
|
32
|
+
|
|
33
|
+
# EXAMPLE
|
|
34
|
+
|
|
35
|
+
``` python
|
|
36
|
+
import nmr
|
|
37
|
+
|
|
38
|
+
assert nmr(0) == ['the']
|
|
39
|
+
assert nmr(2718281828) == ['the', 'race', 'tie', 'hook']
|
|
40
|
+
|
|
41
|
+
for i in range(-2, 3):
|
|
42
|
+
print(i, ':', *nmr(i))
|
|
43
|
+
|
|
44
|
+
# Prints
|
|
45
|
+
# -2 : to
|
|
46
|
+
# -1 : of
|
|
47
|
+
# 0 : the
|
|
48
|
+
# 1 : and
|
|
49
|
+
# 2 : a
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
# HOW IT WORKS
|
|
53
|
+
|
|
54
|
+
All of this involves the same trick over and over again - counting using positional notation:
|
|
55
|
+
https://en.wikipedia.org/wiki/Positional_notation
|
|
56
|
+
|
|
57
|
+
By default `nmr` has 1628 different words, which you can think of as 1628 different
|
|
58
|
+
digits.
|
|
59
|
+
|
|
60
|
+
There's a wrinkle that makes things harder: the author decided that repeating
|
|
61
|
+
words was unaesthetic, so counting is a little trickier, but you don't have to
|
|
62
|
+
understand how it's done.
|
|
63
|
+
|
|
64
|
+
With repeating words eliminated, 1628 is the minimum total number of words
|
|
65
|
+
needed to be able to represent all 64-bit integers with at most six words.
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
Conversion runs as follows.
|
|
69
|
+
|
|
70
|
+
First you take your type, whatever it is, and figure out a way to represent it as a
|
|
71
|
+
number in positional notation, where perhaps the digits have different values.
|
|
72
|
+
|
|
73
|
+
Suppose you're wanting to represent the time of the day, down to milliseconds.
|
|
74
|
+
|
|
75
|
+
You represent it as a four digit number: hours, minutes, seconds, milliseconds
|
|
76
|
+
|
|
77
|
+
Next, you take the positional number, and evaluate it into a "type-relative number",
|
|
78
|
+
a number that makes sense only within this type.
|
|
79
|
+
|
|
80
|
+
For example, to represent the time 15:11:55.823, we'd evaluate the number
|
|
81
|
+
`((((15 * 60) + 11) * 60 + 55) * 1000 + 823)` to get the type relative number,
|
|
82
|
+
but this number might mean something completely different for say, lat-long.
|
|
83
|
+
|
|
84
|
+
Now, each type also gets its own fixed "type identifier number" and that gets combined
|
|
85
|
+
with the type-relative number to give a new number, the `nmr` number which is unique
|
|
86
|
+
over all types.
|
|
87
|
+
|
|
88
|
+
Then that `nmr` number is encoded into a non-repeating sequence of words from the
|
|
89
|
+
word list, which is the `nmr` name.
|
|
90
|
+
|
|
91
|
+
----
|
|
92
|
+
|
|
93
|
+
In the reverse direction, a `nmr` name is given, and it's converted back into an `nmr`
|
|
94
|
+
number, then split into the type identifier number and type-relative number.
|
|
95
|
+
|
|
96
|
+
The type identifier number is used to look up which type is being decoded, and then
|
|
97
|
+
the type-relative number is decoded into the positional number, and finally
|
|
98
|
+
back into the original type.
|
|
99
|
+
|
|
100
|
+
----
|
|
101
|
+
|
|
102
|
+
You are guaranteed that for each original canonical thing, T, there is a unique
|
|
103
|
+
canonical name, and that decoding that name will always reproduce exactly the
|
|
104
|
+
same thing.
|
|
105
|
+
|
|
106
|
+
It's mathematically
|
|
107
|
+
an [injection](https://en.wikipedia.org/wiki/Bijection,_injection_and_surjection).
|
|
108
|
+
|
|
109
|
+
On the other hand, if you start with a random name and try to decode, a data type
|
|
110
|
+
isn't required to give any guarantees at all.
|
|
111
|
+
|
|
112
|
+
You aren't even guaranteed that the decoding will return anything - some names just
|
|
113
|
+
won't correspond even to a known type. If a name can be decoded to a known type,
|
|
114
|
+
that type decoder might still not be able to come up with a type. Or, if it comes
|
|
115
|
+
up with a type, that type might have a different canonical name.
|
|
116
|
+
|
|
117
|
+
This is all good because it makes it really easy to whip up an encoder/decoder, but for
|
|
118
|
+
the basic encoders included with the program, we can offer a better guarantee: that we
|
|
119
|
+
can always decode any any type-relative number and return an instance of the type, even
|
|
120
|
+
if we weren't given a canonical name.
|
|
121
|
+
|
|
122
|
+
Separately, we add a "type wrapping" feature to the main program so that unknown type
|
|
123
|
+
identifier numbers just wrap repeated over known type identifiers - again, this will
|
|
124
|
+
never be a canonical name.
|
|
125
|
+
|
|
126
|
+
With those features, it means you can type random names into the program, and see what
|
|
127
|
+
sort of thing you get.
|
nmr-0.10.0/nmr/.DS_Store
ADDED
|
Binary file
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"""
|
|
2
|
-
🔢 ``nmr``:
|
|
2
|
+
🔢 ``nmr``: name all canonical things 🔢
|
|
3
3
|
|
|
4
|
-
Convert
|
|
5
|
-
non-repeating English words... or use a
|
|
4
|
+
Convert each canonical thing into a number, and then that number into a unique,
|
|
5
|
+
non-repeating name from a short list of common, short English words... or use a
|
|
6
|
+
word list of your choice.
|
|
6
7
|
|
|
7
8
|
Installs both a module named ``nmr`` and an executable called ``nmr.py``
|
|
8
9
|
|
|
@@ -26,12 +27,9 @@ EXAMPLE
|
|
|
26
27
|
# 1 : and
|
|
27
28
|
# 2 : a
|
|
28
29
|
"""
|
|
29
|
-
__version__ = '0.8.0'
|
|
30
30
|
|
|
31
|
-
from .
|
|
32
|
-
import sys
|
|
31
|
+
from .nmr import Nmr
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
nmr.__dict__.update(globals())
|
|
33
|
+
__all__ = "nmr", "Nmr"
|
|
36
34
|
|
|
37
|
-
|
|
35
|
+
nmr = Nmr()
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import typer
|
|
5
|
+
from typer import Argument, Option, Typer
|
|
6
|
+
|
|
7
|
+
from . import types
|
|
8
|
+
|
|
9
|
+
HELP = """
|
|
10
|
+
The command line utility `nmr` can be used in three ways:
|
|
11
|
+
|
|
12
|
+
1. From the command line, with arguments
|
|
13
|
+
|
|
14
|
+
$ nmr
|
|
15
|
+
frog spot on no
|
|
16
|
+
|
|
17
|
+
$nmr frog spot on on
|
|
18
|
+
12 January 2001
|
|
19
|
+
|
|
20
|
+
2. From the command line, without arguments
|
|
21
|
+
|
|
22
|
+
$ nmr
|
|
23
|
+
In: 12 January 2001
|
|
24
|
+
Out: frog spot on
|
|
25
|
+
In: 17 / 21
|
|
26
|
+
Out: he the word
|
|
27
|
+
In: will new an
|
|
28
|
+
Out: 34.5N 15:24E"
|
|
29
|
+
|
|
30
|
+
3. As a unix pipe:
|
|
31
|
+
|
|
32
|
+
cat first.txt second.txt | nmr > out.txt
|
|
33
|
+
|
|
34
|
+
Each line is determined to be either a name, or a type, and then converted.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
app = Typer(
|
|
38
|
+
add_completion=False,
|
|
39
|
+
context_settings={"help_option_names": ["-h", "--help"]},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@app.command(help=HELP)
|
|
44
|
+
def nmr_main(
|
|
45
|
+
arguments: list[str] = Argument(
|
|
46
|
+
None, help="Things to convert to names, or vice-versa"
|
|
47
|
+
),
|
|
48
|
+
label: bool = Option(
|
|
49
|
+
False,
|
|
50
|
+
"--label",
|
|
51
|
+
"-l",
|
|
52
|
+
help="If true, display the input as a label to the output",
|
|
53
|
+
),
|
|
54
|
+
output_type: str | None = Option(
|
|
55
|
+
None,
|
|
56
|
+
"--output-type",
|
|
57
|
+
"-t",
|
|
58
|
+
help='Try to convert inputs to one of these output formats:'
|
|
59
|
+
f'{" ".join(types.names())}. Abbreviations are possible',
|
|
60
|
+
),
|
|
61
|
+
raise_exceptions: bool = Option(
|
|
62
|
+
False,
|
|
63
|
+
"--raise-exceptions",
|
|
64
|
+
"-e",
|
|
65
|
+
help="If True, don't catch exceptions, allow the program to terminate",
|
|
66
|
+
),
|
|
67
|
+
random_count: int = Option(0, "--random-count", "-r", help="Print random names"),
|
|
68
|
+
word_count: int | None = Option(
|
|
69
|
+
None, "--word-count", "-c", help="How many words from the word file to use"
|
|
70
|
+
),
|
|
71
|
+
word_file: Path | None = Option(
|
|
72
|
+
None,
|
|
73
|
+
"--word-file",
|
|
74
|
+
"-f",
|
|
75
|
+
help="A file containing unique words with one word per line",
|
|
76
|
+
),
|
|
77
|
+
) -> None:
|
|
78
|
+
d = dict(locals())
|
|
79
|
+
|
|
80
|
+
from ._main import Main
|
|
81
|
+
|
|
82
|
+
Main(**d)()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def main() -> None:
|
|
86
|
+
typer.main.get_command(app)()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
if __name__ == "__main__":
|
|
90
|
+
main()
|
nmr-0.10.0/nmr/_main.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import random
|
|
4
|
+
import shlex
|
|
5
|
+
import sys
|
|
6
|
+
from functools import cached_property
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any, NoReturn
|
|
9
|
+
from collections.abc import Iterable
|
|
10
|
+
|
|
11
|
+
import dtyper
|
|
12
|
+
|
|
13
|
+
from .__main__ import nmr_main
|
|
14
|
+
from .nmr import Nmr
|
|
15
|
+
from .type_namer import TypeNamer, get_class
|
|
16
|
+
|
|
17
|
+
"""See __main__.HELP"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dtyper.dataclass(nmr_main)
|
|
21
|
+
class Main:
|
|
22
|
+
returncode = 0
|
|
23
|
+
|
|
24
|
+
# Copied from nmr_main
|
|
25
|
+
arguments: list[str]
|
|
26
|
+
label: bool
|
|
27
|
+
output_type: str | None
|
|
28
|
+
raise_exceptions: bool
|
|
29
|
+
random_count: int
|
|
30
|
+
word_count: int | None
|
|
31
|
+
word_file: Path | None
|
|
32
|
+
|
|
33
|
+
def __call__(self) -> None:
|
|
34
|
+
if self.is_pipe and self.arguments:
|
|
35
|
+
raise ValueError("nmr takes no arguments when used as a pipe")
|
|
36
|
+
if self.random_count and (self.arguments or self.is_pipe):
|
|
37
|
+
raise ValueError("nmr takes no arguments when --random-count is set")
|
|
38
|
+
|
|
39
|
+
lines: Iterable[str]
|
|
40
|
+
if self.arguments:
|
|
41
|
+
lines = [shlex.join(self.arguments)]
|
|
42
|
+
elif self.is_pipe:
|
|
43
|
+
lines = sys.stdin
|
|
44
|
+
else:
|
|
45
|
+
|
|
46
|
+
def _lines() -> Iterable[str]:
|
|
47
|
+
while True:
|
|
48
|
+
try:
|
|
49
|
+
yield input("In: ")
|
|
50
|
+
except EOFError:
|
|
51
|
+
return
|
|
52
|
+
|
|
53
|
+
lines = _lines()
|
|
54
|
+
|
|
55
|
+
for line in lines:
|
|
56
|
+
if words := line.partition("#")[0].strip():
|
|
57
|
+
try:
|
|
58
|
+
result = self.nmr.convert(words)
|
|
59
|
+
except Exception as e:
|
|
60
|
+
if self.raise_exceptions:
|
|
61
|
+
raise
|
|
62
|
+
self.returncode = 1
|
|
63
|
+
print("ERROR:", e, file=sys.stderr)
|
|
64
|
+
else:
|
|
65
|
+
if not (self.is_pipe or self.arguments):
|
|
66
|
+
print("Out: ", end="")
|
|
67
|
+
print(result)
|
|
68
|
+
|
|
69
|
+
@cached_property
|
|
70
|
+
def nmr(self) -> Nmr:
|
|
71
|
+
from nmr import Nmr
|
|
72
|
+
|
|
73
|
+
return Nmr(self.word_file, self.word_count)
|
|
74
|
+
|
|
75
|
+
@cached_property
|
|
76
|
+
def is_pipe(self) -> bool:
|
|
77
|
+
return not sys.stdin.isatty()
|
|
78
|
+
|
|
79
|
+
@cached_property
|
|
80
|
+
def _type_class(self) -> type[TypeNamer[Any]] | None:
|
|
81
|
+
if self.output_type:
|
|
82
|
+
return get_class(self.output_type)
|
|
83
|
+
return None
|
|
84
|
+
|
|
85
|
+
def rnd(self) -> None: # TODO re-enable
|
|
86
|
+
for _ in range(self.random_count):
|
|
87
|
+
r = int(10 ** random.uniform(0, 50))
|
|
88
|
+
print(f"{r}:", *self.nmr.encode_to_name(r))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def exit(*error: Any) -> NoReturn:
|
|
92
|
+
if error:
|
|
93
|
+
print(*error, file=sys.stderr)
|
|
94
|
+
sys.exit(bool(error))
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import IntEnum, auto
|
|
4
|
+
|
|
5
|
+
DEFAULT_RADIX = 8
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Category:
|
|
9
|
+
name: str
|
|
10
|
+
value: int
|
|
11
|
+
|
|
12
|
+
@classmethod
|
|
13
|
+
def divmod(cls, n: int, strict: bool = False) -> tuple[Category, int]:
|
|
14
|
+
d, m = divmod(n, cls.radix())
|
|
15
|
+
lc = len(cls) # type: ignore[arg-type]
|
|
16
|
+
if m >= lc:
|
|
17
|
+
if strict:
|
|
18
|
+
raise IndexError(f"Out of range {m=}, len(cls)={lc}")
|
|
19
|
+
m = m % lc
|
|
20
|
+
|
|
21
|
+
self = cls(m + 1) # type: ignore[call-arg]
|
|
22
|
+
return self, d
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def radix(cls) -> int:
|
|
26
|
+
return _RADIX_TABLE.get(cls, DEFAULT_RADIX) # type: ignore[arg-type]
|
|
27
|
+
|
|
28
|
+
_SUBCLASSES: dict[str, type[Category]] = {}
|
|
29
|
+
|
|
30
|
+
def __init_subclass__(cls) -> None:
|
|
31
|
+
assert cls.__name__ not in cls._SUBCLASSES
|
|
32
|
+
cls._SUBCLASSES[cls.__name__] = cls
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Group(Category, IntEnum):
|
|
36
|
+
MATH = auto()
|
|
37
|
+
SCIENCE = auto()
|
|
38
|
+
ART = auto()
|
|
39
|
+
LOCATION = auto()
|
|
40
|
+
|
|
41
|
+
COMPUTER = auto()
|
|
42
|
+
GAME = auto()
|
|
43
|
+
COMMERCIAL = auto()
|
|
44
|
+
COMBINE = auto()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Subcategory(Category):
|
|
48
|
+
def number_to_index(self, n: int) -> int:
|
|
49
|
+
g = Group[self.__class__.__name__.upper()]
|
|
50
|
+
return g.value - 1 + Group.radix() * (self.value - 1 + self.radix() * n)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Math(Subcategory, IntEnum):
|
|
54
|
+
INTEGER = auto()
|
|
55
|
+
FRACTION = auto()
|
|
56
|
+
FLOATING = auto()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class Science(Subcategory, IntEnum):
|
|
60
|
+
ELEMENT = auto()
|
|
61
|
+
UNIT = auto()
|
|
62
|
+
SMILES = auto()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class Art(Subcategory, IntEnum):
|
|
66
|
+
MUSIC = auto()
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class Location(Subcategory, IntEnum):
|
|
70
|
+
LAT_LONG = auto()
|
|
71
|
+
TIME = auto()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class Computer(Subcategory, IntEnum):
|
|
75
|
+
IP_V4_ADDRESS = auto()
|
|
76
|
+
IP_V6_ADDRESS = auto()
|
|
77
|
+
SEMVER = auto()
|
|
78
|
+
UUID = auto()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class Game(Subcategory, IntEnum):
|
|
82
|
+
BACKGAMMON = auto()
|
|
83
|
+
CARDS = auto()
|
|
84
|
+
CHESS = auto()
|
|
85
|
+
GO = auto()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class Commercial(Subcategory, IntEnum):
|
|
89
|
+
ISBN = auto()
|
|
90
|
+
UPC = auto()
|
|
91
|
+
PHONE_NUMBER = auto()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class Combine(Subcategory, IntEnum):
|
|
95
|
+
COMBINE = auto()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
_RADIX_TABLE = {Location: 4, Computer: 32, Commercial: 32, Combine: 1}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def make_category(n: int, strict: bool = False) -> tuple[Category, int]:
|
|
102
|
+
group, d = Group.divmod(n, strict)
|
|
103
|
+
subclass = Category._SUBCLASSES[group.name.capitalize()]
|
|
104
|
+
return subclass.divmod(d, strict)
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import threading
|
|
2
|
-
from typing import Optional
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
class CountWords:
|
|
6
|
-
def __init__(self, n):
|
|
7
|
+
def __init__(self, n: int) -> None:
|
|
7
8
|
self.n = n
|
|
8
9
|
self._perm_count = [(1, 0)]
|
|
9
10
|
self._lock = threading.Lock()
|
|
10
11
|
|
|
11
|
-
def
|
|
12
|
+
def count(self, c: int | None = None) -> int:
|
|
12
13
|
if c is None:
|
|
13
14
|
c = self.n
|
|
14
15
|
|
nmr-0.10.0/nmr/nmr.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
|
|
5
|
+
from . import types
|
|
6
|
+
from .words import Words
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Nmr(Words):
|
|
10
|
+
"""Nmr handles encoding integers to names, and decoding names into integers.
|
|
11
|
+
|
|
12
|
+
The exact correspondence depends on the choice and number of words
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def name_to_str(self, name: Sequence[str]) -> str:
|
|
16
|
+
"""Given a name, a sequence of strings from `self.words`"""
|
|
17
|
+
index = self.decode_from_name(name)
|
|
18
|
+
return types.index_to_str(index)
|
|
19
|
+
|
|
20
|
+
def str_to_name(self, s: str) -> Sequence[str]:
|
|
21
|
+
index = types.str_to_index(s)
|
|
22
|
+
return self.encode_to_name(index)
|
|
23
|
+
|
|
24
|
+
def convert(self, s: str) -> str:
|
|
25
|
+
if self.is_name(split := s.split()):
|
|
26
|
+
return self.name_to_str(split)
|
|
27
|
+
return " ".join(self.str_to_name(s))
|