generated from cvxgrp/simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* jinja2 experiment * testing dataclass * testing dataclass * moving jinja2 up * remove outdated test
- Loading branch information
Showing
12 changed files
with
246 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2023 Stanford University Convex Optimization Group | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2023 Stanford University Convex Optimization Group | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""DataAPI for {{ strategy }}""" | ||
# This class has been generated by cvxbson | ||
from dataclasses import dataclass, field | ||
from typing import Dict | ||
|
||
import polars as pl | ||
|
||
from cvx.bson.dataclass import Data | ||
|
||
@dataclass(frozen=True) | ||
class DataAPI(Data): | ||
# List of tables | ||
{% for name in names.keys() %} | ||
{{ name }}: pl.DataFrame | ||
{% endfor %} | ||
|
||
# Mapping from short name to full name | ||
tables: Dict[str, str] = field(default_factory=lambda: {{ names }}) |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2023 Stanford University Convex Optimization Group | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Create a dataclass from a template""" | ||
from pathlib import Path | ||
|
||
from jinja2 import Template | ||
|
||
|
||
def render(names, strategy): | ||
# gain this dictionary through reflection | ||
# every table has a shortname, e.g. prices and a longer descriptive name | ||
# we use the shortname to name the variable refering to the code | ||
# names = {"A": "AAA", "B": "BBB", "C": "CCC"} | ||
path = Path(__file__).parent | ||
|
||
# open the template | ||
template = Template(open(path / "data_api.py.jinja2", encoding="utf-8").read()) | ||
|
||
# we copy & paste the new class into a new file | ||
return template.render(names=names, strategy=strategy) |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from dataclasses import dataclass, field | ||
from typing import Dict | ||
|
||
import polars as pl | ||
|
||
from cvx.bson.dataclass import Data | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DataAPI(Data): | ||
# List of tables expected | ||
A: pl.DataFrame | ||
B: pl.DataFrame | ||
C: pl.DataFrame | ||
|
||
# Define a mutable value for the mapping from short name to full name | ||
tables: Dict[str, str] = field( | ||
default_factory=lambda: {"A": "AAA", "B": "BBB", "C": "CCC"} | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
# We add this as a little demo | ||
import numpy as np | ||
|
||
data = DataAPI( | ||
A=pl.DataFrame(np.random.rand(10, 5)), | ||
B=pl.DataFrame(np.random.rand(20, 3)), | ||
C=pl.DataFrame(np.random.rand(50, 5)), | ||
) | ||
|
||
# We can access the tables | ||
print(data.tables) | ||
print(data.A) | ||
print(data.B) | ||
print(data.C) | ||
|
||
# convert all data into one bson file | ||
print(data.to_bson("xxx.bson")) | ||
|
||
data2 = DataAPI.from_bson("xxx.bson") | ||
print(data2) | ||
|
||
def strategy(api: DataAPI): | ||
# the strategy has no idea how the data is stored | ||
print(api.A) | ||
|
||
strategy(data2) |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from cvx.data.render import render | ||
|
||
if __name__ == "__main__": | ||
# gain this dictionary through reflection | ||
# every table has a shortname, e.g. prices and a longer descriptive name | ||
# we use the shortname to name the variable refering to the code | ||
names = {"A": "AAA", "B": "BBB", "C": "CCC"} | ||
strategy = "s239" | ||
|
||
print(render(names, strategy)) | ||
|
||
# open the template | ||
# template = Template(open("creator.py.jinja2").read()) | ||
|
||
# we copy & paste the new class into a new file | ||
# print(template.render(names=names, strategy="s239")) |
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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