Skip to content

Commit 4fd0e8c

Browse files
committed
refactor: make Config and Credentials read-only
1 parent 5b85e82 commit 4fd0e8c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

models/const.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
raise FileNotFoundError("No .env file found in root directory of repository")
2828

2929

30-
class Config:
30+
class ReadOnly(type):
31+
"""Metaclass to make all class attributes read-only."""
32+
33+
def __setattr__(cls, name, value):
34+
if name in cls.__dict__:
35+
raise TypeError(f"Cannot change a read-only attribute {name}")
36+
super().__setattr__(name, value)
37+
38+
39+
class Config(metaclass=ReadOnly):
3140
"""Configuration parameters."""
3241

3342
DEBUG_MODE: bool = DEBUG_MODE
@@ -41,7 +50,7 @@ class Config:
4150
PINECONE_VECTORSTORE_TEXT_KEY: str = PINECONE_VECTORSTORE_TEXT_KEY
4251

4352

44-
class Credentials:
53+
class Credentials(metaclass=ReadOnly):
4554
"""Credentials."""
4655

4756
OPENAI_API_KEY = OPENAI_API_KEY

models/yt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
# 8.) LangChain agents
3838
from langchain_experimental.agents.agent_toolkits.python.base import create_python_agent
3939

40+
from models.const import Config, Credentials
41+
4042

4143
# Load environment variables from .env file in all folders
4244
# pylint: disable=duplicate-code
@@ -45,8 +47,6 @@
4547
load_dotenv(dotenv_path=dotenv_path, verbose=True)
4648
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
4749
OPENAI_API_ORGANIZATION = os.environ["OPENAI_API_ORGANIZATION"]
48-
PINECONE_API_KEY = os.environ["PINECONE_API_KEY"]
49-
PINECONE_ENVIRONMENT = os.environ["PINECONE_ENVIRONMENT"]
5050
else:
5151
raise FileNotFoundError("No .env file found in root directory of repository")
5252

@@ -66,7 +66,7 @@ class LangChainDev:
6666
tool=PythonREPL(),
6767
verbose=True,
6868
)
69-
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENVIRONMENT) # minute 10:43
69+
pinecone.init(api_key=Credentials.PINECONE_API_KEY, environment=Config.PINECONE_ENVIRONMENT) # minute 10:43
7070

7171
# LLM wrappers. minute 5:46
7272
def test_01_basic(self):

0 commit comments

Comments
 (0)