Example #1
0
# mode: run
# tag: annotation_typing, pure3.0, mypy

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])  # type: cython.StructType


@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)


def test_struct(n: cython.int, x: cython.double) -> MyStruct2:
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493)
    >>> d = test_struct.__annotations__
    >>> sorted(d)
    ['n', 'return', 'x']
    """
    assert cython.typeof(n) == 'int', cython.typeof(n)
Example #2
0
    False
    """
    result = False
    should_raise_bool = True if should_raise else False  # help the type inference ...
    with nogil:
        print("WORKS")
        with cython.nogil:
            result = True
            if should_raise_bool:
                raise ValueError("RAISED!")
    return result


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


def test_struct(n, x):
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493)
    """
    a = cython.declare(MyStruct2)
    a[0] = MyStruct(is_integral=True, data=MyUnion(n=n))
    a[1] = MyStruct(is_integral=False, data={'x': x})
    return a[0].data.n, a[1].data.x


import cython as cy
from cython import declare, cast, locals, address, typedef, p_void, compiled
Example #3
0
    >>> test_with_nogil(nogil())
    WORKS
    True
    >>> raised
    [None]
    """
    result = False
    with nogil:
        print("WORKS")
        with cython.nogil:
            result = True
    return result

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

def test_struct(n, x):
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493)
    """
    a = cython.declare(MyStruct2)
    a[0] = MyStruct(is_integral=True, data=MyUnion(n=n))
    a[1] = MyStruct(is_integral=False, data={'x': x})
    return a[0].data.n, a[1].data.x

import cython as cy
from cython import declare, cast, locals, address, typedef, p_void, compiled
from cython import declare as my_declare, locals as my_locals, p_void as my_void_star, typedef as my_typedef, compiled as my_compiled
Example #4
0
    False
    """
    result = False
    should_raise_bool = True if should_raise else False  # help the type inference ...
    with nogil:
        print("WORKS")
        with cython.nogil:
            result = True
            if should_raise_bool:
                raise ValueError("RAISED!")
    return result


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


def test_struct(n, x):
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493, False)
    """
    a = cython.declare(MyStruct3)
    a[0] = MyStruct(is_integral=True, data=MyUnion(n=n))
    a[1] = MyStruct(is_integral=False, data={'x': x})
    if sys.version_info >= (3, 6):
        # dict is ordered => struct creation via keyword arguments above was deterministic!
        a[2] = MyStruct(False, MyUnion(x=x))
    else:
Example #5
0
# -*- coding: utf-8 -*-
# @Author: lcl1026504480
# @Date:   2019-08-29 17:11:37
# @Last Modified by:   lcl1026504480
# @Last Modified time: 2019-08-29 17:17:06
import cython
cython.declare(x=cython.int, x_ptr=cython.p_int)
x = 5
x_ptr = cython.address(x)
cython.declare(n=cython.longlong)
print(cython.sizeof(cython.longlong))
n = 400
print(cython.sizeof(n))
MyStruct = cython.struct(x=cython.int, y=cython.int, data=cython.double)
a = cython.declare(MyStruct)
T = cython.typedef(cython.p_int)  # ctypedef int* T
# t1 = cython.cast(T, t)
# t2 = cython.cast(T, t, typecheck=True)
Example #6
0
import numpy as np
import cython
from cython.cimports.libc.math import cos, sin, pi


@cython.cfunc
@cython.returns((cython.double, cython.double))
def __define_point():
    return (0, 0)


Point = cython.typedef((cython.double, cython.double))


@cython.cclass
class Transform:
    m = cython.declare(object, visibility="public")

    def __init__(self, **kwargs):
        self.set(**kwargs)

    def set(self, **kwargs):
        self.m = Transform.matrix(**kwargs)

    @staticmethod
    def matrix(t_x=0.0, t_y=0.0, s_x=1.0, s_y=1.0, degrees=0.0, t=None):
        if t:
            return np.array(t.m)

        theta = degrees * pi / 180
        c = cos(theta)
Example #7
0
    return y[0]

@cython.locals(x=cython.int)
@cython.locals(y=cython.bint)
def test_locals(x):
    """
    >>> bool(test_locals(5))
    True
    """
    y = x
    return y
    

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

def test_struct(n, x):
    """
    >>> test_struct(389, 1.64493)
    (389, 1.64493)
    """
    a = cython.declare(MyStruct2)
    a[0] = MyStruct(is_integral=True, data=MyUnion(n=n))
    a[1] = MyStruct(is_integral=False, data={'x': x})
    return a[0].data.n, a[1].data.x

import cython as cy
from cython import declare, cast, locals, address, typedef, p_void, compiled
from cython import declare as my_declare, locals as my_locals, p_void as my_void_star, typedef as my_typedef, compiled as my_compiled