Protege: Create new class out of an attribute - protege

So, I would like to do what the title suggests without manually rewriting everything. Here's what I want in Protege terms:
Given a file where there are:
A Class MyClass
Some Instances I of MyClass
Some values V_i such as for every I: I hasMyAttribute V_i
I am looking for a way to create this new file:
A Class MyClass
A Class MyAttributeClass
V_i Subclasses of MyAttributeClass where V_i are the values of hasMyAttribute
Now, I is an instance of Subclass V_i if in the original file I hasMyAttribute V_i
Is this possible?
Thanks in advance.

Since there's no other answer available I will describe my way around the problem.
I did a bit of manual work:
I add MyAttributeClass
I add one by one the V_i subclasses
On the description tab, I define "Equivalent to:" hasMyAttribute value V_i
Start the Reasoner(Hermit in my case)
File -> Export Inferred Axioms
Hope that will be helpful.

Related

calling help(MyClass) also shows base class attributes: how to avoid that?

MyClass is derived from "list": MyClass(list)
I would like to document MyClass nicely.
Unfortunately, when trying help(MyClass),
I get my own documentation, but I also get a lot of stuff about "list".
Would there be a simple way to control that?
I read something about metaclasses, but I was unable to do something.
Thanks for your suggestions,
Michel
Well, that is what help does. It introspects into your class and show the name and the associated __doc__ for each callable attribute in the class, and that is not customizable.
Attributes of the superclass are considered attributes of the class, and are reached in the introspection Python's help do.
Metaclasses could even be used to customize the output one gets when he does "dir" on your class - but they do not change the output of the help text. To change "dir" output, create a metaclass implementing a __dir__ method, and return a list of what you want visible as dir's output.
class M(type):
def __dir__(self):
return [] # blank dir contents
class MyList(list, metaclass=M):
...
On the other hand, the help contents displayed for list attributes are not that verbose, and can actually be helpful - if you override any methods to do something different than described, the incorrect text won't show anyway. So you might just live with it.
Another tip is that instead of subclassing list you might prefer to subclass collections.abc.MutableSequence instead, and use an inner agregated (normal) list to keep your data: that will require you to implement a lot less methods to have your class working properly as a sequence and is preferable in most cases to subclass list. That won't change help's verbosity though.

How do I define a `NewType` that's subscriptable, like `List[int]`

I've tried things like NewType['SomeType', (SomeClass, typing.Container)], and it doesn't work.
Suppose I have a class that's really simple, as follows.
class A:
def __init__(self, a):
self.a = a
Now I want to annotate a function that returns this type as NewA[int], i.e. using __getitem__, which I obviously haven't defined in A above, but it adds an extra layer of description. Is this possible? And if not, is there a reason I shouldn't try to do something like this?
The answer is to use Generic, as defined in the standard library's typing documentation.

Is there any way to interpolate colors when using a structure grid in VTK?

I want to make a color map on vtkstructuredgrid and I need the colors to be interpolated between cells. The other option is using point data but when ever I use
structuredgrid->PointData()->SetScalars(Floatarray);
it says I cant have a pointer to a incomplete class type.
Any help would be greatly appreciated.
Your approach should work...
However, PointData is not a method, for the vtkStructuredGrid class: you should avoid (), and that's the reason for the error (Pointer to incomplete class type is not allowed).
Moreover, PointData is protected, in the "standard" definition of vtkStructuredGrid, and you should derive the entire class to access it from your code.
Before trying that, by the way, can you try with
structuredgrid->GetPointData()->SetScalars(Floatarray);
?
It should work too (not sure about the parameter type passed to SetScalar(), BTW).

Sectioning / grouping attributes inside a class

I have a class with a large number of attributes; I would like to present them grouped (rather than in a flat list), with a section-like appearance inside the class documentation.
Is this possible with docutils/sphinx? Any suggestion to achieve something visually similar, perhaps by inserting dummy attributes?
Regular reST section headings do not work (see this fairly recent mailing list thread, and also this older thread), but the .. rubric:: directive can be used as a heading in docstrings. Perhaps you can use something like this:
class MyClass(object):
"""
.. rubric:: Class variables
:cvar foo: foo documentation
:cvar bar: bar documentation
.. rubric:: Instance variables
:ivar baz: baz documentation
"""
pass

python: manipulating __dict__ of the class

(All in ActivePython 3.1.2)
I tried to change the class (rather than instance) attributes. The __dict__ of the metaclass seemed like the perfect solution. But when I tried to modify, I got:
TypeError: 'dict_proxy' object does
not support item assignment
Why, and what can I do about it?
EDIT
I'm adding attributes inside the class definition.
setattr doesn't work because the class is not yet built, and hence I can't refer to it yet (or at least I don't know how).
The traditional assignment doesn't work because I'm adding a large number of attributes, whose names are determined by a certain rule (so I can't just type them out).
In other words, suppose I want class A to have attributes A.a001 through A.a999; and all of them have to be defined before it's fully built (since otherwise SQLAlchemy won't instrument it properly).
Note also that I made a typo in the original title: it's __dict__ of a regular class, not a metaclass, that I wanted to modify.
The creation of a large number of attributes following some rule smells like something is seriously wrong. I'd go back and see if there isn't a better way of doing that.
Having said there here is "Evil Code" (but it'll work, I think)
class A:
locals()['alpha'] = 1
print A.alpha
This works because while the class is being defined there is a dictionary that tracks the local variables you are definining. These local variables eventually become the class attributes. Be careful with locals as it won't necessarily act "correctly." You aren't really supposed to be modifying locals, but it does seem to work when I tried it.
Instead of using the declarative syntax, build the table seperately and then use mapper on it. see http://www.sqlalchemy.org/docs/05/ormtutorial.html# I think there is just no good way to add computed attributes to class while defining it.
Alternatively, I don't know whether this will work but:
class A(object):
pass
A.all_my_attributes = values
class B(declarative_base, A):
pass
might possibly work.
I'm not too familiar with how 3 treats dict but you might be able to circumvent this problem by simply inheriting the dictionary class like so:
class A(dict):
def __init__(self,dict_of_args):
self['key'] = 'myvalue'
self.update(dict_of_args)
# whatever else you need to do goes here...
A() can be referenced like so:
d = {1:2,3:4}
obj = A(mydict)
print obj['test'],obj[3] # this will print myvalue and 4
Hope this helps.

Resources