Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Update model code with types, tests, and a better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Jun 18, 2021
1 parent 16b0bc5 commit 9166d95
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 81 deletions.
2 changes: 1 addition & 1 deletion client/src/Components/Tree/ModuleNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ModuleNode = ({

/** This is the Name of this module without its packages name prefixed. */

const [_first, ...moduleName] = pythonModule.name.split(".");
const [, ...moduleName] = pythonModule.name.split(".");

const path = parentPath.concat(moduleName)
const [childVisible, setChildVisibility] = useState(false);
Expand Down
4 changes: 2 additions & 2 deletions client/src/Components/TreeView/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import Tree from "../Tree/Tree";
import './tree-view.css';
import packageJson from "../../data/sklearn.json";
import PythonPackageBuilder from "../../model/PythonPackageBuilder";
import {parsePythonPackageJson, PythonPackageJson} from "../../model/PythonPackageBuilder";
import PythonFunction from "../../model/PythonFunction";

type TreeViewProps = {
Expand All @@ -13,7 +13,7 @@ type TreeViewProps = {
}

const TreeView = ({setParameters, selection, setSelection, setSelectedFunction}: TreeViewProps) => {
let pythonPackage = PythonPackageBuilder.make(packageJson);
let pythonPackage = parsePythonPackageJson(packageJson as PythonPackageJson);
return (
<div className="tree-view">
<h2 className="package-name">{pythonPackage.name}</h2>
Expand Down
13 changes: 13 additions & 0 deletions client/src/model/PythonClass.test.ts
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 {}
38 changes: 33 additions & 5 deletions client/src/model/PythonClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,44 @@ export default class PythonClass {

readonly name: string;
readonly decorators: string[];
// TODO Superclasses zum Typ string machen
readonly superclasses: PythonClass[];
readonly docstring: string;
readonly superclasses: string[];
readonly methods: PythonFunction[];
readonly summary: string;
readonly description: string;
readonly fullDocstring: string;

constructor(name: string, decorators: string[], superclasses: PythonClass[], docstring: string, methods: PythonFunction[]) {
constructor(
name: string,
decorators: string[] = [],
superclasses: string[] = [],
methods: PythonFunction[] = [],
summary: string = "",
description: string = "",
fullDocstring: string = "",
) {
this.name = name;
this.decorators = decorators;
this.superclasses = superclasses;
this.docstring = docstring;
this.methods = methods;
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 += `class ${this.name}`

if (this.superclasses.length > 0) {
result += `(${this.superclasses.join(", ")})`
}

return result
}
}
13 changes: 13 additions & 0 deletions client/src/model/PythonFromImport.test.ts
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 {}
20 changes: 20 additions & 0 deletions client/src/model/PythonFromImport.ts
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}`
}
}
}
20 changes: 20 additions & 0 deletions client/src/model/PythonFunction.test.ts
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 {}
41 changes: 26 additions & 15 deletions client/src/model/PythonFunction.ts
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
}
}
13 changes: 13 additions & 0 deletions client/src/model/PythonImport.test.ts
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 {}
18 changes: 18 additions & 0 deletions client/src/model/PythonImport.ts
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}`
}
}
}
8 changes: 8 additions & 0 deletions client/src/model/PythonModule.test.ts
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 {}
20 changes: 17 additions & 3 deletions client/src/model/PythonModule.ts
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}"`
}
}
8 changes: 8 additions & 0 deletions client/src/model/PythonPackage.test.ts
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 {}
6 changes: 5 additions & 1 deletion client/src/model/PythonPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ export default class PythonPackage {
readonly name: string;
readonly modules: PythonModule[];

constructor(name: string, modules: PythonModule[]) {
constructor(name: string, modules: PythonModule[] = []) {
this.name = name;
this.modules = modules;
}

toString() {
return `Package "${this.name}"`
}
}
Loading

0 comments on commit 9166d95

Please sign in to comment.