Skip to content

Instantly share code, notes, and snippets.

View lanfon72's full-sized avatar
💭
Nothing new

Lanfon lanfon72

💭
Nothing new
View GitHub Profile
@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 / 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 / fb.py
Last active February 27, 2023 17:36
fb feeds
import facebook
token = "your token"
api = facebook.GraphAPI(token)
timestamp = " your timestamp "
feeds = api.request('/me/posts/', args={'filter':'created_time', 'until':timestamp})
@lanfon72
lanfon72 / export.py
Created June 8, 2017 03:39
export word images via python win32com
from pathlib import Path
from win32com.client import Dispatch
xls = Dispatch("Excel.Application")
doc = Dispatch("Word.Application")
def export_images(fp, prefix="img_", suffix="png"):
""" export all of images(inlineShapes) in the word file.
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 / ohlc.py
Created February 7, 2017 09:22
OHLC chart with XlsxWriter.
# extended from http://xlsxwriter.readthedocs.io/example_chart_stock.html
from datetime import datetime
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_ohlc_stock.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})
date_format = workbook.add_format({'num_format': 'dd/mm/yyyy'})
@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']