AlanCristhian / statically

Compiles a python function with cython using only a decorator.
MIT License
185 stars 6 forks source link

Add fallback for REPL to standard Python code #4

Closed disconnect3d closed 6 years ago

disconnect3d commented 6 years ago

Hey,

Thanks for such great project :).

This PR improves it a bit so that standard Python REPL works fine.

I have also extended README and included that statically works fine with IPython shell.

Side notes

  1. I haven't check whether IDLE works, could you do that?
  2. If we want to add a testcase for it - it probably should be done as a bash script that would do something similar as attached below.
(cytho) [dc@dc:statically|support-repl %]$ cat test.py
import cython
import statically

@statically.typed
def func():
    # Cython types are evaluated as for cdef declarations
    x: cython.int               # cdef int x
    y: cython.double = 0.57721  # cdef double y = 0.57721
    z: cython.float  = 0.57721  # cdef float z  = 0.57721

    # Python types shadow Cython types for compatibility reasons
    a: float = 0.54321          # cdef double a = 0.54321
    b: int = 5                  # cdef object b = 5
    c: long = 6                 # cdef object c = 6

    return y+z + a+b+c

@statically.typed
class A:
    a: cython.int
    b: cython.int
    def __init__(self, b=0):
        self.a = 3
        self.b = b

print('func = ', func())
a = A()
print(a.a, a.b)
(cytho) [dc@dc:statically|support-repl %]$ python -c "import test"
/home/dc/Projects/statically/statically.py:100: RuntimeWarning: Current code isn't launched from a file so statically.typed isn't able to cythonize stuff.Falling back to normal Python code.
  RuntimeWarning
func =  12.69763
3 0
(cytho) [dc@dc:statically|support-repl %]$ echo "import test" > a.py
(cytho) [dc@dc:statically|support-repl %]$ python a.py
func =  12.69763
3 0
(cytho) [dc@dc:statically|support-repl %]$ ipython -c "import test"
func =  12.69763
3 0
(cytho) [dc@dc:statically|support-repl %]$ ipython a.py
func =  12.69763
3 0
(cytho) [dc@dc:statically|support-repl %]$ 
AlanCristhian commented 6 years ago

Thanks for your contribution. I will explore a way to test it.

AlanCristhian commented 6 years ago

Done :)