def test_022(_): # stagr restr on r2 results in list of remainder (r1, r3). r1, r2, r3 = Rdng(txt='r1'), Rdng(txt='r2'), Rdng(txt='r3') x1 = Restr() restrs = [x1] r2._stagr = [x1] result = jdb.restrs2ext_(restrs, [r1, r2, r3], '_stagr') _.assertEqual([r1, r3], result)
def test_012(_): # stagk restr on k2 results in list of remainder (k1, k3). k1, k2, k3 = Kanj(txt='k1'), Kanj(txt='k2'), Kanj(txt='k3') x1 = Restr() restrs = [x1] k2._stagk = [x1] result = jdb.restrs2ext_(restrs, [k1, k2, k3], '_stagk') _.assertEqual([k1, k3], result)
def test_003(_): # Restr on k1,k2 results in list of remainder (k2). k1, k2, k3 = Kanj(txt='k1'), Kanj(txt='k2'), Kanj(txt='k3') x1, x3 = Restr(), Restr() restrs = [x1, x3] k1._restr = [x1] k3._restr = [x3] result = jdb.restrs2ext_(restrs, [k1, k2, k3], '_restr') _.assertEqual([k2], result)
def test_004(_): # All kanji restricted results in None ("nokanji" sentinal). k1, k2, k3 = Kanj(txt='k1'), Kanj(txt='k2'), Kanj(txt='k3') x1, x2, x3 = Restr(), Restr(), Restr() restrs = [x1, x2, x3] k1._restr = [x1] k2._restr = [x2] k3._restr = [x3] result = jdb.restrs2ext_(restrs, [k1, k2, k3], '_restr') _.assertEqual(None, result)
def restrtxts(restrs, kanjs, attr, quote_func=lambda x: x): """Return list of 'kanj.txt' strings of those 'kanj' items without a matching item in 'restrs'. "Maching" means two items with the same value in the attribute named by 'key'. if there are no items in 'restrs', and empty list is returned (rather than a list of all 'kanji.txt' values). If every item in 'kanj' has a matching item in 'restrs', a one-item list is returned containing the string 'noXXX' where XXX is a derived from the value of 'key'. Each restr item will be passed to function 'quote_func' and the string returned from that function is actually uwed top build the return list. This function is convenient for getting restriction text from an entry for display. Assuming 'entr' is a jdb entry with a non-empty list of kanji items in entr._kanj and rdng has been set to a reading from entr._rdng: restrtxt = '' if hasattr (rdng, '_restr'): restrtxt = ','.join (restrtxts (rdng._restr, 'kanj', entr._kanj)) if restrtxt: restrtxt = " (%s)" % restrtxt print rdng.txt + restrtxt It can also be used for stagr or stagk restrictions (although one would not normally expect the ['noXXX'] form of output to occur in these cases): if hasattr (sens, '_stagr'): restrtxt = ','.join (restrtxts (sens._stagr, 'rdng', entr._rdng)) If the "noXXX" case does not occur (as it would be expected not to), the above is equivalent to: if getattr (s,'_stagr', None): #Use getattr to filter out empty list case. restrtxt = ','.join ([x.txt for x in jdb.filt (rdng, ["rdng"], s._stagr, ["rdng"])]) """ if not restrs: return [] if len(restrs) == len(kanjs): return [ 'no' + { '_restr': "kanji", '_stagr': "readings", '_stagk': "kanji" }[attr] ] return [quote_func(x.txt) for x in jdb.restrs2ext_(restrs, kanjs, attr)]
def test_001(_): # An empty restr list should produce an empty result. k1, k2, k3 = Kanj(txt='k1'), Kanj(txt='k2'), Kanj(txt='k3') restrs = [] result = jdb.restrs2ext_(restrs, [k1, k2, k3], '_restr') _.assertEqual([], result)