mw.types – common types

class mw.Timestamp(time_thing)

An immutable type for working with MediaWiki timestamps in their various forms.

Parameters:
time_thing : mw.Timestamp | time_struct | datetime | str | int | float

The timestamp type from which to construct the timestamp class.

Returns:

mw.Timestamp

You can make use of a lot of different time things to initialize a mw.Timestamp.

  • If a time_struct or datetime are provided, a Timestamp will be constructed from their values.
  • If an int or float are provided, they will be assumed to a unix timestamp in seconds since Jan. 1st, 1970 UTC.
  • If a str is provided, it will be be checked against known MediaWiki timestamp formats. E.g., '%Y%m%d%H%M%S' and '%Y-%m-%dT%H:%M:%SZ'.
  • If a mw.Timestamp is provided, the same Timestamp will be returned.

For example:

>>> import datetime, time
>>> from mw import Timestamp
>>> Timestamp(1234567890)
Timestamp('2009-02-13T23:31:30Z')
>>> Timestamp(1234567890) == Timestamp("2009-02-13T23:31:30Z")
True
>>> Timestamp(1234567890) == Timestamp("20090213233130")
True
>>> Timestamp(1234567890) == Timestamp(datetime.datetime.utcfromtimestamp(1234567890))
True
>>> Timestamp(1234567890) == Timestamp(time.strptime("2009-02-13T23:31:30Z", "%Y-%m-%dT%H:%M:%SZ"))
True
>>> Timestamp(1234567890) == Timestamp(Timestamp(1234567890))
True

You can also do math and comparisons of timestamps.:

>>> from mw import Timestamp
>>> t = Timestamp(1234567890)
>>> t
Timestamp('2009-02-13T23:31:30Z')
>>> t2 = t + 10
>>> t2
Timestamp('2009-02-13T23:31:40Z')
>>> t += 1
>>> t
Timestamp('2009-02-13T23:31:31Z')
>>> t2 - t
9
>>> t < t2
True
classmethod from_datetime(dt)

Constructs a mw.Timestamp from a datetime.datetime.

Parameters:
dt : datetime.datetime`

A datetime.

Returns:

mw.Timestamp

classmethod from_string(string)

Constructs a mw.Timestamp from a MediaWiki formatted string. This method is provides a convenient way to construct from common MediaWiki timestamp formats. E.g., %Y%m%d%H%M%S and %Y-%m-%dT%H:%M:%SZ.

Parameters:
string : str

A formatted timestamp

Returns:

mw.Timestamp

classmethod from_time_struct(time_struct)

Constructs a mw.Timestamp from a time.time_struct.

Parameters:
time_struct : time.time_struct

A time structure

Returns:

mw.Timestamp

classmethod from_unix(seconds)

Constructs a mw.Timestamp from a unix timestamp (in seconds since Jan. 1st, 1970 UTC).

Parameters:
seconds : int

A unix timestamp

Returns:

mw.Timestamp

long_format()

Constructs a long, '%Y-%m-%dT%H:%M:%SZ' formatted string common to the API. This method is roughly equivalent to calling strftime('%Y-%m-%dT%H:%M:%SZ').

Parameters:
format : str

The string format

Returns:

A formatted string

short_format()

Constructs a long, '%Y%m%d%H%M%S' formatted string common to the database. This method is roughly equivalent to calling strftime('%Y%m%d%H%M%S').

Parameters:
format : str

The string format

Returns:

A formatted string

strftime(format)

Constructs a formatted string. See https://docs.python.org/3/library/time.html#time.strftime for a discussion of formats descriptors.

Parameters:
format : str

The format description

Returns:

A formatted string

classmethod strptime(string, format)

Constructs a mw.Timestamp from an explicitly formatted string. See https://docs.python.org/3/library/time.html#time.strftime for a discussion of formats descriptors.

Parameters:
string : str

A formatted timestamp

format : str

The format description

Returns:

mw.Timestamp

unix()
Returns:the number of seconds since Jan. 1st, 1970 UTC.
class mw.Namespace(id, name, canonical=None, aliases=None, case=None, content=False)

Namespace meta data.

aliases

Alias names : set( str )

canonical

Canonical name : str | None

case

Case sensitivity : str | None

content = None

Is considered a content namespace : bool

classmethod from_doc(doc, aliases={})

Constructs a namespace object from a namespace doc returned by the API site_info call.

id

Namespace ID : int

name

Namespace name : str

Previous topic

MediaWiki Utilities

Next topic

mw.api – MediaWiki API abstraction

This Page