将jupter笔记本代码转换为适当的python函数

2024-10-05 15:24:53 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我在Jupyter笔记本中创建了一些代码,从API中提取数据,然后将其写入Dynamodb。 它在Jupyter中运行良好,但在使用def将其转换为python函数时失败。 代码如下:

import boto3
import pyowm
import time
import json
import requests
from datetime import datetime, date, timedelta, timezone
import pandas as pd
from geopy.geocoders import Nominatim


client = boto3.client('ssm')
parameter = client.get_parameter(Name='OpenWeather_API_Key', WithDecryption=False)
ssmkey = parameter['Parameter']['Value']
api_key = print("\"%s\""% ssmkey )
owm = pyowm.OWM(api_key)
city = 'Berlin, DE'

geolocator = Nominatim(user_agent='aerieous@myserver.com')
location = geolocator.geocode(city)
lat = location.latitude
lon = location.longitude

x = (datetime.now() - timedelta(days=1))
d = x.isoformat(' ', 'seconds')

# convert time to epoch
p = '%Y-%m-%d %H:%M:%S'
dt = int(time.mktime(time.strptime(d, p)))

url = "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=%s&lon=%s&dt=%s&appid=%s&units=metric" % (
lat, lon, dt, api_key)
response = requests.get(url)
data_history = json.loads(response.text)

df_history2 = pd.json_normalize(data_history, record_path='hourly', meta=['lat', 'lon', 'timezone'],
                            errors='ignore')
df_history2['dt'] = pd.to_datetime(df_history2['dt'], unit='s').dt.strftime("%m/%d/%Y %H:%M:%S")
# replace the column header
df_history2 = df_history2.rename(columns={'dt': 'timestamp'})
df_history2['uuid'] = df_history2[['timestamp', 'timezone']].agg('-'.join, axis=1)
df_select_hist2 = df_history2[
['uuid', 'lat', 'lon', 'timezone', 'timestamp', 'temp', 'feels_like', 'humidity', 'pressure']]

# change column types to strings to be on safer side :)
df_select_hist2 = df_select_hist2.astype(str)

# Convert dataframe to list of dictionaries (JSON) that can be consumed by any no-sql database
content = df_select_hist2.to_dict('records')

# create a DynamoDB client
dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table("Dev_Weather")

for item in content:
   unique_id = item['uuid']
   location = item['timezone']
   timestamp = item['timestamp']
   lat = item['lat']
   lon = item['lon']
   temp = item['temp']
   feels_like = item['feels_like']
   humidity = item['humidity']
   pressure = item['pressure']

   table.put_item(
      Item={
        'pk_id': unique_id,
        'sk': timestamp,
        'gsi_1_pk': lat,
        'gsi_1_sk': lon,
        'gsi_2_pk': temp,
        'gsi_2_sk': feels_like,
        'humidity': humidity,
        'pressure': pressure,
        'timezone': location
    }
    )

我知道必须有一个函数调用和一些print(),但我不知道把它们放在哪里。谢谢

A


Tags: toimportdfdatetimetimedtlocationitem