def test_hasT(): assert isT(T) assert isT(T1) matrix = BTAtom.ensure('matrix2') inout = BTAtom.ensure('inout') out = BTAtom.ensure('out') assert matrix[inout, T1].hasT ccy = BType('ccy') fx = BType('fx') GBP = ccy['GBP'] USD = ccy['USD'] assert fx[S(domestic=GBP, foreign=T1)].hasT N = BType('N') assert (T**N).hasT assert ((T * num) ^ num).hasT assert ((num * num) ^ T).hasT assert S(name=T, age=num).hasT assert (T**num).hasT assert (num**T).hasT assert (num**T).hasT with assertRaises(TypeError): 1 | T
def testTemplates(): fred = BTAtom.ensure('fred') num * fred >> fitsWithin >> T * fred >> check >> equal >> True pyint * fred >> fitsWithin >> T1 * T2 >> check >> equal >> True (fred**N) >> fitsWithin >> (T**N) >> check >> equal >> True # simple wildcards (N**fred)[pylist] >> fitsWithin >> (N**T1)[T2] >> check >> equal >> True (N**fred)[pylist] >> fitsWithin >> (N** T)[pydict] >> check >> equal >> False (pystr** fred)[pylist] >> fitsWithin >> (N**T1)[pylist] >> check >> equal >> False (fred** pystr)[pylist] >> fitsWithin >> (T1**T2)[pylist] >> check >> equal >> True (fred**pystr)[pylist] >> fitsWithin >> (T1** T2)[T3] >> check >> equal >> True (fred**pystr)[pylist] >> fitsWithin >> ( T1**T2)[pydict] >> check >> equal >> False (fred**pystr)[pylist] >> fitsWithin >> ( T1**T1)[pylist] >> check >> equal >> False
from coppertop.core import PP, Missing from coppertop.pipe import * from bones.core.types import N, num, aliased, anon, pytuple, pylist, tv, named from bones.core.metatypes import BTAtom, cacheAndUpdate, fitsWithin as _fitsWithin from coppertop.std import tvarray, check, equal matrix = N**N**num colvec = matrix['colvec'] rowvec = matrix['rowvec'] square = BTAtom.ensure('square') colmaj = BTAtom.ensure('colmaj').setImplicit rowmaj = BTAtom.ensure('rowmaj').setOrthogonal lol = BTAtom.ensure('lol').setConstructor(tv) _matrix = matrix & tvarray _colvec = _matrix & colvec _rowvec = _matrix & rowvec _llmatrix = (matrix & lol).setConstructor(tv) # lol is a tvseq of tvseq, i.e. ragged array that we are making regular _llcolvec = _llmatrix & colvec _llrowvec = _llmatrix & rowvec
def testStructCreation(): label = BTAtom.ensure('label').setConstructor(tvstruct) title = label(text='My cool Tufte-compliant scatter graph') title._names() == ['text']
# Copyright (c) 2019-2021 David Briant. All rights reserved. # # ******************************************************************************* import sys # sys._TRACE_IMPORTS = True if hasattr(sys, '_TRACE_IMPORTS') and sys._TRACE_IMPORTS: print(__name__) from coppertop.core import Missing, ProgrammerError from coppertop.testing import assertRaises from coppertop.std import check, equal, each, to, joinAll, _, sortUsing, box, tvstruct from bones.core.metatypes import BTAtom, BType, BTArray, BTMap, BTFn, S, isT from bones.core.types import Holder, index, count, offset, num, pystr, N, null, T, T1, T2, T3 tFred = BTAtom.ensure('fred') tJoe = BTAtom.ensure('joe') tSally = BTAtom.ensure('sally') def testNominalAndAtoms(): assert repr(tFred) == 'fred' assert tFred == BType('fred') assert tFred is BType('fred') x = 'hello' >> box | tFred assert isinstance(x, tFred) assert x._v == 'hello' i1 = 1 | index assert i1._t == index
# ******************************************************************************* # # Copyright (c) 2021 David Briant. All rights reserved. # # ******************************************************************************* from coppertop.std.structs import tvstruct as _tvstruct from bones.core.metatypes import BTAtom from bones.core.types import num, N, tv adhoc = BTAtom.define('adhoc').setConstructor(_tvstruct) agg = BTAtom.define('agg').setConstructor(_tvstruct) ellipsis = type(...) TBT = BTAtom.define( 'TBT') # temporary type to allow 'fred' >> box | pystr - aka to be types void = BTAtom.define( 'void' ) # nothing returned on the stack from this function (should not be assignable) # NB a 1x1 matrix is assumed to be a scalar, e.g. https://en.wikipedia.org/wiki/Dot_product#Algebraic_definition vec = (N**num) # N**num is common so don't name it matrix = (N**N**num).nameAs('matrix').setNonExclusive colvec = matrix['colvec'].setNonExclusive rowvec = matrix['rowvec'].setNonExclusive I = BTAtom.define('I') square = BTAtom.define('square') right = BTAtom.define('right')
# # ******************************************************************************* import sys if hasattr(sys, '_TRACE_IMPORTS') and sys._TRACE_IMPORTS: print(__name__) from coppertop.pipe import * from bones.core.metatypes import BTAtom from coppertop.std import check, equal, fitsWithin from coppertop.std import tvarray from coppertop.tests.take1 import _take from coppertop.tests.take2 import _take from bones.core.types import pyint, pylist mat = BTAtom.ensure("mat2") vec = BTAtom.ensure("vec2") @coppertop(style=binary2) def mmul(A:mat, B:vec) -> vec: answer = A @ B | vec return answer def test_mmul(): a = tvarray(mat, [[1, 2], [3, 4]]) b = tvarray(vec, [1, 2]) res = a >> mmul >> b res >> check >> typeOf >> vec
# # Copyright (c) 2021 David Briant. All rights reserved. # # ******************************************************************************* import sys if hasattr(sys, '_TRACE_IMPORTS') and sys._TRACE_IMPORTS: print(__name__) from bones.core.metatypes import BTAtom as _BTAtom, BType as _BType, weaken as _weaken _Void = sys._VOID _ProgrammerError = sys._ProgrammerError _Missing = sys._Missing __all__ = [] i8 = _BTAtom.define('i8').setExclusive u8 = _BTAtom.define('u8').setExclusive i16 = _BTAtom.define('i16').setExclusive u16 = _BTAtom.define('u16').setExclusive i32 = _BTAtom.define('i32').setExclusive u32 = _BTAtom.define('u32').setExclusive i64 = _BTAtom.define('i64').setExclusive u64 = _BTAtom.define('u64').setExclusive f32 = _BTAtom.define('f32').setExclusive f64 = _BTAtom.define('f64').setExclusive T = _BType('T') for i in range(1, 21): t = T.ensure(_BType(f'{i}')) locals()[t.name] = t for o in range(26):
# ******************************************************************************* # # Copyright (c) 2022 David Briant. All rights reserved. # # ******************************************************************************* from bones.core.metatypes import BTAtom, S from coppertop.std import tvstruct from coppertop.core import Missing from bones.core.types import pydict, count, pystr card = BTAtom.ensure('card') handId = BTAtom.ensure('handId') ndmap = BTAtom.ensure('ndmap') pad_element = S(has=pystr, suggestions=count, like=count) cluedo_pad = ( (card * handId)**pad_element)[ndmap] & BTAtom.ensure('cluedo_pad') cluedo_pad = pydict #& BTAtom.ensure('cluedo_pad') once we have tvmao we can do this cluedo_bag = (tvstruct & BTAtom.ensure('_cluedo_bag')).nameAs('cluedo_bag') YES = 'X' NO = '-' MAYBE = '?' TBI = 'TBI' # the hand with the people, weapon and room - To Be Inferred class HasOne(object): def __init__(self, handId=Missing): self.handId = handId def __rsub__(self, handId): # handId / has
# ******************************************************************************* from coppertop.core import PP as _PP, Void as _Void from coppertop.pipe import * from coppertop.pipe import NO_TYPE from bones.core.metatypes import BTAtom from bones.core.types import pystr, N, pyint, pylist, pyset, pydict_keys, pydict_values, T1, T2, pytuple, tv from coppertop.std import count, equal, tvseq, each, max, pad from dm.utils import formatStruct from dm.pmf import PMF, L from coppertop.std.types import void display_table = (N**pystr)[tvseq][BTAtom.ensure('table')].setCoercer(tvseq) Void = tv(void, _Void) # move to std and the singleton should have type void @coppertop def PP(t:display_table) -> display_table: for string in t: string >> PP return t @coppertop(style=binary) def hjoin(a:display_table, b:display_table, options={}) -> display_table: assert (a >> count) == (b >> count) answer = tvseq(display_table, []) for i in range(len(a)): answer.append(a[i] + options.get('sep', '') + b[i])
from coppertop.std import sort, both, zip, nvs, values, names, override, select, to from coppertop.std.structs import tvstruct, tvarray from coppertop.std.linalg import matrix from coppertop.std.types import adhoc from bones.core.metatypes import S, BTAtom from bones.core.types import num, pystr, T, T1, T2, T3, pylist, pydict, pyint, pytuple, pyfloat from .misc import sequence # numsEtAl models the storage mechanism for a pmf or likelihood and the multiplication thereof pre # normalise, i.e. the numbers may not add up to one. Additionally for compactness we wish to be able # to add tags, e.g to label a pmf as `d6`, etc, or to a likelihood with the data such as `PDoorBIf` # noinspection PyUnreachableCode if False: numsEtAl = BTAtom.ensure('numsEtAl') # nominal for the moment # notionally a union of mappings for str**str, num**str and num**num @coppertop def at( x: numsEtAl[T], k: pystr ) -> pystr + num: # remember str is weakened to pystr and float to num return x[k] @coppertop def at(x: numsEtAl[T], k: num) -> num: return x[k] # str**str and num**str above imply a dynamic type - i.e. return type depends on key value - however # the reality is that the programmer always knows which one is needed
# ******************************************************************************* import random from coppertop.pipe import * from coppertop.core import PP from coppertop.testing import assertRaises from coppertop.std import check, equal, _v, to, drop, fitsWithin, doesNotFitWithin from coppertop.std.structs import tvarray, tvseq from bones.core.types import index, count, num, pystr, N, T, \ T1, T2, T3, pyint, pyfloat, tv, anon, named, aliased, pylist from bones.core.metatypes import BTAtom, S, _partition, BType, weaken from coppertop.std.types import ccy, fx from coppertop.std.linalg import square, right tFred = BTAtom.ensure('fred') tJoe = BTAtom.ensure('joe') tSally = BTAtom.ensure('sally') pystr2 = BTAtom.ensure('pystr2') weaken(pystr, (pystr2, )) # # we have these metatypes - atom, union, intersection, product (tuples and structs), exponential (arrays, maps # and functions) & distinguished. # # # when we do a A >> fitsWithin >> B we partition the two sets into three parts # # B intersect A' - stuff in B but not in A - anything here then it's not a fit # B intersect A - common stuff, if we only have common stuff then it's an exact fit # B' intersect A - stuff in A but not in B - we term this the residual