Skip to content

New python version (major changes) #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mecsimcalc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# included in __all__: import using "from mecsimcalc import *" or "import mecsimcalc"

from .file_utils.general_utils import input_to_file
from .file_utils.general_utils import input_to_file, metadata_to_filetype

from .file_utils.image_utils import input_to_PIL, file_to_PIL, print_image

Expand Down Expand Up @@ -33,6 +33,7 @@
"input_to_dataframe",
"file_to_dataframe",
"input_to_file",
"metadata_to_filetype" # Deprecated
"input_to_PIL",
"table_to_dataframe",
"print_dataframe",
Expand Down
48 changes: 45 additions & 3 deletions mecsimcalc/file_utils/general_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import re
from typing import Union, Tuple
from mimetypes import guess_type, guess_extension
from warnings import warn

# This is only nessesary for python 3.6
# This is only necessary for python 3.6
EXTENSION_MAP = {
".jpe": ".jpg",
".htm": ".html",
Expand All @@ -13,7 +14,7 @@
}

def input_to_file(
input_file: str, get_file_extension: bool = False
input_file: str, get_file_extension: bool = False, metadata: bool = False
) -> Union[io.BytesIO, Tuple[io.BytesIO, str]]:
"""
>>> input_to_file(
Expand Down Expand Up @@ -68,6 +69,47 @@ def input_to_file(

extension = guess_extension(guess_type(meta_data)[0])
extension = EXTENSION_MAP.get(extension, extension) # Only necessary for python 3.6


# Deprecated
if metadata:
return (file_data, meta_data)

return (file_data, extension) if get_file_extension else file_data

# Deprecated
def metadata_to_filetype(metadata: str) -> str:
"""
# Deprecated
>>> metadata_to_filetype(metadata: str) -> str

Extracts the file type from the metadata string.

Parameters
----------
metadata : str
A metadata string typically in the form `Data:<MIME type>;base64,`

Returns
-------
* `str` :
The extracted file type (e.g., 'csv'). For a Microsoft Excel file, it returns 'xlsx'.

Examples
--------
>>> input_file = inputs["input_file"]
>>> open_file, metadata = msc.input_to_file(input_file, metadata=True)
>>> file_type = msc.metadata_to_filetype(metadata)
>>> print(file_type)
'jpeg'
"""
warn("metadata_to_filetype is deprecated. Use guess_extension instead.", DeprecationWarning)

# Extract mime type from metadata
match = re.search(r"/(.+);base64,", metadata)
file_type = match[1] if match else ""

# Convert the file type to a more common format
if file_type == "vnd.openxmlformats-officedocument.spreadsheetml.sheet":
file_type = "xlsx"

return file_type
8 changes: 7 additions & 1 deletion mecsimcalc/file_utils/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def file_to_PIL(file: io.BytesIO) -> Image.Image:


def input_to_PIL(
input_file: str, get_file_extension: bool = False
input_file: str, get_file_extension: bool = False, get_file_type: bool = False
) -> Union[Image.Image, Tuple[Image.Image, str]]:
"""
>>> input_to_PIL(
Expand Down Expand Up @@ -84,6 +84,9 @@ def input_to_PIL(

(image is now ready to be used with Pillow functions)
"""
# get_file_type is deprecated
get_file_extension = get_file_extension or get_file_type

# Get file extension from metadata
file_data = input_to_file(input_file)
image = file_to_PIL(file_data)
Expand All @@ -103,6 +106,7 @@ def print_image(
download: bool = False,
download_text: str = "Download Image",
download_file_name: str = "myimg",
download_file_type: str = None,
) -> Union[str, Tuple[str, str]]:
"""
>>> print_image(
Expand Down Expand Up @@ -158,6 +162,8 @@ def print_image(
}

"""
# download_file_type is deprecated

# get metadata (file type) from image
dummy_filename = f"dummy.{image.format.lower()}"
mime_type, _ = guess_type(dummy_filename)
Expand Down
5 changes: 4 additions & 1 deletion mecsimcalc/file_utils/spreadsheet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def file_to_dataframe(file: io.BytesIO) -> pd.DataFrame:


def input_to_dataframe(
input_file: str, get_file_extension: bool = False
input_file: str, get_file_extension: bool = False, get_file_type: bool = False
) -> Union[pd.DataFrame, Tuple[pd.DataFrame, str]]:
"""
>>> input_to_dataframe(
Expand Down Expand Up @@ -83,6 +83,9 @@ def input_to_dataframe(
0 1 2 3
1 4 5 6
"""
# get_file_type is deprecated
get_file_extension = get_file_extension or get_file_type

# converts input file into a dataframe
file_data, file_extension = input_to_file(input_file, get_file_extension=True)

Expand Down