Skip to content

Commit

Permalink
format code && add format ci and publish ci
Browse files Browse the repository at this point in the history
  • Loading branch information
张一涵 committed Apr 23, 2024
1 parent c856973 commit 582f2a4
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 56 deletions.
50 changes: 27 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: CI
on: [push, pull_request]
on: [ push, pull_request ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
OS: [Ubuntu, macOS, Windows]
python-version: ["3.7"]
OS: [ Ubuntu, macOS, Windows ]
python-version: [ "3.7" ]
include:
- os: Ubuntu
image: ubuntu-22.04
Expand All @@ -20,26 +20,30 @@ jobs:
shell: bash
steps:

- name: Checkout
uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2

- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
run: pip install poetry
- name: Install isort black
run: |
pip install isort==5.11.5 black==22.12.0
isort --check .
black --check . --force-exclude .tongyuan
- name: Test
run: |
pip install .
pip install pytest coverage
julia -e "import Pkg; Pkg.add(\"TyPython\")"
coverage run --source=tyjuliasetup -m pytest -s && coverage report && coverage xml
- name: Python Cov
uses: codecov/codecov-action@v2
with:
files: coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
- name: Test
run: |
pip install .
pip install pytest coverage
julia -e "import Pkg; Pkg.add(\"TyPython\")"
coverage run --source=tyjuliasetup -m pytest -s && coverage report && coverage xml
- name: Python Cov
uses: codecov/codecov-action@v2
with:
files: coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
41 changes: 41 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [ published ]
# 提交代码时,手动触发


permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
4 changes: 2 additions & 2 deletions .tongyuan/0826测试/0826测试-tyjuliacall.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def get_timespan():

print(f"TyJuliaCall 启动Julia+自动环境切换和检查+初始化PyCall+初始化PythonCall: {get_timespan():.2f} s")

from tyjuliacall import TySignalProcessing as sp
from tyjuliacall import TyPlot as typ
import numpy as np

from tyjuliacall import TyPlot as typ
from tyjuliacall import TySignalProcessing as sp

print(f"TyJuliaCall 导入TySignalProcess, TyPlot和NumPy: {get_timespan():.2f} s")

Expand Down
11 changes: 7 additions & 4 deletions .tongyuan/bench.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from tyjuliasetup import use_sysimage
import os

from tyjuliasetup import use_sysimage

if os.path.exists("my-image.dll"):
use_sysimage("my-image.dll")

from tyjuliacall import TySignalProcessing as sp # type: ignore
from tyjuliacall import Base # type: ignore
import numpy as np

from tyjuliacall import Base # type: ignore
from tyjuliacall import TySignalProcessing as sp # type: ignore

fs = 10000
t = np.arange(fs + 1) / fs
x = np.sin(2 * np.pi * t*3) + 0.25*np.sin(2 * np.pi * t*40)
x = np.sin(2 * np.pi * t * 3) + 0.25 * np.sin(2 * np.pi * t * 40)

y = sp.medfilt1(x, 9)

Expand Down
18 changes: 9 additions & 9 deletions build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tyjuliasetup')
TARGET = os.path.join(ROOT, 'julia_src_binding.py')
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tyjuliasetup")
TARGET = os.path.join(ROOT, "julia_src_binding.py")

# 递归遍历所有文件夹,对于后缀为jl的文件
# 读取其中的内容,作为字符串写入目标文件中
Expand All @@ -10,18 +10,18 @@
def read_jl_file(file):
file_name = os.path.basename(file)
code_name, _ = os.path.splitext(file_name)
with open(file, 'r', encoding="utf-8") as f:
with open(file, "r", encoding="utf-8") as f:
return code_name, f.read()


with open(TARGET, 'w', encoding="utf-8") as f:
f.write('# this file is auto-generated from build.py\n')
f.write('JL_SRC = {}\n\n')
with open(TARGET, "w", encoding="utf-8") as f:
f.write("# this file is auto-generated from build.py\n")
f.write("JL_SRC = {}\n\n")
for root, dirs, files in os.walk(ROOT):
for file in files:
if file.endswith('.jl'):
print(f'writing {file} into {TARGET}...')
if file.endswith(".jl"):
print(f"writing {file} into {TARGET}...")
code_name, code = read_jl_file(os.path.join(root, file))
f.writelines(f'JL_SRC["{code_name}"] = r"""\n')
f.writelines([code, '\n'])
f.writelines([code, "\n"])
f.writelines('"""\n')
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ julia-numpy = "^0.4.3"
[tool.poetry.dev-dependencies]
pytest = "^5.2"


[tool.poetry.group.dev.dependencies]
isort = "5.11.5"
black = "22.12.0"


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


[tool.isort]
profile = "black"
skip = [".tongyuan", ".venv", "logs"]
5 changes: 3 additions & 2 deletions tests/test_tyjuliacall.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ def test_all():
use_sysimage(sysimage.decode("utf-8").strip())

# test conversion
from tyjuliacall import JV, JuliaEvaluator, Base
from tyjuliasetup import _get_pyjulia_core_provider
import numpy as np

from tyjuliacall import JV, Base, JuliaEvaluator
from tyjuliasetup import _get_pyjulia_core_provider

xs = JuliaEvaluator["zs = String[]"]
push_ = JuliaEvaluator["push!"]
push_(xs, "2")
Expand Down
4 changes: 2 additions & 2 deletions tyjuliacall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _setup_help():
class Helper(object):
def __init__(self):
if isinstance(__builtins__, dict):
self.help = __builtins__['help']
self.help = __builtins__["help"]
else:
self.help = __builtins__.help

Expand All @@ -26,7 +26,7 @@ def __call__(self, obj):
return self.help(obj)

if isinstance(__builtins__, dict):
__builtins__['help'] = Helper()
__builtins__["help"] = Helper()
else:
__builtins__.help = Helper()

Expand Down
46 changes: 33 additions & 13 deletions tyjuliasetup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from __future__ import annotations
from . import compat
import time

import contextlib
import ctypes
import shutil
import subprocess
import io
import os
import sys
import pathlib
import typing
import jnumpy
from jnumpy.init import JuliaError
import shlex
import shutil
import subprocess
import sys
import textwrap
import time
import typing
from types import ModuleType

import jnumpy
from jnumpy.init import JuliaError

from . import compat
from .julia_src_binding import JL_SRC

PYTHONPATH = pathlib.Path(sys.executable).resolve().as_posix()
Expand Down Expand Up @@ -216,9 +219,11 @@ def use_sysimage(path: str | pathlib.Path):
def use_system_typython(yes: bool = True):
pass

def use_backend(backend : typing.Literal['pycall', 'jnumpy']):

def use_backend(backend: typing.Literal["pycall", "jnumpy"]):
Environment.PYJULIA_CORE = backend


class _JuliaCodeEvaluatorClass:
_eval_func: typing.Any

Expand Down Expand Up @@ -258,12 +263,14 @@ def _exec_julia(x, use_template=True):
"name '_eval_jl' is not defined, should call tyjuliasetup.setup() first."
)


code_template = r"""
begin
{}
end
"""


def get_sysimage_and_projdir(jl_exe: str):
if Environment.TYPY_JL_SYSIMAGE:
sys_image = Environment.TYPY_JL_SYSIMAGE
Expand Down Expand Up @@ -297,6 +304,7 @@ def get_sysimage_and_projdir(jl_exe: str):
global_proj_dir = pathlib.Path(global_proj_dir.strip()).absolute().as_posix()
return sys_image, global_proj_dir


def setup():
global BASE_IMAGE
global GLOBAL_PROJ_DIR
Expand All @@ -310,7 +318,9 @@ def setup():
Environment.PYTHON = PYTHONPATH
Environment.PYCALL_INPROC_LIBPYPTR = hex(ctypes.pythonapi._handle)
Environment.PYCALL_INPROC_PROCID = str(os.getpid())
Environment.TYPY_JL_OPTS = shlex.join(["--sysimage", BASE_IMAGE, f"--project={GLOBAL_PROJ_DIR}"])
Environment.TYPY_JL_OPTS = shlex.join(
["--sysimage", BASE_IMAGE, f"--project={GLOBAL_PROJ_DIR}"]
)
Environment.add_path(os.path.dirname(PYTHONPATH))

# in case that users work with PythonCall
Expand All @@ -336,10 +346,15 @@ def _eval_jl(x: str, use_template=True):

if lib.jl_exception_occurred():
lib.jl_exception_clear()
raise JuliaError("Julia exception occurred while calling julia code:\n{}".format(textwrap.indent(x, " ")))
raise JuliaError(
"Julia exception occurred while calling julia code:\n{}".format(
textwrap.indent(x, " ")
)
)
else:
lib.jl_exception_clear()
return None

return

user_set_pyjulia_core = Environment.PYJULIA_CORE
Expand All @@ -359,7 +374,9 @@ def _eval_jl(x: str, use_template=True):
try:
_exec_julia("import TyPython")
except JuliaError:
raise JuliaError("Failed to import Julia package TyPython, try to install TyPython in Julia.") from None
raise JuliaError(
"Failed to import Julia package TyPython, try to install TyPython in Julia."
) from None

_exec_julia("TyPython.CPython.init()")

Expand All @@ -371,11 +388,14 @@ def _eval_jl(x: str, use_template=True):
f"""
{TyJuliaSetup_SRC}
TyJuliaSetup.init()
""", use_template=False)
""",
use_template=False,
)
except JuliaError:
raise JuliaError("Failed to init TyJuliaSetup.") from None

import _tyjuliacall_jnumpy # type: ignore

from tyjuliasetup import jv

_tyjuliacall_jnumpy.setup_jv(jv.JV, jv)
Expand Down
4 changes: 3 additions & 1 deletion tyjuliasetup/julia_src_binding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# this file is auto-generated from build.py
JL_SRC = {}

JL_SRC["TyJuliaSetup"] = r"""
JL_SRC[
"TyJuliaSetup"
] = r"""
module TyJuliaSetup
using TyPython
using TyPython.CPython
Expand Down
1 change: 1 addition & 0 deletions tyjuliasetup/jv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations

import typing

__jl_invoke__: typing.Callable[[JV, tuple, dict], typing.Any]
Expand Down

0 comments on commit 582f2a4

Please sign in to comment.