This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update model code with types, tests, and a better structure
- Loading branch information
1 parent
16b0bc5
commit 9166d95
Showing
20 changed files
with
400 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import PythonClass from "./PythonClass"; | ||
|
||
test("toString without decorators and superclasses", () => { | ||
const pythonClass = new PythonClass("Class") | ||
expect(pythonClass.toString()).toBe("class Class") | ||
}) | ||
|
||
test("toString with decorators and superclasses", () => { | ||
const pythonClass = new PythonClass("Class", ["deco1", "deco2"], ["super1", "super2"]) | ||
expect(pythonClass.toString()).toBe("@deco1 @deco2 class Class(super1, super2)") | ||
}) | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import PythonFromImport from "./PythonFromImport"; | ||
|
||
test("toString without alias", () => { | ||
const pythonFromImport = new PythonFromImport("module", "declaration") | ||
expect(pythonFromImport.toString()).toBe("from module import declaration") | ||
}) | ||
|
||
test("toString with alias", () => { | ||
const pythonFromImport = new PythonFromImport("module", "declaration", "d") | ||
expect(pythonFromImport.toString()).toBe("from module import declaration as d") | ||
}) | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default class PythonFromImport { | ||
|
||
readonly module: string; | ||
readonly declaration: string; | ||
readonly alias: Nullable<string> | ||
|
||
constructor(module: string, declaration: string, alias: Nullable<string> = null) { | ||
this.module = module; | ||
this.declaration = declaration; | ||
this.alias = alias; | ||
} | ||
|
||
toString() { | ||
if (this.alias === null) { | ||
return `from ${this.module} import ${this.declaration}` | ||
} else { | ||
return `from ${this.module} import ${this.declaration} as ${this.alias}` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import PythonFunction from "./PythonFunction"; | ||
import PythonParameter from "./PythonParameter"; | ||
|
||
test("toString without decorators and parameters", () => { | ||
const pythonFunction = new PythonFunction("function") | ||
expect(pythonFunction.toString()).toBe("def function()") | ||
}) | ||
|
||
test("toString with decorators and parameters", () => { | ||
const pythonFunction = new PythonFunction( | ||
"function", | ||
["deco1", "deco2"], | ||
[ | ||
new PythonParameter("param1"), | ||
new PythonParameter("param2") | ||
]) | ||
expect(pythonFunction.toString()).toBe("@deco1 @deco2 def function(param1, param2)") | ||
}) | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,47 @@ | ||
import PythonParameter from "./PythonParameter"; | ||
import PythonReturnType from "./PythonReturnType"; | ||
import PythonResult from "./PythonResult"; | ||
|
||
export default class PythonFunction { | ||
|
||
readonly name: string; | ||
readonly decorators: string[]; | ||
// TODO: Implementieren | ||
readonly parameters: PythonParameter[]; | ||
readonly hasReturnType: boolean; | ||
// TODO: Implementieren | ||
readonly returnType: PythonReturnType; | ||
readonly docstring: string; | ||
readonly results: PythonResult[]; | ||
readonly returnType: string; | ||
readonly summary: string; | ||
readonly description: string; | ||
readonly fullDocstring: string; | ||
|
||
constructor( | ||
name: string, | ||
decorators: string[], | ||
parameters: PythonParameter[], | ||
hasReturnType: boolean, | ||
returnType: PythonReturnType, | ||
docstring: string, | ||
summary: string, | ||
description: string, | ||
decorators: string[] = [], | ||
parameters: PythonParameter[] = [], | ||
results: PythonResult[] = [], | ||
returnType: string = "Any", | ||
summary: string = "", | ||
description: string = "", | ||
fullDocstring: string = "", | ||
) { | ||
this.name = name; | ||
this.decorators = decorators; | ||
this.parameters = parameters; | ||
this.hasReturnType = hasReturnType; | ||
this.results = results; | ||
this.returnType = returnType; | ||
this.docstring = docstring; | ||
this.summary = summary; | ||
this.description = description; | ||
this.fullDocstring = fullDocstring; | ||
} | ||
|
||
toString() { | ||
let result = "" | ||
|
||
if (this.decorators.length > 0) { | ||
result += this.decorators.map(it => `@${it}`).join(" ") | ||
result += " " | ||
} | ||
|
||
result += `def ${this.name}(${this.parameters.map(it => it.name).join(", ")})` | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import PythonImport from "./PythonImport"; | ||
|
||
test("toString without alias", () => { | ||
const pythonImport = new PythonImport("module") | ||
expect(pythonImport.toString()).toBe("import module") | ||
}) | ||
|
||
test("toString with alias", () => { | ||
const pythonImport = new PythonImport("module", "m") | ||
expect(pythonImport.toString()).toBe("import module as m") | ||
}) | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default class PythonImport { | ||
|
||
readonly module: string; | ||
readonly alias: Nullable<string> | ||
|
||
constructor(module: string, alias: Nullable<string> = null) { | ||
this.module = module; | ||
this.alias = alias; | ||
} | ||
|
||
toString() { | ||
if (this.alias === null) { | ||
return `import ${this.module}` | ||
} else { | ||
return `import ${this.module} as ${this.alias}` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import PythonModule from "./PythonModule"; | ||
|
||
test("toString", () => { | ||
const pythonModule = new PythonModule("module") | ||
expect(pythonModule.toString()).toBe(`Module "module"`) | ||
}) | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import PythonFunction from "./PythonFunction"; | ||
import PythonClass from "./PythonClass"; | ||
import PythonFromImport from "./PythonFromImport"; | ||
import PythonImport from "./PythonImport"; | ||
|
||
export default class PythonModule { | ||
|
||
readonly name: string; // "sklearn.base" | ||
readonly imports: string[]; // ["import copy", "import warnings"] | ||
readonly name: string; | ||
readonly imports: PythonImport[]; | ||
readonly fromImports: PythonFromImport[]; | ||
readonly classes: PythonClass[]; | ||
readonly functions: PythonFunction[]; | ||
|
||
constructor(name: string, imports: string[], classes: PythonClass[], functions: PythonFunction[]) { | ||
constructor( | ||
name: string, | ||
imports: PythonImport[] = [], | ||
fromImports: PythonFromImport[] = [], | ||
classes: PythonClass[] = [], | ||
functions: PythonFunction[] = [] | ||
) { | ||
this.name = name; | ||
this.imports = imports; | ||
this.fromImports = fromImports; | ||
this.classes = classes; | ||
this.functions = functions; | ||
} | ||
|
||
toString() { | ||
return `Module "${this.name}"` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import PythonPackage from "./PythonPackage"; | ||
|
||
test("toString", () => { | ||
const pythonPackage = new PythonPackage("package") | ||
expect(pythonPackage.toString()).toBe(`Package "package"`) | ||
}) | ||
|
||
export {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.