How to tell child modules to use specific logger - python-2.7

I have created two loggers in my logging module like logger1,logger2 and my application has two submodules module1 and module2.I want to configure/tell module1 should use logger1 and module2 should use logger2 only?

Assign logger1 to some variable in module1 and let functions in that module use that variable to call correct logger. And remeber to check in functions whether variable is not None.

Related

How to create library for v lang by c++

I would like to ask how to create a module on c++ for v lang.
For example
с++
int some_method() {
return 6
}
v
import my_c_module
my_c_module.some_method()
I will be grateful for any information
And can I call c++ class method in v lang?
In V every file in the root of a folder is part of the same module. Simple programs don't need to specify module name, in which case it defaults to 'main'.
V is a very modular language. Creating reusable modules is encouraged and is quite easy to do. To create a new module, create a directory with your module's name containing .v files with code:
cd ~/code/modules
mkdir mymodule
vim mymodule/myfile.v
Inside myfile.v
// myfile.v
module mymodule
// To export a function we have to use `pub`
pub fn say_hi() {
println('hello from mymodule!')
}
Now you can call your module
import mymodule
fn main() {
mymodule.say_hi()
}
You can create also your custom module, and here is the official docs

How can I test functions inside a single file Lua script?

I would like to unit test functions from a single file Lua script, say script.lua. The script looks something like follows:
-- some fields from gvsp dissector which shall be post processed in custom dissector
gvsp_field0_f = Field.new("gvsp.<field0-name>")
gvsp_field1_f = Field.new("gvsp.<field1-name>")
-- custom protocol declaration
custom_protocol = Proto("custom","Custom Postdissector")
-- custom protocol field declarations
field0_f = ProtoField.string("custom.<field0-name>","Custom Field 0")
field1_f = ProtoField.string("custom.<field1-name>","Custom Field 1")
-- register custom protocol as postdissector
register_postdissector(custom_protocol)
function custom_protocol.dissector(buffer,pinfo,tree)
-- local field values of "pre" dissector which are analyzed
local gvsp_field0_value = gvsp_field0_f()
local gvsp_field1_value = gvsp_field1_f()
-- functions which shell be unit tested
function0(...)
function1(...)
end
function0(...)
-- implementation
end
function1(...)
-- implementation
end
Let's say I do not want to separate the functions from the script file into a separate module file (which would probably make things easier). How can I define tests (preferably with luaunit because easy to integrate, but other tool would be ok as well) for the functions defined in script.lua inside the script.lua file or in a separate test_script.lua file?
To enable separate script and unit test execution one needs at least 3 files (in this example 4 because the unit test framework luaunit which consists of a single file is integrated into the project directory). For this example all files reside in the same directory. The script script.lua may not define any functions in it but must import all functions it needs from its module module.lua.
-- script imports module functions
module = require('module')
-- ... and uses it to print the result of the addition function
result = module.addtwo(1,1)
print(result)
module.lua is implemented accoring to the Lua module skeleton that its functions are automatically registered for import through other script files or modules.
-- capture the name searched for by require
local NAME=...
-- table for our functions
local M = { }
-- A typical local function that is also published in the
-- module table.
local function addtwo(a,b) return a+b end
M.addtwo = addtwo
-- Shorthand form is less typing and doesn't use a local variable
function M.subtwo(x) return x-2 end
return M
test_module.lua contains the unit tests for the module functions and imports luaunit.lua (unit test framework) for its execution. test_module.lua has the following content.
luaunit = require('luaunit')
script = require('module')
function testAddPositive()
luaunit.assertEquals(module.addtwo(1,1),2)
end
os.exit( luaunit.LuaUnit.run() )
If you run the tests by executing lua test_module.lua the tests are executed separately from the script functionality.
.
Ran 1 tests in 0.000 seconds, 1 success, 0 failures
OK
The script is executed as usual with lua script.lua with output 2.
Simple answer: You can't!
I've asked the Lua team about this myself a few years ago as there is no obvious way for a script to know if it is the main script running or included (e.g., 'require'd).
There does not seem to be interest for adding such capability in the foreseeable future, either!

Testing typescript modules without exports

I'm working on legacy environment and I have Typescript files which contain modules or/and classes
class SomeClass {
...
}
module AndAModule {
export namespace NestedNamespace {
...
}
}
Notice the lack of "export" keyword in the top level modules/classes.
I would like to test functions in the "NestedNamespace" using Jest library. However when I import the Typescript file in a test file:
var service = require("/path/to/file/SomeService.ts")
I have no access neither to a class nor module defined in the SomeService.ts file. If add the "export" keyword before module and class I'm able to access them through the object returned from require, however this breaks whole environment and other files, as after this, the module and the class are not present in the outFile generated by typescript.
How can I import desired modules from files, without adding any export statement?
If you have a function which is not being exported, then some other function which is exported, is calling that un-exported function. Test the exported method, and you will indirectly be testing the un-exported one.

Unit Testing local function in Lua

So I'm using Busted to create unit tests for an existing Lua file, without changing the code in the file if possible. The file imports another file, and then stores various methods from that file in local functions, like so.
[examplefile.lua]
local helper = require "helper.lua"
local helper_accept = helper.accept
local helper_reject = helper.reject
foo = new function()
-- do something which uses helper_accept
-- do something which uses helper_reject
end
I want to spy on these methods in my tests to ensure that they have been called at the right places. However, I can't find any way to do this from the test.
I've tried simply mocking out the helper methods, as in:
[exampletest.lua]
local helper = require "helper.lua"
local examplefile = require "examplefile.lua"
-- mock the helper function to simply return true
helper.accept = new function() return true end
spy.on(helper, "accept")
examplefile:foo
assert.spy(helper).was().called()
but that doesn't work as the real file uses the helper_accept and helper_reject methods, not helper.accept and helper.reject.
Can this be done without changing the code?
Thanks.
The easiest way I can think of for accomplishing this is to override the "helper" library with hook stubs. You can do this by modifying the package.loaded table. The package.loaded table stores the result of an initial call to require "lib", so that if the same require is called again, the module does not need to be reloaded. If you place something in there before the first call to require "lib", it will never actually load the library from the filesystem.
In your case you may want to actually load the library, but hook all the library accesses. I'd do that something like this...
local lib = require "lib"
local function hook_func(_, key)
print('Accessing "lib" attribute '..tostring(key))
-- other stuff you might want to do in the hook
return lib[key]
end
package.loaded["lib"] = setmetatable({}, {__index = hook_func})

Setting metaclass of wrapped class with Boost.Python

I have an Event class defined in C++ that I expose to Python using Boost. My scripts are expected to derive from this class, and I'd like to do some initialization whenever a new child class is defined.
How can I set the metaclass of the exposed Event class such that whenever a Python script derives from this class, the metaclass could do the required initialization?
I would like to avoid having to explicitly use a metaclass in the scripts...
class KeyboardEvent(Event): # This is what I want
pass
class KeyboardEvent(Event, metaclass=EventMeta): # This is not a good solution
pass
Edit: Part of the solution
It seems there's no way to set the metaclass with Boost.Python. The next best thing is to improvise and change the metaclass after the class was defined. In native Python, the safe way to change a metaclass is to do this:
B = MetaClass(B.__name__, B.__bases__, B.__dict__)
In Boost, it'd look something like this:
BOOST_PYTHON_MODULE(event)
{
using namespace boost::python;
using boost::python::objects::add_to_namespace;
class_<EventMetaClass> eventmeta("__EventMetaClass")
...;
class_<Event> event("Event")
...;
add_to_namespace(scope(), "Event",
eventmeta(event["__name__"], event["__bases__"], event["__dict__"]));
}
The problem is that I can't seem to find a way to define a metaclass with Boost.Python, which is why I've opened How to define a Python metaclass with Boost.Python?.
If boost does not offer a way to do it from withn c++, and it looks like it don't, the way to go is to create wrapper classes that implement the metaclass -
It can be done more or less automatically usign a ittle bit of instrospection. Let's suppose your boost module is named "event" - you should either name the file as _event or place it inside you module, and write an python file - named "event.py" (or an __init__.py file on your module that would do more or less this:
import _event
class eventmeta(type):
...
event_dict = globals()
for key, value in _event.__dict__.items():
if isinstance(value, type):
event_dict[key] = eventmeta(key, (value,),{})
else:
#set other module members as members of this module
event_dict[key] = value
del key, value, event_dict
Thos cpde will automatically set module variables equal to any names found in the native"_event" module - and for each class it encounters, create a new class changing the metaclass, as in your example.
It may be that you get a metaclass conflict by doing this. If so, the way is to make the newly created classes to be proxies to the native classes, by creating proper __getattribute__ and __setattr__ methods. Just ask in a comment if you will need to do that.