|
17 | 17 | DataFrames from this package. |
18 | 18 | """ |
19 | 19 |
|
20 | | -from __future__ import annotations |
21 | | - |
22 | | -import copy |
23 | | -from dataclasses import dataclass, field |
24 | | -import threading |
25 | | -from typing import Optional |
26 | | - |
27 | | -import bigframes_vendored.pandas._config.config as pandas_config |
28 | | - |
29 | | -import bigframes._config.bigquery_options as bigquery_options |
30 | | -import bigframes._config.compute_options as compute_options |
31 | | -import bigframes._config.display_options as display_options |
32 | | -import bigframes._config.experiment_options as experiment_options |
33 | | -import bigframes._config.sampling_options as sampling_options |
34 | | - |
35 | | - |
36 | | -@dataclass |
37 | | -class ThreadLocalConfig(threading.local): |
38 | | - # If unset, global settings will be used |
39 | | - bigquery_options: Optional[bigquery_options.BigQueryOptions] = None |
40 | | - # Note: use default factory instead of default instance so each thread initializes to default values |
41 | | - display_options: display_options.DisplayOptions = field( |
42 | | - default_factory=display_options.DisplayOptions |
43 | | - ) |
44 | | - sampling_options: sampling_options.SamplingOptions = field( |
45 | | - default_factory=sampling_options.SamplingOptions |
46 | | - ) |
47 | | - compute_options: compute_options.ComputeOptions = field( |
48 | | - default_factory=compute_options.ComputeOptions |
49 | | - ) |
50 | | - experiment_options: experiment_options.ExperimentOptions = field( |
51 | | - default_factory=experiment_options.ExperimentOptions |
52 | | - ) |
53 | | - |
54 | | - |
55 | | -class Options: |
56 | | - """Global options affecting BigQuery DataFrames behavior.""" |
57 | | - |
58 | | - def __init__(self): |
59 | | - self.reset() |
60 | | - |
61 | | - def reset(self) -> Options: |
62 | | - """Reset the option settings to defaults. |
63 | | -
|
64 | | - Returns: |
65 | | - bigframes._config.Options: Options object with default values. |
66 | | - """ |
67 | | - self._local = ThreadLocalConfig() |
68 | | - |
69 | | - # BigQuery options are special because they can only be set once per |
70 | | - # session, so we need an indicator as to whether we are using the |
71 | | - # thread-local session or the global session. |
72 | | - self._bigquery_options = bigquery_options.BigQueryOptions() |
73 | | - return self |
74 | | - |
75 | | - def _init_bigquery_thread_local(self): |
76 | | - """Initialize thread-local options, based on current global options.""" |
77 | | - |
78 | | - # Already thread-local, so don't reset any options that have been set |
79 | | - # already. No locks needed since this only modifies thread-local |
80 | | - # variables. |
81 | | - if self._local.bigquery_options is not None: |
82 | | - return |
83 | | - |
84 | | - self._local.bigquery_options = copy.deepcopy(self._bigquery_options) |
85 | | - self._local.bigquery_options._session_started = False |
86 | | - |
87 | | - @property |
88 | | - def bigquery(self) -> bigquery_options.BigQueryOptions: |
89 | | - """Options to use with the BigQuery engine. |
90 | | -
|
91 | | - Returns: |
92 | | - bigframes._config.bigquery_options.BigQueryOptions: |
93 | | - Options for BigQuery engine. |
94 | | - """ |
95 | | - if self._local.bigquery_options is not None: |
96 | | - # The only way we can get here is if someone called |
97 | | - # _init_bigquery_thread_local. |
98 | | - return self._local.bigquery_options |
99 | | - |
100 | | - return self._bigquery_options |
101 | | - |
102 | | - @property |
103 | | - def display(self) -> display_options.DisplayOptions: |
104 | | - """Options controlling object representation. |
105 | | -
|
106 | | - Returns: |
107 | | - bigframes._config.display_options.DisplayOptions: |
108 | | - Options for controlling object representation. |
109 | | - """ |
110 | | - return self._local.display_options |
111 | | - |
112 | | - @property |
113 | | - def sampling(self) -> sampling_options.SamplingOptions: |
114 | | - """Options controlling downsampling when downloading data |
115 | | - to memory. |
116 | | -
|
117 | | - The data can be downloaded into memory explicitly |
118 | | - (e.g., to_pandas, to_numpy, values) or implicitly (e.g., |
119 | | - matplotlib plotting). This option can be overridden by |
120 | | - parameters in specific functions. |
121 | | -
|
122 | | - Returns: |
123 | | - bigframes._config.sampling_options.SamplingOptions: |
124 | | - Options for controlling downsampling. |
125 | | - """ |
126 | | - return self._local.sampling_options |
127 | | - |
128 | | - @property |
129 | | - def compute(self) -> compute_options.ComputeOptions: |
130 | | - """Thread-local options controlling object computation. |
131 | | -
|
132 | | - Returns: |
133 | | - bigframes._config.compute_options.ComputeOptions: |
134 | | - Thread-local options for controlling object computation |
135 | | - """ |
136 | | - return self._local.compute_options |
137 | | - |
138 | | - @property |
139 | | - def experiments(self) -> experiment_options.ExperimentOptions: |
140 | | - """Options controlling experiments |
141 | | -
|
142 | | - Returns: |
143 | | - bigframes._config.experiment_options.ExperimentOptions: |
144 | | - Thread-local options for controlling experiments |
145 | | - """ |
146 | | - return self._local.experiment_options |
147 | | - |
148 | | - @property |
149 | | - def is_bigquery_thread_local(self) -> bool: |
150 | | - """Indicator that we're using a thread-local session. |
151 | | -
|
152 | | - A thread-local session can be started by using |
153 | | - `with bigframes.option_context("bigquery.some_option", "some-value"):`. |
154 | | -
|
155 | | - Returns: |
156 | | - bool: |
157 | | - A boolean value, where a value is True if a thread-local session |
158 | | - is in use; otherwise False. |
159 | | - """ |
160 | | - return self._local.bigquery_options is not None |
161 | | - |
162 | | - @property |
163 | | - def _allow_large_results(self) -> bool: |
164 | | - """The effective 'allow_large_results' setting. |
165 | | -
|
166 | | - This value is `self.compute.allow_large_results` if set (not `None`), |
167 | | - otherwise it defaults to `self.bigquery.allow_large_results`. |
168 | | -
|
169 | | - Returns: |
170 | | - bool: |
171 | | - Whether large query results are permitted. |
172 | | - - `True`: The BigQuery result size limit (e.g., 10 GB) is removed. |
173 | | - - `False`: Results are restricted to this limit (potentially faster). |
174 | | - BigQuery will raise an error if this limit is exceeded. |
175 | | - """ |
176 | | - if self.compute.allow_large_results is None: |
177 | | - return self.bigquery.allow_large_results |
178 | | - return self.compute.allow_large_results |
179 | | - |
180 | | - |
181 | | -options = Options() |
182 | | -"""Global options for default session.""" |
183 | | - |
184 | | -option_context = pandas_config.option_context |
| 20 | +from bigframes._config.bigquery_options import BigQueryOptions |
| 21 | +from bigframes._config.compute_options import ComputeOptions |
| 22 | +from bigframes._config.display_options import DisplayOptions |
| 23 | +from bigframes._config.experiment_options import ExperimentOptions |
| 24 | +from bigframes._config.global_options import option_context, Options |
| 25 | +import bigframes._config.global_options as global_options |
| 26 | +from bigframes._config.sampling_options import SamplingOptions |
185 | 27 |
|
| 28 | +options = global_options.options |
| 29 | +"""Global options for the default session.""" |
186 | 30 |
|
187 | 31 | __all__ = ( |
188 | 32 | "Options", |
189 | 33 | "options", |
190 | 34 | "option_context", |
| 35 | + "BigQueryOptions", |
| 36 | + "ComputeOptions", |
| 37 | + "DisplayOptions", |
| 38 | + "ExperimentOptions", |
| 39 | + "SamplingOptions", |
191 | 40 | ) |
0 commit comments