Skip to content

pfun.files.Files dataclass

Module that enables reading and writing from files

append(self, path, content)

Get an Effect that appends to a file

Examples:

>>> files = Files()
>>> files\
...     .append('foo.txt', 'contents')\
...     .discard_and_then(files.read('foo.txt'))\
...     .run(None)
'contents'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Try[OSError, None]

Effect that that appends content to file at path

append_bytes(self, path, content)

Get an Effect that appends to a file

Examples:

>>> files = Files()
>>> files\
...     .append_bytes('foo.txt', b'contents')\
...     .discard_and_then(files.read('foo.txt'))\
...     .run(None)
'contents

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Try[OSError, None]

Effect that that appends content to file at path

read(self, path)

get an Effect that reads the content of a file as a str

Examples:

>>> Files().read('foo.txt').run(None)
'contents of foo.txt'

Parameters:

Name Type Description Default
path str

path to file

required

Returns:

Type Description
Try[OSError, str]

Effect that reads file located at path

read_bytes(self, path)

get an Effect that reads the content of a file as bytes

Examples:

>>> Files().read_bytes('foo.txt').run(None)
b'contents of foo.txt'

Parameters:

Name Type Description Default
path str

path to file

required

Returns:

Type Description
Try[OSError, bytes]

Effect that reads file located at path

write(self, path, content)

Get an Effect that writes to a file

Examples:

>>> files = Files()
>>> files\
...     .write('foo.txt', 'contents')\
...     .discard_and_then(files.read('foo.txt'))\
...     .run(None)
'contents'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Try[OSError, None]

Effect that that writes content to file at path

write_bytes(self, path, content)

Get an Effect that writes to a file

Examples:

>>> files = Files()
>>> files\
...     .write_bytes('foo.txt', b'contents')\
...     .discard_and_then(files.read('foo.txt'))\
...     .run(None)
'contents'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Try[OSError, None]

Effect that that writes content to file at path

pfun.files.HasFiles

Module provider that provides the files module

:attribute files: The Files instance

pfun.files.read(path)

get an Effect that reads the content of a file as a str

Examples:

>>> class Env:
...     files = Files()
>>> read('foo.txt').run(Env())
'contents of foo.txt'

Parameters:

Name Type Description Default
path str

path to file

required

Returns:

Type Description
Effect[HasFiles, OSError, str]

Effect that reads file located at path

pfun.files.read_bytes(path)

get an Effect that reads the content of a file as bytes

Examples:

>>> class Env:
...     files = Files()
>>> read_bytes('foo.txt').run(Env())
b'contents of foo.txt'

Parameters:

Name Type Description Default
path str

path to file

required

Returns:

Type Description
Effect[HasFiles, OSError, bytes]

Effect that reads file located at path

pfun.files.write(path, content)

Get an Effect that writes to a file

Examples:

>>> class Env:
...     files = Files()
>>> write('foo.txt')('contents')\
...     .discard_and_then(read('foo.txt'))\
...     .run(Env())
'content of foo.txt'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Effect[HasFiles, OSError, None]

Effect that that writes content to file at path

pfun.files.write_bytes(path, content)

Get an Effect that writes to a file

Examples:

>>> class Env:
...     files = Files()
>>> write_bytes('foo.txt')(b'content of foo.txt')\
...     .discard_and_then(read('foo.txt'))\
...     .run(Env())
'content of foo.txt'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Effect[HasFiles, OSError, None]

Effect that that writes content to file at path

pfun.files.append(path, content)

Get an Effect that appends to a file

Examples:

>>> class Env:
...     files = Files()
>>> append('foo.txt')('content of foo.txt')\
...     .discard_and_then(read('foo.txt'))\
...     .run(Env())
'content of foo.txt'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Effect[HasFiles, OSError, None]

Effect that that appends content to file at path

pfun.files.append_bytes(path, content)

Get an Effect that appends to a file

Examples:

>>> class Env:
...     files = Files()
>>> append_bytes('foo.txt')(b'content of foo.txt')\
...     .discard_and_then(read('foo.txt'))\
...     .run(Env())
'content of foo.txt'

Parameters:

Name Type Description Default
path str

the path of the file to be written

required

Returns:

Type Description
Effect[HasFiles, OSError, None]

Effect that that appends content to file at path