🐛 Fix exception caused by the change in get_item method in pyo3 0.20.0

This commit is contained in:
洛水居室 2023-12-21 16:54:25 +08:00
parent 1358a2ca57
commit d8d4b1e34e
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
16 changed files with 3134 additions and 188 deletions

View File

@ -20,8 +20,8 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.9', '3.10', '3.11', '3.12']
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v4

7
develop.ps1 Normal file
View File

@ -0,0 +1,7 @@
$env:RUSTFLAGS="-Awarnings";
pip uninstall python_genshin_artifact -y
maturin develop
# py -m pip install maturin
# $env:PYTHONPATH="C:\Users\luoshuijs\AppData\Local\Programs\Python\Python311"
# cargo test

0
install.sh Normal file
View File

5
main.py Normal file
View File

@ -0,0 +1,5 @@
from python_genshin_artifact import Artifact
a = Artifact()
a.main_stat = ("ATKFixed", 1)

3
my_install.sh Normal file
View File

@ -0,0 +1,3 @@
$env:RUSTFLAGS="-Awarnings";
cargo build
maturin develop

4
rustfmt.toml Normal file
View File

@ -0,0 +1,4 @@
#max_width = 120
ignore = [
"genshin_artifact"
]

View File

@ -162,12 +162,14 @@ mod tests {
let hutao_dict = py_dict
.get_item("HuTao")
.unwrap()
.unwrap()
.downcast::<PyDict>()
.unwrap();
assert_eq!(
hutao_dict
.get_item("le_50")
.unwrap()
.unwrap()
.extract::<&str>()
.unwrap(),
"true"

View File

@ -153,12 +153,14 @@ mod tests {
let params_dict = py_dict
.get_item("StaffOfHoma")
.unwrap()
.unwrap()
.downcast::<PyDict>()
.unwrap();
assert_eq!(
params_dict
.get_item("be50_rate")
.unwrap()
.unwrap()
.extract::<f64>()
.unwrap(),
1.0

View File

@ -73,7 +73,8 @@ pub struct PyDamageAnalysis {
#[pymethods]
impl PyDamageAnalysis {
#[getter]
fn __dict__(&self, py: Python) -> PyResult<PyObject> { // skipcq: RS-R1000
fn __dict__(&self, py: Python) -> PyResult<PyObject> {
// skipcq: RS-R1000
let dict = PyDict::new(py);
fn insert_hashmap(

24
test.ps1 Normal file
View File

@ -0,0 +1,24 @@
$env:RUSTFLAGS="-Awarnings"
venv/Scripts/activate.ps1
maturin build
deactivate
$pythonPath = py -c "import sys; print(sys.exec_prefix)"
$env:PYTHONHOME = $pythonPath
$env:PYTHONPATH = $pythonPath
$Env:Path += ";$pythonPath"
py -m pip uninstall python_genshin_artifact -y
$wheelDirectory = ".\target\wheels"
$latestWheel = Get-ChildItem $wheelDirectory -Filter *.whl | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$wheelPath = Join-Path $wheelDirectory $latestWheel.Name
py -m pip install $wheelPath
cargo test
py -m pip uninstall python_genshin_artifact -y

2914
tests/data/enka.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,185 +0,0 @@
{
"character": {
"name": "HuTao",
"level": 81,
"ascend": false,
"constellation": 1,
"skill1": 9,
"skill2": 8,
"skill3": 7,
"params": {
"HuTao": {
"le_50": true
}
}
},
"weapon": {
"name": "DragonSBane",
"level": 90,
"ascend": false,
"refine": 4,
"params": "NoConfig"
},
"buffs": [],
"artifacts": [
{
"set_name": "WandererSTroupe",
"slot": "Flower",
"level": 20,
"star": 5,
"sub_stats": [
[
"CriticalRate",
0.14800000000000002
],
[
"Recharge",
5.8
],
[
"HPPercentage",
0.111
],
[
"CriticalDamage",
0.14800000000000002
]
],
"main_stat": [
"HPFixed",
4780.0
],
"id": 15003
},
{
"set_name": "CrimsonWitchOfFlames",
"slot": "Feather",
"level": 20,
"star": 5,
"sub_stats": [
[
"CriticalRate",
0.113
],
[
"ATKPercentage",
0.099
],
[
"CriticalDamage",
0.132
],
[
"DEFFixed",
19.0
]
],
"main_stat": [
"ATKFixed",
311.0
],
"id": 15006
},
{
"set_name": "CrimsonWitchOfFlames",
"slot": "Sand",
"level": 20,
"star": 5,
"sub_stats": [
[
"DEFPercentage",
0.11699999999999999
],
[
"CriticalRate",
0.039
],
[
"CriticalDamage",
0.19399999999999998
],
[
"Recharge",
14.9
]
],
"main_stat": [
"HPPercentage",
0.466
],
"id": 15006
},
{
"set_name": "EmblemOfSeveredFate",
"slot": "Goblet",
"level": 20,
"star": 5,
"sub_stats": [
[
"DEFFixed",
21.0
],
[
"CriticalDamage",
0.225
],
[
"HPFixed",
209.0
],
[
"CriticalRate",
0.113
]
],
"main_stat": [
"PyroBonus",
0.466
],
"id": 15020
},
{
"set_name": "WandererSTroupe",
"slot": "Head",
"level": 20,
"star": 5,
"sub_stats": [
[
"HPPercentage",
0.122
],
[
"DEFFixed",
21.0
],
[
"CriticalDamage",
0.21
],
[
"HPFixed",
418.0
]
],
"main_stat": [
"CriticalRate",
0.311
],
"id": 15003
}
],
"artifact_config": {
"config_crimson_witch_of_flames": {
"level": 1
}
},
"skill": {
"index": 7,
"config": {
"HuTao": {
"after_e": true
}
}
},
"enemy": null
}

32
tests/test_enka.py Normal file
View File

@ -0,0 +1,32 @@
import json
import logging
import os
from python_genshin_artifact import get_damage_analysis, get_transformative_damage
from python_genshin_artifact.enka.enka_parser import enka_parser
from python_genshin_artifact import CalculatorConfig
from python_genshin_artifact import SkillInterface
PATH = os.path.dirname(os.path.abspath(__file__))
logger = logging.getLogger(__name__)
enka_test_file = os.path.join(PATH, "data", "enka.json")
with open(enka_test_file, "r", encoding="utf-8") as file:
enka_data = json.load(file)
def test_enka_parser():
character, weapon, artifacts = enka_parser(enka_data, 10000046)
logger.info(character)
logger.info(weapon)
logger.info(artifacts)
s = {"index": 7, "config": {"HuTao": {"after_e": True}}}
artifact_config = {"config_crimson_witch_of_flames": {"level": 1}}
skill = SkillInterface(**s)
calculator_config = CalculatorConfig(
character=character, weapon=weapon, artifacts=artifacts, skill=skill, artifact_config=artifact_config
)
damage_analysis = get_damage_analysis(calculator_config)
logger.info(damage_analysis)
transformative_damage = get_transformative_damage(calculator_config)
logger.info(transformative_damage)

54
tools/ambe.py Normal file
View File

@ -0,0 +1,54 @@
import httpx
from python_genshin_artifact.enka.characters import characters_map
from python_genshin_artifact.enka.weapon import weapon_name_map, test_weapon_name_map
from python_genshin_artifact.enka.artifacts import artifacts_name_map
response = httpx.get("https://api.ambr.top/v2/chs/weapon")
items: dict = response.json()["data"]["items"]
need_add = {}
for key, value in items.items():
key_id = int(key)
if key_id in weapon_name_map:
continue
if key_id in test_weapon_name_map:
continue
name = value["route"].replace(" ", "")
need_add.setdefault(int(key), name)
print(need_add)
response = httpx.get("https://api.ambr.top/v2/chs/avatar")
items: dict = response.json()["data"]["items"]
need_add = {}
for key, value in items.items():
try:
key_id = int(key)
except ValueError as exc:
print(exc)
continue
if key_id in characters_map:
continue
name = value["route"].replace(" ", "")
need_add.setdefault(int(key), name)
print(need_add)
response = httpx.get("https://api.ambr.top/v2/chs/reliquary")
items: dict = response.json()["data"]["items"]
need_add = {}
for key, value in items.items():
key_id = int(key)
if key_id in artifacts_name_map:
continue
name = value["route"].replace(" ", "")
need_add.setdefault(int(key), name)
print(need_add)

30
tools/character.py Normal file
View File

@ -0,0 +1,30 @@
from typing import List
from python_genshin_artifact.assets import Assets
assets = Assets()
character = assets.character.get("Yelan")
locale = assets.locale.get("zh-cn")
print(f"name {locale[character.get('name_locale')]}")
for skill_map1 in character.get("skill_map1"):
index = skill_map1.get("index")
locale_index = skill_map1.get("locale_index")
print(f"skill_map1 index {index} locale {locale[locale_index]}")
for skill_map2 in character.get("skill_map2"):
index = skill_map2.get("index")
locale_index = skill_map2.get("locale_index")
print(f"skill_map2 index {index} locale {locale[locale_index]}")
for skill_map3 in character.get("skill_map3"):
index = skill_map3.get("index")
locale_index = skill_map3.get("locale_index")
print(f"skill_map3 index {index} locale {locale[locale_index]}")
print(locale.index("夜兰"))

53
tools/main.py Normal file
View File

@ -0,0 +1,53 @@
import json
from python_genshin_artifact import get_damage_analysis, gen_character_meta_as_json, gen_weapon_meta_as_json, \
gen_artifact_meta_as_json, gen_generate_locale_as_json
data = {
"character": {
"name": "HuTao",
"level": 90,
"ascend": True,
"constellation": 0,
"skill1": 10,
"skill2": 10,
"skill3": 10,
"params": "NoConfig",
},
"weapon": {
"name": "StaffOfHoma",
"level": 90,
"ascend": True,
"refine": 1,
"params": "NoConfig",
},
"buffs": [],
"artifacts": [],
"artifact_config": None,
"skill": {"index": 1, "config": {"HuTao": {"after_e": True}}},
"enemy": None,
}
# character_meta = gen_character_meta_as_json()
# with open("character.json", mode="w+", encoding="utf-8") as f:
# f.write(character_meta)
# print(character_meta)
#
# weapon_meta = gen_weapon_meta_as_json()
# with open("weapon.json", mode="w+", encoding="utf-8") as f:
# f.write(weapon_meta)
#
# artifact_meta = gen_artifact_meta_as_json()
# with open("artifact.json", mode="w+", encoding="utf-8") as f:
# f.write(artifact_meta)
#
# locale_meta = gen_generate_locale_as_json("zh-cn")
# with open("locale_zh_cn.json", mode="w+", encoding="utf-8") as f:
# f.write(locale_meta)
damage_analysis = get_damage_analysis(json.dumps(data))
data: dict = json.loads(damage_analysis)
for key, value in data.items():
if value:
print(key)
print(value)