Skip to main content

fast lambda expression serializer

Project description

lambdser

A lambda expression serializer for python. Can be used to make pickle eat lambdas with closures.

A typical use case is serializing lambdas for multiprocessing. Using lambdser in front, let multiprocessing eat the lambda expression.

Install

pip install lambdser

or install it from github

pip install git+https://github.com/cloasdata/lambdser.git

or just clone it

todo

I did not find a way to register lambdser es pickler in the pickle module. It would be really useful if somebody can help me. However, one can use the LambdserPickler class to overwrite the default behaviour of pickle.Pickler (?) or use LamdserPickler as the one pickler.

I also did not test for particular edge cases. But feel free to contribute such tests.

usage

Example 1: module namespace

using it in module namespace.

    import pickle
    import lambdser
    
    
    two = "2"
    expression = lambda x: "number" + x + two
    
    result1 = expression("4")
    ser = lambdser.dumps(expression)
    # now pickle can dump!
    s = pickle.dumps(ser)
    ser = pickle.loads(s)
    
    expression = lambdser.loads(ser)
    result2 = expression("4")
    assert result1 == result2

Example 2: Using closure

Make a proxy of what you want to spawn in a separate process.

    import lambdser
    import multiprocessing as mp
    
    
    def make_proxy(para, *funcs):
        # make proxy for the mp
        ser_list = []
        for f in funcs:
            ser_list.append(lambdser.dumps(f))
        return para, ser_list
    
    
    def processor(*ser):
        # unzip the proxy and to the work
        para, funcs = ser
        funcs = [lambdser.loads(ser) for ser in funcs]
        res = None
        for f in funcs:
            res = f(para)
        print(res)
        return res
    
    
    def do_stuff():
        two = "2"
        ser = make_proxy("4", lambda x: x + two)
        mp.Process(target=processor, args=ser).start()

    do_stuff()

performance

it is around 100 times faster than dill. This was one reason to develop this package.

py .\test\profiling.py

Results dumps
lambdser: 0.012459
dill:     1.485589
times:    119.236291

Copyright © 2022 Simon Bauer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambdser-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lambdser-0.1.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file lambdser-0.1.2.tar.gz.

File metadata

  • Download URL: lambdser-0.1.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for lambdser-0.1.2.tar.gz
Algorithm Hash digest
SHA256 29b0ecf64a25173ad01da8a1a52f61a67221c36059b672029b4f6453c7f19512
MD5 ee666382bdd08ee0834b96e3bfae8050
BLAKE2b-256 9980565be71118a78c38251f04cd8942e44acb8b8795172ce4e07c9546f92e84

See more details on using hashes here.

File details

Details for the file lambdser-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: lambdser-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for lambdser-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c1a2364ea276de938720c9f978e94beb526baf90a34cef7dd3d0e8a1ee1701fe
MD5 5c5d85e4774170bdb44e5a04d883f93d
BLAKE2b-256 ee670a8a200e86c38b14f132b3b8d37b4681dbe39eb25656ff9ed52cbef2f9db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page