Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 1 | import pexpect,os |
| 2 | import time |
| 3 | import json |
| 4 | import collections |
| 5 | import sys |
| 6 | import robot |
| 7 | import os.path |
| 8 | from os.path import expanduser |
| 9 | |
| 10 | class utils(object): |
| 11 | |
| 12 | @staticmethod |
| 13 | def listToDict(alist, intListIndex): |
| 14 | dictInfo = alist[int(intListIndex)] |
| 15 | return dictInfo |
| 16 | |
| 17 | @staticmethod |
| 18 | def jsonToList(strFile, strListName): |
| 19 | data = json.loads(open(strFile).read()) |
| 20 | #print "data...",data |
| 21 | dataList = data[strListName] |
| 22 | return dataList |
| 23 | |
Suchitra.Vemuri | 8522006 | 2016-10-25 10:44:11 -0700 | [diff] [blame] | 24 | ''' |
| 25 | @method compare_dict |
| 26 | @Description: validates if contents of dict1 exists in dict2 |
| 27 | @params: dict1 = input_data entered through api |
| 28 | dict2 = retrieved data from GET method |
| 29 | returns True if contents of dict1 exists in dict2 |
| 30 | ''' |
| 31 | |
| 32 | @staticmethod |
| 33 | def compare_dict(dict1, dict2): |
| 34 | print "input_data", dict1 |
| 35 | print "get data", dict2 |
| 36 | if dict1 == None or dict2 == None: |
| 37 | return False |
| 38 | |
| 39 | if type(dict1) is not dict or type(dict2) is not dict: |
| 40 | return False |
| 41 | |
| 42 | for key1,value1 in dict1.items(): |
| 43 | try: |
| 44 | if key1 in dict2: |
| 45 | for key2, value2 in value1.items(): |
| 46 | if value2 != dict2[key1][key2]: |
| 47 | return False |
| 48 | except: |
| 49 | print "Additional items" |
| 50 | return True |
| 51 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 52 | ''' |
| 53 | @method search_dictionary |
| 54 | @Description: Searches for a key in the provided nested dictionary |
| 55 | @params: input_dict = dictionary to be searched |
| 56 | search_key = name of the key to be searched for |
| 57 | returns two values: search_key value and status of the search. |
| 58 | True if found (False when not found) |
| 59 | |
| 60 | ''' |
| 61 | def search_dictionary(self,input_dict, search_key): |
| 62 | input_keys = input_dict.keys() |
| 63 | key_value = '' |
| 64 | found = False |
| 65 | for key in input_keys: |
| 66 | if key == search_key: |
| 67 | key_value = input_dict[key] |
| 68 | found = True |
| 69 | break |
| 70 | elif type(input_dict[key]) == dict: |
| 71 | key_value, found = self.search_dictionary(input_dict[key],search_key) |
| 72 | if found == True: |
| 73 | break |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 74 | elif type(input_dict[key]) == list: |
| 75 | if not input_dict[key]: |
| 76 | found = False |
| 77 | break |
| 78 | for item in input_dict[key]: |
| 79 | if isinstance(item, dict): |
| 80 | key_value, found = self.search_dictionary(item, search_key) |
| 81 | if found == True: |
| 82 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 83 | return key_value,found |
| 84 | ''' |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 85 | @method getDictFromListOfDict |
| 86 | return key_value,found |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 87 | @Description: Searches for the dictionary in the provided list of dictionaries |
| 88 | that matches the value of the key provided |
| 89 | @params : List of dictionaries(getResponse Data from the URL), |
| 90 | SearchKey - Key that needs to be searched for (ex: account_num) |
| 91 | searchKeyValue - Value of the searchKey (ex: 21) |
| 92 | @Returns: Dictionary returned when match found for searchKey with the corresponding |
| 93 | searchKeyValue provided |
| 94 | ''' |
| 95 | |
| 96 | def getDictFromListOfDict(self, getJsonDataList, searchKey, searchKeyValue): |
| 97 | return_dict = {} |
| 98 | result = '' |
| 99 | for data in getJsonDataList: |
| 100 | return_dict = {} |
| 101 | found = False |
| 102 | input_keys = data.keys() |
| 103 | for key in input_keys: |
| 104 | if key == searchKey and str(data[key]) == str(searchKeyValue): |
| 105 | found = True |
| 106 | return_dict = data |
| 107 | break |
| 108 | elif type(data[key]) == dict: |
| 109 | result, found = self.search_dictionary(data[key],searchKey) |
| 110 | if found == True and str(result) == str(searchKeyValue): |
| 111 | return_dict = data |
| 112 | break |
| 113 | elif type(data[key]) == list: |
| 114 | for item in data[key]: |
| 115 | if isinstance(item, dict): |
| 116 | result, found = self.search_dictionary(data[key], searchKey) |
| 117 | if found == True and str(result) == str(searchKeyValue): |
| 118 | return_dict = data |
| 119 | break |
| 120 | if return_dict: |
| 121 | break |
| 122 | return return_dict |
| 123 | |
| 124 | |
| 125 | ''' |
| 126 | @method getFieldValueFromDict |
| 127 | @params : search_dict - Dictionary to be searched |
| 128 | field - Key to be searched for (ex: account_num) |
| 129 | @Returns: Returns the value of the Key that was provided |
| 130 | ''' |
| 131 | def getFieldValueFromDict(self,search_dict, field): |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 132 | print "search_dict", search_dict, "field...", field |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 133 | results = '' |
| 134 | found = False |
| 135 | input_keys = search_dict.keys() |
| 136 | for key in input_keys: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 137 | print "key...", key |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 138 | if key == field: |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 139 | print "entered if..." |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 140 | results = search_dict[key] |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 141 | print "results...", results |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 142 | if not results: |
| 143 | found = True |
| 144 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 145 | elif type(search_dict[key]) == dict: |
| 146 | results, found = self.search_dictionary(search_dict[key],field) |
| 147 | if found == True: |
| 148 | break |
| 149 | elif type(search_dict[key]) == list: |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 150 | if not search_dict[key]: |
| 151 | found = False |
| 152 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 153 | for item in search_dict[key]: |
| 154 | if isinstance(item, dict): |
| 155 | results, found = self.search_dictionary(item, field) |
| 156 | if found == True: |
| 157 | break |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 158 | if results: |
| 159 | break |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 160 | |
| 161 | return results |
| 162 | |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 163 | def setFieldValueInDict(self,input_dict,field,field_value): |
| 164 | input_dict[field]=field_value |
| 165 | return input_dict |
| 166 | |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 167 | ''' |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 168 | #Test |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 169 | dict_list = { |
| 170 | "humanReadableName": "cordSubscriber-17", |
| 171 | "id": 17, |
| 172 | "features": { |
| 173 | "uplink_speed": 1000000000, |
| 174 | "downlink_speed": 1000000000, |
| 175 | "status": "enabled" |
| 176 | }, |
| 177 | "identity": { |
| 178 | "account_num": "20", |
| 179 | "name": "My House" |
| 180 | }, |
| 181 | "related": {} |
| 182 | } |
| 183 | input_dict = { |
| 184 | "s_tag" : "111", |
| 185 | "c_tag" : "222", |
| 186 | "subscriber" : "" |
| 187 | } |
| 188 | new_value = 3 |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 189 | test = utils() |
Suchitra.Vemuri | fdb220a | 2016-10-19 14:09:53 -0700 | [diff] [blame] | 190 | #data=test.jsonToList("Subscribers.json","SubscriberInfo") |
| 191 | #print test.jsonToList("Subscribers.json","SubscriberInfo") |
| 192 | #print "index 1...",test.listToDict(data,1) |
Suchitra.Vemuri | 8be1880 | 2016-11-16 16:59:54 -0800 | [diff] [blame] | 193 | #result = test.getDictFromListOfDict(dict_list,"email",21) |
Suchitra.Vemuri | d203534 | 2016-11-22 17:44:40 -0800 | [diff] [blame] | 194 | #result = test.getFieldValueFromDict(dict_list,"id") |
| 195 | #result = test.getDictFromListOfDict(dict_list,"account_num",21) |
| 196 | result = test.setFieldValueInDict(input_dict,"subscriber",new_value) |
Suchitra.Vemuri | 32e03c2 | 2016-11-03 11:57:53 -0700 | [diff] [blame] | 197 | print "finalllllll result....", result |
| 198 | ''' |