Skip to content

Instantly share code, notes, and snippets.

View hrchu's full-sized avatar
:octocat:
Have an Octotastic day!

petertc hrchu

:octocat:
Have an Octotastic day!
View GitHub Profile
@hrchu
hrchu / s3.py
Created February 24, 2022 02:34
s3 python util/example code
import logging
from typing import Iterable, Dict, BinaryIO
import boto3
from boto3.s3.transfer import TransferConfig
from botocore.config import Config
from botocore.exceptions import ClientError
KB = 1024
MB = KB * KB
@hrchu
hrchu / pycon-us-cfp.md
Last active December 8, 2021 12:47
Decentralized web application with Solid protocol

In 30mins, I will address crucial parts of implementing Solid in Python, so attendees can understand how to build a decentralized web application with Python and get the benefits of this promising architecture.

Overview: why we need decentralized web architecture and what does it look like in a Solid way?

In classical web architecture, application and data are bundled together, hence user data are naturally owned by service providers. This causes security, privacy, interoperability, and many other well-known issues for both users and service providers.

Service providers need to pay additional responsibility to protect user data in the scenario. Moreover, users cannot fully control and migrate their data without additional mechanisms provided by the service providers (GDPR tries to address this in a nontechnical way.)

With Solid, all personal data is stored in a data repository named Pod controlled by users. That could be set up on a user's server at home, or provided by a data storage supplier chos

@hrchu
hrchu / play-n3.ts
Created December 4, 2021 12:10
Use N3.js in TypeScript
// https://github.com/rdfjs/N3.js/
import {Parser, StreamParser, Writer} from "n3";
import * as fs from "fs";
import {DataFactory} from 'n3';
import {Writable} from 'stream';
function fromQualsToAString() {
const writer = new Writer({prefixes: {c: 'http://example.org/cartoons#'}});
const {namedNode, literal, defaultGraph, quad} = DataFactory;
@hrchu
hrchu / gen.py
Last active July 5, 2021 12:58
Python generator simple usage
def gen():
a = range(5)
for x in a:
yield x
x = gen() # instanlize
x.__next__() # get next
Out[36]: 0
next(x) # alternative way
18.04
apt install python3-pip
16.04
sudo apt-get install vim htop nmon ngrep iftop tmux dnsmasq curl ifstat iperf httpie python-virtualenv build-essential
@hrchu
hrchu / blink.py
Last active July 15, 2020 12:00
Blink LED on Raspberry Pi
@hrchu
hrchu / eviocgmodule.c
Last active July 7, 2020 03:01
Get input event device name via ioctl by CPython extension module
#include <Python.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
char* getDeviceName(char* device, char* name, size_t len)
@hrchu
hrchu / README.md
Created March 20, 2020 08:19
Daily report from systemd service/timer via email

TRY IT

  1. sudo apt-get install heirloom-mail
  2. config your email address and MTA server in send-mail.sh
  3. sudo cp send-mail.sh /opt/send-mail.sh
  4. sudo cp dummy.* /etc/systemd/system/
  5. execute sudo systemctl start dummy.service to confirm it works.
  6. sudo systemctl start dummy.timer
  7. Check the schedule via systemctl list-timers
@hrchu
hrchu / test.py
Created November 25, 2019 06:08
Why map/filter is weaker than list comprehension or for loop iterating?
alist = [[1,1],[1,1]]
assert([1, 1] == [x for x, y in alist]) # OK
for x, y in alist: # OK
assert x == 1
assert([1, 1] == list(map(lambda x, y: x, alist))) # TypeError: <lambda>() missing 1 required positional argument: 'y'
@hrchu
hrchu / how-to.md
Last active January 24, 2020 12:47 — forked from reywood/how-to.md
How to get a stack trace from a stuck/hanging python script

How to get a stack trace for each thread in a running python script

Sometimes a python script will simply hang forever with no indication of where things went wrong. Perhaps it's polling a service that will never return a value that allows the program to move forward. Here's a way to see where the program is currently stuck.

Install gdb and pyrasite

Install gdb.

# Redhat, CentOS, etc