How to use shallow method in hypothesis

Best Python code snippet using hypothesis

nest_test.py

Source:nest_test.py Github

copy

Full Screen

1# Copyright 2017 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 numpy as np21from tensorflow.python.data.util import nest22from tensorflow.python.framework import constant_op23from tensorflow.python.framework import sparse_tensor24from tensorflow.python.ops import array_ops25from tensorflow.python.ops import math_ops26from tensorflow.python.platform import test27class NestTest(test.TestCase):28 def testFlattenAndPack(self):29 structure = ((3, 4), 5, (6, 7, (9, 10), 8))30 flat = ["a", "b", "c", "d", "e", "f", "g", "h"]31 self.assertEqual(nest.flatten(structure), [3, 4, 5, 6, 7, 9, 10, 8])32 self.assertEqual(33 nest.pack_sequence_as(structure, flat), (("a", "b"), "c",34 ("d", "e", ("f", "g"), "h")))35 point = collections.namedtuple("Point", ["x", "y"])36 structure = (point(x=4, y=2), ((point(x=1, y=0),),))37 flat = [4, 2, 1, 0]38 self.assertEqual(nest.flatten(structure), flat)39 restructured_from_flat = nest.pack_sequence_as(structure, flat)40 self.assertEqual(restructured_from_flat, structure)41 self.assertEqual(restructured_from_flat[0].x, 4)42 self.assertEqual(restructured_from_flat[0].y, 2)43 self.assertEqual(restructured_from_flat[1][0][0].x, 1)44 self.assertEqual(restructured_from_flat[1][0][0].y, 0)45 self.assertEqual([5], nest.flatten(5))46 self.assertEqual([np.array([5])], nest.flatten(np.array([5])))47 self.assertEqual("a", nest.pack_sequence_as(5, ["a"]))48 self.assertEqual(49 np.array([5]), nest.pack_sequence_as("scalar", [np.array([5])]))50 with self.assertRaisesRegexp(ValueError, "Structure is a scalar"):51 nest.pack_sequence_as("scalar", [4, 5])52 with self.assertRaisesRegexp(TypeError, "flat_sequence"):53 nest.pack_sequence_as([4, 5], "bad_sequence")54 with self.assertRaises(ValueError):55 nest.pack_sequence_as([5, 6, [7, 8]], ["a", "b", "c"])56 def testFlattenDictOrder(self):57 """`flatten` orders dicts by key, including OrderedDicts."""58 ordered = collections.OrderedDict([("d", 3), ("b", 1), ("a", 0), ("c", 2)])59 plain = {"d": 3, "b": 1, "a": 0, "c": 2}60 ordered_flat = nest.flatten(ordered)61 plain_flat = nest.flatten(plain)62 self.assertEqual([0, 1, 2, 3], ordered_flat)63 self.assertEqual([0, 1, 2, 3], plain_flat)64 def testPackDictOrder(self):65 """Packing orders dicts by key, including OrderedDicts."""66 ordered = collections.OrderedDict([("d", 0), ("b", 0), ("a", 0), ("c", 0)])67 plain = {"d": 0, "b": 0, "a": 0, "c": 0}68 seq = [0, 1, 2, 3]69 ordered_reconstruction = nest.pack_sequence_as(ordered, seq)70 plain_reconstruction = nest.pack_sequence_as(plain, seq)71 self.assertEqual(72 collections.OrderedDict([("d", 3), ("b", 1), ("a", 0), ("c", 2)]),73 ordered_reconstruction)74 self.assertEqual({"d": 3, "b": 1, "a": 0, "c": 2}, plain_reconstruction)75 def testFlattenAndPackWithDicts(self):76 # A nice messy mix of tuples, lists, dicts, and `OrderedDict`s.77 named_tuple = collections.namedtuple("A", ("b", "c"))78 mess = (79 "z",80 named_tuple(3, 4),81 {82 "c": (83 1,84 collections.OrderedDict([85 ("b", 3),86 ("a", 2),87 ]),88 ),89 "b": 590 },91 1792 )93 flattened = nest.flatten(mess)94 self.assertEqual(flattened, ["z", 3, 4, 5, 1, 2, 3, 17])95 structure_of_mess = (96 14,97 named_tuple("a", True),98 {99 "c": (100 0,101 collections.OrderedDict([102 ("b", 9),103 ("a", 8),104 ]),105 ),106 "b": 3107 },108 "hi everybody",109 )110 unflattened = nest.pack_sequence_as(structure_of_mess, flattened)111 self.assertEqual(unflattened, mess)112 # Check also that the OrderedDict was created, with the correct key order.113 unflattened_ordered_dict = unflattened[2]["c"][1]114 self.assertIsInstance(unflattened_ordered_dict, collections.OrderedDict)115 self.assertEqual(list(unflattened_ordered_dict.keys()), ["b", "a"])116 def testFlattenSparseValue(self):117 st = sparse_tensor.SparseTensorValue([[0]], [0], [1])118 single_value = st119 list_of_values = [st, st, st]120 nest_of_values = ((st), ((st), (st)))121 dict_of_values = {"foo": st, "bar": st, "baz": st}122 self.assertEqual([st], nest.flatten(single_value))123 self.assertEqual([[st, st, st]], nest.flatten(list_of_values))124 self.assertEqual([st, st, st], nest.flatten(nest_of_values))125 self.assertEqual([st, st, st], nest.flatten(dict_of_values))126 def testIsSequence(self):127 self.assertFalse(nest.is_sequence("1234"))128 self.assertFalse(nest.is_sequence([1, 3, [4, 5]]))129 self.assertTrue(nest.is_sequence(((7, 8), (5, 6))))130 self.assertFalse(nest.is_sequence([]))131 self.assertFalse(nest.is_sequence(set([1, 2])))132 ones = array_ops.ones([2, 3])133 self.assertFalse(nest.is_sequence(ones))134 self.assertFalse(nest.is_sequence(math_ops.tanh(ones)))135 self.assertFalse(nest.is_sequence(np.ones((4, 5))))136 self.assertTrue(nest.is_sequence({"foo": 1, "bar": 2}))137 self.assertFalse(138 nest.is_sequence(sparse_tensor.SparseTensorValue([[0]], [0], [1])))139 def testAssertSameStructure(self):140 structure1 = (((1, 2), 3), 4, (5, 6))141 structure2 = ((("foo1", "foo2"), "foo3"), "foo4", ("foo5", "foo6"))142 structure_different_num_elements = ("spam", "eggs")143 structure_different_nesting = (((1, 2), 3), 4, 5, (6,))144 nest.assert_same_structure(structure1, structure2)145 nest.assert_same_structure("abc", 1.0)146 nest.assert_same_structure("abc", np.array([0, 1]))147 nest.assert_same_structure("abc", constant_op.constant([0, 1]))148 with self.assertRaisesRegexp(ValueError,149 "don't have the same number of elements"):150 nest.assert_same_structure(structure1, structure_different_num_elements)151 with self.assertRaisesRegexp(ValueError,152 "don't have the same number of elements"):153 nest.assert_same_structure((0, 1), np.array([0, 1]))154 with self.assertRaisesRegexp(ValueError,155 "don't have the same number of elements"):156 nest.assert_same_structure(0, (0, 1))157 with self.assertRaisesRegexp(ValueError,158 "don't have the same nested structure"):159 nest.assert_same_structure(structure1, structure_different_nesting)160 named_type_0 = collections.namedtuple("named_0", ("a", "b"))161 named_type_1 = collections.namedtuple("named_1", ("a", "b"))162 self.assertRaises(TypeError, nest.assert_same_structure, (0, 1),163 named_type_0("a", "b"))164 nest.assert_same_structure(named_type_0(3, 4), named_type_0("a", "b"))165 self.assertRaises(TypeError, nest.assert_same_structure,166 named_type_0(3, 4), named_type_1(3, 4))167 with self.assertRaisesRegexp(ValueError,168 "don't have the same nested structure"):169 nest.assert_same_structure(named_type_0(3, 4), named_type_0((3,), 4))170 with self.assertRaisesRegexp(ValueError,171 "don't have the same nested structure"):172 nest.assert_same_structure(((3,), 4), (3, (4,)))173 structure1_list = {"a": ((1, 2), 3), "b": 4, "c": (5, 6)}174 with self.assertRaisesRegexp(TypeError,175 "don't have the same sequence type"):176 nest.assert_same_structure(structure1, structure1_list)177 nest.assert_same_structure(structure1, structure2, check_types=False)178 nest.assert_same_structure(structure1, structure1_list, check_types=False)179 def testMapStructure(self):180 structure1 = (((1, 2), 3), 4, (5, 6))181 structure2 = (((7, 8), 9), 10, (11, 12))182 structure1_plus1 = nest.map_structure(lambda x: x + 1, structure1)183 nest.assert_same_structure(structure1, structure1_plus1)184 self.assertAllEqual(185 [2, 3, 4, 5, 6, 7],186 nest.flatten(structure1_plus1))187 structure1_plus_structure2 = nest.map_structure(188 lambda x, y: x + y, structure1, structure2)189 self.assertEqual(190 (((1 + 7, 2 + 8), 3 + 9), 4 + 10, (5 + 11, 6 + 12)),191 structure1_plus_structure2)192 self.assertEqual(3, nest.map_structure(lambda x: x - 1, 4))193 self.assertEqual(7, nest.map_structure(lambda x, y: x + y, 3, 4))194 with self.assertRaisesRegexp(TypeError, "callable"):195 nest.map_structure("bad", structure1_plus1)196 with self.assertRaisesRegexp(ValueError, "same nested structure"):197 nest.map_structure(lambda x, y: None, 3, (3,))198 with self.assertRaisesRegexp(TypeError, "same sequence type"):199 nest.map_structure(lambda x, y: None, ((3, 4), 5), {"a": (3, 4), "b": 5})200 with self.assertRaisesRegexp(ValueError, "same nested structure"):201 nest.map_structure(lambda x, y: None, ((3, 4), 5), (3, (4, 5)))202 with self.assertRaisesRegexp(ValueError, "same nested structure"):203 nest.map_structure(lambda x, y: None, ((3, 4), 5), (3, (4, 5)),204 check_types=False)205 with self.assertRaisesRegexp(ValueError, "Only valid keyword argument"):206 nest.map_structure(lambda x: None, structure1, foo="a")207 with self.assertRaisesRegexp(ValueError, "Only valid keyword argument"):208 nest.map_structure(lambda x: None, structure1, check_types=False, foo="a")209 def testAssertShallowStructure(self):210 inp_ab = ("a", "b")211 inp_abc = ("a", "b", "c")212 expected_message = (213 "The two structures don't have the same sequence length. Input "214 "structure has length 2, while shallow structure has length 3.")215 with self.assertRaisesRegexp(ValueError, expected_message):216 nest.assert_shallow_structure(inp_abc, inp_ab)217 inp_ab1 = ((1, 1), (2, 2))218 inp_ab2 = {"a": (1, 1), "b": (2, 2)}219 expected_message = (220 "The two structures don't have the same sequence type. Input structure "221 "has type <(type|class) 'tuple'>, while shallow structure has type "222 "<(type|class) 'dict'>.")223 with self.assertRaisesRegexp(TypeError, expected_message):224 nest.assert_shallow_structure(inp_ab2, inp_ab1)225 nest.assert_shallow_structure(inp_ab2, inp_ab1, check_types=False)226 inp_ab1 = {"a": (1, 1), "b": {"c": (2, 2)}}227 inp_ab2 = {"a": (1, 1), "b": {"d": (2, 2)}}228 expected_message = (229 r"The two structures don't have the same keys. Input "230 r"structure has keys \['c'\], while shallow structure has "231 r"keys \['d'\].")232 with self.assertRaisesRegexp(ValueError, expected_message):233 nest.assert_shallow_structure(inp_ab2, inp_ab1)234 inp_ab = collections.OrderedDict([("a", 1), ("b", (2, 3))])235 inp_ba = collections.OrderedDict([("b", (2, 3)), ("a", 1)])236 nest.assert_shallow_structure(inp_ab, inp_ba)237 def testFlattenUpTo(self):238 input_tree = (((2, 2), (3, 3)), ((4, 9), (5, 5)))239 shallow_tree = ((True, True), (False, True))240 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)241 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)242 self.assertEqual(flattened_input_tree, [(2, 2), (3, 3), (4, 9), (5, 5)])243 self.assertEqual(flattened_shallow_tree, [True, True, False, True])244 input_tree = ((("a", 1), (("b", 2), (("c", 3), (("d", 4))))))245 shallow_tree = (("level_1", ("level_2", ("level_3", ("level_4")))))246 input_tree_flattened_as_shallow_tree = nest.flatten_up_to(shallow_tree,247 input_tree)248 input_tree_flattened = nest.flatten(input_tree)249 self.assertEqual(input_tree_flattened_as_shallow_tree,250 [("a", 1), ("b", 2), ("c", 3), ("d", 4)])251 self.assertEqual(input_tree_flattened, ["a", 1, "b", 2, "c", 3, "d", 4])252 ## Shallow non-list edge-case.253 # Using iterable elements.254 input_tree = ["input_tree"]255 shallow_tree = "shallow_tree"256 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)257 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)258 self.assertEqual(flattened_input_tree, [input_tree])259 self.assertEqual(flattened_shallow_tree, [shallow_tree])260 input_tree = ("input_tree_0", "input_tree_1")261 shallow_tree = "shallow_tree"262 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)263 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)264 self.assertEqual(flattened_input_tree, [input_tree])265 self.assertEqual(flattened_shallow_tree, [shallow_tree])266 # Using non-iterable elements.267 input_tree = (0,)268 shallow_tree = 9269 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)270 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)271 self.assertEqual(flattened_input_tree, [input_tree])272 self.assertEqual(flattened_shallow_tree, [shallow_tree])273 input_tree = (0, 1)274 shallow_tree = 9275 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)276 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)277 self.assertEqual(flattened_input_tree, [input_tree])278 self.assertEqual(flattened_shallow_tree, [shallow_tree])279 ## Both non-list edge-case.280 # Using iterable elements.281 input_tree = "input_tree"282 shallow_tree = "shallow_tree"283 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)284 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)285 self.assertEqual(flattened_input_tree, [input_tree])286 self.assertEqual(flattened_shallow_tree, [shallow_tree])287 # Using non-iterable elements.288 input_tree = 0289 shallow_tree = 0290 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)291 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)292 self.assertEqual(flattened_input_tree, [input_tree])293 self.assertEqual(flattened_shallow_tree, [shallow_tree])294 ## Input non-list edge-case.295 # Using iterable elements.296 input_tree = "input_tree"297 shallow_tree = ("shallow_tree",)298 expected_message = ("If shallow structure is a sequence, input must also "299 "be a sequence. Input has type: <(type|class) 'str'>.")300 with self.assertRaisesRegexp(TypeError, expected_message):301 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)302 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)303 self.assertEqual(flattened_shallow_tree, list(shallow_tree))304 input_tree = "input_tree"305 shallow_tree = ("shallow_tree_9", "shallow_tree_8")306 with self.assertRaisesRegexp(TypeError, expected_message):307 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)308 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)309 self.assertEqual(flattened_shallow_tree, list(shallow_tree))310 # Using non-iterable elements.311 input_tree = 0312 shallow_tree = (9,)313 expected_message = ("If shallow structure is a sequence, input must also "314 "be a sequence. Input has type: <(type|class) 'int'>.")315 with self.assertRaisesRegexp(TypeError, expected_message):316 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)317 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)318 self.assertEqual(flattened_shallow_tree, list(shallow_tree))319 input_tree = 0320 shallow_tree = (9, 8)321 with self.assertRaisesRegexp(TypeError, expected_message):322 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)323 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)324 self.assertEqual(flattened_shallow_tree, list(shallow_tree))325 # Using dict.326 input_tree = {"a": ((2, 2), (3, 3)), "b": ((4, 9), (5, 5))}327 shallow_tree = {"a": (True, True), "b": (False, True)}328 flattened_input_tree = nest.flatten_up_to(shallow_tree, input_tree)329 flattened_shallow_tree = nest.flatten_up_to(shallow_tree, shallow_tree)330 self.assertEqual(flattened_input_tree, [(2, 2), (3, 3), (4, 9), (5, 5)])331 self.assertEqual(flattened_shallow_tree, [True, True, False, True])332 def testMapStructureUpTo(self):333 ab_tuple = collections.namedtuple("ab_tuple", "a, b")334 op_tuple = collections.namedtuple("op_tuple", "add, mul")335 inp_val = ab_tuple(a=2, b=3)336 inp_ops = ab_tuple(a=op_tuple(add=1, mul=2), b=op_tuple(add=2, mul=3))337 out = nest.map_structure_up_to(338 inp_val, lambda val, ops: (val + ops.add) * ops.mul, inp_val, inp_ops)339 self.assertEqual(out.a, 6)340 self.assertEqual(out.b, 15)341 data_list = ((2, 4, 6, 8), ((1, 3, 5, 7, 9), (3, 5, 7)))342 name_list = ("evens", ("odds", "primes"))343 out = nest.map_structure_up_to(344 name_list, lambda name, sec: "first_{}_{}".format(len(sec), name),345 name_list, data_list)346 self.assertEqual(out, ("first_4_evens", ("first_5_odds", "first_3_primes")))347if __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 hypothesis 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