如何将目录转换为包? [英] How to convert a directory into a package?

查看:353
本文介绍了如何将目录转换为包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些帮助程序功能的目录,应将其放入程序包中.显然,第一步是将目录命名为+mypackage\之类,因此我可以使用mypackage.somefunction调用函数.问题是,某些函数相互依赖,并且显然MATLAB要求包函数仍然通过显式说明包名称来调用同一包中的函数,因此我必须重写所有函数调用.更糟糕的是,如果我决定重命名程序包,那么所有函数调用也都必须重写.当我将cd的名称以+开头时,这些功能甚至无法正常工作.

I have a directory with some helper functions that should be put into a package. Step one is obviously naming the directory something like +mypackage\ so I can call functions with mypackage.somefunction. The problem is, some functions depend on one another, and apparently MATLAB requires package functions to call functions in the very same package still by explicitly stating the package name, so I'd have to rewrite all function calls. Even worse, should I decide to rename the package, all function calls would have to be rewritten as well. These functions don't even work correctly anymore when I cd into the directory as soon as its name starts with a +.

有没有比重写大得多的解决方案? 还是至少使用诸如import this.*之类的自引用内容,以方便将来的程序包重命名?

Is there an easier solution than rewriting a lot? Or at least something self-referential like import this.* to facilitate future package renaming?

编辑我注意到类和静态方法也是如此,这就是为什么我将自引用部分放入

edit I noticed the same goes for classes and static methods, which is why I put the self-referential part into this separate question.

推荐答案

实际上,我不知道您是否应该经常重命名软件包.在我看来, MATLAB 是将一组相关的函数和类组织到一个集合中,您可以轻松地将其用作工具箱"或作为工具箱"分发,而不必担心名称冲突.

In truth, I don't know that you should really be renaming your packages often. It seems to me that the whole idea behind a package in MATLAB is to organize a set of related functions and classes into a single collection that you could easily use or distribute as a "toolbox" without having to worry about name collisions.

这样,将函数和类放入包中就像执行 final 步骤一样,是为了制作精美的工具集而执行的,因此,您实际上没有太多理由来重命名包.此外,只需要在将包名称添加到包函数调用之前进行一次操作即可.

As such, placing functions and classes into packages is like a final step that you perform to make a nice polished collection of tools, so you really shouldn't have much reason to rename your packages. Furthermore, you should only have to go through once prepending the package name to package function calls.

...((想想我要建议的是一个好主意;)))...

但是,如果您确实希望避免浏览软件包并在函数调用之前添加新的软件包名称,则一种方法是使用函数

However, if you really want to avoid having to go through your package and prepend your function calls with a new package name, one approach would be to use the function mfilename to get the full file path for the currently running package function, parse the path string to find the parent package directories (which start with "+"), then pass the result to the import function to import the parent packages. You could even place these steps in a separate function packagename (requiring that you also use the function evalin):

function name = packagename

  % Get full path of calling function:
  callerPath = evalin('caller', 'mfilename(''fullpath'')');

  % Parse the path string to get package directories:
  name = regexp(callerPath, '\+(\w)+', 'tokens');

  % Format the output:
  name = strcat([name{:}], [repmat({'.'}, 1, numel(name)-1) {''}]);
  name = [name{:}];

end

然后您可以将其放置在包函数的最开始,以使它们自动包含其父包名称空间:

And you could then place this at the very beginning of your package functions to automatically have them include their parent package namespace:

import([packagename '.*']);

这是个好主意吗?好吧,我不确定如果每次 都执行此操作,则调用包函数会对计算产生什么影响.另外,如果您有嵌套在包中的包,您将从packagename获得如下输出:

Is this a good idea? Well, I'm not sure what the computational impacts will be if you're doing this every time you call a package function. Also, if you have packages nested within packages you will get output from packagename that looks like this:

'mainpack.subpack.subsubpack'

并且对import的调用将仅包括直接父包subsubpack.如果还希望包括其他父包,则必须从上述字符串中顺序删除最后一个包,然后导入其余字符串.

And the call to import will only include the immediate parent package subsubpack. If you also want to include the other parent packages, you would have to sequentially remove the last package from the above string and import the remainder of the string.

简而言之,这不是一个非常干净的解决方案,但是有可能使您的包以这种方式更容易重命名.但是,我仍然建议最好将软件包的创建视为创建一组核心工具过程中的最后一步,在这种情况下,重命名应该是不太可能的情况,并且将带有软件包名称的软件包函数调用放在前面只需要做一次.

In short, this isn't a very clean solution, but it is possible to make your package a little easier to rename in this way. However, I would still suggest that it's better to view the creation of a package as a final step in the process of creating a core set of tools, in which case renaming should be an unlikely scenario and prepending package function calls with the package name would only have to be done once.

这篇关于如何将目录转换为包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆