Avoid multiple calls to rospy.init_node() - ros

I am trying to check is a node has already been init another part of a python script? So if I am writting a client class that wraps around a lot of pubs/subs. It needs a node to be active, so it should one should be created. But If I create a node before initializing the object. It will throw the following error:
raise rospy.exceptions.ROSException("rospy.init_node() has already been called with different arguments: "+str(_init_node_args))
So is there a way to check if the scrip has already been initialized? so I can create one if it does not exist and if one does exist and does not try to create another node.

A quick and dirty way to do this is put the init_node call in a try...except block. Something like this:
try:
rospy.init_node("NODE_NAME_HERE")
except rospy.exceptions.ROSException as e:
print("Node has already been initialized, do nothing")

Related

What does WaitForChild and FindFirstChild mean in Roblox?

Watched several tutorials about these two... What are they even called? Still can't completely understand what they do. Can someone explain it like you would to a non-programmer?
https://developer.roblox.com/en-us/api-reference
https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild
Returns the child of the Instance with the given name. If the child
does not exist, it will yield the current thread until it does.
If the timeOut parameter is specified, this function will return nil
and time out after timeOut seconds elapsing without the child being
found.
https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChild
Returns the first child of the Instance found with the given name. If
no child exists with the given name, this function returns nil. If the
optional recursive argument is true, this function searches all
descendants rather than only the immediate children of the Instance.
Use this function if your code cannot guarantee the existence of an
object with a given name.
Watching video tutorials is pointless if you do not refer to a manual.

OCMock: OCMVerify times gets wrong count when using OCMPartialMock

OCMock Version: 3.6
Hi, I use OCMPartialMock to mock a config object. When I use the reference to the real object, it is right to verify the times the function networkStatusCacheTime in the object is called.
This is the screenshot -verifyInvocation:withQuantifier:atLocation: called when it is matching invocation to count. Only one invocation ocmock_replaced_networkStatusCacheTime can match.
But the test failed when I use the reference to the mock object.
OCPartialMockObject(NATritonConfig): Method networkStatusCacheTime was invoked 2 times; but was expected once.
Invocations networkStatusCacheTime and ocmock_replaced_networkStatusCacheTime both match by this time.
Is it different using the real object from the mock object? Maybe is it wrong the way I used?
This is a bug. I see you have opened an issue already: https://github.com/erikdoe/ocmock/issues/444

In BillingClient setup .setListener(this) references my calling activity instead of a PurchasesUpdatedListener

The docs show the following for setting up a Billingclient.
private BillingClient billingClient;
...
billingClient = BillingClient.newBuilder(activity).setListener(this).build();
In .setListener(this), the 'this' part is supposed to reference a PurchasesUpdatedListener, even though you don't explicitly create one to put in these parenthesis. Apparently just using 'this' is supposed to be enough. In the docs and all the examples I've seen, a PurchasesUpdatedListener is never created to put here, it just uses 'this', apparently self-referencing the billingclient being created. This hasn't worked for me though, and I keep getting:
Should I use something else for the (activity) part than (getApplicationContext())? I've tried (this) and various other things here as just the word 'activity' isn't recognized.
Instead setListener(this) put setListener(new PurchasesUpdatedListener(){... }) and implement the required methods (usually AndroidStudio does it automatically)
Or
add .. implements PurchasesUpdatedListener at the end of your MainActivity declaration
the same thing as you can do on many listeners, for example the well known OnClickListener

call arbitrary chained methods on wrapper class

I'm creating a wrapper class for an API because my application will need to call it with different credentials at different times. I started off passing the wrapper a method and arguments and then doing (basically) this when calling it
call
set_credentials
TheAPI::Thing.send(method, args)
ensure
reset_credentials_to_default
end
HOWEVER, I realized that a challenge here is if I need to chain methods; this way I can only call one at a time; so for example I wouldn't be able to to TheAPI::Thing.find(id).delete. (At least, not without re-calling the credentials setter, which is undesirable since I have to fetch a token).
Is there a way using ruby to collect the methods/args being chained onto an object? Or would I simply have to pass in some ugly ordered set of things?
EDIT: Surely activerecord's query builder does something like this, collecting the chained methods before returning, and then after they're all collected, ensuring that as a final step the query is built, called, and returned?
the way to do this is to define a proxy object and to pass that around instead of the actual thing.
In the object itself, hold a reference to the Thing and override method_missing: http://ruby-doc.org/core-2.1.0/BasicObject.html#method-i-method_missing
In the method missing do the thing you are doing today. In a nutshell this is what ActiveRecord is doing.
As far as chaining things, I don't believe it would be a problem as methods that can be chained usually return references to self. When you call the 2nd method in the chain, you actually have done the work for the first one and have an object that has the 2nd method in the chain. The only trick you need to pay attention to is that you want to return a reference to the proxy class that encapsulates the thing instead of the actual return of the thing if you want the chaining to succeed.
Give it a shot and ping me if you run into trouble and I can spin up a fully working example.

How to initialize global variables in TurboGears 2 with values from a table

I need a global variable that I can call from the templates.
I edited app_globals.py in lib directory to declare PATH_TO_IMAGES like this
class Globals(object):
"""Container for objects available throughout the life of the application.
One instance of Globals is created during application initialization and
is available during requests via the 'app_globals' variable.
"""
PATH_TO_IMAGES = ""
def __init__(self):
"""Do nothing, by default."""
pass
Now I can call from any template the image path like this
<img src="${g.PATH_TO_IMAGES}/${p.image}" />
The image path is stored inside a settings table on the app's database, but I can't initialize it from Globals declaration, i get this error:
sqlalchemy.exc.UnboundExecutionError:
Could not locate a bind configured on
mapper
Mapper|Settings|settings,
SQL expression or this Session
My guess is that database binding happens after Globals is initialized. So my questions is, which is the best place to initialize a global variable in TurboGears 2 and which is the best practice to that.
Just use a cached property:
class Globals(object):
"""Container for objects available throughout the life of the application.
One instance of Globals is created during application initialization and
is available during requests via the 'app_globals' variable.
"""
#property
def PATH_TO_IMAGES(self):
try:
return self._path_to_images
except AttributeError:
self._path_to_images = db_session.query(XXX) # Fill in your query here
return self._path_to_images
PS : your question is a generic Python question really. I suggest you read the official Python docs before posting other similar questions.
You probably need to create your own database connection to get this data from the database.
In SQLAlchemy terms, you'll want to create your own engine, session, etc. Just make sure to clean up after you're done.
I would probably do this in app_cfg.py using on_startup to get it into the config, and then stick it in the Globals object later on if you still need to.
You may set PATH_TO_IMAGES to it's definite value once the models are initialized. The sooner being at the end of the 'init_model' function declared in model/init.py.

Resources