Documenting user created type declarations with Sphinx - python-sphinx

I'm making a type declaration. For example:
from typing import Tuple
Type1 = Tuple[str, float]
I would like Sphinx to document this declaration like a function declaration. How can I do this? Thanks in advance.
If I bilded documentation and add usual description the result is that the declaration of these types is displayed as a plain text, not something special.

A canonical Python way is to use TypeAlias from PEP-653:
#: Funny type that is mixing strings and floats - sounds like JavaScript programming!
Type1: TypeAlias = Tuple[str, float]
It was added in Python 3.10.
As the writing of this, Sphinx component that does autogenerated documentation, autodoc, does not support TypeAlias yet.

Related

generate a yaml file dynamically in golang

I want to generate a yaml file using a struct's yaml tags, but I want to ignore the omitempty tags. So I figured I should go over the struct recursively with reflection, and get the information using Tag.GetTag("yaml").
This is fine, but it leaves me with the task of creating a yaml with this information.
I started writing this -
const Indent = " "
const NL = ":\n"
type Yaml struct {
Simple []string
Slices [][]Yaml
Nested map[string] *Yaml
}
But I actually hope there is a useful library to do this, as it seems like a relatively common task to do. Unfortunately,a search in Google didn't find anything.
Does such a library exist?
Thanks,

Protobuf syntax with colons

Here are three examples of protobuf syntax.
mscoco_label_map.pbtxt
item {
name: "/m/01g317"
id: 1
display_name: "person"
}
item {
name: "/m/0199g"
id: 2
display_name: "bicycle"
}
...
en.wikipedia.org
anotherfield {
foo: 123
bar: 456
}
anotherfield {
foo: 222
bar: 333
}
Official documentation
syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
Official (#3) example is obviously differs from #1 and #2. Did I miss a paragraph in official documentation which states that colon can be used instead of equal sign?
Official documentation describes a JSON Mapping, but there is not a single example that looks like #1 and #2. Also #1 and #2 is not a valid JSON either (missing quotes around keys, missing commas).
Q: where are #1 and #2 syntax came from?
Link to better (than official docs) syntax description is appreciated.
Thanks to Marc's Gravell response I was able to find out the answer.
#3 is a schema, proto syntax, think XSD (XML Schema Definition).
#1, #2 is a text dump of an actual data (payload), textproto syntax, *.pbtxt file extension, think XML or JSON.
Related question: What does the protobuf text format look like?
Related links:
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.text_format
C++ API for printing and parsing protocol messages in a human-readable, text-based format.
https://googleapis.dev/python/protobuf/latest/google/protobuf/text_format.html
Python API
https://developers.google.com/protocol-buffers/docs/reference/proto3-spec
Protocol Buffers Version 3 Language Specification
https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/text_format.cc#L288
Source code (C++) of text format ASCII representation parser.
https://medium.com/#nathantnorth/protocol-buffers-text-format-14e0584f70a5
Protocol Buffers: Text Format (2019-10-11, Nathan North)
There are a couple different ways to structure the data (instead of using brackets, you could use <>) but unfortunately nothing in this realm is terribly well documented so it’s more of a game of try it and see if it works.
https://gist.github.com/henridf/704c1c812f04a502c1c26f77a739090b
Encoding a protobuf with protoc --encode (syntax example).
Clear syntax documentation of protobuf text format is still missing.

PyGtk3 and Gettext - problem choosing the language at run-time

Long story short, when I thought the strings would be translated, nothing happens.
I have a PyGtk application and a module where I store all the strings that I want to be translated in the following way:
'''
Strings module.
'''
....
CANCEL_BUTTON_TEXT = _("Cancel")
BACK_BUTTON_TEXT = _("Back")
....
And so on. So then they are used from other modules like:
'''
View module.
'''
import strings
# Usage example
button.set_label(strings.CANCEL_BUTTON_TEXT)
button.set_tooltip(strings.TOOLTIP)
window_title.set_title(strings.WINDOW_TITLE)
...
I have created the necessary .mo files. This is the jerarquy I have:
/locales
/es
LC_MESSAGES
base.mo
base.po
/en
LC_MESSAGES
base.mo
base.po
base.pot
As the documentation says (https://docs.python.org/3/library/gettext.html), in my main.py I have the following lines:
import gettext
gettext.install('myapplication')
es_lang = gettext.translation('base', localedir='locales', languages=['es'])
en_lang = gettext.translation('base', localedir='locales', languages=['en'])
es_lang.install()
Then, in my application I have a button, that when is pressed, the following line is executed:
en_lang.install()
But the spanish language is still used on the widgets. Could anyone help me out?
So, what I was doing wrong is I was translating the strings but I wasn't updating the GTK Widgets labels, among other things.
The way I solved this was:
i) Created a class where I put all the strings I want to translate when the user selects a language. That class is the only one to import the gettext module.
ii) When the user selects a language, a method of that class is called and it translates all strings to that language.
iii) Another method outside that class updates all labels and widgets that use those strings.

protoc-gen-go struct xxx covert to map[string]interface{}

The struct in the .pb.go file generated by .proto file has three additional fields and some other things.like this:
When converting this struct to json, if one field is empty, the field will not appear in json. Now I know it can be done using jsonpb.Marshaler.
m := jsonpb.Marshaler{EmitDefaults: true}
Now, I coverting struct to map[string]interface{}, put it in
InfluxDB. I have to convert struct to map[string]interface{}.The function NewPoint needs. like this:
I use structs.Map(value) function in go ,The transformed map has three additional fields, and running the program causes errors,like this:
{"error":"unable to parse 'txt,severity=1 CurrentValue=\"1002\",MetricAlias=\"CPU\",XXX_sizecache=0i,XXX_unrecognized= 1552551101': missing field value"}
When I remove these three fields, the program runs OK.These three fields are automatically generated, and I have a lot of structs.
What should I do?Thank you!
Protobuf generator adds some additional fields with names starting from XXX that are intended for optimizations. You can't change this behavior of protoc-gen-go.
The problem is in the way you convert struct to map[sting]interface{}. It's hard to figure out from which package exactly structs.Map comes from. Seems like it goes from here: https://github.com/fatih/structs/blob/master/structs.go#L89 - this code uses reflect to iterate through all fields of the structure and push them to map[sting]interface{}. You just need to write your own slightly modified version of FillMap routine that will omit XXX fields.

xcode: How to localize strings in code?

With localize I mean translate into different languages.
I only found Instructions for localizing the storyboard but I also have strings that I set in Code.
(I'm using Swift)
In short — see the NSLocalizedString function. This is also there in Swift... its declaration looks like this:
func NSLocalizedString(
key: String,
tableName: String? = default,
bundle: NSBundle = default,
value: String = default,
#comment: String) -> String
The third through sixth arguments have default values, so you can call it like this:
NSLocalizedString("foo") // -> Maybe「フー」in your Japanese strings file?
Or like this:
NSLocalizedString("foo", comment: "metasyntactic variable")
Or with any other combination of arguments.
Check out the Internationalization and Localization Guide in Apple's documentation.

Resources