How can we access a property value within a #classmethod? For example:
class Account(polymodel.PolyModel):
someprop = ndb.StringProperty(required=True)
#classmethod
def get_or_create_someprop(cls):
if not cls.someprop:
# create someprop
else:
return cls.someprop
In this example code above, I am trying create someprop if it doesn’t exist, or return it if it already exists. I assumed that the above code would achieve this. However, the first step I need to do is access the someprop value from within the classmethod. Using cls.someprop does not actually return the value of someprop but instead returns StringProperty('state').
I have tried to use this and self which are undefined.
So, is it possible to access a property value of an entity using a classmethod? If so, how?
In general you cannot do this from a class method because a property belongs to an object, i.e. an instance of the class (the class is just the object generator). In other words you need the self argument to refer to the object and its properties.
In your particular case the class is an entity model (the blueprint for creating entities), not an entity and you can only refer to a property of an entity itself.
But you should be able to achieve what you seek simply by not declaring it a class method - then it becomes a method of the object/entity and in that case you can reference the entity's property via self instead of cls: self.someprop.
I'd make the check a bit more specific, though, to cover the case in which the property has a value like 0 or an empty string which is interpreted by python as False in a logical check: if self.someprop is None instead of if not self.someprop.
Related
I have a class that has property like this
public string Foo
{
get { return _foo; }
set
{
if (!string.Equals(_foo, value))
{
_foo= value;
OnPropertyChanged();
}
}
}
When I create object with _fixture = new Fixture {OmitAutoProperties = true};, I expect it has value but it's null and setter never hit. Do I miss something?
This is by design. As the documentation states:
Gets or sets if writable properties should generally be assigned a value when generating an anonymous object.
In other words, in AutoFixture, the term auto-property refers the the feature of AutoFixture that automatically populates writable properties. Perhaps a better word would have been DoNotAutomaticallyPopulateProperties.
I can understand the confusion, as in C#, auto-property can also be interpreted as meaning Auto-Implemented Properties.
Frankly, AutoFixture's terminology should, perhaps, have been chosen with greater care, but in all these years, I don't think this has ever been brought to my attention before.
Specifically in the OP Foo is a writable property, and when you disable auto-properties, the setter is never invoked.
The OmitAutoProperties setting determines if a writable property should be set or not:
Gets or sets if writable properties should generally be assigned a value when generating an anonymous object.
So, if it's true, AutoFixture does not try to set any property values and that is by design.
My class Posts has a column that is a pointer to another class called Styles. Each post must be associated to a Style object as a rule of thumb.
My problem: I can't get only the posts that are associated to one or more styles.
My object selectedStyles, that is an array of PFObjects that already contains the style objects I would like to use to match the query. So populating the selectedStyles is not an issue, but how to use it to produce my query is.
What I am doing at the moment is:
override func queryForTable() -> PFQuery {
var query = PFQuery(className: "Posts")
query.whereKey("bellongsToStyle", containsAllObjectsInArray: [selectedStyles])
query.orderByDescending("createdAt")
return query
If I could translate the whereKey method in plain english I would say:
...contains - ANY OF THE - PFObjectsInArray [selectedStyles]
I am not so sure if that is possible... any ideas?!
I think I am too late for this but you can just add
[IncludeKey:"bellongsToStyle"]
when you are querying in your 'Posts' class
don't need to run any extra query for that
here's a small reference
http://blog.parse.com/announcements/queries-for-relational-data/
Is there any way I can give AutoFixture an instance of an object and have it go through all the setters and set random data? The wiki examples only show how to obtain an instance from AutoFixture, e.g.
var autoGeneratedClass = fixture.Create<ComplexParent>();
My example use case is a factory method which generate instances of objects with dynamic properties based on a configuration. I want to test that my methods correctly, detect and interact (e.g. copy) these dynamic properties.
dynamic dynamicPropertyObject1 = factoryMethod(configuration);
dynamic dynamicPropertyObject2 = factoryMethod(configuration);
dynamicPropertyObject1.propA = random.Next();
dynamicPropertyObject1.CopyTo(dynamicPropertyObject2);
Assert.That(dynamicPropertyObject2.propA, Is.EqualTo(dynamicPropertyObject1.propA);
Thanks
AutoFixture has a lot of built-in heuristics for creating objects, including some for factory methods.
If AutoFixture finds no public constructors on a type, it starts to look for factory methods; i.e. static methods that return objects of the type of the class that defines that static method, e.g.
public class Foo
{
public static Foo CreateFoo();
// ... other members
}
If, on the other hand, a factory method exists on another class, you'll need to help AutoFixture a bit. The easiest way would be to use the Customize method:
fixture.Customize<Foo>(c => c
.FromFactory(() => FooFactory.CreateFoo())
.WithAutoProperties());
When you subsequently ask a Fixture object for a Foo object, FooFactory.CreateFoo() will be invoked, and because of WithAutoProperties that object will be populated with data created by AutoFixture.
I have a class A that has a db.ReferenceProperty referencing a type of entity that is defined later in the file. How do I refer to B from A?
If you can't move B above A, then you can use the following workaround:
foo = db.ReferenceProperty(reference_class=db.Model)
This essentially allows you to reference any class that extends db.Model. You can add validation logic if needed.
You also cannot use the collection_name attribute to back reference A from B.
i have a simple extension of a BaseModelData in a form of MyModel, and i can call new MyModel().getObj1().getObj2() to get to obj2's string value. i have a number of MyModel instances, so i would like to populate a ComboBox instance with an obj2 value from each MyModel instance. first, i called ComboBox.setDisplayField("obj1.obj2"), because using such hierarchical property approach works for TextField.setName() cases. then, i took a store which contains all MyModel instances, and set it to a ComboBox via setStore(). however, the combobox is empty. it looks as though setting the aforementioned property via ComboBox.setDisplayField() does not work the same way as it does for TextField.setName(). i tried using my own instance of ListModelPropertyEditor, but without success. so what are my alternatives?
thank you for your time!!!
I am not sure about accessing hierarchical data from ComboBox.setDisplayField() method, but can you can achieve it by adding a new method say getObj2() in MyModel class, which will essentially represent obj1.obj2.
public Obj2 getObj2() {
return getObj1().getObj2(); //with possible null checks
}
Now you can call ComboBox.setDisplayField("obj2") and get the work done.