Skip to content

Commit f4ab4bc

Browse files
authored
Merge pull request #49 from ourstudio-se/48-plog-to-json-always-returns-id
to_json ID is returning only when explicitly defined
2 parents 19197f6 + cd5cfe3 commit f4ab4bc

2 files changed

Lines changed: 72 additions & 37 deletions

File tree

puan/logic/plog/__init__.py

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,7 @@ def to_json(self) -> dict:
658658
-------
659659
out : dict
660660
"""
661-
return {
662-
'id': self.id,
661+
d = {
663662
'type': self.__class__.__name__,
664663
'propositions': list(
665664
map(
@@ -669,6 +668,10 @@ def to_json(self) -> dict:
669668
),
670669
'value': self.value
671670
}
671+
if not self.generated_id:
672+
d['id'] = self.id
673+
674+
return d
672675

673676
def to_b64(self, str_decoding: str = 'utf8') -> str:
674677

@@ -810,17 +813,9 @@ def to_json(self) -> dict:
810813
-------
811814
out : dict
812815
"""
813-
return {
814-
'id': self.id,
815-
'type': self.__class__.__name__,
816-
'propositions': list(
817-
map(
818-
operator.methodcaller("to_json"),
819-
self.propositions
820-
)
821-
),
822-
'value': -1*self.value
823-
}
816+
d = super().to_json()
817+
d['value'] = -1*self.value
818+
return d
824819

825820
class All(AtLeast):
826821

@@ -871,16 +866,15 @@ def to_json(self) -> dict:
871866
-------
872867
out : dict
873868
"""
874-
return {
875-
'id': self.id,
876-
'type': self.__class__.__name__,
877-
'propositions': list(
878-
map(
879-
operator.methodcaller("to_json"),
880-
self.propositions
881-
)
882-
),
883-
}
869+
d = super().to_json()
870+
del d['value']
871+
d['propositions'] = list(
872+
map(
873+
operator.methodcaller("to_json"),
874+
self.propositions
875+
)
876+
)
877+
return d
884878

885879
class Any(AtLeast):
886880

@@ -931,16 +925,15 @@ def to_json(self) -> dict:
931925
-------
932926
out : dict
933927
"""
934-
return {
935-
'id': self.id,
936-
'type': self.__class__.__name__,
937-
'propositions': list(
938-
map(
939-
operator.methodcaller("to_json"),
940-
self.propositions
941-
)
942-
),
943-
}
928+
d = super().to_json()
929+
del d['value']
930+
d['propositions'] = list(
931+
map(
932+
operator.methodcaller("to_json"),
933+
self.propositions
934+
)
935+
)
936+
return d
944937

945938
class Imply(Any):
946939

@@ -1002,12 +995,14 @@ def to_json(self) -> dict:
1002995
-------
1003996
out : dict
1004997
"""
1005-
return {
1006-
'id': self.id,
998+
d = {
1007999
'type': self.__class__.__name__,
10081000
'condition': self.condition.negate().to_json(),
10091001
'consequence': self.consequence.to_json(),
10101002
}
1003+
if not self.generated_id:
1004+
d['id'] = self.id
1005+
return d
10111006

10121007
@staticmethod
10131008
def from_cicJE(data: dict, id_ident: str = "id") -> "Imply":
@@ -1131,8 +1126,7 @@ def to_json(self) -> dict:
11311126
-------
11321127
out : dict
11331128
"""
1134-
return {
1135-
'id': self.id,
1129+
d = {
11361130
'type': self.__class__.__name__,
11371131
'propositions': list(
11381132
map(
@@ -1141,6 +1135,9 @@ def to_json(self) -> dict:
11411135
)
11421136
) if len(self.propositions) > 0 else [],
11431137
}
1138+
if not self.generated_id:
1139+
d['id'] = self.id
1140+
return d
11441141

11451142
class Not():
11461143

@@ -1213,6 +1210,28 @@ def from_json(data: dict, class_map) -> "XNor":
12131210
def from_list(propositions: list, variable: typing.Union[str, puan.variable] = None):
12141211
return XNor(*propositions, variable=variable)
12151212

1213+
def to_json(self) -> dict:
1214+
1215+
"""
1216+
Returns proposition as a readable json.
1217+
1218+
Returns
1219+
-------
1220+
out : dict
1221+
"""
1222+
d = {
1223+
'type': self.__class__.__name__,
1224+
'propositions': list(
1225+
map(
1226+
operator.methodcaller("to_json"),
1227+
self.propositions[0].propositions
1228+
)
1229+
) if len(self.propositions) > 0 else [],
1230+
}
1231+
if not self.generated_id:
1232+
d['id'] = self.id
1233+
return d
1234+
12161235
def from_json(data: dict, class_map: list = [puan.variable,AtLeast,AtMost,All,Any,Xor,Not,XNor,Imply]) -> typing.Any:
12171236

12181237
"""

tests/test_puan.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ def test_model_json_conversion(propositions):
190190
_model = cc.StingyConfigurator.from_json(model.to_json())
191191
assert model == _model
192192

193+
@given(cc_proposition_strategy())
194+
def test_to_from_b64(proposition):
195+
string = proposition.to_b64()
196+
_proposition = pg.from_b64(string)
197+
assert proposition.id == _proposition.id
198+
assert proposition.bounds == _proposition.bounds
199+
200+
@given(proposition_strategy())
201+
def test_json_conversion_id_should_be_returned_if_explicitly_defined(proposition):
202+
json_model = pg.from_json(proposition.to_json()).to_json()
203+
# generated id (i.e. no ID was explicitly defined) implies that there shouldn't be an ID in the json
204+
assert not (proposition.generated_id and 'id' in json_model)
205+
193206
def test_json_conversion_special_cases():
194207

195208
model = cc.StingyConfigurator(
@@ -1505,6 +1518,9 @@ def test_json_conversion():
15051518
converted = cc.StingyConfigurator.from_json(model.to_json())
15061519
assert model == converted
15071520

1521+
json_model = {"type": "All", "propositions": [{"id": "x", "bounds": {"lower": -10, "upper": 10}}]}
1522+
assert json_model == pg.from_json(json_model).to_json()
1523+
15081524
def test_xnor_proposition():
15091525

15101526
actual_model = pg.XNor("x","y","z") # <- meaning none or at least 2

0 commit comments

Comments
 (0)