Clock Schedule Once Error in class functions/methods - kivy

I have a class with two functions in a Kivy app. One function calls the second with Clock.schedule_once(function_name).
class SivaCEFBrowser(Screen):
def back_to_login(self):
App.get_running_app().root.current='login_screen'
App.get_running_app().root.transition.direction='right'
def go_to_verify(self):
App.get_running_app().root.current='verify_screen'
App.get_running_app().root.transition.direction='left'
def launch_cef_browser(self):
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error.
cef.Initialize()
cef.CreateBrowserSync(url="https://www.google.com/", window_title="Hello World!")
cef.MessageLoop()
cef.Shutdown()
def trigger_browser(self):
Clock.schedule_once(self.launch_cef_browser)
When my code runs, the trigger_browser() function is called which, in turn, invokes launch_cef_browser(). This is the error that I get:
What am I missing here?

You need to define launch_cef_browser function like def launch_cef_browser(self, *args).
The *args argument is being used by kivy for internal processing of the function.

Related

Issues Extending a jobdsl command to a groovy class

I am having an issue extending a utilizing a jobdsl command from a groovy script.
I have created a helper class and i am passing "this". It works for jobdsl commands that do not require closures. But fails with invalid property for job dsl command that accepts closures. Example is the freestylejob("inputstring"){}
Helper Code
static freeJob(def fJ, def dslFactory){
def txt = dslFactory.freeStyleJob(fJ){
}
}
Caller Code
def testjob = DS.freeJob("inputstring", this)
I expect no errors. Instead i am getting the error below from jenkins.
ERROR: (filename.groovy, line 119) No signature of method: .filename.freeStyleJob() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl, classname$_classname_closure3) values: [inputstring, classname$_project_closure3#5c7bc682]
The code was calling the method from a class deep. Issue resolved.

Is there an equivalent to __MODULE__ for named functions in Elixir/ Erlang?

Is there an equivalent for retrieving the name of a function just like like __MODULE__ retrieves the name of a Module in Elixir/Erlang?
Example:
defmodule Demo do
def home_menu do
module_name = __MODULE__
func_name = :home_menu
# is there a __FUNCTION__
end
End
EDITED
The selected answer works,
but calling the returned function name with apply/3 yields this error:
[error] %UndefinedFunctionError{arity: 4, exports: nil, function: :public_home, module: Demo, reason: nil}
I have a function :
defp public_home(u, m, msg, reset) do
end
The function in question will strictly be called within its module.
Is there a way to dynamically call a private function by name within its own module?
▶ defmodule M, do: def m, do: __ENV__.function
▶ M.m
#⇒ {:m, 0}
Essentially, __ENV__ structure contains everything you might need.
Yes, there is. In Erlang there are several predefined macros that should be able to provide the information you need:
% The name of the current function
?FUNCTION_NAME
% The arity of the current function (remember name alone isn't enough to identify a function in Erlang/Elixir)
?FUNCTION_ARITY
% The file name of the current module
?FILE
% The line number of the current line
?LINE
Source: http://erlang.org/doc/reference_manual/macros.html#id85926
To add to Aleksei's answer, here is an example of a macro, f_name(), that returns just the name of the function.
So if you use it inside a function, like this:
def my_very_important_function() do
Logger.info("#{f_name()}: about to do important things")
Logger.info("#{f_name()}: important things, all done")
end
you will get a log statement similar to this:
my_very_important_function: about to do important things
my_very_important_function: important things, all done
Details:
Here is the definition of the macro:
defmodule Helper do
defmacro f_name() do
elem(__CALLER__.function, 0)
end
end
(__CALLER__ is just like __ENV__, but it's the environment of the caller.)
And here is how the macro can be used in a module:
defmodule ImportantCodes do
require Logger
import Helper, only: [f_name: 0]
def my_very_important_function() do
Logger.info("#{f_name()}: doing very important things here")
end
end

Clock.schedule_interval doesn't schedule callback

I am testing the functionality of the kivy.clock.Clock.schedule_interval function.
My schedule_interval isn't calling the test function but rather exits without any errors.
What is it that I'm not understanding? I have correctly modeled this test by the documentation.
from kivy.clock import Clock
class TestClass:
def __init__(self):
print("function __init__.")
schedule = Clock.schedule_interval(self.test, 1)
def test(self, dt):
print("function test.")
if __name__ == '__main__':
a = TestClass()
The expected output should be:
function __init__.
function test.
function test.
function test.
function test.
function test.
function test.
Instead I'm just getting:
function __init__.
The main problem is that your program exits before one second passes. I'm not sure but I also assume that there has to be a kivy app in order for the Clock to work (I tried to make an empty while loop instead of running an app but that didn't help).
Here's an easy fix that gives the desired output:
from kivy.clock import Clock
from kivy.base import runTouchApp
class TestClass:
def __init__(self, **kwargs):
print("function __init__.")
schedule = Clock.schedule_interval(self.test, 1)
def test(self, dt):
print("function test.")
if __name__ == '__main__':
test = TestClass()
runTouchApp() # run an empty app so the program doesn't close
Otherwise consider making TestClass inherit from kivy's App and running it with TestClass().run() - you will achieve the same result.

How can my block/yield pass a changing variable?

I'm writing the following module to capture SIGTERM that gets occasionally sent to my Delayed Job workers, and sets a variable called term_now that lets my job gracefully terminate itself before it's complete.
The following code in my module works perfect if I put it inline in my job, but I need it for several jobs and when I put it in a module it doesn't work.
I assume it's not working because it only passes term_now one time (when it's false), and even when it returns true it doesn't pass it again, therefore it never stops the job.
module StopJobGracefully
def self.execute(&block)
begin
term_now = false
old_term_handler = trap('TERM') do
term_now = true
old_term_handler.call
end
yield(term_now)
ensure
trap('TERM', old_term_handler)
end
end
end
Here's the working inline code how it's normally used (this is the code I'm trying to convert to a module):
class SMSRentDueSoonJob
def perform
begin
term_now = false
old_term_handler = trap('TERM') do
term_now = true
old_term_handler.call
end
User.find_in_batches(batch_size: 1000) do
if term_now
raise 'Gracefully terminating job early...'
end
# do lots of complicated work here
end
ensure
trap('TERM', old_term_handler)
end
end
end
you basically answered it yourself. in the example code you provided, term_now will only become true when the trap snapped before yield is called.
what you need to do is provide a mechanism that periodically fetches the information, so that you can check within the runs of ie find_in_batches.
so instead of yielding the result, your module should have a term_now method that might return an instance variable #term_now.

How do I use thrift_client library hook feature?

I'm using thrift_client 0.8.1 library. And it supports some hook interfaces.
client = ThriftClient.new(FooService, host)
client.add_callback :before_method do |*args|
ActionController::Base.logger.info instance_eval("#current_server")
logger.info args.inspect
end
client.add_callback :post_connect do |client|
logger.info "A thrift server connection has been made<#{client.current_server}>"
end
Everytime calling a method, I would like to write log the information of thrift server.
So I installed a :before method hook but I think instance_eval("#current_server") doesn't work as I expected. The #current_server is an instance variable in the ThriftClient class. What is the problem?
I don't believe that the :before_method callback is invoked within the ThriftClient instance context. This is the invocation of :before_method:
def do_callbacks(callback_type_sym, *args)
return unless #callbacks[callback_type_sym]
#callbacks[callback_type_sym].each do |callback|
callback.call(*args)
end
end
The :before_method CB is invoked with the method name. For :post_connect it is the client instance.

Resources