/
utils.py
882 lines (726 loc) · 27 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership. The Modin Development Team licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
"""Collection of general utility functions, mostly for internal use."""
import codecs
import functools
import importlib
import inspect
import json
import os
import re
import sys
import types
import warnings
from pathlib import Path
from textwrap import dedent, indent
from typing import (
Any,
Callable,
Iterable,
List,
Mapping,
Optional,
Protocol,
TypeVar,
Union,
runtime_checkable,
)
import numpy as np
import pandas
from packaging import version
from pandas._typing import JSONSerializable
from pandas.util._decorators import Appender # type: ignore
from pandas.util._print_versions import ( # type: ignore[attr-defined]
_get_dependency_info,
_get_sys_info,
)
from modin._version import get_versions
from modin.config import DocModule, Engine, StorageFormat
T = TypeVar("T")
"""Generic type parameter"""
Fn = TypeVar("Fn", bound=Callable)
"""Function type parameter (used in decorators that don't change a function's signature)"""
@runtime_checkable
class SupportsPublicToPandas(Protocol): # noqa: PR01
"""Structural type for objects with a ``to_pandas`` method (without a leading underscore)."""
def to_pandas(self) -> Any: # noqa: GL08
pass
@runtime_checkable
class SupportsPublicToNumPy(Protocol): # noqa: PR01
"""Structural type for objects with a ``to_numpy`` method (without a leading underscore)."""
def to_numpy(self) -> Any: # noqa: GL08
pass
@runtime_checkable
class SupportsPrivateToNumPy(Protocol): # noqa: PR01
"""Structural type for objects with a ``_to_numpy`` method (note the leading underscore)."""
def _to_numpy(self) -> Any: # noqa: GL08
pass
MIN_RAY_VERSION = version.parse("1.13.0")
MIN_DASK_VERSION = version.parse("2.22.0")
MIN_UNIDIST_VERSION = version.parse("0.2.1")
PANDAS_API_URL_TEMPLATE = f"https://pandas.pydata.org/pandas-docs/version/{pandas.__version__}/reference/api/{{}}.html"
# The '__reduced__' name is used internally by the query compiler as a column name to
# represent pandas Series objects that are not explicitly assigned a name, so as to
# distinguish between an N-element series and 1xN dataframe.
MODIN_UNNAMED_SERIES_LABEL = "__reduced__"
def _make_api_url(token: str) -> str:
"""
Generate the link to pandas documentation.
Parameters
----------
token : str
Part of URL to use for generation.
Returns
-------
str
URL to pandas doc.
Notes
-----
This function is extracted for better testability.
"""
return PANDAS_API_URL_TEMPLATE.format(token)
def _get_indent(doc: str) -> int:
"""
Compute indentation in docstring.
Parameters
----------
doc : str
The docstring to compute indentation for.
Returns
-------
int
Minimal indent (excluding empty lines).
"""
indents = _get_indents(doc)
return min(indents) if indents else 0
def _get_indents(source: Union[list, str]) -> list:
"""
Compute indentation for each line of the source string.
Parameters
----------
source : str or list of str
String to compute indents for. Passed list considered
as a list of lines of the source string.
Returns
-------
list of ints
List containing computed indents for each line.
"""
indents = []
if not isinstance(source, list):
source = source.splitlines()
for line in source:
if not line.strip():
continue
for pos, ch in enumerate(line):
if ch != " ":
break
indents.append(pos)
return indents
def format_string(template: str, **kwargs: str) -> str:
"""
Insert passed values at the corresponding placeholders of the specified template.
In contrast with the regular ``str.format()`` this function computes proper
indents for the placeholder values.
Parameters
----------
template : str
Template to substitute values in.
**kwargs : dict
Dictionary that maps placeholder names with values.
Returns
-------
str
Formated string.
"""
# We want to change indentation only for those values which placeholders are located
# at the start of the line, in that case the placeholder sets an indentation
# that the filling value has to obey.
# RegExp determining placeholders located at the beginning of the line.
regex = r"^( *)\{(\w+)\}"
for line in template.splitlines():
if line.strip() == "":
continue
match = re.search(regex, line)
if match is None:
continue
nspaces = len(match.group(1))
key = match.group(2)
value = kwargs.get(key)
if not value:
continue
value = dedent(value)
# Since placeholder is located at the beginning of a new line,
# it already has '\n' before it, so to avoid double new lines
# we want to discard the first leading '\n' at the value line,
# the others leading '\n' are considered as being put on purpose
if value[0] == "\n":
value = value[1:]
# `.splitlines()` doesn't preserve last empty line,
# so we have to restore it further
value_lines = value.splitlines()
# We're not indenting the first line of the value, since it's already indented
# properly because of the placeholder indentation.
indented_lines = [
indent(line, " " * nspaces) if line != "\n" else line
for line in value_lines[1:]
]
# If necessary, restoring the last line dropped by `.splitlines()`
if value[-1] == "\n":
indented_lines += [" " * nspaces]
indented_value = "\n".join([value_lines[0], *indented_lines])
kwargs[key] = indented_value
return template.format(**kwargs)
def align_indents(source: str, target: str) -> str:
"""
Align indents of two strings.
Parameters
----------
source : str
Source string to align indents with.
target : str
Target string to align indents.
Returns
-------
str
Target string with indents aligned with the source.
"""
source_indent = _get_indent(source)
target = dedent(target)
return indent(target, " " * source_indent)
def append_to_docstring(message: str) -> Callable[[Fn], Fn]:
"""
Create a decorator which appends passed message to the function's docstring.
Parameters
----------
message : str
Message to append.
Returns
-------
callable
"""
def decorator(func: Fn) -> Fn:
to_append = align_indents(func.__doc__ or "", message)
return Appender(to_append)(func)
return decorator
def _replace_doc(
source_obj: object,
target_obj: object,
overwrite: bool,
apilink: Optional[Union[str, List[str]]],
parent_cls: Optional[Fn] = None,
attr_name: Optional[str] = None,
) -> None:
"""
Replace docstring in `target_obj`, possibly taking from `source_obj` and augmenting.
Can append the link to pandas API online documentation.
Parameters
----------
source_obj : object
Any object from which to take docstring from.
target_obj : object
The object which docstring to replace.
overwrite : bool
Forces replacing the docstring with the one from `source_obj` even
if `target_obj` has its own non-empty docstring.
apilink : str | List[str], optional
If non-empty, insert the link(s) to pandas API documentation.
Should be the prefix part in the URL template, e.g. "pandas.DataFrame".
parent_cls : class, optional
If `target_obj` is an attribute of a class, `parent_cls` should be that class.
This is used for generating the API URL as well as for handling special cases
like `target_obj` being a property.
attr_name : str, optional
Gives the name to `target_obj` if it's an attribute of `parent_cls`.
Needed to handle some special cases and in most cases could be determined automatically.
"""
if isinstance(target_obj, (staticmethod, classmethod)):
# we cannot replace docs on decorated objects, we must replace them
# on original functions instead
target_obj = target_obj.__func__
source_doc = source_obj.__doc__ or ""
target_doc = target_obj.__doc__ or ""
overwrite = overwrite or not target_doc
doc = source_doc if overwrite else target_doc
if parent_cls and not attr_name:
if isinstance(target_obj, property):
attr_name = target_obj.fget.__name__ # type: ignore[union-attr]
elif isinstance(target_obj, (staticmethod, classmethod)):
attr_name = target_obj.__func__.__name__
else:
attr_name = target_obj.__name__ # type: ignore[attr-defined]
if (
source_doc.strip()
and apilink
and "pandas API documentation for " not in target_doc
and (not (attr_name or "").startswith("_"))
):
apilink_l = [apilink] if not isinstance(apilink, list) and apilink else apilink
links = []
for link in apilink_l:
if attr_name:
token = f"{link}.{attr_name}"
else:
token = link
url = _make_api_url(token)
links.append(f"`{token} <{url}>`_")
indent_line = " " * _get_indent(doc)
notes_section = f"\n{indent_line}Notes\n{indent_line}-----\n"
url_line = f"{indent_line}See pandas API documentation for {', '.join(links)} for more.\n"
notes_section_with_url = notes_section + url_line
if notes_section in doc:
doc = doc.replace(notes_section, notes_section_with_url)
else:
doc += notes_section_with_url
if parent_cls and isinstance(target_obj, property):
if overwrite:
target_obj.fget.__doc_inherited__ = True # type: ignore[union-attr]
assert attr_name is not None
setattr(
parent_cls,
attr_name,
property(target_obj.fget, target_obj.fset, target_obj.fdel, doc),
)
else:
if overwrite:
target_obj.__doc_inherited__ = True # type: ignore[attr-defined]
target_obj.__doc__ = doc
def _inherit_docstrings(
parent: object,
excluded: List[object] = [],
overwrite_existing: bool = False,
apilink: Optional[Union[str, List[str]]] = None,
) -> Callable[[Fn], Fn]:
"""
Create a decorator which overwrites decorated object docstring(s).
It takes `parent` __doc__ attribute. Also overwrites __doc__ of
methods and properties defined in the target or its ancestors if it's a class
with the __doc__ of matching methods and properties from the `parent`.
Parameters
----------
parent : object
Parent object from which the decorated object inherits __doc__.
excluded : list, default: []
List of parent objects from which the class does not
inherit docstrings.
overwrite_existing : bool, default: False
Allow overwriting docstrings that already exist in
the decorated class.
apilink : str | List[str], optional
If non-empty, insert the link(s) to pandas API documentation.
Should be the prefix part in the URL template, e.g. "pandas.DataFrame".
Returns
-------
callable
Decorator which replaces the decorated object's documentation with `parent` documentation.
Notes
-----
Keep in mind that the function will override docstrings even for attributes which
are not defined in target class (but are defined in the ancestor class),
which means that ancestor class attribute docstrings could also change.
"""
# Import the docs module and get the class (e.g. `DataFrame`).
imported_doc_module = importlib.import_module(DocModule.get())
# Set the default parent so we can use it in case some docs are missing from
# parent module.
default_parent = parent
# Try to get the parent object from the doc module, and if it isn't there,
# get it from parent instead. We only do this if we are overriding pandas
# documentation. We don't touch other docs.
if DocModule.get() != DocModule.default and "pandas" in str(
getattr(parent, "__module__", "")
):
parent = getattr(imported_doc_module, getattr(parent, "__name__", ""), parent)
if parent != default_parent:
# Reset API link in case the docs are overridden.
apilink = None
overwrite_existing = True
def _documentable_obj(obj: object) -> bool:
"""Check if `obj` docstring could be patched."""
return bool(
callable(obj)
or (isinstance(obj, property) and obj.fget)
or (isinstance(obj, (staticmethod, classmethod)) and obj.__func__)
)
def decorator(cls_or_func: Fn) -> Fn:
if parent not in excluded:
_replace_doc(parent, cls_or_func, overwrite_existing, apilink)
if not isinstance(cls_or_func, types.FunctionType):
seen = set()
for base in cls_or_func.__mro__: # type: ignore[attr-defined]
if base is object:
continue
for attr, obj in base.__dict__.items():
if attr in seen:
continue
seen.add(attr)
# Try to get the attribute from the docs class first, then
# from the default parent (pandas), and if it's not in either,
# set `parent_obj` to `None`.
parent_obj = getattr(
parent, attr, getattr(default_parent, attr, None)
)
if (
parent_obj in excluded
or not _documentable_obj(parent_obj)
or not _documentable_obj(obj)
):
continue
_replace_doc(
parent_obj,
obj,
overwrite_existing,
apilink,
parent_cls=cls_or_func,
attr_name=attr,
)
return cls_or_func
return decorator
def expanduser_path_arg(argname: str) -> Callable[[Fn], Fn]:
"""
Decorate a function replacing its path argument with "user-expanded" value.
Parameters
----------
argname : str
Name of the argument which is containing a path to be expanded.
Returns
-------
callable
Decorator which performs the replacement.
"""
def decorator(func: Fn) -> Fn:
signature = inspect.signature(func)
assert (
getattr(signature.parameters.get(argname), "name", None) == argname
), f"Function {func} does not take '{argname}' as argument"
@functools.wraps(func)
def wrapped(*args: tuple, **kw: dict) -> Any:
params = signature.bind(*args, **kw)
if patharg := params.arguments.get(argname, None):
if isinstance(patharg, str) and patharg.startswith("~"):
params.arguments[argname] = os.path.expanduser(patharg)
elif isinstance(patharg, Path):
params.arguments[argname] = patharg.expanduser()
return func(*params.args, **params.kwargs)
return func(*args, **kw)
return wrapped # type: ignore[return-value]
return decorator
def func_from_deprecated_location(
func_name: str, module: str, deprecation_message: str
) -> Callable:
"""
Create a function that decorates a function ``module.func_name`` with a ``FutureWarning``.
Parameters
----------
func_name : str
Function name to decorate.
module : str
Module where the function is located.
deprecation_message : str
Message to print in a future warning.
Returns
-------
callable
"""
def deprecated_func(*args: tuple[Any], **kwargs: dict[Any, Any]) -> Any:
"""Call deprecated function."""
func = getattr(importlib.import_module(module), func_name)
# using 'FutureWarning' as 'DeprecationWarnings' are filtered out by default
warnings.warn(deprecation_message, FutureWarning)
return func(*args, **kwargs)
return deprecated_func
to_numpy = func_from_deprecated_location(
"to_numpy",
"modin.pandas.io",
"Importing ``to_numpy`` from ``modin.pandas.utils`` is deprecated and will be removed in a future version. "
+ "This function was moved to ``modin.pandas.io``, please import it from there instead.",
)
to_pandas = func_from_deprecated_location(
"to_pandas",
"modin.pandas.io",
"Importing ``to_pandas`` from ``modin.pandas.utils`` is deprecated and will be removed in a future version. "
+ "This function was moved to ``modin.pandas.io``, please import it from there instead.",
)
def hashable(obj: bool) -> bool:
"""
Return whether the `obj` is hashable.
Parameters
----------
obj : object
The object to check.
Returns
-------
bool
"""
# Happy path: if there's no __hash__ method, the object definitely isn't hashable
if not hasattr(obj, "__hash__"):
return False
# Otherwise, we may still need to check for type errors, as in the case of `hash(([],))`.
# (e.g. an unhashable object inside a tuple)
try:
hash(obj)
except TypeError:
return False
return True
def try_cast_to_pandas(obj: Any, squeeze: bool = False) -> Any:
"""
Convert `obj` and all nested objects from Modin to pandas if it is possible.
If no convertion possible return `obj`.
Parameters
----------
obj : object
Object to convert from Modin to pandas.
squeeze : bool, default: False
Squeeze the converted object(s) before returning them.
Returns
-------
object
Converted object.
"""
if isinstance(obj, SupportsPublicToPandas) or hasattr(obj, "modin"):
result = obj.modin.to_pandas() if hasattr(obj, "modin") else obj.to_pandas()
if squeeze:
result = result.squeeze(axis=1)
# QueryCompiler/low-level ModinFrame case, it doesn't have logic about convertion to Series
if (
isinstance(getattr(result, "name", None), str)
and result.name == MODIN_UNNAMED_SERIES_LABEL
):
result.name = None
return result
if isinstance(obj, (list, tuple)):
return type(obj)([try_cast_to_pandas(o, squeeze=squeeze) for o in obj])
if isinstance(obj, dict):
return {k: try_cast_to_pandas(v, squeeze=squeeze) for k, v in obj.items()}
if callable(obj):
module_hierarchy = getattr(obj, "__module__", "").split(".")
fn_name = getattr(obj, "__name__", None)
if fn_name and module_hierarchy[0] == "modin":
return (
getattr(pandas.DataFrame, fn_name, obj)
if module_hierarchy[-1] == "dataframe"
else getattr(pandas.Series, fn_name, obj)
)
return obj
def execute(*objs: Iterable[Any], trigger_hdk_import: bool = False) -> None:
"""
Trigger the lazy computations for each obj in `objs`, if any, and wait for them to complete.
Parameters
----------
*objs : Iterable[Any]
A collection of objects to trigger lazy computations.
trigger_hdk_import : bool, default: False
Trigger import execution. Makes sense only for HDK storage format.
Safe to use with other storage formats.
"""
for obj in objs:
if not hasattr(obj, "_query_compiler"):
continue
query_compiler = obj._query_compiler
query_compiler.execute()
if trigger_hdk_import and hasattr(query_compiler, "force_import"):
query_compiler.force_import()
def wrap_into_list(*args: Any, skipna: bool = True) -> List[Any]:
"""
Wrap a sequence of passed values in a flattened list.
If some value is a list by itself the function appends its values
to the result one by one instead inserting the whole list object.
Parameters
----------
*args : tuple
Objects to wrap into a list.
skipna : bool, default: True
Whether or not to skip nan or None values.
Returns
-------
list
Passed values wrapped in a list.
"""
def isnan(o: Any) -> bool:
return o is None or (isinstance(o, float) and np.isnan(o))
res = []
for o in args:
if skipna and isnan(o):
continue
if isinstance(o, list):
res.extend(o)
else:
res.append(o)
return res
def wrap_udf_function(func: Callable) -> Callable:
"""
Create a decorator that makes `func` return pandas objects instead of Modin.
Parameters
----------
func : callable
Function to wrap.
Returns
-------
callable
"""
def wrapper(*args: Any, **kwargs: Any) -> Any:
result = func(*args, **kwargs)
# if user accidently returns modin DataFrame or Series
# casting it back to pandas to properly process
return try_cast_to_pandas(result)
wrapper.__name__ = func.__name__
return wrapper
def get_current_execution() -> str:
"""
Return current execution name as a string.
Returns
-------
str
Returns <StorageFormat>On<Engine>-like string.
"""
return f"{StorageFormat.get()}On{Engine.get()}"
def instancer(_class: Callable[[], T]) -> T:
"""
Create a dummy instance each time this is imported.
This serves the purpose of allowing us to use all of pandas plotting methods
without aliasing and writing each of them ourselves.
Parameters
----------
_class : object
Returns
-------
object
Instance of `_class`.
"""
return _class()
def import_optional_dependency(name: str, message: str) -> types.ModuleType:
"""
Import an optional dependecy.
Parameters
----------
name : str
The module name.
message : str
Additional text to include in the ImportError message.
Returns
-------
module : ModuleType
The imported module.
"""
try:
return importlib.import_module(name)
except ImportError:
raise ImportError(
f"Missing optional dependency '{name}'. {message} "
+ f"Use pip or conda to install {name}."
) from None
def _get_modin_deps_info() -> Mapping[str, Optional[JSONSerializable]]:
"""
Return Modin-specific dependencies information as a JSON serializable dictionary.
Returns
-------
Mapping[str, Optional[pandas.JSONSerializable]]
The dictionary of Modin dependencies and their versions.
"""
import modin # delayed import so modin.__init__ is fully initialized
result = {"modin": modin.__version__}
for pkg_name, pkg_version in [
("ray", MIN_RAY_VERSION),
("dask", MIN_DASK_VERSION),
("distributed", MIN_DASK_VERSION),
]:
try:
pkg = importlib.import_module(pkg_name)
except ImportError:
result[pkg_name] = None
else:
result[pkg_name] = pkg.__version__ + (
f" (outdated; >={pkg_version} required)"
if version.parse(pkg.__version__) < pkg_version
else ""
)
try:
# We import ``DbWorker`` from this module since correct import of ``DbWorker`` itself
# from HDK is located in it with all the necessary options for dlopen.
from modin.experimental.core.execution.native.implementations.hdk_on_native.db_worker import ( # noqa
DbWorker,
)
result["hdk"] = "present"
except ImportError:
result["hdk"] = None
return result
def show_versions(as_json: Union[str, bool] = False) -> None:
"""
Provide useful information, important for bug reports.
It comprises info about hosting operation system, pandas version,
and versions of other installed relative packages.
Parameters
----------
as_json : str or bool, default: False
* If False, outputs info in a human readable form to the console.
* If str, it will be considered as a path to a file.
Info will be written to that file in JSON format.
* If True, outputs info in JSON format to the console.
Notes
-----
This is mostly a copy of pandas.show_versions() but adds separate listing
of Modin-specific dependencies.
"""
sys_info = _get_sys_info()
sys_info["commit"] = get_versions()["full-revisionid"]
modin_deps = _get_modin_deps_info()
deps = _get_dependency_info()
if as_json:
j = {
"system": sys_info,
"modin dependencies": modin_deps,
"dependencies": deps,
}
if as_json is True:
sys.stdout.writelines(json.dumps(j, indent=2))
else:
assert isinstance(as_json, str) # needed for mypy
with codecs.open(as_json, "wb", encoding="utf8") as f:
json.dump(j, f, indent=2)
else:
assert isinstance(sys_info["LOCALE"], dict) # needed for mypy
language_code = sys_info["LOCALE"]["language-code"]
encoding = sys_info["LOCALE"]["encoding"]
sys_info["LOCALE"] = f"{language_code}.{encoding}"
maxlen = max(max(len(x) for x in d) for d in (deps, modin_deps))
print("\nINSTALLED VERSIONS\n------------------") # noqa: T201
for k, v in sys_info.items():
print(f"{k:<{maxlen}}: {v}") # noqa: T201
for name, d in (("Modin", modin_deps), ("pandas", deps)):
print(f"\n{name} dependencies\n{'-' * (len(name) + 13)}") # noqa: T201
for k, v in d.items():
print(f"{k:<{maxlen}}: {v}") # noqa: T201
class ModinAssumptionError(Exception):
"""An exception that allows us defaults to pandas if any assumption fails."""
pass
class classproperty:
"""
Decorator that allows creating read-only class properties.
Parameters
----------
func : method
Examples
--------
>>> class A:
... field = 10
... @classproperty
... def field_x2(cls):
... return cls.field * 2
...
>>> print(A.field_x2)
20
"""
def __init__(self, func: Any):
self.fget = func
def __get__(self, instance: Any, owner: Any) -> Any: # noqa: GL08
return self.fget(owner)