Python: How do I do this?

lilfiend

Supreme [H]ardness
Joined
Nov 28, 2008
Messages
6,726
I'm making a python dnd character sheet thing to try to learn python but I've run into an issue that I can't figure out.
Code:
 for x in usernamelist]
user = Query()
class MyApplication(npyscreen.NPSAppManaged):
    def onStart(self):
        self.addForm("MAIN", startscreen, name = "Welcome!")
#if __name__ == '__main__':
#   TestApp = MyApplication().run()
class startscreen(npyscreen.ActionForm):
    c = ''
    s = ''
    db = ''
    def activate(self):
        self.edit()
       
    #Display and allow selection of existing characters in database/ or enter name of new character
    def create(self):
        self.newchar = self.add(npyscreen.TitleText, name = "New Character Name", value = '')
        self.charselect = self.add(npyscreen.TitleSelectOne, max_height = 10, value = [1, ], name = "Who would you like to edit?", values = cleanlist , scroll_exit=True)
   
    def on_ok(self):
        startscreen.c = self.newchar.value
        if startscreen.c == '':
            startscreen.c = self.charselect.values[self.charselect.value[0]]
        startscreen.db = TinyDB(rel_path + startscreen.c + ".json")
        self.tdbnames = ['cname', 'heigt', 'weigt', 'eyeco', 'hairc', 'class', 'level', 'crace', 'csize', 'cagev', 'gendr', 'skinc', 'align', 'deity', 'expnt', 'acnum', 'initi', 'speed', 'inspi', 'stren', 'dexte', 'const', 'intel', 'wisdo', 'chari', 'strmo', 'dexmo', 'conmo', 'intmo', 'wismo', 'chamo']
        #if database is empty fill it with self.tdbnames and set cname = 
        if not startscreen.db.all():
            for self.x in self.tdbnames :
                if self.x == 'cname' : 
                    startscreen.db.insert({'cname' : startscreen.c})
                    continue
                startscreen.db.insert({self.x : ''})
        startscreen.s = str(startscreen.db.all())
        startscreen.s = re.sub('\[|\]|\"|\'|\{|\}', '', startscreen.s)
        startscreen.s = startscreen.s.split(',')
        self.parentApp.addForm("Character", character, name = "Character Sheet")
        self.parentApp.switchForm("Character")
       
    def on_cancel(self):
        self.parentApp.switchForm(None)
class character(npyscreen.FormWithMenus):
    n = []
    v = []
    def activate(self):
        self.edit()
        self.parentApp.setNextForm(None)
    #main display aera
    def create(self):
        self.counter = 0
        for self.y in startscreen.s:
            self.y = str(startscreen.s[self.counter])
            self.counter += 1
            self.a = self.y.split(':')
            self.titledirty = str(self.a[0])
            self.valuedirty = str(self.a[1])
            self.title = self.titledirty
            self.title = re.sub('^\s\w|^\w', '', self.title)
            self.value = self.valuedirty
            self.value = re.sub('^\s\w|^\w', '', self.value)
            character.n.append(self.title)
            self.f = self.add(npyscreen.TitleText, name = self.title + ":", value = self.value)
            #Can't do this because it'll just add the loaded values into the character.v array, i need to find a way to pull each self.f.value where self.f.name = character.n
#            character.v.append(self.f.value)
    def on_ok(self):
        self.count = 0
        for self.l in character.n:
            if self.f.name in character.n[self.count]:
                character.v.append(self.f.value)
                continue
            self.count +=1
        self.parentApp.switchForm(None)
   
if __name__ == '__main__':
    TestApp = MyApplication().run()
   
co = 0
for i in character.n:
    print character.n[co]
    print character.v[co]

class startscreen:
What it does:
Displays a character selection page where you can select characters from a list or type in a new character name. It gets this list from a folder called database.

if you have typed in a character name it creates a new database and ignores whatever existing you had selected.

on pressing OK is when it actually makes the database or loads it, then it does some fancy regex and sets some class variables so the next page/class can access them. Small note here, self.tdbnames used to have to be 5 characters long, it no longer does but I've not changed it yet.

class character:
What it does:
in a for loop it does some regex stuff to split things from " 'name', 'character's name' " to self.title "name" and self.value "character's name"
it then adds each self.title to the class array n for use later
then it displays all the loaded info


What I'm trying to do here is update the database with whatever the user enters. you can see in the function on_ok in character I've tried to do that but it doesn't work. I think this is because in the create function i'm just over-writing the variable self.f so it will only contain one value.

I can't just add character.v.append(self.f.value) to add the values to the class array v in my for loop either as it will just add the pre-exisiting data it loaded from the database.

How can I do this? It is beyond my google-fu.
 
Back
Top