Account cleaner doesnt work

Ok, so im doing an account cleaner for my project i need to run to delete all accounts without password, but for some reason it doesnt delete them…

Code:

        keys_to_remove = []
        for key in db.keys():
            try: 
                if isinstance(key, str) and db[key]['password'] == "":
                    keys_to_remove.append(key)
                    print("Removed a user! (Blank Password)")
            except KeyError:
                keys_to_remove.append(key)
                print("Removed a user! (No Password)")
        for key in keys_to_remove:
            del db[key]
        print("Removal complete")

Could you provide us a sample Database input, and output from the sample?

1 Like
{
'someguy': {balance: 150, password:1234"},
's4il: {balance: 150,} ,
'mrcheese': {balance: 150, password:},
}

something like that, of course waay larger, like 400 users aprox, but that structure

please send an example of what happens instead

1 Like

I think this should work:

def safeKeyCheck(_dict: dict, key: any) -> any:
    try:
        return _dict[key]
    except KeyError:
        return None
for key in db.keys():
    value = safeKeyCheck(db, key)
    if value in ["", None]:
        print(f"Removed a user! ({'Blank' if value=='' else 'No'} Password)")
        del db[key]
print("Removal complete")
2 Likes

it didnt work… it just did nothing for a while, then said removal complete, but did nothing

1 Like

basically says its removing things, but its not

or do i have to edit something? i just did crl+c, crl+v

ok basically did you check the db and it did nothing?

1 Like

yep, it did nothing, it didnt delete anything

1 Like

can I have a repl link

1 Like

Hmm. Try this:

def safeKeyCheck(_dict: dict, key: any) -> any:
    try:
        return _dict[key]
    except KeyError:
        return None
for key in db.keys():
    value = safeKeyCheck(db[key], "password" )
    if value in ["", None]:
        print(f"Removed {key}. Reason: {'Blank' if value!=None else 'No'} Password")
        del db[key]
    else:
        print(f"Not removing {key}, has a valid password.")
print("Removal complete")

@S4IL ^^^ (I replied to my post by mistake, lol)

3 Likes

Thanks! It worked! Until… someone’s db must be corrupted or something because it gives an error now

Huh. Could you do a raw data dump and see if there’s any unexpected key/value combinations?

for k, v in db:
    try:
        print(f"{k}: {v}")
    except:
        print(f"Bad key: {k}")
1 Like

image
i runned that and error

Okay, that’s fine, try this:

for k in db:
    try:
        print(f"{k}: {db[k]}")
    except:
        print(f"Bad key: {k}")
2 Likes

wierd, there are not any bad keys

Odd. Does the original remover code still crash? If so, modify it to this version so you can see which key is bad:

def safeKeyCheck(_dict: dict, key: any) -> any:
    try:
        return _dict[key]
    except KeyError:
        return None
for key in db:
    try:
        value = safeKeyCheck(db[key], "password" )
        if value in ["", None]:
            print(f"Removed {key}. Reason: {'Blank' if value!=None else 'No'} Password")
            del db[key]
        else:
            print(f"Not removing {key}, has a valid password.")
    except TypeError:
        print(f"Bad key: {key}")
print("Removal complete")
1 Like

Thanks! That worked! Now everything is clean!! Thanks you!!! :]

1 Like