私信  •  关注

Random Davis

Random Davis 最近创建的主题
Random Davis 最近回复了
3 年前
回复了 Random Davis 创建的主题 » 如何在向量类中添加不同的向量?python

从技术上讲,你只需要一次改变就能让它发挥作用; v3 是一个 tuple 共有3个值。你可以把它传递给 Vector 结果将是一个适当的 矢量 可以添加到其他向量的对象,如下所示:

v3 = Vector(Vector.fill_n(3,1))

正如我在评论中所说的 fill_n 方法不引用 self 参数,所以你应该 @staticmethod 在它上面。在这种情况下,这是不必要的,但它会丢弃IDE使用的任何类型的静态分析仪或短绒:

@staticmethod
    def fill_n(size, val):
        ...

考虑到这些,你的脚本运行良好。输出:

5, 4, 5
2, 3, 4
1, 1, 1
3 年前
回复了 Random Davis 创建的主题 » 如何在Python开发文档中提交问题

如果转到链接页面右下角的“V:Latest”链接,然后转到GitHub->编辑后,您将进入此页面: https://github.com/python/devguide/edit/main//compiler.rst

上面写着:

您需要使用此存储库来提出更改。
抱歉,您无法直接编辑此存储库-您需要将其分叉并从那里提出您的更改。

所以,你可以进行回购,进行修正,然后提交一个包含你提议的变更的请求。

或者,正如在评论中指出的,您链接的文档有 page 也就是说你可以在回购协议的 issues page .

当然,核实一下你是否提出了 有效问题 在提交之前,不要浪费开发者的时间。你帖子上的一些评论指出,你认为存在的问题实际上根本不是问题。

3 年前
回复了 Random Davis 创建的主题 » 有限的选项链信息python

我运行了代码的scraper部分,得到了813个股票代码符号的列表,然后直接将该列表放入脚本中,这样我就不必每次调试时都进行scraper。

我也打断了电话线 yf.Ticker(i).options[0] 这样我就可以知道它的哪一部分产生了异常。像这样在一行上有一堆不同的东西,这会让调试变得更加困难。

你也只是在做 except: ,它明确地丢弃了错误消息,这意味着我不知道它是什么。我还让它明确地告诉我哪些代码错误,如果遇到错误,只需继续将代码添加到列表中。

代码只会循环直到出现错误,在这种情况下,字符串“error”将被添加到列表中 opts ,然后程序就结束了,因为您没有捕获循环中的错误,然后继续执行下一项。这意味着它只会在第一个错误时停止,因此您的列表中没有很多项。

第一个错误出现在索引116处的项目之后,这解释了为什么列表中只有那么多的项目。

下面是我的测试代码的外观(代码列表被截断):

import yfinance as yf

#I actually have all tickers in the list,
#I just removed a big chunk from the middle for example purposes
tickers = ['A', 'AA', 'AAL', 'AAP', 'ABB', 'ABC', 'ZM', 'ZNH', 'ZS', 'ZTO', 'ZTS']

opts = []

for i in range(len(tickers)):
    ticker = tickers[i]
    try:
        ticker_obj = yf.Ticker(ticker)
    except Exception as e:
        print('cannot create yf.Ticker object', i, ticker, e)
        continue
    try:
        ticker_obj_options = ticker_obj.options
    except Exception as e:
        print('cannot get options', i, ticker, e)
        continue
    try:
        first_option = ticker_obj_options[0]
    except Exception as e:
        print('cannot get first option', i, ticker, e)
        continue
    opts.append(first_option)

print(opts)

这段代码的输出是:

cannot get first option 116 BTO tuple index out of range
cannot get first option 141 CEA tuple index out of range
cannot get first option 286 FERG tuple index out of range
cannot get first option 373 IHG tuple index out of range
cannot get first option 392 IX tuple index out of range
cannot get first option 397 JHX tuple index out of range
cannot get first option 525 NVR tuple index out of range
cannot get first option 600 RELX tuple index out of range
cannot get first option 637 SHG tuple index out of range
cannot get first option 676 SUZ tuple index out of range
cannot get first option 701 TLK tuple index out of range
cannot get first option 767 WBK tuple index out of range

意思是,对于那些股票代码 yf.Ticker 对象有一个空的 options 元组。至于为什么,我不理解这种金融方面的东西,所以这取决于你来找出为什么雅虎金融没有这些符号的选项。也许他们应该在他们的房间里放些东西 选项 也许他们不应该,但我不知道。

4 年前
回复了 Random Davis 创建的主题 » NameError:未在Python中定义名称“x”

你还没有定义这些全局变量。打字 global whatever 不会自动定义一个名为 whatever .它只是告诉解释器存在一个名为 无论什么 在全球范围内。

例如,以下代码不会产生错误:

blah = 'yada'

def foo(bar):
    global blah
    print(blah, bar)
    
foo('test') # output: yada test

然而在这个例子中,如果全局变量没有事先定义(我把它注释掉),它会得到与您相同的错误:

#blah = 'yada'

def foo(bar):
    global blah
    print(blah, bar)
    
foo('test') # output: NameError: name 'blah' is not defined

所以,为了避免错误,你必须事先给你的全局变量一些值,比如 None .虽然如果你使用一个类来保存你需要的值,你可能会完全避免全局变量,比如:

import numpy as np

class MyClass(object):
    def __init__(self):
        #you'll have to set the numerical values to something that causes num_var_list to not loop infinitely
        self.number_list = None
        self.number_exit = 0
        self.number_target = 0
        self.price_buy = 0

    # This function generates a list of numbers under certain rules
    def num_var_list(self):
        self.number_list = []
        self.number_target = self.number_exit
        num_max_robot = (self.number_target * 20) / 100
        while num_max_robot > 0:
            num_robot = np.random.randint(1,int(num_max_robot))
            if num_robot > self.number_target:
                    self.number_list.append(self.number_target)
            else: 
                self.number_list.append(num_robot)
            self.number_target = self.number_target - self.number_target
        return self.number_list
            
    # This function generates a random number between a certain range
    def fun_price_buy(self):
        self.price_buy = np.random.randint(50000,300000)
        return self.price_buy

    # This function generates a random number between a certain range
    def fun_mx_buy(self):
        self.number_exit = np.random.randint(50, 150)
        return self.number_exit

def main():
    lista_number_list = []
    lista_price_buy = []
    lista_mx_buy = []
    
    my_class_instance = MyClass()

    # This loop append each function 50 times to a new list
    while len(lista_price_buy) <= 50: 
        lista_number_list.append(my_class_instance.num_var_list())
        lista_price_buy.append(my_class_instance.fun_price_buy())
        lista_mx_buy.append(my_class_instance.fun_mx_buy())

if __name__ == '__main__':
    main()