-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment_helper.py
143 lines (104 loc) · 3.21 KB
/
environment_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Enviroment Variables helper
--------------------------
This module defines variables based on the Current Working Directory (CWD), to
find out if we are in a "kaggle" or a "local" environment.
"""
import os
__author__ = "Lucas Hohmann"
__email__ = "[email protected]"
__user__ = "@lfhohmann"
__status__ = "Production"
__date__ = "2022/03/06"
__version__ = "11.0.0"
__license__ = "MIT"
class Environment:
"""
Enviroment variables class
--------------------------
Gets the Current Working Directory (CWD) and sets the `mode` and `dirpath`
variable attributes accordingly. More attributes can be set, by passing a
dictionary with the attributes and their values for each mode (kaggle or
local).
"""
def __init__(self, data: dict = {}) -> None:
"""
Init environment variables
--------------------------
Initializes the environment variables based on the Current Working
Directory (CWD).
Parameters
----------
#### data : dict (required)
Dictionary containing the environment variables inside the keys:
("kaggle", "local").
>>> {
... "kaggle": {"token": 123},
... "local": {"token": 321},
... }
Returns
-------
>>> None
"""
# Set the main attributes for "kaggle" mode
if "kaggle" in os.getcwd():
self.__mode = "kaggle"
self.__dirpath = "/kaggle/working/"
# Set the attributes for "kaggle" mode
if data and "kaggle" in data:
self.__set_data_attr(data["kaggle"])
# Set the main attributes for "local" mode
else:
self.__mode = "local"
self.__dirpath = ".\\data\\"
# Set the attributes for "local" mode
if data and "local" in data:
self.__set_data_attr(data["local"])
def __set_data_attr(self, data: dict) -> None:
"""
Set attributes
--------------
Sets each attribute of the class with the key-value pair from the data
dictionary.
Parameters
----------
#### data : dict (required)
The dictionary passed to it from the constructor (__init__) function
Returns
-------
>>> None
"""
# Iterate over the passed data
for attribute, value in data.items():
# Set the attribute
setattr(self, attribute, value)
@property
def mode(self) -> str:
"""
"mode" property
--------------
Makes sure that the "mode" attribute is read-only.
Returns
-------
#### str:
A string containing the environment "mode"
>>> "kaggle"
>>> "local"
"""
return self.__mode
@property
def dirpath(self) -> str:
"""
"dirpath" property
--------------
Makes sure that the "dirpath" attribute is read-only.
Returns
-------
#### str:
A string containing the environment "dirpath"
>>> ".//data//"
>>> "/kaggle/working/"
"""
return self.__dirpath