Skip to content

Commit

Permalink
Merge pull request #79 from CADET/dt/doc-improvements
Browse files Browse the repository at this point in the history
Improve docs
  • Loading branch information
daniel-thom authored and GitHub Enterprise committed Apr 3, 2024
2 parents 39c5e20 + f711064 commit be8dc56
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/explanation/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,37 @@ may not be appropriate for all classes. The `Location` class in this package is
cases like that developers can define their own name field and set its default value to `""`.

Refer to the [Components API](#components-api) for more information.

## Inheritance
Recommended rule: A `Component` that has subclasses should never be directly instantiated.

Consider a scenario where a developer defines a `Load` class and then later decides a new load is
needed because of one custom field.

The temptation may be to create `CustomLoad(Load)`. This is very problematic in the design of
the infrasys API. There will be no way to retrieve only `Load` instances. Consider this example:

```python
for load in system.get_components(Load)
print(load.name)
```

This will retrieve both `Load` and `CustomLoad` instances.

Instead, our recommendation is to create a base class with the common fields.

```python
class LoadBase(Component)
"""Defines common fields for all Loads."""

common_field1: float
common_field2: float

class Load(LoadBase):
"""A load component"""

class CustomLoad(LoadBase):
"""A custom load component"""

custom_field: float
```
1 change: 1 addition & 0 deletions docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
:caption: Contents:
api/index
system_json
```
51 changes: 51 additions & 0 deletions docs/reference/system_json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## Viewing System Data in JSON Format

System data can be serialized to JSON files with `system.to_json("system.json")`.

It can be useful to view and filter the system data in this format. There
are many tools available to browse JSON data.

Here is an example [GUI tool](http://jsonviewer.stack.hu) that is available
online in a browser.

The command line utility [jq](https://stedolan.github.io/jq/) offers even more
features. The rest of this page provides example commands.

Some of the examples assume a UNIX operating system (our apologies to Windows users).

- View the entire file pretty-printed

```
$ jq . system.json
```

- View the System component types

```
$ jq -r '.components | .[] | .__metadata__.fields.type' system.json | sort | uniq
```

- View specific components

```
$ jq '.components | .[] | select(.__metadata__.fields.type == "Bus")' system.json
```

- Get the count of a component type

```
# There is almost certainly a better way.
$ jq '.components | .[] | select(.__metadata__.fields.type == "Bus")' system.json | grep -c Bus
```

- View specific component by name

```
$ jq '.components | .[] | select(.__metadata__.fields.type == "Bus" and .name == "bus1")' system.json
```

- Filter on a field value

```
$ jq '.components | .[] | select(.__metadata__.fields.type == "Bus" and .voltage > 1.1)' system.json
```

0 comments on commit be8dc56

Please sign in to comment.