Skip to content

Instantly share code, notes, and snippets.

View jimbaker's full-sized avatar

Jim Baker jimbaker

View GitHub Profile
$ jython27 -Dpython.security.respectJavaAccessibility=false
Jython 2.7b3+ (default:68aaff268c3c, Sep 10 2014, 20:03:31)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_21
>>> from org.python.tests import Invisible
>>> dir(Invisible)
['EXTRA_ARG_PUBLIC_METHOD', 'EXTRA_ARG_PUBLIC_STATIC_METHOD', 'OVERLOADED_EXTRA_ARG_PUBLIC_METHOD', 'OVERLOADED_PROTECTED_METHOD', 'OVERLOADED_PUBLIC_METHOD', 'OVERLOADED_PUBLIC_STATIC_METHOD', 'OVERLOADED_STATIC_METHOD', 'PACKAGE_FIELD', 'PACKAGE_METHOD', 'PACKAGE_STATIC_FIELD', 'PACKAGE_STATIC_METHOD', 'PROTECTED_FIELD', 'PROTECTED_METHOD', 'PROTECTED_STATIC_FIELD', 'PROTECTED_STATIC_METHOD', 'PUBLIC_FIELD', 'PUBLIC_METHOD', 'PUBLIC_METHOD_FIELD', 'PUBLIC_STATIC_FIELD', 'PUBLIC_STATIC_METHOD', 'PUBLIC_STATIC_METHOD_FIELD', 'SUBCLASS_OVERLOAD', 'SUBCLASS_OVERRIDE', 'SUBCLASS_STATIC_OVERLOAD', 'SUBCLASS_STATIC_OVERRIDE', 'SUBVISIBLE_SHARED_NAME_FIELD', 'UNUSED', 'VISIBLE_SHARED_NAME_FIELD', '__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', '_
@jimbaker
jimbaker / test_cjk.py
Last active August 29, 2015 14:01
Working with CJK encodings
from array import array
from jarray import zeros
from java.nio import ByteBuffer, CharBuffer
from java.nio.charset import Charset, CodingErrorAction # FIXME actually use CodingErrorAction
shift_jis = Charset.forName("Shift_JIS")
decoder = shift_jis.newDecoder()
jis_data = open("shift_jis.txt", "rb").read() # data encoded in shift_jis format
utf_8_data = open("shift_jis-utf8.txt", "rb").read() # same data, encoded as utf-8
input_buffer = ByteBuffer.wrap(array("b", jis_data)) # FIXME break into chunks
@jimbaker
jimbaker / subprocess.diff
Created May 27, 2014 20:28
Diff between CPython and Jython's subprocess module
392a393
> jython = sys.platform.startswith("java")
397d397
< import gc
399d398
< import errno
427a427,439
> elif jython:
> import errno
> import threading
@jimbaker
jimbaker / gist:a13cdfbbe7d1a108aef7
Created May 6, 2014 17:21
Regrtest output for Jython
[exec] 355 tests OK.
[exec] 13 tests skipped:
[exec] test__osx_support test_commands test_crypt test_curses test_dbm
[exec] test_lib2to3 test_pickle test_pipes test_readline test_smtpnet
[exec] test_subprocess test_urllib2net test_urllibnet
[exec] 8 skips unexpected:
[exec] test__osx_support test_commands test_crypt test_dbm test_lib2to3
[exec] test_pickle test_pipes test_readline
[exec] 17 tests failed:
[exec] test_asynchat test_asyncore test_binascii test_dumbdbm test_gzip
@jimbaker
jimbaker / greenlet.py
Created April 8, 2014 16:36
Greenlet emulation for Jython using threads. Development has moved to https://github.com/jythontools/artificialturf, but this shows my original thoughts. There are reports of running the JVM with 1000s or low 10000s of threads on Linux, but this emulation at least can be used as a bridge to better solutions, such as asyncio or Quasar.
import threading
from java.util.concurrent import ArrayBlockingQueue
class GreenletExit(Exception):
pass
# Consider two greenlets, which we will name alice and bob. Some code
# is running in the context of alice, then it calls
@jimbaker
jimbaker / conditional.py
Created March 21, 2014 15:01
Conditional Jython class definition
try:
from better.baz.package import ImprovedBar
class Foo(ImprovedBar):
...
except ImportError:
from ok.baz.package import ImprovedBar
class Foo(Bar):
...
# class Foo is now available, but Clamp in general cannot make both available
@jimbaker
jimbaker / how-to-test.sh
Last active August 29, 2015 13:57
One way to isolate tests in test_socket for running against socket-reboot branch (https://bitbucket.org/jimbaker/jython-socket-reboot). There are some minor fixes in this gist that will be eventually committed, but mostly it just comments out tests and test suites that are currently have problems.
$ cp test_socket.py dist/Lib/test/. && dist/bin/jython dist/Lib/test/test_socket.py
@jimbaker
jimbaker / mapping-none-to-null.txt
Created March 13, 2014 17:13
Example of Java <-> Jython integration around None <-> null conversion
$ jython27
Jython 2.7b1+ (default:6e438088c0e3, Feb 10 2014, 11:00:25)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_21
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.util import HashMap
>>> x = {"a": 42, "b": None}
>>> y = HashMap(x)
>>> y
{b=null, a=42}
@jimbaker
jimbaker / alljars
Created March 7, 2014 18:07
Makes a classpath compatible list of paths, including expanding all globs and making all paths absolute. Run like so: export CLASSPATH=$CLASSPATH:$(alljars some/*/*.jar)
#!/usr/bin/env python
import glob
import os
import sys
def main():
cwd = os.getcwd()
print ":".join(os.path.normpath(os.path.join(cwd, path)) for path in sys.argv[1:])
@jimbaker
jimbaker / apachebench.txt
Created March 6, 2014 22:29
Fireside overhead, as tested by ab. This doesn't include any issues around GC pauses.
$ ab -k -c 10 -n 5000 localhost:8080/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests