Repository: koaning/clumper

Issue:
Data Loader: .from_csv()
It'd be nice if we could also read data from disk. A syntax like this would be nice: 

```python
Clumper.from_csv(path, settings)
```

Discussion:

Patch:
diff --git a/clumper/clump.py b/clumper/clump.py
index 2fa98f1..134e204 100644
--- a/clumper/clump.py
+++ b/clumper/clump.py
@@ -1,4 +1,5 @@
 import json
+import csv
 import pathlib
 import itertools as it
 import urllib.request
@@ -112,6 +113,54 @@ class Clumper:
         except Exception:
             raise RuntimeError("Error occured during reading in JSONL file")
 
+    @classmethod
+    def read_csv(cls, path, delimiter=",", fieldnames=None, nrows=None):
+        """
+        Reads in a csv file. Can also read files from url.
+        Arguments
+        ---------
+        path : filename or url
+        delimiter: must be a single character. `,` is the default.
+        fieldnames: You may prefer a different set of keys for the data, in which case, you can supply new keys with the fieldnames.
+        By default, the first row of the csv will provide the Clumper keys if fieldnames is None. If fieldnames is provided,
+        then the first row stays as part of the data. You should ensure that the correct number of fieldnames is supplied,
+        as an incorrect number can lead to truncation of the clumper. So, if you have seven columns and your fieldnames length is 3,
+        then every row will have only 3 values, the remaining four will be cut off.
+        nrows: Number of rows to read in. Useful when reading large files. If `None`, all rows are read.
+        Usage:
+        ```python
+        from clumper import Clumper
+        clump = Clumper.read_csv("tests/monopoly.csv")
+        assert len(clump) == 22
+        clump = Clumper.read_csv("tests/monopoly.csv", nrows = 10)
+        assert len(clump) == 10
+        clump = Clumper.read_csv("https://calmcode.io/datasets/monopoly.csv")
+        assert len(clump) == 22
+        # By default, the first row of the csv is treated as the keys of the Clumper.
+        # If the fieldnames argument is not None, then the first row stays as part of the data.
+        fieldnames = ['date', 'currency', 'country', 'price', 'dollar_rate', 'cost']
+        clump = Clumper.read_csv("https://calmcode.io/datasets/bigmac.csv",
+                                # supply new fieldnames
+                                 fieldnames=fieldnames)
+        # check first row :
+        first_row = ['date', 'currency_code','name','local_price', 'dollar_ex', 'dollar_price']
+        assert clump.head(1).equals([dict(zip(fieldnames, first_row))])
+        ```
+        """
+        if path.startswith("https:") or path.startswith("http:"):
+            with urllib.request.urlopen(path) as resp:
+                if fieldnames is None:
+                    fieldnames = resp.readline().decode().strip().split(",")
+                # this section allows us to chunk the rows, if nrows is supplied
+                body = it.islice(resp, 0, nrows)
+                body = (word.decode().strip().split(",") for word in body)
+                body = it.product([fieldnames], body)
+                return Clumper([dict(zip(key, values)) for key, values in body])
+
+        with open(path, newline="") as csvfile:
+            reader = csv.DictReader(csvfile, delimiter=delimiter, fieldnames=fieldnames)
+            return Clumper(list(it.islice(reader, 0, nrows)))
+
     def _create_new(self, blob):
         """
         Creates a new collection of data while preserving settings of the


Test patch:
diff --git a/tests/monopoly.csv b/tests/monopoly.csv
new file mode 100644
index 0000000..1a5fe78
--- /dev/null
+++ b/tests/monopoly.csv
@@ -0,0 +1,23 @@
+name,rent,house_1,house_2,house_3,house_4,hotel,deed_cost,house_cost,color,tile
+Mediterranean Avenue,2,10,30,90,160,250,60,50,purple,1
+Baltic Avenue,4,20,60,180,320,450,60,50,purple,3
+Oriental Avenue,6,30,90,270,400,550,100,50,light_blue,6
+Vermont Avenue,6,30,90,270,400,550,100,50,light_blue,8
+Connecticut Avenue,8,40,100,300,450,600,120,50,light_blue,9
+St. Charles Place,10,50,150,450,625,750,140,100,pink,11
+States Avenue,10,50,150,450,625,750,140,100,pink,13
+Virginia Avenue,12,60,180,500,700,900,160,100,pink,14
+Tennessee Avenue,14,70,200,550,750,950,180,100,orange,16
+St. James Place,14,70,200,550,750,950,180,100,orange,18
+New York Avenue,16,80,220,600,800,1000,200,100,orange,19
+Kentucky Avenue,18,90,250,700,875,1050,220,150,red,21
+Indiana Avenue,18,90,250,700,875,1050,220,150,red,23
+Illinois Avenue,20,100,300,750,925,1100,240,150,red,24
+Atlantic Avenue,22,110,330,800,975,1150,260,150,yellow,26
+Ventnor Avenue,22,110,330,800,975,1150,260,150,yellow,27
+Marvin Gardens,24,120,360,850,1025,1200,280,150,yellow,29
+Pacific Avenue,26,130,390,900,1100,1275,300,200,green,31
+North Carolina Avenue,26,130,390,900,1100,1275,300,200,green,32
+Pennsylvania Avenue,28,150,450,1000,1200,1400,320,200,green,34
+Park Place,35,175,500,1100,1300,1500,350,200,blue,37
+Boardwalk,50,200,600,1400,1700,2000,400,200,blue,39
diff --git a/tests/test_read_write/test_read_csv.py b/tests/test_read_write/test_read_csv.py
new file mode 100644
index 0000000..33fddbb
--- /dev/null
+++ b/tests/test_read_write/test_read_csv.py
@@ -0,0 +1,85 @@
+import pytest
+from itertools import product
+from clumper import Clumper
+
+
+paths = ["tests/monopoly.csv", "https://calmcode.io/datasets/monopoly.csv"]
+nrows = [(None, 22), (10, 10), (15, 15), [80, 22]]
+fields = [
+    (
+        None,
+        [
+            "name",
+            "rent",
+            "house_1",
+            "house_2",
+            "house_3",
+            "house_4",
+            "hotel",
+            "deed_cost",
+            "house_cost",
+            "color",
+            "tile",
+        ],
+    ),
+    (
+        [
+            "namee",
+            "rent",
+            "house1",
+            "house2",
+            "house3",
+            "house4",
+            "hotell",
+            "deed_cost",
+            "house_cost",
+            "colour",
+            "tille",
+        ],
+        [
+            "namee",
+            "rent",
+            "house1",
+            "house2",
+            "house3",
+            "house4",
+            "hotell",
+            "deed_cost",
+            "house_cost",
+            "colour",
+            "tille",
+        ],
+    ),
+]
+
+path_nrows = [(path, nrows, length) for path, (nrows, length) in product(paths, nrows)]
+path_fields = [
+    (path, fieldnames, fields_check)
+    for path, (fieldnames, fields_check) in product(paths, fields)
+]
+
+
+@pytest.mark.parametrize("path,nrows,length", path_nrows)
+def test_read_csv(path, nrows, length):
+    "Test that the length of clumper matches the total number of rows in the csv."
+    clump = Clumper.read_csv(path=path, nrows=nrows)
+    assert len(clump) == length
+
+
+@pytest.mark.parametrize("path,fieldnames,field_check", path_fields)
+def test_fieldnames(path, fieldnames, field_check):
+    "Test that fieldnames matches keys of Clumper."
+    clump = Clumper.read_csv(path=path, fieldnames=fieldnames)
+    assert not set(field_check).difference(clump.keys())
+
+
+def test_wrong_delimiter():
+    "Test that an error is raised if a wrong delimiter is supplied."
+    with pytest.raises(TypeError):
+        Clumper.read_csv("tests/monopoly.csv", delimiter=", ")
+
+
+def test_read_csv_negative_nrows():
+    "Test that an error is raised if nrows is negative."
+    with pytest.raises(ValueError):
+        Clumper.read_csv("tests/monopoly.csv", nrows=-5)