SimpleAttribute instead of Attribute in Rapidminer script? - groovy

I am trying to extract an Attribute from an ExampleSet in a RapidMiner 'Execute script' like this:
ExampleSet exSet = input[0];
Attributes attrs = exSet.getAttributes();
Attribute attr = attrs.getAttribute("h_area");
but then I get an error and it says that attrs is not a Attributes but a SimpleAttributes object.
This works:
Attribute[] attrs2 = exSet.createRegularAttributeArray();
Attribute attr2 = attrs2.getAt(1);
What is the correct way to get an Attribute from an ExampleSet?

From these docs, it looks like the getAttributes() call will return an object implementing the Attributes abstract class, which SimpleAttributes is, so it looks pretty fair at this stage. However, the getAttribute() method doesn't look like it's defined in either object. I can't test this here and now, but have you tried the following:
ExampleSet exSet = input[0];
Attributes attrs = exSet.getAttributes();
Attribute attr = attrs.get("h_area");

Related

How to get the content of the class attribut with Python/XPATH?

Is it possible to get the content of the class attribut and how to implement this?
The class name it-self.
tform = driver.find_elements_by_xpath("//table[#id = 'table-type-1']/tbody/tr/td[#class ='form col_form']/div/a[#class]")
Print(tform.text) returns the correct number of hits, but strings are empty
linkClass = driver.find_elements_by_xpath("//table[#id = 'table-type-1']/tbody/tr/td[#class ='form col_form']/div/a[#href]").__getattribute__('class')
AttributeError: 'list' object has no attribute 'class'
Try this.
linkClass = driver.find_element_by_xpath("//table[#id = 'table-type-1']/tbody/tr/td[#class ='form col_form']/div/a").get_attribute('class')
print(linkClass)

How to set `invalidAttributeNamePrefix` value in Java?

Suppose I'm cleaning some html using HtmlCleaner (v2.18) and I want to set the property invalidAttributeNamePrefix (see section Cleaner parameters) to some value, i.e.: data-.
This way an attribute my-custom-attr="my-value" in the HTML will be transformed to data-my-custom-attr="my-value".
How can I do that? I wasn't able to find any example for the Java usage.
You can take as reference this piece of code:
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties properties = cleaner.getProperties();
properties.setOmitComments(true);
// properties.setInvalidAttributeNamePrefix("data-"); there is no such method
// html is a declared variable which contains some html content
TagNode rootTagNode = cleaner.clean(html);
XmlSerializer xmlSerializer = new PrettyXmlSerializer(properties);
String cleanedHtml = xmlSerializer.getAsString(rootTagNode);
Upgrading to version 2.22 solves this.
Now it can be done
// ...
properties.setInvalidXmlAttributeNamePrefix("data-");
//...

ExpandoObject - why a Type behaves differently?

One for the gurus, please convince me/us what is going on.
List<ExpandoObject> peopleList = new List<ExpandoObject>();
dynamic expandoObj1 = new ExpandoObject();
expandoObj1.id = 1;
expandoObj1.first = "fred";
expandoObj1.last = "krugger";
peopleList.Add(expandoObj1);
dynamic expandoObj2 = new ExpandoObject();
expandoObj2.id = 2;
expandoObj2.first = "george";
expandoObj2.last = "benson";
peopleList.Add(expandoObj2);
//test access the props
var expObj = expandoObj1;
var name = expObj.first;
var expObj2 = peopleList[0] as dynamic;
var name2 = expObj2.first;
IDictionary<string, object> expObj3 = peopleList[0] as ExpandoObject;
var name3 = expObj3["first"];
var expObj4 = peopleList[0] as ExpandoObject;
//var name4 = expObj4.first; //THIS DOESN'T WORK - ExpandoObject does not contain a definition for 'first' etc...
In all cases, the LEFT-HAND SIDE is a System.Dynamic.ExpandoObject;
Why then, on the 4th case expObj4, i cannot access the property expObj4.first ?
ExpandoObject is a sealed class which stores data in a dictionary. It implements IDynamicMetaObjectProvider interface which provides dynamic behaviour to the classes implementing it. It also implements IDictionary interface which provides dictionary like behaviour to it. It is supposed to be checked and validated at compile time.
dynamic is a type which is not supposed to be checked by the compiler at compile time. It is checked and breaks at runtime. At compile time, a dynamic entity is assumed to support any operation. So, when you say, it is a expandoobject, the field called first does not get appended to object itself.
Check source code of expando object here
https://github.com/Microsoft/referencesource/blob/master/System.Core/Microsoft/Scripting/Actions/ExpandoObject.cs
Think of dynamic behavior like an object. You can put any type there. When you are adding to list, you are adding to list as dynamic, but the inherent type of item being added is ExpandoObject. So, you are able to cast it back to ExpandoObject.
When you say,
expandoObj1.first = "fred";
it is same as saying,
expandoObj1.Add("first", "fred");
When you used
var expObj = expandoObj1;
var name = expObj.first;
you were using expandoObject in dynamic form. So, you were able to access properties directly. When you cast it to ExpandoObject class, you were using actual ExpandoObject class which stores fields in Dictionary, so dot(.) notation does not work.
var expObj4 = peopleList[0] as ExpandoObject;
variable on left hand side is still ExpandoObject, not a dictionary. ExpandoObject exposes its members through collection search.
var name4 = expObj4.Where(t=>t.Key == "first").First().Value;
When you cast it to a dictionary, it works like a dictionary.
IDictionary<string, object> expObj3 = peopleList[0] as ExpandoObject;
var name3 = expObj3["first"];
When you cast it to a dynamic, you can access these keys like they are properties of the class.
Further reference
Dynamically adding properties to an ExpandoObject
This is because the variable expObj4 is declared as ExpandoObject and not as dynamic. This is an important difference.
Try this:
dynamic a = new ExpandoObject();
a.Name = "Test";
This compiles, but the following doesn't:
ExpandoObject a = new ExpandoObject();
a.Name = "Test";
you get this:
CS1061 'ExpandoObject' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'ExpandoObject' could be found
The variables you have that are related to this are:
expandoObj1 - dynamic
expandoObj2 - dynamic
expObj1 - dynamic
expObj2 - dynamic
expObj3 - dictionary, but you use dictionary access here, not dot-access
The magic "let's see if we can access the thing at runtime" code of the compiler only kicks in if the expression or variable is dynamic. ExpandoObject is just a type that supports this.

What is the purpose of the SvgElement.tag(String tag) constructor?

There is no documentation in the API doc for the constructors. I would like to understand the purpose/use cases for SvgElement.tag() .
The SvgElement.tag(String tag) constructor creates a new SvgElement for a corresponding tag value.
For example:
var foo = new SvgElement.tag('view');
print(foo is ViewElement); // prints 'true'
would create a new SvgElement specified by the <view> tag.
This means that the above code is the same as:
var bar = new ViewElement();
print(bar is ViewElement); // prints 'true'
See also the tag constructor from the superclass Element.
Use cases for this constructor are places where you get the value of the tag from text and want to generate a new element of that tag value.
You might get the tag from parsing the DOM, or maybe from a different API. The tag constructor is a way to write DOM code in a "Darty" way (with objects and classes) while being able to work with DOM elements via text.
In many cases, it is preferable to create this Element object instead of say, using innerHtml to set the DOM inside of another Element.
Compare:
var someTagName = 'view';
var someDomNode = query('#id');
// BAD
someDomNode.innerHtml = '<$someTagName> ... </$someTagName>';
// GOOD
var myElement = new SvgElement.tag(someTagName);
someDomNode.append(myElement);

convert string data into Type for List<>

I'd like to take a string and create a typed List based on the
"Type" documented in the string. For example, suppose str is "System.string". I'd
like the method to create a List for me. Of course, the string could contain text
that "references" any object in the assembly. I've unsuccessfully tried the following:
Type classType = Type.GetType(str);
List<classType> wgList = new List<classType>();
I get a message stating that "classType is a field but is used like a type"..
How do I fix this up to get what I need ?
The following code provided a nice solution:
Type ty = Type.GetType(ItemType);
wgList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(ty));
You can not do that or i don't understand what is your purpose?
Use List< object > instead.
You may use Extension method is Cast< T>() in System.Linq namespace to convert your List< object > to List< T >.
List<Entry> entries = wgList.Cast<Entry>().ToList()

Resources