Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Current Project
#1
I like the post from here and thought I would give it a try using tkinter.
Only got a basic text version at the moment. Uses a json file for the database.
I will probably break the search function down into three separate methods when I get ready to apply it to a tkinter gui.
Planning on adding a add and delete button as well. Anyhow here is my attempt at a search for the characters.

json file
{
    "wizard": [
        {
            "name": "ralph",
            "age": 20,
            "spells": {"fireball": 10, "firestorm": 15}
        },

        {
            "name": "george",
            "age": 20,
            "spells": {"firestorm": 15, "meteor": 30}
        }
    ],

    "cleric": [
        {
            "name": "fred",
            "age": 18,
            "spells": {"heal": 5, "heal all": 12}
        }
    ]
}
python file
import json

# Get json file
file = "character.json"

# Open json file and load
with open(file, 'r') as rfile:
    data = json.load(rfile)

# Define function for searching and returning data
def search(term=None, card=None):

    # If card == name, we're seaching by name
    if card == 'name':

        # Create an empty return list
        ret = []

        # Get key, value pair from json file
        for info, stat in data.items():

            # Loop over stat to find the name and append to mylist all data related
            for item in stat:
                if item['name'] == term.lower():
                    ret.append(f'character type: {info}')
                    mylist = [item for item in item.items()]

        # The list has dict, so we loop over and format the dicts then append to
        # the ret list along with data that is not a dict
        for items in mylist:
            tmp = []
            if isinstance(items[1], dict):
                for key, value in items[1].items():
                    tmp.append(f'{key}: {value}')
                string = ', '.join(tmp)
                ret.append(f'{items[0]}: {string}')
            else:
                string = f'{items[0]}: {items[1]}'
                ret.append(string)

    # Elif the card is a guild search, we loop over the json file and return all
    # data for that character type
    elif card == 'guild':
        ret = []
        for items in data[term]:
            ret.append(f'character type: {term}')
            tmp = []
            for key, val in items.items():
                if isinstance(val, dict):
                    for item, strength in val.items():
                        tmp.append(f'{item}: {strength}')
                    string = ', '.join(tmp)
                    ret.append(f'{key}: {string}')
                else:
                    string = f'{key}: {val}'
                    ret.append(string)

    # Else we want to return all characters
    else:
        ret = []
        for _type, stats in data.items():
            for items in stats:
                ret.append(f'character type: {_type}')
                tmp = []
                for item, stat in items.items():
                    if isinstance(stat, dict):
                        for key, val in stat.items():
                            tmp.append(f'{key}: {val}')
                        string = ', '.join(tmp)
                        ret.append(string)
                    else:
                        string = f'{item}: {stat}'
                        ret.append(string)

    return ret

# Displays all characters
# i = 0
# for items in search():
#     print(items)
#     if i > 2:
#         print()
#         i = 0
#     else:
#         i += 1

# Returns all guild type characters
card = search('cleric', 'guild')
i = 0
for items in card:
    print(items)
    if i > 2:
        print()
        i = 0
    else:
        i += 1

# Returns single character data
# card = search('fred', 'name')
# for data in card:
#     print(data)
Output:
character type: cleric name: fred age: 18 spells: heal: 5, heal all: 12
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Current Project - by menator01 - Apr-10-2022, 08:31 AM
RE: Current Project - by menator01 - Apr-11-2022, 06:39 AM
RE: Current Project - by menator01 - Apr-13-2022, 11:12 AM
RE: Current Project - by menator01 - Apr-27-2022, 03:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Current project - Simple tkinter login system menator01 3 1,745 Sep-01-2023, 05:56 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020