|
22 | 22 | from enum import Enum |
23 | 23 | from io import DEFAULT_BUFFER_SIZE |
24 | 24 | from textwrap import fill |
25 | | -from typing import Dict, Optional, Tuple, Union |
| 25 | +from typing import Any, Dict, Optional, Tuple, Union |
26 | 26 | from urllib import request |
27 | 27 | from urllib.parse import urlparse |
28 | 28 |
|
|
63 | 63 | # Maximum distinct values by cardinality will be used for plotting |
64 | 64 | MAX_DISPLAY_VALUES = 10 |
65 | 65 |
|
| 66 | +UNKNOWN = "" |
| 67 | + |
66 | 68 | # par link of the index json file. |
67 | 69 | PAR_LINK = "https://objectstorage.us-ashburn-1.oraclecloud.com/p/WyjtfVIG0uda-P3-2FmAfwaLlXYQZbvPZmfX1qg0-sbkwEQO6jpwabGr2hMDBmBp/n/ociodscdev/b/service-conda-packs/o/service_pack/index.json" |
68 | 70 |
|
|
82 | 84 | color=["teal", "blueviolet", "forestgreen", "peru", "y", "dodgerblue", "r"] |
83 | 85 | ) |
84 | 86 |
|
| 87 | + |
85 | 88 | # sqlalchemy engines |
86 | 89 | _engines = {} |
87 | 90 |
|
@@ -149,6 +152,22 @@ def oci_key_location(): |
149 | 152 | ) |
150 | 153 |
|
151 | 154 |
|
| 155 | +def text_sanitizer(content): |
| 156 | + if isinstance(content, str): |
| 157 | + return ( |
| 158 | + content.replace("“", '"') |
| 159 | + .replace("”", '"') |
| 160 | + .replace("’", "'") |
| 161 | + .replace("‘", "'") |
| 162 | + .replace("—", "-") |
| 163 | + .encode("utf-8", "ignore") |
| 164 | + .decode("utf-8", "ignore") |
| 165 | + ) |
| 166 | + if isinstance(content, dict): |
| 167 | + return json.dumps(content) |
| 168 | + return str(content) |
| 169 | + |
| 170 | + |
152 | 171 | @deprecated( |
153 | 172 | "2.5.10", |
154 | 173 | details="Deprecated, use: from ads.common.auth import AuthState; AuthState().oci_config_path", |
@@ -212,6 +231,37 @@ def random_valid_ocid(prefix="ocid1.dataflowapplication.oc1.iad"): |
212 | 231 | return f"{left}.{fake}" |
213 | 232 |
|
214 | 233 |
|
| 234 | +def parse_bool(value: Any) -> bool: |
| 235 | + """ |
| 236 | + Converts a value to boolean. For strings, it interprets 'true', '1', or 'yes' |
| 237 | + (case insensitive) as True; everything else as False. |
| 238 | +
|
| 239 | + Parameters |
| 240 | + ---------- |
| 241 | + value : Any |
| 242 | + The value to convert to boolean. |
| 243 | +
|
| 244 | + Returns |
| 245 | + ------- |
| 246 | + bool |
| 247 | + The boolean interpretation of the value. |
| 248 | + """ |
| 249 | + if isinstance(value, bool): |
| 250 | + return value |
| 251 | + if isinstance(value, str): |
| 252 | + return value.strip().lower() in ("true", "1", "yes") |
| 253 | + return bool(value) |
| 254 | + |
| 255 | + |
| 256 | +def read_file(file_path: str, **kwargs) -> str: |
| 257 | + try: |
| 258 | + with fsspec.open(file_path, "r", **kwargs.get("auth", {})) as f: |
| 259 | + return f.read() |
| 260 | + except Exception as e: |
| 261 | + logger.debug(f"Failed to read file {file_path}. {e}") |
| 262 | + return UNKNOWN |
| 263 | + |
| 264 | + |
215 | 265 | def get_dataframe_styles(max_width=75): |
216 | 266 | """Styles used for dataframe, example usage: |
217 | 267 |
|
|
0 commit comments