-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpython_repr.rs
More file actions
142 lines (130 loc) · 4.39 KB
/
python_repr.rs
File metadata and controls
142 lines (130 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::convert::AsRef;
use std::fmt::Display;
use std::fmt::Formatter;
use itertools::Itertools;
use vortex::dtype::DType;
use vortex::dtype::Nullability;
use vortex::dtype::PType;
use vortex::dtype::extension::ExtId;
pub trait PythonRepr {
fn python_repr(&self) -> impl Display;
}
struct DTypePythonRepr<'a>(&'a DType);
impl PythonRepr for DType {
fn python_repr(&self) -> impl Display {
DTypePythonRepr(self)
}
}
// TODO(connor): We should probably just use the `Display` impl on `DType`.
impl Display for DTypePythonRepr<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let DTypePythonRepr(dtype) = self;
match dtype {
DType::Null => write!(f, "null()"),
DType::Bool(n) => write!(f, "bool(nullable={})", n.python_repr()),
DType::Primitive(ptype, n) => match ptype {
PType::U8 | PType::U16 | PType::U32 | PType::U64 => {
write!(
f,
"uint({}, nullable={})",
ptype.bit_width(),
n.python_repr()
)
}
PType::I8 | PType::I16 | PType::I32 | PType::I64 => {
write!(
f,
"int({}, nullable={})",
ptype.bit_width(),
n.python_repr()
)
}
PType::F16 | PType::F32 | PType::F64 => {
write!(
f,
"float({}, nullable={})",
ptype.bit_width(),
n.python_repr()
)
}
},
DType::Decimal(decimal_type, n) => {
write!(
f,
"decimal(precision={}, scale={}, nullable={})",
decimal_type.precision(),
decimal_type.scale(),
n.python_repr()
)
}
DType::Utf8(n) => write!(f, "utf8(nullable={})", n.python_repr()),
DType::Binary(n) => write!(f, "binary(nullable={})", n.python_repr()),
DType::Struct(st, n) => write!(
f,
"struct({{{}}}, nullable={})",
st.names()
.iter()
.zip(st.fields())
.map(|(n, dt)| format!("\"{}\": {}", n, dt.python_repr()))
.join(", "),
n.python_repr()
),
DType::List(edt, n) => write!(
f,
"list({}, nullable={})",
edt.python_repr(),
n.python_repr()
),
DType::FixedSizeList(edt, size, n) => write!(
f,
"fixed_size_list({}, {}, nullable={})",
edt.python_repr(),
size,
n.python_repr()
),
DType::Extension(ext) => {
write!(
f,
"ext({}, {}",
ext.id().python_repr(),
ext.storage_dtype().python_repr()
)?;
let opts = ext.display_metadata().to_string();
if !opts.is_empty() {
write!(f, ", {}", opts)?
}
write!(f, ")")
}
DType::Variant(_) => write!(f, "variant()"),
}
}
}
struct NullabilityPythonRepr<'a>(&'a Nullability);
impl PythonRepr for Nullability {
fn python_repr(&self) -> impl Display {
NullabilityPythonRepr(self)
}
}
impl Display for NullabilityPythonRepr<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let NullabilityPythonRepr(x) = self;
match x {
Nullability::NonNullable => write!(f, "False"),
Nullability::Nullable => write!(f, "True"),
}
}
}
struct ExtIdPythonRepr<'a>(&'a ExtId);
impl PythonRepr for ExtId {
fn python_repr(&self) -> impl Display {
ExtIdPythonRepr(self)
}
}
impl Display for ExtIdPythonRepr<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let ExtIdPythonRepr(ext_id) = self;
write!(f, "\"{}\"", ext_id.as_ref().escape_default())
}
}