Skip to content

Instantly share code, notes, and snippets.

View BitAndQuark's full-sized avatar

Bit and Quark BitAndQuark

View GitHub Profile
@BitAndQuark
BitAndQuark / mmapcopy.c
Created June 16, 2019 09:57
CSAPP: Use memory mapping to copy any file on disk to stdout.
// Use memory mapping to copy any file on disk to stdout.
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv) {
long sz;
@BitAndQuark
BitAndQuark / lxml_parse_QC_logs.py
Created March 7, 2019 09:47
Use lxml to parse xml QC logs
import lxml.html
from lxml import etree
root = etree.fromstring(open('UserDefine-UUT ID-2019_02_27_01_51_00_179--00_P.xml').read())
# Get all "Test" nodes
root.xpath('//Test')
# Get date stamp
'/'.join((root.xpath('./*[1]/Date/YYYY')[0].text, root.xpath('./*[1]/Date/MM')[0].text, root.xpath('./*[1]/Date/DD')[0].text))
@BitAndQuark
BitAndQuark / selenium_parse_html.py
Created March 7, 2019 09:14
example of using selenium to parse HTML
# Get a table object
table = driver.find_element_by_class_name('my_table_class')
# Get all rows, including rows in rows
rows = table.find_elements_by_tag_name('tr')
# Get header which is in the first row
# Get text from <th> element
header = rows[0]
header_text_list = [i.text for i in header.find_elements_by_tag_name('th')]
@BitAndQuark
BitAndQuark / selenium_example.py
Created March 7, 2019 08:47
example of using selenium to interact with web page
# webdriver can initialize object that control supported browsers, such as Chrome
from selenium import webdriver
# Keys contain keyboard keys such as left arrow
from selenium.webdriver.common.keys import Keys
# Select can wrap html <select> tag into an object that has powerful methods
from selenium.webdriver.support.ui import Select
# this is a parser for HTML
import lxml.html
# Create driver object
@BitAndQuark
BitAndQuark / decorator.py
Last active December 23, 2018 07:07
Python decorator
# Python decorator is a convenient way to wrap functions with reused logic
def Dec(F):
def G(*args):
print('wrapper starts')
F(*args)
print('wrapper ends')
return G
@Dec
@BitAndQuark
BitAndQuark / descriptor.py
Created December 23, 2018 06:48
Python descriptor
# descriptor, is like a simplification on property
# one descriptor can be used in multiple classes, instead of repeated property definitions
class Name:
def __get__(self, instance, owner):
print(instance, 'get name')
return instance._name
def __set__(self, instance, value):
print(instance, 'set name to', value)
instance._name = value
@BitAndQuark
BitAndQuark / two_section_treeview_labelview.py
Created September 15, 2018 06:38
two section GUI: treeview and labelview
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as mbx
import tkinter.filedialog as fdg
import tkinter.scrolledtext as stxt
class MyWindow(tk.Tk):
def __init__(self, screenName=None, baseName=None, className='Tk', useTk=1, sync=0, use=None,
geometry='600x400', title='My tkinter Window'):
super().__init__(screenName, baseName, className, useTk, sync, use)
def merge_sort(input_list: list) -> list:
"""Sort list using merge sort algorithm.
:param input_list: list to be sorted.
:type input_list: list
:return: sorted list in ascending order.
:rtype: list
"""
def merge(lst:list, p: int, q: int, r: int):
"""Helper function for merge sort: merge lst[p:q+1] and lst[q+1:r+1].
:param lst: list to be sorted.
def insertion_sort(input_list: list) -> list:
"""Sort a list with insertion sort algorithm.
:param input_list: a list to be sorted.
:type input_list: list
:return: sorted list in ascending order.
:rtype: list
"""
# Check if need to sort
if input_list == [] or len(input_list) == 1:
return input_list
@BitAndQuark
BitAndQuark / adb_screenshot.bat
Created May 18, 2018 08:26 — forked from Adam--/adb_screenshot.bat
Take an Android screenshot using ADB on Windows
adb devices
adb shell screencap -p /sdcard/screen.png
adb pull -p -a /sdcard/screen.png
adb shell rm /sdcard/screen.png
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-3 delims=/:" %%a in ("%TIME%") do (set mytime=%%a-%%b-%%c)
set mytime=%mytime: =%