def _format_value(self, context, element, value) -> "str or None": """Format value in FDS notation for me. On error raise BFException.""" # Expected output: # ID='example' or PI=3.14 or COLOR=3,4,5 # Free parameters (no self.fds_label) needs special treatment in fds.props or fds.props_geometry if value is None: return None # If value is not an iterable, then put it in a tuple if not isiterable(value): values = tuple((value,)) else: values = value # Check first element of the iterable and choose formatting if isinstance(values[0], bool): value = ",".join(value and ".TRUE." or ".FALSE." for value in values) elif isinstance(values[0], int): value = ",".join(str(value) for value in values) elif isinstance(values[0], float): value = ",".join("{:.{}f}".format(value, self.precision) for value in values) else: value = ",".join("'{}'".format(value) for value in values) return "=".join((self.fds_label, value))
def _format(self, context, element, my_res, children_res) -> "str or None": """Format to FDS notation. On error raise BFException.""" # Expected output: # ! OBST: Message < my_res.labels # ! OBST: ID: Message + children labels (from self.bf_props) # ! OBST: XB: Message # &OBST ID='example' XB=... /\n < body < c_multivalue[0] + c_value # &OBST ID='example' XB=... /\n c_multivalue[1] + c_value # &XXXX P1=... /\n < my_res.value # Append children messages to my messages (children are self.bf_props) my_res.msgs.extend(label for child_res in children_res for label in child_res.labels) # Search and extract the first (and should be only) multivalue from children values child_multivalues = None for child_res in children_res: if isiterable(child_res.value): # Set the multivalue and remove multivalue child_res from single value children_res child_multivalues = child_res.value children_res.remove(child_res) # Each multivalue contains its multi ID, then remove ordinary single ID sent by "bf_id" children_res.remove(children_res["bf_id"]) break # Set fds_label: use self.fds_label or last children_res value, that should be bf_free # When using last children value, remove child from BFList if self.fds_label: fds_label = self.fds_label else: fds_label = children_res.pop().value # Join children values children_value = " ".join(child_res.value for child_res in children_res if child_res.value) # Build body: When multivalue exists, ID is embedded into each child_multivalue; # else ID is embedded into children_value if child_multivalues: body = "".join("&{} {} {} /\n".format( fds_label, child_multivalue, children_value, ) for child_multivalue in child_multivalues ) else: body = "&{} {} /\n".format(fds_label, children_value) # Return return "".join(( fds_format.to_comment(my_res.labels), body, my_res.value or str(), # my_res.value could be None ))