Skip to content

Commit

Permalink
add compression utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
trisongz committed Nov 17, 2023
1 parent d1c6d9c commit a9cf889
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lazyops/libs/compress/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Compression Utilities
"""

from typing import Union
from .base import BaseCompress
from .gz import GzipCompress
from .lz import Lz4Compress
from .zb import ZLibCompress
from .ztd import ZStdCompress

CompressionT = Union[BaseCompress, GzipCompress]

class Compress:

gzip = GzipCompress
zstd = ZStdCompress
zlib = ZLibCompress
lz4 = Lz4Compress

36 changes: 36 additions & 0 deletions lazyops/libs/compress/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import abc
from typing import Union
from lazyops.utils.pooler import ThreadPoolV2 as ThreadPooler

class BaseCompress(abc.ABC):

@abc.abstractclassmethod
def compress(cls, data: Union[str, bytes], **kwargs) -> bytes:
"""
Base Compress
"""
pass

@abc.abstractclassmethod
def decompress(cls, data: Union[str, bytes], **kwargs) -> bytes:
"""
Base Decompress
"""
pass


@abc.abstractclassmethod
async def acompress(cls, data: Union[str, bytes], **kwargs) -> bytes:
"""
Base Compress
"""
return await ThreadPooler.run_async(cls.compress, data, **kwargs)

@abc.abstractclassmethod
async def adecompress(cls, data: Union[str, bytes], **kwargs) -> bytes:
"""
Base Decompress
"""
return await ThreadPooler.run_async(cls.decompress, data, **kwargs)


26 changes: 26 additions & 0 deletions lazyops/libs/compress/gz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
GZIP Compress
"""

import gzip
from .base import BaseCompress
from typing import Union

class GzipCompress(BaseCompress):

@classmethod
def compress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
GZip Compress
"""
if isinstance(data, str): data = data.encode(encoding)
return gzip.compress(data)

@classmethod
def decompress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
GZip Decompress
"""
if isinstance(data, str): data = data.encode(encoding = encoding)
return gzip.decompress(data)

47 changes: 47 additions & 0 deletions lazyops/libs/compress/lz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
LZ4 Compress
"""

from .base import BaseCompress
from typing import Union

try:
import lz4.frame
_lz4_available = True
except ImportError:
_lz4_available = False

def ensure_lz4_available():
"""
Ensure LZ4 is available
"""
global _lz4_available, lz4
if _lz4_available is False:
from lazyops.utils.imports import resolve_missing
resolve_missing('lz4', required = True)
import lz4.frame
_lz4_available = True
globals()['lz4'] = lz4


class Lz4Compress(BaseCompress):


@classmethod
def compress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
LZ4 Compress
"""
ensure_lz4_available()
if isinstance(data, str): data = data.encode(encoding)
return lz4.frame.compress(data)

@classmethod
def decompress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
LZ4 Decompress
"""
ensure_lz4_available()
if isinstance(data, str): data = data.encode(encoding = encoding)
return lz4.frame.decompress(data)

26 changes: 26 additions & 0 deletions lazyops/libs/compress/zb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
ZLib Compress
"""

import zlib
from .base import BaseCompress
from typing import Union

class ZLibCompress(BaseCompress):

@classmethod
def compress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
ZLib Compress
"""
if isinstance(data, str): data = data.encode(encoding)
return zlib.compress(data)

@classmethod
def decompress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
ZLib Decompress
"""
if isinstance(data, str): data = data.encode(encoding = encoding)
return zlib.decompress(data)

49 changes: 49 additions & 0 deletions lazyops/libs/compress/ztd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
ZStd Compress
Note - ZStd doesn't work as well as it returned some errors when decompressing.
"""

from .base import BaseCompress
from typing import Union

try:
import zstd
_zstd_available = True
except ImportError:
_zstd_available = False

def ensure_zstd_available():
"""
Ensure Zstd is available
"""
global _zstd_available, zstd
if _zstd_available is False:
from lazyops.utils.imports import resolve_missing
resolve_missing('zstd', required = True)
import zstd
_zstd_available = True
globals()['zstd'] = zstd


class ZStdCompress(BaseCompress):


@classmethod
def compress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
ZStd Compress
"""
ensure_zstd_available()
if isinstance(data, str): data = data.encode(encoding)
return zstd.compress(data)

@classmethod
def decompress(cls, data: Union[str, bytes], encoding: str = 'utf-8', **kwargs) -> bytes:
"""
ZStd Decompress
"""
ensure_zstd_available()
if isinstance(data, str): data = data.encode(encoding = encoding)
return zstd.decompress(data)

2 changes: 1 addition & 1 deletion lazyops/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '0.2.62rc11'
VERSION = '0.2.62rc12'

0 comments on commit a9cf889

Please sign in to comment.