A Python module and bridge for reflecting KoLmafia's Java environment
Project description
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:
- Install poetry
- Clone this repository
- Run
poetry install
inside the cloned repository
Acknowledgements
This project was inspired by Samuel Gaus's frattlesnake and tadpoleloop's pymafia repositories.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pymafia-0.2.4.tar.gz
.
File metadata
- Download URL: pymafia-0.2.4.tar.gz
- Upload date:
- Size: 40.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 595e2839cfd95d72771d08bbc6828bcede410fdfc920531c00da2c6018a5e9a7 |
|
MD5 | 9a7db3ffdb0f5dd1f96c1da1fd98bb9f |
|
BLAKE2b-256 | 412ca12cc6f79fe876440103e6d58bd0646e3e1d778e6e5c3e1934f54fb5ae40 |
File details
Details for the file pymafia-0.2.4-py3-none-any.whl
.
File metadata
- Download URL: pymafia-0.2.4-py3-none-any.whl
- Upload date:
- Size: 57.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12a7bcf0e0b795899466223645fecc835ebfcc79015488138752d37ae2f80d16 |
|
MD5 | 3d634176055e9fc6206be0d585fec6c7 |
|
BLAKE2b-256 | 3f9a611290fb8901cf87711b001b1ec85154b9f9c41bbbcecc4672cf39369930 |