nhpip

nhpip

Writing a term (nested structs) in human-readable form to a text file - is there a nicer way to do this?

Hi,

I want to write a term (nested structs) in human-readable form to a text file. This is what I’ve got, there must be a better way…

 i_am_a_term_now_a_string  = :io_lib.format("~p.", [i_am_a_term]) 
                                          |> List.flatten()   
                                          |> to_string()
File.write!("/tmp/a_term_on_your_disk", i_am_a_term_now_a_string)

Thanks

Most Liked

c4710n

c4710n

If you think quoted expression is human-readable. Try to use:

  • quote
  • Code.eval_quoted

Or, just use :erlang.term_to_binary and :erlang.binary_to_term. When you want to inspect it, use something like following code instead of reading it directly.

File.read!("path/to/file")
|> :erlang.binary_to_term()
|> IO.inspect()

I think it is better to tell us your scenario. It’s an XY problem probably.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

A bit of info will be helpful. The way to handle (most) terms in a human readable way is to use inspect to dump the data to disk, and then Code.eval_string to turn it back into a value. However this has a variety of trade offs and won’t handle arbitrary terms because things like pid values for example don’t have an output from inspect that will turn back into a pid when evaled. Whether that matters or not depends on your use case.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @nhpip did you try inspect/1 ?

i_am_a_term_now_a_string = inspect(term, limit: :infinity)
derek-zhou

derek-zhou

I’d use ~tp.~n so there is unicode characters. and a newline between terms. also you don’t need to flatten the iostring prior to write.

Most Erlang config files are written like this and read back by consult. It is very nice and simple. Or you can use other serialization format like json, or toml but they don’t support all erlang types.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@nhpip Can you explain what this does and what you’re trying to do? Are you trying to turn that term back into an Elixir value?

Where Next?

Popular in Questions Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement