#Python 사용자를 위한 Nabi
이 페이지의 내용
Python 프로그래머를 위한 안내입니다. 그대로 통하는 것, Nabi가 다르게 쓰는 것, Python 표준 라이브러리 모듈이 Nabi의 어디에 있는지를 정리했습니다. 아래 블록은 모두 완결된 프로그램이니 nabi run file.nabi로 실행해 보세요. 각 부분은 이 가이드의 해당 장에서 자세히 다룹니다.
#그대로 통하는 것
- 파일은
main없이 위에서 아래로 실행됩니다. 스크립트처럼nabi file.nabi로 실행하고,#!/usr/bin/env nabi줄을 쓸 수 있으며, 의존성도 파일 안에 적습니다(nabi add json file.nabi). - f-string과 같은 서식 지정:
f"{x:.2f}",f"{name:>10}",f"{x=}". - 음수 인덱스와 step이 있는 슬라이스:
xs[-1],xs[1:3],xs[::-1]. - 기본 인자와 이름 붙은 인자:
fn f(a: i32, sep: str = " ")를f(1, sep: ", ")로 호출합니다. in, 연쇄 비교(0 <= i < n), 튜플 분해와 교환((a, b) = (b, a)).try/catch/finally, 제너레이터(yield fn),async/await, guard가 있는match.- 큰 정수:
int에는 크기 제한이 없습니다. - REPL(
nabi repl)과 Jupyter 커널(nabi jupyter).
#처음 놀라는 점
print는 줄을 바꾸지 않고,println이 줄을 바꿉니다. 둘 다 값을 여러 개 받고sep:도 받습니다.- 모든 값의 타입을 컴파일러가 압니다. 타입을 적지 않은 매개변수는 제네릭이라, 본문이 부르는 메서드가 있는 타입이면 무엇이든 받습니다. 컴파일 시점에 검사하는 덕 타이핑입니다.
- 블록은 들여쓰기가 아니라
{ }로 묶고, 새 이름은let으로 선언합니다. - truthiness가 없습니다. 조건은
bool이어야 하니!xs.is_empty(),n != 0,x != none처럼 씁니다. - 값이 없음은
none이고, 그것을 담을 수 있는 타입은T | none으로 씁니다. - 정수의
/는 0 쪽으로 버립니다(7 / 2는3). 내림 나눗셈은n.div_floor(2)이고,%%가 내림 나머지입니다.//는 주석의 시작입니다. - 정수가 자기 타입의 범위를 넘치면 panic이 납니다. 접미사 없는 정수는 쓰임이 다른 타입을 정하지 않으면
i64이고, 크기 제한이 없는 것은int입니다. - 구조체는 값입니다.
let q = p;는 복사합니다.List,Dict,Set은 Python처럼 공유되는 핸들입니다. str슬라이스는 문자가 아니라 바이트를 셉니다. 문자 단위로 다니려면s.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
#제어 흐름
if는 식이라 a if c else b 자리도 대신합니다. elif는 else if이고, for i in range(n)은 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, not은 &&, ||, !이고, not in은 !in입니다.
#컴프리헨션은 반복자 체인으로
Nabi에는 리스트, 딕셔너리, 집합 컴프리헨션이 없습니다. 컬렉션에 반복자 메서드가 있고, to_list(), to_dict(), to_set()으로 결과를 모읍니다. sum, min, max, sorted, any, all도 메서드입니다.
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
Python의 lambda x: x + 1은 클로저 |x| x + 1입니다.
#None 대신 none
아무것도 못 찾을 수 있는 함수는 T | none을 반환합니다. ??는 기본값을 주고, x == none은 검사하고, if let은 값을 꺼냅니다. x != none 뒤나 if x == none { return; } 아래에서는 x가 값 자체입니다.
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
#에러
실패할 수 있는 함수는 throws라고 밝히고, 그 함수를 부를 때마다 try를 붙입니다. throw "message"는 간단한 에러를 던지고, try .. catch가 에러를 처리하며, try?는 실패를 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")는 try "42".parse::<int>()이고, float(s)는 try s.parse::<f64>()입니다.
#클래스는 구조체, 트레잇, impl로
구조체가 필드를 담고, impl 블록이 메서드를 담으며, #[derive(..)]가 __eq__, __repr__, __hash__를 대신 씁니다. 값을 바꾸는 메서드는 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
상속은 없습니다. 트레잇이 타입이 제공할 메서드를 나열하고(원하면 기본 본문도 줍니다), any Shape는 그것을 구현한 어떤 타입이든 담습니다.
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
#값, 핸들, 숫자
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
**는 Python처럼 거듭제곱 연산자입니다. 2 ** 10이고, -x ** 2는 -(x ** 2)입니다(21장). 정수 밑은 u32 지수를 받으므로 2 ** -1은 에러이며, 2.0 ** -1.0으로 쓰라고 알려 줍니다.
반복으로 만든 격자에서는 둘이 다릅니다. Python의 [[0] * 3] * 2는 같은 행 리스트를 두 자리에 넣으므로 g[0][1] = 5가 두 행을 다 바꿉니다. Nabi의 [[0; 3]; 2]는 행을 한 번 평가하고 자리마다 복사하므로(고정 배열, 2장) g[0][1] = 5는 첫 행만 바꿉니다. 리스트를 가진 원소는 clone()으로 복사합니다. [List::<i32>::new(); 3]은 서로 다른 리스트 세 개입니다.
#with는 using으로
using으로 묶은 값은 스코프가 끝날 때 닫힙니다. with 블록이 파일을 닫는 것과 같습니다.
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
#파일과 텍스트
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 관용구를 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) { } (빈 블록에는 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() 또는 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 (행을 공유) | [[0; 3]; 2] (행마다 복사본) |
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)와 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=}"), 값을 찍어 보는 디버깅 | 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"); |
self를 쓰는 class C: | struct C { .. }와 impl C { fn m(self) { .. } } |
C("h", 1) | C { host: "h", port: 1 } |
import m / from m import x | use m; / use m::x; |
이 Python 형태를 Nabi에 쓰면 빌드가 에러 하나로 멈추고, 그 help가 대신 쓸 Nabi 형태를 보여 줍니다: and / or / not, is not, a if c else b, lambda, 컴프리헨션(help가 반복자 체인을 써 줍니다), elif, except T as e, with open(..) as f, assert c, m, for i, x in .., """..""", import math와 from m import x, pass, f(end=""), f"{x!r}", 함수로 부른 내장 함수(len(xs), str(x), int(s)), Counter(xs)와 xs.most_common(n), C("h", 1), ", ".join(xs), s * 3, truthiness(if xs:), self 없는 메서드.
#Python 모듈을 Nabi에서
| Python | Nabi |
|---|---|
math | 숫자의 메서드(x.sqrt(), x.sin(), x.floor(), x.is_close(y), a.gcd(b))와 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"]), 파이프는 Command) |
glob | fs::glob("*.txt") |
random | std::random(random::range(1..7), random::choice(xs), random::shuffle(xs). 시드를 준 Rng는 Xoshiro256PlusPlus::seed(42)이며 normal, gamma, poisson, weighted 등이 있음) |
cmath, complex | Complex와 i 접미사: 3.0 + 4.0i |
numpy.linspace, numpy.arange | num::linspace(0.0, 1.0, 5), num::arange(0.0, 1.0, 0.25). n차원 데이터는 Array(21장) |
numpy 집계(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 | 패키지 linalg, sparse, stats, signal, optimize, table, plot 등(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과 await |
collections | 기본으로 있는 Dict, Set, Deque, BinaryHeap. Counter는 xs.counts()와 most_common(n), defaultdict는 d[k, default: v] |
sortedcontainers | core::collections의 SortedDict, SortedSet |
itertools, functools | 반복자 메서드: zip, chain, windows, chunks, pairwise, combinations, permutations, group_by, fold, reduce, scan |
json | json 패키지: nabi add json |
input() | try io::input("prompt") |
unittest, pytest | #[test] 함수와 nabi test |
black, ruff | nabi fmt, nabi lint |
pip, venv | nabi add, nabi env |
re는 공식 패키지 regex입니다(nabi add regex). logging과 argparse에 해당하는 것은 아직 없으며, log와 cli 패키지로 계획되어 있습니다.
잘못된 곳이 있나요? GitHub에서 이 페이지 고치기. 마지막 수정