Skip to content

Commit

Permalink
hxlquickmeta (#9): MVP of hxlquickmeta (not at least do something); w…
Browse files Browse the repository at this point in the history
…orks both for inline debug and inspection of local or remote file
  • Loading branch information
fititnt committed Feb 20, 2021
1 parent fea25b0 commit 2ab16d8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 29 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ exported from HXL (The Humanitarian Exchange Language)**
- [1.2 `HXL2` Command line tools](#12-hxl2-command-line-tools)
- [1.2.1 `hxl2example`: create your own exporter/importer](#121-hxl2example-create-your-own-exporterimporter)
- [1.2.2 `hxl2tab`: tab format, focused for compatibility with Orange Data Mining](#122-hxl2tab-tab-format-focused-for-compatibility-with-orange-data-mining)
- [1.2.3 `hxlquickimport`: (like the `hxltag`)](#123-hxlquickimport-like-the-hxltag)
- [1.2.3 `hxlquickmeta`:](#123-hxlquickmeta)
- [1.2.4 `hxlquickimport`: (like the `hxltag`)](#124-hxlquickimport-like-the-hxltag)
- [2. Reasons behind](#2-reasons-behind)
- [2.1 Why?](#21-why)
- [2.2 How?](#22-how)
Expand Down Expand Up @@ -105,7 +106,44 @@ instead of cli to generate the file. Uses [hug 🐨 🤗](https://github.com/hug

> If you want quick expose outside localhost, try [ngrok](https://ngrok.com/).
##### 1.2.3 `hxlquickimport`: (like the `hxltag`)
##### 1.2.3 `hxlquickmeta`:
- Main issue: <https://github.com/EticaAI/HXL-Data-Science-file-formats/issues/6>
- Source code: [bin/hxlquickmeta](bin/hxlquickmeta)

What it does: `hxlquickmeta` output information about a local or remote
dataset. If the file already is HXLated, it will print even more information.

**Quick examples**

```bash
#### inline result for and hashtag and (optional) value ________________________

hxlquickmeta --hxlquickmeta-hashtag="#adm2+code" --hxlquickmeta-value="BR3106200"
# > get_hashtag_info
# >> hashtag: #adm2+code
# >>> HXLMeta._parse_heading: #adm2+code
# >>> HXLMeta.is_hashtag_base_valid: None
# >>> libhxl_is_token None
# >> value: BR3106200
# >>> libhxl_is_empty False
# >>> libhxl_is_date False
# >>> libhxl_is_number False
# >>> libhxl_is_string True
# >>> libhxl_is_token None
# >>> libhxl_is_truthy False
# >>> libhxl_typeof string

#### Output information for an file, and (if any) HXLated information __________
# Local file
hxlquickmeta tests/files/iris_hxlated-csv.csv

# Remove file
hxlquickmeta https://docs.google.com/spreadsheets/u/1/d/1l7POf1WPfzgJb-ks4JM86akFSvaZOhAUWqafSJsm3Y4/edit#gid=634938833


```

##### 1.2.4 `hxlquickimport`: (like the `hxltag`)
- Main issue: <https://github.com/EticaAI/HXL-Data-Science-file-formats/issues/6>
- Source code: [bin/hxlquickimport](bin/hxlquickimport)

Expand Down
69 changes: 46 additions & 23 deletions bin/hxlquickmeta
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
# hug -f bin/hxlquickmeta
# ngrok http 8000
#
# DESCRIPTION: hxlquickmeta is ...
# DESCRIPTION: hxlquickmeta output information about a local or remote
# dataset. If the file already is HXLated, it will print
# even more information.
#
# Hug API can be used to create an ad-hoc web interface to your
# script. This can be both useful if you are using an software
Expand Down Expand Up @@ -58,8 +60,7 @@ STDIN = sys.stdin.buffer

class HXLQuickMeta:
"""
HXLQuickMeta is a classe to export already HXLated data in the format
example.
HXLQuickMeta is the main class used on hxlquickmeta. (...)
"""

def __init__(self):
Expand All @@ -79,10 +80,9 @@ class HXLQuickMeta:

self.hxlhelper = HXLUtils()
parser = self.hxlhelper.make_args(
description=("hxlquickmeta is an example script to create other "
"scripts with some bare minimum command line "
"interfaces that could work to export HXL files to "
"other formats."))
description=("hxlquickmeta output information about a local or "
+ "remote dataset. If the file already is HXLated, "
+ "it will print even more information."))

# TODO: implement draft of --hxlmeta-rebuild/--hxlmeta-path
# parser.add_argument(
Expand Down Expand Up @@ -161,15 +161,20 @@ class HXLQuickMeta:
temp = tempfile.NamedTemporaryFile()
args.outfile = temp.name

print('> Connection overview')
print(' >> TODO: implement raw connection, HTTP headers, etc')
print(' >> (this should output debug information even')
print(' >> for inputs that would break libhxl)')

with self.hxlhelper.make_source(args, stdin) as source, \
self.hxlhelper.make_output(args, stdout) as output:
hxl.io.write_hxl(output.output, source,
show_tags=not args.strip_tags)

if True: # if args.hxlmeta:
print('TODO: hxlmeta')
print('output.output', output.output)
print('source', source)
print('> lihxl-python overview')
print(' >> output.output', output.output)
print(' >> source', source)
# print('source.columns', source.headers())
hxlmeta = HXLMeta(local_hxl_file=output.output.name)
hxlmeta.debuginfo()
Expand Down Expand Up @@ -816,7 +821,7 @@ class HXLMeta:
return heading1

def debuginfo(self):
print('debuginfo')
print('> HXLMeta debuginfo')
with open(self.local_hxl_file, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
line_1st = next(csv_reader)
Expand All @@ -839,27 +844,45 @@ class HXLMeta:

# Hotfix: skip first non-HXL header. Ideally I think the already
# exported HXlated file should already save without headers.
print('self.text_headers', self.text_headers)
print('self.hxl_headers', self.hxl_headers)
print(' >> HXLMeta.text_headers', self.text_headers)
print(' >> HXLMeta.hxl_headers', self.hxl_headers)

for header in self.hxl_headers:
self.get_hashtag_info(header)

# header_original = next(csv_reader)
# print('header_original', header_original)

# hxlquickmeta --hxlquickmeta-hashtag="#adm2+code"
# --hxlquickmeta-value="BR3106200"
def get_hashtag_info(self, hashtag, value = None):
print('> get_hashtag_info')

# TODO: implement some type of external webservice checking to print
# more information when an value is an PCode. Maybe
# https://gistmaps.itos.uga.edu/arcgis/rest/services/COD_External
# have this?
def get_hashtag_info(self, hashtag, value=None):
print('> get_hashtag_info [', hashtag, ']', '[', value, ']')
print('>> hashtag:', hashtag)
print('>>> HXLMeta._parse_heading:', self._parse_heading(hashtag))
print('>>> HXLMeta.is_hashtag_base_valid:',
self.is_hashtag_base_valid(hashtag))

print('>> value:', value)
print('>>> libhxl_is_empty', self.HXLMetaType.libhxl_is_empty(value))
print('>>> libhxl_is_date', self.HXLMetaType.libhxl_is_date(value))
print('>>> libhxl_is_number', self.HXLMetaType.libhxl_is_number(value))
print('>>> libhxl_is_string', self.HXLMetaType.libhxl_is_string(value))
print('>>> libhxl_is_truthy', self.HXLMetaType.libhxl_is_truthy(value))
print('>>> libhxl_typeof', self.HXLMetaType.libhxl_typeof(value))
print('>>> libhxl_is_token', self.HXLMetaType.libhxl_is_token(hashtag))

if value is not None:
print('>> value:', value)
print('>>> libhxl_is_empty',
self.HXLMetaType.libhxl_is_empty(value))
print('>>> libhxl_is_date', self.HXLMetaType.libhxl_is_date(value))
print('>>> libhxl_is_number',
self.HXLMetaType.libhxl_is_number(value))
print('>>> libhxl_is_string',
self.HXLMetaType.libhxl_is_string(value))
print('>>> libhxl_is_token',
self.HXLMetaType.libhxl_is_token(hashtag))
print('>>> libhxl_is_truthy',
self.HXLMetaType.libhxl_is_truthy(value))
print('>>> libhxl_typeof',
self.HXLMetaType.libhxl_typeof(value))

def is_hashtag_base_valid(self, term, strict=True):
if strict:
Expand Down
9 changes: 5 additions & 4 deletions tests/manual-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ hug -f bin/hxl2tab
# others to use your computer as quick interface

### hxlquickmeta ---------------------------------------------------------------
hxlquickmeta --hxlquickmeta-hashtag="#adm2+code" --hxlquickmeta-value="BR3106200"

hxlquickmeta tests/files/iris_hxlated-csv.csv | head
hxlquickmeta tests/files/iris_hxlated-csv.csv temp/iris.tab
hxlquickmeta https://docs.google.com/spreadsheets/u/1/d/1l7POf1WPfzgJb-ks4JM86akFSvaZOhAUWqafSJsm3Y4/edit#gid=634938833 | head
hxlquickmeta https://docs.google.com/spreadsheets/u/1/d/1l7POf1WPfzgJb-ks4JM86akFSvaZOhAUWqafSJsm3Y4/edit#gid=634938833 data-mining-projects/output/HXL-CPLP-Exemplar_iris.tab
hxlquickmeta tests/files/iris_hxlated-csv.csv
hxlquickmeta tests/files/iris_hxlated-csv.csv
hxlquickmeta https://docs.google.com/spreadsheets/u/1/d/1l7POf1WPfzgJb-ks4JM86akFSvaZOhAUWqafSJsm3Y4/edit#gid=634938833
hxlquickmeta https://docs.google.com/spreadsheets/u/1/d/1l7POf1WPfzgJb-ks4JM86akFSvaZOhAUWqafSJsm3Y4/edit#gid=634938833

## hug -f bin/hxlquickmeta ......................................................
#@see https://hugapi.github.io/hug/
Expand Down

0 comments on commit 2ab16d8

Please sign in to comment.