Skip to content

Instantly share code, notes, and snippets.

View jimbaker's full-sized avatar

Jim Baker jimbaker

View GitHub Profile
@jimbaker
jimbaker / war-contents
Created March 6, 2014 20:31
Fireside setup
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/lib/
WEB-INF/lib/fireside-0.1-single.jar
WEB-INF/web.xml
@jimbaker
jimbaker / install-maven-artifacts.py
Created February 4, 2014 04:44
Translation of InstallArtifacts.java from aether-demo-snippets
# analog to InstallArtifacts.java
# run from aether-demo-snippets dir; relies on target/, pom.xml
from java.io import File
from org.eclipse.aether import RepositorySystem
from org.apache.maven.repository.internal import MavenRepositorySystemUtils
from org.eclipse.aether.artifact import DefaultArtifact
from org.eclipse.aether.installation import InstallRequest
from org.eclipse.aether.spi.connector import RepositoryConnectorFactory
from org.eclipse.aether.util.artifact import SubArtifact
@jimbaker
jimbaker / clamp-constructor.py
Created January 16, 2014 21:53
Possible constructor DSL for Clamp
from java.lang import Integer, String
from clamp import Constructor
BarBase = clamp_base("bar")
class Foo(BarBase, Callable):
# need to specify a no-args constructor with @Constructor() if
# other constructors are specified, given that __init__ needs
# specific args to unpack, possibly with defaults
@jimbaker
jimbaker / clamp-metaprogramming.md
Created January 15, 2014 21:55
Clamp uses 6 - or maybe 7 - metaprogramming functionalities available in Python, Jython specifically, and Java.

Clamp employs six (possibly seven) metaprogramming functionalities in Python/Jython:

  1. Metaclass support, which required unifying Java types (rooted by java.lang.Class) and Python new-style classes (rooted by type). This then enable the metaclass to build the desired class shape. It can do the following: constant type inference; construction of getters/setters; etc.

  2. Brand new, __proxymaker__ allows for a CustomMaker to intercept

@jimbaker
jimbaker / typesignature.py
Last active January 3, 2016 09:19
Possible Clamp DSL to support reflection on Java generics, which will then generate the necessary type signatures in Jython
# Uses names from a motivating paste by Oti Humbel, http://pastebin.com/1deBqEAV
from some_source import View, IEntityView, EntityManagerPlug2
# clamp_base is implemented, extends is a hypothetical class decorator similar to proposed support
# for Java class annotations that will rewrite the class bytecode with the necessary type signature data, using
# http://asm.ow2.org/asm40/javadoc/user/org/objectweb/asm/signature/SignatureVisitor.html
from clamp import clamp_base, extends
@jimbaker
jimbaker / metaclass.diff
Created January 8, 2014 16:32
Jython trunk currently does not allow mixing together in the use of a metaclass Java classes/interfaces with Python types, even though both are new-style classes. Currently this gets a TypeError: Error when calling the metaclass bases (metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all …
diff -r b2890af7a5e8 src/org/python/core/PyType.java
--- a/src/org/python/core/PyType.java Thu Jan 02 16:25:39 2014 -0800
+++ b/src/org/python/core/PyType.java Wed Jan 08 09:31:55 2014 -0700
@@ -1119,6 +1119,10 @@
return best;
}
+ private static boolean isJavaRootClass(PyType type) {
+ return type instanceof PyJavaType && type.fastGetName().equals("java.lang.Class");
+ }
@jimbaker
jimbaker / monkeypatch
Created January 8, 2014 16:18
You can monkeypatch any imported Java class, just as if it's a Python class.
>>> from java.util.concurrent import ConcurrentHashMap as CHM
>>> def silly(self): return 2 * len(self)
...
>>> CHM.silly = silly
>>> x = CHM()
>>> x.silly()
0
>>> x["a"] = 42
>>> x.silly()
2
@jimbaker
jimbaker / gist:8061856
Created December 20, 2013 21:29
Problem in running setup.py from 2.0.2 branch of setuptools
$ jython-ssl setup.py install
running install
Checking .pth file support in /Users/jbaker/jythondev/jython-ssl/dist/Lib/site-packages/
/Users/jbaker/jythondev/jython-ssl/dist/bin/jython -E -c pass
TEST PASSED: /Users/jbaker/jythondev/jython-ssl/dist/Lib/site-packages/ appears to support .pth files
/Users/jbaker/opensource/setuptools/setuptools/command/easy_install.py:304: UserWarning: Unbuilt egg for setuptools [unknown version] (/Users/jbaker/opensource/setuptools)
self.local_index = Environment(self.shadow_path+sys.path)
running bdist_egg
running egg_info
writing setuptools.egg-info/PKG-INFO
@jimbaker
jimbaker / nettyclient.py
Last active December 31, 2015 17:09
Rough translation using Netty 4 of blocking SSL socket example in Python docs.
# Rough translation of http://docs.python.org/2/library/ssl.html#client-side-operation
from io.netty.bootstrap import Bootstrap, ChannelFactory
from io.netty.buffer import PooledByteBufAllocator, Unpooled
from io.netty.channel import ChannelInboundHandlerAdapter, ChannelInitializer, ChannelOption
from io.netty.channel.nio import NioEventLoopGroup
from io.netty.channel.socket.nio import NioSocketChannel
from io.netty.handler.ssl import SslHandler
from javax.net.ssl import SSLContext
from java.util.concurrent import TimeUnit
@jimbaker
jimbaker / blocking_aio.py
Last active December 30, 2015 00:59
Support for blocking and nonblocking sockets, using Asynchronous*Channel support in Java 7. Next step is to consider how to implement SSLEngine in conjunction.
from java.net import InetSocketAddress
from java.nio.channels import AsynchronousSocketChannel as ASC
from java.nio import ByteBuffer
from array import array
remote = "jim-baker.com"
s = ASC.open()
s.connect(InetSocketAddress(remote, 80)).get()