Skip to content

Commit

Permalink
Allowed arbitrary python to be used while compiling. setup.py now che…
Browse files Browse the repository at this point in the history
…cks if the python specified in the common.mk file is the identical to currently running python and changes the common.mk file if necessary. Fixes #52. Also outputs the compile time options once each for the theory and mocks directories
  • Loading branch information
manodeep committed Aug 3, 2016
1 parent 8e7f172 commit 318272d
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 179 deletions.
41 changes: 31 additions & 10 deletions Corrfunc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,38 @@
if sys.version_info[0] < 3:
__all__ = [n.encode('ascii') for n in __all__]

__version__ = "1.1.0"
__version__ = "1.2.0"

if sys.version_info[0] >= 3:
def rd(filename):
with open(filename, encoding="utf-8") as f:
r = f.read()

return r
else:
def rd(filename):
with open(filename) as f:
def read_text_file(filename, encoding="utf-8"):
"""
Reads a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
"""
try:
with open(filename, 'r', encoding) as f:
r = f.read()
except TypeError:
with open(filename, 'r') as f:
r = f.read()
return r


def write_text_file(filename, contents, encoding="utf-8"):
"""
Writes a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
"""
try:
with open(filename, 'w', encoding) as f:
f.write(contents)

except TypeError:
with open(filename, 'w') as f:
f.write(contents)


return r
4 changes: 2 additions & 2 deletions Corrfunc/call_correlation_functions_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
except ImportError:
pd = None

from Corrfunc import rd
from Corrfunc import read_text_file
from Corrfunc._countpairs_mocks import countpairs_rp_pi_mocks as rp_pi_mocks
from Corrfunc._countpairs_mocks import countpairs_theta_mocks as theta_mocks
from Corrfunc._countpairs_mocks import countspheres_vpf_mocks as vpf_mocks
Expand All @@ -40,7 +40,7 @@ def main():
include_file = path.join(path.dirname(path.abspath(__file__)),
"../include/", "countpairs_rp_pi_mocks.h")
try:
includes = rd(include_file)
includes = read_text_file(include_file)
except (IOError, OSError) as e:
print("ERROR: Could not find file {0} error = {1}.\n \
Please compile the `Corrfunc' library directly before \
Expand Down
55 changes: 26 additions & 29 deletions Corrfunc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.utils import bytes_to_native_str
import sys
import os.path as path
import os

__all__ = ['rd', 'read_catalog']
__all__ = ['read_text_file', 'read_catalog']

if sys.version_info[0] >= 3:
def rd(filename):
"""
Reads a file under python3 assuming an UTF-8 encoding.
"""
with open(filename, encoding="utf-8") as f:
r = f.read()

return r
else:
def rd(filename):
"""
Reads a file under python2.
"""
with open(filename) as f:
def read_text_file(filename, encoding="utf-8"):
"""
Reads a file under python3 with encoding (default UTF-8).
Also works under python2, without encoding.
Uses the EAFP (https://docs.python.org/2/glossary.html#term-eafp)
principle.
"""
try:
with open(filename, 'r', encoding) as f:
r = f.read()
except TypeError:
with open(filename, 'r') as f:
r = f.read()

return r
return r


def read_catalog(filebase=None):
Expand Down Expand Up @@ -142,23 +139,23 @@ def read_fastfood(filename, return_dtype=None, need_header=None):
return x, y, z

if filebase is None:
filename = path.join(path.dirname(path.abspath(__file__)),
"../xi_theory/tests/data/", "gals_Mr19")
filename = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../xi_theory/tests/data/", "gals_Mr19")
# Figure out the datatype, use the header file in the include directory
# because that is most likely correct (common.mk might have been
# modified but not recompiled)
include_file = path.join(path.dirname(path.abspath(__file__)),
"../include/", "countpairs.h")
includes = rd(include_file)
include_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../include/", "countpairs.h")
includes = read_text_file(include_file)
vector_type = re.search(r'(\w+)\s*\*\s*rupp\s*\;', includes,
re.I).group(1)
allowed_types = {"float": np.float32, "double": np.float}
if vector_type not in list(allowed_types.keys()):
print("Error: Unknown precision={0} found in header file {1}. \
msg = "Error: Unknown precision={0} found in header file {1}. \
Allowed types are `{2}'".format(vector_type,
include_file,
allowed_types))
sys.exit()
allowed_types)
raise AssertionError(msg)

dtype = allowed_types[vector_type]
allowed_exts = {'.ff': read_fastfood,
Expand All @@ -168,7 +165,7 @@ def read_fastfood(filename, return_dtype=None, need_header=None):
}

for e in allowed_exts:
if path.exists(filename + e):
if os.path.exists(filename + e):
f = allowed_exts[e]
x, y, z = f(filename + e, dtype)
return x, y, z
Expand All @@ -177,8 +174,8 @@ def read_fastfood(filename, return_dtype=None, need_header=None):
= {1}".format(filename, allowed_exts.keys()))
else:
# Likely an user-supplied value
if path.exists(filebase):
extension = path.splitext(filebase)[1]
if os.path.exists(filebase):
extension = os.path.splitext(filebase)[1]
f = read_fastfood if '.ff' in extension else read_ascii

# default return is double
Expand Down
Loading

0 comments on commit 318272d

Please sign in to comment.