Skip to content

Commit

Permalink
Add tests for dataclasses that use Slots and Keyword-Only options.
Browse files Browse the repository at this point in the history
  • Loading branch information
brocla committed Mar 28, 2024
1 parent 6e0467e commit 38023bd
Show file tree
Hide file tree
Showing 6 changed files with 2,023 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/example-dataclass-normalization/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"authors": [
"brocla"
],
"files": {
"solution": [
"example_dataclass_normalization.py"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
""" Dataclasses:
Example code, from three exercises,
that demonstrates the new `slots` and
the `keyword-only` features of dataclasses.
The slots option is invoked as a parameters to the dataclass decorator.
Key-words are demonstrated 3 ways:
*** as a parameters to @dataclass,
*** as a parameters to field(),
*** as a sentinel value
"""
from dataclasses import dataclass, field, KW_ONLY



"""
From the `sgf_parsing` exercise
Demonstrates the combined use of slots and kw_only as parameters to @dataclass
"""
@dataclass(frozen=True, slots=True, kw_only=True) # *** ***
class SgfTree:
properties: dict = field(default_factory=dict)
children: list = field(default_factory=list)

def _parse(input):
properties = input.read_properties()
children = []
while input.expect(";"):
children.append(SgfTree(properties=input.read_properties()))
while input.expect("(", advance=False):
children.append(_parse(input))
return SgfTree(properties=properties, children=children)




"""
From `go_counting` exercise.
Demonstrates the use of slots.
This exercise also demonstrates uninitialized variables.
See x and y.
"""
@dataclass(unsafe_hash=True, slots=True)
class Point:
x: int
y: int

def __add__(self, o):
return Point(self.x + o.x, self.y + o.y)

def __eq__(self, o):
if isinstance(o, Point):
return self.x == o.x and self.y == o.y
return self.x == o[0] and self.y == o[1]



"""
From the `tree_building` exercise.
The Records() class demonstrates slots and kw_only as a field() parameter.
The Node() class demonstrates use of KW_ONLY sentinel.
Also demonstrates a mix of initialized and uninitialized variables.
See record_id and parent_id.
"""
@dataclass(slots=True) #***
class Record:
record_id: int
parent_id: int = field(kw_only=True) #***

def __lt__(self, other):
return self.record_id < other.record_id

@dataclass
class Node:
node_id: int
_: KW_ONLY #***
children: list = field(default_factory=list)

def BuildTree(records):
records.sort()

# initialize one node for every record.
trees = []
for record in records:
trees.append(Node(record.record_id))

# match children to parents
for record in records[1:]:
parent = trees[record.parent_id]
child = trees[record.record_id]
parent.children.append(child)

return trees[0]
19 changes: 19 additions & 0 deletions test/example-dataclass-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"placeholder_0": "SgfTree",
"placeholder_1": "properties",
"placeholder_2": "default_factory",
"placeholder_3": "children",
"placeholder_4": "frozen",
"placeholder_5": "slots",
"placeholder_6": "kw_only",
"placeholder_7": "_parse",
"placeholder_8": "advance",
"placeholder_9": "Node",
"placeholder_10": "node_id",
"placeholder_11": "BuildTree",
"placeholder_12": "records",
"placeholder_13": "trees",
"placeholder_14": "record",
"placeholder_15": "parent",
"placeholder_16": "child"
}
3 changes: 3 additions & 0 deletions test/example-dataclass-normalization/representation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": 2
}
Loading

0 comments on commit 38023bd

Please sign in to comment.