Skip to content
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

Add ini file processor #329

Merged
merged 12 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions docs/task_on_kart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ It is also possible to specify a file format other than pkl. The supported file
- .feather
- .png
- .jpg
- .ini


If dump something other than the above, can use :func:`~gokart.TaskOnKart.make_model_target`.
Expand Down
18 changes: 18 additions & 0 deletions gokart/file_processor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import configparser
import os
import pickle
import xml.etree.ElementTree as ET
Expand Down Expand Up @@ -266,6 +267,22 @@ def dump(self, obj, file):
dump_obj.to_feather(file.name)


class IniFileProcessor(FileProcessor):
maronuu marked this conversation as resolved.
Show resolved Hide resolved

def format(self):
return None

def load(self, file):
config = configparser.ConfigParser()
maronuu marked this conversation as resolved.
Show resolved Hide resolved
config.read_file(file)
return config

def dump(self, obj, file):
assert isinstance(obj, configparser.ConfigParser), \
f'requires configparser.ConfigParser, but {type(obj)} is passed.'
obj.write(file)


def make_file_processor(file_path: str, store_index_in_feather: bool) -> FileProcessor:
extension2processor = {
'.txt': TextFileProcessor(),
Expand All @@ -280,6 +297,7 @@ def make_file_processor(file_path: str, store_index_in_feather: bool) -> FilePro
'.feather': FeatherFileProcessor(store_index_in_feather=store_index_in_feather),
'.png': BinaryFileProcessor(),
'.jpg': BinaryFileProcessor(),
'.ini': IniFileProcessor(),
}

extension = os.path.splitext(file_path)[1]
Expand Down
15 changes: 15 additions & 0 deletions test/test_target.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import configparser
import io
import os
import shutil
Expand Down Expand Up @@ -127,6 +128,20 @@ def test_save_and_load_feather_without_store_index_in_feather(self):

pd.testing.assert_frame_equal(loaded, obj)

def test_save_and_load_ini(self):
obj = configparser.ConfigParser()
obj['DEFAULT'] = {'a': '1', 'b': 'yes', 'c': 2}
obj['example'] = {}
obj['example']['d'] = 'foo'
obj['example']['e'] = 'true'
file_path = os.path.join(_get_temporary_directory(), 'test.ini')

target = make_target(file_path=file_path, unique_id=None)
target.dump(obj)
loaded = target.load()

self.assertEqual(loaded, obj)

def test_last_modified_time(self):
obj = pd.DataFrame(dict(a=[1, 2], b=[3, 4]))
file_path = os.path.join(_get_temporary_directory(), 'test.csv')
Expand Down