Skip to content

Instantly share code, notes, and snippets.

View ods's full-sized avatar
🇺🇦
Stand with Ukraine

Denis Otkidach ods

🇺🇦
Stand with Ukraine
View GitHub Profile
@ods
ods / maybe_raises.py
Created August 11, 2022 11:54
pytest.raises() variation accepting None for cases when no exception is expected
from contextlib import nullcontext
from typing import ContextManager, Optional, Union
import pytest
def maybe_raises(
exception: Union[type[BaseException], tuple[type[BaseException], ...]]
) -> ContextManager[Optional[pytest.ExceptionInfo[BaseException]]]:
if exception is None:
import asyncio
from contextlib import AsyncExitStack, ExitStack
import contextvars
import functools
import inspect
import sys
__all__ = ['exit_stack']
import time, asyncio, hashlib
def sync_cpu_bound():
s = bytes(range(256))
for i in range(100000):
hashlib.sha256(s)
start = time.time()
sync_cpu_bound()
@ods
ods / bench_asyncio_lock.py
Created September 27, 2018 12:43
Benchmark for PR to serialize execution of SQL in asyncpg: https://github.com/MagicStack/asyncpg/pull/367
"""
Benchmark for PR to serialize execution of SQL in asyncpg.
See: https://github.com/MagicStack/asyncpg/pull/367
"""
import asyncio
from asyncio import Lock
class _Atomic:
@ods
ods / sa_doc_link_blocks.py
Last active August 29, 2015 14:06
Bug in SQLAlchemy: None assigned to relationship is not stored to DB
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Link(Base):
__tablename__ = 'Link'
id = Column(Integer, primary_key=True)
doc_id = Column(Integer, ForeignKey('Doc.id'), nullable=False)
import sphinx_xmlpipe2
pipe = sphinx_xmlpipe2.Pipe(
fields = ['title', 'description', 'body'],
attrs = [sphinx_xmlpipe2.Timestamp('date'), sphinx_xmlpipe2.Int8('type')],
)
with pipe:
for doc in docs:
pipe.add(
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, create_engine
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.orderinglist import ordering_list
Base = declarative_base()
class Parent(Base):
__tablename__ = 'parents'
@ods
ods / test2.py
Last active January 1, 2016 07:19 — forked from anonymous/test2.py
registry = Registry()
ReplicatedModel = registry.multidb(
admin=AdminBase,
front=FrontBase,
)
@registry.model(ReplicatedModel)
def Media(models):
@ods
ods / sa_merge_m2m_backref.py
Last active December 24, 2015 12:09
This schema fails as well as relationship definitions on both side with back_populates. Relationship definitions on both side without back_populates work fine. Exception is: sqlalchemy.orm.exc.FlushError: New instance <B at 0x2135e90> with identity key (<class '__main__.B'>, (1,)) conflicts with persistent instance <B at 0x212d850> Commenting th…
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, ForeignKey, create_engine
from sqlalchemy.orm import sessionmaker, Session, relationship
Base = declarative_base()
class AB(Base):
__tablename__ = 'AB'
a_id = Column(ForeignKey('A.id'), primary_key=True)