The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Make a code snippet collector

Here’s a simple code snippet collector implemented in Python. It allows you to add, list, search, and retrieve code snippets stored locally in a JSON file.

python
import json import os SNIPPETS_FILE = "snippets.json" def load_snippets(): if not os.path.exists(SNIPPETS_FILE): return {} with open(SNIPPETS_FILE, "r") as f: return json.load(f) def save_snippets(snippets): with open(SNIPPETS_FILE, "w") as f: json.dump(snippets, f, indent=4) def add_snippet(title, code, description=""): snippets = load_snippets() if title in snippets: print(f"Snippet '{title}' already exists.") return snippets[title] = { "code": code, "description": description } save_snippets(snippets) print(f"Snippet '{title}' added.") def list_snippets(): snippets = load_snippets() if not snippets: print("No snippets found.") return for title in snippets: print(f"- {title}") def get_snippet(title): snippets = load_snippets() snippet = snippets.get(title) if not snippet: print(f"No snippet found with title '{title}'.") return print(f"Title: {title}") print(f"Description: {snippet.get('description', '')}") print("Code:") print(snippet['code']) def search_snippets(keyword): snippets = load_snippets() results = {title: s for title, s in snippets.items() if keyword.lower() in title.lower() or keyword.lower() in s.get("description", "").lower()} if not results: print(f"No snippets found matching '{keyword}'.") return for title in results: print(f"- {title}") def main(): while True: print("nCode Snippet Collector") print("1. Add snippet") print("2. List snippets") print("3. Get snippet by title") print("4. Search snippets") print("5. Exit") choice = input("Choose an option: ").strip() if choice == '1': title = input("Enter snippet title: ").strip() description = input("Enter description (optional): ").strip() print("Enter your code (end with a line containing only 'END'):") lines = [] while True: line = input() if line == "END": break lines.append(line) code = "n".join(lines) add_snippet(title, code, description) elif choice == '2': list_snippets() elif choice == '3': title = input("Enter snippet title to retrieve: ").strip() get_snippet(title) elif choice == '4': keyword = input("Enter keyword to search: ").strip() search_snippets(keyword) elif choice == '5': print("Goodbye!") break else: print("Invalid choice. Try again.") if __name__ == "__main__": main()

How it works:

  • Snippets are stored in snippets.json.

  • You can add snippets with a title, description, and multiline code.

  • List all snippet titles.

  • Retrieve full snippet by title.

  • Search snippets by keyword in title or description.

Let me know if you want a web version or integration with databases!

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About