Skip to content

Dictionary Data Object#1974

Merged
lmccart merged 21 commits into
processing:masterfrom
stalgiag:p5.NumberDict
Jul 3, 2017
Merged

Dictionary Data Object#1974
lmccart merged 21 commits into
processing:masterfrom
stalgiag:p5.NumberDict

Conversation

@stalgiag

Copy link
Copy Markdown
Contributor

Not ready to merge. I just wanted to put this here in progress to get feedback on my design. I also welcome any thoughts on methods that people think would be useful.

closes #422

@lmccart lmccart left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good so far, I made a few small notes

Comment thread src/data/p5.TypedDict.js Outdated

var p5 = require('../core/core');

//TESTED

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can these //TESTED notes turn into unit tests? ;)

Comment thread src/data/p5.TypedDict.js Outdated


//TESTED
p5.TypedDict.prototype.create = function(key, value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with a variable number of params, our convention is to leave the parens empty

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it this a public method? seems like it could be private in which case let's call it _create.

Comment thread src/data/p5.TypedDict.js Outdated
this.count = 0;

if(arguments.length === 1) {
if(keys instanceof Object) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm is it keys or key? or are these two different things?

@shiffman shiffman mentioned this pull request Jun 6, 2017
@stalgiag

Copy link
Copy Markdown
Contributor Author

Okay this is getting close. All of the included unit tests work.

I feel like what it mainly needs at this point is the documentation and methods to write out to a file. I was thinking about adding the ability to write to json and csv. Any opinions on any of the above or anything else about the code?

Comment thread src/data/p5.TypedDict.js Outdated
}
}
else if(arguments.length === 2) {
var key = arguments[0];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code below looks a little familiar... could we just call this._addObj() here with the arguments obj?

@stalgiag stalgiag Jun 16, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm I guess I am a bit confused, I was trying to handle if the user says stringDict.create('string1', 'string2' or they say stringDict.create( {'string1': 'string2', 'string3' : 'string4'} ). Sorry for my confusion!

@lmccart lmccart Jun 16, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right so the first part of the if takes care of your second case: stringDict.create( {'string1': 'string2', 'string3' : 'string4'} ).

the else if part could read like:

else if (arguments.length == 2) {
  this.addObj(arguments);
}

to take care of the case:
stringDict.create('string1', 'string2');

Comment thread src/data/p5.TypedDict.js Outdated
// */
p5.StringDict = function(key, value) {
this.data = {};
this.count = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about the need to keep track of count vs just calling Object.keys(this.data).length?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally!

Comment thread src/data/p5.TypedDict.js Outdated
// * @extends p5.TypedDict
// * @param
// */
p5.NumberDict = function(key, value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are all these constructors generally the same? can we pass this work on to the p5.TypedDict() constructor instead to reduce redundancy?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the latest commit, I used apply to set call the TypedDict constructor which works but creates a certain degree of messiness with the passing of arguments. I tried other approaches like Object.create and setting their constructor to the TypedDict constructor but they weren't retaining the variables made by the constructor like data. Not sure if there is a more elegant way.

Comment thread src/data/p5.TypedDict.js
};

p5.TypedDict.prototype._validate = function(key, value) {
return true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the default method could make use of the type of object.. we already have number or string in the name, instead of having to write a custom validation method for each.

for example, on construction we could store a type:
this.type = m.constructor.name.replace('Dict', '').toLowerCase();

then the test would be
return (typeof value === this.type);

something like that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't figure out how to do this now that both types are falling on the TypedDict constructor. I tried things like p5.StringDict.constructor = p5.StringDict to unexpected results.

Comment thread src/data/p5.TypedDict.js Outdated
} else {
var result = Object.keys(this.data)[0];
for(var i=1; i>this.data.count; i++) {
if(Object.keys(this.data)[i] < result) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be >

Comment thread src/data/p5.TypedDict.js Outdated
*
*
*/
p5.NumberDict.prototype.minValue = function() {

@lmccart lmccart Jun 16, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have the feeling these four blocks could be reduced into one function, since only one symbol or line changes between them. for example, if it's just a < that flips.. you can't pass operators as arguments but you could write a function that takes 1 or -1, and multiply both sides of the comparison by that operator. multiplying by -1 would have the effect of flipping the < to a >.

@lmccart

lmccart commented Jun 16, 2017

Copy link
Copy Markdown
Member

looking good! I left a few comments. I love the idea of being able to export to json and csv.

@stalgiag

Copy link
Copy Markdown
Contributor Author

I made a save function but the way it currently works is that the user needs to pass in either 'csv' or 'json' as the first argument. Making people write it as a string might be a bit confusing, not sure. Other options I can think of are either making a constant or making separate saveJSON & saveCSV.

@lmccart

lmccart commented Jun 20, 2017

Copy link
Copy Markdown
Member

we actually have saveJSON() and saveTable() methods in p5 already, so it may make sense to duplicate this syntax.

@stalgiag

Copy link
Copy Markdown
Contributor Author

Don't know how I overlooked those, I should probably duplicate that syntax but I should also just use them to do the work. slaps head

Didn’t use p5.prototype.saveTable in TypedDict.saveTable because the
data object would have to be converted to a p5.Table first which seemed
like overkill
@lmccart

lmccart commented Jun 26, 2017

Copy link
Copy Markdown
Member

@mlarghydracept is this ready for final review?

@stalgiag

Copy link
Copy Markdown
Contributor Author

@lmccart Thanks for checking in! I am going to do another once over, write comments/examples, and a few more unit tests. I will do that this week and let you know when it is finished

@lmccart

lmccart commented Jun 30, 2017

Copy link
Copy Markdown
Member

@mlarghydracept great, I'm going to close this until it's ready for review. just reopen whenever it is. this way I'll get the email notification to review.

@lmccart lmccart closed this Jun 30, 2017
@stalgiag stalgiag reopened this Jul 3, 2017
@stalgiag

stalgiag commented Jul 3, 2017

Copy link
Copy Markdown
Contributor Author

I think this is ready for review!

A few notes -- I said this elsewhere in you comments on my code but I couldn't put all of the construction work in the TypedDict constructor because of the way the initializing data (the first arguments) were being retained. I tried a bunch of different approaches but maybe I am missing something.

Also this is my first time writing a new file with new functions so I wasn't completely sure what I was doing with the commenting and documentation work. I tried to follow examples from other files.

Comment thread src/data/p5.TypedDict.js
* @return {Number|String} the value stored at that key
*
* @example
* <div><code>

@lmccart lmccart Jul 3, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the examples aren't drawing anything, let's add class="norender" so we don't have blank gray squares in the ref.
https://github.com/processing/p5.js/wiki/Inline-documentation#adding-example-code

Comment thread src/data/p5.TypedDict.js
*
*/

p5.TypedDict.prototype.clear = function(){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be simplified as
this.data = [];
i think?

@lmccart

lmccart commented Jul 3, 2017

Copy link
Copy Markdown
Member

@mlarghydracept looks good! thanks for this big contribution. I'm going to go ahead and merge. if you can make a later pull request with the two small notes I made, that'd be great.

will you make a note of that question about constructors and we can look at it again when we're in the same place? I couldn't quite remember what I was thinking so maybe my point didn't even make sense, but I want to take one last look together at some point just in case there was some further refinement we could make.

@lmccart lmccart merged commit 14b14fd into processing:master Jul 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data objects

2 participants