Skip to content

Commit

Permalink
add dev tools an unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchobben committed Aug 14, 2024
1 parent f3a0e32 commit 044aa42
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 73 deletions.
9 changes: 9 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: eo-datascience
channels:
- conda-forge
dependencies:
- python=3.10
- pip
- mamba
- jupyter
- nbformat
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
34 changes: 34 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[metadata]
name = eo_datascience
version = attr: eo_datascience.__version__
description = Examples of TUWien Jupyter notebooks for education
author = TU Wien GEO MRS group
author_email = [email protected]
long_description = file: README.md
url = https://github.com/TUW-GEO/eo-datascience
platforms = any
classifiers =
Intended Audience :: Science/Research
Topic :: Scientific/Engineering
Topic :: Scientific/Engineering :: GIS
Topic :: Software Development :: Libraries
Programming Language :: Python :: 3
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Operating System :: POSIX
Natural Language :: English

[options]
package_dir =
= src
packages = find:
install_requires =
nbformat

[options.packages.find]
where = src

[options.extras_require]
test =
pytest
pytest-cov
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from setuptools import setup

setup()
73 changes: 0 additions & 73 deletions src/clean-nb.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/eo_datascience/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from eo_datascience._version import __commit__
from eo_datascience._version import __version__

name = "eo_datascience"
2 changes: 2 additions & 0 deletions src/eo_datascience/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__version__ = "v0.0.0"
__commit__ = "0000000"
60 changes: 60 additions & 0 deletions src/eo_datascience/clean_nb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import nbformat
from pathlib import Path

def clean_up_frontmatter(dir = './notebooks', save=False):
# Define the path to the notebooks
nb_paths = find_ipynb(dir)

# Iterate over the notebooks
for nb_path in nb_paths:
# Load the notebook
nb = nbformat.read(nb_path, as_version=4)
if nb.cells[0].source.startswith('---'):
#Load frontmatter
fm = nb.cells[0].source.split('\n')

# Extract the title and the subtitle
title, subtitle = '', ''
for line in fm:
if line.startswith('title'):
title = line.split(': ')[1]
if line.startswith('subtitle'):
subtitle = line.split(': ')[1]

# Update the cell
nb.cells[0].source = f'# {title}\n{subtitle}\n'

# Save the notebook
if save:
nbformat.write(nb, nb_path)
else:
return nb

def convert_refs(dir="./notebooks", save=True):
nb_paths = find_ipynb(dir)

# Iterate over the notebooks
for nb_path in nb_paths:
# Load the notebook
nb = nbformat.read(nb_path, as_version=4)
for i in range(len(nb.cells)):
if i != 0:
nb.cells[i].source = nb.cells[i].source.replace(r"[@", r"{cite}`").replace(r"]", r"`")

# Save the notebook
if save:
nbformat.write(nb, nb_path)
else:
return nb

def find_ipynb(dir):
root = Path(dir).resolve()
nb_paths = [root / file for file in os.listdir(root) if file.endswith('.ipynb')]
return nb_paths

def main():
clean_up_frontmatter()

if __name__ == '__main__':
main()
File renamed without changes.
29 changes: 29 additions & 0 deletions tests/mock.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"title: This a mock Jupyter file\n",
"subtitle: We use it for testing\n",
"author: anonymous\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[@ref1] "
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
10 changes: 10 additions & 0 deletions tests/test_quarto_nb_conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import nbformat
from pathlib import Path
import pytest
from eo_datascience.clean_nb import clean_up_frontmatter, convert_refs

def test_remove_front_matter():
assert clean_up_frontmatter("./tests", False)["cells"][0]["source"] == "# This a mock Jupyter file\nWe use it for testing\n"

def test_conversion_of_refs():
assert convert_refs("./tests", False)["cells"][1]["source"] == r'{cite}`ref1` '

0 comments on commit 044aa42

Please sign in to comment.