How to use Snippets in vscode to define python function input parameter comments (parameter name and type)? - visual-studio-code

For example, when I type """ under the signature of a python function, it triggers the automatic parsing of the function parameter comments, which contain information about the definition, type, default value, etc. of the input and output parameters of the function. I don't want to use a third-party plugin to do this, I want to use vscode's own snippets to do it.
def myfun(a,b,c=1,d="defaultvalue"):
"""
Parameters
----------
a : TYPE
DESCRIPTION.
b : TYPE
DESCRIPTION.
c : TYPE, optional
DESCRIPTION. The default is 1.
d : TYPE, optional
DESCRIPTION. The default is "defaultvalue".
Returns
-------
str
DESCRIPTION.
"""
return "test"

Related

Three dots in Swift init method

I was looking at some Apple Combine source code and saw a Publisher name MergeMany with an init method defined like this:
public init(_ upstream: Upstream...)
What are the three dots ... after the Upstream? The value of Upstream is just another publisher as defined in the Struct definition.
public struct MergeMany<Upstream> : Publisher where Upstream : Publisher {
The three dots mean that it is a variadic function and can take a list of Upstream. This is a function of the Swift Language and not the Combine Framework.
From Sergio Pietri's Blogpost:
Why should we use variadic functions? When you declare this type of function you are adding flexibility to the code. For example: the printf is probably the most notable function that uses this principle. The printf will take N amount of arguments and has many different parameters to format the output it will print.
And from the Swift documentation:
A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (...) after the parameter’s type name.
The values passed to a variadic parameter are made available within the function’s body as an array of the appropriate type. For example, a variadic parameter with a name of numbers and a type of Double... is made available within the function’s body as a constant array called numbers of type [Double].

Get parameter name in Clion plugin

I am developing a plugin for Clion (C++) that needs to access caller parameter name, e.g. if a function is declared:
void fun(int a);
and called
fun(42);
when the intent is invoked on 42 PsiElement it should get the corresponding parameter name, "a" in this case.
In a similar plugin for Intellij (Java), I get the parameter name with PsiCallExpression.resolveMethod() which contains the list of parameters. However, I cannot figure out how to do this in a Clion plugin. I can get a reference of a corresponding OCCallExpression, but it does not seem to contain a reference to the declared function. I tried to play around with ReferencesSearch.search(), but it did not find the declaration of the function.
At the same time, the IDE itself displays all the parameter name hints:
so I suppose it must be possible.
How can I get the parameter name for a given caller argument expression?
Please, look at InlayParameterHintsExtension.forLanguage(OCLanguage.getInstance()) and InlayParameterHintsProvider.getParameterHints

What is the meaning of the : in Swift function signatures and why is there no comma when it seems there should be one?

Here's an example of what I'm talking about: https://developer.apple.com/documentation/foundation/nsmutableorderedset/1410287-insert
The insert function is shown as insert(_:at:)
When it's actually used, the insert function looks more like:
namesArray.insert("John", at: 3)
There's no : after "John" (though I suppose it could be "John":String -- is that what it's there for?), and the , that actually needs to go there is not mentioned in the function signature in the documentation. Am I just supposed to know/assume that the comma goes there when I actually use it? Is this the case for all Swift functions?
Please note that this is not a question about the underscore, _ -- I understand why it's there and what it's for. I'm specifically asking about the reasons for including : and not including , in the function signature.
The convention, frequently seen in documentation, is a concise way of referring to a function, using only the function name and argument labels.
So consider this function declaration:
func insert(_ objects: [Any], at indexes: IndexSet)
This is
a method called insert;
the first parameter name is called objects, this parameter has no argument label (designated with the _) and has a type of [Any]; and
the second parameter name is indexes, its argument label is at and has a type of IndexSet.
So you call it like so:
insert(someObjects, at: someIndex)
When you call it, you don’t care what the parameter names that are used inside that function, but rather just what the argument labels are. So, the convention when referring to that method in documentation is to omit the parameter names and types and just use the function name and the argument labels, but no comma:
insert(_:at:)
Note, you’d never actually use this syntax in your code. This is just a documentation convention employed to distill long function declarations down to something more manageable and concise:
For more information about argument labels and parameter names, see Function Argument Labels and Parameter Names
A : in the function signature always means an input argument to the function. The function signature contains the argument label followed by a :. The _ means there's no argument label, in which case you can omit the : as well when supplying that specific input argument to the function call.
This is exactly what you see with the insert(_:at:) function. You supply two input arguments to it, "John" and 3, but it only needs an argument label for the second input argument, hence you only need one :. It's also important to note that at call time, you separate the input arguments using ,, not :.
Swift functions have a unique way of specifying the signature, which is a carry over pattern from Objective C. There are 3 parts to specifying each input argument to the function signature. The signature for the function you mentioned is as follows:
func insert(_ objects: [Any],
at indexes: IndexSet)
Let's look at the second argument first:
at indicates the argument label, which is how the caller specifies the parameter.
indexes indicates the function's parameter name to the object. This means that in the body of the function, whatever was passed as at: would be referred to as indexes.
IndexSet is the type of the argument.
Part 1 can be something besides a name, too:
if it is not specified, the argument name and parameter label are the same. For example, if the signature were func insert(objects: [Any], indexes: IndexSet), the function would be called as o.insert(objects: ['a','b'], at: [1,2]).
If it is an underscore (_), then the argument has no label for the caller. This allows the caller to use the simpler, intuitive call o.insert(['a','b'], at: [1,2]).
In a signature, the colon separates the name of a parameter from its value. Functions have this anatomy:
func functionName(label1 parameter1: Type1, label2 paramter2: Type2) -> ReturnType {
...
}
where labels are the names seen when calling the function and parameters are the names of the values as used in the body of the function. A label of _ removes the label from the parameter when calling it.
When calling a function, the colon simply separates parameter labels from the values passed to the function. Commas separate different parameters. A parameter with no label requires no colon. The following function would have no colons at all:
func myFunc(_ par1: Int, _ par2: String) {
print(par1, par2)
}
myFunc(3, "what") // 3 "what"

What does [parametertype[]]$Parameter mean?

I have seen PowerShell function parameters defined using the syntax param([parametertype[]]$Parameter) as well as param([parametertype]$Parameter).
Personally, I have always used the latter with (as far as I can tell) no problems. Can anyone explain what (if any) is the difference and why both are allowed and work?
[parametertype[]]$Parameter is a parametertype array of $Parameter
[parametertype]$Parameter is a $Parameter of parametertype
i.e.
> [string]$param_s = "parameter"
> [string[]]$param_a = "parameter", "param2", "param3"
> $param_s[1]
a
> $param_a[1]
param2
Note how param_s is a plain string and accesses the second position in the string when accessing index [1] compared to what's returned by param_a[1]
When used by the param keyword in a function / cmdlet definition, it just ensures the function will be passed correct data type
in powershell you don't have to define what parameter you are using but you can.
sometimes it can be handy if you want to define a parameter as [mandatory] for example.
in your example you defind an array type param[] and single type.
you can read more about Defining Parameters.

functions and operator for user define type

I have built User Define Type with function_in and function_out.
CREATE OR REPLACE FUNCTION MyOwnType_in(cstring)
RETURNS MyOwnType
AS '/home/postgres/ENCRIPTION/MyOwnType/MyOwnType.so','MyOwnType_in'
LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION MyOwnType_out(MyOwnType)
RETURNS cstring
AS '/home/postgres/ENCRIPTION/MyOwnType/MyOwnType.so','MyOwnType_out'
LANGUAGE C IMMUTABLE STRICT;
As you can see each function_in receives cstring and function_out returns cstring.
The thing is function overloading and operator overriding on which I don't want to waste my time.
Is there any way to declare DEFAULT TYPE for my MyOwnType type which tells postgres that functions and operators with MyOwnTypearguments can parse and rewrite it into text?
The most you can do with your type is to mark it as a string type, with:
CREATE TYPE MyOwnType(
INPUT = MyOwnType_in,
OUTPUT = MyOwnType_out,
CATEGORY = 'S'
-- ...
);
The category parameter is especially useful when adding a user-defined type to an existing built-in category, such as the numeric or string types. However, it is also possible to create new entirely-user-defined type categories. Select any ASCII character other than an upper-case letter to name such a category ...
It may require additional CASTs, as #IgorRomanchenko suggested.
You need to CREATE CAST ... AS IMPLICIT from your datatype to text. This way your type will be automatically cast to text when a function/operator requires text.
Details here: http://www.postgresql.org/docs/current/static/sql-createcast.html