Пример #1
0
def rotate(matrix: List[List[optional(int)]]) -> List[List[optional(int)]]:

    if not is_square(matrix):
        raise ValueError()
    # Rotate 90 degrees
    # O(n) + O(n2) + O(n2) ~ O(n2)
    return list(map(list, zip(*matrix[::-1])))
Пример #2
0
    def test_self_referencing_interface(self):
        class TreeNode(Interface):
            pass

        TreeNode.add_attribute('left', optional(TreeNode))
        TreeNode.add_attribute('right', optional(TreeNode))

        class TreeNodeImplementation:
            def __init__(self):
                self.left = None
                self.right = None

        tree = TreeNodeImplementation()
        tree.left = TreeNodeImplementation()
        tree.right = TreeNodeImplementation()

        self.assertIsInstance(tree, TreeNode)

        tree.left = 1
        self.assertNotIsInstance(tree, TreeNode)
Пример #3
0
    def test_self_referencing_interface(self):
        class TreeNode(Interface):
            pass

        TreeNode.add_attribute('left', optional(TreeNode))
        TreeNode.add_attribute('right', optional(TreeNode))

        class TreeNodeImplementation:
            def __init__(self):
                self.left = None
                self.right = None

        tree = TreeNodeImplementation()
        tree.left = TreeNodeImplementation()
        tree.right = TreeNodeImplementation()

        self.assertIsInstance(tree, TreeNode)

        tree.left = 1
        self.assertNotIsInstance(tree, TreeNode)
Пример #4
0
def is_square(matrix: List[List[optional(int)]]) -> bool:
    # Check rows are of equal size O(n)
    width = None
    for i in matrix:
        if not width:
            width = len(i)
        elif width != len(i):
            return False

    # Rectangular matrix O(1)
    if width != len(matrix):
        return False

    return True
Пример #5
0
def get_session(engine: optional(Engine)=None) -> SqlalchemySession:
    """:py:mod:`sqlalchemy` 의 쿼리를 날릴때 사용하는 세션을 가지고옵니다.

    :param sqlalchemy.engine.Engine engine: :py:mod:`sqlalchemy` 엔진
    :return: DB에 쿼리를 날리때 사용하는 세션
    :rtype: :py:class:`sqlalchemy.orm.session.Session`
    """
    if engine is None:
        engine = get_engine()
    if hasattr(g, 'sess'):
        return g.sess
    session = Session(bind=engine)
    try:
        g.sess = session
    except RuntimeError:
        pass
    return session
Пример #6
0
 def test_optional(self):
     self.assertIsInstance(1, optional(int))
     self.assertIsInstance(None, optional(int))
     self.assertNotIsInstance('string', optional(int))
Пример #7
0
 def test(a: optional(int)):
     return a
Пример #8
0
 def test(a: optional(int)):
     return a
Пример #9
0
 def test_optional(self):
     self.assertIsInstance(1, optional(int))
     self.assertIsInstance(None, optional(int))
     self.assertNotIsInstance('string', optional(int))
Пример #10
0
 def __init__(self, validators: list=[], default: optional(datetime)=None,
              name: optional(str)=None):
     validators.append(is_datetime)
     super(DatetimeArgument, self).__init__(validators=validators,
                                            name=name,
                                            default=default)