summaryrefslogtreecommitdiff
path: root/scipy/base/_internal.py
blob: f5d37f57fe6fcd2b3d0bb3cd35d1df9ed92e1250 (plain)
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

from multiarray import _flagdict

_defflags = _flagdict.keys()

_setable = ['WRITEABLE', 'NOTSWAPPED', 'SWAPPED', 'UPDATEIFCOPY', 'ALIGNED',
            'W','N','S','U','A']
_setable2 = ['write','swap','swap','uic','align']*2
_firstltr = {'W':'WRITEABLE',
             'N':'NOTSWAPPED',
             'A':'ALIGNED',
             'C':'CONTIGUOUS',
             'F':'FORTRAN',
             'O':'OWNDATA',
             'U':'UPDATEIFCOPY'}

_anum = _flagdict['ALIGNED']
_nnum = _flagdict['NOTSWAPPED']
_wnum = _flagdict['WRITEABLE']
_cnum = _flagdict['CONTIGUOUS']
_fnum = _flagdict['FORTRAN']

class flagsobj(dict):
    def __init__(self, arr, flags):
        self._arr = arr
        self._flagnum = flags
        for k in _defflags:
            num = _flagdict[k]
            dict.__setitem__(self, k, flags & num == num)

    def __getitem__(self, key):
        if not isinstance(key, str):
            raise KeyError, "Unknown flag %s" % key
        if len(key) == 1:
            try:
                return dict.__getitem__(self, _firstltr[key])
            except:
                if (key == 'B'):
                    num = _anum + _nnum + _wnum
                    return self._flagnum & num == num
                elif (key == 'S'):
                    return not (self._flagnum & _nnum == _nnum)
        else:
            try:
                return dict.__getitem__(self, key)
            except: # special cases
                if (key == 'FNC'):
                    return (self._flagnum & _fnum == _fnum) and not \
                           (self._flagnum & _cnum == _cnum)
	        if (key == 'FORC'):
		    return (self._flagnum & _fnum == _fnum) or \
                           (self._flagnum & _cnum == _cnum)
                if (key == 'SWAPPED'):
                    return not (self._flagnum & _nnum == _nnum)
                if (key == 'BEHAVED'):
                    num = _anum + _nnum + _wnum
                    return self._flagnum & num == num
                if (key in ['BEHAVED_RO', 'BRO']):
                    num = _anum + _nnum
                    return self._flagnum & num == num
                if (key in ['CARRAY','CA']):
                    num = _anum + _nnum + _wnum + _cnum
                    return self._flagnum & num == num
                if (key in ['FARRAY','FA']):
                    num = _anum + _nnum + _wnum + _fnum
                    return (self._flagnum & num == num) and not \
                           (self._flagnum & _cnum == _cnum)
        raise KeyError, "Unknown flag: %s" % key
        
    def __setitem__(self, item, val):
        val = not not val  # convert to boolean
        if item not in _setable:
            raise KeyError, "Cannot set flag", item
        dict.__setitem__(self, item, val) # Does this matter?

        kwds = {}
        for k, name in enumerate(_setable):
            if item == name:
                kwds[_setable2[k]] = val
        if (item == 'NOTSWAPPED' or item == 'N'):
            kwds['swap'] = not val

        # now actually update array flags
        self._arr.setflags(**kwds)