Skip to content

Commit

Permalink
Merge pull request #1 from marchdf/update
Browse files Browse the repository at this point in the history
Major rework
  • Loading branch information
marchdf authored Dec 4, 2023
2 parents e26c6e8 + d129ac8 commit 42aceed
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 100 deletions.
13 changes: 13 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[flake8]
max-line-length = 88
max-complexity = 10
select = C,E,F,W,B,B950,N,D
ignore = E203,E302,E501,W503,C901,FS003
docstring-convention = numpy
exclude =
.git,
__pycache__,
*.egg-info,
.nox,
.pytest_cache,
.mypy_cache
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: book-list-CI

on:
push:
branches: [main]
pull_request:
branches: [main]

concurrency:
group: ${{github.ref}}-${{github.head_ref}}-ci
cancel-in-progress: true

jobs:
CI:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.11']
poetry-version: ['1.7']
defaults:
run:
working-directory: ${{github.workspace}}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{matrix.python-version}}
- name: Run image
uses: abatilo/[email protected]
with:
poetry-version: ${{matrix.poetry-version}}
- name: Install Dependencies using Poetry
run: poetry install
- name: Formatting with black
run: poetry run black --check .
- name: Sort imports with isort
run: poetry run isort --check-only --diff .
- name: Lint with flake8
run: poetry run flake8 .
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
old
.DS_Store
__pycache__
poetry.lock
12 changes: 0 additions & 12 deletions Pipfile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The following repository contains several lists pertaining to books:
# Using

1. Add a recently read book to `data/read_books.md`
2. Run `./utils/alphabetize_list.py` to generate an alphabetized list of books
2. Run `poetry run alphabetize` to generate an alphabetized list of books
3. Commit and push

# To do
Expand Down
3 changes: 3 additions & 0 deletions book_list/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""book-list."""

__version__ = "0.1.0"
6 changes: 6 additions & 0 deletions book_list/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Executable for book-list package."""

import book_list.alphabetize as al

if __name__ == "__main__":
al.main()
77 changes: 77 additions & 0 deletions book_list/alphabetize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Alphabetize a list of books.
A simple script to parse lists of books and alphabetize based on the
author last name.
"""

import os
import time
from datetime import timedelta

import pandas as pd


def main():
"""Alphabetize a list of books."""
start = time.time()

# Setup
tmpfile = "tmp.md"
basedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
ifile = f"{basedir}/data/read_books.md"
ofile = f"{basedir}/data/read_books_alpha.md"

# Parse the list of books
df = pd.read_csv(
ifile,
sep="|",
skiprows=3, # skip markdown header
header=None,
usecols=[0, 1, 2, 3, 4], # dont take extra columns
names=["name", "title", "status", "date", "own"],
)

# Get the first and last names
names = df["name"].str.split()
df["first_name"] = [" ".join(n[:-1]) for n in names]
df["last_name"] = [n[-1] for n in names]

# sort based on last name
df = df.sort_values(
by=["last_name", "first_name", "title", "date", "status", "own"]
)

# Save to file
df.to_csv(
tmpfile,
sep="|",
index=False,
columns=["name", "title", "status", "date", "own"],
header=False,
)

# Add header lines from original table
with open(ifile) as f:
h1 = f.readline()
h2 = f.readline()
h3 = f.readline()

ftmp = open(tmpfile, "r")
with open(tmpfile, "r") as ftmp, open(ofile, "w") as of:
of.write(h1.rstrip() + " (alphabetized)\n")
of.write(h2)
of.write(h3)
for line in ftmp.readlines():
of.write(line)

# Remove temporary file
os.remove(tmpfile)

# output timer
end = time.time() - start
print(f"Elapsed time {timedelta(seconds=end)} (or {end:f} seconds)")


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion data/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Author | Series name | Next book
:------ |:------ |:------
Joe Abercrombie | Age of Madness |
James S. A. Corey | The Expanse | Nemisis gate
James S. A. Corey | The Expanse | Babylon's Ashes
Seth Dickinson | Masquerade |
Steve Erikson | Malazan Book of the Fallen | Deadhouse Gates
James Edward Gordon | | Structures
Expand Down
1 change: 1 addition & 0 deletions data/read_books.md
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,4 @@ James S. A. Corey | Cibola Burn | r | 10/2023 | N
George Saunders | Lincoln in the Bardo | r | 11/2023 | Y
Todd Rogers | Writing for Busy Readers | r | 11/2023 | N
Mick Herron | Spook Street | r | 11/2023 | N
James S. A. Corey | Nemesis Games | r | 12/2023 | N
1 change: 1 addition & 0 deletions data/read_books_alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ James S. A. Corey | Abaddon's Gate | r | 07/2023 | N
James S. A. Corey | Caliban's War | r | 03/2023 | N
James S. A. Corey | Cibola Burn | r | 10/2023 | N
James S. A. Corey | Leviathan Wakes | r | 02/2023 | N
James S. A. Corey | Nemesis Games | r | 12/2023 | N
Corneille | Le Cid | r | | Y
Bernard Cornwell | Azincourt | r | 06/2014 | Y
Stephen Crane | The Red Badge of Courage | r | 11/2019 | Y
Expand Down
58 changes: 58 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[tool.poetry]
name = "book-list"
version = "0.1.0"
description = "Lists of read books and recommendations"
authors = ["Marc Henry de Frahan <[email protected]>"]
license = "GPL-3.0"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"
pandas = "^2.1.3"

[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"
black = "^23.11.0"
flake8 = "^6.1.0"
flake8-bugbear = "^23.12.2"
pep8-naming = "^0.13.3"
flake8-docstrings = "^1.7.0"
flake8-use-fstring = "^1.4"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
alphabetize = "book_list.alphabetize:main"

[tool.black]
line-length = 88
target-version = ['py310']
preview = true
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
)
'''

[tool.isort]
profile = "black"
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 88
86 changes: 0 additions & 86 deletions utils/alphabetize_list.py

This file was deleted.

0 comments on commit 42aceed

Please sign in to comment.