Ejemplo n.º 1
0
        raise Exception("Invalid currency conversion: %s -> %s" %
                        (source_currency, destination_currency))
    return (amount * CurrencyRates[tc] / CurrencyRates[fc])


class Item(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "shopping.item"

    def __init__(self, *args, **kwargs):
        super(Item, self).__init__(*args, **kwargs)

    def value_in(self, currency=DefaultCurrency):
        return convert_currencies(self.value, self.currency, currency)


class Basket(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "shopping.basket"

    def __init__(self, *args, **kwargs):
        super(Basket, self).__init__(*args, **kwargs)

    def value_in(self, currency=DefaultCurrency):
        return reduce(lambda x, y: x + y.value_in(currency), self.items, 0.0)


das.register_mixins(Item, Basket)
Ejemplo n.º 2
0
            if hasResolutionsWithMargins:
                for name, res in self.resolutionsWithMargins.iteritems():
                    scl = None
                    if hasMarginPresets and name in self.marginPresets:
                        scl = self.marginPresets[name]
                    self.set_margin(name, resolution=res, scale=scl)

            if hasMarginPresets:
                for name, scl in self.marginPresets.iteritems():
                    if hasResolutionsWithMargins and name in self.resolutionsWithMargins:
                        continue
                    self.set_margin(name, scale=scl)
                del (self.marginPresets)

            if hasResolutionsWithMargins:
                del (self.resolutionsWithMargins)

            if hasattr(self, "defaultMargin"):
                self.resolution.defaultMargin = self.defaultMargin
                del (self.defaultMargin)

        # Validate margin names
        if self.resolution.defaultMargin and not self.resolution.defaultMargin in self.resolution.margins:
            raise Exception("Invalid default margin '%s'. Must be one of %s" %
                            (self.resolution.defaultMargin, ", ".join(
                                map(repr, self.resolution.margins.keys()))))


das.register_mixins(Test)
Ejemplo n.º 3
0
import das  # pylint: disable=import-error


class SomeTypeValidator(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "validation.SomeType"

    def __init__(self, *args, **kwargs):
        super(SomeTypeValidator, self).__init__(*args, **kwargs)

    def _validate_globally(self):
        for k, v in self.value_pairs.iteritems():
            if not k in self.valid_keys:
                raise Exception("Invalid key '%s'" % k)
            if not self.accepted_values.boolean and isinstance(v, bool):
                raise Exception("Boolean not allowed")
            if not self.accepted_values.integer and isinstance(v, (int, long)):
                raise Exception("Integer not allowed")
            if not self.accepted_values.real and isinstance(v, float):
                raise Exception("Real not allowed")
            if not self.accepted_values.string and isinstance(v, basestring):
                raise Exception("String not allowed")


das.register_mixins(SomeTypeValidator)
Ejemplo n.º 4
0
import das  # pylint: disable=import-error
import math


class Resolution(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "extend.Resolution"

    def __init__(self, *args, **kwargs):
        super(Resolution, self).__init__(*args, **kwargs)

    def pixel_count(self):
        return self.width * self.height


class Scale(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "extend.Scale"

    def __init__(self, *args, **kwargs):
        super(Scale, self).__init__(*args, **kwargs)

    def is_uniform(self):
        return (math.fabs(self.x - self.y) < 0.000001)


das.register_mixins(Resolution, Scale)
Ejemplo n.º 5
0
def test_mixin1():
    print("=== Mixin tests using timeline.ClipSource schema type ===")

    class Range(das.Mixin):
        @classmethod
        def get_schema_type(klass):
            return "timeline.Range"

        def __init__(self, *args, **kwargs):
            super(Range, self).__init__(*args, **kwargs)

        def expand(self, start, end):
            cs, ce = self[0], self[1]
            if start < cs:
                cs = start
            if end > ce:
                ce = end
            self[0], self[1] = cs, ce

    class ClipSource(das.Mixin):
        @classmethod
        def get_schema_type(klass):
            return "timeline.ClipSource"

        def __init__(self, *args, **kwargs):
            super(ClipSource, self).__init__(*args, **kwargs)

        def set_media(self, path):
            _, ext = map(lambda x: x.lower(), os.path.splitext(path))
            if ext == ".fbx":
                print("Get range from FBX file")
            elif ext == ".abc":
                print("Get range from Alembic file")
            elif ext == ".mov":
                print("Get range from Movie file")
            self.media = os.path.abspath(path).replace("\\", "/")

        def set_clip_offsets(self, start, end):
            data_start, data_end = self.dataRange
            clip_start = min(data_end, data_start + max(0, start))
            clip_end = max(data_start, data_end + min(end, 0))
            if clip_start == data_start and clip_end == data_end:
                self.clipRange = None
            else:
                self.clipRange = (clip_start, clip_end)

    das.register_mixins(Range, ClipSource)

    print("-- make def (1)")
    dv = das.make_default("timeline.ClipSource")
    print("-- write (1)")
    das.write(dv, "./out.tl")
    print("-- make def (2)")
    cs = das.make_default("timeline.ClipSource")
    print("-- read (1)")
    cs = das.read("./out.tl")
    das.pprint(cs)
    cs.dataRange = (100, 146)
    cs.dataRange.expand(102, 150)
    cs.set_media("./source.mov")
    cs.set_clip_offsets(1, -1)
    das.pprint(cs)
    print("-- write (2)")
    das.write(cs, "./out.tl")
    c = das.copy(cs)
    das.pprint(c)
    for k, v in c.iteritems():
        print("%s = %s" % (k, v))
    os.remove("./out.tl")
Ejemplo n.º 6
0
              (len(self), ", ".join(map(lambda x: "'%s'" % x, self))))


class SetEcho(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "mix.set"

    def __init__(self, *args, **kwargs):
        super(SetEcho, self).__init__(*args, **kwargs)

    def niceEcho(self):
        print("Set value [%s] : (%s)" %
              (len(self), ", ".join(map(lambda x: "'%s'" % x, self))))


class DictEcho(das.Mixin):
    @classmethod
    def get_schema_type(klass):
        return "mix.dict"

    def __init__(self, *args, **kwargs):
        super(DictEcho, self).__init__(*args, **kwargs)

    def niceEcho(self):
        print("Dict value [%s] : {%s}" % (len(self), ", ".join(
            map(lambda x: "%s = '%s'" % x, self.items()))))


das.register_mixins(TupleEcho, SequenceEcho, SetEcho, DictEcho)