-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from yesinkim/feature/handling-txt
Feature/handling txt
- Loading branch information
Showing
6 changed files
with
238 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from ezpkl.pickling import load_pkl, save_pkl | ||
from ezpkl.texting import save_txt, load_txt | ||
|
||
__all__ = [save_pkl, load_pkl] | ||
__all__ = [save_pkl, load_pkl, save_txt, load_txt] | ||
|
||
__version__ = "0.1.7" | ||
__version__ = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,29 @@ | ||
ingredients = [ | ||
'apricot', 'broccoli', 'cherry', 'date', 'elderberry', | ||
'fig', 'grape', 'honeydew', 'iceberg', 'jalapeno', | ||
'kiwi', 'lime', 'mushroom', 'nectarine', 'olive', | ||
'pumpkin', 'quince', 'raspberry', 'strawberry', 'tangerine', | ||
'uglifruit', 'vanilla', 'watermelon', 'xigua', 'yam', 'zucchini' | ||
] | ||
"apricot", | ||
"broccoli", | ||
"cherry", | ||
"date", | ||
"elderberry", | ||
"fig", | ||
"grape", | ||
"honeydew", | ||
"iceberg", | ||
"jalapeno", | ||
"kiwi", | ||
"lime", | ||
"mushroom", | ||
"nectarine", | ||
"olive", | ||
"pumpkin", | ||
"quince", | ||
"raspberry", | ||
"strawberry", | ||
"tangerine", | ||
"uglifruit", | ||
"vanilla", | ||
"watermelon", | ||
"xigua", | ||
"yam", | ||
"zucchini" "jamon", | ||
"melon", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Make your life simply""" | ||
import inspect | ||
import random | ||
from typing import Union | ||
|
||
from ezpkl.refrigerator import ingredients | ||
|
||
|
||
def save_txt(data, file_name: str = None, separator: str = "\n") -> None: | ||
"""Saves data to a text file. | ||
Args: | ||
data: The data to be saved. Can be a string, list, or any other object that can be converted to a string. | ||
file_name: The name of the text file. If not provided, the function attempts to determine the variable name from the previous frame's local variables. | ||
separator: The separator to use between items in a list. Defaults to "\n", which means items will be joined with a newline character. | ||
Returns: | ||
None | ||
Raises: | ||
FileNotFoundError: If the file cannot be found. | ||
PermissionError: If there is a permission error while saving the file. | ||
""" | ||
var_name = None | ||
frame = inspect.currentframe() | ||
try: | ||
if not file_name: | ||
previous_frame = frame.f_back | ||
for name, value in previous_frame.f_locals.items(): | ||
if value is data: | ||
var_name = name | ||
break | ||
if var_name: | ||
file_name = f"{var_name}.txt" | ||
else: | ||
file_name = f"{random.choice(ingredients)}.txt" | ||
print("I couldn't find the variable name, but here's some ingredient for you instead!") | ||
elif not file_name.endswith(".txt"): | ||
file_name = f"{file_name}.txt" | ||
|
||
try: | ||
with open(file_name, "w") as file: | ||
if isinstance(data, list): | ||
if separator is None: | ||
file.write("".join(str(item) for item in data)) | ||
else: | ||
file.write(separator.join(str(item) for item in data)) | ||
else: | ||
file.write(str(data)) | ||
print(f"Data saved to '{file_name}'.") | ||
except (FileNotFoundError, PermissionError) as e: | ||
print(f"Error saving data: {str(e)}") | ||
finally: | ||
del frame | ||
|
||
|
||
def load_txt(filename: str) -> Union[str, None]: | ||
"""Loads data from a text file. | ||
Args: | ||
filename: The name of the text file. | ||
Returns: | ||
The data loaded from the file, or None if the file is not found. | ||
Raises: | ||
FileNotFoundError: If the file cannot be found. | ||
""" | ||
try: | ||
with open(filename, "r") as file: | ||
return file.read() | ||
except FileNotFoundError: | ||
print(f"File '{filename}' not found.") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
|
||
from ezpkl import load_txt, save_txt | ||
|
||
|
||
def test_save_txt_string(): | ||
data = "안뇽 세상!" | ||
save_txt(data, file_name="test_string") | ||
assert load_txt("test_string.txt") == data | ||
|
||
|
||
def test_save_txt_list(): | ||
data = ["apple", "banana", "cherry"] | ||
save_txt(data, file_name="test_list") | ||
assert load_txt("test_list.txt") == "\n".join(data) | ||
|
||
|
||
def test_save_txt_invalid_filename(): | ||
data = "춘의 쾌유를 빕니다" | ||
save_txt(data, file_name="test.txt") | ||
assert load_txt("test.txt") == data | ||
|
||
|
||
def test_load_txt_nonexistent_file(): | ||
filename = "nonexistent_file.txt" | ||
assert load_txt(filename) is None | ||
|
||
|
||
def test_save_txt_no_filename_with_variable(): | ||
data = "This is a test." | ||
save_txt(data) | ||
assert load_txt("data.txt") == data | ||
|
||
|
||
def test_save_txt_with_separator(): | ||
data = ["치유", "치유", "빔", "!", "✨"] | ||
save_txt(data, separator=None) | ||
assert load_txt("data.txt") == "치유치유빔!✨" |