Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
import threading
from collections import deque
import functools
import traceback
MAX_THREADS = 8
class ThreadPool(object):
@caetanus
caetanus / bencode.cpp
Last active August 10, 2021 19:39
Qt Bencode and Decoder and Torrent Creator
#include "bencode.h"
BEncode::BEncode()
: m_decodeError{ false }
{
}
QByteArray BEncode::encode(QVariant value)
{
return encodeVariant(value);
#include "XMLHttpRequest.h"
XMLHttpRequest::XMLHttpRequest(QObject *parent)
: QObject(parent),
p_status(0),
p_readyState(0)
{
}
void XMLHttpRequest::open(QString method, QString url)
@caetanus
caetanus / sqrt.c
Last active August 29, 2015 14:10
binary search sqrt
#include <stdio.h>
#include <stdlib.h>
#define PRECISION 0.0000001
#define POW2(x) x * x
#define ABS(x) x > 0 ? x : -x
#define TEST(VALUE, EXPECT) {\
printf("testing: %0.4f expecting %0.4f", VALUE, EXPECT);\
if (ABS(_sqrt(VALUE) - EXPECT) <= PRECISION)\
@caetanus
caetanus / single_linked_list.py
Created November 6, 2014 23:01
single linked list
# -*- coding: utf-8 -*-
import sys
class _ListNode(object):
def __init__(self, obj):
self._obj = obj
self._next = None
def _append(self, obj):
@caetanus
caetanus / popcount.c
Created October 12, 2014 20:44
popcount compile with gcc -std=c99
#include <stdio.h>
#include <stdint.h>
uint8_t lookup_table[0xFFFF] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2,
3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3,
4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3,
@caetanus
caetanus / bitmap_printing.py
Created July 27, 2014 22:14
bitmap printing for cb55-c
def prepare_bitmap(self, bitmap, align="left"):
bitmap = bitmap.convert("1")
width = self._bitmap_res\
if bitmap.size[0] > self._bitmap_res else bitmap.size[0]
height = bitmap.size[1]
if width < bitmap.size[0]:
bitmap = bitmap.crop((0, 0, width, height))
@caetanus
caetanus / lpr_client.py
Created July 27, 2014 18:06
LPR client
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import select
class RSerialError(IOError):
pass
@caetanus
caetanus / lpr_server.py
Last active August 29, 2015 14:04
LPR server
#!/usr/bin/env python2.7
"""
LPR server for remote control to serial port via network supporting auto discover of the server.
"""
import serial
import socket
import select
@caetanus
caetanus / connect.h
Created December 10, 2013 02:08
Qt4 lambda connect
#include <function>
#include <QString>
#include <QObject>
class LambdaConnectorHelper: public QObject
{
Q_OBJECT
LambdaConnectorHelper(QObject *parent, const function<void()> &f)
: QObject(parent), m_lamba(f)
{