Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2313#discussion_r17222379
  
    --- Diff: python/pyspark/rddsampler.py ---
    @@ -22,24 +22,26 @@
     class RDDSamplerBase(object):
     
         def __init__(self, withReplacement, seed=None):
    -        try:
    -            import numpy
    -            self._use_numpy = True
    -        except ImportError:
    -            print >> sys.stderr, (
    -                "NumPy does not appear to be installed. "
    -                "Falling back to default random generator for sampling.")
    -            self._use_numpy = False
    -
             self._seed = seed if seed is not None else random.randint(0, 
sys.maxint)
             self._withReplacement = withReplacement
             self._random = None
             self._split = None
             self._rand_initialized = False
    +        self._tried_numpy = False
     
         def initRandomGenerator(self, split):
    +        if not self._tried_numpy:
    +            try:
    +                import numpy
    +                self._use_numpy = True
    +            except ImportError:
    +                print >> sys.stderr, (
    +                    "NumPy does not appear to be installed. "
    +                    "Falling back to default random generator for 
sampling.")
    +                self._use_numpy = False
    +            self._tried_numpy = True
    +
    --- End diff --
    
    How about put detecting numpy and fallback at the module level?
    
    Such as:
    
    ```python
    try:
        from numpy.random import RandomState
    except ImportError:
        class RandomState(object):
              def __init__(self, seed):
                   self._random = random.Random(seed)
              def random_sample(self):
                    pass
              def poisson(self):
                    pass
              def shuffle(self):
                    pass
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to