def deserialize(cls, value: serialization_model, inflator: Inflator) -> DistributionModel: return cls( tuple( tuple( ConstrainedNode.deserialize(node, inflator) for node in trap) for trap in value['traps']))
def post(self, request, *args, **kwargs): serializer: serializers.ParseTrapSerializer = self.get_serializer( data=request.data) serializer.is_valid(raise_exception=True) value = serializer.validated_data.get('weight', 1) try: constrained_node = ConstrainedNode( node=PrintingTreeParser( db, allow_volatile=True, ).parse(serializer.validated_data['query']), groups=[ group.rstrip().lstrip() for group in serializer.validated_data.get('groups', '').split(',') ], value=value if value is not None else 1, ) except PrintingTreeParserException as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST) return Response(orpserialize.ConstrainedNodeOrpSerializer.serialize( constrained_node), status=status.HTTP_200_OK, content_type='application/json')
def serialize(cls, constrained_node: ConstrainedNode) -> compacted_model: return { 'id': constrained_node.persistent_hash(), 'value': constrained_node.value, 'groups': list(constrained_node.groups), 'node': NodeSerializer.serialize( constrained_node.node ) }
def _fetch(self, start_column: int) -> t.List[ConstrainedNode]: exceptions = [] constrained_cubeables = [] for row in self._sheet_client.read_sheet( self.SHEET_NAME, start_column=start_column, start_row=4, end_column=start_column + 3, end_row=1000, ): if not row: print('Empty row in trapables input!') # TODO should probably be a runtime warning, but that requires logging... continue amount_cell, printings_cell, value_cell = row[:3] groups_cell = row[3] if len(row) > 3 else '' try: amount = int(amount_cell) except ValueError: amount = 1 try: node = self._printing_tree_parser.parse(printings_cell) except PrintingTreeParserException as e: exceptions.append(e) continue try: # value = self._value_value_map[int(value_cell)] value = int(value_cell) except (ValueError, KeyError) as e: exceptions.append(e) continue try: groups = self._parse_groups(groups_cell) except ConstrainedCubeablesFetchException as e: exceptions.append(e) continue for _ in range(amount): constrained_cubeables.append( ConstrainedNode( node=node, value=value, groups=groups, )) if exceptions: raise ConstrainedCubeablesFetchException(exceptions) return constrained_cubeables
def deserialize(cls, value: serialization_model, inflator: Inflator) -> 'Serializeable': return cls( ConstrainedNode.deserialize(value['before'], inflator), ConstrainedNode.deserialize(value['after'], inflator), )
def deserialize(cls, value: serialization_model, inflator: Inflator) -> 'Serializeable': return cls( trap=Trap.deserialize(value['trap'], inflator), node=ConstrainedNode.deserialize(value['node'], inflator), )
def deserialize(cls, value: serialization_model, inflator: Inflator) -> NodeCubeChange: return cls(ConstrainedNode.deserialize(value['node'], inflator))
def as_constrained_node(self): return ConstrainedNode( value=self._value, node=self._node, groups=self._groups, )
def test(): db = Loader.load() strategy = JsonId(db) cube = CubeLoader(db).load() constrained_nodes = NodeCollection( ConstrainedNodeFetcher(db).fetch_garbage()) groups = GroupMap(_GROUP_WEIGHTS) # s = '{"cube_delta": {}, "nodes_delta": {"nodes": []}}' # patch = strategy.deserialize(CubePatch, s) patch = CubePatch( CubeDeltaOperation({ db.cardboards['Brainstorm'].from_expansion('ICE'): -1, db.cardboards['Brainstorm'].from_expansion('EMA'): 1, # Trap( # AllNode( # ( # db.cardboards['Brainstorm'].from_expansion('ICE'), # db.cardboards['Web'].from_expansion('LEA'), # ) # ), # intention_type=IntentionType.SYNERGY, # ): 2 }), NodesDeltaOperation({ # ConstrainedNode( # node = AllNode( # ( # db.cardboards['Web'].from_expansion('LEA'), # ) # ), # groups = ['ok', 'lmao'], # value = 2, # ): 1, ConstrainedNode( node=AllNode(( db.cardboards['Brainstorm'].from_expansion('ICE'), db.cardboards['Web'].from_expansion('LEA'), )), groups=['lolHAHA'], value=1, ): 1, })) print(patch) meta_cube = MetaCube(cube, constrained_nodes, groups) verbose_patch = patch.as_verbose(meta_cube) print(verbose_patch) updater = CubeUpdater(meta_cube, patch) print(updater) report = UpdateReport(updater) for notification in report.notifications: print(notification.title + '\n' + notification.content + '\n\n')