acatton / python-spm

:muscle: Simple and secure sub processes manager
https://python-spm.readthedocs.org/en/latest/
MIT License
10 stars 3 forks source link

Command logic API #7

Open acatton opened 9 years ago

acatton commented 9 years ago

I would like an API like this:

from spm import run

run('echo', 'header').and_(
run('cat', 'file')).and_(
run('echo', 'footer'))
# This should be equivalent to:
#  $ echo header && cat file && echo footer
rockymeza commented 9 years ago

I'm going to make two silly suggestions:

  1. bitwise
run('echo', 'header') & run('cat', 'file')

# also
from operator import and_
and_(run('echo', 'header'), run('cat', 'file'))
  1. return a Truthy
run('echo', 'header') and run('echo', 'header')
acatton commented 9 years ago

@rockymeza thank you for your suggestion. This issue has been brought up in another thread, and here's my answer.

acatton commented 9 years ago

Oh and BTW, and can't be overridden in Python.

rockymeza commented 9 years ago

and doesn't have to be overwritten, and just evaluates if a value is Truthy.

class ReturnValue(object):
    def __init__(self, code, output):
        self.code = code
        # ...

    def __bool__(self):
        return self.code == 0
rockymeza commented 9 years ago

let me redo my example code since I read the source:

class Subprocess(object):
    # ...
    def __bool__(self):
        self.wait()
        return self.returncode == 0
gavinwahl commented 9 years ago

The point of and isn't for short-circuit evaluation, but to compose a new 'command' object that outputs the concatenation of two commands' outputs, to use in a pipeline.

On Wed, Jul 15, 2015 at 11:09 PM, Rocky Meza notifications@github.com wrote:

let me redo my example code since I read the source:

class Subprocess(object):

...

def **bool**(self):
    self.wait()
    return self.returncode == 0

— Reply to this email directly or view it on GitHub https://github.com/acatton/python-spm/issues/7#issuecomment-121831116.

rockymeza commented 9 years ago

oh ok, that's fair

-rocky

On Fri, Jul 17, 2015 at 12:19 AM, Gavin Wahl notifications@github.com wrote:

The point of and isn't for short-circuit evaluation, but to compose a new 'command' object that outputs the concatenation of two commands' outputs, to use in a pipeline.

On Wed, Jul 15, 2015 at 11:09 PM, Rocky Meza notifications@github.com wrote:

let me redo my example code since I read the source:

class Subprocess(object):

...

def bool(self): self.wait() return self.returncode == 0

— Reply to this email directly or view it on GitHub https://github.com/acatton/python-spm/issues/7#issuecomment-121831116.

— Reply to this email directly or view it on GitHub https://github.com/acatton/python-spm/issues/7#issuecomment-122008874.