Re: [Tutor] Open a libreoffice calc file in Python

2016-12-22 Thread eryk sun
On Thu, Dec 22, 2016 at 4:20 PM, boB Stepp  wrote:
>
> Both you and Eryk seem to be speaking in terms of using
> subprocess.Popen() directly.  So I think I need some clarification.
> At 
> https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module
> it says:
>
> "The recommended approach to invoking subprocesses is to use the run()
> function for all use cases it can handle. For more advanced use cases,
> the underlying Popen interface can be used directly.

The high-level functions such as `run` pass their positional arguments
and Popen keyword arguments to the Popen constructor. Everything
stated about handling args as a list or string and shell=True applies
the same to the high-level API.

> My current understanding as to why the subprocess module is preferred
> to using the older os.system() is to avoid shell injection attacks.
> So my assumption is that even when using "shell=True" with either
> run() or Popen(), this is avoided.  Is this true?

Using shell=True is insecure. The command-line string is passed
directly to the shell, the same as with os.system. Try to avoid using
shell=True as much as possible, especially when the command is based
on user input.

But the subprocess module has more to offer other than just avoiding
the shell. For example, it allows communicating directly with the
child process using standard I/O (stdin, stdout, stderr) pipes;
controlling inheritance of Unix file descriptors or Windows handles;
and creating a new Unix session or Windows process group.

On Windows, Popen also allows passing creationflags [1] (e.g.
CREATE_NEW_CONSOLE, CREATE_NO_WINDOW, DETACHED_PROCESS) and a subset
of the process startupinfo [2], including the standard handles and
wShowWindow.

[1]: https://msdn.microsoft.com/en-us/library/ms684863
[2]: https://docs.python.org/3/library/subprocess.html#subprocess.STARTUPINFO

> Are there subtleties as to when to use run() and when to use Popen()?

The high-level API executes a child process synchronously -- i.e.
write some (optional) input, read the output, close the standard I/O
files, and wait for the process to exit. For example, here's the
implementation of `run` in 3.5:

def run(*popenargs, input=None, timeout=None, check=False, **kwargs):
if input is not None:
if 'stdin' in kwargs:
raise ValueError(
'stdin and input arguments may not both be used.')
kwargs['stdin'] = PIPE
with Popen(*popenargs, **kwargs) as process:
try:
stdout, stderr = process.communicate(
input, timeout=timeout)
except TimeoutExpired:
process.kill()
stdout, stderr = process.communicate()
raise TimeoutExpired(process.args, timeout,
 output=stdout, stderr=stderr)
except:
process.kill()
process.wait()
raise
retcode = process.poll()
if check and retcode:
raise CalledProcessError(retcode, process.args,
 output=stdout, stderr=stderr)
return CompletedProcess(process.args, retcode, stdout, stderr)

In most cases the high-level API is all that you'll need. Rarely you
may need to execute a child process asynchronously and interact with a
long-running process. In that case call Popen directly.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Open a libreoffice calc file in Python

2016-12-22 Thread Jim Byrnes

On 12/22/2016 03:54 AM, Alan Gauld via Tutor wrote:

On 22/12/16 03:37, Jim Byrnes wrote:

Python 3.4 on Ubuntu

If I was going to open a libreoffice calc file from the terminal I would
go: libreoffice --calc /home/path/to/myfile.ods.

How would I do this from Python?


Others have advised how to run the Libreoffice app from
within Python.

If you really want to open the actual spreadsheet file
in Python rather than Libreoffice then its a bit more tricky.
If that is what you really meant get back to us and
we can start on the options available...




First thanks to everyone that responded pointing me in the right direction.

Alan,

I want to open Libreoffice with a particular file loaded.

I started out to write a libreoffice macro in python to go to a website, 
get some info, copy it to the clipboard and then paste it in libreoffice 
calc.


I started out by writing a script using Selenium that successfully did 
what I wanted. Once I had it working the plan was to put it in a macro. 
When I did that I got a large error message where both libreoffice and 
selenium complained.  I seemed to say that the first element of the 
webpage I tried to manipulate was in a state that it could not be 
interacted with, even though outside of libreoffice the script ran fine.


This got me thinking that maybe I should attack the problem from the 
other end, ie run the script and have it load libreoffice at the end. 
Hence my question.


for completeness here is the error msg I received:

com.sun.star.uno.RuntimeExceptionError during invoking function login in 
module 
file:///home/jfb/.config/libreoffice/4/user/Scripts/python/funds/funds.py 
(: 
Message: invalid element state: Element is not currently interactable 
and may not be manipulated

  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.25.426924 
(649f9b868f6783ec9de71c123212b908bf3b232e),platform=Linux 
4.4.0-57-generic x86_64)



/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/errorhandler.py:192 
in function check_response() [raise exception_class(message, screen, 
stacktrace)]


/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py:236 
in function execute() [self.error_handler.check_response(response)]


/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webelement.py:494 
in function _execute() [return self._parent.execute(command, params)]


/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webelement.py:92 
in function clear() [self._execute(Command.CLEAR_ELEMENT)]
  /home/jfb/.config/libreoffice/4/user/Scripts/python/funds/funds.py:29 
in function login() [username.clear()]
  /usr/lib/libreoffice/program/pythonscript.py:869 in function invoke() 
[ret = self.func( *args )]

)


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


Re: [Tutor] Open a libreoffice calc file in Python

2016-12-22 Thread boB Stepp
On Wed, Dec 21, 2016 at 10:50 PM,   wrote:

> To my mind the more important thing is to use the "shell=False" version of
> Popen. os.system() inherently accepts a shell command string, which means
> you need to hand quote the /home/path/to/myfile.ods. But it is better to
> pass an array of strings:
>
>  ['libreoffice', '--calc', path_value]
>
> where path_value is a Python variable containing "/home/path/to/myfile.ods"
> or whatever the path is. This way you don't need to do anything special for,
> um, "unusual" paths because you're not passing the string _through_ the
> shell.

Both you and Eryk seem to be speaking in terms of using
subprocess.Popen() directly.  So I think I need some clarification.
At https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module
it says:

"The recommended approach to invoking subprocesses is to use the run()
function for all use cases it can handle. For more advanced use cases,
the underlying Popen interface can be used directly.

"The run() function was added in Python 3.5; if you need to retain
compatibility with older versions, see the Older high-level API
section."

The OP is using Python 3.4, so that is why I referred him to the
"Older high-level API section".  Anyway this portion of the docs
suggests normally using subprocess.run().  OTOH, for using
subprocess.Popen() directly at
https://docs.python.org/3/library/subprocess.html#popen-constructor it
says:

"The underlying process creation and management in this module is
handled by the Popen class. It offers a lot of flexibility so that
developers are able to handle the less common cases not covered by the
convenience functions."

My current understanding as to why the subprocess module is preferred
to using the older os.system() is to avoid shell injection attacks.
So my assumption is that even when using "shell=True" with either
run() or Popen(), this is avoided.  Is this true?  So on to the
requested clarification:  Are there subtleties as to when to use run()
and when to use Popen()?

Thanks!

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


Re: [Tutor] Trouble Launching Python

2016-12-22 Thread Alan Gauld via Tutor
On 19/12/16 22:38, Joseph Olugbohunmi via Tutor wrote:
>  Hello,Good day, I installed Python 3.5.2 on my Windows 8.1 PC 

Are you sure you used the right Python version.
There are separate downloads for 32 and 64 bit machines.

It may be that you downloaded the 64 bit version and
have a 32bit OS running? I don't know if that would
give that error but it sounds like an OS type of issue.

-- 
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] Open a libreoffice calc file in Python

2016-12-22 Thread Alan Gauld via Tutor
On 22/12/16 03:37, Jim Byrnes wrote:
> Python 3.4 on Ubuntu
> 
> If I was going to open a libreoffice calc file from the terminal I would 
> go: libreoffice --calc /home/path/to/myfile.ods.
> 
> How would I do this from Python?

Others have advised how to run the Libreoffice app from
within Python.

If you really want to open the actual spreadsheet file
in Python rather than Libreoffice then its a bit more tricky.
If that is what you really meant get back to us and
we can start on the options available...


-- 
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] Using Python to solve factoria

2016-12-22 Thread Alan Gauld via Tutor
On 21/12/16 07:50, Hedgar wrote:

> I really happy to be accepted to the list!

You're welcome, but one very important thing to
note is that you should always post to this list
in plain text, not HTML. That's because HTML loses
the formatting of the code (see below) and in
Python formatting is very important.

> def factoria(numb):
> While numb > 1:
> If numb==0:
> return 1
> Else:
> result = numb*(numb-1)
> numb = numb -1
> return result
> factoria(5)

As you see we lost formatting, however this code
would never run because it is not valid Python.
Python is case sensitive so you need to use
'while' not 'While' etc.

> #should output 120
> What am I not getting right?

Another important point when postring is to
always include any error messages(in full)
that you get. They contain vital clues as
to what is wrong.

I see Bob Stepp has replied re the actual code,
so I assume his mail reader managed to interpret
it correctly - or maybe he just guessed. But
following the above guidelines will help you
get good answers more quickly in future.

-- 
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] Fwd: Question about selenium with python

2016-12-22 Thread Alan Gauld via Tutor
On 21/12/16 01:29, Fazal Khan wrote:

> Im a new programmer and this is my first time posting here. I have a
> question about using selenium with python. 

I notice you haven't had an answer yet.
That may be because Selenium is not part of the
standard Python library and this list is for questions
about the core language and its standard library.

You may be better off asking on a selenium specific
list or forum. This page lists several options:

http://www.seleniumhq.org/support/

Meanwhile, this page may help with your specific
question:

http://selenium-python.readthedocs.io/navigating.html

HTH
-- 
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