User avatar
RDK
Posts: 262
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Using command line code in a Python program, ie i2cset... or moreutils function ifne

Sun Aug 27, 2017 11:54 am

I would like to call from my Python programs some system or Command Line (CL) commands. For example, in one case I need to duplicate this CL command
sudo i2cset -y 1 0x6b 0x05 0x0f
in my Python program. In another case I want to use the moreutils command ifne to check on the status of a pipe.

moreutils is installed on my Pi v2 running Jessie.

For example, in the following code I would like to use ifne to display the status of the pipe before and after I read in the next record:

Code: Select all

#!/usr/bin/env python
import subprocess
#import moreutils
nul_f = open('/dev/null', 'w') 
try:
  tcpflow = subprocess.Popen(['/usr/bin/sudo /usr/local/bin/tcpflow  -C -0 -i eth1 -s -o /mnt/usbdrive/TCPFLOW/ tcp port 80'], 
                        stdout=subprocess.PIPE, shell=True,
                        stderr=nul_f,)
  grep = subprocess.Popen(['stdbuf -oL egrep "GET"'], shell=True,
                        stdin=tcpflow.stdout,
                        stdout=subprocess.PIPE,
                        stderr=nul_f)
  print 'Listener running'
  i=0
  while i<10:
    i=i+1
#    print 'ifne before = ' & moreutils.ifne(sh -c)
    line = grep.stdout.readline()
#    print 'ifne after = ' & moreutils.ifne(sh -c)
    print '\t --', i, line
except KeyboardInterrupt:
#end_of_pipe = tcpdmp.stdout
  print 'done'
tcpflow.terminate()
tcpflow.kill()
grep.terminate()
grep.kill()
nul_f.close()
However, this code throws an error on either the
import moreutils
or the
print ... & moreutils.ifne...
I have tried to search for this but have not found anything which seems to help. Any help or "how to do it" references would be appreciated. Thanks....RDK

User avatar
MrYsLab
Posts: 376
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Using command line code in a Python program, ie i2cset... or moreutils function ifne

Sun Aug 27, 2017 3:34 pm

I may be wrong here, but I think that moreutils is a Linux package that extends the standard shell command set and is not a python package. Therefore you would not import it but install it using sudo apt-get install moreutiils, and then make the call to ifne using subprocess.


You might want to check this article out this article: https://www.linux.com/learn/moreutils-y ... l-tool-set

User avatar
RDK
Posts: 262
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Using command line code in a Python program, ie i2cset... or moreutils function ifne

Sun Aug 27, 2017 3:50 pm

Thanks. I have already installed moreutils on my PI. I've used subprocess before, as you can see from the code, but in the case of moreutils module ifne, I'm not sure how to code it using subprocess so that I get the return value, ie the state of my pipe. This Python code is a test platform for a more complex project where I have to read from the pipe when records are available, which may be full time, but also have gaps of minutes or even hours. When records are not available I will branch to another data source/process.

Any help on handling ifne and it return value will be appreciated......Rob

User avatar
MrYsLab
Posts: 376
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Using command line code in a Python program, ie i2cset... or moreutils function ifne

Sun Aug 27, 2017 4:15 pm

I am not sure if this helps or not, but perhaps subprocess.check_output might help solve your problem.

User avatar
DougieLawson
Posts: 36306
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Using command line code in a Python program, ie i2cset... or moreutils function ifne

Sun Aug 27, 2017 4:28 pm

If you want to write to an I²C device from a python program you'll have much better success using

Code: Select all

import smbus
and the functions that gives you.

It'll run faster, it'll be easier to interpret the results. It's a more pythonic way of doing it.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
RDK
Posts: 262
Joined: Wed Aug 13, 2014 10:19 am
Location: Wyoming and France

Re: Using command line code in a Python program, ie i2cset... or moreutils function ifne

Mon Aug 28, 2017 5:02 am

Thanks guys, I'll try your ideas and see what happens....RDK

Return to “Python”