Skip to content

Commit

Permalink
feat(Python and R bindings): Initial versions of bindings for Python …
Browse files Browse the repository at this point in the history
…and R
  • Loading branch information
nokome committed Jul 22, 2019
1 parent 0972799 commit 8266cf7
Show file tree
Hide file tree
Showing 13 changed files with 618 additions and 19 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
/dist
/node_modules
.DS_Store

### Generated files ###
types.ts
/.mypy_cache
*.out.*
4 changes: 1 addition & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ built
dist
node_modules
public

### Generated files ###
types.ts
.mypy_cache
59 changes: 59 additions & 0 deletions R/util.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Any <- function () {
self <- list()
class(self) <- "Any"
self
}

format.Any <- function (type) {
"Any()"
}

Array <- function (items) {
self <- list(items=items)
class(self) <- "Array"
self
}

format.Array <- function (type) {
paste0("Array(", paste(sapply(type$items, format), collapse=", "), ")")
}

Union <- function (...) {
self <- list(types=as.character(c(...)))
class(self) <- "Union"
self
}

format.Union <- function (type) {
paste0("Union(", paste(sapply(type$types, format), collapse=", "), ")")
}

isType <- function (value, type) {
if(class(type) == "Any") {
TRUE
} else if (class(type) == "character") {
type_obj <- get(type)
if (class(type_obj) %in% c('Any', 'Array', 'Union')) isType(value, type_obj)
else inherits(value, type)
} else if (class(type) == "Array") {
if(class(value) != "list") return(FALSE)
for(item in value) {
if(!isType(item, type$items)) return(FALSE)
}
TRUE
} else if(class(type) == "Union") {
inherits(value, type$types)
} else {
FALSE
}
}

assertType <- function (value, type) {
if(!isType(value, type)) stop(paste("value is type", class(value), "not expected type", format(type)), call. = FALSE)
value
}

setProp <- function (node, name, type, value) {
if(!isType(value, type)) stop(paste("value for", name, "is type", class(value), "not expected type", format(type)), call. = FALSE)
node[[name]] <- value
}
53 changes: 42 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"scripts": {
"lint": "prettier --write './**/*.{js,json,md,ts,yaml}' && eslint './**/*.{js,ts}' --fix",
"test": "gulp test",
"build": "npm run build:ts",
"build": "npm run build:jsonschema && npm run build:ts && npm run build:py",
"build:jsonschema": "ts-node src/schema.ts",
"build:jsonld": "gulp jsonld",
"build:ts": "ts-node src/typescript.ts",
"build:py": "ts-node src/python.ts",
"build:r": "ts-node src/r.ts",
"docs": "npm run docs:readme && npm run docs:build",
"docs:readme": "markdown-toc -i --maxdepth=4 README.md",
"docs:build": "ts-node scripts/docs && scripts/docs.sh",
Expand All @@ -35,6 +37,7 @@
"@stencila/encoda": "^0.59.1",
"@types/fs-extra": "^8.0.0",
"@types/js-yaml": "^3.12.1",
"@types/toposort": "^2.0.1",
"ajv": "^6.10.2",
"better-ajv-errors": "^0.6.4",
"fs-extra": "^8.1.0",
Expand All @@ -48,6 +51,7 @@
"object.fromentries": "^2.0.0",
"tempy": "^0.3.0",
"through2": "^3.0.1",
"toposort": "^2.0.2",
"ts-node": "^8.3.0",
"typescript": "^3.5.3",
"vscode-json-languageservice": "^3.2.0"
Expand Down Expand Up @@ -85,5 +89,6 @@
"prettier": "@stencila/dev-config/prettier-config.json",
"release": {
"extends": "@stencila/semantic-release-config"
}
},
"dependencies": {}
}
9 changes: 9 additions & 0 deletions python.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -e

export PYTHONPATH=${PYTHONPATH}:${PWD}

python3 tests/article.py

mypy tests/article.py
1 change: 1 addition & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import types
5 changes: 5 additions & 0 deletions r.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -e

Rscript tests/article.R
Loading

0 comments on commit 8266cf7

Please sign in to comment.