Skip to content

Commit

Permalink
fixed typos in README
Browse files Browse the repository at this point in the history
  • Loading branch information
kaliv0 committed Nov 29, 2024
1 parent 13602e7 commit 20bfa6c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ from pyrio import Item
["230::xza", "110::abba", "30::a"]
```
- querying <i>csv</i> and <i>tsv</i> files
<br>(each row is read as a dict with keys taken from the header row)
<br>(each row is read as a dict with keys taken from the header)
```python
FileStream("path/to/file").map(lambda x: f"fizz: {x['fizz']}, buzz: {x['buzz']}").to_tuple()
```
Expand All @@ -396,15 +396,15 @@ FileStream("path/to/file").map(itemgetter('fizz', 'buzz')).to_tuple()
```shell
(('42', '45'), ('aaa', 'bbb'))
```
you could query the nested dicts by creating streams out of them
You could query the nested dicts by creating streams out of them
```python
(FileStream("path/to/file")
.map(lambda x: (Stream(x).to_dict(lambda y: Item(y.key, y.value or "Unknown"))))
.save())
```
- reading a file with <i>process</i>
<br>pass extra <i>f_open_options</i> (for the underlying <i>open file</i> function)
<br> and <i>f_read_options</i> (to be passed to the corresponding library function that is loading the file content e.g. tomllib, json)
- reading a file with <i>process()</i> method
- use extra <i>f_open_options</i> (for the underlying <i>open file</i> function)
- <i>f_read_options</i> (to be passed to the corresponding library function that is loading the file content e.g. tomllib, json)
```python
from decimal import Decimal

Expand All @@ -417,7 +417,7 @@ from decimal import Decimal
```shell
['foo', True, Decimal('1.22'), Decimal('5.456367654369698986')]
```
to include the <i>root</i> tag when loading an <i>.xml</i> file pass <i>'include_root=True'</i>
To include the <i>root</i> tag when loading an <i>.xml</i> file pass <i>'include_root=True'</i>
```python
FileStream.process("path/to/custom_root.xml", include_root=True).map(
lambda x: f"root={x.key}: inner_records={str(x.value)}"
Expand All @@ -428,24 +428,24 @@ FileStream.process("path/to/custom_root.xml", include_root=True).map(
```
--------------------------------------------
- saving to a file
<br>save the contents of a FileStream by passing a <i>file_path</i> to the <i>save()</i> method
<br>(write the contents of a FileStream by passing a <i>file_path</i> to the <i>save()</i> method)
```python
in_memory_dict = Stream(json_dict).filter(lambda x: len(x.key) < 6).to_tuple()
FileStream("path/to/file.json").prepend(in_memory_dict).save("./tests/resources/updated.json")
```
if no path is given, the source file for the FileStream will be updated
If no path is given, the source file for the FileStream will be updated
```python
FileStream("path/to/file.json").concat(in_memory_dict).save()
```
NB: if while updating the file something goes wrong, the original content will be restored/preserved
- handle null values
pass null_handler function to replace null values
<br>(pass <i>null_handler</i> function to replace null values)
```python
FileStream("path/to/test.toml").save(null_handler=lambda x: Item(x.key, x.value or "N/A"))
```
(especially useful for writing <i>.toml</i> files which don't allow None values)
NB: useful for writing <i>.toml</i> files which don't allow None values
- passing advanced <i>file open</i> and <i>write</i> options
similar to the <i>process</i> method you could provide
<br>similarly to the <i>process</i> method you could provide
- <i>f_open_options</i> (for the underlying <i>open</i> function)
- <i>f_write_options</i> (passed to the corresponding library that will 'dump' the contents of the stream e.g. tomli-w, pyyaml)
```python
Expand All @@ -455,7 +455,7 @@ FileStream("path/to/file.json").concat(in_memory_dict).save(
f_write_options={"indent": 4},
)
```
to add <i>custom root</i> tag when saving an <i>.xml</i> file pass <i>'xml_root="my-custom-root"'</i>
To add <i>custom root</i> tag when saving an <i>.xml</i> file pass <i>'xml_root="my-custom-root"'</i>
```python
FileStream("path/to/file.json").concat(in_memory_dict).save(
file_path="path/to/custom.xml",
Expand Down Expand Up @@ -485,4 +485,7 @@ FileStream("path/to/file.json").concat(in_memory_dict).save(
.save("path/to/third/file.tsv")
)
```
or how hideous can it get?
or how hideous can it get?
<p align="center">
<img src="https://github.com/kaliv0/pyrio/blob/main/assets/Chubby.jpg?raw=true" width="400" alt="Chubby">
</p>
Binary file added assets/Chubby.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[tool.poetry]
name = "pyrio"
version = "1.3.1"
version = "1.3.0"
authors = ["kaliv0 <[email protected]>"]
readme = "README.md"
description = "Functional-style Streams library for processing collections and querying files (json, toml, yaml, xml, csv, tsv) - including creating and updating files. Provides out-of-the-box integration with itertools."
description = "Functional-style Streams library for processing collections. Supports querying files (json, toml, yaml, xml, csv, tsv) - as well as creating and updating them. Provides easy integration with itertools"
keywords = ["stream", "functional", "processing", "collections", "fluent API", "file processing", "queries"]
repository = "https://github.com/kaliv0/pyrio"

Expand Down
8 changes: 5 additions & 3 deletions pyrio/streams/file_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __new__(cls, file_path, f_open_options=None, f_read_options=None, **kwargs):

@classmethod
def process(cls, file_path, f_open_options=None, f_read_options=None, **kwargs):
"""Creates Stream from a file with advanced 'reading' options passed by the user"""
return cls.__new__(cls, file_path, f_open_options, f_read_options, **kwargs)

# ### reading from file ###
Expand Down Expand Up @@ -119,6 +120,7 @@ def _read_generic(path, f_open_options=None, f_read_options=None, **kwargs):
def save(
self, file_path=None, null_handler=None, f_open_options=None, f_write_options=None, **kwargs
):
"""Writes Stream to a new file (or updates an existing one) with advanced 'writing' options passed by the user"""
path, tmp_path = self._prepare_file_paths(file_path)
if path.suffix in {".csv", ".tsv"}:
return self._write_csv(
Expand All @@ -141,7 +143,7 @@ def _write_csv(
f_write_options = {}
f_write_options["delimiter"] = "\t" if path.suffix == ".tsv" else ","
f_write_options["fieldnames"] = output[0].keys() if output else ()
with self.atomic_write(path, tmp_path, "w", f_open_options) as f:
with self._atomic_write(path, tmp_path, "w", f_open_options) as f:
writer = csv.DictWriter(f, **f_write_options)
writer.writeheader()
writer.writerows(output)
Expand All @@ -165,7 +167,7 @@ def _write_generic(
f_write_options["pretty"] = True

dump = getattr(importlib.import_module(config["import_mod"]), config["callable"])
with self.atomic_write(path, tmp_path, config["write_mode"], f_open_options) as f:
with self._atomic_write(path, tmp_path, config["write_mode"], f_open_options) as f:
dump(output, f, **(f_write_options or {}))

# ### helpers ###
Expand All @@ -188,7 +190,7 @@ def _prepare_file_paths(self, file_path):
return path, tmp_path

@contextmanager
def atomic_write(self, path, tmp_path, mode="w", f_open_options=None):
def _atomic_write(self, path, tmp_path, mode="w", f_open_options=None):
try:
with open(tmp_path, mode, **(f_open_options or {})) as f:
yield f
Expand Down

0 comments on commit 20bfa6c

Please sign in to comment.