-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83ebda6
commit 69c14f0
Showing
6 changed files
with
83 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,105 +11,87 @@ | |
"text": [ | ||
"Setting default log level to \"WARN\".\n", | ||
"To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).\n", | ||
"23/07/03 18:53:46 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"+----+\n", | ||
"| age|\n", | ||
"+----+\n", | ||
"|null|\n", | ||
"|null|\n", | ||
"|null|\n", | ||
"+----+\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
" \r" | ||
"23/08/14 20:09:46 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from typing import Protocol, TypeVar\n", | ||
"from pyspark.sql import SparkSession\n", | ||
"from pyspark.sql.types import LongType, StringType\n", | ||
"from typedspark import Schema, DataSet, Column, PartialDataSet, transform_to_schema, create_empty_dataset\n", | ||
"from typedspark import (\n", | ||
" Schema,\n", | ||
" DataSet,\n", | ||
" Column,\n", | ||
" DataSetExtends,\n", | ||
" DataSetImplements,\n", | ||
" transform_to_schema,\n", | ||
" create_empty_dataset,\n", | ||
")\n", | ||
"\n", | ||
"\n", | ||
"class Person(Schema):\n", | ||
" name: Column[StringType]\n", | ||
" age: Column[LongType]\n", | ||
"\n", | ||
"\n", | ||
"class Job(Schema):\n", | ||
" role: Column[StringType]\n", | ||
"\n", | ||
"\n", | ||
"class Age(Schema, Protocol):\n", | ||
" type: Column[StringType]\n", | ||
" age: Column[LongType]\n", | ||
"\n", | ||
"def get_age(df: PartialDataSet[Age]) -> DataSet[Age]:\n", | ||
" return transform_to_schema(df, Age)\n", | ||
"\n", | ||
"spark = SparkSession.builder.getOrCreate()\n", | ||
"\n", | ||
"df = create_empty_dataset(spark, Person)\n", | ||
"get_age(df).show()" | ||
"person = create_empty_dataset(spark, Person)\n", | ||
"job = create_empty_dataset(spark, Job)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"+----+----+\n", | ||
"|name| age|\n", | ||
"+----+----+\n", | ||
"|null|null|\n", | ||
"|null|null|\n", | ||
"|null|null|\n", | ||
"+----+----+\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"outputs": [], | ||
"source": [ | ||
"T = TypeVar(\"T\", bound=Schema)\n", | ||
"def get_age(df: DataSetExtends[Age]) -> DataSet[Age]:\n", | ||
" return transform_to_schema(df, Age)\n", | ||
"\n", | ||
"def birthday(df: PartialDataSet[Age], schema: T) -> DataSet[T]:\n", | ||
" return transform_to_schema(\n", | ||
" df, \n", | ||
" schema, # type: ignore\n", | ||
" {Age.age: Age.age + 1}\n", | ||
" )\n", | ||
"\n", | ||
"res: DataSet[Person] = birthday(df, Person)\n", | ||
"res.show()" | ||
"get_age(person)\n", | ||
"try:\n", | ||
" get_age(job) # linting error: Job is not a subtype of Age\n", | ||
"except:\n", | ||
" pass" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"execution_count": 11, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'Base class for protocol classes.\\n\\nProtocol classes are defined as::\\n\\n class Proto(Protocol):\\n def meth(self) -> int:\\n ...\\n\\nSuch classes are primarily used with static type checkers that recognize\\nstructural subtyping (static duck-typing), for example::\\n\\n class C:\\n def meth(self) -> int:\\n return 0\\n\\n def func(x: Proto) -> int:\\n return x.meth()\\n\\n func(C()) # Passes static type check\\n\\nSee PEP 544 for details. Protocol classes decorated with\\[email protected]_checkable act as simple-minded runtime protocols that check\\nonly the presence of given attributes, ignoring their type signatures.\\nProtocol classes can be generic, they are defined as::\\n\\n class GenProto(Protocol[T]):\\n def meth(self) -> T:\\n ...'" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"outputs": [], | ||
"source": [ | ||
"Age.get_docstring()" | ||
"T = TypeVar(\"T\", bound=Schema)\n", | ||
"\n", | ||
"\n", | ||
"def birthday(\n", | ||
" df: DataSetImplements[Age, T],\n", | ||
") -> DataSet[T]:\n", | ||
" return transform_to_schema(\n", | ||
" df,\n", | ||
" df.typedspark_schema, # type: ignore\n", | ||
" {\n", | ||
" Age.age: Age.age + 1,\n", | ||
" },\n", | ||
" )\n", | ||
"\n", | ||
"\n", | ||
"res = birthday(person)\n", | ||
"try:\n", | ||
" res = birthday(job) # linting error: Job is not a subtype of Age\n", | ||
"except:\n", | ||
" pass" | ||
] | ||
}, | ||
{ | ||
|
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
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