rfile.readline()

2010-08-28 Thread sarah
i want to know that what this function returns???

and what does this function do??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Terry Reedy writes:
 On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:
  Dave Angel writes:
 
  There could easily be a .reverse() method on strings. It would return
  the reversed string, like .swapcase() returns the swapcased string.
 
 Could be, but the main use case seems to be for palindrome testing ;-)
 Given that slicing and reversed() can do the same thing, the need is thin.

The need is quite thin, but immutability of strings is not an issue,
just like there can be .swapcase() though strings are immutable. That
is all I am saying above.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Walking deeply nested lists

2010-08-28 Thread Peter Otten
donn wrote:

 * If an Instance calls a method on another Instance of the same
 class, is this still recursion? And how does this 'stack up'? I mean,
 literally, on the stack. Does each instance get its own stack, or does
 all the push, call, pop stuff happen in one main stack?
 (I worry about recursion depth limits because svg trees can get quite
 deep.)

If you call functions within functions (or methods, it doesn't matter) they 
consume stack space, e. g: 

 def alpha():
... return beta()
...
 def beta():
... return gamma()
...
 import random
 def gamma():
... return random.choice([alpha, beta, gamma])()
...
 import sys
 sys.setrecursionlimit(10)
 alpha()
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 2, in alpha
  File stdin, line 2, in beta
  File stdin, line 2, in gamma
  File stdin, line 2, in gamma
  File stdin, line 2, in alpha
  File stdin, line 2, in beta
  File stdin, line 2, in gamma
  File stdin, line 2, in beta
  File stdin, line 2, in gamma
RuntimeError: maximum recursion depth exceeded

The normal recursion limit is 1000, I'm reducing it to give you a smaller 
traceback.

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


Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Richard Arts writes:

 On Fri, Aug 27, 2010 at 10:51 PM, Jussi Piitulainen wrote:

 Meanwhile, I have decided to prefer this:

 def palindromep(s):
    def reversed(s):
        return s[::-1]
    return s == reversed(s)
 
 That seems like a bit of overkill... Why would you want to define a
 function in a function for something trivial like this? Just
 
 def palindrome(s):
 return s[::-1]
 
 will do fine.

I'm sure your version will do something just fine, but what that
something is, I can not tell. The body of your version is quite
obscure and does not seem to agree with the name of the function.

I find (s == reversed(s)) a clearer expression than (s == s[::-1]),
and I found a simple way to use my preferred expression.

 Of course, you can stick the inner function in a library somewhere
 if you like.

From my point of view, it would be an understatement to say that
setting up a library for this would be an overkill. A simple local
auxiliary function is nothing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rfile.readline()

2010-08-28 Thread Peter Otten
sarah wrote:

 i want to know that what this function returns???
 and what does this function do??

Where do you get rfile from?

The readline() function (or method) could read one line from a file (likely) 
or wipe your harddisk (unlikely). Without some context it is impossible to 
tell.

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


Re: rfile.readline()

2010-08-28 Thread Chris Rebert
On Fri, Aug 27, 2010 at 10:57 PM, sarah maral.nik...@gmail.com wrote:
 i want to know that what this function returns???

 and what does this function do??

RTFM:
http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects

Honestly, it's the 2nd google hit for python readline.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Walking deeply nested lists

2010-08-28 Thread Arnaud Delobelle
donn donn.in...@gmail.com writes:

 This is all about walking trees, recursion and generators. None of
 which fit my brain at all!

 From an XML tree (an SVG file) I build a bunch of Tag objects.

 [I use lxml, but I am combining multiple svg files into a 'forest' of
 trees, so I can't use the lxml walking methods because they all stop
 at the 'end' of a branch where there is actually a 'link' over to
 another tree.]

 Each Tag has a flatwalk() method. The return from that is a list which
 can be deeply nested. As an example, something like this:
 L=[1, [2, 3, [4, [5, 6], 7], 8], [9, 10] ]

 (The numbers in the example would actually be Tag Instances.)

 Aim 1
 ---
 I'm trying to write a generator around such lists so that I can 'walk' them:

 for parent, child in mystery_generator(L):
  print level, item

 Right now, I am running a 'flatten' function on the entire list (which
 I don't savvy, I found it online) and then I iterate over that
 flattened list. This doesn't feel right to me. (Code at end.)

 Aim 2
 ---
 The Objects that represent the svg tags use that flatwalk() method to
 build the nested list, I'd far prefer flatwalk() to be directly
 useable in something like this way:

 for container, child in some_tag.flatwalk():
  print container, child

 The flatwalk() function is (and this is another puzzle see *) kind of
 recursive. For each of my children, tell that child to go
 flatwalk().
 (Code at end.)
 I am not sure how to turn such a thing into a generator.

 I keep wondering how os.walk() does its thing. My hierarchy of Tag
 objects can be thought of as directories (tags:g, symbol, svg), files
 (path, circle, etc.) and soft-links (use tags).

 * If an Instance calls a method on *another* Instance of the *same*
 class, is this still recursion? And how does this 'stack up'? I mean,
 literally, on the stack. Does each instance get its own stack, or does
 all the push, call, pop stuff happen in one main stack?
 (I worry about recursion depth limits because svg trees can get quite deep.)


 The walking code so far:

 ## Found code.
 def flatten(input):
  output = []
  stack = []
  stack.extend(reversed(input))
  while stack:
   top = stack.pop()
   if isinstance(top, list):
stack.extend(reversed(top))
   else:
output.append(top)
  return output

not a bad idea.  I would rather write it as:

def flatten(input):
output = []
stack = list(input)
while stack:
top = stack.pop()
if isinstance(top, list):
stack.extend(top)
else:
output.append(top)
output.reverse()
return output

If you want to make it a generator function though, the initial version
is better.  All you need to do is:

* change the line output.append(top) to yield top
* delete the line return output

Or you can go for the simple recursive approach:

def flatten(lst):
for el in lst:
if isinstance(el, list):
for x in flatten(el):
yield x
else:
yield el

 ## My walker
 def test_walk(e): #lxml Element comes in
  obj = ebag_get(e)['obj'] #get a tag object
  l=obj.flatwalk()
  ll= flatten(l)
  #No current solution to get 'parent' in this iterator
  #ie. for parent, child in ...
  for tag in ll:
   print tag

 Here's one of my Tag objects:

 class Brancher(object):
   def __init__(self, elem):
 self.elem = elem
 self.children = []

   def flatwalk(self):
 l=[self]
 for child in self.children:
   l.append( child.flatwalk() ) #recur(ish)ion here
 return l

This flattens the list in the flatwalk method (which IMHO it should do
given its name!):

def flatwalk(self):
flatlist = [self]
for child in self.children:
for el is child.flatwalk():
flatlist.append(el)
return flatlist

This turns it into a generator method:

def flatwalk(self):
yield self
for child in self.children:
for el is child.flatwalk():
yield el

This turns it into a generator method which yields parents as well:

def flatwalk(self, parent=None):
yield self, parent
for child in self.children:
for el is child.flatwalk(self):
yield el

Then you can write:

for tag, parent in mytag.flatwalk():
...

Of course, for the above to work, leaf objects need a modified
flatwalk method, e.g.:

Class LeafTag:
 def flatwalk(self, parent=None):
 yield self, parent

HTH (warning: all code untested).

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


Re: Walking deeply nested lists

2010-08-28 Thread donn

On 28/08/2010 08:43, Peter Otten wrote:

If you call functions within functions (or methods, it doesn't matter) they
consume stack space


Right, got it. Darn, but at least there's that setrecursionlimit call.

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


Re: Queue cleanup

2010-08-28 Thread Paul Rubin
Dennis Lee Bieber wlfr...@ix.netcom.com writes:
   The nice thing about it [reference counting] is that it is sort
 of deterministic -- one can examine code and determine that an object
 is collected at some point in the execution...
   Heap marking, OTOH, tends to run at indeterminate times, which could
 have an impact if one needs predictable response timings

Reference counting has the same problem.  If you drop the last reference
to a complex structure, it could take quite a long time to free all the
components.  By contrast there are provably real-time tracing gc
schemes, including some parallelizeable ones.  One reason CPython still
can't run threads on parallel cores is it would have to lock the
reference counts every time they're updated, and the slowdown from that
is terrible.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Walking deeply nested lists

2010-08-28 Thread donn

On 28/08/2010 09:21, Arnaud Delobelle wrote:


This flattens the list in the flatwalk method (which IMHO it should
do given its name!):

Heh, I often name things ahead of my actual capacity to implement them!


el is child.flatwalk():

Ah, I see what you mean. I think 'is' is 'in', but I kind of get the idea.


This turns it into a generator method:

And thanks for the generator versions too. I shall hack them in and poke
them with a stick.


Of course, for the above to work, leaf objects need a modified
flatwalk method, e.g.:

Yes, My 'stubs' (leaves) do have such, but I will edit to use yield.

Thanks a mill.
\d
--
http://mail.python.org/mailman/listinfo/python-list


Re: ftplib limitations?

2010-08-28 Thread Stefan Schwarzer
Hi Lawrence,

On 2010-08-28 01:49, Lawrence D'Oliveiro wrote:
 Now it may be that the data connection, after having started
 the transfer, works as it should, but the control connection
 times out because the duration of the transfer is too long.
 
 It might not be the fault of the FTP server. If you’re going through a 
 router doing NAT, that could be where the timeout is happening.

Good point, thanks! That may explain why it's a low-level
socket error instead of a 4xx timeout message from the
server which I would have expected.

If it's the router, the OP might try to change their router
settings to get rid of the problem.

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


Re: palindrome iteration

2010-08-28 Thread Ian

 On 27/08/2010 21:51, Jussi Piitulainen wrote:

Meanwhile, I have decided to prefer this:

def palindromep(s):
 def reversed(s):
 return s[::-1]
 return s == reversed(s)

I like this.

s[::-1] is obscure and non-obvious, especially to Python noobs.

This makes it clear what is going on and why at a cost of very little code.

Very helpful to the maintenance programming in 18 months time!

Regards

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


Re: How to convert (unicode) text to image?

2010-08-28 Thread Dave Angel

kj wrote:

Hi!  Does anyone know of an easy way to convert a Unicode string into an image 
file (either jpg or png)?

TIA!

~k

  

The question has no meaning as presently worded.

If you have Unicode text that you need to render, so that you end up 
with an image of the text, as printed in some particular font and style, 
you'd probably start with PIL.  Or perhaps one of the gui packages, like 
tkinter, wxpython, etc.


If you have Unicode that was produced by trying to decode some jpeg 
image, then back up and don't do that.  See the recent thread called  
Writing byte stream as jpeg format to disk.  The OP there had run a 
unicode decode on a byte stream that represented a jpeg file, and then 
tried to encode it again to get the jpeg data.  Bad idea.


If you have Unicode that specifies a file path that refers to a jpeg 
file, then you need to open the file, in rb mode, and copy it.


If you have Unicode that gives the name of a person, and you want a 
portrait of that person, you need to call a photographer() function.



The last one was a weak attempt at humor.

DaveA

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


Re: Walking deeply nested lists

2010-08-28 Thread Carl Banks
On Aug 27, 8:21 pm, donn donn.in...@gmail.com wrote:

 Each Tag has a flatwalk() method. The return from that is a list which
 can be deeply nested. As an example, something like this:
 L=[1, [2, 3, [4, [5, 6], 7], 8], [9, 10] ]

 (The numbers in the example would actually be Tag Instances.)

 Aim 1
 ---
 I'm trying to write a generator around such lists so that I can 'walk' them:

 for parent, child in mystery_generator(L):
   print level, item

 Right now, I am running a 'flatten' function on the entire list (which I
 don't savvy, I found it online) and then I iterate over that flattened
 list. This doesn't feel right to me. (Code at end.)


Hmm.

In the past I've argued that iterative techniques are superior to
recursive approaches in terms of readability, understandability, and
conciseness, and thus Python made the right decision to stress
iteration over the Lisp/functional preference for recursion.

I did consider recursion to be superior to operate on branched
structures like trees.  However, lately I've started thinking it's
better to use iterative techniques even for situations like that.  I
say that as someone with no problem getting my head around recursion.

Even if you disagree, I think there's value in learning iterative
approaches to nested problems, in the same way that there's value to
learning recursive approaches to linear problems.  So here it is:


def flatten_iter(s):
stack = list()
stack.extend(reversed(s))
while stack:
item = stack.pop()
if isinstance(item,list):
stack.extend(reversed(item))
else:
yield item


It's simple. Copy the object to flatten onto your stack. Pop one item
off the stack. If the item you popped is a list, push each item of
that list onto the stack. Otherwise yield the value. Loop until stack
is empty.

There's many advantages to iterative approaches:
1. Less function call overhead (in time and space, I'd think)
2. Opportunity to optimize by scanning through the stack, something
you can't do *at all* with recursion
3. Might be able to avoid things like passing around a namespace
4. Iteration is more readable, understandable, and concise in
general (though I'd expect recursion is more refactorable than
iteration so as the system grows the refactorability of recursion will
start to outweigh other factors)

The main advantage of recursion is if you have baggage associated with
processing a node which does needed to be passed around.  In the
iterative approach that state has to be stored on the stack.  So in
those cases recursion is better.

So I'm not suggesting that recursion be avoided (I've probably written
a dozen recursive functions in the last week), I'm just saying
sometimes it makes sense to use iteration even for problems recursion
is tailor-made for.


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


Re: palindrome iteration

2010-08-28 Thread Paul Rubin
Ian hobso...@gmaiil.com writes:
  On 27/08/2010 21:51, Jussi Piitulainen wrote:
 Meanwhile, I have decided to prefer this:

 def palindromep(s):
  def reversed(s):
  return s[::-1]
  return s == reversed(s)
 I like this.
 s[::-1] is obscure and non-obvious, especially to Python noobs.

Overriding the 'reversed' builtin even in an inner scope is a little bit
ugly.

If you don't mind some overhead, list(s)==list(reversed(s)) (using the
built-in reversed, not the special obscure one) is pretty clear.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rfile.readline()

2010-08-28 Thread Dave Angel

sarah wrote:

i want to know that what this function returns???

and what does this function do??

  

rfile.readline()

No way to tell what the function returns from your subject line.

As for what calling it does, that depends entirely on the object rfile.  
If it's an instance of a class you wrote, you'll have to look up in your 
source code.  If it's an instance of one of the built-in system types, 
or of a class in the standard library, then you can tell by looking up 
that type.


I see standard classes  bz2.BZ2File, codecs.StreamReader, 
distutils.text_file, file, imaplib, mmap, io.IOBase, io.TextIOBase, and 
multifile.   These were from the docs for Python 2.6.


Most of these are probably analogous to file, in which case I can elaborate.

If rfile is an instance of file, perhaps by doing
   rfile = open(myfile.txt, r)

then  rfile.readline() reads one line from that file, starting at the 
current position, and leaves the file position after that line.  
readline() stops when it reaches a newline (which it may convert, 
depending on the setting of 'rb' versus 'r'), or when it reaches end of 
file.  The trailing newline (if any) is included in the returned string.


That string may be in byte form (Python 2.x), or in unicode (Python 
3.x).  In the latter case, it has been decoded according to parameters 
of the particular open file.


DaveA

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


Re: palindrome iteration

2010-08-28 Thread Arnaud Delobelle
Paul Rubin no.em...@nospam.invalid writes:

 Ian hobso...@gmaiil.com writes:
  On 27/08/2010 21:51, Jussi Piitulainen wrote:
 Meanwhile, I have decided to prefer this:

 def palindromep(s):
  def reversed(s):
  return s[::-1]
  return s == reversed(s)
 I like this.
 s[::-1] is obscure and non-obvious, especially to Python noobs.

It may be non-obvious to newcomers, but it is quite a well known idiom.
Also, I an not aware that it is customary in python to name predicate
functions with a p suffix - Python is not Lisp!


 Overriding the 'reversed' builtin even in an inner scope is a little bit
 ugly.

I agree.

 If you don't mind some overhead, list(s)==list(reversed(s)) (using the
 built-in reversed, not the special obscure one) is pretty clear.

May I suggest a comment instead:

def ispalindrome(s):
# s[::-1] evaluates to the string s reversed
return s == s[::-1]

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


Re: Walking deeply nested lists

2010-08-28 Thread Peter Otten
donn wrote:

 On 28/08/2010 08:43, Peter Otten wrote:
 If you call functions within functions (or methods, it doesn't matter)
 they consume stack space
 
 Right, got it. Darn, but at least there's that setrecursionlimit call.

But be warned that if you set the limit too high instead of giving you a 
RuntimeError your program will segfault.

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


Re: Walking deeply nested lists

2010-08-28 Thread Carl Banks
On Aug 28, 3:03 am, Peter Otten __pete...@web.de wrote:
 donn wrote:
  On 28/08/2010 08:43, Peter Otten wrote:
  If you call functions within functions (or methods, it doesn't matter)
  they consume stack space

  Right, got it. Darn, but at least there's that setrecursionlimit call.

 But be warned that if you set the limit too high instead of giving you a
 RuntimeError your program will segfault.

Ah, an advantage of iteration I forgot: no recursion limits or stack
overflows.


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


Re: Queue cleanup

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 00:33:10 -0700, Paul Rubin wrote:

 Dennis Lee Bieber wlfr...@ix.netcom.com writes:
  The nice thing about it [reference counting] is that it is sort
 of deterministic -- one can examine code and determine that an object
 is collected at some point in the execution...
  Heap marking, OTOH, tends to run at indeterminate times, which 
could
 have an impact if one needs predictable response timings
 
 Reference counting has the same problem.  

In theory, yes, but in practice ref counting tends to spread out the 
performance impact more smoothly. There are exceptions, such as the one 
you mention below, but as a general rule ref counting isn't subject to 
the embarrassing pauses that tracing garbage collectors tend to be 
subject to.


 If you drop the last reference
 to a complex structure, it could take quite a long time to free all the
 components.  By contrast there are provably real-time tracing gc
 schemes, including some parallelizeable ones.

I could be wrong, but how can they not be subject to the same performance 
issue? If you have twenty thousand components that all have to be freed, 
they all have to be freed whether you do it when the last reference is 
cleared, or six seconds later when the gc does a sweep.


 One reason CPython still
 can't run threads on parallel cores is it would have to lock the
 reference counts every time they're updated, and the slowdown from that
 is terrible.

On the other hand, the reason that CPython still has reference counting 
is that the alternatives tried so far are unacceptably for non-threaded 
code.




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


Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 09:48:47 +0100, Ian wrote:

 On 27/08/2010 21:51, Jussi Piitulainen wrote:
 Meanwhile, I have decided to prefer this:

 def palindromep(s):
  def reversed(s):
  return s[::-1]
  return s == reversed(s)
 I like this.

It's silly, needlessly complicated, and inefficient. Why create a *one 
line* nested function that only gets called once? Every single time you 
call the function, it has to create the inner function again, then call 
it once, then throw it away. Admittedly Python does recreate the inner 
function from pre-compiled parts, which is quick, but still, it doesn't 
gain you anything that a simple comment wouldn't give:

def palindromep(s):
return s == s[::-1]  # Compare s to its reverse.


 s[::-1] is obscure and non-obvious, especially to Python noobs.

*Only* to Python noobs. Slicing is fundamental to Python, and using a 
slice of [::-1] to reverse something is a basic Python idiom.


 This makes it clear what is going on and why at a cost of very little
 code.
 
 Very helpful to the maintenance programming in 18 months time!

Only if writing three lines when one would do is your definition of 
helpful.




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


Re: How to convert (unicode) text to image?

2010-08-28 Thread kj
In mailman.123.1282955703.29448.python-l...@python.org Benjamin Kaplan 
benjamin.kap...@case.edu writes:

On Fri, Aug 27, 2010 at 8:01 PM, kj no.em...@please.post wrote:


 Hi! =A0Does anyone know of an easy way to convert a Unicode string into a=
n image file (either jpg or png)?


Do you mean you have some text and you want an image containing that
text? PIL's ImageDraw module can do that.

Thanks for the pointer, but...

RANT
The documentation I have found for PIL (at
http://www.pythonware.com/library/pil/handbook) is beyond atrocious.
If this is the only way to learn how to use this library, then I
really don't understand how anyone who is not clairvoyant can do it.

Example: I went to the docs page for ImageDraw.  There I find that
the constructor for an ImageDraw.Draw object takes an argument,
but *what* this argument should be (integer? object? string?) is
left entirely undefined.  From the examples given I *guessed* that
it was an object of class Image, so I repeated the exercise: I
consulted the docs for the Image module.  There I learn that the
constructor for the Image class takes among its parameters one
called mode and one called color, but, here again, what these
parameters are is left completely undefined.  (mode is left both
syntactically and semantically undefined; color is left syntactically
undefined, though the documentation includes a bit by way of semantic
definition of this parameter.)

What's up with this practice of leaving parameters undefined like
this???  Wasn't it obvious to the person writing the Image module
docs that without explaining what these parameters should be the
documentation is nearly useless?  Is such poor documentation an
unintended consequence of duck typing???

Sorry for the outburst, but unfortunately, PIL is not alone in
this.  Python is awash in poor documentation.

The number two complaint I've heard from those who dislike Python
is the poor quality of its documentation, and in particular the
fact that function parameters are typically left undefined, as is
the case in the PIL docs.  I like Python a lot, but I have to agree
with this criticism.  (The number one complaint has to do with the
syntactic significance of whitespace; of course, I find *this*
criticism silly.)

What is most frustrating about such poor documentation is that it
is exactly the opposite from what one would expect from the
carefulness and thoroughness found in the PEPs...

I have been using Python as my primary scripting language for about
one year, after many years of programming in Perl, and now Python
is my language of choice.  But I must say that the documentation
standards I found in the Perl world are *well above* those in the
Python world.  This is not to say that Perl documentation is always
excellent; it certainly has its gaps, as one would expect from
volunteer-contributed software.  But I don't recall being frustrated
by Perl module docs anywhere nearly as often as I am by Python
module docs.  I have to conclude that the problem with Python docs
is somehow systemic...

/RANT

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


Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote:

 Terry Reedy writes:
 On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:
  Dave Angel writes:
 
  There could easily be a .reverse() method on strings. It would return
  the reversed string, like .swapcase() returns the swapcased string.
 
 Could be, but the main use case seems to be for palindrome testing ;-)
 Given that slicing and reversed() can do the same thing, the need is
 thin.
 
 The need is quite thin, but immutability of strings is not an issue,
 just like there can be .swapcase() though strings are immutable. That is
 all I am saying above.


You're right, there could be a reversed() method for strings. There could 
also be a disemvowel method that removes vowels, a randomise method that 
shuffles the letters around, a studlycaps method that changes the case of 
each letter randomly, and a method to check that brackets () are well-
formed. They would all be useful to somebody. There are lots of different 
methods that strings could have. Where do you draw the line?

Not everything needs to be a built-in method. There is already a standard 
way to spell reverse a string:

astring[::-1]

If you don't like that, you can do this:

''.join(reversed(astring))


I don't object to a hypothetical reverse() method on strings, but the 
gain is minimal.



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


Simple hack to get $5000 to your *Paypal account

2010-08-28 Thread Hot Hot Hot
Simple hack to get $5000 to your *Paypal account At http://ucanget.co.cc
i have hidden the Paypal Form link in an image. in that website on
Right Side below search box, click on image and enter your name and
Paypal ID.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ftplib limitations?

2010-08-28 Thread Lawrence D'Oliveiro
In message 4c78cc0d.8050...@sschwarzer.net, Stefan Schwarzer wrote:

 In message i59iug$eu...@lust.ihug.co.nz, Lawrence D'Oliveiro wrote:
 
 It might not be the fault of the FTP server. If you’re going through a
 router doing NAT, that could be where the timeout is happening.
 
 Good point, thanks! That may explain why it's a low-level
 socket error instead of a 4xx timeout message from the
 server which I would have expected.

The reason why I thought of it was because it kept happening to me back when 
I was using a D-Link DSL-500 to provide my ADSL connection. Unfortunately...

 If it's the router, the OP might try to change their router
 settings to get rid of the problem.

... if they’re using a typical consumer ADSL router box like the above, they 
may not have any NAT table timeout settings to play with to cure the 
problem. I certainly couldn’t find any in mine.

In my case, I solved the problem by using a Linux box as my router.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Queue cleanup

2010-08-28 Thread Steven D'Aprano
On Fri, 27 Aug 2010 18:06:19 -0700, Paul Rubin wrote:

 Steven D'Aprano st...@remove-this-cybersource.com.au writes:
 I've repeatedly asked, both here and elsewhere, why reference counting
 isn't real garbage collection. Nobody has been able to give me a
 satisfactory answer. As far as I can tell, it's a bit of
 pretentiousness with no basis in objective fact.
 
 Well, it's a bit of a subjective matter.  I'd say it's not real gc
 because 1) it's unsound (misses reference cycles), 

You can add cycle detection to a reference count gc, at the cost of more 
complexity.

If you read the Wikipedia article I linked to, tracing algorithms can 
also be unsound:

Some collectors running in a particular environment can 
correctly identify all pointers (references) in an object; 
these are called precise (also exact or accurate) 
collectors, the opposite being a conservative or partly
conservative collector. Conservative collectors have to 
assume that any bit pattern in memory could be a pointer if 
(when interpreted as a pointer) it would point into any 
allocated object. Thus, conservative collectors may have 
some false negatives, where storage is not released because 
of accidental fake pointers...

http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)



 and 2) it requires
 constant attention from the mutator to incr and decr the reference
 counts.

Yes. And?


 So developing modules for the CPython API means endlessly
 finding and fixing refcount bugs that lead to either crashes/security
 failures, or memory leaks.  If you program the Java JNI or a typical
 Lisp FFI, you'll find that real gc is a lot simpler to use since you
 avoid all the refcount maintenance hassles.  You allocate memory and
 shut your eyes, and the gc takes care of freeing it when it figures out
 that you are done.  Refcounting is basically a form of manual memory
 management, while gc is automatic.


That's a problem with the CPython API, not reference counting. The 
problem is that the CPython API is written at too low a level, beneath 
that at which the garbage collector exists, so naturally you have to 
manually manage memory.


 Someone said here recently that as a program gets larger, saying this
 will work as long as we do X every time without fail becomes equal to
 saying this won't work.  Substitute properly maintain all ref counts
 for X and you can see the problem.  I've seen released production
 tested Python C modules with subtle refcount bugs on more than one
 occasion.  In gc'd systems there are fewer places for the code to go
 wrong.

On the other hand, tracing gcs have their own set of problems too, mostly 
due to the use of finalizers and attempts to make garbage collection run 
more predictably. See here:

http://publib.boulder.ibm.com/infocenter/javasdk/v1r4m2/topic/com.ibm.java.doc.diagnostics.142j9/html/coexistwithgc.html

Quote:

For tidying Java resources, think about the use of a clean 
up routine. When you have finished with an object, call the 
routine to null out all references, deregister listeners, 
clear out hash tables, and so on. This is far more efficient 
than using a finalizer and has the useful side-benefit of 
speeding up garbage collection. The Garbage Collector does 
not have so many object references to chase in the next 
garbage collection cycle.


Translated: Rather than relying on the garbage collector to clean up 
resources after you, do it yourself, manually, so the garbage collector 
has less work to do.

Tracing garbage collectors aren't a panacea. They're software themselves, 
and complex software, which means they're subject to bugs like the one 
which plagued Flash plugin 9:

http://gskinner.com/blog/archives/2008/04/failure_to_unlo.html

The more complicated the garbage collector, the more scope you have for 
some interaction between your high-level code and the gc leading to 
memory not be reclaimed or extreme slowdown. Like this:

http://tech.puredanger.com/2009/02/11/linkedblockingqueue-garbagecollection/





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


Re: How to convert (unicode) text to image?

2010-08-28 Thread Arnaud Delobelle
kj no.em...@please.post writes:

 Thanks for the pointer, but...

 RANT
 The documentation I have found for PIL (at
 http://www.pythonware.com/library/pil/handbook) is beyond atrocious.
 If this is the only way to learn how to use this library, then I
 really don't understand how anyone who is not clairvoyant can do it.

 Example: I went to the docs page for ImageDraw.  There I find that
 the constructor for an ImageDraw.Draw object takes an argument,
 but *what* this argument should be (integer? object? string?) is
 left entirely undefined.  From the examples given I *guessed* that
 it was an object of class Image, so I repeated the exercise: I
 consulted the docs for the Image module.  There I learn that the
 constructor for the Image class takes among its parameters one
 called mode and one called color, but, here again, what these
 parameters are is left completely undefined.  (mode is left both
 syntactically and semantically undefined; color is left syntactically
 undefined, though the documentation includes a bit by way of semantic
 definition of this parameter.)

The first time you read the PIL docs, read the introduction.  After that
I find the docs pretty easy to use, even though it is true that it is
quite terse.

E.g. for the mode, look at the concepts page in the intro:

http://www.pythonware.com/library/pil/handbook/concepts.htm

 What's up with this practice of leaving parameters undefined like
 this???  Wasn't it obvious to the person writing the Image module
 docs that without explaining what these parameters should be the
 documentation is nearly useless?  Is such poor documentation an
 unintended consequence of duck typing???

 Sorry for the outburst, but unfortunately, PIL is not alone in
 this.  Python is awash in poor documentation.

 The number two complaint I've heard from those who dislike Python
 is the poor quality of its documentation, and in particular the
 fact that function parameters are typically left undefined, as is
 the case in the PIL docs.  I like Python a lot, but I have to agree
 with this criticism.  (The number one complaint has to do with the
 syntactic significance of whitespace; of course, I find *this*
 criticism silly.)

 What is most frustrating about such poor documentation is that it
 is exactly the opposite from what one would expect from the
 carefulness and thoroughness found in the PEPs...

I find the Python docs very good on the whole.

 I have been using Python as my primary scripting language for about
 one year, after many years of programming in Perl, and now Python
 is my language of choice.  But I must say that the documentation
 standards I found in the Perl world are *well above* those in the
 Python world.  This is not to say that Perl documentation is always
 excellent; it certainly has its gaps, as one would expect from
 volunteer-contributed software.  But I don't recall being frustrated
 by Perl module docs anywhere nearly as often as I am by Python
 module docs.  I have to conclude that the problem with Python docs
 is somehow systemic...

I have never programmed in Perl (although I have needed to read some
Perl) but over the years I have used C, C++, lisp variants, PHP, Ruby, Caml
variants, Haskell, Javascript (and others before the era of the
web).  I don't find that Python online docs on the web are worse than
online docs for any of those languages.

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


Re: Walking deeply nested lists

2010-08-28 Thread donn

On 28/08/2010 11:17, Carl Banks wrote:

It's simple. Copy the object to flatten onto your stack. Pop one item
off the stack. If the item you popped is a list, push each item of
that list onto the stack. Otherwise yield the value. Loop until stack
is empty.
Nice. The reversed thing was throwing me, but I think it's so that what 
comes first in a list will thus come first (on the end) of the stack.



So I'm not suggesting that recursion be avoided (I've probably written
a dozen recursive functions in the last week), I'm just saying
sometimes it makes sense to use iteration even for problems recursion
is tailor-made for.

Thanks for that. In parsing the XML (using lxml) I actually did my own 
stack thing with while loops, to build the entire Tag object 'tree' — 
just because I wanted to see how to do it sans recursion. I still get 
cold shakes when I scroll past that monster!


On the subject of recursion, and looking at my OP object model: the Tag 
objects that model the tags in an SVG file; how would I 'walk' the 
object tree without employing recursion?
I am stuck on the eventual need to call child.flatwalk() and bang! 
there's recursion.


I get the sense that it would be an external walk() function that does 
some stackery-trickery and reuturns/yields the tree/branch — all 
divorced from the actual objects. So, no flatwalk() methods needed at 
all. This kind of bothers me because it's nice to have objects 'know' 
what to do and addressing their siblings and children seems a vital part 
of that.


Look at the case of asking a Tag for its XML source:
Say g is a G() instance: print g.get_xml(), would have to do some string 
churning (specific to a g tag) and then start walking its children and 
ask them to do specific string stuff (in their contexts).
 This means I have short methods in each Tag instance that know how 
to represent themselves as XML and they can return that value.
If I divorce the thing, it becomes a large loop with a lot of 
switchy-ifs to engage various blocks of string-fu.


I hope that made sense. I can post my code if that would help.

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


Re: Walking deeply nested lists

2010-08-28 Thread donn

On 28/08/2010 12:03, Peter Otten wrote:

But be warned that if you set the limit too high instead of giving you a
RuntimeError your program will segfault.
Silly question: is there any way to tell the future in this case? I 
mean, ask for X recursion limit, and catch an error (or something) if 
that won't fly.


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


Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Steven D'Aprano writes:
 On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote:
 Terry Reedy writes:
 On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:
  Dave Angel writes:
 
  There could easily be a .reverse() method on strings. It would return
  the reversed string, like .swapcase() returns the swapcased string.
 
 Could be, but the main use case seems to be for palindrome testing ;-)
 Given that slicing and reversed() can do the same thing, the need is
 thin.
 
 The need is quite thin, but immutability of strings is not an issue,
 just like there can be .swapcase() though strings are immutable. That is
 all I am saying above.
 
 You're right, there could be a reversed() method for strings. There
 could also be a disemvowel method that removes vowels, a randomise
 method that shuffles the letters around, a studlycaps method that
 changes the case of each letter randomly, and a method to check that
 brackets () are well- formed. They would all be useful to
 somebody. There are lots of different methods that strings could
 have. Where do you draw the line?

When I said that there could be such a method, I was merely objecting
to a statement, made in response to me, that there could not be such a
method because strings are immutable. You clearly agree with me that
that statement was not correct. Would you have let it stand if it was
made to you?

To answer your question, I don't see a real need for .reversed() in
strings, but I do think .reversed() would be much more useful than
.swapcase() which is in Python now and for which I see no use at all.

I have not proposed adding anything to Python. I have only asked if
there is any nicer expression for string reversal than [::-1] in
Python now, and corrected an incorrect statement that was made in
response to me that there could not be a string reversal method
because Python strings are immutable.

I am still not proposing that anything be added to Python.

I have not even criticized Python for not having a nicer expression
for string reversal than [::-1]. I have merely asked if there is one,
because I didn't know if there is one, and I have shown some snippets
of code to illustrate what I might mean by nicer. Someone even
understood me. (Thanks.)

I think I have received the answer to my question by now - that there
is no obviously nicer way, and all other string reversal expressions
require some extra cruft and overhead.

 Not everything needs to be a built-in method. There is already a
 standard way to spell reverse a string:
 
 astring[::-1]
 
 If you don't like that, you can do this:
 
 ''.join(reversed(astring))

I know. I agree. I was also shown a different way to test for
palindromicity,

list(s) == list(reversed(s))

which is quite nice apart from the overhead.

 I don't object to a hypothetical reverse() method on strings, but
 the gain is minimal.

I have not suggested that such a method should be added to the
language. I merely corrected a statement that there could not be such
a method because strings are immutable. I would not have bothered to
do even that if that incorrect statement had not been made in response
to my own post.

I agree that the gain would be minimal. There is no harm in the method
either, so I would not object to it if somebody were to propose its
addition, but just to clarify my position: I have not proposed it.

Hope this helps.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Arnaud Delobelle writes:

 Also, I an not aware that it is customary in python to name
 predicate functions with a p suffix - Python is not Lisp!

Just to clarify my position: I did not mean to imply that names like
palindromep might be customary in Python - clearly they are not - and
I am quite aware that Python is not Lisp.

My background is elsewhere, I was not paying particular attention to
the name at all, and I just could not be bothered to look up what
implications any of palindrome, palindromic, ispalindrome,
is_palindrome, isPalindrome, has_palindrome_nature, check_palindrome
and so on might have in Python.

Perhaps I should have used a neutral name like f or test or so, but it
did not occur to me at the time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-28 Thread Jussi Piitulainen
Paul Rubin writes:
 Ian writes:
   On 27/08/2010 21:51, Jussi Piitulainen wrote:
  Meanwhile, I have decided to prefer this:
 
  def palindromep(s):
   def reversed(s):
   return s[::-1]
   return s == reversed(s)
  I like this.
  s[::-1] is obscure and non-obvious, especially to Python noobs.
 
 Overriding the 'reversed' builtin even in an inner scope is a little
 bit ugly.
 
 If you don't mind some overhead, list(s)==list(reversed(s)) (using
 the built-in reversed, not the special obscure one) is pretty clear.

Thanks for that. I'm beginning to like it - not its overhead but
certainly its natural clarity. It wins over ''.join(reversed(s))
easily, in my eyes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GSM to ISO / UCS2 to ISO

2010-08-28 Thread Alexander Gattin
Hello,

On Mon, Aug 16, 2010 at 08:01:36PM +1000, James Mills wrote:
 In an effort to avoid re-inventing the wheel so to speak
 I was wondering if anyone's come across libraries/tools, etc
 that achieve the same kind of functionality as the tools
 library in this java app.

unfortunately, no (except some Perl
implementations). I'd suggest to add GSM0338
module to standard GNU libc's iconv:
/usr/lib/gconv/GSM0338.so
/usr/lib/gconv/gconv-modules

Probably I'm wrong, but this way it'd become
readily available to all programs using iconv(3),
including python.

 http://code.google.com/p/ipddump/source/browse/trunk/src/ipddump/tools/Gsm2Iso.java

I'm Amazed (c) Pixies.
This Java code really sucks.

-- 
With best regards,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Queue cleanup

2010-08-28 Thread Aahz
In article 4c78572c$0$28655$c3e8...@news.astraweb.com,
Steven D'Aprano  st...@remove-this-cybersource.com.au wrote:
On Fri, 27 Aug 2010 09:16:52 -0700, Aahz wrote:
 In article mailman.1967.1281549328.1673.python-l...@python.org, MRAB 
 pyt...@mrabarnett.plus.com wrote:

An object will be available for garbage collection when nothing refers
to it either directly or indirectly. If it's unreferenced then it will
go away.
 
 This isn't actually garbage collection as most people think of it.
 Refcounting semantics mean that objects get reaped as soon as nothing
 points at them.  OTOH, CPython does also have garbage collection to back
 up refcounting so that when you have unreferenced object cycles they
 don't stay around.

I've repeatedly asked, both here and elsewhere, why reference counting 
isn't real garbage collection. Nobody has been able to give me a 
satisfactory answer. As far as I can tell, it's a bit of pretentiousness 
with no basis in objective fact.

You'll notice that I was very careful to qualify my statement with as
most people think of it.  Also, because CPython has two different memory
management mechanisms, refcounting and cycle detection, and the module
that controls cycle detection is called gc, I think it's simpler to
follow along with the Python docs -- and critically important to remind
people that there are in fact two different systems.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

...if I were on life-support, I'd rather have it run by a Gameboy than a
Windows box.  --Cliff Wells
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Walking deeply nested lists

2010-08-28 Thread Peter Otten
donn wrote:

 On 28/08/2010 12:03, Peter Otten wrote:
 But be warned that if you set the limit too high instead of giving you a
 RuntimeError your program will segfault.
 Silly question: is there any way to tell the future in this case? I
 mean, ask for X recursion limit, and catch an error (or something) if
 that won't fly.

I don't think it's a silly question, and I don't think there is an exact 
answer,  even for a given platform. You'll want to stay well below the 
number calculated by the following script:

import os
import subprocess

SCRIPT = tmp_check.py

def ok(n):
return subprocess.call([python, SCRIPT, str(n)]) == 0

if __name__ == __main__:
if not os.path.exists(SCRIPT):
with open(SCRIPT, w) as out:
out.write(\
import sys

def f():
return f()

if __name__ == __main__:
n = int(sys.argv[1])
sys.setrecursionlimit(n)
try: 
f()
except RuntimeError:
pass
)

low = 1000
while True:
new_low = 2*low
if not ok(new_low):
high = new_low
break
low = new_low

while high - low  1:
mid = (low + high) // 2
if ok(mid):
low = mid
else:
high = mid
print max recursion limit, low, high

BTW, I didn't expect it but I get different results on different runs.

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


Re: Queue cleanup

2010-08-28 Thread Aahz
In article 4c78e7a5$0$28655$c3e8...@news.astraweb.com,
Steven D'Aprano  st...@remove-this-cybersource.com.au wrote:

On the other hand, the reason that CPython still has reference counting 
is that the alternatives tried so far are unacceptably for non-threaded 
code.

No, it's *a* reason, the other main reason being that refcounting is much
easier for a random C library.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

...if I were on life-support, I'd rather have it run by a Gameboy than a
Windows box.  --Cliff Wells
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-28 Thread Jon Clements
On Aug 28, 11:55 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote:
  Terry Reedy writes:
  On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:
   Dave Angel writes:

[snip]
 Not everything needs to be a built-in method. There is already a standard
 way to spell reverse a string:

 astring[::-1]

 If you don't like that, you can do this:

 ''.join(reversed(astring))

I've had to debug code that assumed str(reversed('abc')) == 'cba'
 str(reversed('abc'))
'reversed object at 0xa66f78c'

So, a str doesn't construct like tuple/list...it's a call to
__str__().
It's designated as a friendly print out (that's my phrasing).

 list('abc')
['a', 'b', 'c']

I s'pose str is special (2.6) in some way, but it doesn't parallel the
other builtins.

[Not at Terry / Steve intended -- just most relevant post to respond
to]

Jon.







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


Re: Trouble importing cx_Oracle on HPUX

2010-08-28 Thread Alexander Gattin
Hello,

On Thu, Aug 26, 2010 at 08:08:42PM -0700, Cliff
Martin wrote:
 I have just gotten done building Python 3.1.2 on
 HPUX 11.31 Itanium (IA64) using gcc 4.4.3, and
 have tried building cx_Oracle to go with it. The
 build succeeds, but test and importing does not.
 I have tried building Python with threads and
 without. The only exotic thing I do with the
 configure for python is to supply -mlp64, which

BTW, did you build all GNU toolchain in 64 bit
mode? I made some tries to get 64bit python etc
but stubmled over compilation errors and didn't
get enough free time to finish the effort.

 makes it a 64 bit build. Python 3 appears to
 work just fine, and cx_Oracle has worked on this
 same architecture in the past with Python 2.6.5.

did you try to start python -d and
 import cx_Oracle?

It may reveal that some symbols in libnnz10 aren't
resolvable. If this is the case, try linking with
bith libttsh10 and libnnz10:

.../cx_Oracle-x.y.z $ LDFLAGS=-lttsh10 python setup.py install

Alternatively, try linking against static
version of libclntsh10 -- libclntst10.

-- 
With best regards,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trouble importing cx_Oracle on HPUX

2010-08-28 Thread Cliff Martin
Hi, thank you for getting back to me.

Yes, our entire toolchain is 64 bit - a mix of stuff I have downloaded and
built and some packages from HP (in the form of depot files) GCC was
downloaded from HP, for example.

I had to manually add -mlp64 to the CC and CXX lines in the Python Makefile
to get it to build 64 bit Python 3. I also had to define PATH_MAX in about 5
files, because it was not resolving it, and it was just easier to add it
than to spend more time trying to make it work. I hate HP-UX, BTW.

Python -d did not generate any additional information, and so was not
helpful (should this work?). Python -v did, however, and it came up with a
number of unresolved symbols all seeming to be from libnnz11.so. I tried
linking against all of the *.so files in ORACLE_HOME/lib, but I don't
remember trying libttsh11 specifically. I will try it again on Monday.

--

Cliff


On Sat, Aug 28, 2010 at 9:11 AM, Alexander Gattin xr...@yandex.ru wrote:

 Hello,

 On Thu, Aug 26, 2010 at 08:08:42PM -0700, Cliff
 Martin wrote:
  I have just gotten done building Python 3.1.2 on
  HPUX 11.31 Itanium (IA64) using gcc 4.4.3, and
  have tried building cx_Oracle to go with it. The
  build succeeds, but test and importing does not.
  I have tried building Python with threads and
  without. The only exotic thing I do with the
  configure for python is to supply -mlp64, which

 BTW, did you build all GNU toolchain in 64 bit
 mode? I made some tries to get 64bit python etc
 but stubmled over compilation errors and didn't
 get enough free time to finish the effort.

  makes it a 64 bit build. Python 3 appears to
  work just fine, and cx_Oracle has worked on this
  same architecture in the past with Python 2.6.5.

 did you try to start python -d and
  import cx_Oracle?

 It may reveal that some symbols in libnnz10 aren't
 resolvable. If this is the case, try linking with
 bith libttsh10 and libnnz10:

 .../cx_Oracle-x.y.z $ LDFLAGS=-lttsh10 python setup.py install

 Alternatively, try linking against static
 version of libclntsh10 -- libclntst10.

 --
 With best regards,
 xrgtn

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


Re: palindrome iteration

2010-08-28 Thread D'Arcy J.M. Cain
On Sat, 28 Aug 2010 09:48:47 +0100
Ian hobso...@gmaiil.com wrote:
  def palindromep(s):
   def reversed(s):
   return s[::-1]
   return s == reversed(s)
 I like this.
 
 s[::-1] is obscure and non-obvious, especially to Python noobs.
 
 This makes it clear what is going on and why at a cost of very little code.

It seems unnecessary to me.  Even if you can't figure it out through
simple inspection, it takes seconds to fire up Python and type print
'abc'[::-1] into it to see what that does. Then you have another tool
in your toolbox.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Walking deeply nested lists

2010-08-28 Thread donn

On 28/08/2010 14:41, Peter Otten wrote:

BTW, I didn't expect it but I get different results on different
runs.
Clever code. I will give it a go soonest. Elec off for the next 24 hours 
in my neck of the woods. Urgh. Python can't import electricity just yet :)


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


love me

2010-08-28 Thread love me
1st step
add me in like box on my facbook page at this link
http://www.facebook.com/pages/loveme/145529285481739

2nd step
   visit this link http://www.kqzyfj.com/click-3778203-10786395
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rouble importing cx_Oracle on HPUX

2010-08-28 Thread Alexander Gattin
Hello,

On Sat, Aug 28, 2010 at 09:27:05AM -0400, Cliff
Martin wrote:
 Yes, our entire toolchain is 64 bit - a mix of
 stuff I have downloaded and built and some
 packages from HP (in the form of depot files)
 GCC was downloaded from HP, for example.

I see. I bootstrapped from bundled cc, hence all
the problems.

 Python -d did not generate any additional
 information, and so was not helpful (should this
 work?). 

Oops I was wrong about the python -d --
correct option is -v of course...

 Python -v did, however, and it came up with a
 number of unresolved symbols all seeming to be
 from libnnz11.so. I tried linking against all of
 the *.so files in ORACLE_HOME/lib, but I don't
 remember trying libttsh11 specifically. I will
 try it again on Monday.

You're using Oracle 11 vs our v10 (we also have
v8, v9 and v11 in production, but not on this
HP-UX server), but I think the problem with the
libnnz is the same: Oracle doesn't put correct
shared library dependencies into the libnnzXX.so
dynamic section header (it should list
libttshXX.so as NEEDED but apperently doesn't).

Probably their distribution for Solaris is better,
I didn't check (I'll ask our DBAs on Monday).

-- 
With best regards,
xrgtn
-- 
http://mail.python.org/mailman/listinfo/python-list


Tag parsing in python

2010-08-28 Thread agnibhu
Hi all,

I'm a newbie in python. I'm trying to create a library for parsing
certain keywords.
For example say I've key words like abc: bcd: cde: like that... So the
user may use like
abc: How are you bcd: I'm fine cde: ok

So I've to extract the How are you and I'm fine and ok..and
assign them to abc:, bcd: and cde: respectively.. There may be
combination of keyowords introduced in future. like abc: xy: How are
you
So new keywords qualifying the other keywords so on..
So I would like to know the python way of doing this. Is there any
library already existing for making my work easier. ?

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


Functional composition in python

2010-08-28 Thread Dmitry Groshev
Hello all. Some time ago I wrote a little library:
http://github.com/si14/python-functional-composition/ , inspired by
modern functional languages like F#. In my opinion it is quite useful
now, but I would like to discuss it.
An example of usage:

import os
from pyfuncomp import composable, c, _

def comment_cutter(s):
t = s.find(#)
return s if t  0 else s[0:t].strip()

@composable #one can use a decorator to make a composable function
def empty_tester(x):
return len(x)  0 and x[0] != #

path_prefix = test

config_parser = (c(open)   #or use a transformer function
 c(str.strip).map  #map acts like a function modifier
 c(comment_cutter).map 
 empty_tester.filter  #so does filter
 c(os.path.join)[path_prefix, _].map) #f[a, _, b] is
used to make a partial.
#f[a, foo:bar,
baz:_] is also correct

print config_parser(test.txt)
print (c([x ** %s for x in %s])[2, _]  c(lambda x: x * 2).map)([1, 2, 3])

Any suggestions are appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tag parsing in python

2010-08-28 Thread Tim Chase

On 08/28/10 11:14, agnibhu wrote:

For example say I've key words like abc: bcd: cde: like that... So the
user may use like
abc: How are you bcd: I'm fine cde: ok

So I've to extract the How are you and I'm fine and ok..and
assign them to abc:, bcd: and cde: respectively..


For this, you can do something like

 s = abc: how are you bcd: I'm fine cde: ok
 import re
 r = re.compile(r'(\w+):\s*((?:[^:](?!\w+:))*)')
 r.findall(s)
[('abc', 'how are you'), ('bcd', I'm fine), ('cde', 'ok')]

Yes, it's a bit of a gnarled regexp, but it seems to do the job.


There may be combination of keyowords introduced in future.
like abc: xy: How are you So new keywords qualifying the other
keywords so on.


I'm not sure I understand this bit of what you're asking.  If you 
have


  s = abc: xy: How are you

why should that not be parsed as

 r.findall(abc: xy: How are you)
[('abc', ''), ('xy', 'How are you')]

as your initial description prescribes?

-tkc





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


Object containing a list of objects.

2010-08-28 Thread cocolombo
Hello.

I am putting objects (test) into a container object (tests) and the
test object is also a container for a another list of object
(scores).

Problem is that all instances of class tests have the same value.

To illustrate:

class score(object):
val = 0
def __init__(self, val):
self.val = val
def __str__(self):
return str(self.val) + \n

class test(object):
listOfScores = []
def __str__(self):
ret = 
for s in self.listOfScores:
ret += str(s)
return ret

class tests(object):
listOfTest = []
def __str__(self):
ret = 
for t in self.listOfTest:
ret += str(t)
return ret


Now I run the script
:
==
score1 = score(10)
score2 = score(20)
score3 = score(30)
score4 = score(40)

test1 = test()
test2 = test()


test1.listOfScores.append(score1)
test1.listOfScores.append(score2)
test2.listOfScores.append(score3)
test2.listOfScores.append(score4)

theTests = tests()
theTests.listOfTest.append(test1)
theTests.listOfTest.append(test2)

print theTests.listOfTest[0]
print theTests.listOfTest[1]

==

This is the data structure I am EXPECTING:

theTests
 test1
 ---score1=10
 ---score2=20
 test2
 ---score3=30
 ---score4=40


But what I get is this:

theTests
test1
  ---score1=10
  ---score2=20
  ---score3=30
  ---score4=40
 test2
  ---score1=10
  ---score2=20
  ---score3=30
  ---score4=40

What is wrong ?

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


Re: Object containing a list of objects.

2010-08-28 Thread MRAB

On 28/08/2010 18:48, cocolombo wrote:

Hello.

I am putting objects (test) into a container object (tests) and the
test object is also a container for a another list of object
(scores).

Problem is that all instances of class tests have the same value.

To illustrate:

class score(object):
 val = 0
 def __init__(self, val):
 self.val = val
 def __str__(self):
 return str(self.val) + \n

class test(object):
 listOfScores = []
 def __str__(self):
 ret = 
 for s in self.listOfScores:
 ret += str(s)
 return ret

class tests(object):
 listOfTest = []
 def __str__(self):
 ret = 
 for t in self.listOfTest:
 ret += str(t)
 return ret


Now I run the script
:
==
score1 = score(10)
score2 = score(20)
score3 = score(30)
score4 = score(40)

test1 = test()
test2 = test()


test1.listOfScores.append(score1)
test1.listOfScores.append(score2)
test2.listOfScores.append(score3)
test2.listOfScores.append(score4)

theTests = tests()
theTests.listOfTest.append(test1)
theTests.listOfTest.append(test2)

print theTests.listOfTest[0]
print theTests.listOfTest[1]

==

This is the data structure I am EXPECTING:

theTests
  test1
  ---score1=10
  ---score2=20
  test2
  ---score3=30
  ---score4=40


But what I get is this:

theTests
 test1
   ---score1=10
   ---score2=20
   ---score3=30
   ---score4=40
  test2
   ---score1=10
   ---score2=20
   ---score3=30
   ---score4=40

What is wrong ?


When you write:

class test(object):
listOfScores = []

you're making 'listOfScores' an attribute of the class.

If you want it to be an attribute of an instance you should write:

class test(object):
def __init__(self):
self.listOfScores = []
--
http://mail.python.org/mailman/listinfo/python-list


Re: Object containing a list of objects.

2010-08-28 Thread Peter Otten
cocolombo wrote:

 Problem is that all instances of class tests have the same value.
 
 To illustrate:

 class tests(object):
 listOfTest = []

This makes listOfTest a class attribute. To get one list per instance define 
it in the initializer:

class Tests(object):
def __init__(self):
self.tests = []

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


Re: Object containing a list of objects.

2010-08-28 Thread Chris Rebert
On Sat, Aug 28, 2010 at 10:48 AM, cocolombo cocolo...@gmail.com wrote:
 Hello.

 I am putting objects (test) into a container object (tests) and the
 test object is also a container for a another list of object
 (scores).

 Problem is that all instances of class tests have the same value.

 To illustrate:

 class score(object):
    val = 0
The previous line does nothing useful; delete it.

    def __init__(self, val):
        self.val = val
    def __str__(self):
        return str(self.val) + \n

 class test(object):
    listOfScores = []
No! This makes the list a class/static variable *shared between all
instances*. Delete the previous line and define a proper initializer:

def __init__(self):
self.listOfScores = []

    def __str__(self):
        ret = 
        for s in self.listOfScores:
            ret += str(s)
        return ret

 class tests(object):
    listOfTest = []
Again, same problem.

def __init__(self):
self.listOfTest = []

    def __str__(self):
        ret = 
        for t in self.listOfTest:
            ret += str(t)
        return ret

That is more efficiently+concisely written as:
return .join(str(t) for t in self.listOfTest)


 Now I run the script
 :
 ==
 score1 = score(10)
 score2 = score(20)
 score3 = score(30)
 score4 = score(40)

 test1 = test()
 test2 = test()


 test1.listOfScores.append(score1)
 test1.listOfScores.append(score2)
 test2.listOfScores.append(score3)
 test2.listOfScores.append(score4)

 theTests = tests()
 theTests.listOfTest.append(test1)
 theTests.listOfTest.append(test2)

 print theTests.listOfTest[0]
 print theTests.listOfTest[1]

 ==

 This is the data structure I am EXPECTING:
snip
 But what I get is this:
snip
 What is wrong ?

Python is not Java/C# and has no instance variable declarations. You
just assign to an attribute of self in __init__ and *that* is what
creates instance variables.

Any variables you assign to directly in the class body (as you were
doing with listOfScores and listOfTest) are made class variables (Java
lingo: static variables), and are /shared between all instances/,
which is rarely what one actually wants.
To get regular instance variables, define a proper __init__() and
assign the variables to self therein.

Also, per PEP 8 (http://www.python.org/dev/peps/pep-0008/ ):
- Classes are conventionally CapitalizedWords, so name your classes
Score, Test, and Tests rather than score, test, and tests.
- Variables/methods are conventionally underscored_between_words, so
list_of_test rather than listOfTest.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Object containing a list of objects.

2010-08-28 Thread cocolombo
Thanks MRAB and Peter Otten that solved the problem.

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


Re: Object containing a list of objects.

2010-08-28 Thread cocolombo
Chris I take good notice of your comments and suggestions. Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String substitution VS proper mysql escaping

2010-08-28 Thread Νίκος
On 20 Αύγ, 09:04, Nik Gr nikos.the.gr...@gmail.com wrote:
 With regard to the % operator, it considers the string on the left to
 be a format string with multiple %blah things in it to replace. The
 thing on the right is a sequence of items to place into the format
 string.

Can you please clarify what you mean by that?

 In you usage above you're supplying page instead of (page,).
 The latter matches the .execute() method's requirements.

I tried it and page as a string and not a as a single element tuple
works ok.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional composition in python

2010-08-28 Thread Peter Otten
Dmitry Groshev wrote:

 Hello all. Some time ago I wrote a little library:
 http://github.com/si14/python-functional-composition/ , inspired by
 modern functional languages like F#. In my opinion it is quite useful
 now, but I would like to discuss it.
 An example of usage:
 
 import os
 from pyfuncomp import composable, c, _
 
 def comment_cutter(s):
 t = s.find(#)
 return s if t  0 else s[0:t].strip()
 
 @composable #one can use a decorator to make a composable function
 def empty_tester(x):
 return len(x)  0 and x[0] != #
 
 path_prefix = test
 
 config_parser = (c(open)   #or use a transformer function
  c(str.strip).map  #map acts like a function modifier
  c(comment_cutter).map 
  empty_tester.filter  #so does filter
  c(os.path.join)[path_prefix, _].map) #f[a, _, b] is
 used to make a partial.
 #f[a, foo:bar,
 baz:_] is also correct
 
 print config_parser(test.txt)
 
 Any suggestions are appreciated.

With some effort you could perhaps tweak your library to accept something 
like

config_parser = c(open) | str.strip | comment_cutter | empty_tester | 
c(os.path.join)(path_prefix, _)

This looks more like a shell pipe than a C++ print statement -- which I 
think is a good thing.

More general: Yes, I know that the functional style is contagious. However, 
I find that more traditional Python code is easier to understand. Compare:

import os

def config_parser(configfile, folder):
with open(configfile) as lines:
for line in lines:
name = line.partition(#)[0].strip()
if name:
yield os.path.join(folder, name)

for path in config_parser(test.txt, test):
print path

(at least that's what I'm guessing your code is trying to achieve)

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


Re: String substitution VS proper mysql escaping

2010-08-28 Thread MRAB

On 28/08/2010 20:10, Νίκος wrote:

On 20 Αύγ, 09:04, Nik Grnikos.the.gr...@gmail.com  wrote:

With regard to the % operator, it considers the string on the left to
be a format string with multiple %blah things in it to replace. The
thing on the right is a sequence of items to place into the format
string.


Can you please clarify what you mean by that?


Basically:

format_string % (item_1, item_2, item_3)


In you usage above you're supplying page instead of (page,).
The latter matches the .execute() method's requirements.


I tried it and page as a string and not a as a single element tuple
works ok.


Although the .execute() method might accept a single string:

cursor.execute(sql_query, page)

as well as a tuple containing the string:

cursor.execute(sql_query, (page, ))

try to be consistent. As I said before:

When there's more than one value you provide a tuple. It's makes sense
from the point of view of consistency that you also provide a tuple when
there's only one value.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem checking an existing browser cookie

2010-08-28 Thread Νίκος
On 22 Αύγ, 10:27, Νίκος nikos.the.gr...@gmail.com wrote:
 On 16 Αύγ, 14:31, Peter Otten __pete...@web.de wrote:









  Νίκος wrote:
   # initializecookie
  cookie=Cookie.SimpleCookie()
  cookie.load( os.environ.get('HTTP_COOKIE', '') )
   mycookie =cookie.get('visitor')

   if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta|
   yandex|13448|spider|crawl)', host ) is None:
       blabla...
   

   I checked and Chrome has acookienames visitor with a value ofnikos
   within.
   So, i have to ask why the if fails?

  Maybe it's because != != ==

 Iwant ti if code block to be executed only if the browsercookienames
 visitor fetched doesnt cotnain the vbalue of 'nikos'

 Is there somethign wrong with the way i wrote it?

Please do help me with this too becaus eif i dont solve this my
website keeps count my each visit like iam a guest visitor!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String substitution VS proper mysql escaping

2010-08-28 Thread Νίκος
On 28 Αύγ, 22:35, MRAB pyt...@mrabarnett.plus.com wrote:
 On 28/08/2010 20:10, Νίκος wrote: On 20 Αύγ, 09:04, Nik 
 Grnikos.the.gr...@gmail.com  wrote:
  With regard to the % operator, it considers the string on the left to
  be a format string with multiple %blah things in it to replace. The
  thing on the right is a sequence of items to place into the format
  string.

  Can you please clarify what you mean by that?

 Basically:

      format_string % (item_1, item_2, item_3)

I still don't follow by means that i dotn see the point here...


  In you usage above you're supplying page instead of (page,).
  The latter matches the .execute() method's requirements.

  I tried it and page as a string and not a as a single element tuple
  works ok.

 Although the .execute() method might accept a single string:

      cursor.execute(sql_query, page)

 as well as a tuple containing the string:

      cursor.execute(sql_query, (page, ))

 try to be consistent. As I said before:

 When there's more than one value you provide a tuple. It's makes sense
 from the point of view of consistency that you also provide a tuple when
 there's only one value.

cursor.execute(sql_query, (page, ))

is different than?

cursor.execute(sql_query, page, )

?

===
Why in mysql string substitution example i have to use page='%s' and
in the comma way(automatic mysql convertion i dont need the single
quotes and use it as page=%s ?
What is the diff?
===
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String substitution VS proper mysql escaping

2010-08-28 Thread Νίκος
On 28 Αύγ, 22:35, MRAB pyt...@mrabarnett.plus.com wrote:

 When there's more than one value you provide a tuple. It's makes sense
 from the point of view of consistency that you also provide a tuple when
 there's only one value.

Can you write something that make use of more than one value?


Perhaps you mena somethign like?

cursor.execute( '''SELECT hits FROM counters WHERE page = %s and date
= %s and host = %s''' , (page,) )

Is this what you mean?

All those special format strign identifiers will grab their values out
of the tuple?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String substitution VS proper mysql escaping

2010-08-28 Thread Rami Chowdhury
2010/8/29 Νίκος nikos.the.gr...@gmail.com:
 On 28 Αύγ, 22:35, MRAB pyt...@mrabarnett.plus.com wrote:

 When there's more than one value you provide a tuple. It's makes sense
 from the point of view of consistency that you also provide a tuple when
 there's only one value.

 Can you write something that make use of more than one value?


 Perhaps you mena somethign like?

 cursor.execute( '''SELECT hits FROM counters WHERE page = %s and date
 = %s and host = %s''' , (page,) )

 Is this what you mean?

 All those special format strign identifiers will grab their values out
 of the tuple?

Yes, that's exactly right -- they'll try to grab values out of the
tuple, and since in that particular code snippet the tuple doesn't
contain enough items, you'll get an error :-)

HTH,
Rami

-- 
Rami Chowdhury
Never assume malice when stupidity will suffice. -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String substitution VS proper mysql escaping

2010-08-28 Thread MRAB

On 28/08/2010 20:48, Νίκος wrote:

On 28 Αύγ, 22:35, MRABpyt...@mrabarnett.plus.com  wrote:

On 28/08/2010 20:10, Νίκος wrote:  On 20 Αύγ, 09:04, Nik 
Grnikos.the.gr...@gmail.comwrote:

With regard to the % operator, it considers the string on the left to
be a format string with multiple %blah things in it to replace. The
thing on the right is a sequence of items to place into the format
string.



Can you please clarify what you mean by that?


Basically:

  format_string % (item_1, item_2, item_3)


I still don't follow by means that i dotn see the point here...




In you usage above you're supplying page instead of (page,).
The latter matches the .execute() method's requirements.



I tried it and page as a string and not a as a single element tuple
works ok.


Although the .execute() method might accept a single string:

  cursor.execute(sql_query, page)

as well as a tuple containing the string:

  cursor.execute(sql_query, (page, ))

try to be consistent. As I said before:

When there's more than one value you provide a tuple. It's makes sense
from the point of view of consistency that you also provide a tuple when
there's only one value.


cursor.execute(sql_query, (page, ))

is different than?

cursor.execute(sql_query, page, )

?


Yes.

The first has 2 arguments: a string and a tuple containing the value of
'page'.

The second has 2 arguments: a string and the value of 'page'.


===
Why in mysql string substitution example i have to use page='%s' and
in the comma way(automatic mysql convertion i dont need the single
quotes and use it as page=%s ?
What is the diff?
===


In the first case you're doing the substitution yourself, but you might
not get it right, leaving your website open an SQL injection attacks.

In the second case you're letting the .execute method do the
substitution. It will have been written to do it correctly and safely.
--
http://mail.python.org/mailman/listinfo/python-list


Re: String substitution VS proper mysql escaping

2010-08-28 Thread MRAB

On 28/08/2010 20:51, Νίκος wrote:

On 28 Αύγ, 22:35, MRABpyt...@mrabarnett.plus.com  wrote:


When there's more than one value you provide a tuple. It's makes sense
from the point of view of consistency that you also provide a tuple when
there's only one value.


Can you write something that make use of more than one value?


Perhaps you mena somethign like?

cursor.execute( '''SELECT hits FROM counters WHERE page = %s and date
= %s and host = %s''' , (page,) )

Is this what you mean?

All those special format strign identifiers will grab their values out
of the tuple?


Your example contains 3 placeholders, so it needs 3 values:

cursor.execute('''SELECT hits FROM counters WHERE page = %s and 
date = %s and host = %s''', (page, date, host))


This will be safe. Any quoting that's needed will be done by .execute().
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem checking an existing browser cookie

2010-08-28 Thread Peter Otten
Νίκος wrote:

 On 22 Αύγ, 10:27, Νίκος nikos.the.gr...@gmail.com wrote:
 On 16 Αύγ, 14:31, Peter Otten __pete...@web.de wrote:

  Νίκος wrote:
   # initializecookie
  cookie=Cookie.SimpleCookie()
  cookie.load( os.environ.get('HTTP_COOKIE', '') )
   mycookie =cookie.get('visitor')

   if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta|
   yandex|13448|spider|crawl)', host ) is None:
   blabla...
   

   I checked and Chrome has acookienames visitor with a value ofnikos
   within.
   So, i have to ask why the if fails?

  Maybe it's because != != ==

 Iwant ti if code block to be executed only if the browsercookienames
 visitor fetched doesnt cotnain the vbalue of 'nikos'

 Is there somethign wrong with the way i wrote it?
 
 Please do help me with this too becaus eif i dont solve this my
 website keeps count my each visit like iam a guest visitor!

In your initial post it sounded like you wanted the if-branch to execute for 
a user named nikos, but now it seems that I misunderstood you and swapping 
'mycookie.value != nikos' for 'mycookie.value == nikos' won't help. 
Maybe you could add a print statement like

print mycookie.value

to start with your debugging efforts.

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


Re: Problem checking an existing browser cookie

2010-08-28 Thread MRAB

On 28/08/2010 20:37, Νίκος wrote:

On 22 Αύγ, 10:27, Νίκοςnikos.the.gr...@gmail.com  wrote:

On 16 Αύγ, 14:31, Peter Otten__pete...@web.de  wrote:










Νίκος wrote:

# initializecookie
cookie=Cookie.SimpleCookie()
cookie.load( os.environ.get('HTTP_COOKIE', '') )
mycookie =cookie.get('visitor')



if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta|
yandex|13448|spider|crawl)', host ) is None:
 blabla...




I checked and Chrome has acookienames visitor with a value ofnikos
within.
So, i have to ask why the if fails?



Maybe it's because != != ==


Iwant ti if code block to be executed only if the browsercookienames
visitor fetched doesnt cotnain the vbalue of 'nikos'

Is there somethign wrong with the way i wrote it?


Please do help me with this too becaus eif i dont solve this my
website keeps count my each visit like iam a guest visitor!


Print out mycookie, repr(mycookie.value) (unless mycookie is None) and
repr(host). Then follow the code yourself to see whether the condition
is True.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading the access attributes of directories in Windows

2010-08-28 Thread Rami Chowdhury
On Fri, Aug 27, 2010 at 14:16, Nobody nob...@nowhere.com wrote:
 On Fri, 27 Aug 2010 13:28:46 +0600, Rami Chowdhury wrote:

 Having this as a separate permission allows normal users to add entries
 to log files but not to erase existing entries.

 Unix/Linux systems can do this already.

 Ooh, I didn't know that -- what combination of permissions would I have to
 use to get such an effect?

 You can't do it with permissions, you need to use ext2 attributes.
 Specifically, chattr +a filename will set the append attribute,
 which prevents the file being opened for write except in append mode.
 Changing this attribute requires root privilege or the CAP_LINUX_IMMUTABLE
 capability.

Fascinating, thank you!

-- 
Rami Chowdhury
Never assume malice when stupidity will suffice. -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading the access attributes of directories in Windows

2010-08-28 Thread Rami Chowdhury
On Sat, Aug 28, 2010 at 05:07, Lawrence D'Oliveiro
l...@geek-central.gen.new_zealand wrote:
 In message mailman.99.1282894128.29448.python-l...@python.org, Rami
 Chowdhury wrote:

 On Wed, Aug 25, 2010 at 05:04, Lawrence D'Oliveiro
 l...@geek-central.gen.new_zealand wrote:

 In message pan.2010.08.22.04.26.33.547...@nowhere.com, Nobody wrote:

 Having this as a separate permission allows normal users to add entries
 to log files but not to erase existing entries.

 Unix/Linux systems can do this already.

 Ooh, I didn't know that -- what combination of permissions would I
 have to use to get such an effect?

 No special permissions needed at all—just use the syslog(3) functions.

 And the nice thing is, you don’t have to know whether the system logs are
 kept on the local machine or on a remote machine, or how different
 categories of messages are divided up into different files, how log rotation
 is done, or anything like that—it’s all transparent.

Ah, thanks -- I think I'd misread your first post as indicating that
that kind of append effect would work for any file. Apologies for
the misunderstanding!

-- 
Rami Chowdhury
Never assume malice when stupidity will suffice. -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-28 Thread Dave Angel

Jussi Piitulainen wrote:

Steven D'Aprano writes:
  

On Sat, 28 Aug 2010 09:22:13 +0300, Jussi Piitulainen wrote:


Terry Reedy writes:
  

On 8/27/2010 3:43 PM, Jussi Piitulainen wrote:


Dave Angel writes:
  
There could easily be a .reverse() method on strings. It would return

the reversed string, like .swapcase() returns the swapcased string.
  

Could be, but the main use case seems to be for palindrome testing ;-)
Given that slicing and reversed() can do the same thing, the need is
thin.


The need is quite thin, but immutability of strings is not an issue,
just like there can be .swapcase() though strings are immutable. That is
all I am saying above.
  

You're right, there could be a reversed() method for strings. There
could also be a disemvowel method that removes vowels, a randomise
method that shuffles the letters around, a studlycaps method that
changes the case of each letter randomly, and a method to check that
brackets () are well- formed. They would all be useful to
somebody. There are lots of different methods that strings could
have. Where do you draw the line?



When I said that there could be such a method, I was merely objecting
to a statement, made in response to me, that there could not be such a
method because strings are immutable. You clearly agree with me that
that statement was not correct. Would you have let it stand if it was
made to you?

  
Since you repeat that assertion three times, I figure you must think 
it's important.  And it was I who asserted that a reverse() method 
wouldn't be possible on an immutable object.  reverse() would reverse 
the characters in place, and return None.  At least it would if it tried 
to be at all consistent with the list, array, and audioop methods of the 
same name.


reversed() is certainly possible, and it'd make a new string with the 
reverse order of the original.



DaveA

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


InteractiveConsole and namespace

2010-08-28 Thread David ROBERT
Hi all,

I want to use an InteractiveConsole at some stage in a program to
interact with the local namespace: access, but also modify objects.
When the interactive console ends (ctrl-d) I want the program to
continue processing with the variables that may have been modified
interactively.

The code below works (block invoking the console is not in a
function). During the interactive session, I can read value of a, I
can change value of a and the new value is updated in the block
namespace.

import code
if __name__ == '__main__':
a=1
c = code.InteractiveConsole(locals())
c.interact()  # Here I interactively change the value of a (a=2)
print Value of a: , a

print returns -- Value of a: 2

However, on the other code below (the console is invoked from within a
function block), during the interactive session, I can read value of
a, I can change value of a. But the local namespace of the function is
not updated:

import code
def test():
a=1
c = code.InteractiveConsole(locals())
c.interact() # Here I interactively change the value of a (a=2)
print Value of a: , a

if __name__ == '__main__':
test()

print returns -- Value of a: 1

I need to run the InteractiveConsole from a function block. I tried
different things with the local and parent frames (sys._getframe())
but nothing successful. If I put a in the global namespace it works,
but I would like to find a nicer solution and also understand what the
problem is.

Thanks for any help

-- 
David ROBERT
http://blog.ombrepixel.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyxser-1.5r --- Python Object to XML serializer/deserializer

2010-08-28 Thread Josh English
On Aug 26, 10:02 pm, Stefan Behnel stefan...@behnel.de wrote:
 Josh English, 27.08.2010 01:30:

  solve a lot of the problems I'm running into in my own attempt to
  build a python Class implementation of an XML Validation object.

 How would object serialisation help here?



I'm running into the same problem in a lot of projects I'm working on.
I can't decide
one the best way to serialize instances of classes. I want to be able
to store these
instances in a human-readable and editable format that I can reload
back into their
objects.

The XML validation tool requires the rules of the XML to be written
out in Python
as classes and instances. I would love to have a consistent way to
write the definition
file and load it. For example, Relax NG can be written out, the rules
loaded into a parser,
and actual XML data can be validated against it.

Since I started in Python, I want that bridge back to serialization.

In this project, and others, subclassing plays a big role in using the
code, so I need
a reliable way of writing a definition, loading it into the proper
subclass, and running
with those subclasses.

I am familiar with XML, but I've played with YAML and may try that.
I'm hesitant to use YAML
because it requires that I add the YAML.object.

I even wrote a program to store data similar to GEDCOM format, which
seemed the simplest from
a user's perspective.

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


Re: Tag parsing in python

2010-08-28 Thread Josh English
On Aug 28, 9:14 am, agnibhu dee...@gmail.com wrote:
 Hi all,

 I'm a newbie in python. I'm trying to create a library for parsing
 certain keywords.
 For example say I've key words like abc: bcd: cde: like that... So the
 user may use like
 abc: How are you bcd: I'm fine cde: ok

 So I've to extract the How are you and I'm fine and ok..and
 assign them to abc:, bcd: and cde: respectively.. There may be
 combination of keyowords introduced in future. like abc: xy: How are
 you
 So new keywords qualifying the other keywords so on..
 So I would like to know the python way of doing this. Is there any
 library already existing for making my work easier. ?

 ~
 Agnibhu

Have you looked at pyparsing? (http://pyparsing.wikispaces.com/) It
may
be possible to use that library to do this.

Josh



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


Helper app for intranet site

2010-08-28 Thread kevinlcarlson
I'm exploring the possibility of developing a helper app for an
existing internal company website.  Basically, it would automatically
scan the current page contents, including prepopulated forms, and
provide context-related business rule comments to the user, via a stay-
on-top wxPython panel.  Any suggestions on how to do this? (The web
developers are unavailable to do this within the site.)

Thanks,
Kevin


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


Fibonacci: How to think recursively

2010-08-28 Thread Baba
Level: beginner

I would like to know how to approach the following Fibonacci problem:
How may rabbits do i have after n months?

I'm not looking for the code as i could Google that very easily. I'm
looking for a hint to put me on the right track to solve this myself
without looking it up.

my brainstorming so far brought me to a stand still as i can't seem to
imagine a recursive way to code this:

my attempted rough code:

def fibonacci(n):
# base case:
result = fibonacci (n-1) + fibonacci (n-2)
 this will end up in a mess as it will create overlapping recursions

OR

def fibonacci(n):
# base case:
fibonacci(n+2) - fibonacci(n+1) - n = 0
 this too would create overlapping recursions


How to go about this?

Thanks
Baba

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


Re: Fibonacci: How to think recursively

2010-08-28 Thread Mel
Baba wrote:

 Level: beginner
 
 I would like to know how to approach the following Fibonacci problem:
 How may rabbits do i have after n months?
 
 I'm not looking for the code as i could Google that very easily. I'm
 looking for a hint to put me on the right track to solve this myself
 without looking it up.
 
 my brainstorming so far brought me to a stand still as i can't seem to
 imagine a recursive way to code this:
 
 my attempted rough code:
 
 def fibonacci(n):
 # base case:
 result = fibonacci (n-1) + fibonacci (n-2)
 this will end up in a mess as it will create overlapping recursions

I don't think this is the base case.  The base case would be one or more 
values of `n` that you already know the fibonacci number for.  Your 
recursive function can just test for those and return the right answer right 
away.  The the expression you've coded contains a good way to handle the 
non-base cases.  There's no such problem as overlapping recursions.

Mel.

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


Re: InteractiveConsole and namespace

2010-08-28 Thread Chris Rebert
On Sat, Aug 28, 2010 at 2:37 PM, David ROBERT da...@ombrepixel.com wrote:
 Hi all,

 I want to use an InteractiveConsole at some stage in a program to
 interact with the local namespace: access, but also modify objects.
 When the interactive console ends (ctrl-d) I want the program to
 continue processing with the variables that may have been modified
 interactively.

 The code below works (block invoking the console is not in a
 function). During the interactive session, I can read value of a, I
 can change value of a and the new value is updated in the block
 namespace.

 import code
 if __name__ == '__main__':
    a=1
    c = code.InteractiveConsole(locals())
    c.interact()  # Here I interactively change the value of a (a=2)
    print Value of a: , a

 print returns -- Value of a: 2

 However, on the other code below (the console is invoked from within a
 function block), during the interactive session, I can read value of
 a, I can change value of a. But the local namespace of the function is
 not updated:

 import code
 def test():
    a=1
    c = code.InteractiveConsole(locals())
    c.interact() # Here I interactively change the value of a (a=2)
    print Value of a: , a

 if __name__ == '__main__':
    test()

 print returns -- Value of a: 1

 I need to run the InteractiveConsole from a function block. I tried
 different things with the local and parent frames (sys._getframe())
 but nothing successful. If I put a in the global namespace it works,
 but I would like to
[...]
 understand what the
 problem is.

Read http://docs.python.org/library/functions.html#locals (emphasis added):

locals()
[...]
Note: The contents of this dictionary should not be modified;
***changes may not affect the values of local and free variables used
by the interpreter***.

At the top-level / module scope, it just so happens that `locals() is
globals() == True`; and unlike locals(), globals() does not have the
aforequoted limitation.

I don't know if there's a non-ugly workaround.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fibonacci: How to think recursively

2010-08-28 Thread Ben Finney
Baba raoul...@gmail.com writes:

 my brainstorming so far brought me to a stand still as i can't seem to
 imagine a recursive way to code this:

 my attempted rough code:

 def fibonacci(n):
 # base case:
 result = fibonacci (n-1) + fibonacci (n-2)
  this will end up in a mess as it will create overlapping recursions

It also never returns anything (which, in Python, means it returns the
None object).

Worse, it will endlessly recurse; every time it's called it will call
itself (twice).

Perhaps a way to approach the problem is: How will your function know
when *not* to call itself? What will it do instead? Try writing that
case first, and then write the rest of it on that basis.

-- 
 \ “Science is a way of trying not to fool yourself. The first |
  `\ principle is that you must not fool yourself, and you are the |
_o__)   easiest person to fool.” —Richard P. Feynman, 1964 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fibonacci: How to think recursively

2010-08-28 Thread Matteo Landi
I suggest you to memoize results in order to prevent overlapping recursion.

Regards,
Matteo

On Sun, Aug 29, 2010 at 2:23 AM, Ben Finney ben+pyt...@benfinney.id.au wrote:
 Baba raoul...@gmail.com writes:

 my brainstorming so far brought me to a stand still as i can't seem to
 imagine a recursive way to code this:

 my attempted rough code:

 def fibonacci(n):
     # base case:
         result = fibonacci (n-1) + fibonacci (n-2)
  this will end up in a mess as it will create overlapping recursions

 It also never returns anything (which, in Python, means it returns the
 None object).

 Worse, it will endlessly recurse; every time it's called it will call
 itself (twice).

 Perhaps a way to approach the problem is: How will your function know
 when *not* to call itself? What will it do instead? Try writing that
 case first, and then write the rest of it on that basis.

 --
  \         “Science is a way of trying not to fool yourself. The first |
  `\     principle is that you must not fool yourself, and you are the |
 _o__)               easiest person to fool.” —Richard P. Feynman, 1964 |
 Ben Finney
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
Matteo Landi
http://www.matteolandi.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


looking for open source python project

2010-08-28 Thread mo reina
looking for a python project (preferably something a bit small) that
is looking for contributors. the small bit is because i've never
worked in a team before and haven't really read source code that's
1000s of lines long, so i'm not too sure i can keep up.

my python fu is decent (i think), i recently wrote a small archive/
grimoire program (command line only) that can store multiline text
with title, tags, and basic search functionality (not using curses so
the entry, once entered, can't be modified), entries are stored in a
pickle file.

anybody have any suggestions? i'm keen to work on something with
others, both for learning and i'd like to do something a bit
meaningful, plus i'm sure it's fun.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: palindrome iteration

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 15:11:03 +0300, Jussi Piitulainen wrote:

[...]
 When I said that there could be such a method, I was merely objecting to
 a statement, made in response to me, that there could not be such a
 method because strings are immutable. You clearly agree with me that
 that statement was not correct. Would you have let it stand if it was
 made to you?

Ha ha, you're new here aren't you?

 To answer your question, I don't see a real need for .reversed() in
 strings, but I do think .reversed() would be much more useful than
 .swapcase() which is in Python now and for which I see no use at all.

It's hard to disagree with that. I'm not entirely sure what the use-case 
for swapcase is. It's not quite as specialised as sTUdlEycApS but not far 
off.


[...]
 I agree that the gain would be minimal. There is no harm in the method
 either, so I would not object to it if somebody were to propose its
 addition, but just to clarify my position: I have not proposed it.

Then we are in agreement :)

I think the only thing we disagree on is that I think [::-1] is a 
perfectly nice expression for reversal, while you don't. True, it's not 
entirely intuitive to newbies, or self-documenting, you need to learn 
slicing to understand it. But then, if you were Dutch and had not learned 
English, you would probably be looking for a method called omgekeerde and 
would find reverse equally unintuitive.



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


Re: Functional composition in python

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 21:30:39 +0400, Dmitry Groshev wrote:

 Hello all. Some time ago I wrote a little library:
 http://github.com/si14/python-functional-composition/ , inspired by
 modern functional languages like F#. In my opinion it is quite useful
 now, but I would like to discuss it.
 An example of usage:
 
 import os
 from pyfuncomp import composable, c, _
 
 def comment_cutter(s):
 t = s.find(#)
 return s if t  0 else s[0:t].strip()
 
 @composable #one can use a decorator to make a composable function 
 def empty_tester(x):
 return len(x)  0 and x[0] != #


Why do you need a decorator to make a composable function? Surely all 
functions are composable -- it is the nature of functions that you can 
call one function with the output of another function.


 path_prefix = test
 
 config_parser = (c(open)   #or use a transformer function
  c(str.strip).map  #map acts like a function modifier
  c(comment_cutter).map 
  empty_tester.filter  #so does filter
  c(os.path.join)[path_prefix, _].map) #f[a, _, b] is
 used to make a partial.
 #f[a, foo:bar,
 baz:_] is also correct
 
 print config_parser(test.txt)
 print (c([x ** %s for x in %s])[2, _]  c(lambda x: x * 2).map)([1,
 2, 3])
 
 Any suggestions are appreciated.


Did you expect us to guess what the above code would do? Without showing 
the output, the above is just line noise.

What does c() do? What does compose() do that ordinary function 
composition doesn't do? You say that map acts as a function modifier, 
but don't tell us *what* it modifies or in what way. Same for filter.

So anyone not familiar with C syntax, the use of  is just line noise. 
You need to at say what you're using it for.


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


Re: Fibonacci: How to think recursively

2010-08-28 Thread Steven D'Aprano
On Sat, 28 Aug 2010 16:12:36 -0700, Baba wrote:

 Level: beginner
 
 I would like to know how to approach the following Fibonacci problem:
 How may rabbits do i have after n months?
 
 I'm not looking for the code as i could Google that very easily. I'm
 looking for a hint to put me on the right track to solve this myself
 without looking it up.
 
 my brainstorming so far brought me to a stand still as i can't seem to
 imagine a recursive way to code this:


Perhaps you need to start with a simpler case to get your head around the 
ideas. Let's look at factorial first.

fact(n) = n*(n-1)*(n-2)*(n-3)*...*2*1

Working upwards:

fact(0) = 1  # this is by definition.
fact(1) = 1  # also by definition.
fact(2) = 2*1 = 2*fact(1)
fact(3) = 3*2*1 = 3*fact(2)
...
fact(n) = n*fact(n-1)

So now you have the base case:
if n is 1 or smaller, fact(n) returns 1

and the recursive case:
otherwise fact(n) returns n*fact(n-1)

Now write that as a Python function, and test it to see that it is 
correct. Come back once you've got it working satisfactorily.



Now, can you apply the same reasoning to the Fibonacci problem?

What is your base case? You need some way to halt the recursion, 
something that *doesn't* make a recursive call.

def fib(n):
if SOME_CONDITION:
return SOME_VALUE
else:
return SOME_RECURSION

is the most basic form you can have. Notice that you must *return* 
something. That's basic Python syntax -- if you don't call return, the 
function will return the special None object, which is not what you want.

More generally, you could have multiple base cases and multiple recursive 
cases, not necessarily one of each. But there must be at least one non-
recursive expression that gets returned, or the recursion can never 
terminate.


 my attempted rough code:
 
 def fibonacci(n):
 # base case:
 result = fibonacci (n-1) + fibonacci (n-2)
 this will end up in a mess as it will create overlapping recursions

Mathematically, there is nothing wrong with overlapping recursion. It 
will work, and Python can handle it easily.

But in practical terms, it can lead to great inefficiency. In this 
example, it should be avoided because it is slow. Very slow. To calculate 
the nth Fibonacci number using naive recursion requires *many* calls:

fib(4)# first call
= fib(3) + fib(2)# three calls
= fib(2) + fib(1) + fib(1) + fib(0)  # seven calls
= fib(1) + fib(0) + 1 + 1 + 0# nine calls
= 1 + 0 + 1 + 1 + 0 = 3

So to get fib(4) = 3 requires nine calls to fib().

This growth function doesn't have a name (as far as I know), but it grows 
much faster than fib() itself:

n  = 0   1   2   3   4   5   6   ... 35   ...
fib(n) = 0   1   1   2   3   5   8   ... 9227465  ...
calls  = 1   1   3   5   9   15  25  ... 29860703 ...

As you can see, the number of calls is also calculable by a recursive 
expression R:

R(0) = R(1) = 1
R(n) = R(n-1) + R(n-2) + 1

This is very similar to the Fibonacci recursion, only it grows more 
quickly. But I digress...


You can make the recursive version more efficient if you give it a 
memory. In the call to fib(5), for example, it ends up calling fib(4) 
once, fib(3) twice, fib(2) three times, fib(1) four times, and fib(0) 
twice. If it could remember each value once it saw it, it could 
potentially save nine calls (out of fifteen). That's a less inefficient 
use of recursion. Think about ways to give it a short-term memory.

You can make it even more efficient by giving fib() a long-term cache, so 
that each call to fib(5) requires one cache lookup rather than six (or 
fifteen) recursive calls. Other than the first time, obviously. This is 
called memoisation, but again I digress.


There are other techniques, but this will do to get started.




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


Re: Functional composition in python

2010-08-28 Thread Dmitry Groshev
On Aug 29, 5:14 am, Steven D'Aprano st...@remove-this-
cybersource.com.au wrote:
 On Sat, 28 Aug 2010 21:30:39 +0400, Dmitry Groshev wrote:
  Hello all. Some time ago I wrote a little library:
 http://github.com/si14/python-functional-composition/, inspired by
  modern functional languages like F#. In my opinion it is quite useful
  now, but I would like to discuss it.
  An example of usage:

  import os
  from pyfuncomp import composable, c, _

  def comment_cutter(s):
      t = s.find(#)
      return s if t  0 else s[0:t].strip()

  @composable #one can use a decorator to make a composable function
  def empty_tester(x):
      return len(x)  0 and x[0] != #

 Why do you need a decorator to make a composable function? Surely all
 functions are composable -- it is the nature of functions that you can
 call one function with the output of another function.



  path_prefix = test

  config_parser = (c(open)   #or use a transformer function
               c(str.strip).map  #map acts like a function modifier
               c(comment_cutter).map 
               empty_tester.filter  #so does filter
               c(os.path.join)[path_prefix, _].map) #f[a, _, b] is
  used to make a partial.
                                                      #f[a, foo:bar,
  baz:_] is also correct

  print config_parser(test.txt)
  print (c([x ** %s for x in %s])[2, _]  c(lambda x: x * 2).map)([1,
  2, 3])

  Any suggestions are appreciated.

 Did you expect us to guess what the above code would do? Without showing
 the output, the above is just line noise.

 What does c() do? What does compose() do that ordinary function
 composition doesn't do? You say that map acts as a function modifier,
 but don't tell us *what* it modifies or in what way. Same for filter.

 So anyone not familiar with C syntax, the use of  is just line noise.
 You need to at say what you're using it for.

 --
 Steven

Yep, it's my mistake. I thought this syntax is quite intuitive. Here
is some explanations in code:

@composable
def f1(x):
return x * 2

@composable
def f2(x):
return x + 3

@composable
def f3(x):
return (-1) * x

@composable
def f4(a):
  return a + [0]

@composable
def sqrsum(x, y):
return x ** 2 + y ** 2

print f1(2) #4
print f2(2) #5
print (f1  f2  f1)(2) #14
print (f3  f2)(2) #1
print (f2  f3)(2) #-5
print (c(float)  f1  f2)(4) #14.0
print (sqrsum[_, 1]  f1)(2) #17
print (sqrsum[_, _].map)([1, 2, 3, 4, 5]) #[2, 8, 18, 32, 50]
print (c(lambda x: x * 2).map  c([x * %s for x in %s])[3, _])([1,
2, 3]) #[6, 12, 18]

Generally, f1  f2 means lambda x: f2(f1(x)) or pass the result of
f1 to f2. But in python function can return only one value, so a
composable function should be a function of one argument. So some form
of making partial is needed, and here comes a
f[a, b, _] notation, which means substitute 3rd argument of f, first
twos are a and b. Finally, we need some form of syntactic sugar for
this: c(map)[c(f),_], so we have a map modifier, which transforms
function F to an isomorphism or mapping between lists. For example,
c(lambda x: x * 2).map is equal to lambda x: map(lambda y: y * 2, x).
Filter modifier is the same thing for boolean functions.

What does c() do? What does compose() do that ordinary function
composition doesn't do?
I need c() or composable() to make an objects with overloaded
operators.

All in all, all this stuff is just a syntactic sugar for nested
functions, maps and filters, which brings a new semantics for old
operators (so one can call it edsl).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fibonacci: How to think recursively

2010-08-28 Thread Mark Tolonen


Steven D'Aprano st...@remove-this-cybersource.com.au wrote in message 
news:4c79c510$0$28655$c3e8...@news.astraweb.com...

On Sat, 28 Aug 2010 16:12:36 -0700, Baba wrote:



There are other techniques, but this will do to get started.


Once you realize recursion for Fibonacci numbers is still fairly slow, look 
up generator functions :^)


-Mark


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


Re: Fibonacci: How to think recursively

2010-08-28 Thread Chris Rebert
On Sat, Aug 28, 2010 at 7:25 PM, Steven D'Aprano
st...@remove-this-cybersource.com.au wrote:
 On Sat, 28 Aug 2010 16:12:36 -0700, Baba wrote:
 Level: beginner

 I would like to know how to approach the following Fibonacci problem:
snip
 my attempted rough code:

 def fibonacci(n):
     # base case:
         result = fibonacci (n-1) + fibonacci (n-2)
 this will end up in a mess as it will create overlapping recursions

 Mathematically, there is nothing wrong with overlapping recursion. It
 will work, and Python can handle it easily.

 But in practical terms, it can lead to great inefficiency. In this
 example, it should be avoided because it is slow. Very slow. To calculate
 the nth Fibonacci number using naive recursion requires *many* calls:

    fib(4)                                # first call
    = fib(3) + fib(2)                    # three calls
    = fib(2) + fib(1) + fib(1) + fib(0)  # seven calls
    = fib(1) + fib(0) + 1 + 1 + 0        # nine calls
    = 1 + 0 + 1 + 1 + 0 = 3

 So to get fib(4) = 3 requires nine calls to fib().

 This growth function doesn't have a name (as far as I know),

It's A001595 in OEIS; http://www.research.att.com/~njas/sequences/A001595
Sometimes called 'Leonardo numbers'
Also apparently 2-ranks of difference sets constructed from Segre hyperovals.

 but it grows
 much faster than fib() itself:

 n      = 0   1   2   3   4   5   6   ... 35       ...
 fib(n) = 0   1   1   2   3   5   8   ... 9227465  ...
 calls  = 1   1   3   5   9   15  25  ... 29860703 ...

 As you can see, the number of calls is also calculable by a recursive
 expression R:

 R(0) = R(1) = 1
 R(n) = R(n-1) + R(n-2) + 1

 This is very similar to the Fibonacci recursion, only it grows more
 quickly. But I digress...

Other formulations courtesy OEIS:

R(n) = sum(fib(i) for i in range(1, n+1)) - fib(n-1)

R(n) = 2*fib(n+1) - 1

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tag parsing in python

2010-08-28 Thread Paul McGuire
On Aug 28, 11:14 am, agnibhu dee...@gmail.com wrote:
 Hi all,

 I'm a newbie in python. I'm trying to create a library for parsing
 certain keywords.
 For example say I've key words like abc: bcd: cde: like that... So the
 user may use like
 abc: How are you bcd: I'm fine cde: ok

 So I've to extract the How are you and I'm fine and ok..and
 assign them to abc:, bcd: and cde: respectively.. There may be
 combination of keyowords introduced in future. like abc: xy: How are
 you
 So new keywords qualifying the other keywords so on..
 So I would like to know the python way of doing this. Is there any
 library already existing for making my work easier. ?

 ~
 Agnibhu

Here's how pyparsing can parse your keyword/tags:

from pyparsing import Combine, Word, alphas, Group, OneOrMore, empty,
SkipTo, LineEnd

text1 = abc: How are you bcd: I'm fine cde: ok
text2 = abc: xy: How are you

tag = Combine(Word(alphas)+:)
tag_defn = Group(OneOrMore(tag))(tag) + empty + SkipTo(tag |
LineEnd())(body)

for text in (text1,text2):
print text
for td in tag_defn.searchString(text):
print td.dump()
print

Prints:

abc: How are you bcd: I'm fine cde: ok
[['abc:'], 'How are you']
- body: How are you
- tag: ['abc:']
[['bcd:'], I'm fine]
- body: I'm fine
- tag: ['bcd:']
[['cde:'], 'ok']
- body: ok
- tag: ['cde:']

abc: xy: How are you
[['abc:', 'xy:'], 'How are you']
- body: How are you
- tag: ['abc:', 'xy:']



Now here's how to further use pyparsing to actually use those tags as
substitution macros:

from pyparsing import Forward, MatchFirst, Literal, And, replaceWith,
FollowedBy

# now combine macro detection with substitution
macros = {}
macro_substitution = Forward()
def make_macro_sub(tokens):
macros[tuple(tokens.tag)] = tokens.body

# define macro substitution
macro_substitution  MatchFirst(
[(Literal(k[0]) if len(k)==1
else And([Literal(kk) for kk in
k])).setParseAction(replaceWith(v))
for k,v in macros.items()] ) + ~FollowedBy(tag)

return 
tag_defn.setParseAction(make_macro_sub)

scan_pattern = macro_substitution | tag_defn

test_text = text1 + \nBob said, 'abc:?' I said, 'bcd:.' + text2 +
\nThen Bob said 'abc: xy:?'

print test_text
print scan_pattern.transformString(test_text)


Prints:

abc: How are you bcd: I'm fine cde: ok
Bob said, 'abc:?' I said, 'bcd:.'abc: xy: How are you
Then Bob said 'abc: xy:?'

Bob said, 'How are you?' I said, 'I'm fine.'
Then Bob said 'How are you?'

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


Re: String substitution VS proper mysql escaping

2010-08-28 Thread Νίκος
On 28 Αύγ, 23:12, MRAB pyt...@mrabarnett.plus.com wrote:
 On 28/08/2010 20:51, Νίκος wrote:









  On 28 Αύγ, 22:35, MRABpyt...@mrabarnett.plus.com  wrote:

  When there's more than one value you provide a tuple. It's makes sense
  from the point of view of consistency that you also provide a tuple when
  there's only one value.

  Can you write something that make use of more than one value?

  Perhaps you mena somethign like?

  cursor.execute( '''SELECT hits FROM counters WHERE page = %s and date
  = %s and host = %s''' , (page,) )

  Is this what you mean?

  All those special format strign identifiers will grab their values out
  of the tuple?

 Your example contains 3 placeholders, so it needs 3 values:

      cursor.execute('''SELECT hits FROM counters WHERE page = %s and
 date = %s and host = %s''', (page, date, host))

 This will be safe. Any quoting that's needed will be done by .execute().

Will this also work without the parentheses?

 cursor.execute('''SELECT hits FROM counters WHERE page = %s and
 date = %s and host = %s''', page, date, host)

or python will not allow it cause it might think there are 4 args
isntead of two?


 cursor.execute(''' SELECT hits FROM counters WHERE page = '%s' and
 date = '%s' and host = '%s' ''', (page, date, host))

Whats happens if i attempt to also quote by single or double quoting
the above although now i'm aware that .execute method does the quoting
for me?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem checking an existing browser cookie

2010-08-28 Thread Νίκος
On 28 Αύγ, 23:15, MRAB pyt...@mrabarnett.plus.com wrote:
 On 28/08/2010 20:37, Íßêïò wrote:









  On 22 Áýã, 10:27, Íßêïònikos.the.gr...@gmail.com  wrote:
  On 16 Áýã, 14:31, Peter Otten__pete...@web.de  wrote:

  Íßêïò wrote:
  # initializecookie
  cookie=Cookie.SimpleCookie()
  cookie.load( os.environ.get('HTTP_COOKIE', '') )
  mycookie =cookie.get('visitor')

  if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta|
  yandex|13448|spider|crawl)', host ) is None:
       blabla...
  

  I checked and Chrome has acookienames visitor with a value ofnikos
  within.
  So, i have to ask why the if fails?

  Maybe it's because != != ==

  Iwant ti if code block to be executed only if the browsercookienames
  visitor fetched doesnt cotnain the vbalue of 'nikos'

  Is there somethign wrong with the way i wrote it?

  Please do help me with this too becaus eif i dont solve this my
  website keeps count my each visit like iam a guest visitor!

 Print out mycookie, repr(mycookie.value) (unless mycookie is None) and
 repr(host). Then follow the code yourself to see whether the condition
 is True.

print mycookie outputs 'None'

Thts weird because i check with the browser and the cookie is there!

print repr(host) outputs '78-236-176.adsl.cyta.gr'

repr(mycookie.value) (unless mycookie is None)

and also

print mycookie.value gives an error too. Maybe there is not a value
method?

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


Re: Problem checking an existing browser cookie

2010-08-28 Thread Νίκος
On 28 Αύγ, 23:07, Peter Otten __pete...@web.de wrote:
 Νίκος wrote:
  On 22 Αύγ, 10:27, Νίκος nikos.the.gr...@gmail.com wrote:
  On 16 Αύγ, 14:31, Peter Otten __pete...@web.de wrote:
   Νίκος wrote:
# initializecookie
   cookie=Cookie.SimpleCookie()
   cookie.load( os.environ.get('HTTP_COOKIE', '') )
mycookie =cookie.get('visitor')

if ( mycookie and mycookie.value != 'nikos' ) or re.search( r'(cyta|
yandex|13448|spider|crawl)', host ) is None:
blabla...


I checked and Chrome has acookienames visitor with a value ofnikos
within.
So, i have to ask why the if fails?

   Maybe it's because != != ==

  Iwant ti if code block to be executed only if the browsercookienames
  visitor fetched doesnt cotnain the vbalue of 'nikos'

  Is there somethign wrong with the way i wrote it?

  Please do help me with this too becaus eif i dont solve this my
  website keeps count my each visit like iam a guest visitor!

 In your initial post it sounded like you wanted the if-branch to execute for
 a user named nikos, but now it seems that I misunderstood you and swapping
 'mycookie.value != nikos' for 'mycookie.value == nikos' won't help.
 Maybe you could add a print statement like

 print mycookie.value

 to start with your debugging efforts.

 Peter

Maybe

 # initialize cookie
 cookie = Cookie.SimpleCookie()
 cookie.load( os.environ.get('HTTP_COOKIE', '') )
 mycookie = cookie.get('visitor')

wont load the cookie correctly? because

print mycookie.value

outputs an error.
-- 
http://mail.python.org/mailman/listinfo/python-list


comp.lang.python

2010-08-28 Thread roshini begum
www.127760.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


[issue9624] 2755

2010-08-28 Thread Jay Ballard

Jay Ballard jay.ballar...@gmail.com added the comment:

I'm trying to install Python

--
title: failure to find drive error message when trying to install something 
unspecified - 2755
Added file: http://bugs.python.org/file18660/unnamed
Added file: http://bugs.python.org/file18661/Python.JPG

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue9624
___ I#39;m trying to install Python
attachment: Python.JPG___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue9694] argparse: Default Help Message Lists Required Args As Optional

2010-08-28 Thread Steven Bethard

Steven Bethard steven.beth...@gmail.com added the comment:

I think this is still really a feature request. We can't just change the text 
from optional - that would silently change a large number of help messages 
without any warning. So to fix this bug, we're going to have to add an API to 
explicitly set the group names - which can only be done as a new feature. 
People using 2.7 will have to use the workaround using parser._optionals I 
posted here.

--
type: behavior - feature request
versions:  -Python 2.7

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



[issue9704] 3.2 - zlib.pc.in is missing in source tree

2010-08-28 Thread Martin v . Löwis

Martin v. Löwis mar...@v.loewis.de added the comment:

Ok, I have now added these files in r84332.

--
resolution:  - fixed
status: open - closed

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



[issue9696] xdrlib's pack_int generates DeprecationWarnings for negative in-range values

2010-08-28 Thread Mark Dickinson

Changes by Mark Dickinson dicki...@gmail.com:


--
nosy: +mark.dickinson

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



[issue9053] distutils compiles extensions so that Python.h cannot be found

2010-08-28 Thread Éric Araujo

Changes by Éric Araujo mer...@netwok.org:


--
stage:  - committed/rejected

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



[issue5217] testExtractDir (test.test_zipfile.TestWithDirectory) fails when python built with srcdir != builddir

2010-08-28 Thread Éric Araujo

Changes by Éric Araujo mer...@netwok.org:


--
status: closed - open

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



[issue9702] Python violates most users expectations via the modification differences of immutable and mutable objects in methods

2010-08-28 Thread R. David Murray

R. David Murray rdmur...@bitdance.com added the comment:

This is not an appropriate discussion for the bug tracker.  Please take it to 
the Python mailing list.

--

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



[issue9702] Python violates most users expectations via the modification differences of immutable and mutable objects in methods

2010-08-28 Thread david

david db.pub.m...@gmail.com added the comment:

On 28 August 2010 22:34, R. David Murray rep...@bugs.python.org wrote:

 R. David Murray rdmur...@bitdance.com added the comment:

 This is not an appropriate discussion for the bug tracker.  Please take it to 
 the Python mailing list.

Fair enough.
One last comment though (here) - I think that  making mutable objects
being immutable outside their immediate scope  (by *default* ) would
be a good solution.

--

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



[issue9702] Python violates most users expectations via the modification differences of immutable and mutable objects in methods

2010-08-28 Thread david

david db.pub.m...@gmail.com added the comment:

On 28 August 2010 22:41, david rep...@bugs.python.org wrote:

 david db.pub.m...@gmail.com added the comment:

 On 28 August 2010 22:34, R. David Murray rep...@bugs.python.org wrote:

 R. David Murray rdmur...@bitdance.com added the comment:

 This is not an appropriate discussion for the bug tracker.  Please take it 
 to the Python mailing list.

 Fair enough.
 One last comment though (here) - I think that  making mutable objects
 being immutable outside their immediate scope  (by *default* ) would
 be a good solution.

%s/being//

--

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



[issue2830] Copy cgi.escape() to html

2010-08-28 Thread Pablo Mouzo

Changes by Pablo Mouzo pablomo...@gmail.com:


Removed file: http://bugs.python.org/file18636/issue2830.diff

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



[issue2830] Copy cgi.escape() to html

2010-08-28 Thread Pablo Mouzo

Pablo Mouzo pablomo...@gmail.com added the comment:

I'm attaching a new patch without escaping the slash.

--
Added file: http://bugs.python.org/file18667/issue2830.diff

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



[issue1697175] winreg module for cygwin?

2010-08-28 Thread Brian Curtin

Changes by Brian Curtin cur...@acm.org:


--
status: open - languishing
versions: +Python 3.2 -Python 2.7

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



[issue1111130] tkSimpleDialog broken on MacOS X (Aqua Tk)

2010-08-28 Thread R. David Murray

R. David Murray rdmur...@bitdance.com added the comment:

I think we need a vendor problem resolution :)

Closed as 'works for me' instead since that's effectively what Ned said.

--
nosy: +r.david.murray
resolution:  - works for me
status: open - closed

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



  1   2   >