Setting attributes on a collection – backbone js

Just .extend the collection with a meta data storage function. var MyCollection = Backbone.Collection.extend({ initialize: function() { … this._meta = {}; }, model: … meta: function(prop, value) { if (value === undefined) { return this._meta[prop] } else { this._meta[prop] = value; } }, }); var collection = new MyCollection(); collection.add(someModels); collection.meta(“someProperty”, value); … var value … Read more

Why does Python not support record type? (i.e. mutable namedtuple)

Python <3.3 You mean something like this? class Record(object): __slots__= “attribute1”, “attribute2”, “attribute3”, def items(self): “dict style items” return [ (field_name, getattr(self, field_name)) for field_name in self.__slots__] def __iter__(self): “iterate over fields tuple/list style” for field_name in self.__slots__: yield getattr(self, field_name) def __getitem__(self, index): “tuple/list style getitem” return getattr(self, self.__slots__[index]) >>> r= Record() >>> r.attribute1= … Read more

Map equality using Hamcrest

The shortest way I’ve come up with is two statements: assertThat( affA.entrySet(), everyItem(isIn(affB.entrySet()))); assertThat( affB.entrySet(), everyItem(isIn(affA.entrySet()))); But you can probably also do: assertThat(affA.entrySet(), equalTo(affB.entrySet())); depending on the implementations of the maps, and sacrificing the clarity of the difference report: that would just tell you that there is a difference, while the statement above would also … Read more

Best way to get value from Collection by index

You shouldn’t. a Collection avoids talking about indexes specifically because it might not make sense for the specific collection. For example, a List implies some form of ordering, but a Set does not. Collection<String> myCollection = new HashSet<String>(); myCollection.add(“Hello”); myCollection.add(“World”); for (String elem : myCollection) { System.out.println(“elem = ” + elem); } System.out.println(“myCollection.toArray()[0] = ” … Read more

How to sort a Collection?

Collections by themselves do not have a predefined order, therefore you must convert them to a java.util.List. Then you can use one form of java.util.Collections.sort Collection< T > collection = …; List< T > list = new ArrayList< T >( collection ); Collections.sort( list ); // or Collections.sort( list, new Comparator< T >( ){…} ); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)