feat: support tqdm

This commit is contained in:
Shunsuke Shibayama 2023-02-25 01:58:37 +09:00
parent 88ccfb9078
commit af3c912b17
11 changed files with 77 additions and 8 deletions

View file

@ -128,10 +128,10 @@
math, time = pyimport "math", "time"
{sin; pi} = math
# using an external Python module
Tqdm! = pyimport("tqdm").'tqdm'
tqdm = pyimport "tqdm"
print! sin pi # 1.2246467991473532e-16
for! Tqdm!.'__call__'(0..99), i =>
for! tqdm.Tqdm!(0..99), i =>
time.sleep! 0.01 * i
```

View file

@ -127,10 +127,10 @@
math, time = pyimport "math", "time"
{sin; pi} = math
# Pythonの外部モジュールを使います
Tqdm! = pyimport("tqdm").'tqdm'
tqdm = pyimport("tqdm")
print! sin pi # 1.2246467991473532e-16
for! Tqdm!.'__call__'(0..99), i =>
for! tqdm.Tqdm!(0..99), i =>
time.sleep! 0.01 * i
```

View file

@ -127,10 +127,10 @@
math, time = pyimport "math", "time"
{sin; pi} = math
# 使用外部Python模块
Tqdm! = pyimport("tqdm").'tqdm'
tqdm = pyimport "tqdm"
print! sin pi # 1.2246467991473532e-16
for! Tqdm!.'__call__'(0..99), i =>
for! tqdm.Tqdm!(0..99), i =>
time.sleep! 0.01 * i
```

View file

@ -127,10 +127,10 @@
math, time = pyimport "math", "time"
{sin; pi} = math
# 使用外部Python模塊
Tqdm! = pyimport("tqdm").'tqdm'
tqdm = pyimport "tqdm"
print! sin pi # 1.2246467991473532e-16
for! Tqdm!.'__call__'(0..99), i =>
for! tqdm.Tqdm!(0..99), i =>
time.sleep! 0.01 * i
```

View file

@ -577,6 +577,27 @@ pub fn env_magic_number() -> u32 {
detect_magic_number(&which_python())
}
pub fn module_exists(py_command: &str, module: &str) -> bool {
let code = format!("import importlib; errc = 1 if importlib.util.find_spec('{module}') is None else 0; exit(errc)");
let out = if cfg!(windows) {
Command::new("cmd")
.arg("/C")
.arg(py_command)
.arg("-c")
.arg(code)
.output()
.expect("cannot get module spec")
} else {
let exec_command = format!("{py_command} -c '{code}'");
Command::new("sh")
.arg("-c")
.arg(exec_command)
.output()
.expect("cannot get module spec")
};
out.status.success()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PythonVersion {
pub major: u8,

View file

@ -1276,6 +1276,10 @@ impl Context {
}
other => {
let one = self.get_singular_ctx_by_hir_expr(obj, &self.name).ok();
let one = one
.zip(attr_name.as_ref())
.and_then(|(ctx, attr)| ctx.get_singular_ctx_by_ident(attr, &self.name).ok())
.or(one);
let two = obj
.qual_name()
.and_then(|name| self.get_same_name_context(name));

View file

View file

@ -0,0 +1,32 @@
.Tqdm! = 'tqdm': ClassType
.Tqdm! <: Iterable Int
.Tqdm!.
# TODO: iterable should be Comparable
__call__: (
iterable: Iterable(Int),
desc := Str,
total := Int or Float,
leave := Bool,
file := File!,
ncols := Int,
mininterval := Float,
maxinterval := Float,
miniters := Int or Float,
ascii := Bool or Str,
disable := Bool,
unit := Str,
unit_scale := Bool or Int or Float,
dynamic_ncols := Bool,
smoothing := Float,
bar_format := Str,
initial := Int or Float,
position := Int,
postfix := {Str: Obj},
unit_divisor := Float,
write_bytes := Bool,
lock_args := [Obj; _],
nrows := Int,
colour := Str,
delay := Float,
gui := Bool,
) -> .Tqdm!

12
examples/external.er Normal file
View file

@ -0,0 +1,12 @@
time = pyimport "time"
tqdm = pyimport "tqdm"
for! tqdm.Tqdm!(0..<100), _ =>
time.sleep! 0.01
plt = pyimport "matplotlib/pyplot"
discard plt.plot! 0..<10, [2, 3, 2, 3, 2, 3, 2, 3, 2, 3]
discard plt.title! "My Plot"
discard plt.xlabel! "X"
plt.show!()