Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

# -*- coding: utf-8 -*- 

from __future__ import absolute_import 

import sys 

import types 

 

# Python 2/3 compatibility layer. Based on six. 

 

PY3 = sys.version_info[0] == 3 

 

if PY3: 

    def b(s): 

        return s.encode("latin-1") 

    def u(s): 

        return s 

 

    string_types = str, 

    integer_types = int, 

    class_types = type, 

    text_type = str 

    binary_type = bytes 

 

    MAXSIZE = sys.maxsize 

    im_class = lambda meth: meth.__self__.__class__ 

    xrange = range 

    _iterkeys = "keys" 

    _itervalues = "values" 

    _iteritems = "items" 

    from imp import reload 

 

    imap = map 

    izip = zip 

 

    import io 

    StringIO = io.StringIO 

    BytesIO = io.BytesIO 

 

    import html.entities as htmlentitydefs 

    from urllib.request import (urlopen, ProxyHandler, build_opener, 

        install_opener, getproxies, HTTPPasswordMgrWithDefaultRealm, 

        ProxyBasicAuthHandler, ProxyDigestAuthHandler) 

    from urllib.error import HTTPError, URLError 

    from urllib.parse import quote_plus, unquote_plus, urlencode 

 

else: 

    def b(s): 

        return s 

    def u(s): 

        return unicode(s, "unicode_escape") 

 

    string_types = basestring, 

    integer_types = (int, long) 

    class_types = (type, types.ClassType) 

    text_type = unicode 

    binary_type = str 

    im_class = lambda meth: meth.im_class 

    xrange = xrange 

    _iterkeys = "iterkeys" 

    _itervalues = "itervalues" 

    _iteritems = "iteritems" 

    reload = reload 

 

    from itertools import imap, izip 

 

    try: 

        from cStringIO import StringIO 

    except ImportError: 

        from StringIO import StringIO 

    BytesIO = StringIO 

 

    import htmlentitydefs 

    from urllib2 import (urlopen, HTTPError, URLError, 

        ProxyHandler, build_opener, install_opener, 

        HTTPPasswordMgrWithDefaultRealm, ProxyBasicAuthHandler, 

        ProxyDigestAuthHandler) 

    from urllib import getproxies, quote_plus, unquote_plus, urlencode 

 

def iterkeys(d): 

    """Return an iterator over the keys of a dictionary.""" 

    return getattr(d, _iterkeys)() 

 

def itervalues(d): 

    """Return an iterator over the values of a dictionary.""" 

    return getattr(d, _itervalues)() 

 

def iteritems(d): 

    """Return an iterator over the (key, value) pairs of a dictionary.""" 

    return getattr(d, _iteritems)() 

 

try: 

    from functools import total_ordering 

except ImportError: # python 2.6 

    def total_ordering(cls): 

        """Class decorator that fills in missing ordering methods""" 

        convert = { 

            '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)), 

                       ('__le__', lambda self, other: self < other or self == other), 

                       ('__ge__', lambda self, other: not self < other)], 

            '__le__': [('__ge__', lambda self, other: not self <= other or self == other), 

                       ('__lt__', lambda self, other: self <= other and not self == other), 

                       ('__gt__', lambda self, other: not self <= other)], 

            '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)), 

                       ('__ge__', lambda self, other: self > other or self == other), 

                       ('__le__', lambda self, other: not self > other)], 

            '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other), 

                       ('__gt__', lambda self, other: self >= other and not self == other), 

                       ('__lt__', lambda self, other: not self >= other)] 

        } 

        roots = set(dir(cls)) & set(convert) 

        if not roots: 

            raise ValueError('must define at least one ordering operation: < > <= >=') 

        root = max(roots)       # prefer __lt__ to __le__ to __gt__ to __ge__ 

        for opname, opfunc in convert[root]: 

            if opname not in roots: 

                opfunc.__name__ = opname 

                opfunc.__doc__ = getattr(int, opname).__doc__ 

                setattr(cls, opname, opfunc) 

        return cls