jsonl_reader_writer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +11 -0
- data/LICENSE +21 -0
- data/README.md +69 -0
- data/Rakefile +5 -0
- data/jsonl_reader_writer.gemspec +20 -0
- data/lib/jsonl_reader_writer/reader.rb +56 -0
- data/lib/jsonl_reader_writer/writer.rb +15 -0
- data/lib/jsonl_reader_writer.rb +2 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ec02c52057118cbb7551ad488d1f93984accb2d4a08e6ed433f7cf23475bc59e
|
4
|
+
data.tar.gz: 8654deef936ac24154e1a044ecd713100aebeb734f2dc4b71b05faad51a0a5be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 901123e04f267482079665b587fc77ddf59fffa76acf4dcd791ffa53eefeef21ee740bad650b2ab1f7abf9726729fbc4ca44796aef9ad30368054b71a00d9022
|
7
|
+
data.tar.gz: c4d10dc0f1f911130c7349ec013dea3923c4f1961c95ccfa813207506dcca444ecf468f4ae4a0a22f0d6bd0c0c5a36f1d8b4625e0cfc16a2bac607befbcdaf40
|
data/Gemfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in jsonl_reader_writer.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
# Add any additional gems needed for development here.
|
7
|
+
# For example, you might use 'pry' for debugging or 'rspec' for testing.
|
8
|
+
group :development, :test do
|
9
|
+
gem 'rspec'
|
10
|
+
gem 'pry'
|
11
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 katsuya tanaka
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# JSONL Reader Writer
|
2
|
+
|
3
|
+
`jsonl_reader_writer` is a Ruby gem that provides utilities for reading, mapping, filtering, and writing JSON Lines formatted files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'jsonl_reader_writer'
|
11
|
+
```
|
12
|
+
And then execute:
|
13
|
+
```
|
14
|
+
$ bundle install
|
15
|
+
```
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
```
|
19
|
+
$ gem install jsonl_reader_writer
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
```ruby
|
24
|
+
require 'jsonl_reader_writer'
|
25
|
+
|
26
|
+
# Create a reader instance
|
27
|
+
reader = JsonlReaderWriter::Reader.new('/path/to/your/jsonl_file.jsonl')
|
28
|
+
|
29
|
+
# Read and print each line
|
30
|
+
reader.each do |data, line_number|
|
31
|
+
puts "Line #{line_number + 1}: #{data}"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Filter the data
|
35
|
+
filtered_data = reader.filter do |data|
|
36
|
+
data['some_key'] == 'some_value'
|
37
|
+
end
|
38
|
+
|
39
|
+
# Map the data
|
40
|
+
mapped_data = reader.map do |data|
|
41
|
+
data['some_key']
|
42
|
+
end
|
43
|
+
|
44
|
+
# Create a writer instance
|
45
|
+
writer = JsonlReaderWriter::Writer.new('/path/to/your/output_file.jsonl')
|
46
|
+
|
47
|
+
# Write data
|
48
|
+
writer.write({ some_key: 'some_value' })
|
49
|
+
|
50
|
+
# Write multiple records
|
51
|
+
records = [
|
52
|
+
{ record_1_key: 'record_1_value' },
|
53
|
+
{ record_2_key: 'record_2_value' },
|
54
|
+
]
|
55
|
+
writer.write(records)
|
56
|
+
|
57
|
+
# Write data with validation
|
58
|
+
writer.write({ some_key: 'some_value' }, validate: true)
|
59
|
+
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/your_username/jsonl_reader_writer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
|
67
|
+
|
68
|
+
## License
|
69
|
+
The gem is available as open source under the terms of the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "jsonl_reader_writer"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = "ka2yatanaka"
|
5
|
+
spec.email = "katwoya513@gmail.com"
|
6
|
+
|
7
|
+
spec.summary = %q{JSON Lines file reader and writer for Ruby.}
|
8
|
+
spec.description = %q{This gem provides utilities for reading, filtering, mapping, and writing JSON Lines formatted files in Ruby.}
|
9
|
+
spec.homepage = "https://github.com/ka2-ya/jsonl_reader_writer"
|
10
|
+
spec.license = "MIT"
|
11
|
+
|
12
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
13
|
+
spec.bindir = "exe"
|
14
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
|
17
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
18
|
+
|
19
|
+
spec.add_dependency "json", ">= 2.0"
|
20
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module JsonlReaderWriter
|
4
|
+
# Reader class: Provides methods to read and process .jsonl files
|
5
|
+
class Reader
|
6
|
+
# initialize method: Takes a file path as an argument, and validates that the file is a .jsonl file
|
7
|
+
def initialize(file_path)
|
8
|
+
@file_path = file_path
|
9
|
+
validate_file_extension
|
10
|
+
end
|
11
|
+
|
12
|
+
# each method: Opens the file, reads each line, parses it as JSON, and yields it along with its index
|
13
|
+
# If there's an error parsing a line, it rescues the exception and prints an error message
|
14
|
+
def each
|
15
|
+
File.open(@file_path, 'r') do |f|
|
16
|
+
f.each_line.with_index do |line, index|
|
17
|
+
begin
|
18
|
+
yield JSON.parse(line.chomp), index
|
19
|
+
rescue => e
|
20
|
+
puts "Error parsing line #{index + 1}: #{e.message}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# filter method: Iterates over each line in the file and adds it to an array if it meets the condition specified in the block
|
27
|
+
def filter(&block)
|
28
|
+
filtered_data = []
|
29
|
+
|
30
|
+
each do |data, line_number|
|
31
|
+
filtered_data << data if block.call(data)
|
32
|
+
end
|
33
|
+
|
34
|
+
filtered_data
|
35
|
+
end
|
36
|
+
|
37
|
+
# map method: Iterates over each line in the file and adds the result of the block operation to an array
|
38
|
+
def map(&block)
|
39
|
+
mapped_data = []
|
40
|
+
|
41
|
+
each do |data, line_number|
|
42
|
+
mapped_data << block.call(data)
|
43
|
+
end
|
44
|
+
|
45
|
+
mapped_data
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
# validate_file_extension method: Checks if the file is a .jsonl file. If not, it raises an ArgumentError
|
50
|
+
def validate_file_extension
|
51
|
+
unless @file_path.end_with?('.jsonl')
|
52
|
+
raise ArgumentError, 'File must be a .jsonl file'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module JsonlReaderWriter
|
4
|
+
class Writer
|
5
|
+
def initialize(file_path)
|
6
|
+
@file_path = file_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def write(data)
|
10
|
+
File.open(@file_path, 'w') do |f|
|
11
|
+
data.each { |datum| f.puts(JSON.generate(datum)) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonl_reader_writer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ka2yatanaka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: This gem provides utilities for reading, filtering, mapping, and writing
|
28
|
+
JSON Lines formatted files in Ruby.
|
29
|
+
email: katwoya513@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- jsonl_reader_writer.gemspec
|
39
|
+
- lib/jsonl_reader_writer.rb
|
40
|
+
- lib/jsonl_reader_writer/reader.rb
|
41
|
+
- lib/jsonl_reader_writer/writer.rb
|
42
|
+
homepage: https://github.com/ka2-ya/jsonl_reader_writer
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.5.0
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.3.12
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: JSON Lines file reader and writer for Ruby.
|
65
|
+
test_files: []
|