Skip to content

Instantly share code, notes, and snippets.

View icedraco's full-sized avatar
🐲
Rawr

Artex icedraco

🐲
Rawr
View GitHub Profile
@icedraco
icedraco / pikabu.py
Last active April 26, 2018 13:17
Polling and Telegram notification script for @KogotsuchiDark at pikabu.ru (with persistence!)
#!/usr/bin/env python3
#
# New article polling script for a pikabu.ru page that notifies via a Telegram
# bot or any other BASH command.
#
# Syntax:
# python3 pikabu.py
# ./pikabu.py [after `chmod +x pikabu.py`]
#
# Requirements:
@icedraco
icedraco / permutations.kt
Created February 7, 2018 15:29
Kotlin permutation generator function: creates sequences
/* Created by IceDragon on 07-Feb-2018 */
import kotlin.coroutines.experimental.buildSequence
fun permutations(items: List<Any?>, qty: Int): Sequence<List<Any?>> = buildSequence {
if (items.size < qty || qty == 0)
return@buildSequence
val availableIndices = Array<Int?>(items.size, { it })
val iterators = Array<Iterator<Int>>(qty, { (0 until availableIndices.size).iterator() })
@icedraco
icedraco / product.kt
Last active February 7, 2018 15:30
Cartesian product of multiple iterables as a sequence
/* Created by IceDragon on 06-Feb-2018 */
import kotlin.coroutines.experimental.buildSequence
fun product(vararg lists: Iterable<Any?>): Sequence<List<Any?>> = buildSequence {
if (lists.isEmpty())
return@buildSequence // no combinations
val iterators = Array(lists.size, { i -> lists[i].iterator() })
if (iterators.any { !it.hasNext() })
@icedraco
icedraco / filewatch.py
Created December 16, 2017 21:30
Watch a text file for new content (a-la "tail -f"); reload file if it seems to have been truncated
from time import sleep
def watch_fp(fp, delim: str = "\n"):
buf = []
while True:
raw_input = fp.read()
if raw_input != "":
buf.append(raw_input)
if delim in raw_input:
@icedraco
icedraco / toposort.py
Created August 3, 2017 04:44
Topological sort in python (not mine)
#######################################################################
# Implements a topological sort algorithm.
#
# Copyright 2014 True Blade Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@icedraco
icedraco / is_busy.py
Created August 1, 2017 10:13
Python: Check if system is busy using load average and cpu_count
def is_system_busy(threshold=0.5):
"""
:param float threshold: relative threshold (load_15min/num_cpus) past which the system is considered busy
:return: True if the system is considered busy; False otherwise
:rtype: bool
"""
from psutil import cpu_count
from os import getloadavg
load_1, load_5, load_15 = getloadavg()
return load_15 / cpu_count() >= threshold
@icedraco
icedraco / test.cpp
Created July 28, 2017 19:48
Stuff...
#include <cstdio>
#include <cstdlib>
#include <assert.h>
/*** Factorial ***************************************************************/
#define FACT_LIMIT 17
bool fact_initialized = false;
unsigned long long G_FACTORIAL[FACT_LIMIT];
@icedraco
icedraco / python_logging_conf.ini
Created February 6, 2017 17:19
Sample logging config file for Python (load via logging.config.fileConfig('python_logging_conf.ini'))
[loggers]
keys=root, other
[logger_root]
level=DEBUG
handlers=hand01
[logger_other]
qualname=util.other
level=DEBUG
@icedraco
icedraco / sshrc
Created January 26, 2017 22:23
SSHRC file to notify via telegram script of a new login
#!/bin/bash
# /etc/ssh/sshrc
TELEGRAM=$HOME/bin/telegram
HOST=$(hostname)
DATE=$(date)
MSG="SSH Login: *$USER@$HOST*
Conn: *$SSH_CONNECTION*
@icedraco
icedraco / telegram.sh
Created January 2, 2017 15:10
Simple personal Telegram notifiation script
#!/bin/bash
# Based on the guide here:
# https://www.forsomedefinition.com/automation/creating-telegram-bot-notifications/
TOKEN="################################"
CHAT_ID="12345678"
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
TIME=10
TEXT="$1"