defl/tests/test_basic.py

199 lines
4.4 KiB
Python
Raw Normal View History

2024-09-11 11:14:03 -04:00
#!/usr/bin/env python
import enum
import itertools
import json
import os
import re
import sys
from dataclasses import KW_ONLY, dataclass, field
from functools import partial, partialmethod
from operator import itemgetter
from time import sleep
import defl
from defl import CLIError, DotDict, Null, Path, Undefined, cl, log
from defl._assert_ import Assert
from defl._typing_ import *
2025-03-09 09:17:53 -04:00
from defl.testing_ import Tester, testFail
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
log.setLevel('i')
2024-09-11 11:14:03 -04:00
tester = Tester(name=__file__)
2025-03-09 09:17:53 -04:00
def randGen(rang: range):
yield from rang
@tester.add()
def roundRobbin():
x = [x for x in defl.roundRobin(range(1, 10), range(5, 10), range(15, 30))]
Assert(x) == [1, 5, 15, 2, 6, 16, 3, 7, 17, 4, 8, 18, 5, 9, 19, 6, 20, 7, 21, 8, 22, 9, 23, 24, 25, 26, 27, 28, 29]
2024-09-11 11:14:03 -04:00
@tester.add()
def dotDictTest():
print()
DotDict()
2025-03-09 09:17:53 -04:00
a = DotDict({'b': {'c': {'d': 'e'}}})
2024-09-11 11:14:03 -04:00
assert isinstance(a, DotDict), type(a)
2025-03-09 09:17:53 -04:00
assert isinstance(a['b'], DotDict), type(a['b'])
assert isinstance(a['b']['c'], DotDict), type(a['b']['c'])
2024-09-11 11:14:03 -04:00
assert a.get('z') is None, a.get('z')
assert isinstance(a.get('b'), DotDict), a.get('b')
assert isinstance(a.b, DotDict), type(a.b)
assert isinstance(a.b.c, DotDict), type(a.b.c)
assert isinstance(a.b.c.d, str), type(a.b.c.d)
a.b['c'] = 1
assert a.b.c == 1
assert a.b['c'] == 1
a.b.d = 2
assert a.b.d == 2
assert a.b['d'] == 2
del a.b.d
assert a.b.get('d', -9) == -9
a.g = {'k': {'v': 'q'}}
assert isinstance(a.g, DotDict), type(a.g)
d = json.dumps(a, sort_keys=True)
assert d == '{"b": {"c": 1}, "g": {"k": {"v": "q"}}}', d
2025-03-09 09:17:53 -04:00
a = DotDict({'b': [{'a': 'b'}, {'a': 'b'}, 2]})
2024-09-11 11:14:03 -04:00
assert isinstance(a.b[0], DotDict)
assert isinstance(a.b[1], DotDict)
assert isinstance(a.b[2], int)
assert isinstance(a, DotDict)
assert isinstance(a.b, list)
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def chunk():
assert defl.chunkRange(0, 5, chunks=1) == [(0, 4)]
assert defl.chunkRange(0, 5, chunkSize=1) == [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
assert defl.chunkRange(0, 5, chunks=2) == [(0, 2), (3, 4)]
assert defl.chunkRange(0, 5, chunkSize=2) == [(0, 1), (2, 3), (4, 4)]
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def rniy():
d = {1: 1}
a = [[1], [2, [3, [4], (5, set([6]), '7')]], d]
a = [x for x in defl.recursiveNonIterableYield(a)]
b = [1, 2, 3, 4, 5, 6, '7', d]
assert a == b, (a, b)
a = a[:-1]
a = [x for x in defl.recursiveNonIterableYield(a, oper=int)]
b = [1, 2, 3, 4, 5, 6, 7]
assert a == b, (a, b)
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def listFromArgsStar():
Assert(defl.listFromArgsStar(1, 2, 3, 4)).eq([1, 2, 3, 4])
Assert(defl.listFromArgsStar([1, 2, 3, 4])).eq([1, 2, 3, 4])
Assert(defl.listFromArgsStar([1])).eq([1])
Assert(defl.listFromArgsStar(1)).eq([1])
Assert(defl.listFromArgsStar()).eq(list())
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def listFromArgsStar():
assert (1 == 2 == False) == False
assert ((1 == 2) == False) == True
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
# @tester.add()
def retainProperyReference():
import weakref
@dataclass(slots=T, kw_only=T, frozen=F)
class A:
i: ClassVar[int] = 0
@property
def b(_) -> int:
_.i = _.i + 1
return _.i
2025-03-09 09:17:53 -04:00
class B:
2024-09-11 11:14:03 -04:00
__call__ = None
a = A()
b = B()
b.__call__ = lambda: 1
print(b)
# <__main__.retainProperyReference.<locals>.B object at 0x7517276fa300>
assert b == 1, b
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def listFromArgsStar():
a = [[1]]
b = defl.listFromArgsStar(a)
if len(a) == 1 and inst(a, list | tuple):
c = a[0]
assert a == b
Assert(defl.listFromArgsStar('python - import -p a')).eq(['python - import -p a'])
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def listFromArgsStar():
from ctypes import c_bool
2025-03-09 09:17:53 -04:00
c_bool('test')
2024-09-11 11:14:03 -04:00
@tester.add()
2025-03-09 09:17:53 -04:00
def decor():
class A:
def a():
return 'a'
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
def b(self):
return ('b', self)
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
@classmethod
def c(cls):
return ('c', cls)
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
@staticmethod
def d():
return 'd'
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
a = A()
with testFail(TypeError):
a.a()
Assert(a.b()) is a
Assert(a.c()) is A
Assert(a.d()) == 'd'
Assert(A.a()) == 'a' # call class directly
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
@tester.add()
def contman():
class CM:
def __enter__(_):
_.a = 1
return _
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
def __exit__(_, *args):
_.a = 2
2024-09-11 11:14:03 -04:00
2025-03-09 09:17:53 -04:00
with CM() as cm:
assert cm.a == 1
assert cm.a == 2
2024-09-11 11:14:03 -04:00
log.info(tester.run())
tester.exitWithStatus()
# for i in ch:
# print(defl.printTable([[y for y in x] for x in ch], squareFill=True))