Skip to content

Commit 3dede6f

Browse files
committed
Merge pull request selwin#10 from loisaidasam/master
__str__() method for convenience pretty string
2 parents 2578126 + 2e304c5 commit 3dede6f

6 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎AUTHORS.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Author:
22

3-
* Selwin Ong (https://github.com/selwin)
3+
* Selwin Ong (https://github.com/selwin)
4+
* Loisaida Sam Sandberg (https://github.com/loisaidasam)

‎README.rst‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ and `os` attributes. For example:
5757
user_agent.device # returns Device(family='iPhone')
5858
user_agent.device.family # returns 'iPhone'
5959

60+
# Viewing a pretty string version
61+
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"
62+
6063

6164
``user_agents`` also expose a few other more "sophisticated" attributes that are derived from one or
6265
more basic attributes defined above. As for now, these attributes should correctly identify
@@ -85,6 +88,7 @@ For example:
8588
user_agent.is_touch_capable # returns False
8689
user_agent.is_pc # returns False
8790
user_agent.is_bot # returns False
91+
str(user_agent) # returns "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700"
8892

8993
# Now a Samsung Galaxy S3
9094
ua_string = 'Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
@@ -94,6 +98,7 @@ For example:
9498
user_agent.is_touch_capable # returns True
9599
user_agent.is_pc # returns False
96100
user_agent.is_bot # returns False
101+
str(user_agent) # returns "GT-I9300 / Android 4.0.4 / Android 4.0.4"
97102

98103
# iPad's user agent string
99104
ua_string = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'
@@ -103,6 +108,7 @@ For example:
103108
user_agent.is_touch_capable # returns True
104109
user_agent.is_pc # returns False
105110
user_agent.is_bot # returns False
111+
str(user_agent) # returns "iPad / iOS 3.2 / Mobile Safari 4.0.4"
106112

107113
# Kindle Fire's user agent string
108114
ua_string = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true'
@@ -112,6 +118,7 @@ For example:
112118
user_agent.is_touch_capable # returns True
113119
user_agent.is_pc # returns False
114120
user_agent.is_bot # returns False
121+
str(user_agent) # returns "Kindle Fire / Android / Amazon Silk 1.1.0-80"
115122

116123
# Touch capable Windows 8 device
117124
ua_string = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0; Touch)'
@@ -121,6 +128,7 @@ For example:
121128
user_agent.is_touch_capable # returns True
122129
user_agent.is_pc # returns True
123130
user_agent.is_bot # returns False
131+
str(user_agent) # returns "PC / Windows 8 / IE 10"
124132

125133

126134
Running Tests
@@ -132,6 +140,10 @@ Running Tests
132140
Changelog
133141
=========
134142

143+
Version 0.3.0
144+
-------------
145+
* Added __str__/__unicode__ methods for convenience of pretty string
146+
135147
Version 0.2.0
136148
-------------
137149
* Fixed errors when running against newer versions if ua-parser

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='user-agents',
6-
version='0.2.1',
6+
version='0.3.0',
77
author='Selwin Ong',
88
author_email='selwin.ong@gmail.com',
99
packages=['user_agents'],

‎user_agents/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = (0, 2, 0)
1+
VERSION = (0, 3, 0)
22

33
from .parsers import parse

‎user_agents/parsers.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ def __init__(self, user_agent_string):
106106
self.browser = parse_browser(**ua_dict['user_agent'])
107107
self.device = parse_device(**ua_dict['device'])
108108

109-
109+
def __str__(self):
110+
device = self.is_pc and "PC" or self.device.family
111+
os = ("%s %s" % (self.os.family, self.os.version_string)).strip()
112+
browser = ("%s %s" % (self.browser.family, self.browser.version_string)).strip()
113+
return " / ".join([device, os, browser])
114+
115+
def __unicode__(self):
116+
return unicode(str(self))
117+
110118
def _is_android_tablet(self):
111119
# Newer Android tablets don't have "Mobile" in their user agent string,
112120
# older ones like Galaxy Tab still have "Mobile" though they're not

‎user_agents/tests.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,39 @@ def test_is_bot(self):
177177
self.assertFalse(ie_ua.is_bot)
178178
self.assertFalse(nokia_n97_ua.is_bot)
179179
self.assertFalse(android_firefox_aurora_ua.is_bot)
180+
181+
def test_strings(self):
182+
self.assertEqual(str(iphone_ua), "iPhone / iOS 5.1 / Mobile Safari 5.1")
183+
self.assertEqual(str(ipad_ua), "iPad / iOS 3.2 / Mobile Safari 4.0.4")
184+
self.assertEqual(str(galaxy_tab), "SCH-I800 / Android 2.2 / Android 2.2")
185+
self.assertEqual(str(galaxy_s3_ua), "GT-I9300 / Android 4.0.4 / Android 4.0.4")
186+
self.assertEqual(str(kindle_fire_ua), "Kindle Fire / Android / Amazon Silk 1.1.0-80")
187+
self.assertEqual(str(playbook_ua), "BlackBerry Playbook / BlackBerry Tablet OS 2.0.1 / BlackBerry WebKit 2.0.1")
188+
self.assertEqual(str(nexus_7_ua), "Nexus 7 / Android 4.1.1 / Chrome 18.0.1025")
189+
self.assertEqual(str(windows_phone_ua), "Samsung SGH-i917 / Windows Phone 7.5 / IE Mobile 9")
190+
self.assertEqual(str(windows_rt_ua), "PC / Windows RT / IE 10")
191+
self.assertEqual(str(blackberry_torch_ua), "BlackBerry 9800 / BlackBerry OS 6 / BlackBerry WebKit 6")
192+
self.assertEqual(str(blackberry_bold_ua), "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700")
193+
self.assertEqual(str(blackberry_bold_touch_ua), "BlackBerry 9930 / BlackBerry OS 7 / BlackBerry WebKit 7")
194+
self.assertEqual(str(j2me_opera_ua), "Other / Other / Opera Mini 9.80")
195+
self.assertEqual(str(ie_ua), "PC / Windows 8 / IE 10")
196+
self.assertEqual(str(ie_touch_ua), "PC / Windows 8 / IE 10")
197+
self.assertEqual(str(mac_safari_ua), "PC / Mac OS X 10.6.8 / WebKit Nightly 537.13")
198+
self.assertEqual(str(windows_ie_ua), "PC / Windows 7 / IE 9")
199+
self.assertEqual(str(ubuntu_firefox_ua), "PC / Ubuntu / Firefox 15.0.1")
200+
self.assertEqual(str(google_bot_ua), "Spider / Other / Googlebot 2.1")
201+
self.assertEqual(str(nokia_n97_ua), "Nokia N97 / Symbian OS 9.4 / Nokia Browser 7.1.12344")
202+
self.assertEqual(str(android_firefox_aurora_ua), "Other / Android / Firefox Mobile 27")
203+
204+
def test_unicode_strings(self):
205+
try:
206+
# Python 2
207+
unicode_ua_str = unicode(iphone_ua)
208+
self.assertEqual(unicode_ua_str,
209+
u"iPhone / iOS 5.1 / Mobile Safari 5.1")
210+
self.assertTrue(isinstance(unicode_ua_str, unicode))
211+
except NameError:
212+
# Python 3
213+
unicode_ua_str = str(iphone_ua)
214+
self.assertEqual(unicode_ua_str,
215+
"iPhone / iOS 5.1 / Mobile Safari 5.1")

0 commit comments

Comments
 (0)