Using a RaspberryPi Pico, V1.19.1.
When I define my timer the first execution works fine, however subsequent periods fail with 'TypeError: 'NoneType' object isn't callable.
Edited; to simplify post.
import machine, time
from machine import Timer
class app():
def __init__(self):
self.pulse = machine.Timer(-1)
self.pulse.init(mode=Timer.PERIODIC, period=1000, callback=self.cb_pulse())
def cb_pulse(self):
print("whai!")
app()
You must specify the callback function themself, so without the ()
# Good
self.pulse.init(mode=Timer.PERIODIC, period=200, callback=self.cb_pulse)
# Bad
self.pulse.init(mode=Timer.PERIODIC, period=200, callback=self.cb_pulse())
With the added (), you are actually passing the result/output of the callback method to the timer.
And as that returns nothing == None, so the timer tries to call 'None', which is indeed not a callable.
Working sample in simulator: https://wokwi.com/projects/354050429354521601
I am using a 3rd party library for logging some events, setup in my component:
In component componentDidMount:
this.evenStream = new thirdPartyLib();
this.evenStream.setArgs(args);
this.evenStream.addQueue('the'); // should be 'the-spell' for misspell page
const event1 = new Event1();
const event2 = new Event2();
this.evenStream.addQueue(event1); // adds events in sequence in queue
this.evenStream.addQueue(event2); // adds events in sequence in queue
this.evenStream.reportEvents(); // reports events previously added to queue, once reported the queue is cleared
In test:
this.wrapper = mount(<component />); // looks good
this.evenStreamInstance = this.wrapper.instance().evenStream; //returns properties of evenStream
this.eventStub = sinon.stub(this.evenStreamInstance, 'reportEvents');
expect(this.wrapper).to.be.ok; // looks good
expect(this.evenStreamInstance).to.exist; // looks good
expect(this.eventStub).to.have.been.called; // returns error
AssertionError: expected reportEvents to have been called at least once, but it was never called
I am sure the events are being logged correctly, I've checked the stream and all looks good, am not sure how to test it. Ideally I would like to test what all events are added to addQueue and then get reported but I am not able to capture that information.
Any help is appreciated. Thanks.
Update
I tried again as per this post with stub, but still getting same error
const eventStub = sinon.stub(new thirdPartyLib());
this.wrapper = mount(<component evenStream={eventStub} />); //looks good
this.evenStreamInstance = this.wrapper.instance().evenStream; //returns properties of evenStream
this.evenStreamInstance.reportEvents = sinon.spy(this.evenStreamInstance.reportEvents);
expect(this.wrapper).to.be.ok; // looks good
expect(this.evenStreamInstance).to.exist; //looks good
this.wrapper.update();
expect(this.evenStreamInstance.reportEvents).to.have.been.calledOnce; // gives error
AssertionError: expected reportEvents to have been called exactly once, but it was called 0 times
I have a requirement where I need to store local settings of user into some cache object. I tried to implement this using $cacheFactory e.g.
var userCache = $cacheFactory('users');
However, when my code hit this line again, it gives me following error:
Error : cacheId 'users' is already taken !
I am not sure, how to check if this ID is already exists, because I need to fetch settings from this cache object on each time component loads.
It's actually listed how to do this on the documentation page:
The $cacheFactory() function is not a "get or create" call, it's only a create.
This is how you would check if the cache has already been created:
if (!$cacheFactory.get('users')) {
var userCache = $cacheFactory('users');
}
which can be changed to
var userCache = $cacheFactory.get('users') || $cacheFactory('users');
I'm building a Drive Realtime project using custom types: https://developers.google.com/google-apps/realtime/custom-objects.
I'm having an issue where the fields in my custom objects are not exported in the model.toJson() output. I'm sure I'm just missing something, but I haven't been able to find any differences with how I'm constructing the custom object vs. the realtime playground or the realtime API documentation.
Sample repro case using the realtime playground is below.
1) go to realtime playground: https://realtimeplayground.appspot.com/
2) open developer console
3) Run the following code
test = function () {}
test.prototype = { init: function() { this.name = 'testName';}};
test.prototype.name = gapi.drive.realtime.custom.collaborativeField('name');
gapi.drive.realtime.custom.registerType(test, 'testType')
gapi.drive.realtime.custom.setInitializer(test, test.prototype.init);
var model = window.doc.getModel()
model.getRoot().set('myTest', model.create(test));
model.toJson()
observed output:
"{"id":"root","type":"Map","value":
{"demo_string":
{"id":"Tq50c9iybcXi","type":"EditableString","value":"Edit Me!"},
"demo_list":{"id":"ZmjclOeUbcXj","type":"List","value":
[{"json":"Cat"},{"json":"Dog"},{"json":"Sheep"},{"json":"Chicken"}]},
"demo_cursors":{"id":"6TJ6Zzd2bcXj","type":"Map","value":{}},
"demo_map":{"id":"ukRRMPHbbcXj","type":"Map","value":
{"key1":{"json":"value 1"},"key2":{"json":"value 2"},"key3":{"json":"value 3"}}},
"demo_custom":{"id":"44nsuMAPbcXk","type":"DemoMovie","value":
{"name":{"json":"Minority Report"},
"director":{"json":"Steven Spielberg"},
"notes":{"json":""},"rating":{"json":""}}},
"myTest":{"id":"Kq4hcV4UbcvW","type":"testType","value":{}}}}"
Expected:
"{"id":"root","type":"Map","value":
{"demo_string":
{"id":"Tq50c9iybcXi","type":"EditableString","value":"Edit Me!"},
"demo_list":{"id":"ZmjclOeUbcXj","type":"List","value":
[{"json":"Cat"},{"json":"Dog"},{"json":"Sheep"},{"json":"Chicken"}]},
"demo_cursors":{"id":"6TJ6Zzd2bcXj","type":"Map","value":{}},
"demo_map":{"id":"ukRRMPHbbcXj","type":"Map","value":
{"key1":{"json":"value 1"},"key2":{"json":"value 2"},"key3":{"json":"value 3"}}},
"demo_custom":{"id":"44nsuMAPbcXk","type":"DemoMovie","value":
{"name":{"json":"Minority Report"},
"director":{"json":"Steven Spielberg"},
"notes":{"json":""},"rating":{"json":""}}},
"myTest":{"id":"Kq4hcV4UbcvW","type":"testType","value":{"json":"testName"}}}}}"
Registering custom types can only occur during the "Pre-Load" phase of the document life cycle. Your code is correct, but is being executed on the document after the document has loaded. This causes the custom object to not be properly constructed, which is why it is lacking the JSON value that you have specified in the init function.
To see the correct flow in action, put a break point on line 88 of static/elements/playground-app.js in the Realtime Playground application. Refresh the page and when execution has paused, run this code from the console:
test = function () {}
test.prototype = { init: function() { this.name = 'testName';}};
test.prototype.name = gapi.drive.realtime.custom.collaborativeField('name');
gapi.drive.realtime.custom.registerType(test, 'testType')
gapi.drive.realtime.custom.setInitializer(test, test.prototype.init);
Resume execution. When the application has fully loaded, run the rest of your code:
var model = window.doc.getModel()
model.getRoot().set('myTest', model.create(test));
model.toJson()
You will see that the outputted JSON matches what you are expecting:
"myTest":{"id":"1OiQd2QoEqBs","type":"testType","value":{"name":{"json":"testName"}}}
This workflow is documented under the "Registering custom types and fields" title of the Custom Collaborative Objects guide.
I am trying to create a RESTish service using grails. I have the following...
def delete(Question q){
def text = request.reader.text;
def slurper = new JsonSlurper();
def result = slurper.parseText(text)
println "Request body is ${text} but the parsed version has a text of ${q.text} whereas the slurper gives me ${result as JSON}"
render noteService.delete(result.key)
}
This gives me an output of...
Request body is {"text":"Test Text","desc":"Test Desc","voteCount":0,"key":0} but the parsed version has a text of null whereas the slurper gives me {"desc":"Test Desc","key":0,"text":"Test Text","voteCount":0}
Why is this not wiring properly? The command object looks as follows...
#Validateable
class Question {
Integer key
String text
String desc
Integer voteCount
}
Is the delete request a GET under the hood or something? Is it expecting some other format?
Update
The create (POST) request is wiring fine which leads me to believe it is something with the differance between the Restangular call and what grails is expecting (So I think my request type guess might be right). My restangular code is simply...
this.delete = function(index) {
var questionToUpdate = _this.questions[index];
questionToUpdate.remove();
}
Also appears to fail with update (put) as well
Grails version is 2.4.3
Problem was that request.reader.text turns of the parsing of command objects.