General Utilities
This module provides various utility functions used throughout the NetSecGame framework.
netsecgame.utils.utils
generate_valid_actions
Function that generates a list of all valid actions in a given GameState Args: state (GameState): The current game state. include_blocks (bool): Whether to include BlockIP actions. Defaults to False.
Returns:
| Name | Type | Description |
|---|---|---|
set |
Set[Action]
|
A set of valid Action objects. |
Source code in netsecgame/utils/utils.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
get_file_hash
Computes hash of a given file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
The path to the file to hash. |
required |
hash_func
|
str
|
The hash function to use (default is 'sha256'). |
'sha256'
|
chunk_size
|
int
|
The size of each chunk to read from the file (default is 4096 bytes). |
4096
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The hexadecimal hash of the file. |
Source code in netsecgame/utils/utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
get_logging_level
Configures the logging level based on the provided debug_level string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
debug_level
|
str
|
The level name (e.g., 'DEBUG', 'INFO'). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The corresponding logging level constant. |
Source code in netsecgame/utils/utils.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
get_str_hash
Computes hash of a given string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The input string to hash. |
required |
hash_func
|
str
|
The hash function to use (default is 'sha256'). |
'sha256'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The hexadecimal hash of the input string. |
Source code in netsecgame/utils/utils.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
observation_as_dict
Generates a dictionary representation of a given Observation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation
|
Observation
|
The observation object to convert. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: The dictionary representation of the observation. |
Source code in netsecgame/utils/utils.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
observation_from_dict
Reconstructs an Observation object from a dictionary representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
The dictionary containing observation data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Observation |
Observation
|
The reconstructed Observation namedtuple. |
Source code in netsecgame/utils/utils.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
observation_from_str
Reconstructs an Observation object from a JSON string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_str
|
str
|
The JSON string representation of the observation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Observation |
Observation
|
The reconstructed Observation namedtuple. |
Source code in netsecgame/utils/utils.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
observation_to_str
Generates a JSON string representation of a given Observation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation
|
Observation
|
The observation object to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The JSON string representation. |
Source code in netsecgame/utils/utils.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
parse_log_content
Parses a JSON string of log content into a list of log entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_content
|
str
|
The raw JSON log content. |
required |
Returns:
| Type | Description |
|---|---|
Optional[List[Dict[str, Any]]]
|
Optional[List[Dict[str, Any]]]: A list of log entries if successful, None otherwise. |
Source code in netsecgame/utils/utils.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
read_replay_buffer_from_csv
Reads steps from a CSV file and restores objects for the replay buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvfile
|
str
|
Path to the CSV file. |
required |
Returns:
| Type | Description |
|---|---|
List[Tuple[GameState, Action, float, GameState, bool]]
|
List[Tuple[GameState, Action, float, GameState, bool]]: The restored replay buffer. |
Source code in netsecgame/utils/utils.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
read_trajectories_from_jsonl
Reads trajectories from a JSONL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the JSONL file. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: A list of trajectories read from the file. |
Source code in netsecgame/utils/utils.py
283 284 285 286 287 288 289 290 291 292 293 | |
state_as_ordered_string
Converts a GameState into a deterministic ordered string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
GameState
|
The game state to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The ordered string representation of the state. |
Source code in netsecgame/utils/utils.py
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 | |
store_replay_buffer_in_csv
Stores steps from a replay buffer into a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replay_buffer
|
List[Tuple[GameState, Action, float, GameState, bool]]
|
The buffer items to store. |
required |
filename
|
str
|
The name of the output file. |
required |
delimiter
|
str
|
The delimiter to use in the CSV (default is ';'). |
';'
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in netsecgame/utils/utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
store_trajectories_to_jsonl
Stores trajectories to a JSONL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trajectories
|
Any
|
The trajectory data to store (usually a dict or list). |
required |
dir
|
str
|
Directory where the file will be stored. |
required |
filename
|
str
|
Name of the file (without extension). |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in netsecgame/utils/utils.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |