Python Forum

Full Version: AttributeError: 'list' object has no attribute 'upper'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Get the AttributeError: 'list' object has no attribute 'upper'
Return this line the keywords 'upper'
def keygen(self, gp, sk, i, gid, pkey):
h = gp['H'](gid) 
K = (gp['g'] ** sk[i.upper()]['alpha_i']) * (h ** sk[i.upper()]['y_i'])
        
        pkey[i.upper()] = {'k': K}
        pkey['gid'] = gid
        if(debug):
            print("Key gen for %s on %s" % (gid, i))
            print("H(GID): '%s'" % h)
            print("K = g^alpha_i * H(GID) ^ y_i: %s" % K)
        return None
Error:
Traceback (most recent call last): File "/home/ali/Downloads/MedShare2021-main/test.py", line 9, in <module> class Dabe(ABEncMultiAuth): File "/home/ali/Downloads/MedShare2021-main/test.py", line 25, in Dabe for i in usr_attrs1,usr_attrs2: dabe.keygen(public_parameters, master_secret_key, i, ID, secret_keys) File "/usr/local/lib/python3.9/dist-packages/Charm_Crypto-0.50-py3.9-linux-x86_64.egg/charm/schemes/abenc/dabe_aw11.py", line 95, in keygen K = (gp['g'] ** sk[i.upper()]['alpha_i']) * (h ** sk[i.upper()]['y_i']) AttributeError: 'list' object has no attribute 'upper' Process finished with exit code 1
Nobody can do anything with this snippet.
As the error message says, there is no 'upper' in a list, only in a string.
The indentation is wrong too.
@ Axel_Erfurt before modifying the code running after I modify the code give an error but the keywords 'upper is already in the code.
You only call "i.upper()", so "i" must be a list. You can ask if i is a list.
def keygen(self, gp, sk, i, gid, pkey):
    print(i, type(i))   # What is the type of i?
    h = gp['H'](gid) 
    K = (gp['g'] ** sk[i.upper()]['alpha_i']) * (h ** sk[i.upper()]['y_i'])

    pkey[i.upper()] = {'k': K}
    pkey['gid'] = gid
    if(debug):
            print("Key gen for %s on %s" % (gid, i))
            print("H(GID): '%s'" % h)
            print("K = g^alpha_i * H(GID) ^ y_i: %s" % K)
    return None
If "i" is a list your are also going to have a problem with sk[i]. You cannot use a list as a key for a dictionary (list is not a hashable type), so "i" is guaranteed to not match any of the keys in "sk".

After you change the calling code to pass a string instead of a list, I would also modify the function so you don't have to keep calling ".upper()"
[python]def keygen(self, gp, sk, i, gid, pkey):
    i = i.upper()
    h = gp['H'](gid) 
    K = (gp['g'] ** sk[i]['alpha_i']) * (h ** sk[i]['y_i'])

    pkey[i] = {'k': K}
    pkey['gid'] = gid
    if(debug):
            print("Key gen for %s on %s" % (gid, i))
            print("H(GID): '%s'" % h)
            print("K = g^alpha_i * H(GID) ^ y_i: %s" % K)
    return None
[/python]
@deanhystad yes right I got your point