Find All Functions (Including Private) in a Package

find all functions (including private) in a package

you can use asNamespace:

> methods(cbind)
[1] cbind.data.frame cbind.grobGrid cbind.ts*

Non-visible functions are asterisked
> r <- unclass(lsf.str(envir = asNamespace("stats"), all = T))
> r[grep("cbind.ts", r)]
[1] ".cbind.ts" "cbind.ts"

cbind.ts in stats package is invisible but can find in envir = asNamespace("stats").

Is there a command in R to view all the functions present in a package?

You can use lsf.str.

For instance:

lsf.str("package:dplyr")

To list all objects in the package use ls

ls("package:dplyr")

Note that the package must be attached.

To see the list of currently loaded packages use

search()

Alternatively calling the help would also do, even if the package is not attached:

help(package = dplyr)

Finally, you can use RStudio which provides an autocomplete function. So, for instance, typing dplyr:: in the console or while editing a file will result in a popup list of all dplyr functions/objects.

How to list all functions in a module?

Use the inspect module:

from inspect import getmembers, isfunction

from somemodule import foo
print(getmembers(foo, isfunction))

Also see the pydoc module, the help() function in the interactive interpreter and the pydoc command-line tool which generates the documentation you are after. You can just give them the class you wish to see the documentation of. They can also generate, for instance, HTML output and write it to disk.

Find all Base R function names that contain the word `list`

lsf.str returns all the functions in a particular package, you can then use grep with the keyword and return all the names of the functions which match it.

grep("list", lsf.str("package:base"), value = TRUE)

# [1] "[.Dlist" "[.listof" "[.simple.list"
# [4] "alist" "all.equal.list" "as.data.frame.list"
# [7] "as.list" "as.list.data.frame" "as.list.Date"
#[10] "as.list.default" "as.list.environment" "as.list.factor"
#[13] "as.list.function" "as.list.numeric_version" "as.list.POSIXct"
#[16] "as.list.POSIXlt" "as.pairlist" "iconvlist"
#[19] "is.list" "is.pairlist" "list"
#[22] "list.dirs" "list.files" "list2env"
#[25] "pairlist" "print.Dlist" "print.listof"
#[28] "print.simple.list" "sort.list" "unlist"
#[31] "within.list"

Calling private function within package body

The issue you have (assuming that you are calling the correctly named procedure/function in the correct manner) is that you are trying to invoke a call to a function that hasn't yet been declared. There are two ways around this, assuming you want to keep the function private:

  1. Declare the ADD_STUDENT function before any procedures/functions that invoke it.
  2. Use forward declaration to declare the function before it is invoked.

So, for option 1, your example code would look like:

PACKAGE BODY SCHOOL AS
FUNCTION ADD_STUDENT(...)
...
END ADD_STUDENT;

PROCEDURE ADD_PEOPLE(...)
...
some_var := ADD_STUDENT();
END ADD_PEOPLE;
END SCHOOL;
/

And for option 2 your code would look like:

PACKAGE BODY SCHOOL AS
-- forward declared function
FUNCTION ADD_STUDENT(...);

PROCEDURE ADD_PEOPLE(...)
...
some_var := ADD_STUDENT();
END ADD_PEOPLE;

FUNCTION ADD_STUDENT(...)
...
END ADD_STUDENT;
END SCHOOL;
/

Personally, I favour option 1, as it means there's less stuff cluttering up the package body, but option 2 might be necessary if you have two modules that reference each other.

Private fields and methods for a struct

That's not how "privacy" works in Go: the granularity of privacy is the package.

If you really want only the members of mytype to access some fields, then you must isolate the struct and the functions in their own package.

But that's not the usual practice. Whether Go is OOP or not is debatable but clearly the practice isn't to encapsulate the code by a struct like you seem to want to do. Usually a package is small enough to be coherent: if you don't want to access fields from within the package, don't access them.

How do you use ReflectionUtils to get package private methods/fields?


for (Method m : getAllMethods(cls, ReflectionUtils.getAllMethods(cls, Predicates.not(withModifier(Modifier.PRIVATE)), Predicates.not(withModifier(Modifier.PUBLIC)), Predicates.not(withModifier(Modifier.PROTECTED)))))
{
// DO STUFF
}

I used the following to test the above. MyClass contain 5 methods; one for each access modifier and one static:

public class MyClass
{

public void method1()
{
System.out.println("method1() invoked");
}

private void method2()
{
System.out.println("method2() invoked");
}

protected void method3()
{
System.out.println("method3() invoked");
}

void method4()
{
System.out.println("method4() invoked");
}

public static void method5()
{
System.out.println("method5() invoked");
}
}

My test class:

import static org.reflections.ReflectionUtils.withModifier;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;

import org.reflections.ReflectionUtils;

import accessmodifier.bar.MyClass;

import com.google.common.base.Predicates;

public class ReflectionTest
{

public static void main(String[] args)
{
Class<MyClass> cls = MyClass.class;
Set<Method> privateMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.PRIVATE));
Set<Method> protectedMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.PROTECTED));
Set<Method> publicMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.PUBLIC));
Set<Method> defaultMethods = ReflectionUtils.getAllMethods(cls, Predicates.not(withModifier(Modifier.PRIVATE)), Predicates.not(withModifier(Modifier.PUBLIC)), Predicates.not(withModifier(Modifier.PROTECTED)));
Set<Method> staticMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.STATIC));

System.out.println("Private Methods");
for (Method m : privateMethods)
System.out.println(m.getName());

System.out.println("\nProtected Methods");
for (Method m : protectedMethods)
System.out.println(m.getName());

System.out.println("\nPublic Methods");
for (Method m : publicMethods)
System.out.println(m.getName());

System.out.println("\nPackage-Private Methods");
for (Method m : defaultMethods)
System.out.println(m.getName());

System.out.println("\nStatic Methods");
for (Method m : staticMethods)
System.out.println(m.getName());
}
}

PROGRAM OUTPUT

Private Methods
method2

Protected Methods
method3

Public Methods
method1
method5

Package-Private Methods
method4

Static Methods
method5

UPDATE
If you want to be able to invoke an accessible method (such as package-private), you will have to something like this:

        m.setAccessible(true); // bypass access
try
{
m.invoke(new MyClass(), null); // invoke method (you have to know parameter types and pass them if needed. Use *Method.getParameter...()* methods for that.
}
catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e)
{
e.printStackTrace();
}

Defining private module functions in python

In Python, "privacy" depends on "consenting adults'" levels of agreement - you can't force it (any more than you can in real life;-). A single leading underscore means you're not supposed to access it "from the outside" -- two leading underscores (w/o trailing underscores) carry the message even more forcefully... but, in the end, it still depends on social convention and consensus: Python's introspection is forceful enough that you can't handcuff every other programmer in the world to respect your wishes.

((Btw, though it's a closely held secret, much the same holds for C++: with most compilers, a simple #define private public line before #includeing your .h file is all it takes for wily coders to make hash of your "privacy"...!-))



Related Topics



Leave a reply



Submit