-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
125 lines (116 loc) · 2.63 KB
/
example.py
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
# from typing import Type
# import numpy as np
import pandas as pd
import typedpath as tp
# class Person(tp.StructDir):
# name: tp.TextFile
# config: tp.JSONFile
#
#
# class Database(tp.StructDir):
# people: tp.DictDir[str, Person]
#
#
# d = Database("database")
# d.people["alice"].name.write("Alice")
# d.people["alice"].config.write({"require_authentication": True})
# d.people["bob"].name.write("Bob")
# d.people["bob"].config.write({"require_authentication": False})
#
#
# tf = tp.TextFile("my_text.txt")
# tf.write("Hello, world!")
# print(tf.read())
#
# bf = tp.BytesFile("my_bytes.bin")
# bf.write(b"Hello, world!")
# print(bf.read())
#
#
# class Person(tp.StructDir):
# name: tp.TextFile = tp.withargs(encoding="ascii")
# config: tp.JSONFile
#
#
# p = Person("person")
# p.name.write("Eve")
#
# people = tp.DictDir("people", str, Person)
# people["eve"].name.write("Eve")
#
# configs = tp.DictDir("configs", str, tp.TextFile, value_args=tp.withargs(encoding="ascii"))
# configs["json"].write("test")
#
#
# class BoolKeyCodec(tp.KeyCodec[bool]):
# def encode(self, key: bool) -> str:
# return "True" if key else "False"
#
# def decode(self, key_str: str, key_type: Type[bool]) -> bool:
# assert issubclass(key_type, bool), key_type
# match key_str:
# case "True":
# return True
# case "False":
# return False
# raise AssertionError(f"Don't know how to interpret {key_str} as a bool")
#
#
# bools = tp.DictDir("bools", bool, tp.TextFile, key_codec=BoolKeyCodec())
#
# tp.add_key_codec(bool, BoolKeyCodec())
#
# json = tp.JSONFile("example.json")
# json.write(
# {
# "is_example": True,
# "example_names": ["alice", "bob", "eve"],
# }
# )
# print(json.read())
#
#
# class A:
# def __init__(self, value: int) -> None:
# self.value = value
#
# def talk(self) -> None:
# print(self.value)
#
#
# class MyDir(tp.StructDir):
# a: tp.PickleFile[A]
# b: tp.TextFile
#
#
# md = MyDir("my_dir")
# md.a.write(A(42))
# md.a.read().talk()
#
# pf = tp.PickleFile("a.pickle", A)
# pf.write(A(13))
# pf.read().talk()
#
#
# npy = tp.NpyFile("array.npy")
# npy.write(np.array([1, 2, 3]))
# print(npy.read())
#
# npz = tp.NpzFile("array.npz")
# npz.write(np.array([1, 2, 3]))
# print(npz.read())
df = pd.DataFrame(
{
"a": [1, 2, 3],
"b": [True, False, True],
}
)
csv = tp.PandasCsvFile("df.csv")
csv.write(df)
print(csv.read())
feather = tp.PandasFeatherFile("df.feather")
feather.write(df)
print(feather.read())
parquet = tp.PandasParquetFile("df.parquet")
parquet.write(df)
print(parquet.read())