Skip to content

Commit

Permalink
Add pyproject.toml and make various fixes (#10)
Browse files Browse the repository at this point in the history
* Add pyproject.toml and make various fixes

Signed-off-by: 1597463007 <squid-shape-fifty@duck.com>

* Fix flake8 error

Signed-off-by: 1597463007 <squid-shape-fifty@duck.com>

* Fix mypy error

Signed-off-by: 1597463007 <squid-shape-fifty@duck.com>

---------

Signed-off-by: 1597463007 <squid-shape-fifty@duck.com>
  • Loading branch information
1597463007 authored Sep 11, 2024
1 parent 9f3447f commit 423df9d
Show file tree
Hide file tree
Showing 17 changed files with 833 additions and 245 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python Linter And Unittest

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

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pyproject-flake8 mypy
pip install -r requirements.txt
- name: Lint with flake8
run: |
pflake8 .
- name: Lint with MyPy
run: |
mypy .
- name: Run python unittest
run: |
python -m unittest discover -v tests
58 changes: 58 additions & 0 deletions .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Scorecard supply-chain security
on:
# For Branch-Protection check. Only the default branch is supported. See
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection
branch_protection_rule:
# To guarantee Maintained check is occasionally updated. See
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained
schedule:
- cron: '26 18 * * 0'
push:
branches: [ "main" ]

# Declare default permissions as read only.
permissions: read-all

jobs:
analysis:
name: Scorecard analysis
runs-on: ubuntu-latest
permissions:
# Needed to upload the results to code-scanning dashboard.
security-events: write
# Needed to publish results and get a badge (see publish_results below).
id-token: write
# Uncomment the permissions below if installing in a private repository.
# contents: read
# actions: read

steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- name: "Checkout code"
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
persist-credentials: false

- name: "Run analysis"
uses: ossf/scorecard-action@62b2cac7ed8198b15735ed49ab1e5cf35480ba46 # v2.4.0
with:
results_file: results.sarif
results_format: sarif
# (Optional) "write" PAT token. Uncomment the `repo_token` line below if:
# - you want to enable the Branch-Protection check on a *public* repository, or
# - you are installing Scorecard on a *private* repository
# To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat.
# repo_token: ${{ secrets.SCORECARD_TOKEN }}

# Public repositories:
# - Publish results to OpenSSF REST API for easy access by consumers
# - Allows the repository to include the Scorecard badge.
# - See https://github.com/ossf/scorecard-action#publishing-results.
# For private repositories:
# - `publish_results` will always be set to `false`, regardless
# of the value entered here.
publish_results: true
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
__pychace__/
*.py[cod]
*$py.class

build/
dist/
sdist/
wheels/
eggs/
.eggs/
.idea/
.vscode/
*.egg-info/
*.egg
.mypy_cache/

venv*/
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ _doc: requirements.txt
rm -fr docsvenv build; mkdir build
python3.8 -m venv docsvenv
. docsvenv/bin/activate; \
pip install -r docs/requirements.txt; \
pip install -r requirements.txt; \
cd docs; make clean && make html
pip install -r docs/requirements.txt; \
pip install -r requirements.txt; \
cd docs; make clean && make html
zip -r build/pargraph_docs.zip docs/build/html/*
10 changes: 4 additions & 6 deletions docs/source/api/index.rst → docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ Graph Generation
.. autoclass:: pargraph.Result
:members:

.. automethod:: __init__

.. autoclass:: pargraph.graph.decorators.Graphable
.. autoclass:: pargraph.graph.decorators.Graphable()
:members:
:special-members: __call__

.. automethod:: __call__
.. autoclass:: pargraph.graph.objects.Graph()
:members:

Graph Engine
------------

.. autoclass:: pargraph.GraphEngine
:members:

.. automethod:: __init__
3 changes: 3 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_css_files = ["style.css"]

# Autodoc settings
autodoc_member_order = "bysource"
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Welcome to Pargraph's documentation!
.. toctree::
:maxdepth: 2

api/index
api

Indices and tables
==================
Expand Down
2 changes: 1 addition & 1 deletion pargraph/about.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.1"
__version__ = "0.8.0"
17 changes: 8 additions & 9 deletions pargraph/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
import logging
from collections import defaultdict, deque
from concurrent.futures import FIRST_COMPLETED, Future, wait
from typing import Any, Dict, Iterable, List, Optional, Set
from typing import Any, DefaultDict, Dict, Hashable, Iterable, Optional, Set

from loky import get_reusable_executor

try:
from pargraph.utility.graphlib_graphblas import TopologicalSorter
except ImportError:
from graphlib import TopologicalSorter
from graphlib import TopologicalSorter # type: ignore[assignment,no-redef]


class Backend(metaclass=abc.ABCMeta):
@abc.abstractmethod
def submit(self, fn, /, *args, **kwargs) -> Future:
...
def submit(self, fn, /, *args, **kwargs) -> Future: ...


class GraphEngine:
Expand Down Expand Up @@ -58,8 +57,8 @@ def get(self, dsk: Dict, keys: Any, **kwargs) -> Any:
graph = TopologicalSorter(graphlib_graph)
graph.prepare()

results = {}
future_to_key = {}
results: Dict[Hashable, Any] = {}
future_to_key: Dict[Future[Any], Hashable] = {}

# perform automatic reference counting to free results that are no longer needed
def dereference_key(key):
Expand Down Expand Up @@ -139,7 +138,7 @@ def _pack_results(cls, results: Dict, keys: Any) -> Any:
return results[keys]

@classmethod
def _flatten_iter(cls, seq: Iterable, container=list) -> List:
def _flatten_iter(cls, seq: Iterable, container=list) -> Iterable:
for el in seq:
# recursively flatten specific container types
if isinstance(el, container):
Expand All @@ -149,7 +148,7 @@ def _flatten_iter(cls, seq: Iterable, container=list) -> List:

@staticmethod
def _is_submittable_function_computation(computation: Any) -> bool:
return isinstance(computation, tuple) and computation and callable(computation[0])
return isinstance(computation, tuple) and len(computation) > 0 and callable(computation[0])

def _submit_function_computation(self, computation: Any, results: Dict, **kwargs) -> Optional[Future]:
func, *args = computation
Expand Down Expand Up @@ -214,7 +213,7 @@ def flatten(value: Any) -> Set[Any]:

@staticmethod
def _create_ref_count_graph(graph: Dict) -> Dict:
ref_count_graph = defaultdict(int)
ref_count_graph: DefaultDict[str, int] = defaultdict(int)
for value in graph.values():
for sub_value in value:
ref_count_graph[sub_value] += 1
Expand Down
7 changes: 6 additions & 1 deletion pargraph/graph/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def my_function() -> Annotated[str, Result("my_output")]:
"""

def __init__(self, name: str):
"""
Initialize Result annotation
:param name: result name
"""
self.name = name

def get_name(self) -> str:
Expand All @@ -32,7 +37,7 @@ def get_name(self) -> str:
return self.name


def _get_output_names(function: Callable) -> Union[str, Tuple[str]]:
def _get_output_names(function: Callable) -> Union[str, Tuple[str, ...]]:
annotation = inspect.signature(function).return_annotation
origin = get_origin(annotation)

Expand Down
Loading

0 comments on commit 423df9d

Please sign in to comment.