Skip to content

Commit

Permalink
Changed to well-formed package
Browse files Browse the repository at this point in the history
  • Loading branch information
bbollen23 committed Jan 11, 2025
1 parent b8a2918 commit 4a26d26
Show file tree
Hide file tree
Showing 21 changed files with 7,071 additions and 536 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dist
__pycache__
.ipynb_checkpoints

src/revisit_notebook_widget/static
src/revisit/static
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ yarn run dev
Open `example.ipynb` in JupyterLab, VS Code, or your favorite editor
to start developing. Changes made in `js/` will be reflected
in the notebook.


# CODE GEN

```bash
datamodel-codegen --input StudyConfigSchema.json --output models.py --custom-template-dir custom_templates --output-model-type pydantic_v2.BaseModel --additional-imports typing.TypedDict --input-file-type jsonschema --special-field-name-prefix we_are_going_to_replace_this && sed -i '' 's/we_are_going_to_replace_this_//g' src/revisit/models.py
```
1,935 changes: 1,935 additions & 0 deletions StudyConfigSchema.json

Large diffs are not rendered by default.

1,770 changes: 1,770 additions & 0 deletions config.json

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions custom_templates/pydantic_v2/BaseModel.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
warnings.filterwarnings("ignore", module="pydantic")
{% for decorator in decorators -%}
{{ decorator }}
{% endfor -%}
class {{ class_name }}({{ base_class }}):{% if comment is defined %} # {{ comment }}{% endif %}
{%- if description %}
"""
{{ description | indent(4) }}
"""
{%- endif %}
{%- if not fields and not description %}
pass
{%- endif %}
{%- if config %}
{%- filter indent(4) %}
{% include 'ConfigDict.jinja2' %}
{%- endfilter %}
{%- endif %}
{%- for field in fields -%}
{%- if not field.annotated and field.field %}
{%- if not field.required %}
{{ field.name }}: {{ field.type_hint }} = {{ field.field }}
{%- else %}
{{ field.name }}: {{ field.type_hint }} = {{ field.field }}
{%- endif %}
{%- else %}
{%- if field.annotated %}
{{ field.name }}: {{ field.annotated }}
{%- else %}
{{ field.name }}: {{ field.type_hint }}
{%- endif %}
{%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none)) or field.data_type.is_optional
%} = {{ field.represented_default }}
{%- endif -%}
{%- endif %}
{%- if field.docstring %}
"""
{{ field.docstring | indent(4) }}
"""
{%- endif %}
{%- for method in methods -%}
{{ method }}
{%- endfor -%}
{%- endfor -%}

# End of class

class {{ class_name }}Type(TypedDict):{% if comment is defined %} # {{ comment }}{% endif %}
{%- if description %}
"""
{{ description | indent(4) }}
"""
{%- endif %}
{%- if not fields and not description %}
pass
{%- endif %}
{%- if config %}
{%- filter indent(4) %}
{% include 'ConfigDict.jinja2' %}
{%- endfilter %}
{%- endif %}
{%- for field in fields -%}
{%- if not field.annotated and field.field %}
{%- if not field.required %}
{{ field.name }}: {{ field.type_hint }} = {{ field.field }}
{%- else %}
{{ field.name }}: {{ field.type_hint }} = {{ field.field }}
{%- endif %}
{%- else %}
{%- if field.annotated %}
{{ field.name }}: {{ field.annotated }}
{%- else %}
{{ field.name }}: {{ field.type_hint }}
{%- endif %}
{%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none)) or field.data_type.is_optional
%} = {{ field.represented_default }}
{%- endif -%}
{%- endif %}
{%- if field.docstring %}
"""
{{ field.docstring | indent(4) }}
"""
{%- endif %}
{%- for method in methods -%}
{{ method }}
{%- endfor -%}
{%- endfor -%}
5 changes: 5 additions & 0 deletions custom_templates/pydantic_v2/ConfigDict.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
model_config = ConfigDict(
{%- for field_name, value in config.dict(exclude_unset=True).items() %}
{{ field_name }}={{ value }},
{%- endfor %}
populate_by_name=True)
81 changes: 81 additions & 0 deletions custom_templates/pydantic_v2/RootModel.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{%- macro get_type_hint(_fields) -%}
{%- if _fields -%}
{#There will only ever be a single field for RootModel#}
{{- _fields[0].type_hint}}
{%- endif -%}
{%- endmacro -%}


{% for decorator in decorators -%}
{{ decorator }}
{% endfor -%}

class {{ class_name }}({{ base_class }}{%- if fields -%}[{{get_type_hint(fields)}}]{%- endif -%}):{% if comment is defined %} # {{ comment }}{% endif %}
{%- if description %}
"""
{{ description | indent(4) }}
"""
{%- endif %}
{%- if config %}
{%- filter indent(4) %}
{% include 'ConfigDict.jinja2' %}
{%- endfilter %}
{%- endif %}
{%- if not fields and not description %}
pass
{%- else %}
{%- set field = fields[0] %}
{%- if not field.annotated and field.field %}
root: {{ field.type_hint }} = {{ field.field }}
{%- else %}
{%- if field.annotated %}
root: {{ field.annotated }}
{%- else %}
root: {{ field.type_hint }}
{%- endif %}
{%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none))
%} = {{ field.represented_default }}
{%- endif -%}
{%- endif %}
{%- if field.docstring %}
"""
{{ field.docstring | indent(4) }}
"""
{%- endif %}
{%- endif %}

# End of class

class {{ class_name }}Type(TypedDict):{% if comment is defined %} # {{ comment }}{% endif %}
{%- if description %}
"""
{{ description | indent(4) }}
"""
{%- endif %}
{%- if config %}
{%- filter indent(4) %}
{% include 'ConfigDict.jinja2' %}
{%- endfilter %}
{%- endif %}
{%- if not fields and not description %}
pass
{%- else %}
{%- set field = fields[0] %}
{%- if not field.annotated and field.field %}
root: {{ field.type_hint }} = {{ field.field }}
{%- else %}
{%- if field.annotated %}
root: {{ field.annotated }}
{%- else %}
root: {{ field.type_hint }}
{%- endif %}
{%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none))
%} = {{ field.represented_default }}
{%- endif -%}
{%- endif %}
{%- if field.docstring %}
"""
{{ field.docstring | indent(4) }}
"""
{%- endif %}
{%- endif %}
Loading

0 comments on commit 4a26d26

Please sign in to comment.