<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://groupkos.com/dev/index.php?action=history&amp;feed=atom&amp;title=Simple_Python_Chat_Agency_with_File_Load%2FSave</id>
	<title>Simple Python Chat Agency with File Load/Save - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://groupkos.com/dev/index.php?action=history&amp;feed=atom&amp;title=Simple_Python_Chat_Agency_with_File_Load%2FSave"/>
	<link rel="alternate" type="text/html" href="http://groupkos.com/dev/index.php?title=Simple_Python_Chat_Agency_with_File_Load/Save&amp;action=history"/>
	<updated>2026-04-19T08:19:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>http://groupkos.com/dev/index.php?title=Simple_Python_Chat_Agency_with_File_Load/Save&amp;diff=4268&amp;oldid=prev</id>
		<title>XenoEngineer: Created page with &quot;{{menuSimplePythonChatAgency}} &lt;pre style=&quot;margin-left:3em; font:normal 14px terminal;&quot;&gt;import os import json  class ChatAgent:     def __init__(self, name):         self.name = name         self.messages = []      def add_message(self, message):         self.messages.append(message)      def save_to_file(self, file_path):         with open(file_path, &#039;w&#039;) as file:             json.dump(self.__dict__, file)      @staticmethod     def load_from_file(file_path):         wi...&quot;</title>
		<link rel="alternate" type="text/html" href="http://groupkos.com/dev/index.php?title=Simple_Python_Chat_Agency_with_File_Load/Save&amp;diff=4268&amp;oldid=prev"/>
		<updated>2024-10-15T02:23:25Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{menuSimplePythonChatAgency}} &amp;lt;pre style=&amp;quot;margin-left:3em; font:normal 14px terminal;&amp;quot;&amp;gt;import os import json  class ChatAgent:     def __init__(self, name):         self.name = name         self.messages = []      def add_message(self, message):         self.messages.append(message)      def save_to_file(self, file_path):         with open(file_path, &amp;#039;w&amp;#039;) as file:             json.dump(self.__dict__, file)      @staticmethod     def load_from_file(file_path):         wi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{menuSimplePythonChatAgency}}&lt;br /&gt;
&amp;lt;pre style=&amp;quot;margin-left:3em; font:normal 14px terminal;&amp;quot;&amp;gt;import os&lt;br /&gt;
import json&lt;br /&gt;
&lt;br /&gt;
class ChatAgent:&lt;br /&gt;
    def __init__(self, name):&lt;br /&gt;
        self.name = name&lt;br /&gt;
        self.messages = []&lt;br /&gt;
&lt;br /&gt;
    def add_message(self, message):&lt;br /&gt;
        self.messages.append(message)&lt;br /&gt;
&lt;br /&gt;
    def save_to_file(self, file_path):&lt;br /&gt;
        with open(file_path, &amp;#039;w&amp;#039;) as file:&lt;br /&gt;
            json.dump(self.__dict__, file)&lt;br /&gt;
&lt;br /&gt;
    @staticmethod&lt;br /&gt;
    def load_from_file(file_path):&lt;br /&gt;
        with open(file_path, &amp;#039;r&amp;#039;) as file:&lt;br /&gt;
            data = json.load(file)&lt;br /&gt;
            agent = ChatAgent(data[&amp;#039;name&amp;#039;])&lt;br /&gt;
            agent.messages = data[&amp;#039;messages&amp;#039;]&lt;br /&gt;
            return agent&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class ChatAgency:&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        self.agents = []&lt;br /&gt;
&lt;br /&gt;
    def add_agent(self, agent):&lt;br /&gt;
        self.agents.append(agent)&lt;br /&gt;
&lt;br /&gt;
    def remove_agent(self, agent):&lt;br /&gt;
        self.agents.remove(agent)&lt;br /&gt;
&lt;br /&gt;
    def find_agent_by_name(self, name):&lt;br /&gt;
        for agent in self.agents:&lt;br /&gt;
            if agent.name == name:&lt;br /&gt;
                return agent&lt;br /&gt;
        return None&lt;br /&gt;
&lt;br /&gt;
    def save_to_file(self, file_path):&lt;br /&gt;
        with open(file_path, &amp;#039;w&amp;#039;) as file:&lt;br /&gt;
            data = [agent.__dict__ for agent in self.agents]&lt;br /&gt;
            json.dump(data, file)&lt;br /&gt;
&lt;br /&gt;
    @staticmethod&lt;br /&gt;
    def load_from_file(file_path):&lt;br /&gt;
        with open(file_path, &amp;#039;r&amp;#039;) as file:&lt;br /&gt;
            data = json.load(file)&lt;br /&gt;
            agency = ChatAgency()&lt;br /&gt;
            for agent_data in data:&lt;br /&gt;
                agent = ChatAgent(agent_data[&amp;#039;name&amp;#039;])&lt;br /&gt;
                agent.messages = agent_data[&amp;#039;messages&amp;#039;]&lt;br /&gt;
                agency.add_agent(agent)&lt;br /&gt;
            return agency&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    agency = ChatAgency()&lt;br /&gt;
&lt;br /&gt;
    while True:&lt;br /&gt;
        print(&amp;quot;1. Add agent&amp;quot;)&lt;br /&gt;
        print(&amp;quot;2. Remove agent&amp;quot;)&lt;br /&gt;
        print(&amp;quot;3. Add message to agent&amp;quot;)&lt;br /&gt;
        print(&amp;quot;4. Display agent messages&amp;quot;)&lt;br /&gt;
        print(&amp;quot;5. Save agency to file&amp;quot;)&lt;br /&gt;
        print(&amp;quot;6. Load agency from file&amp;quot;)&lt;br /&gt;
        print(&amp;quot;7. Exit&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        choice = input(&amp;quot;Enter your choice: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        if choice == &amp;#039;1&amp;#039;:&lt;br /&gt;
            name = input(&amp;quot;Enter agent name: &amp;quot;)&lt;br /&gt;
            agent = ChatAgent(name)&lt;br /&gt;
            agency.add_agent(agent)&lt;br /&gt;
            print(f&amp;quot;Agent {name} added successfully.&amp;quot;)&lt;br /&gt;
        elif choice == &amp;#039;2&amp;#039;:&lt;br /&gt;
            name = input(&amp;quot;Enter agent name: &amp;quot;)&lt;br /&gt;
            agent = agency.find_agent_by_name(name)&lt;br /&gt;
            if agent:&lt;br /&gt;
                agency.remove_agent(agent)&lt;br /&gt;
                print(f&amp;quot;Agent {name} removed successfully.&amp;quot;)&lt;br /&gt;
            else:&lt;br /&gt;
                print(f&amp;quot;Agent {name} not found.&amp;quot;)&lt;br /&gt;
        elif choice == &amp;#039;3&amp;#039;:&lt;br /&gt;
            name = input(&amp;quot;Enter agent name: &amp;quot;)&lt;br /&gt;
            agent = agency.find_agent_by_name(name)&lt;br /&gt;
            if agent:&lt;br /&gt;
                message = input(&amp;quot;Enter message: &amp;quot;)&lt;br /&gt;
                agent.add_message(message)&lt;br /&gt;
                print(f&amp;quot;Message added to agent {name} successfully.&amp;quot;)&lt;br /&gt;
            else:&lt;br /&gt;
                print(f&amp;quot;Agent {name} not found.&amp;quot;)&lt;br /&gt;
        elif choice == &amp;#039;4&amp;#039;:&lt;br /&gt;
            name = input(&amp;quot;Enter agent name: &amp;quot;)&lt;br /&gt;
            agent = agency.find_agent_by_name(name)&lt;br /&gt;
            if agent:&lt;br /&gt;
                print(f&amp;quot;Messages for agent {name}:&amp;quot;)&lt;br /&gt;
                for message in agent.messages:&lt;br /&gt;
                    print(message)&lt;br /&gt;
            else:&lt;br /&gt;
                print(f&amp;quot;Agent {name} not found.&amp;quot;)&lt;br /&gt;
        elif choice == &amp;#039;5&amp;#039;:&lt;br /&gt;
            file_path = input(&amp;quot;Enter file path: &amp;quot;)&lt;br /&gt;
            agency.save_to_file(file_path)&lt;br /&gt;
            print(f&amp;quot;Agency saved to {file_path} successfully.&amp;quot;)&lt;br /&gt;
        elif choice == &amp;#039;6&amp;#039;:&lt;br /&gt;
            file_path = input(&amp;quot;Enter file path: &amp;quot;)&lt;br /&gt;
            if os.path.exists(file_path):&lt;br /&gt;
                agency = ChatAgency.load_from_file(file_path)&lt;br /&gt;
                print(f&amp;quot;Agency loaded from {file_path} successfully.&amp;quot;)&lt;br /&gt;
            else:&lt;br /&gt;
                print(f&amp;quot;File {file_path} not found.&amp;quot;)&lt;br /&gt;
        elif choice == &amp;#039;7&amp;#039;:&lt;br /&gt;
            print(&amp;quot;Exiting...&amp;quot;)&lt;br /&gt;
            break&lt;br /&gt;
        else:&lt;br /&gt;
            print(&amp;quot;Invalid choice. Please try again.&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>XenoEngineer</name></author>
	</entry>
</feed>