[issue17005] Add a topological sort algorithm

2020-08-26 Thread Paddy McCarthy


Paddy McCarthy  added the comment:

Please ignore my earlier Message-id 
<1598493715.04.0.06462575371.issue17...@roundup.psfhosted.org>.

I missed a dependency in cutting down a larger example. Sorry.

--

___
Python tracker 
<https://bugs.python.org/issue17005>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue17005] Add a topological sort algorithm

2020-08-26 Thread Paddy McCarthy

Paddy McCarthy  added the comment:

I've been playing with Python 3.9.0rc1 and was looking at a particular graph to 
see when it released tasks for processing.
I ran the following code:

from functools import reduce
from pprint import pprint as pp
from collections import defaultdict
from graphlib import TopologicalSorter
from heapq import heapify, heappush, heappop, heappushpop

print("""
###
### TASK DEPENDENCY 
###

A -> B -> C
↓↓↓
D -> E -> F
""")
graph3 = {"A": {"B", "D"},
  "B": {"C", "E"},
  "C": {"F"},
  "D": {"E"},
  "E": {"F"},
  }
tasktime = {task: 2 for task in 'ABCDEF'}


def simulate(graph, tasktime):
print("\n## NEW SIMULATION")
t = 0
heap = []
heapify(heap)
print("\n# Task runtimes:")
for node, tm in tasktime.items():
print(f"  {node}: {tm}")

print("\n# OUTPUT. (:> for task start times, <: for stop times).\n")
ts = TopologicalSorter(graph)
ts.prepare()
while ts.is_active():
for node in ts.get_ready():
finish = t + tasktime[node]
heappush(heap, (finish, node))
print(f"{'  ' * t}{node}:> @{t}")
t, node = heappop(heap)
print(f"{'  ' * t}{node}<: @{t}")
ts.done(node)

simulate(graph3, tasktime)
tasktime['C'] = 1
simulate(graph3, tasktime)


I got the following output:


###
### TASK DEPENDENCY 
###

A -> B -> C
↓↓↓
D -> E -> F


## NEW SIMULATION

# Task runtimes:
  A: 2
  B: 2
  C: 2
  D: 2
  E: 2
  F: 2

# OUTPUT. (:> for task start times, <: for stop times).

F:> @0
F<: @2
C:> @2
E:> @2
C<: @4
E<: @4
B:> @4
D:> @4
B<: @6
D<: @6
A:> @6
A<: @8

## NEW SIMULATION

# Task runtimes:
  A: 2
  B: 2
  C: 1
  D: 2
  E: 2
  F: 2

# OUTPUT. (:> for task start times, <: for stop times).

F:> @0
F<: @2
C:> @2
E:> @2
  C<: @3
E<: @4
B:> @4
D:> @4
B<: @6
D<: @6
A:> @6
A<: @8
>>> 


Note that in the second simulation, C finish, but B isn't then immediately 
started. I have my own code that also works like this but it isn't optimal.

Thanks guys.

--
nosy: +Paddy McCarthy

___
Python tracker 
<https://bugs.python.org/issue17005>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33235] Better help text for dict.setdefault

2018-04-06 Thread Paddy McCarthy

New submission from Paddy McCarthy <paddy3...@gmail.com>:

Hi, I was answering some question and used dict.setdefault as part of the 
solution and posted the help() on it as part of my answer.

The help was this:

In [15]: help(mapper.setdefault)
Help on built-in function setdefault:

setdefault(...) method of builtins.dict instance
D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

This seems the wrong way around. Is it not better expressed as

D.setdefault(k[,d]) -> set D[k]=d if k not in D and then D.get(k,d)

--
assignee: docs@python
components: Documentation
messages: 315015
nosy: Paddy McCarthy, docs@python
priority: normal
severity: normal
status: open
title: Better help text for dict.setdefault
type: enhancement
versions: Python 3.6

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33235>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24914] Python: Not just OO style but this is not mentioned on python.org or in FAQ

2015-08-23 Thread Paddy McCarthy

Paddy McCarthy added the comment:

OK, here's a suggested re-wording:

Python is an interpreted, interactive, object-oriented programming language 
that also supports programming in procedural and functional styles. It 
incorporates modules, exceptions, dynamic typing, very high level dynamic data 
types, and classes; but statements are not required to be contained in class 
definitions and functions are first class - being able to be composed, returned 
from other functions and be a member of other container types such as dicts, 
sets, and lists.

If that is too long,an alternative would be to delete my addition to the last 
sentence from ; but classes are not ... Leaving it to the reader to research 
just how procedural and functional Python can be. (Where should a programmer, 
but newbie-to-Python look)?

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24914
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue24914] Python: Not just OO style but this is not mentioned on python.org or in FAQ

2015-08-22 Thread Paddy McCarthy

New submission from Paddy McCarthy:

Just read 
http://www.ibmsystemsmag.com/ibmi/developer/general/different-world-python/?utm_campaign=ibm-enewsutm_medium=emailutm_source=ibmi-jul22-2015?utm_content=exclusive1-headline

It states that they could have had an officially supported version of Python on 
that IBM platform much earlier but for this:

 The second was that everything we read on Python, and all the examples we 
 encountered, led us to believe that it was a completely object oriented (OO) 
 language

They may have used it earlier had they known then that Python can be written in 
a procedural style they having no love of Java's OO, but being able to use PHP 
and access PHP's OO bits.

Looking again on python.org, the examples are not OO, but when you delve down, 
say to the FAQ - it gives the mistaken impression that OO is the _only_ style 
of programming supported: 
https://docs.python.org/2/faq/general.html#what-is-python

Somehow we need to explain that OO is an implementation style, but the language 
allows code to be written in just as much - or as little, of 
proceedural/OO/functional styles as the programmer is comfortable with.

--
assignee: docs@python
components: Documentation
messages: 248987
nosy: Paddy McCarthy, docs@python
priority: normal
severity: normal
status: open
title: Python: Not just OO style but this is not mentioned on python.org or in 
FAQ
type: enhancement

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24914
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Need opinions on P vs NP

2015-04-18 Thread Paddy
On Saturday, 18 April 2015 08:09:06 UTC+1, wxjm...@gmail.com  wrote:
 Le samedi 18 avril 2015 03:19:40 UTC+2, Paddy a écrit :
  Having just seen Raymond's talk on Beyond PEP-8 here: 
  https://www.youtube.com/watch?v=wf-BqAjZb8M, it reminded me of my own 
  recent post where I am soliciting opinions from non-newbies on the relative 
  Pythonicity of different versions of a routine that has non-simple array 
  manipulations.
  
  The blog post: 
  http://paddy3118.blogspot.co.uk/2015/04/pythonic-matrix-manipulation.html
  
  The first, (and original), code sample:
  
  def cholesky(A):
  L = [[0.0] * len(A) for _ in range(len(A))]
  for i in range(len(A)):
  for j in range(i+1):
  s = sum(L[i][k] * L[j][k] for k in range(j))
  L[i][j] = sqrt(A[i][i] - s) if (i == j) else \
(1.0 / L[j][j] * (A[i][j] - s))
  return L
  
  
  The second equivalent code sample:
  
  def cholesky2(A):
  L = [[0.0] * len(A) for _ in range(len(A))]
  for i, (Ai, Li) in enumerate(zip(A, L)):
  for j, Lj in enumerate(L[:i+1]):
  s = sum(Li[k] * Lj[k] for k in range(j))
  Li[j] = sqrt(Ai[i] - s) if (i == j) else \
(1.0 / Lj[j] * (Ai[j] - s))
  return L
  
  
  The third:
  
  def cholesky3(A):
  L = [[0.0] * len(A) for _ in range(len(A))]
  for i, (Ai, Li) in enumerate(zip(A, L)):
  for j, Lj in enumerate(L[:i]):
  #s = fsum(Li[k] * Lj[k] for k in range(j))
  s = fsum(Lik * Ljk for Lik, Ljk in zip(Li, Lj[:j]))
  Li[j] = (1.0 / Lj[j] * (Ai[j] - s))
  s = fsum(Lik * Lik for Lik in Li[:i])
  Li[i] = sqrt(Ai[i] - s)
  return L
  
  My blog post gives a little more explanation, but I have yet to receive any 
  comments on relative Pythonicity.
 
 
 
 def cholesky999(A):
 n = len(A)
 L = [[0.0] * n for i in range(n)]
 for i in range(n):
 for j in range(i+1):
 s = 0.0
 for k in range(j):
 s += L[i][k] * L[j][k]
 if i == j:
 L[i][j] = sqrt(A[i][i] - s)
 else:
 L[i][j] = (1.0 / L[j][j] * (A[i][j] - s))
 return L
 
 Simple, clear, logical, consistant.

And so most Pythonic? Maybe so.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need opinions on P vs NP

2015-04-17 Thread Paddy
On Saturday, 18 April 2015 03:34:57 UTC+1, Ian  wrote:
 On Fri, Apr 17, 2015 at 7:19 PM, Paddy paddy3...@..l.com wrote:
  Having just seen Raymond's talk on Beyond PEP-8 here: 
  https://www.youtube.com/watch?v=wf-BqAjZb8M, it reminded me of my own 
  recent post where I am soliciting opinions from non-newbies on the relative 
  Pythonicity of different versions of a routine that has non-simple array 
  manipulations.
 
  The blog post: 
  http://paddy3118.blogspot.co.uk/2015/04/pythonic-matrix-manipulation.html
 
  The first, (and original), code sample:
 
  def cholesky(A):
  L = [[0.0] * len(A) for _ in range(len(A))]
  for i in range(len(A)):
  for j in range(i+1):
  s = sum(L[i][k] * L[j][k] for k in range(j))
  L[i][j] = sqrt(A[i][i] - s) if (i == j) else \
(1.0 / L[j][j] * (A[i][j] - s))
  return L
 
 
  The second equivalent code sample:
 
  def cholesky2(A):
  L = [[0.0] * len(A) for _ in range(len(A))]
  for i, (Ai, Li) in enumerate(zip(A, L)):
  for j, Lj in enumerate(L[:i+1]):
  s = sum(Li[k] * Lj[k] for k in range(j))
  Li[j] = sqrt(Ai[i] - s) if (i == j) else \
(1.0 / Lj[j] * (Ai[j] - s))
  return L
 
 
  The third:
 
  def cholesky3(A):
  L = [[0.0] * len(A) for _ in range(len(A))]
  for i, (Ai, Li) in enumerate(zip(A, L)):
  for j, Lj in enumerate(L[:i]):
  #s = fsum(Li[k] * Lj[k] for k in range(j))
  s = fsum(Lik * Ljk for Lik, Ljk in zip(Li, Lj[:j]))
  Li[j] = (1.0 / Lj[j] * (Ai[j] - s))
  s = fsum(Lik * Lik for Lik in Li[:i])
  Li[i] = sqrt(Ai[i] - s)
  return L
 
  My blog post gives a little more explanation, but I have yet to receive any 
  comments on relative Pythonicity.
 
 I prefer the first version. You're dealing with mathematical formulas
 involving matrices here, so subscripting seems appropriate, and
 enumerating out rows and columns just feels weird to me.
 
 That said, I also prefer how the third version pulls the last column
 of each row out of the inner loop instead of using a verbose
 conditional expression that you already know will be false for every
 column except the last one. Do that in the first version, and I think
 you've got it.

But shouldn't the maths transcend the slight change in representation? A 
programmer in the J language might have a conceptually neater representation of 
the same thing due to its grounding in arrays (maybe) and for a J 
representation it would become J-thonic. In Python, it is usual to iterate over 
collections and also to use enumerate where we must have indices. 

Could it be that there is a also a strong pull in the direction of using 
indices because that is what is predominantly given in the way matrix maths is 
likely to be expressed mathematically? A case of TeX likes indices so we 
should too?
-- 
https://mail.python.org/mailman/listinfo/python-list


Need opinions on P vs NP

2015-04-17 Thread Paddy
Having just seen Raymond's talk on Beyond PEP-8 here: 
https://www.youtube.com/watch?v=wf-BqAjZb8M, it reminded me of my own recent 
post where I am soliciting opinions from non-newbies on the relative 
Pythonicity of different versions of a routine that has non-simple array 
manipulations.

The blog post: 
http://paddy3118.blogspot.co.uk/2015/04/pythonic-matrix-manipulation.html

The first, (and original), code sample:

def cholesky(A):
L = [[0.0] * len(A) for _ in range(len(A))]
for i in range(len(A)):
for j in range(i+1):
s = sum(L[i][k] * L[j][k] for k in range(j))
L[i][j] = sqrt(A[i][i] - s) if (i == j) else \
  (1.0 / L[j][j] * (A[i][j] - s))
return L


The second equivalent code sample:

def cholesky2(A):
L = [[0.0] * len(A) for _ in range(len(A))]
for i, (Ai, Li) in enumerate(zip(A, L)):
for j, Lj in enumerate(L[:i+1]):
s = sum(Li[k] * Lj[k] for k in range(j))
Li[j] = sqrt(Ai[i] - s) if (i == j) else \
  (1.0 / Lj[j] * (Ai[j] - s))
return L


The third:

def cholesky3(A):
L = [[0.0] * len(A) for _ in range(len(A))]
for i, (Ai, Li) in enumerate(zip(A, L)):
for j, Lj in enumerate(L[:i]):
#s = fsum(Li[k] * Lj[k] for k in range(j))
s = fsum(Lik * Ljk for Lik, Ljk in zip(Li, Lj[:j]))
Li[j] = (1.0 / Lj[j] * (Ai[j] - s))
s = fsum(Lik * Lik for Lik in Li[:i])
Li[i] = sqrt(Ai[i] - s)
return L

My blog post gives a little more explanation, but I have yet to receive any 
comments on relative Pythonicity.
-- 
https://mail.python.org/mailman/listinfo/python-list


[issue10395] new os.path function to extract common prefix based on path components

2015-03-20 Thread Paddy McCarthy

Paddy McCarthy added the comment:

Can we now:
 1. Move os.path.commonprefix to str.commonprefix or string.commonprefix
 2. Deprecate the use of os.path.commonprefix
 3. Add os.path.commonpath
 4. Update the documentation.

This seems to have lingered for too long and yet people have been willing to do 
the work it seems (from 1999).

--
nosy: +Paddy McCarthy

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue10395
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23695] idiom for clustering a data series into n-length groups

2015-03-18 Thread Paddy McCarthy

Paddy McCarthy added the comment:

I like R. David Murray's suggestion, but I am also aware of how it works and so 
cannot judge how it would look to the intermediate Python programmer who knows 
iterators and zip, but is new to this grouper; (who I think should be the 
target audience).

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23695
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23695] idiom for clustering a data series into n-length groups

2015-03-17 Thread Paddy McCarthy

Paddy McCarthy added the comment:

Hmmm. It seems that the problem isn't to do with the fact that it works, or how 
to apply it; the problem is with *how* it works. 

Making it an idiom means that too many will use it without knowing why it works 
which could lead to later maintenance issues. I think a better description of 
how it works may be needed for the docs.

Unfortunately my description of the how at 
http://paddy3118.blogspot.co.uk/2012/12/that-grouping-by-zipiter-trick-explained.html
 was not written with the docs in mind, but you are welcome to any part or the 
whole, for the Python docs.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23695
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue23695] idiom for clustering a data series into n-length groups

2015-03-17 Thread Paddy McCarthy

New submission from Paddy McCarthy:

In the zip section of the documentation, e.g. 
https://docs.python.org/3/library/functions.html#zip There is mention of an 
idiom for clustering a data series into n-length groups that I seem to only 
come across when people are explaining how it works on blog entries such as the 
three mentioned here: 
http://www.reddit.com/r/programming/comments/2z4rv4/a_function_for_partitioning_python_arrays/cpfvwun?context=3

It is not a straight-forward bit of code and so I think it should either be 
explained in more detail in the documentation or removed as an idiom, or I 
guess it could be encapsulated in a function and added to the stdlib.

--
assignee: docs@python
components: Documentation
messages: 238365
nosy: Paddy McCarthy, docs@python
priority: normal
severity: normal
status: open
title: idiom for clustering a data series into n-length groups
type: enhancement
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23695
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Bug in timsort!?

2015-02-25 Thread Paddy
On Wednesday, 25 February 2015 00:08:32 UTC, Chris Angelico  wrote:
 On Wed, Feb 25, 2015 at 10:50 AM, Skip Montanaro
 skip.mo...@gmail.com wrote:
  Even if/when we get to the point where machines can hold an array of
  2**49 elements, I suspect people won't be using straight Python to
  wrangle them.
 
snip
 
 Would it be sufficient to stick a comment into the source saying This
 may have problems with lists in excess of 2**49 elements, and leave
 it until that's actually likely to happen?
 
 ChrisA

If we are given a proven fix with little downside then if we are to touch the 
source then we should MAKE THE FIX. To do otherwise would send the wrong signal 
to those that depend on the perceived integrity of the C-Python developers.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Natural use of cmp= in sort

2014-11-12 Thread Paddy
On Tuesday, 11 November 2014 18:07:27 UTC, Ian  wrote:
 The example that I posted is one that I recall being brought up on
 this list in the past, but I don't have a link for you.

THanks Ian for your help in this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Natural use of cmp= in sort

2014-11-11 Thread Paddy
On Tuesday, 11 November 2014 09:07:14 UTC, Ian  wrote:
 On Tue, Nov 11, 2014 at 12:44 AM, Paddy paddyxxx-at-xmail.com wrote:
  Thanks Ian. The original author states ...and it is sure that the given 
  inputs will give an output, i.e., the inputs will always be valid., which 
  could be taken as meaning that all inputs are sufficient, well formed, and 
  contain all relations as their first example does.
 
 Well, I brought it up because the start of that sentence is There can
 be multiple inequalities as answer but I need any one which is
 correct The only way there would be more than one correct answer
 would be if the inputs were only partially ordered. I take the second
 part of the sentence as meaning only that the input can be safely
 assumed to be consistent.
 
  Yes, I knew that there are cases where a cmp function is more natural than 
  key; the idea is to squirrel out a few. We have already made the, (well 
  reasoned in my opinion), decision to go down the key= route in Python 3. I 
  also like to track where my algorithms might originally map to cmp=. (It is 
  not often).
 
 Basically any time you have a comparison that isn't easily expressed
 by mapping the values to some bunch of ordered objects. 

Yep. I want to track when this comes up for me and others during their normal 
programming rather than in examples made to highlight the issue.

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


Natural use of cmp= in sort

2014-11-10 Thread Paddy
Hi, I do agree with 
 Raymond H. about the relative merits of cmp= and key= in 
sort/sorted, but I decided to also not let natural uses of cmp= pass silently.

In answering this question, http://stackoverflow.com/a/26850434/10562 about 
ordering subject to inequalities it seemed natural to use the cmp= argument of 
sort rather than key=.

The question is about merging given inequalities to make 1 inequality such that 
the inequalities also stays true.


Here is a copy of my code:

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on 
win32
Type copyright, credits or license() for more information.
 ineq = f4  f2  f3
f4  f1  f3
f4  f2  f1
f2  f1  f3
 print(ineq)
f4  f2  f3
f4  f1  f3
f4  f2  f1
f2  f1  f3
 greater_thans, all_f = set(), set()
 for line in ineq.split('\n'):
tokens = line.strip().split()[::2]
for n, t1 in enumerate(tokens[:-1]):
for t2 in tokens[n+1:]:
greater_thans.add((t1, t2))
all_f.add(t1)
all_f.add(t2)


 sorted(all_f, cmp=lambda t1, t2: 0 if t1==t2 else 
...(1 if (t1, t2) not in greater_thans else -1))
['f4', 'f2', 'f1', 'f3']
 

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


Re: Natural use of cmp= in sort

2014-11-10 Thread Paddy
On Monday, 10 November 2014 19:44:39 UTC, Ian  wrote:
 On Mon, Nov 10, 2014 at 12:19 PM, Peter Otten xxx@yyy wrote:
  I'm not sure this works. I tried:
 
 Here's a simpler failure case.
 
  ineq = f2  f3
 ... f3  f1
 
 [Previously posted code elided]
 
  greater_thans
 set([('f3', 'f1'), ('f2', 'f3')])
  sorted(all_f, cmp=lambda t1, t2: 0 if t1==t2 else
 ... (1 if (t1, t2) not in greater_thans else -1))
 ['f1', 'f2', 'f3']
 
 Note that the greater_thans set is missing the implication by
 transitivity that f2  f1, so the given cmp function would
 inconsistently return -1 for both comparisons cmp('f1', 'f2') and
 cmp('f2', 'f1').

Thanks. I will look into this...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Natural use of cmp= in sort

2014-11-10 Thread Paddy
On Monday, 10 November 2014 18:45:15 UTC, Paddy  wrote:
 Hi, I do agree with   
Raymond H. about the relative merits of cmp= and key= in 
 sort/sorted, but I decided to also not let natural uses of cmp= pass silently.
 
 In answering this question, http://stackoverflow.com/a/26850434/10562 about 
 ordering subject to inequalities it seemed natural to use the cmp= argument 
 of sort rather than key=.
 
 The question is about merging given inequalities to make 1 inequality such 
 that the inequalities also stays true.
 
 

Thanks Peter, Ian. I have modified my code to expand transitive relations and 
ask you to view it on stackoverflow via the original link (as posting code on 
newsgroups is an ugly hack).

My main reason for the post to c.l.p remains though; it seems like a *natural* 
use of the cmp= comparator function to sorted rather than using key= .

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


Re: Natural use of cmp= in sort

2014-11-10 Thread Paddy
On Tuesday, 11 November 2014 06:37:18 UTC, Ian  wrote:
 On Mon, Nov 10, 2014 at 8:09 PM, Paddy paddyxxx-at-xmail.com wrote:
  On Monday, 10 November 2014 18:45:15 UTC, Paddy  wrote:
  Hi, I do agree with
Raymond H. about the relative merits of cmp= and key= in 
  sort/sorted, but I decided to also not let natural uses of cmp= pass 
  silently.
 
  In answering this question, http://stackoverflow.com/a/26850434/10562 
  about ordering subject to inequalities it seemed natural to use the cmp= 
  argument of sort rather than key=.
 
  The question is about merging given inequalities to make 1 inequality such 
  that the inequalities also stays true.
 
 
 
  Thanks Peter, Ian. I have modified my code to expand transitive relations 
  and ask you to view it on stackoverflow via the original link (as posting 
  code on newsgroups is an ugly hack).
 
 You still run into trouble though if the given inequalities don't
 provide enough information for a total ordering. E.g.:
 
  '  '.join(extract_relations(f4  f1
 ... f2  f3))
 'f1  f2  f3  f4'
 
 By adding some debugging prints, we can see what cmp calls were made
 by the sort routine and what the results were:
 
 cmp('f2', 'f1') - 1
 cmp('f3', 'f2') - 1
 cmp('f4', 'f3') - 1
 
 There is no information about the relative order of f2 and f1, so the
 cmp function just returns 1 there.
 f2 is known to be greater than f3, so that call correctly returns 1.
 There is again no information about the relative order of f4 and f3,
 so it again just returns 1. However, this is inconsistent with the
 first comparison that placed f1  f2, because it implies that f1  f4.
 
 As you can see, giving an inconsistent cmp function to sort produces
 bogus results. If you only have a partial ordering of the inputs, you
 need to make sure that the cmp function you provide is consistent with
 *some* total ordering.
 
 Another issue is that your expand_transitive_relations function is I
 think O(n**3 log n), which looks unattractive compared to the O(n**2)
 topological sort given in the other answers. Another advantage of the
 topological sort is that it will detect if the graph is cyclic (i.e.
 the input data itself is inconsistent), rather than just return a
 bogus output.
 
  My main reason for the post to c.l.p remains though; it seems like a 
  *natural* use of the cmp= comparator function to sorted rather than using 
  key= .
 
 There are cases where a cmp function is more natural than a key
 function, but for these we have the functools.cmp_to_key adapter.

Thanks Ian. The original author states ...and it is sure that the given inputs 
will give an output, i.e., the inputs will always be valid., which could be 
taken as meaning that all inputs are sufficient, well formed, and contain all 
relations as their first example does.

In that case, expand_transitive_relations is not even needed. Lets say it isn't 
for the sake of argument, then we are left with the direct use of cmp= versus a 
conversion to a key= function.

It seems to me that *in this case* the cmp= function naturally flows from the 
solution algorithm and that cmp_to_key is less so.

Yes, I knew that there are cases where a cmp function is more natural than key; 
the idea is to squirrel out a few. We have already made the, (well reasoned in 
my opinion), decision to go down the key= route in Python 3. I also like to 
track where my algorithms might originally map to cmp=. (It is not often).

My only other case of this type is here: 
http://stackoverflow.com/questions/15797120/can-this-cmp-function-be-better-written-as-a-key-for-sorted.
-- 
https://mail.python.org/mailman/listinfo/python-list


[issue16580] Add examples to int.to_bytres and int.from_bytes

2012-12-09 Thread Paddy McCarthy

Paddy McCarthy added the comment:

On 06/12/2012 14:31, Ezio Melotti wrote:
 Ezio Melotti added the comment:

 I agree.  The examples in the doc seem clear to me, whereas the ones you 
 proposed are not as clear.  Do you think there's something that they don't 
 currently cover that should be added?

 --
 nosy: +ezio.melotti

 ___
 Python tracker rep...@bugs.python.org
 http://bugs.python.org/issue16580
 ___

First, Thanks Ezio and Andrew for your replies.

My problem was that when working on bitcoin address validation I saw 
code that was shifting and 'ing with 0xFF to convert to multiple bytes 
and half remembered that there might be a Python function to do that. On 
finding the .to_bytes method and its parameter big or little, the 
only way I had of working out which to use was to try each until I found 
out which worked.

I therefore thought that what would have helped me was code that showed 
the equivalent expanded Python for the method in a similar way to what 
is done for some of the itertools functions etc.

If we split my request into two:

 1. Is such extra explanation necessary.
 2. Is my specific code that extra explanation.

I can work on the code a bit more.
Have I persuaded you that an extra explanation is necessary?

Thanks, Paddy.

P.S. I guess what is currently present shows the result of the methods 
but nothing on how it could be generated. I am stating that the 
generation can aid comprehension.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16580
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16580] Add examples to int.to_bytres and int.from_bytes

2012-12-09 Thread Paddy McCarthy

Paddy McCarthy added the comment:

On 09/12/2012 10:55, Ezio Melotti wrote:
 Ezio Melotti added the comment:

 Usually we add plain Python equivalents when they are simple enough that the 
 code equivalent is as understandable as the prose or more (see for example 
 http://docs.python.org/3/library/functions.html#all, or the itertools 
 functions you mentioned).
 For this case I think it would help if you presented an equivalent function, 
 e.g.:

 def to_bytes(n, length, order):
  if order == 'little':
  return bytes((n  i*8)  0xff for i in range(length))
  elif order == 'big':
  return bytes((n  i*8)  0xff for i in reversed(range(length)))

 or even:

 def to_bytes(n, length, order):
  indexes = range(length) if order == 'little' else reversed(range(length))
  return bytes((n  i*8)  0xff for i in indexes)

 This is also done for 
 http://docs.python.org/3.3/library/stdtypes.html#int.bit_length just above 
 to/from_bytes, so it might be a good addition.
 If this is done, the equivalent function can also be added to the test suite, 
 so we can verify that it's indeed equivalent.

 --
 keywords: +easy
 stage:  - needs patch
 versions: +Python 2.7, Python 3.2, Python 3.4

 ___
 Python tracker rep...@bugs.python.org
 http://bugs.python.org/issue16580
 ___

The second example looks great. I like the dual use for testing too and 
will try and remember both the next time I find I have ireas about the 
documentation.

Thanks guys. It's appreciated!

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16580
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue16580] Add examples to int.to_bytres and int.from_bytes

2012-11-29 Thread Paddy McCarthy

New submission from Paddy McCarthy:

http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes
 and 
http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes
 would benefit from an example showing what they do based on simpler coding.

I have such an example that I wrote here: 
http://paddy3118.blogspot.co.uk/2012/11/some-identities-for-python-inttobytes.html
 that you can use.

I.e.

 n = 2491969579123783355964723219455906992268673266682165637887
 length = 25
 n2bytesbig= n.to_bytes(length, 'big')
 n2byteslittle = n.to_bytes(length, 'little')
 assert n2bytesbig== bytes( (n  i*8)  0xff for i in 
 reversed(range(length)))
 assert n2byteslittle == bytes( (n  i*8)  0xff for i in range(length))
 assert n == sum( n2bytesbig[::-1][i]  i*8 for i in range(length) )
 assert n == sum( n2byteslittle[i] i*8 for i in range(length) )
 assert n == int.from_bytes(n2bytesbig,byteorder='big')
 assert n == int.from_bytes(n2byteslittle, byteorder='little')


--
assignee: docs@python
components: Documentation
messages: 176671
nosy: docs@python, paddy3118
priority: normal
severity: normal
status: open
title: Add examples to int.to_bytres and int.from_bytes
type: enhancement
versions: Python 3.3

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue16580
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Python 3.X: nonlocal support in eval/exec?

2011-08-11 Thread Paddy
We can access nonlocal variables in a function, but if we were to eval/
exec the function we cannot set up a nested stack of evironment dicts.
We are limited to just two: global and local.

How about eval/exec take a new env argument that is a nested
dictionary whose outer level corresponds to locals, and which has a
__up__ value which is the next outer level environment dict. The
outermost level of the nested dictionary would correspond to the
global level?

I haven't given much thought to my suggested solution - I just didn't
want to leave a possible solution out. The major point is that there
seems to be no support for nonlocal in eval/exec (unless, trivially,
nonlocal==global).

- Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.X: nonlocal support in eval/exec?

2011-08-11 Thread Paddy
On Aug 11, 8:48 am, Terry Reedy tjre...@udel.edu wrote:
 On 8/11/2011 3:19 AM, Paddy wrote:

  We can access nonlocal variables in a function, but if we were to eval/
  exec the function we cannot set up a nested stack of evironment dicts.
  We are limited to just two: global and local.

 Right. That was and is Python's execution model.
 Note that when you exec code, including a function call, the locals
 passed is the local context in which the code is executed. It is not the
 locals of any particular function called by the exec-ed code.

 If you exec a function that is a closure, the closure or non-local
 objects come with the function. A 'stack of dicts' has nothing to do
 with how function and nested funcs operate.

 --
 Terry Jan Reedy

Thanks Terry.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Testing for performance regressions

2011-04-04 Thread Paddy
In an extended case when you try and capture how a function works over a range 
of inputs, you might want to not assume some relationship between input size 
and time, as this mnight limit your ability to change algorithms and still have 
acceptable performance. 

I.e. instead of this:

input_range = (MIN, AVERAGE, MAX)
for i in inpute_range:
..baseline = Timer(simple_func(i)).timeit()
..time_taken = Timer(my_func(i)).timeit()
..assert time_taken = simple_relation(i) * baseline

It might be better to do this:

input_range = (MIN_R, AVERAGE_R, MAX_R)
time_ranges = (MIN_T, AVERAGE_T, MAX_T)
for i,t in zip(inpute_range, time_ranges):
..baseline = Timer(simple_func(i)).timeit()
..time_taken = Timer(my_func(i)).timeit()
..assert time_taken = t * baseline

This comes from electronic circuit design where designs must be proven to work 
over a range for different values for example voltage ranges and temperature 
ranges. 

The action of the function being timed might not be simple, for example if 
my_func swapped algorithms depending on its input to favour the average case. 

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


Re: Guido rethinking removal of cmp from sort method

2011-04-04 Thread Paddy
On Tuesday, April 5, 2011 2:16:07 AM UTC+1, harrismh777 wrote:
 Steven D'Aprano wrote:
  I prefer to consider Python 2.7 and Python 3.x as different dialects of
  the same language. There are a very few handful of incompatibilities,
  most of which can be automatically resolved by the 2to3 fixers.
 
 Yes, I am actually finding this to be consistent with my experience of 
 trying to come up to speed with 3.2.  I have been relieved to find that 
 less has changed than the fear-mongering and bickering was leading me to 
 believe.
 
 Another item that would be nice as an IDLE enhancement would be a menu 
 option that applies the fixers (either direction depending on version 
 2.7 -- 3.2) right in the IDE. Entries that could not be fixed could be 
 flagged for manual update.
 
 If there are good tools for app developers to use to make the transition 
 smoother then the development community won't get their ear chewed off 
 so ragged, I'm supposing.

Hats off and three cheers to the developers and python community as a whole, as 
some are down to sugesting easier access to 2-3 rather than OMG! How do I 
port!!

Now that is an excellent sign that Python works.

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


functools.partial doesn't work without using named parameter

2011-03-25 Thread Paddy
Hi, I just found the following oddity where for function fsf1 I am forced to 
use a named parameter for correct evaluation and was wondering why it doesn't 
work, yet the example from the docs of wrapping int to create basetwo doesn't 
need this?
The example:

 from functools import partial
 basetwo = partial(int, base=2)
 basetwo('10010')
18
 
 def fs(f, s): return [f(value) for value in s]

 def f1(value): return value * 2

 s = [0, 1, 2, 3]
 fs(f1, s)
[0, 2, 4, 6]
 
 fsf1 = partial(fs, f=f1)
 fsf1(s)
Traceback (most recent call last):
  File pyshell#24, line 1, in module
fsf1(s)
TypeError: fs() got multiple values for keyword argument 'f'
 # BUT
 fsf1(s=s)
[0, 2, 4, 6]
 

Would someone help?

- Thanks in advance, Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: functools.partial doesn't work without using named parameter

2011-03-25 Thread Paddy
P.S: Python 3.2!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: functools.partial doesn't work without using named parameter

2011-03-25 Thread Paddy
Thanks Ian, Benjamin, and Steven.

I now know why it works as it does.

Thinking about it a little more, Is it reasonable to *expect* partial acts as 
it does, rather than this way being an implementation convenience? (That was 
written as a straight question not in any way as a dig).

I had thought that partial had enough information to, and would in fact, remove 
named parameter f from the signature of fsf1 returning something that could be 
called that is the equivalent of:

def fsf1_( s ): ...

I accept that that is not done, but would welcome discussion on if my 
expectation above was reasonable. 

Thanks guys :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: functools.partial doesn't work without using named parameter

2011-03-25 Thread Paddy
Aha!

Thanks Ian for this new snippet. It is what I will use for my current example. 
(But please see my third posting on this too).
-- 
http://mail.python.org/mailman/listinfo/python-list


Whats new in Python 3: concurrent-futures example error?

2011-02-22 Thread Paddy
I just noted the example here:
http://docs.python.org/dev/whatsnew/3.2.html#pep-3148-the-concurrent-futures-module

import concurrent.futures, shutil
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as e:
e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
e.submit(shutil.copy, 'src3.txt', 'dest4.txt')

Should the last line show a copy of src4.txt rather than src3.txt
going to dest4.txt?

- Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Whats new in Python 3: concurrent-futures example error?

2011-02-22 Thread Paddy
My thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: EXOR or symmetric difference for the Counter class

2010-08-17 Thread Paddy
On 17 Aug, 02:29, Raymond Hettinger pyt...@rcn.com wrote:
 [Paddy]

  Lets say you have two *sets* of integers representing two near-copies
  of some system, then a measure of their difference could be calculated
  as:

  len(X.symmetric_difference(Y)) / (len(X) + len(Y)) * 100 %

  If the two collections of integers are allowed duplicates then you
  need a Counter/bag/multi-set type and the diff calculation I gave
  originally.

 Thanks for sharing your use case.

 It's unlikely that I will add this method to the Counter API because
 the rarity of use case does not warrant the added API complexity.
 IMO, adding a method like this makes the class harder to learn,
 understand and remember.  It doesn't seem like much of a win over
 using the existing alternatives:

  * (b - c) + (c - b)
  * (b | c) - (b  c)
  * DIY using the two counters as simple dicts
  * writing a subclass providing additional binary operations

 I would like to see someone post a subclass to the ASPN Cookbook that
 adds a number of interesting, though not common operations.  Your
 symmetric_difference() method could be one.  A dot_product() operation
 could be another.  Elementwise arithmetic is another option (we
 already have add and subtract, but could possibly use multiply,
 divide, etc).  Scaling operations are another possibility (multiple
 all elements by five, for example).

 The Counter() class has low aspirations.  It is a dictionary that
 fills-in missing values with zero and is augmented by a handful of
 basic methods for managing the counts.

 Raymond

I created this that could be an addition to the bottom of the Python 3
collections.Counter class definition:

def __xor__(self, other):
''' symmetric difference: Subtract count, but keep only abs
results with non-zero counts.

 Counter('abbbc') ^ Counter('bccd')
Counter({'b': 2, 'a': 1, 'c': 1, 'd': 1})
 a, b = Counter('abbbc'), Counter('bccd')
 (a-b) + (b - a) == a ^ b
True

'''
if not isinstance(other, Counter):
return NotImplemented
result = Counter()
for elem in set(self) | set(other):
newcount = self[elem] - other[elem]
if newcount != 0:
result[elem] = newcount if newcount  0 else -newcount
return result

- Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: EXOR or symmetric difference for the Counter class

2010-08-17 Thread Paddy
On Aug 17, 10:47 pm, Paddy paddy3...@googlemail.com wrote:
 On 17 Aug, 02:29, Raymond Hettinger pyt...@rcn.com wrote:



  [Paddy]

   Lets say you have two *sets* of integers representing two near-copies
   of some system, then a measure of their difference could be calculated
   as:

   len(X.symmetric_difference(Y)) / (len(X) + len(Y)) * 100 %

   If the two collections of integers are allowed duplicates then you
   need a Counter/bag/multi-set type and the diff calculation I gave
   originally.

  Thanks for sharing your use case.

  It's unlikely that I will add this method to the Counter API because
  the rarity of use case does not warrant the added API complexity.
  IMO, adding a method like this makes the class harder to learn,
  understand and remember.  It doesn't seem like much of a win over
  using the existing alternatives:

   * (b - c) + (c - b)
   * (b | c) - (b  c)
   * DIY using the two counters as simple dicts
   * writing a subclass providing additional binary operations

  I would like to see someone post a subclass to the ASPN Cookbook that
  adds a number of interesting, though not common operations.  Your
  symmetric_difference() method could be one.  A dot_product() operation
  could be another.  Elementwise arithmetic is another option (we
  already have add and subtract, but could possibly use multiply,
  divide, etc).  Scaling operations are another possibility (multiple
  all elements by five, for example).

  The Counter() class has low aspirations.  It is a dictionary that
  fills-in missing values with zero and is augmented by a handful of
  basic methods for managing the counts.

  Raymond

 I created this that could be an addition to the bottom of the Python 3
 collections.Counter class definition:

     def __xor__(self, other):
         ''' symmetric difference: Subtract count, but keep only abs
 results with non-zero counts.

          Counter('abbbc') ^ Counter('bccd')
         Counter({'b': 2, 'a': 1, 'c': 1, 'd': 1})
          a, b = Counter('abbbc'), Counter('bccd')
          (a-b) + (b - a) == a ^ b
         True

         '''
         if not isinstance(other, Counter):
             return NotImplemented
         result = Counter()
         for elem in set(self) | set(other):
             newcount = self[elem] - other[elem]
             if newcount != 0:
                 result[elem] = newcount if newcount  0 else -newcount
         return result

 - Paddy.

And heres the cartesian product/multiply:

def __mul__(self, other):
'''Multiply counts by an integer; or cartesioan product
of two counters.

 Counter('abbb') * 3
Counter({'b': 9, 'a': 3})
 Counter('12') * Counter('21')
Counter({('2', '1'): 1, ('1', '2'): 1, ('1', '1'): 1, ('2',
'2'): 1})
 Counter('122') * Counter('211')
Counter({('2', '1'): 4, ('1', '1'): 2, ('2', '2'): 2, ('1',
'2'): 1})
'''
if isinstance(other, int):
return Counter(**dict((k, v*other)
  for k,v in self.items()))
elif isinstance(other, Counter):
return Counter( (x, y)
for x in self.elements()
for y in other.elements() )
else:
return NotImplemented

(Although I don't have a use case for this one).

- Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: EXOR or symmetric difference for the Counter class

2010-08-17 Thread Paddy
On Aug 17, 2:29 am, Raymond Hettinger pyt...@rcn.com wrote:

 I would like to see someone post a subclass to the ASPN Cookbook that
 adds a number of interesting, though not common operations.  Your
 symmetric_difference() method could be one.  A dot_product() operation
 could be another.  Elementwise arithmetic is another option (we
 already have add and subtract, but could possibly use multiply,
 divide, etc).  Scaling operations are another possibility (multiple
 all elements by five, for example).



 Raymond

Sample code is at 
http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: EXOR or symmetric difference for the Counter class

2010-08-16 Thread Paddy
On 14 Aug, 18:14, Raymond Hettinger pyt...@rcn.com wrote:
 On Aug 12, 1:20 pm, Paddy paddy3...@googlemail.com wrote:

  I find myself needing to calculate the difference between two Counters
  or multisets or bags.

  I want those items that are unique to each bag.

 Tell us about your use cases.  I'm curious how a program would ascribe
 semantic meaning to the result.  The phrase unique to each bag
 doesn't quite cover it, perhaps something like number in either
 source above the minimum held in common.

 AFAICT, I've never needed something like this as a primitive.  Even
 the xor operation for regular sets is rarely used.

  I know how to
  calculate it:

       b = Counter(a=1, b=2)
       c = Counter(a=3, b=1)
       diff = (b - c) + (c - b)
       diff
      Counter({'a': 2, 'b': 1})

 That seems simple enough.
 You could also use:

  diff = (b | c) - (b  c)   # max(b,c) - min(b,c)

 Raymond

Hi Raymond and others,

Lets say you have two *sets* of integers representing two near-copies
of some system, then a measure of their difference could be calculated
as:

len(X.symmetric_difference(Y)) / (len(X) + len(Y)) * 100 %

If the two collections of integers are allowed duplicates then you
need a Counter/bag/multi-set type and the diff calculation I gave
originally.

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: EXOR or symmetric difference for the Counter class

2010-08-13 Thread Paddy
On Aug 13, 6:36 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Thu, 12 Aug 2010 13:20:19 -0700, Paddy wrote:
  I find myself needing to calculate the difference between two Counters
  or multisets or bags.

 Is this collections.Counter from Python 3.1? If so, you should say so,
 and if not, you should tell us which Counter class this is. It will save
 people (by which I mean *me*) from spending an unrewarding few minutes
 trying to import Counter in Python 2.5 and writing up a sarcastic
 response ... :)

  I want those items that are unique to each bag. I know how to calculate
  it:

       b = Counter(a=1, b=2)
       c = Counter(a=3, b=1)
       diff = (b - c) + (c - b)
       (b - c)
      Counter({'b': 1})
       (c - b)
      Counter({'a': 2})
       diff
      Counter({'a': 2, 'b': 1})

  But thought why doesn't this operation appear already as a method of the
  class?

 Don't know. Perhaps you should put in a feature request.

 --
 Steven

Yes it is Counter in both 3.1 and 2.7 (And somewhere on Activestate
too).

Before I put in a feature request, I wanted to know if other have seen
the need.
-- 
http://mail.python.org/mailman/listinfo/python-list


EXOR or symmetric difference for the Counter class

2010-08-12 Thread Paddy
I find myself needing to calculate the difference between two Counters
or multisets or bags.

I want those items that are unique to each bag. I know how to
calculate it:

 b = Counter(a=1, b=2)
 c = Counter(a=3, b=1)
 diff = (b - c) + (c - b)
 (b - c)
Counter({'b': 1})
 (c - b)
Counter({'a': 2})
 diff
Counter({'a': 2, 'b': 1})

But thought why doesn't this operation appear already as a method of
the class?

- Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Accessing a method from within its own code

2009-11-02 Thread Paddy O'Loughlin
Hi,
I was wondering if there was a shorthand way to get a reference to a method
object from within that method's code.

Take this code snippet as an example:
import re

class MyClass(object):
def find_line(self, lines):
if not hasattr(MyClass.do_work, matcher):
MyClass.do_work.matcher = re.compile(\d - (.+))
for line in lines:
m = MyClass.do_work.matcher.match(line)
if m:
return m.groups()

Here, I have a method which uses a regular expression object to find
matches. I want the regexp object to be tied to the function, but I don't
want to have to recreate it every time the function is called (I am aware
that regexp objects are cached, avoiding this problem, but I'm just using
this as an example for what I want to know), so I've added it to the method
object as an attribute and create it only if it doesn't exist.

However, typing out Classname.MethodName.variablename everytime is
pretty long and susceptible to refactoring issues, so I was wondering if
there was a way in Python that I am missing which allows you to reference
the method that the code is in (like __module__ gives a reference to the
parent module).

Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing a method from within its own code

2009-11-02 Thread Paddy O'Loughlin

 I suspect that the inspection module has your answer, but that it'll be
 bulkier, and much slower than just doing what you're doing already.

Hmm.
Yeah, it does appear to be bulky. I don't think it's really any more use
than what I'm doing already.


Why not use the default arguments gimmick?  Since this cached item is to
 have a module lifetime, it'd be desirable to create it when the method is
 being defined, which is exactly what default arguments do.


I've a few different ways of emulating static function variables from C/Java
and the function/method attribute one is easily the one that appeals most to
my sensibilities.
I find the default arguments gimmick to be a gimmick. It's co-opting a piece
of functionality for something it doesn't seem like it was originally
intended for and as a consequence is less readable (to my eyes).
The only problem with the function/method way is that it gets rather verbose
when you are dealing with a user-defined method and have to give the class
name and method name and it's also a bit of a pain for moving code around
when refactoring.

You ever wish there was more to python scoping than just locals(), globals()
and __builtins__? Like a method's class's scope too?
That's where I am at with this.


-- 
Ray, when someone asks you if you're a god, you say YES!
-- 
http://mail.python.org/mailman/listinfo/python-list


Rosetta Code: request for help

2009-06-20 Thread Paddy
Hi, If you like programming problems and reading about/creating
solutions to tasks in many languages not just Python, then take a look
at Rosetta Code: http://www.rosettacode.org . If you 'lurk' for a
while and read the submissions of others to get a feal for the site,
then their is a list of tasks that still have no Python solution
posted here: http://www.rosettacode.org/wiki/Tasks_not_implemented_in_Python
that you might help out with.

Some problems might need the use of specific but well known libraries
such as PIL for the graphics, or specific knowledge. Others might be
straight forward; or could do with an example translated to Python 3.x
if it would change a lot from 2.x etc.

Please take a look, I know I know I enjoy being involved.

- Paddy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorted() erraticly fails to sort string numbers

2009-04-30 Thread Paddy O'Loughlin
2009/4/30 Lie Ryan lie.1...@gmail.com

   container[:] = sorted(container, key=getkey)

  is equivalent to:

   container.sort(key=getkey)


 Equivalent, and in fact better since the sorting is done in-place instead
 of creating a new list, then overwriting the old one.


Not when, as pointed out by uuid, container is not list-like (at least as
far as the sort() method goes).
:)

Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Reading an exact number of characters from input

2009-04-16 Thread Paddy O'Loughlin
Hi,
How would I use python to simply read a specific number of characters
from standard input?

raw_input() only returns when the user inputs a new line (or some
other special character).
I tried
 import sys
 sys.stdin.read(15)

and that *returns* up to 15 characters, but it keeps accepting input
(and doesn't return) until I press Enter.

My initial thoughts are that a function like C's fgetc would be the
easiest way to do it, but I haven't been able to find an equivalent in
my google search, so I was wondering if anyone here might have some
ideas.

What say you?

Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Introducing Python to others

2009-03-26 Thread Paddy O'Loughlin
Hi,
As our resident python advocate, I've been asked by my team leader to
give a bit of a presentation as an introduction to python to the rest
of our department.
It'll be less than an hour, with time for taking questions at the end.

There's not going to be a whole lot of structure to it. First, I'm
going to open up a python terminal and show them how the interpreter
works and a few basic syntax things and then a file .py files (got to
show them that python's indenting structure is not something to be
afraid of :P). I think I'll mostly show things in the order that they
appear in the python tutorial (http://docs.python.org/tutorial/).

My question to you, dear python-list, is what suggestions do you have
for aspects of python that I should show them to make them maybe think
that python is better than what they are using at the moment.
All of the audience will be experienced (4+ years) programmers, almost
all of them are PHP developers (2 others, plus myself, work in C, know
C#, perl, java, etc.).
Because of this, I was thinking of making sure I included exceptions
and handling, the richness of the python library and a pointing out
how many modules there were out there to do almost anything one could
think of.
Anything else you think could make PHP developers starting think that
python is a better choice?
If I were to do a (very) short demonstration one web framework for the
PHP devs, what should I use? CherryPy (seems to be the easiest),
Django (seems to be the biggest/most used), or something else?

Any other suggestions for a possible wow reaction from an audience like that?

Thanks,
Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Introducing Python to others

2009-03-26 Thread Paddy O'Loughlin
Thanks for all your replies.
A lot of very strong answers :)

2009/3/26 Mensanator mensana...@aol.com:
 What would you have to do to make this work?

 x+x+x      # expecting [3,6]
 [2, 4, 1, 2]

What's happening is that the call to map() is returning a list object.
So after it calculates the first x+x, you are left with the equivalent of:
[6, 6] + x
Because the list object is on the left, it's __add__() function is
called, which appends x to [6,6].

Instead, convert the list returned by map to a Vector before returning
it. Like so:
 class Vector(list):
... def __add__(self, other):
... return Vector(map(add, self, other))
...
 x = Vector([3,3])
 x+x+x
[9, 9]



-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Is their an expression to create a class?

2009-03-17 Thread Paddy
We the def statement and the lambda expression. We have the class
statement, but is their an expression to create a class?

Or:

 def F(): pass

 type(F)
type 'function'
 # Is to:
 F2 = lambda : none
 type(F2)
type 'function'

 # As
 class O(object): pass

 type(O)
type 'type'
 # is to:
 # 

Thanks.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is their an expression to create a class?

2009-03-17 Thread Donald 'Paddy' McCarthy

Chris Rebert wrote:

On Tue, Mar 17, 2009 at 2:24 PM, Robert Kern robert.k...@gmail.com wrote:

On 2009-03-17 16:13, Paddy wrote:

We the def statement and the lambda expression. We have the class
statement, but is their an expression to create a class?

Or:


def F(): pass
type(F)

type 'function'

# Is to:
F2 = lambda : none
type(F2)

type 'function'

# As
class O(object): pass
type(O)

type 'type'

# is to:
# 

type('O', (object,), {})


Further detail from the docs (http://docs.python.org/library/functions.html):

type(name, bases, dict)

Return a new type object. This is essentially a dynamic form of
the class statement. The name string is the class name and becomes the
__name__ attribute; the bases tuple itemizes the base classes and
becomes the __bases__ attribute; and the dict dictionary is the
namespace containing definitions for class body and becomes the
__dict__ attribute. For example, the following two statements create
identical type objects:

 class X(object):
... a = 1
...
 X = type('X', (object,), dict(a=1))

New in version 2.2.

Cheers,
Chris



Thanks guys. Youve put my mind at rest!

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Run a python script as an exe and run a new process from it

2009-02-27 Thread Paddy O'Loughlin
2009/2/27 venutaurus...@gmail.com venutaurus...@gmail.com:
 Thanks for the reply,,
            I am trying to use the above application using psexec()in
 command line.But it failed returning the error message

  exited with error code 255.

              But when I ran the application normally it worked
 fine.Do I need to add anything to my python script(either the main one
 or the script which is calling it) to make it work.

I don't have any experience with psexec, so I'm not sure if I can help
you with that.
I assume this means that you are trying to run the script on a remote
computer? (Elsewise, why use psexec?)

I took a look at it though and it seems that it always exits with
*some* error code. I think it's more usual for that error code to be
zero, though (it does when I run it on my machine).

You might try psexec start script1.py, which might work if you've
successfully set Explorer to run .py files with python.exe when you
double-click on them.

psexec python script1.py will work if the python.exe is in the correct path.

You can also specify the full path to your python.exe. if you know it
e.g.: psexec C:\python25\python.exe script1.py



-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Run a python script as an exe and run a new process from it

2009-02-26 Thread Paddy O'Loughlin
Try this as an outline:
 script1.py
from subprocess import Popen

if __name__ == '__main__':
scriptname = script2.py

Popen(python %s % scriptname, shell=True)


print I'm done


 script2.py
from time import sleep

if __name__ == '__main__':
while (True):
print waiting..
sleep(2)

###
If you install python using the Windows Installer (.exe), it should
set up .py files to be opened by python when double-clicking on them.
Alternatively, you can right-click on your .py file and go to Open
With... then choose program, then click Browse..., you can Browse
to the python executable, so Explorer will use it to open .py files
when you double-click on them.

As someone else mentioned there is also a py2exe program. Google it.

2009/2/26 venutaurus...@gmail.com venutaurus...@gmail.com:
 Hello all,
           I've a strange requirement where I need to run a python
 script just as we run an exe (by double clicking through windows
 explorer or by typing the script name at command prompt). In that
 process I should be able to execute another python script in such a
 way that, the second script should continue running but the main one
 should terminate without effecting the second one.

                    My requirement may be little confusing so please
 get back if you didn't understand what I meant above.


 Thank you,
 Venu.
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Byte type?

2009-02-24 Thread Paddy O'Loughlin
2009/2/24 John Nagle na...@animats.com:
 Martin v. Löwis wrote:

 Please don't call something dumb that you don't fully understand. It's
 offenses the people who have spent lots of time developing Python --
 personal, unpaid and voluntary time!

   Some of the people involved are on Google's payroll.

Uh, what does that have to do with anything?
It would only be relevant if you are saying that Google is paying them
to do the work (so not just on their payroll).

More importantly, it's also only relevant if ALL the people
contributing are being paid by Google to do the work, which I'm pretty
sure is not the case.

There are people are spending lots of personal, unpaid and voluntary
time developing Python.

Paddy
-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I declare global vars or class vars in Python ?

2009-02-20 Thread Paddy O'Loughlin
2009/2/20 Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid:
 Note that while you *can* do direct access to the implementation attribute
 (here, '_A' for property 'A'), you don't *need* to so (and usually shouldn't
 - unless you have a very compelling reason).

Interesting. Why shouldn't you?
I haven't used the property() function before and probably have no
call to, but when you say usually shouldn't, what is there against
it?

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I declare global vars or class vars in Python ?

2009-02-20 Thread Paddy O'Loughlin
2009/2/20 Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid:
 Interesting. Why shouldn't you?
 I haven't used the property() function

 s/function/object/

Nice try, but what I wrote was what I intended to say:
http://docs.python.org/library/functions.html#property

For all I know I could have used property objects several times in modules :)

 The case is that the whole point of using a computed attribute is to perform
 some computation on the value. IOW, except for a couple corner cases, only
 the accessors should directly access the implementation(s) attributes(s).

 And of course, like for any other GoldenRule(tm), it's not meant to be
 blindly followed. It's just that most of the times, going thru the accessors
 is really what you want - even from within the class code.

Hmm, it doesn't seem to me like it's much of a big deal, for it to
described as anything like a GoldenRule or to advise against its
overuse.
You use it when its appropriate and don't use it when you it's not,
like any other feature.

Paddy


-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I declare global vars or class vars in Python ?

2009-02-20 Thread Paddy O'Loughlin
2009/2/20 Bruno Desthuilliers bruno.42.desthuilli...@websiteburo.invalid:
 Check by yourself:

 import inspect
 inspect.isfunction(property)
 False
Using this, every single builtin function returns False. That's a
pretty limited definition to be being pedantic over, especially when
they are in the Built-in Functions section of the manual.

 property()
 property object at 0xb7cbc144
I never said that calling it didn't return a property object :)

 property
type 'property'

So it's a type.
I think I didn't make such a bad error on my part. My intent in what I
was saying was to indicate that I hadn't created a property type. It's
fair enough to consider type constructors as functions given how they
are listed as such by the python documentation.
I don't think that your correction was much help, unless you just want
to say that everything is an object. Maybe you'd be right, but it's
not much use to use the term that way, imo

shrug

 dir(property)
 ['__class__', '__delattr__', '__delete__', '__doc__', '__get__',
 '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
 '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__str__', 'fdel',
 'fget', 'fset']

Doesn't prove a whole lot. So types have attributes...
So do functions:
 def myfunc():
... pass
...
 dir(myfunc)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code',
'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']


 Hmm, it doesn't seem to me like it's much of a big deal, for it to
 described as anything like a GoldenRule

 One could say the same for each and any of the usual GoldenRules(tm).
Not really. To do so would be over-generalising and not useful to discussion
I guess it's your pedantry that I'm questioning.
Something like don't use goto's works as a GoldenRule because it's
been observed that without it, people start using goto statements in
places where it's not really appropriate.
When you said that [you] usually shouldn't [use properties] - unless
you have a very compelling reason, your tone implied that properties
are easy to misuse and tend to be.
Not being familiar with properties and seeing them as being pretty
harmless, I was intrigued by this, which is why I asked for an
explanation.
Your explanation seems to show that your tone was likely to be more
personal bias than any real issue with properties.

Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I declare global vars or class vars in Python ?

2009-02-17 Thread Paddy O'Loughlin
Use the global statement.
http://docs.python.org/reference/simple_stmts.html#the-global-statement

A working example based on your pseudocode would be:

def getA():
global A
return A

def getB():
global B
return B

def setA(value):
global A
A = value

def setB(value):
global B
B = value

def main():
setA(5)
setB(6)
print getA()
print getB()

if __name__ == '__main__':
main()

#

Although honestly, I think you'd be best not coding like that (with
gets and sets and all) in python.

Paddy

2009/2/17 Linuxguy123 linuxguy...@gmail.com:
 How do I do this in Python ?

 #
 declare A,B

 function getA
return A

 function getB
return B

 function setA(value)
 A = value

 function setB(value)
 B = value

 main()
getA
getB
dosomething
setA(aValue)
setB(aValue)
 

 The part I don't know to do is declare the variables, either as globals
 or as vars in a class.  How is this done in Python without setting them
 to a value ?

 Thanks



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




-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do I declare global vars or class vars in Python ?

2009-02-17 Thread Paddy O'Loughlin
2009/2/17 MRAB goo...@mrabarnett.plus.com:
 It isn't possible to have an uninitialised variable. If it doesn't have
 a value then it doesn't exist.

True, but you can use the global statement to refer to the variable
within a function and read from the variable there, without it being
already initialised in the module.

Of course, if you try to call that function before the global has been
initialised, python will complain [and rightly so :)]

Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pythonic way to determine if a string is a number

2009-02-17 Thread Paddy O'Loughlin
2009/2/16 Python Nutter pythonnut...@gmail.com:
 silly me, forgot to mention

 build a set from digits + '.' and use that for testing.

 Cheers,
 PN


 2009/2/16 Python Nutter pythonnut...@gmail.com:
 Type casting seems to be the wrong way to go about this.

 teststring = '15719'
 teststring.isdigit()
 returns True

 That takes care of integers.

 from string import digits
 digits
 '0123456789'

 now you have all the digits and you can do set testing in your logic
 to see if the teststring has anything in digits

 A dumb way to test is alphanumeric

 teststring2 = '105.22'
 teststring2.isalnum()
 returns True

 now you can go on from there and test to further to eliminate
 'abcd385laf8' which on alnum() also returns true.

Hmmm, this doesn't seem right to me.

Unless I'm missing something, won't your code think that 123.123.123
is numeric? What about scientific or engineering notation with
exponents?

What's wrong with using python's own casting rules (given that you are
trying to emulate the way python behaves? Or, alternatively, using a
regular expression (as Nick Craig-Wood did).

Given these solutions, type-conversion and catching the ValueError
appears, to me, to be correct, the most concise, and the most readable
solution.

Of course, if you want to use your own set of rules for number
encoding, then building your own regular expression would seem to be
the right way to go.

Paddy

-- 
Ray, when someone asks you if you're a god, you say YES!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Will multithreading make python less popular?

2009-02-16 Thread Paddy
On Feb 16, 9:34 am, rushen...@gmail.com wrote:
 Hi everybody,
 I am an engineer. I am trying to improve my software development
 abilities. I have started programming with ruby. I like it very much
 but i want to add something more. According to my previous research i
 have designed a learning path for myself. It's like something below.
       1. Ruby (Mastering as much as possible)
       2. Python (Mastering as much as possible)
       3. Basic C++ or Basic Java
 And the story begins here. As i search on the net,  I have found that
 because of the natural characteristics of python such as GIL, we are
 not able to write multi threaded programs.

You are likely to find a lot of 'tick-list' type of comparison data on
the web that either needs a lot of knowledge to interpret, or is
misleading/wrong or all three!

Their are several versions of Python out their such as Ironpython,
Stackless Python, Jython as well as CPython - the main Python release.
They have different threading capabilities, but compilers of feature
comparison tick-lists tend to just stick to what CPython can do.

As an aside; if you were thinking of using threading for performance
reasons, then its best to first think of improving your general
ability to explore different algorithms. A change to an algorithm
often has the most impact on the performance of code. A better single
threaded, single process algorithm can offer better performaance than
throwing threadds or multiple processes alone when using a poor
underlying algorithm.

I was just exploring different ways of solving a problem on my blog:
  http://paddy3118.blogspot.com/2009/02/comparison-of-python-solutions-to.html
(But no parallel solutions were attempted).

Have fun programming!

- Paddy.

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


Re: Pythonic way to determine if a string is a number

2009-02-16 Thread Paddy
On Feb 15, 5:46 pm, pyt...@bdurham.com wrote:
 What's the Pythonic way to determine if a string is a number? By
 number I mean a valid integer or float.

 I searched the string and cMath libraries for a similar function
 without success. I can think of at least 3 or 4 ways to build my
 own function.

 Here's what I came up with as a proof-of-concept. Are there
 'better' ways to perform this type of test?

 Thanks,
 Malcolm

 code
 def isnumber( input ):
     try:
         if '.' in input:
             num = float( input )
         else:
             num = int( input )
         return True

     except ValueError:
         return False

 if __name__ == '__main__':
     tests = 
         12
         -12
         -12.34
         .0
         .
         1 2 3
         1 . 2
         just text
     

     for test in tests.split( '\n' ):
         print 'test (%0s), isnumber: %1s' % \
           ( test.strip(), isnumber( test ) )

 /code

Their is a good answer given on Rosetta Code here:
  http://www.rosettacode.org/wiki/IsNumeric#Python

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Python Fractions issue.

2008-09-22 Thread Paddy
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html)

There seems to be a problem/difference in calculating with the new
fractions module when comparing Python 26rc2 and 30rc1

I was reading the paper Interval Arithmetic: Python Implementation
and Applications and thought to try the first example function
f(x,y), which needs more precision than Python floating point provides
(on my Windows PC), to calculate a sensible answer.

Ahah, I thought, lets try this with Fractions - so I first installed
Python 30rc1, typed in the Function - saw the rubbish result, then
used Fractions and got the 'right' result.
I then wondered if fractions had been back-ported to Python 2.6rc2,
found that it had, but got different results.

As you can see from the table, Python 2.6rc2 has a problem calculating
t3 = F(11,2) * y**8 + x/(2*y), where F  is Fraction.

I wonder where the problem lies?

(For the Table, please see the blog entry).

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes: loading .so file on Linux

2008-08-26 Thread Paddy
On Aug 23, 2:33 pm, Paddy [EMAIL PROTECTED] wrote:
 Hi,
 I am am falling at the first hurdle when trying to access a library
 using ctypes.

 I have a file libucdb.so which the file command says is shared object,
 but I cannot get it to load:

 Any help would be appreciated:

 dmccarthy: file /opt/questasim_6.4/questasim/linux/libucdb.a /opt/
 questasim_=
 6.4/questasim/linux/libucdb.so
 /opt/questasim_6.4/questasim/linux/libucdb.a:  current ar archive
 /opt/questasim_6.4/questasim/linux/libucdb.so: ELF 32-bit LSB shared
 object,=
  Intel 80386, version 1 (SYSV), not stripped
 dmccarthy: python
 ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
 Python 2.5.1 (r251:54863, May  2 2007, 08:46:07)
 [GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
 Type help, copyright, credits or license for more information. 
 cdll.LoadLibrary(libc.so.6)

 Traceback (most recent call last):
   File stdin, line 1, in module
 NameError: name 'cdll' is not defined from ctypes import *
  ^[[A^[[A

     ^e stdin, line 1
 SyntaxError: invalid syntax cdll.LoadLibrary(libc.so.6)

 CDLL 'libc.so.6', handle 2a958a2900 at 2a95dda490 
 cdll.LoadLibrary(libucdb.so)

 Traceback (most recent call last):
   File stdin, line 1, in module
   File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
 lib/pyth=
 on2.5/ctypes/__init__.py, line 423, in LoadLibrary
     return self._dlltype(name)
   File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
 lib/pyth=
 on2.5/ctypes/__init__.py, line 340, in __init__
     self._handle =3D _dlopen(self._name, mode)
 OSError: libucdb.so: cannot open shared object file: No such file or
 directo=
 ry cdll.LoadLibrary(libc.so.6)

 CDLL 'libc.so.6', handle 2a958a2900 at 2a95df3ad0 libc =3D 
 CDLL(libc.so.6)
  libc

 CDLL 'libc.so.6', handle 2a958a2900 at 2a95dda490 libc =3D 
 CDLL(libucdb.so)

 Traceback (most recent call last):
   File stdin, line 1, in module
   File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
 lib/pyth=
 on2.5/ctypes/__init__.py, line 340, in __init__
     self._handle =3D _dlopen(self._name, mode)
 OSError: libucdb.so: cannot open shared object file: No such file or
 directo=
 ry libc =3D CDLL(/opt/questasim_6.4/questasim/linux/libucdb.so)

 Traceback (most recent call last):
   File stdin, line 1, in module
   File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
 lib/pyth=
 on2.5/ctypes/__init__.py, line 340, in __init__
     self._handle =3D _dlopen(self._name, mode)
 OSError: /opt/questasim_6.4/questasim/linux/libucdb.so: cannot open
 shared o=
 bject file: No such file or directory ^[[A

   File stdin, line 1
     ^
 SyntaxError: invalid syntax



 - Paddy.

Any help would be appreciated.

Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Psyco == tracing optimization?

2008-08-24 Thread Paddy
On Aug 24, 8:11 am, Fredrik Lundh [EMAIL PROTECTED] wrote:
 Banibrata Dutta wrote:
  AFAIK, the same logic used in runtime optimization of several other
  dynamic languages or shell-scripting languages (including Perl), for
  quite a while.

 Trace trees are widely used?  Got any references for that?

 /F

I too feel that if Perl had such optimizations as Psyco gives Python
then they would shout about it.

I wonder about the new term and if it fits in the same 'box' as what
Psyco does, for example, who was aware of whose work?

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regex on a huge text

2008-08-24 Thread Paddy
On Aug 22, 9:19 pm, Medardo Rodriguez [EMAIL PROTECTED] wrote:
 On Fri, Aug 22, 2008 at 11:24 AM, Dan [EMAIL PROTECTED] wrote:
  I'm looking on how to apply a regex on a pretty huge input text (a file
  that's a couple of gigabytes). I found finditer which would return results
  iteratively which is good but it looks like I still need to send a string
  which would be bigger than my RAM. Is there a way to apply a regex directly
  on a file?

  Any help would be appreciated.

 You can call *grep* posix utility.
 But if the regex's matches are possible only inner the context of a
 line of that file:
 #code
 res = []
 with file(filename) as f:
     for line in f:
         res.extend(getmatches(regex, line))
 #  Of course getmatches describes the concept.
 #/code

 Regards

Try and pre-filter your file on a line basis to cut it down , then
apply a further filter on the result.

For example, if you were looking for consecutive SPAM records with the
same Name field then you might first extract only the SPAM records
from the gigabytes to leave something more manageable to search for
consecutive Name fields in.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


ctypes: loading .so file on Linux

2008-08-23 Thread Paddy
Hi,
I am am falling at the first hurdle when trying to access a library
using ctypes.

I have a file libucdb.so which the file command says is shared object,
but I cannot get it to load:

Any help would be appreciated:

dmccarthy: file /opt/questasim_6.4/questasim/linux/libucdb.a /opt/
questasim_=
6.4/questasim/linux/libucdb.so
/opt/questasim_6.4/questasim/linux/libucdb.a:  current ar archive
/opt/questasim_6.4/questasim/linux/libucdb.so: ELF 32-bit LSB shared
object,=
 Intel 80386, version 1 (SYSV), not stripped
dmccarthy: python
ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May  2 2007, 08:46:07)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type help, copyright, credits or license for more information.
 cdll.LoadLibrary(libc.so.6)
Traceback (most recent call last):
  File stdin, line 1, in module
NameError: name 'cdll' is not defined
 from ctypes import *
 ^[[A^[[A
^e stdin, line 1
SyntaxError: invalid syntax
 cdll.LoadLibrary(libc.so.6)
CDLL 'libc.so.6', handle 2a958a2900 at 2a95dda490
 cdll.LoadLibrary(libucdb.so)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
lib/pyth=
on2.5/ctypes/__init__.py, line 423, in LoadLibrary
return self._dlltype(name)
  File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
lib/pyth=
on2.5/ctypes/__init__.py, line 340, in __init__
self._handle =3D _dlopen(self._name, mode)
OSError: libucdb.so: cannot open shared object file: No such file or
directo=
ry
 cdll.LoadLibrary(libc.so.6)
CDLL 'libc.so.6', handle 2a958a2900 at 2a95df3ad0
 libc =3D CDLL(libc.so.6)
 libc
CDLL 'libc.so.6', handle 2a958a2900 at 2a95dda490
 libc =3D CDLL(libucdb.so)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
lib/pyth=
on2.5/ctypes/__init__.py, line 340, in __init__
self._handle =3D _dlopen(self._name, mode)
OSError: libucdb.so: cannot open shared object file: No such file or
directo=
ry
 libc =3D CDLL(/opt/questasim_6.4/questasim/linux/libucdb.so)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /software/unix-soft/linux/ActivePython-2.5.1.1-linux-x86_64/
lib/pyth=
on2.5/ctypes/__init__.py, line 340, in __init__
self._handle =3D _dlopen(self._name, mode)
OSError: /opt/questasim_6.4/questasim/linux/libucdb.so: cannot open
shared o=
bject file: No such file or directory
 ^[[A
  File stdin, line 1
^
SyntaxError: invalid syntax


- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Psyco == tracing optimization?

2008-08-22 Thread Paddy
Just wondered if this:
http://arstechnica.com/news.ars/post/20080822-firefox-to-get-massive-javascript-performance-boost.html,
is a new name for what is done by Psyco?

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Should Python raise a warning for mutable default arguments?

2008-08-22 Thread Paddy
On Aug 22, 4:55 pm, Peter Otten [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  DrScheme is an implementation of Scheme that is very newbie-friendly.
  It has several limited sub-languages, etc.

  So maybe a command line option can be added to Python3 ( -
  newbie ? :-) ) that just switches on similar warnings, to help newbies
  (in schools, where there's a teacher that encourages to always use
  that command line option) avoid some of the most common traps.

 Or maybe bundle pychecker with idle?

 $ cat tmp.py
 def test(x, a=[]):
     a.append(x)
     return a

 for i in range(5):
     print test(i)

 $ pychecker tmp.py
 Processing tmp...
 [0]
 [0, 1]
 [0, 1, 2]
 [0, 1, 2, 3]
 [0, 1, 2, 3, 4]

 Warnings...

 tmp.py:2: Modifying parameter (a) with a default value may have unexpected
 consequences

 Though it might be interesting to ask a newbie what he expects when warned
 of unexpected consequences ;)

 Peter

+1 on this.

It seems an obvious think to add to a lint-like tool rather than
burdening core Python.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python one-liner??

2008-08-22 Thread Paddy
On Aug 22, 10:03 pm, Matimus [EMAIL PROTECTED] wrote:
  Do we have python one-liner like perl one-liner 'perl -e'??

 The answer is python -c...

 but python -h is useful too.

 Matt

And Python is not optimised for one-liner solutions.
I have been known to construct multi-line -c arguments using
the bash shell on Unix (as bash supports multi-line quotes),
but creating and then deleting a temporary file saves me from
'quoting hell'.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Validation of parameters

2008-08-10 Thread Paddy
On Aug 10, 9:13 am, Kless [EMAIL PROTECTED] wrote:
 Which is the best mode for validation of parameters? I'm using the
 next way but it is not well testable.

 -
 if not self._foo:
     try:
         raise ValueError('ValueError: foo not found')
     except ValueError, err:
         print (err.message)
         print(Valid values: %s\n % self.valid_foo)
         sys.exit(1)

 ...
 ...
 -

If you just want the absense of _foo to raise an exception then why to
just reference self._foo and leave the hard work to ptyhon? Something
like:

  self._foo

If it is present then fine, if it is not then an exception is raised.

Here it is in the Python shell:
 class tmp(object):
def __init__(self):
self._foo


 dummy = tmp()

Traceback (most recent call last):
  File pyshell#4, line 1, in module
dummy = tmp()
  File pyshell#3, line 3, in __init__
self._foo
AttributeError: 'tmp' object has no attribute '_foo'


- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: What Python looks like

2008-08-05 Thread Paddy
On Aug 4, 8:06 pm, iu2 [EMAIL PROTECTED] wrote:
 Hi,

 This is a little bit strange post, but I'm curious...

 I learned Python from its tutorial step by step, and practicing
 writing small scripts.
 I haven't seen a Python program before knowing Python.

 I'm curious, what did Python code look like to those of you who have
 seen a bunch of Python code for the first time before knowing Python?

 (I can tell, for example, that seeing perl for the first time looked
 like C with many $$$, I could see if and for and while but they
 were meaningless. Or Lisp for the first time looked like many words,
 no operators, how could that make a program???)

 Thanks

Strange you should mention this. I am currently new to Rosetta Code
http://www.rosettacode.org/ where some pages I have been looking at
such as the page for Zig Zag, and a page I created called Spiral
have had very terse solutions, and long explanations in the talk
pages all written in the J language. I can read the lines between
the code samples enough to intrigue , (OK frustrate), me - but the
code is impenetrable.
I am not sure if J attracts good programmers or if learning J
forces you to think about solutions in different or useful ways.
I've learnt A LISP-like language, dabbled with forth  prolog 
constraints - maybe its time to learn J and find out if this
array programming malarky will bring new insight to my problem
solving - but it will never be readable :-)

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Native Code vs. Python code for modules

2008-07-30 Thread Paddy
On Jul 30, 4:56 am, koblas [EMAIL PROTECTED] wrote:
 Ruby has been getting pummeled for the last year or more on the
 performance subject.  They've been working hard at improving it.  From
 my arm chair perspective Python is sitting on it's laurels and not
 taking this as seriously as it probably should.

I personally DON'T think that the developers should be chasing down
every micro-benchmark. Someone has to look-up and see the bigger
picture.
If you take a look at a larger set of micro benchmarks, then Ruby
is below par when compared to Python and Perl on speed. I had the
misfortune to read their development group entries when a critical
bug was 'fixed' by a release from Japan that didn't have enough
testing and so think the Ruby development 'group' have problems of
quality that they also need to address. (And are doing I think by
adding to their test-suite).

PyPy and previous tools like Psyco are ways in which Python
developers are continuing to see above the eternal grind of micro-
optimising the C-interpreter to try and give us tools that can
approach compiled language speeds from the language we love.
I like to think of it as working smarter rather than harder - the
brains a much better muscle :-)

- Paddy.


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


Re: Parsing VHDL with python, where to start.

2008-07-29 Thread Paddy
On Jul 21, 12:09 pm, Svenn Are Bjerkem [EMAIL PROTECTED]
wrote:
 Hi,
 I am in the need to write an application for PyQt to visualise the
 structure of a VHDL project I am working on. Looking for a sensible
 way to parse VHDL files and putting them into a data structure that
 PyQt can represent as a tree (or whatever the MVC is supporting)
 through search engines does not give me many hints. From what I know,
 VHDL is not a very easy language to parse. There seems to be a parser
 for perl available, but I do not know if it is wise to use a perl
 module as a template for writing something similar in python.

 My initial idea is to start simple and extend features in my
 application, but I fear that I may start off with wrong ideas how to
 parse and then code myself into a dead-end requiring myself to rewrite
 the whole application in order to get any further. I would start
 finding definitions of entities and the instantiations of these and
 build a tree from a set of external vhdl files stored in a file
 hierarchy. If somebody have a starting point where to get going with a
 task like this, I would be happy to know.

 --
 kind regards,
 Svenn

Been their, partially done that.
I started parsing an old version of VHDL (VHDL 87 I think); using
regular expressions and carrying state around myself. It was tough,
and I was doing it , but then Work mentioned that they were going to
need to parse later versions of VHDL and that was a whole new ball-
game, specifically configuration type information could be mixed in
with the architecture. Luckily work had also decided to by in a
commercial parser.

I learned that add-hoc parsers have limits to their maintainability
and become complex. You might not have such problems if your VHDL is
all in one library.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Doubt

2008-07-23 Thread Paddy
On Jul 23, 3:51 pm, ജഗന്നാഥ് [EMAIL PROTECTED] wrote:
 Friends

 I am a Perl programmer new to Python. I have a small doubt.
 How to convert the perl notation
 $a = ; expression in Python ?

 How to represent the loop
 for ($a = $b; $a=$c;$a++){

 } in Python

 Jagan
 Linguist

This might help you generally:
  http://wiki.python.org/moin/PerlPhrasebook

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list

Re: software engineering foundations?

2008-07-22 Thread Paddy
On Jul 22, 7:52 pm, [EMAIL PROTECTED] wrote:
 Sorry for the off-topic-ish post.  My son (a fairly junior sysadmin type)
 mentioned to me today that he was looking for online courses for Perl.  (I
 don't hold that against him.  Perl is still a lingua franca in the sysadmin
 realm.)  In my work I have from time-to-time had to pick up and maintain
 scripts (generally shell/Python stuff) which non-professional programmers
 have written.  It's never what you would call a pleasant task.

 There are software construction skills which are entirely distinct from the
 language in which you are programming.  We can tout object-oriented,
 structured programming, test-driven development or other software
 engineering techniques, but there is a body of knowledge out there which is
 orthogonal to the language in which the code is written.  People who are not
 professional programmers often lack those skills and their code shows it.

 Are there any good online resources for this software structure axis?
 Googling for object oriented programming tutorial yields a bunch of stuff,
 much of it language-specific.  I'm trying to find something a bit more
 general than that though.

 I Googled for software engineering tutorial as well.  Most of the early
 hits were either inaccessible (ACM subscription only) or contents-like stuff
 (conference announcements, for example).  Number eight on the list was this
 rather promising page:

    http://www.cmcrossroads.com/bradapp/links/swe-links.html

 It was last updated over 10 years ago.  I find it hard to believe that
 so little has changed in that time that some other page with more recent
 references hasn't percolated to the top of Google's page rank!  After all,
 the Web has grown just a tad in that timeframe.

 I have a sneaking suspicion that what I'm looking for is out there, but that
 I'm not asking Google in the right manner.  Any and all pointers/suggestions
 cheerfully accepted.

 Thx,

 --
 Skip Montanaro - [EMAIL PROTECTED] -http://www.webfast.com/~skip/
 --
 ELON MUSK: If fuel cells were good, don't think you'd see them somewhere,
 like maybe in a laptop or a cell phone or a $200 million military satellite
 maybe?  And yet, where do you see them?
 SPENCER MICHELS: You don't.
 ELON MUSK: Exactly.

Is this the kind of thing you are after?
  http://www.swc.scipy.org/

Software Carpentry

Overview

Many scientists and engineers spend much of their lives programming,
but only a handful have ever been taught how to do this well. As a
result, they spend their time wrestling with software, instead of
doing research, but have no idea how reliable or efficient their
programs are.

This course is an intensive introduction to basic software development
practices for scientists and engineers that can reduce the time they
spend programming by 20-25%. All of the material is open source: it
may be used freely by anyone for educational or commercial purposes,
and research groups in academia and industry are actively encouraged
to adapt it to their needs.


- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bidirectional Generators

2008-07-22 Thread Paddy
On Jul 22, 10:07 pm, william tanksley [EMAIL PROTECTED] wrote:
 Okay, I'm almost finished with my first bidirectional generator. By
 almost finished I mean both that it's almost working, and that I'm
 almost about to replace it with a class that works a bit more like
 what I currently understand.

 Surely some other people have worked with this feature... Are there
 any pages that discuss how it's been useful?

 No, I don't want to see an implementation of coroutines. I get that
 one already. :-)

 -Wm

What's one of them then?

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question

2008-07-20 Thread Paddy
On Jul 19, 10:27 am, [EMAIL PROTECTED] wrote:
 Why is Perl so much better than python?

Coz its endorsed by:
 Chernobble valve controls.
 Barings Bank.
 The society of the Mortgage Brokers of America.
 The Bush Disaster relief fund for the Southern States.

And, of course, is the tool of choice when looking for weapons of mass
destruction in Iraq.


- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: question

2008-07-20 Thread Paddy
On Jul 20, 6:39 pm, [EMAIL PROTECTED] wrote:
 Nobody any sensible answers. Too complicated I suppose!

The sensible question was?
--
http://mail.python.org/mailman/listinfo/python-list


Re: singletons

2008-07-18 Thread Paddy
On Jul 16, 11:20 pm, Craig Allen [EMAIL PROTECTED] wrote:
 Hey, forgive me for just diving in, but I have a question I was
 thinking of asking on another list but it really is a general question
 so let me ask it here.  It's about how to approach making singletons.

Hi Craig,
This might be good for a general background on Design Patters in
Python:

http://uk.youtube.com/watch?v=0vJJlVBVTFg

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: read file into list of lists

2008-07-11 Thread Paddy
On Jul 11, 2:15 pm, antar2 [EMAIL PROTECTED] wrote:
 Hello,

 I can not find out how to read a file into a list of lists. I know how
 to split a text into a list

 sentences = line.split(\n)

 following text for example should be considered as a list of lists (3
 columns and 3 rows), so that when I make the print statement list[0]
 [0], that the word pear appears

 pear noun singular
 books nouns plural
 table noun singular

 Can someone help me?

 Thanks

lofl = [line.strip().split() for line in the_opened_file]

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: read file into list of lists

2008-07-11 Thread Paddy
On Jul 11, 9:32 pm, John Machin [EMAIL PROTECTED] wrote:
 On Jul 11, 11:35 pm, Paddy [EMAIL PROTECTED] wrote:



  On Jul 11, 2:15 pm, antar2 [EMAIL PROTECTED] wrote:

   Hello,

   I can not find out how to read a file into a list of lists. I know how
   to split a text into a list

   sentences = line.split(\n)

   following text for example should be considered as a list of lists (3
   columns and 3 rows), so that when I make the print statement list[0]
   [0], that the word pear appears

   pear noun singular
   books nouns plural
   table noun singular

   Can someone help me?

   Thanks

  lofl = [line.strip().split() for line in the_opened_file]

  line = '   foo   bar   '
  line.strip().split()
 ['foo', 'bar']
  line.split()

 ['foo', 'bar']

Thanks , ta.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.search much slower then grep on some regular expressions

2008-07-05 Thread Paddy
On Jul 5, 7:01 am, Peter Otten [EMAIL PROTECTED] wrote:
 Paddy wrote:
  It is not a smarter algorithm that is used in grep. Python RE's have
  more capabilities than grep RE's which need a slower, more complex
  algorithm.

 So you're saying the Python algo is alternatively gifted...

 Peter

The following isn't the article I read on regexp types and their speed
differences but it does give info on regexp engine types:
http://books.google.co.uk/books?id=GX3w_18-JegCpg=PA145lpg=PA145dq=%2Bregex+%2Bspeed+%2Bgrep+%2Btcl+source=webots=PHljNqeuJZsig=CyBCOZYsyZaJTMfj2Br53if46Bchl=ensa=Xoi=book_resultresnum=3ct=result#PPA145,M1

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: re.search much slower then grep on some regular expressions

2008-07-05 Thread Paddy
On Jul 5, 4:13 pm, Mark Dickinson [EMAIL PROTECTED] wrote:
 It seems like an appropriate moment to point out *this* paper:

 http://swtch.com/~rsc/regexp/regexp1.html


That's the one!

Thanks Mark.

- Paddy.

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


Re: re.search much slower then grep on some regular expressions

2008-07-04 Thread Paddy
On Jul 4, 1:36 pm, Peter Otten [EMAIL PROTECTED] wrote:
 Henning_Thornblad wrote:
  What can be the cause of the large difference between re.search and
  grep?

 grep uses a smarter algorithm ;)



  This script takes about 5 min to run on my computer:
  #!/usr/bin/env python
  import re

  row=
  for a in range(156000):
      row+=a
  print re.search('[^ =]*/',row)

  While doing a simple grep:
  grep '[^ =]*/' input                  (input contains 156.000 a in
  one row)
  doesn't even take a second.

  Is this a bug in python?

 You could call this a performance bug, but it's not common enough in real
 code to get the necessary brain cycles from the core developers.
 So you can either write a patch yourself or use a workaround.

 re.search('[^ =]*/', row) if / in row else None

 might be good enough.

 Peter

It is not a smarter algorithm that is used in grep. Python RE's have
more capabilities than grep RE's which need a slower, more complex
algorithm.
You could argue that if the costly RE features are not used then maybe
simpler, faster algorithms should be automatically swapped in but 

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting sorting order

2008-07-02 Thread Paddy
On Jul 1, 8:26 pm, Tobiah [EMAIL PROTECTED] wrote:
  master,slave1,slave2=zip(*x)

 What does the asterisk do here?

 Thanks
 ** Posted fromhttp://www.teranews.com**

Hi Tobiah,
I blogged extensively on this zip(*x) trick here:
  http://paddy3118.blogspot.com/2007/02/unzip-un-needed-in-python.html

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting two dimentional array by column?

2008-07-02 Thread Paddy
On Jul 2, 10:35 pm, Tobiah [EMAIL PROTECTED] wrote:
  Imagine an excel spreadsheet.  I can choose
  a column and sort the records based on the items
  in that column.  I would like to do the same
  thing with a large two dimensional array.
  What would be the fastest way (in computation time)
  to accomplish this?

 Now that I think about the problem more, I really want
 to sort an array of dictionaries according one of the
 keys of each.  Could I use:

 array.sort(key = something)

 Where something looks at the proper item in the
 current row?  I can't quite visualize how to pull
 the item out of the dictionary.

 Thanks

 ** Posted fromhttp://www.teranews.com**

Hi Tobiah,
Try this:

arrayofdicts.sort(
  key = lambda dictinarray: dictinarray.get(sortkeyname)
  )

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


[issue3214] Suggest change to glossary explanation: Duck Typing

2008-07-01 Thread Paddy McCarthy

Paddy McCarthy [EMAIL PROTECTED] added the comment:

Hi Georg,
A bit of relevant background about me: 
  I've been interested in Duck Typing _specifically_ for a couple of
years when I started watching edits to it on Wikipedia. I researched the
history of the use of the term and changed the attribution of first use
to Alex Martelli after digging in Google, and still trawl reading code
and articles on the subject. But I DONT think this makes me an expert -
Duck typing is a 'mould-able' term and the things we write about it help
to define it.

On your comment about hasattr:
  I think more and more that Duck-Typing is what allows us, (as in
Python), to substitute one class for another in a method previously
designed with only the other in mind - you know, as in the canonical
example of file-like objects such as StringIO substituting in methods
defined originally for files. In this case hasattr checks don't add
anything, and EAFP is all important.

I tried to get wider context on this by posting it originally to
comp.lang.python but no one was interested. I don't normally frequent
the developers forumbut maybe we should open the issue to that audience?

- Paddy.

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3214
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue3214] Suggest change to glossary explanation: Duck Typing

2008-06-26 Thread Paddy McCarthy

New submission from Paddy McCarthy [EMAIL PROTECTED]:

The official glossary entry here:
http://docs.python.org/tut/node18.html#l2h-46
says:

duck-typing
Pythonic programming style that determines an object's type by
inspection of its method or attribute signature rather than by
explicit relationship to some type object (If it looks like a duck
and quacks like a duck, it must be a duck.) By emphasizing interfaces
rather than specific types, well-designed code improves its
flexibility by allowing polymorphic substitution. Duck-typing avoids
tests using type() or isinstance(). Instead, it typically employs
hasattr() tests or EAFP programming.


I think it is should be changed to delete the use of hasattr and just
rely on EAFP as in the second example here:
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions

The text would then read:

duck-typing
Pythonic programming style that determines an object's type by
inspection of its method or attribute signature rather than by
explicit relationship to some type object (If it looks like a duck
and quacks like a duck, it must be a duck.) By emphasizing interfaces
rather than specific types, well-designed code improves its
flexibility by allowing polymorphic substitution. Duck-typing avoids
tests using type(), hasattr() or isinstance(). Instead, it typically
employs an EAFP style of programming.


The reason is that a hasattr test only tests for an attribute name. If
it is present and say the method signature is wrong, then its the
excecution of the code using the attribute that will find that out so
it is redundant. Best to use EAFP for Duck typing as we trust you know
what it is you are doing.

- Paddy.

--
assignee: georg.brandl
components: Documentation
messages: 68815
nosy: georg.brandl, paddy3118
severity: normal
status: open
title: Suggest change to glossary explanation:  Duck Typing
type: feature request
versions: Python 2.5, Python 2.6, Python 3.0

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue3214
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Propose slight change to tutorial glossary entry on Duck Typing

2008-06-25 Thread Paddy
The official glossary entry here: http://docs.python.org/tut/node18.html#l2h-46
says:

duck-typing
Pythonic programming style that determines an object's type by
inspection of its method or attribute signature rather than by
explicit relationship to some type object (If it looks like a duck
and quacks like a duck, it must be a duck.) By emphasizing interfaces
rather than specific types, well-designed code improves its
flexibility by allowing polymorphic substitution. Duck-typing avoids
tests using type() or isinstance(). Instead, it typically employs
hasattr() tests or EAFP programming.


I think it is should be changed to delete the use of hasattr and just
rely on EAFP as in the second example here:
http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions

The text would then read:

duck-typing
Pythonic programming style that determines an object's type by
inspection of its method or attribute signature rather than by
explicit relationship to some type object (If it looks like a duck
and quacks like a duck, it must be a duck.) By emphasizing interfaces
rather than specific types, well-designed code improves its
flexibility by allowing polymorphic substitution. Duck-typing avoids
tests using type(), hasattr() or isinstance(). Instead, it typically
employs an EAFP style of programming.


The reason is that a hasattr test only tests for an attribute name. If
it is present and say the method signature is wrong, then its the
excecution of the code using the attribute that will find that out so
it is redundant. Best to use EAFP for Duck typing as we trust you know
what it is you are doing.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question: How do I format printing in python

2008-06-25 Thread Donald 'Paddy' McCarthy

Lie wrote:

On Jun 24, 12:12 am, [EMAIL PROTECTED] wrote:

Hi All,

How do I format printed data in python?
I could not find this in the Python Reference 
Manual:http://docs.python.org/ref/print.html
Nor could I find it in Matloff's great 
tutorial:http://heather.cs.ucdavis.edu/~matloff/Python/PythonIntro.pdf

For example, how do I turn this:

512 Jun 5 2004 X11r6
22 Jan 17 2005 a2p
22 Jan 17 2005 acctcom
5374 Sep 15 2002 acledit
5664 May 13 2004 aclget
12020 May 13 2004 aclput
115734 Jun 2 2004 adb
46518 Jun 4 2004 admin
66750 Sep 16 2002 ali
1453 Sep 15 2002 alias
28150 Jun 4 2004 alog
15 May 12 2005 alstat

into this:

512Jun   5   2004X11r6
22 Jan   17  2005a2p
22 Jan   17  2005acctcom
5374   Sep   15  2002acledit
5664   May   13  2004aclget
12020  May   13  2004aclput
115734 Jun   2   2004adb
46518  Jun   4   2004admin
66750  Sep   16  2002ali
1453   Sep   15  2002alias
28150  Jun   4   2004alog
15 May   12  2005alstat

Thank you


There is string formatting

print formatspecifier_string % data_sequence
The format specifier is similar to the one used in C's printf, and
data sequence may be tuple or list. Dictionary may also be used for
data, but it has its own way to specify string formatting since
dictionary is unordered but indexed by the dict key.


I have attached a prog I wrote to answer someones elses similar problem.

- Paddy.


from StringIO import StringIO
from pprint import pprint as pp
left_justified = False
debug = False

textinfile = '''I$created$a$solution$for$a$program$I$am$writing$that
makes$columns$line$up$when$outputted$to$the$command
line.$I$am$new$to$Python,$and$am$hoping$I$might$get
some$input$on$this$topic.$In$the$text$file$that$the
program$reads,$the$fields$(columns)$are$delimited$by
'dollar'$and$the$records$(lines)$are$delimited$by
newlines$'\\n'.$So$one$line$looks$like:$'''



Solution to problem posed at:
  http://www.kbrandt.com/2008/06/getting-command-line-output-columns-to.html
  
Output is the following if left-justified:

# Column-aligned output:
Icreated asolution for   a program   Iam  
writing that 
makescolumns line up   when  outputted tothe  command   
   
line.I   am   new  toPython,   and   am   hoping  I 
  might get
some input   on   this topic.Inthe   text filethat  
  the  
program  reads,  the  fields   (columns) are   delimited by 
   
'dollar' and the  records  (lines)   are   delimited by 
   
newlines '\n'.   So   one  line  looks like:
   

And like this if not left-justified:

# Column-aligned output:
   I createda solution   for a   programI  am 
writing  that
   makes columns line   up  when outputtedto  the command   
   
   line.   I   am  newto   Python,   and   am  hoping   
I might get
some   input   on thistopic.In   the textfile
that   the
 program  reads,  the   fields (columns)   are delimited   by   
   
'dollar' and  the  records   (lines)   are delimited   by   
   
newlines   '\n'.   So  one  line looks like:
   



infile = StringIO(textinfile)

fieldsbyrecord= [line.strip().split('$') for line in infile]
if debug: print fieldsbyrecord:; print (fieldsbyrecord)
# pad to same number of fields per record
maxfields = max(len(record) for record in fieldsbyrecord)
fieldsbyrecord = [fields + ['']*(maxfields - len(fields))
  for fields in fieldsbyrecord]
if debug: print padded fieldsbyrecord:; print (fieldsbyrecord)
# rotate
fieldsbycolumn = zip(*fieldsbyrecord)
if debug: print fieldsbycolumn:; print (fieldsbycolumn)
# calculate max fieldwidth per column
colwidths = [max(len(field) for field in column)
 for column in fieldsbycolumn]
if debug: print colwidths:; print (colwidths)
# pad fields in columns to colwidth with spaces
# (Use -width for left justification,)
fieldsbycolumn = [ [%*s % (-width if left_justified else width, field) for 
field in column]
   for width, column in zip(colwidths, fieldsbycolumn) ]
if debug: print padded fieldsbycolumn:; print (fieldsbycolumn)
# rotate again
fieldsbyrecord = zip(*fieldsbycolumn)
if debug: print padded rows and fields, fieldsbyrecord:; print 
(fieldsbyrecord)
# printit
print \n# Column-aligned output:
for record in fieldsbyrecord:
  print  .join(record)
  

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

Re: Is there any way to find out sizeof an object

2008-06-24 Thread Paddy
On Jun 24, 6:00 am, srinivasan srinivas [EMAIL PROTECTED]
wrote:
 Hi,
 I have written a class which has some attributes. I want to know how do i 
 find out the size of an instance of this class??
 class T(object):
     def __init__(self, fn_name, *args, **kwds):
         self.fn_name = fn_name
         self.args = args
         self.kwds = kwds
 Thanks,
 Srini

       Bollywood, fun, friendship, sports and more. You name it, we have it 
 onhttp://in.promos.yahoo.com/groups/bestofyahoo/

Check memory
Create a million
Check memory
Do maths!

;-)

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Ruby doctest

2008-06-22 Thread Paddy
I monitor changes to the Wikipedia doctest page, and so noticed a
change in the Ruby implementation of doctest.

Scooting around their implementation I found that they have an !!!
special directive that says drop into the interpreter at this point
when testing allowing debugging in context.
http://github.com/tablatom/rubydoctest/wikis/special-directives

- Paddy.

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


Re: ISO dict = xml converter

2008-06-20 Thread Paddy
On Jun 20, 1:37 pm, kj [EMAIL PROTECTED] wrote:
 Hi.  Does anyone know of a module that will take a suitable Python
 dictionary and return the corresponding XML structure?

 In Perl I use XML::Simple's handy XMLout function:

   use XML::Simple 'XMLout';
   my %h = ( 'Foo' = +{
                         'Bar' = +{
                                     'Baz' = [ { 'meenie' = 3 },
                                                { 'meenie' = 7 } ],
                                     'eenie' = 4,
                                   },
                         'minie' = 1,
                         'moe' = 2,
                       } );

   print XMLout( \%h, KeepRoot = 1, KeyAttr = undef );
   __END__
 Foo minie=1 moe=2
   Bar eenie=4
     Baz meenie=3 /
     Baz meenie=7 /
   /Bar
 /Foo

 Is there a Python module that can do a similar conversion from
 a Python dict to an XML string?

 (FWIW, I'm familiar with xml.marshal.generic.dumps, but it does
 not produce an output anywhere similar to the one illustrated
 above.)

 TIA!

 Kynn

 --
 NOTE: In my address everything before the first period is backwards;
 and the last period, and everything after it, should be discarded.

Try:
  http://pyxml.sourceforge.net/topics/howto/node26.html

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pattern Matching Over Python Lists

2008-06-19 Thread Paddy
On Jun 20, 1:44 am, Chris [EMAIL PROTECTED] wrote:
 Thanks for your help. Those weren't quite what I was looking for, but
 I ended up figuring it out on my own. Turns out you can actually
 search nested Python lists using simple regular expressions.

Strange?
How do you match nested '[' ... ']' brackets?

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mapping None. Why?

2008-06-13 Thread Paddy
On Jun 13, 12:49 pm, David C. Ullrich [EMAIL PROTECTED] wrote:
 On Thu, 12 Jun 2008 12:05:02 -0700 (PDT), Paddy

 [EMAIL PROTECTED] wrote:

 Iam wondering why the peculiar behavior of map when the function in
 given as None:

 If you start with a value x and then apply no function
 at all to it, what results is x.

 David C. Ullrich

True, but None is not a function. It's a sentinel value to turn on the
functionality.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Summing a 2D list

2008-06-13 Thread Paddy
On Jun 13, 1:12 pm, Karsten Heymann [EMAIL PROTECTED]
wrote:
 Hi Mark,



 Mark [EMAIL PROTECTED] writes:
  I have a scenario where I have a list like this:

  UserScore
  1 0
  1 1
  1 5
  2 3
  2 1
  3 2
  4 3
  4 3
  4 2

  And I need to add up the score for each user to get something like
  this:

  UserScore
  1 6
  2 4
  3 2
  4 8

  Is this possible? If so, how can I do it? I've tried looping through
  the arrays and not had much luck so far.

 Although your problem has already been solved, I'd like to present a
 different approach which can be quite a bit faster. The most common
 approach seems to be using a dictionary:

 summed_up={}
 for user,vote in pairs:
   if summed_up.has_key(user):
 summed_up[user]+=vote
   else:
 summed_up[user]=vote

 But if the list of users is compact and the maximum value is known
 before, the using a list and coding the user into the list position is
 much more elegant:

 summed_up=list( (0,) * max_user )
 for user,vote in pairs:
   summed_up[user] += vote

 I've run a quick and dirty test on these approaches and found that the
 latter takes only half the time than the first. More precisely, with
 about 2 million pairs, i got:

 * dict approach: 2s
 (4s with try: ... except KeyError: instead of the if)
 * list approach: 0.9s

 BTW this was inspired by the book Programming Pearls I read some
 years ago where a similar approach saved some magnitudes of time
 (using a bit field instead of a list to store reserved/free phone
 numbers IIRC).

 Yours,
 Karsten

How does your solution fare against the defaultdict solution of:

d = collections.defaultdict(int)
for u,s in zip(users,score): d[u] += s


- Paddy
--
http://mail.python.org/mailman/listinfo/python-list


Re: Counting things fast - was Re: Summing a 2D list

2008-06-12 Thread Paddy
On Jun 12, 4:14 pm, Gerhard Häring [EMAIL PROTECTED] wrote:
 Aidan wrote:
  does this work for you?

  users = [1,1,1,2,2,3,4,4,4]
  score = [0,1,5,3,1,2,3,3,2]

  d = dict()

  for u,s in zip(users,score):
if d.has_key(u):
  d[u] += s
else:
  d[u] = s

  for key in d.keys():
print 'user: %d\nscore: %d\n' % (key,d[key])

 I've recently had the very same problem and needed to optimize for the
 best solution. I've tried quite a few, including:

 1) using a dictionary with a default value

 d = collections.defaultdict(lambda: 0)
 d[key] += value

SNIP
 -- Gerhard

This might be faster, by avoiding the lambda:

d = collections.defaultdict(int)
d[key] += value

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Mapping None. Why?

2008-06-12 Thread Paddy

Iam wondering why the peculiar behavior of map when the function in
given as None:

Help on built-in function map in module __builtin__:

map(...)
map(function, sequence[, sequence, ...]) - list

Return a list of the results of applying the function to the items
of
the argument sequence(s).  If more than one sequence is given, the
function is called with an argument list consisting of the
corresponding
item of each sequence, substituting None for missing values when
not all
sequences have the same length.  If the function is None, return a
list of
the items of the sequence (or a list of tuples if more than one
sequence).


It seems as the action whith none is the same as using a function of
  lambda *x: x
As in the following example:

 l1 = 'asdf'
 l2 = 'qwertyuip'
 l3 = range(3)
 l1,l2,l3
('asdf', 'qwertyuip', [0, 1, 2])
 map(lambda *x: x, l1,l2,l3) == map(None, l1,l2,l3)
True



On looking up map on Wikipedia there is no mention of this special
behaviour,
So my question is why?

Thanks,  Paddy.

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


Re: Mapping None. Why?

2008-06-12 Thread Paddy
On Jun 12, 8:55 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:

 And the OP's question was about map not being conforming to the
 definition on wikipedia - which I don't think it's not. It is not
 defined what map is to do with None (or NULL or nil or... ) as argument.

 Diez

Oh no!
Sorry to give that impression. I don't think that map should be like
what Wikipedia says, I was just looking for another example of an
implementation that might mention the behaviour.

I just want to know the thoughts behind this behaviour in the Python
map.

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mapping None. Why?

2008-06-12 Thread Paddy
On Jun 12, 9:48 pm, Robert Kern [EMAIL PROTECTED] wrote:
 Paddy wrote:
  On looking up map on Wikipedia there is no mention of this special
  behaviour,
  So my question is why?

 My question is why you are looking up the semantics of Python functions on
 Wikipedia instead of the Python documentation. I don't see any particular
 discussion of map() there at all. Am I missing something?

 --
 Robert Kern

As I said in an answer to Diez B. Roggish, I had been reminded of the
behaviour, thought it odd, and looked for other implementations that
might have the behaviour via wikipedia. My intension was most
definitely NOT to say that Pythons map should do what Wikipedia says
slavishly.

Sometimes when I pick at these seeming inconsistencies I learn a lot
from the c.l.p replies. Someone elses thread on -0.0 versus +0.0
taught me some more on floating point for example.

- Paddy.


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


Re: Mapping None. Why?

2008-06-12 Thread Paddy
On Jun 12, 9:36 pm, Terry Reedy [EMAIL PROTECTED] wrote:
 Paddy [EMAIL PROTECTED] wrote in message

 news:[EMAIL PROTECTED]
 |
 | Iam wondering why the peculiar behavior of map when the function in
 | given as None:

 The 'peculiar behavior' is the same as zip (except for padding short
 iterators versus truncating long iterators.  Map was added years before
 zip.  After that, map(None,...) was kept for back compatibility.

 In 3.0, the doc for map is
 Return an iterator that applies function to every item of iterable,
 yielding the results. If additional iterable arguments are passed, function
 must take that many arguments and is applied to the items from all
 iterables in parallel. With multiple iterables, the iterator stops when the
 shortest iterable is exhausted.

 Using a map defined with None raises
 TypeError: 'NoneType' object is not callable

 tjr

I really should get into the habit of reading the 3.0 docs before
asking questions :-)

My original question came about after answering this query:
 http://gmcnaughton.livejournal.com/27955.html?thread=70451#t70451


- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: value is in list?

2008-06-12 Thread Paddy
On Jun 12, 11:46 pm, Steven Clark [EMAIL PROTECTED] wrote:
  Hello ,
  following scenario

  list_current = [ welcome, search, done, result]
  list_ldap = [ welcome, hello]

  result:

  list_toadd = [ hello]

  by words said , i want to check if list item from list_ldap exists in
  list_current if not i want to add it to list_toadd.

  Thanks!

  D.

 list_toadd = [i for i in list_ldap if i not in list_current]
 seems to work.

 I'm sure there's a way to do it with set objects as well.
 -Steven

 list_current = [ welcome, search, done, result]
 list_ldap = [ welcome, hello]
 to_add = set(list_ldap) - set(list_current)
 to_add
set(['hello'])

- Paddy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q re documentation Python style

2008-06-08 Thread Paddy
On Jun 8, 10:17 pm, kj [EMAIL PROTECTED] wrote:
 I'm a Perlhead trying to learn the Way of Python.  I like Python
 overall, but every once in a while I find myself trying to figure
 out why Python does some things the way it does.  At the moment
 I'm scratching my head over Python's docstrings.  As far as I
 understand this is the standard way to document Python code.  I
 think that's fine for simple functions, but I have some functions
 that require a very long docstring to document, and somehow I find
 it a bit disconcerting to stick a few screenfuls of text between
 the top line of a function definition and its body.
Hi Kynn,
Strings on their own are also valid statements so if in Perl you might
documentbefore the sub statement or even after the function definition
then you could put extra information in a tripple quoted string
statement before your function and put the bare essentials in the
function itself. This would leave the def nearer the body of the
function, but I don't know of anyone else that does this.

- Paddy.

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


  1   2   3   4   5   6   7   >