How to access object values using object["key"] approach - arrays

In the code I am referring, object attributes are accessed using the object["key"] method instead of calling object.key to access attribute values.
But when I try to create a simple object array and access attributes using above approach, I am getting below error.
if bank_record.effective_date.strip() == "25/07/2019" and bank_record["description"].__contains__("50036"):
TypeError: 'COM' object is not subscriptable
The reason given for object is not subscriptable error is missing __getitem__ method for the class. But in the code I am referring, it doesn't contain such method for any of the dto classes. But the above object["key"] method works just fine. What am I missing. I have been trying to figure this out for a while.
I just want to loop through a object array and access object attributes and modify them on the run. In order to make the function generic, I want to access these object attributes using object["key"] approach. Please help..

My mistake, I have missed set of steps. In the code I am referring, they are looping a json object array, which is created by dumping, python object array values into a json string and loaded back to a json object array.
excel_dto_list = []
#add objects to the list
#...
json_string = json.dumps([ob.__dict__ for ob in excel_dto_list])
#done in another method
downloaded_object = json.loads(json_string)
for x in downloaded_object:
print(x["comment"])

Related

Set parameters of an object using a variable in the reference visual basic

I would like to achieve the following in visual basic. Is it possible to be able to have the reference change with each iteration inside the for loop? I have an array of object names (as Strings) stored in "objectArray" that contains the names of objects with the property "valuePath". I would like to change the property of "valuePath" for each object from "objectArray" in one for loop. I do not want to statically call each of the objects manually. I can access the property of the object by calling "Me.objectName.valuePath" from inside the method where this code exists.
For item in objectArray
Me.item.valuePath = "some value"
Next
Any help would be greatly appreciated! Thank you.
I ended up saving an array of Objects instead of an array of Strings and that solved my problem. Thank you for your assistance!

Jmeter Property with array of values

Requirement: Need to store 50+ values to a Jmeter property and use with idx
In the case of normal variable we can use Country_1 or Country_2.
Do we have any function to set an array of values to jmeter Property and how to get value using index?
Note: In this case,value has to be used in different thread group.
Your ArrayList initialization is not correct, you should be doing something like:
List myList = Arrays.asList('India', 'USA', 'UK')
There is no putObject method in props shorthand (which is basically an instance of java.util.Properties class so you will need to amend your code like:
props.put('Middle', myList)
Once done you will be able to access individual list members using __groovy() function like:
${__groovy(props.get('Middle').get(0),)} - for first member
${__groovy(props.get('Middle').get(1),)} - for second member
${__groovy(props.get('Middle').get(2),)} - for third member
etc.
Demo:
See Apache Groovy - Why and How You Should Use It article for more details on using Groovy scripting in JMeter tests.

Order collection of array objects

After making some queryes i have a collection of array objects. But after orderByDesc the data transform in hole objects, instead of how it was (array of objects), what it happened? Is like the method orderByDesc tranform the collection in another type.
Ex:
some logic code, queries...etc
in the end
return $total->sortByDesc('date');
After using sortByDesc:
Before to sortByDesc:
Now that is in type of objects is giving some issues working with it in my javascript application (angularjs), it not a array of objects how should it be.
Try to copy the collection when sorting and use values()
$sorted = $total->sortByDesc('date');
dd($sorted->values());
Same result?

Storing array data in firebase, and how ID's are generated

I have a set of objects in my firebase data that all have an array under them. When I create the initial object, I create the initial array with its first object with a line of code like this:
ref.child('items').set([{firstobject: id123}])
this seems to set the id to zero, as the first item in the array. However when I later try to push() a new item to the array with this line of code, I get a more complex id (ZwPiVMIrzbSdvfwxkts).
ref.child('items').push(someNewObject);
In your first line of code, you're calling the Firebase.set() method passing it a JavaScript array that contains a single object.
In your second line of code, you're calling the Firebase.push() method with an object.
Given that Firebase lists/collections are not the same as JavaScript arrays, you end up with a mismatch.
Unlike JavaScript arrays, Firebase's lists are architected to scale well in highly concurrent, multi-user scenarios. I'd recommend to use them instead of arrays from the start.
ref.child('items').push({firstobject: id123});
ref.child('items').push(someNewObject);
With this snippet, all your items will be stored under so-called push ids.

What is the difference between a model object queried by filter and an object queried by get() in Django?

I keep coming across this issue where I am trying to update a record using the update() method.
It always works when I query an object using filter.
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.filter(pk=1)
thing[0].update(**my_dictionary) wrote it wrong in the original question.
thing.update(**my_dictionary)
When I query the object using get() it keeps telling me that the object has no method update()
my_dictionary = {"key":"Val","another":"Val"}
thing = Thing.objects.get(pk=1)
thing.update(**my_dictionary)
Isn't a model object the same in both cases? Why would one have an update method and the other one not? Any insight would be greatly appreciated.
The documentation is very explicit about this:
filter() will always give you a QuerySet, even if only a single object matches the query - in this case, it will be a QuerySet containing a single element.
If you know there is only one object that matches your query, you can use the get() method on a Manager which returns the object directly.
Your first snippet returns a QuerySet, which has an update method. The second snippet returns a model instance, which doesn't.
Note that you have not shown the exact code you are using: thing[0].update would give exactly the same error as the second snippet.
You're using QuerySet.update() and ModelInstance.save().
If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory. For example, instead of doing this:
e = Entry.objects.get(id=10)
e.comments_on = False
e.save()
...do this:
Entry.objects.filter(id=10).update(comments_on=False)

Resources