1
1
mirror of https://github.com/harelba/q.git synced 2024-10-03 22:39:52 +03:00

Added filename parsing functions

This commit is contained in:
Harel Ben-Attia 2022-01-22 17:55:47 +02:00 committed by GitHub
parent 2f2d99eba1
commit 0321d6d825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 1 deletions

View File

@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v2
- id: vars
run: |
set -e -x
set -x -e
echo "github event ref is ${{ github.ref }}"

View File

@ -147,6 +147,28 @@ def sqrt(data):
def power(data,p):
return data**p
def file_ext(data):
if data is None:
return None
return os.path.splitext(data)[1]
def file_folder(data):
if data is None:
return None
return os.path.split(data)[0]
def file_basename(data):
if data is None:
return None
return os.path.split(data)[1]
def file_basename_no_ext(data):
if data is None:
return None
return os.path.split(os.path.splitext(data)[0])[-1]
def percentile(l, p):
# TODO Alpha implementation, need to provide multiple interpolation methods, and add tests
if not l:
@ -276,6 +298,26 @@ user_functions = [
"Raise expr1 to the power of expr2",
power,
2),
UserFunctionDef(FunctionType.REGULAR,
"file_ext","file_ext(<expr>) = <filename-extension-or-empty-string>",
"Get the extension of a filename",
file_ext,
1),
UserFunctionDef(FunctionType.REGULAR,
"file_folder","file_folder(<expr>) = <folder-name-of-filename>",
"Get the folder part of a filename",
file_folder,
1),
UserFunctionDef(FunctionType.REGULAR,
"file_basename","file_basename(<expr>) = <basename-of-filename-including-extension>",
"Get the basename of a filename, including extension if any",
file_basename,
1),
UserFunctionDef(FunctionType.REGULAR,
"file_basename_no_ext","file_basename_no_ext(<expr>) = <basename-of-filename-without-extension>",
"Get the basename of a filename, without the extension if there is one",
file_basename_no_ext,
1),
UserFunctionDef(FunctionType.AGG,
"percentile","percentile(<expr>,<percentile-in-the-range-0-to-1>) = <percentile-value>",
"Calculate the strict percentile of a set of a values.",

View File

@ -4332,6 +4332,31 @@ class UserFunctionTests(AbstractQTestCase):
self.assertEqual(o[3],six.b('32.0'))
self.assertEqual(o[4],six.b('55.9016994375'))
def test_file_functions(self):
filenames = [
"file1",
"file2.csv",
"/var/tmp/file3",
"/var/tmp/file4.gz",
""
]
data = "\n".join(filenames)
cmd = 'echo "%s" | %s -c 1 -d , "select file_folder(c1),file_ext(c1),file_basename(c1),file_basename_no_ext(c1) from -"' % (data,Q_EXECUTABLE)
retcode, o, e = run_command(cmd)
self.assertEqual(retcode,0)
self.assertEqual(len(o),5)
self.assertEqual(len(e),0)
self.assertEqual(o,[
b',,file1,file1',
b',.csv,file2.csv,file2',
b'/var/tmp,,file3,file3',
b'/var/tmp,.gz,file4.gz,file4',
b',,,'
])
def test_sha1_function(self):
cmd = 'seq 1 4 | %s -c 1 -d , "select c1,sha1(c1) from -"' % Q_EXECUTABLE
retcode, o, e = run_command(cmd)