Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example code in python-client/README.md not working #19

Open
matzl opened this issue May 23, 2023 · 1 comment
Open

Example code in python-client/README.md not working #19

matzl opened this issue May 23, 2023 · 1 comment

Comments

@matzl
Copy link

matzl commented May 23, 2023

Please update the python example code in python-client/README.md - there are several issues with it (host wrong, discard_unknown_keys should be False or it will fail, it is not self-explaining what timestamps can be used, ...).
I was able to download data with this code:

import pandas as pd
from deutschland import smard
from pprint import pprint
from deutschland.smard.api import default_api


configuration = smard.Configuration(
    host="http://www.smard.de/app/chart_data",  # host/path given in example is wrong
    discard_unknown_keys=True  # suppress "deutschland.smard.exceptions.ApiTypeError: Invalid type for variable '1'.
    # Required value type is float and passed type was NoneType at ['received_data']['series'][140][1]"
)


def run():
    with smard.ApiClient(configuration) as api_client:
        api_instance = default_api.DefaultApi(api_client)
        smard_filter = 1223
        smard_region = "DE"
        smard_resolution = "quarterhour"

        try:
            # this will return all timestamps that can be used in smard_timestamp
            api_response = api_instance.filter_region_index_resolution_json_get(
                smard_filter, region=smard_region, resolution=smard_resolution)

            timestamp_df = pd.Series(api_response["timestamps"])
            last_timestamp_ms = timestamp_df.iloc[-1]
            last_datetime = pd.to_datetime(last_timestamp_ms, unit="ms")
            last_datetime = last_datetime.tz_localize('UTC')
            print("Latest json data starts at unixtimestamp %u ms (%s)"
                  % (last_timestamp_ms, last_datetime.tz_convert('Europe/Berlin')))

            smard_timestamp = int(last_timestamp_ms)
            api_response = api_instance.filter_region_filter_copy_region_copy_resolution_timestamp_json_get(
                smard_filter, smard_filter, smard_region, smard_timestamp,
                region=smard_region, resolution=smard_resolution)

            pprint(api_response)
        except smard.ApiException as e:
            print("SMART Api Exception filter_region_filter_copy_region_copy_resolution_timestamp_json_get: %s\n" % e)


if __name__ == "__main__":
    run()

@Vohwinkelh
Copy link

Thanks for the updated example. Works like i expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants