Git Product home page Git Product logo

lambda-ex's Introduction

lambda-ex

python lambda expression in multiple lines.

install

pip install git+https://github.com/likianta/lambda-ex

note: it requires python 3.8+.

usage

from lambda_ex import xlambda

add = xlambda('a, b', """
    return a + b
""")

print(add(1, 2))  # -> 3

kwargs

from lambda_ex import xlambda

add = xlambda('a, b, c=0', """
    return a + b + c
""")

print(add(1, 2))  # -> 3
print(add(1, 2, 3))  # -> 6
print(add(a=1, b=2, c=3))  # -> 6

if you are passing a complex object, for example a long list, you can also use "post" kwargs like below:

from lambda_ex import xlambda

print_names = xlambda('title_case=False', """
    for n in names:
        if title_case:
            print(n.title())
        else:
            print(n)
""", kwargs={
    'names': (
        'anne',
        'bob',
        'charlie',
        'david',
        'erin',
    )
})

print_names(title_case=True)
#   -> Anne
#      Bob
#      Charlie
#      David
#      Erin
print_names(title_case=True, names=('fred', 'george', 'harry'))
#   -> Fred
#      George
#      Harry

type annotations

from lambda_ex import xlambda

add = xlambda('a: int, b: int', """
    return a + b
""")

print(add(1, 2))  # -> 3

recursive call

use __selfunc__ to call itself:

from lambda_ex import xlambda

fibonacci = xlambda(('n'), """
    if n <= 0:
        raise ValueError(n)
    if n <= 2:
        return 1
    return __selfunc__(n - 1) + __selfunc__(n - 2)
""")

fibonacci(10)  # -> 55

context (locals and globals)

lambda-ex can directly access locals and globals in its occurrence:

from lambda_ex import xlambda

a = 1
b = 2

add = xlambda('', """
    return a + b
""")

add()  # -> 3

and modify "global" values:

from lambda_ex import xlambda

a = 1
b = 2
c = 0

add = xlambda('', """
    global c
    c = 3
    return a + b + c
""")

print(add())  # -> 6
print(a, b, c)  # -> 1 2 3

warning: there is some limitation in this case, see here.

tips & tricks

  • please check examples folder to get more usages.

  • if you're using pycharm, you can add a code template to pycharm's live template:

    xlambda('$END$', """
    
    """)
    

  • by default lambda-ex inherits caller context, if you want to forbid this (it would be little faster then), set inherit_context to False:

    from lambda_ex import xlambda
    hello_world = xlambda('', """
        print('hello world')
    """, inherit_context=False)

cautions & limitations

  • use \\n instead of \n in your lambda expression. or you may use the r-string (example below).

    from lambda_ex import xlambda
    foo = xlambda('', r"""
        print('one\ntwo\nthree')
    """)
    foo()

  • you can only use global when xlambda in top module, otherwise it won't affect outside variables:

    from lambda_ex import xlambda
    
    def foo():
        a = 1
        b = 2
        c = 0
    
        add = xlambda('', """
            global c  # no effect
            # btw do never use `nonlocals ...` in xlambda, it will raise an
            #   error at once.
            c = 3
            return a + b + c
        """)
    
        print(add())  # -> 6
        print(a, b, c)  # -> 1 2 0
        #                        ^ no change
    
    foo()

lambda-ex's People

Contributors

likianta avatar

Watchers

 avatar

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.