File: __init__.py

package info (click to toggle)
theano 1.0.3+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 30,752 kB
  • sloc: python: 141,182; ansic: 9,505; makefile: 259; sh: 214; pascal: 81
file content (136 lines) | stat: -rw-r--r-- 4,104 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Code supporting compatibility across versions of Python.
"""
from __future__ import absolute_import, print_function, division

# Python 3.x compatibility
from six import PY3, b, BytesIO, next
from six.moves import configparser
from six.moves import reload_module as reload
import collections

__all__ = ['PY3', 'b', 'BytesIO', 'next', 'configparser', 'reload']

if PY3:
    from operator import truediv as operator_div
    izip = zip
    imap = map
    ifilter = filter

    # In python 3.x, when an exception is reraised it saves original
    # exception in its args, therefore in order to find the actual
    # message, we need to unpack arguments recursively.
    def exc_message(e):
        msg = e.args[0]
        if isinstance(msg, Exception):
            return exc_message(msg)
        return msg

    def cmp(x, y):
        """Return -1 if x < y, 0 if x == y, 1 if x > y."""
        return (x > y) - (x < y)

    def get_unbound_function(unbound):
        # Op.make_thunk isn't bound, so don't have a __func__ attr.
        # But bound method, have a __func__ method that point to the
        # not bound method. That is what we want.
        if hasattr(unbound, '__func__'):
            return unbound.__func__
        return unbound

    from collections import OrderedDict, MutableMapping as DictMixin

    def decode(x):
        return x.decode()

    def decode_iter(itr):
        for x in itr:
            yield x.decode()

    def decode_with(x, encoding):
        return x.decode(encoding)
else:
    from six import get_unbound_function
    from operator import div as operator_div
    from itertools import izip, imap, ifilter

    def exc_message(e):
        return e[0]

    cmp = cmp

    from collections import OrderedDict, MutableMapping as DictMixin

    def decode(x):
        return x

    def decode_iter(x):
        return x

    def decode_with(x, encoding):
        return x

__all__ += ['cmp', 'operator_div', 'DictMixin', 'OrderedDict', 'decode',
            'decode_iter', 'get_unbound_function', 'imap', 'izip', 'ifilter']


class DefaultOrderedDict(OrderedDict):
    def __init__(self, default_factory=None, *a, **kw):
        if (default_factory is not None and
                not isinstance(default_factory, collections.Callable)):
            raise TypeError('first argument must be callable')
        OrderedDict.__init__(self, *a, **kw)
        self.default_factory = default_factory

    def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)

    def __missing__(self, key):
        if self.default_factory is None:
            raise KeyError(key)
        self[key] = value = self.default_factory()
        return value

    def __reduce__(self):
        if self.default_factory is None:
            args = tuple()
        else:
            args = self.default_factory,
        return type(self), args, None, None, list(self.items())

    def copy(self):
        return self.__copy__()

    def __copy__(self):
        return type(self)(self.default_factory, self)

__all__ += ['DefaultOrderedDict']


def maybe_add_to_os_environ_pathlist(var, newpath):
    '''Unfortunately, Conda offers to make itself the default Python
       and those who use it that way will probably not activate envs
       correctly meaning e.g. mingw-w64 g++ may not be on their PATH.

       This function ensures that, if `newpath` is an absolute path,
       and it is not already in os.environ[var] it gets added to the
       front.

       The reason we check first is because Windows environment vars
       are limited to 8191 characters and it is easy to hit that.

       `var` will typically be 'PATH'. '''

    import os
    if os.path.isabs(newpath):
        try:
            oldpaths = os.environ[var].split(os.pathsep)
            if newpath not in oldpaths:
                newpaths = os.pathsep.join([newpath] + oldpaths)
                os.environ[var] = newpaths
        except Exception:
            pass

__all__ += ['maybe_add_to_os_environ_pathlist']