API documentation¶
Knead¶
-
class
dataknead.
Knead
(inp, parse_as=None, read_as=None, is_data=False, **kwargs)[source]¶ -
__init__
(inp, parse_as=None, read_as=None, is_data=False, **kwargs)[source]¶ Creates a new
Knead
instance.By default it is assumed that
inp
is a path to a file if it’s a string. The file extension will be used to determine the correct loader (e.g..csv
will get theCsvLoader
).Knead("cities.csv")
If
inp
is not a string it is assumed to be a data structure (e.g. a list or dict).Knead([1, 2, 3])
parse_as
is used when you’re feedingKnead
a string that is not a file path, like the results of a HTTP call. In that case you need to specify the loader yourself as a file extension (e.g.csv
,json
, etc.)json = "[1,2,3]" Knead(json, parse_as = "json")
read_as
is used when you’re giving a path to a file that doesn’t have a valid extension, and doesn’t have an extension at all. The same mechanism asparse_as
is at work here: give it a file extension.Knead("query", read_as = "csv")
is_data
is used if you want to feedKnead
a string as data, and it shouldn’t be parsed at allKnead("a line of text", is_data = True)
Any additional parameters will be passed on to the loader
Knead("cities.csv", has_header = True)
-
add_loader
(loader)[source]¶ Adds a custom loader. See extending dataknead.
-
data
(check_instance=None)[source]¶ Returns the loaded data.
Knead("cities.csv").data() # Will return the parsed contents of cities.csv
To check if the data is in a specific type use
check_instance
, which will raise an exception if it’s not correctKnead([1,2,3]).data(check_instance = dict) # Will raise an error, it's a list
-
filter
(fn)[source]¶ Applies a function on every item and removes it if it returns
False
. This only works when your data is alist
.Knead([1,10,100]).filter(lambda v: v > 9).data() # Returns 10 and 100
-
loader
¶ alias of
dataknead.loaders.xml.XmlLoader
-
map
(iteratee)[source]¶ Applies a function on every item and replaces it with the return value.
Knead([1, 4, 9]).map(lambda x:x * x).data() # [1, 16, 81]
If you have a list with dicts, you can give a string to return the values of a specific key
Knead("cities.csv").map("city").write("city-names.csv") # Is the same as Knead("cities.csv").map(lambda c:c["city"]).write("city-names.csv")
To return multiple keys with values, you can use a tuple:
Knead("cities.csv").map(("city", "country")).write("city-country-names.csv") # Is the same as Knead("cities.csv").map(lambda c:{ "city" : c["city"], "country" : c["country"] }).write("city-country-names.csv") # Or def mapcity(city): return { "city" : city["city"], "country" : city["country"] } Knead("cities.csv").map(mapcity).write("city-country-names.csv")
-
write
(path, write_as=None, **kwargs)[source]¶ Writes the data to a file. Type is implied by file extension.
Knead("cities.csv").write("cities.json")
To force the type to something else, pass the format to write_as.
Knead("cities.csv").map("city").write("cities.txt", write_as="csv")
Any additional parameters will be passed on to the loader:
Knead("cities.csv").write("cities.json", indent = 4) Knead("cities.csv").map("city").write("ciites.csv", fieldnames=["city"])
-
Loaders¶
These loaders are available by default. Note that you never need to access these directly, they are merely documented here for reference purposes and as an example if you want to write your own loader.