Skip to content

Instantly share code, notes, and snippets.

View Syncrossus's full-sized avatar

Syncrossus

View GitHub Profile
def histogram(in_list):
hist = [[0] * len(in_list) for _ in range(max(in_list))]
for i in range(len(hist)):
for j, val in enumerate(in_list):
if val > i:
hist[i][j] = 1
out_str = ''
for subl in hist[::-1]:
for val in subl:
if val:
@Syncrossus
Syncrossus / python_monokaish.nanorc
Last active January 14, 2022 20:33
My attempt at making a Monokai python syntax coloring for nano based on scopatz/nanorc
## Python syntax highlighting rules for Nano
## The colors chosen here may seem strange.
## In my terminal, "yellow" = orange, "magenta" = purple, "brightyellow" = yellow, "cyan" = teal.
## Since there is no actual magenta, I used "brightred" which is the closest I could get.
## Since there is no grey, black is difficult to read in a terminal and setting the background to
## white is harsh on the eyes, I decided to use teal for comments.
syntax "python" "\.py$"
header "^#!.*/(env +)?python[-0-9._]*( |$)"
magic "Python script"
@Syncrossus
Syncrossus / generate_random_names.py
Created September 6, 2021 11:34
This function attempts to generate mostly pronounceable character combinations to create random names.
import string
import random
vowels = 'aeiouy'
consonants = list(set(string.ascii_lowercase).difference(set(vowels)))
def generate_name():
name = ''
consecutive_consonants = 0
for i in range(random.randint(3, 10)):
new_letter = random.choice(string.ascii_lowercase)
@Syncrossus
Syncrossus / merge_pdf.py
Last active October 22, 2021 13:43
A script for merging PDF files which doesn't halt with a PdfReadError. No longer works with some newer PDFs. Based on this thread: https://github.com/mstamy2/PyPDF2/issues/244
import PyPDF2 as PDF
import sys
def pdf_cat(allPdfFiles):
merger = PDF.PdfFileMerger(strict=False)
for filename in allPdfFiles:
merger.append(PDF.PdfFileReader(filename, strict=False))
@Syncrossus
Syncrossus / comment_proportions.py
Last active December 6, 2019 16:13
Determines the proportion of python code that is comments. To use, type `python comment_proportions.py file1.py [file2.py] [file3.py] [...]` in your terminal.
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import BBCodeFormatter
import re
import sys
def get_comments(code):
""" Extracts comments and docstrings from python code.
@Syncrossus
Syncrossus / roman_numerals.py
Last active September 18, 2019 09:12
This is an attempt at making the most concise roman numeral "solver" possible using recursivity.
import sys
from numpy import argmax
translate = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
@Syncrossus
Syncrossus / find_dominant_color.py
Last active October 4, 2019 11:14
Finds the dominant color in an image. Requires files from https://gist.github.com/Syncrossus/38e2886205602443620c871957ddfdd6
import sys
from math import ceil
from hsv_to_rgb import hsv_to_rgb
from rgb_to_hsv import rgb_to_hsv
def compress_color(pixel, level=16):
""" Compresses color by a specified factor.
Compressed colors are rounded up.
@Syncrossus
Syncrossus / hsv_to_rgb.py
Last active September 5, 2019 08:40
Functions to convert RGB values to HSV and vice versa, and a code sample to get them working on an image.
def hsv_to_rgb(pixel):
""" converts a pixel expressed in RGB to an HSV expression
see https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB
Args:
pixel<(h, s, v)>:
h <float[0-360]>: hue
s <float[0-1]>: saturation
v <float[0-1]>: "value" (brightness)
Return:
r <int[0-255]>: red value
@Syncrossus
Syncrossus / xkcd_car_model_names.py
Last active October 5, 2019 01:55
Car model name score calculator based on XKCD #1571 : "Car Model Names"
import sys
# values for 0-9 a-z as specified in the comic, with an additional 0 for
# unrecognized characters.
values = [60, -74, 6, 55, 35, 74, 6, -58, -67, -37, -14, -5, 27, -21, -45, 5,
27, -44, -21, 64, 32, 12, 19, -46, -80, -27, 40, 8, 15, -18, -68,
41, -20, 126, -90, 83, 0]
def to_index(c):
""" Determines the index to look at in the `values' list given a character c
@Syncrossus
Syncrossus / useful_regexes.pl
Last active October 22, 2021 13:55
A collection of useful regular expressions, in PCRE and Python syntax
# Garbage matcher
# matches any string consisting of only |, <, >, +, *, ^, #, =, and hyphen chains.
# this is to identify patterns like ++<===> ######## <---->^^ which serve no purpose but to decorate the text
/(\||(--+)|(__+)|<|>|\+|\*|\^|#|=|~)+|(\\|_|\/){2,}/
# Garbage matcher 2
# matches anything that isn't a letter, space or basic punctuation.
# this is typically useful for cleaning up emojis
/.(?<!([a-zA-Z0-9]|,|\.|'| |\?|\!))/