Skip to content

Commit 5528d77

Browse files
committed
Cover spark_singleton.py
1 parent 6029144 commit 5528d77

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

dbldatagen/spark_singleton.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,39 @@
99
for use cases like unit testing rather than in a Databricks workspace environment
1010
"""
1111

12-
import os
1312
import logging
13+
import os
14+
1415
from pyspark.sql import SparkSession
1516

1617

1718
class SparkSingleton:
1819
"""A singleton class which returns one Spark session instance"""
1920

2021
@classmethod
21-
def getInstance(cls):
22-
"""Create a Spark instance for Datalib.
22+
def getInstance(cls) -> SparkSession:
23+
"""Creates a `SparkSession` instance for Datalib.
2324
2425
:returns: A Spark instance
2526
"""
2627

2728
return SparkSession.builder.getOrCreate()
2829

2930
@classmethod
30-
def getLocalInstance(cls, appName="new Spark session", useAllCores=True):
31-
"""Create a machine local Spark instance for Datalib.
31+
def getLocalInstance(cls, appName: str = "new Spark session", useAllCores: bool = True) -> SparkSession:
32+
"""Creates a machine local `SparkSession` instance for Datalib.
3233
By default, it uses `n-1` cores of the available cores for the spark session,
3334
where `n` is total cores available.
3435
36+
:param appName: Name to use for the local `SparkSession` instance
3537
:param useAllCores: If `useAllCores` is True, then use all cores rather than `n-1` cores
36-
:returns: A Spark instance
38+
:returns: A `SparkSession` instance
3739
"""
3840
cpu_count = os.cpu_count()
3941

40-
if useAllCores:
42+
if not cpu_count:
43+
spark_core_count = 1
44+
elif useAllCores:
4145
spark_core_count = cpu_count
4246
else:
4347
spark_core_count = cpu_count - 1

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ exclude = [
164164
"dbldatagen/nrange.py",
165165
"dbldatagen/schema_parser.py",
166166
"dbldatagen/serialization.py",
167-
"dbldatagen/spark_singleton.py",
168167
"dbldatagen/text_generator_plugins.py",
169168
"dbldatagen/text_generators.py",
170169
"dbldatagen/utils.py"
@@ -250,7 +249,6 @@ ignore = [
250249
"dbldatagen/nrange.py",
251250
"dbldatagen/schema_parser.py",
252251
"dbldatagen/serialization.py",
253-
"dbldatagen/spark_singleton.py",
254252
"dbldatagen/text_generator_plugins.py",
255253
"dbldatagen/text_generators.py",
256254
"dbldatagen/utils.py"
@@ -291,7 +289,6 @@ ignore-paths = [
291289
"dbldatagen/nrange.py",
292290
"dbldatagen/schema_parser.py",
293291
"dbldatagen/serialization.py",
294-
"dbldatagen/spark_singleton.py",
295292
"dbldatagen/text_generator_plugins.py",
296293
"dbldatagen/text_generators.py",
297294
"dbldatagen/utils.py"
@@ -427,7 +424,6 @@ exclude = [
427424
"dbldatagen/nrange.py",
428425
"dbldatagen/schema_parser.py",
429426
"dbldatagen/serialization.py",
430-
"dbldatagen/spark_singleton.py",
431427
"dbldatagen/text_generator_plugins.py",
432428
"dbldatagen/text_generators.py",
433429
"dbldatagen/utils.py"

0 commit comments

Comments
 (0)