useless.pipes

Generator sugar
Download

useless.pipes Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Herr Kaste
  • Publisher web site:
  • http://github.com/kaste/

useless.pipes Tags


useless.pipes Description

useless.pipes is a Python module that provides pseudo-hacky sugar around generators. If you write nested for loops or nested functions a(b(c())) this might help you.Basically it looks like this: | add(2) | list == where@workerdef add(items, n): for i in items: yield i + nThe leftmost argument always is the iterator coming from the left side. The other arguments represent a bound state. Using this worker is a two-step process.adder = add(2) # bind a state in a closureadder() == # apply an iteratorworkers tend to be very simple and short, reusable and easy to test.@workerdef echo(items): for i in items: yield iecho = echo() # echo 'has' no stateassert | echo == Sometimes you can achieve something like this:filter_audio_files = fs.filter_by_ext()@producerdef folders_with_audio_files(path): for root, folders, filenames in os.walk(path): if any(filenames | filter_audio_files): yield root@workerdef that_need_fix(paths): for path in paths: files = listdir(path) | filter_audio_files | join_path(path) | list dos_names = files | get_83DOS_name | list if files.sort() != dos_names.sort(): yield paththat_need_fix = that_need_fix()# and the outermost commands in a script then look like this#give me all folders with mp3-files inside, e.g. print them to stdoutfolders_with_audio_files(root)#give me all folder that need a specific fix, aka dry-modefolders_with_audio_files(root) | that_need_fix#actually apply a fix to these foldersfolders_with_audio_files(root) | that_need_fix | apply_fixProduct's homepage


useless.pipes Related Software