From rikudou__sennin at outlook.com  Sat Dec  3 13:53:45 2016
From: rikudou__sennin at outlook.com (adil gourinda)
Date: Sat, 3 Dec 2016 18:53:45 +0000
Subject: [Tutor] Python Documentation
Message-ID: <CY1PR16MB00964F882E0DFAAB8972DE95B6810@CY1PR16MB0096.namprd16.prod.outlook.com>

   Hi everyone



   Those are some free resources for Python documentation in addition to your tutorial and I wish that can help the new peoples who want to learn Python.


   I wish that I was helpful


.www.tutorialspoint.com/python/index.htm<http://www.tutorialspoint.com/python/index.htm>

[https://www.tutorialspoint.com/python/images/python-mini.jpg]<http://www.tutorialspoint.com/python/index.htm>

Python - Tutorial<http://www.tutorialspoint.com/python/index.htm>
www.tutorialspoint.com
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990.




.www.tutorialspoint.com/python3/index.htm<http://www.tutorialspoint.com/python3/index.htm>

[https://www.tutorialspoint.com/python3/images/python.jpg]<http://www.tutorialspoint.com/python3/index.htm>

Python 3 tutorial - Tutorials for Kanban, Erlang, SAP ...<http://www.tutorialspoint.com/python3/index.htm>
www.tutorialspoint.com
Python 3 Tutorial for Beginners - Learn Python 3 in simple and easy steps starting from basic to advanced concepts with examples including Python 3 Syntax Object ...




.python.cs.southern.edu/pythonbook/pythonbook.pdf<http://python.cs.southern.edu/pythonbook/pythonbook.pdf>

P y th o n - Southern Adventist University<http://python.cs.southern.edu/pythonbook/pythonbook.pdf>
python.cs.southern.edu
1 Chapter 1 The Context of Software Development A computer program, from one perspective, is a sequence of instructions that dictate the ?ow of electri-




From kumarmysore at hotmail.com  Mon Dec  5 11:20:56 2016
From: kumarmysore at hotmail.com (anatta anatta)
Date: Mon, 5 Dec 2016 16:20:56 +0000
Subject: [Tutor] copy files selectively from source to destination
Message-ID: <DB6P192MB02132FDCEF31A39D628E7679B0830@DB6P192MB0213.EURP192.PROD.OUTLOOK.COM>

Dear tutor!



Here is my working code - to copy files from one drive to another.

I however want to copy selective files.

For example I like to copy only .txt files only from the source to destination, and not other types of files.

How could I do this selective copying?

Thanks in advance for the hints.


Best,

Kumar.


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 01 17:05:07 2016

@author: anatta
"""

import os
import shutil
sourcePath = r'H://'
destPath = r'O://test_o/'
ls=os.listdir('.')#list current dir
#print('listing current dir\n')
#print(ls)
for root, dirs, files in os.walk(sourcePath):

    #figure out where we're going
    dest = destPath + root.replace(sourcePath, '')

    #if we're in a directory that doesn't exist in the destination folder
    #then create a new folder
    if not os.path.isdir(dest):
        os.mkdir(dest)
        print 'Directory created at: ' + dest

    #loop through all files in the directory
    for f in files:

        #compute current (old) & new file locations
        oldLoc = root + '\\' + f
        newLoc = dest + '\\' + f

        if not os.path.isfile(newLoc):
            try:
                shutil.copy2(oldLoc, newLoc)
                print 'File ' + f + ' copied.'
            except IOError:
                print 'file "' + f + '" already exists'


___



From emilevansebille at gmail.com  Mon Dec  5 19:03:54 2016
From: emilevansebille at gmail.com (Emile van Sebille)
Date: Mon, 05 Dec 2016 16:03:54 -0800
Subject: [Tutor] copy files selectively from source to destination
In-Reply-To: <DB6P192MB02132FDCEF31A39D628E7679B0830@DB6P192MB0213.EURP192.PROD.OUTLOOK.COM>
References: <DB6P192MB02132FDCEF31A39D628E7679B0830@DB6P192MB0213.EURP192.PROD.OUTLOOK.COM>
Message-ID: <o24v96$qc0$1@blaine.gmane.org>

On 12/05/2016 08:20 AM, anatta anatta wrote:
> Dear tutor!
>
>
>
> Here is my working code - to copy files from one drive to another.
>
> I however want to copy selective files.
>
> For example I like to copy only .txt files only from the source to destination, and not other types of files.
>
> How could I do this selective copying?

You could test f as you loop over the files to ensure it ends '.txt'.

Emile


>
> Thanks in advance for the hints.
>
>
> Best,
>
> Kumar.
>
>
> # -*- coding: utf-8 -*-
> """
> Created on Wed Jun 01 17:05:07 2016
>
> @author: anatta
> """
>
> import os
> import shutil
> sourcePath = r'H://'
> destPath = r'O://test_o/'
> ls=os.listdir('.')#list current dir
> #print('listing current dir\n')
> #print(ls)
> for root, dirs, files in os.walk(sourcePath):
>
>      #figure out where we're going
>      dest = destPath + root.replace(sourcePath, '')
>
>      #if we're in a directory that doesn't exist in the destination folder
>      #then create a new folder
>      if not os.path.isdir(dest):
>          os.mkdir(dest)
>          print 'Directory created at: ' + dest
>
>      #loop through all files in the directory
>      for f in files:
>
>          #compute current (old) & new file locations
>          oldLoc = root + '\\' + f
>          newLoc = dest + '\\' + f
>
>          if not os.path.isfile(newLoc):
>              try:
>                  shutil.copy2(oldLoc, newLoc)
>                  print 'File ' + f + ' copied.'
>              except IOError:
>                  print 'file "' + f + '" already exists'
>
>
> ___
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



From alan.gauld at yahoo.co.uk  Mon Dec  5 19:05:08 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Tue, 6 Dec 2016 00:05:08 +0000
Subject: [Tutor] copy files selectively from source to destination
In-Reply-To: <DB6P192MB02132FDCEF31A39D628E7679B0830@DB6P192MB0213.EURP192.PROD.OUTLOOK.COM>
References: <DB6P192MB02132FDCEF31A39D628E7679B0830@DB6P192MB0213.EURP192.PROD.OUTLOOK.COM>
Message-ID: <o24vbe$3d2$1@blaine.gmane.org>

On 05/12/16 16:20, anatta anatta wrote:

> I however want to copy selective files.
> For example I like to copy only .txt files only 

> How could I do this selective copying?

By applying an if test just before you copy

filetype = '.txt'  # or read it as an input
...
    if sourcefile extension == filetype
       copy sourcefile to destination

You can extract the extension using the
os.path module functions such as

splitext(p)
    Split the extension from a pathname.

    Extension is everything from the last dot to the end, ignoring
    leading dots.  Returns "(root, ext)"; ext may be empty.
(END)


>         if not os.path.isfile(newLoc):
>             try:

The if test goes right about here... or you could
even put it higher up before all your tests.

>                 shutil.copy2(oldLoc, newLoc)
>                 print 'File ' + f + ' copied.'
>             except IOError:
>                 print 'file "' + f + '" already exists'

An alternative option is to use glob.glob on the current
folder to get a list of files that match your desired
pattern and copy only those files.


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



From __peter__ at web.de  Tue Dec  6 06:13:03 2016
From: __peter__ at web.de (Peter Otten)
Date: Tue, 06 Dec 2016 12:13:03 +0100
Subject: [Tutor] copy files selectively from source to destination
References: <DB6P192MB02132FDCEF31A39D628E7679B0830@DB6P192MB0213.EURP192.PROD.OUTLOOK.COM>
Message-ID: <o266fv$sju$1@blaine.gmane.org>

anatta anatta wrote:

> Here is my working code - to copy files from one drive to another.
> 
> I however want to copy selective files.
> 
> For example I like to copy only .txt files only from the source to
> destination, and not other types of files.
> 
> How could I do this selective copying?

Such a question almost answers itself when you put a bit more structure into 
your code. You might write a generator that produces (sourcefile, destfile) 
pairs and a copyfile() function that performs the same checks you have 
inlined in your code below. A sketch of the resulting stript:

def filepairs(sourcefolder, destfolder):
    for root, dirs, files in os.walk(sourcefolder):
        for name in files:
             sourcefile = ...
             destfile = ...
             yield sourcefile, destfile

def copyfile(sourcefile, destfile):
    if not os.path.isfile(destfile):
        ... # copy
    else:
        ... # complain
 

for sourcefile, destfile in filepairs("H://", "O://test_o"):
    copyfile(sourcefile, destfile)

To copy only select files you have to add a check to the for loop:

for sourcefile, destfile in filepairs(...):
   if copy_wanted(sourcefile):
       copyfile(sourcefile, destfile)

Now you can experiment with various implementations of that function without 
touching the bulk of your code. To copy only text files it might look like 
this

def copy_wanted(sourcefile):
    return os.path.splitext(sourcefile)[0] == ".txt"

... or this

def predicate_from_glob(glob):
    def is_match(filename):
        return fnmatch.fnmatch(filename, glob)
    return is_match

copy_wanted = predicate_from_glob("*.txt")

... or something completely different like, say, a check based on the MIME 
type.

> 
> Thanks in advance for the hints.
> 
> 
> Best,
> 
> Kumar.
> 
> 
> # -*- coding: utf-8 -*-
> """
> Created on Wed Jun 01 17:05:07 2016
> 
> @author: anatta
> """
> 
> import os
> import shutil
> sourcePath = r'H://'
> destPath = r'O://test_o/'
> ls=os.listdir('.')#list current dir
> #print('listing current dir\n')
> #print(ls)
> for root, dirs, files in os.walk(sourcePath):
> 
>     #figure out where we're going
>     dest = destPath + root.replace(sourcePath, '')
> 
>     #if we're in a directory that doesn't exist in the destination folder
>     #then create a new folder
>     if not os.path.isdir(dest):
>         os.mkdir(dest)
>         print 'Directory created at: ' + dest
> 
>     #loop through all files in the directory
>     for f in files:
> 
>         #compute current (old) & new file locations
>         oldLoc = root + '\\' + f
>         newLoc = dest + '\\' + f
> 
>         if not os.path.isfile(newLoc):
>             try:
>                 shutil.copy2(oldLoc, newLoc)
>                 print 'File ' + f + ' copied.'
>             except IOError:
>                 print 'file "' + f + '" already exists'
> 
> 
> ___
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



From palani at vahaitech.com  Thu Dec  8 01:04:47 2016
From: palani at vahaitech.com (Palanikumar)
Date: Thu, 8 Dec 2016 11:34:47 +0530
Subject: [Tutor] function argument unpacking
Message-ID: <f6bedcca-f894-7dbe-defc-39226c2a9539@vahaitech.com>

#Function Argument unpacking
def myfunc(x, y, z):
     print(x. v. z)

tuple_vec = {1, 0, 1)
dict_vec = {'x':1, 'y':0, 'z':1}

myfunc(*tuple_vec)
myfunc(*dict_vec)


It returns following error


File "func.py", line 8
     tuple_vec = {1, 0, 1)
                         ^
SyntaxError: invalid syntax


How did i solve it?

From __peter__ at web.de  Thu Dec  8 04:03:05 2016
From: __peter__ at web.de (Peter Otten)
Date: Thu, 08 Dec 2016 10:03:05 +0100
Subject: [Tutor] function argument unpacking
References: <f6bedcca-f894-7dbe-defc-39226c2a9539@vahaitech.com>
Message-ID: <o2b7k5$5c7$1@blaine.gmane.org>

Palanikumar wrote:

> File "func.py", line 8
>      tuple_vec = {1, 0, 1)
>                          ^
> SyntaxError: invalid syntax

The opening and the closing parenthesis have to match. 
To get a tuple:

tuple_vec = (1, 0, 1)

To get a set (with two values in undefined order, so not a good match for 
your use case):

set_val = {1, 0, 1}




From alan.gauld at yahoo.co.uk  Thu Dec  8 04:11:22 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Thu, 8 Dec 2016 09:11:22 +0000
Subject: [Tutor] function argument unpacking
In-Reply-To: <f6bedcca-f894-7dbe-defc-39226c2a9539@vahaitech.com>
References: <f6bedcca-f894-7dbe-defc-39226c2a9539@vahaitech.com>
Message-ID: <o2b83k$pfr$1@blaine.gmane.org>

On 08/12/16 06:04, Palanikumar wrote:
> #Function Argument unpacking
> def myfunc(x, y, z):
>      print(x. v. z)
> 

Please always send the actual code that generates
the error, do not retype as it causes us to chase
phantom bugs. In this case the fact that the v
in the print statement should be a y and that
you are using periods as separators instead
of commas...

> File "func.py", line 8
>      tuple_vec = {1, 0, 1)
>                          ^
> SyntaxError: invalid syntax

Change the opening brace { to a parenthesis (

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



From edmundb at talktalk.net  Thu Dec  8 07:45:31 2016
From: edmundb at talktalk.net (Edmund Butterworth)
Date: Thu, 8 Dec 2016 12:45:31 +0000
Subject: [Tutor] Regex ^$ not behaving as expected
Message-ID: <1a4c9c13-fcc9-517d-8a43-33b6343123a4@talktalk.net>

Hello,

I am new to Python and trying to get to grips with the re regex module. 
I?m running Python 3.4 under Debian Jessie with a Cinnamon desktop.

My understanding is that when the re.M flag is raised, |^| will match at 
the beginning of the string and also at the beginning of each line 
within the string immediately following each newline \n. Similarly, |$ 
|matches both at the end of the whole string and at the end of each line 
immediately preceding the \n.

However I don?t seem to be able to get re.sub (for instance) to behave 
in this way. Both ^ and $ seem only to match the beginning and end of 
the string and not the individual lines within it. The session below 
hopefully demonstrates my confusion.


Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.

 >>> import re
 >>> s = "AAAcBBB\nAAAdBBB"
 >>> print(s)
AAAcBBB
AAAdBBB
 >>> print(re.sub(r'^.+?c', "X", s))
XBBB
AAAdBBB
 >>> print(re.sub(r'^.+?d', "X", s, re.M))
AAAcBBB
AAAdBBB
 >>> print(re.sub(r'^.+?d', "X", s, re.M, re.S))
XBBB
 >>> print(re.sub(r'd.+?$', "X", s))
AAAcBBB
AAAX
 >>> print(re.sub(r'c.+?$', "X", s, re.M))
AAAcBBB
AAAdBBB
 >>> print(re.sub(r'c.+?$', "X", s, re.M, re.S))
AAAX
 >>>

What am I doing wrong? Thank you

Ed





From MAli6 at dixonsaa.com  Thu Dec  8 05:00:54 2016
From: MAli6 at dixonsaa.com (M Ali)
Date: Thu, 8 Dec 2016 10:00:54 +0000
Subject: [Tutor] 1 to 49 numbered grid
Message-ID: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>

Hi Team

I was wondering if you can help me, as I am struggling to create a numbered grid in Python. I am trying to be able to create a snakes and ladders game in Python and it must have a numbered grid and involve 2 players. I would appreciate it if you can help me or guide me to create this game.

Regards

Mr Ali

From robertvstepp at gmail.com  Thu Dec  8 09:51:28 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Thu, 8 Dec 2016 08:51:28 -0600
Subject: [Tutor] 1 to 49 numbered grid
In-Reply-To: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
References: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
Message-ID: <CANDiX9JmHj_uymvLuRX6H5Ui2oE_RBEs-n9ws_r_TQuCpSN+TA@mail.gmail.com>

Greetings!

On Thu, Dec 8, 2016 at 4:00 AM, M Ali <MAli6 at dixonsaa.com> wrote:
>
> I was wondering if you can help me, as I am struggling to create a numbered grid in Python. I am trying to be able to create a snakes and ladders game in Python and it must have a numbered grid and involve 2 players. I would appreciate it if you can help me or guide me to create this game.

There are plenty of people here ready and willing to help.  However,
you will need to be much more specific on where you are stuck.
Normally you should clearly state the problem you are trying to solve,
show the code relevant to your problem that you have written (copy and
paste), give any error tracebacks in full (copy and paste), state what
you were trying to do and what you expected, etc.  It is also usually
helpful to state what version of Python you are using and your
operating system.  Be forewarned:  We will *not* do your homework for
you.  You need to show effort and we will help you when you get stuck.

As to what little you said, if you are printing your grid of numbers
to a terminal window, perhaps using triple quotes might be helpful?
Suppose you wanted a big number one:

print("""
   __
 /    |
      |
      |
      |
      |
 ___|___
""")

You could do things like set a variable equal to such a string.  For
instance you can replace the print() with "one =" and put your
triple-quoted string here.  You could do the same with variables two,
three, ... , nine.  This might give you more flexibility down the
line.  For instance you could have a dictionary of big numbers based
on doing this that might correlate with your grid numbers.  As for
your grid you could do similar things, break your grid boxes into
repeating tiles and store these elements in a suitable variable to
reuse as needed.  I am being very general here, but perhaps this can
give you some ideas.

HTH!


-- 
boB

From robertvstepp at gmail.com  Thu Dec  8 10:01:38 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Thu, 8 Dec 2016 09:01:38 -0600
Subject: [Tutor] 1 to 49 numbered grid
In-Reply-To: <CANDiX9JmHj_uymvLuRX6H5Ui2oE_RBEs-n9ws_r_TQuCpSN+TA@mail.gmail.com>
References: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
 <CANDiX9JmHj_uymvLuRX6H5Ui2oE_RBEs-n9ws_r_TQuCpSN+TA@mail.gmail.com>
Message-ID: <CANDiX9LVJdwRGWYZXJ3FKAmDQ-GLzSCGFtcjxYm1L+bf2n0kyQ@mail.gmail.com>

Apologies!  My ever friendly Gmail reset my font preferences along the way
and put me on a non-monospace font.  However, I hope the OP can determine
my intent below despite the misalignment of my big one.

Cheers!
boB

On Thu, Dec 8, 2016 at 8:51 AM, boB Stepp <robertvstepp at gmail.com> wrote:
> Greetings!
>
> On Thu, Dec 8, 2016 at 4:00 AM, M Ali <MAli6 at dixonsaa.com> wrote:
>>
>> I was wondering if you can help me, as I am struggling to create a
numbered grid in Python. I am trying to be able to create a snakes and
ladders game in Python and it must have a numbered grid and involve 2
players. I would appreciate it if you can help me or guide me to create
this game.
>
> There are plenty of people here ready and willing to help.  However,
> you will need to be much more specific on where you are stuck.
> Normally you should clearly state the problem you are trying to solve,
> show the code relevant to your problem that you have written (copy and
> paste), give any error tracebacks in full (copy and paste), state what
> you were trying to do and what you expected, etc.  It is also usually
> helpful to state what version of Python you are using and your
> operating system.  Be forewarned:  We will *not* do your homework for
> you.  You need to show effort and we will help you when you get stuck.
>
> As to what little you said, if you are printing your grid of numbers
> to a terminal window, perhaps using triple quotes might be helpful?
> Suppose you wanted a big number one:
>
> print("""
>    __
>  /    |
>       |
>       |
>       |
>       |
>  ___|___
> """)
>
> You could do things like set a variable equal to such a string.  For
> instance you can replace the print() with "one =" and put your
> triple-quoted string here.  You could do the same with variables two,
> three, ... , nine.  This might give you more flexibility down the
> line.  For instance you could have a dictionary of big numbers based
> on doing this that might correlate with your grid numbers.  As for
> your grid you could do similar things, break your grid boxes into
> repeating tiles and store these elements in a suitable variable to
> reuse as needed.  I am being very general here, but perhaps this can
> give you some ideas.
>
> HTH!
>
>
> --
> boB



-- 
boB

From robertvstepp at gmail.com  Thu Dec  8 10:39:04 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Thu, 8 Dec 2016 09:39:04 -0600
Subject: [Tutor] 1 to 49 numbered grid
In-Reply-To: <DB4PR01MB0463DECA81432BAFC12CDCA38D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
References: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
 <CANDiX9JmHj_uymvLuRX6H5Ui2oE_RBEs-n9ws_r_TQuCpSN+TA@mail.gmail.com>
 <CANDiX9LVJdwRGWYZXJ3FKAmDQ-GLzSCGFtcjxYm1L+bf2n0kyQ@mail.gmail.com>
 <DB4PR01MB0463DECA81432BAFC12CDCA38D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
Message-ID: <CANDiX9KbmN+hT4ZwfyCnsAr7cYN4MAo61tjcfbkZeBJ1R1wtKQ@mail.gmail.com>

Please respond to the entire Tutor list.  Also, note that Tutor is a plain
text only list so your image is not going to show up.

On Thu, Dec 8, 2016 at 9:21 AM, M Ali <MAli6 at dixonsaa.com> wrote:
>
> Hi Bob
>
>
>
> Thank you ever so much in getting in touch. I have been given an exemplar
work of what I am suppose do by examination board, but it?s in VB and I am
using Python 3.3.4 version. I do not know where to start from as I have
never created a program like this before. I am trying to create something
like this in Python and was wondering would it be possible.
>
< Here the OP had inserted an image of a GUI version of the game with 49
numbered buttons in a 7x7 display along with other buttons to perform game
functions.>
>
>
> My students need to create something like the above, but I am not sure
where to start from and any help or start will be much appreciated.

If you are used to VB and you have not coded in Python before then you will
have some catch up to do!  Python can do GUIs, but the standard library
does not come with drag-and-drop GUI building capabilities.  tkinter is the
GUI library that comes with Python.  There are third party libraries out
there that lots of people use as well.  But if you use tkinter, then you
will have to code the GUI elements by hand.  https://docs.python.org/3.3/
are the Python docs for version 3.3 (More specifically 3.3.6).  There is a
tutorial on the language there as well as a standard library reference that
has the docs on tkinter.  So your project is quite doable once you learn
what you need to learn!  If you are comfortable programming in VB, then I
would think that the Python tutorial can get you quickly up to speed on
using Python.  But if you have never hand-coded GUIs before, you may
struggle with tkinter, but the good news is there is a lot of tkinter
documentation out on the Web.

boB

From robertvstepp at gmail.com  Thu Dec  8 10:41:11 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Thu, 8 Dec 2016 09:41:11 -0600
Subject: [Tutor] FW:  1 to 49 numbered grid
In-Reply-To: <DB4PR01MB0463601443F13CFD869EA9D48D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
References: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
 <CANDiX9JmHj_uymvLuRX6H5Ui2oE_RBEs-n9ws_r_TQuCpSN+TA@mail.gmail.com>
 <CANDiX9LVJdwRGWYZXJ3FKAmDQ-GLzSCGFtcjxYm1L+bf2n0kyQ@mail.gmail.com>
 <DB4PR01MB0463601443F13CFD869EA9D48D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
Message-ID: <CANDiX9+Rj39P45Aw7Vw4sKo_s9zLHE9NsHxPD+9-nAemf2v=qg@mail.gmail.com>

Forwarding this to the Tutor list.  Please note that your formatting
will be lost.  Again, this is a plain text list.  Also, please don't
top-post.

On Thu, Dec 8, 2016 at 9:38 AM, M Ali <MAli6 at dixonsaa.com> wrote:
> Hi Bob
>
>
>
> I am trying to create the below and wanted an idea as to how I can start it
> off. I am willing to put the hours in and create the program myself from
> scratch before I can start teaching my students it. I would appreciate it if
> you can guide me and get me started:
>
>
>
> ?      There are board positions 1 to 49
>
> ?      There are two players
>
> ?      Each player takes it in turn to play
>
> ?      A player rolls two dice (1?6 each) o Display value of dice rolled o
> Player moves that number of places
>
> o   IF a double is rolled, then move back that number of spaces
>
> o   Message must be displayed with dice numbers o Message must be displayed
> when a double is rolled
>
> ?      Player wins by getting to square 49 o Does not need to be exact
>
> ?      Must have a start game message
>
> ?      Must have a 'won' message
>
> ?      All messages need to be stored in a text file and loaded at the start
> of the program
>
> ?      Need to have 4 obstacles stored in a text file, along with the number
> of squares they move forward or backward by.  Must be loaded at the start of
> the game
>
>
>
>
>
> From: M Ali
> Sent: 08 December 2016 15:22
> To: 'boB Stepp'
> Subject: RE: [Tutor] 1 to 49 numbered grid
> Importance: High
>
>
>
> Hi Bob
>
>
>
> Thank you ever so much in getting in touch. I have been given an exemplar
> work of what I am suppose do by examination board, but it?s in VB and I am
> using Python 3.3.4 version. I do not know where to start from as I have
> never created a program like this before. I am trying to create something
> like this in Python and was wondering would it be possible.
>
>
>
>
>
> My students need to create something like the above, but I am not sure where
> to start from and any help or start will be much appreciated.
>
>
>
> Regards
>
>
>
> Mr Ali
>
>
>
> From: boB Stepp [mailto:robertvstepp at gmail.com]
> Sent: 08 December 2016 15:02
> To: M Ali
> Cc: tutor at python.org
> Subject: Re: [Tutor] 1 to 49 numbered grid
>
>
>
> Apologies!  My ever friendly Gmail reset my font preferences along the way
> and put me on a non-monospace font.  However, I hope the OP can determine my
> intent below despite the misalignment of my big one.
>
>
>
> Cheers!
>
> boB
>
> On Thu, Dec 8, 2016 at 8:51 AM, boB Stepp <robertvstepp at gmail.com> wrote:
>> Greetings!
>>
>> On Thu, Dec 8, 2016 at 4:00 AM, M Ali <MAli6 at dixonsaa.com> wrote:
>>>
>>> I was wondering if you can help me, as I am struggling to create a
>>> numbered grid in Python. I am trying to be able to create a snakes and
>>> ladders game in Python and it must have a numbered grid and involve 2
>>> players. I would appreciate it if you can help me or guide me to create this
>>> game.
>>
>> There are plenty of people here ready and willing to help.  However,
>> you will need to be much more specific on where you are stuck.
>> Normally you should clearly state the problem you are trying to solve,
>> show the code relevant to your problem that you have written (copy and
>> paste), give any error tracebacks in full (copy and paste), state what
>> you were trying to do and what you expected, etc.  It is also usually
>> helpful to state what version of Python you are using and your
>> operating system.  Be forewarned:  We will *not* do your homework for
>> you.  You need to show effort and we will help you when you get stuck.
>>
>> As to what little you said, if you are printing your grid of numbers
>> to a terminal window, perhaps using triple quotes might be helpful?
>> Suppose you wanted a big number one:
>>
>> print("""
>>    __
>>  /    |
>>       |
>>       |
>>       |
>>       |
>>  ___|___
>> """)
>>
>> You could do things like set a variable equal to such a string.  For
>> instance you can replace the print() with "one =" and put your
>> triple-quoted string here.  You could do the same with variables two,
>> three, ... , nine.  This might give you more flexibility down the
>> line.  For instance you could have a dictionary of big numbers based
>> on doing this that might correlate with your grid numbers.  As for
>> your grid you could do similar things, break your grid boxes into
>> repeating tiles and store these elements in a suitable variable to
>> reuse as needed.  I am being very general here, but perhaps this can
>> give you some ideas.
>>
>> HTH!
>>
>>
>> --
>> boB
>
>
>
> --
> boB



-- 
boB

From bgailer at gmail.com  Thu Dec  8 10:41:20 2016
From: bgailer at gmail.com (Bob Gailer)
Date: Thu, 8 Dec 2016 10:41:20 -0500
Subject: [Tutor] 1 to 49 numbered grid
In-Reply-To: <CAP1rxO6ao1-a52GPhB+KFRmbt9qWEVHFLQwXd8HwauMNo-C3Bg@mail.gmail.com>
References: <DB4PR01MB046359823B986F06EB68E4F28D840@DB4PR01MB0463.eurprd01.prod.exchangelabs.com>
 <CAP1rxO6oveG450RFWLJa1r3CY9qDSQJzrV6jj33bxR5-rsKmeQ@mail.gmail.com>
 <CAP1rxO6ao1-a52GPhB+KFRmbt9qWEVHFLQwXd8HwauMNo-C3Bg@mail.gmail.com>
Message-ID: <CAP1rxO7pV88g1CRgzL+9Y9-G+Fo3goWWBMnKei93QJygHXB33A@mail.gmail.com>

On Dec 8, 2016 8:52 AM, "M Ali" <MAli6 at dixonsaa.com> wrote:
>
> Hi Team
>
> I was wondering if you can help me, as I am struggling to create a
numbered grid in Python. I am trying to be able to create a snakes and
ladders game in Python and it must have a numbered grid and involve 2
players. I would appreciate it if you can help me or guide me to create
this game.
Assuming you are taking a class, what tools have you been given?
What Graphics packages?
The more information you give us the more helpful we can be. Be assured we
are here to help.
>
> Regards
>
> Mr Ali
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

From dyoo at hashcollision.org  Thu Dec  8 11:58:40 2016
From: dyoo at hashcollision.org (Danny Yoo)
Date: Thu, 8 Dec 2016 08:58:40 -0800
Subject: [Tutor] Regex ^$ not behaving as expected
In-Reply-To: <1a4c9c13-fcc9-517d-8a43-33b6343123a4@talktalk.net>
References: <1a4c9c13-fcc9-517d-8a43-33b6343123a4@talktalk.net>
Message-ID: <CAGZAPF4nV3pVKfQA-BfADQ350x3KD1iu-xrV35zFNOgXS-ECLg@mail.gmail.com>

Hi Edmund,

For each of the cases that surprise you, next time, can you also say
what you expected to see?  That can help us see where the confusion
lies; as it stands, if we have the same mental model as what's
happening in Python, then the results look correct to us.  :P


I can guess at what you were expecting.  For example, a variation of
one of your cases would be:

>>> print(re.sub(r'^AAA', "aaa", s, re.MULTILINE))
aaacBBB
AAAdBBB

where we would have expected both occurrences of 'AAA' to be replaced
by their lowercased examples, but instead, it seems like it's only
matching the first occurrence.


If that's your expectation as well, then yes, we agree, that looks
weird.  Let's look at the documentation for re.sub.

    https://docs.python.org/3/library/re.html#re.sub

... Oh!  It looks like it takes several potential optional parameters,
not only 'flags', but 'count' as well.


Perhaps we've passed re.MULTILINE by accident as a 'count'.  Let's
explicitly pass it as the 'flags' argument instead.  Does that make a
difference

>>> print(re.sub(r'^AAA', "aaa", s, flags=re.MULTILINE))
aaacBBB
aaadBBB



Yes!  Ok, so that result looks more reasonable.  So I think that's
where the problem is.


I'm still somewhat confused as to what the regexp module is doing when
passing a non-numeric count parameter.  That looks like it should
raise a TypeError to me, so perhaps someone needs to file a bug
against the standard library?  Unsure.

From dyoo at hashcollision.org  Thu Dec  8 12:03:49 2016
From: dyoo at hashcollision.org (Danny Yoo)
Date: Thu, 8 Dec 2016 09:03:49 -0800
Subject: [Tutor] Regex ^$ not behaving as expected
In-Reply-To: <CAGZAPF4nV3pVKfQA-BfADQ350x3KD1iu-xrV35zFNOgXS-ECLg@mail.gmail.com>
References: <1a4c9c13-fcc9-517d-8a43-33b6343123a4@talktalk.net>
 <CAGZAPF4nV3pVKfQA-BfADQ350x3KD1iu-xrV35zFNOgXS-ECLg@mail.gmail.com>
Message-ID: <CAGZAPF4-h2Bytoxw1aASQMn1-Z6YyV9PxeooVLmU1XFEpVSKtQ@mail.gmail.com>

> I'm still somewhat confused as to what the regexp module is doing when
> passing a non-numeric count parameter.  That looks like it should
> raise a TypeError to me, so perhaps someone needs to file a bug
> against the standard library?  Unsure.


Ok, I'm filing a bug to the Python developers so that hopefully this
gets fixed soon.  For reference:

    http://bugs.python.org/issue28905


Hope this helps!

From dyoo at hashcollision.org  Thu Dec  8 12:20:19 2016
From: dyoo at hashcollision.org (Danny Yoo)
Date: Thu, 8 Dec 2016 09:20:19 -0800
Subject: [Tutor] Regex ^$ not behaving as expected
In-Reply-To: <CAGZAPF4-h2Bytoxw1aASQMn1-Z6YyV9PxeooVLmU1XFEpVSKtQ@mail.gmail.com>
References: <1a4c9c13-fcc9-517d-8a43-33b6343123a4@talktalk.net>
 <CAGZAPF4nV3pVKfQA-BfADQ350x3KD1iu-xrV35zFNOgXS-ECLg@mail.gmail.com>
 <CAGZAPF4-h2Bytoxw1aASQMn1-Z6YyV9PxeooVLmU1XFEpVSKtQ@mail.gmail.com>
Message-ID: <CAGZAPF6Xacyp3S1OKtcu+9hyZuDm0-Zs=PM_teFgcNHQiYOamg@mail.gmail.com>

Following up: drats!  Detecting this conceptual TypeError is not
feasible under the current design, due to the choice of data
representation used in this API.

The reason is because the flags are being represented as integers, and
we're using bitwise operations to define the union of flags.  That is:

#######################################
>>> import re
>>> re.MULTILINE
8
>>> re.DOTALL
16
>>> re.MULTILINE | re.DOTALL
24
########################################

Flags are integers here.  Since re.sub is taking two optional
integer-based arguments, count and flags, Python's type system cannot
determine that we've passed flags in the wrong place, because it
legitimately looks like a valid count too.  Bits are bits.

This is a very rough design edge and quite unfortunate.  In an ideal
world, I can imagine that the representation of flags would be
different such that this mistake could be caught earlier.  But in
absence of this, we've just got to be careful, I suppose.  :(


Anyway, hope this helps!

From dyoo at hashcollision.org  Thu Dec  8 12:43:03 2016
From: dyoo at hashcollision.org (Danny Yoo)
Date: Thu, 8 Dec 2016 09:43:03 -0800
Subject: [Tutor] function argument unpacking
In-Reply-To: <o2b83k$pfr$1@blaine.gmane.org>
References: <f6bedcca-f894-7dbe-defc-39226c2a9539@vahaitech.com>
 <o2b83k$pfr$1@blaine.gmane.org>
Message-ID: <CAGZAPF6M-frM0Vj8UgZwXg4wN5J89RyU19C9kG=koLszYNmPwA@mail.gmail.com>

On Thu, Dec 8, 2016 at 1:11 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 08/12/16 06:04, Palanikumar wrote:
>> #Function Argument unpacking
>> def myfunc(x, y, z):
>>      print(x. v. z)
>>
>
> Please always send the actual code that generates
> the error, do not retype as it causes us to chase
> phantom bugs. In this case the fact that the v
> in the print statement should be a y and that
> you are using periods as separators instead
> of commas...
>
>> File "func.py", line 8
>>      tuple_vec = {1, 0, 1)
>>                          ^
>> SyntaxError: invalid syntax
>
> Change the opening brace { to a parenthesis (


Hi Palanikumar,

Also, when you have time, see if you can use a text editor or IDE that
does "syntax highlighting".  You should be able to catch this kind of
error early on, during program entry, if the editing environment is
smart enough.  Most IDEs that are Python-aware will tell you, via
textual hints or coloring, when the parentheses, brackets or braces
have been mismatching.

See:  https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
for a list of known text editors or IDEs that are Python-aware.  I
personally use Emacs, but I've heard very good things about other
environments like Vim, Sublime Text, and others.  You may want to ask
other peers about their recommendations.  A good editing environment
can be a valuable tool for programming.

From edmundb at talktalk.net  Thu Dec  8 13:53:53 2016
From: edmundb at talktalk.net (Edmund Butterworth)
Date: Thu, 8 Dec 2016 18:53:53 +0000
Subject: [Tutor] Regex ^$ not behaving as expected
In-Reply-To: <CAGZAPF6Xacyp3S1OKtcu+9hyZuDm0-Zs=PM_teFgcNHQiYOamg@mail.gmail.com>
References: <1a4c9c13-fcc9-517d-8a43-33b6343123a4@talktalk.net>
 <CAGZAPF4nV3pVKfQA-BfADQ350x3KD1iu-xrV35zFNOgXS-ECLg@mail.gmail.com>
 <CAGZAPF4-h2Bytoxw1aASQMn1-Z6YyV9PxeooVLmU1XFEpVSKtQ@mail.gmail.com>
 <CAGZAPF6Xacyp3S1OKtcu+9hyZuDm0-Zs=PM_teFgcNHQiYOamg@mail.gmail.com>
Message-ID: <fb976945-ff69-a2cb-d67d-cf5a5918d0a9@talktalk.net>

Dear Danny,

That was great, just the information I wanted and very prompt too. I've 
tried the /f//lags  =/ and it works. Thank you so much.

I had been banging my head against this for the last two days, googling 
the universe and even considering going back to PERL, but for a lot of 
reasons I want to do this in Python. I can and shall now.

Best regards

Ed

-------- Original Message --------
*Subject: *Re: [Tutor] Regex ^$ not behaving as expected
*From: *Danny Yoo <dyoo at hashcollision.org>
*To: *Edmund Butterworth <edmundb at talktalk.net>
*Cc: *Python Tutor Mailing List <tutor at python.org>
*Date: *08/12/2016, 17:20:19
> Following up: drats!  Detecting this conceptual TypeError is not
> feasible under the current design, due to the choice of data
> representation used in this API.
>
> The reason is because the flags are being represented as integers, and
> we're using bitwise operations to define the union of flags.  That is:
>
> #######################################
>>>> import re
>>>> re.MULTILINE
> 8
>>>> re.DOTALL
> 16
>>>> re.MULTILINE | re.DOTALL
> 24
> ########################################
>
> Flags are integers here.  Since re.sub is taking two optional
> integer-based arguments, count and flags, Python's type system cannot
> determine that we've passed flags in the wrong place, because it
> legitimately looks like a valid count too.  Bits are bits.
>
> This is a very rough design edge and quite unfortunate.  In an ideal
> world, I can imagine that the representation of flags would be
> different such that this mistake could be caught earlier.  But in
> absence of this, we've just got to be careful, I suppose.  :(
>
>
> Anyway, hope this helps!
>


From isaac.tetteh at jacks.sdstate.edu  Thu Dec  8 20:45:05 2016
From: isaac.tetteh at jacks.sdstate.edu (Tetteh, Isaac  - SDSU Student)
Date: Fri, 9 Dec 2016 01:45:05 +0000
Subject: [Tutor] (no subject)
Message-ID: <9q2vi2s8ape460suids4mvou.1481247361947@email.lge.com>

Hello,

I am trying to find the number of times a word occurs on a webpage so I used bs4 code below

Let assume html contains the "html code"
soup = BeautifulSoup(html, "html.pa<http://html.pa>rser")
print(len(soup.fi<http://soup.fi>nd_all(string=["Engineering","engineering"])))
But the result is different from when i use control + f on my keyboard to find

Please help me understand why it's different results. Thanks
I am using Python 3.5





Sent from my Verizon LG Smartphone

From bgailer at gmail.com  Sat Dec 10 14:54:26 2016
From: bgailer at gmail.com (Bob Gailer)
Date: Sat, 10 Dec 2016 14:54:26 -0500
Subject: [Tutor] (no subject)
In-Reply-To: <9q2vi2s8ape460suids4mvou.1481247361947@email.lge.com>
References: <9q2vi2s8ape460suids4mvou.1481247361947@email.lge.com>
Message-ID: <CAP1rxO5o_V-DobQfa=5p5NUEwKq+89DKJD+aiSHbAZdvvO8UBQ@mail.gmail.com>

On Dec 10, 2016 12:15 PM, "Tetteh, Isaac - SDSU Student" <
isaac.tetteh at jacks.sdstate.edu> wrote:
>
> Hello,
>
> I am trying to find the number of times a word occurs on a webpage so I
used bs4 code below
>
> Let assume html contains the "html code"
> soup = BeautifulSoup(html, "html.pa<http://html.pa>rser")
> print(len(soup.fi<http://soup.fi
>nd_all(string=["Engineering","engineering"])))
> But the result is different from when i use control + f on my keyboard to
find
>
> Please help me understand why it's different results. Thanks
> I am using Python 3.5
>
What is the URL of the web page?
To what are you applying control-f?
What are the two different counts you're getting?
Is it possible that the page is being dynamically altered after it's loaded?

From itetteh34 at hotmail.com  Sat Dec 10 20:52:02 2016
From: itetteh34 at hotmail.com (isaac tetteh)
Date: Sun, 11 Dec 2016 01:52:02 +0000
Subject: [Tutor] (regular expression)
In-Reply-To: <CAP1rxO5o_V-DobQfa=5p5NUEwKq+89DKJD+aiSHbAZdvvO8UBQ@mail.gmail.com>
References: <9q2vi2s8ape460suids4mvou.1481247361947@email.lge.com>,
 <CAP1rxO5o_V-DobQfa=5p5NUEwKq+89DKJD+aiSHbAZdvvO8UBQ@mail.gmail.com>
Message-ID: <HE1PR0701MB25245E63509938BE006270D6BD990@HE1PR0701MB2524.eurprd07.prod.outlook.com>

this is the real code


with urllib.request.urlopen("https://www.sdstate.edu/electrical-engineering-and-computer-science") as cs:
    cs_page = cs.read()
    soup = BeautifulSoup(cs_page, "html.parser")
    print(len(soup.body.find_all(string = ["Engineering","engineering"])))

i used control + f on the link in the code and i get 11 for ctrl + f and 3 for the code

THanks




________________________________
From: Tutor <tutor-bounces+itetteh34=hotmail.com at python.org> on behalf of Bob Gailer <bgailer at gmail.com>
Sent: Saturday, December 10, 2016 7:54 PM
To: Tetteh, Isaac - SDSU Student
Cc: Python Tutor
Subject: Re: [Tutor] (no subject)

On Dec 10, 2016 12:15 PM, "Tetteh, Isaac - SDSU Student" <
isaac.tetteh at jacks.sdstate.edu> wrote:
>
> Hello,
>
> I am trying to find the number of times a word occurs on a webpage so I
used bs4 code below
>
> Let assume html contains the "html code"
> soup = BeautifulSoup(html, "html.pa<http://html.pa>rser")
> print(len(soup.fi<http://soup.fi
>nd_all(string=["Engineering","engineering"])))
> But the result is different from when i use control + f on my keyboard to
find
>
> Please help me understand why it's different results. Thanks
> I am using Python 3.5
>
What is the URL of the web page?
To what are you applying control-f?
What are the two different counts you're getting?
Is it possible that the page is being dynamically altered after it's loaded?
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Tutor Info Page - Python<https://mail.python.org/mailman/listinfo/tutor>
mail.python.org
This list is for folks who want to ask questions regarding how to learn computer programming with the Python language and its standard library.




From martin at linux-ip.net  Sat Dec 10 22:36:57 2016
From: martin at linux-ip.net (Martin A. Brown)
Date: Sat, 10 Dec 2016 19:36:57 -0800
Subject: [Tutor] (regular expression)
In-Reply-To: <HE1PR0701MB25245E63509938BE006270D6BD990@HE1PR0701MB2524.eurprd07.prod.outlook.com>
References: <9q2vi2s8ape460suids4mvou.1481247361947@email.lge.com>,
 <CAP1rxO5o_V-DobQfa=5p5NUEwKq+89DKJD+aiSHbAZdvvO8UBQ@mail.gmail.com>
 <HE1PR0701MB25245E63509938BE006270D6BD990@HE1PR0701MB2524.eurprd07.prod.outlook.com>
Message-ID: <alpine.LSU.2.11.1612101806360.24960@qnttre.jbaqresebt.arg>


Hello Isaac,

This second posting you have made has provided more information 
about what you are trying to accomplish and how (and also was 
readable, where the first one looked like it got mangled by your 
mail user agent; it's best to try to post only plain text messages 
to this sort of mailing list).

I suspect that we can help you a bit more, now.

If we knew even more about what you were looking to do, we might be 
able to help you further (with all of the usual remarks about how we 
won't do your homework for you, but all of us volunteers will gladly 
help you understand the tools, the systems, the world of Python and 
anything else we can suggest in the realm of computers, computer 
science and problem solving).

I will credit the person who assigned this task for you, as this is 
not dissimilar from the sort of problem that one often has when 
facing a new practical computing problem.  Often (and in your case) 
there is opaque structure and hidden assumptions in the question 
which need to be understood.  See further below....

These were your four lines of code:

>with urllib.request.urlopen("https://www.sdstate.edu/electrical-engineering-and-computer-science") as cs:
>    cs_page = cs.read()
>    soup = BeautifulSoup(cs_page, "html.parser")
>    print(len(soup.body.find_all(string = ["Engineering","engineering"])))

The fourth line is an impressive attempt at compressing all of the 
searching, finding, counting and reporting steps into a single line.  

Your task (I think), is more complicated than that single line can 
express.  So, that will need to be expanded to a few more lines of 
code.

You may have heard these aphorisms before:

  * brevity is the soul of wit
  * fewer lines of code are better
  * prefer a short elegant solution

But, when complexity intrudes into brevity, the human mind 
struggles.  As a practitioner, I will say that I spend more of my 
time reading and understanding code than writing it, so writing 
simple, self-contained and understandable units of code leads to 
intelligibility for humans and composability for systems.

Try this at a Python console [1].

  import this

>i used control + f on the link in the code and i get 11 for ctrl + 
>f and 3 for the code

Applause!  Look at the raw data!  Study the raw data!  That is an 
excellent way to start to try to understand the raw data.  You must 
always go back to the raw input data and then consider whether your 
tooling or the data model in your program matches what you are 
trying to extract/compute/transform.

The answer (for number of occurrences of the word 'engineering', 
case-insensitive) that I get is close to your answer when searching 
with control + f, but is a bit larger than 11.

Anyway, here are my thoughts.  I will start with some tips that are 
relevant to your 4-line pasted program:

  * BeautifulSoup is wonderfully convenient, but also remember it 
    is another high-level tool; it is often forgiving where other 
    tools are more rigorous, however it is excellent for learning 
    and (I hope you see below) that it is a great tool for the 
    problem you are trying to solve

  * in your code, soup.body is a handle that points to the <body>
    tag of the HTML document you have fetched; so why can't you 
    simply find_all of the strings "Engineering" and "engineering" 
    in the text and count them?

      - find_all is a method that returns all of the tags in the
        structured document below (in this case) soup.body

      - your intent is not to count tags with the string
        'engineering' but rather , you are looking for that string 
        in the text (I think)

  * it is almost always a mistake to try to process HTML with 
    regular expressions, however, it seems that you are trying to 
    find all matches of the (case-insensitive) word 'engineering' in 
    the text of this document; that is something tailor-made for 
    regular expressions, so there's the Python regular expression 
    library, too:  'import re'

  * and on a minor note, since you are using urllib.request.open()
    in a with statement (using contexts this way is wonderful), you
    could collect the data from the network socket, then drop out of 
    the 'with' block to allow the context to close, so if your block 
    worked as you wanted, you could adjust it as follows:

      with urllib.request.urlopen(uri as cs:
          cs_page = cs.read()
      soup = BeautifulSoup(cs_page, "html.parser")
      print(len(soup.body.find_all(string = ["Engineering","engineering"])))

  * On a much more minor point, I'll mention that urllib / urllib2 
    are available with the main Python releases but there are other 
    libraries for handling fetching; I often recommend the 
    third-party requests [0] library, as it is both very Pythonic, 
    reasonably high-level and frightfully flexible

So, connecting the Zen of Python [1] to your problem, I would 
suggest making shorter, simpler lines and separating the logic.  See 
below:

Here are some code suggestions.

  * collect the relevant data:  Once you have fetched the text into 
    a variable, get just the part that you know you want to process 
    as pure text, for example:

      soup = BeautifulSoup(r.text, "html.parser")
      bodytext = soup.body.text

  * walk/process/compute the data:  search that text to find the 
    subset of data you wish to operate on or which are the answer:

      pattern = re.compile('engineering', re.I)
      matches = re.findall(pattern, bodytext)

  * report to the end user:  Finally, print it out

      print('Found "engineering" (case-insensitive) %d times.' % (len(matches),))

Good luck and enjoy Python,

-Martin

 [0] http://docs.python-requests.org/en/master/

     url = "https://www.sdstate.edu/electrical-engineering-and-computer-science"
     r = requests.get(url)
     if not r.ok:
         # -- die/return/handle-error here
     soup = BeautifulSoup(r.text, "html.parser")

 [1] You do use the Python console to explore Python, your data and your code,
     don't you?

     $ python3
     Python 3.4.5 (default, Jul 03 2016, 13:55:08) [GCC] on linux
     Type "help", "copyright", "credits" or "license" for more information.
     >>> import this
     The Zen of Python, by Tim Peters

     Beautiful is better than ugly.
     Explicit is better than implicit.
     Simple is better than complex.
     Complex is better than complicated.
     Flat is better than nested.
     Sparse is better than dense.
     Readability counts.
     Special cases aren't special enough to break the rules.
     Although practicality beats purity.
     Errors should never pass silently.
     Unless explicitly silenced.
     In the face of ambiguity, refuse the temptation to guess.
     There should be one-- and preferably only one --obvious way to do it.
     Although that way may not be obvious at first unless you're Dutch.
     Now is better than never.
     Although never is often better than *right* now.
     If the implementation is hard to explain, it's a bad idea.
     If the implementation is easy to explain, it may be a good idea.
     Namespaces are one honking great idea -- let's do more of those!

-- 
Martin A. Brown
http://linux-ip.net/

From oliverjamespatterson at hotmail.com  Sun Dec 11 11:30:52 2016
From: oliverjamespatterson at hotmail.com (oliver patterson)
Date: Sun, 11 Dec 2016 16:30:52 +0000
Subject: [Tutor] unknown syntax error
Message-ID: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>

hey i dont know if this is the right place but i was just coding in idle and kept getting this syntax error and i can not see m to fix it here is my bit of code:

my_age=14
age=input("How old are you?:")
print("type start()")
def start():
    print("hey")
    if age == my_age:
        print("i'm",age,"too")
        else:
            if age < 14:
                print(" i'm older that you i'm",my_age,":)")
                else:
                    print("your older than me i'm",my_age,":(")



please help thank you.





From bryonadams at openmailbox.org  Mon Dec 12 11:29:20 2016
From: bryonadams at openmailbox.org (Bryon Adams)
Date: Mon, 12 Dec 2016 11:29:20 -0500
Subject: [Tutor] Created Function, Need Argument to be a String
Message-ID: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>

Is there a way to force my argument to always be a string before 
entering the function? Else, is there a better way to go about this? In 
whatever program I write, I could change what I want as input to be a 
string prior to tossing it into the function but I think it would make 
more sense for my function to already do it. The function otherwise 
works. This is on Python3.5 under Fedora 25

The only other thing I could think of would be to put exceptions in for 
syntax error and whatever else pops up as I go along, though to be 
honest it *should* always be a string that gets dumped into the 
function. Not sure how I'd put the exception together though since it's 
not making it into the function prior to failing.

-------------------------------------------
Error from interpreter: (looks like it's taking issue with it being a 
number it doesn't know how to deal with)

 >>> ip_checker(169.254.0.1)
   File "<stdin>", line 1
     ip_checker(169.254.0.1)
                        ^
SyntaxError: invalid syntax

-------------------------------------------
My function:

def ip_checker(ip_address):
   '''
   Takes one IP address and checks whether it is valid or not.
   '''
   # Try to convert to integers
   try:
     ip_addr = [int(i) for i in ip_address.split('.')]
   except ValueError:
     print('Invalid characters were entered or an octet is empty, please 
try again.')
     return False

   # Determine how many octets were entered
   if len(ip_addr) != 4:
     print('Incorrect number of octets, please try again.')
     return False

   # Determine validity of first octet
   if ((ip_addr[0] > 223) and (ip_addr[0] < 256)) or (ip_addr[0] == 0):
     print('First octet is reserved or zero.')
     return False

   # Determine if this is a loopback address
   if ip_addr[0] == 127:
     print('I think that was a loopback address, please try again.')
     return False

   # Determine if this is an APIPA address
   if (ip_addr[0] == 169) and (ip_addr[1] == 254):
     print('I think that was an APIPA address, please try again.')
     return False

   # Determine if the last three octets are between 0-255
   for octet in (ip_addr[1], ip_addr[2], ip_addr[3]):
     if octet not in [i for i in range(0,256)]:
       print('Octet too large or too small, please try again.')
       return False
     else:
       print('The IP address {} is valid.'.format(ip_address))
       return True


From juan0christian at gmail.com  Sun Dec 11 15:21:02 2016
From: juan0christian at gmail.com (Juan C.)
Date: Sun, 11 Dec 2016 18:21:02 -0200
Subject: [Tutor] (no subject)
In-Reply-To: <CAP1rxO5o_V-DobQfa=5p5NUEwKq+89DKJD+aiSHbAZdvvO8UBQ@mail.gmail.com>
References: <9q2vi2s8ape460suids4mvou.1481247361947@email.lge.com>
 <CAP1rxO5o_V-DobQfa=5p5NUEwKq+89DKJD+aiSHbAZdvvO8UBQ@mail.gmail.com>
Message-ID: <CAAp0bGth-du9euaeZ4me5DoB5ujZRSwZAwdR5pkdpctp+YCNmA@mail.gmail.com>

On Dec 10, 2016 12:15 PM, "Tetteh, Isaac - SDSU Student"
> isaac.tetteh at jacks.sdstate.edu> wrote:
> >
> > Hello,
> >
> > I am trying to find the number of times a word occurs on a webpage so I
> used bs4 code below
> >
> > Let assume html contains the "html code"
> > soup = BeautifulSoup(html, "html.pa<http://html.pa>rser")
> > print(len(soup.fi<http://soup.fi
> >nd_all(string=["Engineering","engineering"])))
> > But the result is different from when i use control + f on my keyboard
to
> find
> >
> > Please help me understand why it's different results. Thanks
> > I am using Python 3.5
> >

Well, depending on the word you're looking for it's pretty possible that
when you execute your code it finds matches inside javascript functions,
html/js comments and so on because you're doing a search against the actual
html file. If you execute a simple CRTL+F using a web browser it will just
look for "visual info" and won't be looking into the actual code. For
example, if we go to https://www.python.org/psf/ and do a CRTL+F and search
for "Upgrade to a different browser" we will find zero results, on the
other hand if we do this inside the view-source we will find one result,
because this sentence is inside a commented line.

From robertvstepp at gmail.com  Thu Dec 15 11:34:00 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Thu, 15 Dec 2016 10:34:00 -0600
Subject: [Tutor] unknown syntax error
In-Reply-To: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
References: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
Message-ID: <CANDiX9LrqwZzoJEPuYXi3Ly7NyzRZGqnq1OfYs0JA+aSZ6Z82A@mail.gmail.com>

On Sun, Dec 11, 2016 at 10:30 AM, oliver patterson <
oliverjamespatterson at hotmail.com> wrote:
> hey i dont know if this is the right place but i was just coding in idle
and kept getting this syntax error and i can not see m to fix it here is my
bit of code:

Yes, this is the right place.  Welcome to Tutor
?!

When you get some form of error, please copy and paste the full error
message in your email.  It is also usually helpful to state your version of
Python and operating system you are using.?


> my_age=14
> age=input("How old are you?:")
> print("type start()")
> def start():
>     print("hey")
>     if age == my_age:
>         print("i'm",age,"too")
>         else:
>             if age < 14:
>                 print(" i'm older that you i'm",my_age,":)")
>                 else:
>                     print("your older than me i'm",my_age,":(")
>

?When you use an if ... elif ... else construct, all of these need to be at
the same level of indentation and line up with each other.  That is:

if condition 1:
    insert condition 1 lines of code here
    ...

elif condition 2:
    condition 2 code here ...
    ...

else:
    code for else ...


In your given code, your "else" keyword should line up with the "if"
keyword above it.  Do you know about "elif"?  You might want to look that
up.  It would prove helpful in your example.

I'm curious as to how you will call your start() function.  Judging from
the "print("type start()")" line, I am wondering if you might have a
misconception.  But go ahead and plow forward and see what happens.  That
is how we learn.

HTH!


-- 
boB

From bgailer at gmail.com  Thu Dec 15 15:57:42 2016
From: bgailer at gmail.com (bob gailer)
Date: Thu, 15 Dec 2016 15:57:42 -0500
Subject: [Tutor] unknown syntax error
In-Reply-To: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
References: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
Message-ID: <f838300b-4100-8c8d-66c2-46a4f8c323a6@gmail.com>

On 12/11/2016 11:30 AM, oliver patterson wrote:
> hey i dont know if this is the right place but i was just coding in idle and kept getting this syntax error and i can not see m to fix it here is my bit of code:
>
> my_age=14
> age=input("How old are you?:")
> print("type start()")
> def start():
>      print("hey")
>      if age == my_age:
>          print("i'm",age,"too")
>          else:
>              if age < 14:
>                  print(" i'm older that you i'm",my_age,":)")
>                  else:
>                      print("your older than me i'm",my_age,":(")
>
>
>
> please help thank you.
IDLE's way of reporting some errors is different than the standard 
traceback, so you may not be able to follow Bob Stepp's advice.

When I select Run on your module I see the first "else" highlighted in 
red, and the invalid syntax message.

If you undent the else blocks giving:

my_age=14
age=input("How old are you?:")
print("type start()")
def start():
     print("hey")
     if age == my_age:
         print("i'm",age,"too")
     else:
         if age < 14:
             print(" i'm older that you i'm",my_age,":)")
         else:
             print("your older than me i'm",my_age,":(")

the program will pass the syntax checks. When you run it it will fail:

How old are you?:as
type start()
 >>> start()
hey
Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     start()
   File "C:/Python35/oliver.py", line 9, in start
     if age < 14:
TypeError: unorderable types: str() < int()
 >>>

Can you figure out why? Can you propose a fix?

If you want to impress potential employers I recommend cleaning up your 
English.

From nulla.epistola at web.de  Fri Dec 16 13:49:25 2016
From: nulla.epistola at web.de (Sibylle Koczian)
Date: Fri, 16 Dec 2016 19:49:25 +0100
Subject: [Tutor] Created Function, Need Argument to be a String
In-Reply-To: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
References: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
Message-ID: <41268055-da49-9e3d-0471-d900695d1dba@web.de>

Am 12.12.2016 um 17:29 schrieb Bryon Adams:
> Is there a way to force my argument to always be a string before
> entering the function? Else, is there a better way to go about this? In
> whatever program I write, I could change what I want as input to be a
> string prior to tossing it into the function but I think it would make
> more sense for my function to already do it. The function otherwise
> works. This is on Python3.5 under Fedora 25
>
> The only other thing I could think of would be to put exceptions in for
> syntax error and whatever else pops up as I go along, though to be
> honest it *should* always be a string that gets dumped into the
> function. Not sure how I'd put the exception together though since it's
> not making it into the function prior to failing.
>

Syntax errors don't raise exceptions, because the program doesn't start 
at all as long as it contains them. Exceptions are raised at runtime.

> -------------------------------------------
> Error from interpreter: (looks like it's taking issue with it being a
> number it doesn't know how to deal with)
>
>>>> ip_checker(169.254.0.1)
>   File "<stdin>", line 1
>     ip_checker(169.254.0.1)
>                        ^
> SyntaxError: invalid syntax
>
You are calling the function with an invalid literal: a number can't 
contain more than one decimal point and a string literal needs quotes 
around it.

If the argument is valid but is no string (a number, a list or anything 
else) trying to split it raises an AttributeError:

 >>> s = 123.456
 >>> s.split('.')
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
     s.split('.')
AttributeError: 'float' object has no attribute 'split'
 >>> ['a', 'b', 'c'].split('.')
Traceback (most recent call last):
   File "<pyshell#2>", line 1, in <module>
     ['a', 'b', 'c'].split('.')
AttributeError: 'list' object has no attribute 'split'

So you could use a second except clause:

try:
     ip_addr = ...
except ValueError:
     ...
except AttributeError:
     print("Argument must be a string, please try again.")
     return False


> -------------------------------------------
> My function:
>
> def ip_checker(ip_address):
>   '''
>   Takes one IP address and checks whether it is valid or not.
>   '''
>   # Try to convert to integers
>   try:
>     ip_addr = [int(i) for i in ip_address.split('.')]
>   except ValueError:
>     print('Invalid characters were entered or an octet is empty, please
> try again.')
>     return False
>
...


HTH



From nulla.epistola at web.de  Fri Dec 16 13:49:25 2016
From: nulla.epistola at web.de (Sibylle Koczian)
Date: Fri, 16 Dec 2016 19:49:25 +0100
Subject: [Tutor] Created Function, Need Argument to be a String
In-Reply-To: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
References: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
Message-ID: <41268055-da49-9e3d-0471-d900695d1dba@web.de>

Am 12.12.2016 um 17:29 schrieb Bryon Adams:
> Is there a way to force my argument to always be a string before
> entering the function? Else, is there a better way to go about this? In
> whatever program I write, I could change what I want as input to be a
> string prior to tossing it into the function but I think it would make
> more sense for my function to already do it. The function otherwise
> works. This is on Python3.5 under Fedora 25
>
> The only other thing I could think of would be to put exceptions in for
> syntax error and whatever else pops up as I go along, though to be
> honest it *should* always be a string that gets dumped into the
> function. Not sure how I'd put the exception together though since it's
> not making it into the function prior to failing.
>

Syntax errors don't raise exceptions, because the program doesn't start 
at all as long as it contains them. Exceptions are raised at runtime.

> -------------------------------------------
> Error from interpreter: (looks like it's taking issue with it being a
> number it doesn't know how to deal with)
>
>>>> ip_checker(169.254.0.1)
>   File "<stdin>", line 1
>     ip_checker(169.254.0.1)
>                        ^
> SyntaxError: invalid syntax
>
You are calling the function with an invalid literal: a number can't 
contain more than one decimal point and a string literal needs quotes 
around it.

If the argument is valid but is no string (a number, a list or anything 
else) trying to split it raises an AttributeError:

 >>> s = 123.456
 >>> s.split('.')
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
     s.split('.')
AttributeError: 'float' object has no attribute 'split'
 >>> ['a', 'b', 'c'].split('.')
Traceback (most recent call last):
   File "<pyshell#2>", line 1, in <module>
     ['a', 'b', 'c'].split('.')
AttributeError: 'list' object has no attribute 'split'

So you could use a second except clause:

try:
     ip_addr = ...
except ValueError:
     ...
except AttributeError:
     print("Argument must be a string, please try again.")
     return False


> -------------------------------------------
> My function:
>
> def ip_checker(ip_address):
>   '''
>   Takes one IP address and checks whether it is valid or not.
>   '''
>   # Try to convert to integers
>   try:
>     ip_addr = [int(i) for i in ip_address.split('.')]
>   except ValueError:
>     print('Invalid characters were entered or an octet is empty, please
> try again.')
>     return False
>
...


HTH


From matt.williams45.mw at gmail.com  Thu Dec 15 03:01:52 2016
From: matt.williams45.mw at gmail.com (Matt Williams)
Date: Thu, 15 Dec 2016 08:01:52 +0000
Subject: [Tutor] Created Function, Need Argument to be a String
In-Reply-To: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
References: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
Message-ID: <CAFTVGQabopD2uriRHgCtWmixaeKXDd1dyU__DRzF8aqXEArkzQ@mail.gmail.com>

Use the str() function.

M

On Thu, 15 Dec 2016, 07:56 Bryon Adams, <bryonadams at openmailbox.org> wrote:

> Is there a way to force my argument to always be a string before
> entering the function? Else, is there a better way to go about this? In
> whatever program I write, I could change what I want as input to be a
> string prior to tossing it into the function but I think it would make
> more sense for my function to already do it. The function otherwise
> works. This is on Python3.5 under Fedora 25
>
> The only other thing I could think of would be to put exceptions in for
> syntax error and whatever else pops up as I go along, though to be
> honest it *should* always be a string that gets dumped into the
> function. Not sure how I'd put the exception together though since it's
> not making it into the function prior to failing.
>
> -------------------------------------------
> Error from interpreter: (looks like it's taking issue with it being a
> number it doesn't know how to deal with)
>
>  >>> ip_checker(169.254.0.1)
>    File "<stdin>", line 1
>      ip_checker(169.254.0.1)
>                         ^
> SyntaxError: invalid syntax
>
> -------------------------------------------
> My function:
>
> def ip_checker(ip_address):
>    '''
>    Takes one IP address and checks whether it is valid or not.
>    '''
>    # Try to convert to integers
>    try:
>      ip_addr = [int(i) for i in ip_address.split('.')]
>    except ValueError:
>      print('Invalid characters were entered or an octet is empty, please
> try again.')
>      return False
>
>    # Determine how many octets were entered
>    if len(ip_addr) != 4:
>      print('Incorrect number of octets, please try again.')
>      return False
>
>    # Determine validity of first octet
>    if ((ip_addr[0] > 223) and (ip_addr[0] < 256)) or (ip_addr[0] == 0):
>      print('First octet is reserved or zero.')
>      return False
>
>    # Determine if this is a loopback address
>    if ip_addr[0] == 127:
>      print('I think that was a loopback address, please try again.')
>      return False
>
>    # Determine if this is an APIPA address
>    if (ip_addr[0] == 169) and (ip_addr[1] == 254):
>      print('I think that was an APIPA address, please try again.')
>      return False
>
>    # Determine if the last three octets are between 0-255
>    for octet in (ip_addr[1], ip_addr[2], ip_addr[3]):
>      if octet not in [i for i in range(0,256)]:
>        print('Octet too large or too small, please try again.')
>        return False
>      else:
>        print('The IP address {} is valid.'.format(ip_address))
>        return True
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

From george at fischhof.hu  Thu Dec 15 04:07:35 2016
From: george at fischhof.hu (George Fischhof)
Date: Thu, 15 Dec 2016 10:07:35 +0100
Subject: [Tutor] unknown syntax error
In-Reply-To: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
References: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
Message-ID: <CAFwcP0gTB7M1qxL1X9uvpHzM+5RirqG203DiyxCd_UE-X1709Q@mail.gmail.com>

Hi Oliver,

Your else statement should be unindented by one level ;-)
The else must be at same indent level as the if it belongs to.

BR,
George

2016-12-11 17:30 GMT+01:00 oliver patterson <
oliverjamespatterson at hotmail.com>:

> hey i dont know if this is the right place but i was just coding in idle
> and kept getting this syntax error and i can not see m to fix it here is my
> bit of code:
>
> my_age=14
> age=input("How old are you?:")
> print("type start()")
> def start():
>     print("hey")
>     if age == my_age:
>         print("i'm",age,"too")
>         else:
>             if age < 14:
>                 print(" i'm older that you i'm",my_age,":)")
>                 else:
>                     print("your older than me i'm",my_age,":(")
>
>
>
> please help thank you.
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

From george at fischhof.hu  Thu Dec 15 04:24:55 2016
From: george at fischhof.hu (George Fischhof)
Date: Thu, 15 Dec 2016 10:24:55 +0100
Subject: [Tutor] Created Function, Need Argument to be a String
In-Reply-To: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
References: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
Message-ID: <CAFwcP0ij5VzUdaVaTCdO2sFX83dKSL3WYuRX=PF9y3Vd+LzqvQ@mail.gmail.com>

2016-12-12 17:29 GMT+01:00 Bryon Adams <bryonadams at openmailbox.org>:

> Is there a way to force my argument to always be a string before entering
> the function? Else, is there a better way to go about this? In whatever
> program I write, I could change what I want as input to be a string prior
> to tossing it into the function but I think it would make more sense for my
> function to already do it. The function otherwise works. This is on
> Python3.5 under Fedora 25
>
> The only other thing I could think of would be to put exceptions in for
> syntax error and whatever else pops up as I go along, though to be honest
> it *should* always be a string that gets dumped into the function. Not sure
> how I'd put the exception together though since it's not making it into the
> function prior to failing.
>
> -------------------------------------------
> Error from interpreter: (looks like it's taking issue with it being a
> number it doesn't know how to deal with)
>
> >>> ip_checker(169.254.0.1)
>   File "<stdin>", line 1
>     ip_checker(169.254.0.1)
>                        ^
> SyntaxError: invalid syntax
>
> -------------------------------------------
> My function:
>
> def ip_checker(ip_address):
>   '''
>   Takes one IP address and checks whether it is valid or not.
>   '''
>   # Try to convert to integers
>   try:
>     ip_addr = [int(i) for i in ip_address.split('.')]
>   except ValueError:
>     print('Invalid characters were entered or an octet is empty, please
> try again.')
>     return False
>
>   # Determine how many octets were entered
>   if len(ip_addr) != 4:
>     print('Incorrect number of octets, please try again.')
>     return False
>
>   # Determine validity of first octet
>   if ((ip_addr[0] > 223) and (ip_addr[0] < 256)) or (ip_addr[0] == 0):
>     print('First octet is reserved or zero.')
>     return False
>
>   # Determine if this is a loopback address
>   if ip_addr[0] == 127:
>     print('I think that was a loopback address, please try again.')
>     return False
>
>   # Determine if this is an APIPA address
>   if (ip_addr[0] == 169) and (ip_addr[1] == 254):
>     print('I think that was an APIPA address, please try again.')
>     return False
>
>   # Determine if the last three octets are between 0-255
>   for octet in (ip_addr[1], ip_addr[2], ip_addr[3]):
>     if octet not in [i for i in range(0,256)]:
>       print('Octet too large or too small, please try again.')
>       return False
>     else:
>       print('The IP address {} is valid.'.format(ip_address))
>       return True
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



Hi Btryon,

to to force to process a string is convert the function argument to string:


def ip_checker(ip_address):
  '''
  Takes one IP address and checks whether it is valid or not.
  '''
  ip_address = str(ip_address)

And after this you can process it as a string.


To write more readable and more beautiful code, You can put all your
checkings into small functions inside the ip_checker function:




def ip_checker(ip_address):
    def determine_validity_of_first_octet():
    ....

    def determine_if_this_is_a_loopback():

    ...

    determine_validity_of_first_octet()
    determine ...

and so on
:-)

BR.
George

From Joaquin.Alzola at lebara.com  Thu Dec 15 04:51:37 2016
From: Joaquin.Alzola at lebara.com (Joaquin Alzola)
Date: Thu, 15 Dec 2016 09:51:37 +0000
Subject: [Tutor] unknown syntax error
In-Reply-To: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
References: <VI1PR0802MB23840D59EBE0B5C176ECCB9CC0990@VI1PR0802MB2384.eurprd08.prod.outlook.com>
Message-ID: <DB5PR07MB0806187A0FE2A736636B1F70F09D0@DB5PR07MB0806.eurprd07.prod.outlook.com>

>hey i dont know if this is the right place but i was just coding in idle and kept getting this syntax error and i can not see m to fix it here is my bit of code:

 >   if age == my_age:
 >       print("i'm",age,"too")
 >       else:
 >           if age < 14:
 >               print(" i'm older that you i'm",my_age,":)")
 >               else:
 >                   print("your older than me i'm",my_age,":(")

If else should be at the same level of indentation.
It is a condition . http://www.peachpit.com/articles/article.aspx?p=1312792&seqNum=3

if x==0:
    print(x)
else:
    print(x)

Use whitespaces or tabs as your indentation.
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.

From Joaquin.Alzola at lebara.com  Thu Dec 15 05:50:11 2016
From: Joaquin.Alzola at lebara.com (Joaquin Alzola)
Date: Thu, 15 Dec 2016 10:50:11 +0000
Subject: [Tutor] Created Function, Need Argument to be a String
In-Reply-To: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
References: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
Message-ID: <DB5PR07MB08061958367BAE77BAE6755DF09D0@DB5PR07MB0806.eurprd07.prod.outlook.com>



>-------------------------------------------
>Error from interpreter: (looks like it's taking issue with it being a
>number it doesn't know how to deal with)
>
 >>>> ip_checker(169.254.0.1)
 >  File "<stdin>", line 1
>     ip_checker(169.254.0.1)
>                        ^
>SyntaxError: invalid syntax
>
>-------------------------------------------

You are passing the IP as an invalid number. 169.254.0.1 as an integer or float or long doesn't exist.
I suppose you want to pass it as a String

ip_checker("169.254.0.1")
This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.

From juan0christian at gmail.com  Thu Dec 15 11:49:08 2016
From: juan0christian at gmail.com (Juan C.)
Date: Thu, 15 Dec 2016 14:49:08 -0200
Subject: [Tutor] Created Function, Need Argument to be a String
In-Reply-To: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
References: <796af002-470f-0a80-9c03-a0637e6efd9a@openmailbox.org>
Message-ID: <CAAp0bGvCmCFHN0jRgejRaWYw94zTfM3J1w=Td0pFY7ANdB0z3Q@mail.gmail.com>

On Mon, Dec 12, 2016 at 2:29 PM, Bryon Adams <bryonadams at openmailbox.org> wrote:
> Is there a way to force my argument to always be a string before entering
> the function?

You could do the following:

1. Use `def ip_checker(ip_address: str):` to make it more clear that
you're expecting a str, but remember, this is just a "hint", it
doesn't enforce anything. (Read more at
https://docs.python.org/3/library/typing.html#module-typing)
2. You could also write something like that:

valid_ip = "192.168.0.1"
invalid_ip = 10


def ip_check(ip_addr: str):
  if type(ip_addr) is not str:
    raise TypeError('The IP address should be a string.')  #
https://docs.python.org/3/library/exceptions.html#TypeError

  # continue your function...
  print(ip_addr, 'is valid!')


ip_check(valid_ip)  # 192.168.0.1 is valid!
ip_check(invalid_ip)  # TypeError: The IP address should be a string.

From joseolu4gsm at yahoo.com  Mon Dec 19 17:38:07 2016
From: joseolu4gsm at yahoo.com (Joseph Olugbohunmi)
Date: Mon, 19 Dec 2016 22:38:07 +0000 (UTC)
Subject: [Tutor] Trouble Launching Python
References: <105659980.1897733.1482187087343.ref@mail.yahoo.com>
Message-ID: <105659980.1897733.1482187087343@mail.yahoo.com>

 Hello,Good day, I installed Python 3.5.2 on my Windows 8.1 PC and then I tried launching IDLE as well as the Interpreter but I got a message that api-ms-win-crt-runtime-l1-1-0.dll was missing. I downloaded and installed that after which I got another message that 
api-ms-win-crt-math-l1-1-0.dll was also missing, I got that, and then another dll was missing and it goes on and on. Please what can I do?ThanksJoseph 

From eryksun at gmail.com  Tue Dec 20 13:52:28 2016
From: eryksun at gmail.com (eryk sun)
Date: Tue, 20 Dec 2016 18:52:28 +0000
Subject: [Tutor] Trouble Launching Python
In-Reply-To: <105659980.1897733.1482187087343@mail.yahoo.com>
References: <105659980.1897733.1482187087343.ref@mail.yahoo.com>
 <105659980.1897733.1482187087343@mail.yahoo.com>
Message-ID: <CACL+1at-gPoDf9_yr5mvixG6teia35ijuy4n0Dp0QFAohsUT7w@mail.gmail.com>

On Mon, Dec 19, 2016 at 10:38 PM, Joseph Olugbohunmi via Tutor
<tutor at python.org> wrote:
> Hello,Good day, I installed Python 3.5.2 on my Windows 8.1 PC and then I tried launching
> IDLE as well as the Interpreter but I got a message that api-ms-win-crt-runtime-l1-1-0.dll was
> missing. I downloaded and installed that after which I got another message that
> api-ms-win-crt-math-l1-1-0.dll was also missing, I got that, and then another dll was missing
> and it goes on and on. Please what can I do?

The implementation of Python that you're using is written in C and
relies on the C runtime library to provide a measure of cross-platform
portability. The latest C runtime from Microsoft is called the
"Universal CRT". It's an operating system component. As such, you
should already have ucrtbase.dll and all of the API set DLLs, such as
api-ms-win-crt-runtime-l1-1-0.dll, assuming your system is up to date
via Windows Update. If for some reason your system doesn't have this
update, you can manually download and install it from the following
link:

https://www.microsoft.com/en-us/download/details.aspx?id=50410

Install either "Windows8.1-KB3118401-x64.msu" (64-bit) or
"Windows8.1-KB3118401-x86.msu" (32-bit), depending on whether your
version of Windows is 64-bit or 32-bit.

From george at fischhof.hu  Tue Dec 20 14:12:26 2016
From: george at fischhof.hu (George Fischhof)
Date: Tue, 20 Dec 2016 20:12:26 +0100
Subject: [Tutor] Trouble Launching Python
In-Reply-To: <105659980.1897733.1482187087343@mail.yahoo.com>
References: <105659980.1897733.1482187087343.ref@mail.yahoo.com>
 <105659980.1897733.1482187087343@mail.yahoo.com>
Message-ID: <CAFwcP0jE2KgO9C1+BhqcM8nq=HfWp9859DT+YR2z0ryJ-k_qUw@mail.gmail.com>

2016-12-19 23:38 GMT+01:00 Joseph Olugbohunmi via Tutor <tutor at python.org>:

>  Hello,Good day, I installed Python 3.5.2 on my Windows 8.1 PC and then I
> tried launching IDLE as well as the Interpreter but I got a message that
> api-ms-win-crt-runtime-l1-1-0.dll was missing. I downloaded and installed
> that after which I got another message that
> api-ms-win-crt-math-l1-1-0.dll was also missing, I got that, and then
> another dll was missing and it goes on and on. Please what can I
> do?ThanksJoseph
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


Hi Joseph,

this is a Microsoft visual c++ redistributable is missing from system.
I found this stackoverflow link which seems to describing a solution

http://stackoverflow.com/questions/33265663/api-ms-win-crt-runtime-l1-1-0-dll-is-missing-when-opening-microsoft-office-file

BR,
George

From fazal.h.khan at gmail.com  Tue Dec 20 20:29:54 2016
From: fazal.h.khan at gmail.com (Fazal Khan)
Date: Tue, 20 Dec 2016 18:29:54 -0700
Subject: [Tutor] Fwd: Question about selenium with python
In-Reply-To: <CAAsX1=1qr8XOYsK_x2cqKH3GDgOGrk8kZbMM17S2RrRm-ymn8A@mail.gmail.com>
References: <CAAsX1=1qr8XOYsK_x2cqKH3GDgOGrk8kZbMM17S2RrRm-ymn8A@mail.gmail.com>
Message-ID: <CAAsX1=0ZGr8J=cHSWD8gzkQZmXcKQBvMs3tdJY6mGRzjRacP9Q@mail.gmail.com>

Hello,

Im a new programmer and this is my first time posting here. I have a
question about using selenium with python. My first question is how do I
get selenium to click on a link contained within a specific cell of a
table? I know how to get selenium to click on a link in general but in this
case there are several links which have the same name, but each of these
links are in a different cell. Basically Id like to give selenium the
location of a specific cell, and then tell it to click on the link inside
that cell.

Lets say the link is located in the 3nd row and 3nd column of a table. I
tried the following code but it didnt work
link = driver.find_element_by_xpath("//tr[3]/td[3]")
link.click()

Thanks
Fuzz

heres an example of the HTML code im working with:
</form><table class="table">
    <tr>
    <tr>
        <th>
            <a href="/RadOncProton/2ndcheck_mca/MCR/Patients?sortOrder=
Name">Name</a>
        </th>
        <th>
            <a href="/RadOncProton/2ndcheck_mca/MCR/Patients?sortOrder=clinicnum_desc">Clinic
#</a>
        </th>
        <th>
        </th>
    </tr>

            <tr>
                <td>
                    Name1
                </td>
                <td>
                    MRN1
                </td>
                    <td>
                        <a href="/RadOncProton/2ndcheck_
mca/MCR/Plans/Index/743">Plans</a>
                    </td>
                                    <td>
                        <b> | </b>
                        <a href="/RadOncProton/2ndcheck_
mca/MCR/Patients/Delete/743">Delete</a>
                    </td>
            </tr>

From ajakzhedgar at gmail.com  Wed Dec 21 02:50:02 2016
From: ajakzhedgar at gmail.com (Hedgar)
Date: Wed, 21 Dec 2016 08:50:02 +0100
Subject: [Tutor] Using Python to solve factoria
Message-ID: <3E8D9EB5-7447-4D6E-822C-1C3998BA3888@gmail.com>

Hello,

I really happy to be accepted to the list!
This is my current function:

def factoria(numb):
While numb > 1:
If numb==0:
return 1
Else:
result = numb*(numb-1)
numb = numb -1
return result
factoria(5)
#should output 120
What am I not getting right?
Thanks
Heddy


Sent from my iPhone

> On 21 Dec 2016, at 01:17, tutor-request at python.org wrote:
> 
> Welcome to the Tutor at python.org mailing list! This list is for folks
> who want to ask (and/or answer) questions from folks who wish to learn
> how to program with Python.  Feel free to ask even the most basic of
> questions -- that's what the list is for!
> 
> For best results when asking a question on this list: - Try to write
> some code to solve your problem - Show the code you have written -
> Describe what the code does and what you want it to do - If the code
> generates an error, copy and paste the entire error message, including
> the traceback, into your email. - Tell us what OS and Python version
> you are using.
> 
> - Don't ask us to do your homework. - Don't assume we know what you
> are talking about. If you are having trouble with a third-party
> library, include a link to the library home page.
> 
> When replying to a posting: - Use Reply All to reply to the entire
> list - Don't top post - put your reply after the text to which you are
> replying
> 
> For all posts: - Format your email as plain text, not HTML
> 
> 
> To post to this list, send your message to:
> 
>  tutor at python.org
> 
> General information about the mailing list is at:
> 
>  https://mail.python.org/mailman/listinfo/tutor
> 
> If you ever want to unsubscribe or change your options (eg, switch to
> or from digest mode, change your password, etc.), visit your
> subscription page at:
> 
>  https://mail.python.org/mailman/options/tutor/ajakzhedgar%2Bnewsletter%40gmail.com
> 
> 
> You can also make such adjustments via email by sending a message to:
> 
>  Tutor-request at python.org
> 
> with the word `help' in the subject or body (don't include the
> quotes), and you will get back a message with instructions.
> 
> You must know your password to change your options (including changing
> the password, itself) or to unsubscribe without confirmation.  It is:
> 
>  analyst
> 
> Normally, Mailman will remind you of your python.org mailing list
> passwords once every month, although you can disable this if you
> prefer.  This reminder will also include instructions on how to
> unsubscribe or change your account options.  There is also a button on
> your options page that will email your current password to you.

From robertvstepp at gmail.com  Wed Dec 21 14:30:05 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Wed, 21 Dec 2016 13:30:05 -0600
Subject: [Tutor] Using Python to solve factoria
In-Reply-To: <3E8D9EB5-7447-4D6E-822C-1C3998BA3888@gmail.com>
References: <3E8D9EB5-7447-4D6E-822C-1C3998BA3888@gmail.com>
Message-ID: <CANDiX9LzB85LvWivP3NxED4TD83CP=Yoa9VhKSONNm3BGnzZRQ@mail.gmail.com>

Welcome!

On Wed, Dec 21, 2016 at 1:50 AM, Hedgar <ajakzhedgar at gmail.com> wrote:
> Hello,
>
> I really happy to be accepted to the list!
> This is my current function:
>
> def factoria(numb):
> While numb > 1:
> If numb==0:
> return 1
> Else:
> result = numb*(numb-1)
> numb = numb -1
> return result
> factoria(5)
> #should output 120
> What am I not getting right?

I'll help you part way.  Firstly Python is case sensitive.  You have
several examples of where you start Python keywords with a capital
letter when it should be lowercase. Secondly Python uses indentation
(Conventionally 4 spaces.) to segregate code blocks.  Thirdly, you
won't see the result of your function if you don't print it.  So if I
correct these in your code I get:

def factoria(numb):
    while numb > 1:
        if numb==0:
            return 1
        else:
            result = numb*(numb-1)
            numb = numb -1

        return result

print(factoria(5))

But alas!  There is more work to be done.  This will not give you a
syntax error, but now there is an issue with logic error(s).  Can you
figure it out?  It might help if you pretend you are the computer and
work out the while loop on paper through each iteration until it
exits.

Also, when you ask for help you should copy and paste the full
traceback of the error you received.  What did you get?  Did the error
relate to anything I corrected above?  Unfortunately you won't get a
traceback for logic errors!  Also give the version of Python and your
operating system.  I am assuming you are using Python 3.

HTH!

boB

From eryksun at gmail.com  Wed Dec 21 18:17:31 2016
From: eryksun at gmail.com (eryk sun)
Date: Wed, 21 Dec 2016 23:17:31 +0000
Subject: [Tutor] Trouble Launching Python
In-Reply-To: <CAFwcP0jE2KgO9C1+BhqcM8nq=HfWp9859DT+YR2z0ryJ-k_qUw@mail.gmail.com>
References: <105659980.1897733.1482187087343.ref@mail.yahoo.com>
 <105659980.1897733.1482187087343@mail.yahoo.com>
 <CAFwcP0jE2KgO9C1+BhqcM8nq=HfWp9859DT+YR2z0ryJ-k_qUw@mail.gmail.com>
Message-ID: <CACL+1avsdGN1Hg8S_iBzdwsMW+M-E-yPX4Y0=83L+2GuQxEv1A@mail.gmail.com>

On Tue, Dec 20, 2016 at 7:12 PM, George Fischhof <george at fischhof.hu> wrote:
> 2016-12-19 23:38 GMT+01:00 Joseph Olugbohunmi via Tutor <tutor at python.org>:
>
> this is a Microsoft visual c++ redistributable is missing from system.
> I found this stackoverflow link which seems to describing a solution
>
> http://stackoverflow.com/questions/33265663/
> api-ms-win-crt-runtime-l1-1-0-dll-is-missing-when-opening-microsoft-office-file

There is no reason in this case to install the VC++ redistributable
package. Python is written in C, so it only needs the C runtime, which
is now an OS component. I provided a link for the updated download for
the Universal C Runtime, KB3118401. The above Stack Overflow answer
mentions KB2999226, which is the old, outdated update from well over a
year ago.

From jf_byrnes at comcast.net  Wed Dec 21 22:37:42 2016
From: jf_byrnes at comcast.net (Jim Byrnes)
Date: Wed, 21 Dec 2016 21:37:42 -0600
Subject: [Tutor] Open a libreoffice calc file in Python
Message-ID: <o3fhq1$t64$1@blaine.gmane.org>

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?

Thanks,  Jim


From robertvstepp at gmail.com  Wed Dec 21 22:54:37 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Wed, 21 Dec 2016 21:54:37 -0600
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <o3fhq1$t64$1@blaine.gmane.org>
References: <o3fhq1$t64$1@blaine.gmane.org>
Message-ID: <CANDiX9Lvh8zSN9dvSYBhJP1337-=xANssR-TJOH3ZmXtjVqJzw@mail.gmail.com>

On Wed, Dec 21, 2016 at 9:37 PM, Jim Byrnes <jf_byrnes at comcast.net> 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?

My first thought was:

import os

os.system(insert_your_command)

But looking at the documentation
(https://docs.python.org/3/library/os.html#os.system) it says it is
preferable to use the subprocess module with documentation here:
https://docs.python.org/3/library/subprocess.html#replacing-os-system

It makes me wonder if I should go back and revisit some code I wrote
as I used the os.system()approach.  But was this option available in
Py 2.4.4?  I'll have to check.


-- 
boB

From cs at zip.com.au  Wed Dec 21 23:50:13 2016
From: cs at zip.com.au (cs at zip.com.au)
Date: Thu, 22 Dec 2016 15:50:13 +1100
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <CANDiX9Lvh8zSN9dvSYBhJP1337-=xANssR-TJOH3ZmXtjVqJzw@mail.gmail.com>
References: <CANDiX9Lvh8zSN9dvSYBhJP1337-=xANssR-TJOH3ZmXtjVqJzw@mail.gmail.com>
Message-ID: <20161222045013.GA2345@cskk.homeip.net>

On 21Dec2016 21:54, boB Stepp <robertvstepp at gmail.com> wrote:
>On Wed, Dec 21, 2016 at 9:37 PM, Jim Byrnes <jf_byrnes at comcast.net> 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?
>
>My first thought was:
>
>import os
>os.system(insert_your_command)
>
>But looking at the documentation
>(https://docs.python.org/3/library/os.html#os.system) it says it is
>preferable to use the subprocess module with documentation here:
>https://docs.python.org/3/library/subprocess.html#replacing-os-system
>
>It makes me wonder if I should go back and revisit some code I wrote
>as I used the os.system()approach.  But was this option available in
>Py 2.4.4?  I'll have to check.

Subprocess arrived with Python 2.4, so you should be fine.

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.

BTW, the array form is Popen's default mode; sensibly you need to _ask_ to use 
a shell string with shell=True, because that is harder and more fragile.

Cheers,
Cameron Simpson <cs at zip.com.au>

From eryksun at gmail.com  Thu Dec 22 01:20:40 2016
From: eryksun at gmail.com (eryk sun)
Date: Thu, 22 Dec 2016 06:20:40 +0000
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <20161222045013.GA2345@cskk.homeip.net>
References: <CANDiX9Lvh8zSN9dvSYBhJP1337-=xANssR-TJOH3ZmXtjVqJzw@mail.gmail.com>
 <20161222045013.GA2345@cskk.homeip.net>
Message-ID: <CACL+1aszfmJ2e8GmGsPRM7_JB6p2UVScjKBG-rRF5oWtwEc5yQ@mail.gmail.com>

On Thu, Dec 22, 2016 at 4:50 AM,  <cs at zip.com.au> wrote:
> BTW, the array form is Popen's default mode; sensibly you need to _ask_ to
> use a shell string with shell=True, because that is harder and more fragile.

Without shell=True, args as a string on POSIX is generally an error
because it will look for the entire string as the executable. The
exception is if the string has no command-line arguments (e.g.
Popen('ls')).

On Windows it's always acceptable to pass args as a string. For a
complex command line it may be easier to pass args as a list and let
Popen call list2cmdline to create a string that's properly quoted and
escaped. Here 'properly' assumes the target executable parses its
command line using VC++ rules. It may use custom rules, in which case
you have to pass args as a string.

From alan.gauld at yahoo.co.uk  Thu Dec 22 04:41:21 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Thu, 22 Dec 2016 09:41:21 +0000
Subject: [Tutor] Fwd: Question about selenium with python
In-Reply-To: <CAAsX1=0ZGr8J=cHSWD8gzkQZmXcKQBvMs3tdJY6mGRzjRacP9Q@mail.gmail.com>
References: <CAAsX1=1qr8XOYsK_x2cqKH3GDgOGrk8kZbMM17S2RrRm-ymn8A@mail.gmail.com>
 <CAAsX1=0ZGr8J=cHSWD8gzkQZmXcKQBvMs3tdJY6mGRzjRacP9Q@mail.gmail.com>
Message-ID: <o3g73s$j8u$1@blaine.gmane.org>

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



From alan.gauld at yahoo.co.uk  Thu Dec 22 04:47:48 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Thu, 22 Dec 2016 09:47:48 +0000
Subject: [Tutor] Using Python to solve factoria
In-Reply-To: <3E8D9EB5-7447-4D6E-822C-1C3998BA3888@gmail.com>
References: <3E8D9EB5-7447-4D6E-822C-1C3998BA3888@gmail.com>
Message-ID: <o3g7fv$i8u$1@blaine.gmane.org>

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



From alan.gauld at yahoo.co.uk  Thu Dec 22 04:54:27 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Thu, 22 Dec 2016 09:54:27 +0000
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <o3fhq1$t64$1@blaine.gmane.org>
References: <o3fhq1$t64$1@blaine.gmane.org>
Message-ID: <o3g7sf$ul$1@blaine.gmane.org>

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



From alan.gauld at yahoo.co.uk  Thu Dec 22 04:50:04 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Thu, 22 Dec 2016 09:50:04 +0000
Subject: [Tutor] Trouble Launching Python
In-Reply-To: <105659980.1897733.1482187087343@mail.yahoo.com>
References: <105659980.1897733.1482187087343.ref@mail.yahoo.com>
 <105659980.1897733.1482187087343@mail.yahoo.com>
Message-ID: <o3g7k7$i8u$2@blaine.gmane.org>

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



From robertvstepp at gmail.com  Thu Dec 22 11:20:31 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Thu, 22 Dec 2016 10:20:31 -0600
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <20161222045013.GA2345@cskk.homeip.net>
References: <CANDiX9Lvh8zSN9dvSYBhJP1337-=xANssR-TJOH3ZmXtjVqJzw@mail.gmail.com>
 <20161222045013.GA2345@cskk.homeip.net>
Message-ID: <CANDiX9+PUcOpXf5iJfPE4yLVOi6DPjAagpGjoYgZCJ1vxY+F8A@mail.gmail.com>

On Wed, Dec 21, 2016 at 10:50 PM,  <cs at zip.com.au> 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

From jf_byrnes at comcast.net  Thu Dec 22 13:12:13 2016
From: jf_byrnes at comcast.net (Jim Byrnes)
Date: Thu, 22 Dec 2016 12:12:13 -0600
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <o3g7sf$ul$1@blaine.gmane.org>
References: <o3fhq1$t64$1@blaine.gmane.org> <o3g7sf$ul$1@blaine.gmane.org>
Message-ID: <o3h51o$9u3$1@blaine.gmane.org>

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 
(<class 'selenium.common.exceptions.InvalidElementStateException'>: 
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 )]
)



From eryksun at gmail.com  Thu Dec 22 16:20:11 2016
From: eryksun at gmail.com (eryk sun)
Date: Thu, 22 Dec 2016 21:20:11 +0000
Subject: [Tutor] Open a libreoffice calc file in Python
In-Reply-To: <CANDiX9+PUcOpXf5iJfPE4yLVOi6DPjAagpGjoYgZCJ1vxY+F8A@mail.gmail.com>
References: <CANDiX9Lvh8zSN9dvSYBhJP1337-=xANssR-TJOH3ZmXtjVqJzw@mail.gmail.com>
 <20161222045013.GA2345@cskk.homeip.net>
 <CANDiX9+PUcOpXf5iJfPE4yLVOi6DPjAagpGjoYgZCJ1vxY+F8A@mail.gmail.com>
Message-ID: <CACL+1avS=5WeThhUO2DqwBXB0aX-dhmgVtrTxY6Ut6kZ7BAT8g@mail.gmail.com>

On Thu, Dec 22, 2016 at 4:20 PM, boB Stepp <robertvstepp at gmail.com> 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.

From donpryor206 at gmail.com  Thu Dec 22 20:47:47 2016
From: donpryor206 at gmail.com (Don Pryor)
Date: Thu, 22 Dec 2016 20:47:47 -0500
Subject: [Tutor] I download package and it says computer is missing this
 file, how do I fix?
Message-ID: <CANL2+T8tTHiVDQ=tpG4+weoRRiK_NUw2DFzHY22VGOp-uJBU3w@mail.gmail.com>

[image: Inline image 1]

-- 
Donald E. Pryor
*7GT    Seventh Generation Technologies L.L.C.  *
*Email*: donpryor206 at gmail.com
*Cell*:  330-554-8650

*NOTICE:* This message and any attached files are intended only for
the use of the addressee indicated above & are protected products
either patented, trademarked or intellectual property of Seventh
Generation Technologies L.L.C.  The products and information here may
not be shared or reproduced for profit without compensating 7GT and/or
our consultants/owners.

From alan.gauld at yahoo.co.uk  Fri Dec 23 04:10:08 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Fri, 23 Dec 2016 09:10:08 +0000
Subject: [Tutor] I download package and it says computer is missing this
 file, how do I fix?
In-Reply-To: <CANL2+T8tTHiVDQ=tpG4+weoRRiK_NUw2DFzHY22VGOp-uJBU3w@mail.gmail.com>
References: <CANL2+T8tTHiVDQ=tpG4+weoRRiK_NUw2DFzHY22VGOp-uJBU3w@mail.gmail.com>
Message-ID: <o3iplb$4g0$1@blaine.gmane.org>

On 23/12/16 01:47, Don Pryor wrote:
> [image: Inline image 1]
> 
This is a text mailing list so the server strips out most attachments.
You need to tell us a lot more.
1) What package are you trying to install
2) How are you downloading/installing it(pip/ftp/binary installer?)
3) Which OS and Python version
4) Which file is missing

If you get an error message that you can copy/paste into
a mail that will help too.

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



From jf_byrnes at comcast.net  Sat Dec 24 17:40:06 2016
From: jf_byrnes at comcast.net (Jim Byrnes)
Date: Sat, 24 Dec 2016 16:40:06 -0600
Subject: [Tutor] How to interact with the result of subprocess.call()
Message-ID: <o3mtg2$jt2$1@blaine.gmane.org>

subprocess.call(['libreoffice', '/home/jfb/test.ods'])
k.tap_key(k.enter_key)
k.tap_key(k.enter_key)

If I run the above code, libreoffice opens the test.ods spreadsheet then 
just sits there. When I close libreoffice the two enter_keys are 
executed in the terminal that originated the script.

How can I continue to send keystrokes to libreoffice from the script 
once it has been opened by subprocess.call()?

Thanks,  Jim



From dyoo at hashcollision.org  Sat Dec 24 18:10:19 2016
From: dyoo at hashcollision.org (Danny Yoo)
Date: Sat, 24 Dec 2016 15:10:19 -0800
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <o3mtg2$jt2$1@blaine.gmane.org>
References: <o3mtg2$jt2$1@blaine.gmane.org>
Message-ID: <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>

On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
> subprocess.call(['libreoffice', '/home/jfb/test.ods'])
> k.tap_key(k.enter_key)
> k.tap_key(k.enter_key)
>
> If I run the above code, libreoffice opens the test.ods spreadsheet then
> just sits there. When I close libreoffice the two enter_keys are executed in
> the terminal that originated the script.
>
> How can I continue to send keystrokes to libreoffice from the script once it
> has been opened by subprocess.call()?


Hi Jim,

You can not use subprocess to automate a GUI application.  This
approach will not work because libreoffice isn't written to pay
attention to the stdin file handle of its process.

That's one of the traditional reasons why GUI applications are
unpopular for programmers: in general, GUI-based applications are not
trivial to automate.

You do have a few options:


* See if the application provides a programming interface (an "API").

In the case of libreoffice, there does appear to be such an API:

    http://api.libreoffice.org/examples/examples.html#python_examples

Accessing it is very much outside the domain of Python-tutor: you will
likely need to talk with with the libreoffice folks.  But if you can
do this, it's probably nicer since the interface will use the terms of
libreoffice, rather than in terms of keystrokes, timer delays, and
mouse movement.


* More general automation of GUI applications is possible.  Here is a
link to pyautogui, a third-party library that handles GUI automation:

     http://pyautogui.readthedocs.io/en/latest/

Again, you'll probably need to talk with folks who have experience
with pyautogui; I don't think many of us on Tutor are very familiar
with it.


Good luck!

From jf_byrnes at comcast.net  Sat Dec 24 20:21:20 2016
From: jf_byrnes at comcast.net (Jim Byrnes)
Date: Sat, 24 Dec 2016 19:21:20 -0600
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
Message-ID: <o3n6ub$e9e$1@blaine.gmane.org>

On 12/24/2016 05:10 PM, Danny Yoo wrote:
> On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
>> subprocess.call(['libreoffice', '/home/jfb/test.ods'])
>> k.tap_key(k.enter_key)
>> k.tap_key(k.enter_key)
>>
>> If I run the above code, libreoffice opens the test.ods spreadsheet then
>> just sits there. When I close libreoffice the two enter_keys are executed in
>> the terminal that originated the script.
>>
>> How can I continue to send keystrokes to libreoffice from the script once it
>> has been opened by subprocess.call()?
>
>
> Hi Jim,
>
> You can not use subprocess to automate a GUI application.  This
> approach will not work because libreoffice isn't written to pay
> attention to the stdin file handle of its process.
>
> That's one of the traditional reasons why GUI applications are
> unpopular for programmers: in general, GUI-based applications are not
> trivial to automate.
>
> You do have a few options:
>
>
> * See if the application provides a programming interface (an "API").
>
> In the case of libreoffice, there does appear to be such an API:
>
>     http://api.libreoffice.org/examples/examples.html#python_examples
>
> Accessing it is very much outside the domain of Python-tutor: you will
> likely need to talk with with the libreoffice folks.  But if you can
> do this, it's probably nicer since the interface will use the terms of
> libreoffice, rather than in terms of keystrokes, timer delays, and
> mouse movement.
>
>
> * More general automation of GUI applications is possible.  Here is a
> link to pyautogui, a third-party library that handles GUI automation:
>
>      http://pyautogui.readthedocs.io/en/latest/
>
> Again, you'll probably need to talk with folks who have experience
> with pyautogui; I don't think many of us on Tutor are very familiar
> with it.
>
>
> Good luck!

Danny,


I am not trying to automate libreoffice using subprocess. In an earlier 
message I was told that subprocess was the way to open libreoffice from 
a python script. It does do that but now it seems to be blocking my 
attempts to send keystrokes to libreoffice.

Up until this point in the script I have used a combination of Selenium 
and pykeyboard to log on to a web site and put some info in the 
clipboard. Now I need to send keystrokes to libreoffice to paste from 
the clipboard into the spreadsheet.

I have used pyuno api to automate libreoffice in the past, but it was a 
time consuming and confusing process.  I was trying this approach 
because it looked like I could avoid the uno complexity.  I think it 
would work if I could figure out how to send keystrokes to libreoffice 
after it is opened using subprocess or some alternative that would work 
better.

Regards,  Jim


From alan.gauld at yahoo.co.uk  Sat Dec 24 20:43:16 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Sun, 25 Dec 2016 01:43:16 +0000
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <o3n6ub$e9e$1@blaine.gmane.org>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org>
Message-ID: <o3n87f$kg$1@blaine.gmane.org>

On 25/12/16 01:21, Jim Byrnes wrote:

> I am not trying to automate libreoffice using subprocess. 

No, but you are trying to automate LO from within Python
by sending it keystrokes and that's not easy. That's why I
previously asked whether you really wanted to open the LO
file directly and manipulate it from within Python
- that's (slightly) easier than manipulating LO directly
and much easier than manipulating LO from Python via
keystrokes.

> message I was told that subprocess was the way to open libreoffice from 
> a python script. 

Which is true if you want to bring up a LO session for
your user to manipulate. But it's not the way to drive
LO automatically. (One option is to start LO from Python
then use macros within LO to do the automation - there may
even be a command line switch to trigger a macro - I can't
remember off hand)

To drive LO via keystrokes your program needs to inject
key/mouse events into the LO event queue. That's not easy
and not very reliable either(*). There are some libraries that
can help but it should be the path of last resort.

(*)LO remembers its last screen setting and opens with them,
if those screen settings are different than the ones you
programmed for then navigation will be different and so on.
That's easy to deal with for a human who can see the screen
but sending keystrokes programmatically you are effectively
trying to drive the system blindfolded!

> Up until this point in the script I have used a combination of Selenium 
> and pykeyboard to log on to a web site and put some info in the 
> clipboard. Now I need to send keystrokes to libreoffice to paste from 
> the clipboard into the spreadsheet.

Or you could just open the spreadsheet file directly
and insert the data directly into it from Python. I think
there is a library for that - there are several for doing
it in Excel (so if your spreadsheet is in Excel format it
is fairly easy). Or, if you can use CSV format, its just a
standard library module.

Alternatively you can use the LO API to directly inject
the data into the spreadsheet objects (like using COM
in Microsoft land).

> I have used pyuno api to automate libreoffice in the past, but it was a 
> time consuming and confusing process.  

Trust me it is nowhere near as confusing and frustrating
as trying to drive LO (Or any other GUI) via keystrokes!

> I was trying this approach 
> because it looked like I could avoid the uno complexity.  

If there isn't a direct file manipulation library for LO
spreadsheets then UNO is probably the easiest option.


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



From robertvstepp at gmail.com  Sat Dec 24 20:58:19 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Sat, 24 Dec 2016 19:58:19 -0600
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <o3n6ub$e9e$1@blaine.gmane.org>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org>
Message-ID: <CANDiX9+9qQVjPjhbAPifEywxopgH5pJP=F047-=AdKLgG9eCQA@mail.gmail.com>

On Sat, Dec 24, 2016 at 7:21 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
> On 12/24/2016 05:10 PM, Danny Yoo wrote:
>>
>> On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
>>>
>>> subprocess.call(['libreoffice', '/home/jfb/test.ods'])
>>> k.tap_key(k.enter_key)
>>> k.tap_key(k.enter_key)
>>>
>>> If I run the above code, libreoffice opens the test.ods spreadsheet then
>>> just sits there. When I close libreoffice the two enter_keys are executed
>>> in
>>> the terminal that originated the script.
>>>
>>> How can I continue to send keystrokes to libreoffice from the script once
>>> it
>>> has been opened by subprocess.call()?

I was just looking at Alan's response which is probably the way to go,
but having never used the subprocess module to date, I am wondering if
the stdin option of call()might be used to direct the desired
keystrokes to LO?  After looking at

"subprocess.call(args, *, stdin=None, stdout=None, stderr=None,
shell=False, timeout=None)

Run the command described by args. Wait for command to complete, then
return the returncode attribute."

from the docs, I wonder if setting "stdin=PIPE" (or does it need to be
"subprocess.PIPE"?) might do what you want?  If I am understanding the
docs correctly, this will result in stdin going to the the new process
you created with call().  I don't have time to play with this --
Christmas Eve and all -- but I am curious if it would work.

Merry Christmas!

boB

From alan.gauld at yahoo.co.uk  Sun Dec 25 04:08:39 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Sun, 25 Dec 2016 09:08:39 +0000
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <CANDiX9+9qQVjPjhbAPifEywxopgH5pJP=F047-=AdKLgG9eCQA@mail.gmail.com>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org>
 <CANDiX9+9qQVjPjhbAPifEywxopgH5pJP=F047-=AdKLgG9eCQA@mail.gmail.com>
Message-ID: <o3o2ai$bil$1@blaine.gmane.org>

On 25/12/16 01:58, boB Stepp wrote:

> the stdin option of call()might be used to direct the desired
> keystrokes to LO?  After looking at

The problem is that keystrokes in a GUI are not read from
stdin, they are read as events from the GUI event loop.
So, if LO was a CLI tool (like vim or top, say) then you
are right, you could pipe the keystrokes through stdin,
but in a GUI those keystrokes would never be seen (or
if seen do something very different to what is expected)

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



From jf_byrnes at comcast.net  Sun Dec 25 11:33:34 2016
From: jf_byrnes at comcast.net (Jim Byrnes)
Date: Sun, 25 Dec 2016 10:33:34 -0600
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <o3n87f$kg$1@blaine.gmane.org>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org> <o3n87f$kg$1@blaine.gmane.org>
Message-ID: <o3oscp$k6t$1@blaine.gmane.org>

On 12/24/2016 07:43 PM, Alan Gauld via Tutor wrote:
> On 25/12/16 01:21, Jim Byrnes wrote:
>
>> I am not trying to automate libreoffice using subprocess.
>
> No, but you are trying to automate LO from within Python
> by sending it keystrokes and that's not easy. That's why I
> previously asked whether you really wanted to open the LO
> file directly and manipulate it from within Python
> - that's (slightly) easier than manipulating LO directly
> and much easier than manipulating LO from Python via
> keystrokes.


>> message I was told that subprocess was the way to open libreoffice from
>> a python script.
>
> Which is true if you want to bring up a LO session for
> your user to manipulate. But it's not the way to drive
> LO automatically. (One option is to start LO from Python
> then use macros within LO to do the automation - there may
> even be a command line switch to trigger a macro - I can't
> remember off hand)


> To drive LO via keystrokes your program needs to inject
> key/mouse events into the LO event queue. That's not easy
> and not very reliable either(*). There are some libraries that
> can help but it should be the path of last resort.
>
> (*)LO remembers its last screen setting and opens with them,
> if those screen settings are different than the ones you
> programmed for then navigation will be different and so on.
> That's easy to deal with for a human who can see the screen
> but sending keystrokes programmatically you are effectively
> trying to drive the system blindfolded!

I don't think I need to "know where stuff is" to manipulate LO. At first 
I was just using Selenium to get the data from the web page, but the 
focus would end up in the url bar. I forget the exact details but I 
could not get Selenium to manipulate Chrome anymore at that point. I did 
some searching and found pykeyboard. Using it I was able to send Ctrl-A 
and then Ctrl-C to copy the page to the clipboard.

My thinking is if I can get LO to accept keystrokes I can send 
Shift-Ctrl-V to paste special and the two enter keys to answer dialog 
questions and paste the info into a sheet.  I would use the fact that LO 
reopens to where it was closed to my advantage by not having to use a 
macro to navigate to the proper page.

>> Up until this point in the script I have used a combination of Selenium
>> and pykeyboard to log on to a web site and put some info in the
>> clipboard. Now I need to send keystrokes to libreoffice to paste from
>> the clipboard into the spreadsheet.
>
> Or you could just open the spreadsheet file directly
> and insert the data directly into it from Python. I think
> there is a library for that - there are several for doing
> it in Excel (so if your spreadsheet is in Excel format it
> is fairly easy). Or, if you can use CSV format, its just a
> standard library module.

I'll look into these alternatives if I can't figure out how to get 
keystrokes into LO using my present approach.

> Alternatively you can use the LO API to directly inject
> the data into the spreadsheet objects (like using COM
> in Microsoft land).
>
>> I have used pyuno api to automate libreoffice in the past, but it was a
>> time consuming and confusing process.
>
> Trust me it is nowhere near as confusing and frustrating
> as trying to drive LO (Or any other GUI) via keystrokes!

Based on my success with pykeyboard and Chrome I thought it would be 
easier than diving back into Uno.  However, using subprocess seems to be 
blocking me from sending any keystrokes to LO. I don't understand 
subprocess well enough to know if it is actually blocking my keystrokes. 
I concluded that based on the fact that when I closed LO the two 
enter_keys at the end of the script were executed in the terminal.

Is there a way to terminate subprocess and still keep LO open so 
pykeyboard can send it keystrokes from the script?

>> I was trying this approach
>> because it looked like I could avoid the uno complexity.
>
> If there isn't a direct file manipulation library for LO
> spreadsheets then UNO is probably the easiest option.
>
>

Regards,  Jim



From robertvstepp at gmail.com  Sun Dec 25 12:08:06 2016
From: robertvstepp at gmail.com (boB Stepp)
Date: Sun, 25 Dec 2016 11:08:06 -0600
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <o3o2ai$bil$1@blaine.gmane.org>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org>
 <CANDiX9+9qQVjPjhbAPifEywxopgH5pJP=F047-=AdKLgG9eCQA@mail.gmail.com>
 <o3o2ai$bil$1@blaine.gmane.org>
Message-ID: <CANDiX9++kd_FCVaVF8ruuc+Je7-6=rHpzQ_sxK5hhR8VjQ5Gfg@mail.gmail.com>

On Sun, Dec 25, 2016 at 3:08 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
>
> On 25/12/16 01:58, boB Stepp wrote:
>
> > the stdin option of call()might be used to direct the desired
> > keystrokes to LO?  After looking at
>
> The problem is that keystrokes in a GUI are not read from
> stdin, they are read as events from the GUI event loop.
> So, if LO was a CLI tool (like vim or top, say) then you
> are right, you could pipe the keystrokes through stdin,
> but in a GUI those keystrokes would never be seen (or
> if seen do something very different to what is expected)

Then I see that I have a GCE (Gross Conceptual Error) floating around.
I thought that event loops are just intercepting the redirected stdin
from the keyboard.  This is not true?  If not, then how is this
working?

boB

From alan.gauld at yahoo.co.uk  Sun Dec 25 14:26:13 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Sun, 25 Dec 2016 19:26:13 +0000
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <CANDiX9++kd_FCVaVF8ruuc+Je7-6=rHpzQ_sxK5hhR8VjQ5Gfg@mail.gmail.com>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org>
 <CANDiX9+9qQVjPjhbAPifEywxopgH5pJP=F047-=AdKLgG9eCQA@mail.gmail.com>
 <o3o2ai$bil$1@blaine.gmane.org>
 <CANDiX9++kd_FCVaVF8ruuc+Je7-6=rHpzQ_sxK5hhR8VjQ5Gfg@mail.gmail.com>
Message-ID: <o3p6gg$bpj$1@blaine.gmane.org>

On 25/12/16 17:08, boB Stepp wrote:

> Then I see that I have a GCE (Gross Conceptual Error) floating around.
> I thought that event loops are just intercepting the redirected stdin
> from the keyboard.  This is not true?  If not, then how is this
> working?

No event loops don't use stdin. They are monitoring the
hardware devices directly and creating event objects that
are put in a queue maintained by the GUI framework itself.
Most frameworks allow you create your own events and
inject them into the queue but they don;t come from stdin.
You could write a background thread that read stdin and
generated events from there but that's not normal by any
means. Some GUI programs will read stdin and process anything
found there but its usually a completely different set
of commands from those used by the normal user - and
even this is very rare.

For more exact detail of how GUIs get their input you
will need to investigate the individual GUIs, even
drilling down to X-Server level or the Windows API.

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



From sunil.techspk at gmail.com  Mon Dec 26 03:03:37 2016
From: sunil.techspk at gmail.com (Sunil Tech)
Date: Mon, 26 Dec 2016 13:33:37 +0530
Subject: [Tutor] Manipulating Dictionary values
Message-ID: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>

Hi Team,

Dictionary is like

a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'again'}, {'k':
'test', 'm': 'again'}]}

I am trying to modify a value in the dictionary value at a['l'] & at 'm'
expecting it to be

a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'replaced'}, {'k':
'test', 'm': 'replaced'}]}

for which I tried to do using list comprehension

>>> a['l'] = [i['m'].replace('again', 'replaced') for i in a['l']]
>>> a
{'a': 'New', 'b': 'Two', 'l': ['replaced', 'replaced']}

Any help will be appreciated.

From sunil.techspk at gmail.com  Mon Dec 26 03:45:59 2016
From: sunil.techspk at gmail.com (Sunil Tech)
Date: Mon, 26 Dec 2016 14:15:59 +0530
Subject: [Tutor] Manipulating Dictionary values
In-Reply-To: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
References: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
Message-ID: <CAExJxTPU9hL=4iG4itP+A9tCnM7XZ6uYfqA9Zyi_FBD71Eoqtg@mail.gmail.com>

??
Can this be achievable in one liner?

On Mon, Dec 26, 2016 at 1:33 PM, Sunil Tech <sunil.techspk at gmail.com> wrote:
>
> Hi Team,
>
> Dictionary is like
>
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'again'}, {'k':
'test', 'm': 'again'}]}
>
> I am trying to modify a value in the dictionary value at a['l'] & at 'm'
> expecting it to be
>
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'replaced'}, {'k':
'test', 'm': 'replaced'}]}
>
> for which I tried to do using list comprehension
>
> >>> a['l'] = [i['m'].replace('again', 'replaced') for i in a['l']]
> >>> a
> {'a': 'New', 'b': 'Two', 'l': ['replaced', 'replaced']}
>
> Any help will be appreciated.

From smileyunder18 at yahoo.com  Sun Dec 25 23:08:47 2016
From: smileyunder18 at yahoo.com (syafiqah amir)
Date: Mon, 26 Dec 2016 04:08:47 +0000 (UTC)
Subject: [Tutor] accessing attribute from python programming for absolute
 beginner
References: <1828078494.1857725.1482725327255.ref@mail.yahoo.com>
Message-ID: <1828078494.1857725.1482725327255@mail.yahoo.com>

I did the critter programming however i did not achieve the desired outcome which is the name of the critters did not print on the screen insted of self.name .I'm not sure what is wrong with this .Hope someone can help me.Thank you so much .
#attribute Critter#Demonstrates creating and accessing object attributes
class Critter(object):? ? """A virtual pet"""? ? def __init__(self,name):? ? ? ? print ("A new critter has been born!")? ? ? ? self.name = name
? ? def __str__(self):? ? ? ? rep= "Critter object\n"? ? ? ? rep+="name: "+self.name+"\n"? ? ? ? return rep
? ? def talk(self):? ? ? ? print("Hi.I'm, self.name ", "\n")
#main? ? ? ? ? ? ??crit1 = Critter("Poochie")crit1.talk()
crit2= Critter("Randolph")crit2.talk()
print("Printing crit1:")print(crit1)
print("Directly accessing crit1.name")print(crit1.name)
print("\n\nPress the enter key to exit.")
#########################################A new critter has been born!Hi.I'm, self.name ?
A new critter has been born!Hi.I'm, self.name ?
Printing crit1:name: Poochie
Directly accessing crit1.namePoochie

Press the enter key to exit.



From steve at pearwood.info  Mon Dec 26 05:32:17 2016
From: steve at pearwood.info (Steven D'Aprano)
Date: Mon, 26 Dec 2016 21:32:17 +1100
Subject: [Tutor] Manipulating Dictionary values
In-Reply-To: <CAExJxTPU9hL=4iG4itP+A9tCnM7XZ6uYfqA9Zyi_FBD71Eoqtg@mail.gmail.com>
References: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
 <CAExJxTPU9hL=4iG4itP+A9tCnM7XZ6uYfqA9Zyi_FBD71Eoqtg@mail.gmail.com>
Message-ID: <20161226103216.GL3365@ando.pearwood.info>

On Mon, Dec 26, 2016 at 02:15:59PM +0530, Sunil Tech wrote:
> ??
> Can this be achievable in one liner?

Why? Can you only fit one more line of code before your hard drive is 
full?



-- 
Steve

From __peter__ at web.de  Mon Dec 26 05:48:58 2016
From: __peter__ at web.de (Peter Otten)
Date: Mon, 26 Dec 2016 11:48:58 +0100
Subject: [Tutor] How to interact with the result of subprocess.call()
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org> <o3n87f$kg$1@blaine.gmane.org>
 <o3oscp$k6t$1@blaine.gmane.org>
Message-ID: <o3qsim$pi9$1@blaine.gmane.org>

Jim Byrnes wrote:

> Is there a way to terminate subprocess and still keep LO open so
> pykeyboard can send it keystrokes from the script?

In theory you can open Libre Office from another thread, wait a moment and 
then send it keystrokes from the main thread. I managed to do this with the 
script below.

However, the procedure is very brittle; while experimenting I managed to 
"press" the control key without releasing it afterwards. The interval from 
doing it to realizing what was going on to "fixing" it (reboot) was an 
interesting experience...

from contextlib import contextmanager
import subprocess
import threading
import time

import pykeyboard

kb = pykeyboard.PyKeyboard()


@contextmanager
def press_key(key, kb=kb):
    kb.press_key(key)
    try:
        yield
        time.sleep(1)
    finally:
        kb.release_key(key)


def open_it(filename):
    subprocess.call(["libreoffice", filename])
    print("exiting libreoffice thread")


LONG_ENOUGH = 15  # seconds

# enter text into an existing odt file
filename = "demo.odt"
text = "hello and goodbye"

opener = threading.Thread(target=open_it, args=(filename,))
opener.start()

time.sleep(LONG_ENOUGH)  # for libreoffice to start and open the file

kb.type_string(text)

with press_key(kb.control_key):
    kb.tap_key("s")
with press_key(kb.alt_key):
    kb.tap_key(kb.function_keys[4])


print("exiting main thread")



From alan.gauld at yahoo.co.uk  Mon Dec 26 05:51:22 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Mon, 26 Dec 2016 10:51:22 +0000
Subject: [Tutor] accessing attribute from python programming for
 absolute beginner
In-Reply-To: <1828078494.1857725.1482725327255@mail.yahoo.com>
References: <1828078494.1857725.1482725327255.ref@mail.yahoo.com>
 <1828078494.1857725.1482725327255@mail.yahoo.com>
Message-ID: <o3qsn5$s7m$1@blaine.gmane.org>

On 26/12/16 04:08, syafiqah amir via Tutor wrote:
> i did not achieve the desired outcome which is the name of the critters did not print 

> #attribute Critter#Demonstrates creating and accessing object attributes
> class Critter(object):    """A virtual pet"""    def __init__(self,name):        print ("A new critter has been born!")        self.name = name
>     def __str__(self):        rep= "Critter object\n"        rep+="name: "+self.name+"\n"        return rep
>     def talk(self):        print("Hi.I'm, self.name ", "\n")
> #main              crit1 = Critter("Poochie")crit1.talk()
> crit2= Critter("Randolph")crit2.talk()
> print("Printing crit1:")print(crit1)
> print("Directly accessing crit1.name")print(crit1.name)
> print("\n\nPress the enter key to exit.")
> #########################################A new critter has been born!Hi.I'm, self.name  
> A new critter has been born!Hi.I'm, self.name  
> Printing crit1:name: Poochie
> Directly accessing crit1.namePoochie
> 
> Press the enter key to exit.

Please post in plain text because, as you can see, the code
is mangled when you use HTML. However in this case its
easy to spot the problem...

    def talk(self):        print("Hi.I'm, self.name ", "\n")

The second quotation sign should be after I'm, before the comma.
You probably don;t need the \n at the end since print() puts
one in by default.

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



From steve at pearwood.info  Mon Dec 26 06:09:02 2016
From: steve at pearwood.info (Steven D'Aprano)
Date: Mon, 26 Dec 2016 22:09:02 +1100
Subject: [Tutor] Manipulating Dictionary values
In-Reply-To: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
References: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
Message-ID: <20161226110901.GM3365@ando.pearwood.info>

On Mon, Dec 26, 2016 at 01:33:37PM +0530, Sunil Tech wrote:
> Hi Team,
> 
> Dictionary is like
> 
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'again'}, {'k':
> 'test', 'm': 'again'}]}
> 
> I am trying to modify a value in the dictionary value at a['l'] & at 'm'
> expecting it to be
> 
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'replaced'}, {'k':
> 'test', 'm': 'replaced'}]}

Simplify the problem. The dictionary "a" is not relevant. All the 
processing is happening to the list:

[{'k': 'test', 'm': 'again'}, {'k': 'test', 'm': 'again'}]

First thing: don't use the string.replace() method unless that is what 
you *really* mean. Remember that replace works on substrings, not the 
entire string. Suppose you have:

[{'k': 'test', 'm': 'against'}, {'k': 'test', 'm': 'again'}]

If you use string.replace('again', 'replaced') then your result will be:

[{'k': 'test', 'm': 'replacedst'}, {'k': 'test', 'm': 'again'}]

which is surely not what you want.

So your first question should be: how do you change the value of a 
dict with key 'm' from 'again' to 'replaced'?

# Before
d = {'a': 'something', 'b': 'who cares?', 'm': 'again'}

# After
d = {'a': 'something', 'b': 'who cares?', 'm': 'replaced'}

And the answer is:

if d['m'] == 'again':
    d['m'] = 'replaced'


Now you just need to write a loop to do that for every dict in the list:


alist = [{'k': 'test', 'm': 'again'}, {'k': 'test', 'm': 'again'}]
for adict in alist:
    if adict['m'] == 'again':
        adict['m'] = 'replaced'
    
print(alist)


What if the list is inside a dict? It doesn't matter.

a = {'a': 'New', 
     'b': 'Two', 
     'l': [{'k': 'test', 'm': 'again'}, {'k': 'foo bar', 'm': 'again'}],
     'z': 'something else',
     }

for adict in a['l']:
    if adict['m'] == 'again':
        adict['m'] = 'replaced'

print(a)



Last one: can we do this as an expression, using a list comprehension? 
(Why do we want to?) Yes, but only by writing more complicated code and 
doing much more work, which means it will probably be slower. MUCH 
slower.

a = {'a': 'New', 
     'b': 'Two', 
     'l': [{'k': 'test', 'm': 'again'}, {'k': 'foo bar', 'm': 'again'}],
     'z': 'something else',
     }


a['l'] = [**** for adict in a['l']]

What code goes into the **** in the list comp? It has to be a function 
which takes a dict, and returns the same dict, or a copy of that dict, 
with value associated with the key 'm' conditionally replaced.

(If this sounds complicated to you, that's because it is complicated. 
Why does this have to be a list comprehension?)

I can easily write a helper function:

def modify(adict):
    if adict['m'] == 'again':
        adict['m'] = 'replaced'
    return adict

and then use:

a['l'] = [modify(adict) for adict in a['l']]

but what if you don't want the helper function? Then it becomes really 
complicated, but here is a one-liner with no dependencies and no helper 
functions needed:

a['l'] = [dict([(key, 'replaced' if value is 'again' else value) for (key, value) in adict.items()]) for adict in a['l']]

Look how much unnecessary and pointless work this does, and how hard it 
is to understand, compared to this version:

for adict in a['l']:
    if adict['m'] == 'again':
        adict['m'] = 'replaced'


One really complicated, hard to understand, slow line, versus three 
simple, easy to understand, fast lines.

Beginners often want to do things as one-liners. More experienced coders 
keep asking, why does it need to be a one-liner? How much extra work do 
you want to do just to avoid pressing the Enter key on your keyboard?





-- 
Steve

From alan.gauld at yahoo.co.uk  Mon Dec 26 06:11:54 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Mon, 26 Dec 2016 11:11:54 +0000
Subject: [Tutor] Manipulating Dictionary values
In-Reply-To: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
References: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
Message-ID: <o3qttl$86f$1@blaine.gmane.org>

On 26/12/16 08:03, Sunil Tech wrote:
> Hi Team,
> 
> Dictionary is like
> 
> a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'again'}, {'k':
> 'test', 'm': 'again'}]}
> 
> I am trying to modify a value in the dictionary value at a['l']

So make life easy for yourself and get rid of the outer dictionary
and use the python prompt to experiment:

>>> L = [{'k': 'test', 'm': 'again'}, {'k': 'test', 'm': 'again'}]
>>> [{'k':d['k'],'m':'replaced'} for d in L]
[{'k': 'test', 'm': 'replaced'}, {'k': 'test', 'm': 'replaced'}]
>>>

But that's not very general, you really should have a generator
for the dictionary that has a conditional expression within to
replace the m. But that means having a generator within a
comprehension inside a dictionary access.

It's all getting a bit ugly and overly complex and our goal as
programmers is to write clear, easily maintainable code, so
this is probably a bad idea.

Better to unroll into an explicit loop and make it obvious
what you are doing.

for d in a['l']: d['m'] = 'replaced'

Isn't that clearer?

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



From sunil.techspk at gmail.com  Mon Dec 26 06:37:05 2016
From: sunil.techspk at gmail.com (Sunil Tech)
Date: Mon, 26 Dec 2016 17:07:05 +0530
Subject: [Tutor] Manipulating Dictionary values
In-Reply-To: <o3qttl$86f$1@blaine.gmane.org>
References: <CAExJxTMtVV6RyrhNXDSwag2Jk_6pB-LvG3AzXsCQ04kwnHFQ0w@mail.gmail.com>
 <o3qttl$86f$1@blaine.gmane.org>
Message-ID: <CAExJxTMm4cZGuZc0Q6e_n0g65aJ=dXhyGCYFaquSBTxNTjb26g@mail.gmail.com>

Thank you Steven D'Aprano and Alan Gauld.

On Mon, Dec 26, 2016 at 4:41 PM, Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 26/12/16 08:03, Sunil Tech wrote:
> > Hi Team,
> >
> > Dictionary is like
> >
> > a = {'a': 'New', 'b': 'Two', 'l': [{'k': 'test', 'm': 'again'}, {'k':
> > 'test', 'm': 'again'}]}
> >
> > I am trying to modify a value in the dictionary value at a['l']
>
> So make life easy for yourself and get rid of the outer dictionary
> and use the python prompt to experiment:
>
> >>> L = [{'k': 'test', 'm': 'again'}, {'k': 'test', 'm': 'again'}]
> >>> [{'k':d['k'],'m':'replaced'} for d in L]
> [{'k': 'test', 'm': 'replaced'}, {'k': 'test', 'm': 'replaced'}]
> >>>
>
> But that's not very general, you really should have a generator
> for the dictionary that has a conditional expression within to
> replace the m. But that means having a generator within a
> comprehension inside a dictionary access.
>
> It's all getting a bit ugly and overly complex and our goal as
> programmers is to write clear, easily maintainable code, so
> this is probably a bad idea.
>
> Better to unroll into an explicit loop and make it obvious
> what you are doing.
>
> for d in a['l']: d['m'] = 'replaced'
>
> Isn't that clearer?
>
> --
> 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 at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

From alan.gauld at yahoo.co.uk  Mon Dec 26 08:57:02 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Mon, 26 Dec 2016 13:57:02 +0000
Subject: [Tutor] How to interact with the result of subprocess.call()
In-Reply-To: <o3oscp$k6t$1@blaine.gmane.org>
References: <o3mtg2$jt2$1@blaine.gmane.org>
 <CAGZAPF7Fq=fKhoStohHGHE0KFuA+t5JYvfmwOSoQ9mhgtW8WPA@mail.gmail.com>
 <o3n6ub$e9e$1@blaine.gmane.org> <o3n87f$kg$1@blaine.gmane.org>
 <o3oscp$k6t$1@blaine.gmane.org>
Message-ID: <o3r7j8$gqi$1@blaine.gmane.org>

On 25/12/16 16:33, Jim Byrnes wrote:

>> (*)LO remembers its last screen setting and opens with them,
>> if those screen settings are different than the ones you
>> programmed for then navigation will be different and so on.
> 
> I don't think I need to "know where stuff is" to manipulate LO. 

It depends on what you are doing. If you want to insert text
into a cell say you need to navigate to that cell.
In this respect a spreadsheet is much easier to work
with than, say, a word processor, but you still need
to be careful.

> some searching and found pykeyboard. Using it I was able to send Ctrl-A 
> and then Ctrl-C to copy the page to the clipboard.

I've never used (or even heard of) pykeyboard but it sounds
like its doing most of the hard stuff for you. It may well
work with LO.

> easier than diving back into Uno.  However, using subprocess seems to be 
> blocking me from sending any keystrokes to LO. 

subprocess has nothing to do with the keystrokes part
but you may need to run it in the background or somesuch
to get the keystrokes stuff working in parallel.

> subprocess well enough to know if it is actually blocking my keystrokes. 

No it's not blocking your keystrokes but it may well
be blocking your process.

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



From jjhartley at gmail.com  Tue Dec 27 11:48:00 2016
From: jjhartley at gmail.com (James Hartley)
Date: Tue, 27 Dec 2016 10:48:00 -0600
Subject: [Tutor] overriding brackets in lvalue assignment possible?
Message-ID: <CAKeNXXvusLAzDt9JvJX+HHT0vr998eC+AHLw+HEjs78o9wQP8A@mail.gmail.com>

I can successfully override __getitem__() for rvalues, but the following
example shows that more is required when used as an lvalue:

===8<-----
#!/usr/bin/env python

class Foo():
    def __init__(self, n):
        self.d = dict.fromkeys([i for i in range(0, n)])

    def __getitem__(self, i):
        return self.d[i]

def main():
    foo = Foo(4)
    print(foo[0])
    foo[0] = 2               # not as an lvalue?
    print(foo[0])

if __name__ == '__main__':
    main()
===8<-----

Python 3.4 generates the following output when this example is executed:

None
Traceback (most recent call last):
  File "./test.py", line 17, in <module>
    main()
  File "./test.py", line 13, in main
    foo[0] = 2
TypeError: 'Foo' object does not support item assignment

I am surprised that the error states that the object itself cannot accept
assignment.  From the C++ perspective, the underlying dictionary should be
exposed.  Does Python overloading allow use of bracket overriding in
lvalues?

Thanks.

From zachary.ware+pytut at gmail.com  Tue Dec 27 12:01:15 2016
From: zachary.ware+pytut at gmail.com (Zachary Ware)
Date: Tue, 27 Dec 2016 11:01:15 -0600
Subject: [Tutor] overriding brackets in lvalue assignment possible?
In-Reply-To: <CAKeNXXvusLAzDt9JvJX+HHT0vr998eC+AHLw+HEjs78o9wQP8A@mail.gmail.com>
References: <CAKeNXXvusLAzDt9JvJX+HHT0vr998eC+AHLw+HEjs78o9wQP8A@mail.gmail.com>
Message-ID: <CAKJDb-Nta6WDkcn0qgSnWOvJD+VJP-QmPbxy0dzeOtks46BABA@mail.gmail.com>

On Tue, Dec 27, 2016 at 10:48 AM, James Hartley <jjhartley at gmail.com> wrote:
> I can successfully override __getitem__() for rvalues, but the following
> example shows that more is required when used as an lvalue:
>
> ===8<-----
> #!/usr/bin/env python
>
> class Foo():
>     def __init__(self, n):
>         self.d = dict.fromkeys([i for i in range(0, n)])
>
>     def __getitem__(self, i):
>         return self.d[i]
>
> def main():
>     foo = Foo(4)
>     print(foo[0])
>     foo[0] = 2               # not as an lvalue?
>     print(foo[0])
>
> if __name__ == '__main__':
>     main()
> ===8<-----
>
> Python 3.4 generates the following output when this example is executed:
>
> None
> Traceback (most recent call last):
>   File "./test.py", line 17, in <module>
>     main()
>   File "./test.py", line 13, in main
>     foo[0] = 2
> TypeError: 'Foo' object does not support item assignment
>
> I am surprised that the error states that the object itself cannot accept
> assignment.  From the C++ perspective, the underlying dictionary should be
> exposed.  Does Python overloading allow use of bracket overriding in
> lvalues?

Hi James,

You're looking for __setitem__(), see
https://docs.python.org/3/reference/datamodel.html#object.__setitem__

Hope this helps,
-- 
Zach

From __peter__ at web.de  Tue Dec 27 12:13:16 2016
From: __peter__ at web.de (Peter Otten)
Date: Tue, 27 Dec 2016 18:13:16 +0100
Subject: [Tutor] overriding brackets in lvalue assignment possible?
References: <CAKeNXXvusLAzDt9JvJX+HHT0vr998eC+AHLw+HEjs78o9wQP8A@mail.gmail.com>
Message-ID: <o3u7f8$6f7$1@blaine.gmane.org>

James Hartley wrote:

> I can successfully override __getitem__() for rvalues, but the following
> example shows that more is required when used as an lvalue:
> 
> ===8<-----
> #!/usr/bin/env python
> 
> class Foo():
>     def __init__(self, n):
>         self.d = dict.fromkeys([i for i in range(0, n)])
> 
>     def __getitem__(self, i):
>         return self.d[i]
> 
> def main():
>     foo = Foo(4)
>     print(foo[0])
>     foo[0] = 2               # not as an lvalue?
>     print(foo[0])
> 
> if __name__ == '__main__':
>     main()
> ===8<-----
> 
> Python 3.4 generates the following output when this example is executed:
> 
> None
> Traceback (most recent call last):
>   File "./test.py", line 17, in <module>
>     main()
>   File "./test.py", line 13, in main
>     foo[0] = 2
> TypeError: 'Foo' object does not support item assignment
> 
> I am surprised that the error states that the object itself cannot accept
> assignment.  From the C++ perspective, the underlying dictionary should be
> exposed.  Does Python overloading allow use of bracket overriding in
> lvalues?

Since Python doesn't allow multiple method definitions with different 
signatures under the same name there's a dedicated __setitem__() method:

>>> class Foo:
...     def __setitem__(self, index, value): print("Setting Foo()[{!r}] to 
{!r}".format(index, value))
... 
>>> foo = Foo()
>>> foo[42] = "bar"
Setting Foo()[42] to 'bar'


Of course with the above definition

>>> foo[42]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Foo' object does not support indexing

but you already know how to fix that. To get a class that works like 
Python's list or dict while only writing a few methods yourself you can 
inherit from collections.MutableSequence or MutableMapping.

PS: Even without function overloading the developers might have chosen a 
common method for both, e. g.

def __item__(self, index, *args):
   if args:
       [value] = args
       # set item
   else:
       result = ... # calculate current value
       return result

The actual choice is cleaner and a tad more efficient.


From Kumar.Mysore at cargolux.com  Tue Dec 27 10:22:21 2016
From: Kumar.Mysore at cargolux.com (Mysore Ventaka Rama Kumar)
Date: Tue, 27 Dec 2016 15:22:21 +0000
Subject: [Tutor] IndexError: list index out of range
Message-ID: <C564C49E7552CB45912C6E61D2BF6509EFE64472@exdag11.cargolux.com>

Please review and comment/correct my error!
Many thanks in advance.



# Required module
import os

# Function for getting files from a folder
def fetchFiles(pathToFolder, flag, keyWord):
	'''	fetchFiles() requires three arguments: pathToFolder, flag and 
 keyWord flag must be 'STARTS_WITH' or 'ENDS_WITH' keyWord is a string to 
  search the file's name  Be careful, the keyWord is case sensitive and must
  be exact.  Example: fetchFiles('/Documents/Photos/','ENDS_WITH','.jpg')
returns: _pathToFiles and _fileNames '''
	
	_pathToFiles = []
	_fileNames = []

	for dirPath, dirNames, fileNames in os.walk(pathToFolder):
		if flag == 'ENDS_WITH':
			selectedPath = [os.path.join(dirPath,item) for item in fileNames if item.endswith(keyWord)]
			_pathToFiles.extend(selectedPath)
			
			selectedFile = [item for item in fileNames if item.endswith(keyWord)]
			_fileNames.extend(selectedFile)
			
		elif flag == 'STARTS_WITH':
			selectedPath = [os.path.join(dirPath,item) for item in fileNames if item.startswith(keyWord)]
			_pathToFiles.extend(selectedPath)
			
			selectedFile = [item for item in fileNames if item.startswith(keyWord)]
			_fileNames.extend(selectedFile) 
			    
		else:
			print fetchFiles.__doc__
			break
						
		# Try to remove empty entries if none of the required files are in directory
		try:
			_pathToFiles.remove('')
			_imageFiles.remove('')
		except ValueError:
			pass
			
		# Warn if nothing was found in the given path
		#if selectedFile == []: 
			#print 'No files with given parameters were found in:\n', dirPath, '\n'
                
                #print len(_fileNames), 'files were found is searched folder(s)' 
      		
        #return _pathToFiles, _fileNames
        #print _pathToFiles, _fileNames
        #print _pathToFiles [1]
        print _pathToFiles
        s = ' '.join(_pathToFiles [1]) #convert tuple element 1 to string
        #print len (s)
        s_no_white = s.replace(" ", "") #remove white spaces
        #print s_no_white[4:7] #extract rgeistration
        s1 = s_no_white[4:7] #extract rgeistration
        print 'str1 is:', str1
        str2 = 'FLDAT'
        print 'str2 is: ', str2
        str3 = str1.__add__(str2)
        print 'str 3 is: ',str3
        str4 = 'H//'
        print 'str4 is: ', str4
        str5 = str4.__add__(str3)
        print 'str5 is: ', str5        
        
        
        print _fileNames
        print 'Number of files found: ', len(_fileNames)
        
fetchFiles('str5','ENDS_WITH','.FLD')

####code end##

###Output##

  File "D:/university_2/my_code_2/good_2016_12_01/get_registration.py", line 52, in fetchFiles
    s = ' '.join(_pathToFiles [1]) #convert tuple element 1 to string

IndexError: list index out of range

##output end##



From alan.gauld at yahoo.co.uk  Tue Dec 27 14:16:47 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Tue, 27 Dec 2016 19:16:47 +0000
Subject: [Tutor] IndexError: list index out of range
In-Reply-To: <C564C49E7552CB45912C6E61D2BF6509EFE64472@exdag11.cargolux.com>
References: <C564C49E7552CB45912C6E61D2BF6509EFE64472@exdag11.cargolux.com>
Message-ID: <o3uemp$mg6$1@blaine.gmane.org>

On 27/12/16 15:22, Mysore Ventaka Rama Kumar wrote:
> Please review and comment/correct my error!

Rather than expect us to read through many lines of code
it would help id you posted the entire error message
which will tell us exactly where the problem lies.

Also tell us for completeness  which Python version
and OS you are using.

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



From richkappler at gmail.com  Tue Dec 27 14:44:36 2016
From: richkappler at gmail.com (richard kappler)
Date: Tue, 27 Dec 2016 14:44:36 -0500
Subject: [Tutor] formatting xml (again)
Message-ID: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>

Using python 2.7 - I have a large log file we recorded of streamed xml data
that I now need to feed into another app for stress testing. The problem is
the data comes in 2 formats.

1. each 'event' is a full set of xml data with opening and closing tags +
x02 and x03 (stx and etx)

2. some events have all the xml data on one 'line' in the log, others are
in typical nested xml format with lots of white space and multiple 'lines'
in the log for each event, the first line of th e 'event' starting with an
stx and the last line of the 'event' ending in an etx.

Examples of how the data looks from an editor (gedit specifically in this
case):
1[x02]<data><level1_data>some stuff<level2_data>some more
stuff></level2_data></level1_data></data>[x03]
2[x02]<data>
3     <level1_data>
4           some stuff
5           <level2_data>somestuff</level2_data>
6    </level1_data>
7</data>[x03]

I have tried to feed this raw into our other app (Splunk) and the app reads
each line (gedit numbered line) as an event. I want everything in between
each stx and etx to be one event.

I have tried:

#####################################
with open("original.log", 'r') as f1:
    with open("new.log", 'a') as f2:
        for line in f1:
            line2 = line.replace("\n", "")
            f2.write(line2)
######################################

Now this obviously doesn't work because, as stated above, each tag and
datum in the example above from lines 2 to 7 is on a different line, so
python is doing exactly as I tell it, it's stripping the \n and then
printing the line, but not concatenating everything between stx and etx on
one line, which is what I want it to do.

What I'm trying to do is collapse the 'expanded lines' between stx and etx
to one line, but I just can't wrap my head around how to do it. Or to put,
and do, it another way, how do I read each line from the original file, but
write it to another file so that everything from stx to etx, including stx
and etx, are on one line in the file?

regards Richard

From david at graniteweb.com  Tue Dec 27 15:25:16 2016
From: david at graniteweb.com (David Rock)
Date: Tue, 27 Dec 2016 14:25:16 -0600
Subject: [Tutor] formatting xml (again)
In-Reply-To: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
Message-ID: <20161227202516.GA18771@apple.graniteweb.com>

* richard kappler <richkappler at gmail.com> [2016-12-27 14:44]:
> 
> I have tried to feed this raw into our other app (Splunk) and the app reads
> each line (gedit numbered line) as an event. I want everything in between
> each stx and etx to be one event.
> 
> I have tried:
> 
> #####################################
> with open("original.log", 'r') as f1:
>     with open("new.log", 'a') as f2:
>         for line in f1:
>             line2 = line.replace("\n", "")
>             f2.write(line2)
> ######################################
> 
> Now this obviously doesn't work because, as stated above, each tag and
> datum in the example above from lines 2 to 7 is on a different line, so
> python is doing exactly as I tell it, it's stripping the \n and then
> printing the line, but not concatenating everything between stx and etx on
> one line, which is what I want it to do.
> 
> What I'm trying to do is collapse the 'expanded lines' between stx and etx
> to one line, but I just can't wrap my head around how to do it. Or to put,
> and do, it another way, how do I read each line from the original file, but
> write it to another file so that everything from stx to etx, including stx
> and etx, are on one line in the file?

Concatinate all your lines into a single output variable first, then
write that to your log

Pseudocode (ie, this won't run), but this should give you the idea
(defining the parts to loop will be the challenge you will have to
define for yourself).

with open("original.log", 'r') as f1:
    with open("new.log", 'a') as f2:
        output = ""
        for line in f1:
            if between [x02] and [x03]:
                output =+ line.strip()
            else:
                f2.write(output)
                output = ""

Basically, you need to loop over everything between your markers and put
them in a single entry, then send that one entry all at once instead of
piecemeal. 

I can come up with something a little more complete if you still need
more help.

-- 
David Rock
david at graniteweb.com

From richkappler at gmail.com  Tue Dec 27 15:39:36 2016
From: richkappler at gmail.com (richard kappler)
Date: Tue, 27 Dec 2016 15:39:36 -0500
Subject: [Tutor] formatting xml (again)
In-Reply-To: <20161227202516.GA18771@apple.graniteweb.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
 <20161227202516.GA18771@apple.graniteweb.com>
Message-ID: <CAG7edPHcgKHs2H2FhnrZtNYLMcnM-n71ijvPMe+3_Eba-CMXfA@mail.gmail.com>

I was actually working somewhat in that direction while I waited. I had in
mind to use something along the lines of:


stx = '\x02'
etx = '\x03'
line1 = ""

with open('original.log', 'r') as f1:
   with open('new.log', 'w') as f2:
        for line in f1:
            if stx in line:
                line1 = line1 + line
            if not stx in line:
                if not etx in line:
                    line1 = line1 + line
            if etx in line:
                line1 = line1 + line + '\n'
                f2.write(line1)
                line1 = ""


but that didn't work. It neither broke each line on etx (multiple events
with stx and etx on one line) nor did it concatenate the multi-line events.

On Tue, Dec 27, 2016 at 3:25 PM, David Rock <david at graniteweb.com> wrote:

> * richard kappler <richkappler at gmail.com> [2016-12-27 14:44]:
> >
> > I have tried to feed this raw into our other app (Splunk) and the app
> reads
> > each line (gedit numbered line) as an event. I want everything in between
> > each stx and etx to be one event.
> >
> > I have tried:
> >
> > #####################################
> > with open("original.log", 'r') as f1:
> >     with open("new.log", 'a') as f2:
> >         for line in f1:
> >             line2 = line.replace("\n", "")
> >             f2.write(line2)
> > ######################################
> >
> > Now this obviously doesn't work because, as stated above, each tag and
> > datum in the example above from lines 2 to 7 is on a different line, so
> > python is doing exactly as I tell it, it's stripping the \n and then
> > printing the line, but not concatenating everything between stx and etx
> on
> > one line, which is what I want it to do.
> >
> > What I'm trying to do is collapse the 'expanded lines' between stx and
> etx
> > to one line, but I just can't wrap my head around how to do it. Or to
> put,
> > and do, it another way, how do I read each line from the original file,
> but
> > write it to another file so that everything from stx to etx, including
> stx
> > and etx, are on one line in the file?
>
> Concatinate all your lines into a single output variable first, then
> write that to your log
>
> Pseudocode (ie, this won't run), but this should give you the idea
> (defining the parts to loop will be the challenge you will have to
> define for yourself).
>
> with open("original.log", 'r') as f1:
>     with open("new.log", 'a') as f2:
>         output = ""
>         for line in f1:
>             if between [x02] and [x03]:
>                 output =+ line.strip()
>             else:
>                 f2.write(output)
>                 output = ""
>
> Basically, you need to loop over everything between your markers and put
> them in a single entry, then send that one entry all at once instead of
> piecemeal.
>
> I can come up with something a little more complete if you still need
> more help.
>
> --
> David Rock
> david at graniteweb.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

From david at graniteweb.com  Tue Dec 27 15:55:36 2016
From: david at graniteweb.com (David Rock)
Date: Tue, 27 Dec 2016 14:55:36 -0600
Subject: [Tutor] formatting xml (again)
In-Reply-To: <CAG7edPHcgKHs2H2FhnrZtNYLMcnM-n71ijvPMe+3_Eba-CMXfA@mail.gmail.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
 <20161227202516.GA18771@apple.graniteweb.com>
 <CAG7edPHcgKHs2H2FhnrZtNYLMcnM-n71ijvPMe+3_Eba-CMXfA@mail.gmail.com>
Message-ID: <20161227205536.GB18771@apple.graniteweb.com>

* richard kappler <richkappler at gmail.com> [2016-12-27 15:39]:
> I was actually working somewhat in that direction while I waited. I had in
> mind to use something along the lines of:
> 
> 
> stx = '\x02'
> etx = '\x03'
> line1 = ""
> 
> with open('original.log', 'r') as f1:
>    with open('new.log', 'w') as f2:
>         for line in f1:
>             if stx in line:
>                 line1 = line1 + line
>             if not stx in line:
>                 if not etx in line:
>                     line1 = line1 + line
>             if etx in line:
>                 line1 = line1 + line + '\n'
>                 f2.write(line1)
>                 line1 = ""
> 
> 
> but that didn't work. It neither broke each line on etx (multiple events
> with stx and etx on one line) nor did it concatenate the multi-line events.

A big part of the challenge sounds like it's inconsistent data
formatting.  You are going to have to identify some way to reliably
check for the beginning/end of your data for it to work.  Do you know if
you will always have \x02 at the start of a section of input, for example?  

The way I usually do log parsing in that case is use the stx as a flag
to start doing other things (ie, if I find stx, stuff lines until I see
the next stx, then dump and continue).  If you have intermediary data
that is not between your stx and etx (comment lines, other data that you
don't want), then it gets a lot harder.

If you don't have at least a marginally consistent input, your only real
option is probably going to be scanning by character and looking for the
\x02 and \x03 to get a glob of data, then parse that glob with some kind
of xml parser, since the data between those two is likely safe-ish.

-- 
David Rock
david at graniteweb.com

From richkappler at gmail.com  Tue Dec 27 16:05:26 2016
From: richkappler at gmail.com (richard kappler)
Date: Tue, 27 Dec 2016 16:05:26 -0500
Subject: [Tutor] formatting xml (again)
In-Reply-To: <20161227205536.GB18771@apple.graniteweb.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
 <20161227202516.GA18771@apple.graniteweb.com>
 <CAG7edPHcgKHs2H2FhnrZtNYLMcnM-n71ijvPMe+3_Eba-CMXfA@mail.gmail.com>
 <20161227205536.GB18771@apple.graniteweb.com>
Message-ID: <CAG7edPE6k1=aHbcum=RPy_d4ADA--MQD6OvT7GQXa+xH83-YGg@mail.gmail.com>

The input is consistent in that it all has stx at the beginning of each
'event.' I'm leaning towards regex. When you say:

" find stx, stuff lines until I see the next stx, then dump and continue"

Might I trouble you for an example of how you do that? I can find stx, I
can find etx using something along the lines of :

a = [m.start() for m in re.finditer(r"<devicename>", line)]

but then I get a little lost, mostly because I have some lines that have
"data data [\x03][\x02] data" and then to the next line. More succinctly,
the stx aren't always at the beginning of the line, etx not always at the
end. No problem, I can find them, but then I'm guessing I would have to
write to a buffer starting with stx, keep writing to the buffer until I get
to etx, write the buffer to file (or send it over the socket, either way is
fine) then continue on. The fact that 'events' span multiple lines is
challenging me.

On Tue, Dec 27, 2016 at 3:55 PM, David Rock <david at graniteweb.com> wrote:

> * richard kappler <richkappler at gmail.com> [2016-12-27 15:39]:
> > I was actually working somewhat in that direction while I waited. I had
> in
> > mind to use something along the lines of:
> >
> >
> > stx = '\x02'
> > etx = '\x03'
> > line1 = ""
> >
> > with open('original.log', 'r') as f1:
> >    with open('new.log', 'w') as f2:
> >         for line in f1:
> >             if stx in line:
> >                 line1 = line1 + line
> >             if not stx in line:
> >                 if not etx in line:
> >                     line1 = line1 + line
> >             if etx in line:
> >                 line1 = line1 + line + '\n'
> >                 f2.write(line1)
> >                 line1 = ""
> >
> >
> > but that didn't work. It neither broke each line on etx (multiple events
> > with stx and etx on one line) nor did it concatenate the multi-line
> events.
>
> A big part of the challenge sounds like it's inconsistent data
> formatting.  You are going to have to identify some way to reliably
> check for the beginning/end of your data for it to work.  Do you know if
> you will always have \x02 at the start of a section of input, for example?
>
> The way I usually do log parsing in that case is use the stx as a flag
> to start doing other things (ie, if I find stx, stuff lines until I see
> the next stx, then dump and continue).  If you have intermediary data
> that is not between your stx and etx (comment lines, other data that you
> don't want), then it gets a lot harder.
>
> If you don't have at least a marginally consistent input, your only real
> option is probably going to be scanning by character and looking for the
> \x02 and \x03 to get a glob of data, then parse that glob with some kind
> of xml parser, since the data between those two is likely safe-ish.
>
> --
> David Rock
> david at graniteweb.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

From david at graniteweb.com  Tue Dec 27 16:47:32 2016
From: david at graniteweb.com (David Rock)
Date: Tue, 27 Dec 2016 15:47:32 -0600
Subject: [Tutor] formatting xml (again)
In-Reply-To: <CAG7edPE6k1=aHbcum=RPy_d4ADA--MQD6OvT7GQXa+xH83-YGg@mail.gmail.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
 <20161227202516.GA18771@apple.graniteweb.com>
 <CAG7edPHcgKHs2H2FhnrZtNYLMcnM-n71ijvPMe+3_Eba-CMXfA@mail.gmail.com>
 <20161227205536.GB18771@apple.graniteweb.com>
 <CAG7edPE6k1=aHbcum=RPy_d4ADA--MQD6OvT7GQXa+xH83-YGg@mail.gmail.com>
Message-ID: <20161227214732.GA30439@apple.graniteweb.com>

* richard kappler <richkappler at gmail.com> [2016-12-27 16:05]:
> The input is consistent in that it all has stx at the beginning of each
> 'event.' I'm leaning towards regex. When you say:
> 
> " find stx, stuff lines until I see the next stx, then dump and continue"
> 
> Might I trouble you for an example of how you do that? I can find stx, I
> can find etx using something along the lines of :
> 
> a = [m.start() for m in re.finditer(r"<devicename>", line)]
> 
> but then I get a little lost, mostly because I have some lines that have
> "data data [\x03][\x02] data" and then to the next line. More succinctly,
> the stx aren't always at the beginning of the line, etx not always at the
> end. No problem, I can find them, but then I'm guessing I would have to
> write to a buffer starting with stx, keep writing to the buffer until I get
> to etx, write the buffer to file (or send it over the socket, either way is
> fine) then continue on. The fact that 'events' span multiple lines is
> challenging me.

Well, that shows that in the context of line-based data, it is not
consistent.  That's the main issue.  If you knew that every event
started on a new line, then you could fairly easily:

if '\x02' in line:
    output = line.strip()
    while '\x02' not in line:
        output = output + line.strip()

etc.

Unfortunately, we don't have that kind of line-based consistency.  You
are either going to have to treat it more like a binary stream of data,
triggering on stx and etx on a character-by-character basis, or you are
going to have to test for both stx and etx on each line and do different
things based on the combination you find.  Some possible options for
a single line appear to be:

[\x02]
[\x02] data
[\x02] data [\x03]
[\x02] data [\x03][\x02]
[\x03]
data [\x03]
data [\x03][\x02]
data [\x03][\x02] data

etc

That's assuming something really ugly like this couldn't happen on a
single line (but somehow I think it probably can):
data [\x03][\x02] data [\x03][\x02]

I think you are stuck reading as a character stream, rather than a
line-based text file due to the unstable nature of the input.

Another possibility (I suppose) would be to read per line and split on
the \x02 yourself (I'm assuming that's actually a single hex character).
That would artificially create "record" data that you could manipulate
and combine partial segments into complete xml records to parse.  Might
be faster, might not, probably would get complicated pretty quickly but
could be an option.

Without seeing actual data, it's tough to speculate what the best approach
would be.

-- 
David Rock
david at graniteweb.com

From alan.gauld at yahoo.co.uk  Tue Dec 27 19:40:56 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Wed, 28 Dec 2016 00:40:56 +0000
Subject: [Tutor] formatting xml (again)
In-Reply-To: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
Message-ID: <o3v1mi$hro$1@blaine.gmane.org>

On 27/12/16 19:44, richard kappler wrote:
> Using python 2.7 - I have a large log file we recorded of streamed xml data
> that I now need to feed into another app for stress testing. The problem is
> the data comes in 2 formats.
> 
> 1. each 'event' is a full set of xml data with opening and closing tags +
> x02 and x03 (stx and etx)
> 
> 2. some events have all the xml data on one 'line' in the log, others are
> in typical nested xml format with lots of white space and multiple 'lines'
> in the log for each event, the first line of th e 'event' starting with an
> stx and the last line of the 'event' ending in an etx.

It sounds as if an xml parser should work for both. After all
xml doesn't care about layout and whitespace etc.

Which xml parser are you using - I assume you are not trying
to parse it manually using regex or string methjods - that's
rarely a good idea for xml.


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



From david at graniteweb.com  Tue Dec 27 19:46:33 2016
From: david at graniteweb.com (David Rock)
Date: Tue, 27 Dec 2016 18:46:33 -0600
Subject: [Tutor] formatting xml (again)
In-Reply-To: <o3v1mi$hro$1@blaine.gmane.org>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
 <o3v1mi$hro$1@blaine.gmane.org>
Message-ID: <20161228004633.GB30439@apple.graniteweb.com>

* Alan Gauld via Tutor <tutor at python.org> [2016-12-28 00:40]:
> On 27/12/16 19:44, richard kappler wrote:
> > Using python 2.7 - I have a large log file we recorded of streamed xml data
> > that I now need to feed into another app for stress testing. The problem is
> > the data comes in 2 formats.
> > 
> > 1. each 'event' is a full set of xml data with opening and closing tags +
> > x02 and x03 (stx and etx)
> > 
> > 2. some events have all the xml data on one 'line' in the log, others are
> > in typical nested xml format with lots of white space and multiple 'lines'
> > in the log for each event, the first line of th e 'event' starting with an
> > stx and the last line of the 'event' ending in an etx.
> 
> It sounds as if an xml parser should work for both. After all
> xml doesn't care about layout and whitespace etc.
> 
> Which xml parser are you using - I assume you are not trying
> to parse it manually using regex or string methjods - that's
> rarely a good idea for xml.

Yeah, since everything appears to be <data>..</data>, the "event" flags
of [\x02] [\x03] may not even matter if you use an actual parser.

-- 
David Rock
david at graniteweb.com

From richkappler at gmail.com  Wed Dec 28 10:10:06 2016
From: richkappler at gmail.com (richard kappler)
Date: Wed, 28 Dec 2016 10:10:06 -0500
Subject: [Tutor] formatting xml (again)
In-Reply-To: <20161228004633.GB30439@apple.graniteweb.com>
References: <CAG7edPE9VmrMy8OMZ_ZRKvqNmda=GeAAyu9Y8z131yCMKXf62w@mail.gmail.com>
 <o3v1mi$hro$1@blaine.gmane.org> <20161228004633.GB30439@apple.graniteweb.com>
Message-ID: <CAG7edPEBo2w1jjxrJcy2ysDToJ+5pnL7ru1ebz3B6sEL38+B_g@mail.gmail.com>

It occurred to me last night on the drive home that I should just run this
through an xml parser, then lo and behold this email was sitting in my
inbox when I got  home. Having tried that, my data is not as clean as I
first thought. It seems like a fairly simple fix, but durned if I can
figure out how to do it. One of the problems is data such as this (viewed
in the text editor, this is a log, not a stream):

1\x02 data data data \x03\x02 more data more data more data \x03\x02 even
more data even
2more data even more data\x03\x02 Mary had a little\x03\x02 lamb whose
fleece was white as
3snow\x03\x02

and so on. The 1,2,3 at the beginning of each above are just line numbers
in the text editor, they do not actually exist.

How do I read in the file, either in it's entirety or line by line, then
output the text with as \x02 the event data \x03 on each line, and when
python sees the \x03 it goes to a new line and continues to output?

On Tue, Dec 27, 2016 at 7:46 PM, David Rock <david at graniteweb.com> wrote:

> * Alan Gauld via Tutor <tutor at python.org> [2016-12-28 00:40]:
> > On 27/12/16 19:44, richard kappler wrote:
> > > Using python 2.7 - I have a large log file we recorded of streamed xml
> data
> > > that I now need to feed into another app for stress testing. The
> problem is
> > > the data comes in 2 formats.
> > >
> > > 1. each 'event' is a full set of xml data with opening and closing
> tags +
> > > x02 and x03 (stx and etx)
> > >
> > > 2. some events have all the xml data on one 'line' in the log, others
> are
> > > in typical nested xml format with lots of white space and multiple
> 'lines'
> > > in the log for each event, the first line of th e 'event' starting
> with an
> > > stx and the last line of the 'event' ending in an etx.
> >
> > It sounds as if an xml parser should work for both. After all
> > xml doesn't care about layout and whitespace etc.
> >
> > Which xml parser are you using - I assume you are not trying
> > to parse it manually using regex or string methjods - that's
> > rarely a good idea for xml.
>
> Yeah, since everything appears to be <data>..</data>, the "event" flags
> of [\x02] [\x03] may not even matter if you use an actual parser.
>
> --
> David Rock
> david at graniteweb.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

From sjeik_appie at hotmail.com  Fri Dec 30 11:46:26 2016
From: sjeik_appie at hotmail.com (Albert-Jan Roskam)
Date: Fri, 30 Dec 2016 16:46:26 +0000
Subject: [Tutor] ReadableInt
Message-ID: <VI1PR1001MB1149CA2AE5A276D9F282DD4E836A0@VI1PR1001MB1149.EURPRD10.PROD.OUTLOOK.COM>

Hi,


Why does the call to str() below return '1' and not 'one'? Should I implement __new__ because int is immutable? I have a datafile that contains values and an associated metadata file with the associated labels. I need to recode the values. Because just using the values is a bit error prone I would like to see the associated labels while debugging. How stupid is the code below on a scale from 1-10? Remember I only intend to use it while debugging the given script.


Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> class ReadableInt(int):
...     def __str__(self):
...         return {1: "one", 2: "two"}.get(self, self)
...
>>> builtins.int = ReadableInt
>>> str(1)  # I expected 'one'
'1'

>>> builtins.int.__str__
<function ReadableInt.__str__ at 0x00ADFB70>



Btw, initially I hoped to be able to do this with the (newish) pandas dtype 'category', but unless

I am overlooking something, but this does not seem possible:


>>> import pandas as pd
>>> pd.Categorical.from_codes([1, 2], ["one", "two"], ordered=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Albert-Jan\AppData\Local\Programs\Python\Python35-32\lib\site-p
ackages\pandas\core\categorical.py", line 471, in from_codes
    raise ValueError("codes need to be between -1 and "
ValueError: codes need to be between -1 and len(categories)-1
>>> pd.Categorical.from_codes([0, 1], ["one", "two"], ordered=True)
[one, two]
Categories (2, object): [one < two]


Happy coding in 2017!


Best wishes,

Albert-Jan

From eryksun at gmail.com  Fri Dec 30 12:31:52 2016
From: eryksun at gmail.com (eryk sun)
Date: Fri, 30 Dec 2016 17:31:52 +0000
Subject: [Tutor] ReadableInt
In-Reply-To: <VI1PR1001MB1149CA2AE5A276D9F282DD4E836A0@VI1PR1001MB1149.EURPRD10.PROD.OUTLOOK.COM>
References: <VI1PR1001MB1149CA2AE5A276D9F282DD4E836A0@VI1PR1001MB1149.EURPRD10.PROD.OUTLOOK.COM>
Message-ID: <CACL+1avUeRUVEjHQkZmFzjhVVGnsuyg25=+sZn0VK2uNLgrj4A@mail.gmail.com>

On Fri, Dec 30, 2016 at 4:46 PM, Albert-Jan Roskam
<sjeik_appie at hotmail.com> wrote:
>
> Remember I only intend to use it while debugging the given script.

Have you tried hooking sys.displayhook?

> Why does the call to str() below return '1' and not 'one'? Should I implement __new__
> because int is immutable?

You can't reliably replace a built-int type with a late binding. The
concrete C API (in this case PyLong_FromLong) is called directly by
the compiler:

    >>> str(1)

    Breakpoint 0 hit
    python35_d!PyAST_FromNodeObject:
    00000000`5ea92ad0 4c894c2420      mov     qword ptr [rsp+20h],r9
                                      ss:00000021`b39ef2d8=
                                      {python35_d!_PyParser_Grammar
                                       (00000000`5ed84ad8)}

    0:000> bp python35_d!PyLong_FromLong
    0:000> g
    Breakpoint 1 hit
    python35_d!PyLong_FromLong:
    00000000`5e95fee0 894c2408        mov     dword ptr [rsp+8],ecx
                                      ss:00000021`b39ee9e0=c19956a8

    0:000> kc 0n15
    Call Site
    python35_d!PyLong_FromLong
    python35_d!parsenumber
    python35_d!ast_for_atom
    python35_d!ast_for_atom_expr
    python35_d!ast_for_power
    python35_d!ast_for_expr
    python35_d!ast_for_call
    python35_d!ast_for_trailer
    python35_d!ast_for_atom_expr
    python35_d!ast_for_power
    python35_d!ast_for_expr
    python35_d!ast_for_testlist
    python35_d!ast_for_expr_stmt
    python35_d!ast_for_stmt
    python35_d!PyAST_FromNodeObject

    0:000> r rcx
    rcx=0000000000000001

    0:000> bd 0-1; g
    '1'

From steve at pearwood.info  Fri Dec 30 19:28:09 2016
From: steve at pearwood.info (Steven D'Aprano)
Date: Sat, 31 Dec 2016 11:28:09 +1100
Subject: [Tutor] ReadableInt
In-Reply-To: <VI1PR1001MB1149CA2AE5A276D9F282DD4E836A0@VI1PR1001MB1149.EURPRD10.PROD.OUTLOOK.COM>
References: <VI1PR1001MB1149CA2AE5A276D9F282DD4E836A0@VI1PR1001MB1149.EURPRD10.PROD.OUTLOOK.COM>
Message-ID: <20161231002808.GD3887@ando.pearwood.info>

On Fri, Dec 30, 2016 at 04:46:26PM +0000, Albert-Jan Roskam wrote:
> Hi,
> 
> 
> Why does the call to str() below return '1' and not 'one'? 

Because the literals 1, 2, 3 etc are hard-coded in the interpreter to 
return objects of type `int`. Shadowing the name in the builtins module 
doesn't change the type of object the literals return.

# Python 2.7

py> import __builtin__
py> class MyInt(int):
...     pass
...
py> __builtin__.int = MyInt
py> type(int("12"))
<class '__main__.MyInt'>
py> type(12)
<type 'int'>


Python 3 is the same, except you would import `builtins` instead of 
`__builtin__`. (Do not use `__builtins__` with an "s".)


So I'm afraid that unfortunately, or perhaps fortunately, there's no way 
to do what you are trying to do. You would have to modify the Python 
interpreter itself.


> Should I implement __new__ because int is immutable?

That won't help.


> I have a datafile that 
> contains values and an associated metadata file with the associated 
> labels. I need to recode the values. Because just using the values is 
> a bit error prone I would like to see the associated labels while 
> debugging.

Can you be a little more specific about what you are doing? Wouldn't you 
be recoding the values of a datafile using an external editor?

 How stupid is the code below on a scale from 1-10? Remember 
> I only intend to use it while debugging the given script.

> Btw, initially I hoped to be able to do this with the (newish) pandas dtype 'category', but unless
> I am overlooking something, but this does not seem possible:
> 
> 
> >>> import pandas as pd
> >>> pd.Categorical.from_codes([1, 2], ["one", "two"], ordered=True)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "C:\Users\Albert-Jan\AppData\Local\Programs\Python\Python35-32\lib\site-p
> ackages\pandas\core\categorical.py", line 471, in from_codes
>     raise ValueError("codes need to be between -1 and "
> ValueError: codes need to be between -1 and len(categories)-1
> >>> pd.Categorical.from_codes([0, 1], ["one", "two"], ordered=True)
> [one, two]
> Categories (2, object): [one < two]

I don't understand what I'm reading there, or what you're attempting to 
accomplish.



-- 
Steve

From cebirim at gmail.com  Fri Dec 30 16:51:46 2016
From: cebirim at gmail.com (angela ebirim)
Date: Fri, 30 Dec 2016 21:51:46 +0000
Subject: [Tutor] Puzzling case of assertRaises not running properly
Message-ID: <CAL_gTQEdKMuLR-9EH0J=PO=Nn=W2ZhWUxgnL7yBC9ckdh54zMg@mail.gmail.com>

Hi,

Learning about how to write tests in Python and have a query.

*#bill.py*
class Bill(object):

   def tax(self, total):
     tax = float(total * 0.05)
     return round(tax, 2)

   def check_input(self, seat1, app1):
     try:
        seat1 = float(seat1)
        app1 = float(app1)
     except ValueError:
         print "Not a float"
     return (seat1, app1)

   def bill(self, seat1, app1):
     (seat1, app1) = self.check_input(seat1, app1)
     tax = self.tax(seat1 + app1)
     total = seat1 + app1 + tax
     return (total, tax)

if __name__=='__main__':
     bill = Bill()
     (total, tax) =  bill.bill(2,3)
     print("tax $%.2f" % tax)
     print("total charge for meal $%.2f" % total)

*#bill_test.py*

class BillTest(unittest.TestCase):
    def setUp(self):
      self.bill = Bill()

    def test_bill_pay_3_4(self):
      self.assertEqual((7.35, 0.35), self.bill.bill(3,4))

    def test_input_not_float_raises_ValueError(self):
      with self.assertRaises(ValueError):
          self.bill.check_input("aa","bb")

if __name__=='__main__':
   unittest.main()

I'm using Python 2.7.

When I run the test doing python bill_test.py -v, I get the following
result:-

test_bill_pay_3_4 (__main__.BillTest) ... ok
test_input_not_float_raises_ValueError (__main__.BillTest) ... Not a float
FAIL

======================================================================
FAIL: test_input_not_float_raises_ValueError (__main__.BillTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "billa_test.py", line 13, in test_input_not_float_raises_ValueError
    self.bill.check_input("aa","bb")
AssertionError: ValueError not raised

----------------------------------------------------------------------
Ran 2 tests in 0.003s

FAILED (failures=1)

Can someone please tell me what I'm doing wrong?

Many thanks

From martin at linux-ip.net  Fri Dec 30 20:32:31 2016
From: martin at linux-ip.net (Martin A. Brown)
Date: Fri, 30 Dec 2016 17:32:31 -0800
Subject: [Tutor] Puzzling case of assertRaises not running properly
In-Reply-To: <CAL_gTQEdKMuLR-9EH0J=PO=Nn=W2ZhWUxgnL7yBC9ckdh54zMg@mail.gmail.com>
References: <CAL_gTQEdKMuLR-9EH0J=PO=Nn=W2ZhWUxgnL7yBC9ckdh54zMg@mail.gmail.com>
Message-ID: <alpine.LSU.2.11.1612301725520.1941@qnttre.jbaqresebt.arg>


Hello,

Overall, I must say.... good work and your question was clear.

>Learning about how to write tests in Python and have a query.

Excellent!

I will snip everything except where I think you should look...

   def check_input(self, seat1, app1):
     try:
        seat1 = float(seat1)
        app1 = float(app1)
     except ValueError:
         print "Not a float"
     return (seat1, app1)

You have a try / except block here and your test is deliberately 
attempting to trigger the ValueError.  That is good.

But, in the except handling, you simply print something and you do 
not re-raise the error.  So, Python thinks you have caught the 
Exception, done what you needed and will continue program flow.

If you wish to propagate the exception (instead of catching it and 
handling it), you could consider something like the following:

   def check_input(self, seat1, app1):
     try:
        seat1 = float(seat1)
        app1 = float(app1)
     except ValueError:
         print "Not a float"
         raise
     return (seat1, app1)

[I will observe that in your __name__ == '__main__' code, you are 
using the print_function, but in your except handling, you are using 
the print statement.  You probably want to pick one.  And, I would 
recommend the print_function--something that seems you already have 
a handle on.]

>if __name__=='__main__':
>     bill = Bill()
>     (total, tax) =  bill.bill(2,3)
>     print("tax $%.2f" % tax)
>     print("total charge for meal $%.2f" % total)


>if __name__=='__main__':
>   unittest.main()
>
>I'm using Python 2.7.
>
>When I run the test doing python bill_test.py -v, I get the following
>result:-
>
>test_bill_pay_3_4 (__main__.BillTest) ... ok
>test_input_not_float_raises_ValueError (__main__.BillTest) ... Not a float
>FAIL

Good luck!

-Martin

-- 
Martin A. Brown
http://linux-ip.net/

From steve at pearwood.info  Sat Dec 31 01:15:19 2016
From: steve at pearwood.info (Steven D'Aprano)
Date: Sat, 31 Dec 2016 17:15:19 +1100
Subject: [Tutor] Puzzling case of assertRaises not running properly
In-Reply-To: <CAL_gTQEdKMuLR-9EH0J=PO=Nn=W2ZhWUxgnL7yBC9ckdh54zMg@mail.gmail.com>
References: <CAL_gTQEdKMuLR-9EH0J=PO=Nn=W2ZhWUxgnL7yBC9ckdh54zMg@mail.gmail.com>
Message-ID: <20161231061519.GE3887@ando.pearwood.info>

On Fri, Dec 30, 2016 at 09:51:46PM +0000, angela ebirim wrote:

> class Bill(object):
> 
>    def tax(self, total):
>      tax = float(total * 0.05)
>      return round(tax, 2)
> 
>    def check_input(self, seat1, app1):
>      try:
>         seat1 = float(seat1)
>         app1 = float(app1)
>      except ValueError:
>          print "Not a float"
>      return (seat1, app1)

Here is your problem. The check_input method doesn't raise ValueError.

*Internally* it gets raised, but then the exception is caught, a 
completely useless message is mysteriously printed ("Not a float" -- 
what's not a float? what is expecting a float?) and the method continues 
as if nothing had happened, only to fail later on with some other error.

Because the ValueError is caught and discarded, there's no ValueError 
for your unit test to catch. Hence the test fails.

(1) The first rule of catching exceptions is: Don't.

(2) The second rule, for experts: If you must, but only if you can 
    recover from the error.

Of course in real life sometimes we do catch exceptions, that's why 
Python has a try...except command so that they can be caught.

But too many people think that the solution to programming bugs is to 
surround everything with try...except, catch the exception, print a 
useless error message (or worse, don't print anything!) and then try to 
continue. It never works, of course, it just makes the code even harder 
to debug. 

As a beginner, before you write a try...except code, you should take a 
quick run around the block followed by a cold shower. If you still think 
you need a try...except block, then *maybe* you can use it.

But as a beginner, you should think that 
exceptions are your best friend in the world. Exceptions show when you 
have made a programming error, or passed the wrong data. How can you fix 
the bug if you don't know where it is? Catching exceptions should be 
done only rarely, when you absolutely need to, because you can recover 
from the error and continue. Never[1] catch an exception just to print a 
bland, boring error message and then continue.



[1] For expert programmers, never say never. Experts know when to break 
the rules.


-- 
Steve

From smileyunder18 at yahoo.com  Sat Dec 31 11:05:46 2016
From: smileyunder18 at yahoo.com (syafiqah amir)
Date: Sat, 31 Dec 2016 16:05:46 +0000 (UTC)
Subject: [Tutor] help :making window
In-Reply-To: <230190206.4631308.1483199734917@mail.yahoo.com>
References: <230190206.4631308.1483199734917.ref@mail.yahoo.com>
 <230190206.4631308.1483199734917@mail.yahoo.com>
Message-ID: <185340330.4591913.1483200346883@mail.yahoo.com>



     On Saturday, December 31, 2016 3:55 PM, syafiqah amir <smileyunder18 at yahoo.com> wrote:
 

 Hello,Im trying to execute the code but the window does not come out.(script from Maya python for games and film)I'm not sure what I did wrong
import maya.cmds as cmds;
class AR_OptionsWIndow(object):? ? def __init__(self):? ? ? ? self.window ='ar_optionsWindow';? ? ? ? self.title='Options Window';? ? ? ? self.size=(546,350);? ? ? ? self.supportsToolAction=False;? ??? ? def create(self):? ? ? ? if cmds.window(self.window,exists=True):? ? ? ? ? ? cmds.deleteUI(self.window,window=True);? ? ? ? self.window = cmds.window(self.window,title=self.title,widthHeight=self.size,menuBar=True);? ? ? ? self.commonMenu();? ? ? ? cmds.showWindow() ? ?
? ? def commonMenu(self):? ? ? ? self.editMenu=cmds.menu(label='Edit');? ? ? ? self.editMenuSave=cmds.menuItem(label='Save Settings');? ? ? ? self.editMenuReset=cmds.menuitem(label='Reset Settings');? ? ? ? self.editMenuDiv=cmds.menuItem(d=True);? ? ? ? self.editMenuRadio=cmds.radioMenuItemCollection();? ? ? ? self.editMenuTool=cmds.menuItem(label='As Tool',radioButton=True,enable=self.supportsToolAction);? ? ? ? self.editMenuAction=cmds.menuItem(label='As Action' , radioButton=True ,enable=self.supportsToolAction);? ? ? ? self.helpMenu=cmds.menu(label='Help');? ? ? ? self.helpMenuItem=cmds.menuItem(label='Help on &s'% self.title)

thank you so much in advanced.

   
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: practice.txt
URL: <http://mail.python.org/pipermail/tutor/attachments/20161231/60259113/attachment.txt>

From joel.goldstick at gmail.com  Sat Dec 31 12:35:15 2016
From: joel.goldstick at gmail.com (Joel Goldstick)
Date: Sat, 31 Dec 2016 12:35:15 -0500
Subject: [Tutor] help :making window
In-Reply-To: <185340330.4591913.1483200346883@mail.yahoo.com>
References: <230190206.4631308.1483199734917.ref@mail.yahoo.com>
 <230190206.4631308.1483199734917@mail.yahoo.com>
 <185340330.4591913.1483200346883@mail.yahoo.com>
Message-ID: <CAPM-O+zDyS6RxQocAL-xoxw+K6C4ett4TX2Jg9GLCp5eHNGx9A@mail.gmail.com>

On Sat, Dec 31, 2016 at 11:05 AM, syafiqah amir via Tutor
<tutor at python.org> wrote:
>
>
>      On Saturday, December 31, 2016 3:55 PM, syafiqah amir <smileyunder18 at yahoo.com> wrote:
>
>
>  Hello,Im trying to execute the code but the window does not come out.(script from Maya python for games and film)I'm not sure what I did wrong
> import maya.cmds as cmds;
> class AR_OptionsWIndow(object):    def __init__(self):        self.window ='ar_optionsWindow';        self.title='Options Window';        self.size=(546,350);        self.supportsToolAction=False;        def create(self):        if cmds.window(self.window,exists=True):            cmds.deleteUI(self.window,window=True);        self.window = cmds.window(self.window,title=self.title,widthHeight=self.size,menuBar=True);        self.commonMenu();        cmds.showWindow()
>     def commonMenu(self):        self.editMenu=cmds.menu(label='Edit');        self.editMenuSave=cmds.menuItem(label='Save Settings');        self.editMenuReset=cmds.menuitem(label='Reset Settings');        self.editMenuDiv=cmds.menuItem(d=True);        self.editMenuRadio=cmds.radioMenuItemCollection();        self.editMenuTool=cmds.menuItem(label='As Tool',radioButton=True,enable=self.supportsToolAction);        self.editMenuAction=cmds.menuItem(label='As Action' , radioButton=True ,enable=self.supportsToolAction);        self.helpMenu=cmds.menu(label='Help');        self.helpMenuItem=cmds.menuItem(label='Help on &s'% self.title)
>
> thank you so much in advanced.

You need to post in plaintext.  Whitespace is vital in python, and
yours has been destroyed.  Also, you seem to be writing in a dialect
that is from some other language.  Semicolon (;) isn't used in python
as a statement separator
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

From akleider at sonic.net  Sat Dec 31 13:06:34 2016
From: akleider at sonic.net (Alex Kleider)
Date: Sat, 31 Dec 2016 10:06:34 -0800
Subject: [Tutor] help :making window
In-Reply-To: <CAPM-O+zDyS6RxQocAL-xoxw+K6C4ett4TX2Jg9GLCp5eHNGx9A@mail.gmail.com>
References: <230190206.4631308.1483199734917.ref@mail.yahoo.com>
 <230190206.4631308.1483199734917@mail.yahoo.com>
 <185340330.4591913.1483200346883@mail.yahoo.com>
 <CAPM-O+zDyS6RxQocAL-xoxw+K6C4ett4TX2Jg9GLCp5eHNGx9A@mail.gmail.com>
Message-ID: <048fb08be18033b1f12f3c0ae6efc2e1@sonic.net>

On 2016-12-31 09:35, Joel Goldstick wrote:

> Semicolon (;) isn't used in python
> as a statement separator

alex at X301n3:/mnt$ python3
Python 3.4.3 (default, Nov 17 2016, 01:11:57)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> gee = "really"; print(gee)
really
>>> 


Considered 'bad style' by many, but it does appear to be part of the 
language.

From alan.gauld at yahoo.co.uk  Sat Dec 31 17:09:20 2016
From: alan.gauld at yahoo.co.uk (Alan Gauld)
Date: Sat, 31 Dec 2016 22:09:20 +0000
Subject: [Tutor] help :making window
In-Reply-To: <185340330.4591913.1483200346883@mail.yahoo.com>
References: <230190206.4631308.1483199734917.ref@mail.yahoo.com>
 <230190206.4631308.1483199734917@mail.yahoo.com>
 <185340330.4591913.1483200346883@mail.yahoo.com>
Message-ID: <o49aaa$5mj$1@blaine.gmane.org>

On 31/12/16 16:05, syafiqah amir via Tutor wrote:

>  Hello,Im trying to execute the code but the window does not come out.

Please always post python code in plain text, otherwise the html gets
wrapped and it becomes illegible.

> (script from Maya python for games and film)

This group is for core python language and library issues,
Maya is a bit off topic (although you may get lucky)
You would probably get a better response on the Maya
support fora:

http://forums.autodesk.com/t5/Maya/ct-p/area-c2

> I'm not sure what I did wrong

Nor me, and because the code is so mangled I can't
guess either...

> import maya.cmds as cmds;
> class AR_OptionsWIndow(object):    def __init__(self):        self.window ='ar_optionsWindow';        self.title='Options Window';        self.size=(546,350);        self.supportsToolAction=False;        def create(self):        if cmds.window(self.window,exists=True):            cmds.deleteUI(self.window,window=True);        self.window = cmds.window(self.window,title=self.title,widthHeight=self.size,menuBar=True);        self.commonMenu();        cmds.showWindow()    
>     def commonMenu(self):        self.editMenu=cmds.menu(label='Edit');        self.editMenuSave=cmds.menuItem(label='Save Settings');        self.editMenuReset=cmds.menuitem(label='Reset Settings');        self.editMenuDiv=cmds.menuItem(d=True);        self.editMenuRadio=cmds.radioMenuItemCollection();        self.editMenuTool=cmds.menuItem(label='As Tool',radioButton=True,enable=self.supportsToolAction);        self.editMenuAction=cmds.menuItem(label='As Action' , radioButton=True ,enable=self.supportsToolAction);        self.helpMenu=cmds.menu(label='Help');        self.helpMenuItem=cmds.menuItem(label='Help on &s'% self.title)
> 

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