extracting data from typing types

Checking if a variable conforms to a typing object To check if string_list conforms to string_list_class, you can use the typeguard type checking library. from typeguard import check_type try: check_type(‘string_list’, string_list, string_list_class) print(“string_list conforms to string_list_class”) except TypeError: print(“string_list does not conform to string_list_class”) Checking the generic type of a typing object To check if … Read more

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

Your understanding is correct: invoking PyEval_InitThreads does, among other things, acquire the GIL. In a correctly written Python/C application, this is not an issue because the GIL will be unlocked in time, either automatically or manually. If the main thread goes on to run Python code, there is nothing special to do, because Python interpreter … Read more

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

This error message… ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine …implies that the initialization of a new WebBrowsing Session i.e. Firefox Browser session was aborted. An established connection was aborted by the software in your host machine As per your code attempt the error is clearly coming … Read more

Python type hint for (any) class

I’d recommend using a combination of TypeVar, to indicate that your self.o value could be any arbitrary type, and Type, in the following way: from typing import TypeVar, Type T = TypeVar(‘T’) class MyObj: def __init__(self, o: T) -> None: self.o = o def get_obj_class(self) -> Type[T]: return type(self.o) def accept_int_class(x: Type[int]) -> None: pass … Read more

How to make two rows in a pandas dataframe into column headers

If using pandas.read_csv() or pandas.read_table(), you can provide a list of indices for the header argument, to specify the rows you want to use for column headers. Python will generate the pandas.MultiIndex for you in df.columns: df = pandas.read_csv(‘DollarUnitSales.csv’, header=[0,1]) You can also use more than two rows, or non-consecutive rows, to specify the column … Read more

SqlAlchemy asyncio orm: How to query the database

session.query is the old API. The asynchronous version uses select and accompanying methods. from sqlalchemy.future import select from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker engine = create_async_engine(_build_async_db_uri(CONFIG.general.sqlalchemy_db_uri)) async_session = sessionmaker( engine, expire_on_commit=False, class_=AsyncSession ) CACHE = {} async def _load_all(): global CACHE try: async with async_session() as session: q = select(TableClass) result = … Read more

how to cache asyncio coroutines

Maybe a bit late, but I’ve started a new package that may help: https://github.com/argaen/aiocache. Contributions/comments are always welcome. An example: import asyncio from collections import namedtuple from aiocache import cached from aiocache.serializers import PickleSerializer Result = namedtuple(‘Result’, “content, status”) @cached(ttl=10, serializer=PickleSerializer()) async def async_main(): print(“First ASYNC non cached call…”) await asyncio.sleep(1) return Result(“content”, 200) if … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)