Skip to content

__str__() method for convenience pretty string #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Author:

* Selwin Ong (https://github.com/selwin)
* Selwin Ong (https://github.com/selwin)
* Loisaida Sam Sandberg (https://github.com/loisaidasam)
15 changes: 13 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ Python User Agents
==================

``user_agents`` is a Python library that provides an easy way to identify/detect devices like mobile
phones, tablets and their capabilities by parsing (browser) user agent strings. The goal is to reliably
detect whether:
phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. The goal is to reliably detect whether:

* User agent is a mobile, tablet or PC based device
* User agent has touch capabilities (has touch screen)
Expand Down Expand Up @@ -58,6 +57,9 @@ and `os` attributes. For example:
user_agent.device # returns Device(family='iPhone')
user_agent.device.family # returns 'iPhone'

# Viewing a pretty string version
str(user_agent) # returns "iPhone / iOS 5.1 / Mobile Safari 5.1"


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

# Now a Samsung Galaxy S3
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'
Expand All @@ -95,6 +98,7 @@ For example:
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "GT-I9300 / Android 4.0.4 / Android 4.0.4"

# iPad's user agent string
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'
Expand All @@ -104,6 +108,7 @@ For example:
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "iPad / iOS 3.2 / Mobile Safari 4.0.4"

# Kindle Fire's user agent string
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'
Expand All @@ -113,6 +118,7 @@ For example:
user_agent.is_touch_capable # returns True
user_agent.is_pc # returns False
user_agent.is_bot # returns False
str(user_agent) # returns "Kindle Fire / Android / Amazon Silk 1.1.0-80"

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


Running Tests
Expand All @@ -133,6 +140,10 @@ Running Tests
Changelog
=========

Version 0.3.0
-------------
* Added __str__/__unicode__ methods for convenience of pretty string

Version 0.2.0
-------------
* Fixed errors when running against newer versions if ua-parser
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

setup(
name='user-agents',
version='0.2.0',
version='0.3.0',
author='Selwin Ong',
author_email='selwin.ong@gmail.com',
packages=['user_agents'],
url='https://github.com/selwin/python-user-agents',
license='MIT',
description='A library to identify devices (phones, tablets) and their capabilities by parsing (browser) user agent strings',
description='A library to identify devices (phones, tablets) and their capabilities by parsing (browser/HTTP) user agent strings',
long_description=open('README.rst').read(),
zip_safe=False,
include_package_data=True,
package_data = { '': ['README.rst'] },
install_requires=['ua-parser'],
classifiers=[
'Development Status :: 3 - Alpha',
'Development Status :: 5 - Production/Stable',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
Expand Down
2 changes: 1 addition & 1 deletion user_agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 2, 0)
VERSION = (0, 3, 0)

from .parsers import parse
10 changes: 9 additions & 1 deletion user_agents/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ def __init__(self, user_agent_string):
self.browser = parse_browser(**ua_dict['user_agent'])
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])

def __unicode__(self):
return unicode(str(self))

def _is_android_tablet(self):
# Newer Android tablets don't have "Mobile" in their user agent string,
# older ones like Galaxy Tab still have "Mobile" though they're not
Expand Down
36 changes: 36 additions & 0 deletions user_agents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,39 @@ def test_is_bot(self):
self.assertFalse(ie_ua.is_bot)
self.assertFalse(nokia_n97_ua.is_bot)
self.assertFalse(android_firefox_aurora_ua.is_bot)

def test_strings(self):
self.assertEqual(str(iphone_ua), "iPhone / iOS 5.1 / Mobile Safari 5.1")
self.assertEqual(str(ipad_ua), "iPad / iOS 3.2 / Mobile Safari 4.0.4")
self.assertEqual(str(galaxy_tab), "SCH-I800 / Android 2.2 / Android 2.2")
self.assertEqual(str(galaxy_s3_ua), "GT-I9300 / Android 4.0.4 / Android 4.0.4")
self.assertEqual(str(kindle_fire_ua), "Kindle Fire / Android / Amazon Silk 1.1.0-80")
self.assertEqual(str(playbook_ua), "BlackBerry Playbook / BlackBerry Tablet OS 2.0.1 / BlackBerry WebKit 2.0.1")
self.assertEqual(str(nexus_7_ua), "Nexus 7 / Android 4.1.1 / Chrome 18.0.1025")
self.assertEqual(str(windows_phone_ua), "Samsung SGH-i917 / Windows Phone 7.5 / IE Mobile 9")
self.assertEqual(str(windows_rt_ua), "PC / Windows RT / IE 10")
self.assertEqual(str(blackberry_torch_ua), "BlackBerry 9800 / BlackBerry OS 6 / BlackBerry WebKit 6")
self.assertEqual(str(blackberry_bold_ua), "BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700")
self.assertEqual(str(blackberry_bold_touch_ua), "BlackBerry 9930 / BlackBerry OS 7 / BlackBerry WebKit 7")
self.assertEqual(str(j2me_opera_ua), "Other / Other / Opera Mini 9.80")
self.assertEqual(str(ie_ua), "PC / Windows 8 / IE 10")
self.assertEqual(str(ie_touch_ua), "PC / Windows 8 / IE 10")
self.assertEqual(str(mac_safari_ua), "PC / Mac OS X 10.6.8 / WebKit Nightly 537.13")
self.assertEqual(str(windows_ie_ua), "PC / Windows 7 / IE 9")
self.assertEqual(str(ubuntu_firefox_ua), "PC / Ubuntu / Firefox 15.0.1")
self.assertEqual(str(google_bot_ua), "Spider / Other / Googlebot 2.1")
self.assertEqual(str(nokia_n97_ua), "Nokia N97 / Symbian OS 9.4 / Nokia Browser 7.1.12344")
self.assertEqual(str(android_firefox_aurora_ua), "Other / Android / Firefox Mobile 27")

def test_unicode_strings(self):
try:
# Python 2
unicode_ua_str = unicode(iphone_ua)
self.assertEqual(unicode_ua_str,
u"iPhone / iOS 5.1 / Mobile Safari 5.1")
self.assertTrue(isinstance(unicode_ua_str, unicode))
except NameError:
# Python 3
unicode_ua_str = str(iphone_ua)
self.assertEqual(unicode_ua_str,
"iPhone / iOS 5.1 / Mobile Safari 5.1")