How to use contains_key method in assertpy

Best Python code snippet using assertpy_python

hash_map.py

Source:hash_map.py Github

copy

Full Screen

...100 if node.key == key:101 bucket.remove(key)102 self.size -= 1103 return104 def contains_key(self, key: str) -> bool:105 """106 contains_key returns True if the given key exists in the hash map,107 otherwise it returns False108 """109 if self.size == 0:110 return False111 for i in range(self.capacity):112 bucket = self.buckets.get_at_index(i)113 for node in bucket:114 if node.key == key:115 return True116 return False117 def empty_buckets(self) -> int:118 """119 empty_buckets returns the number of empty 'buckets' in the hash map120 """121 tally = 0122 for i in range(self.buckets.length()):123 if self.buckets.get_at_index(i).length() == 0:124 tally += 1125 return tally126 def table_load(self) -> float:127 """128 table_load returns the value of the load on the table which is equal129 to the table's size / table's capacity130 """131 return self.size / self.capacity132 def resize_table(self, new_capacity: int) -> None:133 """134 resize_table resizes the hash map to the given new_capacity135 """136 if new_capacity < 1:137 pass138 new_hash_map = HashMap(new_capacity, self.hash_function)139 for i in range(self.capacity):140 bucket = self.buckets.get_at_index(i)141 for node in bucket:142 new_hash_map.put(node.key, node.value)143 self.clear()144 cap_difference = new_capacity - self.capacity145 for i in range(cap_difference):146 self.buckets.append(LinkedList())147 self.capacity += 1148 for i in range(new_hash_map.capacity):149 bucket = new_hash_map.buckets.get_at_index(i)150 for node in bucket:151 self.put(node.key, node.value)152 def get_keys(self) -> DynamicArray:153 """154 get_keys returns an array of all keys present in the hash map155 """156 keys_arr = DynamicArray()157 for i in range(self.capacity-1, -1, -1):158 bucket = self.buckets.get_at_index(i)159 j = 0160 for node in bucket:161 if j % 2 == 0:162 if node.next is not None:163 keys_arr.append(node.next.key)164 if node is not None:165 keys_arr.append(node.key)166 j += 1167 return keys_arr168# BASIC TESTING169if __name__ == "__main__":170 print("\nPDF - empty_buckets example 1")171 print("-----------------------------")172 m = HashMap(100, hash_function_1)173 print(m.empty_buckets(), m.size, m.capacity)174 m.put('key1', 10)175 print(m.empty_buckets(), m.size, m.capacity)176 m.put('key2', 20)177 print(m.empty_buckets(), m.size, m.capacity)178 m.put('key1', 30)179 print(m.empty_buckets(), m.size, m.capacity)180 m.put('key4', 40)181 print(m.empty_buckets(), m.size, m.capacity)182 print("\nPDF - empty_buckets example 2")183 print("-----------------------------")184 m = HashMap(50, hash_function_1)185 for i in range(150):186 m.put('key' + str(i), i * 100)187 if i % 30 == 0:188 print(m.empty_buckets(), m.size, m.capacity)189 print("\nPDF - table_load example 1")190 print("--------------------------")191 m = HashMap(100, hash_function_1)192 print(m.table_load())193 m.put('key1', 10)194 print(m.table_load())195 m.put('key2', 20)196 print(m.table_load())197 m.put('key1', 30)198 print(m.table_load())199 print("\nPDF - table_load example 2")200 print("--------------------------")201 m = HashMap(50, hash_function_1)202 for i in range(50):203 m.put('key' + str(i), i * 100)204 if i % 10 == 0:205 print(m.table_load(), m.size, m.capacity)206 print("\nPDF - clear example 1")207 print("---------------------")208 m = HashMap(100, hash_function_1)209 print(m.size, m.capacity)210 m.put('key1', 10)211 m.put('key2', 20)212 m.put('key1', 30)213 print(m.size, m.capacity)214 m.clear()215 print(m.size, m.capacity)216 print("\nPDF - clear example 2")217 print("---------------------")218 m = HashMap(50, hash_function_1)219 print(m.size, m.capacity)220 m.put('key1', 10)221 print(m.size, m.capacity)222 m.put('key2', 20)223 print(m.size, m.capacity)224 m.resize_table(100)225 print(m.size, m.capacity)226 m.clear()227 print(m.size, m.capacity)228 print("\nPDF - put example 1")229 print("-------------------")230 m = HashMap(50, hash_function_1)231 for i in range(150):232 m.put('str' + str(i), i * 100)233 if i % 25 == 24:234 print(m.empty_buckets(), m.table_load(), m.size, m.capacity)235 print("\nPDF - put example 2")236 print("-------------------")237 m = HashMap(40, hash_function_2)238 for i in range(50):239 m.put('str' + str(i // 3), i * 100)240 if i % 10 == 9:241 print(m.empty_buckets(), m.table_load(), m.size, m.capacity)242 print("\nPDF - contains_key example 1")243 print("----------------------------")244 m = HashMap(10, hash_function_1)245 print(m.contains_key('key1'))246 m.put('key1', 10)247 m.put('key2', 20)248 m.put('key3', 30)249 print(m.contains_key('key1'))250 print(m.contains_key('key4'))251 print(m.contains_key('key2'))252 print(m.contains_key('key3'))253 m.remove('key3')254 print(m.contains_key('key3'))255 print("\nPDF - contains_key example 2")256 print("----------------------------")257 m = HashMap(75, hash_function_2)258 keys = [i for i in range(1, 1000, 20)]259 for key in keys:260 m.put(str(key), key * 42)261 print(m.size, m.capacity)262 result = True263 for key in keys:264 # all inserted keys must be present265 result &= m.contains_key(str(key))266 # NOT inserted keys must be absent267 result &= not m.contains_key(str(key + 1))268 print(result)269 print("\nPDF - get example 1")270 print("-------------------")271 m = HashMap(30, hash_function_1)272 print(m.get('key'))273 m.put('key1', 10)274 print(m.get('key1'))275 print("\nPDF - get example 2")276 print("-------------------")277 m = HashMap(150, hash_function_2)278 for i in range(200, 300, 7):279 m.put(str(i), i * 10)280 print(m.size, m.capacity)281 for i in range(200, 300, 21):282 print(i, m.get(str(i)), m.get(str(i)) == i * 10)283 print(i + 1, m.get(str(i + 1)), m.get(str(i + 1)) == (i + 1) * 10)284 print("\nPDF - remove example 1")285 print("----------------------")286 m = HashMap(50, hash_function_1)287 print(m.get('key1'))288 m.put('key1', 10)289 print(m.get('key1'))290 m.remove('key1')291 print(m.get('key1'))292 m.remove('key4')293 print("\nPDF - resize example 1")294 print("----------------------")295 m = HashMap(20, hash_function_1)296 m.put('key1', 10)297 print(m.size, m.capacity, m.get('key1'), m.contains_key('key1'))298 m.resize_table(30)299 print(m.size, m.capacity, m.get('key1'), m.contains_key('key1'))300 print("\nPDF - resize example 2")301 print("----------------------")302 m = HashMap(75, hash_function_2)303 keys = [i for i in range(1, 1000, 13)]304 for key in keys:305 m.put(str(key), key * 42)306 print(m.size, m.capacity)307 308 for capacity in range(111, 1000, 117):309 m.resize_table(capacity)310 311 m.put('some key', 'some value')312 result = m.contains_key('some key')313 m.remove('some key')314 315 for key in keys:316 result &= m.contains_key(str(key))317 result &= not m.contains_key(str(key + 1))318 print(capacity, result, m.size, m.capacity, round(m.table_load(), 2))319 print("\nPDF - get_keys example 1")320 print("------------------------")321 m = HashMap(10, hash_function_2)322 for i in range(100, 200, 10):323 m.put(str(i), str(i * 10))324 print(m.get_keys())325 m.resize_table(1)326 print(m.get_keys())327 m.put('200', '2000')328 m.remove('100')329 m.resize_table(2)...

Full Screen

Full Screen

configurations_manager.py

Source:configurations_manager.py Github

copy

Full Screen

...28 This class will store the values of all property files.29 """30 def __init__(self):31 self.__dict = {}32 def contains_key(self, key: str) -> bool:33 """34 Check that configurations manager contains key.35 Args:36 key (str): Key name to verify key is exist or not.37 Returns:38 bool: Returns True If dict contains key else returns False39 """40 return True if key in self.__dict else False41 def set_object_for_key(self, key: str, value=object) -> None:42 """43 Set object for key into the dict44 Args:45 key (str): Key name to store value46 value (str): Value which needs to be store47 Returns:48 None49 """50 self.__dict[key] = value51 def get_object_for_key(self, key: str, default_value: Optional[object] = None) -> object:52 """53 Returns object for key.54 Args:55 key (str): Key name to store value56 default_value (Optional(object)): This will default value for key57 Returns:58 Optional(object): Stored value for key59 """60 return self.__dict[key] if self.contains_key(key) else default_value61 def get_str_for_key(self, key: str, default_value=None) -> str:62 """63 Returns object for key.64 Args:65 key (str): Key name to store value66 default_value (Optional(str)): This will default value for key67 Returns:68 Optional(str): Stored value for key69 """70 return str(self.__dict[key]) if self.contains_key(key) else default_value71 def get_int_for_key(self, key: str, default_value=None) -> int:72 """73 Returns object for key.74 Args:75 key (str): Key name to store value76 default_value (Optional(int)): This will default value for key77 Returns:78 Optional(int): Stored value for key79 """80 return int(self.__dict[key]) if self.contains_key(key) else default_value81 def get_bool_for_key(self, key: str, default_value=False) -> bool:82 """83 Returns object for key.84 Args:85 key (str): Key name to store value86 default_value (Optional(bool)): This will default value for key87 Returns:88 Optional(bool): Stored value for key89 """90 return to_boolean(self.__dict[key]) if self.contains_key(key) else default_value91 def get_list_for_key(self, key: str, default_value=None) -> list:92 """93 Returns object for key.94 Args:95 key (str): Key name to store value96 default_value (Optional(list)): This will default value for key97 Returns:98 Optional(list): Stored value for key99 """100 if default_value is None:101 default_value = []102 if self.contains_key(key) and isinstance(self.__dict[key], str):103 return self.__dict[key].split(";")104 return default_value105 def get_dict_for_key(self, key: str, default_value=None) -> dict:106 """107 Returns object for key.108 Args:109 key (str): Key name to store value110 default_value (Optional(dict)): This will default value for key111 Returns:112 Optional(dict): Stored value for key113 """114 if default_value is None:115 default_value = {}116 if self.contains_key(key) and isinstance(self.__dict[key], str):117 return json.loads(self.__dict[key])...

Full Screen

Full Screen

test_db_core_multi_map.py

Source:test_db_core_multi_map.py Github

copy

Full Screen

...9def test_map_length_nonzero():10 mm = MultiMap()11 mm.add_edge("1", "2")12 assert len(mm) == 113def test_map_contains_key():14 mm = MultiMap()15 mm.add_edge("1", "2")16 assert mm.contains_key("1") is True17 assert mm.contains_key("2") is False18def test_map_contains_value():19 mm = MultiMap()20 mm.add_edge("1", "2")21 assert mm.contains_value("1", "2") is True22 assert mm.contains_value("1", "1") is False23def test_map_contains_value_if_value_None():24 mm = MultiMap()25 assert mm.contains_value("1", "1") is False26def test_map_removes_key():27 mm = MultiMap()28 mm.add_edge("1", "2")29 assert mm.contains_key("1") is True30 assert mm.contains_value("1", "2") is True31 assert mm.remove_key("1") is True32 assert mm.contains_key("1") is False33 assert mm.contains_value("1", "2") is False34def test_map_removes_value():35 mm = MultiMap()36 mm.add_edge("1", "2")37 assert mm.contains_key("1") is True38 assert mm.contains_value("1", "2") is True39 mm.remove_edge("1", "2")40 assert mm.contains_key("1") is True41 assert mm.contains_value("1", "2") is False42def test_map_clears_key():43 mm = MultiMap()44 mm.add_edge("1", "2")45 assert mm.contains_key("1") is True46 assert mm.contains_value("1", "2") is True47 mm.clear_key("1")48 assert mm.contains_key("1") is True49 assert mm.contains_value("1", "2") is False50def test_map_safe_get():51 mm = MultiMap()52 mm.add_edge("1", "2")53 assert mm.get("1") == ["2"]54 assert mm.get("2") is None55def test_map_clear():56 mm = MultiMap()57 mm.add_edge("1", "2")58 mm.add_edge("1", "3")59 mm.add_edge("2", "4")60 assert mm.get("1") == ["2", "3"]61 assert mm.contains_key("1") is True62 assert mm.contains_key("2") is True63 assert mm.contains_value("1", "2") is True64 assert mm.contains_value("1", "3") is True65 assert mm.contains_value("2", "4") is True66 assert len(mm) == 267 mm.clear()...

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 assertpy 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