def test_object_binary_compatibility(self): ibc = _types.isBinaryCompatible self.assertTrue(ibc(NoneType(), NoneType())) self.assertTrue(ibc(Int8(), Int8())) NT = NamedTuple(a=int,b=int) class X(NamedTuple(a=int,b=int)): pass class Y(NamedTuple(a=int,b=int)): pass self.assertTrue(ibc(X, X)) self.assertTrue(ibc(X, Y)) self.assertTrue(ibc(X, NT)) self.assertTrue(ibc(Y, NT)) self.assertTrue(ibc(NT, Y)) self.assertFalse(ibc(OneOf(int, float), OneOf(float, int))) self.assertTrue(ibc(OneOf(int, X), OneOf(int, Y))) self.assertIsInstance(OneOf(None, X)(Y()), X) self.assertIsInstance(NamedTuple(x=OneOf(None, X))(x=Y()).x, X)
def test_named_tuple(self): self.assertEqual( NamedTuple(x=int, y=int, z=OneOf(10, 20)).ElementTypes, (Int64(), Int64(), OneOf(10, 20))) self.assertEqual( NamedTuple(x=int, y=int, z=OneOf(10, 20)).ElementNames, ('x', 'y', 'z'))
def test_serialize_named_tuples_with_extra_fields(self): T1 = NamedTuple(x=int) T2 = NamedTuple(x=int, y=float, z=str) self.assertEqual( deserialize(T2, serialize(T1, T1(x=10))), T2(x=10, y=0.0, z="") )
def test_embedded_messages(self): T = NamedTuple(x=TupleOf(int)) T_with_message = NamedTuple(x=EmbeddedMessage) T_with_two_messages = NamedTuple(x=EmbeddedMessage, y=EmbeddedMessage) T2 = NamedTuple(x=TupleOf(int), y=TupleOf(int)) t = T(x=(1, 2, 3, 4)) tm = deserialize(T_with_message, serialize(T, t)) tm2 = T_with_two_messages(x=tm.x, y=tm.x) t2 = deserialize(T2, serialize(T_with_two_messages, tm2)) self.assertEqual(t2.x, t.x) self.assertEqual(t2.y, t.x)
def test_subclassing(self): BaseTuple = NamedTuple(x=int,y=float) class NTSubclass(BaseTuple): def f(self): return self.x + self.y def __repr__(self): return "ASDF" inst = NTSubclass(x=10,y=20) self.assertTrue(isinstance(inst, BaseTuple)) self.assertTrue(isinstance(inst, NTSubclass)) self.assertTrue(type(inst) is NTSubclass) self.assertEqual(repr(inst), "ASDF") self.assertNotEqual(BaseTuple.__repr__(inst), "ASDF") self.assertEqual(inst.x, 10) self.assertEqual(inst.f(), 30) TupleOfSubclass = TupleOf(NTSubclass) instTup = TupleOfSubclass((inst,BaseTuple(x=20,y=20.0))) self.assertTrue(isinstance(instTup[0], NTSubclass)) self.assertTrue(isinstance(instTup[1], NTSubclass)) self.assertEqual(instTup[0].f(), 30) self.assertEqual(instTup[1].f(), 40) self.assertEqual(BaseTuple(inst).x, 10) self.assertTrue(OneOf(None, NTSubclass)(None) is None) self.assertTrue(OneOf(None, NTSubclass)(inst) == inst)
class Exterior(Class): x = Member(int) i = Member(Interior) iTup = Member(NamedTuple(x=Interior, y=Interior)) def __init__(self): self.i = Interior()
def test_serializing_named_tuples_in_loop(self): NT = NamedTuple(x=OneOf(int, float), y=OneOf(int, lambda: NT)) context = SerializationContext({'NT': NT}) self.serializeInLoop(lambda: NT(x=10, y=NT(x=20, y=2)), context=context)
class CodeSelection(NamedTuple(start_row = int, start_column = int, end_row = int, end_column = int)): @staticmethod def fromAceEditorJson(selection): return CodeSelection( start_row = selection["start"]["row"], start_column = selection["start"]["column"], end_row = selection["end"]["row"], end_column = selection["end"]["column"] ) def slice(self, buffer): """Select just the text that is in the selection from the given buffer.""" bufferLines = buffer.split('\n') lines = bufferLines[self.start_row:self.end_row+1] if len(lines) == 1: if self.start_column != self.end_column: lines[0] = lines[0][self.start_column:self.end_column] elif len(lines): lines[0] = lines[0][self.start_column:] lines[-1] = lines[-1][:self.end_column] return ('\n' * self.start_row) + '\n'.join(lines)
def test_const_dict_of_tuple(self): K = NamedTuple(a=OneOf(float, int), b=OneOf(float, int)) someKs = [K(a=0,b=0), K(a=1), K(a=10), K(b=10), K()] T = ConstDict(K, K) indexDict = {} x = T() numpy.random.seed(42) for _ in range(100): i1 = numpy.random.choice(len(someKs)) i2 = numpy.random.choice(len(someKs)) add = numpy.random.choice([False, True]) if add: indexDict[i1] = i2 x = x + {someKs[i1]: someKs[i2]} else: if i1 in indexDict: del indexDict[i1] x = x - (someKs[i1],) self.assertEqual(x, T({someKs[i]:someKs[v] for i,v in indexDict.items()})) for k in x: self.assertTrue(k in x) x[k]
def test_serializing_named_tuples_in_loop(self): NT = Forward("NT") NT = NT.define(NamedTuple(x=OneOf(int, float), y=OneOf(int, TupleOf(NT)))) context = SerializationContext({'NT': NT}) self.serializeInLoop(lambda: NT(x=10, y=(NT(x=20, y=2),)), context=context)
def test_serializing_anonymous_recursive_named_tuples(self): NT = NamedTuple(x=OneOf(int, float), y=OneOf(int, lambda: NT)) nt = NT(x=10, y=NT(x=20, y=2)) nt_ponged = ping_pong(nt) self.assertEqual(nt_ponged.y.x, 20)
def test_named_tuple_getattr(self): NT = NamedTuple(a=float, b=int, c=str) @Compiled def f(x: NT) -> str: return x.c + x.c nt = NT(a=0.0, b=1, c="hi") self.assertEqual(f(nt), "hihi")
def test_one_of_py_subclass(self): class X(NamedTuple(x=int)): def f(self): return self.x O = OneOf(None, X) self.assertEqual(NamedTuple(x=int)(x=10).x, 10) self.assertEqual(X(x=10).f(), 10) self.assertEqual(O(X(x=10)).f(), 10)
def test_tuple_of_string_perf(self): t = NamedTuple(a=str, b=str) t0 = time.time() for i in range(1000000): t(a="a", b="b").a elapsed = time.time() - t0 print("Took ", elapsed, " to do 1mm") self.check_expected_performance(elapsed)
def test_named_tuple_str(self): t = NamedTuple(a=str, b=str) self.assertEqual(t(a='1',b='2').a, '1') self.assertEqual(t(a='1',b='2').b, '2') self.assertEqual(t(b='2').a, '') self.assertEqual(t(b='2').b, '2') self.assertEqual(t().a, '') self.assertEqual(t().b, '')
def test_named_tuple_passing(self): NT = NamedTuple(a=float, b=int, c=str) @Compiled def f(x: NT) -> NT: y = x return y nt = NT(a=0.0, b=1, c="hi") self.assertEqual(f(nt), nt)
def test_recursives_held_infinitely_throws(self): X = Forward("X") with self.assertRaisesRegex(Exception, "type-containment cycle"): X = X.define(NamedTuple(x=X)) with self.assertRaisesRegex(Exception, "type-containment cycle"): X = X.define(OneOf(None, X)) with self.assertRaisesRegex(Exception, "type-containment cycle"): X = X.define(Tuple(X))
def test_serialize_primitive_compound_types(self): class A: pass B = Alternative("B", X={'a': A}) ts = SerializationContext({'A': A, 'B': B}) for t in [ ConstDict(int, float), NamedTuple(x=int, y=str), TupleOf(bool), Tuple(int, int, bool), OneOf(int, float), OneOf(1, 2, 3, "hi", b"goodbye"), TupleOf(NamedTuple(x=int)), TupleOf(object), TupleOf(A), TupleOf(B) ]: self.assertIs(ping_pong(t, ts), t)
def test_python_objects_in_tuples(self): class NormalPyClass(object): pass class NormalPySubclass(NormalPyClass): pass NT = NamedTuple(x=NormalPyClass, y=NormalPySubclass) nt = NT(x=NormalPyClass(),y=NormalPySubclass()) self.assertIsInstance(nt.x, NormalPyClass) self.assertIsInstance(nt.y, NormalPySubclass)
def test_named_tuple(self): t = NamedTuple(a=int, b=int) with self.assertRaisesRegex(AttributeError, "object has no attribute"): t().asdf with self.assertRaisesRegex(AttributeError, "immutable"): t().a = 1 self.assertEqual(t()[0], 0) self.assertEqual(t().a, 0) self.assertEqual(t()[1], 0) self.assertEqual(t(a=1,b=2).a, 1) self.assertEqual(t(a=1,b=2).b, 2)
def test_named_tuple_assignment_refcounting(self): class C(Class): x = Member(int) NT = NamedTuple(c=C) @Compiled def f(x: NT): y = x return y.c c = C(x=20) res = f(NT(c=c)) self.assertEqual(res.x, 20) self.assertEqual(_types.refcount(res), 2)
def test_named_tuple_construction(self): t = NamedTuple(a=int, b=int) self.assertEqual(t(a=10).a, 10) self.assertEqual(t(a=10).b, 0) self.assertEqual(t(a=10,b=2).a, 10) self.assertEqual(t(a=10,b=2).b, 2) self.assertEqual(t({'a': 10,'b':2}).a, 10) self.assertEqual(t({'a': 10,'b':2}).b, 2) self.assertEqual(t({'b':2}).a, 0) self.assertEqual(t({'b':2}).b, 2) with self.assertRaises(TypeError): t({'c':10}) with self.assertRaises(TypeError): t(c=10)
def test_inject_exception_into_context(self): NT = NamedTuple() context = SerializationContext({'NT': NT}) context2 = SerializationContext({'NT': NT}) def throws(*args): raise Exception("Test Exception") context.nameForObject = throws context2.objectFromName = throws with self.assertRaisesRegex(Exception, "Test Exception"): context.serialize(NT) data = context2.serialize(NT) with self.assertRaisesRegex(Exception, "Test Exception"): context2.deserialize(data)
def test_named_tuple_from_dict(self): N = NamedTuple(x=int, y=str,z=OneOf(None,"hihi")) self.assertEqual(N().x, 0) self.assertEqual(N().y, "") self.assertEqual(N().z, None) self.assertEqual(N({}).x, 0) self.assertEqual(N({}).y, "") self.assertEqual(N({}).z, None) self.assertEqual(N({'x': 20}).x, 20) self.assertEqual(N({'x': 20, 'y': "30"}).y, "30") self.assertEqual(N({'y': "30", 'x': 20}).y, "30") self.assertEqual(N({'z': "hihi"}).z, "hihi") with self.assertRaises(Exception): N({'r': 'hi'}) N({'y': 'hi', 'z': "not hihi"}) N({'a': 0, 'b': 0, 'c': 0, 'd': 0})
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from object_database.view import _cur_view, coerce_value from typed_python.hash import sha_hash from typed_python import Alternative, OneOf, TupleOf, ConstDict, Tuple, NamedTuple from types import FunctionType _base = NamedTuple(_identity=str) class DatabaseObject(_base): __types__ = None __schema__ = None def __ne__(self, other): return not (self == other) def __eq__(self, other): if not isinstance(other, DatabaseObject): return False if not type(self) is type(other): return False return self._identity == other._identity
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import inspect from object_database.object import DatabaseObject, Index, Indexed from types import FunctionType from typed_python import ConstDict, NamedTuple, Tuple, TupleOf TypeDefinition = NamedTuple(fields=TupleOf(str), indices=TupleOf(str)) SchemaDefinition = ConstDict(str, TypeDefinition) def SubscribeLazilyByDefault(t): t.__object_database_lazy_subscription__ = True return t class Schema: """A collection of types that can be used to access data in a database.""" def __init__(self, name): self._name = name # Map: typename:str -> cls(DatabaseObject) self._types = {} self._supportingTypes = {}
import io import pydoc import research_app.Displayable as Displayable from typed_python import python_ast, OneOf, Alternative, TupleOf,\ NamedTuple, Tuple, Class, ConstDict, Member, ListOf import datetime import ast from object_database import Schema, Indexed, current_transaction schema = Schema("research_app.ResearchBackend") @schema.define class ServiceConfig: pass Error = NamedTuple(error=str, line=int, trace = str) CodeBlock = NamedTuple(code=str, line_range=Tuple(int,int)) class ResearchBackend(ServiceBase): def initialize(self, chunkStoreOverride=None): self._logger = logging.getLogger(__file__) self.db.subscribeToSchema(schema) self.db.subscribeToSchema(ContentSchema.schema) self.db.subscribeToSchema(EvaluationSchema.schema) @staticmethod def configureService(database, serviceObject): pass
class X(NamedTuple(x=int)): def f(self): return self.x
def test_serialize_named_tuple(self): X = NamedTuple(x=int) self.check_idempotence(X(x=20))
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import object_database._types as _types from object_database.object import Index, Indexed from types import FunctionType from typed_python import ConstDict, NamedTuple, Tuple, TupleOf, serialize ObjectId = int FieldId = int ObjectFieldId = NamedTuple(objId=int, fieldId=int, isIndexValue=bool) IndexValue = bytes IndexId = NamedTuple(fieldId=int, indexValue=IndexValue) DatabaseObjectBase = NamedTuple(_identity=int) TypeDefinition = NamedTuple(fields=TupleOf(str), indices=TupleOf(str)) SchemaDefinition = ConstDict(str, TypeDefinition) FieldDefinition = NamedTuple(schema=str, typename=str, fieldname=str) def SubscribeLazilyByDefault(t): t.__object_database_lazy_subscription__ = True return t