示例#1
0
def test_compose():
    from syn.base_utils import compose

    def f(x): return x + 2
    def g(x): return 2 * x
    def h(x): return x - 2

    f1 = compose(g, f)
    f2 = compose(f, g)
    f3 = compose(f, g, h)

    assert f1(3) == 10
    assert f2(3) == 8
    assert f3(3) == 4
示例#2
0
def sudoku(S):
    strs = compose(list, partial(map, str))

    vars = {}
    values = list(range(1, 10))
    for i in range(1, 10):
        for k in range(i*10 + 1, i*10 + 10):
            vars[str(k)] = list(values)
    
    cons = []
    for i in range(1, 10):
        cons.append(AllDifferentConstraint(strs(range(i*10+1, i*10+10)))) # rows
        cons.append(AllDifferentConstraint(strs(range(10+i, 100+i, 10)))) # cols

    # 3x3 squares
    cons.append(AllDifferentConstraint(strs([11,12,13,21,22,23,31,32,33])))
    cons.append(AllDifferentConstraint(strs([41,42,43,51,52,53,61,62,63])))
    cons.append(AllDifferentConstraint(strs([71,72,73,81,82,83,91,92,93])))
    cons.append(AllDifferentConstraint(strs([14,15,16,24,25,26,34,35,36])))
    cons.append(AllDifferentConstraint(strs([44,45,46,54,55,56,64,65,66])))
    cons.append(AllDifferentConstraint(strs([74,75,76,84,85,86,94,95,96])))
    cons.append(AllDifferentConstraint(strs([17,18,19,27,28,29,37,38,39])))
    cons.append(AllDifferentConstraint(strs([47,48,49,57,58,59,67,68,69])))
    cons.append(AllDifferentConstraint(strs([77,78,79,87,88,89,97,98,99])))

    # init = [[0, 9, 0, 7, 0, 0, 8, 6, 0],
    #         [0, 3, 1, 0, 0, 5, 0, 2, 0],
    #         [8, 0, 6, 0, 0, 0, 0, 0, 0],
    #         [0, 0, 7, 0, 5, 0, 0, 0, 6],
    #         [0, 0, 0, 3, 0, 7, 0, 0, 0],
    #         [5, 0, 0, 0, 1, 0, 7, 0, 0],
    #         [0, 0, 0, 0, 0, 0, 1, 0, 9],
    #         [0, 2, 0, 6, 0, 0, 0, 5, 0],
    #         [0, 5, 4, 0, 0, 8, 0, 7, 0]]

    init = [[8, 2, 7, 1, 5, 4, 3, 9, 6],
            [9, 6, 5, 3, 2, 7, 1, 4, 8],
            [3, 4, 1, 6, 8, 9, 7, 5, 2],
            [5, 9, 3, 4, 6, 8, 2, 7, 1],
            [4, 7, 2, 5, 1, 3, 6, 8, 9],
            [6, 1, 8, 9, 7, 2, 4, 3, 5],
            [7, 8, 6, 2, 3, 5, 9, 1, 4],
            [1, 5, 4, 7, 9, 6, 8, 2, 3],
            [2, 3, 9, 8, 4, 1, 5, 6, 7]]

    for i in range(1, 10):
        for j in range(1, 10):
            if init[i-1][j-1] != 0:
                cons.append(EqualConstraint(str(i*10+j), init[i-1][j-1]))
    
    sol = {}
    for i in range(1, 10):
        for j in range(1, 10):
            sol[str(i*10+j)] = init[i-1][j-1]

    p = Problem(Domain(**vars), cons)
    s = S(p)

    assert p.check(sol)
    assert first(s.solutions())
示例#3
0
文件: base.py 项目: pombredanne/syn-1
def safe_sorted(obj, **kwargs):
    if not isinstance(obj, Iterable):
        return obj

    try:
        return sorted(obj, **kwargs)
    except (TypeError, UnicodeDecodeError):
        kwargs['key'] = kwargs.get('key', compose(hash, hashable))
        return sorted(obj, **kwargs)
示例#4
0
def test_compose():
    from syn.base_utils import compose

    def f(x):
        return x + 2

    def g(x):
        return 2 * x

    def h(x):
        return x - 2

    f1 = compose(g, f)
    f2 = compose(f, g)
    f3 = compose(f, g, h)

    assert f1(3) == 10
    assert f2(3) == 8
    assert f3(3) == 4
示例#5
0
from functools import partial
from nose.tools import assert_raises
from syn.base_utils import compose, pyversion
from syn.python.b import Expr, from_source, MatMult, Call, Name, BinOp, \
    Add, Assign, Module, Num, Return, UnaryOp, USub, Not, And, Or, BoolOp, \
    Compare, Lt, LtE, Keyword, Starred, IfExp, Attribute
from syn.util.log.b import Logger
from .test_literals import examine

VER = pyversion()
eparse = compose(partial(from_source, mode='eval'), str)

#-------------------------------------------------------------------------------
# Expr

def test_expr():
    e = Expr(Num(1))
    assert e.emit() == '1'
    assert e.emit(indent_level=1) == '    1'

    assert e._children == [Num(1)]
    assert e._node_count == 2
    e._set_child(0, Num(2))
    assert e.emit() == '2'
    assert e._children == [Num(2)]
    assert e._node_count == 2

#-------------------------------------------------------------------------------
# Unary Operators

def test_unary_operators():
示例#6
0
from functools import partial
from nose.tools import assert_raises
from syn.base_utils import compose
from syn.python.b import Statement, from_source, from_ast, Pass, Num, Name, \
    Assign, Return, ProgN, Module, Alias, Import, Break, Continue

mparse = compose(partial(from_source, mode='exec'), str)

#-------------------------------------------------------------------------------
# Utilities


def examine(s, s2=None):
    if s2 is None:
        s2 = s
    tree = mparse(s)
    assert tree.emit() == s2
    tree2 = from_ast(tree.to_ast(), mode='exec')
    assert tree2.emit() == s2


#-------------------------------------------------------------------------------
# Statement


def test_statement():
    Statement


#-------------------------------------------------------------------------------
# Assign
示例#7
0
import ast
from nose.tools import assert_raises
from functools import partial
from syn.python.b import Literal, from_source, from_ast, PythonNode, \
    Num, Str, Bytes, List, Tuple, Set, NameConstant, Load
from syn.base_utils import compose, getkey
from syn.five import PY3

eparse = compose(partial(from_source, mode='eval'), str)
iparse = compose(partial(from_source, mode='single'), str)
mparse = compose(partial(from_source, mode='exec'), str)

#-------------------------------------------------------------------------------
# Utilities


def examine(s, s2=None):
    if s2 is None:
        s2 = s
    tree = eparse(s)
    assert tree.emit() == s2
    tree2 = from_ast(tree.to_ast(), mode='eval')
    assert tree2.emit() == s2


#-------------------------------------------------------------------------------
# Base Class


def test_literal():
    Literal()
示例#8
0
from functools import partial
from nose.tools import assert_raises
from syn.base_utils import compose
from syn.python.b import Statement, from_source, from_ast, Pass, Num, Name, \
    Assign, Return, ProgN, Module, Alias, Import, Break, Continue

mparse = compose(partial(from_source, mode='exec'), str)

#-------------------------------------------------------------------------------
# Utilities

def examine(s, s2=None):
    if s2 is None:
        s2 = s
    tree = mparse(s)
    assert tree.emit() == s2
    tree2 = from_ast(tree.to_ast(), mode='exec')
    assert tree2.emit() == s2

#-------------------------------------------------------------------------------
# Statement

def test_statement():
    Statement

#-------------------------------------------------------------------------------
# Assign

def test_assign():
    examine('a = 1')
    examine('a = b = 1')
示例#9
0
import ast
from nose.tools import assert_raises
from functools import partial
from syn.python.b import Literal, from_source, from_ast, PythonNode, \
    Num, Str, Bytes, List, Tuple, Set, NameConstant, Load
from syn.base_utils import compose, getkey
from syn.five import PY3

eparse = compose(partial(from_source, mode='eval'), str)
iparse = compose(partial(from_source, mode='single'), str)
mparse = compose(partial(from_source, mode='exec'), str)

#-------------------------------------------------------------------------------
# Utilities

def examine(s, s2=None):
    if s2 is None:
        s2 = s
    tree = eparse(s)
    assert tree.emit() == s2
    tree2 = from_ast(tree.to_ast(), mode='eval')
    assert tree2.emit() == s2

#-------------------------------------------------------------------------------
# Base Class

def test_literal():
    Literal()

#-------------------------------------------------------------------------------
# Num