Esempio n. 1
0
    def test_serialize_detects_cycles(self):
        outer = [1]
        inner = [2]
        outer.append(inner)
        outer.append(inner)
        # No cycle yet, even though `inner` is used multiple times.
        self.assertEqual(serialization.serialize(outer), '[1, [2], [2]]')

        # Introduce cycle.
        inner.append(outer)
        with self.assertRaises(ValueError):
            serialization.serialize(outer)
Esempio n. 2
0
  def test_round_trip(self):
    to_serialize = [
        [1, 2, 3],
        [[1.2, 3.4], [5.6, 7.8]],
        'Description with \'quotes" and unicode \u1234',
        tf.constant([1, 2, 3], dtype=tf.int16),
        tf.constant(4.5),
        tf.constant('tf-coder'),
        tf.float16,
        tf.string,
        {'my': 'dict', 1: 2},
        {'my', 'set', 1, 0, (True, False)},
        (True, False),
        None,
        tf.sparse.from_dense(tf.constant([[0, 1, 0], [2, 0, 0], [0, 3, 4]])),
    ]
    serialized = serialization.serialize(to_serialize)
    parsed = serialization.parse(serialized)

    self.assertLen(to_serialize, len(parsed))
    for before, after in zip(to_serialize, parsed):
      self.assertEqual(type(before), type(after))
      if isinstance(before, (dict, set)):
        # Do not use string comparison because the order of elements may change.
        # Make sure that the test dict and set do not contain tensors, as they
        # will cause the == comparison to fail!
        self.assertEqual(before, after)
      else:
        # Two "identical" tensors with different IDs will not be equal (==). So,
        # compare str representations. The repr of a tensor contains its id, but
        # the str representation does not.
        self.assertEqual(str(before), str(after))
Esempio n. 3
0
def create_gtag_js_string(action, category, label=None, logging_dict=None):
    """Creates JavaScript code that sends info to Google Analytics."""
    custom_dimensions_js = ''
    if logging_dict is not None:
        unescaped_text = serialization.serialize(logging_dict)
        dimension_index = 0
        while unescaped_text:
            dimension_index += 1
            this_dimension_text = repr(unescaped_text[:CHARS_PER_DIMENSION])
            unescaped_text = unescaped_text[CHARS_PER_DIMENSION:]
            custom_dimensions_js += "'dimension{}': {}, ".format(
                dimension_index, this_dimension_text)
        if dimension_index > NUM_DIMENSIONS:
            return None

    maybe_label = ("'event_label': '{}', ".format(label)
                   if label is not None else '')
    return ("gtag('event', '{action}', {{'event_category': '{category}', "
            "{maybe_label}{custom_dimensions_js}}})").format(
                action=action,
                category=category,
                maybe_label=maybe_label,
                custom_dimensions_js=custom_dimensions_js)
Esempio n. 4
0
 def test_serialize_raises_for_unsupported_objects(self,
                                                   unsupported_object):
     # Weird objects result in TypeError.
     with self.assertRaises(TypeError):
         serialization.serialize(unsupported_object)