cube-python 0.4.0__cp312-cp312-macosx_10_15_x86_64.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.
- cube_python-0.4.0.dist-info/METADATA +88 -0
- cube_python-0.4.0.dist-info/RECORD +9 -0
- cube_python-0.4.0.dist-info/WHEEL +5 -0
- cube_python-0.4.0.dist-info/licenses/LICENSE +20 -0
- cube_python-0.4.0.dist-info/top_level.txt +1 -0
- pycube/__init__.py +51 -0
- pycube/_exceptions.py +20 -0
- pycube/_ffi.py +53 -0
- pycube/_native.cpython-312-darwin.so +0 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cube-python
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Python binding for Cube C++ library
|
|
5
|
+
Author-email: coshz <fsinhx@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Documentation, https://github.com/coshz/cube/tree/master/bindings/python#readme
|
|
8
|
+
Project-URL: Source, https://github.com/coshz/cube
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/coshz/cube/issues
|
|
10
|
+
Keywords: game,rubiks-cube,bindings
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: C++
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# cube-python
|
|
21
|
+
|
|
22
|
+
[](https://pypi.org/project/cube-python/)
|
|
23
|
+
[](https://github.com/yourusername/cube/blob/main/LICENSE)
|
|
24
|
+
[](https://github.com/coshz/cube/tree/master/bindings/python)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
High-performance Rubik's Cube solver Python bindings powered by C/C++ FFI.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install cube-python
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quickstart
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
iimport pycube
|
|
41
|
+
from pycube import SolveError
|
|
42
|
+
|
|
43
|
+
# Default solved cube string representation
|
|
44
|
+
print(f"Target State: {pycube.CUBE_ID}")
|
|
45
|
+
|
|
46
|
+
# Validate state solvability
|
|
47
|
+
scrambled = pycube.facecube("U F U' L2 R L' D2 B")
|
|
48
|
+
|
|
49
|
+
if pycube.solvable(scrambled):
|
|
50
|
+
try:
|
|
51
|
+
# Find solution sequence
|
|
52
|
+
solution = pycube.solve(scrambled, step=30, best=True)
|
|
53
|
+
print(f"Solution: {solution}")
|
|
54
|
+
|
|
55
|
+
# Apply solution to verify end state
|
|
56
|
+
end_state = pycube.facecube(solution, scrambled)
|
|
57
|
+
assert end_state == pycube.CUBE_ID
|
|
58
|
+
|
|
59
|
+
except SolveError as e:
|
|
60
|
+
print(f"Failed to solve cube: {e}")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## 📖 API Reference
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import pycube
|
|
67
|
+
from pycube import SolveError, CUBE_ID
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Functions
|
|
71
|
+
|
|
72
|
+
| Function | Description |
|
|
73
|
+
| :--- | :--- |
|
|
74
|
+
| `solve(src, tgt=CUBE_ID, step=30, best=False)` | Solves the cube state string `src`. Returns solution string or raises `SolveError`. |
|
|
75
|
+
| `facecube(maneuver, cube=CUBE_ID)` | Applies move sequence (e.g., `"U F U' L2"`) to a cube state and returns the new state. |
|
|
76
|
+
| `solvable(cube)` | Returns `True` if the 54-character state string is physically solvable. |
|
|
77
|
+
| `permutation(ms, fmt=2)` | Returns a permutation representation for a move sequence or state. |
|
|
78
|
+
|
|
79
|
+
### Constants & Exceptions
|
|
80
|
+
|
|
81
|
+
| Name | Type | Description |
|
|
82
|
+
| :--- | :--- | :--- |
|
|
83
|
+
| `CUBE_ID` | `str` | Constant representing the standard solved state (`"UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB"`). |
|
|
84
|
+
| `SolveError` | `Exception` | Raised when the state is invalid, unsolvable, or exceeds search step limits. |
|
|
85
|
+
|
|
86
|
+
## 📄 License
|
|
87
|
+
|
|
88
|
+
[MIT](LICENSE) © coshz
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
cube_python-0.4.0.dist-info/licenses/LICENSE,sha256=5aug9F9MisxQd1Gd8T_2O8BQRvO-FdDVxmZxVkjA4AM,1049
|
|
2
|
+
pycube/__init__.py,sha256=SQNdI3oSZL3MRqesA71OX8V9u16B8XTWEgSSWAmVIDY,1665
|
|
3
|
+
pycube/_exceptions.py,sha256=k_3G4Abh6Q8mWfctG19d0M3eWcHZ9JMxB6hfTHq6awo,430
|
|
4
|
+
pycube/_ffi.py,sha256=TocmwoSwKBvSwmWg2pYzYnRe3bXhJr5JdlVAh8v7_AA,1484
|
|
5
|
+
pycube/_native.cpython-312-darwin.so,sha256=LF7RfZNqYcEAImkLaaCAIbKlHCudUM-IvRDBMQUl45g,267224
|
|
6
|
+
cube_python-0.4.0.dist-info/METADATA,sha256=VfBnTEfBRA-M1T4N4xaHUdgMEit1V4uiOH0UrjQIUvw,2847
|
|
7
|
+
cube_python-0.4.0.dist-info/WHEEL,sha256=2rZ8eqxxuiDxbYnSQc_T1EfLxym8FKbM652VP35vVF4,111
|
|
8
|
+
cube_python-0.4.0.dist-info/top_level.txt,sha256=ioKlXjCw0-J53WSFSUPfhI-3sU1IbOVH2rR8nFl6FMc,7
|
|
9
|
+
cube_python-0.4.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2026 coshz
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included
|
|
12
|
+
in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pycube
|
pycube/__init__.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from ._ffi import _libcube as lib
|
|
2
|
+
from ._exceptions import SolveResult, SolveError
|
|
3
|
+
import ctypes
|
|
4
|
+
|
|
5
|
+
__all__ = [ 'solve', 'facecube', 'permutation', 'solvable', 'CUBE_ID' ]
|
|
6
|
+
|
|
7
|
+
CUBE_ID = lib.CUBE_ID
|
|
8
|
+
|
|
9
|
+
def solve(src: str, tgt: str = CUBE_ID, step: int = 30, best: bool = False) -> str:
|
|
10
|
+
""" Solve the cube.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
src : the source color configuration;
|
|
14
|
+
tgt : the target color configuration; (None => cid)
|
|
15
|
+
step : the maximum allowed steps;
|
|
16
|
+
best : find the shorter (but slower) solution
|
|
17
|
+
|
|
18
|
+
Return:
|
|
19
|
+
the sequence of moves
|
|
20
|
+
|
|
21
|
+
Raise:
|
|
22
|
+
SolveError(code) if the src/tgt cube configuration is invalid or
|
|
23
|
+
the cube is unsolvable or no solution found within the step limit.
|
|
24
|
+
"""
|
|
25
|
+
src_bytes = src.encode('utf-8')
|
|
26
|
+
tgt_bytes = tgt.encode('utf-8')
|
|
27
|
+
buffer = ctypes.create_string_buffer(lib.CUBE_BS)
|
|
28
|
+
code = lib.solve(buffer,src_bytes,tgt_bytes,step,best)
|
|
29
|
+
if code != SolveResult.SUCCESS:
|
|
30
|
+
raise SolveError(code)
|
|
31
|
+
return buffer.value.decode('utf-8')
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def facecube(manuuver: str, cube: str = CUBE_ID) -> str:
|
|
35
|
+
maneuver_bytes = manuuver.encode('utf-8')
|
|
36
|
+
cube_bytes = cube.encode('utf-8')
|
|
37
|
+
buffer = ctypes.create_string_buffer(lib.CUBE_BS)
|
|
38
|
+
lib.facecube(buffer,maneuver_bytes,cube_bytes)
|
|
39
|
+
return buffer.value.decode('utf-8')
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def permutation(ms_or_cube: str, fmt: int = 2) -> str:
|
|
43
|
+
mc = ms_or_cube.encode('utf-8')
|
|
44
|
+
buffer = ctypes.create_string_buffer(lib.CUBE_BS)
|
|
45
|
+
lib.permutation(buffer,mc,fmt)
|
|
46
|
+
return buffer.value.decode('utf-8')
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def solvable(cube: str) -> bool:
|
|
50
|
+
cube_bytes = cube.encode('utf-8')
|
|
51
|
+
return lib.solvable(cube_bytes)
|
pycube/_exceptions.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from enum import IntEnum
|
|
2
|
+
from ._ffi import _libcube as lib
|
|
3
|
+
|
|
4
|
+
__all__ = ['SolveResult', 'SolveError']
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SolveResult(IntEnum):
|
|
8
|
+
SUCCESS = 0
|
|
9
|
+
UNSOLVABLE = 1
|
|
10
|
+
NOT_FOUND = 2
|
|
11
|
+
INVALID_SRC = 3
|
|
12
|
+
INVALID_TGT = 4
|
|
13
|
+
UNKNOWN_ERR = 5
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class SolveError(Exception):
|
|
17
|
+
def __init__(self, sr: SolveResult):
|
|
18
|
+
self.value = sr
|
|
19
|
+
msg = lib.solve_result_to_string(sr)
|
|
20
|
+
super().__init__(f"[{sr.name}] {msg}")
|
pycube/_ffi.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Python ctypes wrapper and FFI definitions for the C++ shared library.
|
|
3
|
+
|
|
4
|
+
Ref:
|
|
5
|
+
../cxx_src/cube.h
|
|
6
|
+
|
|
7
|
+
See Also:
|
|
8
|
+
../cxx_src/cube_amalg.min.cpp
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from ctypes import CDLL, c_char_p, c_int32, c_bool
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
class CubeCDLL(CDLL):
|
|
15
|
+
CUBE_BS = 128
|
|
16
|
+
CUBE_ID = "UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _load_library() -> CubeCDLL:
|
|
20
|
+
package_dir = Path(__file__).parent
|
|
21
|
+
allowed_suffixes = { '.so', '.dylib', '.dll', '.pyd' }
|
|
22
|
+
candidates = [
|
|
23
|
+
p for p in package_dir.glob("_native*")
|
|
24
|
+
if p.suffix in allowed_suffixes
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
if not candidates:
|
|
28
|
+
raise FileNotFoundError(
|
|
29
|
+
f"Could not find compiled C++ shared library inside '{package_dir}'.\n"
|
|
30
|
+
"If you are developing locally, run `pip install -e .` or "
|
|
31
|
+
"`python setup.py build_ext --inplace` to build the native extension first."
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
lib_path = str(candidates[0])
|
|
35
|
+
return CubeCDLL(lib_path)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
_libcube = _load_library()
|
|
39
|
+
|
|
40
|
+
_libcube.solve.argtypes = [c_char_p, c_char_p, c_char_p, c_int32, c_bool]
|
|
41
|
+
_libcube.solve.restype = c_int32
|
|
42
|
+
|
|
43
|
+
_libcube.facecube.argtypes = [c_char_p, c_char_p, c_char_p]
|
|
44
|
+
_libcube.facecube.restype = None
|
|
45
|
+
|
|
46
|
+
_libcube.solvable.argtypes = [c_char_p]
|
|
47
|
+
_libcube.solvable.restype = c_bool
|
|
48
|
+
|
|
49
|
+
_libcube.permutation.argtypes = [c_char_p, c_char_p]
|
|
50
|
+
_libcube.permutation.restype = None
|
|
51
|
+
|
|
52
|
+
_libcube.solve_result_to_string.argtypes = [c_int32]
|
|
53
|
+
_libcube.solve_result_to_string.restype = c_char_p
|
|
Binary file
|