-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjsonnet_conversion_example.py
More file actions
333 lines (264 loc) · 10.6 KB
/
jsonnet_conversion_example.py
File metadata and controls
333 lines (264 loc) · 10.6 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
Example of using the Jsonnet conversion utilities.
This script demonstrates how to use the Jsonnet conversion utilities
for converting data between Jsonnet and other formats (JSONL, Parquet, CAR, IPLD).
"""
import os
import sys
import tempfile
import json
# Add parent directory to path so we can import the module
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from ipfs_datasets_py.utils.jsonnet_utils import (
JsonnetConverter,
jsonnet_to_json,
jsonnet_to_jsonl,
jsonl_to_jsonnet,
jsonnet_to_parquet,
)
from ipfs_datasets_py.data_transformation.serialization.car_conversion import DataInterchangeUtils
from ipfs_datasets_py.data_transformation.serialization.dataset_serialization import DatasetSerializer
def create_sample_jsonnet(file_path):
"""Create a sample Jsonnet file for testing."""
# Jsonnet template with variables and functions
jsonnet_template = """
// Sample Jsonnet template demonstrating various features
local users = [
{ name: "Alice", age: 30, role: "engineer" },
{ name: "Bob", age: 25, role: "designer" },
{ name: "Charlie", age: 35, role: "manager" },
];
local department = std.extVar("department");
local multiplier = std.parseJson(std.extVar("multiplier"));
// Generate records with computed fields
[
user + {
id: i,
department: department,
salary: (user.age * 1000) * multiplier,
is_senior: user.age >= 30,
}
for i in std.range(0, std.length(users) - 1)
for user in [users[i]]
]
"""
with open(file_path, 'w') as f:
f.write(jsonnet_template)
return file_path
def example_jsonnet_to_json():
"""Example of converting Jsonnet to JSON."""
print("\n=== Example: Jsonnet to JSON ===")
# Simple Jsonnet template
jsonnet_str = """
{
name: "Example",
items: [
{ id: 1, value: "first" },
{ id: 2, value: "second" },
{ id: 3, value: "third" },
],
timestamp: "2024-01-01T00:00:00Z",
}
"""
# Convert to JSON
json_str = jsonnet_to_json(jsonnet_str)
print("Evaluated JSON:")
print(json_str)
# Parse to verify
data = json.loads(json_str)
print(f"\nParsed data has {len(data['items'])} items")
def example_jsonnet_with_variables():
"""Example of using Jsonnet with external variables."""
print("\n=== Example: Jsonnet with External Variables ===")
with tempfile.TemporaryDirectory() as temp_dir:
# Create sample Jsonnet file
jsonnet_path = os.path.join(temp_dir, "sample.jsonnet")
create_sample_jsonnet(jsonnet_path)
print(f"Created sample Jsonnet file at {jsonnet_path}")
# Evaluate with external variables
converter = JsonnetConverter()
json_str = converter.jsonnet_file_to_json(
jsonnet_path,
ext_vars={
"department": "Engineering",
"multiplier": "1.5"
}
)
print("\nEvaluated JSON:")
data = json.loads(json_str)
print(json.dumps(data, indent=2))
def example_jsonnet_to_jsonl():
"""Example of converting Jsonnet to JSONL."""
print("\n=== Example: Jsonnet to JSONL ===")
with tempfile.TemporaryDirectory() as temp_dir:
# Create sample Jsonnet file
jsonnet_path = os.path.join(temp_dir, "sample.jsonnet")
create_sample_jsonnet(jsonnet_path)
# Convert to JSONL using the file function
converter = JsonnetConverter()
jsonl_path = os.path.join(temp_dir, "output.jsonl")
output_path = converter.jsonnet_file_to_jsonl(
jsonnet_path,
jsonl_path,
ext_vars={
"department": "Engineering",
"multiplier": "2.0"
}
)
print(f"Converted to JSONL: {output_path}")
# Read and display
with open(output_path, 'r') as f:
lines = f.readlines()
print(f"JSONL file has {len(lines)} lines")
print("\nFirst line:")
print(json.dumps(json.loads(lines[0]), indent=2))
def example_jsonl_to_jsonnet():
"""Example of converting JSONL to Jsonnet."""
print("\n=== Example: JSONL to Jsonnet ===")
with tempfile.TemporaryDirectory() as temp_dir:
# Create sample JSONL file
jsonl_path = os.path.join(temp_dir, "sample.jsonl")
sample_data = [
{"id": 1, "name": "Item 1", "value": 100},
{"id": 2, "name": "Item 2", "value": 200},
{"id": 3, "name": "Item 3", "value": 300},
]
with open(jsonl_path, 'w') as f:
for record in sample_data:
f.write(json.dumps(record) + '\n')
print(f"Created sample JSONL file with {len(sample_data)} records")
# Convert to Jsonnet
jsonnet_path = os.path.join(temp_dir, "output.jsonnet")
output_path = jsonl_to_jsonnet(jsonl_path, jsonnet_path)
print(f"Converted to Jsonnet: {output_path}")
# Read and display
with open(output_path, 'r') as f:
content = f.read()
print("\nJsonnet content (first 200 chars):")
print(content[:200] + "..." if len(content) > 200 else content)
def example_jsonnet_to_parquet():
"""Example of converting Jsonnet to Parquet."""
print("\n=== Example: Jsonnet to Parquet ===")
try:
import pyarrow.parquet as pq
with tempfile.TemporaryDirectory() as temp_dir:
# Create sample Jsonnet file
jsonnet_path = os.path.join(temp_dir, "sample.jsonnet")
create_sample_jsonnet(jsonnet_path)
# Convert to Parquet using the file function
converter = JsonnetConverter()
parquet_path = os.path.join(temp_dir, "output.parquet")
output_path = converter.jsonnet_file_to_parquet(
jsonnet_path,
parquet_path,
ext_vars={
"department": "Engineering",
"multiplier": "1.8"
},
compression="snappy"
)
print(f"Converted to Parquet: {output_path}")
# Read and display statistics
table = pq.read_table(output_path)
print(f"Row count: {table.num_rows}")
print(f"Column count: {len(table.column_names)}")
print(f"Columns: {', '.join(table.column_names)}")
print(f"File size: {os.path.getsize(output_path) / 1024:.2f} KB")
# Display first few rows
print("\nFirst 2 rows:")
df = table.to_pandas().head(2)
print(df)
except ImportError:
print("PyArrow not available, skipping Parquet example")
def example_jsonnet_to_car():
"""Example of converting Jsonnet to CAR (Content Archive)."""
print("\n=== Example: Jsonnet to CAR ===")
try:
with tempfile.TemporaryDirectory() as temp_dir:
# Create sample Jsonnet file
jsonnet_path = os.path.join(temp_dir, "sample.jsonnet")
create_sample_jsonnet(jsonnet_path)
# Convert to CAR
car_path = os.path.join(temp_dir, "output.car")
converter = DataInterchangeUtils()
root_cid = converter.jsonnet_to_car(
jsonnet_path,
car_path,
ext_vars={
"department": "Engineering",
"multiplier": "2.5"
}
)
print(f"Converted to CAR: {car_path}")
print(f"Root CID: {root_cid}")
print(f"File size: {os.path.getsize(car_path) / 1024:.2f} KB")
# Convert back to Jsonnet
output_jsonnet_path = os.path.join(temp_dir, "recovered.jsonnet")
recovered_path = converter.car_to_jsonnet(car_path, output_jsonnet_path)
print(f"\nConverted back to Jsonnet: {recovered_path}")
# Verify content
with open(recovered_path, 'r') as f:
content = f.read()
data = json.loads(content)
print(f"Recovered {len(data)} records")
except ImportError as e:
print(f"Required libraries not available: {e}")
print("Skipping CAR example")
def example_jsonnet_ipld_storage():
"""Example of storing Jsonnet data in IPLD."""
print("\n=== Example: Jsonnet to IPLD Storage ===")
try:
with tempfile.TemporaryDirectory() as temp_dir:
# Create sample Jsonnet file
jsonnet_path = os.path.join(temp_dir, "sample.jsonnet")
create_sample_jsonnet(jsonnet_path)
# Serialize to IPLD
serializer = DatasetSerializer()
cid = serializer.serialize_jsonnet(
jsonnet_path,
ext_vars={
"department": "Engineering",
"multiplier": "2.2"
}
)
print(f"Serialized to IPLD with CID: {cid}")
# Deserialize from IPLD
output_jsonnet_path = os.path.join(temp_dir, "recovered.jsonnet")
recovered_path = serializer.deserialize_jsonnet(cid, output_jsonnet_path)
print(f"Deserialized to: {recovered_path}")
# Verify content
with open(recovered_path, 'r') as f:
content = f.read()
data = json.loads(content)
print(f"Recovered data with {len(data)} records")
except ImportError as e:
print(f"Required libraries not available: {e}")
print("Skipping IPLD example")
def main():
"""Run all examples."""
print("Jsonnet Conversion Examples")
print("=" * 50)
# Check if jsonnet is available
try:
import _jsonnet
has_jsonnet = True
except ImportError:
print("\nWarning: jsonnet library not available!")
print("Install it with: pip install jsonnet")
print("\nSome examples will be skipped.\n")
has_jsonnet = False
if has_jsonnet:
# Run examples
example_jsonnet_to_json()
example_jsonnet_with_variables()
example_jsonnet_to_jsonl()
example_jsonl_to_jsonnet()
example_jsonnet_to_parquet()
example_jsonnet_to_car()
example_jsonnet_ipld_storage()
print("\n" + "=" * 50)
print("All examples completed successfully!")
else:
print("Please install jsonnet to run the examples.")
if __name__ == "__main__":
main()