blob: 134b190881ac5768eaf7f4100b222450cf8cccd4 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001import traceback
2
3try:
4 from urllib import quote
5except:
6 from urllib.parse import quote
7
8import pydevd_constants
9import pydev_log
10
11def to_number(x):
12 if is_string(x):
13 try:
14 n = float(x)
15 return n
16 except ValueError:
17 pass
18
19 l = x.find('(')
20 if l != -1:
21 y = x[0:l-1]
22 #print y
23 try:
24 n = float(y)
25 return n
26 except ValueError:
27 pass
28 return None
29
30def compare_object_attrs(x, y):
31 try:
32 if x == y:
33 return 0
34 x_num = to_number(x)
35 y_num = to_number(y)
36 if x_num is not None and y_num is not None:
37 if x_num - y_num<0:
38 return -1
39 else:
40 return 1
41 if '__len__' == x:
42 return -1
43 if '__len__' == y:
44 return 1
45
46 return x.__cmp__(y)
47 except:
48 if pydevd_constants.IS_PY3K:
49 return (to_string(x) > to_string(y)) - (to_string(x) < to_string(y))
50 else:
51 return cmp(to_string(x), to_string(y))
52
53def cmp_to_key(mycmp):
54 'Convert a cmp= function into a key= function'
55 class K(object):
56 def __init__(self, obj, *args):
57 self.obj = obj
58 def __lt__(self, other):
59 return mycmp(self.obj, other.obj) < 0
60 def __gt__(self, other):
61 return mycmp(self.obj, other.obj) > 0
62 def __eq__(self, other):
63 return mycmp(self.obj, other.obj) == 0
64 def __le__(self, other):
65 return mycmp(self.obj, other.obj) <= 0
66 def __ge__(self, other):
67 return mycmp(self.obj, other.obj) >= 0
68 def __ne__(self, other):
69 return mycmp(self.obj, other.obj) != 0
70 return K
71
72def is_string(x):
73 if pydevd_constants.IS_PY3K:
74 return isinstance(x, str)
75 else:
76 return isinstance(x, basestring)
77
78def to_string(x):
79 if is_string(x):
80 return x
81 else:
82 return str(x)
83
84def print_exc():
85 if traceback:
86 traceback.print_exc()
87
88def quote_smart(s, safe='/'):
89 if pydevd_constants.IS_PY3K:
90 return quote(s, safe)
91 else:
92 if isinstance(s, unicode):
93 s = s.encode('utf-8')
94
95 return quote(s, safe)
96
97
98
99