Error occurred when loading a custom Julia module - module

I have been puzzled by how to define and use custom module in Julia.
For example, I defined a module named myMoldule to wrap a mutable struct Param and a function add in D:\\run\\defineModule.jl:
module myMoldule
export Param, add
mutable struct Param
x ::Int64
y ::Int64
end
function add(x::Int64, y::Int64)
sum ::Int64
sum = x + y
return sum
end
end
and used this module in D:\\run\\useModule.jl like:
include("D:\\run\\defineModule.jl")
using .myMoldule
function testModule()
param = Param(1, 2)
sum = add(param.x, param.y)
println(sum)
end
An error occurred when running testModule() as follows:
julia> testModule()
ERROR: UndefVarError: Param not defined
Stacktrace:
[1] testModule() at D:\run\useModule.jl:8
[2] top-level scope at none:1
Note that I used the absolute path in the include(...) to avoid using LOAD_PATH stuff, and added . before the module name (i.e., using .myMoldule).
What seems to be the problem?
P.S.: Julia version information:
julia> versioninfo()
Julia Version 1.5.2
Commit 539f3ce943 (2020-09-23 23:17 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-8700K CPU # 3.70GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
JULIA_DEPOT_PATH = C:\Users\f\.julia;C:\opt\JuliaPro-1.5.2-1\Julia-1.5.2\local\share\julia;C:\opt\JuliaPro-1.5.2-1\Julia-1.5.2\share\julia
JULIA_LOAD_PATH = #;#v#.#;#stdlib
JULIA_NUM_THREADS = 6
JULIA_PKG_SERVER = pkg.juliahub.com

Corrections to be made:
sum is a function in Base you should use a different name
no need to declare sum variable (and it should be named something like mysum)
Remove space before ::
Module names should start with a CapitalLetter
You have a typo in module name perhaps you are loading a different module than you think?
Once corrected your code works.

The issue happens because you are using .MyModule1, not in the MyModule2, and thus you import Param to the Main module but not to the MyModule2, thus Module2 does not see Param.
If you will put using ..MyModule1 (two dots instead of one as you there is one more level) into MyModule2 this issue will go.
However, your code still will not work, since julia's include function just runs all the content of the included file, thus even you included the same file, you will create different instances of the modules. This warning WARNING: replacing module... indicates that somewhere in your code you might use the different version of the module (in you case, Main.Module1 and Main.Module2.Module1).
The common practice in julia is to include all the files in one place (they should be included only once). For instance, you can put all the includes in the file useModule.jl:
include("./defineModule1.jl")
include("./defineModule2.jl")
using .MyModule1
using .MyModule2
function testModule()
param = Param(1, 2)
# call myAdd to get the sum of param.x and param.y
sumValue = myAdd(param)
println(sumValue)
# call mySubtract to get the difference of param.x and param.y
difValue = mySubtract(param)
println(difValue)
end
Do not include files in other places. e.g.
defineModule2.jl content:
module MyModule2
using ..MyModule1
export myAdd, mySubtract
function myAdd(param::Param)
return param.x + param.y
end
function mySubtract(param::Param)
return param.x - param.y
end
end # end of module

Questioner's note: The following new question voted downwards (perhaps) was extended form the original one and was well answered by Vitaliy Yakovchuk.
I fixed all the issues pointed out by Przemyslaw Szufel. In my case above, it's not the improper ways of naming that cause the issue.
Now, I have a better exmaple to clarify my issue.
Suppose that, to meet the needs, I have to seperate my julia source code into two modules, e.g., define of a mutable struct Param in defineModule1.jl and define of functions in defineModule2.jl. The code scripts are as follows:
"D:\\run\\defineModule1.jl":
module MyModule1
export Param
mutable struct Param
x::Int64
y::Int64
end
end # end of module
"D:\\run\\defineModule2.jl":
include("D:\\run\\defineModule1.jl"); using .MyModule1
module MyModule2
export myAdd, mySubtract
function myAdd(param::Param)
return param.x + param.y
end
function mySubtract(param::Param)
return param.x - param.y
end
end # end of module
Note that Param is not defined here, and to make Param available, a line include("D:\\run\\defineModule1.jl"); using .MyModule1 is added as the first line of this file.
"D:\\run\\useModule.jl":
include("D:\\run\\defineModule1.jl"); using .MyModule1
include("D:\\run\\defineModule2.jl"); using .MyModule2
function testModule()
param = Param(1, 2)
# call myAdd to get the sum of param.x and param.y
sumValue = myAdd(param)
println(sumValue)
# call mySubtract to get the difference of param.x and param.y
difValue = mySubtract(param)
println(difValue)
end
Note that both function myAdd(param) and mySubtract(param) in the script defineModule2.jl need the predefined mutable struct Param in defineModule1.jl.
This is what I got when I run D:\\run\\useModule.jl:
julia> include("D:\\run\\useModule.jl")
WARNING: replacing module MyModule1.
WARNING: replacing module MyModule1.
WARNING: replacing module MyModule2.
ERROR: LoadError: LoadError: UndefVarError: Param not defined
Stacktrace:
[1] top-level scope at D:\run\defineModule2.jl:7
[2] include(::String) at .\client.jl:457
[3] top-level scope at D:\run\useModule.jl:2
[4] include(::String) at .\client.jl:457
[5] top-level scope at none:1
in expression starting at D:\run\defineModule2.jl:7
in expression starting at D:\run\useModule.jl:2
I believed that, by using the following lines in the beginning of "D:\run\useModule.jl", the mutable struct Param should have be found:
include("D:\\run\\defineModule1.jl"); using .MyModule1
include("D:\\run\\defineModule2.jl"); using .MyModule2
...
Still, error LoadError: UndefVarError: Param not defined is reported.
So, Why can't Param be found by D:\\run\\useModule.jl?

Related

readNetFromDarknet assertion error separator_index < line.size()

I am failing to use readNetFromDarknet function for the reasons I do not understand. I am trying to use yolo3-spp with this configuration file https://github.com/pjreddie/darknet/blob/master/cfg/yolov3-spp.cfg.
import cv2
net = cv2.dnn.readNetFromDarknet("../models/yolov3-spp.weights", "../models/yolov3-spp.cfg")
However, this gives me the following error:
error: OpenCV(4.5.4) /tmp/pip-req-build-3129w7z7/opencv/modules/dnn/src/darknet/darknet_io.cpp:660: error: (-215:Assertion failed) separator_index < line.size() in function 'ReadDarknetFromCfgStream
Interestingly, if I use readNet instead of readNetFromDarknet, it seems to work just fine. Should I stick to using readNet instead and why readNetFromDarknet is not actually working?
Your problem in order function arguments. readNetFromDarknet waits first config file and second - weights. A readNet function can swap arguments if they have a wrong order.
So right code:
net = cv2.dnn.readNetFromDarknet("../models/yolov3-spp.cfg", "../models/yolov3-spp.weights")

how to call function inside function and registering it in spark, Got error as PicklingError see SPARK-5063

i'm trying to create a udf which contains other function, then i'm registering the function in spark to use that in select query. I need to pass the df(temp table) values as input to the function which is registered.
When tried executing the function getting error as follows
Error: PicklingError: Could not serialize object: Exception: It appears that you are attempting to reference SparkContext from a broadcast variable, action, or transformation. SparkContext can only be used on the driver, not in code that it run on workers. For more information, see SPARK-5063
def funcmain(a,b,c,lvl,x):
lvl1=lvl
x1=x
while lvl1!=lv1:
x1=x
if x1!= something:
x1= something
func1(a,b,c)
return x1
udf_fun = spark.register.udf("funcmain",funcmain)

chef ruby syntax error while converting hash to yaml

I'm using the mongodb3 cookbook (took from chef supermarket) in my environment. When ever we pass input for mongos template like [10.10.0.10,127.0.0.1] , the output file is created with improper syntax:
PFB out put -
net:
port: 27017
bindIp: ! '[10.10.0.10,127.0.0.1]'
There's a ! and '' around the square brackets instead of only [10.10.0.10,127.0.0.1]
I am using below function to convert.
module Mongodb3Helper
def mongodb_config(config)
config.to_hash.compact.to_yaml
end
end
class Hash
def compact
inject({}) do |new_hash, (k, v)|
if v.is_a?(Hash)
v = v.compact
new_hash[k] = v unless v.empty?
else
new_hash[k] = v unless v.nil?
end
new_hash
end
end
end
Your code, while horribly unmaintainable due to making changes to global objects, is fine. As pointed out several times in the comments, the problem is with the data you are feeding in to that code.

error : can't read .... no such variable in TCL script using "after"

I am working in a TCL automation environent at my workplace.
I'm trying to run a delayed command in my script using "after".
The problem i encounter is that when i try to specify commands with variables inside a code block under an "after" , the vars are not recognized and i get an error message.
I'll quote the relevant parts of the code.
proc test_script1 {{bh0 0} ...more vars....} {
.
.
.
after [expr 20000] {
set some_value[ ns1::probe_TCP_connections $bh0 $main_duration 1 15] }
puts "the value i got is $some_value"
}
And i seem to get an error:
can't read "some_value": no such variable
Can anyone suggets what is the problem , and how to oversome it?
thx
You can capture the values of local variables with the help of apply (and it's advisable to use list to build callbacks when they get even slightly non-trivial). Since the body of an apply is a lambda expression with its own scope — a little nameless procedure — you have to use global in it to access state that will persist, and in any case after callbacks are called from the global namespace always (since the mechanism doesn't know how to keep arbitrary stack frames around for the duration of the asynchronous operation).
after 20000 [list apply {{bh0 main_duration} {
global some_value
set some_value [ns1::probe_TCP_connections $bh0 $main_duration 1 15]
}} $bh0 $main_duration]
global some_value
vwait some_value
puts "The value was changed to $some_value"
It's possible to get even more sophisticated, especially in Tcl 8.6 which has a coroutine system that can be used to hide the complexity of using continuation passing style programming.
Here is a proc that will do something similar to what you're trying to do:
proc foo {} {
# Make 'a' available to both the global scope and local scope
global a
# This is to check the current level
puts "Current level: [info level]"
# The command to be run in 500 ms, and I have added another check for the level
after 500 {puts "Current level: [info level]"; set a 100}
# Wait for further execution until the global variable 'a' changes
vwait a
# Prints a
puts "Value of \$a: $a"
}
The output of the above will be:
Current level: 1
# After 500 ms, the below will print
Current level: 0
Value of $a: 100
When you use after, the variable a is being created in the global scope, that is not accessible to the proc unless explicitly given access. One of the simplest ways is to first make sure that a within the proc is accessible both globally and locally, with global (level 0).

Fortran Module Version Fatal Error

I have a very scary error on Fortran 95:
Fatal Error: While reading module 'list5.mod' found module version 0, expected 6
I have included the main structure of my module. I highly doubt it has anything to do with the substance inside my module since it has given me error messages on those and I have been able to fix them which means it is able to go through my module fine it is just something small related to the way I use the module. Perhaps?
MODULE list5
IMPLICIT NONE
CONTAINS
----
END MODULE
The main program is structured something like this:
PROGRAM mainlist
USE list5
IMPLICIT NONE
!Variable Declaration
INTEGER:: opt
INTEGER, PARAMETER:: maxitems=50
INTEGER:: size=0
CHARACTER(20):: itemarray(50)
INTEGER:: quantityarray(50)
INTEGER:: totalquantity, i=0
REAL:: totalprice=0, pricearray(50)=0
CHARACTER(20), DIMENSION(:)::Item
CHARACTER(20):: ItemSought
LOGICAL:: Found
INTEGER:: Location
INTEGER:: NumItems=0, SmallestItem=0
!Select statement for the menu
DO
opt=choices()
SELECT CASE (opt)
CASE(1)
size=size+1
CALL getItemData(itemarray,pricearray,quantityarray)
CASE(2)
CALL getFileItems(size,itemarray,pricearray,quantityarray)
CASE(3)
CALL pickItemRandomly (size)
CASE(4)
CALL calcListTotals
(pricearray,quantityarray,totalprice,totalquantity)
CALL printList(size,itemarray,pricearray,quantityarray,totalprice, totalquantity)
CASE(5)
CALL sortByItem(itemarray, pricearray, quantityarray)
CASE(6)
CALL sortByPrice(itemarray, pricearray, quantityarray)
CASE(7)
CALL writeListtoFile(size,itemarray, pricearray, quantityarray)
CASE(8)
CALL search(itemarray, ItemSought, Found, Location)
CASE(9)
STOP
END SELECT
END DO
END PROGRAM
Any suggestions at all?? I really need to solve this so any help would be appreciated. Thanks so much!!
As Rook says, the issue is with compiler versions; somehow the .mod file from by compiling list5 the first time around was generated by an older compiler. Clear out all your .o and .mod files, and try again, first compiling list5.f90 (or whatever the file containing module list5 is) and then compiling the main program.