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])))
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)
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
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
def test_optional(self): self.assertIsInstance(1, optional(int)) self.assertIsInstance(None, optional(int)) self.assertNotIsInstance('string', optional(int))
def test(a: optional(int)): return a
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)