代码之家  ›  专栏  ›  技术社区  ›  Jennifer Crosby

Python:获取TypeError:rndm\u json\u data\u gen()缺少1个必需的位置参数:“rangenum”。不知道为什么

  •  0
  • Jennifer Crosby  · 技术社区  · 4 年前

    错误消息:
    TypeError: rndm_json_data_gen() missing 1 required positional argument: 'rangenum'

    我看不出少了什么。我已经查看了stackoverflow上的许多类似线程,但看不出任何东西如何适用于我的情况。

    这段代码首先在终端Python上运行 handler.py :

    #########################################################
    ##              HANDLER.PY                             ##
    #########################################################
    import psycopg2
    from lib.feeder import Feeder
    from lib.tle import TLE
    from lib.import_data import IMPORTDATA
    from lib.random_json import DATA_GEN
    
    ##TECHNICALLY THIS COMES PRE-LOADED
    conn_string = "database connection"
    
    # print the connection string we will use to connect
    #print("Connecting to database\n    ->%s" % (conn_string))
    # get a connection, if a connect cannot be made an exception will be raised here
    conn = psycopg2.connect(conn_string)
    
    ##THIS IS THE PART THAT I’M TRYING TO GET TO WORK
    
    TypeProcessor = IMPORTDATA()
    num_json_items = 5
    
    #####Error happens with last item here######
    Feeder(conn).insertOrUpdateItem("database_table", TypeProcessor.getDictionary(),
    TypeProcessor.getIds(), TypeProcessor.getModifiedKey(), TypeProcessor.getUpdateOnConflict(),
    DATA_GEN.rndm_json_data_gen(num_json_items))
    
    print("Import Data complete")
    '''
    ## Below was what was in the last spot, where the problem is occurring,
    ## before I started changing it to a method
        {"uuid": "UUID1", "column1": "data1", "column2": data2, "column3": "data3", \
        "column4": "data4", "file_name": "filename", "load_date": "2020-12-09"}
    

    #########################################################
    ##              IMPORT_DATA.PY                         ##
    #########################################################
    
    modified_key = "load_date"
    update_on_conflict = True
    ids = ["uuid"]
    dictionary = {
        "uuid": {"input_field": "uid", "update_on_conflict": False},
        "column1": {"input_field": "column1", "update_on_conflict": True},
        "column2": {"input_field": "column2", "update_on_conflict": True},
        "column3": {"input_field": "column3", "update_on_conflict": True},
        "column4": {"input_field": "column4", "update_on_conflict": True},
        "load_date": {"input_field": "load_date", "update_on_conflict": True}
        };
    
    class IMPORTDATA():
    
        def getDictionary(self):
            return dictionary;
    
        def getIds(self):
            return ids;
    
        def getModifiedKey(self):
            return modified_key;
    
        def getUpdateOnConflict(self):
            return modified_key;
    

    ###############################################
    ##            RANDOM_JSON.PY                 ##
    ###############################################
    
    import json
    from faker import Faker
    import random
    from random import randint
    from datetime import datetime
    
    class DATA_GEN():
    def rndm_json_data_gen(self,rangenum):
        fake = Faker()
        Faker.seed(0)
        for _ in range(rangenum):
            import_data={
                "uuid": fake.bothify(text='UUUID##########'),\
                "column1": fake.bothify(text='Data##'), \
                "column2": fake.bothify(text='##'), \
                "column3": fake.bothify(text='##'), \
                "file_nm": fake.file_name(), \
                “import_date”: datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
            }
            return import_data;
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   Noah    4 年前

    rndm_json_data_gen 作为一个实例方法,但是您像静态方法一样调用它。因此 self 参数没有自动完成。你要么先实例化它,

    data_gen = DATA_GEN()
    
    Feeder(conn).insertOrUpdateItem("database_table", TypeProcessor.getDictionary(),
    TypeProcessor.getIds(), TypeProcessor.getModifiedKey(), TypeProcessor.getUpdateOnConflict(),
    data_gen.rndm_json_data_gen(num_json_items))
    

    或者标记它 @staticmethod 自己 参数。因为你没有使用 自己

    顺便说一句, official Python style 调用PascalCase类名,而不是CAPS。