diff --git a/ag/build.sh b/ag/build.sh index e69de29..901160a 100755 --- a/ag/build.sh +++ b/ag/build.sh @@ -0,0 +1,4 @@ +#! /bin/zsh +# encoding: utf-8 + +echo "- nothing to do" diff --git a/build-all.sh b/build-all.sh index da24863..933aa21 100755 --- a/build-all.sh +++ b/build-all.sh @@ -3,7 +3,7 @@ for dir in ag core m serial do - echo "Building Cython implementations in ${dir}" + echo "Building Cython implementations in ${dir}:" cd "$dir" ./build.sh cd .. diff --git a/core/build.sh b/core/build.sh index a1cc611..6e0ba70 100755 --- a/core/build.sh +++ b/core/build.sh @@ -8,56 +8,15 @@ function die() { exit 1 } -function translate() { - echo -n "Translating from Python to C: " - - first=1 - - for item in ${@} - do - if [ $first -eq 1 ]; then - echo -n "${item}" - first=0 - else - echo -n ", ${item}" - fi - - cython -3 ${item}.pyx -o ${item}.c || die "translating" - done - - echo "." -} - -function combobulate() { - name="$1" - - echo -n > "$name.pyx" - - for part in src/$name.*.pyx - do - # echo "Combobulating $part..." - echo "###############################################################################" >> "$name.pyx" - echo "# Combobulated from file $part" >> "$name.pyx" - echo "###############################################################################\n" >> "$name.pyx" - cat "$part" >> "$name.pyx" - echo >> "$name.pyx" - done -} - CFLAGS=(-Wall -pedantic -std=c99 -fPIC) LFLAGS=(-shared) -combobulate core -ln -s src/interface/* . +echo "- translating from Python to C" +cython -3 cython_types.pyx -o cython_types.c || die "translating" -# call Cython -translate core +echo "- compiling and linking" +gcc $CFLAGS -I/usr/include/python3.3 -pthread -c cython_types.c || die "compilation" +gcc $LFLAGS -L/usr/lib -lpython3.3 cython_types.o -o cython_types.so || die "linking" +rm -f cython_types.{c,o} -echo -n "Compiling and linking: core" -gcc $CFLAGS -I/usr/include/python3.3 -pthread -c core.c || die "compilation" -gcc $LFLAGS -L/usr/lib -lpython3.3 core.o -o core.so || die "linking" -rm -f core.{c,o} -echo "." - -# remove temporary sources -rm -f *.pyx *.pxd +echo "- done" diff --git a/core/cython_types.pyx b/core/cython_types.pyx new file mode 100644 index 0000000..d735c18 --- /dev/null +++ b/core/cython_types.pyx @@ -0,0 +1,148 @@ +#! /bin/env python3 +# encoding: utf-8 + +# -------------------------------------------------- + +cdef class ndict(dict): + """ + A dictionary superclass whose keys are exposed as attributes. + + >>> d = ndict() + >>> d.alpha = 1 + >>> d["alpha"] + 1 + >>> d["beta"] = 42 + >>> d.beta + 42 + >>> d + {'alpha': 1, 'beta': 42} + """ + + def __init__(self, *args, **kwargs): + dict.__init__(self, *args, **kwargs) + + def __getattr__(self, str name): + if name in self: + return self[name] + else: + raise AttributeError("'ndict' object has no attribute '%s'" %(name)) + + def __setattr__(self, str name, value): + self[name] = value + +# -------------------------------------------------- + +cdef class map: + """ + An ordered dict. + """ + + def __init__(self, source=None): + """ + Initialize the map. + + The source is a sequence of (key, value) tuples. + + TODO fixme bug + >>> map(((1, 2, 3, 4), ("a", "b", "c", "d"))) + """ + + print(repr(source)) + + if source: + self.keys, self.vals = zip(*source) + else: + self.keys = [] + self.vals = [] + + def __get_index(self, key): + if key in self.keys: + return self.keys.index(key) + else: + return None + + def __getitem__(self, key): + i = self.__get_index(key) + + if i is None: + raise KeyError(key) + else: + return self.vals[i] + + def __setitem__(self, key, val): + i = self.__get_index(key) + + if i is None: + self.keys.append(key) + self.vals.append(val) + else: + self.vals[i] = val + + def __contains__(self, item): + return item in self.keys + + def index(self, item): + return self.keys.index(item) + + def sort(self, key=None): + tmp_keys = self.keys + tmp_vals = self.vals + + self.keys = [] + self.vals = [] + + for K, V in sorted(zip(tmp_keys, tmp_vals), key=key): + self.keys.append(K) + self.vals.append(V) + + @property + def items(self): + return zip(self.keys, self.vals) + + def __len__(self): + return len(self.vals) + + def __repr__(self): + pairs = [] + + for i in range(len(self.keys)): + pairs.append("%s: %s" %(repr(self.keys[i]), repr(self.vals[i]))) + + return "<{%s}>" %(", ".join(pairs)) + +# -------------------------------------------------- + +cdef class enum: + """ + Emulates a C++-like enum type. + + Based on a py2 enum function by Alec Thomas and acjohnson55. + """ + + def __init__(self, str name, words, int start=0): + """ + Initializes the enum. + + name is used for __repr__ only + words is a sequence of enum keys + start is the numerical value of the first key + """ + + self.typename = name + self.reverse_enums = dict(enumerate(words, start)) + self.enums = dict((value, key) for key, value in self.reverse_enums.items()) + + def name(self, int value): + if value in self.reverse_enums: + return self.reverse_enums[value] + else: + raise AttributeError("no %s has a value of %s" %(self, value)) + + def __getattr__(self, aname): + if aname in self.enums: + return self.enums[aname] + else: + raise AttributeError("%s not in %s" %(aname, self)) + + def __repr__(self): + return "" %(self.typename) diff --git a/m/build.sh b/m/build.sh index a1cc611..901160a 100755 --- a/m/build.sh +++ b/m/build.sh @@ -1,63 +1,4 @@ #! /bin/zsh # encoding: utf-8 -setopt extendedglob - -function die() { - echo "\n\n>>> Failed during ${1}, aborting." - exit 1 -} - -function translate() { - echo -n "Translating from Python to C: " - - first=1 - - for item in ${@} - do - if [ $first -eq 1 ]; then - echo -n "${item}" - first=0 - else - echo -n ", ${item}" - fi - - cython -3 ${item}.pyx -o ${item}.c || die "translating" - done - - echo "." -} - -function combobulate() { - name="$1" - - echo -n > "$name.pyx" - - for part in src/$name.*.pyx - do - # echo "Combobulating $part..." - echo "###############################################################################" >> "$name.pyx" - echo "# Combobulated from file $part" >> "$name.pyx" - echo "###############################################################################\n" >> "$name.pyx" - cat "$part" >> "$name.pyx" - echo >> "$name.pyx" - done -} - -CFLAGS=(-Wall -pedantic -std=c99 -fPIC) -LFLAGS=(-shared) - -combobulate core -ln -s src/interface/* . - -# call Cython -translate core - -echo -n "Compiling and linking: core" -gcc $CFLAGS -I/usr/include/python3.3 -pthread -c core.c || die "compilation" -gcc $LFLAGS -L/usr/lib -lpython3.3 core.o -o core.so || die "linking" -rm -f core.{c,o} -echo "." - -# remove temporary sources -rm -f *.pyx *.pxd +echo "- nothing to do" diff --git a/serial/build.sh b/serial/build.sh index e69de29..901160a 100755 --- a/serial/build.sh +++ b/serial/build.sh @@ -0,0 +1,4 @@ +#! /bin/zsh +# encoding: utf-8 + +echo "- nothing to do"