Skip to main content

A Python library for playing, visualizing, and storing chess games.

Project description

Chesssnake

chesssnake is a feature-packed Python library for playing, visualizing, and storing chess games

Pronounced "chess - snake", in reference to Python being a type of snake. It is not pronounced cheesecake

Features

  • Play chess in Python with an easy-to-use and intuitive API
  • Store and retrieve chess games in a PostgreSQL database, without having to write any sql
    • Highly optimized SQL included
  • Generate PNG or JPEG images files of your game
  • PIL image support for manipulating images of your chess games
  • Includes a highly optimized python-only chess library

Installation

Basic Installation

To install the core features of Chesssnake, run:

pip install chesssnake

With PostgreSQL Support

This library uses psycopg2 to communicate with postgres. There are two ways to install psycopg2: with a binary or from source

To install chesssnake with PostgresSQL support, using psycopg2-binary (recommended for beginners, development, and for non-serious purposes)

pip install chesssnake[postgres-binary]

To install chesssnake with PostgresSQL support, using psycopg2 from source (recommended for production and packaging)

pip install chesssnake[postgres]

See psycopg2 build prerequisites for prerequisites when installing from source

Usage

This library's API is focused around a Game object. Every Game object represents a game between two players

Basic usage

A simple example:

from chesssnake import Game

# Initialize a new game
game = Game(white_name="Bob", black_name="Phil")

# Make moves
game.move('e4') # Bob's move
game.move('e5') # Phil's move

# Print the board
print(game)

# make the move, return a PIL image object, and show the board in png format
game.move('Nc3', img=True).show()

# save the board as a png
game.save('/path/to/your/image1.png')

# make the move, and save the board as a png
game.move('Bc5', save='/path/to/your/image2.png')

With PostgreSQL support

If you've installed chesssnake with PostgreSQL support, you can store and retrieve games from a database.

Before using chesssnake, you must create environment variables that point to your database. There are many ways to do this, but for this example I will use the python-dotenv package to load variables from a .env file

Create a file named .env. Add your database information

CHESSDB_NAME='name_of_your_postgresql_db'
CHESSDB_USER='user_for_your_postgresql_db'
CHESSDB_PASS='password_for_your_postgresql_user'
CHESSDB_HOST='host_for_your_postgresql_db'
CHESSDB_PORT='port_for_your_postgres_db'

CHESSDB_HOST and CHESSDB_PORT are optional, and will default to localhost and 5432 respectively

Now creating and storing games with PostgreSQL is easy:

from chesssnake import Game
from dotenv import load_dotenv

# load our env vars
load_dotenv()

# Initialize a new game with PostgreSQL
## If a game already exists in our database, we load the game into memory
## If it doesn't, chesssnake creates a new game in the database and loads a new game into memory
## Uniqueness of games is determined by unique combinations of "white_id", "black_id", and "group_id" (all BIG INTs)
game = Game(
  white_id=123,
  black_id=456,
  group_id=789,
  white_name="Bob", 
  black_name="Phil", 
  sql=True
)

game.move('e4') # Bob's move
game.move('e5') # Phil's move

# update the database with any new moves
game.update_db()

If you use auto_sql instead of sql, your games will be automatically updated with every move and with less transactions.

For more information on using chesssnake with PostgreSQL, see the docs (coming soon)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chesssnake-0.3.3.tar.gz (137.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chesssnake-0.3.3-py3-none-any.whl (150.8 kB view details)

Uploaded Python 3

File details

Details for the file chesssnake-0.3.3.tar.gz.

File metadata

  • Download URL: chesssnake-0.3.3.tar.gz
  • Upload date:
  • Size: 137.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for chesssnake-0.3.3.tar.gz
Algorithm Hash digest
SHA256 8eb3046f53f64fac802e0a96bef8ea7c51666af233a62011a97f197eaeb7b288
MD5 68ed8822371e04bbc7f01e2720b36a98
BLAKE2b-256 c0c0ebc19035001948d81531be0b0493abe6ac0acf5e9f8946e2bfec7126752e

See more details on using hashes here.

File details

Details for the file chesssnake-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: chesssnake-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 150.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.7

File hashes

Hashes for chesssnake-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a48d4c8e4a51f122b3ea83f979dc4baffac42ac888efb066b80fa7815c0e3214
MD5 4e7d53f83b9bebc0b1d913b080a52b64
BLAKE2b-256 c2a936d1d650e91776f8ed25e4017e573bfbf1c656243cf278a67718e2616413

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page