第八版

This commit is contained in:
YuWuKunCheng
2026-05-29 04:05:36 +08:00
parent ae57090d7f
commit 5c3179eec8
31 changed files with 4111 additions and 1730 deletions
+177 -148
View File
@@ -22,9 +22,37 @@
* SOFTWARE.
*/
use crate::kline_py::chan_kline_to_py;
use crate::structure_py::{dashed_to_py, fractal_to_py};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyType};
use std::rc::Rc;
use std::collections::HashMap;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::RwLock;
thread_local! {
static HUB_IDENTITY: RwLock<HashMap<usize, Py<Py>>> = RwLock::new(HashMap::new());
}
pub(crate) fn hub_to_py(
py: Python<'_>, inner: Arc<chanlun::algorithm::hub::>
) -> Py<Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
HUB_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
HUB_IDENTITY.with(|c| {
c.write().unwrap().retain(|_, v| v.get_refcnt(py) > 1);
});
let obj = Py::new(py, Py { inner }).unwrap();
HUB_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
use crate::business_py::Py;
use crate::config_py::Py;
@@ -60,7 +88,7 @@ impl 背驰分析Py {
: &str,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K线序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K线序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -107,7 +135,7 @@ impl 背驰分析Py {
K序列: Vec<Py<K线Py>>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -127,7 +155,7 @@ impl 背驰分析Py {
K序列: Vec<Py<K线Py>>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -148,7 +176,7 @@ impl 背驰分析Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> PyResult<bool> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -170,7 +198,7 @@ impl 背驰分析Py {
K序列: Vec<Py<K线Py>>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -192,7 +220,7 @@ impl 背驰分析Py {
: &str,
py: Python<'_>,
) -> PyResult<bool> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -234,13 +262,13 @@ impl 笔Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> PyResult<usize> {
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
let bi_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let bi_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
Ok(chanlun::algorithm::bi::::K数量(
@@ -255,13 +283,13 @@ impl 笔Py {
K序列: Vec<Py<crate::kline_py::K线Py>>,
: bool,
py: Python<'_>,
) -> Option<crate::kline_py::K线Py> {
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
) -> Option<Py<crate::kline_py::K线Py>> {
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::(&ck_list, )
.map(|inner| crate::kline_py::K线Py::from_rc(inner))
.map(|inner| crate::kline_py::chan_kline_to_py(py, inner))
}
#[classmethod]
@@ -271,13 +299,13 @@ impl 笔Py {
K序列: Vec<Py<crate::kline_py::K线Py>>,
: bool,
py: Python<'_>,
) -> Option<crate::kline_py::K线Py> {
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
) -> Option<Py<crate::kline_py::K线Py>> {
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::(&ck_list, )
.map(|inner| crate::kline_py::K线Py::from_rc(inner))
.map(|inner| crate::kline_py::chan_kline_to_py(py, inner))
}
#[classmethod]
@@ -287,13 +315,13 @@ impl 笔Py {
K序列: Vec<Py<crate::kline_py::K线Py>>,
: bool,
py: Python<'_>,
) -> Option<crate::kline_py::K线Py> {
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
) -> Option<Py<crate::kline_py::K线Py>> {
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::(&ck_list, )
.map(|inner| crate::kline_py::K线Py::from_rc(inner))
.map(|inner| crate::kline_py::chan_kline_to_py(py, inner))
}
#[classmethod]
@@ -303,13 +331,13 @@ impl 笔Py {
K序列: Vec<Py<crate::kline_py::K线Py>>,
: bool,
py: Python<'_>,
) -> Option<crate::kline_py::K线Py> {
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
) -> Option<Py<crate::kline_py::K线Py>> {
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::(&ck_list, )
.map(|inner| crate::kline_py::K线Py::from_rc(inner))
.map(|inner| crate::kline_py::chan_kline_to_py(py, inner))
}
#[classmethod]
@@ -335,9 +363,9 @@ impl 笔Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> Option<线Py> {
let bi_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let bi_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::(&bi_list, &.borrow().inner)
.map(|inner| 线Py { inner })
@@ -351,9 +379,9 @@ impl 笔Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> Option<线Py> {
let bi_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let bi_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::(&bi_list, &.borrow().inner)
.map(|inner| 线Py { inner })
@@ -369,9 +397,9 @@ impl 笔Py {
: i64,
py: Python<'_>,
) -> Option<线Py> {
let bi_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let bi_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::bi::::K找笔(&bi_list, &K.borrow().inner, )
.map(|inner| 线Py { inner })
@@ -392,20 +420,20 @@ impl 笔Py {
py: Python<'_>,
) -> PyResult<i64> {
let _ = ; // Python API 兼容参数,核心从0开始计数
let _rc = .map(|f| Rc::clone(&f.borrow().inner));
let mut fr_seq: Vec<Rc<chanlun::structure::fractal_obj::>> =
let _rc = .map(|f| Arc::clone(&f.borrow().inner));
let mut fr_seq: Vec<Arc<chanlun::structure::fractal_obj::>> =
.iter()
.map(|f| Rc::clone(&f.bind(py).borrow().inner))
.map(|f| Arc::clone(&f.bind(py).borrow().inner))
.collect();
let mut bi_seq: Vec<Rc<chanlun::structure::dash_line::线>> =
let mut bi_seq: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
let bar_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let bar_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -435,25 +463,25 @@ impl 笔Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> PyResult<i64> {
let mut fr_seq: Vec<Rc<chanlun::structure::fractal_obj::>> =
let mut fr_seq: Vec<Arc<chanlun::structure::fractal_obj::>> =
.iter()
.map(|f| Rc::clone(&f.bind(py).borrow().inner))
.map(|f| Arc::clone(&f.bind(py).borrow().inner))
.collect();
let mut bi_seq: Vec<Rc<chanlun::structure::dash_line::线>> =
let mut bi_seq: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let ck_list: Vec<Rc<chanlun::kline::chan_kline::K线>> = K序列
let ck_list: Vec<Arc<chanlun::kline::chan_kline::K线>> = K序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
let bar_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let bar_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
let config = .borrow().to_rust_config(py)?;
Ok(chanlun::algorithm::bi::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
&mut fr_seq,
&mut bi_seq,
&ck_list,
@@ -486,7 +514,7 @@ impl 笔Py {
let obs_ref = obs.obs();
chanlun::algorithm::bi::::(&.borrow().inner, &*obs_ref)
.into_iter()
.map(|d| 线Py { inner: Rc::new(d) })
.map(|d| 线Py { inner: Arc::new(d) })
.collect()
}
@@ -496,12 +524,13 @@ impl 笔Py {
_cls: &Bound<'_, PyType>,
: &Bound<'_, 线Py>,
: &Bound<'_, Py>,
) -> Vec<K线Py> {
py: Python<'_>,
) -> Vec<Py<K线Py>> {
let obs = .borrow();
let obs_ref = obs.obs();
chanlun::algorithm::bi::::(&.borrow().inner, &*obs_ref)
.into_iter()
.map(|ck| K线Py::from_rc(ck))
.map(|ck| chan_kline_to_py(py, ck))
.collect()
}
}
@@ -538,7 +567,7 @@ impl 线段Py {
: &Bound<'_, 线Py>,
: &Bound<'_, 线Py>,
) -> PyResult<()> {
let bi_rc = Rc::clone(&.borrow().inner);
let bi_rc = Arc::clone(&.borrow().inner);
let mut ref_mut = .borrow_mut();
chanlun::algorithm::segment::线::线(&mut ref_mut.inner, bi_rc);
Ok(())
@@ -555,7 +584,7 @@ impl 线段Py {
let mut ref_mut = .borrow_mut();
chanlun::algorithm::segment::线::(
&mut ref_mut.inner,
&Rc::clone(&.borrow().inner),
&Arc::clone(&.borrow().inner),
,
);
Ok(())
@@ -578,9 +607,9 @@ impl 线段Py {
py: Python<'_>,
) -> PyResult<()> {
let mut ref_mut = .borrow_mut();
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::segment::线::(&mut ref_mut.inner, &rc_list);
Ok(())
@@ -595,9 +624,9 @@ impl 线段Py {
py: Python<'_>,
) -> PyResult<()> {
let mut ref_mut = .borrow_mut();
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::segment::线::(&mut ref_mut.inner, &rc_list);
Ok(())
@@ -659,7 +688,8 @@ impl 线段Py {
: &Bound<'_, PyAny>,
: u32,
) -> PyResult<()> {
let seq: Vec<Option<Rc<chanlun::structure::segment_feat::线>>> = if .is_none() {
let seq: Vec<Option<Arc<chanlun::structure::segment_feat::线>>> = if .is_none()
{
vec![]
} else if let Ok(list) = .downcast::<pyo3::types::PyList>() {
let mut result = Vec::with_capacity(list.len());
@@ -668,7 +698,7 @@ impl 线段Py {
result.push(None);
} else {
let feat: PyRef<'_, 线Py> = item.extract()?;
result.push(Some(Rc::clone(&feat.inner)));
result.push(Some(Arc::clone(&feat.inner)));
}
}
result
@@ -704,6 +734,7 @@ impl 线段Py {
}
#[classmethod]
#[pyo3(signature = (段, 所属中枢 = None))]
/// 将线段基础序列分割为前/后/第三买卖/贯穿伤四部分
fn (
_cls: &Bound<'_, PyType>,
@@ -719,7 +750,7 @@ impl 线段Py {
let borrowed = .borrow();
let (a, b, c, d) = if let Some(hub_bound) = {
if let Ok(mut hub_ref) = hub_bound.extract::<PyRefMut<'_, Py>>() {
let inner_mut = Rc::make_mut(&mut hub_ref.inner);
let inner_mut = Arc::make_mut(&mut hub_ref.inner);
chanlun::algorithm::segment::线::(&borrowed.inner, Some(inner_mut))
} else {
chanlun::algorithm::segment::线::(&borrowed.inner, None)
@@ -727,7 +758,7 @@ impl 线段Py {
} else {
chanlun::algorithm::segment::线::(&borrowed.inner, None)
};
let wrap = |v: Vec<Rc<chanlun::structure::dash_line::线>>| -> PyResult<Vec<Py<线Py>>> {
let wrap = |v: Vec<Arc<chanlun::structure::dash_line::线>>| -> PyResult<Vec<Py<线Py>>> {
let mut result = Vec::new();
for x in v {
result.push(Py::new(py, 线Py { inner: x })?);
@@ -770,7 +801,7 @@ impl 线段Py {
&mut ref_mut.inner,
&config,
);
let pk_list = |v: Vec<Rc<chanlun::algorithm::hub::>>| -> PyResult<Py<PyAny>> {
let pk_list = |v: Vec<Arc<chanlun::algorithm::hub::>>| -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in v {
list.append(Py::new(py, Py { inner: h })?)?;
@@ -791,10 +822,10 @@ impl 线段Py {
py: Python<'_>,
) -> PyResult<()> {
let config = .borrow().to_rust_config(py)?;
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = vec![];
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = vec![];
chanlun::algorithm::segment::线::_添加线段(
&mut seg_seq,
Rc::clone(&线.borrow().inner),
Arc::clone(&线.borrow().inner),
&config,
,
);
@@ -812,10 +843,10 @@ impl 线段Py {
py: Python<'_>,
) -> PyResult<Option<线Py>> {
let config = .borrow().to_rust_config(py)?;
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = vec![];
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = vec![];
let result = chanlun::algorithm::segment::线::_弹出线段(
&mut seg_seq,
&Rc::clone(&线.borrow().inner),
&Arc::clone(&线.borrow().inner),
&config,
,
);
@@ -831,9 +862,9 @@ impl 线段Py {
: i64,
py: Python<'_>,
) -> PyResult<bool> {
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
Ok(chanlun::algorithm::segment::线::_缺口突破(
@@ -852,9 +883,9 @@ impl 线段Py {
: i64,
py: Python<'_>,
) -> PyResult<bool> {
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
Ok(chanlun::algorithm::segment::线::_非缺口下穿刺(
@@ -873,9 +904,9 @@ impl 线段Py {
: i64,
py: Python<'_>,
) -> PyResult<bool> {
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
Ok(chanlun::algorithm::segment::线::_缺口后紧急修正(
@@ -894,9 +925,9 @@ impl 线段Py {
: i64,
py: Python<'_>,
) -> PyResult<bool> {
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
Ok(chanlun::algorithm::segment::线::_修正(
@@ -918,13 +949,13 @@ impl 线段Py {
: Option<Vec<Py>>,
py: Python<'_>,
) -> PyResult<()> {
let bi_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let bi_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
let default_rel = vec![
@@ -953,13 +984,13 @@ impl 线段Py {
: u32,
py: Python<'_>,
) -> PyResult<()> {
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::segment::线::_添加扩展线段(
&mut seg_seq,
Rc::clone(&线.borrow().inner),
Arc::clone(&线.borrow().inner),
,
);
Ok(())
@@ -974,13 +1005,13 @@ impl 线段Py {
: u32,
py: Python<'_>,
) -> PyResult<Option<线Py>> {
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let result = chanlun::algorithm::segment::线::_弹出扩展线段(
&mut seg_seq,
&Rc::clone(&线.borrow().inner),
&Arc::clone(&线.borrow().inner),
,
);
Ok(result.map(|inner| 线Py { inner }))
@@ -995,13 +1026,13 @@ impl 线段Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> PyResult<()> {
let dash_list: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let dash_list: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let mut seg_seq: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let mut seg_seq: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(py)?;
chanlun::algorithm::segment::线::(&dash_list, &mut seg_seq, &config);
@@ -1034,7 +1065,7 @@ impl 线段Py {
let obs_ref = obs.obs();
chanlun::algorithm::segment::线::(&.borrow().inner, &*obs_ref)
.into_iter()
.map(|d| 线Py { inner: Rc::new(d) })
.map(|d| 线Py { inner: Arc::new(d) })
.collect()
}
@@ -1044,12 +1075,13 @@ impl 线段Py {
_cls: &Bound<'_, PyType>,
: &Bound<'_, 线Py>,
: &Bound<'_, Py>,
) -> Vec<K线Py> {
py: Python<'_>,
) -> Vec<Py<K线Py>> {
let obs = .borrow();
let obs_ref = obs.obs();
chanlun::algorithm::segment::线::(&.borrow().inner, &*obs_ref)
.into_iter()
.map(|ck| K线Py::from_rc(ck))
.map(|ck| chan_kline_to_py(py, ck))
.collect()
}
}
@@ -1093,10 +1125,10 @@ impl 线段Py {
/// 向中枢序列尾部添加(中枢序列, 新中枢) — 维护中枢序列顺序
/// 从中枢序列尾部弹出(中枢序列, 配置) — 弹出最后一个中枢并更新相邻中枢
/// 分析(虚线序列, 中枢序列容器, 允许重叠?, 标识前缀?, ...) — 中枢分析主流程
#[pyclass(name = "中枢", module = "chanlun._chanlun", unsendable, from_py_object)]
#[pyclass(name = "中枢", module = "chanlun._chanlun", from_py_object)]
#[derive(Clone)]
pub struct Py {
pub(crate) inner: Rc<chanlun::algorithm::hub::>,
pub(crate) inner: Arc<chanlun::algorithm::hub::>,
}
#[pymethods]
@@ -1105,12 +1137,12 @@ impl 中枢Py {
fn new(
: i64, : String, : i64, : Vec<Py<线Py>>, py: Python<'_>
) -> Self {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
Self {
inner: Rc::new(chanlun::algorithm::hub::::new(
inner: Arc::new(chanlun::algorithm::hub::::new(
, , , rc_list,
)),
}
@@ -1118,48 +1150,51 @@ impl 中枢Py {
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self) -> String {
self.inner..clone()
self.inner..read().unwrap().clone()
}
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.inner. {
list.append(线Py {
inner: Rc::clone(d),
})?;
for d in self.inner..read().unwrap().iter() {
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
#[getter]
fn 线(&self) -> Option<线Py> {
self.inner.线.as_ref().map(|d| 线Py {
inner: Rc::clone(d),
})
fn 线(&self, py: Python<'_>) -> Option<Py<线Py>> {
self.inner
.线
.read()
.unwrap()
.as_ref()
.map(|d| dashed_to_py(py, Arc::clone(d)))
}
#[getter]
fn _第三买卖线(&self) -> Option<线Py> {
self.inner._第三买卖线.as_ref().map(|d| 线Py {
inner: Rc::clone(d),
})
fn _第三买卖线(&self, py: Python<'_>) -> Option<Py<线Py>> {
self.inner
._第三买卖线
.read()
.unwrap()
.as_ref()
.map(|d| dashed_to_py(py, Arc::clone(d)))
}
/// 向中枢添加新虚线(延伸),重置第三买卖线
fn 线(&mut self, 线: &Bound<'_, 线Py>) {
let inner = Rc::make_mut(&mut self.inner);
inner.线(Rc::clone(&线.borrow().inner));
fn 线(&self, 线: &Bound<'_, 线Py>) {
self.inner.线(Arc::clone(&线.borrow().inner));
}
#[getter]
@@ -1170,10 +1205,8 @@ impl 中枢Py {
#[getter]
/// :return: 最后一条虚线
fn (&self) -> 线Py {
线Py {
inner: self.inner.(),
}
fn (&self, py: Python<'_>) -> Py<线Py> {
dashed_to_py(py, self.inner.())
}
#[getter]
@@ -1210,24 +1243,20 @@ impl 中枢Py {
#[getter]
/// :return: 起点分型
fn (&self) -> Py {
Py {
inner: self.inner.(),
}
fn (&self, py: Python<'_>) -> Py<Py> {
fractal_to_py(py, self.inner.())
}
#[getter]
/// :return: 终点分型
fn (&self) -> Py {
Py {
inner: self.inner.(),
}
fn (&self, py: Python<'_>) -> Py<Py> {
fractal_to_py(py, self.inner.())
}
/// 设置第三类买卖点关联虚线
fn 线(&mut self, 线: &Bound<'_, 线Py>) {
let inner = Rc::make_mut(&mut self.inner);
inner.线(Rc::clone(&线.borrow().inner));
let inner = Arc::make_mut(&mut self.inner);
inner.线(Arc::clone(&线.borrow().inner));
}
/// 获取中枢的完整虚线序列(基础序列+第三买卖线)
@@ -1253,11 +1282,11 @@ impl 中枢Py {
py: Python<'_>,
) -> bool {
let _ = ; // Python 版声明了此参数但未使用
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let inner = Rc::make_mut(&mut self.inner);
let inner = Arc::make_mut(&mut self.inner);
inner.(&rc_list)
}
@@ -1274,9 +1303,9 @@ impl 中枢Py {
: &Bound<'_, crate::config_py::Py>,
py: Python<'_>,
) -> PyResult<()> {
let mut hub_seq: Vec<Rc<chanlun::algorithm::hub::>> =
let mut hub_seq: Vec<Arc<chanlun::algorithm::hub::>> =
.iter()
.map(|h| Rc::clone(&h.bind(py).borrow().inner))
.map(|h| Arc::clone(&h.bind(py).borrow().inner))
.collect();
let config = .borrow().to_rust_config(.py())?;
self.inner.(&mut hub_seq, &config);
@@ -1315,13 +1344,13 @@ impl 中枢Py {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
// ---- classmethods ----
@@ -1353,10 +1382,10 @@ impl 中枢Py {
: &str,
) -> Self {
Self {
inner: Rc::new(chanlun::algorithm::hub::::(
Rc::clone(&.borrow().inner),
Rc::clone(&.borrow().inner),
Rc::clone(&.borrow().inner),
inner: Arc::new(chanlun::algorithm::hub::::(
Arc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
,
,
)),
@@ -1372,9 +1401,9 @@ impl 中枢Py {
: &str,
py: Python<'_>,
) -> Option<Self> {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::algorithm::hub::::(
&rc_list,
@@ -1391,7 +1420,7 @@ impl 中枢Py {
: &Bound<'_, PyAny>,
: &Bound<'_, Self>,
) -> PyResult<()> {
let inner = Rc::clone(&.borrow().inner);
let inner = Arc::clone(&.borrow().inner);
let wrapper = Py::new(.py(), Self { inner })?;
.call_method1("append", (wrapper,))?;
Ok(())
@@ -1410,7 +1439,7 @@ impl 中枢Py {
}
let bound: Bound<'_, Self> = result.extract()?;
Ok(Some(Self {
inner: Rc::clone(&bound.borrow().inner),
inner: Arc::clone(&bound.borrow().inner),
}))
}
@@ -1426,13 +1455,13 @@ impl 中枢Py {
: i64,
py: Python<'_>,
) -> PyResult<()> {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
let mut hub_seq: Vec<Rc<chanlun::algorithm::hub::>> =
let mut hub_seq: Vec<Arc<chanlun::algorithm::hub::>> =
.iter()
.map(|h| Rc::clone(&h.bind(py).borrow().inner))
.map(|h| Arc::clone(&h.bind(py).borrow().inner))
.collect();
chanlun::algorithm::hub::::(&rc_list, &mut hub_seq, , , );
Ok(())
+99 -94
View File
@@ -24,9 +24,13 @@
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyType};
use std::cell::RefCell;
use std::sync::RwLock;
use crate::algorithm_py::hub_to_py;
use crate::kline_py::bar_to_py;
use crate::structure_py::{dashed_to_py, fractal_to_py};
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use crate::algorithm_py::Py;
use crate::config_py::Py;
@@ -44,12 +48,7 @@ use crate::types_py::买卖点类型Py;
/// 有效性: bool — 买卖点是否仍有效
/// 与MACD柱子匹配: bool|None — 是否与MACD柱状图方向匹配
/// 与MACD柱子分型匹配: bool|None — 是否与MACD柱分型匹配
#[pyclass(
name = "基础买卖点",
module = "chanlun._chanlun",
unsendable,
from_py_object
)]
#[pyclass(name = "基础买卖点", module = "chanlun._chanlun", from_py_object)]
#[derive(Clone)]
pub struct Py {
pub(crate) inner: chanlun::business::bsp::,
@@ -69,7 +68,7 @@ impl 基础买卖点Py {
inner: chanlun::business::bsp::::new(
.borrow().inner,
K线.borrow().inner.clone(),
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
,
,
),
@@ -97,35 +96,35 @@ impl 基础买卖点Py {
#[getter]
fn (&self) -> Py {
Py {
inner: Rc::clone(&self.inner.),
inner: Arc::clone(&self.inner.),
}
}
#[getter]
fn K线(&self) -> K线Py {
K线Py::from_rc(Rc::clone(&self.inner.K线))
fn K线(&self, py: Python<'_>) -> Py<K线Py> {
crate::kline_py::chan_kline_to_py(py, Arc::clone(&self.inner.K线))
}
#[getter]
/// 当前K线
fn K线(&self) -> K线Py {
K线Py {
inner: self.inner.K线.clone(),
}
fn K线(&self, py: Python<'_>) -> Py<K线Py> {
bar_to_py(py, self.inner.K线.clone())
}
#[getter]
fn K线(&self) -> Option<K线Py> {
self.inner.K线.as_ref().map(|k| K线Py {
inner: Rc::clone(k),
})
fn K线(&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.inner
.K线
.as_ref()
.map(|k| bar_to_py(py, Arc::clone(k)))
}
#[getter]
fn K线(&self) -> Option<K线Py> {
self.inner.K线.as_ref().map(|k| K线Py {
inner: Rc::clone(k),
})
fn K线(&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.inner
.K线
.as_ref()
.map(|k| bar_to_py(py, Arc::clone(k)))
}
#[getter]
@@ -223,7 +222,7 @@ impl 买卖点Py {
) -> Py {
Py {
inner: chanlun::business::bsp::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
K线.borrow().inner.clone(),
,
,
@@ -244,7 +243,7 @@ impl 买卖点Py {
) -> Py {
Py {
inner: chanlun::business::bsp::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
K线.borrow().inner.clone(),
,
,
@@ -265,7 +264,7 @@ impl 买卖点Py {
) -> Py {
Py {
inner: chanlun::business::bsp::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
K线.borrow().inner.clone(),
,
,
@@ -286,7 +285,7 @@ impl 买卖点Py {
) -> Py {
Py {
inner: chanlun::business::bsp::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
K线.borrow().inner.clone(),
,
,
@@ -307,7 +306,7 @@ impl 买卖点Py {
) -> Py {
Py {
inner: chanlun::business::bsp::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
K线.borrow().inner.clone(),
,
,
@@ -328,7 +327,7 @@ impl 买卖点Py {
) -> Py {
Py {
inner: chanlun::business::bsp::::(
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
K线.borrow().inner.clone(),
,
,
@@ -352,8 +351,8 @@ impl 买卖点Py {
,
,
,
Rc::clone(&.borrow().inner),
Rc::clone(&K.borrow().inner),
Arc::clone(&.borrow().inner),
Arc::clone(&K.borrow().inner),
),
}
}
@@ -390,24 +389,30 @@ impl 买卖点Py {
/// 调试方法:
/// 测试_保存数据(root?) — 将各层级序列保存到文件,用于与Python版对比
/// 读取数据文件(文件路径, 配置) -> 观察者 (classmethod) — 从 .nb 文件加载数据
#[pyclass(name = "观察者", module = "chanlun._chanlun", subclass, unsendable)]
#[pyclass(name = "观察者", module = "chanlun._chanlun", subclass)]
pub struct Py {
pub(crate) inner: Option<Rc<RefCell<chanlun::business::observer::>>>,
pub(crate) inner: Option<Arc<RwLock<chanlun::business::observer::>>>,
}
impl Py {
pub(crate) fn obs(&self) -> std::cell::Ref<'_, chanlun::business::observer::> {
pub(crate) fn obs(
&self,
) -> std::sync::RwLockReadGuard<'_, chanlun::business::observer::> {
self.inner
.as_ref()
.expect("观察者 尚未初始化,请通过 __init__(符号, 周期, 配置) 构造")
.borrow()
.read()
.unwrap_or_else(|e| e.into_inner())
}
pub(crate) fn obs_mut(&self) -> std::cell::RefMut<'_, chanlun::business::observer::> {
pub(crate) fn obs_mut(
&self,
) -> std::sync::RwLockWriteGuard<'_, chanlun::business::observer::> {
self.inner
.as_ref()
.expect("观察者 尚未初始化,请通过 __init__(符号, 周期, 配置) 构造")
.borrow_mut()
.write()
.unwrap_or_else(|e| e.into_inner())
}
}
@@ -492,18 +497,16 @@ impl 观察者Py {
#[getter]
/// :return: 最后一根原始K线
fn K线(&self) -> Option<K线Py> {
self.obs().K线().map(|k| K线Py {
inner: Rc::clone(k),
})
fn K线(&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.obs().K线().map(|k| bar_to_py(py, Arc::clone(k)))
}
#[getter]
/// :return: 最后一根缠论K线
fn K(&self) -> Option<K线Py> {
fn K(&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.obs()
.K()
.map(|k| K线Py::from_rc(Rc::clone(k)))
.map(|k| crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))
}
#[getter]
@@ -551,7 +554,7 @@ impl 观察者Py {
let k线_py = Py::new(
py,
K线Py {
inner: Rc::new(k线),
inner: Arc::new(k线),
},
)?;
slf.call_method1("增加原始K线", (k线_py,))?;
@@ -564,6 +567,7 @@ impl 观察者Py {
self.obs_mut().();
}
#[pyo3(signature = (root = None))]
/// 拆分各序列数据,单独存文件,文件名为对应变量名
fn _保存数据(&self, root: Option<&str>) {
self.obs()._保存数据(root);
@@ -618,7 +622,7 @@ impl 观察者Py {
let k线_py = Py::new(
py,
K线Py {
inner: Rc::new(k线),
inner: Arc::new(k线),
},
)?;
obj.call_method1("增加原始K线", (k线_py,))?;
@@ -634,18 +638,38 @@ impl 观察者Py {
fn K线序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for k in &self.obs().K线序列 {
list.append(K线Py {
inner: Rc::clone(k),
})?;
list.append(bar_to_py(py, Arc::clone(k)))?;
}
Ok(list.into())
}
#[getter]
fn K序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for k in &self.obs().K序列 {
list.append(crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))?;
}
Ok(list.into())
}
#[setter]
#[pyo3(name = "基础缠K序列")]
fn _基础缠K序列(&mut self, value: &Bound<'_, PyAny>) -> PyResult<()> {
let list: &Bound<'_, pyo3::types::PyList> = value.cast()?;
let mut vec = Vec::new();
for item in list {
let k: PyRef<'_, K线Py> = item.extract()?;
vec.push(Arc::clone(&k.inner));
}
self.obs_mut().K序列 = vec;
Ok(())
}
#[getter]
fn K线序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for k in &self.obs().K线序列 {
list.append(K线Py::from_rc(Rc::clone(k)))?;
list.append(crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))?;
}
Ok(list.into())
}
@@ -654,9 +678,7 @@ impl 观察者Py {
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for f in &self.obs(). {
list.append(Py {
inner: Rc::clone(f),
})?;
list.append(fractal_to_py(py, Arc::clone(f)))?;
}
Ok(list.into())
}
@@ -665,9 +687,7 @@ impl 观察者Py {
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.obs(). {
list.append(线Py {
inner: Rc::clone(d),
})?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -676,9 +696,7 @@ impl 观察者Py {
fn _中枢序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.obs()._中枢序列 {
list.append(Py {
inner: Rc::clone(h),
})?;
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -687,9 +705,7 @@ impl 观察者Py {
fn 线(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.obs().线 {
list.append(线Py {
inner: Rc::clone(d),
})?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -698,9 +714,7 @@ impl 观察者Py {
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.obs(). {
list.append(Py {
inner: Rc::clone(h),
})?;
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -709,9 +723,7 @@ impl 观察者Py {
fn 线(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.obs().线 {
list.append(线Py {
inner: Rc::clone(d),
})?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -720,9 +732,7 @@ impl 观察者Py {
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.obs(). {
list.append(Py {
inner: Rc::clone(h),
})?;
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -731,9 +741,7 @@ impl 观察者Py {
fn 线_线段(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.obs().线_线段 {
list.append(线Py {
inner: Rc::clone(d),
})?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -742,9 +750,7 @@ impl 观察者Py {
fn _线段(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.obs()._线段 {
list.append(Py {
inner: Rc::clone(h),
})?;
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -753,9 +759,7 @@ impl 观察者Py {
fn 线_线段序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.obs().线_线段序列 {
list.append(线Py {
inner: Rc::clone(d),
})?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -764,9 +768,7 @@ impl 观察者Py {
fn 线_中枢序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.obs().线_中枢序列 {
list.append(Py {
inner: Rc::clone(h),
})?;
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -775,9 +777,7 @@ impl 观察者Py {
fn 线_扩展线段(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.obs().线_扩展线段 {
list.append(线Py {
inner: Rc::clone(d),
})?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -786,9 +786,7 @@ impl 观察者Py {
fn _扩展线段(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.obs()._扩展线段 {
list.append(Py {
inner: Rc::clone(h),
})?;
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -806,7 +804,7 @@ impl 观察者Py {
/// 投喂(时间戳, 开盘价, 最高价, 最低价, 收盘价, 成交量) -> list[(周期, K线)]
/// — 快捷入口,免去构造K线对象
/// 获取当前K线(周期) -> K线|None — 获取指定周期的当前合成结果
#[pyclass(name = "K线合成器", module = "chanlun._chanlun", unsendable)]
#[pyclass(name = "K线合成器", module = "chanlun._chanlun")]
pub struct K线合成器Py {
pub(crate) inner: chanlun::business::synthesizer::K线合成器,
}
@@ -829,7 +827,7 @@ impl K线合成器Py {
let results = self.inner.K线((*K.borrow().inner).clone());
Ok(results
.into_iter()
.map(|(, k)| (, K线Py { inner: Rc::new(k) }))
.map(|(, k)| (, K线Py { inner: Arc::new(k) }))
.collect())
}
@@ -858,14 +856,21 @@ impl K线合成器Py {
let results = self.inner.K线(k);
results
.into_iter()
.map(|(, k2)| (, K线Py { inner: Rc::new(k2) }))
.map(|(, k2)| {
(
,
K线Py {
inner: Arc::new(k2),
},
)
})
.collect()
}
/// 获取指定周期当前正在合成的K线
fn K线(&self, : i64) -> Option<K线Py> {
self.inner.K线().map(|k| K线Py {
inner: Rc::new(k.clone()),
inner: Arc::new(k.clone()),
})
}
@@ -893,7 +898,7 @@ impl K线合成器Py {
/// 方法:
/// 投喂K线(普K) — 喂入最小周期K线,自动合成大周期并分发给各周期观察者
/// 测试_保存数据(root?) — 保存各周期的分析数据到文件
#[pyclass(name = "立体分析器", module = "chanlun._chanlun", unsendable)]
#[pyclass(name = "立体分析器", module = "chanlun._chanlun")]
pub struct Py {
pub(crate) inner: chanlun::business::multi_frame::,
}
+98 -9
View File
@@ -183,20 +183,16 @@ impl 缠论配置Py {
}
/// 保存配置到 JSON 文件(默认路径 "缠论配置.json")。
fn (&self, py: Python<'_>, path: Option<&str>) -> PyResult<()> {
let path = path.unwrap_or("缠论配置.json");
#[pyo3(signature = (path = "缠论配置.json"))]
fn (&self, py: Python<'_>, path: &str) -> PyResult<()> {
let json = self.to_json(py)?;
std::fs::write(path, json).map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))
}
/// 从 JSON 文件加载配置(默认路径 "缠论配置.json")。
#[classmethod]
fn (
_cls: &Bound<'_, PyType>,
py: Python<'_>,
path: Option<&str>,
) -> PyResult<Self> {
let path = path.unwrap_or("缠论配置.json");
#[pyo3(signature = (path = "缠论配置.json"))]
fn (_cls: &Bound<'_, PyType>, py: Python<'_>, path: &str) -> PyResult<Self> {
let json_str = std::fs::read_to_string(path)
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;
Self::from_json_str(py, &json_str)
@@ -336,11 +332,104 @@ fn dict_to_rust_config(
let mut value: serde_json::Value = serde_json::from_str(&json_str)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("配置转换失败: {e}")))?;
coerce_strings_to_numbers(&mut value);
serde_json::from_value(value)
// 获取默认配置的 JSON 表示作为 schema
let default_config = chanlun::config::::default();
let default_json = serde_json::to_value(&default_config)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("配置转换失败: {e}")))?;
// 用默认值做基准,只合并类型匹配的字段
let mut merged = default_json.clone();
if let serde_json::Value::Object(ref input_map) = value {
if let serde_json::Value::Object(ref default_map) = default_json {
for (key, input_val) in input_map {
if let Some(default_val) = default_map.get(key) {
match validate_field(key, input_val, default_val) {
Ok(()) => {
merged[key] = input_val.clone();
}
Err(msg) => {
eprintln!("\x1b[33m[配置警告]\x1b[m {key}: {msg},已使用默认值 {default_val}");
}
}
}
}
}
}
serde_json::from_value(merged)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("配置转换失败: {e}")))
})
}
/// 字符串字段的有效值集合
fn valid_string_values(field: &str) -> Option<&'static [&'static str]> {
match field {
"指标计算方式" => Some(&[
"",
"",
"",
"",
"高低均值",
"高低收均值",
"开高低收均值",
]),
"买卖点_指标模式" => Some(&["任意", "配置", "全量", "相对"]),
"线段内部背驰_模式" => Some(&["任意", "配置", "全量", "相对"]),
_ => None,
}
}
/// 验证单个字段的值是否与默认值类型兼容
fn validate_field(
key: &str,
input: &serde_json::Value,
default: &serde_json::Value,
) -> Result<(), String> {
use serde_json::Value;
// 输入为 null → 跳过(保留默认)
if input.is_null() {
return Err("值为 null".into());
}
// 字符串字段:检查有效值白名单
if default.is_string() && input.is_string() {
if let Some(valid) = valid_string_values(key) {
let s = input.as_str().unwrap();
if !valid.contains(&s) {
return Err(format!("\"{s}\" 不在有效值 {valid:?}"));
}
}
return Ok(());
}
// 类型匹配:直接通过
match (default, input) {
(Value::Bool(_), Value::Bool(_)) => return Ok(()),
(Value::Number(_), Value::Number(_)) => return Ok(()),
(Value::String(_), Value::String(_)) => return Ok(()),
_ => {}
}
// 类型不匹配
let type_name = match input {
Value::Bool(_) => "布尔",
Value::Number(_) => "数值",
Value::String(_) => "字符串",
Value::Array(_) => "数组",
Value::Object(_) => "字典",
Value::Null => "null",
};
let expected = match default {
Value::Bool(_) => "布尔",
Value::Number(_) => "数值",
Value::String(_) => "字符串",
_ => "其他",
};
Err(format!("类型不匹配(需要 {expected},收到 {type_name}"))
}
/// 递归遍历 JSON Value,将数字/布尔字符串转为对应类型。
fn coerce_strings_to_numbers(value: &mut serde_json::Value) {
match value {
+104 -94
View File
@@ -25,7 +25,9 @@
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyDict, PyType};
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::RwLock;
use crate::config_py::Py;
use crate::indicators_py::{线Py, Py, Py};
@@ -52,9 +54,9 @@ use crate::types_py::相对方向Py;
/// 获取MACD(K线序列, 计算方式, 快线周期?, 慢线周期?, 信号周期?) -> list[平滑异同移动平均线]
/// — 对整个K线序列批量计算 MACD
/// 截取(序列, 起点K线, 终点K线) -> list — 按时间戳截取K线区间
#[pyclass(name = "K线", module = "chanlun._chanlun", unsendable)]
#[pyclass(name = "K线", module = "chanlun._chanlun")]
pub struct K线Py {
pub(crate) inner: Rc<chanlun::kline::bar::K线>,
pub(crate) inner: Arc<chanlun::kline::bar::K线>,
}
#[pymethods]
@@ -73,7 +75,7 @@ impl K线Py {
: f64,
) -> Self {
Self {
inner: Rc::new(chanlun::kline::bar::K线 {
inner: Arc::new(chanlun::kline::bar::K线 {
: .to_string(),
,
,
@@ -94,82 +96,46 @@ impl K线Py {
fn (&self) -> String {
self.inner..clone()
}
#[setter]
fn set_标识(&mut self, v: String) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> i64 {
self.inner.
}
#[setter]
fn set_序号(&mut self, v: i64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> i64 {
self.inner.
}
#[setter]
fn set_周期(&mut self, v: i64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> i64 {
self.inner.
}
#[setter]
fn set_时间戳(&mut self, v: i64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[setter]
fn set_高(&mut self, v: f64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[setter]
fn set_低(&mut self, v: f64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[setter]
fn set_开盘价(&mut self, v: f64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[setter]
fn set_收盘价(&mut self, v: f64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
fn (&self) -> f64 {
self.inner.
}
#[setter]
fn set_成交量(&mut self, v: f64) {
Rc::make_mut(&mut self.inner). = v;
}
#[getter]
/// :return: 相对方向.向上(开盘<收盘)或 相对方向.向下(开盘>收盘)
@@ -243,13 +209,13 @@ impl K线Py {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
#[classmethod]
@@ -268,7 +234,7 @@ impl K线Py {
: Option<i64>,
) -> Self {
Self {
inner: Rc::new(chanlun::kline::bar::K线::K(
inner: Arc::new(chanlun::kline::bar::K线::K(
,
,
,
@@ -306,7 +272,7 @@ impl K线Py {
) -> Option<Self> {
chanlun::kline::bar::K线::(.as_bytes(), , ).map(|inner| {
Self {
inner: Rc::new(inner),
inner: Arc::new(inner),
}
})
}
@@ -333,21 +299,21 @@ impl K线Py {
: &Bound<'_, Self>,
py: Python<'_>,
) -> PyResult<Vec<Py<Self>>> {
let start_ptr = Rc::as_ptr(&.borrow().inner);
let end_ptr = Rc::as_ptr(&.borrow().inner);
let start_ptr = Arc::as_ptr(&.borrow().inner);
let end_ptr = Arc::as_ptr(&.borrow().inner);
let start_ts = .borrow().inner.;
let end_ts = .borrow().inner.;
let start_idx =
.iter()
.position(|k| {
Rc::as_ptr(&k.borrow(py).inner) == start_ptr
Arc::as_ptr(&k.borrow(py).inner) == start_ptr
|| k.borrow(py).inner. == start_ts
})
.ok_or_else(|| pyo3::exceptions::PyValueError::new_err("始 不在序列中"))?;
let end_idx =
.iter()
.position(|k| {
Rc::as_ptr(&k.borrow(py).inner) == end_ptr || k.borrow(py).inner. == end_ts
Arc::as_ptr(&k.borrow(py).inner) == end_ptr || k.borrow(py).inner. == end_ts
})
.ok_or_else(|| pyo3::exceptions::PyValueError::new_err("终 不在序列中"))?;
if start_idx > end_idx {
@@ -380,31 +346,71 @@ impl K线Py {
/// 分析(缠K序列, 配置, 可以逆序包含?, 忽视顺序包含?, 可以逆序包含新?) -> (str, 分型|None)
/// — 分析分型形成结果
/// 截取(序列, 起点分型, 终点分型) -> list — 截取分型间的缠K子序列
#[pyclass(
name = "缠论K线",
module = "chanlun._chanlun",
unsendable,
from_py_object
)]
#[pyclass(name = "缠论K线", module = "chanlun._chanlun", from_py_object)]
pub struct K线Py {
pub(crate) inner: std::rc::Rc<chanlun::kline::chan_kline::K线>,
bsp_set: std::cell::RefCell<Option<Py<pyo3::types::PySet>>>,
pub(crate) inner: std::sync::Arc<chanlun::kline::chan_kline::K线>,
bsp_set: std::sync::RwLock<Option<Py<pyo3::types::PySet>>>,
}
impl K线Py {
pub(crate) fn from_rc(inner: std::rc::Rc<chanlun::kline::chan_kline::K线>) -> Self {
pub(crate) fn from_rc(inner: std::sync::Arc<chanlun::kline::chan_kline::K线>) -> Self {
Self {
inner,
bsp_set: std::cell::RefCell::new(None),
bsp_set: std::sync::RwLock::new(None),
}
}
}
thread_local! {
/// 对象标识缓存:Rc 地址 → 规范 Python 对象
/// 确保同一底层 Rc 指针在 Python 侧始终映射到同一 PyObject
static BAR_IDENTITY: RwLock<HashMap<usize, Py<K线Py>>> = RwLock::new(HashMap::new());
static KLINE_IDENTITY: RwLock<HashMap<usize, Py<K线Py>>> = RwLock::new(HashMap::new());
}
/// 将 Rc<K线> 转为 Py<K线Py>,确保同一 Rc 地址总是返回同一 Python 对象
pub(crate) fn bar_to_py(
py: Python<'_>,
inner: std::sync::Arc<chanlun::kline::bar::K线>,
) -> Py<K线Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
BAR_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
let obj = Py::new(py, K线Py { inner }).unwrap();
BAR_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
/// 将 Rc<缠论K线> 转为 Py<缠论K线Py>,确保同一 Rc 地址总是返回同一 Python 对象
pub(crate) fn chan_kline_to_py(
py: Python<'_>,
inner: std::sync::Arc<chanlun::kline::chan_kline::K线>,
) -> Py<K线Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
KLINE_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
let obj = Py::new(py, K线Py::from_rc(inner)).unwrap();
KLINE_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
impl Clone for K线Py {
fn clone(&self) -> Self {
Self {
inner: std::rc::Rc::clone(&self.inner),
bsp_set: std::cell::RefCell::new(None),
inner: std::sync::Arc::clone(&self.inner),
bsp_set: std::sync::RwLock::new(None),
}
}
}
@@ -418,28 +424,28 @@ impl 缠论K线Py {
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self) -> f64 {
self.inner.
self.inner..get()
}
#[getter]
fn (&self) -> f64 {
self.inner.
self.inner..get()
}
#[getter]
fn (&self) -> Py {
Py {
inner: self.inner.,
inner: *self.inner..read().unwrap(),
}
}
@@ -447,6 +453,8 @@ impl 缠论K线Py {
fn (&self) -> Option<crate::types_py::Py> {
self.inner
.
.read()
.unwrap()
.map(|f| crate::types_py::Py { inner: f })
}
@@ -462,7 +470,7 @@ impl 缠论K线Py {
#[getter]
fn (&self) -> f64 {
self.inner.
self.inner..get()
}
#[getter]
@@ -472,14 +480,12 @@ impl 缠论K线Py {
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn K线(&self) -> K线Py {
K线Py {
inner: self.inner.K线.clone(),
}
fn K线(&self, py: Python<'_>) -> Py<K线Py> {
bar_to_py(py, self.inner.K线.read().unwrap().clone())
}
/// pandas 兼容 — 返回所有字段构成的字典
@@ -516,28 +522,28 @@ impl 缠论K线Py {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
#[getter]
/// 创建当前缠K的浅拷贝副本
fn (&self, py: Python<'_>) -> Self {
let mut mirror = Self {
inner: std::rc::Rc::new(self.inner.()),
bsp_set: std::cell::RefCell::new(None),
inner: std::sync::Arc::new(self.inner.()),
bsp_set: std::sync::RwLock::new(None),
};
if let Some(ref src_set) = *self.bsp_set.borrow() {
if let Some(ref src_set) = *self.bsp_set.read().unwrap() {
if let Ok(new_set) = pyo3::types::PySet::empty(py) {
for item in src_set.bind(py).iter() {
let _ = new_set.add(item);
}
mirror.bsp_set = std::cell::RefCell::new(Some(new_set.into()));
mirror.bsp_set = std::sync::RwLock::new(Some(new_set.into()));
}
}
mirror
@@ -563,16 +569,17 @@ impl 缠论K线Py {
#[getter]
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
if self.bsp_set.borrow().is_none() {
if self.bsp_set.read().unwrap().is_none() {
let set = pyo3::types::PySet::empty(py)?;
for s in self.inner..borrow().iter() {
for s in self.inner..read().unwrap().iter() {
set.add(s.clone())?;
}
*self.bsp_set.borrow_mut() = Some(set.into());
*self.bsp_set.write().unwrap() = Some(set.into());
}
Ok(self
.bsp_set
.borrow()
.read()
.unwrap()
.as_ref()
.unwrap()
.clone_ref(py)
@@ -589,12 +596,13 @@ impl 缠论K线Py {
) -> i64 {
let rc_list: Vec<_> = 线
.iter()
.map(|k| std::rc::Rc::clone(&k.bind(py).borrow().inner))
.map(|k| std::sync::Arc::clone(&k.bind(py).borrow().inner))
.collect();
chanlun::kline::chan_kline::K线::(&rc_list, &k线.borrow().inner)
}
#[classmethod]
#[pyo3(signature = (时间戳, 高, 低, 方向, 结构, 原始序号, 普k, 之前 = None))]
/// 创建新的缠论K线
fn K(
_cls: &Bound<'_, PyType>,
@@ -606,7 +614,8 @@ impl 缠论K线Py {
: i64,
k: &Bound<'_, K线Py>,
: Option<&Bound<'_, Self>>,
) -> Self {
py: Python<'_>,
) -> Py<Self> {
let prev_ref = .map(|prev| prev.borrow());
let prev_inner = prev_ref.as_ref().map(|r| r.inner.as_ref());
let inner = chanlun::kline::chan_kline::K线::K(
@@ -619,7 +628,7 @@ impl 缠论K线Py {
k.borrow().inner.clone(),
prev_inner,
);
Self::from_rc(std::rc::Rc::new(inner))
chan_kline_to_py(py, std::sync::Arc::new(inner))
}
#[classmethod]
@@ -631,7 +640,7 @@ impl 缠论K线Py {
K: &Bound<'_, K线Py>,
: &Bound<'_, Py>,
py: Python<'_>,
) -> PyResult<(Option<Self>, Option<String>)> {
) -> PyResult<(Option<Py<Self>>, Option<String>)> {
let mut ck_inner = (*K.borrow().inner).clone();
let config = .borrow().to_rust_config(py)?;
let prev_ref = K.map(|prev| prev.borrow());
@@ -642,7 +651,7 @@ impl 缠论K线Py {
&K.borrow().inner,
&config,
);
Ok((result.map(Self::from_rc), mode))
Ok((result.map(|rc| chan_kline_to_py(py, rc)), mode))
}
#[classmethod]
@@ -660,7 +669,7 @@ impl 缠论K线Py {
let mut ck_seq: Vec<_> = K序列
.iter()
.map(|k| std::rc::Rc::clone(&k.bind(py).borrow().inner))
.map(|k| std::sync::Arc::clone(&k.bind(py).borrow().inner))
.collect();
let mut bar_seq: Vec<_> = K序列
.iter()
@@ -685,21 +694,22 @@ impl 缠论K线Py {
: &Bound<'_, Self>,
py: Python<'_>,
) -> PyResult<Vec<Py<Self>>> {
let start_ptr = Rc::as_ptr(&.borrow().inner);
let end_ptr = Rc::as_ptr(&.borrow().inner);
let start_ts = .borrow().inner.;
let end_ts = .borrow().inner.;
let start_ptr = Arc::as_ptr(&.borrow().inner);
let end_ptr = Arc::as_ptr(&.borrow().inner);
let start_ts = .borrow().inner..load(Ordering::Relaxed);
let end_ts = .borrow().inner..load(Ordering::Relaxed);
let start_idx =
.iter()
.position(|k| {
Rc::as_ptr(&k.borrow(py).inner) == start_ptr
|| k.borrow(py).inner. == start_ts
Arc::as_ptr(&k.borrow(py).inner) == start_ptr
|| k.borrow(py).inner..load(Ordering::Relaxed) == start_ts
})
.ok_or_else(|| pyo3::exceptions::PyValueError::new_err("始 不在序列中"))?;
let end_idx =
.iter()
.position(|k| {
Rc::as_ptr(&k.borrow(py).inner) == end_ptr || k.borrow(py).inner. == end_ts
Arc::as_ptr(&k.borrow(py).inner) == end_ptr
|| k.borrow(py).inner..load(Ordering::Relaxed) == end_ts
})
.ok_or_else(|| pyo3::exceptions::PyValueError::new_err("终 不在序列中"))?;
if start_idx > end_idx {
+233 -185
View File
@@ -25,11 +25,103 @@
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyType};
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::RwLock;
use crate::algorithm_py::Py;
use crate::algorithm_py::{hub_to_py, Py};
use crate::config_py::Py;
use crate::kline_py::{K线Py, K线Py};
// ---- 身份缓存 (弱引用:通过 refcnt 检测存活,仅缓存持有则视为过期) ----
thread_local! {
static FRACTAL_IDENTITY: RwLock<HashMap<usize, Py<Py>>> = RwLock::new(HashMap::new());
static DASHED_IDENTITY: RwLock<HashMap<usize, Py<线Py>>> = RwLock::new(HashMap::new());
static SEGFEAT_IDENTITY: RwLock<HashMap<usize, Py<线Py>>> = RwLock::new(HashMap::new());
static FEATFRAC_IDENTITY: RwLock<HashMap<usize, Py<Py>>> = RwLock::new(HashMap::new());
}
pub(crate) fn fractal_to_py(
py: Python<'_>,
inner: Arc<chanlun::structure::fractal_obj::>,
) -> Py<Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
FRACTAL_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
// 清理 refcnt==1 的过期条目(仅缓存持有,Python 侧已无引用)
FRACTAL_IDENTITY.with(|c| {
c.write().unwrap().retain(|_, v| v.get_refcnt(py) > 1);
});
let obj = Py::new(py, Py { inner }).unwrap();
FRACTAL_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
pub(crate) fn dashed_to_py(
py: Python<'_>,
inner: Arc<chanlun::structure::dash_line::线>,
) -> Py<线Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
DASHED_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
DASHED_IDENTITY.with(|c| {
c.write().unwrap().retain(|_, v| v.get_refcnt(py) > 1);
});
let obj = Py::new(py, 线Py { inner }).unwrap();
DASHED_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
pub(crate) fn segfeat_to_py(
py: Python<'_>,
inner: Arc<chanlun::structure::segment_feat::线>,
) -> Py<线Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
SEGFEAT_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
SEGFEAT_IDENTITY.with(|c| {
c.write().unwrap().retain(|_, v| v.get_refcnt(py) > 1);
});
let obj = Py::new(py, 线Py { inner }).unwrap();
SEGFEAT_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
pub(crate) fn featfrac_to_py(
py: Python<'_>,
inner: Arc<chanlun::structure::feat_fractal::>,
) -> Py<Py> {
let key = Arc::as_ptr(&inner) as usize;
if let Some(cached) =
FEATFRAC_IDENTITY.with(|c| c.read().unwrap().get(&key).map(|p| p.clone_ref(py)))
{
return cached;
}
FEATFRAC_IDENTITY.with(|c| {
c.write().unwrap().retain(|_, v| v.get_refcnt(py) > 1);
});
let obj = Py::new(py, Py { inner }).unwrap();
FEATFRAC_IDENTITY.with(|c| {
c.write().unwrap().insert(key, obj.clone_ref(py));
});
obj
}
use crate::types_py::{Py, Py, Py};
// ========== 分型 ==========
@@ -47,10 +139,10 @@ use crate::types_py::{分型结构Py, 相对方向Py, 缺口Py};
/// 判断分型(缠K序列, 索引, 配置) -> 分型|None — 在缠K序列中指定位置尝试创建分型
/// 从缠K序列中获取分型(缠K序列, 配置) -> list[分型] — 扫描全序列提取所有分型
/// 向序列中添加(分型序列, 新分型) — 维护分型序列的顺序一致性
#[pyclass(name = "分型", module = "chanlun._chanlun", unsendable, from_py_object)]
#[pyclass(name = "分型", module = "chanlun._chanlun", from_py_object)]
#[derive(Clone)]
pub struct Py {
pub(crate) inner: Rc<chanlun::structure::fractal_obj::>,
pub(crate) inner: Arc<chanlun::structure::fractal_obj::>,
}
#[pymethods]
@@ -62,33 +154,33 @@ impl 分型Py {
: Option<&Bound<'_, K线Py>>,
) -> Self {
Self {
inner: Rc::new(chanlun::structure::fractal_obj::::new(
.map(|k| Rc::clone(&k.borrow().inner)),
Rc::clone(&.borrow().inner),
.map(|k| Rc::clone(&k.borrow().inner)),
inner: Arc::new(chanlun::structure::fractal_obj::::new(
.map(|k| Arc::clone(&k.borrow().inner)),
Arc::clone(&.borrow().inner),
.map(|k| Arc::clone(&k.borrow().inner)),
)),
}
}
#[getter]
fn (&self) -> Option<K线Py> {
fn (&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.inner
.
.as_ref()
.map(|k| K线Py::from_rc(Rc::clone(k)))
.map(|k| crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))
}
#[getter]
fn (&self) -> K线Py {
K线Py::from_rc(Rc::clone(&self.inner.))
fn (&self, py: Python<'_>) -> Py<K线Py> {
crate::kline_py::chan_kline_to_py(py, Arc::clone(&self.inner.))
}
#[getter]
fn (&self) -> Option<K线Py> {
fn (&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.inner
.
.as_ref()
.map(|k| K线Py::from_rc(Rc::clone(k)))
.map(|k| crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))
}
#[getter]
@@ -118,13 +210,13 @@ impl 分型Py {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
#[getter]
@@ -160,11 +252,11 @@ impl 分型Py {
dict.set_item("分型特征值", self.())?;
dict.set_item("强度", self.())?;
dict.set_item("与MACD柱子分型匹配", self.MACD柱子分型匹配())?;
if let Some(v) = self.() {
if let Some(v) = self.(py) {
dict.set_item("", v)?;
}
dict.set_item("", self.())?;
if let Some(v) = self.() {
dict.set_item("", self.(py))?;
if let Some(v) = self.(py) {
dict.set_item("", v)?;
}
if let Some(v) = self.() {
@@ -196,16 +288,16 @@ impl 分型Py {
: &Bound<'_, K线Py>,
py: Python<'_>,
) -> Option<Self> {
let ck_seq: Vec<Rc<chanlun::kline::chan_kline::K线>> = K线序列
let ck_seq: Vec<Arc<chanlun::kline::chan_kline::K线>> = K线序列
.iter()
.map(|k| Rc::clone(&k.bind(py).borrow().inner))
.map(|k| Arc::clone(&k.bind(py).borrow().inner))
.collect();
chanlun::structure::fractal_obj::::K序列中获取分型(
&ck_seq,
&.borrow().inner,
)
.map(|inner| Self {
inner: Rc::new(inner),
inner: Arc::new(inner),
})
}
@@ -215,13 +307,7 @@ impl 分型Py {
: &Bound<'_, PyAny>, : &Bound<'_, Self>
) -> PyResult<()> {
let py = .py();
let inner = Rc::clone(&.borrow().inner);
let wrapper = Py::new(
py,
Self {
inner: Rc::clone(&inner),
},
)?;
let wrapper = fractal_to_py(py, Arc::clone(&.borrow().inner));
.call_method1("append", (wrapper,))?;
Ok(())
}
@@ -257,10 +343,10 @@ impl 分型Py {
/// 计算MACD柱子均值 / 武之全量MACD均值 / 武之MACD均值 / 武之MACD极值
/// 计算K线序列MACD趋向背驰 / 买卖意义 / 计算MACD柱子分段
/// 密集区域按间隔 / 统计MACD行为
#[pyclass(name = "虚线", module = "chanlun._chanlun", unsendable, from_py_object)]
#[pyclass(name = "虚线", module = "chanlun._chanlun", from_py_object)]
#[derive(Clone)]
pub struct 线Py {
pub(crate) inner: Rc<chanlun::structure::dash_line::线>,
pub(crate) inner: Arc<chanlun::structure::dash_line::线>,
}
#[pymethods]
@@ -276,11 +362,11 @@ impl 虚线Py {
: bool,
) -> Self {
Self {
inner: Rc::new(chanlun::structure::dash_line::线::new(
inner: Arc::new(chanlun::structure::dash_line::线::new(
,
,
Rc::clone(&.borrow().inner),
Rc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
,
,
)),
@@ -291,59 +377,50 @@ impl 虚线Py {
#[getter]
fn (&self) -> String {
self.inner..clone()
self.inner..read().unwrap().clone()
}
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self) -> i64 {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self) -> Py {
Py {
inner: Rc::clone(&self.inner.),
}
fn (&self, py: Python<'_>) -> Py<Py> {
fractal_to_py(py, Arc::clone(&self.inner.))
}
#[getter]
fn (&self) -> Py {
Py {
inner: Rc::clone(&self.inner.),
}
fn (&self, py: Python<'_>) -> Py<Py> {
fractal_to_py(py, Arc::clone(&*self.inner..read().unwrap()))
}
#[getter]
fn (&self) -> bool {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn (&self) -> String {
self.inner..clone()
self.inner..read().unwrap().clone()
}
#[getter]
fn _特征序列_显示(&self) -> bool {
self.inner._特征序列_显示
self.inner._特征序列_显示.load(Ordering::Relaxed)
}
#[getter]
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for item in &self.inner. {
for item in self.inner..read().unwrap().iter() {
match item {
Some(feat) => list.append(Py::new(
py,
线Py {
inner: Rc::clone(feat),
},
)?)?,
Some(feat) => list.append(segfeat_to_py(py, Arc::clone(feat)))?,
None => {
list.append(py.None())?;
}
@@ -354,27 +431,38 @@ impl 虚线Py {
#[getter]
fn (&self) -> bool {
self.inner.
self.inner..load(Ordering::Relaxed)
}
#[getter]
fn K线(&self) -> Option<K线Py> {
fn K线(&self, py: Python<'_>) -> Option<Py<K线Py>> {
self.inner
.K线
.read()
.unwrap()
.as_ref()
.map(|k| K线Py::from_rc(Rc::clone(k)))
.map(|k| crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))
}
#[getter]
fn (&self) -> Option<Py> {
self.inner..map(|q| Py { inner: q })
self.inner
.
.read()
.unwrap()
.map(|q| Py { inner: q })
}
#[getter]
fn (&self) -> Option<Self> {
self.inner..as_ref().map(|d| Self {
inner: Rc::clone(d),
})
self.inner
.
.read()
.unwrap()
.as_ref()
.map(|d| Self {
inner: Arc::clone(d),
})
}
// ---- 序列 getters ----
@@ -382,11 +470,11 @@ impl 虚线Py {
#[getter]
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.inner. {
for d in self.inner..read().unwrap().iter() {
list.append(Py::new(
py,
Self {
inner: Rc::clone(d),
inner: Arc::clone(d),
},
)?)?;
}
@@ -396,10 +484,8 @@ impl 虚线Py {
#[getter]
fn _中枢序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.inner._中枢序列 {
list.append(Py {
inner: Rc::clone(h),
})?;
for h in self.inner._中枢序列.read().unwrap().iter() {
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -407,10 +493,8 @@ impl 虚线Py {
#[getter]
fn _中枢序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.inner._中枢序列 {
list.append(Py {
inner: Rc::clone(h),
})?;
for h in self.inner._中枢序列.read().unwrap().iter() {
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -418,10 +502,8 @@ impl 虚线Py {
#[getter]
fn _中枢序列(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for h in &self.inner._中枢序列 {
list.append(Py {
inner: Rc::clone(h),
})?;
for h in self.inner._中枢序列.read().unwrap().iter() {
list.append(hub_to_py(py, Arc::clone(h)))?;
}
Ok(list.into())
}
@@ -432,11 +514,11 @@ impl 虚线Py {
/// 笔序列
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in self.inner.() {
for d in self.inner..read().unwrap().iter() {
list.append(Py::new(
py,
Self {
inner: Rc::clone(d),
inner: Arc::clone(d),
},
)?)?;
}
@@ -490,7 +572,7 @@ impl 虚线Py {
let list = pyo3::types::PyList::empty(.py());
for k in &result {
list.append(K线Py {
inner: Rc::clone(k),
inner: Arc::clone(k),
})?;
}
Ok(list.into())
@@ -502,21 +584,21 @@ impl 虚线Py {
: &Bound<'_, crate::business_py::Py>,
) -> PyResult<Py<PyAny>> {
let obs_ref = .borrow();
let py = .py();
let observer_inner = obs_ref.obs();
let result = self.inner.K序列(&observer_inner.K线序列);
let list = pyo3::types::PyList::empty(.py());
let list = pyo3::types::PyList::empty(py);
for k in &result {
list.append(K线Py::from_rc(Rc::clone(k)))?;
list.append(crate::kline_py::chan_kline_to_py(py, Arc::clone(k)))?;
}
Ok(list.into())
}
#[classmethod]
/// 递归获取虚线的终点分型(笔直接返回武,线段递归到底层笔的武)
fn _武(_cls: &Bound<'_, PyType>, 线: &Bound<'_, Self>) -> Py {
Py {
inner: 线.borrow().inner._武(),
}
fn _武(_cls: &Bound<'_, PyType>, 线: &Bound<'_, Self>) -> Py<Py> {
let py = _cls.py();
fractal_to_py(py, 线.borrow().inner._武())
}
/// 获取用于保存的数据文本
@@ -535,8 +617,8 @@ impl 虚线Py {
dict.set_item("模式", self.())?;
dict.set_item("短路修正", self.())?;
dict.set_item("图表标题", self.())?;
dict.set_item("文_时间戳", self.().())?;
dict.set_item("武_时间戳", self.().())?;
dict.set_item("文_时间戳", self.(py).bind(py).borrow().())?;
dict.set_item("武_时间戳", self.(py).bind(py).borrow().())?;
Ok(dict.into())
}
@@ -550,18 +632,19 @@ impl 虚线Py {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
// ---- 静态工厂方法 ----
#[classmethod]
#[pyo3(signature = (文, 武, 有效性 = true))]
/// :param 文: 起点分型
fn (
_cls: &Bound<'_, PyType>,
@@ -570,9 +653,9 @@ impl 虚线Py {
: bool,
) -> Self {
Self {
inner: Rc::new(chanlun::structure::dash_line::线::(
Rc::clone(&.borrow().inner),
Rc::clone(&.borrow().inner),
inner: Arc::new(chanlun::structure::dash_line::线::(
Arc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
,
)),
}
@@ -581,12 +664,12 @@ impl 虚线Py {
#[classmethod]
/// :param 虚线序列: 构成线段的虚线列表(笔)
fn 线(_cls: &Bound<'_, PyType>, 线: Vec<Py<Self>>, py: Python<'_>) -> Self {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
Self {
inner: Rc::new(chanlun::structure::dash_line::线::线(
inner: Arc::new(chanlun::structure::dash_line::线::线(
&rc_list,
)),
}
@@ -654,7 +737,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> f64 {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -672,7 +755,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -690,7 +773,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -705,7 +788,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -720,7 +803,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> Option<f64> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -738,7 +821,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> Option<f64> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -756,7 +839,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -771,7 +854,7 @@ impl 虚线Py {
线: &Bound<'_, Self>,
py: Python<'_>,
) -> bool {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -786,7 +869,7 @@ impl 虚线Py {
: &Bound<'_, Py>,
py: Python<'_>,
) -> [bool; 3] {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -803,7 +886,7 @@ impl 虚线Py {
k线序列: Vec<Py<K线Py>>,
py: Python<'_>,
) -> Vec<Vec<f64>> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = k线序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = k线序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -811,6 +894,7 @@ impl 虚线Py {
}
#[classmethod]
#[pyo3(signature = (交叉标记, 最大间隔 = 5usize, 最少交叉数 = 3usize))]
/// 交叉标记: 长度为len(macd_list)的列表,0=无交叉, 1=金叉, -1=死叉
fn (
_cls: &Bound<'_, PyType>,
@@ -826,6 +910,7 @@ impl 虚线Py {
}
#[classmethod]
#[pyo3(signature = (普K序列, 最大间隔 = 8usize, 最少交叉数 = 3usize))]
/// :param 普K序列: K线序列
fn MACD行为(
_cls: &Bound<'_, PyType>,
@@ -834,7 +919,7 @@ impl 虚线Py {
: usize,
py: Python<'_>,
) -> PyResult<Py<PyAny>> {
let rc_list: Vec<Rc<chanlun::kline::bar::K线>> = K序列
let rc_list: Vec<Arc<chanlun::kline::bar::K线>> = K序列
.iter()
.map(|k| k.bind(py).borrow().inner.clone())
.collect();
@@ -898,15 +983,10 @@ impl 虚线Py {
/// 新建(序号, 文, 武, 基础序列?) -> 线段特征
/// 静态分析(虚线序列, 配置) -> 线段特征|None
/// 获取分型序列(虚线序列, 配置) -> list[线段特征]
#[pyclass(
name = "线段特征",
module = "chanlun._chanlun",
unsendable,
from_py_object
)]
#[pyclass(name = "线段特征", module = "chanlun._chanlun", from_py_object)]
#[derive(Clone)]
pub struct 线Py {
pub(crate) inner: Rc<chanlun::structure::segment_feat::线>,
pub(crate) inner: Arc<chanlun::structure::segment_feat::线>,
}
#[pymethods]
@@ -918,12 +998,12 @@ impl 线段特征Py {
线: &Bound<'_, Py>,
py: Python<'_>,
) -> Self {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> =
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> =
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
Self {
inner: Rc::new(chanlun::structure::segment_feat::线::new(
inner: Arc::new(chanlun::structure::segment_feat::线::new(
,
rc_list,
线.borrow().inner,
@@ -954,12 +1034,7 @@ impl 线段特征Py {
fn (&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.inner. {
list.append(Py::new(
py,
线Py {
inner: Rc::clone(d),
},
)?)?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
Ok(list.into())
}
@@ -985,38 +1060,27 @@ impl 线段特征Py {
)));
}
let dash = &self.inner.[idx as usize];
let obj: Py<PyAny> = Py::new(
py,
线Py {
inner: Rc::clone(dash),
},
)?
.into();
let obj: Py<PyAny> = dashed_to_py(py, Arc::clone(dash)).into();
Ok(obj)
}
fn __iter__(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
let list = pyo3::types::PyList::empty(py);
for d in &self.inner. {
list.append(Py::new(
py,
线Py {
inner: Rc::clone(d),
},
)?)?;
list.append(dashed_to_py(py, Arc::clone(d)))?;
}
list.call_method0("__iter__").map(|iter| iter.into())
}
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
/// pandas 兼容 — 返回关键标量字段构成的字典
@@ -1040,18 +1104,14 @@ impl 线段特征Py {
#[getter]
/// 起点分型(向上线段取高高中的最大者,向下线段取低低中的最小者)
fn (&self) -> Py {
Py {
inner: self.inner.(),
}
fn (&self, py: Python<'_>) -> Py<Py> {
fractal_to_py(py, self.inner.())
}
#[getter]
/// 终点分型(向上线段取高高中的最大者,向下线段取低低中的最小者)
fn (&self) -> Py {
Py {
inner: self.inner.(),
}
fn (&self, py: Python<'_>) -> Py<Py> {
fractal_to_py(py, self.inner.())
}
#[getter]
@@ -1076,17 +1136,17 @@ impl 线段特征Py {
/// :param 待添加虚线: 待添加的虚线
fn (&mut self, 线: &Bound<'_, 线Py>) -> PyResult<()> {
let inner = Rc::make_mut(&mut self.inner);
let inner = Arc::make_mut(&mut self.inner);
inner
.(Rc::clone(&线.borrow().inner))
.(Arc::clone(&线.borrow().inner))
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e))
}
/// :param 待删除虚线: 待删除的虚线
fn (&mut self, 线: &Bound<'_, 线Py>) -> PyResult<()> {
let inner = Rc::make_mut(&mut self.inner);
let inner = Arc::make_mut(&mut self.inner);
inner
.(&Rc::clone(&线.borrow().inner))
.(&Arc::clone(&线.borrow().inner))
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e))
}
@@ -1100,12 +1160,12 @@ impl 线段特征Py {
线: &Bound<'_, Py>,
py: Python<'_>,
) -> Self {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
Self {
inner: Rc::new(chanlun::structure::segment_feat::线::(
inner: Arc::new(chanlun::structure::segment_feat::线::(
rc_list,
线.borrow().inner,
)),
@@ -1113,6 +1173,7 @@ impl 线段特征Py {
}
#[classmethod]
#[pyo3(signature = (虚线序列, 线段方向, 四象, 是否忽视 = false))]
/// 静态分析虚线序列,生成特征序列
fn (
_cls: &Bound<'_, PyType>,
@@ -1122,9 +1183,9 @@ impl 线段特征Py {
: bool,
py: Python<'_>,
) -> Vec<Self> {
let rc_list: Vec<Rc<chanlun::structure::dash_line::线>> = 线
let rc_list: Vec<Arc<chanlun::structure::dash_line::线>> = 线
.iter()
.map(|d| Rc::clone(&d.bind(py).borrow().inner))
.map(|d| Arc::clone(&d.bind(py).borrow().inner))
.collect();
chanlun::structure::segment_feat::线::(
&rc_list,
@@ -1143,16 +1204,14 @@ impl 线段特征Py {
_cls: &Bound<'_, PyType>,
: Vec<Py<Self>>,
py: Python<'_>,
) -> Vec<Py> {
let rc_list: Vec<Rc<chanlun::structure::segment_feat::线>> =
) -> Vec<Py<Py>> {
let rc_list: Vec<Arc<chanlun::structure::segment_feat::线>> =
.iter()
.map(|s| Rc::clone(&s.bind(py).borrow().inner))
.map(|s| Arc::clone(&s.bind(py).borrow().inner))
.collect();
chanlun::structure::segment_feat::线::(&rc_list)
.into_iter()
.map(|inner| Py {
inner: Rc::new(inner),
})
.map(|inner| featfrac_to_py(py, Arc::new(inner)))
.collect()
}
}
@@ -1164,15 +1223,10 @@ impl 线段特征Py {
/// 属性 (只读):
/// 左: 线段特征|None / 中: 线段特征 / 右: 线段特征|None
/// 结构: 分型结构 — 顶/底分型判定结果
#[pyclass(
name = "特征分型",
module = "chanlun._chanlun",
unsendable,
from_py_object
)]
#[pyclass(name = "特征分型", module = "chanlun._chanlun", from_py_object)]
#[derive(Clone)]
pub struct Py {
pub(crate) inner: Rc<chanlun::structure::feat_fractal::>,
pub(crate) inner: Arc<chanlun::structure::feat_fractal::>,
}
#[pymethods]
@@ -1185,34 +1239,28 @@ impl 特征分型Py {
: &Bound<'_, Py>,
) -> Self {
Self {
inner: Rc::new(chanlun::structure::feat_fractal::::new(
Rc::clone(&.borrow().inner),
Rc::clone(&.borrow().inner),
Rc::clone(&.borrow().inner),
inner: Arc::new(chanlun::structure::feat_fractal::::new(
Arc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
Arc::clone(&.borrow().inner),
.borrow().inner,
)),
}
}
#[getter]
fn (&self) -> 线Py {
线Py {
inner: Rc::clone(&self.inner.),
}
fn (&self, py: Python<'_>) -> Py<线Py> {
segfeat_to_py(py, Arc::clone(&self.inner.))
}
#[getter]
fn (&self) -> 线Py {
线Py {
inner: Rc::clone(&self.inner.),
}
fn (&self, py: Python<'_>) -> Py<线Py> {
segfeat_to_py(py, Arc::clone(&self.inner.))
}
#[getter]
fn (&self) -> 线Py {
线Py {
inner: Rc::clone(&self.inner.),
}
fn (&self, py: Python<'_>) -> Py<线Py> {
segfeat_to_py(py, Arc::clone(&self.inner.))
}
#[getter]
@@ -1240,13 +1288,13 @@ impl 特征分型Py {
fn __eq__(&self, other: &Bound<'_, PyAny>) -> bool {
if let Ok(other) = other.extract::<PyRef<'_, Self>>() {
return Rc::as_ptr(&self.inner) == Rc::as_ptr(&other.inner);
return Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner);
}
false
}
fn __hash__(&self) -> u64 {
Rc::as_ptr(&self.inner) as u64
Arc::as_ptr(&self.inner) as u64
}
}
+5 -7
View File
@@ -286,17 +286,15 @@ impl 分型结构Py {
/// 返回:
/// 分型结构 或 None — 无法判定时返回 None
#[classmethod]
#[pyo3(signature = (左, 中, 右, 可以逆序包含 = false, 忽视顺序包含 = false))]
fn (
_cls: &Bound<'_, PyType>,
: &Bound<'_, PyAny>,
: &Bound<'_, PyAny>,
: &Bound<'_, PyAny>,
: Option<bool>,
: Option<bool>,
: bool,
: bool,
) -> PyResult<Option<Self>> {
let = .unwrap_or(false);
let = .unwrap_or(false);
let get_hl = |obj: &Bound<'_, PyAny>| -> PyResult<(f64, f64)> {
Ok((
obj.getattr("")?.extract::<f64>()?,
@@ -456,13 +454,13 @@ impl 缺口Py {
/// 返回:
/// 缺口 或 None — 起点==终点时返回 None
#[classmethod]
#[pyo3(signature = (起点, 终点, 比例 = 0.15))]
fn (
_cls: &Bound<'_, PyType>,
: f64,
: f64,
: Option<f64>,
: f64,
) -> Option<Self> {
let = .unwrap_or(0.15);
chanlun::types::::(, , ).map(|inner| Self { inner })
}
}