\r\n

51Degrees Device Detection Python  4.3

Device Detection services for 51Degrees Pipeline

hash/failuretomatch.py

This example shows how the 51Degrees device detection engine deals with evidence that does not match any known device. In this scenario, 'HasValue' can be used to check if the property has been populated by the engine. If it hasn't then 'NoValueMessage' can be used to get the reason why.

Expected output:

1 Is user agent '--' a mobile?
2 The results contained a null profile for the component which the required property belongs to.
3 
4 Is user agent 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114' a mobile?
5 true
1 # *********************************************************************
2 # This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 # Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
4 # Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
5 #
6 # This Original Work is licensed under the European Union Public Licence (EUPL)
7 # v.1.2 and is subject to its terms as set out below.
8 #
9 # If a copy of the EUPL was not distributed with this file, You can obtain
10 # one at https://opensource.org/licenses/EUPL-1.2.
11 #
12 # The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 # amended by the European Commission) shall be deemed incompatible for
14 # the purposes of the Work and the provisions of the compatibility
15 # clause in Article 5 of the EUPL shall not apply.
16 #
17 # If using the Work as, or as part of, a network application, by
18 # including the attribution notice(s) required under Article 5 of the EUPL
19 # in the end user terms of the application under an appropriate heading,
20 # such notice(s) shall fulfill the requirements of that article.
21 # ********************************************************************
22 
23 
37 
38 from fiftyone_devicedetection_onpremise.devicedetection_onpremise_pipelinebuilder import DeviceDetectionOnPremisePipelineBuilder
39 
40 # First create the device detection pipeline with the desired settings.
41 
42 data_file = "fiftyone_devicedetection_onpremise/device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash"
43 
44 pipeline = DeviceDetectionOnPremisePipelineBuilder(
45  data_file_path = data_file,
46  licence_keys = "",
47  performance_profile = 'MaxPerformance',
48  auto_update=False).build()
49 
50 # We create a FlowData object from the pipeline
51 # this is used to add evidence to and then process
52 
53 flowdata1 = pipeline.create_flowdata()
54 
55 # Here we add a User-Agent of an iphone as evidence
56 
57 iphone_ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114"
58 
59 flowdata1.evidence.add("header.user-agent", iphone_ua)
60 
61 # Now we process the FlowData
62 
63 flowdata1.process()
64 
65 # To check whether the User-Agent is a mobile device we look at the ismobile property
66 # inside the Device Detection Engine
67 
68 # first we check if this has a meaningful result
69 
70 print("Is User-Agent " + iphone_ua + " a mobile device?: ")
71 if flowdata1.device.ismobile.has_value():
72  print(flowdata1.device.ismobile.value())
73 else:
74  # Output why the value isn't meaningful
75  print(flowdata1.device.ismobile.no_value_message())
76 
77 # Now we do the same with a new User-Agent, this time a corrupted one
78 
79 badUA = "--"
80 
81 flowdata2 = pipeline.create_flowdata()
82 
83 flowdata2.evidence.add("header.user-agent", badUA)
84 
85 flowdata2.process()
86 
87 print("Is User-Agent " + badUA + " a mobile device?: ")
88 if flowdata2.device.ismobile.has_value():
89  print(flowdata2.device.ismobile.value())
90 else:
91  # Output why the value isn't meaningful
92  print(flowdata2.device.ismobile.no_value_message())