Skip to content

Instantly share code, notes, and snippets.

View imankulov's full-sized avatar

Roman Imankulov imankulov

View GitHub Profile
@imankulov
imankulov / sqlalchemy_with_pydantic.py
Last active May 1, 2024 01:09
Using pydantic models as SQLAlchemy JSON fields (convert beween JSON and pydantic.BaseModel subclasses)
#!/usr/bin/env ipython -i
import datetime
import json
from typing import Optional
import sqlalchemy as sa
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.dialects.postgresql import JSONB
from pydantic import BaseModel, Field, parse_obj_as
@imankulov
imankulov / README.md
Last active March 6, 2024 08:35
Parse JSON-encoded parameters from request queries with FastAPI

Problem: FastAPI doesn't accept JSON-encoded pydantic models in query strings. See #884.

Solution: Use json_param() from the snippet below.

Usage example.

from fastapi import FastAPI
from pydantic import BaseModel
@imankulov
imankulov / test_pool.py
Created June 10, 2014 08:57
Test generator and releaser which know how to create fixtures in separate threads
"""
Generator and Releaser objects which could be used to create and destroy
objects in separate threads. Generator constantly keeps the pool of N objects
to use by the test function (by default N equals to 1).
How to use them:
You create a session-scope generator and releaser. They are subclasses
of threading.Thread, so you should start them as well.
@imankulov
imankulov / fastapi_dependencies.py
Last active March 3, 2023 09:53
FastAPI service with and without dependencies
"""
A simple contrived example of a FastAPI application that heavily uses
the FastAPI dependency injection system.
Sample request http://127.0.0.1:9876/tasks?user_id=123456
returns [{"id":1,"name":"Task 1","user":{"id":123456,"name":"John Doe"}}]
Dependency tree:
get_me()
@imankulov
imankulov / default_session.py
Created January 6, 2023 21:57
A Python request.Session() instance with reasonable defaults for retries.
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
USER_AGENT = "My project (https://example.com)"
def get_default_session() -> requests.Session:
"""Return a request.Session() instance with reasonable defaults for retries.
@imankulov
imankulov / db_test.py
Created June 24, 2013 12:44
Performance test for MySQL UPDATE
# -*- coding: utf-8 -*-
"""
Performance test for different types of update
Results sample
-----------------------------------------------
In [1]: import db_test
@imankulov
imankulov / watchmedo.sh
Created March 26, 2013 19:37
One-liner. Auto-building Sphinx docs with watchdog. Forked from http://jacobian.org/writing/auto-building-sphinx/
# pip install watchdog
exec watchmedo shell-command --patterns='*.rst;*.py' --ignore-pattern='_build/*' --recursive --command='make html' --wait
@imankulov
imankulov / 00_README.md
Last active December 7, 2021 06:32
aio chat client and server examples

This is a simple implementation for TCP server and client. The server listens for the port 9090 of the localhost, receives messages from any connected clients, and sends them to all connected peers. The client is started with a username as an argument, reads in an infinite loop messages from the string, re-format them as "name > message\n" and send to the server. In a different async function it also waits for message from the server and prints the to the console.

Files:

  • aio_chat_client.py: client implementation
  • aio_chat_server.py: server implementation

Dependencies: Python 3.6+ and aioconsole

Install aioconsole

@imankulov
imankulov / chrome_moscow.sh
Created April 5, 2017 16:00
How to run a Google Chrome under Mac OS with a different timezone
#!/bin/bash
export TZ='Europe/Moscow'
exec open -na "Google Chrome" --args "--user-data-dir=$HOME/chrome-profile"
@imankulov
imankulov / cpython.geojson
Last active November 4, 2021 22:50
CPython file type codemaps exported as GeoJSON
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.