How to change ElementTree attribute keys? - attributes

I want to change the attributes of an existing element, and not just the values, but add/remove/change the keys too. For example,
<frame_geometry name="border" has_title="false"/>
I would like to add: rounded_top_left="5" etc...
Is modifying element attributes' keys after creation possible?
If not, perhaps I could use a workaround, something like storing all the element's attributes in a temporary dictionary and then creating a new element from that +/- any desired changes?
This solution is not desirable however because the elements I need to modify have several subelements also...

I figured it out. So simple.
Add new attribute:
element.attrib['newkey'] = 'newvalue'
will add an attribute to existing element.
To remove an existing attribute:
del element.attrib['unwanted_key']
As far as modifying existing keys, I still don't know if that's possible, but with add/remove you can easily work around.

I've had success by iterating through the elements I was hoping for a .rename style function
For element in XMLData:
if element.tag = Searching:
element.tag = "NewTag"
#Now its element.NewTag

Related

Python: Modifying a List of Elements while Iterating

I have the below list:
list =[5,10,15,20,25,30]
...and I would like to keep the same list but have its elements increased by 5. How could we do this without creating a new list? What kind of loop should I create for this? Is there another way?
Thanks!
If you want to add another list to the existing list you can use extend in-build functions.
please refer below link to know more about that:
[https://www.geeksforgeeks.org/append-extend-python/][1]

Ignore ID in Attribute Selection

I have a dataset with an unique id for each instance. Now I am running some Attribute Selection on that dataset. Concrete I use the CfsSubsetEval for selecting the relevant attributes.
The problem is that I don't want to include the id in this attribute selection, but I need it later to write out a .arff File (which should contain the id and the selected attributes).
I found some posts which used the FilteredClassifier. But in my case I want to have something like a FilteredAttributeSelection. Something like an exklusive view on the data for the AttributeSelection Algorithm.
Is there a way solving this problem?
Thanks in advance!
I solved the problem by copying the attribute vector (the ids) before the filter. After selecting the attributes I restore it.

Clearing attributes in Tritium

What's the best way to clear all attributes of an element with Tritium? Do I have to enumerate all existing attributes with attribute(%name, "") ?
The best way is to use the remove function by itself after selecting an item.
remove("#*")
You could even select the node first
remove("//node/#*")

XmlDocument SelectNodes: find an element by an attribute value only

I want to add a child node to an element in XmlDocument. For the life of me I can't seem to find a way to all the elements where an attribute with a known value exists without knowing the name of the element.
However I can't get the xpath working.
doc.SelectNodes(/XXXXXX[#Name='the_value_I_want'])
What goes in XXXX please?
I'm more than happy to switch this function Linq2Xml if it's easier.
doc.SelectNodes("//node()[#Name='the_value_I_want']")
or just
doc.SelectNodes("//[#Name='the_value_I_want']")
Just use *
doc.SelectNodes(/*[#Name='the_value_I_want'])
OR
doc.SelectNodes(//*[#Name='the_value_I_want'])

NSArrayController that is sorted and unique (no duplicates) for use in a pop-up in a core-data app

I have core data app with an entity OBSERVATION that has as one of its attributes DEALNAME.
I want to reference through Interface Builder or by making custom modifications to an NSArrayController a list of unique sorted dealnames so that I can use them in a pop-up.
I have attempted to use #distinctUnionOfSets (and #distinctUnionOfArrays) but am unable to locate the proper key sequence.
I can sort the ArrayController by providing a sort descriptor, but do not know how to eliminate duplicates.
Are the #distinct... keys the right methodology? It would seem to provide the easiest way to optimize the use of IB.
Is there a predicate form for removing duplicates?
Or do I need to use my custom controller to extract an NSSet of the specific dealnames, put them back in an array and sort it and reference the custom array from IB?
Any help would be appreciated. I am astounded that other have not tried to create a sorted-unique pop-up in tableviews.
You need to take a look at -[NSFetchRequest returnsDistinctResults]. That is the level you need to be handling the uniquing of data.
Although I do not have a definitive answer for you, I think there are two ways you can go about it.
The way you already started. You need to bind the contents array of the PopUp button, not just against the arrayController.arrangedObjects, but continue on the path and somehow filter only objects with distinct "DealName"s. This means - the arrayController presents ALL the entities (and may sort them for you) but the PopUp button will have its contents filter via some sophisticated binding to the array controller.
Make your filtering at the ArrayController level (as suggested in another answer here). Here it depends how you set up the array controller. If It is set up to use an "Entity" (vs. "Class") which means the array controller will fetch CoreData entities directly - you can modify its "Fetch" to only bring a subset of the "OBSERVATION" entities with distinct values of "DEALNAME". I don't know how to control WHICH entities are filtered out in this case. Otherwise, you can setup the arrayController to work with "Class" objects, and then you can fetch the entities yourself (in code) and populate the arrayController programmatically, with just the entities you like.
In the second option, the Popup button should be bound normally to the arrayController's arrangedObjects.

Resources