Code, Python

NES catalog database

Download the code at https://github.com/brettius/nes-database

A Python database capable of cataloging old cartridge games in a collection, this one specifically for Nintendo Entertainment System cartridges.

The following is the procedural code. The code starts by importing the needed modules. The regular expression module is to be used to attempt to ensure clean data entry.

import database

import pickle

import re

Define constants for use in menu options

GET = 1

ALL = 2

ADD = 3

CHANGE = 4

DELETE = 5

QUIT = 6

Load the database, and then define the highest level function, the main menu for the user.

FILENAME = 'data.dat'

def main():

                NESdatabase = load_database()

                choice = 0

                while choice != QUIT:

                                choice = get_menu()

                                if choice == GET:

                                                look_up(NESdatabase)

                                elif choice == ALL:

                                                get_all(NESdatabase)

                                elif choice == ADD:

                                                add(NESdatabase)

                                elif choice == CHANGE:

                                                change(NESdatabase)

                                elif choice == DELETE:

                                                delete(NESdatabase)


                save(NESdatabase)

Define a lookup function so the user can lookup a game by title. There are more options that could go here to allow for instance a lookup for matching dates.

def look_up(NESdatabase):

                name = input('Enter a game title')

                print(NESdatabase.get(name, 'That game is not found'))

When your just not sure of what you have in the inventory.

def get_all(NESdatabase):

                for k, v in NESdatabase.items():

                                print(v)

The next function starts a loop for the year entry so regular expressions pattern matching can be run. The first number of the year needs to be a ‘1’ or a ‘2’, as well as the entire year needs to be no longer then four digits. If the test fails, the year entry box starts again. If test passes the entry is added to the database.

def add(NESdatabase):

                name = input('Name: ')

                category = input('Category: ')

                year_entry = True

                while year_entry == True:

                                try:

                                                year = input('Year: ')

                                                check_valid_date = re.compile(r'^[12](\d){3}')

                                                passed_test = check_valid_date.search(year)

                                                result = (passed_test.group())

                                                record = database.NES(name,category,result)

                                                year_entry = False

                                except:

                                                print('Date not in correct format')

                                                continue

                #Checks if it already exists if name not in NESdatabase:

                                NESdatabase[name] = record

                                print('Entered')

                else:

                                print('Already exists')

Function that allows the user to update an entry.

def change(NESdatabase):

                name = input('Enter a title')

                if name in NESdatabase:

                                category = input('Enter a new category')

                                year = input('Enter a new year')

                                record = database.NES(category,year)

                                #Update entry

                                NESdatabase[name] = record

                                print('Updated')

                else:

                                print('Title not found')

Next is a simple functions that deletes a title by key.

def delete(NESdatabase):

                name = input('Enter a title')

                #If title found, delete title

                if name in NESdatabase:

                                del NESdatabase[name]

                                print('Title deleted')

                else:

                                print('Title not found')

The next two options load and save the data to a binary format.

def load_database():

                try:

                                input_file = open(FILENAME, 'rb')

                                nes_database = pickle.load(input_file)

                                input_file.close()

                except IOError:

                                nes_database = {}   

                return nes_database                     

               

#Pickels object and saves it to file

def save(NESdatabase):

                output_file = open(FILENAME, 'wb')

                pickle.dump(NESdatabase, output_file)

                output_file.close()

The lowest level function comes last, the printed text for the menu, and then main() is finally run.

def get_menu():

                print()

                print('Menu')

                print('-------------------------------------------------------------')

                print('1. Look up game')

                print('2. See all games')

                print('3. Add new game')

                print('4. Change exisiting game')

                print('5. Delete a game')

                print('6. Quit program')

                print()

                choice = int(input('Enter a choice'))

                #Validating number entered using comparison operator

                while choice < GET or choice > QUIT:

                                choice = int(input('Please enter a valid choice'))        

                return choice  

main()

Next is the Class file used by the program, the code written to store a name, a year and a date. The attributes are set to private as indicated by the _.

#Create a class to hold information about an NES cartridge

class NES:

                def __init__(self, name, category, year):

                                self.__name = name

                                self.__category = category

                                self.__year = year       

                #Set the attributes           

                def set_name(self):

                                self.__name = name          

                def set_category(self):

                                self.__category = category     

                def set_year(self):

                                self.__year = year 

                #Provide methods to get the attributes

                def get_name(self):

                                return self.__name

                def get_category(self):

                                return self.__category  

                def get_year(self):

                                return self.__year

A shortcut for referring to all the data at once is entered here, using ‘def __str__(self): ‘

                #Return database information as a string

                def __str__(self):

                                return 'Name: ' + self.__name + '\tCategory: '+ self.__category + \

                                '\tYear: ' + self.__year

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *