@@ -30,8 +30,20 @@ Just add ``ua-parser`` to your project's dependencies, or run
3030
3131 to install in the current environment.
3232
33- Getting Started
34- ---------------
33+ Installing `google-re2 <https://pypi.org/project/google-re2/ >`_ is
34+ *strongly * recommended as it leads to *significantly * better
35+ performances. This can be done directly via the ``re2 `` optional
36+ dependency:
37+
38+ .. code-block :: sh
39+
40+ $ pip install ' ua_parser[re2]'
41+
42+ If ``re2 `` is available, ``ua-parser `` will simply use it by default
43+ instead of the pure-python resolver.
44+
45+ Quick Start
46+ -----------
3547
3648Retrieve all data on a user-agent string
3749~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -41,25 +53,25 @@ Retrieve all data on a user-agent string
4153 >> > from ua_parser import parse
4254 >> > ua_string = ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36'
4355 >> > parse(ua_string) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
44- ParseResult (user_agent = UserAgent(family = ' Chrome' ,
45- major = ' 41' ,
46- minor = ' 0' ,
47- patch = ' 2272' ,
48- patch_minor = ' 104' ),
49- os = OS(family = ' Mac OS X' ,
50- major = ' 10' ,
51- minor = ' 9' ,
52- patch = ' 4' ,
53- patch_minor = None ),
54- device = Device(family = ' Mac' ,
55- brand = ' Apple' ,
56- model = ' Mac' ),
57- string = ' Mozilla/5.0 (Macintosh; Intel Mac OS...
56+ Result (user_agent = UserAgent(family = ' Chrome' ,
57+ major = ' 41' ,
58+ minor = ' 0' ,
59+ patch = ' 2272' ,
60+ patch_minor = ' 104' ),
61+ os = OS(family = ' Mac OS X' ,
62+ major = ' 10' ,
63+ minor = ' 9' ,
64+ patch = ' 4' ,
65+ patch_minor = None ),
66+ device = Device(family = ' Mac' ,
67+ brand = ' Apple' ,
68+ model = ' Mac' ),
69+ string = ' Mozilla/5.0 (Macintosh; Intel Mac OS...
5870
5971Any datum not found in the user agent string is set to `` None `` ::
6072
6173 >> > parse(" " )
62- ParseResult (user_agent = None , os = None , device = None , string = ' ' )
74+ Result (user_agent = None , os = None , device = None , string = ' ' )
6375
6476Extract only browser data from user- agent string
6577~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -94,43 +106,3 @@ Extract device information from user-agent string
94106 >> > ua_string = ' Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36'
95107 >> > parse_device(ua_string)
96108 Device(family = ' Mac' , brand = ' Apple' , model = ' Mac' )
97-
98- Parser
99- ~~~~~~
100-
101- Parsers expose the same functions (`` parse`` , `` parse_user_agent`` ,
102- `` parse_os`` , and `` parse_device`` ) as the top- level of the package,
103- however these are all * utility* methods.
104-
105- The actual protocol of parsers, and the one method which must be
106- implemented / overridden is ::
107-
108- def __call__ (self , str , Components, / ) -> ParseResult:
109-
110- It' s similar to but more flexible than ``parse``:
111-
112- - The `` str `` is the user agent string.
113- - The `` Components`` is a hint, through which the caller requests the
114- domain (component) they are looking for , any combination of
115- `` Components.USER_AGENT `` , `` Components.OS `` , and
116- `` Components.DEVICE `` . `` Domains.ALL `` exists as a convenience alias
117- for the combination of all three.
118-
119- The parser * must* return at least the requested information, but if
120- that' s more convenient or no more expensive it *can* return more.
121- - The `` ParseResult`` is similar to `` CompleteParseResult`` , except
122- all the attributes are `` Optional`` and it has a `` components:
123- Components`` attribute which specifies whether a component was never
124- requested (its value for the user agent string is unknown) or it has
125- been requested but could not be resolved (no match was found for the
126- user agent).
127-
128- `` ParseResult.complete()`` convert to a `` CompleteParseResult`` if
129- all the components are set , and raise an exception otherwise. If
130- some of the components are set to `` None `` , they' ll be swapped for a
131- default value.
132-
133- Calling the parser directly is part of the public API . One of the
134- advantage is that it does not return default values, as such it allows
135- more easily differentiating between a non- match (= `` None `` ) and a
136- default fallback (`` family = " Other" `` ).
0 commit comments