Re: Finding .so files without setting LD_LIBRARY_PATH

2016-05-12 Thread dieter
Paul Smith  writes:
> ...
> That works fine, but here's the problem: because LD_LIBRARY_PATH is in
> Python's environment it is also passed down to programs invoked by
> Python.  That means if I (for example) invoke subprocess.call(['ssh',
> ...]) then it fails because the system ssh is looking for the system
> libcrypto.so, and when it finds the Python libcrypto.so instead
> (because of LD_LIBRARY_PATH) it fails.

I have had a similar problem - however with "PYTHONPATH" instead
of "LD_LIBRARY_PATH". I solved it by removing "PYTHONPATH" from
"os.environ" during Python startup. This way, the envvar
"PYTHONPATH" was effective to set up "sys.path" but
processes started from this python would not see it.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String concatenation

2016-05-12 Thread Steven D'Aprano
On Tuesday 10 May 2016 18:15, David Palao wrote:

> 2016-05-10 9:54 GMT+02:00 Steven D'Aprano
> :

>> I'm surprised that Spanish names are not affected. Consider a woman who goes
>> by the personal name of Maria Teresa, whose father's first surname was
>> García and mother's first surname was Ramírez. Her name would therefore be
>> Maria Teresa García Ramírez. If she marries Elí Arroyo López, then she might
>> change her name to Maria Teresa García Ramírez de Arroyo. With six words,
>> that would fall foul of Facebook's foul naming policy.

> In Spain "de Arroyo" officially does not become part of the name. The
> same applies to other countries as well. Not 100% sure that it is true
> in every Spanish speaking country though.

Thanks for the clarification David. Nevertheless, whether it is part of her 
*legal* name or not, some Spanish women prefer to use her husband's name as 
part of her preferred real name, that is, the name she answers to and the name 
she prefers to sign on correspondence. Maybe it's a generation thing? Perhaps 
in Spain the women using Facebook and the women taking their husband's name 
don't intersect?


-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Analytical Geometry in Python with GeoMath

2016-05-12 Thread Steven D'Aprano
Hi Vinicius,

On Thursday 05 May 2016 04:16, Vinicius wrote:

> To add a point, you do:
> From geomath import point
> A = point.Point(x,y)
> A.distance(PointB)
> A.mispoint(PointB)
> A.quadrant()


How does your library compare with Eukleides?

http://www.eukleides.org/quickstart.html



-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String concatenation

2016-05-12 Thread Steven D'Aprano
On Tuesday 10 May 2016 12:42, Chris Angelico wrote:

> On Tue, May 10, 2016 at 12:32 PM, Steven D'Aprano 
> wrote:
>> Floats are old (they go back to the first release of Python), they have
>> many quirks (x + y - x is not necessarily equal to y), and people make
>> many errors with floats. Does this mean they are deprecated? Of course
>> not.
> 
> Careful there Steven - now that cdecimal is in core, people might
> start saying that. 

They *might*, but they haven't *yet*.

And people can say all sorts of things, doesn't make them true.


> Particularly if (as is periodically requested, and
> which I think would be a good idea) Decimal literals become a thing.
> And the removal of core types HAS happened. Correct me if I'm wrong,
> but didn't the first release of Python have only the short integer
> type, and then a completely new 'long' type was added? 

The oldest Python I have installed is 0.9.1, which not only lacks the L suffix, 
but it also lacks ** for exponentiation. There's also no "long" type:

>>> n = 2
>>> for i in range(100):
... n = n * 2
... 
Unhandled exception: run-time error: integer overflow
Stack backtrace (innermost last):
  File "", line 2



> Automatic
> promotion blurred the distinction, and then Python 3.0 removed the
> 'int' type and renamed 'long'. So it's theoretically possible for
> Decimal to replace float...
> 
> ... except that that would actually be a bad idea, which a lot of
> people don't realize.

Indeed. Decimal might not be old, but it does have quirks and it can certainly 
lead to errors (both misuse and rounding errors, among others). It is subject 
to the same sorts of floating point issues as binary floats.



-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread srinivas devaki
On May 9, 2016 5:31 AM, "Tim Chase"  wrote:
>
> then that's a bad code-smell (you get quadratic behavior as the
> strings are constantly resized), usually better replaced with
>

I just want to point out that in Python s += str in loop is not giving
quadratic behavior. I don't know why but it runs fast. I'm very much
interested to know why it is so?

In [3]: %%timeit
   ...: s = ''
   ...: for x in xrange(10**6):
   ...: s += str(x)
   ...:
1 loop, best of 3: 383 ms per loop

In [4]: %%timeit
s = ''
for x in xrange(10**6):
s = s + str(x)
   ...:
1 loop, best of 3: 383 ms per loop
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: More list building

2016-05-12 Thread marco . nawijn
On Wednesday, May 11, 2016 at 7:22:09 PM UTC+2, DFS wrote:
> Have:
> p1 = ['Now', 'the', 'for', 'good']
> p2 = ['is', 'time', 'all', 'men']
> 
> want
> [('Now','is','the','time'), ('for','all','good','men')]
> 
> This works:
> 
> p = []
> for i in xrange(0,len(p1),2):
>   p.insert(i,(p1[i],p2[i],p1[i+1],p2[i+1]))
> 
> 
> But it seems clunky.
> 
> Better way(s)?
> 
> Thanks

Another way using some array manipulation:
(using Python 2.7 and numpy)

# coding: utf-8
import numpy as np

p1 = np.array(('Now', 'the', 'for', 'good'))
p2 = np.array(('is', 'time', 'all', 'men'))

p1s = np.split(p1, 2)
p2s = np.split(p2, 2)

a1 = np.vstack((p1, p2))

r1 = np.hstack((a1[:, 0], a1[:, 1]))
r2 = np.hstack((a1[:, 2], a1[:, 3]))

print r1
print r2

Produces the following output:
['Now' 'is' 'the' 'time']
['for' 'all' 'good' 'men']
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Chris Angelico
On Thu, May 12, 2016 at 6:23 PM, srinivas devaki
 wrote:
> On May 9, 2016 5:31 AM, "Tim Chase"  wrote:
>>
>> then that's a bad code-smell (you get quadratic behavior as the
>> strings are constantly resized), usually better replaced with
>>
>
> I just want to point out that in Python s += str in loop is not giving
> quadratic behavior. I don't know why but it runs fast. I'm very much
> interested to know why it is so?
>
> In [3]: %%timeit
>...: s = ''
>...: for x in xrange(10**6):
>...: s += str(x)
>...:
> 1 loop, best of 3: 383 ms per loop
>
> In [4]: %%timeit
> s = ''
> for x in xrange(10**6):
> s = s + str(x)
>...:
> 1 loop, best of 3: 383 ms per loop

Some versions of CPython do include an optimization for this. However,
it's not guaranteed, and it's easy to disrupt. For starters, it works
only if you're appending to a string, not prepending; this will be
much slower:

s = ''
for x in range(10**6):
s = str(x) + s

And other Pythons may not optimize this. So don't depend on it.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: More list building

2016-05-12 Thread Peter Otten
[email protected] wrote:

> On Wednesday, May 11, 2016 at 1:22:09 PM UTC-4, DFS wrote:
>> Have:
>> p1 = ['Now', 'the', 'for', 'good']
>> p2 = ['is', 'time', 'all', 'men']
>> 
>> want
>> [('Now','is','the','time'), ('for','all','good','men')]
>> 
>> This works:
>> 
>> p = []
>> for i in xrange(0,len(p1),2):
>> p.insert(i,(p1[i],p2[i],p1[i+1],p2[i+1]))
>> 
>> 
>> But it seems clunky.
>> 
>> Better way(s)?
>> 
>> Thanks
> 
> Would this work for you? Or do you need something more general?
> t = list(zip(p1,p2))
> p = [t[0]+t[1],t[2]+t[3]]

This can be generalized as

>>> t = iter(zip(p1, p2))
>>> [a + b for a, b in zip(t, t)]
[('Now', 'is', 'the', 'time'), ('for', 'all', 'good', 'men')]

(The iter() call can be omitted in Python 3.)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread alister
On Tue, 10 May 2016 19:40:02 -0400, DFS wrote:

> Sometimes I try to be a funny smart-aleck and it doesn't work.

this is the problem everyone is having with your post, you acknowledge 
that it doesn't work so why keep trying.

I too can fall guilty of this behavior (I can be a bit condescending of 
one of our engineers calls for help & asks a question he should already 
know)  but have learnt that when I am asking for help it is probably not 
a good idea to upset the person I am asking.
> 
> But, I am dead serious about becoming a good Python developer, and I
> truly appreciate all clp replies.
> 
> 
>> and especially, don't call people names.
> 
> Maybe.  I'll always call a spade a spade.
> 
> 
> 
> Note: Angelico is about the coolest last name I've heard in a long time.
>Is it real?





-- 
I can mend the break of day, heal a broken heart, and provide temporary
relief to nymphomaniacs.
-- Larry Lee
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Chris Angelico
On Thu, May 12, 2016 at 7:12 PM, alister  wrote:
> [presumably DFS wrote, but I didn't see]
>> Note: Angelico is about the coolest last name I've heard in a long time.
>>Is it real?

It is, actually! It's a precious heirloom. It belonged to my father,
the great Talldad; and it belonged to his father (my grandfather), and
his father. It's been in the family for quite a while, and I'm very
careful not to let it get damaged.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Ned Batchelder
On Thursday, May 12, 2016 at 4:24:55 AM UTC-4, srinivas devaki wrote:
> On May 9, 2016 5:31 AM, "Tim Chase"  wrote:
> >
> > then that's a bad code-smell (you get quadratic behavior as the
> > strings are constantly resized), usually better replaced with
> >
> 
> I just want to point out that in Python s += str in loop is not giving
> quadratic behavior. I don't know why but it runs fast. I'm very much
> interested to know why it is so?
> 
> In [3]: %%timeit
>...: s = ''
>...: for x in xrange(10**6):
>...: s += str(x)
>...:
> 1 loop, best of 3: 383 ms per loop
> 
> In [4]: %%timeit
> s = ''
> for x in xrange(10**6):
> s = s + str(x)
>...:
> 1 loop, best of 3: 383 ms per loop

The CPython optimization depends on the string having only a single
reference.  A seemingly unrelated change to the code can change the
performance significantly:

In [1]: %%timeit
   ...: s = ""
   ...: for x in xrange(10):
   ...:   s = s + str(x)
   ...:
10 loops, best of 3: 33.5 ms per loop

In [2]: %%timeit
   ...: s = t = ""
   ...: for x in xrange(10):
   ...:   s = t = s + str(x)
   ...:
1 loop, best of 3: 1.57 s per loop

Be careful out there...

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Jon Ribbens
On 2016-05-10, DFS  wrote:
> This isn't a moderated group.

That doesn't represent the whole truth. The Usenet group is
unmoderated but is gatewayed to a mailing list, which of course can be
moderated. So while you can't be moderated on the Usenet group, much
of the potential audience for posts here are actually reading via the
list, and hence won't see your posts if they are moderated out.

(Personally I think this situation is more than a little silly and
the group should be changed to moderated.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Chris Angelico
On Thu, May 12, 2016 at 11:39 PM, Jon Ribbens
 wrote:
> On 2016-05-10, DFS  wrote:
>> This isn't a moderated group.
>
> That doesn't represent the whole truth. The Usenet group is
> unmoderated but is gatewayed to a mailing list, which of course can be
> moderated. So while you can't be moderated on the Usenet group, much
> of the potential audience for posts here are actually reading via the
> list, and hence won't see your posts if they are moderated out.
>
> (Personally I think this situation is more than a little silly and
> the group should be changed to moderated.)

For example, I'm seeing this, but you're replying to something I didn't see.

It's one of the many advantages of reading the mailing list rather
than the newsgroup.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Michael Torrie
On 05/12/2016 03:12 AM, alister wrote:
> On Tue, 10 May 2016 19:40:02 -0400, DFS wrote:
> 
>> Sometimes I try to be a funny smart-aleck and it doesn't work.
> 
> this is the problem everyone is having with your post, you acknowledge 
> that it doesn't work so why keep trying.
> 
> I too can fall guilty of this behavior (I can be a bit condescending of 
> one of our engineers calls for help & asks a question he should already 
> know)  but have learnt that when I am asking for help it is probably not 
> a good idea to upset the person I am asking.

>> Maybe.  I'll always call a spade a spade.

It's really sad to see folks like DFS hop on the list with apparent
enthusiasm for Python and an excitement to learn, only to resort to name
calling and walk away in a huff when folks ask them not to speak that
way around here.  I'm not sure why this is.  I recall that the same
thing happened not so long ago with another poster I recall.  Overall I
think list members are pretty patient with newbies.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?

2016-05-12 Thread Steven D'Aprano
On Thu, 12 May 2016 07:36 pm, Ned Batchelder wrote:

> The CPython optimization depends on the string having only a single
> reference.  A seemingly unrelated change to the code can change the
> performance significantly:
> 
> In [1]: %%timeit
>...: s = ""
>...: for x in xrange(10):
>...:   s = s + str(x)
>...:
> 10 loops, best of 3: 33.5 ms per loop
> 
> In [2]: %%timeit
>...: s = t = ""
>...: for x in xrange(10):
>...:   s = t = s + str(x)
>...:
> 1 loop, best of 3: 1.57 s per loop


Nice demonstration!

But it is actually even worse than that. The optimization depends on memory
allocation details which means that some CPython interpreters cannot use
it, depending on the operating system and version.

Consequently, reliance on it can and has lead to embarrassments like this
performance bug which only affected *some* Windows users. In 2009, Chris
Withers asked for help debugging a problem where Python httplib was
hundreds of times slower than other tools, like wget and Internet Explorer:

https://mail.python.org/pipermail/python-dev/2009-August/091125.html

A few weeks later, Simon Cross realised the problem was probably the
quadratic behaviour of repeated string addition:

https://mail.python.org/pipermail/python-dev/2009-September/091582.html

leading to this quote from Antoine Pitrou:

"Given differences between platforms in realloc() performance, it might be
the reason why it goes unnoticed under Linux but degenerates under
Windows."

https://mail.python.org/pipermail/python-dev/2009-September/091583.html

and Guido's comment:

"Also agreed that this is an embarrassment."

https://mail.python.org/pipermail/python-dev/2009-September/091592.html

So beware of relying on the CPython string concatenation optimization in
production code!


Here's the tracker issue that added the optimization in the first place:

http://bugs.python.org/issue980695

The feature was controversial at the time (and remains slightly so):

https://mail.python.org/pipermail/python-dev/2004-August/046686.html


My opinion is that it is great for interactive use at the Python prompt, but
I would never use it in code I cared about.



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding .so files without setting LD_LIBRARY_PATH

2016-05-12 Thread Paul Smith
On Thu, 2016-05-12 at 07:55 +0300, Jussi Piitulainen wrote:
> eryk sun writes:
> 
> > On Wed, May 11, 2016 at 10:39 PM, Paul Smith wrote:
> > > Hi all.  I have a locally-built version of Python (2.7.11) that I'm
> > > copying around to different systems, running all different versions of
> > > GNU/Linux.
> > ...
> > > What I'd like to do is have a way of setting the library path that
> > > Python uses when it tries to load .so files (for example in the ssl
> > > module which loads lib-dynload/_ssl.so which links to libssl.so and
> > > libcrypto.so), WITHOUT setting LD_LIBRARY_PATH in the environment that
> > > Python passes to its children.
> > 
> > An ELF header can contain either an RPATH or a RUNPATH to extend the
> > library search path at load time.

Yes, I'm familiar with rpath.  However I don't know how to set it for
Python .so's that appear in the lib-dynload directory, including site
-packages, which is where it needs to be ... I tried to go through the
Modules/Setup etc. but to no avail :(.

> There's a tool (GPLv3+) called patchelf that can set these in an ELF
> binary:

Aha!  That's excellent!  The only one I was aware of was chrpath which
doesn't allow you to add an rpath if one doesn't exist, or add an rpath
which is longer than the one that already exists.

This will make things much simpler.

Cheers!
-- 
https://mail.python.org/mailman/listinfo/python-list


Average calculation Program *need help*

2016-05-12 Thread kobsx4
Hello all, I have been struggling with this code for 3 hours now and I'm still 
stumped. My problem is that when I run the following code:
--
#this function will get the total scores
def getScores(totalScores, number):
for counter in range(0, number):
score = input('Enter their score: ')
totalScores = totalScores + score

while not (score >= 0 and score <= 100):

print "Your score must be between 0 and 100."
score = input('Enter their score: ')



return totalScores
--
the program is supposed to find the average of two test scores and if one of 
the scores is out of the score range (0-100), an error message is displayed. 
The main problem with this is that when someone types in a number outside of 
the range, it'll ask them to enter two scores again, but ends up adding all of 
the scores together (including the invalid ones) and dividing by how many there 
are. Please help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Igor Korot
Hi,

On Thu, May 12, 2016 at 11:47 AM,   wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm 
> still stumped. My problem is that when I run the following code:
> --
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
>
> while not (score >= 0 and score <= 100):
>
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
>
>
>
> return totalScores
> --
> the program is supposed to find the average of two test scores and if one of 
> the scores is out of the score range (0-100), an error message is displayed. 
> The main problem with this is that when someone types in a number outside of 
> the range, it'll ask them to enter two scores again, but ends up adding all 
> of the scores together (including the invalid ones) and dividing by how many 
> there are. Please help.

Can you solve this problem with just pen and paper?
Do that and then translate the algorithm to python.

Thank you.

> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Rustom Mody
On Thursday, May 12, 2016 at 9:18:08 PM UTC+5:30, Jake Kobs wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm 
> still stumped. My problem is that when I run the following code:
> --
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
> 
> while not (score >= 0 and score <= 100):
> 
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
> 
> 
> 
> return totalScores
> --
> the program is supposed to find the average of two test scores and if one of 
> the scores is out of the score range (0-100), an error message is displayed. 
> The main problem with this is that when someone types in a number outside of 
> the range, it'll ask them to enter two scores again, but ends up adding all 
> of the scores together (including the invalid ones) and dividing by how many 
> there are. Please help.

I suggest you conceptualize the problem into 3 sub-problems:
- IO
- average computation
- validation (between 0 and 100)

For starters ignore validation.
So we only have IO + computation
ie Input then computation then Output

Now if your input needs to feed into a computation you need a *data-structure*
I suggest you consider lists
eg the 5 scores
4
2
1
3
5
would be represented as the list [4,2,1,3,5]
Their average -- 3 -- is to be *computed* and then to be given back to the user
-- ie *output*

Does this help start you off?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread MRAB

On 2016-05-12 16:47, [email protected] wrote:

Hello all, I have been struggling with this code for 3 hours now and I'm still 
stumped. My problem is that when I run the following code:
--
#this function will get the total scores
def getScores(totalScores, number):
for counter in range(0, number):
score = input('Enter their score: ')
totalScores = totalScores + score

while not (score >= 0 and score <= 100):

print "Your score must be between 0 and 100."
score = input('Enter their score: ')



return totalScores
--
the program is supposed to find the average of two test scores and if one of 
the scores is out of the score range (0-100), an error message is displayed. 
The main problem with this is that when someone types in a number outside of 
the range, it'll ask them to enter two scores again, but ends up adding all of 
the scores together (including the invalid ones) and dividing by how many there 
are. Please help.


Basically what you want is:

repeat as many times as there are scores you want:
ask for the score

while it's invalid:
ask for it again

add it to the total

What you're actually doing is asking for the scores and adding them 
_before_ checking. Don't include something before you've checked that 
it's OK.


--
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Martin A. Brown

Greetings kobsx4,

>Hello all, I have been struggling with this code for 3 hours now 
>and I'm still stumped. My problem is that when I run the following 
>code:

>--
>#this function will get the total scores
>def getScores(totalScores, number):
>for counter in range(0, number):
>score = input('Enter their score: ')
>totalScores = totalScores + score
>
>while not (score >= 0 and score <= 100):
>
>print "Your score must be between 0 and 100."
>score = input('Enter their score: ')
>
>
>
>return totalScores
>--

>the program is supposed to find the average of two test scores and 
>if one of the scores is out of the score range (0-100), an error 
>message is displayed. The main problem with this is that when 
>someone types in a number outside of the range, it'll ask them to 
>enter two scores again, but ends up adding all of the scores 
>together (including the invalid ones) and dividing by how many 
>there are. Please help.

Suggestion #1: 
--
When you are stuck on a small piece of code, set it aside (stop 
looking at it) and start over again; sometimes rewriting with 
different variable names and a clean slate helps to highlight the 
problem.

Professional programmers will tell you that they are quite 
accustomed to 'throwing away' code.  Don't be afraid to do it.

(While you are still learning, you might want to keep the old chunk 
of code around to examine so that you can maybe figure out what you 
did wrong.)
  

Suggestion #2:
--
Put a print statement or two in the loop, so that you see how your 
variables are changing.  For example, just before your 'while' line, 
maybe something like:

  print "score=%d totalScores=%d" % (score, totalScores,)


Suggestion #3:
--
Break the problem down even smaller (Rustom Mody appears to have 
beat me to the punch on that suggestion, so I'll just point to his 
email.)


Hint #1:

What is the value of your variable totalScores each time through the 
loop?  Does it ever get reset?

Good luck with your degubbing!

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
-- 
https://mail.python.org/mailman/listinfo/python-list


Calling python from C with OpenMP

2016-05-12 Thread oysteijo
Hi,

I have a framework written in C and I need to call Python from that framework. 
I have written the code, and it runs fine, however when I recompile with OpenMP 
enabled, I get segmentation faults and some times an error message: 

Fatal Python error: GC object already tracked

I'm able to reconstruct the bug with this simple code:

/* main.c */
#include 
#include 
int main()
{
wchar_t *program = Py_DecodeLocale( "My_problem", NULL );
if( !program ){
fprintf(stderr, "cannot decode python program name\n");
return -1;
}

Py_SetProgramName( program );
Py_Initialize();

PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyUnicode_FromString("."));

PyObject *module_filename = PyUnicode_FromString( "multiplier" );
if(!module_filename){
printf("Cannot create python module  multiplier.\n");
return -1;
}
PyObject *module = PyImport_Import( module_filename );
if(!module){
printf("Cannot create python.\n");
return -1;
}

PyObject *mult_obj = PyObject_CallMethod( module ,"multiplier", "i", 7);
if(!mult_obj){
printf("Cannot create python multiplier class instance\n");
return -1;
}

Py_DECREF( module );
Py_DECREF( module_filename );
Py_DECREF( path );
Py_DECREF( sys );
/* Up to now we have actually done:
 * >>> import multiplier
 * >>> mult_obj = multipier.multiplier(7)
 */

/* lets try something like:
 * >>> for x in range(10):
 * ... printf(mult_obj.do_multiply(x))
 */

#pragma omp parallel for 
for( int i = 0; i < 10; i++ ){
PyObject *ret = PyObject_CallMethod( mult_obj, "do_multiply", "i", i );
if( !ret ){
printf("Cannot call 'do_multiply'\n");
continue;
}
printf("The value calculated in Python was: %3d\n", (int) 
PyLong_AsLong(ret));
Py_DECREF(ret);
}

Py_DECREF(mult_obj);
Py_Finalize();

return 0;
} 

Compile with:
gcc -std=gnu99 -O3 -Wall -Wextra -fopenmp `pkg-config --cflags --libs python3` 
-lgomp main.c -o main

Then you need the python code:

# multiplier.py
class multiplier(object):
def __init__(self, factor):
self.factor = factor
def do_multiply(self, x):
return self.factor * x

First question: Does my C code leak memory? Valgrind says it does, but the 
memory footprint of the executable is stable while looping?

Second and most important question: When I run this code it sometimes 
segementation faults, and sometimes some threads run normal and some other 
threads says "Cannot call 'do_multiply'". Sometimes I get the message: Fatal 
Python error: GC object already tracked. And some times it even runs normally...
I understand there is some kind of race condition here, where python tries to 
refer to some memory that has been already released. But how can I avoid this? 
What am I doing wrong? (or, less likely, is this a bug?)

Maybe needless to say, but the code works when compiled w/o OpenMP.

Using Python 3.5.1

Thanks,
-Øystein
-- 
https://mail.python.org/mailman/listinfo/python-list


Why online forums have bad behaviour (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?)

2016-05-12 Thread Ben Finney
Michael Torrie  writes:

> It's really sad to see folks like DFS hop on the list with apparent
> enthusiasm for Python and an excitement to learn, only to resort to
> name calling and walk away in a huff when folks ask them not to speak
> that way around here. I'm not sure why this is.

TL;DR: because we're all human, and human behaviour needs either
immediate face-to-face feedback or social enforcement to correct
selfishness and abrasiveness. Where face-to-face feedback is lacking,
social enforcement needs to take more of the load.


Many people have a false sense of entitlement to be caustic in dealing
with others, and have no better response to a request that they tone it
down than to escalate their bad behaviour.

This behaviour is usually counteracted in face-to-face interaction, by
being confronted with the immediate result on the other person: most
people don't enjoy *seeing* other people become upset, so most people
tend to work harder to be more polite in face-to-face discussion.

On an internet forum, especially one with such low bandwidth as text,
these feedback mechanisms are not sufficient (not immediate enough, and
not informative enough) for the person to experience a link from their
bad behaviour to the unpleasant consequences.


This isn't a new problem. It's not new to the internet, and it certainly
isn't new to humans.

What is new, though, is that many online communities – the Python
community specifically – have decided we are not going to tolerate
anti-social behaviour, and we have also enacted policies to enforce that
decision.

We'll always have some anti-social actors, and bad actions by otherwise
good actors. Many of them when confronted will respond with petulance
and name-calling and bullying and other schoolyard reflexes. We have to
be consistent in rejecting such behaviour from our community.

-- 
 \  “Writing a book is like washing an elephant: there no good |
  `\place to begin or end, and it's hard to keep track of what |
_o__)  you've already covered.” —anonymous |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Design: Idiom for classes and methods that are customizable by the user?

2016-05-12 Thread Dirk Bächle

Hi there,

I'm one of the SCons (http://www.scons.org) developers and am working on a partial redesign of our architecture. I'd like to come up 
with a decent proposal about how to extend and rewrite our code base, such that it's easier for a user to overload and replace some 
parts of the functionality.



Concrete example


We're a build system and all the code is written in Python. Even more so, our build scripts (build description files) are Python 
scripts...allowing the user to have the full power of this beautiful language at his/her fingertips whenever it's needed.

The single build steps are file-oriented, meaning that our "targets" are always 
either files or directories (simplified).
Each of these files and directories is tracked by an instance of the "Node" 
class, which is able to provide infos like:

  - Did this (source) file change?
  - Which are my children (meaning, on which other Nodes do I depend)?
  - Which commands, or "Actions", are required to build this Node (could be a 
Program or a Library, for example)?

We then have a "Taskmaster" which operates on the set of known "Node"s (think: "Strategy"). It loops over the Nodes and tries to 
find one that isn't up-to-date. If all its dependencies are met (=all its children are up-to-date), the Node is ready to get built 
(again, largely simplified).


What happens now and then is, that users are unhappy with the way this Taskmaster proceeds. One peculiar detail is, that our 
"default" Taskmaster always deletes the old target file before re-building it...and in special situations this may be seen as unwanted.
So it would be good to offer a set of Taskmasters to the user, where he can choose from. Even better would be, if the user could add 
a Taskmaster of his own (probably derived from the "original") and activate it...from a build description file (=Python script), so 
without touching the core sources.


To put it shortly, the user should be able to slip a new Taskmaster under the covers of SCons...and it should behave as if it 
would've been built-in.



My current approach
===

I'm currently following the "Factory" pattern (more or less) as I know it from C++ and similar languages. In the Taskmaster module I 
have a dictionary:



# The built-in Taskmasters
types = {'default' : DefaultTaskmaster,
 'noclean' : NocleanTaskmaster}

def create(key, targets, top, node):
""" Simple factory for creating an actual Taskmaster,
based on the given key.
"""
if key in types:
return types[key](targets, top, node)

return DefaultTaskmaster(targets, top, node)

def add(key, taskm_class):
""" Register the given Taskmaster class, if its key doesn't
exist yet.
"""
if not key in types:
types[key] = taskm_class


with two supporting functions. I'm leaving out all the boilerplate stuff here, like parsing command-line options for the 
to-be-instantiated Taskmaster type and so on.

But this is the core idea so far, simple and certainly "pythonic" to some 
degree...


My questions


- Is this a good approach, that I could use for other parts of the architecture 
as well, e.g. the Node class mentioned above?
- Are there other options that have stood the test of time under operational conditions? If yes, I'd be interested to get links and 
pointers to the corresponding projects (I'm prepared to read stuff and do further investigations on my own).



When talking about "other options" I'm mainly thinking in the direction of "plugins" (Yapsy?)...not sure whether this idea could 
really fly.



Some criteria
=

- The approach should work under 2.7.x (and higher) and Python 3.x as well. We're currently rewriting our code (runs only under 
Python 2.7 so far) to a common codebase, using "futurize".
- It should be applicable to classes and simple methods within arbitrary modules. One specialized scheme for classes and methods 
each would be okay though.

- Heavy-weight mechanism are a no-go...in large build projects we have to 
initialize 800k instances of the Node class, and more.
- Some of our classes, especially Node, use the "slots" mechanism in order to save memory (and yes, it's necessary! ;) ). So, 
certain techniques (=metaclasses?) are probably not compatible with that...



Your advice and input on these thoughts are welcome. If required, I can provide more details in a certain area...but I didn't want 
to make this initial email too long.


Thanks a lot in advance for your answers and best regards,

Dirk Baechle
--
https://mail.python.org/mailman/listinfo/python-list


Re: Design: Idiom for classes and methods that are customizable by the user?

2016-05-12 Thread Marko Rauhamaa
Dirk Bächle :

> I'm one of the SCons (http://www.scons.org) developers

I take the opportunity to thank you and your colleagues for the
wonderful building tool that I have used professionally to great success
since 2003.

> We're a build system and all the code is written in Python. Even more
> so, our build scripts (build description files) are Python
> scripts...allowing the user to have the full power of this beautiful
> language at his/her fingertips whenever it's needed.

It is indeed great to have Python available when needed. However, I have
striven not to need it. SConscript files should be understood by casual
maintainers who aren't necessarily fluent with Python.

> To put it shortly, the user should be able to slip a new Taskmaster
> under the covers of SCons...and it should behave as if it would've
> been built-in.

I have tried to resist the temptation, successfully so far.

> # The built-in Taskmasters
> types = {'default' : DefaultTaskmaster,
>  'noclean' : NocleanTaskmaster}
>
> def create(key, targets, top, node):
> """ Simple factory for creating an actual Taskmaster,
> based on the given key.
> """
> if key in types:
> return types[key](targets, top, node)
>
> return DefaultTaskmaster(targets, top, node)
>
> def add(key, taskm_class):
> """ Register the given Taskmaster class, if its key doesn't
> exist yet.
> """
> if not key in types:
> types[key] = taskm_class
>
> [...]
>
> - Is this a good approach

As an approach, that looks ok. The code looks a bit too lenient, though,
which can easily surprise and baffle the user. I think it would be
better to fail:


class AlreadyExistsError(Exception):
   pass

types = {}

def create(key, targets, top, node):
""" Simple factory for creating an actual Taskmaster,
based on the given key.
"""
return types[key](targets, top, node)

def add(key, taskm_class):
""" Register the given Taskmaster class, if its key doesn't
exist yet.
"""
if key in types:
raise AlreadyExistsError("Task manager '%s' already exists" % key)
types[key] = taskm_class

# The built-in Taskmasters
add('default', DefaultTaskmaster)
add('noclean', NocleanTaskmaster)


> - Are there other options that have stood the test of time under
> operational conditions? If yes, I'd be interested to get links and
> pointers to the corresponding projects (I'm prepared to read stuff and
> do further investigations on my own).

Based on the given information, I can't give recommendations. However,
in general, I'm suspicious of the factory pattern. Whenever possible, I
prefer explicit construction.

For example, why do you need a key? Couldn't you simply pass the task
master class as an argument?


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Calling python from C with OpenMP

2016-05-12 Thread Sturla Molden
 wrote:

> Second and most important question: When I run this code it sometimes
> segementation faults, and sometimes some threads run normal and some
> other threads says "Cannot call 'do_multiply'". Sometimes I get the
> message: Fatal Python error: GC object already tracked. And some times it
> even runs normally...
> I understand there is some kind of race condition here, where python
> tries to refer to some memory that has been already released. But how can
> I avoid this? What am I doing wrong? (or, less likely, is this a bug?)


You must own the GIL before you can safely use the Python C API, object
creation and refcounting in particular. Use the "Simplified GIL API" to
grab the GIL and release it when you are done.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why online forums have bad behaviour (was: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement?)

2016-05-12 Thread Jason Friedman
> TL;DR: because we're all human, and human behaviour needs either
> immediate face-to-face feedback or social enforcement to correct
> selfishness and abrasiveness. Where face-to-face feedback is lacking,
> social enforcement needs to take more of the load.
>
>
> Many people have a false sense of entitlement to be caustic in dealing
> with others, and have no better response to a request that they tone it
> down than to escalate their bad behaviour.
>
> This behaviour is usually counteracted in face-to-face interaction, by
> being confronted with the immediate result on the other person: most
> people don't enjoy *seeing* other people become upset, so most people
> tend to work harder to be more polite in face-to-face discussion.
>
> On an internet forum, especially one with such low bandwidth as text,
> these feedback mechanisms are not sufficient (not immediate enough, and
> not informative enough) for the person to experience a link from their
> bad behaviour to the unpleasant consequences.
>
>
> This isn't a new problem. It's not new to the internet, and it certainly
> isn't new to humans.
>
> What is new, though, is that many online communities – the Python
> community specifically – have decided we are not going to tolerate
> anti-social behaviour, and we have also enacted policies to enforce that
> decision.
>
> We'll always have some anti-social actors, and bad actions by otherwise
> good actors. Many of them when confronted will respond with petulance
> and name-calling and bullying and other schoolyard reflexes. We have to
> be consistent in rejecting such behaviour from our community.

Well said.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Jake Kobs
On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
> Hello all, I have been struggling with this code for 3 hours now and I'm 
> still stumped. My problem is that when I run the following code:
> --
> #this function will get the total scores
> def getScores(totalScores, number):
> for counter in range(0, number):
> score = input('Enter their score: ')
> totalScores = totalScores + score
> 
> while not (score >= 0 and score <= 100):
> 
> print "Your score must be between 0 and 100."
> score = input('Enter their score: ')
> 
> 
> 
> return totalScores
> --
> the program is supposed to find the average of two test scores and if one of 
> the scores is out of the score range (0-100), an error message is displayed. 
> The main problem with this is that when someone types in a number outside of 
> the range, it'll ask them to enter two scores again, but ends up adding all 
> of the scores together (including the invalid ones) and dividing by how many 
> there are. Please help.
 
I still can't get it. Someone please tell me lol. I have done everything I can 
and still I get bad answers.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Ben Finney
Jake Kobs  writes:

> I still can't get it. Someone please tell me lol. I have done
> everything I can and still I get bad answers.

You may want to join our dedicated beginner tutoring forum
https://mail.python.org/mailman/listinfo/tutor>, which specialises
in collaborative teaching of the fundamentals of Python.

-- 
 \ “If we don't believe in freedom of expression for people we |
  `\   despise, we don't believe in it at all.” —Noam Chomsky, |
_o__)   1992-11-25 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Michael Torrie
On 05/12/2016 10:22 PM, Jake Kobs wrote:
> On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
>> Hello all, I have been struggling with this code for 3 hours now and I'm 
>> still stumped. My problem is that when I run the following code:
>> --
>> #this function will get the total scores
>> def getScores(totalScores, number):
>> for counter in range(0, number):
>> score = input('Enter their score: ')
>> totalScores = totalScores + score
>> 
>> while not (score >= 0 and score <= 100):
>> 
>> print "Your score must be between 0 and 100."
>> score = input('Enter their score: ')
>> 
>> 
>> 
>> return totalScores
>> --
>> the program is supposed to find the average of two test scores and if one of 
>> the scores is out of the score range (0-100), an error message is displayed. 
>> The main problem with this is that when someone types in a number outside of 
>> the range, it'll ask them to enter two scores again, but ends up adding all 
>> of the scores together (including the invalid ones) and dividing by how many 
>> there are. Please help.
>  
> I still can't get it. Someone please tell me lol. I have done everything I 
> can and still I get bad answers.

Tell us what you've done, precisely.  We're here to help you learn, not
give you the answers.

One thing that strikes me is that your while loop doesn't appear to be
indented properly.  You have it running after all the scores have been
inputted in the for loop, but I suspect this isn't what you want.  You
want the while loop to occur each time through the for loop.  Do you
know how to move this while loop to be inside the for loop?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Jake Kobs
On Thursday, May 12, 2016 at 11:57:28 PM UTC-5, Michael Torrie wrote:
> On 05/12/2016 10:22 PM, Jake Kobs wrote:
> > On Thursday, May 12, 2016 at 10:48:08 AM UTC-5, Jake Kobs wrote:
> >> Hello all, I have been struggling with this code for 3 hours now and I'm 
> >> still stumped. My problem is that when I run the following code:
> >> --
> >> #this function will get the total scores
> >> def getScores(totalScores, number):
> >> for counter in range(0, number):
> >> score = input('Enter their score: ')
> >> totalScores = totalScores + score
> >> 
> >> while not (score >= 0 and score <= 100):
> >> 
> >> print "Your score must be between 0 and 100."
> >> score = input('Enter their score: ')
> >> 
> >> 
> >> 
> >> return totalScores
> >> --
> >> the program is supposed to find the average of two test scores and if one 
> >> of the scores is out of the score range (0-100), an error message is 
> >> displayed. The main problem with this is that when someone types in a 
> >> number outside of the range, it'll ask them to enter two scores again, but 
> >> ends up adding all of the scores together (including the invalid ones) and 
> >> dividing by how many there are. Please help.
> >  
> > I still can't get it. Someone please tell me lol. I have done everything I 
> > can and still I get bad answers.
> 
> Tell us what you've done, precisely.  We're here to help you learn, not
> give you the answers.
> 
> One thing that strikes me is that your while loop doesn't appear to be
> indented properly.  You have it running after all the scores have been
> inputted in the for loop, but I suspect this isn't what you want.  You
> want the while loop to occur each time through the for loop.  Do you
> know how to move this while loop to be inside the for loop?

Im not sure how to move it inside the for loop. I've been working on this small 
problem for like 4 hours lol.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Design: Idiom for classes and methods that are customizable by the user?

2016-05-12 Thread Dirk Bächle

Hi Marko,

thank you very much for your quick reply.

On 13.05.2016 00:01, Marko Rauhamaa wrote:


[...]

- Is this a good approach


As an approach, that looks ok. The code looks a bit too lenient, though,
which can easily surprise and baffle the user. I think it would be
better to fail:


class AlreadyExistsError(Exception):
pass

[...]



Sure, I'll have exceptions in mind for the final implementation.



[...]

For example, why do you need a key? Couldn't you simply pass the task
master class as an argument?



The idea behind this is, to be able to select classes by giving a parameter on the command-line. So at some point a translation from 
a given "key" to its actual class has to happen, I guess.


Regards,

Dirk

--
https://mail.python.org/mailman/listinfo/python-list


Distinction between “class” and “type”

2016-05-12 Thread Ben Finney
Howdy all,

Ever since Python's much-celebrated Grand Unification of classes and
types, I have used those terms interchangeably: every class is a type,
and every type is a class.

That may be an unwise conflation. With the recent rise of optional type
annotation in Python 3, more people are speaking about the important
distinction between a class and a type.

This recent message from GvR, discussing a relevant PEP, advocates
keeping them separate:

PEP 484 […] tries to make a clear terminological between classes
(the things you have at runtime) and types (the things that type
checkers care about).

There's a big overlap because most classes are also types -- but not
the other way around! E.g. Any is a type but not a class (you can
neither inherit from Any nor instantiate it), and the same is true
for unions and type variables. […]

https://mail.python.org/pipermail/python-ideas/2016-May/040237.html>

As a Bear of Little Brain, this leaves me clueless. What is the
distinction Guido alludes to, and how are Python classes not also types?

And why is this distinction important, and who moved my cheesecake?

-- 
 \“It is the responsibility of intellectuals to tell the truth |
  `\and to expose lies.” —Noam Chomsky, 1967-02-23 |
_o__)  |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Distinction between “class” and “type”

2016-05-12 Thread Chris Angelico
On Fri, May 13, 2016 at 3:07 PM, Ben Finney  wrote:
> This recent message from GvR, discussing a relevant PEP, advocates
> keeping them separate:
>
> PEP 484 […] tries to make a clear terminological between classes
> (the things you have at runtime) and types (the things that type
> checkers care about).
>
> There's a big overlap because most classes are also types -- but not
> the other way around! E.g. Any is a type but not a class (you can
> neither inherit from Any nor instantiate it), and the same is true
> for unions and type variables. […]
>
> https://mail.python.org/pipermail/python-ideas/2016-May/040237.html>
>
> As a Bear of Little Brain, this leaves me clueless. What is the
> distinction Guido alludes to, and how are Python classes not also types?

The difference, as I understand it, is that types are abstract and
classes are concrete. The problem is that 'type' is an actual concrete
thing, so terminology is messy; but the examples are pretty clear -
you can't instantiate Any the way you can instantiate str or int.
There's a similar difference with ABCs like Sequence - you can't
construct a Sequence, but you can document that you expect to be
passed one.

Maybe the solution is to have a new word for "things a type checker
looks for"... except that that really does want to be, uhh, "types".
Or maybe the solution is to have a new word for "things you can
construct with the class keyword"... except that that that's "types"
too, because they're all subclasses of 'type'.

> And why is this distinction important, and who moved my cheesecake?

Because it wouldn't make sense if you asked who moved your bacon.

Mmm. Bacon. With cheesecake for afters.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Distinction between “class” and “type”

2016-05-12 Thread Rustom Mody
On Friday, May 13, 2016 at 10:37:34 AM UTC+5:30, Ben Finney wrote:
> Howdy all,
> 
> Ever since Python's much-celebrated Grand Unification of classes and
> types, I have used those terms interchangeably: every class is a type,
> and every type is a class.
> 
> That may be an unwise conflation. With the recent rise of optional type
> annotation in Python 3, more people are speaking about the important
> distinction between a class and a type.
> 
> This recent message from GvR, discussing a relevant PEP, advocates
> keeping them separate:
> 
> PEP 484 […] tries to make a clear terminological between classes
> (the things you have at runtime) and types (the things that type
> checkers care about).
> 
> There's a big overlap because most classes are also types -- but not
> the other way around! E.g. Any is a type but not a class (you can
> neither inherit from Any nor instantiate it), and the same is true
> for unions and type variables. […]
> 
> https://mail.python.org/pipermail/python-ideas/2016-May/040237.html>
> 
> As a Bear of Little Brain, this leaves me clueless. What is the
> distinction Guido alludes to, and how are Python classes not also types?
> 
> And why is this distinction important, and who moved my cheesecake?

I cannot speak for Guido.
However there is a well-known confusion here, most recently spelt out in
Bob Harper's epochal book:
http://www.cs.cmu.edu/~rwh/pfpl.html

If types are compile time things then they are part of the *syntax* of the 
language
If types are runtime things they are some kind of tags attached to objects *at 
runtime*

When a language like python with a clear historical orientation towards the 
second
starts embracing the first, it is natural that people start getting confused --
which I suspect includes Guido.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Michael Torrie
On 05/12/2016 11:03 PM, Jake Kobs wrote:
> Im not sure how to move it inside the for loop. I've been working on
> this small problem for like 4 hours lol.

I'm sorry it's so frustrating.  Sounds like you haven't got down some of
the most basic fundamentals yet.  In Python, things that should happen
one after another are placed at the same indent level on the screen.

For example:

if something:
do_this()
then_this()
then_that()

Everything indented in from the start of the if statement is in a block
and only happens if the if statement condition is true.  If there're
statements at the same level as the if, then they happen *after* the if
and it's block.  In other words, indentation is what tells Python where
you want things to run.  If you want to move the while loop inside the
for loop, you have to adjust it's indentation accordingly (and the
indentation in it's own block).

This is a big gotcha for people unfamiliar with programming in Python,
and unfamiliar with programming in general.  In other languages, indents
don't have to be a certain way, as long as you have the write statements
to close off the loop.  However, Python's method forces you to think
like a programmer and to lay things out on the screen in a logical
fashion, like a writer's outline.

If you're still stuck, you will probably want to sit down with your
teacher and have him or her go over this with you.  This is important
basic stuff you need to have clear in your mind to program computers.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Average calculation Program *need help*

2016-05-12 Thread Jake Kobs
Thank you for the help..I think I'm getting closer, but I feel like after they 
enter an invalid number, it should reset the invalid number(s) somehow. Here's 
my updated code:
--
#this function will get the total scores
def getScores(totalScores, number):
for counter in range(0, number):
score = input('Enter their score: ')
if (score < 100 and score > 0):
totalScores = totalScores + score
while (score > 100 or score < 0):

print "Your scores must be between 0 and 100."
score = input('Enter their score: ')
score = input('Enter their score: ')
totalScores = totalScores + score 


return totalScores

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Design: Idiom for classes and methods that are customizable by the user?

2016-05-12 Thread Marko Rauhamaa
Dirk Bächle :

>> For example, why do you need a key? Couldn't you simply pass the task
>> master class as an argument?
>
> The idea behind this is, to be able to select classes by giving a
> parameter on the command-line. So at some point a translation from a
> given "key" to its actual class has to happen, I guess.

I see. So are the task masters interchangeable? I would have imagined
they would modify the default operational semantics. If so, the task
manager should be fixed programmatically in the SConstruct file.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Design: Idiom for classes and methods that are customizable by the user?

2016-05-12 Thread Gregory Ewing

Dirk Bächle wrote:

What happens now and then is, that users are unhappy with the way this 
Taskmaster proceeds. One peculiar detail is, that our "default" 
Taskmaster always deletes the old target file before re-building 
it...and in special situations this may be seen as unwanted.


I'm not convinced that replacing the Taskmaster for the entire
build is the right way to address this kind of thing. What if
you want to override that behaviour for some targets but not
others? A single object that controls behaviour globally
makes things inflexible. (Distutils suffers from this problem
with its "command classes" that can only be overridden in their
entirety or not at all.)

It seems to me that it would be better for the Taskmaster to
delegate as much behaviour to the Nodes as it can, and provide
a way of designating which Node classes to use for particular
targets.

I'd even suggest that *all* of the build logic should be in
the Nodes, and the Taskmaster class shouldn't exist at all.
The top level logic should just tell the final Nodes to bring
themselves up to date, and they recursively do likewise for
their dependent nodes.

I'm currently following the "Factory" pattern (more or less) as I know 
it from C++ and similar languages.


This statement sets off alarm bells for me. If you're using some
design pattern in Python just because you learned to do it that
way in C++/Java/whatever, you're probably making it more
complicated than it needs to be.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Distinction between “class” and “type”

2016-05-12 Thread Paul Rubin
Ben Finney  writes:
> There's a big overlap because most classes are also types -- but not
> the other way around! E.g. Any is a type but not a class (you can
> neither inherit from Any nor instantiate it), and the same is true
> for unions and type variables. […]

> As a Bear of Little Brain, this leaves me clueless. What is the
> distinction Guido alludes to, and how are Python classes not also types?

I thought I understood Guido's explanation but maybe I missed something.

Let C be a class, maybe defined by a class statement or maybe a builtin
like "int".  You can make an instance of C the usual way:

   x = C()

And you can have a type annotation that says function f expects an
arg that is an instance of C:

   def f(x : C) -> int: ...

You might alternatively write a function whose arg must be either an int
or a string:

   def f(s : Union[int, str]) -> int : ...

or (I think, I haven't tried it) you can equivalently bind that type to
a variable:

   T = Union[int, str]
   def f(s : T) -> int : ...

The point here is that T is a type but it is not a class.  You can't
instantiate T by saying

   x = T()

and expecting to get back some value that is (indeterminately) an int or
a string.

That is, there's stuff (like instantiation) that you can do with types
that happen to be classes, but there are also types that aren't classes.
-- 
https://mail.python.org/mailman/listinfo/python-list