本页尚未翻译,以下为英文原文。
#Nabi for Pythonistas
本页内容
A map for Python programmers: what carries over, what Nabi writes differently, and where the Python standard library's modules live. Every block below is a complete program; run it with nabi run file.nabi. The chapters of this guide explain each part in full.
#What carries over
- A file runs top to bottom with no
main, as a script does:nabi file.nabi, a#!/usr/bin/env nabiline, and dependencies declared in the file itself (nabi add json file.nabi). - f-strings, with the same format specs:
f"{x:.2f}",f"{name:>10}",f"{x=}". - Slices with negative indices and steps:
xs[-1],xs[1:3],xs[::-1]. - Default and named arguments:
fn f(a: i32, sep: str = " "), calledf(1, sep: ", "). in, chained comparisons (0 <= i < n), tuple unpacking and swapping ((a, b) = (b, a)).try/catch/finally, generators (yield fn),async/await,matchwith guards.- Big integers:
inthas no size limit. - A REPL (
nabi repl) and a Jupyter kernel (nabi jupyter).
#The first surprises
printadds no newline;printlndoes. Both take several values andsep:.- Every value has a type the compiler knows. Parameters with no type are generic: they accept any type with the methods the body calls, which is duck typing checked at compile time.
- Blocks use
{ }, not indentation, and a new name is declared withlet. - There is no truthiness: a condition is a
bool. Write!xs.is_empty(),n != 0, orx != none. - A missing value is
none, and a type that may hold it is writtenT | none. /on integers rounds toward zero (7 / 2is3); floor division isn.div_floor(2)and%%is the floored remainder.//starts a comment.- An integer that overflows its type panics. An unsuffixed integer is an
i64unless its uses give it another type;intholds any size. - Structs are values:
let q = p;copies.List,DictandSetare shared handles, as in Python. strslices count bytes, not characters; walk characters withs.chars().
let name = "Ada";
let xs = [1, 2, 3];
print("no newline, ");
println("then one");
println("total:", xs.len(), xs);
println(1, 2, 3, sep: ", ");
println(f"{name} has {xs.len()} items: {xs:?}");
let x = 5;
println(f"{x=}");
no newline, then one
total: 3 [1, 2, 3]
1, 2, 3
Ada has 3 items: [1, 2, 3]
x=5
#Control flow
if is an expression, so it also takes the place of a if c else b. elif is else if, and for i in range(n) is for i in 0..n.
fn grade(score: i32) -> str {
if score >= 90 { "A" } else if score >= 80 { "B" } else { "C" }
}
let people = [("ada", 92), ("bob", 78)];
for (name, score) in people {
println(f"{name}: {grade(score)}");
}
for (i, (name, _)) in people.enumerate() {
println(f"{i} {name}");
}
for i in 0..3 {
if i == 1 { continue; }
println(i);
}
let label = if people.len() > 1 { "many" } else { "one" };
println(label);
let c = 5;
if 0 < c < 10 && c != 7 { println("in range"); }
if 3 in [1, 2, 3] { println("member"); }
ada: A
bob: C
0 ada
1 bob
0
2
many
in range
member
and, or and not are &&, || and !; not in is !in.
#Comprehensions are iterator chains
Nabi has no list, dict or set comprehensions. A collection has the iterator methods, and to_list(), to_dict() and to_set() collect the result. sum, min, max, sorted, any and all are methods too.
let xs = [1, 2, 3, 4, 5];
// [x * x for x in xs if x % 2 == 1]
let odd_squares = xs.filter(|x| x % 2 == 1).map(|x| x * x).to_list();
println(f"{odd_squares:?}");
println(f"{xs.sum()} {xs.min() ?? 0} {xs.max() ?? 0}");
let words = ["pear", "fig", "apple"];
println(f"{words.sorted():?}");
println(f"{words.sorted_by_key(|w| w.len()):?}");
println(words.join(", "));
println(f"{xs.any(|x| x > 4)} {xs.all(|x| x > 0)}");
// {w: len(w) for w in words}
let lengths = words.map(|w| (w, w.len())).to_dict();
println(lengths["fig"]);
println([1, 1, 2].to_set().len());
let (a, b) = (1, 2);
(a, b) = (b, a);
println(f"{a} {b}");
[1, 9, 25]
15 1 5
["apple", "fig", "pear"]
["fig", "pear", "apple"]
pear, fig, apple
true true
3
2
2 1
Closures are written |x| x + 1 where Python writes lambda x: x + 1.
#none instead of None
A function that may find nothing returns T | none. ?? gives a default, x == none tests, and if let takes the value out. After x != none, or below if x == none { return; }, x is the value itself.
let xs = ["a", "b"];
let i = xs.position(|x| x == "b");
if let k = i { println(f"found at {k}"); }
println(xs.position(|x| x == "z") ?? 99);
if !xs.is_empty() { println(f"{xs.len()} items"); }
found at 1
99
2 items
#Errors
A function that can fail says throws, and each call to it is marked with try. throw "message" raises a plain error; try .. catch handles one, and try? turns a failure into none.
fn parse_port(s: str) throws -> i64 {
let n = try s.parse::<i64>();
if n > 65535 { throw "port out of range"; }
n
}
println(try parse_port("8080") catch _ { 0 });
let p = try parse_port("99999") catch e {
println(f"error: {e}");
0
};
println(p);
println(try? parse_port("x") ?? -1);
8080
error: port out of range
0
-1
int("42") is try "42".parse::<int>(), and float(s) is try s.parse::<f64>().
#Classes are structs, traits and impl
A struct holds the fields, an impl block holds the methods, and #[derive(..)] writes __eq__, __repr__ and __hash__. A method that changes its value takes ref self.
#[derive(Debug, PartialEq)]
struct Point { x: f64; y: f64; }
impl Point {
fn new(x: f64, y: f64) -> Point { Point { x: x, y: y } }
fn norm(self) -> f64 { (self.x * self.x + self.y * self.y).sqrt() }
fn scale(ref self, k: f64) { self.x *= k; self.y *= k; }
}
let p = Point::new(3.0, 4.0);
println(p.norm());
p.scale(2.0);
println(f"{p:?}");
println(p == Point { x: 6.0, y: 8.0 });
5
Point { x: 6.0, y: 8.0 }
true
There is no inheritance. A trait lists the methods a type provides, with default bodies where it wants them, and any Shape holds any type that implements it.
trait Shape {
fn area(self) -> f64;
fn describe(self) -> str { f"area {self.area():.1}" }
}
struct Square { side: f64; }
struct Circle { r: f64; }
impl Shape for Square { fn area(self) -> f64 { self.side * self.side } }
impl Shape for Circle { fn area(self) -> f64 { f64::PI * self.r * self.r } }
let shapes: List<any Shape> = [Square { side: 2.0 }, Circle { r: 1.0 }];
for s in shapes { println(s.describe()); }
fn loud(x) -> str { x.to_uppercase() }
println(loud("hi"));
area 4.0
area 3.1
HI
#Values, handles and numbers
struct P { x: i32; }
let a = [1];
let b = a;
b.push(2);
println(f"{a:?}");
let p = P { x: 1 };
let q = p;
q.x = 9;
println(f"{p.x} {q.x}");
println(7 / 2);
println((-7).div_floor(2));
println(-7 %% 2);
println(7.0 / 2.0);
println((7 as f64) / 2.0);
println(2 ** 10);
[1, 2]
1 9
3
-4
1
3.5
3.5
1024
** is the power operator, as in Python: 2 ** 10, and -x ** 2 is -(x ** 2) (chapter 21). An integer base takes a u32 exponent, so 2 ** -1 is an error that says to write 2.0 ** -1.0.
A grid built by repetition is where the two differ. Python's [[0] * 3] * 2 puts the same row list in both slots, so g[0][1] = 5 changes both rows. Nabi's [[0; 3]; 2] evaluates the row once and copies it into each slot (a fixed array, chapter 2), so g[0][1] = 5 changes only the first row. An element that holds a list is copied with clone(): [List::<i32>::new(); 3] is three separate lists.
#with is using
using binds a value that is closed when its scope ends, as a with block closes its file.
struct Conn { name: str; }
impl Drop for Conn {
fn drop(ref self) { println(f"closed {self.name}"); }
}
fn work() {
using c = Conn { name: "db" };
println(f"using {c.name}");
}
work();
println("done");
using db
closed db
done
#Files and text
use std::{env, fs, process};
let path = env::temp_dir() / f"pynabi_{process::id()}.txt";
try fs::write(path, "one\ntwo\n");
let text = try fs::read_to_str(path);
for line in text.lines() { println(line.to_uppercase()); }
try fs::remove_file(path);
println(42.to_str() + "px");
println(f"{3.14159:.2}");
println("ab".repeat(3));
println("a,b".split(",").to_list().len());
ONE
TWO
42px
3.14
ababab
2
#Python idioms in Nabi
| Python | Nabi |
|---|---|
print(x) / print(x, end="") | println(x) / print(x) |
print(a, b, sep=", ") | println(a, b, sep: ", ") |
f"{x!r}" | f"{x:?}" |
a and b, a or b, not a | a && b, a || b, !a |
x not in xs | x !in xs |
x is None, x is not None | x == none, x != none |
None, True, False | none, true, false |
a if c else b | if c { a } else { b } |
elif | else if |
def f(x): / pass | fn f(x) { } (an empty block needs no pass) |
lambda x: x + 1 | |x| x + 1 |
if xs: | if !xs.is_empty() |
len(xs), sorted(xs), sum(xs) | xs.len(), xs.sorted(), xs.sum() |
min(a, b), abs(x), round(x) | a.min(b), x.abs(), x.round() |
str(x) | x.to_str() or f"{x}" |
int(s), float(s) | try s.parse::<int>(), try s.parse::<f64>() |
", ".join(xs) | xs.join(", ") |
s * 3 | s.repeat(3) |
[[0] * 3] * 2 (rows shared) | [[0; 3]; 2] (each row a copy) |
range(n), range(a, b) | 0..n, a..b |
for i, x in enumerate(xs) | for (i, x) in xs.enumerate() |
for k, v in d.items() | for (k, v) in d |
[f(x) for x in xs if c] | xs.filter(|x| c).map(|x| f(x)).to_list() |
sorted(xs, key=f, reverse=True) | xs.sorted_by_key(f), xs.sorted(reverse: true) |
d.get(k, 0), defaultdict(int) and d[k] += 1 | d[k, default: 0], d[k, default: 0] += 1 |
Counter(xs), Counter(xs).most_common(n) | xs.counts(), xs.counts().most_common(n) |
print(f"{x=}"), breakpoint()-style dumps | f"{x=}", dbg(x) |
x ** 2, n // 2 | x ** 2, n.div_floor(2) |
raise ValueError("m") / except E as e | throw "m" / catch e: E |
with open(p) as f: | using f = try File::open(p); |
assert x == 2, "m" | assert(x == 2, "m"); |
class C: with self | struct C { .. } and impl C { fn m(self) { .. } } |
C("h", 1) | C { host: "h", port: 1 } |
import m / from m import x | use m; / use m::x; |
Each of these Python forms, typed in Nabi, stops the build with one error whose help shows the Nabi form to write: and / or / not, is not, a if c else b, lambda, comprehensions (the help writes the iterator chain), elif, except T as e, with open(..) as f, assert c, m, for i, x in .., """..""", import math and from m import x, pass, f(end=""), f"{x!r}", builtins called as functions (len(xs), str(x), int(s)), Counter(xs) and xs.most_common(n), C("h", 1), ", ".join(xs), s * 3, truthiness (if xs:) and a method without self.
#Python modules in Nabi
| Python | Nabi |
|---|---|
math | methods on numbers (x.sqrt(), x.sin(), x.floor(), x.is_close(y), a.gcd(b)) and f64::PI; num::fsum, num::comb, num::perm |
os, sys | std::env (env::args(), env::var(..)), std::process (process::exit(..), process::id()) |
os.path, pathlib | std::path::Path (Path::new("a") / "b"), std::fs |
open, shutil | std::fs (fs::read_to_str, fs::write, File::open) |
subprocess | std::process (process::run("git", ["status"]) for the output, Command for pipes) |
glob | fs::glob("*.txt") |
random | std::random (random::range(1..7), random::choice(xs), random::shuffle(xs); Xoshiro256PlusPlus::seed(42) for a seeded Rng with normal, gamma, poisson, weighted, ...) |
cmath, complex | Complex and the i suffix: 3.0 + 4.0i |
numpy.linspace, numpy.arange | num::linspace(0.0, 1.0, 5), num::arange(0.0, 1.0, 0.25); Array for n-d data (chapter 21) |
numpy reductions (a.sum(axis=0), np.nanmax, np.where, np.allclose) | a.sum_along(0), a.nanmax(), array::where(m, a, b), a.all_close(b) |
numpy.linalg, scipy.sparse, scipy.stats, scipy.signal, scipy.optimize, pandas, matplotlib | the packages linalg, sparse, stats, signal, optimize, table, plot and more (chapter 22) |
time | core::time (Instant::now(), Duration) |
datetime, zoneinfo | std::time (try Date::new(2024, 1, 31), DateTime, Zoned::now(TimeZone::local()), try TimeZone::get("Europe/Paris"), d.format("%Y-%m-%d"), try Date::parse_with(..)) |
threading | std::thread (thread::spawn(..), join) |
asyncio | async fn and await, built in |
collections | Dict, Set, Deque, BinaryHeap, built in; Counter is xs.counts() with most_common(n), defaultdict is d[k, default: v] |
sortedcontainers | SortedDict, SortedSet in core::collections |
itertools, functools | iterator methods: zip, chain, windows, chunks, pairwise, combinations, permutations, group_by, fold, reduce, scan |
json | the json package: nabi add json |
input() | try io::input("prompt") |
unittest, pytest | #[test] functions and nabi test |
black, ruff | nabi fmt, nabi lint |
pip, venv | nabi add, nabi env |
re is the first-party regex package (nabi add regex). logging and argparse have no counterpart yet; they are planned as the log and cli packages.
Found a mistake? Edit this page on GitHub. Last updated