Day 49 of 100 Days Coding Challenge: Python
One day, you’re happily adding a shiny new genre—Mystery—and the next day it’s gone, vanished like a plot twist in an Agatha Christie novel. No “goodbye,” no note, not even a hint of suspense music. I had proudly added a function yesterday to introduce new genres and assign them pretty colors. But today? That color-coded Mystery entry was MIA.
It turns out I was adding it, but… nowhere permanent. The program was as forgetful as I am without caffeine. So, I rolled up my digital sleeves and decided it was time to get serious. Enter: the humble JSON file. My mission? To build a system where genres and their colors stick around—even after the script shuts down. This also meant rewriting a lot of the functions that were still clinging to the old hardcoded dictionary, like it was a safety blanket. It was a minor genre revolution.
Today’s Motivation / Challenge
Why bother with persistent data? Well, imagine creating something you’re proud of—then watching it vanish the next time you open the program. Frustrating, right? This project reminded me of learning to save Word documents the first time you write them. This was about teaching my Python script to remember what I told it… like a well-trained assistant, not a goldfish.
Purpose of the Code (Object)
This update allows the program to permanently remember newly added genres and their colors. Instead of living only in memory (and disappearing the moment you close your terminal), your genres are now safely stored in a JSON file—ready to return every time you open the app. It’s like giving your code a diary… and making sure it actually writes in it.
AI Prompt
Able to save newly added genres and their colors.
Functions & Features
- Load genre-color pairs from a JSON file at startup
- Add new genres with custom colors on the fly
- Automatically save those updates so nothing gets lost
- Replaces hardcoded dictionaries with dynamic loading
Requirements / Setup
- Python 3.6+
- pandas, matplotlib
- No external JSON library needed—just the built-in json and os modules
Install if needed:
pip install pandas matplotlib
Minimal Code Sample
import json
import os
GENRE_FILE = “genre_colors.json”
def load_genre_colors():
if os.path.exists(GENRE_FILE):
with open(GENRE_FILE, ‘r’) as f:
return json.load(f)
return {
“Fiction”: “gray”,
“Fantasy”: “purple”
}
def save_genre_colors(genre_color_dict):
with open(GENRE_FILE, ‘w’) as f:
json.dump(genre_color_dict, f, indent=4)
# Use these to load and update your color map
genre_color_dict = load_genre_colors()
This snippet loads and saves your genre-color dictionary using a JSON file instead of hardcoded values.
Notes / Lessons Learned
This was actually a significant overhaul of the existing programming. I was so nervous I triple-checked every line and even cloned my file before making changes—just in case things went full chaos mode. First, I had to import json and os, then create a file path for my new JSON genre diary. Next, I rewired the loading function to pull from this file, and rewrote add_new_genre_color() to actually save the additions (not just pretend).
Then came the big cleanup: my program was still using GENRE_COLORS all over the place like it was 2022. Every instance had to be swapped with genre_color_dict to work with my new system. That part? Not glamorous. But when I ran the script and everything held together—and Mystery stayed put—it felt like winning a game of digital Clue.
Optional Ideas for Expansion
- Add a user input menu to pick a color visually from a palette (instead of typing ‘slateblue’)
- Add a “reset to default” feature if your JSON file ever gets out of hand
- Include tooltips in the visualization showing genre + average page counts
