/usr/lib/python3/dist-packages/pysal/common.py is in python3-pysal 1.14.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # external imports
import numpy as np
import numpy.linalg as la
import scipy as sp
import scipy.stats as stats
from .cg.kdtree import KDTree
from scipy.spatial.distance import pdist, cdist
RTOL = .00001
ATOL = 1e-7
import copy
import math
import random
import sys
import time
import unittest
from warnings import warn as Warn
from functools import wraps
from pysal.core.FileIO import FileIO as popen
try:
from patsy import PatsyError
except ImportError:
PatsyError = Exception
#################
# Compatibility #
#################
def iteritems(d, **kwargs):
"""
Implements six compatibility library's iteritems function
appropriately calls either d.iteritems() or d.items() depending on the
version of python being used.
"""
if sys.version_info.major < 3:
return d.iteritems(**kwargs)
else:
return iter(d.items(**kwargs))
######################
# Decorators/Utils #
######################
def simport(modname):
"""
Safely import a module without raising an error.
Parameters
-----------
modname : str
module name needed to import
Returns
--------
tuple of (True, Module) or (False, None) depending on whether the import
succeeded.
Notes
------
Wrapping this function around an iterative context or a with context would
allow the module to be used without necessarily attaching it permanently in
the global namespace:
>>> for t,mod in simport('pandas'):
if t:
mod.DataFrame()
else:
#do alternative behavior here
del mod #or don't del, your call
instead of:
>>> t, mod = simport('pandas')
>>> if t:
mod.DataFrame()
else:
#do alternative behavior here
The first idiom makes it work kind of a like a with statement.
"""
try:
exec('import {}'.format(modname))
return True, eval(modname)
except:
return False, None
def requires(*args, **kwargs):
"""
Decorator to wrap functions with extra dependencies:
Arguments
---------
args : list
list of strings containing module to import
verbose : bool
boolean describing whether to print a warning message on import
failure
Returns
-------
Original function is all arg in args are importable, otherwise returns a
function that passes.
"""
v = kwargs.pop('verbose', True)
wanted = copy.deepcopy(args)
def inner(function):
available = [simport(arg)[0] for arg in args]
if all(available):
return function
else:
def passer(*args,**kwargs):
if v:
missing = [arg for i, arg in enumerate(wanted) if not available[i]]
print(('missing dependencies: {d}'.format(d=missing)))
print(('not running {}'.format(function.__name__)))
else:
pass
return passer
return inner
|