Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd415cd

Browse filesBrowse files
authored
Merge pull request selwin#32 from Xuefeng-Zhu/simplify
Simplify parse_version and compat
2 parents ee85ddb + 8130fb3 commit bd415cd
Copy full SHA for bd415cd

File tree

Expand file treeCollapse file tree

2 files changed

+18
-28
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+18
-28
lines changed

‎user_agents/compat.py

Copy file name to clipboardExpand all lines: user_agents/compat.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
PY3 = sys.version_info[0] == 3
44

55
if PY3:
6+
string_types = str
7+
68
def iteritems(d, **kw):
79
return iter(d.items(**kw))
810
else:
11+
string_types = basestring
12+
913
def iteritems(d, **kw):
1014
return iter(d.iteritems(**kw))

‎user_agents/parsers.py

Copy file name to clipboardExpand all lines: user_agents/parsers.py
+14-28Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
import sys
21
from collections import namedtuple
32

43
from ua_parser import user_agent_parser
5-
6-
7-
PY2 = sys.version_info[0] == 2
8-
PY3 = sys.version_info[0] == 3
9-
10-
if PY3:
11-
string_types = str
12-
else:
13-
string_types = basestring
4+
from .compat import string_types
145

156

167
MOBILE_DEVICE_FAMILIES = (
@@ -74,26 +65,21 @@
7465
)
7566

7667

68+
def verify_attribute(attribute):
69+
if isinstance(attribute, string_types) and attribute.isdigit():
70+
return int(attribute)
71+
72+
return attribute
73+
74+
7775
def parse_version(major=None, minor=None, patch=None, patch_minor=None):
7876
# Returns version number tuple, attributes will be integer if they're numbers
79-
if major is not None and isinstance(major, string_types):
80-
major = int(major) if major.isdigit() else major
81-
if minor is not None and isinstance(minor, string_types):
82-
minor = int(minor) if minor.isdigit() else minor
83-
if patch is not None and isinstance(patch, string_types):
84-
patch = int(patch) if patch.isdigit() else patch
85-
if patch_minor is not None and isinstance(patch_minor, string_types):
86-
patch_minor = int(patch_minor) if patch_minor.isdigit() else patch_minor
87-
if patch_minor:
88-
return (major, minor, patch, patch_minor)
89-
elif patch:
90-
return (major, minor, patch)
91-
elif minor:
92-
return (major, minor)
93-
elif major:
94-
return (major,)
95-
else:
96-
return tuple()
77+
major = verify_attribute(major)
78+
minor = verify_attribute(minor)
79+
patch = verify_attribute(patch)
80+
patch_minor = verify_attribute(patch_minor)
81+
82+
return tuple(filter(None, (major, minor, patch, patch_minor)))
9783

9884

9985
Browser = namedtuple('Browser', ['family', 'version', 'version_string'])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.