Skip to content

Commit

Permalink
Slight modification to tutorial code, one segment had ended up in the…
Browse files Browse the repository at this point in the history
… wrong block.
  • Loading branch information
DavidCEllis committed Jul 17, 2024
1 parent 12c641c commit 984989b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
16 changes: 9 additions & 7 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ print(report_generator(CodegenDemo).source_code)
Here we will make both a simple decorator based builder and then a subclass
based builder that can create `__slots__`.

### Decorator builder ###
### 4a: Decorator builder ###
```python
def reportclass(cls):
gatherer = fields_attribute_gatherer
Expand All @@ -177,28 +177,30 @@ def reportclass(cls):
flags = {"slotted": slotted}

return dtbuild.builder(cls, gatherer=gatherer, methods=methods, flags=flags)
```

# Step 4b: Define a base class builder
### 4b: Base class Builder ###
```python
# Once slots have been made, slot_gatherer should be used.
slot_gatherer = dtbuild.make_slot_gatherer(CustomField)
```

### Base class Builder ###
```python

class ReportClass(metaclass=dtbuild.SlotMakerMeta):
__slots__ = {}
_meta_gatherer = fields_attribute_gatherer

def __init_subclass__(cls):
slotted = '__slots__' in vars(cls) and isinstance(cls.__slots__, dtbuild.SlotFields)
gatherer = slot_gatherer if slotted else fields_attribute_gatherer
# Check if the metaclass has generated slots
meta_slotted = '__slots__' in vars(cls) and isinstance(cls.__slots__, dtbuild.SlotFields)
gatherer = slot_gatherer if meta_slotted else fields_attribute_gatherer
methods = {
dtbuild.eq_maker,
dtbuild.repr_maker,
dtbuild.init_maker,
report_maker
}

# The class may still have slots unrelated to code generation
slotted = "__slots__" in vars(cls)
flags = {"slotted": slotted}

Expand Down
6 changes: 4 additions & 2 deletions docs_code/tutorial_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ class ReportClass(metaclass=dtbuild.SlotMakerMeta):
_meta_gatherer = fields_attribute_gatherer

def __init_subclass__(cls):
slotted = '__slots__' in vars(cls) and isinstance(cls.__slots__, dtbuild.SlotFields)
gatherer = slot_gatherer if slotted else fields_attribute_gatherer
# Check if the metaclass has generated slots
meta_slotted = '__slots__' in vars(cls) and isinstance(cls.__slots__, dtbuild.SlotFields)
gatherer = slot_gatherer if meta_slotted else fields_attribute_gatherer
methods = {
dtbuild.eq_maker,
dtbuild.repr_maker,
dtbuild.init_maker,
report_maker
}

# The class may still have slots unrelated to code generation
slotted = "__slots__" in vars(cls)
flags = {"slotted": slotted}

Expand Down

0 comments on commit 984989b

Please sign in to comment.