Skip to content

Instantly share code, notes, and snippets.

@zampino
Created December 4, 2016 21:32
Show Gist options
  • Save zampino/aaa817010d6d7fcadda584593173bff0 to your computer and use it in GitHub Desktop.
Save zampino/aaa817010d6d7fcadda584593173bff0 to your computer and use it in GitHub Desktop.
julia context eval + hfs5
pip install h5py
Collecting h5py
Downloading h5py-2.6.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.5MB)
100% |████████████████████████████████| 4.5MB 152kB/s
Collecting numpy>=1.6.1 (from h5py)
Downloading numpy-1.11.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.9MB)
100% |████████████████████████████████| 3.9MB 176kB/s
Collecting six (from h5py)
Using cached six-1.10.0-py2.py3-none-any.whl
Installing collected packages: numpy, six, h5py
Successfully installed h5py-2.6.0 numpy-1.11.2 six-1.10.0
(.pye) amantini@amantini:~/dev/elixir/nextjournal (master $%) python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.pwd()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pwd'
>>> import h5py
>>> f = h5py.File('foo.jld', 'r')
>>> f.name
u'/'
>>> f.keys
<bound method File.keys of <HDF5 file "foo.jld" (mode r)>>
>>> f.keys()
[u'_creator', u'u', u'v', u'x']
>>> f['u']
<HDF5 dataset "u": shape (), type "<i8">
>>> f['u']()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Dataset' object is not callable
>>> f.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'File' object has no attribute 'read'
>>> f['/u']
<HDF5 dataset "u": shape (), type "<i8">
>>> f.values()
[<HDF5 group "/_creator" (5 members)>, <HDF5 dataset "u": shape (), type "<i8">, <HDF5 dataset "v": shape (), type "|S6">, <HDF5 dataset "x": shape (), type "<i8">]
>>> f.get('u')
<HDF5 dataset "u": shape (), type "<i8">
>>> f.get('v')
<HDF5 dataset "v": shape (), type "|S6">
>>> f.get('x')
<HDF5 dataset "x": shape (), type "<i8">
>>> f.require_dataset('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: require_dataset() takes at least 4 arguments (2 given)
>>> f.require_dataset('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: require_dataset() takes at least 4 arguments (2 given)
>>> g.get('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'g' is not defined
>>> f.get('x')
<HDF5 dataset "x": shape (), type "<i8">
>>> type(f.get('x'))
<class 'h5py._hl.dataset.Dataset'>
>>> f['x'] + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Dataset' and 'int'
>>> f['x'].dtype
dtype('int64')
>>> f['x'].shape
()
>>> f['x'].val
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Dataset' object has no attribute 'val'
>>> f['x'].value
1
>>> f['u'].value
1
>>> f['v'].value
'234234'
>>> d = dict()
>>> d
{}
>>> def hallo(x, v):
... d[x] = v
... return None
...
>>> hallo('foo', 1)
>>> d
{'foo': 1}
>>> def hallo(x, v):
... local d = {}
File "<stdin>", line 2
local d = {}
^
SyntaxError: invalid syntax
>>> def hallo(x, v):
... local d
File "<stdin>", line 2
local d
^
SyntaxError: invalid syntax
>>> def hallo(x, v):
... d = {}
... local d
File "<stdin>", line 3
local d
^
SyntaxError: invalid syntax
>>> d
{'foo': 1}
>>> globals()
{'hallo': <function hallo at 0x10e37e230>, 'd': {'foo': 1}, 'f': <HDF5 file "foo.jld" (mode r)>, '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'sys': <module 'sys' (built-in)>, 'h5py': <module 'h5py' from '/Users/amantini/dev/elixir/nextjournal/.pye/lib/python2.7/site-packages/h5py/__init__.pyc'>, '__name__': '__main__', '__doc__': None}
>>> ctx = {}
>>> eval("x = 1", ctx)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
x = 1
^
SyntaxError: invalid syntax
>>> h eval
File "<stdin>", line 1
h eval
^
SyntaxError: invalid syntax
>>> help("eval")
>>> eval(compile("x = 1"), ctx)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Required argument 'filename' (pos 2) not found
>>> eval(parse("x = 1"), ctx)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'parse' is not defined
>>> parsecode("x = 1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'parsecode' is not defined
>>> import code
>>> code.compile_command("x = 1", '<input>', 'eval')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codeop.py", line 122, in compile_command
return _maybe_compile(_compile, source, filename, symbol)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/codeop.py", line 99, in _maybe_compile
raise SyntaxError, err1
File "<input>", line 1
x = 1
^
SyntaxError: invalid syntax
>>> code.compile_command("x = 1", '<input>', 'exec')
<code object <module> at 0x10d169f30, file "<input>", line 1>
>>> c = code.compile_command("x = 1", '<input>', 'exec')
cc = {}
>>> eval(c, globals(), cc)
>>> cc
{'x': 1}
>>> expr = "def foo(x):\n return x\n"
>>> f = code.compile_command(expr, '<input>', 'exec')
>>> cc
{'x': 1}
>>> eval(f, globals(), cc)
>>> cc
{'x': 1, 'foo': <function foo at 0x10e5c2500>}
>>> f = h5py.File('foo.jld', 'r')
>>> cc
{'x': 1, 'foo': <function foo at 0x10e5c2500>}
>>> f
<HDF5 file "foo.jld" (mode r)>
>>> def assign(n, ds):
... cc[n] = ds.value
... return None
...
>>> f.visititems(assign)
dd = {}
>>> def assign(n, o):
... if '_creator' in n:
... return None
... dd[n] = o.value
... return None
...
>>> f
<HDF5 file "foo.jld" (mode r)>
>>> f.visititems(assign)
>>> dd
{u'x': 1, u'u': 1, u'v': '234234'}
>>> f = h5py.File('bar.py', 'w')
>>> f.create_dataset('my_var', (), 'f')
<HDF5 dataset "my_var": shape (), type "<f4">
>>> f['my_var']
<HDF5 dataset "my_var": shape (), type "<f4">
f['bla'] = 122342343
>>> f
<HDF5 file "bar.py" (mode r+)>
>>> f.keys()
[u'bla', u'my_var']
>>> f['bla
File "<stdin>", line 1
f['bla
^
SyntaxError: EOL while scanning string literal
>>> f['bla']
<HDF5 dataset "bla": shape (), type "<i8">
>>> np.arange(1:100)
File "<stdin>", line 1
np.arange(1:100)
^
SyntaxError: invalid syntax
>>> np.arange(100)
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
>>> f['arange
File "<stdin>", line 1
f['arange
^
SyntaxError: EOL while scanning string literal
>>> f['arange'] = arange(1000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'arange' is not defined
>>> f['arange'] = np.arange(1000)
>>> f
<HDF5 file "bar.py" (mode r+)>
>>> f.keys()
[u'arange', u'bla', u'my_var']
>>> f['arange']
<HDF5 dataset "arange": shape (1000,), type "<i8">
>>> f['arange'][0;1]
File "<stdin>", line 1
f['arange'][0;1]
^
SyntaxError: invalid syntax
>>> f['arange'][0;1]
File "<stdin>", line 1
f['arange'][0;1]
^
SyntaxError: invalid syntax
>>> f['arange'][0:1]
array([0])
>>> f['arange'][0:2]
array([0, 1])
>>> f['arange'][0:3]
array([0, 1, 2])
>>> f
<HDF5 file "bar.py" (mode r+)>
>>> f.flush()
>>> f.close()
julia> module Ctx
function eval_ex(s)
eval(Ctx, parse(s))
end
function exports()
setdiff(names(Ctx, true), push!(base_names, :base_names))
end
function dumps()
local d = Dict()
for name in exports()
if typeof(eval(Ctx, name)) != Function
d[string(name)] = eval(Ctx, name)
end
end
return d
end
global base_names = names(Ctx, true)
end
WARNING: replacing module Ctx
Ctx
julia> Ctx.eval_ex("x = 1")
1
julia> Ctx.eval_ex("u, v = [1, \"234234\"]")
2-element Array{Any,1}:
1
"234234"
julia> Ctx.eval_ex("f(x) = 2x")
f (generic function with 1 method)
julia>
julia> Ctx.dumps()
Dict{Any,Any} with 3 entries:
"v" => "234234"
"x" => 1
"u" => 1
julia> using HDF5, JLD
INFO: Precompiling module HDF5...
INFO: Precompiling module JLD...
julia> save("foo.jld", Ctx.dumps())
julia> d = load("foo.jld")
Dict{ByteString,Any} with 3 entries:
"v" => "234234"
"x" => 1
"u" => 1
julia> save("foo.jld", Ctx.dumps())
julia> py = load("bar.py")
ERROR: ArgumentError: invalid ASCII sequence
in convert at /Applications/Julia-0.4.5.app/Contents/Resources/julia/lib/julia/sys.dylib
in detectavi at /Users/amantini/.julia/v0.4/FileIO/src/registry.jl:120
in query at /Users/amantini/.julia/v0.4/FileIO/src/query.jl:410
in query at /Users/amantini/.julia/v0.4/FileIO/src/query.jl:366
in load at /Users/amantini/.julia/v0.4/FileIO/src/loadsave.jl:42
julia> d = load("foo.jld")
Dict{ByteString,Any} with 3 entries:
"v" => "234234"
"x" => 1
"u" => 1
julia> py = load("bar.py")
ERROR: ArgumentError: invalid ASCII sequence
in convert at /Applications/Julia-0.4.5.app/Contents/Resources/julia/lib/julia/sys.dylib
in detectavi at /Users/amantini/.julia/v0.4/FileIO/src/registry.jl:120
in query at /Users/amantini/.julia/v0.4/FileIO/src/query.jl:410
in query at /Users/amantini/.julia/v0.4/FileIO/src/query.jl:366
in load at /Users/amantini/.julia/v0.4/FileIO/src/loadsave.jl:42
julia> data = h5read("bar.py", "arange")
1000-element Array{Int64,1}:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
julia> d = h5read("bar.py", "my_var")
0.0f0
julia> d = h5read("bar.py", "bla")
122342343
julia> typeof(d)
Int64
julia> d = h5read("bar.py", "my_var")
0.0f0
julia> typeof(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment