I am trying to source code that exists on an online repository by:
Rcpp::sourceCpp(
url("https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp")
)
I'm encounter this issue:
Error in dirname(file) : a character vector argument expected
Just use R's download.file():
library(Rcpp)
remurl <- "https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp"
locfile <- "/tmp/mcmc.cpp"
download.file(url=remurl, destfile=locfile)
sourceCpp(locfile) # dozens of error for _this_ file
Edit Here is a better way with two important fixes:
You need a different URL. The one you list will download the html page. But you want raw source code, which in this case is https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp
You can create a simple helper function which takes the url, creates a tempfile with extension .cpp (hey, that argument once was my patch to base R ;-) and then returns that file name.
See below:
u2f <- function(url) {
tf <- tempfile()
download.file(url, tf, quiet=TRUE)
tf
}
library(Rcpp)
url <- "https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp"
sourceCpp( u2f( url ) )
and that compiles fine (albeit with warnings about signed/unsigned comparison).
Related
I am trying to use https://github.com/dominique-unruh/scala-isabelle for loading and parsing https://isabelle.in.tum.de/library/HOL/HOL-Imperative_HOL/Imperative_Quicksort.html - Imperative_Quicksort.thy. I am using scala-isabelle code from IntelliJ (path to source is C:\Workspace-IntelliJ\scala-isabelle):
val isabelleHome = "C:\\Homes\\Isabelle2020\\Isabelle2020"
// Differs from example in README: we skip building to make tests faster
val setup = Isabelle.Setup(isabelleHome = Path.of(isabelleHome), logic = "HOL", build=false)
implicit val isabelle: Isabelle = new Isabelle(setup)
// Load the Isabelle/HOL theory "Main" and create a context object
//val ctxt = Context("Main")
//val ctxt = Context("Imperative_Quicksort")
//val ctxt = Context("C:\\Homes\\Isabelle2020\\Isabelle2020\\src\\HOL\\Imperative_HOL\\ex\\Imperative_Quicksort")
val ctxt = Context("HOL.Imperative_HOL.ex.Imperative_Quicksort")
Such configuration give strange error message for loading some required theories, e.g.
Exception in thread "main" de.unruh.isabelle.control.IsabelleException: No such file: "/cygdrive/c/Workspace-IntelliJ/scala-isabelle/Old_Datatype.thy"
The error(s) above occurred for theory "HOL-Library.Old_Datatype" (line 10 of "/cygdrive/c/Workspace-IntelliJ/scala-isabelle/Countable.thy")
(required by "HOL.Imperative_HOL.ex.Imperative_Quicksort" via "HOL.Imperative_HOL.ex.Imperative_HOL" via "HOL.Imperative_HOL.ex.Array" via "HOL.Imperative_HOL.ex.Heap_Monad" via "HOL.Imperative_HOL.ex.Heap" via "HOL-Library.Countable")
at de.unruh.isabelle.control.Isabelle.de$unruh$isabelle$control$Isabelle$$parseIsabelle(Isabelle.scala:268)
My guess is - that theories that are imported using quotes give such error messages and I resolve such error messages one-by-one by copying the required theories in my C:\Workspace-IntelliJ\scala-isabelle. Not good, but I am trying to load this theory, so - if it works, it is fine.
At the end simpldata.ML was required but there are 5 simpdata.ML in Isabelle source (ZF / sequents / HOL/Tools / FOL / FOLP). I copied from FOL (because simpdata.ML was required by FOL.thy) but now I have error message:
Exception in thread "main" de.unruh.isabelle.control.IsabelleException: Failed to load theory "ZF.Bool" (unresolved "ZF.pair")
...
Undefined constant: "eq" (line 12 of "/cygdrive/c/Workspace-IntelliJ/scala-isabelle/simpdata.ML")completionline=12offset=313end_offset=315file=/cygdrive/c/Workspace-IntelliJ/scala-isabelle/simpdata.MLid=258:2:::IFOL.eq::constant:IFOL.eq::Pure.eq::constant:Pure.eq
At command "ML_file" (line 11 of "/cygdrive/c/Workspace-IntelliJ/scala-isabelle/pair.thy")
at de.unruh.isabelle.control.Isabelle.de$unruh$isabelle$control$Isabelle$$parseIsabelle(Isabelle.scala:268)
I tried to copy other simpldata.ML but they gave similar messages for different undefined constants. So - whats wrong with eq? My guess is that is is very basic function.
How to resolve this undefined eq? By some other import? But FOL/simpdata.ML does not have any imports and neither the lack of some source file is reported. How to proceed from here?
My intention was to load Imperative_Quicksort as the Context scala-isabelle varible, then I will try to reflect/digest the resulting class tree of Context and I will use graph neural networks to encode this class tree (I suppose that is represents the abstract syntax tree of Imperative_Quicksort theory).
I am aware that there is Isabelle mailing group but this is pretty technical question, so - it can/should be resolved here in SO.
Facts added 1
simpdata.ML is just include file which is included in FOL.thy with
ML_file \<open>simpdata.ML\<close>
So, simpdata.ML uses the imports and definitions of the enclosing FOL.thy file and it has eq definition indeed (and which is some 100 lines before its use in simpdata.ML include):
ML \<open>
structure Blast = Blast
(
structure Classical = Cla
val Trueprop_const = dest_Const \<^const>\<open>Trueprop\<close>
val equality_name = \<^const_name>\<open>eq\<close>
val not_name = \<^const_name>\<open>Not\<close>
val notE = #{thm notE}
val ccontr = #{thm ccontr}
val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
);
val blast_tac = Blast.blast_tac;
\<close>
So, maybe there are problems with some load order...
It appeared that 2 theory files (pair.thy and FOL.thy - that I have copied to my IntelliJ workspace) used their respective simpdata.ML includes from their respective directories. So - I have copied simpdata.ML for pair.thy as simpdata_pair.ML and modified include command in pair.thy as:
ML_file \<open>simpdata_pair.ML\<close>
This resolved my issue and allowed me to proceed with importing theory is this funny manner.
I have a project L which is published as a library.
Inside L, there is a resources folder and a snippet of code that access the content.
I created a second module in L to add a main as a test, and it works.
Now when I include L into my other project, it doesnt find the folder located inside the resources of L.
in all the below path is equal to folder_i_am_looking_for and I generate varieties with /$path, $path/, $path and /$path/. ( I am desperate at this point )
I tried:
getClass.getClassLoader.getResource(path).getPath,
getClass.getClassLoader.getResource(path).toExternalForm,
getClass.getResource(path).getPath,
getClass.getResource(path).toExternalForm,
as well as
val location: URL = this.getClass.getProtectionDomain.getCodeSource.getLocation
Paths
.get(location.toURI)
.resolve("../classes/" + path)
.normalize()
.toFile
.getAbsoluteFile
.toString
from https://stackoverflow.com/a/54142960
and for each generated path, I've tried to :
input ::
input.split('!').last ::
input.split("file:/").last ::
input.split("file:").last ::
Nil
from https://stackoverflow.com/a/5346020
each time , i get path that looks like :
/path/to/projectFolder/jar:file:/path/to/library/lib_2.11-version.jar!/folder
I let you imagine all the variety of different path I get with all the above operations.
None of them find the directory.
I am testing all path simultaneously with ZIO.validateFirstPar so if you have any idea, I can add more to test.
Thank you.
I am trying to read a CSV file into Scala. I can read fine using the absolute path, but would like to be able to use the relative path.
val filename = "sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
for (line <- Source.fromFile(filename).getLines()) { println(line) }
throws the error:
java.io.FileNotFoundException: sample_data\yahoo_finance\AAPL\AAPL_Historical.csv
(The system cannot find the path specified)
However:
val filename = "C:/Users/hansb/Desktop/Scala Project/src/main/" +
"resources/sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
for (line <- Source.fromFile(filename).getLines()) { println(line) }
works just fine.
My understanding was that scala.io.Source knew to look in the resources folder for the relative path.
What am I missing?
Working code using Phasmid's suggestion:
val relativePath = "/sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
val csv = getClass.getResource(relativePath)
for (line <- Source.fromURL(csv).getLines()){ println(line) }
This is one of the worst things about Java (and, thus, Scala). I imagine many millions of hours have been spent on this kind of problem.
If you want to get a resource from a relative path (i.e. from the class path) you need to treat the resource as a resource. So, something like the following:
getClass.getResource("AAPL_Historical.csv")
while yields a URL which can then convert into a Stream, or whatever. This form will expect to find the resource in the same (relative) folder as the class, but in the resources rather than scala directory.
If you want to put the resource into the top level of the resources folder, then use:
getClass.getResource("/AAPL_Historical.csv")
It may be that there is some other magic which works but I haven't found it.
While debugging it's useful to view the rendered HTML and JS templates through a "view source" menu item in a browser, but doing so forces one to use the UI of the browser.
Does Jinja2 (or Flask) provide a facility to save the last n rendered templates on the server? It would then be possible to use one's favorite editor to view the rendered files, along with using one's familiar font-locking and search facilities.
It's of course possible to implement such a facility by hand, but doing so smacks too much like peppering one's programs while debugging with print statements, an approach that doesn't scale. I'm seeking a better alternative.
I'd think the easiest thing to do would be to use the after_request hook.
from flask import g
#main.route('/')
def index():
models = Model.query.all()
g.template = 'index'
return render_template('index.html', models=models)
#main.after_request
def store_template(response):
if hasattr(g, 'template'):
with open('debug/{0}-{1}'.format(datetime.now(), g.template), 'w') as f:
f.write(response.data)
return response
Here are the docs.
http://flask.pocoo.org/snippets/53/
As far as only collecting the last n templates I'd likely setup a cron job to do that. Here is an example
import os
from datetime import datetime
def make_files(n):
text = '''
<html>
</html>
'''
for a in range(n):
with open('debug/index-{0}.html'.format(datetime.now()), 'w') as f:
f.write(text)
def get_files(dir):
return [file for file in os.listdir(dir) if file.endswith('.html')]
def delete_files(dir, files, amount_kept):
rev = files[::-1]
for file in rev[amount_kept:]:
loc = dir + '/' + file
os.remove(loc)
if __name__ == '__main__':
make_files(7)
files = get_files('debug')
print files
delete_files('debug', files, 5)
files = get_files('debug')
print files
EDIT
reversed order of files inside the delete function so it will keep the most recent files. Also unable to find a way of accessing the original template name to avoid hardcoding.
EDIT 2
Alright so updated it to show how you can use flask.g to pass the template name to the after_request function
docs http://flask.pocoo.org/docs/0.11/testing/#faking-resources
I use github3.py library in order to create and update Gist on github.com
I can access an existing gist and get all information except content of the file:
for g in gh.iter_gists():
if g.id == content[0]:
f = g.iter_files().next()
print g.files #1
print f.raw_url
old_content = f.content #Returns None
Any hint why does it return None in the last line?
It looks as though GitHub's API no longer returns the contents of the gist file as part of the response:
{u'size': 12, u'raw_url': u'https://gist.githubusercontent.com/testgh3/eb911d34ec732e4b7f24/raw/e041e7dc6236db21062c11b2929a72e656bbc40e/fileA.txt', u'type': u'text/plain', u'language': u'Text', u'filename': u'fileA.txt'}
This means that github3.py needs to add functionality to the GistFile class that you're trying to use here. (I've already created an issue for this. If you'd like to help do the work, I'm happy to help you with that and I'd love to merge a pull request with this functionality.)