update map with another map [duplicate] - dictionary

This question already has answers here:
Merge maps with recursive nested maps in Groovy
(4 answers)
Closed 4 years ago.
In groovy, I want to update (left merge) a map with another.
def item = [attributes:[color:'Blue', weight:500], name:'hat', price:150]
def itemUpdate = [attributes:[size: 10]]
item << itemUpdate
println item
Gives:
[attributes:[size:10], name:hat, price:150]
But what I want is:
[attributes:[color:'Blue', weight:500, size:10], name:'hat',
price:150]
I have also tried:
item += itemUpdate
or using Updating groovy object fields from a map.
None fulfils my requirements; in python the method would be the update() method.
Edit: I'm wrong about python actually.

What you're doing is effectively overwriting the attributes entry.
What you want to do, instead, is:
item.attributes = item.attributes + itemUpdate
You can even do:
item.attributes += itemUpdate
Both of which yield the expected
[attributes:[color:Blue, weight:500, size:10], name:hat, price:150]

Related

Coloring cells in Vaadin Grid that uses OpenCSV

I am attempting conditional coloring/formatting for a Vaadin Grid that sources its data from a file (using OpenCSV) rather than a POJO. I was looking at Change color of cell in Grid (Vaadin) to see if this could be modified to support CSV instead, but have so far come up empty.
Can this be done with the cell style generator method? I am referencing https://vaadin.com/blog/read-and-display-a-csv-file-in-java for the reading of the csv data.
You are looking at the questions/blogs from different versions; the SO
question is about Vaadin 8 - the blog about Vaadin Flow (10 and up, but
most likely v14 in this case).
Styling rows and/or columns is documented in the Grid:
https://vaadin.com/docs/latest/components/grid/#styling-rows-and-columns
You can style individual cells based on the data, for example, to
highlight changes or important information.
...
grid.setClassNameGenerator(person -> {
if (person.getRating() >= 8)
return "high-rating";
if (person.getRating() <= 4)
return "low-rating";
return null;
});
Note: there is also setClassNameGenerator for Column.
Going off the CSVReader and Vaadin documentation examples with the generic Person class, you can do something like:
grid.addColumn(str->str[columnIndex])
.setKey(header)
.setHeader(header)
.setClassNameGenerator(str -> {
if (Integer.valueOf(str[columnIndex]) > 8){
return "condition1";
}
if (Integer.valueOf(str[columnIndex]) < 8){
return "condition2";
}
return null;
});
Similar to what is done in the cookbook example, you can then define those conditions in the corresponding css file.

Extracting dict keys from values

I am still learning about python and I face some trouble extracting data from a dict. I need to create a loop which check each values and extract the keys. So for this code I need to find the nice students. I am stuck at line 3 #blank.
How do i go about this?
Thanks in advance
class = {"James":"naughty", "Lisa":"nice", "Bryan":"nice"}
for student in class:
if #blank:
print("Hello, "+student+" students!")
else:
print("odd")
Uses dictionary methods "keys(), values(), items()":
def get_students_by_criteria(student_class, criteria):
students = []
for candidate, value in student_class.items():
if value == criteria:
students.append(candidate)
return students
my_class = {"James":"naughty", "Lisa":"nice", "Bryan":"nice"}
print(get_students_by_criteria(my_class, "nice"))
Warning to the word "class" it is a keyword reserved for python programming oriented object

Update dictionary key inside list using map function -Python

I have a dictionary of phone numbers where number is Key and country is value. I want to update the key and add country code based on value country. I tried to use the map function for this:
print('**Exmaple: Update phone book to add Country code using map function** ')
user=[{'952-201-3787':'US'},{'952-201-5984':'US'},{'9871299':'BD'},{'01632 960513':'UK'}]
#A function that takes a dictionary as arg, not list. List is the outer part
def add_Country_Code(aDict):
for k,v in aDict.items():
if(v == 'US'):
aDict[( '1+'+k)]=aDict.pop(k)
if(v == 'UK'):
aDict[( '044+'+k)]=aDict.pop(k)
if (v == 'BD'):
aDict[('001+'+k)] =aDict.pop(k)
return aDict
new_user=list(map(add_Country_Code,user))
print(new_user)
This works partially when I run, output below :
[{'1+952-201-3787': 'US'}, {'1+1+1+952-201-5984': 'US'}, {'001+9871299': 'BD'}, {'044+01632 960513': 'UK'}]
Notice the 2nd US number has 2 additional 1s'. What is causing that?How to fix? Thanks a lot.
Issue
You are mutating a dict while iterating it. Don't do this. The Pythonic convention would be:
Make a new_dict = {}
While iterating the input a_dict, assign new items to new_dict.
Return the new_dict
IOW, create new things, rather than change old things - likely the source of your woes.
Some notes
Use lowercase with underscores when defining variable names (see PEP 8).
Lookup values rather than change the input dict, e.g. a_dict[k] vs. a_dict.pop(k)
Indent the correct number of spaces (see PEP 8)

Empty string returned while trying to retrieve the text contents of an element of QListView

I am trying to automate a PyQt based application that uses a QListView in icon mode using Squish
Inorder to select a specific item in the view, i need to first identify the text of the item. I am using the below code to do the same
targetList = waitForObject("{name='someListView' type='QListView'}")
object.children(targetList)[11].model().data(object.children(targetList)[11]).toString()
Here object.children(targetList)[11] is of type QModelIndex
But the above code always returns an empty string.
Is there any other way to retrieve the text data
I would rather use QListView API only. So, in case of valid targetList object, i.e. it's found by waitForObject function, I would write:
targetList = waitForObject("{name='someListView' type='QListView'}")
model = targetList.model()
col = targetList.modelColumn
idx = model.index(11, col)
itemString = idx.data().toString()
Here is an example:
def main():
# Use itemviews example included in Squish:
startApplication("itemviews")
# Configure QListView to use IconMode as
# mentioned by original poster:
obj = waitForObject("{occurrence='2' type='QListView' unnamed='1' visible='1'}")
obj.setViewMode(QListView.IconMode)
# Get desired item via object.children();
# this yields a "wrapped" QModelIndex which
# features a property "text" which contains
# the desired text:
it = object.children(obj)[4]
test.log("item text: %s" % it.text)

Error with Python 3 in RPi [duplicate]

This question already has answers here:
Troubleshooting "TypeError: ord() expected string of length 1, but int found"
(2 answers)
Closed 3 years ago.
First, sorry if I have a bad English or if anything is wrong, this is my first "post".
I'm trying to use a USB gamepad to turn an LED on and off with gpiozero.
I have an error while trying to execute the program:
import sys
from gpiozero import LED
led = LED(17)
pipe = open('/dev/input/js0', 'rb')
msg = []
while 1:
for char in pipe.read(1):
msg += [ord(char)]
if len(msg) == 8:
if msg[6] == 1:
if msg[4] == 1:
print ('button', msg[7], 'down')
led.on()
else:
print ('button', msg[7], 'up')
led.off()
msg = []
Error:
File "script.py", line 13, in <module>
msg += [ord(char)]
TypeError: ord() expected string of length 1, but int found
What can I do to solve this?
Thanks.
Looks like to are trying to add items to a list. Make use of append().
Documentation:
list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].
Example of append vs extend for future use:
append vs. extend
In the end I didn't get it to work with the version I was using, I just used a different python version instead.
Thanks for the help.

Resources