Skip to content

Commit

Permalink
Merge pull request #23 from ensan-hcl/arraywrite
Browse files Browse the repository at this point in the history
Add implementation of `write` for list of custard
  • Loading branch information
ensan-hcl authored Mar 28, 2021
2 parents aca0b86 + 1b06ef5 commit bbfe456
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
61 changes: 44 additions & 17 deletions python/source/custard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class CustardJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, Custard):
return o.json()

if isinstance(o, CustardList):
return to_json_list(o.custards)
# 他の型はdefaultのエンコード方式を使用
return super(CustardJSONEncoder, self).default(o)

Expand Down Expand Up @@ -79,8 +80,26 @@ def json(self) -> dict:
"interface": self.interface.json()
}

def write(self, to: str = None, name: str = None, allow_overwrite: bool = False):
"""
Custardファイルを出力する関数
Parameters
----------
to: str = None
出力先のパスを指定
name: str = None
出力先のパスを指定しない場合にファイル名を指定
allow_overwrite: bool = False
上書きを許可するか否か
"""
pass


def write(self, to: str = None, name: str = None, allow_overwrite: bool = False) -> dict:
class CustardList(object):
def __init__(self, custards: list[Custard]):
self.custards = custards

def write(self, to: str = None, name: str = None, allow_overwrite: bool = False):
"""
Custardファイルを出力する関数
Parameters
Expand All @@ -92,18 +111,26 @@ def write(self, to: str = None, name: str = None, allow_overwrite: bool = False)
allow_overwrite: bool = False
上書きを許可するか否か
"""
if to is None:
result_directory_path = Path('__file__').resolve().parent / 'results'
if not result_directory_path.exists():
result_directory_path.mkdir()
if name is None:
name = 'custard'
target = result_directory_path / f'{name}.json'
number = 1
while target.exists() and not allow_overwrite:
number += 1
target = result_directory_path / f'{name}#{number}.json'
to = str(target)

with open(f"{to}", mode="w") as f:
f.write(json.dumps(self, cls=CustardJSONEncoder, ensure_ascii=False))
pass


def write(self, to: str = None, name: str = None, allow_overwrite: bool = False):
if to is None:
result_directory_path = Path('__file__').resolve().parent / 'results'
if not result_directory_path.exists():
result_directory_path.mkdir()
if name is None:
name = 'custard'
target = result_directory_path / f'{name}.json'
number = 1
while target.exists() and not allow_overwrite:
number += 1
target = result_directory_path / f'{name}#{number}.json'
to = str(target)

with open(f"{to}", mode="w") as f:
f.write(json.dumps(self, cls=CustardJSONEncoder, ensure_ascii=False))


Custard.write = write
CustardList.write = write
7 changes: 7 additions & 0 deletions swift/sources/CustardKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public struct Custard: Codable, Equatable {
}
}

extension Array where Element == Custard {
public func write(to url: URL) throws {
let encoded_data = try JSONEncoder().encode(self)
try encoded_data.write(to: url)
}
}

/// - インターフェースのキーのスタイルです
/// - style of keys
public enum CustardInterfaceStyle: String, Codable {
Expand Down

0 comments on commit bbfe456

Please sign in to comment.