Hi all,
Taking a very simple dataflow oriented task such as: process a text file, search and replace certain patterns, save it (to the same file):
open('foobar.txt','r') -> _.read() -> _.replace('Zoom', 'Zuboom') -> open('foobar.txt', 'w').write(_)
But what if the filename is not fixed, how will the latter open() will know which filename to open and write into?
I have drafted 4 implementations (some already-existing, some not) that make it work, feel free to comment, as well as to suggest new implementations:
1. Using Variables
Using variables to store objects, so accessing it directly is made possible:
f = open('foobar.txt', 'rw') -> f.read() -> _.replace('Zoom', 'Zuboom') -> f.write(_)
Note: This method is already working today.
Also, to maintain brevity and readability, a new C-style pointer dereference syntax will be introduced to dereference a variable assignment:
*[f = open('foobar.txt', 'rw')].read() -> _.replace('Zoom', 'Zuboom') -> f.write(_)
2. Using Metadata
Metadata will contain the latest filename opened on the flow, so passing as a function argument is made possible (using Python String Formatting Syntax, or otherwise):
glob.glob('*.TXT') -> open(_, 'r').read() -> _.replace('Zoom', 'Zuboom') -> open(%(FILENAME)s, 'w').write(_)
Alternative (or, in addition) metadata will contain the returned file object, so accessing it directly is made possible:
glob.glob('*.TXT') -> open(_, 'r').read() -> _.replace('Zoom', 'Zuboom') -> %(FILEOBJECT).write(_)
3. Using Stack
A LIFO stack that will be made available per flow to store items on the flow, so accessing them later will be made possible:
push(open('foobar.txt','rw')) -> _.read() -> _.replace('Zoom', 'Zuboom') -> pop().write(_)
Note: This method already working today, using a list() as a stack. The only difference is introducting the push() and pop() as official keywords.
4. Using Advanced Data Structures
Wrapping open() and related functions in a wrapper that returns an advanced data structures that is able to contain multiple data types. e.g.:
def FileReader(filename, modes):
return {'FILENAME': filename, 'DATA': open(filename, modes).read()}
def FileWriter(context, data):
open(context['FILENAME'], 'w').write(context['DATA'])
def ReplaceData(context, from, to):
return dict(context, DATA=context['DATA'].replace(from, to))
Then:
FileReader('foobar.txt', 'r') -> ReplaceData(_, 'Zoom', 'Zuboom') -> FileWriter
Regards,
Itzik Kotler