Re: [Tutor] recursion

2016-02-06 Thread Danny Yoo
On Thu, Feb 4, 2016 at 6:03 PM, noopy via Tutor  wrote:
> Hi,
>
> I just cannot get my head around the following code, maybe someone could
> explain it to me.

One thing to note: the function here is a generator, which is itself
an intermediate subject that's specific to Python.  Recursion is a
more general concept that's shared by a lot of other programming
languages.  Tackling generators at the same time as recursion may make
both subjects harder to learn at first.



You might want to consider a version of permutations that doesn't do
any generator-related stuff.  Here's a version of permutations that's
a regular function:

##
def permutations(items):
n = len(items)
if n == 0:
return [[]]
else:
allPerms = []
for i in range(len(items)):
for cc in permutations(items[:i] + items[i+1:]):
allPerms.append([items[i]] + cc)
return allPerms
##

This version should be a bit easier to understand, since we don't have
to deal with generators at the same time.


Also, we often have the choice of defining what the function should do
for "small" base cases.  Zero isn't always the most comprehensible
case to start with.  If it helps to make the function simpler to
understand, start with a list of length 1 or 2.  You don't have to
make the function's basis something that doesn't make sense to you.

What should the answer to permutations(['x']) be, for example?

I think it should be:

[['x']]

Do you agree?  Did you have something else in mind?


Another question:  What should the answer to permutations(['x', 'y']) be?


It's very easy to write code that's busy or using CPU cycles.  It's
harder to tell whether it's doing something right.  To solve that
problem, we should build up a repertoire of known good solutions:
they'll make great test cases.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] recursion

2016-02-06 Thread Danny Yoo
On Feb 5, 2016 12:07 AM, "noopy via Tutor"  wrote:
>
> Hi,
>
> I just cannot get my head around the following code, maybe someone could
explain it to me.
>
> def permutations(items):

When trying to understand a function (or in this case, a generator),
knowing the types of input and output can be helpful.  Also, knowing some
sample outputs for small inputs is also important.

Without even looking at the implementation, can you say in words what this
function is intended to do? What's the type of the input?  What's the type
of the output? And can you give a few concrete examples of what you'd like
it to do on "small" inputs?

(Trying to demonstrate a general problem strategy that works surprisingly
well, and that we actually use in practice.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-06 Thread boB Stepp
On Sat, Feb 6, 2016 at 3:54 PM, boB Stepp  wrote:
> On Sat, Feb 6, 2016 at 10:07 AM, Anubhav Yadav  wrote:

Just read Danny's reply after sending mine, which means that the
answer to my question:

> If my understanding is indeed correct, then I will leave it to you to
> figure out which logic operator ("and" or "or") makes sense here!
> ~(:>))

must be neither one!  Thanks, Danny!

However, I do believe (Until proven otherwise.) my first point is still valid.

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


Re: [Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-06 Thread boB Stepp
On Sat, Feb 6, 2016 at 10:07 AM, Anubhav Yadav  wrote:


> class TestCase(unittest.TestCase):
> def test_red_temperature_simulation(self):
> """
> Method to test the red_temperature_simulation method
> """
> for i in range(10):
> self.assertLess(red_temperature_simulation(), 100.0) and \
> self.assertGreater(red_temperature_simulation(), 103.0)

Is this really what you want (And similarly in your other test
method.)?  If I am reading things correctly, you are calling
red_temperature_simulation *twice*, which will *usually* give you two
*separate* values, which you then attempt to compare.  Shouldn't you
first do:

for i in range(10):
value_to_test = red_temperature_simulation()
(self.assertLess(value_to_test, 100.0) 
self.assertGreater(value_to_test, 103.0))
?

If my understanding is indeed correct, then I will leave it to you to
figure out which logic operator ("and" or "or") makes sense here!
~(:>))



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


Re: [Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-06 Thread Danny Yoo
On Feb 6, 2016 12:31 PM, "Anubhav Yadav"  wrote:
>
> Hello Everyone,
>
> I am trying to write a simple program that generated random numbers based
> on some rules.
> when my program is in the yellow state, it should generate the numbers
> from 100.0-100.5
> and 102.5-103.0. When my program is in the red state, it should generate
> numbers in the range
> less than 99.9 and greater than 103.1.
>
> I have written two very simple functions, and two unittest.TestCase test
> cases to test the two functions. I somehow feel that both my test cases
> doesn't make any sense, yet one of it passes perfectly and other one does
> not pass.

Hi Anubhav,

Ah!  The assert functions are meant to be used as statements, not as
composable expressions.  If you're familiar with the idea of side effects,
then you need to understand that you should be calling the assert functions
just for their side effects, not for their return value.

If you want to test two conditions, do them as  separate statements.  By
trying to compose them with 'and', the code actually means something
different due to boolean logic short circuiting.

If you have questions, please feel free to ask.  Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-06 Thread Anubhav Yadav
Hello Everyone,

I am trying to write a simple program that generated random numbers based
on some rules.
when my program is in the yellow state, it should generate the numbers
from 100.0-100.5
and 102.5-103.0. When my program is in the red state, it should generate
numbers in the range
less than 99.9 and greater than 103.1.

I have written two very simple functions, and two unittest.TestCase test
cases to test the two functions. I somehow feel that both my test cases
doesn't make any sense, yet one of it passes perfectly and other one does
not pass.

Here is my code:

import random
import unittest

red_temperature_min = 99.9
red_temperature_max = 103.0
normal_temperature_min = 101.0
normal_temperature_max = 102.0
yellow_temperature_min = 100.0
yellow_temperature_max = 103.0

def red_temperature_simulation():
"""
Method to simulate the red temperature condition
"""
choice = random.randint(0,1)
if choice:
return round(random.uniform(red_temperature_min - 5.0,
red_temperature_min))
else:
return round(random.uniform(red_temperature_max,
red_temperature_max + 5.0))

def yellow_temperature_simulation():
"""
Method to simulate the yellow temperature condition
"""
choice = random.randint(0,1)
if choice:
return round(random.uniform(yellow_temperature_min,
normal_temperature_min - 0.5), 2)
else:
return round(random.uniform(normal_temperature_max + 0.5,
yellow_temperature_max), 2)


class TestCase(unittest.TestCase):
def test_red_temperature_simulation(self):
"""
Method to test the red_temperature_simulation method
"""
for i in range(10):
self.assertLess(red_temperature_simulation(), 100.0) and \
self.assertGreater(red_temperature_simulation(), 103.0)

def test_yellow_temperature_simulation(self):
"""
Method to test the yellow_temperature_simulation method
"""
for i in range(100):
(self.assertGreaterEqual(yellow_temperature_simulation(),
 100.0)) and \
(self.assertLessEqual(yellow_temperature_simulation(),
  100.5)) and \
(self.assertGreaterEqual(yellow_temperature_simulation(),
 102.5)) and \
(self.assertLessEqual(yellow_temperature_simulation(), 103.0))



I try to test if a number 99.7 is less than 100.0 and at the same time I
also check if it's greater than 102.5. The sentence itself doesn't make
sense, but somehow the test case for yellow passes, but the test case for
red fails. I have also tried using and instead of or!

I want to test my code, is there a better way of doing it?

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


Re: [Tutor] installing rtree on python 34

2016-02-06 Thread Bachir Bachir via Tutor
Hello AlainI am using windows10 64 bits, my os.name is 'nt', I used pip to  
install but its complaining about the spatialindex_c.dll     File 
"C:\Users\Bachir\AppData\Local\Temp\pip-build-td64lrth\rtree\rtree\core.py", 
line 101, in 
       raise OSError("could not find or load spatialindex_c.dll")   OSError: 
could not find or load spatialindex_c.dll
I downloaded rtree-0.8.2 and then run python setup.py install and i have the 
same message 
 PS C:\Users\Bachir\documents\Python Scripts\Rtree-0.8.2> python setup.py 
installTraceback (most recent call last):  File "setup.py", line 4, in  
   import rtree  File "C:\Users\Bachir\documents\Python 
Scripts\Rtree-0.8.2\rtree\__init__.py", line 1, in     from .index 
import Rtree  File "C:\Users\Bachir\documents\Python 
Scripts\Rtree-0.8.2\rtree\index.py", line 6, in     from . import core  
File "C:\Users\Bachir\documents\Python Scripts\Rtree-0.8.2\rtree\core.py", line 
101, in     raise OSError("could not find or load 
spatialindex_c.dll")OSError: could not find or load spatialindex_c.dllPS 
C:\Users\Bachir\documents\Python Scripts\Rtree-0.8.2>  
i donwloaded the spatialindex dll files ' 
libspatialindex-1.8.1-win-msvc-2010-x64-x32.zip ' . this file contain both 32  
and 64 bits ,  unzip and put in the installation folder, when installing using 
python setuo.py install it still complaining about the spatialindex dll 
fileThanks much 
Bachir 

 

On Saturday, February 6, 2016 9:49 AM, Alan Gauld 
 wrote:
 

 On 06/02/16 06:47, Bachir Bachir via Tutor wrote:
>  Dear all,I am using winpython 34 , could anyone help installing rtree please?
> It seems rtree loading libspatialindex ,
> i donwnloaded the libspatial index but rtree still cant find
> the files locationthanks much for your helpsincerelyBachir

This is a bit off topic for the tutor list so you might get a better
response on an rtree or libspatial specific forum or, failing that, on
the general python mailing list/newsgroup.

However, it will help if you tell us how you are installing both rtree
and libspatial. Are you using binary installers or using pip? Show us
the exact command you typed.

Telling us the OS might help too, although I assume its some
kind of Windows.

-- 
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] installing rtree on python 34

2016-02-06 Thread Alan Gauld
On 06/02/16 06:47, Bachir Bachir via Tutor wrote:
>  Dear all,I am using winpython 34 , could anyone help installing rtree please?
> It seems rtree loading libspatialindex ,
> i donwnloaded the libspatial index but rtree still cant find
> the files locationthanks much for your helpsincerelyBachir

This is a bit off topic for the tutor list so you might get a better
response on an rtree or libspatial specific forum or, failing that, on
the general python mailing list/newsgroup.

However, it will help if you tell us how you are installing both rtree
and libspatial. Are you using binary installers or using pip? Show us
the exact command you typed.

Telling us the OS might help too, although I assume its some
kind of Windows.

-- 
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] installing rtree on python 34

2016-02-06 Thread Bachir Bachir via Tutor
 Dear all,I am using winpython 34 , could anyone help installing rtree 
please?It seems rtree loading libspatialindex , i donwnloaded the libspatial 
index but rtree still cant find the files locationthanks much for your 
helpsincerelyBachir
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor