Package: libhxl
libhxl-pythonPython support library for the Humanitarian Exchange Language (HXL)
data standard.  The library requires Python 3 (versions prior to 4.6
also supported Python 2.7).API docs:https://hxlstandard.github.io/libhxl-python/(and in thedocs/folder)HXL standard:http://hxlstandard.orgQuick startFrom the command line (or inside a Python3 virtual environment):$ pip3 install libhxlIn your code:import hxl

url = "https://github.com/HXLStandard/libhxl-python/blob/main/tests/files/test_io/input-valid.csv"

data = hxl.data(url).with_rows("#sector=WASH").sort("#country")

for line in data.gen_csv():
    print(line)UsageReading from a data sourceThehxl.data()function reads HXL from a file object, filename, URL,
or list of arrays and makes it available for processing, much like$()in JQuery. The following will read HXLated data from standard input:import sys
import hxl

dataset = hxl.data(sys.stdin)Most commonly, you will open a dataset via a URL:dataset = hxl.data("https://example.org/dataset.url"To open a local file rather than a URL, use theallow_localproperty
of theInputOptionsclass:dataset = hxl.data("dataset.xlsx", hxl.InputOptions(allow_local=True))Input cachinglibhxl uses the Pythonrequestslibrary for
opening URLs. If you want to enable caching (for example, to avoid
beating up on your source with repeated requests), your code can use
therequests_cacheplugin, like this:import requests_cache
requests_cache.install_cache('demo_cache', expire_after=3600)The default caching backend is a sqlite database at the location specied.Filter chainsYou can filters to transform the output, and chain them as
needed. Transformation is lazy, and uses the minimum memory
possible. For example, this command selects only data rows where the
country is "Somalia", sorted by the organisation:transformed = hxl.data(url).with_rows("#country=Somalia").sort("#org")For more on filters see the API documentation for thehxl.model.Datasetclass and thehxl.filtersmodule.GeneratorsGenerators allow the re-serialising of HXL data, returning something that works like an iterator.  Example:for line in hxl.data(url).gen_csv():
    print(line)The following generators are available (you can use the parameters to turn the text headers and HXL tags on or off):Generator methodDescriptiongen_raw()Generate arrays of strings, one row at a time.gen_csv()Generate encoded CSV rows, one row at a time.gen_json()Generate JSON output, either as rows or as JSON objects with the HXL hashtags as property names.ValidationTo validate a HXL dataset against a schema (also in HXL), use thevalidate()method at the end of the filter chain:is_valid = hxl.data(url).validate('my-schema.csv')If you don't specify a schema, the library will use a simple, built-in schema:is_valid = hxl.data(url).validate()If you include a callback, you can collect details about the errors and warnings:def my_callback(error_info):
    ## error_info is a HXLValidationException
    sys.stderr.write(error_info)

is_valid = hxl.data(url).validate(schema='my-schema.csv', callback=my_callback)For more information on validation, see the API documentation for thehxl.validationmodule and the format documentation forHXL
schemas.Command-line scriptsThe filters are also available as command-line scripts, installed with
the library. For example,$ hxlcount -t country dataset.csvWill perform the same action asimport hxl

hxl.data("dataset.csv", hxl.InputOptions(allow_local=True)).count("country").gen_csv()See the API documentation for thehxl.scriptsmodule for more information about the command-line scripts
available. All scripts have an-hoption that gives usage
information.InstallationThis repository includes a standard Pythonsetup.pyscript for
installing the library and scripts (applications) on your system. In a
Unix-like operating system, you can install using the following
command:python setup.py installIf you don't need to install from source, try simplypip install libhxlOnce you've installed, you will be able to include the HXL libraries
from any Python application, and will be able to call scripts likehxlvalidatefrom the command line.MakefileThere is also a generic Makefile that automates many tasks, including
setting up a Python virtual environment for testing. The Python3 venv
module is required for most of the targets.make build-venvSet up a local Python virtual environment for testing, if it doesn't
already exist. Will recreate the virtual environment if setup.py has
changed.make testSet up a virtual environment (if missing) and run all the unit testsmake test-installTest a clean installation to verify there are no missing dependencies,
etc.Licenselibhxl-python is released into the Public Domain, and comes with NO
WARRANTY. SeeLICENSE.mdfor details.