diff options
author | Harel Ben-Attia <harelba@gmail.com> | 2022-01-22 17:55:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-22 17:55:47 +0200 |
commit | 0321d6d82529431a68ff899ada787bf45aaaac65 (patch) | |
tree | 222b3a4d452a4fcec73151e33ff0173136cc1a38 | |
parent | 2f2d99eba1c23e081d6a359ac943b2aa5e62369f (diff) |
Added filename parsing functions
-rw-r--r-- | .github/workflows/build-and-package.yaml | 2 | ||||
-rwxr-xr-x | bin/q.py | 42 | ||||
-rwxr-xr-x | test/test_suite.py | 25 |
3 files changed, 68 insertions, 1 deletions
diff --git a/.github/workflows/build-and-package.yaml b/.github/workflows/build-and-package.yaml index 3368220..37add71 100644 --- a/.github/workflows/build-and-package.yaml +++ b/.github/workflows/build-and-package.yaml @@ -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 }}" @@ -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.", diff --git a/test/test_suite.py b/test/test_suite.py index fa8f2ea..aaa7476 100755 --- a/test/test_suite.py +++ b/test/test_suite.py @@ -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) |