Skip to content

Commit

Permalink
Massive reformatting and style checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dalonsoa committed Nov 3, 2023
1 parent 9ba1063 commit d03329c
Show file tree
Hide file tree
Showing 38 changed files with 9,615 additions and 8,251 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ cython_debug/
# 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/
.idea/
128 changes: 85 additions & 43 deletions docs/create_class_page.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,111 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 2 10:37:05 2023
"""Created on Thu Feb 2 10:37:05 2023.
@author: Barney
"""

import os

import pandas as pd

from wsimod.core.core import WSIObj
from wsimod import nodes
from wsimod.nodes.nutrient_pool import NutrientPool
import pandas as pd
import os


def get_classes():
#Function to return all loaded subclasses of WSIObj (import arcs to include that)
"""
Returns:
"""

# Function to return all loaded subclasses of WSIObj (import arcs to include that)
def all_subclasses(cls):
return set(cls.__subclasses__()).union([s for c in cls.__subclasses__() for s in all_subclasses(c)])
"""
Args:
cls:
Returns:
"""
return set(cls.__subclasses__()).union(
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
)

subclasses = all_subclasses(WSIObj)
subclasses = {repr(x).split('.')[-1].replace("'>","") : x for x in subclasses}
subclasses = {repr(x).split(".")[-1].replace("'>", ""): x for x in subclasses}
return subclasses


def format_text(text):
#Convert docstring to a tidy format
lines = text.strip().split('\n')
lines = [line.strip().lstrip(' ') for line in lines]
lines = [' - ' + x.rstrip(' ') for x in ' '.join(lines).split('- ') if len(x) > 0]
return '<br>'.join(lines)
"""
Args:
text:
Returns:
#Identify which reference file to link modules to
"""
# Convert docstring to a tidy format
lines = text.strip().split("\n")
lines = [line.strip().lstrip(" ") for line in lines]
lines = [" - " + x.rstrip(" ") for x in " ".join(lines).split("- ") if len(x) > 0]
return "<br>".join(lines)


# Identify which reference file to link modules to
reference_lookup = {}
for file in os.listdir(os.getcwd()):
if 'reference-' in file:
if "reference-" in file:
with open(file) as f:
for line in f:
if ':::' in line:
reference_lookup[line.replace('::: ','').replace('\n','')] = file.replace('.md','')
if ":::" in line:
reference_lookup[
line.replace("::: ", "").replace("\n", "")
] = file.replace(".md", "")

#Iterate over classes
# Iterate over classes
subclasses = get_classes()
subclasses['NutrientPool'] = NutrientPool
subclasses["NutrientPool"] = NutrientPool
mytable = []
for subclass, obj in subclasses.items():
#Get docstring
# Get docstring
otext = obj.__init__.__doc__
if otext:
#Only include docstrings with Key assumptions
if 'Key assumptions:' in otext:
#Format reference
rclass = '.'.join(repr(obj).replace("<class '",'').replace("'>",'').split('.'))
module = '.'.join(rclass.split('.')[0:-1])
subclass = '[`{0}`](./../{1}/#{2})'.format(subclass, reference_lookup[module], rclass)

#Format assumptions
assum = format_text(otext.split('Key assumptions:')[1].split('Input data and parameter requirements:')[0])

#Format inputs
inputs = format_text(otext.split('Input data and parameter requirements:')[1])

#Append to table
mytable.append({'Component' : subclass, 'Assumptions' : assum, 'Data' : inputs})

#Convert to table
mytable = pd.DataFrame(mytable).sort_values(by = 'Component')
mytable['Assumptions'] = mytable['Assumptions'].str.replace('\n', '\n - ')
mytable['Component'] = mytable['Component'].str.replace('\n', '\n - ')
# Only include docstrings with Key assumptions
if "Key assumptions:" in otext:
# Format reference
rclass = ".".join(
repr(obj).replace("<class '", "").replace("'>", "").split(".")
)
module = ".".join(rclass.split(".")[0:-1])
subclass = "[`{0}`](./../{1}/#{2})".format(
subclass, reference_lookup[module], rclass
)

# Format assumptions
assum = format_text(
otext.split("Key assumptions:")[1].split(
"Input data and parameter requirements:"
)[0]
)

# Format inputs
inputs = format_text(
otext.split("Input data and parameter requirements:")[1]
)

# Append to table
mytable.append(
{"Component": subclass, "Assumptions": assum, "Data": inputs}
)

# Convert to table
mytable = pd.DataFrame(mytable).sort_values(by="Component")
mytable["Assumptions"] = mytable["Assumptions"].str.replace("\n", "\n - ")
mytable["Component"] = mytable["Component"].str.replace("\n", "\n - ")
front_text = """
## Introduction\n
WSIMOD contains a variety of components to represent physical processes.
Expand All @@ -75,7 +117,7 @@ def format_text(text):
## Component Library
"""

#Write
with open('component-library.md', 'w') as f:
# Write
with open("component-library.md", "w") as f:
f.write(front_text)
f.write(mytable.to_markdown(index = False))
f.write(mytable.to_markdown(index=False))
Loading

0 comments on commit d03329c

Please sign in to comment.