How to use _coder method in Pyscreenshot

Best Python code snippet using pyscreenshot_python

nested_structure_coder_test.py

Source:nested_structure_coder_test.py Github

copy

Full Screen

1# Copyright 2018 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 nested structure coding."""16from __future__ import absolute_import17from __future__ import division18from __future__ import print_function19import collections20from tensorflow.core.protobuf import struct_pb221from tensorflow.python.framework import dtypes22from tensorflow.python.framework import tensor_shape23from tensorflow.python.framework import tensor_spec24from tensorflow.python.platform import test25from tensorflow.python.saved_model import nested_structure_coder26class NestedStructureTest(test.TestCase):27 def setUp(self):28 self._coder = nested_structure_coder.StructureCoder()29 def testEncodeDecodeList(self):30 structure = [1.5, 2.5, 3.0]31 self.assertTrue(self._coder.can_encode(structure))32 encoded = self._coder.encode_structure(structure)33 expected = struct_pb2.StructuredValue()34 expected.list_value.values.add().float64_value = 1.535 expected.list_value.values.add().float64_value = 2.536 expected.list_value.values.add().float64_value = 3.037 self.assertEqual(expected, encoded)38 decoded = self._coder.decode_proto(encoded)39 self.assertEqual(structure, decoded)40 def testEncodeDecodeTuple(self):41 structure = ("hello", [3, (2, 1)])42 self.assertTrue(self._coder.can_encode(structure))43 encoded = self._coder.encode_structure(structure)44 expected = struct_pb2.StructuredValue()45 expected.tuple_value.values.add().string_value = "hello"46 list_value = expected.tuple_value.values.add().list_value47 list_value.values.add().int64_value = 348 tuple_value = list_value.values.add().tuple_value49 tuple_value.values.add().int64_value = 250 tuple_value.values.add().int64_value = 151 self.assertEqual(expected, encoded)52 decoded = self._coder.decode_proto(encoded)53 self.assertEqual(structure, decoded)54 def testEncodeDecodeDict(self):55 structure = dict(a=3, b=[7, 2.5])56 self.assertTrue(self._coder.can_encode(structure))57 encoded = self._coder.encode_structure(structure)58 expected = struct_pb2.StructuredValue()59 expected.dict_value.fields["a"].int64_value = 360 list_value = expected.dict_value.fields["b"].list_value61 list_value.values.add().int64_value = 762 list_value.values.add().float64_value = 2.563 self.assertEqual(expected, encoded)64 decoded = self._coder.decode_proto(encoded)65 self.assertIsInstance(decoded["a"], int)66 self.assertEqual(structure, decoded)67 def testEncodeDecodeTensorShape(self):68 structure = [tensor_shape.TensorShape([1, 2, 3]), "hello"]69 self.assertTrue(self._coder.can_encode(structure))70 encoded = self._coder.encode_structure(structure)71 expected = struct_pb2.StructuredValue()72 expected_list = expected.list_value73 expected_tensor_shape = expected_list.values.add().tensor_shape_value74 expected_tensor_shape.dim.add().size = 175 expected_tensor_shape.dim.add().size = 276 expected_tensor_shape.dim.add().size = 377 expected_tensor_shape = expected_list.values.add().string_value = "hello"78 self.assertEqual(expected, encoded)79 decoded = self._coder.decode_proto(encoded)80 self.assertEqual(structure, decoded)81 def testEncodeDecodeNamedTuple(self):82 named_tuple_type = collections.namedtuple("NamedTuple", ["x", "y"])83 named_tuple = named_tuple_type(x=[1, 2], y="hello")84 self.assertTrue(self._coder.can_encode(named_tuple))85 encoded = self._coder.encode_structure(named_tuple)86 expected = struct_pb2.StructuredValue()87 expected_named_tuple = expected.named_tuple_value88 expected_named_tuple.name = "NamedTuple"89 key_value_pair = expected_named_tuple.values.add()90 key_value_pair.key = "x"91 list_value = key_value_pair.value.list_value92 list_value.values.add().int64_value = 193 list_value.values.add().int64_value = 294 key_value_pair = expected_named_tuple.values.add()95 key_value_pair.key = "y"96 key_value_pair.value.string_value = "hello"97 self.assertEqual(expected, encoded)98 decoded = self._coder.decode_proto(encoded)99 self.assertEqual(named_tuple._asdict(), decoded._asdict())100 self.assertEqual(named_tuple.__class__.__name__, decoded.__class__.__name__)101 def testNone(self):102 structure = [1.0, None]103 self.assertTrue(self._coder.can_encode(structure))104 encoded = self._coder.encode_structure(structure)105 expected = struct_pb2.StructuredValue()106 expected.list_value.values.add().float64_value = 1.0107 expected.list_value.values.add().none_value.CopyFrom(struct_pb2.NoneValue())108 self.assertEqual(expected, encoded)109 decoded = self._coder.decode_proto(encoded)110 self.assertEqual(structure, decoded)111 def testBool(self):112 structure = [False]113 self.assertTrue(self._coder.can_encode(structure))114 encoded = self._coder.encode_structure(structure)115 expected = struct_pb2.StructuredValue()116 expected.list_value.values.add().bool_value = False117 self.assertEqual(expected, encoded)118 decoded = self._coder.decode_proto(encoded)119 self.assertEqual(structure, decoded)120 def testEmptyStructures(self):121 structure = [list(), dict(), tuple()]122 self.assertTrue(self._coder.can_encode(structure))123 encoded = self._coder.encode_structure(structure)124 expected = struct_pb2.StructuredValue()125 expected.list_value.values.add().list_value.CopyFrom(struct_pb2.ListValue())126 expected.list_value.values.add().dict_value.CopyFrom(struct_pb2.DictValue())127 expected.list_value.values.add().tuple_value.CopyFrom(128 struct_pb2.TupleValue())129 self.assertEqual(expected, encoded)130 decoded = self._coder.decode_proto(encoded)131 self.assertEqual(structure, decoded)132 def testDtype(self):133 structure = [dtypes.int64]134 self.assertTrue(self._coder.can_encode(structure))135 encoded = self._coder.encode_structure(structure)136 expected = struct_pb2.StructuredValue()137 list_value = expected.list_value.values.add()138 list_value.tensor_dtype_value = dtypes.int64.as_datatype_enum139 self.assertEqual(expected, encoded)140 decoded = self._coder.decode_proto(encoded)141 self.assertEqual(structure, decoded)142 def testEncodeDecodeTensorSpec(self):143 structure = [tensor_spec.TensorSpec([1, 2, 3], dtypes.int64, "hello")]144 self.assertTrue(self._coder.can_encode(structure))145 encoded = self._coder.encode_structure(structure)146 expected = struct_pb2.StructuredValue()147 expected_list = expected.list_value148 expected_tensor_spec = expected_list.values.add().tensor_spec_value149 expected_tensor_spec.shape.dim.add().size = 1150 expected_tensor_spec.shape.dim.add().size = 2151 expected_tensor_spec.shape.dim.add().size = 3152 expected_tensor_spec.name = "hello"153 expected_tensor_spec.dtype = dtypes.int64.as_datatype_enum154 self.assertEqual(expected, encoded)155 decoded = self._coder.decode_proto(encoded)156 self.assertEqual(structure, decoded)157 def testNotEncodable(self):158 class NotEncodable(object):159 pass160 self.assertFalse(self._coder.can_encode([NotEncodable()]))161if __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 Pyscreenshot 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