Kotlin equivalent of python self.__dict__.get(item) - object

I am writing a storage class to store variables with unique IDs. In python, I used this. What would I use in Kotlin?
I already tried using what seemed like equivalents while looking through docs but none worked.

HashMap is what you're looking for. Example:
val hashMap = HashMap<Int, String>()
hashMap.put(12, "str")
hashMap.put(-1, "something else")

Related

How to print/display the hashmap in workday studio?

I am using a hashmap in a Workday studio as below:
props['Classification'] = parts[0].xpath('wd:All_Positions_group/wd:Classification')
props['HashValue'] = props['Classification']
props['HashKey'] = parts[0].xpath('wd:All_Positions_group/wd:Position_ID' )
props['position.hash.map'].put(props['HashKey'], props['HashValue'])
Is there any way to display/print the entire hashmap or the array after the load?
Your design choice to use a HashMap here is probably a mistake - and you should revisit that choice.
If you wish to print out the contents of the map for diagnostic purposes use the toString() method on it
props['position.hash.map'].toString()
which will print out the contents as described at https://docs.oracle.com/javase/8/docs/api/java/util/AbstractMap.html#toString-- . That you didn't look in the JavaDoc before asking this question is another indication that you aren't entirely prepared for using HashMaps in this code and that you should be looking at alternatives

How to get the last KEY from a map in groovy?

I am trying to get the last KEY from this map in groovy. However, it seems like groovy doesn't have a method for returning the last key in the map.
def person = [fname: "john", sname: "smith", age: 25]
I have tried person.lastKey() and person.lastEntry() but these methods seems to be specific for java only.
You can use person.keySet()[-1]​ or person.keySet().last()​
And to get whole entry use entrySet() instead of keySet()
See the Groovy demo online
lastKey is part of the SortedMap interface and the Groovy map literal gives you an LinkedHashMap, which is the explanation, why your attempts failed.
Maps (the interface SortedMap inherits from) are on their own not ordered or sorted. So what you are asking here, will only work for ordered (which the Groovy map literal will give you) or sorted maps, so make sure you have one of those or you will see random elements instead of what you perceive as last. If order is important consider using a list of key-value-tuples instead.

Is there a way to do some kind of overload to like basestring.__rshift__(self, other)

I’m having trouble figuring out how to overload the >> operator for a string. If this can’t be done in this manner I will understand.
I’m using python 3.6
There are plenty of pages showing how I could do it if I wanted to set myclass << string, but I want to do it the other way around with string >> myclass.
I’m having trouble finding reference material for the overload.
Is there a way to do some kind of overload to like basestring.rshift(self, other) to get the result I’m looking for?
basestring.__rshift__(self, other):
return other.doSomething(self)
In Python, you cannot add an operator to a builtin type like str.

Where can I find an overview of how the ec2.instancesCollection is built

In boto3 there's a function:
ec2.instances.filter()
The documentation:
http://boto3.readthedocs.org/en/latest/reference/services/ec2.html#instance
Say it returns a list(ec2.Instance) I wish...
when I try printing the return I get this:
ec2.instancesCollection(ec2.ServiceResource(), ec2.Instance)
I've tried searching for any mention of an ec2.instanceCollection, but the only thing I found was something similar for ruby.
I'd like to iterate through this instanceCollection so I can see how big it is, what machines are present and things like that.
Problem is I have no idea how it works, and when it's empty iteration doesn't work at all(It throws an error)
The filter method does not return a list, it returns an iterable. This is basically a Python generator that will produce the desired results on demand in an efficient way.
You can use this iterator in a loop like this:
for instance in ec2.instances.filter():
# do something with instance
or if you really want a list you can turn the iterator into a list with:
instances = list(ec2.instances.filter())
I'm adding this answer because 5 years later I had the same question and went round in circles trying to find the answer.
First off, the return type in the documentation is wrong (still). As you say, it states that the return type is: list(ec2.Instance)
where it should be:ec2.instancesCollection.
At the time of writing there's an open issue in github covering this - https://github.com/boto/boto3/issues/2000.
When you call the filter method a ResourceCollection is created for the particular type of resource against which you called the method. In this case the resource type is instance which gives an instancesCollection. You can see the code for the ResourceCollection superclass of instancesCollection here:
https://github.com/boto/boto3/blob/develop/boto3/resources/collection.py
The documentation here gives an overview of the collections: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/collections.html
To get to how to use it and actually answer your question, what I did was to turn the iterator into a list and iterate over the list if the size is > 0.
testList = list(ec2.instances.filter(Filters=filters))
if len(testList) > 0;
for item in testList;
.
.
.
This may well not be the best way of doing it but it worked for me.

Returning a list of files in Groovy sorted by modified time

I’m trying to return a list of directories to populate a drop down menu in Jenkins (Scriptler). This is what I have so far:
import groovy.io.FileType
String snapshotBase="/(path-I-wish-to-look-at)/"
def result=[];assert result.size()==0
def releaseDir=new File(snapshotBase)
if ( releaseDir.exists() ) {
releaseDir.eachFile FileType.DIRECTORIES,{
result.add(it.name)
}
}
return result
This returns the list of directories, but for convenience I would like them sorted so that the most recently modified directories appear at the top/beginning of the list. I’m new to Groovy and Java, but I’ve taken a few stabs at some options. I thought maybe there would be some attributes to FileType.DIRECTORIES other than just the name, but I so far haven’t been able to find what I’m looking for. (I guessed at it.date and it.modified, but those appear to be invalid.) I found a snippet of code from virtualeyes that looked promising:
new File(path-to-your-directory).eachFileRecurse{file->
println file.lastModified()
}
I haven’t been able to cobble together the correct syntax, though, to fit that into what I’m doing. I was thinking maybe the Java method lastModified() would hold some solution, but I haven’t been able to find success with that, either.
Instead of adding the names, add the files. Then sort by reverse lastModified and gather the names (*.name is like .collect{it.name})
def files=[]
("/tmp" as File).eachFile groovy.io.FileType.DIRECTORIES, {
files << it
}
def result = files.sort{ a,b -> b.lastModified() <=> a.lastModified() }*.name

Resources