Re: [Tutor] Preparing virtualenvwrapper for Django 1.6 development

2014-08-24 Thread Chris “Kwpolska” Warrick
On Sat, Aug 23, 2014 at 9:37 PM, Alan Gauld  wrote:
> While I wouldn't say its the best environment I don't think its
> fair to say Windows is "a bad environment for Python". Any
> experienced Windows programmer will find it much easier to use Python on
> Windows than to learn a whole new OS and development environment.

There are various pitfalls that happen on Windows and that don’t on
Linux.  Some packages outright assume Windows does not exist, and this
leads to failing code.  In other cases, you must compile a C
extension, which is not easy to configure — and not ever package has
wheels/.exes available…  Linux tries to follow POSIX standards, and
also acknowledges people may want to be programmers.

Also, OP did not state whether they are experienced Windows
programmers, experienced platform-independent programmers or newcomers
to programming in general.

> [ And for context, I run Linux on my main desktop, MacOS on my
> laptop, and Windows 8.1 on a second desktop - used mainly for
> photography/video processing. And I use Python on all 3 of them)

context: I have a dual-boot Arch Linux/Windows 7 system.  Python
installed on both OSes (and even in some Windows VMs).

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] NameError

2014-08-24 Thread Mimi Ou Yang


name = input("Enter your name: ")


age = int(input("Enter your age: "))


gb = input("Are you a boy or a girl? ")


op = input("How are you feeling today? ")


if (age in (1,2,3,4,5,6,7,8,9,10,11,12)):
print (name,"you are a little",gb,"that is feeling",op,"today.")


if (age in (13,14,15,16,17)):
print (name,"you are a teenage",gb,"that is feeling",op,"today.")


if (age in (18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)) and (gb == boy):
print (name,"you are a young man that is feeling",op,"today.")

if (age in (18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)) and (gb == girl):
print (name,"you are a young woman that is feeling",op,"today.")


if (age in 
(34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56)) and (gb 
== boy):
print (name,"you are a man that is feeling",op,"today.")


if (age in 
(34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56)) and (gb 
== girl):
print (name,"you are a woman that is feeling",op,"today.") 


if (age >= 56) and (gb == boy):
print (name,"you are a grandpa that is feeling",op,"today.")


if (age >= 56) and (gb == girl):
print (name,"you are a grandma that is feeling",op,"today.")







when I run it it says: 


Enter your name: jimmy
Enter your age: 45
Are you a boy or a girl? boy
How are you feeling today? good
Traceback (most recent call last):
  File "C:\Users\Jimmy\Desktop\Python\3.4.1 Projects\TEST1.py", line 21, in 

if (age in 
(34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56)) and (gb 
== boy):
NameError: name 'boy' is not defined
>>> 


can you tell me what should I do___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] NameError

2014-08-24 Thread Alan Gauld

On 24/08/14 15:11, Mimi Ou Yang wrote:


age = int(input("Enter your age: "))
gb = input("Are you a boy or a girl? ")


input() returns a string so the values here
should be 'boy' or 'girl' - Notice the quote signs.


op = input("How are you feeling today? ")

if (age in (1,2,3,4,5,6,7,8,9,10,11,12)):
 print (name,"you are a little",gb,"that is feeling",op,"today.")


If you are using loing sequences of values you should consider using the 
range() function instead or use a comparison check:


either

if age in (range(1,13):# last value not included

or

if 1 <= age <= 12:

Its less typing and easier to see the intention.
For small numbers of values the tuple approach is
fine but once you get more than say, 7 values its
harder to read.

The exception is if you are testing for non
contiguous values, in that case an explicit tuple
will be needed.


if (age in (18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)) and (gb ==
boy):


Notice that you are testing

 and (gb == boy):

no quotes so Python looks for a variable called boy.



if (age in (18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)) and (gb ==
girl):


The same here for girl



(34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56))
and (gb == boy):
NameError: name 'boy' is not defined
 >>>


So use quotes when testing for string values

you might also like to force the input to lowercase so that users can 
type Boy or BOY if they wish:


 (gb.lower() == 'boy')

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Shorter way for a little program

2014-08-24 Thread Emile van Sebille

On 8/23/2014 7:16 AM, Mimi Ou Yang wrote:

age = input("K")

age = int(age)

if (age == 1) or (age == 2) or (age  == 3) or (age == 4):
 print ("LOL")

else:
 print ("K")



Is there a shorter way to do this program???


print ('LOL','K')[int(input("k"))>4]


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


Re: [Tutor] NameError

2014-08-24 Thread ALAN GAULD
forwarding to the group.
Please use ReplyAll when responding to the list.

 
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos



>
> From: D.V.N.Sarma డి.వి.ఎన్.శర్మ 
>To: Alan Gauld  
>Sent: Sunday, 24 August 2014, 23:54
>Subject: Re: [Tutor] NameError
> 
>
>
>In the six places you have gb == boy or gb == girl, put 'boy' and 'girl'.
>I did and it works.
>
>
>regards,
>Sarma.
>
>
>On Mon, Aug 25, 2014 at 12:09 AM, Alan Gauld  wrote:
>
>On 24/08/14 15:11, Mimi Ou Yang wrote:
>>
>>
>>age = int(input("Enter your age: "))
>>>gb = input("Are you a boy or a girl? ")
>>>
>>
input() returns a string so the values here
>>should be 'boy' or 'girl' - Notice the quote signs.
>>
>>
>>
>>op = input("How are you feeling today? ")
>>>
>>>if (age in (1,2,3,4,5,6,7,8,9,10,11,12)):
>>>     print (name,"you are a little",gb,"that is feeling",op,"today.")
>>>
>>
If you are using loing sequences of values you should consider using the 
range() function instead or use a comparison check:
>>
>>either
>>
>>if age in (range(1,13):    # last value not included
>>
>>or
>>
>>if 1 <= age <= 12:
>>
>>Its less typing and easier to see the intention.
>>For small numbers of values the tuple approach is
>>fine but once you get more than say, 7 values its
>>harder to read.
>>
>>The exception is if you are testing for non
>>contiguous values, in that case an explicit tuple
>>will be needed.
>>
>>
>>
>>if (age in (18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)) and (gb ==
>>>boy):
>>>
>>
Notice that you are testing
>>
>> and (gb == boy):
>>
>>no quotes so Python looks for a variable called boy.
>>
>>
>>
>>
>>if (age in (18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)) and (gb ==
>>>girl):
>>>
>>
The same here for girl
>>
>>
>>
>>
>>(34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56))
>>>and (gb == boy):
>>>NameError: name 'boy' is not defined
>>> >>>
>>>
>>
So use quotes when testing for string values
>>
>>you might also like to force the input to lowercase so that users can type 
>>Boy or BOY if they wish:
>>
>> (gb.lower() == 'boy')
>>
>>HTH
>>-- 
>>Alan G
>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/
>>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


[Tutor] Using unittest module for Test Driven Development

2014-08-24 Thread Alex Kleider

Given
$ cat compare.py
#!/usr/bin/env python3
import os
import sys

def get_args():
try:
args = sys.argv[1:]
except IndexError:
return
return [arg for arg in args if os.path.isfile(arg)]

if __name__ == "__main__":
print(get_args())

How can one unittest get_args()?
It seems to me that 'unittest'ing only works to test functions and 
methods,

not programs.

Here's my attempt at a testing module:
$ cat testing.py
#!/usr/bin/env python3
import unittest
import compare_sizes

class CompareTesting(unittest.TestCase):

def setUp(self):
pass

def test_nothing(self):
self.assertEqual(not False, True)

def test_get_file_param(self):
self.assertTrue(compare_sizes.get_args() == ['file1', 'file2'])

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

Is there a way that I can provide the file name command line parameters
to compare.py so that its get_args function can be tested?

Thanks in advance for any advice.
AlexK

ps I'm on Ubuntu14.4, using Python3.4.0
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using unittest module for Test Driven Development

2014-08-24 Thread Steven D'Aprano
On Sun, Aug 24, 2014 at 05:23:35PM -0700, Alex Kleider wrote:
> Given
> $ cat compare.py
> #!/usr/bin/env python3
> import os
> import sys
> 
> def get_args():
> try:
> args = sys.argv[1:]
> except IndexError:
> return
> return [arg for arg in args if os.path.isfile(arg)]

The try...except block is redundant, because you are taking a slice, not 
an index, it cannot fail. If the bounds are outside of the actual list, 
the empty list is returned:

py> [1, 2][100:1000]
[]


By the way, is there significance to the name "compare.py"? Because it's 
not really doing any comparisons...


> if __name__ == "__main__":
> print(get_args())
> 
> How can one unittest get_args()?
> It seems to me that 'unittest'ing only works to test functions and 
> methods, not programs.


Well, technically "unit testing" refers to testing individual "units" 
(that is, classes, functions, modules etc.) of a program, not the entire 
program all at once. But it's also flexible enough to test the entire 
program.

To test get_args() alone, I would do something like this:


import sys
import unittest
import compare

class Test_Get_Args(unittest.TestCase):
def setUp(self):
# Create some known files.
open('/tmp/aaa', 'w').close()
open('/tmp/bbb', 'w').close()
# Make sure another file doesn't exist.
if os.path.exists('/tmp/ccc'):
os.unlink('/tmp/ccc')

def tearDown(self):
# Clean up once we're done.
for file in ('/tmp/aaa', '/tmp/bbb'):
if os.path.exists(file):
os.unlink(file)

def test_existing_files(self):
sys.argv = ['testing', '/tmp/aaa', '/tmp/bbb']
result = compare.get_args()
self.assertEqual(result, ['/tmp/aaa', '/tmp/bbb'])

def test_nonexisting_files(self):
sys.argv = ['testing', '/tmp/ccc']
result = compare.get_args()
self.assertEqual(result, [])

def test_not_files(self):
sys.argv = ['testing', '/tmp', '/']
result = compare.get_args()
self.assertEqual(result, [])


The paths I used are suitable for Unix, Linux or Mac. You will need to 
adapt them for Windows.

To avoid having to write to sys.argv, give your get_args function an 
optional argument:

def get_args(args=None):
if args is None:
args = sys.argv[1:]
return [arg for arg in args if os.path.isfile(arg)]

then in your tests, just pass the list directly to the function.



> Here's my attempt at a testing module:
> $ cat testing.py
> #!/usr/bin/env python3
> import unittest
> import compare_sizes

What's compare_sizes ?


> class CompareTesting(unittest.TestCase):
> 
> def setUp(self):
> pass

If you're not doing anything in setUp, just don't override the method. 
The default method does nothing, so you don't need this.


> def test_nothing(self):
> self.assertEqual(not False, True)

What is the purpose of this test? How is it testing *your* code? That 
would be necessary as a test of Python's built-ins.


> def test_get_file_param(self):
> self.assertTrue(compare_sizes.get_args() == ['file1', 'file2'])

You should use assertEqual(a, b) rather than assertTrue(a == b).

> if __name__ == '__main__':
> unittest.main()
> 
> Is there a way that I can provide the file name command line parameters
> to compare.py so that its get_args function can be tested?

sys.argv is writeable, or better still, provide get_args() an optional 
argument to use instead of sys.argv.


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


Re: [Tutor] Using unittest module for Test Driven Development

2014-08-24 Thread Alex Kleider


Thank you very much, Steven.  This is just the help I needed.
Forgive me for causing confusion- I pared down my code for presentation
but not enough and also left the names which now out of context, don't
make sense.  In line explanations provided below although it would 
probably

be of little interest.  You did ask so I feel I owe you an explanation.
Gratefully,
Alex

On 2014-08-24 18:57, Steven D'Aprano wrote:

On Sun, Aug 24, 2014 at 05:23:35PM -0700, Alex Kleider wrote:

Given
$ cat compare.py
#!/usr/bin/env python3
import os
import sys

def get_args():
try:
args = sys.argv[1:]
except IndexError:
return
return [arg for arg in args if os.path.isfile(arg)]


The try...except block is redundant, because you are taking a slice, 
not

an index, it cannot fail. If the bounds are outside of the actual list,
the empty list is returned:

py> [1, 2][100:1000]
[]


This I did not know.  Good to know.





By the way, is there significance to the name "compare.py"? Because 
it's

not really doing any comparisons...


That's the ultimate goal of the script but I've a ways to go before that 
is evident!






if __name__ == "__main__":
print(get_args())

How can one unittest get_args()?
It seems to me that 'unittest'ing only works to test functions and
methods, not programs.



Well, technically "unit testing" refers to testing individual "units"
(that is, classes, functions, modules etc.) of a program, not the 
entire

program all at once. But it's also flexible enough to test the entire
program.


That's good to hear.



To test get_args() alone, I would do something like this:


import sys
import unittest
import compare

class Test_Get_Args(unittest.TestCase):
def setUp(self):
# Create some known files.
open('/tmp/aaa', 'w').close()
open('/tmp/bbb', 'w').close()
# Make sure another file doesn't exist.
if os.path.exists('/tmp/ccc'):
os.unlink('/tmp/ccc')

def tearDown(self):
# Clean up once we're done.
for file in ('/tmp/aaa', '/tmp/bbb'):
if os.path.exists(file):
os.unlink(file)

def test_existing_files(self):
sys.argv = ['testing', '/tmp/aaa', '/tmp/bbb']
result = compare.get_args()
self.assertEqual(result, ['/tmp/aaa', '/tmp/bbb'])

def test_nonexisting_files(self):
sys.argv = ['testing', '/tmp/ccc']
result = compare.get_args()
self.assertEqual(result, [])

def test_not_files(self):
sys.argv = ['testing', '/tmp', '/']
result = compare.get_args()
self.assertEqual(result, [])


The paths I used are suitable for Unix, Linux or Mac. You will need to
adapt them for Windows.


M$ Windows is of little interest to me:-)




To avoid having to write to sys.argv, give your get_args function an
optional argument:

def get_args(args=None):
if args is None:
args = sys.argv[1:]
return [arg for arg in args if os.path.isfile(arg)]

then in your tests, just pass the list directly to the function.




Here's my attempt at a testing module:
$ cat testing.py
#!/usr/bin/env python3
import unittest
import compare_sizes


What's compare_sizes ?


OOPS!  In editing for presentation to the list I forgot to change this
instance of compare_sizes to compare.  The ultimate goal is to compare
file sizes and so I am trying to do TDD of compare_sizes.py which I 
forgot

to rename to 'compare' in the import statement.





class CompareTesting(unittest.TestCase):

def setUp(self):
pass


If you're not doing anything in setUp, just don't override the method.
The default method does nothing, so you don't need this.


I stuck it in as a place holder expecting to soon need it.
Should have removed it for presentation.





def test_nothing(self):
self.assertEqual(not False, True)


What is the purpose of this test? How is it testing *your* code? That
would be necessary as a test of Python's built-ins.

Since I'm learning I wanted to start with a test that I 'knew' shouldn't 
fail.

I should have deleted it for presentation- sorry.




def test_get_file_param(self):
self.assertTrue(compare_sizes.get_args() == ['file1', 
'file2'])


You should use assertEqual(a, b) rather than assertTrue(a == b).


Thanks for this tip. I remember reading about that.


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

Is there a way that I can provide the file name command line 
parameters

to compare.py so that its get_args function can be tested?


sys.argv is writeable, or better still, provide get_args() an optional
argument to use instead of sys.argv.o


I don't understand what you mean by "sys.argv is writeable".

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


Re: [Tutor] Using unittest module for Test Driven Development

2014-08-24 Thread Danny Yoo
>>> Is there a way that I can provide the file name command line parameters
>>> to compare.py so that its get_args function can be tested?
>>
>> sys.argv is writeable, or better still, provide get_args() an optional
>> argument to use instead of sys.argv.o
>
>
> I don't understand what you mean by "sys.argv is writeable".


Hi Alex,

I think Steven's suggestion to have get_args() take in an explicit
args argument makes the most sense: it allows the function to be
testable.  If the behavior of the "function" depends on something
outside of the function, making that thing an explicit parameter
allows you to capture it.  It makes the free variable something under
your control.


What "sys.argv is writeable" means is that you can take a sledgehammer
approach: you can save the old value of sys.argv somewhere in another
temporary variable, assign sys.argv to the test value, do your tests,
and then assign the original value back to sys.argv.  But this
approach is very fragile: anything else that depends on sys.argv can
suddenly stop working.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor