Skip to content

Commit

Permalink
Optimise storages (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur authored Sep 10, 2024
1 parent ac7b2d9 commit c9dd77f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.9.5
-----
2024-09-11

- Minor optimisations.

0.9.4
-----
2024-08-18
Expand All @@ -32,7 +38,7 @@ are used for versioning (schema follows below):

.. code-block:: python
from fakepy import FAKER
from fake import FAKER
from fakepy.pathy_storage.aws_s3 import AWSS3Storage
STORAGE = AWSS3Storage(
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Update version ONLY here
VERSION := 0.9.4
VERSION := 0.9.5
SHELL := /bin/bash
# Makefile for project
VENV := ~/.virtualenvs/fake.py/bin/activate
Expand Down
39 changes: 28 additions & 11 deletions fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from uuid import UUID

__title__ = "fake.py"
__version__ = "0.9.4"
__version__ = "0.9.5"
__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2023-2024 Artur Barseghyan"
__license__ = "MIT"
Expand Down Expand Up @@ -397,6 +397,18 @@ def __init__(self, *args, **kwargs) -> None:
self.args = args
self.kwargs = kwargs

def generate_basename(
self: "BaseStorage",
prefix: str = "tmp",
length: int = 8,
) -> str:
"""Generate a random alphanumeric sequence."""
if not prefix:
prefix = "tmp"
# Use lowercase letters, digits and underscore
characters = string.ascii_lowercase + string.digits + "_"
return prefix + "".join(random.choices(characters, k=length))

@abstractmethod
def generate_filename(
self: "BaseStorage",
Expand Down Expand Up @@ -490,16 +502,21 @@ def generate_filename(
if not extension:
raise Exception("Extension shall be given!")

if basename:
return str(dir_path / f"{basename}.{extension}")
else:
temp_file = NamedTemporaryFile(
prefix=prefix,
dir=str(dir_path),
suffix=f".{extension}",
delete=False,
)
return temp_file.name
if not basename:
basename = self.generate_basename(prefix)

return str(dir_path / f"{basename}.{extension}")

# if basename:
# return str(dir_path / f"{basename}.{extension}")
# else:
# temp_file = NamedTemporaryFile(
# prefix=prefix,
# dir=str(dir_path),
# suffix=f".{extension}",
# delete=False,
# )
# return temp_file.name

def write_text(
self: "FileSystemStorage",
Expand Down
2 changes: 1 addition & 1 deletion fakepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from fake import * # noqa
# from fake import * # noqa
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "fake.py"
description = "Minimalistic, standalone alternative fake data generator with no dependencies."
readme = "README.rst"
version = "0.9.4"
version = "0.9.5"
dependencies = []
authors = [
{name = "Artur Barseghyan", email = "artur.barseghyan@gmail.com"},
Expand Down

0 comments on commit c9dd77f

Please sign in to comment.