Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
m-wells committed Feb 14, 2020
1 parent 7610c9e commit d8b7ba1
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AlignedBinaryFormat"
uuid = "94fc9360-1a5e-4d84-93be-ddbadb32b3a7"
authors = ["m-wells <[email protected]>"]
version = "0.5.0"
version = "0.5.1"

[deps]
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
Expand Down
79 changes: 43 additions & 36 deletions README.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -85,15 +85,22 @@
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"string(AbfSerializer) = \"AbfSerializer\"\n"
]
},
{
"data": {
"text/plain": [
"AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_OArXMO>)\n",
"AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_jrx5dO>)\n",
"┌────────────┬──────────────────┬───────────┬────────┬────────────┐\n",
"│ label │ type │ shape │ bytes │ status │\n",
"├────────────┼──────────────────┼───────────┼────────┼────────────┤\n",
"│ bitmat │ BitArray{2} │ (3, 2) │ <8B> │ unloaded │\n",
"│ foo │ Foo │ (-1,) │ <118B> │ unloaded │\n",
"│ foo │ Foo │ (-1,) │ <121B> │ unloaded │\n",
"│ log │ String │ (28,) │ <112B> │ unloaded │\n",
"│ my x array │ Array{Float16,1} │ (4,) │ <8B> │ unloaded │\n",
"│ type │ DataType │ (-1,) │ <23B> │ unloaded │\n",
Expand All @@ -119,7 +126,7 @@
"\n",
"abfopen(temp, \"r+\") do abf\n",
" write(abf, \"type\", Bar)\n",
" write(abf, \"foo\", Serialized(Foo(rand(3), -1)))\n",
" write(abf, \"foo\", AbfSerializer(Foo(rand(3), -1)))\n",
"end\n",
"\n",
"abf = abfopen(temp, \"r\")"
Expand All @@ -144,16 +151,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"count(abf[\"bitmat\"]) = 2\n",
"count(abf[\"bitmat\"]) = 5\n",
"this is what I did\n",
"and how!\n",
"\n",
"AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_OArXMO>)\n",
"AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_jrx5dO>)\n",
"┌────────────┬──────────────────┬───────────┬────────┬────────────┐\n",
"│ label │ type │ shape │ bytes │ status │\n",
"├────────────┼──────────────────┼───────────┼────────┼────────────┤\n",
"│ bitmat │ BitArray{2} │ (3, 2) │ <8B> │ loaded │\n",
"│ foo │ Foo │ (-1,) │ <118B> │ unloaded │\n",
"│ foo │ Foo │ (-1,) │ <121B> │ unloaded │\n",
"│ log │ String │ (28,) │ <112B> │ loaded │\n",
"│ my x array │ Array{Float16,1} │ (4,) │ <8B> │ loaded │\n",
"│ type │ DataType │ (-1,) │ <23B> │ unloaded │\n",
Expand Down Expand Up @@ -216,42 +223,42 @@
"output_type": "stream",
"text": [
"x = 10-element Array{Float64,1}:\n",
" 0.13117086919645993\n",
" 0.8140523381099063 \n",
" 0.44152928499795685\n",
" 0.6776307667284676 \n",
" 0.2542185344974299 \n",
" 0.6641164907956227 \n",
" 0.6446525613063578 \n",
" 0.17166524697749908\n",
" 0.0775201232187761 \n",
" 0.6548927372122209 \n",
" 0.41808342392430364\n",
" 0.8340965780634206 \n",
" 0.4422525925297145 \n",
" 0.7464876087842214 \n",
" 0.8760053500303144 \n",
" 0.8812312947613306 \n",
" 0.12907825453926614\n",
" 0.8227926275555029 \n",
" 0.5770610771209626 \n",
" 0.2427376128034029 \n",
"\n",
"AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_OArXMO>)\n",
"AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_jrx5dO>)\n",
"┌───────┬──────────────────┬───────┬───────┬────────┐\n",
"│ label │ type │ shape │ bytes │ status │\n",
"├───────┼──────────────────┼───────┼───────┼────────┤\n",
"│ x │ Array{Float64,1} │ (10,) │ <80B> │ loaded │\n",
"└───────┴──────────────────┴───────┴───────┴────────┘\n",
"\n",
"\n",
"AlignedBinaryFormat.AbfFile([closed] <file /tmp/jl_OArXMO>)\n",
"AlignedBinaryFormat.AbfFile([closed] <file /tmp/jl_jrx5dO>)\n",
"┌───────┬──────┬───────┬───────┬────────┐\n",
"│ label │ type │ shape │ bytes │ status │\n",
"├───────┼──────┼───────┼───────┼────────┤\n",
"└───────┴──────┴───────┴───────┴────────┘\n",
"\n",
"x = 10-element Array{Float64,1}:\n",
" 0.13117086919645993\n",
" 0.8140523381099063 \n",
" 0.44152928499795685\n",
" 0.6776307667284676 \n",
" 0.2542185344974299 \n",
" 0.6641164907956227 \n",
" 0.6446525613063578 \n",
" 0.17166524697749908\n",
" 0.0775201232187761 \n",
" 0.6548927372122209 \n",
" 0.41808342392430364\n",
" 0.8340965780634206 \n",
" 0.4422525925297145 \n",
" 0.7464876087842214 \n",
" 0.8760053500303144 \n",
" 0.8812312947613306 \n",
" 0.12907825453926614\n",
" 0.8227926275555029 \n",
" 0.5770610771209626 \n",
" 0.2427376128034029 \n",
"\n"
]
}
Expand Down Expand Up @@ -302,27 +309,27 @@
"output_type": "stream",
"text": [
"file opened with \"w\"\n",
"AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_OArXMO>))\n",
"AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_jrx5dO>))\n",
"\n",
"file opened with \"w+\"\n",
"x[1] = 0.8475538319109697\n",
"x[1] = 0.9897101065968341\n",
"x[1] = -1.0\n",
"\n",
"file opened with \"r+\"\n",
"(abf[\"x\"])[1] = 3.0\n",
"abf[\"y\"] = [0.5593607069994619, 0.9602697380386047]\n",
"abf[\"y\"] = [0.8188509909345121, 0.7005578254412208]\n",
"\n",
"file opened with \"r\"\n",
"x[1] = 3.0\n",
"abf[\"y\"] = [0.5593607069994619, 0.9602697380386047]\n",
"abf[\"y\"] = [0.8188509909345121, 0.7005578254412208]\n",
"ReadOnlyMemoryError()\n",
"\n",
"file opened with \"a\"\n",
"AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_OArXMO>))\n",
"AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_jrx5dO>))\n",
"\n",
"file opened with \"a+\"\n",
"(read(abf, \"x\"))[1] = 3.0\n",
"(read(abf, \"z\"))[1] = 0.14952197365579378\n"
"(read(abf, \"z\"))[1] = 0.07216051054053385\n"
]
}
],
Expand Down Expand Up @@ -468,7 +475,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_OArXMO>)\n",
"AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_jrx5dO>)\n",
"┌───────────┬──────────────────┬─────────┬────────┬────────────┐\n",
"│ label │ type │ shape │ bytes │ status │\n",
"├───────────┼──────────────────┼─────────┼────────┼────────────┤\n",
Expand Down
73 changes: 38 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,24 @@ end

abfopen(temp, "r+") do abf
write(abf, "type", Bar)
write(abf, "foo", Serialized(Foo(rand(3), -1)))
write(abf, "foo", AbfSerializer(Foo(rand(3), -1)))
end

abf = abfopen(temp, "r")
```

string(AbfSerializer) = "AbfSerializer"



AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_OArXMO>)


AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_jrx5dO>)
┌────────────┬──────────────────┬───────────┬────────┬────────────┐
│ label │ type │ shape │ bytes │ status │
├────────────┼──────────────────┼───────────┼────────┼────────────┤
│ bitmat │ BitArray{2} │ (3, 2) │ <8B> │ unloaded │
│ foo │ Foo │ (-1,) │ <118B> │ unloaded │
│ foo │ Foo │ (-1,) │ <121B> │ unloaded │
│ log │ String │ (28,) │ <112B> │ unloaded │
│ my x array │ Array{Float16,1} │ (4,) │ <8B> │ unloaded │
│ type │ DataType │ (-1,) │ <23B> │ unloaded │
Expand All @@ -97,16 +100,16 @@ println(read(abf, "log"))
show(abf)
```

count(abf["bitmat"]) = 2
count(abf["bitmat"]) = 5
this is what I did
and how!

AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_OArXMO>)
AlignedBinaryFormat.AbfFile([read] <file /tmp/jl_jrx5dO>)
┌────────────┬──────────────────┬───────────┬────────┬────────────┐
│ label │ type │ shape │ bytes │ status │
├────────────┼──────────────────┼───────────┼────────┼────────────┤
│ bitmat │ BitArray{2} │ (3, 2) │ <8B> │ loaded │
│ foo │ Foo │ (-1,) │ <118B> │ unloaded │
│ foo │ Foo │ (-1,) │ <121B> │ unloaded │
│ log │ String │ (28,) │ <112B> │ loaded │
│ my x array │ Array{Float16,1} │ (4,) │ <8B> │ loaded │
│ type │ DataType │ (-1,) │ <23B> │ unloaded │
Expand Down Expand Up @@ -150,42 +153,42 @@ println("\n")
```

x = 10-element Array{Float64,1}:
0.13117086919645993
0.8140523381099063
0.44152928499795685
0.6776307667284676
0.2542185344974299
0.6641164907956227
0.6446525613063578
0.17166524697749908
0.0775201232187761
0.6548927372122209
0.41808342392430364
0.8340965780634206
0.4422525925297145
0.7464876087842214
0.8760053500303144
0.8812312947613306
0.12907825453926614
0.8227926275555029
0.5770610771209626
0.2427376128034029

AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_OArXMO>)
AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_jrx5dO>)
┌───────┬──────────────────┬───────┬───────┬────────┐
│ label │ type │ shape │ bytes │ status │
├───────┼──────────────────┼───────┼───────┼────────┤
│ x │ Array{Float64,1} │ (10,) │ <80B> │ loaded │
└───────┴──────────────────┴───────┴───────┴────────┘


AlignedBinaryFormat.AbfFile([closed] <file /tmp/jl_OArXMO>)
AlignedBinaryFormat.AbfFile([closed] <file /tmp/jl_jrx5dO>)
┌───────┬──────┬───────┬───────┬────────┐
│ label │ type │ shape │ bytes │ status │
├───────┼──────┼───────┼───────┼────────┤
└───────┴──────┴───────┴───────┴────────┘

x = 10-element Array{Float64,1}:
0.13117086919645993
0.8140523381099063
0.44152928499795685
0.6776307667284676
0.2542185344974299
0.6641164907956227
0.6446525613063578
0.17166524697749908
0.0775201232187761
0.6548927372122209
0.41808342392430364
0.8340965780634206
0.4422525925297145
0.7464876087842214
0.8760053500303144
0.8812312947613306
0.12907825453926614
0.8227926275555029
0.5770610771209626
0.2427376128034029



Expand Down Expand Up @@ -258,27 +261,27 @@ end;
```

file opened with "w"
AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_OArXMO>))
AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_jrx5dO>))

file opened with "w+"
x[1] = 0.8475538319109697
x[1] = 0.9897101065968341
x[1] = -1.0

file opened with "r+"
(abf["x"])[1] = 3.0
abf["y"] = [0.5593607069994619, 0.9602697380386047]
abf["y"] = [0.8188509909345121, 0.7005578254412208]

file opened with "r"
x[1] = 3.0
abf["y"] = [0.5593607069994619, 0.9602697380386047]
abf["y"] = [0.8188509909345121, 0.7005578254412208]
ReadOnlyMemoryError()

file opened with "a"
AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_OArXMO>))
AlignedBinaryFormat.ReadOnlyError(IOStream(<file /tmp/jl_jrx5dO>))

file opened with "a+"
(read(abf, "x"))[1] = 3.0
(read(abf, "z"))[1] = 0.14952197365579378
(read(abf, "z"))[1] = 0.07216051054053385


## Why not use `JLD/HDF5`?
Expand Down Expand Up @@ -345,7 +348,7 @@ abfopen(temp, "w+") do abf
end;
```

AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_OArXMO>)
AlignedBinaryFormat.AbfFile([read/write] <file /tmp/jl_jrx5dO>)
┌───────────┬──────────────────┬─────────┬────────┬────────────┐
│ label │ type │ shape │ bytes │ status │
├───────────┼──────────────────┼─────────┼────────┼────────────┤
Expand Down
20 changes: 10 additions & 10 deletions src/AlignedBinaryFormat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ using Printf
using Mmap
using Serialization

export abfopen, Serialized
export abfopen, AbfSerializer

#---------------------------------------------------------------------------------------------------

struct ReadOnlyError <: Exception
struct AbfReadError <: Exception
io::IOStream
end

function Base.showerror(io::IO, e::ReadOnlyError)
print(io, "isreadable(", e.io.name, ") = ", isreadable(io))
function Base.showerror(io::IO, e::T) where T<:AbfReadError
print(io, T, ": isreadable(", e.io.name, ") = ", isreadable(e.io))
end

check_readable(io::IOStream) = isreadable(io) || throw(ReadOnlyError(io))
check_readable(io::IOStream) = isreadable(io) ? nothing : throw(AbfReadError(io))

struct WriteOnlyError <: Exception
struct AbfWriteError <: Exception
io::IOStream
end

function Base.showerror(io::IO, e::WriteOnlyError)
print(io, "iswritable(", e.io.name, ") = ", iswritable(io))
end
#function Base.showerror(io::IO, e::AbfWriteError)
# print(io, "iswritable(", e.io.name, ") = ", iswritable(e.io))
#end

check_writable(io::IOStream) = iswritable(io) || throw(WriteOnlyError(io))
check_writable(io::IOStream) = iswritable(io) ? nothing : throw(AbfWriteError(io))

#---------------------------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion src/abffile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function Base.read(abf::AbfFile, k::String)
x = read_str(abf.io)
elseif abfkey.T == DataType
x = deserialize(abf.io)
elseif isa(abfkey.T, Deserialized)
elseif isa(abfkey.T, AbfDeserializer)
x = getfield(deserialize(abf.io), :x)
else
x = Mmap.mmap(abf.io, abfkey.T, abfkey.shape)
Expand Down
Loading

0 comments on commit d8b7ba1

Please sign in to comment.