Skip to content

Instantly share code, notes, and snippets.

View lanfon72's full-sized avatar
💭
Nothing new

Lanfon lanfon72

💭
Nothing new
View GitHub Profile
import time
import asyncio
import contextvars
from random import random
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor
# declare context var
request_id = contextvars.ContextVar('Id of request.')
@lanfon72
lanfon72 / __main__.py
Created March 6, 2021 14:15
fix contextvar behaviors inside asyncio REPL
import ast
import asyncio
import code
import concurrent.futures
import inspect
import sys
import threading
import types
import warnings
import contextvars
@lanfon72
lanfon72 / async_ydl.py
Created March 7, 2020 12:19
integrate youtube_dl with asyncio.
import asyncio
from datetime import datetime
from functools import partial
from concurrent.futures import ProcessPoolExecutor
from youtube_dl import YoutubeDL as YDL
PPE = ProcessPoolExecutor()
@lanfon72
lanfon72 / self.py
Created May 22, 2018 17:13
wtf self and __class__
class A:
def calc(self):
print('A', __class__, self)
class B(A):
def calc(self):
print('B', __class__, self)
super().calc()
class C(B):
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main(url, path="chromedriver.exe"):
chrome_options = Options()
chrome_options.add_argument("--headless") # uncomment for headless
driver = webdriver.Chrome(path, chrome_options=chrome_options)
driver.get(url)
@lanfon72
lanfon72 / ob.py
Created September 29, 2017 10:09
observer for websocket.
import asyncio
from weakref import WeakSet
from json import dumps, loads
def _validator(dict_data):
""" Default validator function for Observer class. """
return dict_data['event'], dict_data['data']
@lanfon72
lanfon72 / async_zipfile.py
Created September 14, 2017 14:17
zipfile polyfill for async extract and extractall.
import os
import asyncio
from zipfile import ZipFile
__all__ = ("ZipFile", )
@asyncio.coroutine
def async_extract(self, member, path=None, pwd=None, loop=None):
loop = loop or asyncio.get_event_loop()
@lanfon72
lanfon72 / requsts_garner.py
Created August 4, 2017 09:42
saving files from garner ftp with requests.
import os
from ftplib import FTP
import requests
def get_connect(url):
root, foldr = url.split('//', 1)[-1].split('/', 1)
ftp = FTP(root)
ftp.login()
@lanfon72
lanfon72 / xls.py
Created August 4, 2017 09:22
open and save xls file.
import os
from win32com.client import Dispatch
def calc_formula(fname):
xls = Dispatch("Excel.Application")
return xls.Workbooks.Open(os.path.abspath(fname)).Close(True)
@lanfon72
lanfon72 / async_garner.py
Last active August 4, 2017 09:46
saving files from garner ftp.
import os
from ftplib import FTP
import asyncio
import aiohttp
from aiofiles import open as aopen
def get_connect(url):
root, foldr = url.split('//', 1)[-1].split('/', 1)