### Install ua-parser with Regex Support Source: https://github.com/ua-parser/uap-python/blob/master/doc/installation.md Install the ua-parser library with the recommended regex feature for improved performance. This command adds 'ua-parser[regex]' to your project's dependencies. ```sh $ pip install 'ua-parser[regex]' ``` -------------------------------- ### Install ua-parser with Regex and YAML Support Source: https://github.com/ua-parser/uap-python/blob/master/doc/installation.md Install the ua-parser library with both the regex and PyYaml optional dependencies. This command adds 'ua-parser[regex,yaml]' to your project's dependencies. ```sh $ pip install 'ua-parser[regex,yaml]' ``` -------------------------------- ### Install ua-parser with RE2 Support Source: https://github.com/ua-parser/uap-python/blob/master/doc/installation.md Install the ua-parser library with the google-re2 optional dependency for efficient regex matching. This command adds 'ua-parser[re2]' to your project's dependencies. ```sh $ pip install 'ua-parser[re2]' ``` -------------------------------- ### Install ua-parser with YAML Support Source: https://github.com/ua-parser/uap-python/blob/master/doc/installation.md Install the ua-parser library with the PyYaml optional dependency to enable loading rulesets from YAML files. This command adds 'ua-parser[yaml]' to your project's dependencies. ```sh $ pip install 'ua-parser[yaml]' ``` -------------------------------- ### Custom foo_resolver Implementation Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md A bespoke resolver that specifically matches and parses user agent strings starting with 'fooapp/'. It returns a PartialResult with parsed user agent, device, and original UA, or a failure result if the UA does not match. ```python def foo_resolver(ua: str, domain: Domain, /) -> PartialResult: if not ua.startswith('fooapp/'): # not our application, match failure return PartialResult(domain, None, None, None, ua) # we've defined our UA as $appname/$version/$user-token app, version, user = ua.split('/', 3) major, minor = version.split('.') return PartialResult( domain, UserAgent(app, major, minor), None, Device(user), ua, ) ``` -------------------------------- ### Set Custom Global Parser Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Replace the default global parser with a custom one, affecting all subsequent calls to global parsing functions. This example demonstrates setting a simple custom parser for the 'foo' user agent. ```python import ua_parser import ua_parser.loaders ua_parser.parse("foo") ua_parser.parser = ua_parser.Parser.from_matchers( ua_parser.loaders.load_data(( [{"regex": "(foo)"} ], [], [], )) ) ua_parser.parse("foo") ``` -------------------------------- ### Load YAML Data for Matchers Source: https://github.com/ua-parser/uap-python/blob/master/doc/api.md Loads YAML data structured according to regexes.yaml. The loader parameter allows customization of matcher generation, defaulting to eager matchers but supporting lazy matchers via load_lazy(). Requires PyYAML to be installed. ```python from ua_parser.loaders import load_yaml from ua_parser.data import load_data from pathlib import Path # Example usage with default eager matchers matchers = load_yaml(Path("path/to/regexes.yaml")) # Example usage with lazy matchers from ua_parser.data import load_lazy lazy_matchers = load_yaml(Path("path/to/regexes.yaml"), loader=load_lazy) ``` -------------------------------- ### Parser Initialization with FallbackResolver Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Demonstrates how to initialize a Parser with a FallbackResolver, prioritizing custom resolvers like foo_resolver before falling back to built-in regex resolvers. ```python Parser(FallbackResolver([ foo_resolver, regex.Resolver(load_lazy_builtins()), ])) ``` -------------------------------- ### Instantiate Base Resolver Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Create a base resolver instance using lazy-loaded built-in regexes. This is the first step in customising parser resolvers. ```python import ua_parser.loaders import ua_parser.regex base = ua_parser.regex.Resolver( ua_parser.loaders.load_lazy_builtins()) ``` -------------------------------- ### Instantiate LRU Cache Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Create an LRU (Least Recently Used) cache with a specified size. This cache will be used to optimize resolver performance. ```python cache = ua_parser.caching.Lru(10000) ``` -------------------------------- ### Benchmark UA-Parser Resolver Performance Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/caches.md The `bench` script measures average parse time per input entry by running resolvers with various caches and sizes. It can report results in human-readable text or CSV format. ```bash python -mua_parser bench ``` -------------------------------- ### Migrate 0.x Parse to 1.0 Parse with Defaults Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/migration.md Converts a 0.x API `Parse` call to the 1.0 equivalent using `ua_parser.parse` and `with_defaults()`. The 1.0 API returns typed dataclasses, requiring `dataclasses.asdict()` for dictionary output. ```python from ua_parser import user_agent_parser from pprint import pprint 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' pprint(user_agent_parser.Parse(ua_string)) ``` ```python import dataclasses import ua_parser from pprint import pprint 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' pprint(dataclasses.asdict(ua_parser.parse(ua_string).with_defaults())) ``` -------------------------------- ### Load Custom Ruleset with YAML Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Load a custom ruleset from a YAML file to create a new parser instance. Requires the PyYAML package. ```python from ua_parser import Parser from ua_parser.loaders import load_yaml # requires PyYaml parser = Parser.from_matchers(load_yaml("regexes.yaml")) parser.parse(some_ua) ``` -------------------------------- ### Add Legacy YAML Ruleset Support in 1.0 Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/migration.md A shim to load a YAML ruleset from the UA_PARSER_YAML environment variable and set it as the global parser for the 1.0 API. ```python import ua_parser from ua_parser.loaders import load_yaml import os if yaml_path := os.environ.get("UA_PARSER_YAML"): ua_parser.parser = ua_parser.Parser.from_matchers( load_yaml(yaml_path)) ``` -------------------------------- ### Cache Performance Analysis Chart Source: https://github.com/ua-parser/uap-python/wiki/Cache-Analysis-History A Mermaid chart visualizing the average performance (in milliseconds) of different cache algorithms across various cache sizes. This chart helps in understanding the relative performance of each algorithm. ```mermaid --- config: xyChart: width: 1200 height: 500 --- xychart-beta x-axis "cache size" [10, 20, 50, 100, 200, 500, 1000, 2000, 5000] y-axis "average (ms)" 0 --> 500 line "test" [420, 420, 420, 420, 420, 420, 420, 420, 420] line [419, 423, 407, 398, 391, 354, 309, 262, 203] line [418, 419, 407, 392, 371, 330, 284, 236, 178] line [420, 417, 417, 388, 371, 326, 283, 238, 180] line [417, 415, 407, 385, 363, 300, 251, 205, 153] line [415, 412, 404, 384, 358, 293, 246, 200, 150] line [414, 411, 402, 388, 370, 326, 283, 237, 180] line [414, 409, 402, 384, 362, 302, 253, 205, 153] line [415, 411, 401, 383, 362, 296, 247, 200, 150] line [424, 412, 404, 386, 364, 310, 261, 213, 158] line [414, 399, 385, 341, 311, 257, 219, 185, 156] line [413, 398, 377, 339, 309, 253, 216, 184, 146] line [410, 392, 365, 341, 304, 253, 216, 185, 162] line [410, 390, 365, 336, 305, 252, 214, 185, 157] line [412, 390, 361, 338, 304, 250, 211, 181, 147] ``` -------------------------------- ### Measure UA-Parser Cache Hit Rates Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/caches.md Use the `hitrates` script to measure the hit rates and memory overhead of UA-parser's caches at various sizes. It simulates cache usage on a sample file and includes BELÁDY’s MIN for theoretical upper bounds. ```bash python -mua_parser hitrates ``` -------------------------------- ### Set Custom Parser with Caching Resolver Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Wrap a custom resolver (including caching) in a Parser instance and set it as the global parser. This applies the new configuration to all library users. ```python ua_parser.parser = ua_parser.Parser(resolver) ``` -------------------------------- ### Parse User Agent String for All Data Source: https://github.com/ua-parser/uap-python/blob/master/README.rst Use the `parse` function to extract all available information (user agent, OS, device) from a user agent string. If data is not found, it will be set to None. ```python >>> from ua_parser import parse >>> 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' >>> parse(ua_string) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS Result(user_agent=UserAgent(family='Chrome', major='41', minor='0', patch='2272', patch_minor='104'), os=OS(family='Mac OS X', major='10', minor='9', patch='4', patch_minor=None), device=Device(family='Mac', brand='Apple', model='Mac'), string='Mozilla/5.0 (Macintosh; Intel Mac OS...) >>> parse("") Result(user_agent=None, os=None, device=None, string='') ``` -------------------------------- ### Parse Full User-Agent String Source: https://github.com/ua-parser/uap-python/blob/master/doc/quickstart.md Use the `parse` function to extract all available data (user agent, OS, device) from a user-agent string. Returns `None` for any data not found. ```python >>> from ua_parser import parse >>> 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' >>> parse(ua_string) Result(user_agent=UserAgent(family='Chrome', major='41', minor='0', patch='2272', patch_minor='104'), os=OS(family='Mac OS X', major='10', minor='9', patch='4', patch_minor=None), device=Device(family='Mac', brand='Apple', model='Mac'), string='Mozilla/5.0 (Macintosh; Intel Mac OS...) ``` ```python >>> parse("") Result(user_agent=None, os=None, device=None, string='') ``` -------------------------------- ### Compose Caching Resolver Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Combine a base resolver with an LRU cache to create a caching resolver. This enhances performance by storing recent results. ```python resolver = ua_parser.caching.CachingResolver( base, cache ) ``` -------------------------------- ### FallbackResolver Class Implementation Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md A higher-order resolver that attempts to resolve a user agent string by sequentially calling a list of sub-resolvers until a match is found. It returns the result from the first successful resolver or a failure result if no resolver matches. ```python class FallbackResolver: def __init__(self, resolvers: List[Resolver]) -> None: self.resolvers = resolvers def __call__(self, ua: str, domain: Domain, /) -> PartialResult: if domain: for resolver in self.resolvers: r = resolver(ua, domain) # if any value is non-none the resolver found a match if r.user_agent_string is not None \ or r.os is not None \ or r.device is not None: return r # if no resolver found a match (or nothing was requested), # resolve to failure return PartialResult(domain, None, None, None, ua) ``` -------------------------------- ### ua_parser.loaders.load_yaml Source: https://github.com/ua-parser/uap-python/blob/master/doc/api.md Loads YAML data following the regexes.yaml structure to create matchers for user agent parsing. It supports custom data loaders for generating different matcher variants. ```APIDOC ## ua_parser.loaders.load_yaml(f: PathOrFile, loader: DataLoader = load_data) ### Description Loads YAML data following the `regexes.yaml` structure. The `loader` parameter customizes which matcher variant is generated; by default, `load_data()` is used to generate eager matchers, and `load_lazy()` can be used to generate lazy matchers instead. ### Parameters #### Path Parameters - **f** (PathOrFile) - Required - The path or file object to load YAML data from. - **loader** (DataLoader) - Optional - The data loader function to use for generating matchers. Defaults to `load_data`. #### WARNING Only available if [`PyYaml`](https://pyyaml.org) is installed. ``` -------------------------------- ### Force Initialization of Global Parser Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/migration.md Explicitly access the global parser on load to avoid unexpected runtime loads caused by lazy instantiation in the 1.0 API. ```python import ua_parser # force initialisation of global parser ua_parser.parser ``` -------------------------------- ### Resolver Protocol Definition Source: https://github.com/ua-parser/uap-python/blob/master/doc/guides.md Defines the structural protocol for a resolver, which is a callable that accepts a user agent string and a domain, returning a PartialResult. ```python class Resolver(Protocol): @abc.abstractmethod def __call__(self, ua: str, domain: Domain, /) -> PartialResult: ... ``` -------------------------------- ### Extract OS Information Source: https://github.com/ua-parser/uap-python/blob/master/doc/quickstart.md Use the `parse_os` function to extract only the operating system details from a user-agent string. Returns `None` for missing data. ```python >>> from ua_parser import parse_os >>> 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' >>> parse_os(ua_string) OS(family='Mac OS X', major='10', minor='9', patch='4', patch_minor=None) ``` -------------------------------- ### Extract Device Information Source: https://github.com/ua-parser/uap-python/blob/master/doc/quickstart.md Use the `parse_device` function to extract only the device details from a user-agent string. Returns `None` for missing data. ```python >>> from ua_parser import parse_device >>> 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' >>> parse_device(ua_string) Device(family='Mac', brand='Apple', model='Mac') ``` -------------------------------- ### Python Object Memory Analysis: Class without __slots__ Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/caches.md Illustrates the memory overhead of a Python object without `__slots__`. This includes an instance dictionary, measured as of Python 3.11 on a 64-bit platform. ```python class: # Without __slots__, a Python object is 48 bytes plus an instance dict. pass ``` -------------------------------- ### Deque Space Requirement Formula Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/caches.md This formula estimates the memory usage for a deque based on the number of elements (n). It considers the fixed metadata size and the space for blocks, where each block holds up to 64 items. ```text 232 + max(1, ceil(n / 64)) * 66 * 8 ``` -------------------------------- ### Python Object Memory Analysis: Class with __slots__ Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/caches.md Demonstrates the memory overhead of a Python object using `__slots__`. This calculation is specific to Python 3.11 on a 64-bit platform and excludes the size of stored data. ```python class: # With __slots__, a Python object is 32 bytes + 8 bytes for each member. # An additional 8 bytes is necessary for weakref support (slotted objects in UA-Parser don’t have weakref support). pass ``` -------------------------------- ### OrderedDict Space Requirement Formula Source: https://github.com/ua-parser/uap-python/blob/master/doc/advanced/caches.md This formula estimates the memory usage for an OrderedDict based on the number of elements (n). It accounts for the underlying dictionary, linked list nodes, and overhead. ```text dict(n) + 64 + 8 * 2**(ceil(log2(n)) + 1) + 32 * n ``` -------------------------------- ### Extract Browser Data from User Agent String Source: https://github.com/ua-parser/uap-python/blob/master/README.rst Use the `parse_user_agent` function to specifically extract only the browser information from a user agent string. Returns None if no browser data is found. ```python >>> from ua_parser import parse_user_agent >>> 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' >>> parse_user_agent(ua_string) UserAgent(family='Chrome', major='41', minor='0', patch='2272', patch_minor='104') >>> parse_user_agent("") ``` -------------------------------- ### Extract Browser Data Source: https://github.com/ua-parser/uap-python/blob/master/doc/quickstart.md Use the `parse_user_agent` function to specifically extract only the browser information from a user-agent string. Returns `None` for missing data. ```python >>> from ua_parser import parse_user_agent >>> 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' >>> parse_user_agent(ua_string) UserAgent(family='Chrome', major='41', minor='0', patch='2272', patch_minor='104') ``` ```python >>> parse_user_agent("") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.