Best Python code snippet using slash
fiPet.py
Source:fiPet.py  
...23            #track last updated24            self._lastUpdated = datetime.datetime.now()25        except TryFiError as e:26            LOGGER.error(f"Unable to set values for Pet.\nException: {e}\nwhile parsing {petJSON}")27            capture_exception(e)28            raise TryFiError("Unable to set Pet Details")29        except Exception as e:30            capture_exception(e)31        try:32            self._photoLink = petJSON['photos']['first']['image']['fullSize']33        except Exception as e:34            #capture_exception(e)35            LOGGER.warning(f"Cannot find photo of your pet. Defaulting to empty string.")36            self._photoLink = ""37        try:38            self._device = FiDevice(petJSON['device']['id'])39            self._device.setDeviceDetailsJSON(petJSON['device'])40        except Exception as e:41            capture_exception(e)42    def __str__(self):43        return f"Last Updated - {self.lastUpdated} - Pet ID: {self.petId} Name: {self.name} Is Lost: {self.isLost} From: {self.homeCityState} ActivityType: {self.activityType} Located: {self.currLatitude},{self.currLongitude} Last Updated: {self.currStartTime}\n \44            using Device/Collar: {self._device}"45    46    # set the Pet's current location details47    def setCurrentLocation(self, activityJSON):48        activityType = activityJSON['__typename']49        self._activityType = activityType50        self._areaName = activityJSON['areaName']51        try:52            if activityType == PET_ACTIVITY_ONGOINGWALK:53                positionSize = len(activityJSON['positions'])54                self._currLongitude = float(activityJSON['positions'][positionSize-1]['position']['longitude'])55                self._currLatitude = float(activityJSON['positions'][positionSize-1]['position']['latitude'])56                self._currStartTime = datetime.datetime.fromisoformat(activityJSON['start'].replace('Z', '+00:00'))            57            else:58                self._currLongitude = float(activityJSON['position']['longitude'])59                self._currLatitude = float(activityJSON['position']['latitude'])60                self._currStartTime = datetime.datetime.fromisoformat(activityJSON['start'].replace('Z', '+00:00'))61            try:62                self._currPlaceName = activityJSON['place']['name']63                self._currPlaceAddress = activityJSON['place']['address']64            except Exception as e:65                #capture_exception(e)66                LOGGER.warning("Could not set place, defaulting to Unknown")67                self._currPlaceName = "UNKNOWN"68                self._currPlaceAddress = "UNKNOWN"69            self._lastUpdated = datetime.datetime.now()70        except TryFiError as e:71            capture_exception(e)72            LOGGER.error(f"Unable to set values Current Location for Pet {self.name}.\nException: {e}\nwhile parsing {activityJSON}")73            raise TryFiError("Unable to set Pet Location Details")74        except Exception as e:75            capture_exception(e)76    # set the Pet's current steps, goals and distance details for daily, weekly and monthly77    def setStats(self, activityJSONDaily, activityJSONWeekly, activityJSONMonthly):78        try:79            #distance is in metres80            self._dailyGoal = int(activityJSONDaily['stepGoal'])81            self._dailySteps = int(activityJSONDaily['totalSteps'])82            self._dailyTotalDistance = float(activityJSONDaily['totalDistance'])83        except TryFiError as e:84            LOGGER.error(f"Unable to set values Daily Stats for Pet {self.name}.\nException: {e}\nwhile parsing {activityJSONDaily}")85            capture_exception(e)86            raise TryFiError("Unable to set Pet Daily Stats")87        except Exception as e:88            capture_exception(e)89        try:90            self._weeklyGoal = int(activityJSONWeekly['stepGoal'])91            self._weeklySteps = int(activityJSONWeekly['totalSteps'])92            self._weeklyTotalDistance = float(activityJSONWeekly['totalDistance'])93        except TryFiError as e:94            LOGGER.error(f"Unable to set values Weekly Stats for Pet {self.name}.\nException: {e}\nwhile parsing {activityJSONWeekly}")95            capture_exception(e)96            raise TryFiError("Unable to set Pet Weekly Stats")97        except Exception as e:98            capture_exception(e)99        try:100            self._monthlyGoal = int(activityJSONMonthly['stepGoal'])101            self._monthlySteps = int(activityJSONMonthly['totalSteps'])102            self._monthlyTotalDistance = float(activityJSONMonthly['totalDistance'])103        except TryFiError as e:104            LOGGER.error(f"Unable to set values Monthly Stats for Pet {self.name}.\nException: {e}\nwhile parsing {activityJSONMonthly}")105            capture_exception(e)106            raise TryFiError("Unable to set Pet Monthly Stats")107        except Exception as e:108            capture_exception(e)109        self._lastUpdated = datetime.datetime.now()110    # set the Pet's current rest details for daily, weekly and monthly111    def setRestStats(self, restJSONDaily, restJSONWeekly, restJSONMonthly):112        #setting default values to zero in case this feature is not supported by older collars113        self._dailyNap = 0114        self._dailySleep = 0115        self._weeklyNap = 0116        self._weeklySleep = 0117        self._monthlyNap = 0118        self._monthlySleep = 0119        try:120            for sleepAmount in restJSONDaily['restSummaries'][0]['data']['sleepAmounts']:121                if sleepAmount['type'] == 'SLEEP':122                    self._dailySleep = int(sleepAmount['duration'])123                if sleepAmount['type'] == "NAP":124                    self._dailyNap = int(sleepAmount['duration'])125        except TryFiError as e:126            LOGGER.error(f"Unable to set values Daily Rest Stats for Pet {self.name}.\nException: {e}\nwhile parsing {restJSONDaily}")127            capture_exception(e)128            raise TryFiError("Unable to set Pet Daily Rest Stats")129        except Exception as e:130            capture_exception(e)131        try:132            for sleepAmount in restJSONWeekly['restSummaries'][0]['data']['sleepAmounts']:133                if sleepAmount['type'] == 'SLEEP':134                    self._weeklySleep = int(sleepAmount['duration'])135                if sleepAmount['type'] == 'NAP':136                    self._weeklyNap = int(sleepAmount['duration'])137        except TryFiError as e:138            LOGGER.error(f"Unable to set values Weekly Rest Stats for Pet {self.name}.\nException: {e}\nwhile parsing {restJSONWeekly}")139            capture_exception(e)140            raise TryFiError("Unable to set Pet Weekly Rest Stats")141        except Exception as e:142            capture_exception(e)143        try:144            for sleepAmount in restJSONMonthly['restSummaries'][0]['data']['sleepAmounts']:145                if sleepAmount['type'] == 'SLEEP':146                    self._monthlySleep = int(sleepAmount['duration'])147                if sleepAmount['type'] == 'NAP':148                    self._monthlyNap = int(sleepAmount['duration'])149        except TryFiError as e:150            LOGGER.error(f"Unable to set values Monthly Rest Stats for Pet {self.name}.\nException: {e}\nwhile parsing {restJSONMonthly}")151            capture_exception(e)152            raise TryFiError("Unable to set Pet Monthly Rest Stats")153        except Exception as e:154            capture_exception(e)155    # Update the Stats of the pet156    def updateStats(self, sessionId):157        try:158            pStatsJSON = query.getCurrentPetStats(sessionId,self.petId)159            self.setStats(pStatsJSON['dailyStat'],pStatsJSON['weeklyStat'],pStatsJSON['monthlyStat'])160            return True161        except Exception as e:162            LOGGER.error(f"Could not update stats for Pet {self.name}.\n{e}")163            capture_exception(e)164    # Update the Stats of the pet165    def updateRestStats(self, sessionId):166        try:167            pRestStatsJSON = query.getCurrentPetRestStats(sessionId,self.petId)168            self.setRestStats(pRestStatsJSON['dailyStat'],pRestStatsJSON['weeklyStat'],pRestStatsJSON['monthlyStat'])169            return True170        except Exception as e:171            LOGGER.error(f"Could not update rest stats for Pet {self.name}.\n{e}")172            capture_exception(e)173    # Update the Pet's GPS location174    def updatePetLocation(self, sessionId):175        try:176            pLocJSON = query.getCurrentPetLocation(sessionId,self.petId)177            self.setCurrentLocation(pLocJSON)178            return True179        except Exception as e:180            LOGGER.error(f"Could not update Pet: {self.name}'s location.\n{e}")181            capture_exception(e)182            return False183    184    # Update the device/collar details for this pet185    def updateDeviceDetails(self, sessionId):186        try:187            deviceJSON = query.getDevicedetails(sessionId, self.petId)188            self.device.setDeviceDetailsJSON(deviceJSON['device'])189            return True190        except Exception as e:191            LOGGER.error(f"Could not update Device/Collar information for Pet: {self.name}\n{e}")192            capture_exception(e)193            return False194    # Update all details regarding this pet195    def updateAllDetails(self, sessionId):196        self.updateDeviceDetails(sessionId)197        self.updatePetLocation(sessionId)198        self.updateStats(sessionId)199        self.updateRestStats(sessionId)200    # set the color code of the led light on the pet collar201    def setLedColorCode(self, sessionId, colorCode):202        try:203            moduleId = self.device.moduleId204            ledColorCode = int(colorCode)205            setColorJSON = query.setLedColor(sessionId, moduleId, ledColorCode)206            try:207                self.device.setDeviceDetailsJSON(setColorJSON['setDeviceLed'])208            except Exception as e:209                LOGGER.warning(f"Updated LED Color but could not get current status for Pet: {self.name}\nException: {e}")210                capture_exception(e)211            return True212        except Exception as e:213            LOGGER.error(f"Could not complete Led Color request:\n{e}")214            capture_exception(e)215            return False216    217    # turn on or off the led light. action = True will enable the light, false turns off the light218    def turnOnOffLed(self, sessionId, action):219        try:220            moduleId = self.device.moduleId221            onOffResponse = query.turnOnOffLed(sessionId, moduleId, action)222            try:223                self.device.setDeviceDetailsJSON(onOffResponse['updateDeviceOperationParams'])224            except Exception as e:225                LOGGER.warning(f"Action: {action} was successful however unable to get current status for Pet: {self.name}")226                capture_exception(e)227            return True228        except Exception as e:229            LOGGER.error(f"Could not complete LED request:\n{e}")230            capture_exception(e)231            return False232    # set the lost dog mode to Normal or Lost Dog. Action is true for lost dog and false for normal (not lost)233    def setLostDogMode(self, sessionId, action):234        try:235            moduleId = self.device.moduleId236            petModeResponse = query.setLostDogMode(sessionId, moduleId, action)237            try:238                self.device.setDeviceDetailsJSON(petModeResponse['updateDeviceOperationParams'])239            except Exception as e:240                LOGGER.warning(f"Action: {action} was successful however unable to get current status for Pet: {self.name}")241                capture_exception(e)242            return True243        except Exception as e:244            LOGGER.error(f"Could not complete Lost Dog Mode request:\n{e}")245            LOGGER.error(f"Could not complete turn on/off light where ledEnable is {action}.\nException: {e}")246            capture_exception(e)247            return False248    @property249    def device(self):250        return self._device251    @property252    def petId(self):253        return self._petId254    @property255    def name(self):256        return self._name257    @property258    def homeCityState(self):259        return self._homeCityState260    @property...query.py
Source:query.py  
...11        LOGGER.debug(f"getUserDetails: {response}")12        return response['data']['currentUser']13    except Exception as e:14        LOGGER.error("Error performing query: " + e)15        capture_exception(e)16def getPetList(sessionId):17    try:18        qString = QUERY_CURRENT_USER_FULL_DETAIL + FRAGMENT_USER_DETAILS \19            + FRAGMENT_USER_FULL_DETAILS + FRAGMENT_PET_PROFILE + FRAGEMENT_BASE_PET_PROFILE \20            + FRAGMENT_BASE_DETAILS + FRAGMENT_POSITION_COORDINATES + FRAGMENT_BREED_DETAILS \21            + FRAGMENT_PHOTO_DETAILS + FRAGMENT_DEVICE_DETAILS + FRAGMENT_LED_DETAILS + FRAGMENT_OPERATIONAL_DETAILS \22            + FRAGMENT_CONNECTION_STATE_DETAILS23        response = query(sessionId, qString)24        LOGGER.debug(f"getPetList: {response}")25        return response['data']['currentUser']['userHouseholds'][0]['household']['pets']26    except Exception as e:27        LOGGER.error("Error performing query: " + e)28        capture_exception(e)29def getBaseList(sessionId):30    try:31        qString = QUERY_CURRENT_USER_FULL_DETAIL + FRAGMENT_USER_DETAILS \32            + FRAGMENT_USER_FULL_DETAILS + FRAGMENT_PET_PROFILE + FRAGEMENT_BASE_PET_PROFILE \33            + FRAGMENT_BASE_DETAILS + FRAGMENT_POSITION_COORDINATES + FRAGMENT_BREED_DETAILS \34            + FRAGMENT_PHOTO_DETAILS + FRAGMENT_DEVICE_DETAILS + FRAGMENT_LED_DETAILS + FRAGMENT_OPERATIONAL_DETAILS \35            + FRAGMENT_CONNECTION_STATE_DETAILS36        response = query(sessionId, qString)37        LOGGER.debug(f"getBaseList: {response}")38        return response['data']['currentUser']['userHouseholds'][0]['household']['bases']39    except Exception as e:40        LOGGER.error("Error performing query: " + e)41        capture_exception(e)42def getCurrentPetLocation(sessionId, petId):43    try:44        qString = QUERY_PET_CURRENT_LOCATION.replace(VAR_PET_ID, petId) + FRAGMENT_ONGOING_ACTIVITY_DETAILS \45            + FRAGMENT_UNCERTAINTY_DETAILS + FRAGMENT_CIRCLE_DETAILS + FRAGMENT_LOCATION_POINT \46            + FRAGMENT_PLACE_DETAILS + FRAGMENT_USER_DETAILS + FRAGMENT_POSITION_COORDINATES47        response = query(sessionId, qString)48        LOGGER.debug(f"getCurrentPetLocation: {response}")49        return response['data']['pet']['ongoingActivity']50    except Exception as e:51        LOGGER.error("Error performing query: " + e)52        capture_exception(e)53def getCurrentPetStats(sessionId, petId):54    try:55        qString = QUERY_PET_ACTIVITY.replace(VAR_PET_ID, petId) + FRAGMENT_ACTIVITY_SUMMARY_DETAILS56        response = query(sessionId, qString)57        LOGGER.debug(f"getCurrentPetStats: {response}")58        return response['data']['pet']59    except Exception as e:60        LOGGER.error("Error performing query: " + e)61        capture_exception(e)62def getCurrentPetRestStats(sessionId, petId):63    try:64        qString = QUERY_PET_REST.replace(VAR_PET_ID, petId) + FRAGMENT_REST_SUMMARY_DETAILS65        response = query(sessionId, qString)66        LOGGER.debug(f"getCurrentPetStats: {response}")67        return response['data']['pet']68    except Exception as e:69        LOGGER.error("Error performing query: " + e)70        capture_exception(e)71def getDevicedetails(sessionId, petId):72    try:73        qString = QUERY_PET_DEVICE_DETAILS.replace(VAR_PET_ID, petId) + FRAGMENT_PET_PROFILE + FRAGEMENT_BASE_PET_PROFILE + FRAGMENT_DEVICE_DETAILS + FRAGMENT_LED_DETAILS + FRAGMENT_OPERATIONAL_DETAILS + FRAGMENT_CONNECTION_STATE_DETAILS + FRAGMENT_USER_DETAILS + FRAGMENT_BREED_DETAILS + FRAGMENT_PHOTO_DETAILS74        response = query(sessionId, qString)75        LOGGER.debug(f"getDevicedetails: {response}")76        return response['data']['pet']77    except Exception as e:78        LOGGER.error("Error performing query: " + e)79        capture_exception(e)80def setLedColor(sessionId, deviceId, ledColorCode):81    try:82        qString = MUTATION_SET_LED_COLOR + FRAGMENT_DEVICE_DETAILS + FRAGMENT_OPERATIONAL_DETAILS + FRAGMENT_CONNECTION_STATE_DETAILS + FRAGMENT_USER_DETAILS + FRAGMENT_LED_DETAILS83        qVariables = '{"moduleId":"'+deviceId+'","ledColorCode":'+str(ledColorCode)+'}'84        response = mutation(sessionId, qString, qVariables)85        LOGGER.debug(f"setLedColor: {response}")86        return response['data']87    except Exception as e:88        LOGGER.error("Error performing query: " + e)89        capture_exception(e)90def turnOnOffLed(sessionId, moduleId, ledEnabled):91    try:92        qString = MUTATION_DEVICE_OPS + FRAGMENT_DEVICE_DETAILS + FRAGMENT_OPERATIONAL_DETAILS + FRAGMENT_CONNECTION_STATE_DETAILS + FRAGMENT_USER_DETAILS + FRAGMENT_LED_DETAILS93        qVariables = '{"input": {"moduleId":"'+moduleId+'","ledEnabled":'+str(ledEnabled).lower()+'}}'94        response = mutation(sessionId, qString, qVariables)95        LOGGER.debug(f"turnOnOffLed: {response}")96        return response['data']97    except Exception as e:98        LOGGER.error("Error performing query: " + e)99        capture_exception(e)100def setLostDogMode(sessionId, moduleId, action):101    try:102        if action:103            mode = PET_MODE_LOST104        else:105            mode = PET_MODE_NORMAL106        qString = MUTATION_DEVICE_OPS + FRAGMENT_DEVICE_DETAILS + FRAGMENT_OPERATIONAL_DETAILS + FRAGMENT_CONNECTION_STATE_DETAILS + FRAGMENT_USER_DETAILS + FRAGMENT_LED_DETAILS107        qVariables = '{"input": {"moduleId":"'+moduleId+'","mode":"'+mode+'"}}'108        response = mutation(sessionId, qString, qVariables)109        LOGGER.debug(f"setLostDogMode: {response}")110        return response['data']111    except Exception as e:112        LOGGER.error("Error performing query: " + e)113        capture_exception(e)114def getGraphqlURL():115    try:116        return API_HOST_URL_BASE + API_GRAPHQL117    except Exception as e:118        LOGGER.error("Error performing query: " + e)119        capture_exception(e)120def mutation(sessionId, qString, qVariables):121    try:122        jsonObject = None123        url = getGraphqlURL()124        params = {"query": qString, "variables": qVariables}125        jsonObject = execute(url, sessionId, params=params, method='POST').json()126        return jsonObject127    except Exception as e:128        LOGGER.error("Error performing query: " + e)129        capture_exception(e)130        131def query(sessionId, qString):132    try:133        jsonObject = None134        url = getGraphqlURL()135        params={'query': qString}136        jsonObject = execute(url, sessionId, params=params).json()137        return jsonObject138    except Exception as e:139        LOGGER.error("Error performing query: " + e)140        capture_exception(e)141def execute(url, sessionId, method='GET', params=None, cookies=None):142    response = None143    try:144        if method == 'GET':145            response = sessionId.get(url, params=params)146        elif method == 'POST':147            response = sessionId.post(url, data=params)148        else:149            raise TryFiError(f"Method Passed was invalid: {method}")150    except requests.RequestException as e:151        capture_exception(e)152        raise requests.RequestException(e)153    except Exception as e:154            capture_exception(e)155    return response...weimembermgmt.py
Source:weimembermgmt.py  
...15		self.click_element("//button[@class='btn' and (text()='æ¥è¯¢')]")16	def click_class_iconyen(self, index):17		element_iconye = "//table[@id='listTable']/tbody/tr[%s]/td[10]/a[1]/i" %index18		element_membertrade_iconye = "//i[@class='icon-yen']"19		if self.capture_exception(element_iconye) == False:20			self.click_element(element_iconye)21			self.wait_until_page_contains_element("//form[@id='Form1']/div[3]/button[2]",settings.TIMEOUT)22		elif self.capture_exception(element_membertrade_iconye) == False:23			self.click_element(element_membertrade_iconye)24			self.wait_until_page_contains_element("//form[@id='Form1']/div[3]/button[2]",settings.TIMEOUT)25		else:26			print 'Not Match Element'			27	def click_class_iconusd(self, index):28		element_iconusd = "//table[@id='listTable']/tbody/tr[%s]/td[10]/a[2]/i" %index29		element_membertrade_iconusd = "//i[@class='icon-usd']"30		if self.capture_exception(element_iconusd) == False:31			self.click_element(element_iconusd)32			self.wait_until_page_contains_element("//form[@id='Form1']/div[3]/button[1]",settings.TIMEOUT)33		elif self.capture_exception(element_membertrade_iconusd) == False:34			self.click_element(element_membertrade_iconusd)35			self.wait_until_page_contains_element("//form[@id='Form1']/div[3]/button[1]",settings.TIMEOUT)36		else:37			print 'Not Match Element'38	def click_class_iconmoney(self, index):39		element_iconmoney = "//table[@id='listTable']/tbody/tr[%s]/td[10]/a[3]/i" %index40		element_membertrade_iconmoney = "//i[@class='icon-money']"41		if self.capture_exception(element_iconmoney) == False:42			self.click_element(element_iconmoney)43			self.wait_until_page_contains_element("//form[@id='Form3]/div/div/div/input[@name='nick']",settings.TIMEOUT)44		elif self.capture_exception(element_membertrade_iconmoney) == False:45			self.click_element(element_membertrade_iconmoney)46			self.wait_until_page_contains_element("//form[@id='Form3]/div/div/div/input[@name='nick']",settings.TIMEOUT)47		else:48			print 'Not Match Element'49	def click_class_iconbarchart(self, index):50		self.click_element("//table[@id='listTable']/tbody/tr[%s]/td[10]/a[4]/i" %index)	51	def click_class_iconlock(self, index):52		self.click_element("//table[@id='listTable']/tbody/tr[%s]/td[10]/a[6]/i" %index)53	def input_id_price(self, text):54		self.input_text('price', text)55	def input_name_nick(self, text):56		element_iconye = "//form[@id='Form1']/div/div/div/input[@name='nick']"57		element_iconusd = "//form[@id='Form2']/div/div/div/input[@name='nick']"58		element_iconmoney = "//form[@id='Form3']/div/div/div/input[@name='nick']"59		if self.capture_exception(element_iconye) == False:60			self.input_text(element_iconye, text)61		elif self.capture_exception(element_iconusd) == False:62			self.input_text(element_iconusd, text)63		elif self.capture_exception(element_iconmoney) == False:64			self.input_text(element_iconmoney, text)65		else:66			print 'Not Match Element'67	def click_class_btnbtnprimary(self):68		element_iconye = "//form[@id='Form1']/div[3]/button[1]"69		element_iconusd = "//form[@id='Form2']/div[3]/button[1]"70		element_iconmoney = "//form[@id='Form3']/div[3]/button[1]"71		element_icongift = "//form[@id='Form3']/div[4]/button[2]"72		if self.capture_exception(element_iconye) == False:73			self.click_element(element_iconye)74		elif self.capture_exception(element_iconusd) == False:75			self.click_element(element_iconusd)76		elif self.capture_exception(element_iconmoney) == False:77			self.click_element(element_iconmoney)78		elif self.capture_exception(icongift) == False:79			self.click_element(icongift)80		else:81			print 'Not Match Element'82	def click_class_btnbtndefault(self):83		element_iconye = "//form[@id='Form1']/div[3]/button[2]"84		element_iconusd = "//form[@id='Form2']/div[3]/button[2]"85		element_iconmoney = "//form[@id='Form3']/div[3]/button[2]"86		element_icongift = "//form[@id='Form3']/div[4]/button[2]"87		if self.capture_exception(element_iconye) == False:88			self.click_element(element_iconye)89		elif self.capture_exception(element_iconusd) == False:90			self.click_element(element_iconusd)91		elif self.capture_exception(element_iconmoney) == False:92			self.click_element(element_iconmoney)93		elif self.capture_exception(icongift) == False:94			self.click_element(icongift)95		else:96			print 'Not Match Element'97	def input_id_money(self, text):98		self.input_text('money', text)99	def input_id_score(self, text):100		self.input_text('score', text)101	def input_id_truename(self, text):102		self.input_text('truename', text)103	def click_class_freeze(self):...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
