From 39becc55607d19c28f5459229370075ef97bb287 Mon Sep 17 00:00:00 2001 From: rodrigondec Date: Mon, 7 Oct 2019 17:07:24 -0300 Subject: [PATCH] method __str__ from UserAgent now using methods Is better to calculate and provide a method to calculate the string values for `device`, `os` and `browser` instead of doing it directly on `__str__` method. This way the users may use those methods to get each data directly. Instead of having to make a `user_agent.__str__().split('/')` to get each value --- user_agents/parsers.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/user_agents/parsers.py b/user_agents/parsers.py index 5292c59..420c73d 100644 --- a/user_agents/parsers.py +++ b/user_agents/parsers.py @@ -136,10 +136,11 @@ def __init__(self, user_agent_string): self.device = parse_device(**ua_dict['device']) def __str__(self): - device = self.is_pc and "PC" or self.device.family - os = ("%s %s" % (self.os.family, self.os.version_string)).strip() - browser = ("%s %s" % (self.browser.family, self.browser.version_string)).strip() - return " / ".join([device, os, browser]) + return "{device} / {os} / {browser}".format( + device=self.get_device(), + os=self.get_os(), + browser=self.get_browser() + ) def __unicode__(self): return unicode(str(self)) @@ -163,6 +164,15 @@ def _is_blackberry_touch_capable_device(self): return True return False + def get_device(self): + return self.is_pc and "PC" or self.device.family + + def get_os(self): + return ("%s %s" % (self.os.family, self.os.version_string)).strip() + + def get_browser(self): + return ("%s %s" % (self.browser.family, self.browser.version_string)).strip() + @property def is_tablet(self): if self.device.family in TABLET_DEVICE_FAMILIES: @@ -251,5 +261,6 @@ def is_email_client(self): return True return False + def parse(user_agent_string): return UserAgent(user_agent_string)