Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@doujiang24
doujiang24 / gist:fd5c7a46831a3df02f57
Created November 23, 2015 03:49
Lua get hostname
local ffi = require "ffi"
local C = ffi.C
ffi.cdef[[
int gethostname(char *name, size_t len);
]]
local size = 50
local buf = ffi.new("unsigned char[?]", size)
@neomantra
neomantra / gethostbyname.lua
Created September 25, 2013 20:25
Exercising gethostbyname with LuaJIT FFI
#!/usr/bin/luajit
local ffi = require 'ffi'
ffi.cdef([[
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
@hrldcpr
hrldcpr / tree.md
Last active April 15, 2024 15:27
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@haldean
haldean / while_test.py
Created February 14, 2012 17:22
Test "while 1" vs. "while True" in Python
# Testing results (lower number is faster):
# while True while 1
# pypy 1.7.0: 0.0733 0.0201
# cpython 2.7.1 0.146 0.109
# cpython 3.2.2 2.79 2.72
loops = 10000
trials = 1000
def while_True():
@leegao
leegao / Rationale.md
Created July 9, 2011 02:30
JIT for dummies: JIT compiling RPN in python

If you don't care about the explanation, scroll down to find the code, it's 50 some odd lines and written by someone who doesn't know any better. You have been warned.

What it does

This is a very simple proof of concept jitting RPN calculator implemented in python. Basically, it takes the source code, tokenizes it via whitespace, and asks itself one simple question: am I looking at a number or not?

First, let's talk about the underlying program flow. Pretend that you are a shoe connoisseur with a tiny desk. You may only have two individual shoes on that desk at any one time, but should you ever purchase a new one or get harassed by an unruly shoe salesman without realizing that you have the power to say no (or even maybe?), you can always sweep aside one of the two shoes on the desk (the one on the right, because you're a lefty and you feel that the left side is always superior) onto the messy floor, put the other shoe on the right hand side, and then place your newly acquired shoe in

@chuangbo
chuangbo / README.md
Last active June 19, 2023 04:48
Python dynamic DNSPod DNS Script

替换上你的 API Token,域名ID,记录ID等参数,就可以运行了。 会在后台一直运行,每隔30秒检查一遍IP,如果修改了就更新IP。

获取 API Token 的方式

获得 domain_id 可以用 curl

curl -k https://dnsapi.cn/Domain.List -d "login_token=TOKEN"`
@larsks
larsks / gist:111839
Created May 14, 2009 19:30
An example of using the itertools module
# 2009-05-14 13:49 < spdr-:#python> hello. is there an elegant way
# to loop for at most N iterations? for a in collection: ...
# 2009-05-14 13:49 < KirkMcDonald:#python> spdr-:
for x in itertools.islice(collection, N):
pass