def test_ksk(self): k = _ksk("1+2") p = _Ki(k) self.failIf(p != 3, 'got %s, expected 3' % p) k = _ksk("{x+y}", _gk(self.f, self.i)) p = _Kf(k) self.failIf(p != 10.0, 'got %s, expected 10.0' % p) self.failUnlessRaises(kerr, _ksk, ')')
def test_dict(self): k = _ksk(".((`a;1;.,(`z;42));(`b;2))") a = _Ki(k['a']) self.failIf(a != 1, 'got "%s" instead of 1' % a) k['a'] = _gi(2) a = _Ki(k['a']) self.failIf(a != 2, 'got "%s" instead of 2' % a) z = _Ki(k['a.']['z']) self.failIf(z != 42, 'attribute - got %s, expected 42')
def _gendict(pyo): values = [K(x) for x in pyo.values()] keys = pyo.keys() # k function to translate a list of values # to a k dictionary kfexpr = ('{[%s] .(%s)}' % (';'.join(keys), ';'.join( ['(`%(key)s;%(key)s)' % { 'key': key } for key in keys]))) return K(_ksk(kfexpr, K(values)))
def _gendict(pyo): values = [K(x) for x in pyo.values()] keys = pyo.keys() # k function to translate a list of values # to a k dictionary kfexpr = ('{[%s] .(%s)}' % (';'.join(keys), ';'.join(['(`%(key)s;%(key)s)' % {'key': key} for key in keys]))) return K(_ksk(kfexpr, K(values)))
def k(s): '''creates a K object given its string representation >>> k('(1; 2 3; "abc")') k(""" (1 2 3 "abc") """) ''' try: return K(_ksk(s)) except kerr, e: raise kerr("%s in %s (vars:%s)" % (e, s, k("!.k")))
def __call__(self, *args): """K objects representing k functions can be called >>> f = k('{x+y}') >>> print f(1,2) 3 """ # XXX using undocumented feature - k functions have type 10 t = self.t #if t != 10: # raise TypeError("k object of type %d is not callable" % t) kk = K((self, K(args))) try: return K(_ksk('.', kk)) except kerr, e: raise kerr("%s\nvars: %s" % (e, k("!.k")))
def k(s): return _ksk(s) class nk20TestCase(unittest.TestCase):
def testDictionaryConversionFromK(self): # just test that we don't crash for now d = _ksk('.((`a; 1;);(`b; 2;.,(`c;4)))') a = _ktoarray(_ktoarray(d)[1])
def k(s): return _ksk(s)