Skip to content

Instantly share code, notes, and snippets.

View hellpanderrr's full-sized avatar
🎯
Focusing

hellpanderrr

🎯
Focusing
View GitHub Profile
@hellpanderrr
hellpanderrr / selenium_save_image.py
Created December 11, 2020 20:41
python selenium save image as base64
url = 'https://www.gravatar.com/avatar/ad45f075aa4d69cbd552b671a32bf3ed?s=32&d=identicon&r=PG&f=1'
def save_image_as_base64(browser, url):
"""
"""
base = browser.execute_async_script("""
var done = arguments[0];
function toDataURL(url, callback) {
var xhr = new XMLHttpRequest();
@hellpanderrr
hellpanderrr / svgs_into_pdf.py
Created March 30, 2020 19:52
Convert a list of svg files into one pdf document
from PyPDF2 import PdfFileMerger
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
def svgs_to_pdf(file_paths=[r'C:\1.svg',r'C:\2.svg',r'C:\3.svg'], output_folder='tmp', pdf_name='test.pdf'):
"""
Convert svg files into pdfs, save pdfs in output_folder, merge them in one pdf file.
@param file_paths: full paths to svg files
@param output_folder: path to folder for saving temporary pdfs
@param pdf_name: name for resulting pdf file.
@hellpanderrr
hellpanderrr / _.py
Last active August 18, 2019 21:08
python scipy numpy add points to existing distance matrix
import numpy as np
from scipy.spatial import distance
def add_points_to_distance_matrix(points, original_array, distance_matrix, metric='euclidean'):
"""
There is an NxM array of points, a square matrix NxN with distances between points.
This function adds new points to the distance matrix.
We need to create a diagonal block holding distances between the new points
and two identical blocks holding distances to original array.
E.g.
@hellpanderrr
hellpanderrr / .py
Created April 18, 2018 15:32
python traverse n-ary tree depth-first without recursion
def dfs_traverse(root):
stack = []
stack.append(root)
while (stack):
node = stack.pop()
for child in node.children:
stack.append(child)
process_node(node)
@hellpanderrr
hellpanderrr / 1.py
Created April 18, 2018 15:28
python traverse n-ary tree breadth-first without recursion
from collections import deque
def bfs_traverse(root):
queue = deque([])
queue.append(root)
while (queue):
node = queue.popleft()
for child in node.children:
queue.append(child)
@hellpanderrr
hellpanderrr / index.html
Last active March 3, 2017 16:14
simple plot keras model in bokeh
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bokeh Plot</title> <link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-0.12.1.min.css" type="text/css" /> <script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-0.12.1.min.js"></script> <script type="text/javascript"> Bokeh.set_log_level("info"); </script> <style> html { width: 100%; height: 100%; } body { width: 90%; height: 100%; margin: auto; } </style> </head> <body> <div class="bk-root"> <div class="plotdiv" id="8f4aa28a-874e-433f-958f-e92f767d005d"></div> </div> <script type="text/javascript"> Bokeh.$(function() { var docs_json = {"be98bf1a-5a89-48ca-b8ef-b563f0b4001a":{"roots":{"references":[{"attributes":{"fill_color":{"value":"blue"},"inner_radius":{"units":"data","value":0.1},"line_color":{"value":"blue"},"outer_radius":{"units":"data","value":0.1},"x":{"value":2},"y":{"field":"y"}},"id":"af43fdc4-5308-4d5e-94d5-ee6751f9f733","type":"Annulus"},{"attributes":{"plot":null,"text":null},"id
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hellpanderrr
hellpanderrr / list_unpack.py
Last active February 21, 2019 08:16
python unpack list with 0 or 1 elements, as an expression (without exceptions)
#e.g. if you have a list comprehension that can return only 1 element or an empty list
>>> reduce(lambda x,y:y, [], None)
None
>>> reduce(lambda x,y:y, [5], None)
5
>>> return [5] if [5] else None
5
Mon May 09 05:55:14 2016 old
28055818 function calls (28031179 primitive calls) in 108.555 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
3666302 33.412 0.000 55.143 0.000 utils.py:307(find)
6232384 12.518 0.000 16.739 0.000 utils.py:266(_getrange)
165520 8.038 0.000 64.496 0.000 layout.py:601(isany)
@hellpanderrr
hellpanderrr / gist:a751d16905d0c27d61ea334f6e9212c9
Last active May 8, 2016 20:24
pdfminer-20140328 profiling
DSD_Y41-650-D-PermitsCompleted.pdf 34 pages
Sun May 08 22:53:13 2016 restats_w
53819963 function calls (53796078 primitive calls) in 187.163 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)