Skip to content

Commit 684ae53

Browse files
committed
Cleanup pass of examples directory.
Switch samples_tcod to use Path objects.
1 parent f07e684 commit 684ae53

File tree

10 files changed

+48
-50
lines changed

10 files changed

+48
-50
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"fmean",
146146
"fontx",
147147
"fonty",
148+
"freetype",
148149
"frombuffer",
149150
"fullscreen",
150151
"fwidth",

examples/distribution/PyInstaller/main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
# copyright and related or neighboring rights for the "hello world" PyInstaller
44
# example script. This work is published from: United States.
55
# https://creativecommons.org/publicdomain/zero/1.0/
6-
import os.path
6+
"""PyInstaller main script example."""
77
import sys
8+
from pathlib import Path
89

910
import tcod
1011

1112
WIDTH, HEIGHT = 80, 60
1213

1314
# The base directory, this is sys._MEIPASS when in one-file mode.
14-
BASE_DIR = getattr(sys, "_MEIPASS", ".")
15+
BASE_DIR = Path(getattr(sys, "_MEIPASS", "."))
1516

16-
FONT_PATH = os.path.join(BASE_DIR, "data/terminal8x8_gs_ro.png")
17+
FONT_PATH = BASE_DIR / "data/terminal8x8_gs_ro.png"
1718

1819

1920
def main() -> None:
21+
"""Entry point function."""
2022
tileset = tcod.tileset.load_tilesheet(FONT_PATH, 16, 16, tcod.tileset.CHARMAP_CP437)
2123
with tcod.context.new(columns=WIDTH, rows=HEIGHT, tileset=tileset) as context:
2224
while True:

examples/distribution/cx_Freeze/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python3
2+
"""cx_Freeze main script example."""
23
import tcod
34

45
WIDTH, HEIGHT = 80, 60
56
console = None
67

78

89
def main() -> None:
10+
"""Entry point function."""
911
tileset = tcod.tileset.load_tilesheet("data/terminal8x8_gs_ro.png", 16, 16, tcod.tileset.CHARMAP_CP437)
1012
with tcod.context.new(columns=WIDTH, rows=HEIGHT, tileset=tileset) as context:
1113
while True:

examples/eventget.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# copyright and related or neighboring rights for this example. This work is
44
# published from: United States.
55
# https://creativecommons.org/publicdomain/zero/1.0/
6-
"""An demonstration of event handling using the tcod.event module.
7-
"""
6+
"""An demonstration of event handling using the tcod.event module."""
87
from typing import List, Set
98

109
import tcod
@@ -15,8 +14,7 @@
1514

1615

1716
def main() -> None:
18-
"""Example program for tcod.event"""
19-
17+
"""Example program for tcod.event."""
2018
event_log: List[str] = []
2119
motion_desc = ""
2220
tcod.sdl.joystick.init()

examples/framerate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# copyright and related or neighboring rights for this example. This work is
44
# published from: United States.
55
# https://creativecommons.org/publicdomain/zero/1.0/
6-
"""A system to control time since the original libtcod tools are deprecated.
7-
"""
6+
"""A system to control time since the original libtcod tools are deprecated."""
87
import statistics
98
import time
109
from collections import deque
@@ -23,6 +22,7 @@ class Clock:
2322
"""
2423

2524
def __init__(self) -> None:
25+
"""Initialize this object with empty data."""
2626
self.last_time = time.perf_counter() # Last time this was synced.
2727
self.time_samples: Deque[float] = deque() # Delta time samples.
2828
self.max_samples = 64 # Number of fps samples to log. Can be changed.
@@ -138,7 +138,7 @@ def main() -> None:
138138
context.convert_event(event) # Set tile coordinates for event.
139139
if isinstance(event, tcod.event.Quit):
140140
raise SystemExit()
141-
elif isinstance(event, tcod.event.MouseWheel):
141+
if isinstance(event, tcod.event.MouseWheel):
142142
desired_fps = max(1, desired_fps + event.y)
143143

144144

examples/samples_tcod.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import copy
99
import math
10-
import os
1110
import random
1211
import sys
1312
import time
1413
import warnings
15-
from typing import Any, List
14+
from pathlib import Path
15+
from typing import Any
1616

1717
import numpy as np
1818
from numpy.typing import NDArray
@@ -21,19 +21,15 @@
2121
import tcod.render
2222
import tcod.sdl.render
2323

24+
# ruff: noqa: S311
25+
2426
if not sys.warnoptions:
2527
warnings.simplefilter("default") # Show all warnings.
2628

29+
DATA_DIR = Path(__file__).parent / "../libtcod/data"
30+
"""Path of the samples data directory."""
2731

28-
def get_data(path: str) -> str:
29-
"""Return the path to a resource in the libtcod data directory,"""
30-
SCRIPT_DIR = os.path.dirname(__file__)
31-
DATA_DIR = os.path.join(SCRIPT_DIR, "../libtcod/data")
32-
assert os.path.exists(DATA_DIR), (
33-
"Data directory is missing," " did you forget to run `git submodule update --init`?"
34-
)
35-
return os.path.join(DATA_DIR, path)
36-
32+
assert DATA_DIR.exists(), "Data directory is missing, did you forget to run `git submodule update --init`?"
3733

3834
WHITE = (255, 255, 255)
3935
GREY = (127, 127, 127)
@@ -45,7 +41,7 @@ def get_data(path: str) -> str:
4541
SAMPLE_SCREEN_HEIGHT = 20
4642
SAMPLE_SCREEN_X = 20
4743
SAMPLE_SCREEN_Y = 10
48-
FONT = get_data("fonts/dejavu10x10_gs_tc.png")
44+
FONT = DATA_DIR / "fonts/dejavu10x10_gs_tc.png"
4945

5046
# Mutable global names.
5147
context: tcod.context.Context
@@ -564,10 +560,9 @@ def draw_ui(self) -> None:
564560
1,
565561
1,
566562
"IJKL : move around\n"
567-
"T : torch fx %s\n"
568-
"W : light walls %s\n"
569-
"+-: algo %s"
570-
% (
563+
"T : torch fx {}\n"
564+
"W : light walls {}\n"
565+
"+-: algo {}".format(
571566
"on " if self.torch else "off",
572567
"on " if self.light_walls else "off",
573568
FOV_ALGO_NAMES[self.algo_num],
@@ -1032,9 +1027,9 @@ class ImageSample(Sample):
10321027
def __init__(self) -> None:
10331028
self.name = "Image toolkit"
10341029

1035-
self.img = tcod.image_load(get_data("img/skull.png"))
1030+
self.img = tcod.image_load(DATA_DIR / "img/skull.png")
10361031
self.img.set_key_color(BLACK)
1037-
self.circle = tcod.image_load(get_data("img/circle.png"))
1032+
self.circle = tcod.image_load(DATA_DIR / "img/circle.png")
10381033

10391034
def on_draw(self) -> None:
10401035
sample_console.clear()
@@ -1068,7 +1063,7 @@ def __init__(self) -> None:
10681063

10691064
self.motion = tcod.event.MouseMotion()
10701065
self.mouse_left = self.mouse_middle = self.mouse_right = 0
1071-
self.log: List[str] = []
1066+
self.log: list[str] = []
10721067

10731068
def on_enter(self) -> None:
10741069
tcod.mouse_move(320, 200)
@@ -1141,15 +1136,15 @@ def __init__(self) -> None:
11411136

11421137
self.curset = 0
11431138
self.delay = 0.0
1144-
self.names: List[str] = []
1145-
self.sets: List[str] = []
1139+
self.names: list[str] = []
1140+
self.sets: list[str] = []
11461141

11471142
def on_draw(self) -> None:
11481143
if not self.sets:
11491144
# parse all *.cfg files in data/namegen
1150-
for file in os.listdir(get_data("namegen")):
1151-
if file.find(".cfg") > 0:
1152-
tcod.namegen_parse(get_data(os.path.join("namegen", file)))
1145+
for file in (DATA_DIR / "namegen").iterdir():
1146+
if file.suffix == ".cfg":
1147+
tcod.namegen_parse(file)
11531148
# get the sets list
11541149
self.sets = tcod.namegen_get_sets()
11551150
print(self.sets)
@@ -1254,7 +1249,7 @@ def on_enter(self) -> None:
12541249
self.frac_t: float = RES_V - 1
12551250
self.abs_t: float = RES_V - 1
12561251
# light and current color of the tunnel texture
1257-
self.lights: List[Light] = []
1252+
self.lights: list[Light] = []
12581253
self.tex_r = 0.0
12591254
self.tex_g = 0.0
12601255
self.tex_b = 0.0

examples/termbox/termbox.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
Implementation of Termbox Python API in tdl.
1+
"""Implementation of Termbox Python API in tdl.
32
43
See README.md for details.
54
"""
@@ -18,10 +17,10 @@
1817

1918

2019
class TermboxException(Exception):
21-
def __init__(self, msg):
20+
def __init__(self, msg) -> None:
2221
self.msg = msg
2322

24-
def __str__(self):
23+
def __str__(self) -> str:
2524
return self.msg
2625

2726

@@ -153,7 +152,7 @@ def __str__(self):
153152

154153

155154
class Event:
156-
"""Aggregate for Termbox Event structure"""
155+
"""Aggregate for Termbox Event structure."""
157156

158157
type = None
159158
ch = None
@@ -169,10 +168,11 @@ def gettuple(self):
169168

170169

171170
class Termbox:
172-
def __init__(self, width=132, height=60):
171+
def __init__(self, width=132, height=60) -> None:
173172
global _instance
174173
if _instance:
175-
raise TermboxException("It is possible to create only one instance of Termbox")
174+
msg = "It is possible to create only one instance of Termbox"
175+
raise TermboxException(msg)
176176

177177
try:
178178
self.console = tdl.init(width, height)
@@ -183,7 +183,7 @@ def __init__(self, width=132, height=60):
183183

184184
_instance = self
185185

186-
def __del__(self):
186+
def __del__(self) -> None:
187187
self.close()
188188

189189
def __exit__(self, *args): # t, value, traceback):
@@ -289,5 +289,6 @@ def poll_event(self):
289289
if e.type == "KEYDOWN":
290290
self.e.key = e.key
291291
return self.e.gettuple()
292+
return None
292293

293294
# return (e.type, uch, e.key, e.mod, e.w, e.h, e.x, e.y)

examples/termbox/termboxtest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/python
2-
# -*- encoding: utf-8 -*-
32

43
import termbox
54

@@ -17,8 +16,8 @@ def print_line(t, msg, y, fg, bg):
1716
t.change_cell(x + i, y, c, fg, bg)
1817

1918

20-
class SelectBox(object):
21-
def __init__(self, tb, choices, active=-1):
19+
class SelectBox:
20+
def __init__(self, tb, choices, active=-1) -> None:
2221
self.tb = tb
2322
self.active = active
2423
self.choices = choices

examples/thread_jobs.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_fov_single(maps: List[tcod.map.Map]) -> None:
4242

4343

4444
def test_fov_threads(executor: concurrent.futures.Executor, maps: List[tcod.map.Map]) -> None:
45-
for result in executor.map(test_fov, maps):
45+
for _result in executor.map(test_fov, maps):
4646
pass
4747

4848

@@ -57,7 +57,7 @@ def test_astar_single(maps: List[tcod.map.Map]) -> None:
5757

5858

5959
def test_astar_threads(executor: concurrent.futures.Executor, maps: List[tcod.map.Map]) -> None:
60-
for result in executor.map(test_astar, maps):
60+
for _result in executor.map(test_astar, maps):
6161
pass
6262

6363

@@ -66,8 +66,7 @@ def run_test(
6666
single_func: Callable[[List[tcod.map.Map]], None],
6767
multi_func: Callable[[concurrent.futures.Executor, List[tcod.map.Map]], None],
6868
) -> None:
69-
"""Run a function designed for a single thread and compare it to a threaded
70-
version.
69+
"""Run a function designed for a single thread and compare it to a threaded version.
7170
7271
This prints the results of these tests.
7372
"""

examples/ttf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def load_ttf(path: str, size: Tuple[int, int]) -> tcod.tileset.Tileset:
5959

6060

6161
def main() -> None:
62+
"""True-type font example script."""
6263
console = tcod.Console(16, 12, order="F")
6364
with tcod.context.new(
6465
columns=console.width,

0 commit comments

Comments
 (0)