Skip to content

Commit

Permalink
Made code changes, made it clean and short for easy understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
sroycho4 authored Dec 6, 2023
1 parent 07ec198 commit 54759af
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions src/openai/cli/_progress.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
from __future__ import annotations

import io
from typing import Callable
from typing_extensions import override

import tqdm

class CancelledError(Exception):
def __init__(self, msg: str) -> None:
self.msg = msg
super().__init__(msg)

@override
def __str__(self) -> str:
return self.msg

__repr__ = __str__


class BufferReader(io.BytesIO):
def __init__(self, buf: bytes = b"", desc: str | None = None) -> None:
super().__init__(buf)
self._len = len(buf)
self._progress = 0
self._callback = progress(len(buf), desc=desc)

def __len__(self) -> int:
return self._len
return len(self.getvalue())

@override
def read(self, n: int | None = -1) -> bytes:
chunk = io.BytesIO.read(self, n)
chunk = super().read(n)
self._progress += len(chunk)

try:
self._callback(self._progress)
except Exception as e: # catches exception from the callback
raise CancelledError("The upload was cancelled: {}".format(e))
except Exception as e:
raise CancelledError(f"The upload was cancelled: {e}")

return chunk


def progress(total: float, desc: str | None) -> Callable[[float], None]:
import tqdm

meter = tqdm.tqdm(total=total, unit_scale=True, desc=desc)

def incr(progress: float) -> None:
Expand All @@ -54,6 +41,5 @@ def incr(progress: float) -> None:

return incr


def MB(i: int) -> int:
return int(i // 1024**2)
return i // 1024**2

0 comments on commit 54759af

Please sign in to comment.