Skip to content

Commit

Permalink
Addition of a glob function
Browse files Browse the repository at this point in the history
This is a *bit* of a hack. Add missing documentation for `read_bytes()` 
function.
  • Loading branch information
jonathanhogg committed Jul 26, 2024
1 parent cac3281 commit 3819aea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ The name `c` will have the value `"o w"`.

## File functions

`glob(` *pattern* `)`
: Return an *n*-vector of strings representing file paths matching the given
shell "glob" pattern. Files are matched relative to the directory containing
the running program.

`csv(` *filename* `,` *row* `)`
: Return a vector of values obtained by reading a
specific *row* (indexed from *0*) from the CSV file with the given *filename*;
Expand All @@ -391,3 +396,9 @@ the row into numeric values.
`read(` *filename* `)`
: Returns a single string value containing the entire text of *filename* (this
function intelligently caches).

`read_bytes(` *filename* `)`
: Returns a numeric vector value containing the entire text of *filename* (this
function intelligently caches) as a sequence of bytes. This is a *very*
memory-inefficient function as each byte will be represented by a
double-precision floating point value.
4 changes: 4 additions & 0 deletions src/flitter/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __init__(self, path, absolute):
def suffix(self):
return self._path.suffix

@property
def parent(self):
return self._path.parent

def exists(self):
return self._path.exists()

Expand Down
24 changes: 15 additions & 9 deletions src/flitter/language/functions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ cdef class normal(uniform):
return self.R * cos(self.th)


@context_func
def glob(Context context, Vector pattern):
return Vector._coerce(sorted(str(p.resolve()) for p in context.path.parent.glob(pattern.as_string())))


@context_func
def read_text(Context context, Vector filename):
cdef str path = filename.as_string()
Expand All @@ -178,6 +183,15 @@ def read_bytes(Context context, Vector filename):
return null_


@context_func
def read_csv(Context context, Vector filename, Vector row_number):
cdef str path = str(filename)
row = row_number.match(1, int)
if filename and row is not None:
return SharedCache.get_with_root(path, context.path).read_csv_vector(row)
return null_


def ordv(Vector text):
cdef str string = text.as_string()
return Vector._coerce([ord(ch) for ch in string])
Expand All @@ -202,15 +216,6 @@ def split(Vector text):
return Vector._coerce(text.as_string().rstrip('\n').split('\n'))


@context_func
def read_csv(Context context, Vector filename, Vector row_number):
cdef str path = str(filename)
row = row_number.match(1, int)
if filename and row is not None:
return SharedCache.get_with_root(path, context.path).read_csv_vector(row)
return null_


def lenv(Vector xs not None):
cdef Vector ys = Vector.__new__(Vector)
ys.allocate_numbers(1)
Expand Down Expand Up @@ -1025,6 +1030,7 @@ STATIC_FUNCTIONS = {
DYNAMIC_FUNCTIONS = {
'debug': Vector(debug),
'sample': Vector(sample),
'glob': Vector(glob),
'csv': Vector(read_csv),
'read': Vector(read_text),
'read_bytes': Vector(read_bytes),
Expand Down

0 comments on commit 3819aea

Please sign in to comment.