How to use nest method in gabbi

Best Python code snippet using gabbi_python

nest_test.py

Source:nest_test.py Github

copy

Full Screen

1# Copyright 2016 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15"""Tests for utilities working with arbitrarily nested structures."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19import collections20import time21import numpy as np22from six.moves import xrange # pylint: disable=redefined-builtin23from tensorflow.python.framework import constant_op24from tensorflow.python.framework import dtypes25from tensorflow.python.framework import test_util26from tensorflow.python.ops import array_ops27from tensorflow.python.ops import math_ops28from tensorflow.python.platform import test29from tensorflow.python.util import nest30class NestTest(test.TestCase):31 PointXY = collections.namedtuple("Point", ["x", "y"]) # pylint: disable=invalid-name32 @test_util.assert_no_new_pyobjects_executing_eagerly33 def testFlattenAndPack(self):34 structure = ((3, 4), 5, (6, 7, (9, 10), 8))35 flat = ["a", "b", "c", "d", "e", "f", "g", "h"]36 self.assertEqual(nest.flatten(structure), [3, 4, 5, 6, 7, 9, 10, 8])37 self.assertEqual(38 nest.pack_sequence_as(structure, flat), (("a", "b"), "c",39 ("d", "e", ("f", "g"), "h")))40 structure = (NestTest.PointXY(x=4, y=2),41 ((NestTest.PointXY(x=1, y=0),),))42 flat = [4, 2, 1, 0]43 self.assertEqual(nest.flatten(structure), flat)44 restructured_from_flat = nest.pack_sequence_as(structure, flat)45 self.assertEqual(restructured_from_flat, structure)46 self.assertEqual(restructured_from_flat[0].x, 4)47 self.assertEqual(restructured_from_flat[0].y, 2)48 self.assertEqual(restructured_from_flat[1][0][0].x, 1)49 self.assertEqual(restructured_from_flat[1][0][0].y, 0)50 self.assertEqual([5], nest.flatten(5))51 self.assertEqual([np.array([5])], nest.flatten(np.array([5])))52 self.assertEqual("a", nest.pack_sequence_as(5, ["a"]))53 self.assertEqual(54 np.array([5]), nest.pack_sequence_as("scalar", [np.array([5])]))55 with self.assertRaisesRegexp(ValueError, "Structure is a scalar"):56 nest.pack_sequence_as("scalar", [4, 5])57 with self.assertRaisesRegexp(TypeError, "flat_sequence"):58 nest.pack_sequence_as([4, 5], "bad_sequence")59 with self.assertRaises(ValueError):60 nest.pack_sequence_as([5, 6, [7, 8]], ["a", "b", "c"])61 @test_util.assert_no_new_pyobjects_executing_eagerly62 def testFlattenDictOrder(self):63 """`flatten` orders dicts by key, including OrderedDicts."""64 ordered = collections.OrderedDict([("d", 3), ("b", 1), ("a", 0), ("c", 2)])65 plain = {"d": 3, "b": 1, "a": 0, "c": 2}66 ordered_flat = nest.flatten(ordered)67 plain_flat = nest.flatten(plain)68 self.assertEqual([0, 1, 2, 3], ordered_flat)69 self.assertEqual([0, 1, 2, 3], plain_flat)70 def testPackDictOrder(self):71 """Packing orders dicts by key, including OrderedDicts."""72 ordered = collections.OrderedDict([("d", 0), ("b", 0), ("a", 0), ("c", 0)])73 plain = {"d": 0, "b": 0, "a": 0, "c": 0}74 seq = [0, 1, 2, 3]75 ordered_reconstruction = nest.pack_sequence_as(ordered, seq)76 plain_reconstruction = nest.pack_sequence_as(plain, seq)77 self.assertEqual(78 collections.OrderedDict([("d", 3), ("b", 1), ("a", 0), ("c", 2)]),79 ordered_reconstruction)80 self.assertEqual({"d": 3, "b": 1, "a": 0, "c": 2}, plain_reconstruction)81 Abc = collections.namedtuple("A", ("b", "c")) # pylint: disable=invalid-name82 @test_util.assert_no_new_pyobjects_executing_eagerly83 def testFlattenAndPack_withDicts(self):84 # A nice messy mix of tuples, lists, dicts, and `OrderedDict`s.85 mess = [86 "z",87 NestTest.Abc(3, 4),88 {89 "c": [90 1,91 collections.OrderedDict([92 ("b", 3),93 ("a", 2),94 ]),95 ],96 "b": 597 },98 1799 ]100 flattened = nest.flatten(mess)101 self.assertEqual(flattened, ["z", 3, 4, 5, 1, 2, 3, 17])102 structure_of_mess = [103 14,104 NestTest.Abc("a", True),105 {106 "c": [107 0,108 collections.OrderedDict([109 ("b", 9),110 ("a", 8),111 ]),112 ],113 "b": 3114 },115 "hi everybody",116 ]117 unflattened = nest.pack_sequence_as(structure_of_mess, flattened)118 self.assertEqual(unflattened, mess)119 # Check also that the OrderedDict was created, with the correct key order.120 unflattened_ordered_dict = unflattened[2]["c"][1]121 self.assertIsInstance(unflattened_ordered_dict, collections.OrderedDict)122 self.assertEqual(list(unflattened_ordered_dict.keys()), ["b", "a"])123 def testFlatten_numpyIsNotFlattened(self):124 structure = np.array([1, 2, 3])125 flattened = nest.flatten(structure)126 self.assertEqual(len(flattened), 1)127 def testFlatten_stringIsNotFlattened(self):128 structure = "lots of letters"129 flattened = nest.flatten(structure)130 self.assertEqual(len(flattened), 1)131 unflattened = nest.pack_sequence_as("goodbye", flattened)132 self.assertEqual(structure, unflattened)133 def testPackSequenceAs_notIterableError(self):134 with self.assertRaisesRegexp(TypeError,135 "flat_sequence must be a sequence"):136 nest.pack_sequence_as("hi", "bye")137 def testPackSequenceAs_wrongLengthsError(self):138 with self.assertRaisesRegexp(139 ValueError,140 "Structure had 2 elements, but flat_sequence had 3 elements."):141 nest.pack_sequence_as(["hello", "world"],142 ["and", "goodbye", "again"])143 @test_util.assert_no_new_pyobjects_executing_eagerly144 def testIsSequence(self):145 self.assertFalse(nest.is_sequence("1234"))146 self.assertTrue(nest.is_sequence([1, 3, [4, 5]]))147 self.assertTrue(nest.is_sequence(((7, 8), (5, 6))))148 self.assertTrue(nest.is_sequence([]))149 self.assertTrue(nest.is_sequence({"a": 1, "b": 2}))150 self.assertFalse(nest.is_sequence(set([1, 2])))151 ones = array_ops.ones([2, 3])152 self.assertFalse(nest.is_sequence(ones))153 self.assertFalse(nest.is_sequence(math_ops.tanh(ones)))154 self.assertFalse(nest.is_sequence(np.ones((4, 5))))155 def testFlattenDictItems(self):156 dictionary = {(4, 5, (6, 8)): ("a", "b", ("c", "d"))}157 flat = {4: "a", 5: "b", 6: "c", 8: "d"}158 self.assertEqual(nest.flatten_dict_items(dictionary), flat)159 with self.assertRaises(TypeError):160 nest.flatten_dict_items(4)161 bad_dictionary = {(4, 5, (4, 8)): ("a", "b", ("c", "d"))}162 with self.assertRaisesRegexp(ValueError, "not unique"):163 nest.flatten_dict_items(bad_dictionary)164 another_bad_dictionary = {(4, 5, (6, 8)): ("a", "b", ("c", ("d", "e")))}165 with self.assertRaisesRegexp(166 ValueError, "Key had [0-9]* elements, but value had [0-9]* elements"):167 nest.flatten_dict_items(another_bad_dictionary)168 # pylint does not correctly recognize these as class names and169 # suggests to use variable style under_score naming.170 # pylint: disable=invalid-name171 Named0ab = collections.namedtuple("named_0", ("a", "b"))172 Named1ab = collections.namedtuple("named_1", ("a", "b"))173 SameNameab = collections.namedtuple("same_name", ("a", "b"))174 SameNameab2 = collections.namedtuple("same_name", ("a", "b"))175 SameNamexy = collections.namedtuple("same_name", ("x", "y"))176 SameName1xy = collections.namedtuple("same_name_1", ("x", "y"))177 SameName1xy2 = collections.namedtuple("same_name_1", ("x", "y"))178 NotSameName = collections.namedtuple("not_same_name", ("a", "b"))179 # pylint: enable=invalid-name180 class SameNamedType1(SameNameab):181 pass182 @test_util.assert_no_new_pyobjects_executing_eagerly183 def testAssertSameStructure(self):184 structure1 = (((1, 2), 3), 4, (5, 6))185 structure2 = ((("foo1", "foo2"), "foo3"), "foo4", ("foo5", "foo6"))186 structure_different_num_elements = ("spam", "eggs")187 structure_different_nesting = (((1, 2), 3), 4, 5, (6,))188 nest.assert_same_structure(structure1, structure2)189 nest.assert_same_structure("abc", 1.0)190 nest.assert_same_structure("abc", np.array([0, 1]))191 nest.assert_same_structure("abc", constant_op.constant([0, 1]))192 with self.assertRaisesRegexp(193 ValueError,194 ("The two structures don't have the same nested structure\\.\n\n"195 "First structure:.*?\n\n"196 "Second structure:.*\n\n"197 "More specifically: Substructure "198 r'"type=tuple str=\(\(1, 2\), 3\)" is a sequence, while '199 'substructure "type=str str=spam" is not')):200 nest.assert_same_structure(structure1, structure_different_num_elements)201 with self.assertRaisesRegexp(202 ValueError,203 ("The two structures don't have the same nested structure\\.\n\n"204 "First structure:.*?\n\n"205 "Second structure:.*\n\n"206 r'More specifically: Substructure "type=list str=\[0, 1\]" '207 r'is a sequence, while substructure "type=ndarray str=\[0 1\]" '208 "is not")):209 nest.assert_same_structure([0, 1], np.array([0, 1]))210 with self.assertRaisesRegexp(211 ValueError,212 ("The two structures don't have the same nested structure\\.\n\n"213 "First structure:.*?\n\n"214 "Second structure:.*\n\n"215 r'More specifically: Substructure "type=list str=\[0, 1\]" '216 'is a sequence, while substructure "type=int str=0" '217 "is not")):218 nest.assert_same_structure(0, [0, 1])219 self.assertRaises(TypeError, nest.assert_same_structure, (0, 1), [0, 1])220 with self.assertRaisesRegexp(221 ValueError,222 ("don't have the same nested structure\\.\n\n"223 "First structure: .*?\n\nSecond structure: ")):224 nest.assert_same_structure(structure1, structure_different_nesting)225 self.assertRaises(TypeError, nest.assert_same_structure, (0, 1),226 NestTest.Named0ab("a", "b"))227 nest.assert_same_structure(NestTest.Named0ab(3, 4),228 NestTest.Named0ab("a", "b"))229 self.assertRaises(TypeError, nest.assert_same_structure,230 NestTest.Named0ab(3, 4), NestTest.Named1ab(3, 4))231 with self.assertRaisesRegexp(232 ValueError,233 ("don't have the same nested structure\\.\n\n"234 "First structure: .*?\n\nSecond structure: ")):235 nest.assert_same_structure(NestTest.Named0ab(3, 4),236 NestTest.Named0ab([3], 4))237 with self.assertRaisesRegexp(238 ValueError,239 ("don't have the same nested structure\\.\n\n"240 "First structure: .*?\n\nSecond structure: ")):241 nest.assert_same_structure([[3], 4], [3, [4]])242 structure1_list = [[[1, 2], 3], 4, [5, 6]]243 with self.assertRaisesRegexp(TypeError,244 "don't have the same sequence type"):245 nest.assert_same_structure(structure1, structure1_list)246 nest.assert_same_structure(structure1, structure2, check_types=False)247 nest.assert_same_structure(structure1, structure1_list, check_types=False)248 with self.assertRaisesRegexp(ValueError,249 "don't have the same set of keys"):250 nest.assert_same_structure({"a": 1}, {"b": 1})251 nest.assert_same_structure(NestTest.SameNameab(0, 1),252 NestTest.SameNameab2(2, 3))253 # This assertion is expected to pass: two namedtuples with the same254 # name and field names are considered to be identical.255 nest.assert_same_structure(256 NestTest.SameNameab(NestTest.SameName1xy(0, 1), 2),257 NestTest.SameNameab2(NestTest.SameName1xy2(2, 3), 4))258 expected_message = "The two structures don't have the same.*"259 with self.assertRaisesRegexp(ValueError, expected_message):260 nest.assert_same_structure(261 NestTest.SameNameab(0, NestTest.SameNameab2(1, 2)),262 NestTest.SameNameab2(NestTest.SameNameab(0, 1), 2))263 self.assertRaises(TypeError, nest.assert_same_structure,264 NestTest.SameNameab(0, 1), NestTest.NotSameName(2, 3))265 self.assertRaises(TypeError, nest.assert_same_structure,266 NestTest.SameNameab(0, 1), NestTest.SameNamexy(2, 3))267 self.assertRaises(TypeError, nest.assert_same_structure,268 NestTest.SameNameab(0, 1), NestTest.SameNamedType1(2, 3))269 EmptyNT = collections.namedtuple("empty_nt", "") # pylint: disable=invalid-name270 @test_util.assert_no_new_pyobjects_executing_eagerly271 def testMapStructure(self):272 structure1 = (((1, 2), 3), 4, (5, 6))273 structure2 = (((7, 8), 9), 10, (11, 12))274 structure1_plus1 = nest.map_structure(lambda x: x + 1, structure1)275 nest.assert_same_structure(structure1, structure1_plus1)276 self.assertAllEqual(277 [2, 3, 4, 5, 6, 7],278 nest.flatten(structure1_plus1))279 structure1_plus_structure2 = nest.map_structure(280 lambda x, y: x + y, structure1, structure2)281 self.assertEqual(282 (((1 + 7, 2 + 8), 3 + 9), 4 + 10, (5 + 11, 6 + 12)),283 structure1_plus_structure2)284 self.assertEqual(3, nest.map_structure(lambda x: x - 1, 4))285 self.assertEqual(7, nest.map_structure(lambda x, y: x + y, 3, 4))286 # Empty structures287 self.assertEqual((), nest.map_structure(lambda x: x + 1, ()))288 self.assertEqual([], nest.map_structure(lambda x: x + 1, []))289 self.assertEqual({}, nest.map_structure(lambda x: x + 1, {}))290 self.assertEqual(NestTest.EmptyNT(), nest.map_structure(lambda x: x + 1,291 NestTest.EmptyNT()))292 # This is checking actual equality of types, empty list != empty tuple293 self.assertNotEqual((), nest.map_structure(lambda x: x + 1, []))294 with self.assertRaisesRegexp(TypeError, "callable"):295 nest.map_structure("bad", structure1_plus1)296 with self.assertRaisesRegexp(ValueError, "at least one structure"):297 nest.map_structure(lambda x: x)298 with self.assertRaisesRegexp(ValueError, "same number of elements"):299 nest.map_structure(lambda x, y: None, (3, 4), (3, 4, 5))300 with self.assertRaisesRegexp(ValueError, "same nested structure"):301 nest.map_structure(lambda x, y: None, 3, (3,))302 with self.assertRaisesRegexp(TypeError, "same sequence type"):303 nest.map_structure(lambda x, y: None, ((3, 4), 5), [(3, 4), 5])304 with self.assertRaisesRegexp(ValueError, "same nested structure"):305 nest.map_structure(lambda x, y: None, ((3, 4), 5), (3, (4, 5)))306 structure1_list = [[[1, 2], 3], 4, [5, 6]]307 with self.assertRaisesRegexp(TypeError, "same sequence type"):308 nest.map_structure(lambda x, y: None, structure1, structure1_list)309 nest.map_structure(lambda x, y: None, structure1, structure1_list,310 check_types=False)311 with self.assertRaisesRegexp(ValueError, "same nested structure"):312 nest.map_structure(lambda x, y: None, ((3, 4), 5), (3, (4, 5)),313 check_types=False)314 with self.assertRaisesRegexp(ValueError, "Only valid keyword argument"):315 nest.map_structure(lambda x: None, structure1, foo="a")316 with self.assertRaisesRegexp(ValueError, "Only valid keyword argument"):317 nest.map_structure(lambda x: None, structure1, check_types=False, foo="a")318 ABTuple = collections.namedtuple("ab_tuple", "a, b") # pylint: disable=invalid-name319 @test_util.assert_no_new_pyobjects_executing_eagerly320 def testMapStructureWithStrings(self):321 inp_a = NestTest.ABTuple(a="foo", b=("bar", "baz"))322 inp_b = NestTest.ABTuple(a=2, b=(1, 3))323 out = nest.map_structure(lambda string, repeats: string * repeats,324 inp_a,325 inp_b)326 self.assertEqual("foofoo", out.a)327 self.assertEqual("bar", out.b[0])328 self.assertEqual("bazbazbaz", out.b[1])329 nt = NestTest.ABTuple(a=("something", "something_else"),330 b="yet another thing")331 rev_nt = nest.map_structure(lambda x: x[::-1], nt)332 # Check the output is the correct structure, and all strings are reversed.333 nest.assert_same_structure(nt, rev_nt)334 self.assertEqual(nt.a[0][::-1], rev_nt.a[0])335 self.assertEqual(nt.a[1][::-1], rev_nt.a[1])336 self.assertEqual(nt.b[::-1], rev_nt.b)337 def testMapStructureOverPlaceholders(self):338 inp_a = (array_ops.placeholder(dtypes.float32, shape=[3, 4]),339 array_ops.placeholder(dtypes.float32, shape=[3, 7]))340 inp_b = (array_ops.placeholder(dtypes.float32, shape=[3, 4]),341 array_ops.placeholder(dtypes.float32, shape=[3, 7]))342 output = nest.map_structure(lambda x1, x2: x1 + x2, inp_a, inp_b)343 nest.assert_same_structure(output, inp_a)344 self.assertShapeEqual(np.zeros((3, 4)), output[0])345 self.assertShapeEqual(np.zeros((3, 7)), output[1])346 feed_dict = {347 inp_a: (np.random.randn(3, 4), np.random.randn(3, 7)),348 inp_b: (np.random.randn(3, 4), np.random.randn(3, 7))349 }350 with self.test_session() as sess:351 output_np = sess.run(output, feed_dict=feed_dict)352 self.assertAllClose(output_np[0],353 feed_dict[inp_a][0] + feed_dict[inp_b][0])354 self.assertAllClose(output_np[1],355 feed_dict[inp_a][1] + feed_dict[inp_b][1])356 def testAssertShallowStructure(self):357 inp_ab = ["a", "b"]358 inp_abc = ["a", "b", "c"]359 expected_message = (360 "The two structures don't have the same sequence length. Input "361 "structure has length 2, while shallow structure has length 3.")362 with self.assertRaisesRegexp(ValueError, expected_message):363 nest.assert_shallow_structure(inp_abc, inp_ab)364 inp_ab1 = [(1, 1), (2, 2)]365 inp_ab2 = [[1, 1], [2, 2]]366 expected_message = (367 "The two structures don't have the same sequence type. Input structure "368 "has type <(type|class) 'tuple'>, while shallow structure has type "369 "<(type|class) 'list'>.")370 with self.assertRaisesRegexp(TypeError, expected_message):371 nest.assert_shallow_structure(inp_ab2, inp_ab1)372 nest.assert_shallow_structure(inp_ab2, inp_ab1, check_types=False)373 inp_ab1 = {"a": (1, 1), "b": {"c": (2, 2)}}374 inp_ab2 = {"a": (1, 1), "b": {"d": (2, 2)}}375 expected_message = (376 r"The two structures don't have the same keys. Input "377 r"structure has keys \['c'\], while shallow structure has "378 r"keys \['d'\].")379 with self.assertRaisesRegexp(ValueError, expected_message):380 nest.assert_shallow_structure(inp_ab2, inp_ab1)381 inp_ab = collections.OrderedDict([("a", 1), ("b", (2, 3))])382 inp_ba = collections.OrderedDict([("b", (2, 3)), ("a", 1)])383 nest.assert_shallow_structure(inp_ab, inp_ba)384 # This assertion is expected to pass: two namedtuples with the same385 # name and field names are considered to be identical.386 inp_shallow = NestTest.SameNameab(1, 2)387 inp_deep = NestTest.SameNameab2(1, [1, 2, 3])388 nest.assert_shallow_structure(inp_shallow, inp_deep, check_types=False)389 nest.assert_shallow_structure(inp_shallow, inp_deep, check_types=True)390 def testFlattenUpTo(self):391 # Shallow tree ends at scalar.392 input_tree = [[[2, 2], [3, 3]], [[4, 9], [5, 5]]]393 shallow_tree = [[True, True], [False, True]]394 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)395 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)396 self.assertEqual(flattened_input_tree, [[2, 2], [3, 3], [4, 9], [5, 5]])397 self.assertEqual(flattened_shallow_tree, [True, True, False, True])398 # Shallow tree ends at string.399 input_tree = [[("a", 1), [("b", 2), [("c", 3), [("d", 4)]]]]]400 shallow_tree = [["level_1", ["level_2", ["level_3", ["level_4"]]]]]401 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,402 input_tree)403 input_tree_flattened = nest.flatten(input_tree)404 self.assertEqual(input_tree_flattened_as_shallow_tree,405 [("a", 1), ("b", 2), ("c", 3), ("d", 4)])406 self.assertEqual(input_tree_flattened, ["a", 1, "b", 2, "c", 3, "d", 4])407 # Make sure dicts are correctly flattened, yielding values, not keys.408 input_tree = {"a": 1, "b": {"c": 2}, "d": [3, (4, 5)]}409 shallow_tree = {"a": 0, "b": 0, "d": [0, 0]}410 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,411 input_tree)412 self.assertEqual(input_tree_flattened_as_shallow_tree,413 [1, {"c": 2}, 3, (4, 5)])414 # Namedtuples.415 ab_tuple = NestTest.ABTuple416 input_tree = ab_tuple(a=[0, 1], b=2)417 shallow_tree = ab_tuple(a=0, b=1)418 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,419 input_tree)420 self.assertEqual(input_tree_flattened_as_shallow_tree,421 [[0, 1], 2])422 # Nested dicts, OrderedDicts and namedtuples.423 input_tree = collections.OrderedDict(424 [("a", ab_tuple(a=[0, {"b": 1}], b=2)),425 ("c", {"d": 3, "e": collections.OrderedDict([("f", 4)])})])426 shallow_tree = input_tree427 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,428 input_tree)429 self.assertEqual(input_tree_flattened_as_shallow_tree, [0, 1, 2, 3, 4])430 shallow_tree = collections.OrderedDict([("a", 0), ("c", {"d": 3, "e": 1})])431 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,432 input_tree)433 self.assertEqual(input_tree_flattened_as_shallow_tree,434 [ab_tuple(a=[0, {"b": 1}], b=2),435 3,436 collections.OrderedDict([("f", 4)])])437 shallow_tree = collections.OrderedDict([("a", 0), ("c", 0)])438 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,439 input_tree)440 self.assertEqual(input_tree_flattened_as_shallow_tree,441 [ab_tuple(a=[0, {"b": 1}], b=2),442 {"d": 3, "e": collections.OrderedDict([("f", 4)])}])443 ## Shallow non-list edge-case.444 # Using iterable elements.445 input_tree = ["input_tree"]446 shallow_tree = "shallow_tree"447 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)448 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)449 self.assertEqual(flattened_input_tree, [input_tree])450 self.assertEqual(flattened_shallow_tree, [shallow_tree])451 input_tree = ["input_tree_0", "input_tree_1"]452 shallow_tree = "shallow_tree"453 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)454 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)455 self.assertEqual(flattened_input_tree, [input_tree])456 self.assertEqual(flattened_shallow_tree, [shallow_tree])457 # Using non-iterable elements.458 input_tree = [0]459 shallow_tree = 9460 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)461 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)462 self.assertEqual(flattened_input_tree, [input_tree])463 self.assertEqual(flattened_shallow_tree, [shallow_tree])464 input_tree = [0, 1]465 shallow_tree = 9466 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)467 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)468 self.assertEqual(flattened_input_tree, [input_tree])469 self.assertEqual(flattened_shallow_tree, [shallow_tree])470 ## Both non-list edge-case.471 # Using iterable elements.472 input_tree = "input_tree"473 shallow_tree = "shallow_tree"474 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)475 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)476 self.assertEqual(flattened_input_tree, [input_tree])477 self.assertEqual(flattened_shallow_tree, [shallow_tree])478 # Using non-iterable elements.479 input_tree = 0480 shallow_tree = 0481 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)482 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)483 self.assertEqual(flattened_input_tree, [input_tree])484 self.assertEqual(flattened_shallow_tree, [shallow_tree])485 ## Input non-list edge-case.486 # Using iterable elements.487 input_tree = "input_tree"488 shallow_tree = ["shallow_tree"]489 expected_message = ("If shallow structure is a sequence, input must also "490 "be a sequence. Input has type: <(type|class) 'str'>.")491 with self.assertRaisesRegexp(TypeError, expected_message):492 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)493 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)494 self.assertEqual(flattened_shallow_tree, shallow_tree)495 input_tree = "input_tree"496 shallow_tree = ["shallow_tree_9", "shallow_tree_8"]497 with self.assertRaisesRegexp(TypeError, expected_message):498 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)499 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)500 self.assertEqual(flattened_shallow_tree, shallow_tree)501 # Using non-iterable elements.502 input_tree = 0503 shallow_tree = [9]504 expected_message = ("If shallow structure is a sequence, input must also "505 "be a sequence. Input has type: <(type|class) 'int'>.")506 with self.assertRaisesRegexp(TypeError, expected_message):507 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)508 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)509 self.assertEqual(flattened_shallow_tree, shallow_tree)510 input_tree = 0511 shallow_tree = [9, 8]512 with self.assertRaisesRegexp(TypeError, expected_message):513 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)514 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)515 self.assertEqual(flattened_shallow_tree, shallow_tree)516 def testMapStructureUpTo(self):517 # Named tuples.518 ab_tuple = collections.namedtuple("ab_tuple", "a, b")519 op_tuple = collections.namedtuple("op_tuple", "add, mul")520 inp_val = ab_tuple(a=2, b=3)521 inp_ops = ab_tuple(a=op_tuple(add=1, mul=2), b=op_tuple(add=2, mul=3))522 out = nest.map_structure_up_to(523 inp_val, lambda val, ops: (val + ops.add) * ops.mul, inp_val, inp_ops)524 self.assertEqual(out.a, 6)525 self.assertEqual(out.b, 15)526 # Lists.527 data_list = [[2, 4, 6, 8], [[1, 3, 5, 7, 9], [3, 5, 7]]]528 name_list = ["evens", ["odds", "primes"]]529 out = nest.map_structure_up_to(530 name_list, lambda name, sec: "first_{}_{}".format(len(sec), name),531 name_list, data_list)532 self.assertEqual(out, ["first_4_evens", ["first_5_odds", "first_3_primes"]])533 def testGetTraverseShallowStructure(self):534 scalar_traverse_input = [3, 4, (1, 2, [0]), [5, 6], {"a": (7,)}, []]535 scalar_traverse_r = nest.get_traverse_shallow_structure(536 lambda s: not isinstance(s, tuple),537 scalar_traverse_input)538 self.assertEqual(scalar_traverse_r,539 [True, True, False, [True, True], {"a": False}, []])540 nest.assert_shallow_structure(scalar_traverse_r,541 scalar_traverse_input)542 structure_traverse_input = [(1, [2]), ([1], 2)]543 structure_traverse_r = nest.get_traverse_shallow_structure(544 lambda s: (True, False) if isinstance(s, tuple) else True,545 structure_traverse_input)546 self.assertEqual(structure_traverse_r,547 [(True, False), ([True], False)])548 nest.assert_shallow_structure(structure_traverse_r,549 structure_traverse_input)550 with self.assertRaisesRegexp(TypeError, "returned structure"):551 nest.get_traverse_shallow_structure(lambda _: [True], 0)552 with self.assertRaisesRegexp(TypeError, "returned a non-bool scalar"):553 nest.get_traverse_shallow_structure(lambda _: 1, [1])554 with self.assertRaisesRegexp(555 TypeError, "didn't return a depth=1 structure of bools"):556 nest.get_traverse_shallow_structure(lambda _: [1], [1])557 def testYieldFlatStringPaths(self):558 for inputs_expected in ({"inputs": [], "expected": []},559 {"inputs": 3, "expected": [()]},560 {"inputs": [3], "expected": [(0,)]},561 {"inputs": {"a": 3}, "expected": [("a",)]},562 {"inputs": {"a": {"b": 4}},563 "expected": [("a", "b")]},564 {"inputs": [{"a": 2}], "expected": [(0, "a")]},565 {"inputs": [{"a": [2]}], "expected": [(0, "a", 0)]},566 {"inputs": [{"a": [(23, 42)]}],567 "expected": [(0, "a", 0, 0), (0, "a", 0, 1)]},568 {"inputs": [{"a": ([23], 42)}],569 "expected": [(0, "a", 0, 0), (0, "a", 1)]},570 {"inputs": {"a": {"a": 2}, "c": [[[4]]]},571 "expected": [("a", "a"), ("c", 0, 0, 0)]},572 {"inputs": {"0": [{"1": 23}]},573 "expected": [("0", 0, "1")]}):574 inputs = inputs_expected["inputs"]575 expected = inputs_expected["expected"]576 self.assertEqual(list(nest.yield_flat_paths(inputs)), expected)577 def testFlattenWithStringPaths(self):578 for inputs_expected in (579 {"inputs": [], "expected": []},580 {"inputs": [23, "42"], "expected": [("0", 23), ("1", "42")]},581 {"inputs": [[[[108]]]], "expected": [("0/0/0/0", 108)]}):582 inputs = inputs_expected["inputs"]583 expected = inputs_expected["expected"]584 self.assertEqual(585 nest.flatten_with_joined_string_paths(inputs, separator="/"),586 expected)587 # Need a separate test for namedtuple as we can't declare tuple definitions588 # in the @parameterized arguments.589 def testFlattenNamedTuple(self):590 # pylint: disable=invalid-name591 Foo = collections.namedtuple("Foo", ["a", "b"])592 Bar = collections.namedtuple("Bar", ["c", "d"])593 # pylint: enable=invalid-name594 test_cases = [595 (Foo(a=3, b=Bar(c=23, d=42)),596 [("a", 3), ("b/c", 23), ("b/d", 42)]),597 (Foo(a=Bar(c=23, d=42), b=Bar(c=0, d="something")),598 [("a/c", 23), ("a/d", 42), ("b/c", 0), ("b/d", "something")]),599 (Bar(c=42, d=43),600 [("c", 42), ("d", 43)]),601 (Bar(c=[42], d=43),602 [("c/0", 42), ("d", 43)]),603 ]604 for inputs, expected in test_cases:605 self.assertEqual(606 list(nest.flatten_with_joined_string_paths(inputs)), expected)607class NestBenchmark(test.Benchmark):608 def run_and_report(self, s1, s2, name):609 burn_iter, test_iter = 100, 30000610 for _ in xrange(burn_iter):611 nest.assert_same_structure(s1, s2)612 t0 = time.time()613 for _ in xrange(test_iter):614 nest.assert_same_structure(s1, s2)615 t1 = time.time()616 self.report_benchmark(iters=test_iter, wall_time=(t1 - t0) / test_iter,617 name=name)618 def benchmark_assert_structure(self):619 s1 = (((1, 2), 3), 4, (5, 6))620 s2 = ((("foo1", "foo2"), "foo3"), "foo4", ("foo5", "foo6"))621 self.run_and_report(s1, s2, "assert_same_structure_6_elem")622 s1 = (((1, 2), 3), 4, (5, 6)) * 10623 s2 = ((("foo1", "foo2"), "foo3"), "foo4", ("foo5", "foo6")) * 10624 self.run_and_report(s1, s2, "assert_same_structure_60_elem")625if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run gabbi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful