Skip to content

Instantly share code, notes, and snippets.

View MagnetonBora's full-sized avatar
🚀
Focusing

MagnetonBora

🚀
Focusing
View GitHub Profile
@MagnetonBora
MagnetonBora / excel_example.y
Created October 25, 2023 13:53
This is an example of how to append some records to a given Excel spreadsheet without altering the rest of the spreadsheet, using pandas and openpyxl.
import openpyxl # for working with Excel files
import pandas as pd # for working with DataFrames
from openpyxl.utils.dataframe import dataframe_to_rows # for converting DataFrames to rows in Excel
# openpyxl and pandas need to be installed with pip
# Function to generate sample data as a DataFrame
def generate_sample_data():
return pd.DataFrame({
@MagnetonBora
MagnetonBora / products.py
Last active May 20, 2021 18:29
Original vs refactored
import json
import os
import pathlib
from . import db, config
sponsored_id_list = config.get_sponsored()
def pyramid(n: int) -> list:
size = 2 * n - 1
table = [size * [0] for _ in range(size)]
for start in range(n):
for j in range(size - start):
table[start][start + j] = start + 1
for j in range(size - start):
table[size - 1][start + j] = start + 1
for i in range(size - start):
table[start + i][start] = start + 1
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Sequence, create_engine
engine = create_engine('sqlite:///:memory:')
Base = declarative_base()
class User(Base):
# This program will demonstrate how we can use python decorators
# to measure the time of function invoking
import time
def chrono(fn):
def wrapper(*args):
start = time.time() # get current time before running the function
from collections import defaultdict
def encode(text):
result = []
freq = defaultdict(int)
for prev, current in zip(text[:-1], text[1:]):
freq[prev] += 1
if current != prev:
result.append((prev, freq[prev]))
import re
import numpy as np
from collections import Counter
def read_data(filename):
with open(filename, 'r') as f:
return f.readlines()
from functools import lru_cache
@lru_cache(maxsize=None)
def calculate_probability_recursively(red, green):
if green == 0 or red == 0:
return 1
p0 = red / (red + green)
if green == 1:
from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession
MAX_WORKERS = 4
URL = "http://mdemo.webtm.ru/bot/test.php?notify_type=signal&callput=CALL&symbol=%A0&tfdigi=5&tfdur=m&logints=R2D2&passts=123123"
def fire_and_forget(url, executor=None):
session = FuturesSession(executor=executor)
@MagnetonBora
MagnetonBora / convert.sh
Created February 16, 2017 21:36
Convert banch of pictures to another format
#!/bin/bash
# http://mywiki.wooledge.org/BashFAQ/001
# http://askubuntu.com/questions/343727/filenames-with-spaces-breaking-for-loop-find-command
function usage {
echo "Example usage: convert.sh dir base-extension target-extenstion"
}
if [[ -z $1 ]]