I may have a few dozen different classes instances with varying attributes that I just need to put patterns/random values into and don't want to have to manually code it all out. Is there a way to loop thru the attributes in a class instance?
For example:
class test_class:
attr1=0
attr2=0
attr3=0
i=test_class()
#How do I do the below in a loop without knowing the attribute names?
i.attr1=1
i.attr2=1
i.attr3=1
I know how I can get a list/dict of the attributes, but how do I do it so I can assign values to those attributes? How do I programmatically loop thru the class instance attributes and assign new values?
if you know the attribute names you can just use setattr
If you just want to get a list of all object's attributes you can use dir
Related
I have an existing tree and I would like to add anytree functionality by adding NodeMixin. The problem is that NodeMixin wants a fixed name 'children' for its sub-elements and I already have a list with a different name.
Another problem (I am using mypy) is that the exiting sub-elements list is not optional - terminal nodes have empty lists and NodeMixin wants 'None' as 'children' of terminal objects.
It will create a lot of changes if I have to rename the object and to deal with the optional nature of the children.
Is it possible to define children as #property or as a reference of the existing sub-elements?
(a) it is possible to use properties for parent and children, (b) it was easy to write custom iterable class for children to account for differences between anytree and my tree iterations.
Update: is_leaf property has to be overridden too - otherwise it will be true for every node.
I need create object with an attribute that is based on a list of other objects. By default when creating a XOM using XSD, Rule Designer assigns lists as vector of java object. I need some orientation how to verbalize and instantiate the list and add the object members.
You need to create a separate Class of Objects you want to store in the list. After that you have to create a list type variable in your Request Class. "listOfMyObjects" as an example. Then, after updating your BOM instance, you will see a new member in it. You can verbolize it as you need. "{list of my objects} from {this}", where the "my object" itself have to be verbolized as well, as an example.
After that you can work with the list in your rules like that:
definition: make this object equals to any of My Object from list of my objects
if: your condition
then: add new object to list of my objects
Is there a bulit-in way to obtain an entity name from the class object of an NSManagedObjectSubclass? I know that this can be readily determined from an instance of a subclass, but I want to ask the class itself. I can write a class function, but I would rather do this introspectively.
You can do it now by executing NSManagedObject.entity().name where NSManagedObject() is your subclass.
Check out mogenerator if you haven't already.
http://raptureinvenice.com/getting-started-with-mogenerator/
It adds a lot of missing features to core data. In particular it keeps you from having to regenerate your entity classes.
You could iterate thru the key values of the entities in the context:
[managedObjectContext registeredObjects];
What is the difference between the System.ComponentModel.BindingList methods Add(object) and AddNew()? The MSDN documentation says this:
Add: Adds an object to the end of the Collection<T>.
AddNew: Adds a new item to the collection.
It seems like both methods add an item to the collection, but Add(object) does it in one shot whereas AddNew() is slightly more complicated. My tests with Add(object) seem to be working, but I want to know if I am using the correct method.
So what is the difference between these methods?
AddNew() creates the object for you (that's why it doesn't have a parameter).
It's designed to be used by grids, which don't know how to create a new object to pass to Add().
AddNew() is very handy (it’s the well-known Factory design pattern) when you implement a class derived of BindingList().
It allows your code to initialize new items with values that depend on the list itself - e.g. a foreign key to the parent object if the binding list contains a list of children.
I've got a SubSonic DAL - works great.
Two classes: TblReceipt and TblReceiptLineItems.
I can create a parallel class of TblReceipt, but seems like a waste, so here's what I need to do:
Have a Class TblReceipt with one additional member, "ReceiptLineItems" - which is simply an ArrayList. This array list will be populated with TblReceiptLineItems types.
So for each Receipt, there are 1..* ReceiptLineItems stored in the array, then the whole thing is serialized.
How can I accomplish this with my existing SubSonic DAL?
A quick code sample would be useful too.
Thank you.
Use a partial class. All classes in Subsonic are defined as partial. What you do is (in a separate file than the one that is generated by Subsonic), you create another part of the partial class with the additional property.
Option 2 here:
http://jamesewelch.wordpress.com/2008/09/24/how-to-use-custom-audit-fields-with-subsonic/