Running doctests using a different function - python-3.x

I wrote a module with a few functions along with their doctests, and I would like to run these tests on functions with the same names but written by someone else.
The documentation provides the following snippet for retrieving all tests for somefunction in mymodule and then running them in the usual way (like running doctest.testmod()):
TESTS = doctest.DocTestFinder().find(mymodule.somefunction)
DTR = doctest.DocTestRunner(verbose=True)
for test in TESTS:
print (test.name, '->', DTR.run(test))
But I don't know where to go from here to have those tests run on theirmodule.somefunction instead. I tried changing the filename field from mymodule to theirmodule in the Example objects for each test, but to no avail. Does anyone know how to achieve this?

This may not be the most elegant solution, but simply copying my docstrings to their functions in my script works:
theirmodule.somefunction.__doc__ = mymodule.somefunction.__doc__
And then I only need to run the snippet in my question on theirmodule.somefunction.

Related

The code in the manual that is actually seemed to be wrong

I was learning Haxe. And I found that a code that is in the official manual doesn't be compiled in https://try.haxe.org/. and I wonder why and how to fix the code?
the code is the one in here: https://haxe.org/manual/types-enum-using.html
try.haxe.org assumes that the class that contains the entry point main() is called Test, while the code example you linked uses Main.
It appears to work fine once you change class Main to class Test in the code snippet: https://try.haxe.org/#68274
Note that some other code examples from the manual may not compile because try.haxe.org still runs Haxe 3.4.4, while the current release is 4.0.5. To work around this, people usually use this fork of try.haxe that lets you specify the Haxe version to use in the options tab:
http://try-haxe.mrcdk.com/

How python brings functions into scope?

I contribute to scikit-image and was using coverage. Now, when I did
coverage run benchmarks/benchmark_name.py
and then generated the report, there were a lot of files that didn't have any link to this file but were still executed when I ran the above command. One interesting thing that I noted in those files, only the lines having a function definition(def abc():) were run. See the image below:
It basically shows the coverage report of a file which didn't have any link to my file. Yet, it was run and only the function definition statements and import statements.
Is this the way python brings functions defined in the project into its scope? If that's the case, I would like to know the flow in which this happened. Please help.
Thanks.
You're looking at import transitive dependencies. At import time, anything not protected by an if __name__ == '__main__': clause will be executed, including the def statements that you mentioned.
Use coverage run --omit=... and similar options to trim your reporting output.

A module that support both command line and exports itself

Is there a clever way to create a module that can both be run with index.js arg1 arg2 as well as exporting itself to be used by others, ie. module.exports?
Say I have a script that kickstart a proxy server, while I can do proxy.js host port, I can't think of an easy way to export it as well. Should I look at process.argv to decide whether to exports or run itself? Or is there a better pattern?
PS: I was trying to write test cases for it but realize without module.exports, it's kinda hard to provide proper test coverage. Just curious how one would tackle this.

Difference between lib and lib-cov in express?

I'm new to Node.js.
module.exports = process.env.EXPRESS_COV
? require("./lib-cov/express")
: require("./lib/express");
I know EXPRESS_COV returns a Boolean value, but what is the difference between lib-cov/express and lib/express?
process.env.EXPRESS_COV would be true when you're running tests and want to see the code coverage of those tests (i.e. how many lines of your codebase are actually executed when the tests are run). Mocha, the test framework used for express, achieves this through the use of jscoverage.
JSCoverage parses through your source code and adds a bunch of lines that look like this:
$_jscoverage[filename][line]++;
Naturally, that's rather confusing to have in one's source code, not to mention adding a lot of bulk. So we'd never want JSCoverage processed files in our codebase. Fortunately, JSCoverage places the modified files in a different directory. In this case, ./lib-cov/ instead of ./lib/. That way, we can see how effective our tests are and not clutter up our code.
For details on how this whole rigamarole runs, see TJ Holowaychuk's article.
If you want to avoid all of this, you can use Istanbul instead, as it's much simpler and doesn't require exceptions in index.js

alternative for module.exports

I've got a question that might sound strange.
We are using node.js, but the way we are writing javascript code is in a functional style. We have a lint tool that reports illegal use of the assignment expression. We can eleminate them all except for the 'module.exports = xxx' statement.
Is there an other way to load modules in node.js?
for example; the following statement would be fine as a solution as it is not an assignment expression but a variable declaration:
var exports = xxx;
if there is a way that nodejs would pick up this 'exports' variable, then we're done.
gr,
Coen
the following did the trick for me:
vm.runInThisContext
Looks like changing lint tool is the best way. :)
However you can use different module loader (there are some AMD implementations for node.js out there) that's pure functional.

Resources