Skip to content

Commit

Permalink
✨ Update type annotations and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeta611 committed May 1, 2024
1 parent d8d4995 commit 8af936c
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 253 deletions.
111 changes: 98 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@

# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
# Created by https://www.toptal.com/developers/gitignore/api/macos,python,vim
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,python,vim

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

### Python ###
# Byte-compiled / optimized / DLL files
Expand All @@ -25,7 +56,6 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
Expand Down Expand Up @@ -55,7 +85,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log
cover/

# Translations
*.mo
Expand All @@ -76,9 +106,9 @@ instance/

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
Expand All @@ -89,7 +119,9 @@ profile_default/
ipython_config.py

# pyenv
.python-version
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand All @@ -98,7 +130,22 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand All @@ -116,7 +163,6 @@ venv/
ENV/
env.bak/
venv.bak/
pythonenv*

# Spyder project settings
.spyderproject
Expand All @@ -139,7 +185,46 @@ dmypy.json
# pytype static type analyzer
.pytype/

# profiling data
.prof

# End of https://www.toptal.com/developers/gitignore/api/python
# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml

# ruff
.ruff_cache/

# LSP config files
pyrightconfig.json

### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim
Sessionx.vim

# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~

# End of https://www.toptal.com/developers/gitignore/api/macos,python,vim
n
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.19
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# GoLPy

[![GitHub
license](https://img.shields.io/github/license/Zeta611/golpy?style=flat-square)](https://github.com/Zeta611/golpy/blob/master/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/golpy?style=flat-square)](https://pypi.org/project/golpy/)
Expand All @@ -7,20 +8,24 @@ license](https://img.shields.io/github/license/Zeta611/golpy?style=flat-square)]
GoLPy is an efficient Conway's Game of Life implemented in Python using NumPy.

## Example Output

The following GIF can be generated using the command:

```sh
life --demo glidergun --out glider_gun.gif --ppc 10 --pos TL -W60 -H40
```

![The Gosper Glider Gun](glider_gun.gif)

## Installation
```

```sh
pip install golpy
```

## Usage
```

```sh
usage: life [-h] (-i GRID_INPUT | -d DEMO) [-o FILE | --debug-print]
[-W WIDTH] [-H HEIGHT] [-M MAX_GEN] [--ppc PIXELS] [-P POSITION]
[-p]
Expand Down Expand Up @@ -50,8 +55,15 @@ optional arguments:
-p, --profile Measure the performance
```

## Input Format
To use without installing,

```sh
python -m golpy # ...
```

## Input Format

```txt
........................O
......................O.O
............OO......OO............OO
Expand All @@ -66,4 +78,5 @@ OO........O...O.OO....O.O
Use `.` for a dead cell, `O` (`chr(79)`) for a live cell.

## License

[MIT](LICENSE)
Binary file modified glider_gun.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions golpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import functools
import textwrap
from pathlib import Path
from typing import Callable, Dict, List, Tuple
from typing import Callable

import numpy as np
from PIL import Image
Expand Down Expand Up @@ -125,7 +125,7 @@ def grid_print(grid: np.ndarray, generation: int) -> None:

@timeit()
def parse_grid(
text: str, size: Tuple[int, int], pos: str = "C", live: str = "O"
text: str, size: tuple[int, int], pos: str = "C", live: str = "O"
) -> np.ndarray:
lines = textwrap.dedent(text).strip().splitlines()
text_width = max(len(line) for line in lines)
Expand All @@ -139,7 +139,7 @@ def parse_grid(

grid = np.zeros((height, width), dtype="uint8")

pos_idx: Dict[str, Tuple[int, int]] = {
pos_idx: dict[str, tuple[int, int]] = {
"C": (height // 2 - text_height // 2, width // 2 - text_width // 2),
"T": (0, width // 2 - text_width // 2),
"B": (height - text_height, width // 2 - text_width // 2),
Expand All @@ -166,7 +166,7 @@ def parse_grid(
def add_grid_frame(
grid: np.ndarray,
generation: int,
grid_frames: List[np.ndarray],
grid_frames: list[np.ndarray],
pixels_per_cell: int,
) -> None:
"""Add the grid to the grid_frames"""
Expand All @@ -185,7 +185,7 @@ def enlarge_image(image: np.ndarray, ratio: int) -> np.ndarray:

@timeit()
def save_frames(
grid_frames: List[Image.Image], filename: str, duration: int = 50
grid_frames: list[Image.Image], filename: str, duration: int = 50
) -> None:
grid_frames[0].save(
filename,
Expand All @@ -196,7 +196,7 @@ def save_frames(
)


def get_demo(name: str, size: Tuple[int, int], pos: str = "C") -> np.ndarray:
def get_demo(name: str, size: tuple[int, int], pos: str = "C") -> np.ndarray:
if name == "random":
return np.random.randint(0, 2, size, dtype="uint8")

Expand Down Expand Up @@ -321,7 +321,7 @@ def main() -> None:
grid = get_demo(args.demo, size, pos)

# Run Game of Life
grid_frames: List[Image.Image] = []
grid_frames: list[Image.Image] = []

if args.debug_print:
driver(grid, handler=grid_print, max_gen=max_gen)
Expand Down
3 changes: 1 addition & 2 deletions golpy/util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import time
from functools import wraps
from typing import Dict


class timeit:
records: Dict[str, float] = {}
records: dict[str, float] = {}
on = False

def __call__(self, func):
Expand Down
Loading

0 comments on commit 8af936c

Please sign in to comment.