Skip to main content

pymafia

pymafia is a Python module and bridge for reflecting KoLmafia's Java environment. It aims to provide an easy-to-use environment for scripting Kingdom of Loathing (KoL) in Python by reflecting and wrapping the community-developed KoLmafia desktop tool. While other languages for scripting KoL exist, they are arguably less approachable to non-developers.

Installation

pymafia is available at the Python Package Index (PyPI):

pip install pymafia

pymafia uses JPype to reflect KoLmafia's Java environment, so you will need to install a Java Development Kit (JDK) on your operating system — KoLmafia's developers recommend Adoptium v17. For information on troubleshooting your Java installation, see JPype's troubleshooting guide.

Usage

To get started, simply import pymafia or any of its components. Doing so will download a KoLmafia jar file (if it is not present in the configured location) and start a Java Virtual Machine (JVM) with the jar file included in the JVM's classpath. This process can take over a minute depending on your internet connection.

You can choose the revision of KoLmafia to use or location to run it from by setting these properties in the pymafia_config module prior to importing pymafia. The revision defaults to the last known working revision at the time of release and the location defaults to a folder named "kolmafia" in the current working directory.

>>> import pymafia_config

>>> pymafia_config.set_revision(27467)

>>> pymafia_config.set_location("C:\\Users\\Documents\\kolmafia\\")

>>> import pymafia # Start the JVM

Once you have configured pymafia and started, you will most likely want to launch the KoLmafia GUI and login to your character. Both of these actions can be performed using the utils module.

>>> from pymafia.utils import launch_gui, login

>>> launch_gui()

>>> login("devster6")

Note that almost all pymafia objects are available at the top level, although this is subject to change.

>>> from pymafia import launch_gui

Accessing KoLmafia

The reflected KoLmafia jar file can be accessed through a KoLmafia wrapper class instance called km. Most, if not all, of KoLmafia's Java classes are available as attributes on km.

>>> from pymafia.kolmafia import km

>>> km.AdventureResult
<java class 'net.sourceforge.kolmafia.AdventureResult'>

These classes behave similar to how they do in Java with the exception of returning Python objects when possible. For more information on type conversion, see JPype's type matching guide.

>>> km.AdventureResult.tallyItem("big rock")
<java object 'net.sourceforge.kolmafia.AdventureResult'>

>>> km.AdventureResult.tallyItem("big rock").isBountyItem()
False

ASH and Special Datatypes

All of KoLmafia's runtime library functions can be accessed through the ash submodule, which returns a LibraryFunction instance.

>>> from pymafia import ash

>>> ash.gameday_to_string
LibraryFunction("gameday_to_string")

The LibraryFunction is a thin wrapper around the desired Java method that will automatically convert the inputs and outputs of the wrapped method to and from Java objects, respectively. This conversion means that the functions accept and return Python objects, including KoLmafia's special datatypes, which have been implemented in the datatypes sub-package.

>>> from pymafia.datatypes import Location

>>> ash.gameday_to_string()
"Dougtember 3"

>>> ash.to_item("big rock")
Item("big rock")

>>> ash.appearance_rates(Location("Noob Cave"))
{Monster("none"): 0.0, Monster("crate"): 100.0}

As demonstrated above, the datatypes can be instantiated directly from their name, id, or string representation where applicable. Each datatype has a set of properties that mirror those available from their KoLmafia proxy record equivalent.

>>> from pymafia.datatypes import Familiar, Item

>>> Item("big rock").tradeable
True

>>> Familiar("God Lobster").hatchling
Item("God Lobster Egg")

Each datatype also has an all() class method that returns every non-none instance of that type.

>>> from pymafia.datatypes import Stat

>>> Stat.all()
[Stat("Muscle"), Stat("Mysticality"), Stat("Moxie")]

Many datatypes contain predefined class instances as class variables for convenient reference. Notably, all datatypes define a NONE class variable.

>>> Coinmaster.NONE
Coinmaster('none')

>>> Slot.HAT
Slot('hat')

Non-Documented Functionality

There are modules and subpackages and available within pymafia that have not been described here; I hope to provide comprehensive documentation in the future.

Contributing

To contribute to pymafia, you will need to set up a development environment using the following steps:

  1. Install poetry
  2. Clone this repository
  3. Run poetry install inside the cloned repository

Acknowledgements

This project was inspired by Samuel Gaus's frattlesnake and tadpoleloop's pymafia repositories.

Metadata

Release files for pymafia 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymafia 0.2.4
File Size Uploaded Provenance Actions
pymafia-0.2.4.tar.gz 40.8 kB Without provenance Details for pymafia-0.2.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pymafia 0.2.4
File Interpreter ABI Platform Actions
pymafia-0.2.4-py3-none-any.whl Python 3 none any Details for pymafia-0.2.4-py3-none-any.whl

Total release size: 98.2 kB

Release files / pymafia-0.2.4.tar.gz

Download URL pymafia-0.2.4.tar.gz
Size 40.8 kB
Tags Source
SHA-256 checksum
How to use checksums
595e2839cfd95d72771d08bbc6828bcede410fdfc920531c00da2c6018a5e9a7
BLAKE2b-256 checksum
How to use checksums
412ca12cc6f79fe876440103e6d58bd0646e3e1d778e6e5c3e1934f54fb5ae40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure

Release files / pymafia-0.2.4-py3-none-any.whl

Download URL pymafia-0.2.4-py3-none-any.whl
Size 57.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12a7bcf0e0b795899466223645fecc835ebfcc79015488138752d37ae2f80d16
BLAKE2b-256 checksum
How to use checksums
3f9a611290fb8901cf87711b001b1ec85154b9f9c41bbbcecc4672cf39369930
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure

Security for pymafia 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page