Skip to content

Commit

Permalink
Refactor NumberMeta to register single typeid and hold dtype explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
c-mita committed Jul 1, 2016
1 parent 9425bc5 commit fee32fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
35 changes: 22 additions & 13 deletions malcolm/core/numbermeta.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
from collections import OrderedDict

import numpy

from malcolm.core.scalarmeta import ScalarMeta
from malcolm.core.serializable import Serializable
from malcolm.compat import base_string

import numpy


@Serializable.register("malcolm:core/Byte:1.0", numpy.int8)
@Serializable.register("malcolm:core/UByte:1.0", numpy.uint8)
@Serializable.register("malcolm:core/Short:1.0", numpy.int16)
@Serializable.register("malcolm:core/UShort:1.0", numpy.uint16)
@Serializable.register("malcolm:core/Int:1.0", numpy.int32)
@Serializable.register("malcolm:core/UInt:1.0", numpy.uint32)
@Serializable.register("malcolm:core/Long:1.0", numpy.int64)
@Serializable.register("malcolm:core/ULong:1.0", numpy.uint64)
@Serializable.register("malcolm:core/Float:1.0", numpy.float32)
@Serializable.register("malcolm:core/Double:1.0", numpy.float64)
@Serializable.register("malcolm:core/NumberMeta:1.0")
class NumberMeta(ScalarMeta):
"""Meta object containing information for a numerical value"""

def __init__(self, name, description, dtype):
super(NumberMeta, self).__init__(name, description, dtype)
super(NumberMeta, self).__init__(name, description)
self.dtype = dtype

def validate(self, value):
Expand All @@ -30,3 +23,19 @@ def validate(self, value):
assert cast == value, \
"Lost information converting %s to %s" % (value, cast)
return cast

def to_dict(self):
d = OrderedDict()
d["typeid"] = self.typeid
d["dtype"] = self.dtype().dtype.name

d.update(super(NumberMeta, self).to_dict())
return d

@classmethod
def from_dict(cls, name, d):
dtype = numpy.dtype(d["dtype"]).type
meta = cls(name, d["description"], dtype)
meta.writeable = d["writeable"]
meta.tags = d["tags"]
return meta
27 changes: 14 additions & 13 deletions tests/test_core/test_numbermeta.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import os
import sys
import unittest
from collections import OrderedDict
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import setup_malcolm_paths

from collections import OrderedDict
import setup_malcolm_paths
from mock import Mock
import numpy as np

import unittest

from malcolm.core.numbermeta import NumberMeta
from malcolm.core.serializable import Serializable


class TestNumberMeta(unittest.TestCase):
def test_init_int(self):
nm = NumberMeta("nm", "desc", np.int32)
self.assertEqual(nm.typeid, "malcolm:core/Int:1.0")

def test_init_float(self):
nm = NumberMeta("nm", "desc", np.float64)
self.assertEqual(nm.typeid, "malcolm:core/Double:1.0")
def test_init(self):
dtype = np.float64
nm = NumberMeta("nm", "desc", dtype)
self.assertEqual(nm.typeid, "malcolm:core/NumberMeta:1.0")
self.assertEqual(nm.dtype, dtype)

def test_to_dict(self):
nm = NumberMeta("nm", "desc", np.float64)
expected = OrderedDict()
expected["typeid"] = "malcolm:core/Double:1.0"
expected["typeid"] = "malcolm:core/NumberMeta:1.0"
expected["dtype"] = "float64"
expected["description"] = "desc"
expected["tags"] = []
expected["writeable"] = True
Expand All @@ -33,11 +32,13 @@ def test_to_dict(self):

def test_from_dict(self):
d = {"description":"desc", "tags":[], "writeable":True,
"typeid":"malcolm:core/Double:1.0"}
"typeid":"malcolm:core/NumberMeta:1.0",
"dtype":"float64"}
nm = Serializable.from_dict("nm", d)
self.assertEqual(NumberMeta, type(nm))
self.assertEqual("nm", nm.name)
self.assertEqual(np.float64, nm.dtype)
self.assertEqual(d, nm.to_dict())

class TestNumberMetaValidation(unittest.TestCase):
def test_float_against_float(self):
Expand Down

0 comments on commit fee32fd

Please sign in to comment.