-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from muodov/python-package
Python package
- Loading branch information
Showing
58 changed files
with
585 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import warnings | ||
|
||
|
||
class SlowContextWarning(Warning): | ||
pass | ||
|
||
try: | ||
import os | ||
from ckociembawrapper import ffi, lib | ||
cache_dir = os.path.join(os.path.dirname(__file__), 'cprunetables') | ||
|
||
def _solve(s): | ||
res = lib.solve(s.encode('utf-8'), cache_dir.encode('utf-8')) | ||
if res != ffi.NULL: | ||
return ffi.string(res).strip() | ||
else: | ||
raise ValueError('Error. Probably cubestring is invalid') | ||
except ImportError: | ||
warnings.warn("Native version of the package is not available. " | ||
"We have to fallback to pure-Python implementation of " | ||
"the algorithm, which is usually many times slower. " | ||
"If performance is important to you, check official " | ||
"project page for a native implementation: " | ||
"https://github.com/muodov/kociemba", | ||
SlowContextWarning) | ||
import pykociemba.search | ||
_solve = lambda s: pykociemba.search.Search().solution(s, 24, 1000, False).strip() | ||
|
||
|
||
def solve(cubestring): | ||
""" | ||
Solve a Rubik's cube using two-phase algorithm. | ||
>>> solve("BBURUDBFUFFFRRFUUFLULUFUDLRRDBBDBDBLUDDFLLRRBRLLLBRDDF") | ||
"B U' L' D' R' D' L2 D' L F' L' D F2 R2 U R2 B2 U2 L2 F2 D'" | ||
""" | ||
|
||
return _solve(cubestring) | ||
|
||
__all__ = ['solve'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import cffi | ||
|
||
ffi = cffi.FFI() | ||
ffi.set_source( | ||
"kociemba.ckociembawrapper", | ||
""" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <search.h> | ||
char* solve(char *cubestring, char *cache_dir) | ||
{ | ||
char *sol = solution( | ||
cubestring, | ||
24, | ||
1000, | ||
0, | ||
cache_dir | ||
); | ||
return sol; | ||
} | ||
""", | ||
include_dirs=['kociemba/ckociemba/include'], | ||
sources=[ | ||
'kociemba/ckociemba/coordcube.c', | ||
'kociemba/ckociemba/cubiecube.c', | ||
'kociemba/ckociemba/facecube.c', | ||
'kociemba/ckociemba/search.c'], | ||
extra_compile_args=['-std=c99']) | ||
|
||
ffi.cdef("char* solve(char *cubestring, char *cache_dir);") | ||
|
||
if __name__ == "__main__": | ||
ffi.compile(verbose=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CKOCIEMBA_SRC = coordcube.c cubiecube.c facecube.c search.c | ||
CKOCIEMBA_INCLUDE = include | ||
CFLAGS = -std=c99 | ||
BINDIR = bin | ||
BIN = kociemba | ||
|
||
solve: solve.c $(CKOCIEMBA_SRC) | ||
mkdir -p $(BINDIR) | ||
gcc $(CFLAGS) $(CKOCIEMBA_SRC) -I$(CKOCIEMBA_INCLUDE) solve.c -o $(BINDIR)/$(BIN) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.