こちらの記事の続きです。
ChatGPT で elixir を学ぶ (その6)text ファイルの update
辞書に変換してから処理を行います。
Python のプログラム
text_delete.py
#! /usr/bin/python
#
# delete/text_delete.py
#
# Mar/07/2024
import sys
#
# ---------------------------------------------------------------
def dict_delete_proc(dict_in,key):
if key in dict_in:
del dict_in[key]
#
return dict_in
#
# ---------------------------------------------------------------
def text_write_proc(file_out,dict_aa):
#
fp_out = open(file_out,mode='w', encoding='utf-8')
for key in dict_aa.keys():
unit = dict_aa[key]
str_out = key + "\t" + str(unit['name']) + "\t"
str_out += "%d\t" % unit['population']
str_out += unit['date_mod'] + "\n"
fp_out.write(str_out)
fp_out.close()
# ---------------------------------------------------------------
def text_read_proc(file_in):
#
fp_in = open(file_in,encoding='utf-8')
lines = fp_in.readlines()
fp_in.close()
#
dict_aa = {}
for line in lines:
if (5 < len(line)):
cols= line[:-1].split('\t')
if (3 < len(cols)):
try:
if (cols[0][0] == "t"):
dict_unit = {'name': cols[1], \
'population':int (cols[2]),'date_mod':cols[3]}
dict_aa[cols[0]] = dict_unit
except:
sys.stderr.write \
("*** error *** %s ***\n" % file_in)
sys.stderr.write \
("*** %s ***\n" % line)
#
return dict_aa
# ---------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_in = sys.argv[1]
id_in = sys.argv[2]
print("%s" % id_in)
dict_aa = text_read_proc(file_in)
dict_bb = dict_delete_proc(dict_aa,id_in)
text_write_proc(file_in,dict_bb)
#
sys.stderr.write("*** 終了 ***\n")
# ---------------------------------------------------------------
実行結果
$ ./text_delete.py cities.txt t2384
*** 開始 ***
t2384
*** 終了 ***
ChatGPT が作成したプログラム
dch01.exs
defmodule TextDelete do
defmodule Functions do
def dict_delete_proc(dict_in, key) do
Map.delete(dict_in, key)
end
def text_write_proc(file_out, dict_aa) do
File.write!(file_out, Enum.map(dict_aa, fn {key, unit} ->
str_out = "#{key}\t#{unit["name"]}\t"
str_out <> "#{unit["population"]}\t#{unit["date_mod"]}\n"
end) |> Enum.join())
end
def text_read_proc(file_in) do
File.read!(file_in)
|> String.split("\n", trim: true)
|> Enum.reduce(%{}, fn line, dict_aa ->
cols = String.split(line, "\t")
if length(cols) > 3 && String.starts_with?(hd(cols), "t") do
try do
dict_unit = %{
"name" => List.first(Enum.drop(cols, 1)),
"population" => String.to_integer(Enum.at(cols, 2)),
"date_mod" => Enum.at(cols, 3)
}
Map.put(dict_aa, hd(cols), dict_unit)
rescue
_ ->
IO.stderr |> IO.puts("*** error *** #{file_in} ***")
IO.stderr |> IO.puts("*** #{line} ***")
dict_aa
end
else
dict_aa
end
end)
end
end
def run([file_in, id_in]) do
IO.puts("#{id_in}")
dict_aa = Functions.text_read_proc(file_in)
dict_bb = Functions.dict_delete_proc(dict_aa, id_in)
Functions.text_write_proc(file_in, dict_bb)
end
end
TextDelete.run(System.argv())
実行結果
$ elixir dch01.exs cities.txt t2386
t2386
warning: IO.stderr/0 is undefined or private
Invalid call found at 2 locations:
dch01.exs:29: TextDelete.Functions.text_read_proc/1
dch01.exs:30: TextDelete.Functions.text_read_proc/1
Gemini が作成したプログラム
dge01.exs
defmodule TextDelete do
# Function to delete an entry from a dictionary based on key
def dict_delete_proc(dict_in, key) do
Map.delete(dict_in, key)
end
# Function to write a dictionary to a text file (same as text_write_proc in previous example)
def text_write_proc(file_out, dict_aa) do
File.open!(file_out, [:write, :encode => "utf-8"]) |>
Enum.each(dict_aa, fn {key, unit} ->
str_out = "#{key}\t#{unit["name"]}\t#{unit["population"]}\t#{unit["date_mod"]}\n"
IO.write(str_out)
end) |>
File.close()
end
# Function to read a text file into a dictionary (same as text_read_proc in previous example)
def text_read_proc(file_in) do
File.stream!(file_in, [:read, :encode => "utf-8"]) |>
Enum.reduce(%{}, fn line, dict ->
if String.length(line) > 5 do
[_, name, population_str, date_mod] = String.split(line, "\t", trim: true)
try do
population = Integer.parse!(population_str)
if String.starts_with?(key, "t") do
dict = Map.put(dict, key, %{
name: name,
population: population,
date_mod: date_mod
})
end
dict
rescue
IO.stderr(:write, "*** error *** #{file_in} ***\n")
IO.stderr(:write, "*** #{line} ***\n")
dict
end
else
dict
end
end)
end
def main(args) do
IO.stderr(:write, "*** 開始 ***\n")
[file_in, id_in] = args
IO.puts(id_in)
dict_aa = text_read_proc(file_in)
dict_bb = dict_delete_proc(dict_aa, id_in)
text_write_proc(file_in, dict_bb)
IO.stderr(:write, "*** 終了 ***\n")
end
end
TextDelete.main(Application.get_argument(:script, []))
実行結果
$ elixir dge01.exs cities.txt t2388
** (SyntaxError) dge01.exs:10:43: syntax error before: '=>'
|
10 | File.open!(file_out, [:write, :encode => "utf-8"]) |>
| ^
(elixir 1.14.0) lib/code.ex:1245: Code.require_file/2
私が作成したプログラム
text_delete.exs
# ------------------------------------------------------------------
#
# text_delete.exs
#
# Mar/05/2024
# ------------------------------------------------------------------
defmodule MyModule do
# ------------------------------------------------------------------
def dict_delete_proc(dict_in, key) do
Map.delete(dict_in, key)
end
# ------------------------------------------------------------------
def text_read_proc(file_in) do
{:ok, file} = File.open(file_in, [:read, :utf8])
contents = IO.read(file,:eof)
File.close(file)
lines = String.split(contents,["\n"])
# IO.puts(length(lines))
lines_bb = Enum.filter(lines, fn x -> cols = String.split(x, "\t", trim: true)
3 < length(cols) end)
list_aa = Enum.map(lines_bb, fn x -> cols = String.split(x, "\t", trim: true)
[key,name,population_str,date_mod] = cols
population = String.to_integer(population_str)
dict_unit = %{name: name,population: population, date_mod: date_mod}
[key, dict_unit]
end)
Enum.reduce(list_aa, %{}, fn [key, value], acc ->
Map.put(acc, key, value)
end)
end
# ------------------------------------------------------------------
def text_write_proc(file_out,dict_aa) do
{:ok, fp_out} = File.open(file_out, [:write, :utf8])
ll = Map.keys dict_aa
Enum.each(ll, fn(s) ->
str_out = to_string(s) <> "\t" <> dict_aa[s].name <> "\t" <> Integer.to_string(dict_aa[s].population) <> "\t" <> dict_aa[s].date_mod <> "\n"
IO.write(fp_out,str_out)
end)
File.close(fp_out)
end
# ------------------------------------------------------------------
end
# ------------------------------------------------------------------
IO.puts :stderr,"*** 開始 ***"
[file_in, id_in] = System.argv()
IO.puts :stderr,file_in
IO.puts("#{id_in}")
dict_aa = MyModule.text_read_proc(file_in)
dict_bb = MyModule.dict_delete_proc(dict_aa, id_in)
# IO.inspect(dict_bb)
MyModule.text_write_proc(file_in, dict_bb)
IO.puts :stderr,"*** 終了 ***"
# ------------------------------------------------------------------
実行結果
$ elixir text_delete.exs cities.txt t2388
t2388
*** 開始 ***
cities.txt
*** 終了 ***