Code Monkey home page Code Monkey logo

Comments (5)

itssoap avatar itssoap commented on June 9, 2024 1

@elisbyberi Thanks for your hint! I made it work in Interoperability mode with types provided by codon:

cache_dict = Dict[str, u64]()

def cache(func):
    global cache_dict
    def wrapper(n: u64) -> u64:
        if str(n) not in cache_dict:
            temp = func(n)
            cache_dict[str(n)] = temp

        return u64(cache_dict[str(n)])

    return wrapper



@cache
def fact(n: u64) -> u64:
    if n == u64(0) | n == u64(1):
        return u64(1)
    return n.__mul__(u64(fact(n-u64(1))))


def main():
    print(fact(u64(15)))

if __name__ == '__main__':
    main()

I guess it doesn't need the type of callable being returned as well, which was my main focus earlier.

Another interesting note is the time taken by Interoperability with Codon provided types vs @python Decorator, interoperability being a faster run:

image

Though it would still be interesting to have support for Python classes as type-hints. Thanks

from codon.

elisbyberi avatar elisbyberi commented on June 9, 2024

@itssoap What you are attempting to do with from python import is not documented anywhere.

from codon.

itssoap avatar itssoap commented on June 9, 2024

@elisbyberi my attempt was inspired by the numpy example available in the same link that you have provided. When you say its not documented, do you refer to my usage of the typing module specifically or am I doing something incorrectly. Thanks

https://docs.exaloop.io/codon/interoperability/python#from-python-import:~:text=from%20python%20import%20numpy%20as%20np

from codon.

itssoap avatar itssoap commented on June 9, 2024

I was able to get it to run by switching to Decorator mode

@python
def run():
    cache_dict: dict = {}
    
    def cache(func):
        def wrapper(*args, **kwargs) -> int:
            n = args[0]
            if str(n) not in cache_dict:
                temp = func(n)
                cache_dict[str(n)] = temp

            return cache_dict[str(n)]

        return wrapper
    
    @cache
    def fact(n: int) -> int:
        if n in (0, 1):
            return 1

        return n * fact(n-1)

    def main() -> None:
        print(fact(15))

    main()


if __name__ == '__main__':
    run()

Also, I could further use the same strongly typed code in this decorator, get the build generated and functioning:

@python
def run():
    import typing
    cache_dict: dict = {}
    T = typing.TypeVar('T')
    P = typing.ParamSpec('P')

    def cache(func: typing.Callable[P, T]) -> typing.Callable[P, T]:
        def wrapper(*args: P.args, **kwargs: P.kwargs) -> typing.Type[T]:
            n = args[0]
            if str(n) not in cache_dict:
                temp = func(n)
                cache_dict[str(n)] = temp

            return cache_dict[str(n)]

        return wrapper

    @cache
    def fact(n: int) -> int:
        if n in (0, 1):
            return 1

        return n * fact(n-1)

    def main() -> None:
        print(fact(15))

    main()


if __name__ == '__main__':
    run()

But I am still interested in knowing how to make my original code (Interoperability mode) work, without shoving my whole code in the python decorator. Thanks

from codon.

elisbyberi avatar elisbyberi commented on June 9, 2024

@itssoap I am referring to your usage of the Python Typing module. Everything imported from python is just a Python object (PyObject). Hence, the test.py:4:4-31: error: expected type expression error. Use Codon objects for typing.

You will get the same error if you use the NumPy Python object for typing in Codon:

from python import numpy as np

x: np.array = np.array([1, 2, 3, 4]) * 10
print(x)  # [10 20 30 40]
i545.py:3:4-12: error: expected type expression
i545.py:4:7-8: error: name 'x' is not defined

from codon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.