コード例 #1
0
ファイル: test_docs.py プロジェクト: samvarankashyap/camel
def test_docs_table_any():
    class Table(object):
        def __init__(self, height, width):
            self.height = height
            self.width = width

        def __repr__(self):
            return "<Table {self.height!r}x{self.width!r}>".format(self=self)

    from camel import Camel, CamelRegistry
    my_types = CamelRegistry()

    @my_types.loader('table', version=any)
    def _load_table(data, version):
        if 'size' in data:
            # version 1
            edge = data['size']**0.5
            return Table(edge, edge)
        else:
            # version 2?)
            return Table(data['height'], data['width'])

    camel = Camel([my_types])
    table1, table2 = camel.load(
        "[!table;1 {size: 49}, !table;2 {height: 5, width: 9}]")

    assert table1.height == 7
    assert table1.width == 7
    assert table2.height == 5
    assert table2.width == 9
コード例 #2
0
ファイル: test_docs.py プロジェクト: samvarankashyap/camel
def test_docs_deleted():
    class DummyData(object):
        def __init__(self, data):
            self.data = data

    from camel import Camel, CamelRegistry
    my_types = CamelRegistry()

    @my_types.loader('deleted-type', version=all)
    def _load_deleted_type(data, version):
        return DummyData(data)

    camel = Camel([my_types])
    assert isinstance(camel.load("""!deleted-type;4 foo"""), DummyData)
コード例 #3
0
ファイル: test_docs.py プロジェクト: samvarankashyap/camel
def test_docs_table_v2():
    # Tables can be rectangles now!
    class Table(object):
        def __init__(self, height, width):
            self.height = height
            self.width = width

        def __repr__(self):
            return "<Table {self.height!r}x{self.width!r}>".format(self=self)

    from camel import Camel, CamelRegistry
    my_types = CamelRegistry()

    @my_types.dumper(Table, 'table', version=2)
    def _dump_table_v2(table):
        return {
            'height': table.height,
            'width': table.width,
        }

    @my_types.loader('table', version=2)
    def _load_table_v2(data, version):
        return Table(data["height"], data["width"])

    @my_types.loader('table', version=1)
    def _load_table_v1(data, version):
        edge = data["size"]**0.5
        return Table(edge, edge)

    table = Table(7, 10)
    assert Camel([my_types]).dump(table) == textwrap.dedent("""
        !table;2
        height: 7
        width: 10
    """).lstrip()

    @my_types.dumper(Table, 'table', version=1)
    def _dump_table_v1(table):
        return {
            # not really, but the best we can manage
            'size': table.height * table.width,
        }

    camel = Camel([my_types])
    camel.lock_version(Table, 1)
    assert camel.dump(Table(5, 7)) == "!table;1\nsize: 35\n"
コード例 #4
0
ファイル: test_docs.py プロジェクト: samvarankashyap/camel
def test_docs_table_v1():
    class Table(object):
        def __init__(self, size):
            self.size = size

        def __repr__(self):
            return "<Table {self.size!r}>".format(self=self)

    from camel import CamelRegistry
    my_types = CamelRegistry()

    @my_types.dumper(Table, 'table', version=1)
    def _dump_table(table):
        return {
            'size': table.size,
        }

    @my_types.loader('table', version=1)
    def _load_table(data, version):
        return Table(data["size"])

    from camel import Camel
    table = Table(25)
    assert Camel([my_types]).dump(table) == "!table;1\nsize: 25\n"

    data = {'chairs': [], 'tables': [Table(25), Table(36)]}
    assert Camel([my_types]).dump(data) == textwrap.dedent("""
        chairs: []
        tables:
        - !table;1
          size: 25
        - !table;1
          size: 36
    """).lstrip()

    table, = Camel([my_types]).load("[!table;1 {size: 100}]")
    assert isinstance(table, Table)
    assert table.size == 100
コード例 #5
0
    assert vanilla_camel.load(dumped) == value


# -----------------------------------------------------------------------------
# Simple custom type


class DieRoll(tuple):
    def __new__(cls, a, b):
        return tuple.__new__(cls, [a, b])

    def __repr__(self):
        return "DieRoll(%s,%s)" % self


reg = CamelRegistry()


@reg.dumper(DieRoll, 'roll', version=None)
def dump_dice(data):
    return "{}d{}".format(*data)


@reg.loader('roll', version=None)
def load_dice(data, version):
    # TODO enforce incoming data is a string?
    a, _, b = data.partition('d')
    return DieRoll(int(a), int(b))


def test_dieroll():
コード例 #6
0
import sys, zlib, logging, collections
from concurrent.futures import ThreadPoolExecutor
from abc import abstractmethod, ABC

from camel import CamelRegistry, Camel

_logger = logging.getLogger("apples.englishActions." + __name__)

thing_camel_registry = CamelRegistry()
misc_camel_registry = CamelRegistry()
camel_registries = (thing_camel_registry, misc_camel_registry)
thing_storage = []

dumper = thing_camel_registry.dumper
loader = thing_camel_registry.loader
misc_dumper = misc_camel_registry.dumper
misc_loader = misc_camel_registry.loader


def dump_raw(thing):
    raw = Camel(camel_registries).dump(thing)
    return raw


def dump(thing):
    _logger.debug("Dumping %s.", thing.name)
    raw = dump_raw(thing).encode("utf-8")
    _logger.debug("Dumped %s to %s bytes.", thing.name, len(raw))
    _logger.debug("Compressing dumped %s.", thing.name)
    data = zlib.compress(raw, level=9)
    _logger.debug("Compressed dumped %s to %s bytes.", thing.name, len(data))
コード例 #7
0
@author: brian
'''

from textwrap import dedent
import pandas, numpy
from pandas.api.types import CategoricalDtype

from pyface.api import error
from traits.api import DelegationError

#### YAML serialization

from camel import Camel, CamelRegistry, YAML_TAG_PREFIX

# the camel registry singletons
camel_registry = CamelRegistry()
standard_types_registry = CamelRegistry(tag_prefix=YAML_TAG_PREFIX)


def load_yaml(path):
    with open(path, 'r') as f:
        data = Camel([camel_registry]).load(f.read())

    return data


def save_yaml(data, path, lock_versions={}):
    with open(path, 'w') as f:
        c = Camel([standard_types_registry, camel_registry])
        for klass, version in lock_versions.items():
            c.lock_version(klass, version)
コード例 #8
0
from camel import CamelRegistry  # type: ignore
from collections import defaultdict

camelRegistry = CamelRegistry()


@camelRegistry.dumper(defaultdict, "defaultdict", version=1)
def _dump_defaultdict(defaultdict_):
    return dict(default_factory=defaultdict_.default_factory,
                dict_=dict(defaultdict_))


@camelRegistry.loader("defaultdict", version=1)
def _load_defaultdict(data, version):
    return defaultdict(data["default_factory"], data["dict_"])


@camelRegistry.dumper(type, "type", version=1)
def _dump_type(type_):
    return dict(name=type_.__name__, module=type_.__module__)


@camelRegistry.loader("type", version=1)
def _load_type(data, version):
    import importlib

    module_ = importlib.import_module(data["module"])
    return getattr(module_, data["name"])
コード例 #9
0

# -----------------------------------------------------------------------------
# Simple custom type


class DieRoll(tuple):
    def __new__(cls, a, b):
        return tuple.__new__(cls, [a, b])

    def __repr__(self):
        return "DieRoll(%s,%s)" % self


# Dump/load as a compact string
reg = CamelRegistry()


@reg.dumper(DieRoll, 'roll', version=None)
def dump_dice(data):
    return "{}d{}".format(*data)


@reg.loader('roll', version=None)
def load_dice(data, version):
    # TODO enforce incoming data is a string?
    a, _, b = data.partition('d')
    return DieRoll(int(a), int(b))


def test_dieroll():
コード例 #10
0
# FSOS: Registries.
# Do not import any other modules from here.
# But add this to your file like this:
#  from reg import version, ui, fs, hw, c, gets, sets
# Registries should always be of type "Dict".
from addict import Dict
import sys, traceback
from html import escape
import fsts
from camel import Camel, CamelRegistry
import pprint, config, os
caramel = CamelRegistry()

version = 0.5

ui = Dict()
fs = Dict()
hw = Dict()
proc = Dict()
cron = Dict()


def extget(k):
    try:
        with open("/run/shm/" + k) as q:
            return q.read()
    except:
        return {}


def extset(k, v):