GitHub Copilot实战 Leetcode和Alpha Vantage API获取股票数据

GitHub Copilot实战 Leetcode和Alpha Vantage API获取股票数据

    正在检查是否收录...

GitHub Copilot 可以提升编码速度25%。

需要在 visual studio code 添加插件 GitHub Copilot


1. Base Python 创建数组和Person class

 # Create a list of 100 numbers. numbers = [i for i in range(100)] def get_even_numbers(numbers): """Return a list of even numbers from the given list.""" return [i for i in numbers if i % 2 == 0] even_numbers = get_even_numbers(numbers) odd_numbers = [i for i in numbers if i not in even_numbers] print("Even numbers: {}".format(even_numbers)) print("Odd numbers: {}".format(odd_numbers)) class Person: def __init__(self, name, age): self.name = name self.age = age # A list of 10 people. PEOPLE = [ Person("John", 32), Person("Jane", 28), Person("Bob", 30), Person("Alice", 24), Person("Jill", 26), Person("Jack", 30), Person("Jen", 28), Person("Bill", 32), Person("Barbara", 24), Person("Jeff", 30), ] 

2. Leetcode 转化罗马数字为阿拉伯数字

""" A program that converts a Roman numberal to an integer. """ ROMAN_NUMERAL = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000, } def roman_to_int(roman_numeral: str) -> int: """ Convert a Roman numeral to an integer. """ result = 0 for i, numeral in enumerate(roman_numeral): value = ROMAN_NUMERAL[numeral] if i + 1 < len(roman_numeral) and ROMAN_NUMERAL[roman_numeral[i + 1]] > value: result -= value else: result += value return result TEST_CASES = [ ("I", 1), ("II", 2), ("III", 3), ("IV", 4), ("V", 5), ("VI", 6), ("VII", 7), ("VIII", 8), ("IX", 9), ("X", 10), ("XI", 11), ("XII", 12), ("XIII", 13), ("XIV", 14), ("XV", 15), ("XVI", 16), ("XVII", 17), ("XVIII", 18), ("XIX", 19), ("XX", 20), ("XXI", 21) ] for test_input, expected in TEST_CASES: result = roman_to_int(test_input) assert result == expected, f"Expected {expected}, got {result}" print(result, result == expected) 

3. 获取股票数据Alpha Vantage API

https://www.alphavantage.co/documentation/
注册 api key
https://www.alphavantage.co/support/#api-key

""" Use Alpha Vantage API to get stock data and plot it in a graph. """ import requests import pandas as pd import matplotlib.pyplot as plt API_KEY = "RTART83NP1RD7G8X" STOCK_SYMBOL = "MSFT" def get_daily_stock_data(symbol: str): """ Get daily stock data for the given symbol. """ url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}&apikey={API_KEY}" response = requests.get(url) response.raise_for_status() data = response.json() return data def get_intraday_stock_data(symbol: str): """ Get intraday stock data for the given symbol. """ url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={API_KEY}" response = requests.get(url) response.raise_for_status() data = response.json() return data def main(): data = get_intraday_stock_data(STOCK_SYMBOL) # print(data) # Turn the data into a pandas DataFrame. df = pd.DataFrame(data["Time Series (5min)"]).T df.index = pd.to_datetime(df.index) df = df.astype(float) # Print the dataframes. print(df.head()) # Plot the data in a graph. plt.plot(df["4. close"]) plt.show() pass if __name__ == "__main__": main() 

参考

https://www.youtube.com/watch?v=tG8PPne7ef0&ab_channel=pixegami

apicodeurlcopilotgithubgitgithub copilotjsonpandasrapyoutubepythonleetcodecreatedoc
  • 本文作者:李琛
  • 本文链接: https://wapzz.net/post-5483.html
  • 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。
本站部分内容来源于网络转载,仅供学习交流使用。如涉及版权问题,请及时联系我们,我们将第一时间处理。
文章很赞!支持一下吧 还没有人为TA充电
为TA充电
还没有人为TA充电
0
  • 支付宝打赏
    支付宝扫一扫
  • 微信打赏
    微信扫一扫
感谢支持
文章很赞!支持一下吧
关于作者
2.3W+
5
0
1
WAP站长官方

【AI绘画--七夕篇】使用Stable Diffusion的艺术二维码完全生成攻略

上一篇

[AIGC] Apache Spark 简介

下一篇
  • 复制图片
按住ctrl可打开默认菜单