Metadata-Version: 2.4
Name: hmt-lzma-pyo3
Version: 0.2.0
Summary: Python module based on the LZ-String javascript, purpose here is to be faster than existing native python implementation
License: The MIT License (MIT)
        
        Copyright (c) 2021
        
        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.
        
        ===============================================================================
        Many files are modified from Thandy and are licensed under the
        following license:
        
        Thandy is distributed under this license:
        
        Copyright (c) 2008, The Tor Project, Inc.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are
        met:
        
            * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
        
            * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following disclaimer
        in the documentation and/or other materials provided with the
        distribution.
        
            * Neither the names of the copyright owners nor the names of its
        contributors may be used to endorse or promote products derived from
        this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
        "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
        A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
        OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
        SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
        LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
        DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
        THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        ===============================================================================
Classifier: License :: OSI Approved :: MIT License
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Rust
Classifier: Operating System :: POSIX
Requires-Python: >=3.9
Description-Content-Type: text/markdown

[![Build Module](https://github.com/spl0i7/lzstring_pyo3/actions/workflows/rust.yml/badge.svg)](https://github.com/spl0i7/lzstring_pyo3/actions/workflows/rust.yml)

# lzma_pyo3

Python module based on the [LZ-String javascript]( http://pieroxy.net/blog/pages/lz-string/index.html), purpose here is to be faster than existing native python [implementation](https://pypi.org/project/lzstring/)

## Installation

- Install Rust using [RustUp](https://rustup.rs/)
  
- Install Maturin for packaging which can build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages. `pip install maturin`
  
- Update pip (old pip creates problems when used with Maturin). `pip3 install --upgrade pip`
  
- Clone this repo `git clone https://github.com/spl0i7/lzstring_pyo3` and goto the directory.
  
- Build with Maturin `maturin build`

- Install build package with `pip3 install <path/to/whl>`

## Benchmark

```python

import timeit
import random
import string

import lzstring
from lzma_pyo3 import compressToBase64, decompressFromBase64

N=1000
SEQ = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))

X = lzstring.LZString()

r1 = timeit.timeit("""
from __main__ import SEQ, X
a = X.compressToBase64(SEQ)
b = X.decompressFromBase64(a)
""", number=100)

print("pure py: ", r1)

r2 = timeit.timeit("""
from __main__ import SEQ, compressToBase64, decompressFromBase64
a = compressToBase64(SEQ)
b = decompressFromBase64(a)
""", number=100)

print("rust-py: ", r2)

```
```
pure py:  1.3936761820077663
rust-py:  0.0515352350048488
```

## Caveats

`lzstring` and `lzma_pyo3` can produce different outputs when `compressToBase64` is called. This is because of different base64 padding. 

```python
import lzstring
import lzma_pyo3

X = lzstring.LZString()
print(X.compressToBase64('hello'))
print(lzma_pyo3.compressToBase64('hello'))

```
```
BYUwNmD2Q===
BYUwNmD2Q====
```

Since this is just related to base64 padding, it does not mean much. `decompressFromBase64` from both package can decompress either of the strings.  
