Skip to content

Instantly share code, notes, and snippets.

View willhyper's full-sized avatar

Chao-Wei Chen willhyper

View GitHub Profile
@willhyper
willhyper / AESCipher.py
Created June 13, 2017 03:06 — forked from mguezuraga/AESCipher.py
Encrypt & Decrypt using PyCrypto AES 256From http://stackoverflow.com/a/12525165/119849
#!/usr/bin/env python
import base64
from Crypto import Random
from Crypto.Cipher import AES
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-s[-1]]
import hashlib as hasher
import datetime as date
# Define what a Snakecoin block is
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
@willhyper
willhyper / key.py
Created November 8, 2018 20:07
python signal KeyboardInterrupt
import signal
def handler(sig, frame):
print(sig)
raise KeyboardInterrupt
signal.signal(signal.SIGINT, handler) # press Ctrl+C to trigger handler
import sys
if sys.platform == 'win32':
@willhyper
willhyper / ss.py
Created November 8, 2018 20:11
spiral search
import sys
import numpy as np
N = int(sys.argv[1])
def SSN(N : int):
ss = SS()
ss_true = SS_ground_truth()
for i in range(N):
pos = next(ss)
pos_true = next(ss_true)
@willhyper
willhyper / click_helloworld.py
Created November 8, 2018 20:14
click hello world
import click
@click.command()
@click.option('-c','--count', default=1, type=int, help="this is count as int")
@click.option('-n','--name', prompt="name please?", help="this is name as str")
def hello(name, count):
"""help message hello somebody"""
for i in range(count):
click.secho(name, fg='green', blink=True, bold=True)
@willhyper
willhyper / bias.py
Created November 8, 2018 20:17
the 1 bias in std proved by simulation
'''
when using n samples to estimate population std,
we will put (n-1) in the denominator:
population_std = sqrt( sample_sum_of_err_sq / (n-1))
i.e.,
population_var * (n-1) = sample_sum_of_err_sq
@willhyper
willhyper / test_fixture.py
Created November 8, 2018 20:42
parameterize fixture template
#!/usr/bin/env python3 -m pytest -v test_fixture.py
import pytest
xy = [(1, 2), (3, 4)]
@pytest.mark.parametrize('x,y', xy)
def test_fixture(x, y):
print('access fixture attributes', x, y)
@willhyper
willhyper / test_unittest.py
Created November 8, 2018 20:46
pytest and unitest are not compatiable
#!/usr/bin/env python3 -m pytest -v test_unittest.py
import unittest
import pytest
xy1 = (1, 2)
xy2 = (3, 4)
@pytest.fixture(scope="class", params=[xy1, xy2])
@willhyper
willhyper / Program.cs
Created May 10, 2019 00:18
pythonnet 2.4.0 overloads functions with ref arguments
// say this code is compiled to test.dll
using System;
namespace Test
{
public class Class1
{
public static void SetByRef(ref Int32 x)
@willhyper
willhyper / ref.cpp
Last active May 13, 2019 21:51
pass by reference and variants
#include <iostream>
using namespace std;
void setByValue(int x) { x = 5; }
void setByReference(int &x) { x = 7; }
void setByPointer(int *x) { *x = 8; }
int main()
{
int x = 0;