Julia : How to use "include" function on 2 modules each other? - include

I'm a beginner in JuliaLang.
I can't understand a thing : how to use "include" function on 2 modules ?
I have two julia code files (modules)
main.jl code file
module main
include("xmd.jl") # Here i include
a = "Hey, i'm a !"
println(xmd.b)
end
xmd.jl code file
module xmd
include("main.jl")
b = "Hey, i'm b !"
println(main.a)
end
When I try to compile the main.jl code file (on the same directory as xmd.jl), I get an error
LoadError: LoadError: LoadError: LoadError: LoadError: LoadError: LoadError: LoadError: LoadError: LoadError: LoadError: StackOverflowError:
Stacktrace:
[1] exec at ./pcre.jl:133 [inlined]
[2] match(::Regex, ::String, ::Int64, ::UInt32) at ./regex.jl:197
[3] match at ./regex.jl:195 [inlined]
[4] match at ./regex.jl:210 [inlined]
[5] splitdir(::String) at ./path.jl:132
[6] dirname(::String) at ./path.jl:151
[7] _include_dependency(::Module, ::String) at ./loading.jl:738
[8] include_relative(::Module, ::String) at ./loading.jl:1030
[9] include at ./sysimg.jl:29 [inlined]
[10] include(::String) at /home/lazen/Bureau/Lazen-dev/tests/main.jl:1
[11] top-level scope at none:0
[12] include at ./boot.jl:317 [inlined]
[13] include_relative(::Module, ::String) at ./loading.jl:1038
[14] include at ./sysimg.jl:29 [inlined]
[15] include(::String) at /home/lazen/Bureau/Lazen-dev/tests/tests.jl:1
[16] top-level scope at none:0
[17] include at ./boot.jl:317 [inlined]
[18] include_relative(::Module, ::String) at ./loading.jl:1038
... (the last 10 lines are repeated 1034 more times)
[10359] include at ./sysimg.jl:29 [inlined]
[10360] include(::String) at /home/lazen/Bureau/Lazen-dev/tests/main.jl:1
[10361] top-level scope at none:0
[10362] include at ./boot.jl:317 [inlined]
[10363] include_relative(::Module, ::String) at ./loading.jl:1038
[10364] include(::Module, ::String) at ./sysimg.jl:29
[10365] exec_options(::Base.JLOptions) at ./client.jl:229
in expression starting at /home/lazen/Bureau/Lazen-dev/tests/main.jl:2
in expression starting at /home/lazen/Bureau/Lazen-dev/tests/tests.jl:2
in expression starting at /home/lazen/Bureau/Lazen-dev/tests/main.jl:2
I'm struggling since almost 2 days for this bug. I don't know where it comes from. Does it means I can't include a module on another module if it's vice-versa ?
Thanks

Related

How to solve "LoadError: could not load library compiling error" probably coming from PyPlot, in Juno command prompt?

When I try to compile my Julia file in a Julia command prompt, as so :
julia> include("file.jl")
it shows me this error, which I can't wrap my head around :
[ Info: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]
ERROR: LoadError: LoadError: could not load library "/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0"
/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0: wrong ELF class: ELFCLASS64
Stacktrace:
[1] dlopen(::String, ::UInt32) at /buildworker/worker/package_linux32/build/usr/share/julia/stdlib/v1.0/Libdl/src/Libdl.jl:97
[2] top-level scope at /home/toto/.julia/packages/PyCall/zqDXB/src/startup.jl:47 [inlined]
[3] top-level scope at ./none:0
[4] include at ./boot.jl:317 [inlined]
[5] include_relative(::Module, ::String) at ./loading.jl:1044
[6] include at ./sysimg.jl:29 [inlined]
[7] include(::String) at /home/toto/.julia/packages/PyCall/zqDXB/src/PyCall.jl:1
[8] top-level scope at none:0
[9] include at ./boot.jl:317 [inlined]
[10] include_relative(::Module, ::String) at ./loading.jl:1044
[11] include(::Module, ::String) at ./sysimg.jl:29
[12] top-level scope at none:2
[13] eval at ./boot.jl:319 [inlined]
[14] eval(::Expr) at ./client.jl:393
[15] top-level scope at ./none:3
in expression starting at /home/toto/.julia/packages/PyCall/zqDXB/src/startup.jl:41
in expression starting at /home/toto/.julia/packages/PyCall/zqDXB/src/PyCall.jl:34
ERROR: LoadError: Failed to precompile PyCall [438e738f-606a-5dbb-bf0a-cddfbfd45ab0] to /home/toto/.julia/compiled/v1.0/PyCall/GkzkC.ji.
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] compilecache(::Base.PkgId, ::String) at ./loading.jl:1203
[3] _require(::Base.PkgId) at ./loading.jl:960
[4] require(::Base.PkgId) at ./loading.jl:858
[5] require(::Module, ::Symbol) at ./loading.jl:853
[6] include at ./boot.jl:317 [inlined]
[7] include_relative(::Module, ::String) at ./loading.jl:1044
[8] include(::Module, ::String) at ./sysimg.jl:29
[9] top-level scope at none:2
[10] eval at ./boot.jl:319 [inlined]
[11] eval(::Expr) at ./client.jl:393
[12] top-level scope at ./none:3
in expression starting at /home/toto/.julia/packages/PyPlot/XHEG0/src/PyPlot.jl:4
ERROR: LoadError: LoadError: LoadError: Failed to precompile PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee] to /home/toto/.julia/compiled/v1.0/PyPlot/oatAj.ji.
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] compilecache(::Base.PkgId, ::String) at ./loading.jl:1203
[3] _require(::Base.PkgId) at ./loading.jl:960
[4] require(::Base.PkgId) at ./loading.jl:858
[5] require(::Module, ::Symbol) at ./loading.jl:853
[6] include at ./boot.jl:317 [inlined]
[7] include_relative(::Module, ::String) at ./loading.jl:1044
[8] include(::Module, ::String) at ./sysimg.jl:29
[9] include(::String) at ./client.jl:392
[10] top-level scope at none:0
[11] include at ./boot.jl:317 [inlined]
[12] include_relative(::Module, ::String) at ./loading.jl:1044
[13] include(::Module, ::String) at ./sysimg.jl:29
[14] include(::String) at ./client.jl:392
[15] top-level scope at none:0
[16] include at ./boot.jl:317 [inlined]
[17] include_relative(::Module, ::String) at ./loading.jl:1044
[18] include(::Module, ::String) at ./sysimg.jl:29
[19] include(::String) at ./client.jl:392
[20] top-level scope at none:0
in expression starting at /home/toto/project/code/julia/file3.jl:1
in expression starting at /home/toto/project/code/julia/file2.jl:2
in expression starting at /home/toto/project/code/julia/file.jl:1
How do I solve this?
I note that I have installed PyPlot :
julia> import Pkg; Pkg.add("PyPlot")
Resolving package versions...
Updating `~/.julia/environments/v1.0/Project.toml`
[no changes]
Updating `~/.julia/environments/v1.0/Manifest.toml`
[no changes]
I also note that I am getting this error in a second Julia command window using Juno, since Juno's standard REPL is occupied running some other code. This other code in REPL includes my "faulty" files file.jl, file2.jl and file3.jl as well, and runs just fine there.
The problem solved itself after I tried to open a second REPL by clicking in the menu bar Juno -> Open External REPL, instead of my initial Juno -> New Terminal -> Current File's Folder.

What is the best way to merge hashes?

I'm having two similar hashes. The first one looks like:
{'1'=>
{'ab'=>{'a'=>1, 'b'=>4},
{'bc'=>{'b'=>2, 'c'=>1},
...
}
The second one is pretty similar:
{'1'=>
{'ab'=>{'v'=>1},
{'bc'=>{'v'=>2},
...
}
I'd like to merge these like:
{'1'=>
{'ab'=>{'a'=>1, 'b'=>4, 'v'=>1},
{'bc'=>{'b'=>2, 'c'=>1, 'v'=>2},
...
}
What is the best way to do that?
For these simple case:
h1.merge(h2) do |_, v1, v2|
v1.merge(v2) { |_, v1, v2| v1.merge(v2) }
end
#⇒ {"1"=>{"ab"=>{"a"=>1, "b"=>4, "v"=>1},
# "bc"=>{"b"=>2, "c"=>1, "v"=>2}}}
Also note that what you have posted as an input are not valid ruby objects.
Try Hash#deep_merge from ActiveSupport. https://apidock.com/rails/Hash/deep_merge
If you don't want to depend on active_support gem or not willing to monkey-patch core classes, you can just copy the algorithm from the AS and adapt to your needs.
# File activesupport/lib/active_support/core_ext/hash/deep_merge.rb, line 21
def deep_merge!(other_hash, &block)
other_hash.each_pair do |current_key, other_value|
this_value = self[current_key]
self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
this_value.deep_merge(other_value, &block)
else
if block_given? && key?(current_key)
block.call(current_key, this_value, other_value)
else
other_value
end
end
end
self
end
UPDATE:
I'm not sure why the answer was downvoted. Here is deep_merge in action:
[10] pry(main)> a = {'1'=>
[10] pry(main)* {'ab'=>{'a'=>1, 'b'=>4},
[10] pry(main)* 'bc'=>{'b'=>2, 'c'=>1}}
[10] pry(main)* };
[11] pry(main)>
[12] pry(main)> b = {'1'=>
[12] pry(main)* {'ab'=>{'v'=>1},
[12] pry(main)* 'bc'=>{'v'=>2}}
[12] pry(main)* };
[13] pry(main)>
[14] pry(main)> a.deep_merge(b)
=> {"1"=>{"ab"=>{"a"=>1, "b"=>4, "v"=>1}, "bc"=>{"b"=>2, "c"=>1, "v"=>2}}}
[15] pry(main)>
Exactly what OP needs.

Stack overflow while serializing user-defined Ruby objects to Riak

While working through http://docs.basho.com/riak/latest/dev/taste-of-riak/querying-ruby
I noticed what I can't store Time object in riak for some reason.
[3] pry(main)> client = Riak::Client.new(:protocol => "pbc", :pb_port => 8087, :host => "192.168.145.34")
=> #<Riak::Client [#<Node 192.168.145.34:8098:8087>]>
[4] pry(main)>
[5] pry(main)>
[6] pry(main)> tt = client.bucket('test')
=> #<Riak::Bucket {test}>
[7] pry(main)> v = tt.new("ttt")
=> #<Riak::RObject {test,ttt} [#<Riak::RContent [application/json]:nil>]>
[8] pry(main)> v.data = 1
=> 1
[9] pry(main)> v.store
=> #<Riak::RObject {test,ttt} [#<Riak::RContent [application/json]:1>]>
[10] pry(main)> v.data = Time.now
=> 2014-06-22 11:34:01 +0400
[11] pry(main)> v.store
SystemStackError: stack level too deep
from /home/maus/.gem/ruby/1.9.1/gems/pry-0.10.0/lib/pry/pry_instance.rb:353
After that I discovered what it's the case for user defined classes too:
[16] pry(main)> class Tst
[16] pry(main)* def initialize(x)
[16] pry(main)* #x = x
[16] pry(main)* end
[16] pry(main)* end
=> nil
[17] pry(main)> t111 = Tst.new(111)
=> #<Tst:0x9b13f4c #x=111>
[18] pry(main)> v.data = t111
=> #<Tst:0x9b13f4c #x=111>
[19] pry(main)> v.store
SystemStackError: stack level too deep
from /home/maus/.gem/ruby/1.9.1/gems/pry-0.10.0/lib/pry/pry_instance.rb:353
[20] pry(main)>
[21] pry(main)> v.data = [1,2,3]
=> [1, 2, 3]
[22] pry(main)> v.store
=> #<Riak::RObject {test,test_key} [#<Riak::RContent [application/json]:[1, 2, 3]>]>
Looks like something is terribly wrong with my install. But how to investigate it? I'm using riak-1.3.2 and ruby 1.9.3p194 with the following versions of gems:
i18n-0.6.9
builder-3.2.2
beefcake-0.3.7
multi_json-1.10.1
innertube-1.0.2
riak-client-1.4.4.1
The only place in the store method code path were I see the value being use is in BeefcakeProtobuffsBackend.ObjectMethods::dump_object which instantiates an RpbContent with :value => maybe_encode(robject.raw_data), so this would appear to be an issue with serializing the custom data.
RContent.raw_data calls Serializers::serialize
def serialize(content_type, content)
serializer_for(content_type).dump(content)
end
Serialize only understands a very few content types:
Serializers['text/plain'] = TextPlain
Serializers['application/json'] = ApplicationJSON
Serializers['application/x-ruby-marshal'] = ::Marshal
The default for content_type is application/json, whose dump method is:
def dump(object)
object.to_json(Riak.json_options)
end
Check that v.raw_data returns the value you expect, and if not, try setting v.content_type='text/plain'
For your custom class, make sure it has a to_json or to_s method that returns a string, and select the appropriate content_type.

How to add/modify/delete a method in Pry?

I am trying Pry for main code REPL editor.
I discovered this is very close to what I was looking for, but I don't know well about how to use it. I want to know how to add/modify/remove each method (or any other members) to/from a class.
When I tried this,
➜ ~ pry
[1] pry(main)> class AAA
[1] pry(main)* def bbb
[1] pry(main)* "ccc"
[1] pry(main)* end
[1] pry(main)* end
=> nil
[2] pry(main)> cd AAA
[3] pry(AAA):1> ls
AAA#methods: bbb
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
[4] pry(AAA):1> def xxx
[4] pry(AAA):1* "yyy"
[4] pry(AAA):1* end
=> nil
[5] pry(AAA):1> def xxx
[5] pry(AAA):1* "zzz"
[5] pry(AAA):1* end
=> nil
[6] pry(AAA):1> cd ..
[7] pry(main)> Pry.WrappedModule(AAA).source
=> "class AAA\n def bbb\n \"ccc\"\n end\nend\ndef xxx\n \"yyy\"\nend\ndef xxx\n \"zzz\"\nend\n"
[8] pry(main)> AAA.new.xxx
=> "zzz"
[9] pry(main)>
It worked well as I expected. But The source code contains duplicated definition of xxx method. If I want to erase older one (or both), how can I do that? Also, if I want to remove existing method (or any other member) without exchanging to a new one, how can I do that?
P.S. I am doing this mainly to edit, store and restore class source code between memory and disk. (a kind of image based persistent)
I found the answer. Use undef command.
[11] pry(main)> cd AAA
[13] pry(AAA):1> ls
AAA#methods: bbb xxx
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
[14] pry(AAA):1> undef xxx
=> nil
[15] pry(AAA):1> ls
AAA#methods: bbb
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
[16] pry(AAA):1> cd ..
[17] pry(main)> Pry.WrappedModule(AAA).source
=> "class AAA\n def bbb\n \"ccc\"\n end\nend\n"
[18] pry(main)>

Does pry or irb allow you to work interactively from inside a block?

I know that pry allows you to "cd" into other scopes.
pry(main)> cd Object
pry(Object)> ls
constants:
ARGF Encoding GC NIL
ArgumentError EncodingError Gem NilClass
ARGV Enumerable Hash Noexec
Array Enumerator IndexError NoMemoryError
...
But is there any way to move the interactive session into a block thats passed to a method?
pry(main)> cd some_instance.some_method do |some_argument|
pry(block)> ls
some_argument some_other_vars_available_within_block
pry(block)> end
result_of_block
pry(main)>
If possible, this would be exceedingly helpful. I've tried getting binding.pry to work myself in this fashion, without some luck, but I wanted to make sure I'm going down the right path before I continue.
Not sure i understand you fully, but you can try this, for example:
(master ✘)✹✭ ᐅ pry
[1] pry(main)> def hello(&block)
| Object.new.instance_exec(&block)
| end
=> nil
[2] pry(main)> block_binding = nil
=> nil
[3] pry(main)> hello { |some_argument = "testing"| u = 20; block_binding = binding }
=> #<Binding:0x007fdd33b1f680>
[4] pry(main)> cd block_binding
[5] pry(#<Object>):1> ls
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_ _super block_binding some_argument u
[6] pry(#<Object>):1> some_argument
=> "testing"
[7] pry(#<Object>):1> u
=> 20
[8] pry(#<Object>):1> self
=> #<Object:0x007fdd33b1f888>
[9] pry(#<Object>):1>
Actually it turns out that nesting pry works beautifully. Kudos to the pry team.
Once I got my little binding.pry issue out of the way (my fault entirely).
Nesting pry: (note the 2 exit statements needed)
Hershwild:~ jstillwell$ pry
[1] pry(main)> binding.pry
[1] pry(main)> exit
=> nil
[2] pry(main)> exit
Hershwild:~ jstillwell$
Bringing forth pry from within a block:
Hershwild:~ jstillwell$ pry
[1] pry(main)> def test
[1] pry(main)* yield true
[1] pry(main)* end
=> nil
[2] pry(main)> test do |inside|
[2] pry(main)* binding.pry
[2] pry(main)* end
# after that end statement, pry resumes inside the block
[1] pry(main)> inside
=> true
[2] pry(main)> exit
=> nil
[3] pry(main)> exit
Hershwild:~ jstillwell$

Resources