code: def term_to_binary(term, compressed=False):
      
      data_uncompressed = _term_to_binary(term)
      if compressed is False:
          return b_chr(_TAG_VERSION) + data_uncompressed
      else:
          if compressed is True:
              compressed = 6
          if compressed < 0 or compressed > 9:
              raise InputException('compressed in [0..9]')
          data_compressed = zlib.compress(data_uncompressed, compressed)
          size_uncompressed = len(data_uncompressed)
          if size_uncompressed > 4294967295:
              raise OutputException('uint32 overflow')
          return (
              b_chr(_TAG_VERSION) + b_chr(_TAG_COMPRESSED_ZLIB) +
              struct.pack(b'>I', size_uncompressed) + data_compressed
          )
docs: Encode Python types into Erlang terms in binary data