Skip to content

ns

Classes and methods for handling namespaces on a Wiki

NS

Bases: IntEnum

Default namespace IDs, for convenience

NSManager

Manages the pairings (id -> name) and (name -> id) of the namespaces on a Wiki. Also contains methods for performing lexical operations with namespaces

__init__(r)

Creates a new NSManager.

Parameters:

Name Type Description Default
r dict

The response from the server. This should be the json object directly under the "query" object.

required

batch_convert_ns(titles, ns, replace_underscores=False)

Convenience method, converts an Iterable of titles to another namespace. PRECONDITION: titles in titles are well-formed.

Parameters:

Name Type Description Default
titles Iterable[str]

The titles to convert

required
ns Union[str, NS]

The namespace to convert the titles to

required
replace_underscores bool

Set True to replace underscore characters with the space characters in the returned value. Defaults to False.

False

Returns:

Type Description
list[str]

list[str]: The titles converted to namespace ns.

canonical_prefix(ns)

Gets the canonical prefix for the specified namespace. This adds a : suffix to ns, or returns the empty string if ns is the Main namespace.

Parameters:

Name Type Description Default
ns Union[NS, str]

The namespace to get the canonical prefix for.

required

Returns:

Name Type Description
str str

The canonical prefix for the specified namepsace.

create_filter(nsl)

Convenience method, creates a pipe-fenced namespace filter for sending with queries.

Parameters:

Name Type Description Default
nsl Union[list[Union[NS, str]], NS, str]

The namespace or namespaces to create a filter out of

required

Raises:

Type Description
ValueError

If there is an invalid namespace in nsl

Returns:

Name Type Description
str str

The pipe-fenced namespace filter for sending with queries

intify(ns)

Convienence method, converts the specified namespace to its int id if possible. This is a lexical operation and does not check if the id actually exists on the server.

Parameters:

Name Type Description Default
ns Union[int, NS, str]

The namespace to get the int of.

required

Returns:

Name Type Description
int int

The int id of ns. None if you passed a str and it does not exist in the lookup table.

nss(title)

Strips the namespace prefix from a title.

Parameters:

Name Type Description Default
title str

The title to remove the namespace from.

required

Returns:

Name Type Description
str str

title, without a namespace.

stringify(ns)

Convienence method, returns the name of a namespace as a str. Does not perform any namespace validation whatsoever.

Parameters:

Name Type Description Default
ns Union[int, NS, str]

The namespace to get the name of. If this is a str, then it will be returned.

required

Returns:

Name Type Description
str str

The name of ns as a str.