manageable code

Posted under » Python on 3 Mar 2025

The tradional way to make code manageable is to use a lot of OOP Methodologies

But for some reason, you want to see things clearly, you write like this

for y in a9d :
    myquery =  {'cid': y ,"s09a8r" : {'$ne' : None} }
    for dog in mthlog.find(myquery):
        howmany = len(dog["s09a8r"])
        for i in range(howmany):
            tetel = dog["s09a8r"][i]['qtitle']
            pilih = dog["s09a8r"][i]['chosen']
            tete = tetel + str('r')
            df.loc[y, [tete]] = [pilih]
            print(y, tetel, pilih )

If you need to change the "s09a8r" part, then you can do a replace and have something like this. We have also use a bit of concatenate.

sid = 's09aqr'
sid = sid.replace("q", "8")
for y in a9d :
    myquery =  {'cid': y ,sid : {'$ne' : None}}
    for dog in mthlog.find(myquery):
        howmany = len(dog[sid])
        print(y, howmany)
        for i in range(howmany):
            tetel = dog[sid][i]['qtitle']
            pilih = dog[sid][i]['chosen']
            tete = tetel + str('r')
            df.loc[y, [tete]] = [pilih]
            print(y, tetel, pilih)

Where q is replaced with any number. The code doesn't look much different from the original.

Instead of replace, you can solely use concatenate

ta = '8'
sid = (f's09a{ta}r')

for y in a9d :
    myquery =  {'cid': y ,sid : {'$ne' : None}}
    for dog in mthlog.find(myquery):
        howmany = len(dog[sid])
...

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data