Code, Python

NES catalog database

Download the code at

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



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:


                                elif choice == ALL:


                                elif choice == ADD:


                                elif choice == CHANGE:


                                elif choice == DELETE:



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():


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:


                                                year = input('Year: ')

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

                                                passed_test =

                                                result = (

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

                                                year_entry = False


                                                print('Date not in correct format')


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

                                NESdatabase[name] = record



                                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('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')


                                print('Title not found')

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

def load_database():


                                input_file = open(FILENAME, 'rb')

                                nes_database = pickle.load(input_file)


                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)


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

def get_menu():




                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')


                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  


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 *