17
17
DataFrames from this package.
18
18
"""
19
19
20
+ from __future__ import annotations
21
+
20
22
import copy
23
+ from dataclasses import dataclass , field
21
24
import threading
25
+ from typing import Optional
22
26
23
27
import bigframes_vendored .pandas ._config .config as pandas_config
24
28
28
32
import bigframes ._config .sampling_options as sampling_options
29
33
30
34
35
+ @dataclass
36
+ class ThreadLocalConfig (threading .local ):
37
+ # If unset, global settings will be used
38
+ bigquery_options : Optional [bigquery_options .BigQueryOptions ] = None
39
+ # Note: use default factory instead of default instance so each thread initializes to default values
40
+ display_options : display_options .DisplayOptions = field (
41
+ default_factory = display_options .DisplayOptions
42
+ )
43
+ sampling_options : sampling_options .SamplingOptions = field (
44
+ default_factory = sampling_options .SamplingOptions
45
+ )
46
+ compute_options : compute_options .ComputeOptions = field (
47
+ default_factory = compute_options .ComputeOptions
48
+ )
49
+
50
+
31
51
class Options :
32
52
"""Global options affecting BigQuery DataFrames behavior."""
33
53
34
54
def __init__ (self ):
35
- self ._local = threading .local ()
36
-
37
- # Initialize these in the property getters to make sure we do have a
38
- # separate instance per thread.
39
- self ._local .bigquery_options = None
40
- self ._local .display_options = None
41
- self ._local .sampling_options = None
42
- self ._local .compute_options = None
55
+ self ._local = ThreadLocalConfig ()
43
56
44
57
# BigQuery options are special because they can only be set once per
45
58
# session, so we need an indicator as to whether we are using the
@@ -61,21 +74,16 @@ def _init_bigquery_thread_local(self):
61
74
@property
62
75
def bigquery (self ) -> bigquery_options .BigQueryOptions :
63
76
"""Options to use with the BigQuery engine."""
64
- if (
65
- bigquery_options := getattr (self ._local , "bigquery_options" , None )
66
- ) is not None :
77
+ if self ._local .bigquery_options is not None :
67
78
# The only way we can get here is if someone called
68
79
# _init_bigquery_thread_local.
69
- return bigquery_options
80
+ return self . _local . bigquery_options
70
81
71
82
return self ._bigquery_options
72
83
73
84
@property
74
85
def display (self ) -> display_options .DisplayOptions :
75
86
"""Options controlling object representation."""
76
- if self ._local .display_options is None :
77
- self ._local .display_options = display_options .DisplayOptions ()
78
-
79
87
return self ._local .display_options
80
88
81
89
@property
@@ -88,17 +96,11 @@ def sampling(self) -> sampling_options.SamplingOptions:
88
96
matplotlib plotting). This option can be overriden by
89
97
parameters in specific functions.
90
98
"""
91
- if self ._local .sampling_options is None :
92
- self ._local .sampling_options = sampling_options .SamplingOptions ()
93
-
94
99
return self ._local .sampling_options
95
100
96
101
@property
97
102
def compute (self ) -> compute_options .ComputeOptions :
98
103
"""Thread-local options controlling object computation."""
99
- if self ._local .compute_options is None :
100
- self ._local .compute_options = compute_options .ComputeOptions ()
101
-
102
104
return self ._local .compute_options
103
105
104
106
@property
0 commit comments