Skip to content

Commit 7728bd0

Browse files
committed
add files required to compile rust code and use as Python module - example rust lib, not required in this iteration
1 parent 6a378c1 commit 7728bd0

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
.idea/
161+
.vscode/

Cargo.toml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
[package]
22
name = "iotext"
3+
authors = ["Marcin Bielak <marcin.bieli@gmail.com>"]
34
version = "0.1.0"
45
rust-version = "1.57.0"
56
edition = "2021"
67

78
[lib]
8-
name = "_iotext"
9+
name = "iotext"
910
crate-type = ["cdylib"]
1011

1112
[dependencies]
12-
pyo3 = { version = "0.19.2", features = ["auto-initialize", "extension-module"] }
13+
pyo3 = { version = "0.19.2", features = ["extension-module"] }
1314
iotext_rs = { git = "https://github.com/bieli/IoText-rs.git" }
15+
rayon = "1.0.2"
16+
17+
#[lib]
18+
#name = "_iotext"
19+
#crate-type = ["cdylib"]
20+
21+
#pyo3 = { version = "0.19.2", features = ["auto-initialize", "extension-module"] }
22+
#iotext_rs = { git = "https://github.com/bieli/IoText-rs.git" }
1423
# dependencies
15-
fancy-regex = "0.11.0"
16-
regex = "1.9.4"
17-
rustc-hash = "1.1.0"
18-
bstr = "1.6.1"
24+
#fancy-regex = "0.11.0"
25+
#regex = "1.9.4"
26+
#rustc-hash = "1.1.0"
27+
#bstr = "1.6.1"
1928

20-
[profile.release]
21-
incremental = true
29+
#[profile.release]
30+
#incremental = true

iotext/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .iotext import search
2+
3+
__all__ = [
4+
"search"
5+
]

noxfile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import nox
2+
3+
nox.options.sessions = ["test"]
4+
5+
6+
@nox.session
7+
def test(session):
8+
session.install("-rrequirements-dev.txt")
9+
session.install("maturin")
10+
session.run_always("maturin", "develop")
11+
session.run("pytest")
12+
13+
14+
@nox.session
15+
def bench(session):
16+
session.install("-rrequirements-dev.txt")
17+
session.install(".")
18+
session.run("pytest", "--benchmark-enable")

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
nox
12
mypy
23
black==22.3.0
34
parameterized==0.9.0

src/lib.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22
#![allow(clippy::borrow_deref_ref)]
33

44
use pyo3::prelude::*;
5+
use rayon::prelude::*;
56

6-
/// Formats the sum of two numbers as string.
7+
/// Searches for the word, parallelized by rayon
78
#[pyfunction]
8-
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
9-
Ok((a + b).to_string())
9+
fn search(contents: &str, needle: &str) -> usize {
10+
contents
11+
.par_lines()
12+
.map(|line| count_line(line, needle))
13+
.sum()
14+
}
15+
16+
/// Count the occurrences of needle in line, case insensitive
17+
fn count_line(line: &str, needle: &str) -> usize {
18+
let mut total = 0;
19+
for word in line.split(' ') {
20+
if word == needle {
21+
total += 1;
22+
}
23+
}
24+
total
1025
}
1126

12-
/// A Python module implemented in Rust. The name of this function must match
13-
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
14-
/// import the module.
1527
#[pymodule]
16-
fn string_sum(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
17-
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
28+
fn iotext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
29+
m.add_function(wrap_pyfunction!(search, m)?)?;
30+
1831
Ok(())
1932
}

0 commit comments

Comments
 (0)