class Wrapper(func, *args, **kwargs)
Wrapper of a function.
It could be used to reduce repetition of calling a function with the same arguments.
Args:
func
: The function needs to be wrapped.*args
: The positional arguments offunc
.**kwargs
: The keyword arguments offunc
.
Example:
>>> from pygentest import *
>>> f = Wrapper(randint, 1, 10)
>>> f()
3
>>> f()
6
List of methods:
-
__call__()
Call the instance as the wrapped function.
-
randint(a, b)
Get a random integer in inclusive range [a,b].
In fact, it is alias for
random.randint
.Args:
a
: Lowerbound of range.b
: Upperbound of range.
-
randfloat(a, b)
Get a random number in the range [a, b) or [a, b] depending on rounding.
In fact, it is alias for
random.uniform
.Args:
a
: Lowerbound of range.b
: Upperbound of range.
-
randchar(chars)
Get a random character from the given list of character.
Args:
chars
: List of character.
-
randlist_any(size, wrapgen, sorted=False, reverse=False)
Get a random list of length
size
, with elements generated bywrapgen
.Note:
wrapgen
must have typeWrapper
.Args:
size
: Length of list.wrapgen
: Wrapped generator for elements.sorted
:True
if the returned list is sorted. Defaults toFalse
.reverse
:True
if the returned list is sorted in descending order. Defaults toFalse
.
-
randlist_uniqueint(size, a, b, sorted=False, reverse=False)
Get a random list of unique integers in inclusive range [a,b].
The algorithm used to implement this function is inspired by Knuth shuffle.
Args:
size
: Length of list.a
: Lower bound of the range.b
: Upper bound of the range.sorted
:True
if the returned list is sorted. Defaults toFalse
.reverse
:True
if the returned list is sorted in descending order. Defaults toFalse
.
-
randmatrix_any(numrow, numcol, wrapgen)
Get a random list of size
numrow
bynumcol
, with elements generated bywrapgen
.Note:
wrapgen
must have typeWrapper
.Args:
numrow
: Number of rows.numcol
: Number of columns.wrapgen
: Wrapped generator for elements.
-
randstring(length, chars)
Get a random string of length
length
whose characters randomly chosen fromchars
.Args:
length
: Length of string.chars
: List of characters.
-
randlist_string(size, length, chars)
Get a random list of
size
strings of lengthlength
with characters fromchars
.Args:
size
: Length of list.length
: Length of each string.chars
: List of characters.
-
randgraph(V, E, /, directed=False, loop=False, *, _1_indexed=True)
Get edge list of a random graph with
V
vertices andE
edges.An error will be raise if
V
is non-positive, or lessArgs:
V
: Number of vertices.E
: Number of edges.directed
:True
if graph is directed. Defaults toFalse
.loop
:True
if graph allows loop. Defaults toFalse
._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randlist_uniqueint(size, a, b, sorted=False, reverse=False)
Get edge list of a random graph with
V
vertices, and the probability for each pair of vertices to be connected isp
.Args:
V
: Number of vertices.p
: Probability for each pair of vertices to be connected. Defaults to0.5
.directed
:True
if graph is directed. Defaults toFalse
.loop
:True
if graph allows loop. Defaults toFalse
._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randmultigraph(V, E, /, loop=False, *, _1_indexed=True)
Get edge list of a random multigraph with
V
vertices andE
edges.Args:
V
: Number of vertices.E
: Number of edges.loop
:True
if graph allows loop. Defaults toFalse
._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randtree_rooted(V, *, _1_indexed=True)
Get list of parent vertex of each vertex in a random rooted tree.
The tree is rooted at vertex 1 if the verticed are 1-indexed, and 0, otherwise.
Args:
V
: Number of vertices._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randtree(V, *, _1_indexed=True)
Get edge list of a random tree with
V
vertices.This function is constructed based on
randtree_rooted
.Args:
V
: Number of vertices._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randgraph_connected(V, E, *, _1_indexed=True)
Get edge list of a random connected undirected graph with
V
vertices andE
edges.This function is constructed based on
randtree
.Args:
V
: Number of vertices.E
: Number of edges._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randDAG(V, E, /, connected=True, *, _1_indexed=True)
Get edge list of a random directed acyclic graph with
V
vertices andE
edges.This function is constructed based on
randgraph
andrandgraph_connected
.Args:
V
: Number of vertices.E
: Number of edges.connected
:True
if graph is (weakly) connected. Defaults toTrue
._1_indexed
:True
if vertices are 1-indexed. Defaults toTrue
.
-
randperm(n, k)
Get a random permutation of
k
number chosen from base set{1,2,...,n}
Args:
n
: Size of base set.k
: Size of permutation.
-
randpartition_integer(n, k)
Randomly partition positive integer
n
intok
summands.Here is more information about integer partition.
Args:
n
: Partitioned number.k
: Number of summands.
-
randpartition_set(baseset, k)
Randomly partition
baseset
intok
subsets.Here is more information about partition of a set.
Args:
baseset
: Base set.k
: Number of summands.
-
randpolygon_lattice(n, minX, maxX, minY, maxY)
Get a random convex lattice polygon with
n
vertices with the given bound for coordinates.A lattice polygon is a polygon whose every vertex has integer coordinates.
Args:
n
: Number of vertices.minX
: Lowerbound of x-coordinate.maxX
: Upperbound of x-coordinate.minY
: Lowerbound of y-coordinate.maxY
: Upperbound of y-coordinate.