Re: [Tutor] selenium, automated testing and python

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 22:09, Alan Gauld via Tutor wrote:

> And the reasons above are only scratching the
> surface, there are many more gains that apply to
> testing in general but are only practical if
> testing is automated.

One thing I meant to add is that "automated" does
not necessarily mean using a framework like
unittest or nose etc.

You can create your own dedicated test harness
and use that. (In fact that's exactly what we
all did for every project before generic test
frameworks were invented around the early 1990s)
For some types of application that can be easier
than trying to force a framework to do your bidding.
It's still automated testing.

But, for most of the time, unittest and its cousins
are a convenient approach that is well understood
and, importantly, supported.

And in python, as a bare minimum, consider doctest.
It is by far the simplest to use, although also the
most limited but it's still a lot better than nothing!

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


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


Re: [Tutor] selenium, automated testing and python

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 19:44, Palm Tree wrote:
> Sorry i'm a bit new to automated testing.
> 
> I have explored python wide and deep but can someone just tell me why as a
> coder i need automated testing?

You don't, you can test it all by hand.
But it will take you much longer, especially after
you've done, say, the 50th update to your program
and had to retest every feature for the 50th time.

Automated testing means you can type a command and
wait while the PC does the work for you, usually
only a matter of a few minutes to an hour - much
less than the several hours it would take you by
hand to do the very same tests.

And the automated tool doesn't forget or use the
wrong data with the wrong test etc. So its
more consistent.

And as you add new features you only need to add
a few new tests and those features get tested
each time too.

When you are starting out it seems like you spend
more time writing test cases than code 9and sometimes
its even true!) but if your project survives for
more than a few iterations its well worth the
investment.

And of course the corollary to that is that if
its a one-off tool that is only for you and
you'll "never use again" manual testing may be
adequate. (But its surprising how many of those
tools do get used again!)

> i have tried some googling but the explanations are a bit crazy.

Really? what kind of crazy?
There are several Youtube videos that cover
the reasons why, as well as the how to do it.
They are mostly short too.

And the reasons above are only scratching the
surface, there are many more gains that apply to
testing in general but are only practical if
testing is automated.

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


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


[Tutor] selenium, automated testing and python

2017-05-04 Thread Palm Tree
Sorry i'm a bit new to automated testing.

I have explored python wide and deep but can someone just tell me why as a
coder i need automated testing?

i have tried some googling but the explanations are a bit crazy.

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


Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Mats Wichmann

> atm_chg.append(float( line.split()[-1] ))
> 
> 
> np.asarray({atm_chg})
> 
> Execution still generates the errors:
> 
> runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py',
> wdir='/home/comp/Apps/Python/Testing')

that means you have a blank line it's reading, the result of splitting
an empty line is an empty list, which you can't index since it has no
members.

$ python
Python 2.7.13 (default, Jan 12 2017, 17:59:37)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ''.split()
[]
>>> ''.split()[-1]
Traceback (most recent call last):
  File "", line 1, in 
IndexError: list index out of range
>>>


you're going to need to do a little input validation, always a good idea
anyway.  that is inside this loop:

  for line in f.readlines():
  atm_chg.append(float( line.split()[-1] ))

check there's something usable in line before you split-and-use it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 13:50, Stephen P. Molnar wrote:
>> I'll assume that's where the problem lies.
>> Try checking if the string is not empty before using it:
>>
>>   for line in f.readlines():
>>   if line:
>>  atm_chg.append(float( line.split()[-1] ))

> I have edited the code:
> 
>  for line in f.readlines():
>  atm_chg.append(float( line.split()[-1] ))

> Execution still generates the errors:

That's because you didn't include the change that
I thought might fix the error. See above.


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


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


Re: [Tutor] Pycharm Edu Lesson 3, Task 7

2017-05-04 Thread David Wolfe
Thanks all for the help.

Pycharm just posted an update to their Pycharm Edu product, and when I
re-ran the script, everything was fine.

Best,
David

On Tue, May 2, 2017 at 4:27 AM, Alan Gauld via Tutor 
wrote:

> On 02/05/17 01:42, David Wolfe wrote:
>
> > I'm working through Pycharm Edu, and I'm stuck on the following:
> >
> > phrase = """
> > It is a really long string
> > triple-quoted strings are used
> > to define multi-line strings
> > """
> > first_half = phrase[:len(phrase)//2]
> > print(first_half)
> >
> >
> > The instructions are:
> > The len() function is used to count how many characters a string
> contains.
> >
> > Get the first half of the string stored in the variable phrase.
> > Note: Remember about type conversion.
>
> I'm assuming this is only part of the story since the instructions don't
> make sense. There is no need for type conversion here.
>
> > and my output is:
> >
> > It is a really long string
> > triple-quoted st
> >
> > which is what I'm supposed to get, but I keep getting the error message
> > "Too short string in the output"
>
> I don't know the PyCharm IDE but is that all it says?
> I'd expect a bit more information about where the error lies.
> It certainly isn't obvious and I don't get any such error when
> I execute your code in IDLE.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Stephen P. Molnar

On 05/03/2017 08:51 PM, Alan Gauld via Tutor wrote:

On 04/05/17 00:32, Stephen P. Molnar wrote:


import numpy as np

name = input("Enter Molecule ID: ")
name = str(name)


You don't need the str(), input always returns a string.


name_in =name[:]+'.lac.dat'


And you don't need the [:]. Just use

name_in = name + '.lac.dat'


print(name_in)

atm_chg = []



"""
atm_chg = open(name_in,'r')
for line in atm_chg:
  print(line, end=' ')
"""

This creates a 3 line string which is not assigned to any object.
It is not executable code and will not be executed. Maybe you
are doing it as a way of commenting out a block? If so it would be
better in a post to just delete it, it just adds confusion
otherwise. (Well, it confused me! :-)


with open(name_in) as f:
  # skip two lines
  f.readline()
  f.readline()
  for line in f.readlines():
  atm_chg.append(float( line.split()[-1] ))

p.asarray({atm_chg})


Where did p come from?
Should it be np? asarray() sounds like it might be a numpy thing.
And I'm not sure what the {atm_chg} is supposed to do - create
a single element set using your list maybe? I get an "unhashable"
error if I try it at the  prompt.


When it is run I get:

IndexError: list index out of range


I'm pretty sure you get more than that, please post the full
error text, it's much harder to diagnose problems with just
the summary.

Since the only indexing you do is in this line


  atm_chg.append(float( line.split()[-1] ))


I'll assume that's where the problem lies.
Try checking if the string is not empty before using it:

  for line in f.readlines():
  if line:
 atm_chg.append(float( line.split()[-1] ))



However, the Variable Explorer shows:


I have no idea what the Variable Explorer is?
Is it part of your IDE? Or of numpy?
If the IDE which IDE are you using?


   [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]

One line of which is exactly what I want as input to the next step in
the larger calculation.

Now, my question is how do I extract just one line of this file?


Any particular line? And which file are you talking about?
The data should be in the list variable, atm_chg.
In which case the first line is therefore:  atm_chg[0]

Or you can process each line using the usual for loop:

for line in atm_chg:
 # use line here


Thanks for your reply.

The Variable Explorer is part of the Spyder IDE.

I have edited the code:

import numpy as np


name = input("Enter Molecule ID: ")
#name = str(name)

name_in =name[:]+'.lac.dat'
print(name_in)

atm_chg = []

with open(name_in) as f:
# skip two lines
f.readline()
f.readline()
for line in f.readlines():
atm_chg.append(float( line.split()[-1] ))


np.asarray({atm_chg})

Execution still generates the errors:

runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


Enter Molecule ID: A
A.lac.dat
Traceback (most recent call last):

  File "", line 1, in 
runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 866, in runfile

execfile(filename, namespace)

  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/comp/Apps/Python/Testing/ReadFile_2.py", line 27, in 
atm_chg.append(float( line.split()[-1] ))

IndexError: list index out of range

from the input file:

runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


Enter Molecule ID: A
A.lac.dat
Traceback (most recent call last):

  File "", line 1, in 
runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', 
wdir='/home/comp/Apps/Python/Testing')


  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 866, in runfile

execfile(filename, namespace)

  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/comp/Apps/Python/Testing/ReadFile_2.py", line 27, in 
atm_chg.append(float( line.split()[-1] ))

IndexError: list index out of range

Finally, here is the code for which I need input data for the 
calculation of the Integrated Charge Transform:


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr  8 15:17:07 2017

@author: comp

Copyright (c) 2017 Stephen P. Molnar, Ph.D.

"""

import matplotlib.pyplot as plt
import numpy as np
# you don't need pandas, np.genfromtxt() reads this type of txt files.

plt.ion() # interactive plotting, stopps plt.show() 

Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 13:21, Stephen P. Molnar wrote:

> Here are the error messages:
> 
> Enter Molecule ID: A
> A.lac.dat
> Traceback (most recent call last):
> 
>File "", line 1, in 
>  runfile('/home/comp/Apps/Python/Testing/ReadFile_1.py', 
> wdir='/home/comp/Apps/Python/Testing')

There appears to be a missing opening paren? Or a surplus closing one?
Either way this line is not in the code you sent us. It may be coming
from your IDE - which seems to be spyder.

Can you run the code from a command prompt so we see the unpolluted
Python output. IDEs are useful but they sometimes add in extra layers
that obfuscate things.

> "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py",
>  
> line 102, in execfile
>  exec(compile(f.read(), filename, 'exec'), namespace)
> 
>File "/home/comp/Apps/Python/Testing/ReadFile_1.py", line 32, in 
>  atm_chg.append(float( line.split()[-1] ))
> 
> IndexError: list index out of range

But it does look like it's this line that causes the error
so I'd try the check for emptiness as described in my previous post.


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


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


Re: [Tutor] Index Out of Range?List

2017-05-04 Thread Stephen P. Molnar

On 05/03/2017 08:32 PM, Steven D'Aprano wrote:

On Wed, May 03, 2017 at 07:32:53PM -0400, Stephen P. Molnar wrote:

[...]

When it is run I get:

IndexError: list index out of range


That alone is useless to us. Please post the full traceback, starting
from the line

 Traceback (most recent call last):

Among other things, it will show us the line of code that causes the
error. Otherwise, we're just guessing.

You might also like to consider a simple debugging technique: print
the index and the length of the list just before trying to use them.
E.g. if you want to extract the nth item of a list, write:

 print(n, len(a_list))
 value = a_list[n]


(And yes, I completely agree that the error message should show that
information, but it currently doesn't.)



Thanks for the reply.

Here are the error messages:

Enter Molecule ID: A
A.lac.dat
Traceback (most recent call last):

  File "", line 1, in 
runfile('/home/comp/Apps/Python/Testing/ReadFile_1.py', 
wdir='/home/comp/Apps/Python/Testing')


  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 866, in runfile

execfile(filename, namespace)

  File 
"/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", 
line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/comp/Apps/Python/Testing/ReadFile_1.py", line 32, in 
atm_chg.append(float( line.split()[-1] ))

IndexError: list index out of range

--
Stephen P. Molnar, Ph.D.Life is a fuzzy set
www.molecular-modeling.net  Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Bind DNS Zone config

2017-05-04 Thread Alan Gauld via Tutor
On 04/05/17 04:11, Nathan D'Elboux wrote:

> when i run the above code it prompts me to enter the domain so i do,
> then the out put is a "Value error: Unexpected '{' in field name

Always, always, always post the full error message.
Do not shorten it to the summary line. We can't
see the location information so er have to guess
and that's never reliable.

> So i comment out the .format statement and i get the exact output i
> want but nothing in the {} statement

The problem could be avoided by not having the format markers
in the string. This is easily done by using traditional
formatting using % characters instead of the {} style.

print("""zone {%s} IN {
type master;
file "zones/1.2.3.4.zone";
allow-transfer { none; };
allow-query { test-test; };
};""" % new_domain)

I'm sure there is a way to do it using format() but
it probably involves a lot of escape characters etc.
I'd take the easy route and use printf style % markers.

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


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


Re: [Tutor] Python Bind DNS Zone config

2017-05-04 Thread Nathan D'Elboux
Thanks so much for your response. Ok so i now have the following code

new_domain = input("Enter domain you wish to blacklist: ")

print("""zone {} IN {
type master;
file "zones/1.2.3.4.zone";
allow-transfer { none; };
allow-query { test-test; };
};""".format(new_domain))

when i run the above code it prompts me to enter the domain so i do,
then the out put is a "Value error: Unexpected '{' in field name

So i comment out the .format statement and i get the exact output i
want but nothing in the {} statement

I need to either escape the opening { after the IN statement as the
.format(new_domain)) statement doesnt seem to be detecting i have the
{} ready to take the parameter new_domain?

Thanks for the link it certainly helped



On Wed, May 3, 2017 at 10:24 PM, boB Stepp  wrote:
> Hello Nathan!
>
> On Wed, May 3, 2017 at 4:40 AM, Nathan D'Elboux
>  wrote:
>
>>
>> What i am trying to do is have the below syntax somehow stored into a block
>> of strings exactly in this syntax. I dont know what data type i should be
>> trying to store this as, a string obviously but how would i preserve the
>> syntax so it outputs in the identical way? The
>>
>> zone "." {
>> 6 type master;
>> 7 //type hint;
>> 8 file "/etc/bind/db.root.honeypot";
>> 9
>>
>>
>>
>>
>> };
>
> Do you know about triple quotes in Python?  They can be used when you
> wish to have a multi-line string.  You can use either single quotes:
>
> py3: my_string = '''Line one...
> ... Line two...
> ... Line three!'''
> py3: print(my_string)
> Line one...
> Line two...
> Line three!
>
> Or you could use three double quotes:
>
> py3: my_string = """Line one...
> ... Line two...
> ... Line three!"""
> py3: print(my_string)
> Line one...
> Line two...
> Line three!
>
>> In between the "." I will be asking a user for input to insert a domain in
>> plain ASCII and place it in that location of the zone file config and print
>> it to STD OUT on the terminal. Perhaps later another version of this i will
>> include writing directly to a file but for now i want to ensure the
>> inserting of string into specific position works and just print it out.
>>
>> So im thinking of something along the lines of
>>
>> User_input_Zone = str(input("Enter the domain you wish to block: ")
>
> Note:  The str() you use with input() is unnecessary.  input() always
> returns a string; no need to convert it to a string.  On the other
> hand if the input was an integer or a float, then you would need to
> use int() or float(), respectively.
>
>> Def zone_blacklist
>> New_zone = "zone "{}" {, user_input_zone
>> 6  type master;
>> 7  //type hint;
>> 8  file "/etc/bind/db.root.honeypot";
>> 9 };"
>> Print(new_zone)
>>
>> What i was thinking of doing is having a place holder of {} in place of
>> where i want the domain to be inserted into but i dont know how to
>> structure the syntax of a zone file in a function within
>
> Have you looked into string formatting?  See:
>
> https://docs.python.org/3/tutorial/inputoutput.html
>
> in the Python Tutorial.  What you appear to be interested in falls a
> bit farther down the page.  And you can always google Python 3 string
> formatting.
>
> HTH!
>
> --
> boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor