Get All Parameters as List

Get all Parameters as List

I think that you want match.call:

tmpfun <- function(a,b,...) {
print(as.list(match.call()))
print(as.list(match.call(expand.dots=FALSE)))
}
> tmpfun(a=1, b=2, c=3, d=4)
[[1]]
tmpfun

$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4

[[1]]
tmpfun

$a
[1] 1

$b
[1] 2

$...
$...$c
[1] 3

$...$d
[1] 4

Get a list of all function parameters from inside the function

You can also return the environment at the beginning of the function:

xf <- function (a, b="Hi", c=TRUE) {
as.list(environment(), all=TRUE)
}

Result:

> xf(a=1)
$a
[1] 1

$b
[1] "Hi"

$c
[1] TRUE

Using R, How to get all parameters passed into a function, with their values?

Here is one possible solution. This solution requires function arguments with no default values to be specified (like z below).

grabFunctionParameters <- function() {
pf <- parent.frame() # get caller environment
dots <- eval(quote(list(...)), envir = pf) # get ... in the caller
nms <- sapply(ls(envir = pf, sorted = FALSE), as.name) # get argument names different from names in ... in the caller
out <- c(lapply(nms, eval, envir = pf), dots) # get all arguments/values
out[names(out) != ""] # remove unnamed values in ... (if any)
}

Example of use case

adebo.deepSearch = function(z, pi_0 = 0.3, families=list(), ... ) {
args = grabFunctionParameters();
args
}

Some scenarios

adebo.deepSearch(z=4)
# $z
# [1] 4
#
# $pi_0
# [1] 0.3
#
# $families
# list()
#
adebo.deepSearch(z=4, pi_0=9, families = list(z=1:2))
# $z
# [1] 4
#
# $pi_0
# [1] 9
#
# $families
# $families$z
# [1] 1 2
#
#
adebo.deepSearch(z=4, pi_0=9, ac=5, bc=6) # some additional arguments for ...
# $z
# [1] 4
#
# $pi_0
# [1] 9
#
# $families
# list()
#
# $ac
# [1] 5
#
# $bc
# [1] 6

Udapte: this is an update of the function above to make it more general.

it always returns a list:

  • an empty list if the caller (function) has no argument (or only ... with unnamed values).
  • formal argument names (not in ...) could start with dot.
    The previous function required the caller to have ...; and the caller with formal argument names starting with dot (not in ...) were not return.

New function

grabFunctionParameters <- function() {
pf <- parent.frame()
args_names <- ls(envir = pf, all.names = TRUE, sorted = FALSE)
if("..." %in% args_names) {
dots <- eval(quote(list(...)), envir = pf)
} else {
dots = list()
}
args_names <- sapply(setdiff(args_names, "..."), as.name)
if(length(args_names)) {
not_dots <- lapply(args_names, eval, envir = pf)
} else {
not_dots <- list()
}
out <- c(not_dots, dots)
out[names(out) != ""] # remove unnamed values in ... (if any)
}

Some scenarios

fn1 <- function() grabFunctionParameters()                              # the initial function (before the update) required ... argument
fn2 <- function(x=1, .a=2, b=list(), ...) grabFunctionParameters() # the initial function did not return .a
fn3 <- function(.x, .a=2, b=list(), ...) grabFunctionParameters()
fn4 <- function(...) grabFunctionParameters()
fn5 <- function(x, .a) grabFunctionParameters() # the initial function required ... argument


fn1() # correct since the caller has no argument. Previously not allowed!
# list()

fn2()
# $x
# [1] 1
#
# $.a
# [1] 2
#
# $b
# list()

fn2(.a=10, ac=4, bc=7, .xy=1) #
# $x
# [1] 1
#
# $.a
# [1] 10
#
# $b
# list()
#
# $ac
# [1] 4
#
# $bc
# [1] 7
#
# $.xy
# [1] 1

fn3(10)
# $.x
# [1] 10
#
# $.a
# [1] 2
#
# $b
# list()

fn3() # throw an error! (.x required!). This will not happen if we use mget function and not lapply/supply inside grabFunctionParameters above.
# Error in FUN(X[[i]], ...) : argument ".x" is missing, with no default

fn4(a = 5, b = 6, c = 6, 6, 7, 9) # unnamed values are dropped
# $a
# [1] 5
#
# $b
# [1] 6
#
# $c
# [1] 6

fn5(6, 8)
# $x
# [1] 6
#
# $.a
# [1] 8

Getting list of parameter names inside python function

Well we don't actually need inspect here.

>>> func = lambda x, y: (x, y)
>>>
>>> func.__code__.co_argcount
2
>>> func.__code__.co_varnames
('x', 'y')
>>>
>>> def func2(x,y=3):
... print(func2.__code__.co_varnames)
... pass # Other things
...
>>> func2(3,3)
('x', 'y')
>>>
>>> func2.__defaults__
(3,)

For Python 2.5 and older, use func_code instead of __code__, and func_defaults instead of __defaults__.

How to list all parameters available to query via API?

It is not possible to find out all available (undocumented) query parameters for a query, unless the API explicitly provides such a method or you can find out how the API server processes the query.

For instance, if the API server code is open source, you could find out from the code how the query is processed. Provided that you find the code also.

The answers in the post you linked are similarly valid for an API site as well as for one that provides content for a web browser (a web server can be both).
Under the hood, there is not necessarily any difference between an API server or a server that provides web content (html) in terms of how queries are handled.

As for the parameters seemingly without an effect, it seems that the API in question does not validate the query parameters, i.e., you can put arbitrary parameters in the query and the server will simply ignore parameters that it is not specifically programmed to use.

Get all parameters passed to a function in PowerShell

The $PSBoundParameters variable is a hashtable that contains only the parameters explicitly passed to the function.

A better way for what you want might be to use parameter sets so that you can name specific combinations of parameters (don't forget to make the appropriate parameters mandatory within those sets).

Then you can do something like:

switch ($PsCmdlet.ParameterSetName) {
'NameOnly' {
# Do Stuff
}
'NameAndZip' {
# Do Stuff
}
# etc.
}

How to get a list of all the url parameters with their values in asp.net c#?

You need HttpUtility.ParseQueryString

NameValueCollection qscollection = HttpUtility.ParseQueryString(querystring);

you can use this to get the querystring value itself:

 Request.Url.Query

To put it together

NameValueCollection qscollection = HttpUtility.ParseQueryString(Request.Url.Query);

More info at http://msdn.microsoft.com/en-us/library/ms150046.aspx

The harder manual way without using the builtin method is this:

NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split('&');
foreach(string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();

queryParameters.Add(key, val);
}
}


Related Topics



Leave a reply



Submit