[ANN] PyWeek 18 will run in May (11th to 18th)

2014-03-13 Thread Richard Jones
Hi all,

The Python Game Programming Challenge  will run
its 18th challenge from the 11th to the 18th of May.

The PyWeek challenge:

1. Invites entrants to write a game in one week from scratch either as
an individual or in a team,
2. Is intended to be challenging and fun,
3. Will increase the public body of game tools, code and expertise,
4. Will let a lot of people actually finish a game, and
5. May inspire new projects (with ready made teams!)

Check out the help page for how to compete (and prepare) and the
growing resources message board post:

   http://pyweek.org/s/help/
   http://pyweek.org/d/4008/



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


Re: Deep vs. shallow copy?

2014-03-13 Thread Ian

On 13/03/2014 03:09, Rustom Mody wrote:

Call the action-world the 'imperative' world.
Call the value-world the 'functional' world ('declarative' would be
better but 'functional' is too entrenched).

[Following table meant to be read with fixed (courier) font]

| | Imperative | Functional  |
| Language entity | Statement  | Expression  |
| Denote (and think with) | Action | Value   |
| Abstracted into | Procedure  | Function|
| Atoms are   | Assignment | Constant/Variable   |
| Data Structures | Mutable| Immutable   |
| Loop primitive  | Recursion  | Iteration   |
| World is| In time| Timeless (Platonic) |

Small typo I think in that the looping Primitives are switched about?

Regards

Ian

--
Ian Hobson
29 Manorfield Close, Northampton NN3 9SL,
Tel: 01604 513875
Preparing eBooks for Kindle and ePub formats to give the best reader experience.

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


Re: Deep vs. shallow copy?

2014-03-13 Thread Roy Smith
In article <5320e8c9$0$29994$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> Because Python doesn't have true procedures

What do you mean by "true procedure"?  Are you just talking about 
subroutines that don't return any value, i.e. fortran's SUBROUTINE vs. 
FUNCTION?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Marko Rauhamaa
Roy Smith :

>  Steven D'Aprano  wrote:
>
>> Because Python doesn't have true procedures
>
> What do you mean by "true procedure"? Are you just talking about
> subroutines that don't return any value, i.e. fortran's SUBROUTINE vs.
> FUNCTION?

Ah, the "no true procedure" argument:

 - No true procedure returns a value.

 - That's false. Python's procedures return None.

 - They are not true procedures.


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


Re: What does gc.get_objects() return?

2014-03-13 Thread Jurko Gospodnetić

  Hi.

On 12.3.2014. 23:40, Ian Kelly wrote:

Or is it?


a = 1,2,3
gc.is_tracked(a)

True

gc.collect()

0

gc.is_tracked(a)

False


  Ufff.. nice one :-D

  Best regards,
Jurko Gospodnetić


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


Re: What does gc.get_objects() return?

2014-03-13 Thread Jurko Gospodnetić

  Hi.

On 13.3.2014. 3:54, Terry Reedy wrote:

On 3/12/2014 3:34 PM, Jurko Gospodnetić wrote:


   I was wondering if someone could explain gc.get_objects() in a bit
more detail to me.

   Does it return a list of 'all objects known to Python'? Only some of
them? Which does it return? Which it does not?


[...detailed explanation snipped...]


  Thank you very much for taking the time to explain it all in detail.

  Best regards,
Jurko Gospodnetić

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


Re: DB API question - where is a stored procedure's return value?

2014-03-13 Thread John Gordon
In  Petite Abeille 
 writes:


> Alternatively=85 if it=92s really a function=85 wrap it in a select =
> statement=85 such as:

> select foo() as value from dual

That will get the return value into an SQL variable, but the OP wanted
to know how to fetch it from python code.

-- 
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'.

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


Re: TypeError: can't multiply sequence by non-int of type 'tuple'

2014-03-13 Thread simiasaro
On Friday, February 21, 2014 1:31:53 AM UTC-5, Jaydeep Patil wrote:
> HI,
> 
> 
> 
> I have a tuple. I need to make sqaure of elements of tuple and after that i 
> want add all suared tuple elements for total. When i trying to do it, below 
> error came.
> 
> 
> 
> 
> 
> Code:
> 
> seriesxlist1 = ((0.0,), (0.01,), (0.02,), (0.03,), (0.04,), (0.05,), (0.06,), 
> (0.07,), (0.08,), (0.09,), (0.1,), (0.11,))
> 
> 
> 
> x2 = [x * x for x in seriesxlist1];
> 
> 
> 
> Error:
> 
> Traceback (most recent call last):
> 
>   File "", line 1, in 
> 
> x2 = [x * x for x in seriesxlist1];
> 
> TypeError: can't multiply sequence by non-int of type 'tuple'
> 
> 
> 
> 
> 
> 
> 
> Please suggest me solution.

You may want to identify which of the tuple value you want to multiply, 
especially since the tuple has only one value:
x2 = [x[0] * x[0] for x in seriesxlist1]

Then, you can add the values in the list:
sum(x2)

or the quickest way:
sum(x[0] * x[0] for x in seriesxlist1)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: DB API question - where is a stored procedure's return value?

2014-03-13 Thread Skip Montanaro
Thanks for the responses. We eventually figured out there appears to
be a bug in the latest version of the python-sybase module (at least
in 0.40, probably in 0.39 as well). It was actually detecting
CS_STATUS_RESULT coming from the server and responding appropriately,
however it was assigning the actual status result to a local variable
instead of an attribute of the Cursor instance. Testing a fix now.

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


Re: Deep vs. shallow copy?

2014-03-13 Thread Rustom Mody
On Thursday, March 13, 2014 5:08:22 PM UTC+5:30, Ian wrote:
> On 13/03/2014 03:09, Rustom Mody wrote:
> > Call the action-world the 'imperative' world.
> > Call the value-world the 'functional' world ('declarative' would be
> > better but 'functional' is too entrenched).
> > [Following table meant to be read with fixed (courier) font]
> > | | Imperative | Functional  |
> > | Language entity | Statement  | Expression  |
> > | Denote (and think with) | Action | Value   |
> > | Abstracted into | Procedure  | Function|
> > | Atoms are   | Assignment | Constant/Variable   |
> > | Data Structures | Mutable| Immutable   |
> > | Loop primitive  | Recursion  | Iteration   |
> > | World is| In time| Timeless (Platonic) |
> Small typo I think in that the looping Primitives are switched about?

Heh! I was hesitating to put that line at all: For one thing its a
hackneyed truth in the non-FP community. For another, in practical
Haskell, use of frank recursion is regarded as as sign of programming
immaturity:

http://www.willamette.edu/~fruehr/haskell/evolution.html

So I guess I ended up typing it in the wrong order!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: DB API question - where is a stored procedure's return value?

2014-03-13 Thread Chris Angelico
On Fri, Mar 14, 2014 at 1:43 AM, John Gordon  wrote:
>> select foo() as value from dual
>
> That will get the return value into an SQL variable, but the OP wanted
> to know how to fetch it from python code.

In theory, that should produce a one-row-one-column SELECT result,
which can then be retrieved as such. (I say "in theory" because not
all back-end databases support the "from dual" notation - which, by
the way, I find extremely odd; what's 'dual' about it?)

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


Re: DB API question - where is a stored procedure's return value?

2014-03-13 Thread random832
On Thu, Mar 13, 2014, at 13:01, Chris Angelico wrote:
> On Fri, Mar 14, 2014 at 1:43 AM, John Gordon  wrote:
> >> select foo() as value from dual
> >
> > That will get the return value into an SQL variable, but the OP wanted
> > to know how to fetch it from python code.
> 
> In theory, that should produce a one-row-one-column SELECT result,
> which can then be retrieved as such. (I say "in theory" because not
> all back-end databases support the "from dual" notation - which, by
> the way, I find extremely odd; what's 'dual' about it?)

DUAL is an Oracle thing. It used to have two rows, in order to be used
to duplicate results by doing a cartesian join to it. At some point,
that was removed, but the name stuck.

Most other servers allow SELECT without FROM.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread random832
On Thu, Mar 13, 2014, at 11:28, Rustom Mody wrote:
> Heh! I was hesitating to put that line at all: For one thing its a
> hackneyed truth in the non-FP community. For another, in practical
> Haskell, use of frank recursion is regarded as as sign of programming
> immaturity:

And IIRC Lisp and Scheme have loop macros (whose semantics are more like
a goal seek than iteration IIRC).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: DB API question - where is a stored procedure's return value?

2014-03-13 Thread Chris Angelico
On Fri, Mar 14, 2014 at 4:22 AM,   wrote:
> DUAL is an Oracle thing. It used to have two rows, in order to be used
> to duplicate results by doing a cartesian join to it. At some point,
> that was removed, but the name stuck.
>
> Most other servers allow SELECT without FROM.

Yeah, I usually use PostgreSQL which does. I don't remember what DB2
has. MySQL I think requires either DUAL or an actual real table.

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


Re: Balanced trees

2014-03-13 Thread Ian Kelly
On Mon, Mar 10, 2014 at 11:34 AM, Marko Rauhamaa  wrote:
> The main thing is there are use cases where order is essential. That's
> why I have had to implement the AVL tree in Python myself. No biggie,
> but a C implementation would probably be much faster. Also, a standard
> version would likely be reviewed and tested better and have all Pythonic
> accessors in place.

That last is actually quite easy to achieve, especially using the
built-in ABCs.  Just subclass from collections.MutableMapping and
implement __len__, __iter__, __getitem__, __setitem__ and __delitem__;
and default implementations of the rest will be mixed in for you.  Or
you can override those with optimized implementations.
-- 
https://mail.python.org/mailman/listinfo/python-list


asyncio question

2014-03-13 Thread Joseph L. Casale
I have a portion of code I need to speed up, there are 3 api calls to an 
external system
where the first enumerates a large collection of objects I then loop through 
and perform
two additional api calls each. The first call is instant, the second and third 
per object are
very slow. Currently after accumulating all the data I write the relevant data 
into a database.

I have the ability to hold all this in memory and dump it once fully 
accumulated, so performing
the second and third call in parallel with fixed batches would be great,

I took a look at coroutines and some skeleton code worked fine, but I am not 
sure how to
perform the acquisition in fixed groups like I might for example with 
multiprocessing and
a pool of workers.

Anyone done something like this or have an opinion?

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


Re: Deep vs. shallow copy?

2014-03-13 Thread Steven D'Aprano
On Thu, 13 Mar 2014 07:44:27 -0400, Roy Smith wrote:

> In article <5320e8c9$0$29994$c3e8da3$54964...@news.astraweb.com>,
>  Steven D'Aprano  wrote:
> 
>> Because Python doesn't have true procedures
> 
> What do you mean by "true procedure"?  Are you just talking about
> subroutines that don't return any value, i.e. fortran's SUBROUTINE vs.
> FUNCTION?


Yes. If somebody wants to argue that the word "procedure" can mean 
something else in other contexts, I won't argue, I'll just point out that 
in the context of my post I had just mentioned Pascal procedures, which 
don't return a value.





-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Steven D'Aprano
On Thu, 13 Mar 2014 14:27:48 +0200, Marko Rauhamaa wrote:

> Roy Smith :
> 
>>  Steven D'Aprano  wrote:
>>
>>> Because Python doesn't have true procedures
>>
>> What do you mean by "true procedure"? Are you just talking about
>> subroutines that don't return any value, i.e. fortran's SUBROUTINE vs.
>> FUNCTION?
> 
> Ah, the "no true procedure" argument:
> 
>  - No true procedure returns a value.
> 
>  - That's false. Python's procedures return None.

Are you trolling again? 

I'm sure that you know quite well that Python doesn't have a procedure 
type. It uses a single keyword, def, for creating both functions and 
functions-that-return-None.

We should all agree that functions-that-return-None are used for the same 
purpose as procedures, but they are still functions, and they have a 
return result, namely None. If you don't believe me, believe Python:

py> def func():
... return 42
...
py> def proc():
... pass
...
py> type(func)

py> type(proc)

py> repr(proc())
'None'

In languages with procedures, that last line would be an error (either at 
compile-time, or run-time) since a procedure wouldn't return anything to 
use as argument to repr. But I'm sure that you know that.


>  - They are not true procedures.

Correct. They are functions that return None, rather than a subroutine 
that doesn't have any return value at all. But I'm sure you know that.





-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Chris Angelico
On Fri, Mar 14, 2014 at 10:41 AM, Steven D'Aprano
 wrote:
> Are you trolling again?
>
> I'm sure that you know quite well that Python doesn't have a procedure
> type. It uses a single keyword, def, for creating both functions and
> functions-that-return-None.

I'm going to troll for a moment and give you a function that has no
return value.

def procedure():
raise Exception

But seriously, this is something that some functions do when they need
to distinguish between returning something and not returning anything.
Look at a dictionary's subscripting (which is effectively a function
call):

>>> x={1:2}
>>> x[1]
2
>>> x[3]
Traceback (most recent call last):
  File "", line 1, in 
x[3]
KeyError: 3

It can't return None to indicate "there was no such key in the
dictionary", so it raises instead. There's only one way for a Python
function to not have a return value: it has to not return.

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


Re: Balanced trees

2014-03-13 Thread Steven D'Aprano
On Mon, 10 Mar 2014 19:34:48 +0200, Marko Rauhamaa wrote:

>> With a high level language like Python, using the provided hash table
>> will almost always cream any hand-built tree, no matter how
>> advantageous the data is to the tree.
> 
> The main thing is there are use cases where order is essential. That's
> why I have had to implement the AVL tree in Python myself.

from collections import OrderedDict

gives you a fast, ordered mapping. In this case, the order is that of 
insertion order. If you want sort order instead, for "small" amounts of 
data, say below a million or ten million items, you're likely to have 
acceptable if not superior results by just sorting them items when and as 
needed.

Otherwise, I expect that following the basic technique of OrderedDict, 
and building your data structure from a dict and an associated list, 
keeping the two in sync, will be faster than a pure Python implementation 
of an AVL tree. But of course only testing it will make that clear.

http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/

Modifying the above recipe to keep items in something other than 
insertion order is left as an exercise.




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Ian Kelly
On Thu, Mar 13, 2014 at 5:55 PM, Chris Angelico  wrote:
> I'm going to troll for a moment and give you a function that has no
> return value.
>
> def procedure():
> raise Exception

>>> import dis
>>> dis.dis(procedure)
  2   0 LOAD_GLOBAL  0 (Exception)
  3 RAISE_VARARGS1
  6 LOAD_CONST   0 (None)
  9 RETURN_VALUE
>>> def get_procedure_return_value():
... """Returns the return value of procedure()."""
... return procedure.__code__.co_consts[0]
...
>>> print(get_procedure_return_value())
None

Look, there it is!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Chris Angelico
On Fri, Mar 14, 2014 at 11:08 AM, Ian Kelly  wrote:
> On Thu, Mar 13, 2014 at 5:55 PM, Chris Angelico  wrote:
>> I'm going to troll for a moment and give you a function that has no
>> return value.
>>
>> def procedure():
>> raise Exception
>
 import dis
 dis.dis(procedure)
>   2   0 LOAD_GLOBAL  0 (Exception)
>   3 RAISE_VARARGS1
>   6 LOAD_CONST   0 (None)
>   9 RETURN_VALUE

That's a return value in the same way that exec() has a return value
[1]. If somehow the raise fails, it'll return None.

 def get_procedure_return_value():
> ... """Returns the return value of procedure()."""
> ... return procedure.__code__.co_consts[0]
> ...
 print(get_procedure_return_value())
> None
>
> Look, there it is!

Succeeds by coincidence. From what I can see, *every* CPython function
has const slot 0 dedicated to None. At least, I haven't been able to
do otherwise.

>>> def function(x):
return x*2+1

>>> import dis
>>> dis.dis(function)
  2   0 LOAD_FAST0 (x)
  3 LOAD_CONST   1 (2)
  6 BINARY_MULTIPLY
  7 LOAD_CONST   2 (1)
 10 BINARY_ADD
 11 RETURN_VALUE
>>> function.__code__.co_consts
(None, 2, 1)

Your return value retriever would say it returns None still, but it doesn't.

Trollbridge: you have to pay a troll to cross.

ChrisA

[1] I'm not talking about Python's 'exec' statement, but about the
Unix exec() API, eg execlpe() - see http://linux.die.net/man/3/exec
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: which async framework?

2014-03-13 Thread Chris Withers

On 11/03/2014 19:41, Terry Reedy wrote:


I suspect I'll just end up cross-posting to the various mailing lists,


Bad idea. Post separately if you must.

 > which I hope won't cause too much offence or kick off any flame wars.

It would do both.


Ye of little faith :-P

I've been pleasantly surprised by the succinct, well reasoned and 
respectful replies from each of the communities!


Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
https://mail.python.org/mailman/listinfo/python-list


Re: which async framework?

2014-03-13 Thread Tim Chase
On 2014-03-14 00:25, Chris Withers wrote:
> I've been pleasantly surprised by the succinct, well reasoned and 
> respectful replies from each of the communities!

As one who doesn't lurk on the other lists, is there a nice executive
summary of their responses?

-tkc



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


pickle.dump (obj, conn)

2014-03-13 Thread Pedro Izecksohn
  Shouldn't pickle.dump (obj, conn) raise an Exception if conn is a TCP 
connection that was closed by the remote host?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pickle.dump (obj, conn)

2014-03-13 Thread Ian Kelly
On Thu, Mar 13, 2014 at 4:36 PM, Pedro Izecksohn  wrote:
>   Shouldn't pickle.dump (obj, conn) raise an Exception if conn is a TCP
> connection that was closed by the remote host?

Can you be more specific about what you are doing, what you expect the
result to be, and what the actual result is?

http://www.catb.org/esr/faqs/smart-questions.html#beprecise

If I try using pickle.dump with a socket, I do get an exception, but
perhaps not the one you are looking for:

>>> pickle.dump([1,2,3], sock)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: file must have a 'write' attribute
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Steven D'Aprano
On Fri, 14 Mar 2014 10:55:44 +1100, Chris Angelico wrote:

> On Fri, Mar 14, 2014 at 10:41 AM, Steven D'Aprano
>  wrote:
>> Are you trolling again?
>>
>> I'm sure that you know quite well that Python doesn't have a procedure
>> type. It uses a single keyword, def, for creating both functions and
>> functions-that-return-None.
> 
> I'm going to troll for a moment and give you a function that has no
> return value.

Heh, you're not trolling. You're just trying to be pedantic. But not 
pedantic enough... 


> def procedure():
> raise Exception

This does have a return result, and it is None. It's just that the 
function never reaches the return, it exits early via an exception.

py> from dis import dis
py> dis(procedure)
  2   0 LOAD_GLOBAL  0 (Exception)
  3 RAISE_VARARGS1
  6 LOAD_CONST   0 (None)
  9 RETURN_VALUE

That *may* be able to be optimized away by a smarter compiler, or perhaps 
it can't be. There may be some technical reason why code objects have to 
end with a return no matter what:

py> dis(compile("func()", "", "exec"))
  1   0 LOAD_NAME0 (func)
  3 CALL_FUNCTION0 (0 positional, 0 keyword pair)
  6 POP_TOP
  7 LOAD_CONST   0 (None)
 10 RETURN_VALUE

Or maybe it's just an optimization that nobody has bothered with since 
the benefit is so trivial. But either way, all callables (sub-routines) 
in Python are functions, i.e. in principle they could, or should, return 
a result, even if in practice some of them don't. There is no callable 
type which lacks the ability to return a result.

Naturally they may not actually return a result if they never exit:

def this_is_a_function():
while 1:
pass
return "You'll never see this!"

or if they exit via an exception:

def also_a_function():
if 1:
raise ValueError
return "You'll never see this either!"

or if they just kill the running Python environment stone dead:

def still_a_function():
import os
os._exit(1)
return "Have I beaten this dead horse enough?"


> But seriously, this is something that some functions do when they need
> to distinguish between returning something and not returning anything.
> Look at a dictionary's subscripting (which is effectively a function
> call):
> 
 x={1:2}
 x[1]
> 2
 x[3]
> Traceback (most recent call last):
>   File "", line 1, in 
> x[3]
> KeyError: 3

Yes, I'm aware that Python functions can raise exceptions :-)

 
> It can't return None to indicate "there was no such key in the
> dictionary", so it raises instead. There's only one way for a Python
> function to not have a return value: it has to not return.

Exactly my point. Functions return a value, and None is a value; 
procedures return, but not with a value. Python has the former, but not 
the later. Instead, we make do with the convention that something which 
is *intended* to be used as a procedure should return None to signal 
that, and the caller should just ignore the return result.




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Steven D'Aprano
On Fri, 14 Mar 2014 11:22:43 +1100, Chris Angelico wrote:

> Trollbridge: you have to pay a troll to cross.

Heh :-)

But seriously, there is a distinction to be made between returning from a 
sub-routine, and returning from a sub-routine with a return result. There 
may be alternative methods of exiting the sub-routine, e.g. a GOTO or 
COMEFROM that jumps outside of the function. Exceptions are a form of 
safe, limited GOTO: they can only jump out of a function, not into the 
middle of an arbitrary chunk of code, and they clean up the call stack 
when they jump. But these alternative methods are not what people 
consider *returning* from a sub-routine.

Unless they're trolling :-)




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Rustom Mody
On Friday, March 14, 2014 5:11:03 AM UTC+5:30, Steven D'Aprano wrote:
> On Thu, 13 Mar 2014 14:27:48 +0200, Marko Rauhamaa wrote:

> > Roy Smith :
> >>  Steven D'Aprano  wrote:
> >>> Because Python doesn't have true procedures
> >> What do you mean by "true procedure"? Are you just talking about
> >> subroutines that don't return any value, i.e. fortran's SUBROUTINE vs.
> >> FUNCTION?
> > Ah, the "no true procedure" argument:
> >  - No true procedure returns a value.
> >  - That's false. Python's procedures return None.

> Are you trolling again? 

> I'm sure that you know quite well that Python doesn't have a procedure 
> type. It uses a single keyword, def, for creating both functions and 
> functions-that-return-None.

> We should all agree that functions-that-return-None are used for the same 
> purpose as procedures, but they are still functions, and they have a 
> return result, namely None. If you don't believe me, believe Python:

> py> def func():
> ... return 42
> ...
> py> def proc():
> ... pass
> ...
> py> type(func)
> py> type(proc)
> py> repr(proc())
> 'None'

> In languages with procedures, that last line would be an error (either at 
> compile-time, or run-time) since a procedure wouldn't return anything to 
> use as argument to repr. But I'm sure that you know that.

> >  - They are not true procedures.

> Correct. They are functions that return None, rather than a subroutine 
> that doesn't have any return value at all. But I'm sure you know that.

I believe that you, Marko (and I) are saying exactly the same thing:

Wear language-lawyer hat: 
Python has no procedures -- just functions which may return None

Wear vanilla programmer hat:
The concept (Pascal) procedure is simulated by function-returning-None
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Balanced trees

2014-03-13 Thread Dan Stromberg
On Thu, Mar 13, 2014 at 4:57 PM, Steven D'Aprano
 wrote:
> On Mon, 10 Mar 2014 19:34:48 +0200, Marko Rauhamaa wrote:
>
>>> With a high level language like Python, using the provided hash table
>>> will almost always cream any hand-built tree, no matter how
>>> advantageous the data is to the tree.
>>
>> The main thing is there are use cases where order is essential. That's
>> why I have had to implement the AVL tree in Python myself.
>
> from collections import OrderedDict
>
> gives you a fast, ordered mapping. In this case, the order is that of
> insertion order. If you want sort order instead, for "small" amounts of
> data, say below a million or ten million items, you're likely to have
> acceptable if not superior results by just sorting them items when and as
> needed.

This is one of my pet things.

Sorting the same (slightly tweaked) data inside of a tight loop is
rarely a good idea - despite the fact that the "sort" itself tends to
be O(n) with Python's rather awesome builtin sorting algorithm.  This
is because sorting inside a loop tends to yield O(n^2) algorithms, or
even O((n^2)*logn).

But if you're sorting once at the end of whatever other processing,
sorting is great.  It's (still) O(nlogn), but it's got a terrific
constant.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deep vs. shallow copy?

2014-03-13 Thread Steven D'Aprano
On Thu, 13 Mar 2014 19:57:53 -0700, Rustom Mody wrote:

> I believe that you, Marko (and I) are saying exactly the same thing:

I believe that you and I are saying practically the same thing.


> Wear language-lawyer hat:
> Python has no procedures -- just functions which may return None

Almost. Functions (or methods) can return None as a regular value, e.g. 
the re.match and re.search functions return a MatchObject if there is a 
match, and None if there is not. Here, the fact the function returns None 
is nothing special -- it could have return 0, or -1, or "Surprise!" if 
the function author had wanted, the important thing is that you use it as 
a function. Normally you call the function for it's return result, even 
if that result happens to be None.

On the other hand, Python also has functions/methods which you call for 
their side-effects, not for their return result. In Pascal, Fortran and 
C, for example, the language provides a special type of subroutine that 
can only be called for it's side-effects. It is common to call these 
subroutines "procedures", and Python does not have them. We only have the 
convention that if a function is intended to be called for it's side-
effects (a procedure), it should return None.


> Wear vanilla programmer hat:
> The concept (Pascal) procedure is simulated by function-returning-None

Yes, agreed on this one.



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


Re: Balanced trees

2014-03-13 Thread Steven D'Aprano
On Thu, 13 Mar 2014 20:12:41 -0700, Dan Stromberg wrote:

> Sorting the same (slightly tweaked) data inside of a tight loop is
> rarely a good idea - despite the fact that the "sort" itself tends to be
> O(n) with Python's rather awesome builtin sorting algorithm.  This is
> because sorting inside a loop tends to yield O(n^2) algorithms, or even
> O((n^2)*logn).

I agree with you except for the word "rarely". It depends on how much 
data you have, and in my experience most people think that N = 100 is a 
lot :-)

Remember that Big Oh doesn't say anything about the constant of 
proportionality. If you have one function that behaves like O(N) but with 
a constant factor of 1000, and another function that behaves like 
O(N**2) with a constant factor of 0.001, the second, theoretically 
slower, function will actually be faster for N < one million.

So *in practice*, a solution that involves a theoretically worse Big Oh 
function but a significantly lower constant factor may turn out to be 
better for all the values you care about. Given the difference in speed 
between Python's built-ins, and the code you can write yourself in pure 
Python, it is normally better to push as much work as you can onto the 
built-ins.

And of course, we should not make assumptions as to what is faster and 
what is slower. After 15+ years, I'm still surprised about what ends up 
being faster in Python.


> But if you're sorting once at the end of whatever other processing,
> sorting is great.  It's (still) O(nlogn), but it's got a terrific
> constant.

Exactly!



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


Re: Deep vs. shallow copy?

2014-03-13 Thread Mark Lawrence

On 14/03/2014 00:22, Chris Angelico wrote:


Trollbridge: you have to pay a troll to cross.



And you mustn't be afraid of the Black Night?

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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