Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

python - how to pass class __init__ attrributes as a function parameter

So right now I'm parsing a pretty big size JSON response into separate custom objects / list of objects.

Everything is working perfectly until I try to implement a efficient so I can return the organized json into different languages that it has stored as a function parameter.

here is a example function that will perfectly return a list of English translations as the default language.

async def getProductInfo(self, localized = None):
    productContent = await self.getProductContent()

    if localized is None: #i.e the default is just english
        # this works fine returns the titles of the products in English
        return [content.titles[i].names for i in range(len(content.titles))]
    else:
        return [content.titles[i].localizedTitleNames.ar_AE for i in range(len(content.characters))]

now the issue I have is if I need to retrieve different language variations I have to do

content.titles[i].localizedTitleNames.ar_AE

which also perfectly works and in this case returns the Arbaic translations of the products. What I want to do is somehow make it so the user can pass just the parameter like .ar_AE and I can just add it to the end of content.titles[i].localizedTitleNames.

currently if I want a certain language I have to use the above code, different language examples:

content.titles[i].localizedTitleNames.ar_AE
content.titles[i].localizedTitleNames.de_DE
content.titles[i].localizedTitleNames.it_IT
content.titles[i].localizedTitleNames.ko_KR

etc (over 20+)...

the first way I thought to do it was simply like this

async def getProductInfo(self, localized: str = None):
    productContent = await self.getProductContent()

    if localized is None: #i.e the default is just english
        # this works fine returns the titles of the products in English
        return [content.titles[i].names for i in range(len(content.titles))]
    if localized == 'ar_AE':
        return [content.titles[i].localizedTitleNames.ar_AE for i in         
    if localized == 'de_DE':
        return [content.titles[i].localizedTitleNames.de_DE for i in range(len(content.characters))]

which would work but seems very messy and inefficient to me. I'm not sure if the cause is entirely on how I structured the overall language class. or maybe I'm just completely overthinking this and its much easier

example of the Language class I use, just removed the extra 20+ languages that are basically identical minus their country code at the end.

class LocalizedTitleNames:
    def __init__(self, json):
        self.ar_AE = get_json_value(json, 'ar-AE')
        self.de_DE = get_json_value(json, 'de-DE')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...