Skip to content

Commit

Permalink
Chore/auto formatting workflow (#109)
Browse files Browse the repository at this point in the history
This PR adds a new GitHub workflow. Purpose of that is, for every commit
to either of the branches master or develop as well as any commit pushed
to a branch that is in a PR with a base branch of either master or
develop the new workflow should run and automatically apply code
formatting with black, import sorting with isort as well as update the
README.tex.md (porting changes over from README.md).

---------

Co-authored-by: Pietropaolo Frisoni <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 25, 2023
1 parent fa1a433 commit 9f771bc
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 60 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/auto-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: AutoUpdateFormatting
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop

jobs:
code-formatting:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10' # Specify the desired Python version

- name: Install dependencies
run: |
pip install -r requirements_test.txt
- name: Updating README files
id: update_readme
run: |
bash scripts/update-readme.sh
bash scripts/auto-commit.sh "Updating README files"
continue-on-error: true

- name: Code formatting and committing changes
id: code_format
run: |
bash scripts/auto-format.sh
bash scripts/auto-commit.sh "Automated formatting changes"
continue-on-error: true

- name: Push changes
if: ${{ steps.update_readme.outcome == 'success' || steps.code_format.outcome == 'success' }}
uses: ad-m/github-push-action@master
with:
branch: ${{ github.head_ref }}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,3 @@ Furthermore, it is also shown how the entire *Efficient Frontier* and the optima
Also, the optimisation of a portfolio and its visualisation based on a *Monte Carlo* is shown.

Finally, *FinQuant*'s visualisation methods allow for overlays, if this is desired. Thus, with only the following few lines of code, one can create an overlay of the *Monte Carlo* run, the *Efficient Frontier*, its optimised portfolios for *Minimum Volatility* and *Maximum Sharpe Ratio*, as well as the portfolio's individual stocks.

3 changes: 2 additions & 1 deletion example/Example-Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

# <codecell>

import datetime
import pathlib

import matplotlib.pyplot as plt
import pandas as pd
import datetime

# importing FinQuant's function to automatically build the portfolio
from finquant.portfolio import build_portfolio
Expand Down
3 changes: 2 additions & 1 deletion example/Example-Build-Portfolio-from-file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

# <codecell>

import datetime
import pathlib

import pandas as pd
import datetime

# importing FinQuant's function to automatically build the portfolio
from finquant.portfolio import build_portfolio
Expand Down
3 changes: 2 additions & 1 deletion example/Example-Build-Portfolio-from-web.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

# <codecell>

import pandas as pd
import datetime

import pandas as pd

# importing some custom functions/objects
from finquant.portfolio import build_portfolio

Expand Down
3 changes: 2 additions & 1 deletion example/Example-Optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

# <codecell>

import datetime
import pathlib

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import datetime

# importing FinQuant's function to automatically build the portfolio
from finquant.portfolio import build_portfolio
Expand Down
6 changes: 4 additions & 2 deletions finquant/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Asset:
"""

def __init__(self, data: pd.Series, name: str, asset_type: str = 'Market index') -> None:
def __init__(
self, data: pd.Series, name: str, asset_type: str = "Market index"
) -> None:
"""
:Input:
:data: ``pandas.Series``, of asset prices
Expand Down Expand Up @@ -119,4 +121,4 @@ def properties(self):
def __str__(self):
# print short description
string = f"Contains information about {self.asset_type}: {self.name}."
return string
return string
3 changes: 2 additions & 1 deletion finquant/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import numpy as np
import pandas as pd

from finquant.asset import Asset
from finquant.returns import daily_returns, historical_mean_return

Expand Down Expand Up @@ -43,4 +44,4 @@ def comp_daily_returns(self) -> pd.Series:
"""Computes the daily returns (percentage change) of the market index.
See ``finquant.returns.daily_returns``.
"""
return daily_returns(self.data)
return daily_returns(self.data)
2 changes: 1 addition & 1 deletion finquant/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
- Individual stocks of the portfolio (Expected Return over Volatility)
"""

from typing import List
import datetime
from typing import List

import matplotlib.pylab as plt
import numpy as np
Expand Down
1 change: 1 addition & 0 deletions finquant/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import numpy as np
import pandas as pd

from finquant.asset import Asset
from finquant.returns import daily_returns, historical_mean_return

Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black==23.1.0
black
mypy
isort
jupyter
Expand Down
3 changes: 3 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pytest>=7.3.2
black
mypy
isort
pylint
25 changes: 25 additions & 0 deletions scripts/auto-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

echo "Running auto commit"

COMMITMSG=$1

if [ -z "$COMMITMSG" ]; then
COMMITMSG="Automated formatting changes"
fi

# Stage changes
git add --update

# Check Git diff-index
git diff-index --quiet HEAD --

if [ $? -eq 0 ]; then
echo "No changes found, nothing to see/do here."
exit 1
else
echo "Changes found. Preparing commit."
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "${COMMITMSG}"
fi
6 changes: 6 additions & 0 deletions scripts/auto-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# Code formatting with isort and black
echo "Code formatting with isort and black:"
isort $(git ls-files '*.py')
black $(git ls-files '*.py')
43 changes: 43 additions & 0 deletions scripts/update-readme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

update_version_readme() {
local version_file="version"
local readme_md="$1"

# Read the current version from the "version" file
local current_version=$(grep -Eo 'version=([0-9]+\.){2}[0-9]+' "$version_file" | cut -d'=' -f2)

update_file() {
local file=$1
sed -i "s/pypi-v[0-9]\+\.[0-9]\+\.[0-9]\+/pypi-v$current_version/" "$file"
echo "Version updated to $current_version in $file"
}

# Update version in README.md
update_file "$readme_md"
}

update_readme_tex() {
local file_path="$1"

# Copy README.md to README.tex.md
cp README.md "$file_path"

# Read the contents of README.tex.md
local content=$(<"$file_path")

# Replace patterns
content=$(echo "$content" | sed -E "s/<img src=\"(.*?)\" align=middle width=194.52263655pt height=46.976899200000005pt\/>/\$\\\\displaystyle\\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_0} + \\\\text{dividend}}{\\\\text{price}_{t_0}}\$/")

content=$(echo "$content" | sed -E "s/<img src=\"(.*?)\" align=middle width=126.07712039999997pt height=48.84266309999997pt\/>/\$\\\\displaystyle\\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_{i-1}}}{\\\\text{price}_{t_{i-1}}}\$/")

content=$(echo "$content" | sed -E "s/<img src=\"(.*?)\" align=middle width=208.3327686pt height=57.53473439999999pt\/>/\$\\\\displaystyle\\\\log\\\\left(1 + \\\\dfrac{\\\\text{price}_{t_i} - \\\\text{price}_{t_{i-1}}}{\\\\text{price}_{t_{i-1}}}\\\\right)\$/")

# Write the updated contents back to README.tex.md
echo "$content" > "$file_path"
}

# Update both readme files:
echo "Updating README files:"
update_version_readme "README.md"
update_readme_tex "README.tex.md"
27 changes: 0 additions & 27 deletions scripts/update_readme.tex.md.sh

This file was deleted.

21 changes: 0 additions & 21 deletions scripts/update_version_readme.sh

This file was deleted.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def read_requirements(file_path):
install_requires = read_requirements("requirements.txt")

extras_require = {
"test": read_requirements("requirements_test.txt"),
"cd": read_requirements("requirements_cd.txt"),
"dev": read_requirements("requirements_dev.txt"),
"docs": read_requirements("requirements_docs.txt"),
"cd": read_requirements("requirements_cd.txt"),
"test": read_requirements("requirements_test.txt"),
}

setuptools.setup(
Expand Down

0 comments on commit 9f771bc

Please sign in to comment.