首页 > 解决方案 > 数据输出和内部函数不一样

问题描述

我目前遇到一个问题,我试图将数据存储在列表中(使用数据类)。当我在函数 (PullIncursionData()) 的列表中打印数据时,它会以一定数量的数字进行响应(从不一样,由于它的性质是不可能的)。在调用它以存储它后打印它时,它以 Var 的形式返回,它以某种方式只打印相同的数字。

我无法分享这些数字,因为它们会使用 EVE Online 的 API 进行更新,所以唯一的方法是在本地运行它并自己阅读第一个列表。

存储库在这里:https ://github.com/AtherActive/EVEAPI-Demo


def PullIncursionData():
    #Pulls data from URL and converts it into JSON
    url = 'https://esi.evetech.net/latest/incursions/?datasource=tranquility'
    data = rq.get(url)
    jsData = data.json()

    #Init var to store incursions
    incursions = []

    #Set lenght for loop. yay
    length = len(jsData)

    # Every loop incursion data will be read by __parseIncursionData(). It then gets added to var Incursions.
    for i in range(length):
        # Add data to var Incursion.
        incursions.append(__parseIncursionData(jsData, i))
        
        # If Dev mode, print some debug. Can be toggled in settings.py
        if settings.developerMode == 1:
            print(incursions[i].constellation_id)

    return incursions

# Basically parses the input data in a decent manner. No comments needed really.
def __parseIncursionData(jsData, i):
    icstruct = stru.Incursion

    icstruct.constellation_id = jsData[i]['constellation_id']
    icstruct.constellation_name = 'none'
    icstruct.staging = jsData[i]['staging_solar_system_id']
    icstruct.region_name = ResolveSystemNames(icstruct.constellation_id, 'con-reg')
    icstruct.status = jsData[i]['state']
    icstruct.systems_id = jsData[i]['infested_solar_systems']
    icstruct.systems_names = ResolveSystemNames(jsData[i]['infested_solar_systems'], 'system')

    return icstruct
    
# Resolves names for systems, regions and constellations. Still WIP.
def ResolveSystemNames(id, mode='constellation'):
    #init value
    output_name = 'none'

    # If constellation, pull data and find region name.
    if mode == 'con-reg':
        url = 'https://www.fuzzwork.co.uk/api/mapdata.php?constellationid={}&format=json'.format(id)
        data = rq.get(url)
        jsData = data.json()
        output_name = jsData[0]['regionname']
    
    # Pulls system name form Fuzzwork.co.uk. 
    elif mode == 'system':
        #Convert output to a list.
        output_name = []
        lenght = len(id)
        # Pulls system name from Fuzzwork. Not that hard.
        for i in range(lenght):
            url = 'https://www.fuzzwork.co.uk/api/mapdata.php?solarsystemid={}&format=json'.format(id[i])
            data = rq.get(url)
            jsData = data.json()

            output_name.append(jsData[i]['solarsystemname'])
    
    return output_name

icdata = PullIncursionData()
print('external data check:')
length = len(icdata)

for i in range(length):
    print(icdata[i].constellation_id)

结构.py(自定义文件)

@dataclass
class Incursion:
    constellation_id = int
    constellation_name = str

    staging = int
    staging_name = str

    systems_id = list
    systems_names = list
    region_name = str
    status = str

    def ___init___(self):
        self.constellation_id = -1
        self.constellation_name = 'undefined'

        self.staging = -1
        self.staging_name = 'undefined'

        self.systems_id = []
        self.systems_names = []
        self.region_name = 'undefined'
        self.status = 'unknown'

标签: pythonpython-dataclasses

解决方案


推荐阅读