Skip to content

Commit 2526f27

Browse files
authored
Merge pull request #170 from tushushu/wip-read-csv-utils
A half done `read_csv` func
2 parents 8a2bffc + feea034 commit 2526f27

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

ulist/python/ulist/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .constructor import arange, cycle, from_seq, repeat # noqa:F401
22
from .control_flow import select # noqa:F401
33
from .core import UltraFastList # noqa:F401
4+
from .io import read_csv # noqa:F401
45
from .ulist import IndexList # noqa:F401
56

67
__version__ = "0.10.0"

ulist/python/ulist/io.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations # To avoid circular import.
2+
from .ulist import read_csv as _read_csv
3+
from typing import List, TYPE_CHECKING
4+
5+
if TYPE_CHECKING: # To avoid circular import.
6+
from . import UltraFastList
7+
8+
9+
def read_csv() -> List[UltraFastList]:
10+
from . import UltraFastList # To avoid circular import.
11+
return [UltraFastList(x) for x in _read_csv()]

ulist/python/ulist/ulist.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ def arange32(start: int, stop: int, step: int) -> IntegerList32: ...
293293
def arange64(start: int, stop: int, step: int) -> IntegerList64: ...
294294

295295

296+
def read_csv() -> list: ...
297+
298+
296299
def select_bool(
297300
conditions: List[BooleanList],
298301
choices: LIST_PY,

ulist/src/io.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::boolean::BooleanList;
2+
use crate::floatings::FloatList64;
3+
use crate::integers::IntegerList64;
4+
use crate::string::StringList;
5+
use pyo3::prelude::*;
6+
use std::collections::HashSet;
7+
use std::iter::FromIterator;
8+
9+
#[pyfunction]
10+
pub fn read_csv(py: Python) -> Vec<PyObject> {
11+
// This is an example implementation of `read_csv` function, which will return
12+
// PyList[BooleanList[True, False, None], IntegerList64[2, 3, None],
13+
// FloatList64[2.0, 3.0, None], StringList['foo', 'bar', None]]
14+
let blist = BooleanList::new(vec![true, false, false], HashSet::from_iter(vec![2]));
15+
let ilist = IntegerList64::new(vec![2, 3, 0], HashSet::from_iter(vec![2]));
16+
let flist = FloatList64::new(vec![2.0, 3.0, 0.0], HashSet::from_iter(vec![2]));
17+
let slist = StringList::new(
18+
vec!["foo".to_string(), "bar".to_string(), "".to_string()],
19+
HashSet::from_iter(vec![2]),
20+
);
21+
let mut result: Vec<PyObject> = Vec::new();
22+
result.push(blist.into_py(py));
23+
result.push(ilist.into_py(py));
24+
result.push(flist.into_py(py));
25+
result.push(slist.into_py(py));
26+
return result;
27+
}

ulist/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod control_flow;
44
mod floatings;
55
mod index;
66
mod integers;
7+
mod io;
78
mod non_float;
89
mod numerical;
910
mod string;
@@ -29,6 +30,7 @@ fn ulist(_py: Python, m: &PyModule) -> PyResult<()> {
2930
m.add_function(wrap_pyfunction!(select_float, m)?)?;
3031
m.add_function(wrap_pyfunction!(select_int, m)?)?;
3132
m.add_function(wrap_pyfunction!(select_string, m)?)?;
33+
m.add_function(wrap_pyfunction!(io::read_csv, m)?)?;
3234

3335
Ok(())
3436
}

0 commit comments

Comments
 (0)