File: typeconv.py

package info (click to toggle)
python-numarray 1.1.1-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,428 kB
  • ctags: 8,469
  • sloc: ansic: 92,018; python: 20,861; makefile: 263; sh: 13
file content (52 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (3)
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
"""typeconv: Initialize tables of all type conversions

$Id: typeconv.py,v 1.2 2002/10/25 17:06:09 jaytmiller Exp $
"""

import _conv

# list of all supported types
numtypes = []

# Organize _conv functions to make intialization of TypeConverter simple.
typeconvfuncs = {}
for key in _conv.functionDict.keys():
    typename, functionKey = eval(key)
    if not typeconvfuncs.has_key(typename):
        numtypes.append(typename)
        typeconvfuncs[typename] = {}
    typeconvfuncs[typename][functionKey] = _conv.functionDict[key]

# Now check to see that the effective type/type matrix is complete.
# Perhaps a check is needed (but isn't done) to see if there are extra entries
for typename1 in numtypes:
    for typename2 in numtypes + ['asPyValue','fromPyValue']:
        if not typeconvfuncs[typename1].has_key(typename2):
            if typename1 != typename2:
                raise ValueError("Missing type conversion functions: %s to %s"  \
                    % (typename1,typename2))


class TypeConverter:

    """A container for all functionality to do type conversions for a given type

    Attributes are pointers to C functions:

    asPyValue: takes a C data pointer and returns a PyObject (value)
    fromPyValue: takes a PyObject (value) pointer and a C data pointer and places
                 the converted value at the data pointer
    """

    def __init__(self, name):
        self.name = name   # probably unnecessary
        tdict = typeconvfuncs[name]
        self.asPyValue = tdict['asPyValue']
        self.fromPyValue = tdict['fromPyValue']
        del tdict['asPyValue']
        del tdict['fromPyValue']
        self.astype = tdict

typeConverters = {}
for typename in numtypes:
    typeConverters[typename] = TypeConverter(typename)