From 0.x to 1.0¶
Don’t Touch A Thing¶
The first and simplest way to upgrade is to not do anything: the 0.x API is still present in 1.x and won’t be removed for a long time, possibly ever.
While it is unlikely to get updated any further and will eventually (hopefully?) fall behind, if you can’t be arsed you probably don’t have to do anything for now, or just now.
Default Ruleset¶
While the 1.0 API was designed to better respect PEP 8 and support
typing, it was also designed to easily be transitioned to.
Given a 0.x API not using YAML, the conversion consists of:
updating the import from
ua_parser.user_agent_parserto justua_parserlowercasing the function names
adding
with_defaults()after the function callwrapping the entire thing in
dataclasses.asdict()
For example, given
>>> from ua_parser import user_agent_parser
>>> pprint(user_agent_parser.Parse(ua_string))
{'device': {'brand': 'Apple', 'family': 'Mac', 'model': 'Mac'},
'os': {'family': 'Mac OS X',
'major': '10',
'minor': '9',
'patch': '4',
'patch_minor': None},
'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',
'user_agent': {'family': 'Chrome',
'major': '41',
'minor': '0',
'patch': '2272'}}
the 1.0 version would be:
>>> import dataclasses
>>> import ua_parser
>>> pprint(dataclasses.asdict(ua_parser.parse(ua_string).with_defaults()))
{'device': {'brand': 'Apple', 'family': 'Mac', 'model': 'Mac'},
'os': {'family': 'Mac OS X',
'major': '10',
'minor': '9',
'patch': '4',
'patch_minor': None},
'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',
'user_agent': {'family': 'Chrome',
'major': '41',
'minor': '0',
'patch': '2272',
'patch_minor': '104'}}
Note
by default, the 1.0 API simply leaves entries
Nonewhen no data was matched,with_defaults()fills them with the default value for the domain matching the 0.x APIthe 1.0 API returns
typed dataclasses rather than untyped dicts, hence the necessary conversion to dict
YAML Ruleset¶
The 1.0 API does not support UA_PARSER_YAML anymore, instead
it provides a layered API which lets clients use multiple parsers at
the same time, and load rulesets from various datasources.
Legacy YAML support can be added via a pretty small shim:
import ua_parser
from ua_parser.loaders import load_yaml
if yaml_path := os.environ.get("UA_PARSER_YAML"):
ua_parser.parser = ua_parser.Parser.from_matchers(
load_yaml(yaml_path))
This will load the YAML file, create a parser from it, and set the new parser as the global parser used by the top-level utility functions.
See also