Re: [Tutor] subprocess.Popen()

2018-12-03 Thread Alan Gauld via Tutor
On 02/12/2018 10:29, Dave Hill wrote:

> Having 'graduated' to Python 3.7, I thought I would explore 
> Traceback (most recent call last):
>File "/home/pi/Code/TestVideo#4.py", line 31, in 
>  player.playVideo(FILE, 'HDMI')
>File "/home/pi/Code/VideoPlayer.py", line 51, in playVideo
>  out = omxp.communicate()
>File "/usr/lib/python3.5/subprocess.py", line 801, in communicate
>  stdout, stderr = self._communicate(input, endtime, timeout)

The first thing I notice is that you say you are using
Python 3.7 yet the libs appear to be 3.5.

It could be some kind of path problem?

Or maybe how you start Python. Do you explicitly call python37?

I don't have the time to go through the code in detail just now.
If nobody else picks it up I'll take a closer look later.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Moving a conda environment to an off-line computer

2018-12-03 Thread Henrique Castro
Thank you for all the insightful replies!
It looks like the problem is exactly that deepchem is looking for an internet 
connection and crashing.
I'll see what I can do and provide some feedback here.

Again, thank you for using some of your time to take a look at this.

--
Henrique C. S. Junior


From: Matt Ruffalo 
Sent: Sunday, December 2, 2018 13:27
To: Henrique Castro; tutor@python.org
Subject: Re: Moving a conda environment to an off-line computer

Hi Henrique-

It is quite easy to transfer an Anaconda installation from one machine
to the other by copying all of the files -- I have done this repeatedly
with cluster compute environments. It is sometimes nicer to run `conda
upgrade --all` in a local VM and then `rsync` the updated Anaconda
installation between machines, since (as you mentioned) internet access
can sometimes be an issue.

It looks like you did everything correctly, and everything is "working"
as well as you would expect. As Alan mentioned, though, it looks like
the 'deepchem' package is trying to access the internet to load one of
its data sets, and this is what is failing. You could perhaps download
that data set and put it somewhere on the cluster where deepchem would
know where to look for it, to avoid having to download it, but I am
completely unfamiliar with deepchem so I can't offer any advice about
how to do that.

MMR...

On 30/11/18 08:47, Henrique Castro wrote:
> Dear colleagues,
> Soon I'll start to use one of the powerful computers on my university as a 
> tool in my Ph.D. The computer does not have an internet connection and I need 
> to find a way to install a conda environment on it.
> At first I tried to install and set the conda environment that I need in a 
> computer with internet connection and taking care to copy everything in a 
> path that is similar in the off-line computer (i.e I installed everything on 
> /home/henrique/bin/anaconda3 at home and tried to copy everything to 
> /home/henrique/bin/anaconda3 in the off-line computer - with the same 
> .bashrc) but when I run conda I get an error(it works on my home computer):
>
> (deepchem) [henrique@europio qm7] $ python qm7_ANI.py
> /home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29:
>  DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and 
> should not be imported. It will be removed in a future NumPy release.
>   from numpy.core.umath_tests import inner1d
> Traceback (most recent call last):
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/urllib/request.py", 
> line 1318, in do_open
> encode_chunked=req.has_header('Transfer-encoding'))
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", 
> line 1239, in request
> self._send_request(method, url, body, headers, encode_chunked)
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", 
> line 1285, in _send_request
> self.endheaders(body, encode_chunked=encode_chunked)
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", 
> line 1234, in endheaders
> self._send_output(message_body, encode_chunked=encode_chunked)
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", 
> line 1026, in _send_output
> self.send(msg)
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", 
> line 964, in send
> self.connect()
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/http/client.py", 
> line 936, in connect
> (self.host,self.port), self.timeout, self.source_address)
>   File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", 
> line 704, in create_connection
> for res in getaddrinfo(host, port, 0, SOCK_STREAM):
>   File "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/socket.py", 
> line 745, in getaddrinfo
> for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
> socket.gaierror: [Errno -2] Name or service not known
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>   File "qm7_ANI.py", line 15, in 
> featurizer='BPSymmetryFunction', split='stratified', move_mean=False)
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/site-packages/deepchem/molnet/load_function/qm7_datasets.py",
>  line 50, in load_qm7_from_mat
> 
> 'https://nam03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fdeepchem.io.s3-website-us-west-1.amazonaws.com%2Fdatasets%2Fqm7.mat&data=02%7C01%7C%7C76013bc4f1c946bc00ea08d6586aa6b0%7C84df9e7fe9f640afb435%7C1%7C0%7C636793612418543403&sdata=v5exjG1hIZlYhQ9zNis87L4sMf%2BXmhIIBF4lGAC8hWU%3D&reserved=0'
>   File 
> "/home/henrique/bin/anaconda3/envs/deepchem/lib/python3.6/site-packages/deepchem/utils/__init__.py",
>  line 85, in download_url
> urlretrieve(url, os.path.joi

[Tutor] Beginners Book, Python and PyScripter

2018-12-03 Thread James Stapleton-Cotton
Hello,

On this page (
http://openbookproject.net/thinkcs/python/english3e/way_of_the_program.html)
- a book for learning Computer Science using Python - I am directed to (
http://code.google.com/p/pyscripter) in order to access the appropriate
program development environment, PyScripter. I have downloaded the latest
version of Python from Python.org and PyScripter from a site that I am
directed to from the original site mentioned in the beginning of this email
- (https://sourceforge.net/projects/pyscripter/files/), however I can't
seem to be able to run PyScripter on my Mac.
The only file that opens is the Python Shell. I am really confused as to
whether or not PyScripter works on Mac OS or if I'm missing a step in the
installation process?

I hope I have been clear in my query. I look forward to hearing from you.

Regards,
James
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginners Book, Python and PyScripter

2018-12-03 Thread Steven D'Aprano
Hi James, and welcome.

On Mon, Dec 03, 2018 at 12:35:29PM +0200, James Stapleton-Cotton wrote:
[...]
> I have downloaded the latest
> version of Python from Python.org and PyScripter from a site that I am
> directed to from the original site mentioned in the beginning of this email
> - (https://sourceforge.net/projects/pyscripter/files/), however I can't
> seem to be able to run PyScripter on my Mac.
> The only file that opens is the Python Shell. I am really confused as to
> whether or not PyScripter works on Mac OS or if I'm missing a step in the
> installation process?

I don't think there are a lot of Mac experts here, but let's see what we 
can do...

You say "the only file that opens is the Python Shell", but you don't 
tell us *precisely* what you did to get it to open. Go through it step 
by step, e.g. if you were a Windows user you might say:

"I clicked on the Start Menu, click on PyScripter, the shell opens"

"I right-clicked on a .py file and choose Open from the menu"

"I double-clicked a .py file on the desktop"

etc. If any of the steps are Mac-specific, you might need to go into a 
bit more detail, but we generally understand things like double- 
clicking, control-click to get the context menu, etc.

When you say "the Python Shell", you are probably seeing a text-based 
console that starts with something similar to this:

Python 3.5.2 (default, Oct 12 2016, 10:47:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux
Type "help", "copyright", "credits" or "license" for more information.


(the details will be different, of course) and then presents you with a 
prompt >>> where you can type commands. Is this correct?

If not, please explain what you get.

Don't bother with a screen shot at this stage: they're not helpful to 
those who are visually impaired or blind (screen readers can't work with 
them) and this mailing list will strip them out of your email. You ought 
to be able to copy any visible text in the shell and paste it into your 
email, if necessary.

Another test you can do is to open the Terminal from Mac OS (I think it 
is found under Applications → Utilities → Terminal) and then enter the 
command "pyscripter" (without the quotes) at the $ or % prompt. What 
happens?

(If you get an error, please copy and paste the entire error, including 
the command you entered. Don't try to summarise it or retype it from 
memory.)


P.S. when replying, reply to the list, not to me personally.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginners Book, Python and PyScripter

2018-12-03 Thread Mats Wichmann
On 12/3/18 3:35 AM, James Stapleton-Cotton wrote:
> Hello,
> 
> On this page (
> http://openbookproject.net/thinkcs/python/english3e/way_of_the_program.html)
> - a book for learning Computer Science using Python - I am directed to (
> http://code.google.com/p/pyscripter) in order to access the appropriate
> program development environment, PyScripter. I have downloaded the latest
> version of Python from Python.org and PyScripter from a site that I am
> directed to from the original site mentioned in the beginning of this email
> - (https://sourceforge.net/projects/pyscripter/files/), however I can't
> seem to be able to run PyScripter on my Mac.

it looks like from the project's own description that PyScripter is
intended solely for the Windows environment.

But there are a TON of other IDE products, free and commercial, out
there, many of which do run on the Mac.

This may be information overload, but try looking here:

https://wiki.python.org/moin/IntegratedDevelopmentEnvironments


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen()

2018-12-03 Thread Mats Wichmann
On 12/2/18 3:29 AM, Dave Hill wrote:

> Having 'graduated' to Python 3.7, I thought I would explore
> subprocess.Popen, and put the code in a Class, see code below. The video
> runs, but an error occurs, which I do not understand, see further below

the error happens in the except clause of your try block ...

> Traceback (most recent call last):
...
>   File "/home/pi/Code/VideoPlayer.py", line 51, in playVideo
> out = omxp.communicate()
...
>   File "/usr/lib/python3.5/selectors.py", line 39, in _fileobj_to_fd
> "{!r}".format(fileobj)) from None
> ValueError: Invalid file object: <_io.BufferedReader name=8>

You can see that the call to communicate() is the one without arguments,
so it's in your cleanup code.   You should catch the specific exception
you expect could happen here, which is probably a timeout - but you
can't tell because you don't specify it.

>     try:
>     out = omxp.communicate(timeout=self.timeout)
>     print("Try outs =  ", StringIO(out))
>     except:
>     omxp.kill()
>     out = omxp.communicate()
>     print("Except outs =  ", StringIO(out))

use

  except TimeoutExpired:

then it looks like the file descriptor for the second communicate is no
longer valid... here's a larger chunk from subprocess that shows what it
is raising and how it got there:

if isinstance(fileobj, int):
fd = fileobj
else:
try:
fd = int(fileobj.fileno())
except (AttributeError, TypeError, ValueError):
raise ValueError("Invalid file object: "
 "{!r}".format(fileobj)) from None


In other words, it's raising the ValueError you see  because it's not a
file object. The "from None" part suppresses the context from the
original exception.

Note that communicate returns a tuple so your 'out' is actually a tuple,
and this is likely what is going wrong - by calling StringIO(out) in the
try: you are causing an exception which isn't the exception one would
expect; by not being explicit in your except: you catch that error and
take cleanup steps that are not appropriate - your video probably
finished normally.

hopefully this will help debug things...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen()

2018-12-03 Thread Dave Hill
I had not spotted that! It appears that, although I have 3.7 on my 
laptop, 3.7 is not a part of the latest standard Raspbian release. I 
will install this later today and try again.


Dave

On 03/12/2018 10:12, Alan Gauld via Tutor wrote:

On 02/12/2018 10:29, Dave Hill wrote:


Having 'graduated' to Python 3.7, I thought I would explore
Traceback (most recent call last):
File "/home/pi/Code/TestVideo#4.py", line 31, in 
  player.playVideo(FILE, 'HDMI')
File "/home/pi/Code/VideoPlayer.py", line 51, in playVideo
  out = omxp.communicate()
File "/usr/lib/python3.5/subprocess.py", line 801, in communicate
  stdout, stderr = self._communicate(input, endtime, timeout)

The first thing I notice is that you say you are using
Python 3.7 yet the libs appear to be 3.5.

It could be some kind of path problem?

Or maybe how you start Python. Do you explicitly call python37?

I don't have the time to go through the code in detail just now.
If nobody else picks it up I'll take a closer look later.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] subprocess.Popen()

2018-12-03 Thread Dave Hill
The combination of installing Python 3.7 on RasPi and removing the 
StringIO() cures the error.


I now get the statistics and the 'have a nice day' output from omxplayer.

I just need to find out how to invoke the idle 3.7, rather than idle 
3.5, but I think that is a question for the RasPi forum.


Thank you.

Dave

On 03/12/2018 16:27, Mats Wichmann wrote:

On 12/2/18 3:29 AM, Dave Hill wrote:


Having 'graduated' to Python 3.7, I thought I would explore
subprocess.Popen, and put the code in a Class, see code below. The video
runs, but an error occurs, which I do not understand, see further below

the error happens in the except clause of your try block ...


Traceback (most recent call last):

...

   File "/home/pi/Code/VideoPlayer.py", line 51, in playVideo
 out = omxp.communicate()

...

   File "/usr/lib/python3.5/selectors.py", line 39, in _fileobj_to_fd
 "{!r}".format(fileobj)) from None
ValueError: Invalid file object: <_io.BufferedReader name=8>

You can see that the call to communicate() is the one without arguments,
so it's in your cleanup code.   You should catch the specific exception
you expect could happen here, which is probably a timeout - but you
can't tell because you don't specify it.


     try:
     out = omxp.communicate(timeout=self.timeout)
     print("Try outs =  ", StringIO(out))
     except:
     omxp.kill()
     out = omxp.communicate()
     print("Except outs =  ", StringIO(out))

use

   except TimeoutExpired:

then it looks like the file descriptor for the second communicate is no
longer valid... here's a larger chunk from subprocess that shows what it
is raising and how it got there:

 if isinstance(fileobj, int):
 fd = fileobj
 else:
 try:
 fd = int(fileobj.fileno())
 except (AttributeError, TypeError, ValueError):
 raise ValueError("Invalid file object: "
  "{!r}".format(fileobj)) from None


In other words, it's raising the ValueError you see  because it's not a
file object. The "from None" part suppresses the context from the
original exception.

Note that communicate returns a tuple so your 'out' is actually a tuple,
and this is likely what is going wrong - by calling StringIO(out) in the
try: you are causing an exception which isn't the exception one would
expect; by not being explicit in your except: you catch that error and
take cleanup steps that are not appropriate - your video probably
finished normally.

hopefully this will help debug things...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] To check for empty string after a portion of the string in python 3.6

2018-12-03 Thread Cameron Simpson

Note: post returned to the tutor list.

Please DO NOT cross post to multiple lists (i.e. tutor and python-list, 
as you have). This makes things difficult for people who are not on both 
lists. Pick a _single_ list, and use that.


On 04Dec2018 02:46, srinivasan  wrote:

Could you please help me, as am still learning python syntax, how can
I add conditional check for empty string after running "hcitool scan"
(ie., when there is no Bluetooth devices discovered) ie., after the
word  "Scanning..." , when there are no Bluetooth discover-able
devices and then return False in the below python code snippet?


Command:
~$ sudo hcitool scan
Scanning ...  -->
When there are no BT devices
~$ sudo hcitool scan
Scanning ...
64:A2:F9:06:63:79 OnePlus 6 ---> When there
are BT devices
~$

Python code snippet:
   def bluetooth_scan(self):
   """
   Start bluetooth scanning process.

   :return: Return device info by mac address and name.
   """
   cmd = "sudo hciconfig hci0 up"
   self._helper.execute_cmd_return_code(cmd)

   cmd = "sudo hcitool scan"
   res = self._helper.execute_cmd_output_string(cmd)

   return res


Well you document your function as returning device info by MAC. So part 
the output if "hcitool scan" and for MAC address and name and store that 
in a dictionary (key MAC, value the name). Return the dictionary.


If the dictionary is empty, there were no devices.

Not that like almost all collections, empty dictionaries are "false", so 
you can go:


 bluetooth_devices = scanner.bluetooth_scan()
 if not bluetooth_devices:
   ... no devices found ...

Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] To check for empty string after a portion of the string in python 3.6

2018-12-03 Thread Cameron Simpson
Again, _please_ use just one list: tutor or python-list. I've have 
directed replies to the tutor list. - Cameron


On 04Dec2018 04:05, srinivasan  wrote:

Thanks a lot for your quick responses, Could you please let me know
when the device is not enabled I get the error " I get the below error
saying "IndexError: list index out of range""



Code snippet:

[...]

res = self._helper.execute_cmd_output_string(cmd)
print("The value of res", res)
res = self._helper.execute_cmd_output_string(cmd).split("\n", 2)
print("the value", res)
print("the value", res[1])
if __name__ == "__main__":
   m = Bt()
   print(m.bluetooth_scan())

1. Output when device is enabled:
The value of res Scanning ...
64:A2:F9:06:63:79 OnePlus 6
the value ['Scanning ...', '\t64:A2:F9:06:63:79\tOnePlus 6']
the value 64:A2:F9:06:63:79 OnePlus 6
None


The "None" is because your bluetooth_scan method has no return value (I 
think), which in Python it means it returns None.



2. Output when device is not enabled
When the device is not connected, I get the below error saying
"IndexError: list index out of range"
The value of res Scanning ...
Traceback (most recent call last):
the value ['Scanning ...']
 File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
line 96, in 
   print(m.bluetooth_scan())
 File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
line 74, in bluetooth_scan
   print("the value", res[1])
IndexError: list index out of range


Well, you've printed your list:

 ['Scanning ...']

It is a single element list. So it has an element at index 0. There is 
no element 1. Therefor Python raises an IndexError if you try to access 
that element.


Cheers,
Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I need help with my project

2018-12-03 Thread Treyton Hendrix
Awesome! Thanks to your help, I finally got my program done. Click here if
you want to see it! >

On Fri, Nov 30, 2018 at 5:14 PM Cameron Simpson  wrote:

> Avi and Alan and Sibylle, you're making this a bit hard on the OP
> (Treyton).
>
> Yes he's supplied no context, but it is easy to make some suggestions.
> Each of yours suggests he design a much wider system (menu entry, web
> interface, some kind of GUI). All of which is (a) beyond him and (b)
> irrelevant.
>
> Why not pretend he _has_ the existing order, from wherever.
>
> Suggest ways to store that order (in a list, or a dict mapping ordable
> items to counts, or something). Then ask him to write a little Python,
> or even detailed English prose.
>
> Treyton: you seem to have recitied a homework question:
> >If the user selected a sandwich, french fries, and a beverage, reduce
> >the
> >total cost of the order by $1.00.
> >This is what I have to do and I don't know where to start.
>
> Ok, this is clear: Treyton can't get off the ground, very common for
> beginning programmers.
>
> The core challenge is to break your problem into a sequence of tasks.
> How would _you_, a person, do this if you had a food order given to you?
>
> Think about a food order. It is usually a list of standard food items, a
> count of how many of each. And each item will have a cost.
>
> The total cost is the sum of (each item's cost * its price * its count),
> for each item in the order. Or for all possible items, by presuming that
> unordered items just have a count of 0.
>
> So you need:
>
> A label for each item, so you can talk about it. You can just use a
> string for this, eg "sandwich" or "fries". Make the strings simple to
> start with to avoid spelling mistakes. You can always associate better
> names with the short strings later.
>
> You need a table of items and their costs. It is normal to make a
> mapping for this, such a Python's dict type. You can write dicts
> literally:
>
>   costs = {
> "sandwich": 200,
> "fries": 100,
>   }
>
> In the example above, I'm imagining you have dollars and cents, and
> making prices in cents.
>
> You also need a representation of the order, being the item type and the
> count. You could use a Python list for this. Example:
>
>   [ "fries", 2 ]
>
> The whole order might be a list of those, example:
>
>   [ ["fries", 2 ], [ "sandwich", 3 ] ]
>
> So, a list of lists.
>
> For purposes of your program you can just set all this stuff up at the
> beginning, not worrying about GUIs or input forma or any complication.
>
>   whole_order = [
> ["fries", 2 ],
> [ "sandwich", 3 ]
>   ]
>
> Now comes the part you need to do:
>
> - write some Python code to compute the total cost of the order (item
>   cost * item count), summed for all the items. Print this raw total so
>   that you can see it is correct.
>
> - write some totally separate code to look at the order and decide if
>   the client met your special condition (sandwich, fries, beverage) and
>   get a true/false result. Print this, too.
>
> - write a Python statement to subtract $1.00 (or 100 cents) from the
>   total if that condition is true. Print that.
>
> Then fiddle the order and run your programme several times to check that
> it is behaving the way it should.
>
> If you find difficulties you cannot surmount, come back here (by
> replying directly to one of the messages in your discussion) with:
>
> - your complete code
>
> - your expected output, and the output from your programme
>
> - a complete transcript of any error message, for example if your
>   programme raised an exception
>
> Make sure these are inline in your message, _not_ attachments. We drop
> attachments in this list.
>
> Cheers,
> Cameron Simpson 
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] sftp using subprocess

2018-12-03 Thread Asad
Hi All ,

I am looking for a solution to automate downloading file using sftp
process using :

batch.txt :contains the following two lines
cd 12345678
get 12345678_1.zip


import subprocess
host="abc.com"
user="sa"
user_host="%s@%s" % (user, host)
sftp_process = subprocess.Popen(['sftp', '-b', 'batch.txt', user_host],
shell=False)

however it gives error even unable to provide password .

Please advise.

Thank you,
-- 
Asad Hasan
+91 9582111698
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Regarding Python api script

2018-12-03 Thread Ravi Kumar
Hi,

I have developed a python script to get api calls for meraki
clientlogevents I am wanting the output of apicalls which is in xml format
integrated to sql server management  studio how do i do that?

Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] To check for empty string after a portion of the string in python 3.6

2018-12-03 Thread srinivasan
Dear Python Experts,

Could you please help me, as am still learning python syntax, how can
I add conditional check for empty string after running "hcitool scan"
(ie., when there is no Bluetooth devices discovered) ie., after the
word  "Scanning..." , when there are no Bluetooth discover-able
devices and then return False in the below python code snippet?


Command:
~$ sudo hcitool scan
Scanning ...  -->
When there are no BT devices
~$ sudo hcitool scan
Scanning ...
64:A2:F9:06:63:79 OnePlus 6 ---> When there
are BT devices
~$

Python code snippet:
def bluetooth_scan(self):
"""
Start bluetooth scanning process.

:return: Return device info by mac address and name.
"""
cmd = "sudo hciconfig hci0 up"
self._helper.execute_cmd_return_code(cmd)

cmd = "sudo hcitool scan"
res = self._helper.execute_cmd_output_string(cmd)

return res

Many Thanks in advance,
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp using subprocess

2018-12-03 Thread David Rock

> On Dec 3, 2018, at 16:01, Asad  wrote:
> 
> Hi All ,
> 
>I am looking for a solution to automate downloading file using sftp
> process using :
> 
> batch.txt :contains the following two lines
> cd 12345678
> get 12345678_1.zip
> 
> 
> import subprocess
> host="abc.com"
> user="sa"
> user_host="%s@%s" % (user, host)
> sftp_process = subprocess.Popen(['sftp', '-b', 'batch.txt', user_host],
> shell=False)
> 
> however it gives error even unable to provide password .

batch mode for sftp assumes you don’t need a password.  From the manage:

 -b batchfile
 Batch mode reads a series of commands from an input batchfile 
instead of stdin.
 Since it lacks user interaction it should be used in conjunction 
with non-inter‐
 active authentication.  

You would need to set up an ssh key with a null passphrase, or set up an 
ssh-agent in order for that to work.

Another option would be to use pexpect to automate console input


— 
David Rock
da...@graniteweb.com




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] To check for empty string after a portion of the string in python 3.6

2018-12-03 Thread srinivasan
Dear Mr. Cameron

Thanks a lot for your quick responses, Could you please let me know
when the device is not enabled I get the error " I get the below error
saying "IndexError: list index out of range""

Code snippet:
cmd = "sudo hcitool scan"
res = self._helper.execute_cmd_output_string(cmd)
print("The value of res", res)
res = self._helper.execute_cmd_output_string(cmd).split("\n", 2)
print("the value", res)
print("the value", res[1])

if __name__ == "__main__":
m = Bt()
print(m.bluetooth_scan())


1. Output when device is enabled:
The value of res Scanning ...
64:A2:F9:06:63:79 OnePlus 6
the value ['Scanning ...', '\t64:A2:F9:06:63:79\tOnePlus 6']
the value 64:A2:F9:06:63:79 OnePlus 6
None

Process finished with exit code 0

2. Output when device is not enabled

When the device is not connected, I get the below error saying
"IndexError: list index out of range"
The value of res Scanning ...
Traceback (most recent call last):
the value ['Scanning ...']
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
line 96, in 
print(m.bluetooth_scan())
  File "/home/srinivasan/Downloads/bt_tests/qa/test_library/Bt.py",
line 74, in bluetooth_scan
print("the value", res[1])
IndexError: list index out of range

Process finished with exit code 1

Could you please do the needful

Thanks in advance



















On Tue, Dec 4, 2018 at 3:09 AM Cameron Simpson  wrote:
>
> Note: post returned to the tutor list.
>
> Please DO NOT cross post to multiple lists (i.e. tutor and python-list,
> as you have). This makes things difficult for people who are not on both
> lists. Pick a _single_ list, and use that.
>
> On 04Dec2018 02:46, srinivasan  wrote:
> >Could you please help me, as am still learning python syntax, how can
> >I add conditional check for empty string after running "hcitool scan"
> >(ie., when there is no Bluetooth devices discovered) ie., after the
> >word  "Scanning..." , when there are no Bluetooth discover-able
> >devices and then return False in the below python code snippet?
> >
> >
> >Command:
> >~$ sudo hcitool scan
> >Scanning ...  -->
> >When there are no BT devices
> >~$ sudo hcitool scan
> >Scanning ...
> >64:A2:F9:06:63:79 OnePlus 6 ---> When there
> >are BT devices
> >~$
> >
> >Python code snippet:
> >def bluetooth_scan(self):
> >"""
> >Start bluetooth scanning process.
> >
> >:return: Return device info by mac address and name.
> >"""
> >cmd = "sudo hciconfig hci0 up"
> >self._helper.execute_cmd_return_code(cmd)
> >
> >cmd = "sudo hcitool scan"
> >res = self._helper.execute_cmd_output_string(cmd)
> >
> >return res
>
> Well you document your function as returning device info by MAC. So part
> the output if "hcitool scan" and for MAC address and name and store that
> in a dictionary (key MAC, value the name). Return the dictionary.
>
> If the dictionary is empty, there were no devices.
>
> Not that like almost all collections, empty dictionaries are "false", so
> you can go:
>
>   bluetooth_devices = scanner.bluetooth_scan()
>   if not bluetooth_devices:
> ... no devices found ...
>
> Cheers,
> Cameron Simpson 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sftp using subprocess

2018-12-03 Thread Steven D'Aprano
On Tue, Dec 04, 2018 at 03:31:53AM +0530, Asad wrote:

> however it gives error even unable to provide password .

Is the error a secret? Would you like us to guess what it is?


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-03 Thread Steven D'Aprano
On Mon, Dec 03, 2018 at 04:15:51PM -0600, Ravi Kumar wrote:
> Hi,
> 
> I have developed a python script to get api calls for meraki
> clientlogevents I am wanting the output of apicalls which is in xml format
> integrated to sql server management  studio how do i do that?

What have you tried?


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regarding Python api script

2018-12-03 Thread Alan Gauld via Tutor
On 03/12/2018 22:15, Ravi Kumar wrote:

> I have developed a python script to get api calls for meraki
> clientlogevents I am wanting the output of apicalls which is in xml format
> integrated to sql server management  studio how do i do that?

XML is such a flexible format that without seeing the
specific schema and data its near impossible to give
specific answers.

However, the etree module that comes with Python is
designed for parsing XML so if you take a look at
its documentation (and search for ElementTree
tutorials) you should get something.

There are other third party modules that are slightly
easier to use but provided its straightforward,
well formed XML, etree should be fine.

If you need to access the SQL Server database to
fetch the XML then the Python DBAPI has modules
that work with SQLserver. The ODBC one if nothing
else works!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor