Re: Case study: debugging failed assertRaises bug

2011-04-27 Thread R David Murray
Ben Finney  benfinney.id.au> writes:
> > (1) assertRaises REALLY needs a better error message. If not a custom
> > message, at least it should show the result it got instead of an
> > exception.
> 
> +1
> 
> Is this one of the many improvements in Python 3.2's ‘unittest’ that
> Michael Foord presided over? Or are we still stuck with the terrible
> behaviour of ‘assertRaises’?

No, but issue 10775 [1] is just waiting for Michael (or someone else
who has time) to commit the patch.

--David

[1] http://bugs.python.org/issue10775


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


Re: should i move on to python3

2009-03-07 Thread R. David Murray
"Martin P. Hellwig"  wrote:
> Wensui Liu wrote:
> > i started learning python with earlier version and am happy with it
> > and all related packages, such as scipy, pywin, and so on.
> > right now, i am wondering if i should move to python3. if i do, will
> > all packages working on earlier version still work in python3? this is
> > my major concern.
>
> I wouldn't bet on it
>
> > my another question is how many python users will move to python3.
>
> Eventually all of them.
>  
> > any insight?
>
> Well I don't have insight in the mind of the core developers, but I 
> think you could compare it with any other major software upgrade.
> Think of it like people upgrading from windows NT4 to 2000, some 
> software works some not, some people are happy with NT4 others prefer to 
> keep up with current technology. Look at it like py3 is 2000 before the 
> first service pack, internally it works, could need some polishing but 
> that's about it. The things that don't work are more often caused by 
> third parties then the core development.

Comparing Python releases to Windows releases is...disturbing :)

> > thanks a lot.
>
> If you want to be more on the safe side, I would say wait for 3.2 or 
> even 3.3 . More specific, wait till the external module developers (the 
> ones you use) say it is stable on Py3

As of 3.1 (which fixes the speed problems of the new io package),
I think the only reason not to move to python3 will be any
dependency one might have on 3rd party packages that haven't
themselves made the switch yet.  Of course, that will be a big
issue for some time to come for many people.

--
R. David Murray   http://www.bitdance.com

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


Re: Packaging Survey

2009-03-09 Thread R. David Murray
"Werner F. Bruhin"  wrote:
> Tarek Ziadé wrote:
> > The Python Langage Summit is coming up. To prepare this event, I have
> > put online a survey you can take to tell us a bit more about you and
> > how you package your Python applications.
> > 
> > * Who should take the survey : any Python developer that packages
> > and distributes his code, no matter how.
> > * Take the survey: http://tinyurl.com/package-survey
> Get the following error on this link:
> Secure Connection Failed
> 
> mdp.cti.depaul.edu uses an invalid security certificate.
> 
> The certificate is not trusted because it is self signed.
> The certificate is only valid for Massimo Di Pierro
> The certificate expired on 01/03/2009 07:56.
> 
> (Error code: sec_error_expired_issuer_certificate)

The web _really, really_ needs some sort of mechanism for a site
to say "I'm not claiming anything about my identity, I'm just
providing you an https channel over which to talk to me
securely".

I fault the designers of https for this oversight.

--
R. David Murray   http://www.bitdance.com

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


Re: Set & Frozenset?

2009-03-10 Thread R. David Murray
Lie Ryan  wrote:
> Matt Nordhoff wrote:
> > Alan G Isaac wrote:
> >>> Hans Larsen schrieb:
> >>>> How could I "take" an elemment from a set or a frozenset 
> >>
> >> On 3/8/2009 2:06 PM Diez B. Roggisch apparently wrote:
> >>> You iterate over them. If you only want one value, use
> >>> iter(the_set).next()
> >>
> >> I recall a claim that
> >>
> >> for result in myset: break
> >>
> >> is the most efficient way to get one result.
> >> Is this right? (It seems nearly the same.)
> >>
> >> Alan Isaac
> > 
> > Checking Python 2.5 on Linux, your solution is much faster, but seeing
> > as they both come in under a microsecond, it hardly matters.
> 
> 
> It's unexpected...
> 
>  >>> timeit.timeit('res=iter(myset).next()', 'myset=range(100)')
> 0.8894412399647
>  >>> timeit.timeit('res=myset.next()', 'myset=range(100); 
> myset=iter(myset)')
> 0.4916552002516
>  >>> timeit.timeit('for res in myset: break', 'myset=range(100)')
> 0.3293300797699
> 
> I'd never expect that for-loop assignment is even faster than a 
> precreated iter object (the second test)... but I don't think this 
> for-looping variable leaking behavior is guaranteed, isn't it?

My guess would be that what's controlling the timing here is
name lookup.  Three in the first example, two in the second,
and one in the third.

--
R. David Murray   http://www.bitdance.com

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


Is this type of forward referencing possible?

2009-03-15 Thread R. David Murray
Kooks are people too  wrote:
> If I try this:
> 
> class  A:
> someType = B
> 
> class B:
> anotherType = A
> 
> I get:
> : name 'B' is not defined
>   args = ("name 'B' is not defined",)
>   message = "name 'B' is not defined"
> 
> Presumably because I'm instantiating an instance of a type object (B)
> that doesn't exist yet.

You've gotten some answers to how to do this already, but just to be
clear, you aren't instantiating anything in the code above, not even a
type object.  someType = B is simply creating 'someType' as a name in the
'class A' namespace that references whatever it is that B references.
This doesn't work, as others have pointed out, because at the point at
which class A's namespace is being constructed, 'B' does not yet have any
value in either the local namespace of class A or the global namespace
of the module.

To figure out how to write code like this that does what you want,
you need to understand how Python namespaces work.  A search on
'python namespace' should get you good information.  I found this
one, which is a nice summary but doesn't give examples:

http://www.network-theory.co.uk/docs/pytut/PythonScopesandNameSpaces.html

--
R. David Murray   http://www.bitdance.com

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


Re: Style question - defining immutable class data members

2009-03-15 Thread R. David Murray
M R A Barnett  wrote:
> Aaron Brady wrote:
> [snip]
> > However, in my (opined) interpretation, 'list.append(...) is an in-
> > place operation' is a factual error.  In-place operations -also-
> > rebind their 'argument' (FLOBW for lack of better words).  'append' is
> > a by-side-effect operation.  However colloquially it's mostly
> > accurate.
> > 
> [snip]
> All the augmented assignments rebind, even for objects which support
> in-place operations. For example:
> 
>  my_list = []
>  my_list += [0]
> 
> rebinds, but the equivalent:
>
>  my_list = []
>  my_list.extend([0])
> 
> doesn't.

Lest someone be mislead by this discussion, how about an example:

>>> a = b = []
>>> a.extend([1])
>>> a
[1]
>>> b
[1]
>>> a += [3]
>>> a
[1, 3]
>>> b
[1, 3]
>>> a = a + [5]
>>> a
[1, 3, 5]
>>> b
[1, 3]

It is technically correct that '+=' bebinds 'a', but what it
rebinds it to is the same object that was just mutated.  Unlike
the '+' case where 'a' is bound to a newly created list object.

Personally I think += and kin were a bad idea and should have been
removed in Python 3.0 :)  Even though I occasionally use them.  (Maybe
I'll stop.)

> Augmented assignments which don't support in-place operations behave
> like normal assignments (binding). For example:
> 
>  my_int = 0
>  my_int += 1
> 
> behaves like:
> 
>  my_int = 0
>  my_int = my_int + 1

--
R. David Murray   http://www.bitdance.com

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


Re: Style question - defining immutable class data members

2009-03-15 Thread R. David Murray
John Posner  wrote:
> Summary: I no longer suspect that "Python is broken". I *do* think that
> there's a situation that is potentially quite confusing:
> 
>  * In the statement "self.x = self.x + 1", the two "self.x" names can
>  sometimes refer to different objects.

But this is fundamental to Python.  The name (or slot, in the case of say
a slice assignment) on the left hand side is being given a new binding
by the assignment statement.  So whatever object the 'self.x' on the
left hand side may have pointed to before the statement is executed is
irrelevant (if we ignore '+=' and kin).  That is the essence of assignment
in Python.  If you are surprised by this, then you should probably study
up a bit on the way namespaces work in Python.

What I think you meant is that even though both are represented by the
same token sequence in the source ('self.x'), the two 'x's are actually
located in two different namespaces.  The one on the left hand side of
the assignment is local to the instance, while the one on the right hand
side can cascade upward to the class namespace and resolve to an object
from there.

>  * Even worse, in the equivalent statement "self.x += 1", the single name
>  "self.x" can sometimes refer to two different objects!

Now on this one I'll agree with you about the "worse" part.  Consider:

>>> class A(object):
... x = [1]
... def f(self):
... self.x = self.x + [2]
... print self.x
... 
>>> 
>>> m = A()
>>> m.f()
[1, 2]
>>> A.x
[1]
>>> class B(object):
... x = [1]
... def g(self):
... self.x += [2]
... print self.x
... 
>>> n = B()
>>> n.g()
[1, 2]
>>> B.x
[1, 2]

I'm inclined to call that a problem, myself, even though I understand
why it happens (+= mutates the object pointed to by the class variable
'x', and _then_ the instance variable 'x' is created and pointed at the
same object.  Whereas in the '+' case, a new list is created and the new
'x' instance variable is pointed at that new list.)

There is a somewhat analogous situation with variables in the local scope of
a function and global variables in the module.   For example, we might have:

>>> x = 1
>>> def f():
... print x
... 
>>> f()
1

So, when 'x' isn't found locally, the value gets picked up from the global
namespace.  The difference from the class/instance case is when we try
to assign to it:

>>> def g():
... x = x + 1
... 
>>> g()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in g
UnboundLocalError: local variable 'x' referenced before assignment
>>> x = []
>>> def g():
... x += [1]
... 
>>> g()
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in g
UnboundLocalError: local variable 'x' referenced before assignment

So in this case Python is warning us about the namespace difference.

Why are the two cases handled differently?  To tell you the truth, I'm
not sure.  I've been programming in Python for years and it just seems
to make sense to me to do it that way.  It does allow you to use class
variables as default values for instance variables, as long as you are
careful when using mutable objects.  But you could argue that it should
behave in a way analogous to local/global.  It would be interesting to
see that argument laid out in full, with all the consequences it would
entail examined.

> I think this situation should be handled in documentation. (I'm a tech writer
> in my day job ... oh wait, I forgot ... I got laid off from my day job in
> December.) I'll look into what the standard Python doc set says on this
> matter.

Doc patches are always welcome, and from what I hear easier to get
accepted than code patches ;)

--
R. David Murray   http://www.bitdance.com


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


Re: print - bug or feature - concatenated format strings in a print statement

2009-03-16 Thread R. David Murray
> On Mar 16, 5:00 pm, bdb112  wrote:
> > #   is the difference between
> > print(" %d,  %d, buckle my shoe" % (1,2))
> > #   and
> > print(" %d, " + " %d, buckle my shoe" % (1,2))
> > # a bug or a feature?

It is correct behavior.  On the other hand, it is one of the, well,
bugs, that is avoided by the 'format' method in 3.x.

--
R. David Murray   http://www.bitdance.com

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


setattr() on "object" instance

2009-03-16 Thread R. David Murray
Sean DiZazzo  wrote:
> Why is it that you can setattr() on an instance of a class that
> inherits from "object", but you can't on an instance of "object"
> itself?
> 
> >>> o = object()
> >>> setattr(o, "x", 1000)
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'object' object has no attribute 'x'
> 
> >>> class Object(object):pass
> ...
> >>> o = Object()
> >>> setattr(o, "x", 1000)
> >>> o.x
> 1000
> 
> I notice that the first example's instance doesn't have a __dict__.
> Is the second way the idiom?

The lack of a __dict__ is why you can't set the attribute.
I've occasionally wanted to use instances of object as holders of
arbitrary attributes and wondered why I couldn't (from a language design
perspective).  But that was only for testing.  In real code I think I'd
always want a fully defined class.

--
R. David Murray   http://www.bitdance.com

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


error writing str to binary stream - fails in Python 3.0.1, works in 2.x

2009-03-16 Thread R. David Murray
walle...@gmail.com wrote:
> I am working with a bit of code that works ok in Python 2.x (tested
> fine in 2.5.4 and 2.6.1) but fails in 3.0.1.
> The code opens a file for binary output to witht the objective to
> produce a .bmp graphics file.  The code below illustrates the first of
> several like errors when a str object is attempted to be written to
> the binary file.  From what I have seen, this was a fairly common
> technique prior to 3.0.1 being released so I am assuming the type
> checking is tighter with the new version.  What is the proper way of
> doing this now, or the work around?  Any help appreciated.  -- Bill
> 
> the code:
> ---
> self.out=open(filename,"wb")
> self.out.write("BM")  # magic number
> 
> 
> 
> This the is the error output from Python:
> 
> Traceback (most recent call last):
>   File "py_mandel.py", line 19, in 
> my_bmp=kohn_bmp("out.bmp",image_width,image_height,3)
>   File "C:\Python30\py_kohn_bmp.py", line 47, in __init__
> self.out.write("BM")  # magic number
>   File "C:\Python30\lib\io.py", line 1038, in write
> raise TypeError("can't write str to binary stream")
> TypeError: can't write str to binary stream

In 3.x the 'str' type is unicode.  If you want to work with binary byte
streams, you want to use the 'bytes' type.  Bytes contstants are
written with a leading 'b', so the code snipped above would become

self.out.write(b'BM')

--
R. David Murray   http://www.bitdance.com

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


Re: error writing str to binary stream - fails in Python 3.0.1, works in 2.x

2009-03-16 Thread R. David Murray
walle...@gmail.com wrote:
> On Mar 16, 4:10 pm, Benjamin Peterson  wrote:
> >   gmail.com> writes:
> >
> >
> >
> > > self.out.write(b'BM') worked beautifully.  Now I also have a similar
> > > issue, for instance:
> > > self.out.write("%c" % y) is also giving me the same error as the other
> > > statement did.
> > > I tried self.out.write(bytes("%c" %y),encoding=utf-8) in an attempt to
> > > convert to bytes, which it did, but not binary.  How do I affect
> > > self.out.write("%c" % y) to write out as a binary byte steam?  I also
> > > tried self.out.write(b"%c" % y), but b was an illegal operator in when
> > > used that way.
> > > It is also supposed to be data being written to the .bmp file. --Bill
> >
> > Are you writing to sys.stdout? If so, use sys.stdout.buffer.write(b'some
> > bytes'). If you're writing to a file, you have to open it in binary mode 
> > like
> > this: open("someimage.bmp", "wb")
> 
> Yes, I am writing to a file.  That portion is correct and goes like
> this:
>
> self.out=open(filename,"wb")
> self.out.write(b"BM")  # This line works thanks to advice given
># in previous reply
> 
> However, here is some more code that is not working and the error it
> gives:
>
> def write_int(self,n):
> str_out='%c%c%c%c' % ((n&255),(n>>8)&255,(n>>16)&255,(n>>24)&255)
> self.out.write(str_out) 
>
> this is line 29, does not work - not
> sure how to get this complex str converted over to binary bytes to
> write to bmp file.

(I reformatted your message slightly to make the code block stand out more.)

A byte array is an array of bytes, and it understands integers as input.
Check out the PEP (the official docs leave some things out):

http://www.python.org/dev/peps/pep-0358/

Here is some example code that works:

out=open('temp', "wb")
out.write(b"BM")

def write_int(out, n):
bytesout=bytes(([n&255), (n>>8)&255, (n>>16)&255, (n>>24)&255])
out.write(bytesout)   

write_int(out, 125)

--
R. David Murray   http://www.bitdance.com

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


urllib2 (py2.6) vs urllib.request (py3)

2009-03-17 Thread R. David Murray
mattia  wrote:
> Hi all, can you tell me why the module urllib.request (py3) add extra 
> characters (b'fef\r\n and \r\n0\r\n\r\n') in a simple example like the 
> following and urllib2 (py2.6) correctly not?
> 
> py2.6
> >>> import urllib2
> >>> f = urllib2.urlopen("http://www.google.com";).read()
> >>> fd = open("google26.html", "w")
> >>> fd.write(f)
> >>> fd.close()
> 
> py3
> >>> import urllib.request
> >>> f = urllib.request.urlopen("http://www.google.com";).read()
> >>> with open("google30.html", "w") as fd:
> ... print(f, file=fd)
> ...
> >>>
> 
> Opening the two html pages with ff I've got different results (the extra 
> characters mentioned earlier), why?

The problem isn't a difference between urllib2 and urllib.request, it
is between fd.write and print.  This produces the same result as
your first example:


>>> import urllib.request
>>> f = urllib.request.urlopen("http://www.google.com";).read()
>>> with open("temp3.html", "wb") as fd:
... fd.write(f)


The "b''" is the stringified representation of a bytes object,
which is what urllib.request returns in python3.  Note the 'wb',
which is a critical difference from the python2.6 case.  If you
omit the 'b' in python3, it will complain that you can't write bytes
to the file object.

The thing to keep in mind is that print converts its argument to string
before writing it anywhere (that's the point of using it), and that
bytes (or buffer) and string are very different types in python3.

--
R. David Murray   http://www.bitdance.com

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


Re: download x bytes at a time over network

2009-03-17 Thread R. David Murray
Saurabh  wrote:
> > This isn't exactly how things work.  The server *sends* you bytes.  It can
> > send you a lot at once.  To some extent you can control how much it sends
> > before it waits for you to catch up, but you don't have anywhere near
> > byte-level control (you might have something like 32kb or 64kb level
> > control).
> 
> What abt in Python3 ?
> It seems to have some header like the one below : b'b495 - binary mode
> with 46229 bytes ? Or is it something else ?
> 
> >>> import urllib.request
> >>> url = "http://feeds2.feedburner.com/jquery/";
> >>> handler = urllib.request.urlopen(url)
> >>> data = handler.read(1000)
> >>> print("""Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100))
> Content :
> 
> b'b495\r\n\r\nhttp://www.bitdance.com

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


Re: download x bytes at a time over network

2009-03-17 Thread R. David Murray
Jean-Paul Calderone  wrote:
> On Tue, 17 Mar 2009 12:15:23 +0530, Saurabh  wrote:
> >> This isn't exactly how things work.  The server *sends* you bytes.  It can
> >> send you a lot at once.  To some extent you can control how much it sends
> >> before it waits for you to catch up, but you don't have anywhere near
> >> byte-level control (you might have something like 32kb or 64kb level
> >> control).
> >
> >What abt in Python3 ?
> >It seems to have some header like the one below : b'b495 - binary mode
> >with 46229 bytes ? Or is it something else ?
> 
> That's just a bug in urllib in Python 3.0.

What makes you say that's a bug?  Did I miss something?  (Which is entirely
possible!)

--
R. David Murray   http://www.bitdance.com

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


Re: urllib2 (py2.6) vs urllib.request (py3)

2009-03-17 Thread R. David Murray
mattia  wrote:
> Il Tue, 17 Mar 2009 10:55:21 +0000, R. David Murray ha scritto:
> 
> > mattia  wrote:
> >> Hi all, can you tell me why the module urllib.request (py3) add extra
> >> characters (b'fef\r\n and \r\n0\r\n\r\n') in a simple example like the
> >> following and urllib2 (py2.6) correctly not?
> >> 
> >> py2.6
> >> >>> import urllib2
> >> >>> f = urllib2.urlopen("http://www.google.com";).read() fd =
> >> >>> open("google26.html", "w")
> >> >>> fd.write(f)
> >> >>> fd.close()
> >> 
> >> py3
> >> >>> import urllib.request
> >> >>> f = urllib.request.urlopen("http://www.google.com";).read() with
> >> >>> open("google30.html", "w") as fd:
> >> ... print(f, file=fd)
> >> ...
> >> >>>
> >> >>>
> >> Opening the two html pages with ff I've got different results (the
> >> extra characters mentioned earlier), why?
> > 
> > The problem isn't a difference between urllib2 and urllib.request, it is
> > between fd.write and print.  This produces the same result as your first
> > example:
> > 
> > 
> >>>> import urllib.request
> >>>> f = urllib.request.urlopen("http://www.google.com";).read() with
> >>>> open("temp3.html", "wb") as fd:
> > ... fd.write(f)
> > 
> > 
> > The "b''" is the stringified representation of a bytes object, which
> > is what urllib.request returns in python3.  Note the 'wb', which is a
> > critical difference from the python2.6 case.  If you omit the 'b' in
> > python3, it will complain that you can't write bytes to the file object.
> > 
> > The thing to keep in mind is that print converts its argument to string
> > before writing it anywhere (that's the point of using it), and that
> > bytes (or buffer) and string are very different types in python3.
> 
> Well... now in the saved file I've got extra characters "fef" at the 
> begin and "0" at the end...

The 'fef' is reminiscent of a BOM.  I don't see any such thing in the
data file produced by my code snippet above.  Did you try running that,
or did you modify your code?  If the latter, maybe if you post your
exact code I can try to run it and see if I can figure out what is going on.

I'm far from an expert in unicode issues, by the way :)  Oh, and I'm running
3.1a1+ from svn, by the way, so it is also possible there's been a bug
fix of some sort.

--
R. David Murray   http://www.bitdance.com

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


Keyword same in right hand side of assignments (rev)

2009-03-17 Thread R. David Murray
hwpus...@yahoo.de wrote:
> What I would like is to extend the augmented assignment
> and make it easy to understand for naive readers.

Good luck. :)

> I hope the following literary definition 
> is consistent enough to convey the correct meaning:
>   "whenever it is possible, modify the target IN PLACE 
>   according to the right hand side expression.
>   If it is not possible to do such a thing,
>   substitute the target object with 
>   an object that is build according to the right hand side expression
>   and subsequently deleted"

I don't think that last part is expressing your intent.  If you delete
the object you've constructed you've got nothing left for the target to
point to.  From what you say later I think you are confusing identifiers
with objects in this text.

> The following examples should be correct:
>   "xx = same + 5"  synonymous with  "xx += 5" 
>   "value =  2*same + 5"  synonymous with  "value =*2; value +=5" 
>   "switch = 1 - same"  synonymous with  "switch *-1; switch +=1" 
>   "lst = same + [5,6]"  synonymous with  "lst += [5,6]"
>   "lst[2] = 1/same" synonymous with  "lst[2] **=-1"

Your revised intent breaks my expectations of how python's assignment
operator works.  At least with += and kin I'm alerted to the fact that
something weird is going on by the fact that the assignment operator
is different.

> The following examples would be extensions:
>   "lst = [5,6] + same" synonymous with
>       "lst.reverse(); lst.extend([6,5]); lst.reverse()"
>   "inmutable = same*(same+1)"  synonymous with
>       "unused=inmutable+1; inmutable*=unused; del unused"
>
> There seems to be no really simple expression for the above extensions,
> and I take that as an indication
> that the proposed feature could be quite useful.

For the first one, we have:

lst[:0] = [5, 6]

And unless I'm misunderstanding you, the second one is trivially
expressed as:

immutable = immutable*(immutable+1)

I'm afraid I'm -1 on this proposal even without the issue of the keyword.

--
R. David Murray   http://www.bitdance.com

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


Re: Keyword same in right hand side of assignments (rev)

2009-03-17 Thread R. David Murray
Jacob Holm  wrote:
> I believe that as soon as the left-hand side stops being a simple 
> variable and it is used in non-trivial expressions on the right-hand 
> side, using the keyword would help clarify the intent.  What I mean is 
> that the examples you should be looking at are more like:
> 
> A[n+1] = same*same + 1
> B[2*j].foo = frobnicate(same, same+1)
> ...
> 
> If you try expanding these into current python with minimal change in 
> semantics you will end up with something like
> 
> _1 = n+1
> _2 = A[_1]
> A[_1] = _2*_2 + 1
> del _1
> del _2

Um, I'm not following you.  Why would this not translate to:

A[n+1] = A[n+1]*A[n+1] + 1

which I find more readable than the 'same' example. 

> _1 = B[2*j]
> _2 = _1.foo
> _1.foo = frobnicate(_2, _2+1)
> del _1
> del _2

I'd be looking for ways to refactor that code, no matter how you
write it.  But really,

B[2*j].foo = frobnicate(B[2*j].foo, B[2*j].foo + 1)

is to my eyes much clearer than the 'same' version.  I had to scan
the 'same' version twice to figure out what it was doing, and then
a third time to make sure I had it right.  This version has more
characters and thus more visual clutter, but all of the information
needed to understand what it is doing is right under my eyes as
I scan it.  I don't have to look back to the beginning of the
statement to remember what the function arguments are.

> I still think that the cost of a new keyword is probably too high a 
> price to pay, but I like the idea.

Yeah, you have to make a really, _really_ compelling case to get
a new keyword added.  Something you can't do any other way, or at
least not without an awful lot of hassle.

--
R. David Murray   http://www.bitdance.com

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


Re: Keyword same in right hand side of assignments (rev)

2009-03-17 Thread R. David Murray
Arg, my apologies, I posted my replies to the wrong group :(

--
R. David Murray   http://www.bitdance.com

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


Re: download x bytes at a time over network

2009-03-17 Thread R. David Murray
Jean-Paul Calderone  wrote:
> On Tue, 17 Mar 2009 15:17:56 + (UTC), "R. David Murray" 
>  wrote:
> >Jean-Paul Calderone  wrote:
> >> On Tue, 17 Mar 2009 12:15:23 +0530, Saurabh  wrote:
> >> >> This isn't exactly how things work.  The server *sends* you bytes.  It 
> >> >> can
> >> >> send you a lot at once.  To some extent you can control how much it 
> >> >> sends
> >> >> before it waits for you to catch up, but you don't have anywhere near
> >> >> byte-level control (you might have something like 32kb or 64kb level
> >> >> control).
> >> >
> >> >What abt in Python3 ?
> >> >It seems to have some header like the one below : b'b495 - binary mode
> >> >with 46229 bytes ? Or is it something else ?
> >>
> >> That's just a bug in urllib in Python 3.0.
> >
> >What makes you say that's a bug?  Did I miss something?  (Which is entirely
> >possible!)
> 
> I saw it in the Python issue tracker. :)  Python 3.0 broke handling of
> chunked HTTP responses.  Instead of interpreting the chunk length prefixes,
> it delivered them as part of the response.

Ah, got you.  Thanks for the info.

--
R. David Murray   http://www.bitdance.com

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


Re: array next pointer

2009-03-17 Thread R. David Murray
Benjamin Peterson  wrote:
> Luis Zarrabeitia  uh.cu> schrieb:
> > 
> > Works for python2.4 and 2.5 also.
> > 
> > In python3, this should be used instead:
> > 
> > >>> b = iter(a)
> > >>> c = next(b)
> > 
> > (btw, I love the new sentinel argument for the next function in python3!)
> 
> next() doesn't have a sentinel argument. It's iter() which does, and that's in
> 2.x also.

But it does have a 'default' argument, and you can pass that
a sentinel, so it amounts to the same thing ;)

--
R. David Murray   http://www.bitdance.com

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


Re: Mangle function name with decorator?

2009-03-18 Thread R. David Murray
> class foo_controller(Controller):
> __metaclass__= ControllerMetaclass
> def __init__(self, action, method = None):
> self.action = action
> self.method = method
> 
> 
> def foo(self):
> print "in foo()"
> 
> @get_only
> def foo(self):
> print "in get_only foo()"
> 
> def bar(self):
> print "in bar()"

I don't have any wisdom on the metaclass/decorator stuff, but what
about slightly reformulating the interface?  Instead of having the
programmer type, eg:

@GET
def foo(self): pass

@POST
def foo(self): pass

have them type:

def GET_foo(self): pass
    def POST_foo(self): pass

It's even one less character of typing (the  :)

--
R. David Murray   http://www.bitdance.com

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


Re: 2to3 does not fix FileType from types Module; no replacement?

2009-03-18 Thread R. David Murray
John Roth  wrote:
> On Mar 18, 5:48 am, Benjamin Peterson  wrote:
> >   tlink.de> schrieb:
> >
> > > and I could not find a replacement for 'FileType'.
> > > Has this been overlooked?
> >
> > > Found this thread which does not mention FileType:
> > >http://mail.python.org/pipermail/stdlib-sig/2008-April/thread.html#172
> >
> > > I guess I just did not see something simple.
> > > Anyone with a hint?
> >
> > That's because this isn't simple. Python 3 doesn't have one file type. It 
> > has
> > several different kinds of streams in the io module. If you're checking for 
> > a
> > file type, you'll want to use isinstance(some_file, io.IOBase). If you're
> > inheriting form it, I suggest you look at the io module's classes.
> 
> Nontheless, a warning would be helpful, possibly with some doc.

Submitting an issue to the tracker with this request would probably be a
good idea.  Even better if you include a suggested patch.

--
R. David Murray   http://www.bitdance.com

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


Re: array next pointer

2009-03-18 Thread R. David Murray
Duncan Booth  wrote:
> Armin  wrote:
> 
> > Could you give an example of next() with a sentinel and describe its
> > use case please?  I have a little trouble understanding what you guys
> > mean! 
> 
> It means you don't have to worry about next() throwing StopIteration. 
> 
> e.g.
> >>> def pairs(sequence, padding=None):
>   sequence = iter(sequence)
>   for a in sequence:
>   b = next(sequence, padding)
>   yield a, b
> 
>   
> >>> list(pairs('abcd'))
> [('a', 'b'), ('c', 'd')]
> >>> list(pairs('abcde'))
> [('a', 'b'), ('c', 'd'), ('e', None)]

That's using the next parameter for its primary purpose: supplying a default.
The sentinel sub-case might be something like this:

SENTINEL = object()
while somecondition:
#do something
val = next(someiterator, SENTINEL)
if val is SENTINEL: break
#do something with val

In most cases you'd refactor code like that into a generator or something,
but there are cases where a pattern like the above can be useful.  The
idea is that next will only return SENTINEL when the iterator is exhausted.
If you didn't use the SENTINEL, then StopIteration would be raised.  So you
could write the above as:

try: val = next(someiterator)
except StopIteration: break

but the version using SENTINEL avoids having the interpreter do the work
of generating a traceback, and is IMO slightly prettier.

I'm sure there are other use cases, too, but probably not a huge number
of them.  Certainly not as many as using the parameter as a default.

--
R. David Murray   http://www.bitdance.com

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


Re: Mangle function name with decorator?

2009-03-18 Thread R. David Murray
Adam  wrote:
> David, would you believe that I just posted about this very idea,  It
> doesn't seem to have shown up yet, though.  This idea works from the
> perspective of being trivially easy to implement.  I can easily write
> a metaclass that looks in the namespace for methods with names that
> start with GET_or POST_, or I can override __getattribute__ to do the
> look up that way.  However, there are a couple of weaknesses that I
> see with this approach.
> 
> First, from a purely aesthetic point of view, prepending the desired
> verb to the method name just looks a bit ugly.  Also, it makes it
> difficult to deal elegantly with avoiding duplicating code when one
> piece of logic should dealing with more than one verb.  So, if I want
> to have one method that works for GET and POST, I can do this:
> 
> def GET_foo(self):
> # Do stuff for GET
> 
> def POST_foo(self):
> return self.GET_foo()
> 
> but then I feel like I am cluttering my controller code with unneeded
> functions when writing
> @GET
> @POST
> def foo(self):
># Blah blah blah
> 
> would be so much neater.

def GET_foo(self):
#do stuff for GET/POST

POST_foo = GET_foo

> Or, I could allow method signatures like:
> def GET_POST_foo(self):
># Blah, blah, blah
> 
> But now my code to parse and manipulate or do lookups on methods names
> is much more complicated.  Also, it introduces difficult ambiguities
> in the case that an action of the controller has the same name as an
> HTTP Verb.  These ambiguities can be coded around, but doing so makes

Hmm.  I don't follow that.  GET_GET would seem to be unambiguous.

> the code more-and-more crufty and prone to breakage.  I don't want to
> build too much of a Rube Goldberg machine here, right?  What I really
> want to do is use Python's metaprogamming facilities to provide an
> elegant solution to a problem.  Unfortunately, I really don't think
> that it is going to work out in any way that is really satisfying.

Someone else suggested the property model, though that is probably not
as elegant as you want either.

Sohow about the below.  Extending it to handle multiple classes is
left as an exercise for the reader :)  As is extending it to handle
stacking the decorators.

--
R. David Murray   http://www.bitdance.com


registry = {}

def HTTP_decorator_factory(name):
def _(f):
registry['%s_%s' % (name, f.__name__)] = f
def _(self, *args, **kw):
return _dispatch(f.__name__, self, *args, **kw)
return _
return _

def _dispatch(name, self, *args, **kw):
return registry['%s_%s' % (self.mode, name)](self, *args, **kw)

GET = HTTP_decorator_factory('GET')
POST = HTTP_decorator_factory('POST')


class Controller(object):

def __init__(self, mode='GET'):
self.mode = mode

@POST
def foo(self):
print "in POST foo"

@GET
def foo(self):
print "in GET foo"


c1 = Controller(mode='GET')
c2 = Controller(mode='POST')

c1.foo()
c2.foo()

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


Disable automatic interning

2009-03-18 Thread R. David Murray
George Sakkis  wrote:
> Is there a way to turn off (either globally or explicitly per
> instance) the automatic interning optimization that happens for small
> integers and strings (and perhaps other types) ? I tried several
> workarounds but nothing worked:

No.  It's an implementation detail.

What use case do you have for wanting to disable it?

--
R. David Murray   http://www.bitdance.com

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


read web page that requires javascript on client

2009-03-18 Thread R. David Murray
Greg  wrote:
> Hello all, I've been trying to find a way to fetch and read a web page
> that requires javascript on the client side and it seems impossible.
> I've read several threads in this group that say as much but I just
> can't believe it to be true (I'm subscribing to the "argument of
> personal incredulity " here).
> 
> Clearly urllib and urllib2 don't seem to support this and I've looked
> at win32com.client and it's ScriptControl but that doesn't seem to be
> viable approach for this particular problem.
> 
> Does anyone have any suggestions, hack or ideas or am I missing
> something really obvious.

Well, this is what is called a Hard Problem :).  It requires not
only supporting the execution of javascript (and therefore an entire
additional language interpreter!), but also translating that
execution into something that doesn't have a browser attached to it
for input or output.

That said, I've heard mention here of something that can apparently be
used for this.  I think it was some incarnation of Webkit.  I remember
someone saying you wanted to use the one with, I think it was GTK
bindings, even though you were dealing with just network IO.  But I don't
remember clearly and did not record the reference.  Perhaps the person
who posted that info will answer you, or you will be able to figure out
from these clues.  Unfortunately I'm not 100% sure it was Webkit.

--
R. David Murray   http://www.bitdance.com

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


Re: alias method definitions / syntactic sugar suggestion

2009-03-18 Thread R. David Murray
Terry Reedy  wrote:
> andrew cooke wrote:
> > [sorry for dup terry; prev not to list]
> > 
> > Terry Reedy wrote:
> >> @alias('justAsFantastic')
> >> def someFantasticMethod(args): ...
> > 
> > does this exist?  i read the previous post and thought "i think a
> > decorator could do that", but i haven't written one.
> 
> Not that I know of -- an exercise for the reader.
> 
> Now that I think about it more, injecting a name into what will become 
> the class dict is not trivial.  I assume that one of the 'get the 
> caller's namespace' tricks would work, but have not tried it.
> 
> > the reason i ask is that for 3->2 backwards compatability i need to do
> > this in a couple of places.  it would be nice to have:
> > 
> >   @alias('__nonzero__')
> >   def __bool__(self):
> > 
> > and even better(?) if it could depend on python version!
> > 
> > or maybe there's another solution to the __bool__ problem above?  (there's
> > also next methods, can't think of anything else off the top of my head)

What we really need is a 3to2 script.  This has been suggested
before and even worked on, but as far as I can see there currently
is no such tool.

--
R. David Murray   http://www.bitdance.com

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


Tuple passed to function recognised as string

2009-03-18 Thread R. David Murray
Mike314  wrote:
> Hello,
> 
>I have following code:
> 
> def test_func(val):
> print type(val)
> 
> test_func(val=('val1'))
> test_func(val=('val1', 'val2'))
> 
> The output is quite different:
> 
> 
> 
> 
> Why I have string in the first case?

Because in Python the syntactic element that defines something
as a tuple is the comma, not the parenthesis:

>>> x = 1, 2
>>> type(x)

>>> y = (1,)
>>> type(y)

>>> z = 1,
>>> type(z)


In your function call the comma would normally be an argument
separator, so in that context you do need the parenthesis as well:

test_func(val=('val1',))

--
R. David Murray   http://www.bitdance.com

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


Re: Can I rely on...

2009-03-19 Thread R. David Murray
"Emanuele D'Arrigo"  wrote:
> Thank you everybody for the informative replies.
> 
> I'll have to comb my code for all the instances of "item in sequence"
> statement because I suspect some of them are as unsafe as my first
> example. Oh well. One more lesson learned.

You may have far fewer unsafe cases than you think, depending
on how you understood the answers you got, some of which
were a bit confusing.  Just to make sure it is clear
what is going on in your example

>From the documentation of 'in':

x in s   True if an item of s is equal to x, else False

(http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange)

Note the use of 'equal' there.  So for lists and tuples,

if x in s: dosomething

is the same as

for item in s:
if item == x:
do something
break

So:

>>> s = ['sdb*&', 'uuyh', 'foo']
>>> x = 'sdb*&'
>>> x is s[0]
False
>>> x in s
True

(I used a string with special characters in it to avoid Python's
interning of identifier-like strings so that x and s[0] would not be
the same object).

Your problem with the regex example is that re makes no promise that
patterns compiled from the same source string will compare equal to
each other.  Thus their _equality_ is not guaranteed.  Switching to
using an equals comparison won't help you avoid your problem in
the example you showed.

Now, if you have a custom sequence type, 'in' and and an '==' loop
might produce different results, since 'in' is evaluated by the special
method __contains__ if it exists (and list iteration with equality if
it doesn't).  But the _intent_ of __contains__ is that comparison be
by equality, not object identity, so if the two are not the same something
weird is going on and there'd better be a good reason for it :)

In summary, 'in' is the thing to use if you want to know if your
sample object is _equal to_ any of the objects in the container.
As long as equality is meaningful for the objects involved, there's
no reason to switch to a loop.

--
R. David Murray   http://www.bitdance.com

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


Concrete Factory Pattern syntax?

2009-03-19 Thread R. David Murray
Austin Schutz  wrote:
> 
> I have a fairly simple bit of code, something like:
> 
> # This should be importing the subclasses somehow, so that the factory
> # can make them.
> # import Parser.One
> # import Parser.Two
> # or.. from Parser import *?
> class Parser():
>def parse:
>   'Implemented only in subclass'

Hmm.  You need to go back to the tutorial, I think :)

This should be

def parse(self):
raise NotImplementedError

The raise NotImplementedError is the pythonic way of indicating
unimplemented methods in a superclass.  But also take a look
at ABCs (Abstract Base Classes), which provide some nice sugar
for this kind of thing.

>def make_parser(which_parser):
>if(which_parser = 'one'):
>  return One()
>else:
>   return Two()

Skip this, I'll rewrite it later.

> # import Parser?
> class One(Parser):
>def parse:
>'one implementation'

Again, 'def parse(self):'.  Might want to make the body something
like "print 'one implementation'" so you can tell if you got
the correct parser when you test it.

> class Two(Parser):
>def parse:
>'another implementation'

Same comments as above.

> The problem I have is that I don't understand how to put this into
> actual files in actual directories and have the interpreter do
> something actually useful :-) . What I would like to do is something
> like:
> 
> lib/
>   Parser.py
>   Parser/

Well, there's your first import mistake :).  You can't have a module
and a package both with the same name, nor do you need to.  So drop the
'Parser.py'.  And rename the directory 'parser'; module and package
names are lower case according to the Python style guide.

>   __init__.py (maybe?)

Yes.  Required if you are going to have a package you can import
things from.

>   One.py
>   Two.py

Again, rename these to 'one.py' and 'two.py'.  Then in your
__init__.py file, do:

from one import One
from two import Two

Now, in your code that uses the parser, do:

from parser import One as Parser

or

from parser import Two as Parser

depending on which parser you want.

Unless your parsers are really heavy, you could probably just
keep all this stuff in one file, parser.py...but if they are complex
enough you want to split the source up, and you want a base class
or ABC, then put that in another file (parserbase.py, perhaps),
and at the top of each of the individual parser files do

from parserbase import ParserBase

(or some such name).

Now, if you really need the parser to instantiate to be chosen at run
time via a string, you could add something like this to your __init__.py:

def parserFactory(which_parser):
return globals()[which_parser.capitalize()]()

which will look up the capitalized version of the string (eg: 'One')
in the __init__.py module's global namespace, thus picking up the
class, and then calls it to create an instance, which is then
returned.

Then your code that uses this can do:

    from parser import parserFactory

myparser = parserFactory('one')


>From your class heavy patterns I am guessing you are coming from
Java or some similar languageyou don't have to work as hard
to get things done in Python.

--
R. David Murray   http://www.bitdance.com

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


file.read() doesn't read the whole file

2009-03-20 Thread R. David Murray
Sreejith K  wrote:
> Hi,
> 
> >>> snapdir = './mango.txt_snaps'
> >>> snap_cnt = 1
> >>> block = 0
> >>> import os
> >>> os.chdir('/mnt/gfs_local')
> >>> snap = open(snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block)),'r')
> >>> snap.read()
> 'dfdfdgagdfgdf\ngdgfadgagadg\nagafg\n\nfs\nf\nsadf\n\nsdfsdfsadf\n'
> >>> snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block))
> './mango.txt_snaps/snap1/0'
> 
> The above code works fine and it reads the whole file till EOF. But
> when this method is used in a different scenario the file is not read
> completely. I'll post the code that read only some part of the file...
> 
> self.snap = open(self.snapdir + '/snap%d/%d' % (self.snap_cnt,
> block),'r') ## opens /mnt/gfs_local/mango.txt_snaps/snap1/0
> self.snap.seek(off%4096) ## seeks to 0 in this case
> bend = 4096-(off%4096) ## 4096 in this case
> if length-bend <= 0:## true in this case as length is 4096
>   tf.writelines("returned \n")
>   data = self.snap.read(length)
>   self.snap.close()
>   break
> 
> the output data is supposed to read the whole fie but it only reads a
> part of it. Why is it encountering an early EOF ?

It's not.  In the second case you told it to read only 4096 bytes.  You
might want to read the docs for the 'read' method, paying particular
attention to the optional argument and its meaning.

--
R. David Murray   http://www.bitdance.com

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


Re: Lambda forms and scoping

2009-03-20 Thread R. David Murray
Benjamin Peterson  wrote:
> Márcio Faustino  gmail.com> writes:
> > 
> > Executing the example below doesn't produce the expected behavior, but
> > using the commented code does. Is this normal, or is it a problem with
> > Python? I've tested it with version 2.6.1 on Windows XP.
> > 
> > Thanks,
> > 
> > --
> > 
> > from abc import *
> > from types import *
> > import re
> > 
> > class Base (ObjectType):
> > __metaclass__ = ABCMeta
> > 
> > def __init__(self):
> > for option in self.get_options().keys():
> > method = 'get_%s_option' % re.sub(' ', '_', option.lower
> > ())
> > setattr(self.__class__, method, lambda self:
> > self.get_option(option))
> 
> This is because the closure over option is changed when it is reassigned in 
> the
> for loop. For example:
> 
> >>> def f():
> ... return [lambda: num for num in xrange(2)] 
> ... 
> >>> f()
> [ at 0x83f30>,  at 0x83e70>]
> >>> f()[0]
>  at 0x83ef0>
> >>> g = f()
> >>> g[0]()
> 1
> >>> g[1]()
> 1

Here's the way I find it useful to think about this:

When your lambda is created in your for loop inside your __init__ method,
it acquires a pointer to __init__'s local namespace.  (That's how I
understand what "closure" means in this case, though I imagine "closure"
probably means something slightly different in computer-science-ese :)

So, when any of those lambdas is executed, they all have a pointer to
the exact same namespace, that of __init__.  And when they are called,
__init__'s namespace is in whatever state it was left in when __init__
ended.  In this case, that means that 'option' is pointing to the value
it had at the _end_ of the for loop.

Hope this helps.  I find that thinking in terms of namespaces helps
me understand how Python works better than any other mental model
I've come across.

--
R. David Murray   http://www.bitdance.com

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


Re: Can I rely on...

2009-03-20 Thread R. David Murray
alex23  wrote:
> On Mar 20, 1:42 am, "Emanuele D'Arrigo"  wrote:
> > I just had a bit of a shiver for something I'm doing often in my code
> > but that might be based on a wrong assumption on my part. Take the
> > following code:
> >
> > pattern = "aPattern"
> >
> > compiledPatterns = [ ]
> > compiledPatterns.append(re.compile(pattern))
> >
> > if(re.compile(pattern) in compiledPatterns):
> >     print("The compiled pattern is stored.")
> 
> Others have discussed the problem with relying on the compiled RE
> objects being the same, but one option may be to use a dict instead of
> a list and matching on the pattern string itself:
> 
> compiledPatterns = { }
> if pattern not in compiledPatterns:
> compiledPatterns[pattern] = re.compile(pattern)
> else:
> print("The compiled pattern is stored.")

FYI this is almost exactly the approach the re module takes to
caching the expressions.  (The difference is re adds a type
token to the front of the key.)

--
R. David Murray   http://www.bitdance.com

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


Re: file.read() doesn't read the whole file

2009-03-20 Thread R. David Murray

On Fri, 20 Mar 2009 at 07:09, Sreejith K wrote:

On Mar 20, 4:43?pm, "R. David Murray"  wrote:

Sreejith K  wrote:

Hi,



snapdir = './mango.txt_snaps'
snap_cnt = 1
block = 0
import os
os.chdir('/mnt/gfs_local')
snap = open(snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block)),'r')
snap.read()

'dfdfdgagdfgdf\ngdgfadgagadg\nagafg\n\nfs\nf\nsadf\n\nsdfsdfsadf\n'

snapdir + '/snap%s/%s' % (repr(snap_cnt), repr(block))

'./mango.txt_snaps/snap1/0'



The above code works fine and it reads the wholefiletill EOF. But
when this method is used in a different scenario thefileis notread
completely. I'll post the code thatreadonly some part of thefile...



self.snap = open(self.snapdir + '/snap%d/%d' % (self.snap_cnt,
block),'r') ## opens /mnt/gfs_local/mango.txt_snaps/snap1/0
self.snap.seek(off%4096) ## seeks to 0 in this case
bend = 4096-(off%4096) ## 4096 in this case
if length-bend <= 0: ? ?## true in this case as length is 4096
? ?tf.writelines("returned \n")
? ?data = self.snap.read(length)
? ?self.snap.close()
? ?break



the output data is supposed toreadthe whole fie but it only reads a
part of it. Why is it encountering an early EOF ?


It's not. ?In the second case you told it toreadonly 4096 bytes. ?You
might want toreadthe docs for the 'read' method, paying particular
attention to the optional argument and its meaning.

--
R. David Murray ? ? ? ? ?http://www.bitdance.com


Thanks for the reply,
Actually the file is only few bytes and file.read() and file.read
(4096) will give the same result, i.e the whole file. But in my case
its not happening (using it in python-fuse file class).. Any other
ideas ?


Not offhand.  If it were me I'd start playing with parameters and moving
things around, trying to find additional clues.  See if calling read
without the argument in the second case works, start stripping the second
case down until it starts working (even if wrongly for the ultimate goal),
and things like that.

--
R. David Murray   http://www.bitdance.com--
http://mail.python.org/mailman/listinfo/python-list


Re: file.read() doesn't read the whole file

2009-03-20 Thread R. David Murray
Sreejith K  wrote:
>   tf.writelines("Reading from Base 
> File\n")
>   self.file.seek(block*4096 + off%4096)
>   bend = 4096-(off%4096)
>   if length-bend <= 0: ## if only a part 
> of a block is to be read
> (not till the end of the block)
>   data = self.file.read(length)
>   break
>   data += self.file.read(bend)
>   length -= bend
>   off = 4096
>   return data
> 
> This is the filesystem class for files. Whenever a read occurs an
> instance is created and read function is called. In my example when
> accessing a file named 'mango.txt' it checks for mango.txt_snaps/snap1
> dirctory and open file '0' as self.snap. But the read() returns (i.e
> data) a small part
> 
> Almost all the code worked weird in this example. Apart from read(),
> the break and continue also works weird. When opening the file
> 'mango.txt' the following output is written by tf (an output file).
> Here METHOD is not NORMAL, self.snap_cnt is 1, blocks is [['0']]
> 
> File initiating..
> File initiated..
> Read length: 4096 offset: 0
> Snapshot 0 opened..
> Snap read
> Partial read from snap
> Snapshot 0 opened..
> Block not in snap
> Reading from Base File
> 
> See the weirdness of continue and break here ?(loop was supposed to
> loop only once as rev_snap_list contains 0 only)

I'm not going to look at all this code now...it looks way too complicated
and in need of some serious refactoring :)

But a couple of on-point comments:

How do you know rev_snap_list contains only 0?  You didn't log it.

Same for the read.  How do you know the read didn't read the whole
file?  You didn't log it.

Both your statements might be true, but until you show the logging
output proving it, you don't _know_ that your assumptions are true.

--
R. David Murray   http://www.bitdance.com

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


Re: How complex is complex?

2009-03-20 Thread R. David Murray
Terry Reedy  wrote:
> Vito De Tullio wrote:
> > Tim Roberts wrote:
> > 
> >> bearophileh...@lycos.com wrote:
> >>> In Python 3 those lines become shorter:
> >>>
> >>> for k, v in a.items():
> >>> {k: v+1 for k, v in a.items()}
> 
> This is nonsensical.  It creates and discards a complete new dict for 
> each item in the original dict.  The reuse of names 'k' and 'v' in the 
> comprehension just confuse.

You have to look back at the original post in which those
lines appeared, and then look back further at the post which
they were commenting on.  Those two lines do not constitute
consecutive lines of code, they are individual replacements
for individual lines in two different previous examples, one
of which updates the dict in place and the other of which
creates a new dict.

I think bearophile left out too much context :)

--
R. David Murray   http://www.bitdance.com

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


Re: Preparing teaching materials

2009-03-21 Thread R. David Murray
"Rhodri James"  wrote:
> On Fri, 20 Mar 2009 11:58:18 -,  wrote:
> 
> > I am considering teaching a beginning programming course using Python.
> > I would like to prepare my class handouts in such a way that I can
> > import the Python code from real ".py" files directly into the
> > documents. This way I can run real unit tests on the code to confirm
> > that they work as expected.
> >
> > I am considering using LaTeX to write the handouts and then converting
> > them to PDF files. I will probably use a Makefile to convert the LaTeX
> > with embedded Python code into the PDF files using pdflatex.
> >
> > I will probably organize my directory structure into sub-directories
> > py-src, py-test, doc-src, and doc-dist.
> >
> > I will be starting out using Windows Vista/cygwin and hopefully switch
> > to a Macbook this summer.
> >
> > Any thoughts?
> 
> Decide right now whether you're using Python 2.x or Python 3.x.  The
> switch from print-as-statement to print-as-function is one of the
> things that will throw beginners very badly indeed if your handouts
> and computers don't make the same assumptions!

Print as function can be used in 2.6+ via 'from __future__ import 
print_function'.
So it is probably best to teach print-as-function regardless.

--
R. David Murray   http://www.bitdance.com

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


Script for a project inside own directory

2009-03-22 Thread R. David Murray
=?UTF-8?Q?Filip_Gruszczy=C5=84ski?=  wrote:
> I am having a project built like this:
> 
> project
>module1.py
>module2.py
>packages1/
>  module3.py
> 
> etc.
> 
> I have script that uses objects from those modules/packages. If I keep
> this script inside project directory it's ok and it works. But I would
> like to move it to own scripts directory and from there it doesn't
> work. I think I understand why it doesn't work (the root of local
> packages and modules is there and it can't see what it above it), but
> I would like to ask if there is any workaround? I would like to keep
> all my scripts in separate dir instead of main dir. I like to keep it
> clean.

You need to put your project directory on the PYTHONPATH one way
or another.  I would suggest reading up on how the module search
path works in python (PYTHONPATH, sys.path) so that you can decide
which of the many possible ways to make this work will serve you
best.

--
R. David Murray   http://www.bitdance.com

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


Using python 3 for scripting?

2009-03-22 Thread R. David Murray
timo.my...@gmail.com (Timo =?utf-8?Q?Myyr=C3=A4?=) wrote:
> Hi,
> 
> I'll have to do some scripting in the near future and I was 
> thinking on using the Python for it. I would like to know which 
> version of Python to use? Is the Python 3 ready for use or should 
> I stick with older releases?

If you are using it for scripting that doesn't need anything not included
in the standard library (which is true of a lot of scripting tasks), then
I would say Python 3 is very ready.  You'll have some I/O performance
issues if do lots of I/O with 3.0, but 3.1 is almost out the door and
that fixes the I/O performance issue.

--
R. David Murray   http://www.bitdance.com

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


Generator

2009-03-22 Thread R. David Murray
mattia  wrote:
> Can you explain me this behaviour:
> 
> >>> s = [1,2,3,4,5]
> >>> g = (x for x in s)
> >>> next(g)
> 1
> >>> s
> [1, 2, 3, 4, 5]
> >>> del s[0]
> >>> s
> [2, 3, 4, 5]
> >>> next(g)
> 3
> >>>
> 
> Why next(g) doesn't give me 2?

Think of it this way:  the generator is exactly equivalent to
the following generator function:

def g(s):
for x in s:
yield x

Now, if you look at the documentation for the 'for' statement, there is
a big "warning" box that talks about what happens when you mutate an
object that is being looped over:

 There is a subtlety when the sequence is being modified by the loop (this
 can only occur for mutable sequences, i.e. lists). An internal counter is
 used to keep track of which item is used next, and this is incremented on
 each iteration. When this counter has reached the length of the sequence
 the loop terminates. This means that if the suite deletes the current (or
 a previous) item from the sequence, the next item will be skipped (since
 it gets the index of the current item which has already been treated).
 Likewise, if the suite inserts an item in the sequence before the current
 item, the current item will be treated again the next time through the
 loop. 

As you can see, your case is covered explicitly there.

If you want next(g) to yield 3, you'd have to do something like:

g = (x for x in s[:])

where s[:] makes a copy of s that is then iterated over.

--
R. David Murray   http://www.bitdance.com

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


Re: Using python 3 for scripting?

2009-03-22 Thread R. David Murray
timo.my...@gmail.com (Timo =?utf-8?Q?Myyr=C3=A4?=) wrote:
> Ok, I think I'll stick with the 2.6 then. I recall it gave 
> warnings about things that are deprecated in 3.0 so it will make 
> porting the scripts to 3.0 easier. 
> 
> I might try 3.0 once I know what kind of scripts are needed.

In case you don't recall, running your scripts under 2.6 with -3 will
give you useful info.

Someone else recommended 2.5, and that is a valid recommendation if you
are planning to ship your scripts off to a variety of target hosts.
Some linux distributions are still shipping with 2.5 as standard.
You'll run into some systems that haven't been updated from 2.4 yet,
for that matter.

But if this is for your own local use, personally I'd do (will be doing
new stuff) everything possible in 3, and only dropping back to 2.6 when
I have to.  Unfortunately in some cases I _do_ have to support a number
of servers that are still running 2.5 :(

--
R. David Murray   http://www.bitdance.com

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


Re: python 3, subclassing TextIOWrapper.

2009-03-22 Thread R. David Murray
"Gabriel Genellina"  wrote:
> En Sat, 21 Mar 2009 23:58:07 -0300,  escribió:
> > import re
> > import io
> >
> > class file(io.TextIOWrapper):
> >
> > '''
> > Enhance TextIO.  Streams have many sources,
> > a file name is insufficient.
> > '''
> >
> > def __init__(self,stream):
> > #self.stream = stream
> > super().__init__(stream.buffer)
> >
> >
> > print(file(open('p.py')).read())
> 
> You're taking a shortcut (the open() builtin) that isn't valid here.
> 
> open() creates a "raw" FileIO object, then a BufferedReader, and finally  
> returns a TextIOWrapper. Each of those has a reference to the previous  
> object, and delegates many calls to it. In particular, close() propagates  
> down to FileIO to close the OS file descriptor.
> 
> In your example, you call open() to create a TextIOWrapper object that is  
> discarded as soon as the open() call finishes - because you only hold a  
> reference to the intermediate buffer. The destructor calls close(), and  
> the underlying OS file descriptor is closed.
> 
> So, if you're not interested in the TextIOWrapper object, don't create it  
> in the first place. That means, don't use the open() shortcut and build  
> the required pieces yourself.
> 
> ---
> 
> There is another alternative that relies on undocumented behaviour: use  
> open to create a *binary* file and wrap the resulting BufferedReader  
> object in your own TextIOWrapper.
> 
> import io
> 
> class file(io.TextIOWrapper):
>   def __init__(self, buffer):
>   super().__init__(buffer)
> 
> print(file(open('p.py','rb')).read())

I'm wondering if what we really need here is either some way to tell open
to use a specified subclass(s) instead of the default ones, or perhaps
an 'open factory' function that would yield such an open function that
otherwise is identical to the default open.

What's the standard python idiom for when consumer code should be
able to specialize the classes used to create objects returned from
a called package?  (I'm tempted to say monkey patching the module,
but that can't be optimal :)

--
R. David Murray   http://www.bitdance.com

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


Re: Lambda forms and scoping

2009-03-22 Thread R. David Murray
"Gabriel Genellina"  wrote:
> En Fri, 20 Mar 2009 23:16:00 -0300, alex goretoy
>  escribió:
> 
> > i looks at lambdas as unbound functions(or super function), in the case
> > above we create the functions in a list places it in memory unboud, once
> > binding a call to the memory address space it returns the value
> >
> > it is basically same as doing this:
> > def f():
> > print "f"
> >
> > a=f #unbound function, same as rename function
> > a() #bind call to address space
> 
> Mmm, I don't quite understand what you said. lambda creates functions that
> aren't different than functions created by def: apart from the name,
> they're really the same thing.

Oh, good, I'm not the only one for whom the above didn't make sense :)
I feel a little less dense now.

> And if you imply that *where* you call a function does matter, it does
> not. A function carries its own local namespace, its own closure, and its
> global namespace. At call time, no additional "binding" is done (except
> parameters -> arguments).
> 
> (and the address space is always the one of the running process)

I poked around in the API docs and experimented with func_closure and
related attributes, and after bending my brain for a while I think I
understand this.  The actual implementation of the closure is a single
list of 'cell' objects which represent namespace slots in the nested
scopes in which the closed-over function is defined.

But the fact that it is a single list is an implementation detail, and
the implementation is in fact carefully designed so that conceptually
we can think of the closure as giving the function access to those
nested-scope namespaces in almost(*) the same sense that it has a
reference to the global and local namespaces.  That is, if what a name
in _any_ of those namespaces points to is changed, then the closed-over
function sees those changes.

In this way, we understand the original example:  when defining a
lambda having a 'free variable' (that is, one not defined in either the
local or global scope) that was a name in the surrounding function's
local namespace, the lambda is going to see any changes made by the
surrounding function with regards to what that name points to.  Thus,
the final value that the lambda uses is whatever the final value of the
for loop variable was when the surrounding function finished executing.

However, I think that a Python closure is not quite the same thing as a
'computer science' closure, for the same reason that people coming from a
language with variables-and-values as opposed to namespaces get confused
when dealing with Python function call semantics.  Consider:

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

That says that a closure can be used to provide a function with a private
set of variables that persist from one invocation to the next, so that
a value established in one call can be accessed in the next.  The last
part of that sentence is not true in Python, since any assignment inside
a function affects only the local (per-invocation) namespace or (given
a global statement) the global namespace.  A function cannot change the
thing pointed to by a name in the closure.  Only the outer function,
for whom that name is in its local namespace, can do that.

(*) That last sentence in the previous paragraph is why I said '_almost_
the same sense' earlier: a function can modify what names point to in
its local and global namespaces, but cannot modify what names point to
in the closure namespace.

Of course, we can produce the same _effect_ as a computer science closure
in Python by using mutable objects...which is exactly parallel to the
difference between passing mutable or immutable objects in a function
call.

--
R. David Murray   http://www.bitdance.com

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


Re: python 3, subclassing TextIOWrapper.

2009-03-22 Thread R. David Murray
"Gabriel Genellina"  wrote:
> En Sun, 22 Mar 2009 15:11:37 -0300, R. David Murray  
>  escribió:
> > "Gabriel Genellina"  wrote:
> >> En Sat, 21 Mar 2009 23:58:07 -0300,  escribió:
> >> >
> >> > class file(io.TextIOWrapper):
> >> >
> >> > '''
> >> > Enhance TextIO.  Streams have many sources,
> >> > a file name is insufficient.
> >> > '''
> >> >
> >> > def __init__(self,stream):
> >> > #self.stream = stream
> >> > super().__init__(stream.buffer)
> >> >
> >> >
> >> > print(file(open('p.py')).read())
> >>
> >>
> >> [...] So, if you're not interested in the TextIOWrapper object, don't  
> >> create it in the first place. That means, don't use the open() shortcut  
> >> and build
> >> the required pieces yourself.
> >>
> > I'm wondering if what we really need here is either some way to tell open
> > to use a specified subclass(s) instead of the default ones, or perhaps
> > an 'open factory' function that would yield such an open function that
> > otherwise is identical to the default open.
> >
> > What's the standard python idiom for when consumer code should be
> > able to specialize the classes used to create objects returned from
> > a called package?  (I'm tempted to say monkey patching the module,
> > but that can't be optimal :)
> 
> I've seen:
> - pass the desired subclass as an argument to the class constructor /  
> factory function.
> - set the desired subclass as an instance attribute of the factory object.
> - replacing the f_globals attribute of the factory function (I wouldn't  
> recomend this! but sometimes is the only way)
> 
> In the case of builtin open(), I'm not convinced it would be a good idea  
> to allow subclassing. But I have no rational arguments - just don't like  
> the idea :(

When 'file' was just a wrapper around C I/O, that probably made as much
sense as anything.  But now that IO is more Pythonic, it would be nice
to have Pythonic methods for using a subclass of the default classes
instead of the default classes.  Why should a user have to reimplement
'open' just in order to use their own TextIOWrapper subclass?

I should shift this thread to Python-ideas, except I'm not sure I'm
ready to take ownership of it (yet?). :)

--
R. David Murray   http://www.bitdance.com

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


loading program's global variables in ipython

2009-03-22 Thread R. David Murray
per  wrote:
> hi all,
> 
> i have a file that declares some global variables, e.g.
> 
> myglobal1 = 'string'
> myglobal2 = 5
> 
> and then some functions. i run it using ipython as follows:
> 
> [1] %run myfile.py
> 
> i notice then that myglobal1 and myglobal2 are not imported into
> python's interactive namespace. i'd like them too -- how can i do
> this?
> 
>  (note my file does not contain a __name__ == '__main__' clause.)

I'm not familiar with IPython, but perhaps 'from myfile import *'
would do what you want?

--
R. David Murray   http://www.bitdance.com

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


Re: Generator

2009-03-22 Thread R. David Murray
mattia  wrote:
> Il Sun, 22 Mar 2009 16:52:02 +0000, R. David Murray ha scritto:
> 
> > mattia  wrote:
> >> Can you explain me this behaviour:
> >> 
> >> >>> s = [1,2,3,4,5]
> >> >>> g = (x for x in s)
> >> >>> next(g)
> >> 1
> >> >>> s
> >> [1, 2, 3, 4, 5]
> >> >>> del s[0]
> >> >>> s
> >> [2, 3, 4, 5]
> >> >>> next(g)
> >> 3
> >> >>>
> >> >>>
> >> Why next(g) doesn't give me 2?
> > 
> > Think of it this way:  the generator is exactly equivalent to the
> > following generator function:
> > 
> > def g(s):
> > for x in s:
> > yield x
> > 
> > Now, if you look at the documentation for the 'for' statement, there is
> > a big "warning" box that talks about what happens when you mutate an
> > object that is being looped over:
> > 
> >  There is a subtlety when the sequence is being modified by the loop
> >  (this can only occur for mutable sequences, i.e. lists). An
> >  internal counter is used to keep track of which item is used next,
> >  and this is incremented on each iteration. When this counter has
> >  reached the length of the sequence the loop terminates. This means
> >  that if the suite deletes the current (or a previous) item from the
> >  sequence, the next item will be skipped (since it gets the index of
> >  the current item which has already been treated). Likewise, if the
> >  suite inserts an item in the sequence before the current item, the
> >  current item will be treated again the next time through the loop.
> > 
> > As you can see, your case is covered explicitly there.
> > 
> > If you want next(g) to yield 3, you'd have to do something like:
> > 
> > g = (x for x in s[:])
> > 
> > where s[:] makes a copy of s that is then iterated over.
> 
> Ok, thanks. Yes, I had the idea that a counter was used in order to 
> explain my problem. Now I know that my intuition was correct. Thanks.

By the way, it's not the 'for' loop that maintains the counter.  It's the
code that implements the iteration protocol for the thing being looped
over.  So theoretically you could make a subclass of list that would
somehow handle items being deleted or added in a sensible fashion.
But I doubt it is worth doing that, since the implementation would be
would be pretty idiosyncratic to the use-case.

--
R. David Murray   http://www.bitdance.com

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


Re: safely rename a method with a decorator

2009-03-22 Thread R. David Murray
Daniel Fetchinson  wrote:
> >> I'd like to implement a decorator that would rename the method which
> >> it decorates. Since it's a tricky thing in general involving all sorts
> >> of __magic__ I thought I would ask around first before writing
> >> something buggy :)
> >>
> >> It should work something like this:
> >>
> >> class myclass( object ):
> >> @rename( 'hello' )
> >> def method( self ):
> >> print 'ok'
> >>
> >> # tests
> >>
> >> inst = myclass( )
> >> inst.method( )   # raise an AttributeError
> >> inst.hello( )   # prints 'ok'
> >> myclass.method   # raise an AttributeError
> >> myclass.hello   # prints 
> >> assert 'method' in dir( myclass ) is False
> >> assert 'hello' in dir( myclass ) is True
> >>
> >> Any ideas?
> >>
> > What is your use case? Why don't you just give the method the right name
> > in the first place? :-)
> 
> The use case is that I'm writing a turbogears application in which the
> URLs are determined by the method names. People might want to change
> these names if they want to change the URLs. One way would be to put
> the method names into a turbogears configuration file and the @rename
> decorator could fetch it from there.

Use a WSGI routing engine instead.

--
R. David Murray   http://www.bitdance.com

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


Re: file.read() doesn't read the whole file

2009-03-23 Thread R. David Murray
Sreejith K  wrote:
> > Try and write an example that shows the problem in fifteen lines or
> > less. Much easier for us to focus on the issue that way.
> 
> import os
> def read(length, offset):
>   os.chdir('/mnt/gfs_local/')
>   snap = open('mango.txt_snaps/snap1/0','r')
>   snap.seek(offset)
>   data = snap.read(length)
>   print data
> 
> read(4096,0)
> 
> This code shows what actually happens inside the code I've written.
> This prints the 4096 bytes from the file '0' which is only 654 bytes.
> When we run the code we get the whole file. That's right. I also get
> it. But when this read() function becomes the file class read()
> function in fuse, the data printed is not the whole but only a few
> lines from the beginning. I usually use less to read a file, when
> 'less'ing a file (whose size is less than 4096bytes) a call to read
> (0,4096) is made and data is returned. 'less' use this data returned
> by my fuse read() function to display its contents. But it was
> supposed to be the whole lines in the file like the example, but its
> not This is the problem I'm facing. Did I do something wrong here ?

If I'm understanding you correctly, you are saying that when you use
this function as the fuse read function you don't get the whole file,
and you are verifying this by using 'less' to read the 'file' exposed
by fuse.  Correct?

So you still have not decoupled the python read from the fuse read in
your debugging.  You are focused on the fact that the python read "must
be failing", yet you still (as far as you have told us) not _proven_
that by logging the value returned from the read.  Until you do that,
you can't even be sure where your problem is.

If you have done it, show us the logging output, please.

--
R. David Murray   http://www.bitdance.com

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


Re: Lambda forms and scoping

2009-03-23 Thread R. David Murray
"Gabriel Genellina"  wrote:
> > However, I think that a Python closure is not quite the same thing as a
> > 'computer science' closure, for the same reason that people coming from a
> > language with variables-and-values as opposed to namespaces get confused
> > when dealing with Python function call semantics.  Consider:
> >
> > http://en.wikipedia.org/wiki/Closure_(computer_science)
> >
> > That says that a closure can be used to provide a function with a private
> > set of variables that persist from one invocation to the next, so that
> > a value established in one call can be accessed in the next.  The last
> > part of that sentence is not true in Python, since any assignment inside
> > a function affects only the local (per-invocation) namespace or (given
> > a global statement) the global namespace.  A function cannot change the
> > thing pointed to by a name in the closure.  Only the outer function,
> > for whom that name is in its local namespace, can do that.
> 
> That's true in Python 2.x, but 3.x has the "nonlocal" keyword - so you can  
> modify variables in outer scopes too:
> 
> p3> z = 1
> p3> def o():
> ...   z = 2
> ...   def i():
> ... nonlocal z
> ... print("z in i:", z)
> ... z = 5
> ...   print("z in o:", z)
> ...   i()
> ...   print("z in o:", z)
> ...   z=3
> ...   print("z in o at exit:", z)
> ...   return i
> ...
> p3> i=o()
> z in o: 2
> z in i: 2
> z in o: 5
> z in o at exit: 3
> p3> z
> 1
> p3> i()
> z in i: 3
> p3> i()
> z in i: 5
> 
> (Anyway I think the inability to "modify" a variable doesn't invalidate  
> the "closure" concept...)

Invalidate, no, but it does mean that the word meant something slightly
different to a Python 2.x programmer than to, say, a Scheme programmer.
We could say that a Python 2.x closure is a "read-only closure".

But now with Python 3.x we can really have fun (thank you for that info):

>>> def g():
... def a(x):
... nonlocal z
... z = z + x
... def b(x):
... nonlocal z
...     z = z - x
... def p():
... print(z)
... z = 1
... return a, b, p
... 
>>> add, sub, pr = g()
>>> pr()
1
>>> add(10)
>>> pr()
11
>>> sub(5)
>>> pr()
6

So, as the wikipedia article says, we could, if we wanted to, use python 3
closures to reimplement objects, in a very confusing fashion :)

--
R. David Murray   http://www.bitdance.com

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


Re: Generator

2009-03-23 Thread R. David Murray
John Posner  wrote:
> 
> [snip]
> > > If you want next(g) to yield 3, you'd have to do something like:
> > > 
> > > g = (x for x in s[:])
> > > 
> > > where s[:] makes a copy of s that is then iterated over.
>  
> 
> BTW, this simpler statement works, too:
> 
>g = iter(s[:])

Yes, but one presumes that in the real code that prompted the OP's
question he wasn't just returning 'x'.

--
R. David Murray   http://www.bitdance.com

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


Re: file.read() doesn't read the whole file

2009-03-23 Thread R. David Murray
Steve Holden  wrote:
> Sreejith K wrote:
> >> Try and write an example that shows the problem in fifteen lines or
> >> less. Much easier for us to focus on the issue that way.
> > 
> > import os
> > def read(length, offset):
> > os.chdir('/mnt/gfs_local/')
> > snap = open('mango.txt_snaps/snap1/0','r')
> > snap.seek(offset)
> > data = snap.read(length)
> > print data
> > 
> > read(4096,0)
> > 
> > This code shows what actually happens inside the code I've written.
> > This prints the 4096 bytes from the file '0' which is only 654 bytes.
> > When we run the code we get the whole file. That's right. I also get
> > it. But when this read() function becomes the file class read()
> > function in fuse, the data printed is not the whole but only a few
> > lines from the beginning.
> 
> This is confusing. I presume you to mean that when you make this
> function a method of some class it stops operating correctly?
> 
> But I am not sure.
> 
> I am still struggling to understand your problem. Sorry,it's just a
> language thing. If we take our time we will understand each other in the
> end.

You may be asking this question for pedagogical reasons, Steve, but
in case not...the OP is apparently doing a 'less ' where  is
the name of a file in a fuse filesystem (that is, a mounted filesystem
whose back end is some application code written by the OP).  So when
the OP runs less, several calls get made to fuse, which passes them to
fuse-python, which calls methods on the OP's python class.  He is looking
in particular at the 'read' call, which happens after 'less' has opened
the file and wants to read a block (apparently either less or fuse is
asking for the first 4096 bytes of the file).  At that point his 'read'
method above is called.  But based on what he's told us it appears his
conclusion that the 'snap.read(length)' call is not returning the whole
file is based on the fact that less is only showing him part of the file.
There are several steps between that 'snap.read' and less displaying on
the terminal whatever bytes it got back from its read call in whatever
way it is less chooses to display them

--
R. David Murray   http://www.bitdance.com

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


udp package header

2009-03-23 Thread R. David Murray
mete  wrote:
> Hi all,
> I got a problem. İ want to send udp package and get this package (server and 
> clinet ). it's easy to python but i want to look the udp header how can i 
> do ?

The English word is 'packet'.

If you are on Linux you can use raw sockets for this.

--
R. David Murray   http://www.bitdance.com

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


Re: script files with python (instead of tcsh/bash)?

2009-03-23 Thread R. David Murray
Esmail  wrote:
> Hello again Nick,
> 
> thanks for the additional script example. I was able to put
> something together where I read the whole file into a list
> as a series of lines (via readlines()) and then loop through
> the lines seeing if the target string was "in" the line .. seems
> to have worked reasonably well.
> 
> I am sure over time I will pick up the more Python(ic?) ways of
> doing things.

Here's a more Pythonic way to do that:

with open('somefile') as f:
for line in f:
if 'somestring' in line:
#do something

In other words, you don't have to read the lines into a list first if all
you are going to do is iterate through them.  (The 'with' clause closes
the file at block exit...which is overkill if this is all the program
is doing since the file will be closed at program termination anyway,
but is a good habit to get in to.)

--
R. David Murray   http://www.bitdance.com

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


importing modules from alternate path

2009-03-24 Thread R. David Murray
=?UTF-8?Q?Alexandru__Mo=C8=99oi?=  wrote:
> I'm trying with no succes to load modules from an alternate path. When
> installing to default location (no --home specifed) everything works
> as expected.
> 
> $ python setup.py install --home=~
> running install
> running build
> running build_ext
> running install_lib
> running install_egg_info
> Removing /home/voodoo/lib/python/PackageName-1.0-py2.6.egg-info
> Writing /home/voodoo/lib/python/PackageName-1.0-py2.6.egg-info
> 
> $ printf "import demo" | PYTHONPATH=~ python
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named demo
> 
> $ printf "import demo" | PYTHONHOME=~ python
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: No module named demo
> 
> 
> Any idea why alternate path is not working?

Possibly because the package gets installed into ~/lib/python, but
you haven't put that directory onto the PYTHONPATH.

--
R. David Murray   http://www.bitdance.com

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


Re: file.read() doesn't read the whole file

2009-03-24 Thread R. David Murray
Sreejith K  wrote:
> On Mar 24, 2:12 pm, Ant  wrote:
> > On Mar 24, 7:59 am, Sreejith K  wrote:
> > ...
> >
> > > data is the whole file, but 'less' gives only the two lines...
> >
> > From this statement (that you are using less), it appears that you are
> > redirecting sys.stdout to a file or similar - if that is the case, you
> > may need to flush or close the output file before it picks up any
> > changes.

It's not a redirect to a file.  Fuse calls the 'read' function on the
class, the read function does a 'return' of the data, and fuse passes
the data up through the OS layer to be the result of the 'read' call
made by less.

If you don't know what a fuse file system is, this all gets very confusing :)

> Yes, I did try a flush() and close on the file which is read, but the
> result is the same. I think when a read comes in fuse redirecting the
> actual path (original file) to some other file for reading causes some
> issues like this. It would be really helpful if someone help me clear
> this issue. The getattr() calls are actually called upon the original
> file (doing an os.lstat()). Does this make any effect on the 'less'
> output ?

I'm afraid we are getting beyond my level of fuse-foo here.  You'd
probably be better off finding a fuse or even fuse-python mailing list
and trying there.

If it were me, I'd start logging everything I could (take a look at
Python's 'logging' module to help you make that easy), and twidling
things.  What happens if you change what gets returned to lstat?
What happens for various sizes and contents of the '0' file?  What
happens if you use 'cat -v' or hexdump instead of less to read the file?
Run experiments until you gather enough clues to make a guess as to what
is going on, then test your theory.  Repeat until success :)

--
R. David Murray   http://www.bitdance.com

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


Re: Relative Imports, why the hell is it so hard?

2009-03-24 Thread R. David Murray
Top posting corrected for clarity.

CinnamonDonkey  wrote:
> On 23 Mar, 18:57, bearophileh...@lycos.com wrote:
> > CinnamonDonkey:
> >
> > >what makes something a package?
> >
> > If you don't know what a package is, then maybe you don't need
> > packages.
> >
> > In your project is it possible to avoid using packages and just use
> > modules in the same directory?
> >
> > Bye,
> > bearophile
>
> Hi Bearophile,
> 
> Thanx for taking the time to post a response but I am afraid I feel
> the need to point out that it is exactly this kind of response that I
> find un-helpful. It is neither constructive nor educational.
> 
> It's a bit like saying "If you don't know what a function is, then
> maybe you don't need it. ... have you tried having a single block of
> code?"
> 
> The point of people coming to these forums is to LEARN and share
> knowledge. Perhaps it's not the best solution for me right now but
> without trying it I won't know when or how to apply it as a solution.
> 
> By the way, my project has about 50 files (modules) in it with a lot
> of shared code that could be used across other projects... seems as
> good a reason as any to try packages out ;-)
> 
> Thanx anyway :)

I think bearophile could have left out the first sentence, but otherwise
his question is perfectly sensible.  If you have a bunch of independent
modules, then you don't need to put them in packages.  Your example
only showed one module file in each package...I understand now that was
just for simplicity of the example, but we had no way of knowing that.
We've had newbies come in and think they _need_ to put a module file into
a subpackage even when they'd only have one module file per subdirectory
and they don't really know what a package is...thus bearophile's (perhaps
poorly phrased) question.

Now that you know what packages are and what the restrictions on relative
imports are, and you've told us that you have '50 modules' with a lot
of shared code that 'could be used across other projects', perhaps you
see why relative imports are generally discouraged.  If you use only
relative imports, the code _can't_ be shared across multiple projects,
because all that project code would have to be in one monster package,
and not be separate projects at all.

So now you'll know better where it makes Pythonic (as opposed to C++)
sense to use it and where not.

--
R. David Murray   http://www.bitdance.com

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


user site-packages, --prefix, --home and friends

2009-03-24 Thread R. David Murray
Carl  wrote:
> I am very confused by PEP-370 per-user site-packages. It is not
> mentioned at all in the document about installing Python modules :
> http://docs.python.org/3.0/install/index.html.
> 
> It seems that --home or --prefix already provide per-user site-
> packages capability. Can someone explain what are the differences and
> what is the best practice in various situations?

I'm by no means an authority, but by my reading of the PEP there are
two major items that differentiate --user from --prefix and --home:
automatic per-python-version installation, and handling of .pth files.

As for best practice, I would say whichever one meets your needs.
The PEP is addressing the needs of users who do not have root privs but
who want to install packages via distutils in such a way that they will
act as if they had been installed in the system site-packages directory.

--
R. David Murray   http://www.bitdance.com

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


Re: fft of a dat file?

2009-03-25 Thread R. David Murray
Soumen banerjee  wrote:
> Hello
> I have not tried the code because in no part of the code is the array
> "out" being created. As such, it is bound to return an error that out
> isnt created. The point here is how i can get sampled values from the
> dat file which has lines like this:-
> 
>\r\n
> 
> i need to isolate the sampled values and put them into an array.

Maybe something like:

samples = []
with open('myfile') as f:
for line in f:
time, value = line.strip().split()
samples.append([float(time), float(value)])

Modify as appropriate to your actual needs.

--
R. David Murray   http://www.bitdance.com

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


split string at commas respecting quotes when string not in csv format

2009-03-26 Thread R. David Murray
OK, I've got a little problem that I'd like to ask the assembled minds
for help with.  I can write code to parse this, but I'm thinking it may
be possible to do it with regexes.  My regex foo isn't that good, so if
anyone is willing to help (or offer an alternate parsing suggestion)
I would be greatful.  (This has to be stdlib only, by the way, I
can't introduce any new modules into the application so pyparsing is
not an option.)

The challenge is to turn a string like this:

a=1,b="0234,)#($)@", k="7"

into this:

[("a", "1"), ("b", "0234,)#($)#"), ("k", "7")]

--
R. David Murray http://www.bitdance.com

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


Re: split string at commas respecting quotes when string not in csv format

2009-03-26 Thread R. David Murray
Tim Chase  wrote:
>r = re.compile(r"""
>  (\w+)
>  \s*=\s*(
>  "(?:[^"]*)"
>  |
>  [^,]+
>  )
>  """, re.VERBOSE)
>results = [
>  (m.group(1), m.group(2).strip('"'))
>  for m in r.finditer(s)
>  ]
> 
> Things like internal quoting ('b="123\"456", c="123""456"') would 
> require a slightly smarter parser.

Thank you thank you.  I owe you a dinner if we are ever in the
same town (are you at Pycon?).

I'm not going to worry about the internal quotes unless it shows up in
the real data.  I'm pretty sure it's now allowed by the spec.

--
R. David Murray http://www.bitdance.com

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


Re: split string at commas respecting quotes when string not in csv format

2009-03-26 Thread R. David Murray
John Machin  wrote:
> On Mar 27, 6:51 am, "R. David Murray"  wrote:
> > OK, I've got a little problem that I'd like to ask the assembled minds
> > for help with.  I can write code to parse this, but I'm thinking it may
> > be possible to do it with regexes.  My regex foo isn't that good, so if
> > anyone is willing to help (or offer an alternate parsing suggestion)
> > I would be greatful.  (This has to be stdlib only, by the way, I
> > can't introduce any new modules into the application so pyparsing is
> > not an option.)
> >
> > The challenge is to turn a string like this:
> >
> >     a=1,b="0234,)#($)@", k="7"
> >
> > into this:
> >
> >     [("a", "1"), ("b", "0234,)#($)#"), ("k", "7")]
> 
> The challenge is for you to explain unambiguously what you want.
> 
> 1. a=1 => "1" and k="7" => "7" ... is this a mistake or are the quotes
> optional in the original string when not required to protect a comma?

optional.

> 2. What is the rule that explains the transmogrification of @ to # in
> your example?

Now that's a mistake :)

> 3. Is the input guaranteed to be syntactically correct?

If it's not, it's the customer that gets to deal with the error.

> The following should do close enough to what you want; adjust as
> appropriate.
> 
>  >>> import re
>  >>> s = """a=1,b="0234,)#($)@", k="7" """
>  >>> rx = re.compile(r'[ ]*(\w+)=([^",]+|"[^"]*")[ ]*(?:,|$)')
>  >>> rx.findall(s)
>  [('a', '1'), ('b', '"0234,)#($)@"'), ('k', '"7"')]
>  >>> rx.findall('a=1, *DODGY*SYNTAX* b=2')
>  [('a', '1'), ('b', '2')]
>  >>>

I'm going to save this one and study it, too.  I'd like to learn
to use regexes better, even if I do try to avoid them when possible :)

--
R. David Murray http://www.bitdance.com

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


Re: split string at commas respecting quotes when string not in csv format

2009-03-26 Thread R. David Murray
Paul McGuire  wrote:
> On Mar 26, 2:51 pm, "R. David Murray"  wrote:
> > OK, I've got a little problem that I'd like to ask the assembled minds
> > for help with.  I can write code to parse this, but I'm thinking it may
> > be possible to do it with regexes.  My regex foo isn't that good, so if
> > anyone is willing to help (or offer an alternate parsing suggestion)
> > I would be greatful.  (This has to be stdlib only, by the way, I
> > can't introduce any new modules into the application so pyparsing is
> > not an option.)
> 
> If you must cram all your code into a single source file, then
> pyparsing would be problematic.  But pyparsing's installation
> footprint is really quite small, just a single Python source file.  So
> if your program spans more than one file, just add pyparsing.py into
> the local directory along with everything else.

It isn't a matter of wanting to cram the code into a single source file.
I'm fixing a bug in a vendor-installed application.  A ten line locally
maintained patch is bad enough, installing a whole new external dependency
is just Not An Option :)

--
R. David Murray http://www.bitdance.com

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


object knows which object called it?

2009-04-06 Thread R. David Murray
Reckoner  wrote:
> hi,
> 
> I have the following problem: I have two objects, say, A and B, which
> are both legitimate stand-alone objects with lives of their own.
> 
> A contains B as a property, so I often do
> 
> A.B.foo()
> 
> the problem is that some functions inside of B actually need A
> (remember I said they were both standalone objects), so I have to
> often do:
> 
> A.B.foo_func(A)
> 
> Which is kind of awkward.
> 
> Is there some way that B.foo_func() could somehow know that it was
> called as a property of A in this way?
> 
> Note that I'm looking for the calling object and NOT the calling
> function.

You could probably do this by creating a custom __getattr__ method (or
maybe even just a property) on A that would recognize B as an object and
return it wrapped in a class that would pick up the __getattr__ call on B
and translate it into a real call on B passing A as the first argument.

But that kind of magic is not considered good Python practice ("explicit
is better than implicit").  And it would be quite inefficient :)

I think the OO way to do this is to provide a method on A that does the
right thing:

def Bfoo_func(self):
self.B.foo_func(self)

Or maybe you could look at generic methods, which provide a way
to do multiple dispatch.

--
R. David Murray http://www.bitdance.com

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


set python default encoding

2009-04-06 Thread R. David Murray
reetesh nigam  wrote:
> Hi All,
> I am unable to set the python default encoding.
> i used the following proccess to set the python encoding
> 
> import sys
> reload(sys)
> sys.setdefaultencoding('latin-1')
> 
> but it is giving me the same error :
> 
>   args = ('utf8', "MEDICINE '\xc4 ", 10, 12, 'invalid data',
>  0x036FFE90>>)
>   encoding = 'utf8'
>   end = 12
>   message = ''
>   object = "MEDICINE '\xc4 "
>   reason = 'invalid data'
>   start = 10
> 
> 
> Please tell me how to solve this problem.

That doesn't look anything like a python traceback.  I'm guessing
you are using some sort of web framework?  Perhaps you should
try asking in the forum for the framework.

--
R. David Murray http://www.bitdance.com

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


Re: print from a python script.

2009-04-06 Thread R. David Murray
Chris Rebert  wrote:
> On Mon, Apr 6, 2009 at 2:24 PM, Ronn Ross  wrote:
> > I'm trying to print a simple string to a network printer. This is what I
> > have so far:
> >
> > import os
> >
> > printer_path = "192.168.200.139"
> > p = os.popen(printer_path, 'w')
> > p.write("this is a printer test")
> > p.close()
> >
> > I'm trying to call the printer from its IP address. When I run the script I
> > get:
> > sh: 192.168.200.139: not found
> 
> Note that os.popen() is for running /commands/, not opening files or
> IP addresses. I think you want the httplib or urllib modules instead.

Actually in this case (_writing_ text to a printer) I think you want the
Linux CUPS package for access to your remote printer, and then popen on
the 'lp' command (but use subprocess.Popen instead, that's better).

By the way, when you get closer to getting the above to work, you're
going to want to add a '\n' to the end of that string you are writing.

--
R. David Murray http://www.bitdance.com

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


Some test fail on my new Python 2.6

2009-04-06 Thread R. David Murray
Sorin Schwimmer  wrote:
> I just downloaded and compiled Python 2.6 on a Gentoo Linux, IBM NetVista.
> 
> After going through the usual steps (./configure, make), I ran a test (make 
> test), and got some unexpected issues, which are detailed here:
> 
> # ./python Lib/test/test_tcl.py
> Traceback (most recent call last):
>   File "Lib/test/test_tcl.py", line 6, in 
> from Tkinter import Tcl
>   File "/install/Python-2.6.1/Lib/lib-tk/Tkinter.py", line 39, in 
> import _tkinter # If this fails your Python may not be configured for Tk
[...] 
> Anybody has a quick fix?

Yep.  Run 'make test' instead of running them one by one.  make test
will run them under regrtest, which has mechanisms for detecting tests
that are expected to or may reasonably fail on a given platform.  Then if
you still have errors, report back :)

--
R. David Murray

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


Re: Some test fail on my new Python 2.6

2009-04-07 Thread R. David Murray
Sorin Schwimmer  wrote:
> 
> > Run 'make test' instead of running them one by one.
> 
> I did it *before* going one by one. I then tried individual tests in hope of 
> getting hints of what to look for. Actualy, for the first three tests, the 
> "make test" script commented that it is unusual for them to fail on my 
> platform (linux2).

Oh, yeah, I forgot that bit.

The _tkinter error means you don't have TCL/TK installed.  Not a problem
unless you want to use tkinter.  Unfortunately I forget what your other
errors were.

Can you just post the Skip messages (and any real errors) from the
make test run?  I can run through them quickly for you then, and
then we can focus on the ones that are the real errors.

--
R. David Murray http://www.bitdance.com

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


cgi file limit size?

2009-04-07 Thread R. David Murray
davidj411  wrote:
> I am wondering where the limitation of filesize comes from when i
> upload a large file.
> it uploads when the filesize is less than 20 MB (but not if larger).
> the script does not limit the filesize so it is either an HTTP
> specification or a webserver limit, right?
> maybe my connection to the server is timing out during the upload?
> web server is IIS 6.0.
> python is 2.5.2.
> IIS webmapping does not use "-u" b/c nothing works when that option is
> used.

What are you using to do the upload?  What error message do you get?

--
R. David Murray http://www.bitdance.com

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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread R. David Murray
"andrew cooke"  wrote:
> Carl Banks wrote:
> > import collections
> > import itertools
> >
> > def createInitialCluster(fileName):
> > fixedPoints = []
> > # quantization is a dict that assigns sequentially-increasing
> > numbers
> > # to values when reading keys that don't yet exit
> > quantization = defaultdict.collections(itertools.count().next)
> > with open(fileName, 'r') as f:
> > for line in f:
> > dimensions = []
> > for s in line.rstrip('\n').split(","):
> > if isNumeric(s):
> > dimensions.append(float(s))
> > else:
> > dimensions.append(float(quantization[s]))
> > fixedPoints.append(Point(dimensions))
> > return Cluster(fixedPoints)
> 
> nice reply (i didn't know defaultdict worked like that - very neat).
> 
> two small things i noticed:
> 
> 1 - do you need a separate quantization for each column?  the code above
> might give, for example, non-contiguous ranges of integers for a
> particular column if a string occurs ("by accident" perhaps) in more than
> one.
> 
> 2 - don't bother with isNumeric.  just return the cast value or catch the
> exception:
> 
>   [...]
>   try:
> dimensions.append(float(s))
>   except:
> dimensions.append(float(quantization[s]))

No, no, no; never use a bare except! :)

Do it MRAB's way and catch ValueError.

--
R. David Murray http://www.bitdance.com

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


Re: in place list modification necessary? What's a better idiom?

2009-04-07 Thread R. David Murray

On Tue, 7 Apr 2009 at 09:01, andrew cooke wrote:

R. David Murray wrote:

  [...]
  try:
dimensions.append(float(s))
  except:
dimensions.append(float(quantization[s]))


No, no, no; never use a bare except! :)


can you explain why?  i can't think of any reason why the code would be
better catching a specific exception.

as a general rule, maybe, but in this particular case i can't see a reason
- so i'm not sure if you're just pedantically following rules or if i've
missed something i should know.


What if the user pressed ctl-c right when the float was being converted
and appended?

Never use a bare except unless you have a specific reason to do so,
and there are very few of those.  (Yes, I should have said it that way
to start with, my apologies for going hyperbolic.)  Using because it
doesn't _look_ like it will cause issues is just asking for hard to
track down bugs :).

--
R. David Murray http://www.bitdance.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with subprocess Module

2009-04-07 Thread R. David Murray
Tim Golden  wrote:
> tarun wrote:
> > Hello All,
> > 
> > I've a batch file to be  invoke using a python script. The batch file has
> > pause, and the time, I need to send some command to the batch file from my
> > scripts. I placed both, the batch file (test.bat) and the python script
> > (test.py) in the same folder. And executed 'test.py'
> 
> I can't reproduce this in Python 2.6.1. The following is the
> result of cut-and-pasting your test.py & test.bat and running
> them from the command line:
> 
> 
> C:\temp>test.py
> 
> C:\temp>echo "START'
> "START'
> 
> C:\temp>pause
> Press any key to continue . . .
> 
> C:\temp>echo 'END'
> 'END'
> 
> C:\temp>
> 
> 
> 
> which is pretty much which I'd expected. I know there have been quite
> a few changes to the subprocess module between Python 2.5 & 2.6 so
> maybe you need to upgrade. (Failing that, we'd have to work a bit
> harder to pin down a specific bug in the 2.5 code since 2.5 is now
> in bugfix-release mode only, I think).

2.5 is in security-fix-only mode.

--
R. David Murray http://www.bitdance.com

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


Re: Some test fail on my new Python 2.6

2009-04-08 Thread R. David Murray
uild failure.

> 320 tests OK.
> 3 tests failed:
> test_httpservers test_socket test_sys

So we understand test_socket and maybe test_sys.  You'll have to show
me the output of test_httpsservers run in verbose mode again to see
if I can make sense of that one, if you care.

> 38 tests skipped:
> test_aepack test_al test_applesingle test_bsddb185 test_bsddb3
> test_cd test_cl test_codecmaps_cn test_codecmaps_hk
> test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses
> test_dbm test_gl test_imgfile test_kqueue test_linuxaudiodev
> test_macos test_macostools test_multiprocessing test_normalization
> test_ossaudiodev test_pep277 test_py3kwarn test_scriptpackages
> test_socketserver test_sqlite test_startfile test_sunaudiodev
> test_tcl test_timeout test_unicode_file test_urllib2net
>     test_urllibnet test_winreg test_winsound test_zipfile64

These as it implies are all normal.

> 3 skips unexpected on linux2:
> test_tcl test_dbm test_multiprocessing

And these we've covered.

So your biggest barrier to getting your python working for your
purposes is getting python to find the libtk8.5.so.  Everything
else you can ignore, unless you want to try to track them down.

--
R. David Murray http://www.bitdance.com

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


Re: genetic algorithms in Python??

2009-04-08 Thread R. David Murray
Esmail  wrote:
> Hello Mohammed,
> 
> Yes, that would great. While I am comfortable with GAs,
> I'm still rather inexperienced with Python so seeing some
> implementation examples would be very useful.

A google for 'python genetic algorithms' turns up a number
of interesting hits.

I also remember seeing at least one package announced on
python-announce that referred to genetic algorithms,
so you might check the archives of that mailing list
as well.

--
R. David Murray http://www.bitdance.com

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


Re: Some test fail on my new Python 2.6

2009-04-08 Thread R. David Murray
ss.  Mine looks like this:

127.0.0.1   partner.bitdance.com partner localhost

I'm inclined to call that a bug in the tests.  You could file a bug
report if you feel so moved :)

> >> test_sys
> >> test test_sys failed -- Traceback (most recent call last):
> >>   File "/install/Python-2.6.1/Lib/test/test_sys.py", line 354, in 
> >> test_43581
> >> self.assert_(sys.__stdout__.encoding == sys.__stderr__.encoding)
> >> AssertionError
> > This one is puzzling.  Did you modify 'site.py'?  Or perhaps you
> > redirected stdout and stderr to different places and they
> > wound up with different encodings for some reason.  In which
> > case that one would be a bug in the tests as well.
> 
> No, I didn't, nor have I done anything with stdout, stderror. When I ran the 
> test, I did a make test | tee test.rez, which in my mind should have been 
> harmless.

Ah, but that does redirect stdout and not stderr.  stdout goes into the
pipe, and thence to 'tee', which writes one copy of the input lines to
stdout and one to the file.  Not sure what python would see as stdout
at that point, but I wouldn't be surprised if that was the source of
the problem (and that means that the test is a little fragile).  If you
rerun that test in verbose mode without the pipe into tee, does it pass?

> The output for individual test_httpservers was posted here:
> http://mail.python.org/pipermail/python-list/2009-April/708041.html

Hmm.  These puzzle me.

The first one is failing because for some reason the code is able to
read a file that has been chmoded to have no access.  Did you run the
tests as root, by any chance?

The rest of them might be failing because 'sys.executable' isn't
pointing at the actual instance of the python command for some reason.
If you do 'import sys; print sys.executable' what does it say?

But I'd think you'd see other failures in that case, so it might be
something else.  The bottom line is that the little script files that the
test writes are not returning anything for some reason.

If you want to debug this one further we may have to start sticking
prints into the tests and things like that.

--
R. David Murray http://www.bitdance.com

PS: 'issue 3111' means issue number 3111 on the Python bug
tracker at http://bugs.python.org.

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


Re: Some test fail on my new Python 2.6

2009-04-08 Thread R. David Murray
Sorin Schwimmer  wrote:
> /usr/local/lib is in /etc/ld.so.conf. The files are real. However,
> creating symlinks in /usr/lib targeting the libraries in /usr/local/lib,
> then recompiling, solved the tkinter problem. So, the conclusion is that
> only /usr/lib is consulted for the tcl/tk libraries.

Hmm.  That's an issue with your linux setup, then, since python uses
system facilities to load the shared libraries.  I'm wondering
if you just needed to run ldconfig...it might be that it hadn't
been run since you installed the libraries into /usr/local/lib...though
I would think the install process would have done that.

> multiprocessing - do I need it? Probably not. I can fork() a new process,
> so I can manage. I don't know what am I loosing without multiprocessing.

What you loose is the nice interprocesses communication facilities
provided by multiprocessing.  You might append your system details
(including the fact that you have /dev/shm) to that ticket, especially
if you think you are going to want to be running cooperating
processes that need to do more than just communicate via stdin/out.

> htttpservers is still failing the same.
> 
> Everything is done as root.

I got failures (different failures) when I tried running that test as
root.  You might try running it as a regular user, both before
and after the install.

> I'll move on with make install.
> 
> Thanks for your help; I'm progressing :-)

You are welcome.  Glad to be able to help out.

--
R. David Murray http://www.bitdamce.com

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


Re: is there a way to collect twitts with python?

2009-04-08 Thread R. David Murray
Michael Torrie  wrote:
> Tim Wintle wrote:
> > On Fri, 2009-04-03 at 14:58 -0600, Michael Torrie wrote:
> >> Oh wow.  If this is what Twitter does to one's ability to articulate
> >> clearly, I hope Twitter dies a horrible death and any APIs and Python
> >> bindings with it!
> > 
> > Thank you, thank you, thank you 
> > 
> > everyone around me seems to love that thing (twitter), and I still can't
> > work out why (apart from hacks such as using it as a hosted queue for
> > cross-server comms, or receiving cheap sms to your app)
> 
> Yeah.  Of course I always thought that IRC was a good fit for this
> purpose.  Virus writers seem to think so.  I wonder how Twitter would
> deal with viruses and worms using Twitter and a command and control
> communications mechanism.
> 
> People who love Twitter are also people who SMS a lot it seems.  Twitter
> probably is a natural evolution of SMS, melding the IRC idea with
> ridiculously short, hard-to-read, cryptic, lol-speak, messages.  I think
> the Japanese just might be on to something as no one over there uses
> SMS.  It's all e-mail to them.  Granted the scourge of abbreviated words
> and lol-speak is just as bad there.   Sigh.

lol-speak originated on IRC.  (Or probably even earlier on the original
bitnet relay chat.)

--
R. David Murray http://www.bitdance.com

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


communication between objects - help

2009-04-09 Thread R. David Murray
Murali kumar  wrote:
> hi all..
> 
> 
> please see my attached document..

I think you'll get more help if you post in plain text.

--
R. David Murray http://www.bitdance.com

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


xml.dom.minidom getElementsByTagName white space issue

2009-04-09 Thread R. David Murray
Leonardo lozanne  wrote:
> Hi,
>  
> I'm getting some XML tags with white spaces from a web service and when I try 
> to get them with the getElements ByTagName I'm not able to do so. I'm getting 
> an empty list. What I'm doing is:
>  
> #XML_response is an xml string
> xml_msg = xml.dom.minidom.parseString(XML_response)
>  
> nodes = xml_msg.getElementsByTagName("tag ten")  #tag name is "tag ten" with 
> a whitespace
>  
> It all works fine with tags like tag_seven but NOT for tag names with a white 
> space. I've tried some escape chars but it doesnt seems to work. 
>  
> Does anybody has the escape char sequence I should be using or a work around 
> for this? Thanks in advanced for your replies. 

As far as I can tell what you are getting is invalid XML.
So I think the answer is "you can't do that".

(cf: http://www.w3.org/TR/2004/REC-xml11-20040204/#NT-NameChar)

--
R. David Murray http://www.bitdance.com

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


iPython help, Docstring [source file open failed]

2009-04-14 Thread R. David Murray
Kegan  wrote:
> I use iPython installed from macport. When I am in the iPython shell,
> I do the following:
> 
> 
> In [8]: from datetime import timedelta
> 
> In [9]: timedelta??
> Type:   type
> Base Class: 
> String Form:
> Namespace:  Interactive
> File:   /opt/local/Library/Frameworks/Python.framework/
> Versions/2.5/lib/python2.5/lib-dynload/datetime.so
> Docstring [source file open failed]:
> Difference between two datetime values.
> 
> 
> Lets say I want more information about timedelta right at the shell
> (as oppose lookup at Python Doc). Can I somehow link the actual Python
> source code to iPython so that it can be accessed in that way?

Notice that the filetype is '.so'.  That means it is a compiled
C module, so there is no python source to view.

--
R. David Murray http://www.bitdance.com

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


Help with run command + variable.

2009-04-14 Thread R. David Murray
Daniel Holm  wrote:
> Hi everybody,
> 
> I'm creating my first app (SixA @ http://www.launchpad/gsixaxis) and I have
> gtk.combobox to chose a profile. When a profile is chosen I save the
> profiles name as a variable, and now I want to combine a command with the
> variable.
> 
> Lets say that the user choses th profile firefox. This should be exactly
> like running: 'sixa action profile firefox'
> So the command run by my python script should be something like: 'sixa
> action profile $profile'
> Where $profile is the chosen profile variable.
> 
> Here is the code:
> def on_profile_switch_changed(self, box):
> model = box.get_model()
> index = box.get_active()
> if index:
> profile = model[index][0]
> os.system("sixa action profile $profile")
> prf = pynotify.Notification ("SixA", "Profile changed to
> $profile", "sixa")
> prf.show ()
> 
> As you can see I also have a notifications on when the profile has been
> changed, which also shows the chosen profile.
> How do I do either of these?

Try googling for 'python string formatting'.  You'll find lot of useful
stuff if you read a bit.  You have three basic choices: traditional
'%' formatting, new-style .format() formatting, or using a Template
string (which would allow you to use the $varname format you used above).
Which you choose depends on how you want to work, but I'd recommend
.format() for most things.

Except for your os.system call, where I'd recommend using the subprocess
module instead, in which case you'd do something like this:

call(['sixa', 'action', 'profile', profile])

--
R. David Murray http://www.bitdance.com

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


Getting Newsgroup Headers

2009-04-16 Thread R. David Murray
aslkoi fdsda  wrote:
> I would like to read just the headers out of a newsgroup.
> Being a Python newbie, I was wondering if this is possible and how difficult
> it would be for a novice Python programmer.
> Thanks for any reply!
> [HTML part not displayed]

It's not hard at all.  I've pulled some bits and pieces out of the
self-written minimalist newsreader I'm responding to your post with,
and added some example usage code.  It should head you in the right
direction, and there's no advanced python involved here:

--
from email.parser import FeedParser
from nntplib import NNTP
from rfc822 import mktime_tz, parsedate_tz

class Article:

def __init__(self):
self.num = None
self.subject = None
self.poster = None
self.date = None
self.id = None
self.references = []
self.size = 0
self.lines = 0
self.newsgroups = []

def loadFromOverview(self, overview):
(self.subject, self.poster, self.date, self.id,
self.references, self.size, self.lines) = overview[1:]
try: self.date = mktime_tz(parsedate_tz(self.date))
except ValueError:
print "ERROR in date parsing (%s)" % self.date
self.date = None
return overview[0]


def loadMessage(self, server):
msgparser = FeedParser()
resp, num, id, lines = server.head(self.id)
msgparser.feed('\n'.join(lines)+'\n\n')
resp, num, id, lines = server.body(self.id)
msgparser.feed('\n'.join(lines)+'\n')
self.message = msgparser.close()



server = NNTP('news.gmane.org')
resp, count, first, last, name = server.group('gmane.comp.python.ideas')
resp, headersets = server.xover(str(int(last)-100), last)
articles = []
for h in headersets:
a = Article()
artnum = a.loadFromOverview(h)
articles.append(a)

anarticle = articles[0]
anarticle.loadMessage(server)
print dir(anarticle.message)
for header in anarticle.message.keys():
print "%s: %s" % (header, anarticle.message[header])

--

Heh, looking at this I remember it is several-years-old code and really
needs to be revisited and updated...so I'm not going to claim
that this is the best code that could be written for this task :)

Oh, and there's more involved in actually printing the headers if you
need to deal with non-ASCII characters ("encoded words") in the headers.
(That's in the docs for the email module, though it took me a bit to
figure out how to do it right.)

--
R. David Murray http://www.bitdance.com

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


how to know the importing file name from an imported file?

2009-04-17 Thread R. David Murray
Visco Shaun  wrote:
> Hi
> 
> Is there a way to know the name of the script(say A), which is importing
> a module(say B), from B?
> ie in above situation i should be able to get name 'A' through some way
> in B, when A contains an 'import B' statement.

Suppose module C imports module B as well?

So, no, not in any useful way.

--
R. David Murray http://www.bitdance.com

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


Re: Supply a plugin interface

2009-04-23 Thread R. David Murray
Ulrich Eckhardt  wrote:
> Johannes Bauer wrote:
> > What I'd like to add: I want the GUI users to supply plugin scripts,
> > i.e. offer some kind of API. That is, I want the user to write short
> > Python pieces which look something like
> > 
> > import guiapp
> > 
> > class myplugin():
> > def __init__(self):
> > guiapp.add_menu("foobar")
> > 
> > def exec(self, param):
> > print("foo")
> > 
> > the GUI application should now browse the plugin directory and read
> > those plugin python files and somehow incorporate (i.e. discover what
> > modules are there, instanciate, etc.)
> 
> You will find ways to scan a directory using os.path and related things. In
> order to import a module, you add the base directory to sys.path. Since you
> have just a variable with the module name (excluding the .py), you can't
> use 'import' as it is. Instead, use the __import__ functio (I wonder if
> there is anything more elegant) which returns the plugin.

In 2.7 and 3.1 there will be:

from importlib import import_module

myname = import_module('somename')

which has much more sensible semantics than __import__ does.

--
R. David Murray http://www.bitdance.com

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread R. David Murray
Gary Herron  wrote:
> MRAB wrote:
> > Grant Edwards wrote:
> >> On 2009-05-21, Christian Heimes  wrote:
> >>> seanm...@gmail.com schrieb:
>  The explaination in my introductory Python book is not very
>  satisfying, and I am hoping someone can explain the following to me:
> 
> >>> 4 / 5.0
>  0.80004
> 
>  4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
>  It bothers me.
> >>> Welcome to IEEE 754 floating point land! :)
> >>
> >> Floating point is sort of like quantum physics: the closer you
> >> look, the messier it gets.
> 
> +1 as QOTW
> 
> And just to add one bit of clarity:  This problem has nothing to do with 
> the OP's division of 4 by 5.0, but rather that the value of 0.8 itself 
> cannot be represented exactly in IEEE 754.  Just try
> 
>  >>> print repr(0.8)  # No division needed
> '0.80004'

Python 3.1b1+ (py3k:72432, May  7 2009, 13:51:24) 
[GCC 4.1.2 (Gentoo 4.1.2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 4 / 5.0
0.8
>>> print(repr(0.8))
0.8

In py3k Eric Smith and Mark Dickinson have implemented Gay's floating
point algorithm for Python so that the shortest repr that will round
trip correctly is what is used as the floating point repr

--David

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


Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1

2011-01-17 Thread R. David Murray
On Mon, 17 Jan 2011 08:33:42 +, Mark Summerfield  
wrote:
> from ..Graphics import Xpm
> SVG = 1
> 
> I can do the relative import with Python 3.0 and 3.1 but not with
> 3.2rc1:

What about 3.1.3?  I wonder if it is related to this issue:

http://bugs.python.org/issue7902

--
R. David Murray  www.bitdance.com
-- 
http://mail.python.org/mailman/listinfo/python-list


how to transfer my utf8 code saved in a file to gbk code

2009-06-07 Thread R. David Murray
higer  wrote:
> My file contains such strings :
> \xe6\x97\xa5\xe6\x9c\x9f\xef\xbc\x9a

If those bytes are what is in the file (and it sounds like they are),
then the data in your file is not in UTF8 encoding, it is in ASCII
encoded as hexidecimal escape codes.

> I want to read the content of this file and transfer it to the
> corresponding gbk code,a kind of Chinese character encode style.

You'll have to convert it from hex-escape into UTF8 first, then.

Perhaps better would be to write the original input files in UTF8,
since it sounds like that is what you were intending to do.

--
R. David Murray http://www.bitdance.com
IT ConsultingSystem AdministrationPython Programming

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


Re: how to transfer my utf8 code saved in a file to gbk code

2009-06-07 Thread R. David Murray
John Machin  wrote:
> On Jun 8, 12:13 am, "R. David Murray"  wrote:
> > higer  wrote:
> > > My file contains such strings :
> > > \xe6\x97\xa5\xe6\x9c\x9f\xef\xbc\x9a
> >
> > If those bytes are what is in the file (and it sounds like they are),
> > then the data in your file is not in UTF8 encoding, it is in ASCII
> > encoded as hexidecimal escape codes.
> 
> OK, I'll bite: what *ASCII* character is encoded as either "\xe6" or
> r"\xe6" by what mechanism in which parallel universe?

Well, you are correct that the OP might have had trouble parsing my
English.  My English is more or less valid ("[the file] is _in_ ASCII",
ie: consists of ASCII characters, "encoded as hexideicmal escape codes",
which specifies the encoding used).  But better perhaps would have been
to just say that the data is encoded as hexidecimal escape sequences.

--David

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


Python preprosessor

2009-06-07 Thread R. David Murray
Tuomas Vesterinen  wrote:
> I am developing a Python application as a Python2.x and Python3.0 
> version. A common code base would make the work easier. So I thought to 
> try a preprosessor. GNU cpp handles this kind of code correct:
> 
> 
> #ifdef python2
> print u'foo', u'bar'
> #endif
> #ifdef python3
> print('foo', 'bar')
> #endif
> 
> 
> results:
>  > cpp -E -Dpython2 test_cpp.py
> ...
> print u'foo', u'bar'
> 
> Any other suggestions?

There's a Google Summer of Code project to create a 3to2 processor.
That would let you maintain the code in 3.x, and have it automatically
translated on demand so that it will run under 2.x (where x goes back
to at least 5, I think, but I'm not sure).

Of course, it isn't finished yet, so it won't do you any good right at
the moment :(

--
R. David Murray http://www.bitdance.com
IT ConsultingSystem AdministrationPython Programming

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


problems while using pexpect: pexcept.TIMEOUT always

2009-06-08 Thread R. David Murray
Phoe6  wrote:
> I have been trying to use pexpect and I am failing with
> pexpect.TIMEOUT for all my attempts. In order to troubleshoot, I
> decided to go with simplest possible one.
> 
[...]
> 
> Can someone help me what I am doing wrong here?
> Why is not working for such a simple thing as ssh to my localhost.?

I would suggest using the 'setlog' method of child to get
more debugging information from pexpect.  I've found that the
best way to diagnose the source of a timeout.

--
R. David Murray http://www.bitdance.com
IT ConsultingSystem AdministrationPython Programming

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


Re: pylint naming conventions?

2009-06-08 Thread R. David Murray
Esmail  wrote:
> Ben Finney wrote:
> > My understanding of Esmail's original message was that, like many of us
> > on first running ‘pylint’ against an existing code base, the output is
> > astonishingly verbose and tedious to read. By the above I presume he's
> > being a good forum member and trying to find a minimal example that
> > shows the problem clearly :-)
> 
> Yes, that is my intention .. because the code I was checking was rather
> long, combined with the long pylint output it would make for a rather
> big posting.
> 
> I'm going to go back and re-read PEP 8 and see if I perhaps don't
> recall the right guidelines since no one else here seems to have had
> the same observation.

Well, I for one looked at that long pylint output when I first tried it,
and switched to another tool :)

(pyflakes...but I don't think it does PEP 8)

--
R. David Murray http://www.bitdance.com
IT ConsultingSystem AdministrationPython Programming

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


Am I doing this the python way? (list of lists + file io)

2009-06-08 Thread R. David Murray
Horace Blegg  wrote:
> So, Example: I'll read in a CSV file (just one, for now.) and store it into
> a list. Sometime later, I'll get another CSV file, almost identical/related
> to the first. However, a few values might have changed, and there might be a
> few new lines (entries) or maybe a few less. I would want to compare the CSV
> file I have in my list (in memory) to new CSV file (which I would probably
> read into a temporary list). I would then want to track and log the
> differences between the two files. After I've figured out what's changed, I
> would either update the original CSV file with the new CSV's information, or
> completely discard the original and replace it with the new one (whichever
> involves less work). Basically, lots of iterating through each entry of each
> CSV file and comparing to other information (either hard coded or variable).
> 
> So, to reiterate, are lists what I want to use? Should I be using something
> else? (even if that 'something else' only really comes into play when
> storing and operating on LOTS of data, I would still love to hear about it!)

Given your description, I don't see any reason to prefer any alternate
data structure.  1000 small CSV files should fit in a modern computer's
memory with no problem...and if it does become an issue, worry about it
then.

One thought, though:  you might want to create a list subclass to hold
your data, so that you can put useful-to-you methods on the subclass...

--
R. David Murray http://www.bitdance.com
IT ConsultingSystem AdministrationPython Programming

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


Re: zipfile doesn't compress very good, are there other solutions?

2009-06-12 Thread R. David Murray
Chris Rebert  wrote:
> On Thu, Jun 11, 2009 at 1:41 PM, Stef Mientki wrote:
> > Peter Otten wrote:
> >> Stef Mientki wrote:
> >>> Peter Otten wrote:
>  Stef Mientki wrote:
> > I packed all sources with zipfile,
> > but the compression doesn't seem to be very good.
> >
> 
>  If you don't specify the compression, the files are not compressed at
>  all. Just in case you didn't know...
> 
> >>>
> >>> .. and would you be willing to tell me how I could set the compression (
> >>> at maximum) ?
> >>>
> >>
> >> According to the documentation (hint, hint) there is only on and off.
> >>
> >> zipfile.ZipFile(filename, "w", compression=zipfile.ZIP_DEFLATED)
> >>
> >>
> >
> > sorry guys I made a mistake,
> > I did read the doc, but as there was no default value metioned,
> 
> Erm...
> 
> class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])
> 
> Open a ZIP file [...] compression is the ZIP compression method to
> use when writing the archive, and should be ZIP_STORED or
> ZIP_DEFLATED; [...] The default is ZIP_STORED. [...]
> 
> Though I admit the docs could definitely do with having
> "compression=ZIP_STORED" in the signature part of the doc.

Georg's been fixing the method signatures bit by bit to use keyword style.
Doc patches would be welcome to help speed up the conversion process.

--David

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