I have built a class in C with the Python/C API. I now have a requirement to iterate over the data items in the C blob. Returning a PyList is undesirable as length is >50K. Is there a way to provide generator-like functionality?
My one solution is to wrap the class in another pure python class and write the generator function at that level. Is there another way?
According to PEP 234, all you need to do is implement the tp_iternext slot in your python C class.
Related
Suppose I have a person.proto file-
syntax = "proto3";
/* Class Person */
message Person{
string name = 2;
}
Then, I use protoc to generate the source code in Python, or C++.
Question 1:
Suppose I generated the API code, now what I can do to create an object of Person class, is:
person = person_pb2.Person() # In Python
Person person; // In C++
What I want is, every time I create an object of Person class, the reference to this object is stored with a unique ID inside a map.
Basically, I want to create something like a map, to store the reference of every object of each class that is ever created.
One way to achieve this I thought of is to add a custom line of code (which calls a different function) to the classes' constructor, such that every time an object is created, that function will add reference to it to the map.
Is this approach good? If yes, then how to achieve this- customizing class constructors of protoc generated source code (in every language- C++, Python, Java, etc.). If no, then what could be a better solution?
Question 2: For the fields, we have setters and getters. e.g.
person.name = ... # In Python
person.set_name(...) // In CPP
So, what if I want to add something more to the set_name() function, how can we do that?
Like if I want to call a function inside set_name(), then how to achieve that?
In short, both the questions sum up as "How to make changes to the functions of API that protoc generates (in all the languages)?"
Is there something to do with plugins? Or with descriptor files? Or something else?
I have following structure of my code:
class A has dependency on class B, which fetches instance of another class C, which in turn contains a List l.
I need to unit test a method in A which gets hold of List l, and updates it.
I am not sure how to go about it? Should I mock a List and put it in mock of C, and then validate its updates. I am not sure if that's even possible.
You want to test a method M in class A. Method M does tricky things with a class B in order to obtain a List<E> I which is modified.
Depending on your class design, you might be able to proceed as follows:
Create a mock of class B so that so that the method used to “get hold of I” returns some interesting, real list I
Inject this mock into your class-under-test A
Invoke your method-under-test M
Assert that the changes to your list are as intended.
If creating the elements of class E in the list is complicated, you could mock those elements, but there is no need to mock the list itself.
What is the use case of composable_factory? It seems it is related to m_outer, but I can not find any code calling it (Searched all files in the cppwinrt directory). Many thanks!!!
composable_factory is used for constructing types that allow inheritance. In the Windows Runtime, since base class and derived classes may be in separate components, or even written in separate languages, they use COM aggregation to stitch the base and derived classes together into what appears to be, to the client, a single object. A good example of this would be if you created your own MyButton class that is not sealed / final. It would both compose with Button and be derivable by other classes.
Much like activation_factory, composable_factory serves as a specialized activation factory for instantiating objects while taking a base class to derive from during construction. You can see how that fits together with the inner & outer args here:
https://github.com/microsoft/cppwinrt/blob/c36cf05f5030726c8ada1b89fc78bd470f737032/strings/base_composable.h
I believe m_outer is typically the base class, and inner is the derived.
Ben
I want to display my data in a table using scala GUI. But I need the contents of the table are read only. I know there is class Table(mutable) and ListView (read-only) in scala GUI.
My problem is I do not know how to combine them.
Thanks
See http://www.coderanch.com/t/334323/GUI/java/create-read-JTable which says
a JTable has a model behind, a TableModel. You can either implement this or you extend from the DefaultTableModel. Anyway, there is a function isCellEditable(int row, int col). This function should return false in every case, then your table is not editable, but selectable.
Ta have a good starting point, you should read the Table section of the Java Tutorial of SUN.
The Java tutorial being referred to here is this.
Here's the scenario: I'm using the same 3rd party parameterized class in 2 different places in my manifests.
I don't know where the 3rd party class will be included first and in both places, I need to pass a different parameter to the class.
Right now, I only include the class in both places since I don't know how to resolve my situation.
Any help appreciated :)
Thanks
You can only declare a class once when you are using the syntax with Parameters.
include classname
can be used multiple times, but
class { 'classname':
parameter => 'yada',
}
only once.