dune-ocaml : No implementations provided for the following modules: [closed] - ocaml

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
I tried to compile an OCaml code with Dune but got the following error:
Error: No implementations provided for the following modules:
CallccBp referenced from bin/.CallccTest.eobjs/native/dune__exe__CallccTest.cmx
by executing the command : $ dune build
My project hierarchy is as follows:
callcc/
bin/
callccTest.ml
dune
[
(executable
(name callccTest)
(libraries CallccBp))
]
lib/
CallccBp.mli
dune
[
(library
(name CallccBp)
(modules_without_implementation CallccBp))
]
test/
callccTest.ml
dune [
(test
(name callccTest))
]
callcc.opam
dune-project
How can I solve this problem?

Looking at the discussion you had with octachron, let's start from the basics:
my_module.mli
File where you declare the signatures of your values, modules etc. Not mandatory, I'd advise not using them if you start with OCaml
my_module.ml
File where you implement your values, modules etc. Mandatory since these are the files that make your program run
Let's see this with a toy project:
.
├── bin
│   ├── dune
(executable
(name main)
)
│   └── main.ml
├── dune-project
├── lib
│   ├── dune
(library
(name my_module)
)
│   └── my_module.ml
└── program.opam
If I want to use values from my_module in bin/main.ml, I have to:
have values in my_module.ml
add the (libraries my_module) stanza in my bin/dune file
use these values with My_module.<name_of_value>
So this looks like:
.
├── bin
│   ├── dune
(executable
(name main)
(libraries my_module)
)
│   └── main.ml
let () =
let b = My_module.incr 3 in
Printf.printf "%d\n" b
├── dune-project
├── lib
│   ├── dune
│   └── my_module.ml
let incr a = a + 1
└── program.opam
Now, let's go back to your hierarchy:
callcc/
bin/
callccTest.ml
dune
[
(executable
(name callccTest)
(libraries CallccBp))
]
lib/
CallccBp.mli
dune
[
(library
(name CallccBp)
(modules_without_implementation CallccBp))
]
test/
callccTest.ml
dune [
(test
(name callccTest))
]
callcc.opam
dune-project
Everything looks fine except from the fact that CallccBp.mli is just an interface, not an implementation. As a starter you could remove this file, create CallccBp.ml filled with these two functions:
CallccBp.ml
let callcc = failwith "TODO"
let throw = failwith "TODO"
If you compile, dune should not complain and now all you'll have to do will be to provide a much useful implementation than failwith "TODO"
And if we go back to our toy project, to see why you'd want to have an mli file:
.
├── bin
│ ├── dune
(executable
(name main)
(libraries my_module)
)
│ └── main.ml
let () =
let b = My_module.incr 3 in
Printf.printf "%d\n" b
├── dune-project
├── lib
│ ├── dune
│ └── my_module.ml
let dummy _ = failwith "USELESS"
let incr a = a + 1
│ └── my_module.mli
val incr : int -> int
(** [incr d] will return [d] incremented by 1. Highly efficient. Trust me. *)
└── program.opam
I'll be able to use My_module.incr in bin/main.ml but not My_module.dummy because it is not shown by the mli file and thus not accessible outside of my_module.ml. And as a bonus, my_module.mli file is the entry point for a library user who doesn't want to know how it is implemented but just wants to use it knowing the available values, their types and, often, what they do from the comment.
The modules_without_implementation stanza is for mli files that don't need implementations, namely, type declarations files so modules looking like this:
AST.mli
type ('a, 'b) res = Ok of 'a | Error of 'b
type num = Int of int | Float of float
type person = {id : int; name : string; age : num}
And you can use them in another file like this:
file.ml
type t = {player1 : AST.person; player2 : AST.person}
let whos_older p1 p2 =
Printf.printf "%s is older\n"
(if p1.AST.age > p2.AST.age then p1.name else p2.name)
But that's not really useful when starting since, once again, I'd advise not touching mli files in the beginning

The error message is complaining that there is no implementation (aka no ml file) for the module CallccBp. Without knowing the content of CallccBp, it is probable that this module contain either an exception or an extension constructor declaration (or any other kind of runtime components). Your first fix should be to add a callccBp.ml file and remove the (module_without_implementation ...) line.

Related

How to properly server static files from a Flask server?

What is the proper way of serving static files (images, PDFs, Docs etc) from a flask server?
I have used the send_from_directory method before and it works fine. Here is my implementation:
#app.route('/public/assignments/<path:filename>')
def file(filename):
return send_from_directory("./public/assignments/", filename, as_attachment=True)
However if I have multiple different folders, it can get a bit hectic and repetitive because you are essentially writing the same code but for different file locations - meaning if I wanted to display files for a user instead of an assignment, I'd have to change it to /public/users/<path:filename> instead of /public/assignments/<path:filename>.
The way I thought of solving this is essentially making a /file/<path:filepath> route, where the filepath is the entire path to the destination folder + the file name and extension, instead of just the file name and extension. Then I did some formatting and separated the parent directory from the file itself and used that data when calling the send_from_directory function:
#app.route('/file/<path:filepath>', methods=["GET"])
def general_static_files(filepath):
filepath = filepath.split("/")
_dir = ""
for i, p in enumerate(filepath):
if i < len(filepath) - 1:
_dir = _dir + (p + "/")
return send_from_directory(("./" + _dir), filepath[len(filepath) - 1], as_attachment=True)
if we simulate the following request to this route:
curl http://127.0.0.1:5000/file/public/files/jobs/images/job_43_image_1.jpg
the _dir variable will hold the ./public/files/jobs/images/ value, and then filepath[len(filepath) - 1] holds the job_43_image_1.jpg value.
If i hit this route, I get a 404 - Not Found response, but all the code in the route body is being executed.
I suspect that the send_from_directory function is the reason why I'm getting a 404 - Not Found. However, I do have the image job_43_image_1.jpg stored inside the /public/files/jobs/images/ directory.
I'm afraid I don't see a lot I can do here except hope that someone has encountered the same issue/problem and found a way to fix it.
Here is the folder tree:
├── [2050] app.py
├── [2050] public
│   ├── [2050] etc
│   └── [2050] files
│   ├── [2050] jobs
│   │   ├── [2050] files
│   │   └── [2050] images
│   │   ├── [2050] job_41_image_decline_1.jpg
│   │   ├── [2050] job_41_image_decline_2554.jpg
│   │   ├── [2050] ...
│   ├── [2050] shop
│   └── [2050] videos
└── [2050] server_crash.log
Edit 1: I have set up the static_url_path. I have no reason to believe that that could be the cause of my problem.
Edit 2: Added tree
Pass these arguments when you initialise the app:
app = Flask(__name__, static_folder='public',
static_url_path='frontend_public' )
This would make the file public/blah.txt available at http://example.com/frontend_public/blah.txt.
static_folder sets the folder on the filesystem
static_url_path sets the path used within the URL
If neither of the variables are set, it defaults to 'static' for both.
Hopefully this is what you're asking.

Rcpp: cannot access module class inside R code of the same package

Let's say I want to build a very simple package in R that wraps c++ code.
My test project would be called bananas.
Let's say I have a folder called "bananas", where inside I have two other folders one called c++ and one called R.
Inside the c++ folder I have folder called include that contains the bananas.hpp header file (with the class definition):
#ifndef BANANAS_H
#define BANANAS_H
class Bananas
{
public:
void add_banana();
int get_bananas();
protected:
int number_of_bananas;
};
#endif
Outside include there is the bananas.cpp file that implements the methods of bananas.hpp:
#include "include/bananas.hpp"
using namespace std;
void Bananas::add_banana(){
// Return False if edge already existed
number_of_bananas ++;
}
int Bananas::get_bananas(){
return number_of_bananas;
}
now in my R folder I have a the wrapper.cpp file that uses Rcpp library to export c++ classes inside R as a module:
#include <Rcpp.h>
#include "include/bananas.hpp"
using namespace Rcpp;
RCPP_EXPOSED_CLASS(Bananas)
RCPP_MODULE(Bananas_cpp){
class_<Bananas>("BananasCPP")
.default_constructor()
.method("add_banana", &Bananas::add_banana)
.method("get_bananas", &Bananas::get_bananas)
;
}
Note that right now #include "include/bananas.hpp" does not mean anything, but in the future this file will be added inside source.
Finally I have my main concern which a R wrapper of this class under:
require(R6)
library(Rcpp)
# For exposing the error
print(ls(all.names = TRUE))
# Load Module
bcpp <- Module("Bananas_cpp", PACKAGE="RBananasC", mustStart = TRUE)$BananasCPP
Bananas <- R6Class("Bananas", list(
bn = new(bcpp),
print_bananas = function() {print(self$bn$get_bananas())},
banana_tree = function(d) {
for(row in 1:d){
self$bn$add_banana()
}
}
))
Now when running the setup.R file in my main directory which looks as follows:
require(Rcpp)
# Make a base package
unlink("RBananasC", recursive=TRUE)
Rcpp.package.skeleton(name = "RBananasC", list = character(),
path = ".", force = FALSE,
cpp_files = c("bananas/R/wrapper.cpp", "bananas/c++/bananas.cpp", "bananas/c++/include/bananas.hpp"))
dir.create("RBananasC/src/include")
file.rename("RBananasC/src/bananas.hpp", "RBananasC/src/include/bananas.hpp")
install.packages("RBananasC", repos=NULL, type="source")
# See that Bananas_cpp works
library(RBananasC) #Without this line it doesn't work
print(Module("Bananas_cpp", PACKAGE="RBananasC", mustStart = TRUE)$BananasCPP)
bcpp <- Module("Bananas_cpp", PACKAGE="RBananasC", mustStart = TRUE)$BananasCPP
ban <- new(bcpp)
ban$add_banana()
print(ban$get_bananas())
# Make the desired package
unlink("RBananas", recursive=TRUE)
Rcpp.package.skeleton(name = "RBananas", list = character(),
path = ".", force = FALSE,
code_files = c("bananas/R/bananas.R"), cpp_files = c("bananas/R/wrapper.cpp", "bananas/c++/bananas.cpp", "bananas/c++/include/bananas.hpp"))
dir.create("RBananas/src/include")
file.rename("RBananas/src/bananas.hpp", "RBananas/src/include/bananas.hpp")
install.packages("RBananas", repos=NULL, type="source")
I have a very strange behaviour inside my bananas.R file, that has to do with the fact that Bananas_cpp module of my package is not visible and so I cannot access the class BananasCPP upon installation.
On the other hand if I ignore the file bananas.R I can import the BananasCPP from the module Bananas_cpp of the package RBananasC, I created using Rcpp.package.skeleton.
To sum up the total file structure looks like:
.
├── bananas
│   ├── c++
│   │   ├── bananas.cpp
│   │   └── include
│   │   └── bananas.hpp
│   └── R
│   ├── bananas.R
│   └── wrapper.cpp
└── setup.R
And to demonstrate what is my problem you just run the setup.R.
I followed a standard tutorial, but I couldn't find any information of how I can load my BananasCPP class from the Bananas_cpp module inside my Bananas.R wrap function Bananas, while searching for days in the internet. This file does not appear in the namespace of the environment active inside the package, so I think this what should be tackled: "which commands I should add and where to expose my Bananas_cpp module inside the current namespace of the package".
Of course this is a reproducible that I made from a real problem I had.
Thanks a lot for your support!

Importing files from other directory in python?

I have following directory structure:
A
|
|--B--Hello.py
|
|--C--Message.py
Now if the path of root directory A is not fixed, how can i import "Hello.py" from B to "Message.py" in C.
At first I suggest to add empty __init__.py file into every directory with python sources. It will prevent many issues with imports because this is how the packages work in Python:
In your case it this should look like this:
A
├── B
│   ├── Hello.py
│   └── __init__.py
├── C
│   ├── Message.py
│   └── __init__.py
└── __init__.py
Let's say the Hello.py contains the function foo:
def foo():
return 'bar'
and the Message.py tries to use it:
from ..B.Hello import foo
print(foo())
The first way to make it work is to let the Python interpreter to do his job and to handle package constructing:
~ $ python -m A.C.Message
Another option is to add your Hello.py file into list of known sources with the following code:
# Message.py file
import sys, os
sys.path.insert(0, os.path.abspath('..'))
from B.Hello import foo
print(foo())
In this case you can execute it with
~/A/C $ python Message.py

How to run multiple Groovy unit tests

I want to keep my groovy source files in their own directory, with the tests being in a separate directory.
I have the directory structure as follows:
.
├── build
│   └── Messenger.class
├── build.xml
├── ivy.xml
├── lib
├── src
│   └── com
│   └── myapp
│   └── Messenger.groovy
└── test
└── unit
├── AnotherTest.groovy
└── MessengerTest.groovy
I can successfully run one test by using the groovy command and specifying the class path for the units under test using -cp to point to build/ but how do I run all the tests in the directory?
Tou can run all unit test with command:
grails test-app unit:
If you have unit, integration, functional... tests you can run all tests with command:
grails test-app
I am new to groovy, but I wrote my own test runner and put it in root directory of my project. Source code:
import groovy.util.GroovyTestSuite
import junit.textui.TestRunner
import junit.framework.TestResult
import static groovy.io.FileType.FILES
public class MyTestRunner {
public static ArrayList getTestFilesPaths(String test_dir) {
// gets list of absolute test file paths
ArrayList testFilesPaths = new ArrayList();
new File(test_dir).eachFileRecurse(FILES) {
if(it.name.endsWith(".groovy")) {
testFilesPaths.add(it.absolutePath)
}
}
return testFilesPaths;
}
public static GroovyTestSuite getTestSuite(ArrayList testFilesPaths) {
// creates test suite using absolute test file paths
GroovyTestSuite suite = new GroovyTestSuite();
testFilesPaths.each {
suite.addTestSuite(suite.compile(it));
}
return suite;
}
public static void runTests(GroovyTestSuite suite) {
// runs test in test suite
TestResult result = TestRunner.run(suite);
// if tests fail return exit code non equal to 0 indicating that
// tests fail it helps if one of your build step is to test files
if (!result.wasSuccessful()) {
System.exit(1);
}
}
}
ArrayList testFilesPaths = MyTestRunner.getTestFilesPaths("tests");
GroovyTestSuite suite = MyTestRunner.getTestSuite(testFilesPaths);
MyTestRunner.runTests(suite)
if you try to use this be aware that if it fails it is most likely that getTestFilesPaths is not working properly.
My directory structure
.
├── test_runner.groovy
├── src
│ └── ...
└── tests
└── Test1.groovy
└── someDir
├── Test2.groovy
└── Test3.groovy
How to run
From the same directory where test_runner.groovy is run:
groovy test_runner.groovy

Eunit error with multiple apps

I have the following directory structure:
myapp
├── apps
│   ├── myapp
│   ├── myotherapp
│   └── myapp_common
├── deps
│   ├── cowboy
......
I run eunit using rebar as follows in the main myapp directory:
./rebar skip_deps=true eunit
It correctly runs eunit for three apps in apps/. After that it tries to run eunit in the parent myapp directory and throws the following error:
......
==> myapp (eunit)
ERROR: eunit failed while processing /home/msheikh/myapp: {'EXIT',{{badmatch,{error,{1,
"cp: missing destination file operand after `.eunit'\nTry `cp --help' for more information.\n"}}},
[{rebar_file_utils,cp_r,2,[]},
{rebar_eunit,eunit,2,[]},
{rebar_core,run_modules,4,[]},
{rebar_core,execute,4,[]},
{rebar_core,process_dir,4,[]},
{rebar_core,process_commands,2,[]},
{rebar,main,1,[]},
{escript,run,2,[{file,"escript.erl"},{line,727}]}]}}
Question: How can I fix this or prevent eunit from running for the parent myapp directory?
The rebar.config file in the main myapp directory looks like this:
{lib_dirs, ["deps", "apps"]}.
{deps, [
{lager, ".*", {git, "https://github.com/basho/lager.git", {branch, "master"}}},
{jsx, ".*", {git, "git://github.com/talentdeficit/jsx.git", {tag, "v0.9.0"}}},
{cowboy, "", {git, "git://github.com/extend/cowboy.git", {branch, "master"}}},
....
]}.
{require_otp_vsn, "R15"}.
{escript_incl_apps, [getopt]}.
{erl_opts, [
debug_info,
warn_missing_spec,
{parse_transform, lager_transform}
]}.
{eunit_opts, [verbose]}.
{validate_app_modules, false}.
{sub_dirs, [
"apps/myapp/",
"apps/myotherapp/",
"apps/myapp_common/"]}.
I have the same project structure, and it works.
Are you sure you don't have src, test, ebin folders in the top-level directory?
If not, what happens if you mkdir .eunit? (I am not suggesting to keep this, but go looking for a solution from there).