How can I get an array with all the fields of an object? Suppose I have an object obj with fields a and b, I want to get an array with all the fields [:a, :b].
You can use fieldnames(obj). See http://docs.julialang.org/en/latest/stdlib/base/#Base.fieldnames.
Related
I have a list of querysets as follows. Is there a way to merge all these query sets of a list into one query set ? The queryset elements of this list are dynamically determined. As in the example below it is three querysets in the list. In next hypothetical iteration it could be 40 querysets.
capture_list =[<QuerySet [<Fields_Model: Fields_Model object (11)>]>,<QuerySet [<Fields_Model: Fields_Model object (12)>]>,<QuerySet [<Fields_Model: Fields_Model object (13)>]>]
You can union these with:
Fields_Model.objects.none().union(*capture_list)
Here the asterisk will thus "unpack" the querysets as individual parameters for the .union(…) call [Django-doc].
A call with .union(…) will only select distinct objects, so no duplicates. If you want to obtain the duplicates, you can the all=True parameter:
Fields_Model.objects.none().union(*capture_list, all=True)
I am supposed to write a class which will allow the user of the script to define what attributes will it's objects have when initializing them by giving it an array of strings like this.
data = Database(tables=['distance', 'speed'])
Then it should be possible to call the class's methods like
data.distance.insert({1: 25, 2: 55})
data.speed.mean()
etc.
I have tried using setattr() this way
data = Database()
tables=['distance', 'speed']
for item in tables:
setattr(data, item, item)
which works, but isn't exactly what it should be.
Any ideas how to do it directly inside the class?
Can anyone tell me how to 'in' clause with Hazelcast predicate. I want to use the following,
Predicates.in('column',value);
I want to pass the value as an ArrayList of values. But it is throwing error as it expects object that implements comparable. Is there any workaround for this?
Predicates.in takes arguments of types (String, Comparable...).
So for the column name you pass a String as you have done.
Comparable... means you can pass the individual values in a comma-separated list, but you can also pass them in an array. An ArrayList won't automatically be converted, but you can do it as follows:
ArrayList<String> values = new ArrayList<>(Arrays.asList("one", "two", "three");
Predicates.in("column", values.toArray(new String[]));
The (new String[]) argument is just to ensure you get back an array of Strings, otherwise you'll get an array of Objects.
I am trying to add all elements of an object into ArrayList. elements of the object are of different type.
e.g. object Employee having attributes like emp_id,name,address,DOB.
I want to store each attribute as an object in an ArrayList. Like,
ArrayList[1] = Employee.emp_id
ArrayList[2] = Employee.name
ArrayList[3] = Employee.address
I want to do it dynamically. Like, in future more attributes are added in this object, without doing a manual work. Is there a way to create an array List?
ArrayList must be of the same type. You can have an ArrayList of type in which you can do what you are doing, since all objects in Java extend Object. If you are trying to store different Object types in an arraylist, however, this is not possible.
You may know that first part, just was a little unclear in your post.
Here's how to do it with objects using reflection:
//make sure you import java.lang.reflect.*
public void addMyFields(Employee e){
ArrayList<Object> list = new ArrayList();
for (Field field : emp.getClass().getDeclaredFields())
{
field.setAccessible(true);
list.add(field.get(emp);
}
}
Just out of curiosity- Is there any way to remove "" entries from an ArrayList object which has been assigned the values using the range?
Dim ArrListChildDetails : Set ArrListChildDetails = CreateObject("System.Collections.ArrayList")
ArrListChildDetails = Range("A4:E6") 'Is it possible?
And If empty Strings("") there,can we remove it in one shot?
Please guide here
ArrayList Reference From MSDN Library
ArrListChildDetails.Clear : Removes all the elements in the ArrayList
Remove :Removes the first occurrence of a specific object from the ArrayList.
RemoveAt :Removes the element at the specified index of the ArrayList.
Choose objects, knowing what they can do, and if they have the properties that you need.