adding tiny resume function, for csv reading iterator
Project description
# cssresumable
Adding tiny resume function for your csv reading iterator.
## examples
00main.py
```python
import sys
import os
import random
from io import StringIO
from csvresumable import DictReader
data = """\
id,name
1,foo
2,bar
3,boo
"""
random.seed(int(os.environ.get("SEED", "0")))
for row in DictReader(StringIO(data)):
n = random.random()
if n < 0.5:
print("fail {}".format(n), file=sys.stderr)
sys.exit(0)
print("\t", row["name"])
print("finished")
```
resuming until success.
```console
SEED=0 python 00main.py
fail 0.420571580830845
foo
bar
SEED=1 RESUME=1 python 00main.py
fail 0.13436424411240122
SEED=2 RESUME=1 python 00main.py
boo
finished
SEED=5 python 00main.py
foo
bar
boo
finished
```
or setting with `resume` option (run this script, such as `python 01main.py --resume`)
```diff
--- 00main.py 2018-06-16 04:19:51.573578443 +0900
+++ 01main.py 2018-06-16 04:20:04.760554103 +0900
@@ -4,17 +4,23 @@
from io import StringIO
from csvresumable import DictReader
-data = """\
+data = """
id,name
1,foo
2,bar
3,boo
"""
+import argparse
+parser = argparse.ArgumentParser()
+parser.add_argument("--seed", default=0, type=int)
+parser.add_argument("--resume", action="store_true")
+args = parser.parse_args()
+
random.seed(int(os.environ.get("SEED", "0")))
-for row in DictReader(StringIO(data)):
+for row in DictReader(StringIO(data), resume=args.resume):
n = random.random()
- if n < 0.5:
+ if n > 0.8:
print("fail {}".format(n), file=sys.stderr)
sys.exit(0)
print("\t", row["name"])
```
### with multi files
```python
from csvresumable import DictReader
files = ["a.csv", "b.csv"]
for f in files:
r = DictReader(f)
for row in r:
do_something(row)
```
## more
- [examples/01multisource](examples/01multisource)
Adding tiny resume function for your csv reading iterator.
## examples
00main.py
```python
import sys
import os
import random
from io import StringIO
from csvresumable import DictReader
data = """\
id,name
1,foo
2,bar
3,boo
"""
random.seed(int(os.environ.get("SEED", "0")))
for row in DictReader(StringIO(data)):
n = random.random()
if n < 0.5:
print("fail {}".format(n), file=sys.stderr)
sys.exit(0)
print("\t", row["name"])
print("finished")
```
resuming until success.
```console
SEED=0 python 00main.py
fail 0.420571580830845
foo
bar
SEED=1 RESUME=1 python 00main.py
fail 0.13436424411240122
SEED=2 RESUME=1 python 00main.py
boo
finished
SEED=5 python 00main.py
foo
bar
boo
finished
```
or setting with `resume` option (run this script, such as `python 01main.py --resume`)
```diff
--- 00main.py 2018-06-16 04:19:51.573578443 +0900
+++ 01main.py 2018-06-16 04:20:04.760554103 +0900
@@ -4,17 +4,23 @@
from io import StringIO
from csvresumable import DictReader
-data = """\
+data = """
id,name
1,foo
2,bar
3,boo
"""
+import argparse
+parser = argparse.ArgumentParser()
+parser.add_argument("--seed", default=0, type=int)
+parser.add_argument("--resume", action="store_true")
+args = parser.parse_args()
+
random.seed(int(os.environ.get("SEED", "0")))
-for row in DictReader(StringIO(data)):
+for row in DictReader(StringIO(data), resume=args.resume):
n = random.random()
- if n < 0.5:
+ if n > 0.8:
print("fail {}".format(n), file=sys.stderr)
sys.exit(0)
print("\t", row["name"])
```
### with multi files
```python
from csvresumable import DictReader
files = ["a.csv", "b.csv"]
for f in files:
r = DictReader(f)
for row in r:
do_something(row)
```
## more
- [examples/01multisource](examples/01multisource)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
csvresumable-0.0.2.tar.gz
(4.8 kB
view hashes)
Built Distribution
Close
Hashes for csvresumable-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 212b34b81b5c3d35ef9c954fcb0aff0b60784838a12f195f8222e98580214a30 |
|
MD5 | d63fdf9e78396840ae7614c9aafb321c |
|
BLAKE2b-256 | 6d6a63b84655dc5f26f43ae5f98e9791ae3c20ed28c8b4463257b56090718923 |