character-encoding-utils 0.0.8__tar.gz → 0.0.10__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.
@@ -1,18 +1,19 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: character-encoding-utils
3
- Version: 0.0.8
4
- Summary: Some character encoding utils.
5
- Project-URL: homepage, https://github.com/TakWolf/character-encoding-utils
6
- Project-URL: source, https://github.com/TakWolf/character-encoding-utils
7
- Project-URL: issues, https://github.com/TakWolf/character-encoding-utils/issues
3
+ Version: 0.0.10
4
+ Summary: Some character encoding utils
5
+ Keywords: encoding,gb2312,big5,shift-jis,ksx1001
8
6
  Author: TakWolf
9
- Maintainer: TakWolf
10
- License: MIT License
11
- License-File: LICENSE
12
- Keywords: big5,encoding,gb2312,ksx1001,shift-jis
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
13
14
  Classifier: License :: OSI Approved :: MIT License
14
15
  Classifier: Operating System :: OS Independent
15
- Classifier: Programming Language :: Python :: 3
16
+ Maintainer: TakWolf
16
17
  Requires-Python: >=3.10
17
18
  Description-Content-Type: text/markdown
18
19
 
@@ -76,4 +77,4 @@ assert ksx1001.decode(bs) == 'abc가쳰'
76
77
 
77
78
  ## License
78
79
 
79
- Under the [MIT license](LICENSE).
80
+ [MIT License](LICENSE)
@@ -58,4 +58,4 @@ assert ksx1001.decode(bs) == 'abc가쳰'
58
58
 
59
59
  ## License
60
60
 
61
- Under the [MIT license](LICENSE).
61
+ [MIT License](LICENSE)
@@ -0,0 +1,33 @@
1
+ [project]
2
+ name = "character-encoding-utils"
3
+ version = "0.0.10"
4
+ description = "Some character encoding utils"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ authors = [
8
+ { name = "TakWolf" },
9
+ ]
10
+ maintainers = [
11
+ { name = "TakWolf" },
12
+ ]
13
+ keywords = ["encoding", "gb2312", "big5", "shift-jis", "ksx1001"]
14
+ requires-python = ">=3.10"
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3.10",
18
+ "Programming Language :: Python :: 3.11",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: 3.13",
21
+ "Programming Language :: Python :: 3.14",
22
+ "License :: OSI Approved :: MIT License",
23
+ "Operating System :: OS Independent",
24
+ ]
25
+
26
+ [dependency-groups]
27
+ dev = [
28
+ "pytest>=8.4.2",
29
+ ]
30
+
31
+ [build-system]
32
+ requires = ["uv_build"]
33
+ build-backend = "uv_build"
@@ -4,31 +4,35 @@ class Big5Exception(Exception):
4
4
 
5
5
 
6
6
  class Big5EncodeError(Big5Exception):
7
- object: str
7
+ obj: str
8
8
  position: int
9
9
  reason: str
10
10
 
11
11
  def __init__(self, obj: str, position: int, reason: str):
12
- super().__init__(f"'big5' codec can't encode character '\\u{ord(obj):x}' in position {position}: {reason}")
13
- self.object = obj
12
+ self.obj = obj
14
13
  self.position = position
15
14
  self.reason = reason
16
15
 
16
+ def __str__(self) -> str:
17
+ return f"'big5' codec can't encode character '\\u{ord(self.obj):x}' in position {self.position}: {self.reason}"
18
+
17
19
 
18
20
  class Big5DecodeError(Big5Exception):
19
- object: bytes
21
+ obj: bytes
20
22
  position: int
21
23
  reason: str
22
24
 
23
25
  def __init__(self, obj: bytes, position: int, reason: str):
24
- if len(obj) <= 1:
25
- super().__init__(f"'big5' codec can't decode byte 0x{obj[0]:x} in position {position}: {reason}")
26
- else:
27
- super().__init__(f"'big5' codec can't decode bytes in position {position}-{position + len(obj) - 1}: {reason}")
28
- self.object = obj
26
+ self.obj = obj
29
27
  self.position = position
30
28
  self.reason = reason
31
29
 
30
+ def __str__(self) -> str:
31
+ if len(self.obj) <= 1:
32
+ return f"'big5' codec can't decode byte 0x{self.obj[0]:x} in position {self.position}: {self.reason}"
33
+ else:
34
+ return f"'big5' codec can't decode bytes in position {self.position}-{self.position + len(self.obj) - 1}: {self.reason}"
35
+
32
36
 
33
37
  def encode(cs: str) -> bytes:
34
38
  bs = bytearray()
@@ -110,9 +114,9 @@ def query_code(c: str) -> int:
110
114
  try:
111
115
  bs = encode(c)
112
116
  except Big5EncodeError as e:
113
- raise Big5Exception(f"'{c}' is not a 'big5' character") from e
117
+ raise Big5Exception(f"'\\u{ord(c):x}' is not a 'big5' character") from e
114
118
  if len(bs) == 1:
115
- raise Big5Exception(f"'{c}' is a ascii character")
119
+ raise Big5Exception(f"'\\u{ord(c):x}' is a ascii character")
116
120
  code = int(f'{bs[0]:02x}{bs[1]:02x}', 16)
117
121
  return code
118
122
 
@@ -7,31 +7,35 @@ class GB2312Exception(Exception):
7
7
 
8
8
 
9
9
  class GB2312EncodeError(GB2312Exception):
10
- object: str
10
+ obj: str
11
11
  position: int
12
12
  reason: str
13
13
 
14
14
  def __init__(self, obj: str, position: int, reason: str):
15
- super().__init__(f"'gb2312' codec can't encode character '\\u{ord(obj):x}' in position {position}: {reason}")
16
- self.object = obj
15
+ self.obj = obj
17
16
  self.position = position
18
17
  self.reason = reason
19
18
 
19
+ def __str__(self) -> str:
20
+ return f"'gb2312' codec can't encode character '\\u{ord(self.obj):x}' in position {self.position}: {self.reason}"
21
+
20
22
 
21
23
  class GB2312DecodeError(GB2312Exception):
22
- object: bytes
24
+ obj: bytes
23
25
  position: int
24
26
  reason: str
25
27
 
26
28
  def __init__(self, obj: bytes, position: int, reason: str):
27
- if len(obj) <= 1:
28
- super().__init__(f"'gb2312' codec can't decode byte 0x{obj[0]:x} in position {position}: {reason}")
29
- else:
30
- super().__init__(f"'gb2312' codec can't decode bytes in position {position}-{position + len(obj) - 1}: {reason}")
31
- self.object = obj
29
+ self.obj = obj
32
30
  self.position = position
33
31
  self.reason = reason
34
32
 
33
+ def __str__(self) -> str:
34
+ if len(self.obj) <= 1:
35
+ return f"'gb2312' codec can't decode byte 0x{self.obj[0]:x} in position {self.position}: {self.reason}"
36
+ else:
37
+ return f"'gb2312' codec can't decode bytes in position {self.position}-{self.position + len(self.obj) - 1}: {self.reason}"
38
+
35
39
 
36
40
  def encode(cs: str) -> bytes:
37
41
  bs = bytearray()
@@ -72,9 +76,9 @@ def query_coord(c: str) -> tuple[int, int]:
72
76
  try:
73
77
  bs = encode(c)
74
78
  except GB2312EncodeError as e:
75
- raise GB2312Exception(f"'{c}' is not a 'gb2312' character") from e
79
+ raise GB2312Exception(f"'\\u{ord(c):x}' is not a 'gb2312' character") from e
76
80
  if len(bs) == 1:
77
- raise GB2312Exception(f"'{c}' is a ascii character")
81
+ raise GB2312Exception(f"'\\u{ord(c):x}' is a ascii character")
78
82
  row = bs[0] - _EUC_OFFSET
79
83
  col = bs[1] - _EUC_OFFSET
80
84
  return row, col
@@ -7,31 +7,35 @@ class KSX1001Exception(Exception):
7
7
 
8
8
 
9
9
  class KSX1001EncodeError(KSX1001Exception):
10
- object: str
10
+ obj: str
11
11
  position: int
12
12
  reason: str
13
13
 
14
14
  def __init__(self, obj: str, position: int, reason: str):
15
- super().__init__(f"'ksx1001' codec can't encode character '\\u{ord(obj):x}' in position {position}: {reason}")
16
- self.object = obj
15
+ self.obj = obj
17
16
  self.position = position
18
17
  self.reason = reason
19
18
 
19
+ def __str__(self) -> str:
20
+ return f"'ksx1001' codec can't encode character '\\u{ord(self.obj):x}' in position {self.position}: {self.reason}"
21
+
20
22
 
21
23
  class KSX1001DecodeError(KSX1001Exception):
22
- object: bytes
24
+ obj: bytes
23
25
  position: int
24
26
  reason: str
25
27
 
26
28
  def __init__(self, obj: bytes, position: int, reason: str):
27
- if len(obj) <= 1:
28
- super().__init__(f"'ksx1001' codec can't decode byte 0x{obj[0]:x} in position {position}: {reason}")
29
- else:
30
- super().__init__(f"'ksx1001' codec can't decode bytes in position {position}-{position + len(obj) - 1}: {reason}")
31
- self.object = obj
29
+ self.obj = obj
32
30
  self.position = position
33
31
  self.reason = reason
34
32
 
33
+ def __str__(self) -> str:
34
+ if len(self.obj) <= 1:
35
+ return f"'ksx1001' codec can't decode byte 0x{self.obj[0]:x} in position {self.position}: {self.reason}"
36
+ else:
37
+ return f"'ksx1001' codec can't decode bytes in position {self.position}-{self.position + len(self.obj) - 1}: {self.reason}"
38
+
35
39
 
36
40
  def encode(cs: str) -> bytes:
37
41
  bs = bytearray()
@@ -86,11 +90,11 @@ def query_coord(c: str) -> tuple[int, int]:
86
90
  try:
87
91
  bs = encode(c)
88
92
  except KSX1001EncodeError as e:
89
- raise KSX1001Exception(f"'{c}' is not a 'ksx1001' character") from e
93
+ raise KSX1001Exception(f"'\\u{ord(c):x}' is not a 'ksx1001' character") from e
90
94
  if len(bs) == 1:
91
- raise KSX1001Exception(f"'{c}' is a ascii character")
95
+ raise KSX1001Exception(f"'\\u{ord(c):x}' is a ascii character")
92
96
  elif bs.startswith(b'\xa4\xd4') and len(bs) > 2:
93
- raise KSX1001Exception(f"'{c}' is not a 'ksx1001' character")
97
+ raise KSX1001Exception(f"'\\u{ord(c):x}' is not a 'ksx1001' character")
94
98
  row = bs[0] - _EUC_OFFSET
95
99
  col = bs[1] - _EUC_OFFSET
96
100
  return row, col
@@ -6,31 +6,35 @@ class ShiftJISException(Exception):
6
6
 
7
7
 
8
8
  class ShiftJISEncodeError(ShiftJISException):
9
- object: str
9
+ obj: str
10
10
  position: int
11
11
  reason: str
12
12
 
13
13
  def __init__(self, obj: str, position: int, reason: str):
14
- super().__init__(f"'shift-jis' codec can't encode character '\\u{ord(obj):x}' in position {position}: {reason}")
15
- self.object = obj
14
+ self.obj = obj
16
15
  self.position = position
17
16
  self.reason = reason
18
17
 
18
+ def __str__(self) -> str:
19
+ return f"'shift-jis' codec can't encode character '\\u{ord(self.obj):x}' in position {self.position}: {self.reason}"
20
+
19
21
 
20
22
  class ShiftJISDecodeError(ShiftJISException):
21
- object: bytes
23
+ obj: bytes
22
24
  position: int
23
25
  reason: str
24
26
 
25
27
  def __init__(self, obj: bytes, position: int, reason: str):
26
- if len(obj) <= 1:
27
- super().__init__(f"'shift-jis' codec can't decode byte 0x{obj[0]:x} in position {position}: {reason}")
28
- else:
29
- super().__init__(f"'shift-jis' codec can't decode bytes in position {position}-{position + len(obj) - 1}: {reason}")
30
- self.object = obj
28
+ self.obj = obj
31
29
  self.position = position
32
30
  self.reason = reason
33
31
 
32
+ def __str__(self) -> str:
33
+ if len(self.obj) <= 1:
34
+ return f"'shift-jis' codec can't decode byte 0x{self.obj[0]:x} in position {self.position}: {self.reason}"
35
+ else:
36
+ return f"'shift-jis' codec can't decode bytes in position {self.position}-{self.position + len(self.obj) - 1}: {self.reason}"
37
+
34
38
 
35
39
  def encode(cs: str) -> bytes:
36
40
  bs = bytearray()
@@ -1,29 +0,0 @@
1
- name: Publish
2
-
3
- on:
4
- release:
5
- types: [published]
6
-
7
- jobs:
8
- publish:
9
- runs-on: ubuntu-latest
10
- environment:
11
- name: pypi
12
- url: https://pypi.org/project/character-encoding-utils/
13
- permissions:
14
- id-token: write
15
- steps:
16
- - name: Checkout
17
- uses: actions/checkout@v4
18
- - name: Setup Python
19
- uses: actions/setup-python@v5
20
- with:
21
- python-version: '3.x'
22
- - name: Install dependencies
23
- run: |
24
- python -m pip install pip --upgrade
25
- python -m pip install build
26
- - name: Build
27
- run: python -m build
28
- - name: Publish
29
- uses: pypa/gh-action-pypi-publish@release/v1
@@ -1,24 +0,0 @@
1
- name: Test
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- test:
7
- runs-on: ubuntu-latest
8
- strategy:
9
- fail-fast: false
10
- matrix:
11
- python-version: ["3.10", "3.11", "3.12"]
12
- steps:
13
- - name: Checkout
14
- uses: actions/checkout@v4
15
- - name: Setup Python
16
- uses: actions/setup-python@v5
17
- with:
18
- python-version: ${{ matrix.python-version }}
19
- - name: Install dependencies
20
- run: |
21
- python -m pip install pip --upgrade
22
- python -m pip install -r requirements.txt
23
- - name: Test
24
- run: python -m pytest
@@ -1,141 +0,0 @@
1
- # Byte-compiled / optimized / DLL files
2
- __pycache__/
3
- *.py[cod]
4
- *$py.class
5
-
6
- # C extensions
7
- *.so
8
-
9
- # Distribution / packaging
10
- .Python
11
- build/
12
- develop-eggs/
13
- dist/
14
- downloads/
15
- eggs/
16
- .eggs/
17
- lib/
18
- lib64/
19
- parts/
20
- sdist/
21
- var/
22
- wheels/
23
- share/python-wheels/
24
- *.egg-info/
25
- .installed.cfg
26
- *.egg
27
- MANIFEST
28
-
29
- # PyInstaller
30
- # Usually these files are written by a python script from a template
31
- # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
- *.manifest
33
- *.spec
34
-
35
- # Installer logs
36
- pip-log.txt
37
- pip-delete-this-directory.txt
38
-
39
- # Unit test / coverage reports
40
- htmlcov/
41
- .tox/
42
- .nox/
43
- .coverage
44
- .coverage.*
45
- .cache
46
- nosetests.xml
47
- coverage.xml
48
- *.cover
49
- *.py,cover
50
- .hypothesis/
51
- .pytest_cache/
52
- cover/
53
-
54
- # Translations
55
- *.mo
56
- *.pot
57
-
58
- # Django stuff:
59
- *.log
60
- local_settings.py
61
- db.sqlite3
62
- db.sqlite3-journal
63
-
64
- # Flask stuff:
65
- instance/
66
- .webassets-cache
67
-
68
- # Scrapy stuff:
69
- .scrapy
70
-
71
- # Sphinx documentation
72
- docs/_build/
73
-
74
- # PyBuilder
75
- .pybuilder/
76
- target/
77
-
78
- # Jupyter Notebook
79
- .ipynb_checkpoints
80
-
81
- # IPython
82
- profile_default/
83
- ipython_config.py
84
-
85
- # pyenv
86
- # For a library or package, you might want to ignore these files since the code is
87
- # intended to run in multiple environments; otherwise, check them in:
88
- # .python-version
89
-
90
- # pipenv
91
- # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
- # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
- # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
- # install all needed dependencies.
95
- #Pipfile.lock
96
-
97
- # PEP 582; used by e.g. github.com/David-OConnor/pyflow
98
- __pypackages__/
99
-
100
- # Celery stuff
101
- celerybeat-schedule
102
- celerybeat.pid
103
-
104
- # SageMath parsed files
105
- *.sage.py
106
-
107
- # Environments
108
- .env
109
- .venv
110
- env/
111
- venv/
112
- ENV/
113
- env.bak/
114
- venv.bak/
115
-
116
- # Spyder project settings
117
- .spyderproject
118
- .spyproject
119
-
120
- # Rope project settings
121
- .ropeproject
122
-
123
- # mkdocs documentation
124
- /site
125
-
126
- # mypy
127
- .mypy_cache/
128
- .dmypy.json
129
- dmypy.json
130
-
131
- # Pyre type checker
132
- .pyre/
133
-
134
- # pytype static type analyzer
135
- .pytype/
136
-
137
- # Cython debug symbols
138
- cython_debug/
139
-
140
- # PyCharm
141
- .idea/
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 TakWolf
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,34 +0,0 @@
1
- [build-system]
2
- requires = ["hatchling"]
3
- build-backend = "hatchling.build"
4
-
5
- [project]
6
- name = "character-encoding-utils"
7
- version = "0.0.8"
8
- description = "Some character encoding utils."
9
- readme = "README.md"
10
- license = { text = "MIT License" }
11
- requires-python = ">=3.10"
12
- authors = [
13
- { name = "TakWolf" },
14
- ]
15
- maintainers = [
16
- { name = "TakWolf" },
17
- ]
18
- keywords = ["encoding", "gb2312", "big5", "shift-jis", "ksx1001"]
19
- classifiers = [
20
- "Programming Language :: Python :: 3",
21
- "License :: OSI Approved :: MIT License",
22
- "Operating System :: OS Independent",
23
- ]
24
-
25
- [project.urls]
26
- homepage = "https://github.com/TakWolf/character-encoding-utils"
27
- source = "https://github.com/TakWolf/character-encoding-utils"
28
- issues = "https://github.com/TakWolf/character-encoding-utils/issues"
29
-
30
- [tool.pytest.ini_options]
31
- pythonpath = "src"
32
- addopts = [
33
- "--import-mode=importlib",
34
- ]
@@ -1 +0,0 @@
1
- pytest==8.2.0
@@ -1,173 +0,0 @@
1
- import pytest
2
-
3
- from character_encoding_utils import big5
4
- from character_encoding_utils.big5 import Big5Exception, Big5EncodeError, Big5DecodeError
5
-
6
-
7
- def test_codec():
8
- assert big5.encode('abc中國') == 'abc中國'.encode('big5')
9
- assert big5.decode(b'abc\xa4\xa4\xb0\xea') == b'abc\xa4\xa4\xb0\xea'.decode('big5')
10
-
11
- with pytest.raises(Big5EncodeError) as info:
12
- big5.encode('abc가')
13
- assert info.value.object == '가'
14
- assert info.value.position == 3
15
- assert info.value.reason == 'illegal multibyte sequence'
16
-
17
- with pytest.raises(Big5DecodeError) as info:
18
- big5.decode(b'abc\xa4\xa4\xb0')
19
- assert info.value.object == b'\xb0'
20
- assert info.value.position == 5
21
- assert info.value.reason == 'incomplete multibyte sequence'
22
-
23
- c = '〸'
24
- assert ord(c) == 0x3038
25
- assert big5.decode(big5.encode(c)) == c
26
-
27
- c = '〹'
28
- assert ord(c) == 0x3039
29
- assert big5.decode(big5.encode(c)) == c
30
-
31
- c = '〺'
32
- assert ord(c) == 0x303A
33
- assert big5.decode(big5.encode(c)) == c
34
-
35
- c = '十'
36
- assert ord(c) == 0x5341
37
- assert big5.decode(big5.encode(c)) == c
38
-
39
- c = '卄'
40
- assert ord(c) == 0x5344
41
- with pytest.raises(Big5EncodeError):
42
- big5.encode(c)
43
-
44
- c = '卅'
45
- assert ord(c) == 0x5345
46
- assert big5.decode(big5.encode(c)) == c
47
-
48
- c = '/'
49
- assert ord(c) == 0xFF0F
50
- assert big5.decode(big5.encode(c)) == c
51
-
52
- c = '\'
53
- assert ord(c) == 0xFF3C
54
- assert big5.decode(big5.encode(c)) == c
55
-
56
- c = '∕'
57
- assert ord(c) == 0x2215
58
- assert big5.decode(big5.encode(c)) == c
59
-
60
- c = '﹨'
61
- assert ord(c) == 0xFE68
62
- assert big5.decode(big5.encode(c)) == c
63
-
64
-
65
- def test_query_code():
66
- assert big5.query_code(' ') == 0xA140
67
- assert big5.query_code('¢') == 0xA246
68
- assert big5.query_code('一') == 0xA440
69
- assert big5.query_code('訐') == 0xB050
70
- assert big5.query_code('乂') == 0xC940
71
- assert big5.query_code('綃') == 0xDF60
72
-
73
- with pytest.raises(Big5Exception):
74
- big5.query_code('abc')
75
- with pytest.raises(Big5Exception):
76
- big5.query_code('d')
77
-
78
- with pytest.raises(Big5Exception) as info:
79
- big5.query_code('가')
80
- assert isinstance(info.value.__cause__, Big5EncodeError)
81
-
82
-
83
- def test_query_chr():
84
- assert big5.query_chr(0xA140) == ' '
85
- assert big5.query_chr(0xA246) == '¢'
86
- assert big5.query_chr(0xA440) == '一'
87
- assert big5.query_chr(0xB050) == '訐'
88
- assert big5.query_chr(0xC940) == '乂'
89
- assert big5.query_chr(0xDF60) == '綃'
90
-
91
- with pytest.raises(Big5Exception) as info:
92
- big5.query_chr(0xA000)
93
- assert isinstance(info.value.__cause__, Big5DecodeError)
94
- with pytest.raises(Big5Exception) as info:
95
- big5.query_chr(0xFFFF)
96
- assert isinstance(info.value.__cause__, Big5DecodeError)
97
-
98
-
99
- def test_query_category():
100
- categories = big5.get_categories()
101
- assert len(categories) == 3
102
- assert 'other' in categories
103
- assert 'level-1' in categories
104
- assert 'level-2' in categories
105
-
106
- assert big5.query_category('■') == 'other'
107
- assert big5.query_category('一') == 'level-1'
108
- assert big5.query_category('乂') == 'level-2'
109
- assert big5.query_category('A') is None
110
- assert big5.query_category('가') is None
111
-
112
-
113
- def test_alphabet():
114
- alphabet = big5.get_alphabet_other()
115
- assert len(alphabet) == 408
116
- for c in alphabet:
117
- code = big5.query_code(c)
118
- assert big5.query_chr(code) == c
119
- assert big5.query_category(c) == 'other'
120
-
121
- alphabet = big5.get_alphabet_level_1()
122
- assert len(alphabet) == 5401
123
- for c in alphabet:
124
- code = big5.query_code(c)
125
- assert big5.query_chr(code) == c
126
- assert big5.query_category(c) == 'level-1'
127
-
128
- alphabet = big5.get_alphabet_level_2()
129
- assert len(alphabet) == 7652
130
- for c in alphabet:
131
- code = big5.query_code(c)
132
- assert big5.query_chr(code) == c
133
- assert big5.query_category(c) == 'level-2'
134
-
135
- alphabet = big5.get_alphabet()
136
- assert len(alphabet) == 13461
137
- for c in alphabet:
138
- code = big5.query_code(c)
139
- assert big5.query_chr(code) == c
140
- assert big5.query_category(c) is not None
141
-
142
-
143
- def test_count():
144
- assert big5.get_other_count() == 408
145
- assert big5.get_level_1_count() == 5401
146
- assert big5.get_level_2_count() == 7652
147
- assert big5.get_count() == 13461
148
-
149
-
150
- def test_unicode():
151
- alphabet_other = []
152
- alphabet_level_1 = []
153
- alphabet_level_2 = []
154
- alphabet = []
155
-
156
- for code_point in range(0, 0x10FFFF + 1):
157
- c = chr(code_point)
158
- category = big5.query_category(c)
159
-
160
- if category == 'other':
161
- alphabet_other.append(c)
162
- elif category == 'level-1':
163
- alphabet_level_1.append(c)
164
- elif category == 'level-2':
165
- alphabet_level_2.append(c)
166
-
167
- if category is not None:
168
- alphabet.append(c)
169
-
170
- assert len(alphabet_other) == big5.get_other_count()
171
- assert len(alphabet_level_1) == big5.get_level_1_count()
172
- assert len(alphabet_level_2) == big5.get_level_2_count()
173
- assert len(alphabet) == big5.get_count()
@@ -1,131 +0,0 @@
1
- import pytest
2
-
3
- from character_encoding_utils import gb2312
4
- from character_encoding_utils.gb2312 import GB2312Exception, GB2312EncodeError, GB2312DecodeError
5
-
6
-
7
- def test_codec():
8
- assert gb2312.encode('abc中国') == 'abc中国'.encode('gb2312')
9
- assert gb2312.decode(b'abc\xd6\xd0\xb9\xfa') == b'abc\xd6\xd0\xb9\xfa'.decode('gb2312')
10
-
11
- with pytest.raises(GB2312EncodeError) as info:
12
- gb2312.encode('abc가')
13
- assert info.value.object == '가'
14
- assert info.value.position == 3
15
- assert info.value.reason == 'illegal multibyte sequence'
16
-
17
- with pytest.raises(GB2312DecodeError) as info:
18
- gb2312.decode(b'abc\xd6\xd0\xb9')
19
- assert info.value.object == b'\xb9'
20
- assert info.value.position == 5
21
- assert info.value.reason == 'incomplete multibyte sequence'
22
-
23
-
24
- def test_query_coord():
25
- assert gb2312.query_coord('$') == (1, 71)
26
- assert gb2312.query_coord('拿') == (36, 35)
27
- assert gb2312.query_coord('贽') == (74, 62)
28
- assert gb2312.query_coord('魍') == (87, 45)
29
-
30
- with pytest.raises(GB2312Exception):
31
- gb2312.query_coord('abc')
32
- with pytest.raises(GB2312Exception):
33
- gb2312.query_coord('d')
34
-
35
- with pytest.raises(GB2312Exception) as info:
36
- gb2312.query_coord('가')
37
- assert isinstance(info.value.__cause__, GB2312EncodeError)
38
-
39
-
40
- def test_query_chr():
41
- assert gb2312.query_chr(1, 79) == '★'
42
- assert gb2312.query_chr(16, 1) == '啊'
43
- assert gb2312.query_chr(26, 26) == '汉'
44
- assert gb2312.query_chr(55, 54) == '字'
45
- assert gb2312.query_chr(87, 94) == '齄'
46
-
47
- with pytest.raises(GB2312Exception):
48
- gb2312.query_chr(-1, 50)
49
- with pytest.raises(GB2312Exception):
50
- gb2312.query_chr(20, 500)
51
-
52
- with pytest.raises(GB2312Exception) as info:
53
- gb2312.query_chr(94, 94)
54
- assert isinstance(info.value.__cause__, GB2312DecodeError)
55
-
56
-
57
- def test_query_category():
58
- categories = gb2312.get_categories()
59
- assert len(categories) == 3
60
- assert 'other' in categories
61
- assert 'level-1' in categories
62
- assert 'level-2' in categories
63
-
64
- assert gb2312.query_category('■') == 'other'
65
- assert gb2312.query_category('闭') == 'level-1'
66
- assert gb2312.query_category('踔') == 'level-2'
67
- assert gb2312.query_category('A') is None
68
- assert gb2312.query_category('가') is None
69
-
70
-
71
- def test_alphabet():
72
- alphabet = gb2312.get_alphabet_other()
73
- assert len(alphabet) == 682
74
- for c in alphabet:
75
- row, col = gb2312.query_coord(c)
76
- assert gb2312.query_chr(row, col) == c
77
- assert gb2312.query_category(c) == 'other'
78
-
79
- alphabet = gb2312.get_alphabet_level_1()
80
- assert len(alphabet) == 3755
81
- for c in alphabet:
82
- row, col = gb2312.query_coord(c)
83
- assert gb2312.query_chr(row, col) == c
84
- assert gb2312.query_category(c) == 'level-1'
85
-
86
- alphabet = gb2312.get_alphabet_level_2()
87
- assert len(alphabet) == 3008
88
- for c in alphabet:
89
- row, col = gb2312.query_coord(c)
90
- assert gb2312.query_chr(row, col) == c
91
- assert gb2312.query_category(c) == 'level-2'
92
-
93
- alphabet = gb2312.get_alphabet()
94
- assert len(alphabet) == 7445
95
- for c in alphabet:
96
- row, col = gb2312.query_coord(c)
97
- assert gb2312.query_chr(row, col) == c
98
- assert gb2312.query_category(c) is not None
99
-
100
-
101
- def test_count():
102
- assert gb2312.get_other_count() == 682
103
- assert gb2312.get_level_1_count() == 3755
104
- assert gb2312.get_level_2_count() == 3008
105
- assert gb2312.get_count() == 7445
106
-
107
-
108
- def test_unicode():
109
- alphabet_other = []
110
- alphabet_level_1 = []
111
- alphabet_level_2 = []
112
- alphabet = []
113
-
114
- for code_point in range(0, 0x10FFFF + 1):
115
- c = chr(code_point)
116
- category = gb2312.query_category(c)
117
-
118
- if category == 'other':
119
- alphabet_other.append(c)
120
- elif category == 'level-1':
121
- alphabet_level_1.append(c)
122
- elif category == 'level-2':
123
- alphabet_level_2.append(c)
124
-
125
- if category is not None:
126
- alphabet.append(c)
127
-
128
- assert len(alphabet_other) == gb2312.get_other_count()
129
- assert len(alphabet_level_1) == gb2312.get_level_1_count()
130
- assert len(alphabet_level_2) == gb2312.get_level_2_count()
131
- assert len(alphabet) == gb2312.get_count()
@@ -1,139 +0,0 @@
1
- import pytest
2
-
3
- from character_encoding_utils import ksx1001
4
- from character_encoding_utils.ksx1001 import KSX1001Exception, KSX1001EncodeError, KSX1001DecodeError
5
-
6
-
7
- def test_codec():
8
- assert ksx1001.encode('abc가쳰') == 'abc가쳰'.encode('ksx1001')
9
- assert ksx1001.decode(b'abc\xb0\xa1\xc3\xc8') == b'abc\xb0\xa1\xc3\xc8'.decode('ksx1001')
10
-
11
- with pytest.raises(KSX1001EncodeError) as info:
12
- ksx1001.encode('abc😈')
13
- assert info.value.object == '😈'
14
- assert info.value.position == 3
15
- assert info.value.reason == 'illegal multibyte sequence'
16
-
17
- with pytest.raises(KSX1001DecodeError) as info:
18
- ksx1001.decode(b'abc\xb0\xa1\xc3')
19
- assert info.value.object == b'\xc3'
20
- assert info.value.position == 5
21
- assert info.value.reason == 'incomplete multibyte sequence'
22
-
23
- assert ksx1001.decode(ksx1001.encode(chr(0x3164))) == chr(0x3164)
24
-
25
- for code_point in range(0xAC00, 0xD7A4):
26
- c = chr(code_point)
27
- assert ksx1001.decode(ksx1001.encode(c)) == c
28
-
29
-
30
- def test_query_coord():
31
- assert ksx1001.query_coord('ㆌ') == (4, 92)
32
- assert ksx1001.query_coord('φ') == (5, 85)
33
- assert ksx1001.query_coord('떰') == (22, 22)
34
- assert ksx1001.query_coord('틘') == (38, 24)
35
- assert ksx1001.query_coord('紺') == (42, 90)
36
- assert ksx1001.query_coord('絿') == (47, 29)
37
-
38
- with pytest.raises(KSX1001Exception):
39
- ksx1001.query_coord('abc')
40
- with pytest.raises(KSX1001Exception):
41
- ksx1001.query_coord('d')
42
-
43
- with pytest.raises(KSX1001Exception) as info:
44
- ksx1001.query_coord('😈')
45
- assert isinstance(info.value.__cause__, KSX1001EncodeError)
46
-
47
-
48
- def test_query_chr():
49
- assert ksx1001.query_chr(1, 50) == '⌒'
50
- assert ksx1001.query_chr(16, 1) == '가'
51
- assert ksx1001.query_chr(35, 40) == '쳰'
52
- assert ksx1001.query_chr(50, 54) == '壟'
53
- assert ksx1001.query_chr(93, 94) == '詰'
54
-
55
- with pytest.raises(KSX1001Exception):
56
- ksx1001.query_chr(-1, 50)
57
- with pytest.raises(KSX1001Exception):
58
- ksx1001.query_chr(20, 500)
59
-
60
- with pytest.raises(KSX1001Exception) as info:
61
- ksx1001.query_chr(94, 94)
62
- assert isinstance(info.value.__cause__, KSX1001DecodeError)
63
-
64
-
65
- def test_query_category():
66
- categories = ksx1001.get_categories()
67
- assert len(categories) == 3
68
- assert 'other' in categories
69
- assert 'syllable' in categories
70
- assert 'hanja' in categories
71
-
72
- assert ksx1001.query_category('ぜ') == 'other'
73
- assert ksx1001.query_category('룝') == 'syllable'
74
- assert ksx1001.query_category('絿') == 'hanja'
75
- assert ksx1001.query_category('A') is None
76
- assert ksx1001.query_category('😈') is None
77
-
78
-
79
- def test_alphabet():
80
- alphabet = ksx1001.get_alphabet_other()
81
- assert len(alphabet) == 988
82
- for c in alphabet:
83
- row, col = ksx1001.query_coord(c)
84
- assert ksx1001.query_chr(row, col) == c
85
- assert ksx1001.query_category(c) == 'other'
86
-
87
- alphabet = ksx1001.get_alphabet_syllable()
88
- assert len(alphabet) == 2350
89
- for c in alphabet:
90
- row, col = ksx1001.query_coord(c)
91
- assert ksx1001.query_chr(row, col) == c
92
- assert ksx1001.query_category(c) == 'syllable'
93
-
94
- alphabet = ksx1001.get_alphabet_hanja()
95
- assert len(alphabet) == 4888
96
- for c in alphabet:
97
- row, col = ksx1001.query_coord(c)
98
- assert ksx1001.query_chr(row, col) == c
99
- assert ksx1001.query_category(c) == 'hanja'
100
-
101
- alphabet = ksx1001.get_alphabet()
102
- assert len(alphabet) == 8226
103
- for c in alphabet:
104
- row, col = ksx1001.query_coord(c)
105
- assert ksx1001.query_chr(row, col) == c
106
- assert ksx1001.query_category(c) is not None
107
-
108
-
109
- def test_count():
110
- assert ksx1001.get_other_count() == 988
111
- assert ksx1001.get_syllable_count() == 2350
112
- assert ksx1001.get_hanja_count() == 4888
113
- assert ksx1001.get_count() == 8226
114
-
115
-
116
- def test_unicode():
117
- alphabet_other = []
118
- alphabet_syllable = []
119
- alphabet_hanja = []
120
- alphabet = []
121
-
122
- for code_point in range(0, 0x10FFFF + 1):
123
- c = chr(code_point)
124
- category = ksx1001.query_category(c)
125
-
126
- if category == 'other':
127
- alphabet_other.append(c)
128
- elif category == 'syllable':
129
- alphabet_syllable.append(c)
130
- elif category == 'hanja':
131
- alphabet_hanja.append(c)
132
-
133
- if category is not None:
134
- alphabet.append(c)
135
-
136
- assert len(alphabet_other) == ksx1001.get_other_count()
137
- assert len(alphabet_syllable) == ksx1001.get_syllable_count()
138
- assert len(alphabet_hanja) == ksx1001.get_hanja_count()
139
- assert len(alphabet) == ksx1001.get_count()
@@ -1,130 +0,0 @@
1
- import pytest
2
-
3
- from character_encoding_utils import shiftjis
4
- from character_encoding_utils.shiftjis import ShiftJISEncodeError, ShiftJISDecodeError
5
-
6
-
7
- def test_codec():
8
- assert shiftjis.encode('abc日本') == 'abc日本'.encode('shift-jis')
9
- assert shiftjis.decode(b'abc\x93\xfa\x96\x7b') == b'abc\x93\xfa\x96\x7b'.decode('shift-jis')
10
-
11
- with pytest.raises(ShiftJISEncodeError) as info:
12
- shiftjis.encode('abc가')
13
- assert info.value.object == '가'
14
- assert info.value.position == 3
15
- assert info.value.reason == 'illegal multibyte sequence'
16
-
17
- with pytest.raises(ShiftJISDecodeError) as info:
18
- shiftjis.decode(b'abc\x93\xfa\x96')
19
- assert info.value.object == b'\x96'
20
- assert info.value.position == 5
21
- assert info.value.reason == 'incomplete multibyte sequence'
22
-
23
- with pytest.raises(ShiftJISEncodeError) as info:
24
- shiftjis.encode('\\')
25
- assert info.value.object == '\\'
26
- assert info.value.position == 0
27
- assert '\\' in info.value.reason
28
- assert '¥' in info.value.reason
29
-
30
- with pytest.raises(ShiftJISEncodeError) as info:
31
- shiftjis.encode('~')
32
- assert info.value.object == '~'
33
- assert info.value.position == 0
34
- assert '~' in info.value.reason
35
- assert '‾' in info.value.reason
36
-
37
- assert shiftjis.decode(shiftjis.encode('¥')) == '¥'
38
- assert shiftjis.decode(shiftjis.encode('‾')) == '‾'
39
-
40
-
41
- def test_query_category():
42
- categories = shiftjis.get_categories()
43
- assert len(categories) == 5
44
- assert 'single-byte-ascii-control' in categories
45
- assert 'single-byte-ascii-printable' in categories
46
- assert 'single-byte-half-width-katakana' in categories
47
- assert 'double-byte-other' in categories
48
- assert 'double-byte-kanji' in categories
49
-
50
- assert shiftjis.query_category('\n') == 'single-byte-ascii-control'
51
- assert shiftjis.query_category('A') == 'single-byte-ascii-printable'
52
- assert shiftjis.query_category('ア') == 'single-byte-half-width-katakana'
53
- assert shiftjis.query_category('あ') == 'double-byte-other'
54
- assert shiftjis.query_category('辻') == 'double-byte-kanji'
55
- assert shiftjis.query_category('가') is None
56
-
57
-
58
- def test_alphabet():
59
- alphabet = shiftjis.get_alphabet_single_byte_ascii_control()
60
- assert len(alphabet) == 33
61
- for c in alphabet:
62
- assert shiftjis.query_category(c) == 'single-byte-ascii-control'
63
-
64
- alphabet = shiftjis.get_alphabet_single_byte_ascii_printable()
65
- assert len(alphabet) == 95
66
- for c in alphabet:
67
- assert shiftjis.query_category(c) == 'single-byte-ascii-printable'
68
-
69
- alphabet = shiftjis.get_alphabet_single_byte_half_width_katakana()
70
- assert len(alphabet) == 63
71
- for c in alphabet:
72
- assert shiftjis.query_category(c) == 'single-byte-half-width-katakana'
73
-
74
- alphabet = shiftjis.get_alphabet_double_byte_other()
75
- assert len(alphabet) == 524
76
- for c in alphabet:
77
- assert shiftjis.query_category(c) == 'double-byte-other'
78
-
79
- alphabet = shiftjis.get_alphabet_double_byte_kanji()
80
- assert len(alphabet) == 6355
81
- for c in alphabet:
82
- assert shiftjis.query_category(c) == 'double-byte-kanji'
83
-
84
- alphabet = shiftjis.get_alphabet()
85
- assert len(alphabet) == 7070
86
- for c in alphabet:
87
- assert shiftjis.query_category(c) is not None
88
-
89
-
90
- def test_count():
91
- assert shiftjis.get_single_byte_ascii_control_count() == 33
92
- assert shiftjis.get_single_byte_ascii_printable_count() == 95
93
- assert shiftjis.get_single_byte_half_width_katakana_count() == 63
94
- assert shiftjis.get_double_byte_other_count() == 524
95
- assert shiftjis.get_double_byte_kanji_count() == 6355
96
- assert shiftjis.get_count() == 7070
97
-
98
-
99
- def test_unicode():
100
- alphabet_single_byte_ascii_control = []
101
- alphabet_single_byte_ascii_printable = []
102
- alphabet_single_byte_half_width_katakana = []
103
- alphabet_double_byte_other = []
104
- alphabet_double_byte_kanji = []
105
- alphabet = []
106
-
107
- for code_point in range(0, 0x10FFFF + 1):
108
- c = chr(code_point)
109
- category = shiftjis.query_category(c)
110
-
111
- if category == 'single-byte-ascii-control':
112
- alphabet_single_byte_ascii_control.append(c)
113
- elif category == 'single-byte-ascii-printable':
114
- alphabet_single_byte_ascii_printable.append(c)
115
- elif category == 'single-byte-half-width-katakana':
116
- alphabet_single_byte_half_width_katakana.append(c)
117
- elif category == 'double-byte-other':
118
- alphabet_double_byte_other.append(c)
119
- elif category == 'double-byte-kanji':
120
- alphabet_double_byte_kanji.append(c)
121
-
122
- if category is not None:
123
- alphabet.append(c)
124
-
125
- assert len(alphabet_single_byte_ascii_control) == shiftjis.get_single_byte_ascii_control_count()
126
- assert len(alphabet_single_byte_ascii_printable) == shiftjis.get_single_byte_ascii_printable_count()
127
- assert len(alphabet_single_byte_half_width_katakana) == shiftjis.get_single_byte_half_width_katakana_count()
128
- assert len(alphabet_double_byte_other) == shiftjis.get_double_byte_other_count()
129
- assert len(alphabet_double_byte_kanji) == shiftjis.get_double_byte_kanji_count()
130
- assert len(alphabet) == shiftjis.get_count()