Posts: 4
Threads: 1
Joined: Jan 2019
Jan-11-2019, 07:09 PM
(This post was last modified: Jan-11-2019, 07:09 PM by giveen.)
I'm struggling to get this list from a json file to call correctly, and I was hoping for help. I'm still really new to learning python. I always end up with "TypeError: list indices must be integers or slices, not str"
1 2 3 |
CONFIG_ITEMS = 'items.json'
with open (CONFIG_ITEMS) as json_data_file:
config_items = json.load(json_data_file)
|
1 2 3 4 5 6 7 |
def get_value(items: str ):
items = items.replace( " " , "").lower()
aliases = config_items[ 'aliases' ]
if items in aliases:
return [value[aliases]]
else :
return None
|
The json looks like this:
1 2 3 4 5 6 |
[
{
"aliases" : [ "carvedtwine" , "carved" , "carvedt" , "ct" ],
"value" : 10
}
]
|
Posts: 2,342
Threads: 62
Joined: Sep 2016
What does your config look like?
Posts: 4
Threads: 1
Joined: Jan 2019
(Jan-11-2019, 07:21 PM)micseydel Wrote: What does your config look like? I posted a snippet above, but here is the full thing.
https://pastebin.com/pKsuKzXZ
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Jan-11-2019, 07:09 PM)giveen Wrote:
1 2 3 |
aliases = config_items[ 'aliases' ]
if items in aliases:
return [value[aliases]]
|
aliases is a list. Using a list as an index doesn't make sense, so what is your intention with this code snippet?
1 2 3 4 5 6 |
>>> aliases = [ "spam" , "eggs" ]
>>> value = list ( range ( 5 ))
>>> value[aliases]
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
TypeError: list indices must be integers or slices, not list
|
Posts: 4
Threads: 1
Joined: Jan 2019
The code feeds back into a discord command. I know this works, as my previous version of the bot works fine, but the json file was a mess and needed to be completely rewritten.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
async def calc(ctx, quantity: float , itemA: str , itemB: str ):
itemAPrice = get_value(itemA)
itemBPrice = get_value(itemB)
if itemAPrice and itemBPrice:
itemQuotient = itemAPrice / itemBPrice
itemBEquivalent = round (quantity * itemQuotient, 2 )
quantity = quantity
itemA = itemA
itemBEquivalent = itemBEquivalent
itemB = itemB
author = ctx.message.author.name
embed = discord.Embed(title = "\N{CONVENIENCE STORE} Recommended Exchange Rate \N{CONVENIENCE STORE}" , description = f '{quantity} {itemA} is equal to {itemBEquivalent} {itemB}' , color = 0xDA70D6 )
embed.set_footer(text = f "Requested by {author}" )
await ctx.send(embed = embed)
await ctx.message.delete()
elif not itemAPrice:
await ctx.send( 'No match found for ' + itemA)
elif not itemBPrice:
await ctx.send( 'No match found for ' + itemB)
|
Previous version of it looked like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CONFIG = 'config.json'
with open (CONFIG) as json_data_file:
config = json.load(json_data_file)
TOKEN = config[ 'token' ]
items = config[ 'items' ]
BOT_PREFIX = config[ 'prefix' ]
aliases = config[ 'aliases' ]
def get_value(item: str ):
if item:
item = item.replace( " " , "").lower()
try :
return items[aliases[item]]
except KeyError:
return
else :
return None
|
with the json looking like this
https://pastebin.com/eNztKujJ
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Jan-11-2019, 07:44 PM)giveen Wrote: return items[aliases[item]] That's different from what you're doing, though. In this version, you're getting one particular item out of the aliases (this time, aliases is probably a dict though), and using the value from aliases as a key/index to items , instead of using the whole aliases list as the index.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Maybe your new code should be something like this?
1 2 3 |
aliases = config_items[ 'aliases' ]
if items in aliases:
return [value[aliases[item]]]
|
Posts: 4
Threads: 1
Joined: Jan 2019
Jan-11-2019, 08:05 PM
(This post was last modified: Jan-11-2019, 08:05 PM by giveen.)
That was what I had played with as well, similar idea. Still studying deeper into JSON syntax to see where I could be going wrong.
TypeError: list indices must be integers or slices, not str
I think slicing needs to be done, so I'm reading up on it.
|