elasticsearch-dsl Document field name serialization with a different name - elasticsearch-dsl

How can I serialize the field name with a different name from what is defined in the Document object?
from elasticsearch_dsl import Document, Text
class MyDocument(Document):
context = Text()
When saving this document to ES, I want to write the 'context' key as '#context'.

Since #context is not a valid python identifier I am afraid this is not easily possible. You can always drop down to the raw json (by overriding the to_dict method) but I would definitely not recommend it.

Related

How to extract fieldname of PdfFormField from PdfAnnotation

So I create a radio group using this PdfFormField.createRadioButton() then calling the setFieldName().
However, the PdfAnnotation does not show any keys that stores the field name. I looked at the other dictionaries inside the PdfAnnotation but could not find any.
reader.getAcroFields().getFields().keySet() does list the field names of the form fields but I wish to ask if there is any way via PdfAnnotation?
I tried to put a custom PdfName inside the radio group object but it does not show up in the PdfAnnotation's dictionary.
You are confusing the concept of an annotation (link annotation, file attachment annotation, widget annotation,...) and a form field (text field, choice field, button field, signature field).
In iText 5, annotations are dealt with in a class named PdfAnnotation; form fields are dealt with in a class named PdfFormField. You are trying to do something that is specific for a PdfFormField using the class PdfAnnotation. That's wrong.
I understand the root of the confusion: every visible form field corresponds with at least one widget annotation. Most of the visible form fields correspond with exactly one widget annotation. That's why we made a design choice in iText 5 to have PdfFormField extends PdfAnnotation.
This design choice is in line with the PDF specification where it says that field dictionaries of fields that correspond with a single widget annotation may be merged into a single PDF dictionary.
In practice, you will find PDF dictionaries in a PDF that combine entries typical for a widget annotation dictionary and a field dictionary. (That also explains why there's a getMerged() method in iText: that method gets you the merged dictionary objects.)
I hope this already explains part of your problem. You seem to have another problem too, but I don't understand what you want to do. Please clarify using references to ISO-32000-1 so that people can understand which technical feature you are trying to implement.

How can I read out the "glade ID" of a gtk3 object

In glade it is possible to set an unique ID to an object. In the code one can obtain a pointer to this object by searching for it's "glade ID" via gtk_builder_get_object().
However for my current use-case I just want to read out this ID from an GObject. What's the API to do so ?
You can't. The builder ID is stored in the builder internally, not in the GObject.
The reason for this is that IDs must be unique per builder, which would be impossible to enforce if you were able to get and set them via some GObject API.
You could use gtk_widget_get_name() to identify an object.
It is possible using Gtk.Buildable.get_name(object). This method will return the Glade object id.
This snippet will print all object ids in your Glade XML:
builder = Gtk.Builder()
builder.add_from_file("my-window.glade"))
for obj in builder.get_objects():
print(Gtk.Buildable.get_name(obj))
As stated by #ptomato it's seems not possible.
I found that in that line in the documentation:
All the fields in the GObject structure are private to the
implementation and should never be accessed directly.
But you can circumvent it because at one point in your code you were refering to it by the id that you typed in (or the code you wrote type in) so you just need to store it at that point. And link it somehow (with a variable or a data structure) to the name of the variable holding the object.

Data property with d2rq

I am using the D2RQ Language to create the mapping file. I have a class Persona with this defination:
# Table persone
map:Persona a d2rq:ClassMap;
d2rq:dataStorage map:database;
d2rq:uriPattern "persona/##persona.cognome_persona##";
d2rq:class prova_rules_M:Persona;
.
I would like to create a data_property called "anni_persona" for this class. How can I do? Can anyone help me with the syntax?
Thank you!
Properties don't "belong" to classes in RDF or OWL. Instead, properties may have domains and ranges, which specifies that the subject or object of a triple with the property belongs to a certain class or datatype. E.g., if we say that the domain of hasName is Agent, then whenever we see "x hasName {something}", then we can infer that "x rdf:type Agent". It sounds like you're trying to say there is a property anni_persona, and that its domain is Persona. That's just a matter of asserting anni_persona rdfs:domain Persona somewhere.
Now, to get values from the database table into RDF data, I think that you'll just want to take a look at section 6 in the documentation:
6. Adding properties to resources (d2rq:PropertyBridge).
A d2rq:PropertyBridge relates a database column to an RDF property.
Property bridges are used to attach properties to the RDF resources
created by a class map. The values of these properties are often
literals, but can also be URIs or blank nodes that relate the resource
to other resources, e.g. the value of a paper's :author property could
be a URI representing a person.
If the one of the columns used in a property bridge is NULL for some
database rows, then no property is created for the resources
corresponding to these rows.
Based on the examples in that documentation, it looks like you'd end up with something like:
map:AnniPersona a d2rq:PropertyBridge ;
d2rq:belongsToClassMap map:Persona ;
d2rq:property :anni_persona ;
d2rq:column "Persone.Anni" .

validating that a field is unique using Bean Validation (or JSF)

I have an simple object that has a name
public class Foo {
private String name
}
Each user on the site may have up to 10 Foo's associated with them. Within this context, when a new Foo is created, I would like to validate that there isn't another foo associated with the same user that already exists.
I could Create a custom Bean Validator But annotations require the paramaeters to be defined during compilation. How would I then pass across the names of the existing Foos?
As suggested in various places, I could use EL expressions as an alternative way to pick up the data. This feels like using a sledgehammer to crack a nut. It also brings in a whole bunch of potential issues to consider least of all being ease of testing.
I could do class-wide validation using a boolean field
#AssertTrue(message="Name already exists")
public boolean isNameUnique() {
return (existingNames.contains(name));
}
But the validation message would not show up next to the name field. It is a cosmetic issue and this can be a backup plan. However, its not ideal.
Which brings me to the question:
Is there a simple way to write a Bean Validator that can check the value against a collection of values at the field level and meet the following restrictions ?
Previous values determined at runtime
Not using things like EL expressions
Field level validation instead of class level.
EDIT in reponse to Hardy:
The Foo class is an entity persisted within a database. They are picked up and used through a DAO interface.
I could loop through the entities but that means plugging the DAO into the validator and not to mention that the I would need to write the same thing again if I have another class that too has this constraint.
It would help to see how you want to use the Foo class. Can you extend your example code? Are they kept in a list of Foo instances. A custom constraint seems to be a good fit. Why do you need to pass any parameters to the constraints. I would just iterate over the foos and check whether the names are unique.

Yii Framework; How to check if a model attribute has a SPECIFIC validator?

I am making a custom ActiveForm method, but it requires the model to have a certain custom validator attached the the attribute that is being passed through (otherwise who knows what will happen!?)
My question is simply... is there a way to run this check in the code that is reliable?
I don't want to add the validator at runtime. That would create confusion and possibly let someone use this type of field where it ought not be used.
So I want to say something like:
if( model NOT HAVE validationMethod ON property)
throw Exception;
I'm also not sure why you want to do this, but in addition to viewing the rules array you can do:
$model->getValidators($attribute)
to check which validators are active for a particular attribute (or all attributes, if the arg is null. (I'm assuming $attribute = property in your example.)
This will return all the validator objects that are active for the current scenario and you can check if a predefined or custom class exists. It also gets you a bit more info than just the rules array (i.e., the properties of the validator class).