Skip to content

Commit bbfe456

Browse files
authored
Merge pull request #23 from ensan-hcl/arraywrite
Add implementation of `write` for list of custard
2 parents aca0b86 + 1b06ef5 commit bbfe456

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

python/source/custard.py

+44-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class CustardJSONEncoder(json.JSONEncoder):
1010
def default(self, o):
1111
if isinstance(o, Custard):
1212
return o.json()
13-
13+
if isinstance(o, CustardList):
14+
return to_json_list(o.custards)
1415
# 他の型はdefaultのエンコード方式を使用
1516
return super(CustardJSONEncoder, self).default(o)
1617

@@ -79,8 +80,26 @@ def json(self) -> dict:
7980
"interface": self.interface.json()
8081
}
8182

83+
def write(self, to: str = None, name: str = None, allow_overwrite: bool = False):
84+
"""
85+
Custardファイルを出力する関数
86+
Parameters
87+
----------
88+
to: str = None
89+
出力先のパスを指定
90+
name: str = None
91+
出力先のパスを指定しない場合にファイル名を指定
92+
allow_overwrite: bool = False
93+
上書きを許可するか否か
94+
"""
95+
pass
96+
8297

83-
def write(self, to: str = None, name: str = None, allow_overwrite: bool = False) -> dict:
98+
class CustardList(object):
99+
def __init__(self, custards: list[Custard]):
100+
self.custards = custards
101+
102+
def write(self, to: str = None, name: str = None, allow_overwrite: bool = False):
84103
"""
85104
Custardファイルを出力する関数
86105
Parameters
@@ -92,18 +111,26 @@ def write(self, to: str = None, name: str = None, allow_overwrite: bool = False)
92111
allow_overwrite: bool = False
93112
上書きを許可するか否か
94113
"""
95-
if to is None:
96-
result_directory_path = Path('__file__').resolve().parent / 'results'
97-
if not result_directory_path.exists():
98-
result_directory_path.mkdir()
99-
if name is None:
100-
name = 'custard'
101-
target = result_directory_path / f'{name}.json'
102-
number = 1
103-
while target.exists() and not allow_overwrite:
104-
number += 1
105-
target = result_directory_path / f'{name}#{number}.json'
106-
to = str(target)
107-
108-
with open(f"{to}", mode="w") as f:
109-
f.write(json.dumps(self, cls=CustardJSONEncoder, ensure_ascii=False))
114+
pass
115+
116+
117+
def write(self, to: str = None, name: str = None, allow_overwrite: bool = False):
118+
if to is None:
119+
result_directory_path = Path('__file__').resolve().parent / 'results'
120+
if not result_directory_path.exists():
121+
result_directory_path.mkdir()
122+
if name is None:
123+
name = 'custard'
124+
target = result_directory_path / f'{name}.json'
125+
number = 1
126+
while target.exists() and not allow_overwrite:
127+
number += 1
128+
target = result_directory_path / f'{name}#{number}.json'
129+
to = str(target)
130+
131+
with open(f"{to}", mode="w") as f:
132+
f.write(json.dumps(self, cls=CustardJSONEncoder, ensure_ascii=False))
133+
134+
135+
Custard.write = write
136+
CustardList.write = write

swift/sources/CustardKit.swift

+7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ public struct Custard: Codable, Equatable {
107107
}
108108
}
109109

110+
extension Array where Element == Custard {
111+
public func write(to url: URL) throws {
112+
let encoded_data = try JSONEncoder().encode(self)
113+
try encoded_data.write(to: url)
114+
}
115+
}
116+
110117
/// - インターフェースのキーのスタイルです
111118
/// - style of keys
112119
public enum CustardInterfaceStyle: String, Codable {

0 commit comments

Comments
 (0)