nodes: 0, module; 1, function_definition; 2, function_name:format_script; 3, parameters; 4, block; 5, identifier:sensor_graph; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, for_statement; 13, for_statement; 14, expression_statement; 15, expression_statement; 16, for_statement; 17, comment:# If we have an app tag and version set program them in; 18, expression_statement; 19, expression_statement; 20, if_statement; 21, expression_statement; 22, return_statement; 23, comment:"""Create a binary script containing this sensor graph.
  
      This function produces a repeatable script by applying a known sorting
      order to all constants and config variables when iterating over those
      dictionaries.
  
      Args:
          sensor_graph (SensorGraph): the sensor graph that we want to format
  
      Returns:
          bytearray: The binary script data.
  
      """; 24, assignment; 25, call; 26, call; 27, call; 28, identifier:node; 29, attribute; 30, block; 31, identifier:streamer; 32, attribute; 33, block; 34, pattern_list; 35, call; 36, block; 37, call; 38, call; 39, identifier:slot; 40, call; 41, block; 42, assignment; 43, assignment; 44, comparison_operator:app_tag is not None; 45, block; 46, assignment; 47, call; 48, identifier:records; 49, list; 50, attribute; 51, argument_list; 52, attribute; 53, argument_list; 54, attribute; 55, argument_list; 56, identifier:sensor_graph; 57, identifier:nodes; 58, expression_statement; 59, identifier:sensor_graph; 60, identifier:streamers; 61, expression_statement; 62, identifier:stream; 63, identifier:value; 64, identifier:sorted; 65, argument_list; 66, expression_statement; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, identifier:sorted; 72, argument_list; 73, for_statement; 74, identifier:app_tag; 75, call; 76, identifier:app_version; 77, call; 78, identifier:app_tag; 79, None; 80, expression_statement; 81, identifier:script; 82, call; 83, attribute; 84, argument_list; 85, identifier:records; 86, identifier:append; 87, call; 88, identifier:records; 89, identifier:append; 90, call; 91, identifier:records; 92, identifier:append; 93, call; 94, call; 95, call; 96, call; 97, keyword_argument; 98, call; 99, identifier:records; 100, identifier:append; 101, call; 102, identifier:records; 103, identifier:append; 104, call; 105, attribute; 106, keyword_argument; 107, identifier:config_id; 108, call; 109, block; 110, attribute; 111, argument_list; 112, attribute; 113, argument_list; 114, call; 115, identifier:UpdateScript; 116, argument_list; 117, identifier:script; 118, identifier:encode; 119, identifier:SetGraphOnlineRecord; 120, argument_list; 121, identifier:ClearDataRecord; 122, argument_list; 123, identifier:ResetGraphRecord; 124, argument_list; 125, attribute; 126, argument_list; 127, attribute; 128, argument_list; 129, attribute; 130, argument_list; 131, identifier:key; 132, lambda; 133, attribute; 134, argument_list; 135, identifier:PersistGraphRecord; 136, argument_list; 137, identifier:ClearConfigVariablesRecord; 138, argument_list; 139, identifier:sensor_graph; 140, identifier:config_database; 141, identifier:key; 142, lambda; 143, identifier:sorted; 144, argument_list; 145, expression_statement; 146, expression_statement; 147, expression_statement; 148, attribute; 149, identifier:get; 150, string; 151, attribute; 152, identifier:get; 153, string; 154, attribute; 155, argument_list; 156, identifier:records; 157, False; 158, keyword_argument; 159, keyword_argument; 160, keyword_argument; 161, identifier:records; 162, identifier:append; 163, call; 164, identifier:records; 165, identifier:append; 166, call; 167, attribute; 168, identifier:items; 169, lambda_parameters; 170, call; 171, identifier:records; 172, identifier:append; 173, call; 174, keyword_argument; 175, lambda_parameters; 176, call; 177, subscript; 178, assignment; 179, assignment; 180, call; 181, identifier:sensor_graph; 182, identifier:metadata_database; 183, string_content:app_tag; 184, identifier:sensor_graph; 185, identifier:metadata_database; 186, string_content:app_version; 187, identifier:records; 188, identifier:append; 189, call; 190, identifier:address; 191, integer:8; 192, identifier:address; 193, integer:8; 194, identifier:address; 195, integer:8; 196, identifier:AddNodeRecord; 197, argument_list; 198, identifier:AddStreamerRecord; 199, argument_list; 200, identifier:sensor_graph; 201, identifier:constant_database; 202, identifier:x; 203, attribute; 204, argument_list; 205, identifier:SetConstantRecord; 206, argument_list; 207, identifier:address; 208, integer:8; 209, identifier:x; 210, attribute; 211, argument_list; 212, attribute; 213, identifier:slot; 214, pattern_list; 215, subscript; 216, identifier:byte_value; 217, call; 218, attribute; 219, argument_list; 220, identifier:SetDeviceTagRecord; 221, argument_list; 222, call; 223, keyword_argument; 224, identifier:streamer; 225, keyword_argument; 226, subscript; 227, identifier:encode; 228, identifier:stream; 229, identifier:value; 230, keyword_argument; 231, identifier:x; 232, identifier:encode; 233, identifier:sensor_graph; 234, identifier:config_database; 235, identifier:config_type; 236, identifier:value; 237, subscript; 238, identifier:config_id; 239, identifier:_convert_to_bytes; 240, argument_list; 241, identifier:records; 242, identifier:append; 243, call; 244, keyword_argument; 245, keyword_argument; 246, identifier:str; 247, argument_list; 248, identifier:address; 249, integer:8; 250, identifier:address; 251, integer:8; 252, identifier:x; 253, integer:0; 254, identifier:address; 255, integer:8; 256, attribute; 257, identifier:slot; 258, identifier:config_type; 259, identifier:value; 260, identifier:SetConfigRecord; 261, argument_list; 262, identifier:app_tag; 263, identifier:app_tag; 264, identifier:app_version; 265, identifier:app_version; 266, identifier:node; 267, identifier:sensor_graph; 268, identifier:config_database; 269, identifier:slot; 270, identifier:config_id; 271, identifier:byte_value
code: def format_script(sensor_graph):
      """Create a binary script containing this sensor graph.
  
      This function produces a repeatable script by applying a known sorting
      order to all constants and config variables when iterating over those
      dictionaries.
  
      Args:
          sensor_graph (SensorGraph): the sensor graph that we want to format
  
      Returns:
          bytearray: The binary script data.
  
      """
  
      records = []
  
      records.append(SetGraphOnlineRecord(False, address=8))
      records.append(ClearDataRecord(address=8))
      records.append(ResetGraphRecord(address=8))
  
      for node in sensor_graph.nodes:
          records.append(AddNodeRecord(str(node), address=8))
  
      for streamer in sensor_graph.streamers:
          records.append(AddStreamerRecord(streamer, address=8))
  
      for stream, value in sorted(sensor_graph.constant_database.items(), key=lambda x: x[0].encode()):
          records.append(SetConstantRecord(stream, value, address=8))
  
      records.append(PersistGraphRecord(address=8))
  
      records.append(ClearConfigVariablesRecord())
      for slot in sorted(sensor_graph.config_database, key=lambda x: x.encode()):
          for config_id in sorted(sensor_graph.config_database[slot]):
              config_type, value = sensor_graph.config_database[slot][config_id]
              byte_value = _convert_to_bytes(config_type, value)
  
              records.append(SetConfigRecord(slot, config_id, byte_value))
  
      # If we have an app tag and version set program them in
      app_tag = sensor_graph.metadata_database.get('app_tag')
      app_version = sensor_graph.metadata_database.get('app_version')
  
      if app_tag is not None:
          records.append(SetDeviceTagRecord(app_tag=app_tag, app_version=app_version))
  
      script = UpdateScript(records)
      return script.encode()