Skip to content

Commit

Permalink
Added filename parsing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
harelba authored Jan 22, 2022
1 parent 2f2d99e commit 0321d6d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
42 changes: 42 additions & 0 deletions bin/q.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -276,6 +298,26 @@ def __init__(self,func_type,name,usage,description,func_or_obj,param_count):
"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.",
Expand Down
25 changes: 25 additions & 0 deletions test/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4332,6 +4332,31 @@ def test_power_function(self):
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)
Expand Down

0 comments on commit 0321d6d

Please sign in to comment.