Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get data directories function #375

Merged
merged 6 commits into from
Oct 31, 2016

Conversation

bryanwweber
Copy link
Member

Add a function to get the data directories that Cantera searches. Since we're using Boost in another spot now as a requirement, I thought it'd be OK to use that here as well. If not, we just need to implement our own string imploder to convert from std::vector<string> to a std::string.

@speth
Copy link
Member

speth commented Oct 26, 2016

Yes, using Boost is definitely fine (and I was actually planning on starting to use the string algorithms to replace some of the functions defined in stringUtils.h). I also agree that having a function to make this information available would be useful.

The question I have is whether this function should return a single string or whether it should just return a vector<string>. When would you want the list of directories in this format, especially given the OS-dependence of the path separator?

@bryanwweber
Copy link
Member Author

I chose that format, and the separator, assuming that most people would interact with the result as though it were something like the system PATH. I also wasn't sure how to translate the vector<string> into something Cython/the C library (for Matlab) would understand. Any pointers on that would be appreciated!

@speth
Copy link
Member

speth commented Oct 26, 2016

I think Cython should understand how to translate vector<string>, it takes just a bit of fiddling to convert the strings to the correct type. For example, we already manage something similar with the Composition type which is std::map<string,double>.

For C or Matlab, returning a single string may indeed be an easier solution.

@speth
Copy link
Member

speth commented Oct 27, 2016

Another option: what about having getDataDirectories take the separator as an argument?

@bryanwweber
Copy link
Member Author

OK I added the argument and rebased onto master. I think returning the string makes the most sense, for easy interoperability with Clib and Matlab.

@@ -31,6 +31,10 @@ def add_directory(directory):
""" Add a directory to search for Cantera data files. """
CxxAddDirectory(stringify(directory))

def get_data_directories(sep=':'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to use os.pathsep instead of ':' here, otherwise this will fail badly on Windows.

Copy link
Member Author

@bryanwweber bryanwweber Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't matter, since the C++ returns just a string, and that's split into a list in Python, right?

Copy link
Member

@speth speth Oct 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, if you call getDataDirectories with : as the separator, it will return a string like:

C:\src\anaconda\envs\py33\lib\site-packages\cantera\data:C:\Program Files\Cantera\data

Which Python will then split as:

['C',
 '\\src\\anaconda\\envs\\py33\\lib\\site-packages\\cantera\\data',
 'C',
 '\\Program Files\\Cantera\\data']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And actually, given that the Python method returns a list, it shouldn't take the separator as an argument, it should just use os.pathsep directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I forgot about the drive letter... Thanks for the note

function getdatadirectories(sep)
% GETDATADIRECTORIES Get a string of the directories searched for data files.
% getdatadirectories(sep)
% Get a string of the directores Cantera searches for data files, with each
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

misspelled 'directories'

@@ -0,0 +1,11 @@
function getdatadirectories(sep)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we camelCase most of the most of the Matlab function names?

@@ -1420,6 +1420,17 @@ extern "C" {
}
}

int getDataDirectories(int buflen, char* buf, const char* sep)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the function should be named ct_getDataDirectories (some of the existing functions need to be updated to follow this convention). Also, I think you forgot to add ct.h.

try {
string d = getDataDirectories(sep);
copyString(d, buf, buflen);
return int(d.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyString was recently modified so that you can simply write:

return copyString(getDataDirectories(sep), buf, buflen);

// get string of data directories
case 5:
sep = getString(prhs[2]);
buflen = getDataDirectories(buflen, output_buf, sep);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is more clear to just put 0 in place of buflen and output_buf in this initial call.

@@ -212,7 +212,7 @@ if localenv['sphinx_docs']:
'@Data': ['Air.m', 'constants.m', 'gasconstant.m', 'GRI30.m',
'Hydrogen.m', 'Methane.m', 'Nitrogen.m', 'oneatm.m',
'Oxygen.m', 'Water.m'],
'@Utilities': ['adddir.m', 'ck2cti.m', 'cleanup.m', 'geterr.m',]
'@Utilities': ['adddir.m', 'ck2cti.m', 'cleanup.m', 'geterr.m', 'getdatadirectories.m']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to update case here as well.

@speth
Copy link
Member

speth commented Oct 30, 2016

I squashed a few of the revisions in preparation for merging this, and fixed two bugs:

  • for the Python version, os.path.sep is not the same as os.pathsep (on Linux, the former is '/' and the latter is ':')
  • In ctfunctions.cpp, the variable sep had not been declared, and the method called was incorrect (I guess you aren't currently compiling the Matlab toolbox)

However, I also noticed that the Matlab function does not work -- it currently does not return anything.

@bryanwweber
Copy link
Member Author

Hmmm... I didn't get a chance to test the Matlab toolbox, I can do that when I get back to the office tomorrow. Thanks for fixing those bugs!

@bryanwweber
Copy link
Member Author

OK I rebased this onto b01f9cd and fixed the Matlab bug. It returns a cell array of strings now, similar to the list in Python.

@speth speth merged commit d59cae1 into Cantera:master Oct 31, 2016
@bryanwweber bryanwweber deleted the add_get_data_dirs branch November 11, 2016 22:46
@speth speth mentioned this pull request Nov 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants