Skip to content

Commit 7ac4e8b

Browse files
author
Davies Liu
committed
rename random.py to rand.py
1 parent 7f37188 commit 7ac4e8b

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

python/pyspark/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@
3737
3838
"""
3939

40-
# The following block allows us to import python's random instead of mllib.random for scripts in
41-
# mllib that depend on top level pyspark packages, which transitively depend on python's random.
42-
# Since Python's import logic looks for modules in the current package first, we eliminate
43-
# mllib.random as a candidate for C{import random} by removing the first search path, the script's
44-
# location, in order to force the loader to look in Python's top-level modules for C{random}.
45-
import sys
46-
s = sys.path.pop(0)
47-
import random
48-
sys.path.insert(0, s)
49-
5040
from pyspark.conf import SparkConf
5141
from pyspark.context import SparkContext
5242
from pyspark.rdd import RDD

python/pyspark/mllib/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,37 @@
2424
import numpy
2525
if numpy.version.version < '1.4':
2626
raise Exception("MLlib requires NumPy 1.4+")
27+
28+
__all__ = ['classification', 'clustering', 'feature', 'linalg', 'random',
29+
'recommendation', 'regression', 'stat', 'tree', 'util']
30+
31+
import sys
32+
import rand as random
33+
random.__name__ = 'random'
34+
random.RandomRDDs.__module__ = __name__ + '.random'
35+
36+
37+
class RandomModuleHook(object):
38+
"""
39+
Hook to import pyspark.mllib.random
40+
"""
41+
fullname = __name__ + '.random'
42+
43+
def find_module(self, name, path=None):
44+
# skip all other modules
45+
if not name.startswith(self.fullname):
46+
return
47+
return self
48+
49+
def load_module(self, name):
50+
if name == self.fullname:
51+
return random
52+
53+
cname = name.rsplit('.', 1)[-1]
54+
try:
55+
return getattr(random, cname)
56+
except AttributeError:
57+
raise ImportError
58+
59+
60+
sys.meta_path.append(RandomModuleHook())

python/pyspark/mllib/feature.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
"""
1919
Python package for feature in MLlib.
2020
"""
21+
from __future__ import absolute_import
22+
2123
import sys
2224
import warnings
25+
import random
2326

2427
from py4j.protocol import Py4JJavaError
2528

@@ -341,8 +344,6 @@ def __init__(self):
341344
"""
342345
Construct Word2Vec instance
343346
"""
344-
import random # this can't be on the top because of mllib.random
345-
346347
self.vectorSize = 100
347348
self.learningRate = 0.025
348349
self.numPartitions = 1
@@ -411,8 +412,5 @@ def _test():
411412
exit(-1)
412413

413414
if __name__ == "__main__":
414-
# remove current path from list of search paths to avoid importing mllib.random
415-
# for C{import random}, which is done in an external dependency of pyspark during doctests.
416-
import sys
417415
sys.path.pop(0)
418416
_test()

python/pyspark/mllib/linalg.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,4 @@ def _test():
614614
exit(-1)
615615

616616
if __name__ == "__main__":
617-
# remove current path from list of search paths to avoid importing mllib.random
618-
# for C{import random}, which is done in an external dependency of pyspark during doctests.
619-
import sys
620-
sys.path.pop(0)
621617
_test()
File renamed without changes.

python/run-tests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function run_mllib_tests() {
7272
run_test "pyspark/mllib/clustering.py"
7373
run_test "pyspark/mllib/feature.py"
7474
run_test "pyspark/mllib/linalg.py"
75-
run_test "pyspark/mllib/random.py"
75+
run_test "pyspark/mllib/rand.py"
7676
run_test "pyspark/mllib/recommendation.py"
7777
run_test "pyspark/mllib/regression.py"
7878
run_test "pyspark/mllib/stat.py"

0 commit comments

Comments
 (0)