How to use the cython.compiled function in Cython

To help you get started, we’ve selected a few Cython examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cython / cython / tests / run / pure_py3.py View on Github external
# mode: run
# tag: annotation_typing, pure3.0

import cython

is_compiled = cython.compiled

MyUnion = cython.union(n=cython.int, x=cython.double)
MyStruct = cython.struct(is_integral=cython.bint, data=MyUnion)
MyStruct2 = cython.typedef(MyStruct[2])


@cython.ccall  # cpdef => C return type
def test_return_type(n: cython.int) -> cython.double:
    """
    >>> test_return_type(389)
    389.0
    """
    assert cython.typeof(n) == 'int', cython.typeof(n)
    return n if is_compiled else float(n)
github cython / cython / tests / run / purecdef.py View on Github external
def test_method():
    """
    >>> test_method()
    4
    True
    """
    x = PureFoo(2)
    print(x.puremeth(2))
    if cython.compiled:
        print(isinstance(x(), float))
    else:
        print(True)
    return
github cython / cython / tests / run / pure_py.py View on Github external
def test_imports():
    """
    >>> test_imports()
    (True, True)
    """
    a = cython.NULL
    b = declare(p_void, cython.NULL)
    c = my_declare(my_void_star, cython.NULL)
    d = cy.declare(cy.p_void, cython.NULL)

    return a == d, compiled == my_compiled
github cython / cython / tests / run / pure_py.py View on Github external
def test_sizeof():
    """
    >>> test_sizeof()
    True
    True
    True
    True
    True
    """
    x = cython.declare(cython.bint)
    print(cython.sizeof(x) == cython.sizeof(cython.bint))
    print(sizeof(cython.char) <= sizeof(cython.short) <= sizeof(cython.int) <= sizeof(cython.long) <= sizeof(cython.longlong))
    print(cython.sizeof(cython.uint) == cython.sizeof(cython.int))
    print(cython.sizeof(cython.p_int) == cython.sizeof(cython.p_double))
    if cython.compiled:
        print(cython.sizeof(cython.char) < cython.sizeof(cython.longlong))
    else:
        print(cython.sizeof(cython.char) == 1)
github theJollySin / mazelib / mazelib / transmute / CuldeSacFiller.py View on Github external
# If the code is not Cython-compiled, we need to add some imports.
from cython import compiled
if not compiled:
    from mazelib.transmute.MazeTransmuteAlgo import MazeTransmuteAlgo


class CuldeSacFiller(MazeTransmuteAlgo):
    """ This algorithm could be called LoopFiller, because it breaks up loop in the maze.

    1. Scan the maze, looking for cells with connecting halls that go in exactly two directions.
    2. At each of these places, travel in both directions until you find your first intersection.
    3. If the first intersection for both paths is the same, you have a loop.
    4. Fill in the cell you started at with a wall, breaking the loop.
    """

    def _transmute(self):
        for r in range(1, self.grid.shape[0], 2):
            for c in range(1, self.grid.shape[1], 2):
                if (r, c) in (self.start, self.end):
github theJollySin / mazelib / mazelib / generate / Sidewinder.py View on Github external
from random import choice, random
import numpy as np
# If the code is not Cython-compiled, we need to add some imports.
from cython import compiled
if not compiled:
    from mazelib.generate.MazeGenAlgo import MazeGenAlgo


class Sidewinder(MazeGenAlgo):
    """
    The Algorithm

    1. Work through the grid row-wise, starting with the cell at 0,0.
    2. Add the current cell to a "run" set.
    3. For the current cell, randomly decide whether to carve East.
    4. If a passage East was carved, make the new cell the current cell and repeat steps 2-4.
    5. If a passage East was not carved, choose any one of the cells in the run set and carve
        a passage North. Then empty the run set. Repeat steps 2-5.
    6. Continue until all rows have been processed.

    Optional Parameters
github theJollySin / mazelib / mazelib / transmute / Perturbation.py View on Github external
from random import choice, randrange
import numpy as np
# If the code is not Cython-compiled, we need to add some imports.
from cython import compiled
if not compiled:
    from mazelib.transmute.MazeTransmuteAlgo import MazeTransmuteAlgo


class Perturbation(MazeTransmuteAlgo):
    """
    The Algorithm

    1. Start with a complete, valid maze.
    2. Add a small number of random walls, blocking current passages.
    3. Go through the maze and reconnect all passages that are not currently open,
        by randomly opening walls.
    4. Repeat steps 3 and 4 a prescribed number of times.

    Optional Parameters

    new_walls: Integer [1, ...)
github Baekalfen / PyBoy / pyboy / plugins / game_wrapper_kirby_dream_land.py View on Github external
# License: See LICENSE.md file
# GitHub: https://github.com/Baekalfen/PyBoy
#
__pdoc__ = {
    "GameWrapperKirbyDreamLand.cartridge_title": False,
    "GameWrapperKirbyDreamLand.post_tick": False,
}

from pyboy.utils import WindowEvent
from pyboy.logger import logger
from .base_plugin import PyBoyGameWrapper


try:
    from cython import compiled
    cythonmode = compiled
except ImportError:
    cythonmode = False


class GameWrapperKirbyDreamLand(PyBoyGameWrapper):
    """
    This class wraps Kirby Dream Land, and provides easy access to score and a "fitness" score for AIs.

    If you call `print` on an instance of this object, it will show an overview of everything this object provides.
    """
    cartridge_title = "KIRBY DREAM LA"

    def __init__(self, *args, **kwargs):
        self.shape = (20, 16)
        """The shape of the game area"""
        self.score = 0
github theJollySin / mazelib / mazelib / generate / TrivialMaze.py View on Github external
from random import randint
import numpy as np
# If the code is not Cython-compiled, we need to add some imports.
from cython import compiled
if not compiled:
    from mazelib.generate.MazeGenAlgo import MazeGenAlgo

SERPENTINE = 1
SPIRAL = 2


class TrivialMaze(MazeGenAlgo):
    """
    The Algorithm

    This is actually a collection of little tools to make simple,
    unicursal mazes. Currently, there are two trivial mazes available:
    serpentine and spiral.
    """

    def __init__(self, h, w, maze_type='spiral'):
github yungyuc / cythonup / module / quick_prototyping_for_speed / knapsack / knapsack_3_memoryview.py View on Github external
from __future__ import division, print_function
import sys
import numpy as np
try:
    import cython
    is_compiled = cython.compiled
except:
    print("can't import cython")
    is_compiled = False
    
if sys.version_info <(3,):
    range = xrange

# simple "Linearization of constraints" estimation
def estimate(items, K0):  
  v, K = 0, K0  
  for i in range(items.shape[0]):
    item = items[i]
    if item.weight > K0:
        continue
    if item.weight> K:
      return v + item.value*K//item.weight