0

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')
  • Perhaps you're looking for `getattr` and `dir`; however, how do you get it into an object in the first place? JSON parse result are usually dicts already. – user202729 Jan 06 '21 at 03:20
  • yeah you're correct it does in fact turn into a dict at first but it later gets used in another class along side some other classes that at the end gets turned into an object which allows for me to call functions like getProductInfo(). I actually tried using both of those right away and I thought they would work but I seem to always be getting AttributeError: type object 'LocalizedTitleNames' has no attribute 'ar_AE' even thought when I print out using dir it shows 'ar_AE' as an attribute alongside the other languages. – mathsisfun Jan 06 '21 at 03:46
  • Show a [example]. What is the type of the mentioned variable? (perhaps in some library?) -- also show the code that you print out the `dir` and call the `getattr` something. "type object" usually means that the object you passed into it is wrong. – user202729 Jan 06 '21 at 03:48
  • wow I can't believe it, you're 100% correct I have been passing the wrong object into getattr. Just tried it again with the actual correct object and it is working perfectly! Can't believe I've been over looking that. Thank you so much! – mathsisfun Jan 06 '21 at 03:53
  • I could post an answer, but this is pretty much a duplicate of [python - How to access object attribute given string corresponding to name of that attribute - Stack Overflow](https://stackoverflow.com/questions/2612610/how-to-access-object-attribute-given-string-corresponding-to-name-of-that-attrib) . – user202729 Jan 06 '21 at 04:06

0 Answers0