Skip to content

Instantly share code, notes, and snippets.

View linkerlin's full-sized avatar
🎯
Focusing

Halo Master linkerlin

🎯
Focusing
View GitHub Profile
@linkerlin
linkerlin / gist:5720106
Created June 6, 2013 08:23
Caches library for Python 2.7
import collections
import functools
from itertools import ifilterfalse
from heapq import nsmallest
from operator import itemgetter
class Counter(dict):
'Mapping where default values are zero'
@linkerlin
linkerlin / jarimport.py
Last active December 11, 2015 21:19
A jar importor for Jython. You only need to add a fullpath of a jar to sys.path , then you can use "Class.forName()". Need classpathhacker.py https://gist.github.com/4654376
import imp
import os
import sys
from marshal import Unmarshaller
from classpathhacker import *
jarloader=ClassPathHacker()
__debugging__ = True
def __readPycHeader(file):
@linkerlin
linkerlin / jymysql.py
Created January 28, 2013 15:15
A demo for using mysql in Jython.
from __future__ import with_statement
from com.ziclix.python.sql import zxJDBC
import sys
#sys.path+=[r'/home/linker/jars/mysql-connector-java.jar']
#sys.path+=[r'/home/linker/jarss/']
from classpathhacker import *
jarloader=classPathHacker()
#jarloader.addFile(r'/home/linker/jars/mysql-connector-java.jar')
@linkerlin
linkerlin / classpathhacker.py
Last active December 11, 2015 20:18
A jar loader for jython. If you want add some jars to JVM in Jython, you can use the following class. Modified from: http://www.jython.org/jythonbook/en/1.0/appendixB.html#working-with-classpath 这是一个修改版本的classPathHacker,用于在Jython里加载一个Jar给Java Code使用。不同于在sys.path里面添加jar。此方法可以让JavaCode也使用到新加载的Jar,而不仅仅是JythonCode. 添加sys.path的方法参见:http://my.oschina.…
class ClassPathHacker :
##########################################################
# from http://forum.java.sun.com/thread.jspa?threadID=300557
#
# Author: SG Langer Jan 2007 translated the above Java to this
# Jython class
# Modified by: Linker Lin linker.lin@me.com
# Purpose: Allow runtime additions of new Class/jars either from
# local files or URL
######################################################
@linkerlin
linkerlin / cached.py
Created January 24, 2013 13:53
A cache decorator with a ability of lossing memory .
def cached(function):
cache = {}
def wrapper(*args):
if args in cache:
ret=cache[args]
if random.randint(1,100)>95: memo.pop(args) # random forget something
return ret
else:
result = function(*args)
@linkerlin
linkerlin / jyswing.py
Created January 23, 2013 10:50
A demo for jython and swing
#! -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print sys.defaultencoding,u"中文"
from java.awt import Color
from javax.swing import ImageIcon
@linkerlin
linkerlin / header.py
Created January 23, 2013 09:03
A general header for Python files.
#! -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')