Skip to content

Instantly share code, notes, and snippets.

View pssolanki111's full-sized avatar
😉

P S Solanki pssolanki111

😉
View GitHub Profile
@pssolanki111
pssolanki111 / tda_streaming_template.py
Last active July 26, 2022 17:09
TDA streaming template to work around the symbols limits
import asyncio
import tda
class TheStreamer:
def __init__(self):
self.queue = asyncio.Queue()
self.stream_client = StreamClient(client, account_id=CONFIG.account_number)
await self.login()
@pssolanki111
pssolanki111 / async_rate_limiter.py
Last active July 26, 2022 16:18
A Rate limiter for async applications. Implements the Leaky Bucket Algorithm
import asyncio
import contextlib
from collections import OrderedDict
import time
class AsyncLeakyBucket(contextlib.AbstractAsyncContextManager):
def __init__(self, max_rate: float, time_period: float = 60, loop=None):
self._loop, self._max_level = loop, max_rate
self._rate_per_sec, self._level = max_rate / time_period, 0.0
@pssolanki111
pssolanki111 / high_performance_polygon_streamer_design.py
Last active November 15, 2023 16:01
This is an advanced use case for streaming a high number of symbols on polygon websockets. This uses a combination of uvloop, orjson, aioredis, grouping messages before transmitting and async streaming to achieve high throughput. This can be easily modified or extended to support simpler or more complex use cases. The key idea is keeping the str…
import asyncio
import polygon
from polygon.enums import StreamCluster
import config
import uvloop
from orjson import dumps
import aioredis
from datetime import datetime, time
# Specify our event loop policy
@pssolanki111
pssolanki111 / first_fit_decreasing.py
Last active April 28, 2022 05:23
Implementation of `First Fit Decreasing` for `Bin Packing Problem`
# Solving the Bin Packing Problem (https://www.wikiwand.com/en/Bin_packing_problem) with first fit algorithm
def first_fit_decreasing(weights: list, max_weight: int) -> list:
"""
An Implementation of "First Fit Decreasing Algorithm" for the "Bin Packing Problem"
:param weights: A List of weights of items to fit
:param max_weight: Maximum weight a Bin can hold
:return: A list of lists, each list containing elements in that bin
"""
bins, current_key, weights = defaultdict(list), 1, sorted(weights, reverse=True)
# =============================================================== #
from pprint import pprint
from tda.orders.generic import OrderBuilder
from typing import Union
import sys
from tda.orders.common import (one_cancels_other as oco,
first_triggers_second as trigger)
# =============================================================== #
@pssolanki111
pssolanki111 / chrome_remote_desktop.py
Last active December 28, 2021 14:02
CRD daemon python file, patched to use default session instead of creating new virtual session. Fixes many bugs in CRD for linux based systems.
#!/usr/bin/python3
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Virtual Me2Me implementation. This script runs and manages the processes
# required for a Virtual Me2Me desktop, which are: X server, X desktop
# session, and Host process.
# This script is intended to run continuously as a background daemon
# process, running under an ordinary (non-root) user account.
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau

alrightttyyy. @Baker XBL and anyone who cares : D

Before i go on to the explanation, keep in mind the difference of IO bound operations, and CPU bound operations. and if possible, recall the short note I commented earlier about which one to use in what situation. It's fine if you don't. You can find that in the very end of this note. But it is nicer to first read the other stuff in sequence.

=================================================

the most basic difference is how they are implemented. threads and processes (multiprocessing) are what we know as concurrent execution. In python, threads are not concurrent due to implementation restrictions (the GIL) but in most languages (even python built on other langs - Jpython or iron python), threads are concurrent. In python, threaded workers run in their own threads but due to the GIL restriction, only one thread can use the interpreter python.exe at a time (which is a fancy way to say: only one thread can be running code at a time).

so even though pyt

@pssolanki111
pssolanki111 / MCVE.py
Last active May 13, 2024 15:23
A working solution to the question.
# this program is just to create an MCVE and to test the detection code. It's not an actual program.
import threading, time
from pynput.keyboard import KeyCode, Listener, Key
from pynput.mouse import Button, Controller
delay = 0.3
button = Button.right
start_stop_key = Key.f6
exit_key = KeyCode(char='e')