Skip to content

Instantly share code, notes, and snippets.

@GaryLee
GaryLee / Dockerfile
Last active October 22, 2022 19:39
Dockerfile to create CENTOS7 with ssh server and x11 server.
FROM centos:centos7
MAINTAINER Gary Lee <garywlee@gmail.com>
# Install ssh server.
RUN yum install -y which openssh-clients openssh-server
RUN ssh-keygen -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN sed -i '/pam_loginuid.so/c session optional pam_loginuid.so' /etc/pam.d/sshd
# Set root's password
@GaryLee
GaryLee / listfiles.py
Created November 29, 2016 16:16
List all text files.
#!python
import glob
for fn in glob.glob('*.txt'):
with open(fn, 'r') as fd:
for ln, content in enumerate(fd):
print '%s:%d> %s' % (fn, ln, content),
print '\n'
@GaryLee
GaryLee / nonblocking_input_win32.py
Created December 23, 2016 02:08
Nonblocking keyboard input example on Windows.
import msvcrt
round = 0
while True:
if msvcrt.kbhit():
if msvcrt.getch() in ('q', 'Q'):
print "Quit"
break
print "Current round: %d" % round
round += 1
@GaryLee
GaryLee / plot_spec.py
Created January 5, 2017 02:20
A example to show how to plot spectrum for given signal.
# -*- coding: utf-8 -*-
"""
Plot spectrum of given signal.
"""
from numpy import sin, linspace, pi
from pylab import plot, show, title, xlabel, ylabel, subplot
from scipy import fft, arange
def plot_spectrum(y, freq_sampling):
@GaryLee
GaryLee / hex2utf8.py
Created February 15, 2017 13:34
Convert script file which contain hex string that are converted from utf-8.
#!python
# coding: utf-8
import os
import re
import os.path as path
import glob
import codecs
src_folder = 'xxx'
@GaryLee
GaryLee / cmd.py
Last active October 2, 2017 13:00
Execute shell command in Perl-like manner.
import sys
import os
from subprocess import CalledProcessError, check_output
def cmd(cmd_arg):
"""Execute shell command in Perl-like manner."""
ctx = globals().copy()
ctx.update(sys._getframe(1).f_locals)
for ln in check_output(cmd_arg.format(**ctx), shell=True).split(os.linesep):
yield ln
@GaryLee
GaryLee / fmt.py
Last active October 2, 2017 13:04
Format string with caller's variable scope.
def fmt(spec):
"""Format string with caller's variable scope."""
return spec.format(**sys._getframe(1).f_locals)
# fmt() can be also written in lambda form.
# fmt = lambda s: s.format(**sys._getframe(1).f_locals)
a = 10
b = 20
c = 30
@GaryLee
GaryLee / ini2tmpl.py
Last active June 23, 2017 10:37
Generate content according to INI and template.
#!python
# coding: utf-8
"""Generate content according to INI and template."""
import sys
import os
import codecs
from argparse import ArgumentParser
from ConfigParser import ConfigParser
from collections import OrderedDict
@GaryLee
GaryLee / yaml2tmpl.py
Last active July 13, 2017 03:37
Generate file by YAML definition and mako template.
#!python
# coding: utf-8
"""Generate content according to YAML and template."""
import sys
import os
import codecs
import yaml
from argparse import ArgumentParser
from mako.template import Template
@GaryLee
GaryLee / fft_analyze.py
Last active October 11, 2020 15:45
Do DFT and plot with Bokeh.
from bokeh.plotting import figure, output_file, vplot, show
import numpy as np
import random
n = 1024
a = 1.0
sample_period = 0.001
t = np.linspace(0, n * sample_period, n)
dt = t[1] - t[0]
data = np.abs(np.sin(t * 2 * np.pi) * a)