Re: Timestamps for TCP packets?

2009-10-02 Thread Miles Kaufmann

On Oct 2, 2009, at 12:03 AM, Thomas Johnson wrote:


Is there any way to get kernel-level timestamps for TCP packets while
still using the standard python sockets library for communication? I
need to communicate over a TCP connection as easily as possible, but
also record the timestamps of the incoming and outgoing timestamps at
microsecond or nanosecond resolution.

The sockets library is of course great for the communication, and I've
seen some python libraries that do packet sniffing and record
timestamps, but it's not clear that I can do both at the same time.


Have you tried it?  I don't know of any reason that using sockets and  
doing a packet capture would interfere with each other.  What are you  
trying to accomplish with the packet sniffing, though?


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


Re: weak reference to bound method

2009-10-02 Thread Miles Kaufmann

On Oct 2, 2009, at 1:54 AM, Ole Streicher wrote:

I am trying to use a weak reference to a bound method:

class MyClass(object):
   def myfunc(self):
   pass

o = MyClass()
print o.myfunc
 bound method MyClass.myfunc of __main__.MyClass object at  
0xc675d0


import weakref
r = weakref.ref(o.myfunc)
print r()

 None


This is what I do not understand. The object o is still alive, and
therefore the bound method o.myfunc shall exists.


Like Peter said, bound methods are created on demand when they are  
obtained from the instance, not when the instance is created.


Why does the weak reference claim that it is removed? And how can I  
hold

the reference to the method until the object is removed?


You could combine unbound methods with a weakref to the object:

r = weakref.ref(o)
MyClass.myfunc(r())

You could also create a wrapper object that holds a weak reference to  
the instance and creates a bound method on demand:


class WeakMethod(object):
def __init__(self, bound_method):
self.im_func = bound_method.im_func
self.im_self = weakref.ref(bound_method.im_self)
self.im_class = bound_method.im_class

def __call__(self):
obj = self.im_self()
if obj is None: return None
return types.MethodType(self.im_func, obj, self.im_class)
# could alternately act like a callableproxy

-Miles

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


Re: Are min() and max() thread-safe?

2009-09-17 Thread Miles Kaufmann

On Sep 16, 2009, at 10:39 PM, Steven D'Aprano wrote:


On Wed, 16 Sep 2009 22:08:40 -0700, Miles Kaufmann wrote:


On Sep 16, 2009, at 9:33 PM, Steven D'Aprano wrote:
I have two threads, one running min() and the other running max()  
over

the same list. I'm getting some mysterious results which I'm having
trouble debugging. Are min() and max() thread-safe, or am I doing
something fundamentally silly by having them walk over the same list
simultaneously?



min() and max() don't release the GIL, so yes, they are safe, and
shouldn't see a list in an inconsistent state (with regard to the  
Python
interpreter, but not necessarily to your application).  But a  
threaded
approach is somewhat silly, since the GIL ensures that they *won't*  
walk
over the same list simultaneously (two separate lists, for that  
matter).


Perhaps that's true for list contents which are built-ins like ints,  
but

with custom objects, I can demonstrate that the two threads operate
simultaneously at least sometimes. Unless I'm misinterpreting what I'm
seeing.


Whoops, sorry.  Yes, if you use Python functions (or C functions that  
release the GIL) for the object comparison methods, a custom key  
function, or the sequence iterator's methods, then the the min()/max()  
calls could overlap between threads.  If you have additional threads  
that could modify the list, you should synchronize access to it; if  
any of the earlier-mentioned functions modify the list, you're likely  
to get mysterious (or at least potentially unexpected) results even  
in a single-threaded context.


On Sep 16, 2009, at 10:41 PM, Niklas Norrthon wrote:


For one time sequences like files and generators your code is broken
for obvious reasons.


s/sequence/iterable/

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


Re: Are min() and max() thread-safe?

2009-09-16 Thread Miles Kaufmann

On Sep 16, 2009, at 9:33 PM, Steven D'Aprano wrote:

I have two threads, one running min() and the other running max() over
the same list. I'm getting some mysterious results which I'm having
trouble debugging. Are min() and max() thread-safe, or am I doing
something fundamentally silly by having them walk over the same list
simultaneously?


See for yourself: 
http://svn.python.org/view/python/trunk/Python/bltinmodule.c?view=markup

min() and max() don't release the GIL, so yes, they are safe, and  
shouldn't see a list in an inconsistent state (with regard to the  
Python interpreter, but not necessarily to your application).  But a  
threaded approach is somewhat silly, since the GIL ensures that they  
*won't* walk over the same list simultaneously (two separate lists,  
for that matter).


-Miles

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


Re: (A Possible Solution) Re: preferred way to set encoding for print

2009-09-16 Thread Miles Kaufmann

On Sep 16, 2009, at 12:39 PM, ~flow wrote:

so: how can i tell python, in a configuration or using a setting in
sitecustomize.py, or similar, to use utf-8 as a default encoding?




[snip Stdout_writer_with_ncrs solution]



This should work:

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,
  encoding=sys.stdout.encoding,
  errors='xmlcharrefreplace')

http://mail.python.org/pipermail/python-list/2009-August/725100.html

-Miles

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


Re: Why indentation is use to denote block of code?

2009-09-14 Thread Miles Kaufmann

On Sep 13, 2009, at 5:38 PM, AggieDan04 wrote:

On Sep 13, 6:27 pm, Steven D'Aprano wrote:

On Sun, 13 Sep 2009 15:15:40 -0700, Chris Rebert wrote:
In fact it's pretty much impossible to automatically indent Python  
code
that has had its indentation removed; it's impossible to know for  
sure

where the dedents should occur.


Just like most other syntactic elements -- if you remove all the  
return

statements from Python code, or dot operators, it's impossible to
automatically add them back in.

The only difference is that some (badly written?) applications mangle
leading whitespace, but very few feel free to remove other text on  
a whim.


I don't recall actually using a mail client or newsreader that  
removes
leading whitespace when posting, but I've occasionally seen posts  
from

others with all indentation removed, so presumably such badly-behaved
applications do exist.


I haven't seen it in a mail client, but it's very common in internet
forums.


If you regularly deal with some sort of transport that messes with  
your leading whitespace, you may find Tools/scripts/pindent.py in the  
Python source distribution useful; it adds comments that act as block  
closers to your code, and can then use those comments to restore the  
correct indentation to a mangled version.  (Most forums offer some  
sort of whitespace-preserving [code] tag, though; and pindent is  
relatively old, and apparently not well maintained (no support for  
with blocks)).


-Miles

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


Re: list as an instance attribute

2009-09-14 Thread Miles Kaufmann

On Sep 14, 2009, at 1:55 AM, Robin Becker wrote:

Bruno Desthuilliers wrote:

pep08 : class names should be Capitalized.
Also, if you're using Python 2.x, make it:
class Primitive(object):
  #...

...

I find it remarkable that the most primitive classes appear to break  
the pep08 convention eg object, str, list etc etc. In fact all such  
conventions appear to be broken more often than not. So the rule  
appears to be create a convention and then break it :)


More like break a convention and then create it. :)  Before Python  
2.2, built-in types were not classes at all; they couldn't be  
instantiated directly (from Python code), so you had to call the str()  
function to create an object of type string.  I think there may have  
been some discussion of renaming the built-ins to match PEP 8 for  
Python 3, but if so I doubt it got very far.


-Miles

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


[issue5468] urlencode does not handle bytes, and could easily handle alternate encodings

2009-08-31 Thread Miles Kaufmann

Changes by Miles Kaufmann mile...@umich.edu:


Removed file: http://bugs.python.org/file14796/urllib_parse.py3k.patch

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



Re: Why does this group have so much spam?

2009-08-30 Thread Miles Kaufmann

casebash walkr...@gmail.com wrote in message
news:7294bf8b-9819-4b6d-92b2- 
afc1c8042...@x6g2000prc.googlegroups.com...

So much of it could be removed even by simple keyword filtering.


Funny, I was just thinking recently about how *little* spam this list  
gets--on the other hand, I'm following it via the python-list@ mailing  
list.  The list owners do a great job of keeping the level of spam at  
a minimum, though there are occasional false positives (like your  
post, apparently, since I'm only seeing the replies).


-Miles

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


[issue5468] urlencode does not handle bytes, and could easily handle alternate encodings

2009-08-28 Thread Miles Kaufmann

Miles Kaufmann mile...@umich.edu added the comment:

I've attached a patch that provides similar functionality to Dan Mahn's 
urlencode(), as well as providing encoding and errors parameters to 
parse_qs and parse_qsl, updating the documentation to reflect the added 
parameters, and adding test cases.  The implementation of urlencode() is 
not the same as dmahn's, and has a more straightforward control flow and 
less code duplication than the current implementation.

(For the tests, I tried to match the style of the file I was adding to 
with regard to (expect, result) order, which is why it's inconsistent.)

--
keywords: +patch
versions: +Python 3.2
Added file: http://bugs.python.org/file14796/urllib_parse.py3k.patch

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



Re: Need help with Python scoping rules

2009-08-27 Thread Miles Kaufmann

On Aug 26, 2009, at 1:11 PM, kj wrote:

I think I understand the answers well enough.  What I *really*
don't understand is why this particular feature of Python (i.e.
that functions defined within a class statement are forbidden from
seeing other identifiers defined within the class statement) is
generally considered to be perfectly OK.  IMO it's a bizarre,
inexplicable blindspot (which, among other things, gives rise to
a certain worry about what other similar craziness lurks under
Python's image of rationality).  I have never seen even a half-hearted
justification, from a language design point of view, for why this
particular feature is worth having.


Guido's design justifications:
http://mail.python.org/pipermail/python-dev/2000-November/010598.html

--

My personal justification:

Python has used the same basic method of class creation since the very  
beginning: create a new local namespace, execute the class suite in  
that namespace, and then create a class, using the contents of the  
namespace as the class attributes.  The important thing to note here  
is that there are really *two* namespaces--the local namespace that  
exists while the class suite is being executed (what I call the suite  
namespace), and the namespace of the class itself--and the first  
ceases to exist when the second is created.  The two namespaces  
generally contain the same names at the point that the transfer  
occurs, but they don't have to; the metaclass (which constructs the  
class) is free to mess with the dictionary of attributes before  
creating the class.


Suppose for a moment that the suite namespace *were* visible to nested  
scopes.  The simplest and most consistent implementation would be to  
have a closure generated by a class statement be similar to that  
generated by a function--i.e., the closure would be over the suite  
namespace.  This hardly seems desirable, though, because the suite  
namespace and the class namespace would get out of sync when different  
objects were assigned to the class namespace:


class C:
  x = 1
  def foo(self):
  print x
  print self.x

 o = C()
 o.foo()
1
1
 o.x = 2
 o.foo()
1
2

Surely such an implementation would be considered an even larger  
Python wart then not having the suite namespace visible to nested  
scopes at all.  But it's better than the alternative of trying to  
unify the class suite namespace and the class namespace, which would  
be a nightmare of special cases (adding/deleting class attributes?  
descriptors? __getattr__?) and require an implementation completely  
separate from that of normal nested scopes.


-Miles

P.S. Just for fun:

import types

def make_class(*bases):
Decorator to allow you to (ab)use a function as a class definition.

The function must take no arguments and end with 'return locals()';
bases are (optionally) specified as arguments to make_class;
metaclasses other than 'type' are not supported.

 @make_class
... def C():
... greeting = 'Hello'
... target = 'world'
... def greet(self):
... print '%s, %s' % (self.greeting, target)
... return locals()
...
 C().greet()
Hello, world


def decorator(func):
  return type(func.func_name, bases, func())
if len(bases) == 1 and isinstance(bases[0], types.FunctionType):
  func = bases[0]
  bases = (object,)
  return decorator(func)
if not bases:
  bases = (object,)
return decorator

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


Re: Need help with Python scoping rules

2009-08-27 Thread Miles Kaufmann

On Aug 27, 2009, at 4:49 PM, kj wrote:


Miles Kaufmann mile...@umich.edu writes:

Guido's design justifications:
http://mail.python.org/pipermail/python-dev/2000-November/010598.html


Ah!  Clarity!  Thanks!  How did you find this?  Did you know of
this post already?  Or is there some special way to search Guido's
design justifications?


I just checked the python-dev archives around the time that PEP 227  
was written.



...because the suite
namespace and the class namespace would get out of sync when  
different

objects were assigned to the class namespace:



class C:
x = 1
def foo(self):
print x
print self.x



o = C()
o.foo()

1
1

o.x = 2
o.foo()

1
2


But this unfortunate situation is already possible, because one
can already define

class C:
 x = 1
 def foo(self):
 print C.x
 print self.x

which would lead to exactly the same thing.


You're right, of course.  If I had been thinking properly, I would  
have posted this:


... the suite namespace and the class namespace would get out of sync  
when different objects were assigned to the class namespace:


   # In a hypothetical Python with nested class suite scoping:
   class C:
   x = 1
   @classmethod
   def foo(cls):
   print x
   print cls.x

C.foo()
   1
   1
C.x = 2
C.foo()
   1
   2

With your example, the result is at least easily explainable: self.x  
is originally 1 because the object namespace inherits from the class  
namespace, but running 'o.x = 2' rebinds 'x' in the object namespace  
(without affecting the class namespace).  It's a distinction that  
sometimes trips up newbies (and me, apparently ;) ), but it's  
straightforward to comprehend once explained.  But the distinction  
between the class suite namespace and the class namespace is far more  
subtle; extending the lifetime of the first so that it still exists  
after the second is created is, IMO, asking for trouble (and trying to  
unify the two double so).


-Miles

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


Re: Waiting for a subprocess to exit

2009-08-21 Thread Miles Kaufmann

On Aug 20, 2009, at 10:13 PM, Ben Finney wrote:

The module documentation has a section on replacing ‘os.system’
http://docs.python.org/library/subprocess#replacing-os-system, which
says to use::

   process = subprocess.Popen(mycmd +  myarg, shell=True)
   status = os.waitpid(process.pid, 0)

But a ‘Popen’ instance has its own ‘wait’ method, which waits for exit
URL:http://docs.python.org/library/subprocess#subprocess.Popen.wait.
Why would I use ‘os.waitpid’ instead of::

   process = subprocess.Popen(mycmd +  myarg, shell=True)
   process.wait()
   status = process.returncode


Really, you can just use:

  process = subprocess.Popen(mycmd +  myarg, shell=True)
  status = process.wait()

I'm not sure why the documentation suggests using os.waitpid.

I would recommend avoiding shell=True whenever possible.  It's used in  
the examples, I suspect, to ease the transition from the functions  
being replaced, but all it takes is for a filename or some other input  
to unexpectedly contain whitespace or a metacharacter and your script  
will stop working--or worse, do damage (cf. the iTunes 2 installer  
debacle[1]).  Leaving shell=False makes scripts more secure and  
robust; besides, when I'm putting together a command and its  
arguments, it's as convenient to build a list (['mycmd', 'myarg']) as  
it is a string (if not more so).


-Miles

[1]: http://apple.slashdot.org/article.pl?sid=01/11/04/0412209#comment_2518563
--
http://mail.python.org/mailman/listinfo/python-list


Re: Object Reference question

2009-08-21 Thread Miles Kaufmann

On Aug 20, 2009, at 11:07 PM, josef wrote:


To begin, I'm new with python. I've read a few discussions about
object references and I think I understand them.

To be clear, Python uses a Pass By Object Reference model.
x = 1
x becomes the object reference, while an object is created with the
type 'int', value 1, and identifier (id(x)). Doing this with a class,
x = myclass(), does the same thing, but with more or less object
attributes. Every object has a type and an identifier (id()),
according to the Python Language Reference for 2.6.2 section 3.1.

x in both cases is the object reference. I would like to use the
object to refer to the object reference.


Stop right there.  'x' is not *the* object reference.  It is *an*  
object reference (or in my preferred terminology, a label).  Suppose  
you do:


x = myclass()
y = x

The labels 'x' and 'y' both refer to the same object with equal  
precedence.  There is no mapping from object back to label; it is a  
one-way pointer.  Also importantly, labels themselves are not objects,  
and cannot be accessed or referred to.


(This is a slight oversimplification; thanks to Python's reflection  
and introspection capabilities, it is possible to access labels to  
some extent, and in some limited situations it is possible to use  
stack inspection to obtain a label for an object.  But this is hackish  
and error-prone, and should never be used when a more Pythonic method  
is available.)



The following is what I would like to do:
I have a list of class instances dk = [ a, b, c, d ], where a, b, c, d
is an object reference. Entering dk gives me the object: [MyClass0
instance at 0x, MyClass1 instance at 0x0008, MyClass2 instance at
0x0010 ... ]

I need the object reference name (a,b,c,d) from dk to use as input for
a file.


It sounds like you should either be storing that name as an attribute  
of the object, or using a dictionary ({'a': a, 'b': b, ...}).


-Miles

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


Re: Skipping a superclass

2009-08-02 Thread Miles Kaufmann

On Aug 2, 2009, at 5:36 AM, Steven D'Aprano wrote:

I have a series of subclasses like this:

class A(object):
   def method(self, *args):
   print Lots of work gets done here in the base class

class B(A):
   def method(self, *args):
   print A little bit of work gets done in B
   super(B, self).method(*args)

class C(B):
   def method(self, *args):
   print A little bit of work gets done in C
   super(C, self).method(*args)


However, the work done in C.method() makes the work done in B.method()
obsolete: I want one to run, or the other, but not both. C does need  
to
inherit from B, for the sake of the other methods, so I want  
C.method()
*only* to skip B while still inheriting from A. (All other methods  
have

to inherit from B as normal.)


This might not be applicable to the larger problem you're trying to  
solve, but for this sample, I would write it as:


class A(object):
def method(self, *args):
self._method(*args)
print Lots of work gets done here in the base class
def _method(self, *args):
pass # or perhaps raise NotImplemented

class B(A):
def _method(self, *args):
print A little bit of work gets done in B

class C(B):
def _method(self, *args):
print A little bit of work gets done in C


So what I have done is change the call to super in C to super(B, self)
instead of super(C, self). It seems to work, but is this safe to do?  
Or

are there strange side-effects I haven't seen yet?


In a diamond-inheritance situation, you may end up skipping methods  
besides just B.method().


-Miles

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


Re: python 3 and stringio.seek

2009-07-29 Thread Miles Kaufmann


On Jul 28, 2009, at 6:30 AM, Michele Petrazzo wrote:


Hi list,
I'm trying to port a my library to python 3, but I have a problem  
with a

the stringio.seek:
the method not accept anymore a value like pos=-6 mode=1, but the  
old

(2.X) version yes...

The error:
 File /home/devel/Py3/lib/python3.0/io.py, line 2031, in seek
   return self._seek(pos, whence)
IOError: Can't do nonzero cur-relative seeks


How solve this?


In Python 2, StringIO is a stream of bytes (non-Unicode characters).   
In Python 3, StringIO is a stream of text (Unicode characters).  In  
the early development of Python 3 (and 3.1's _pyio), it was  
implemented as a TextIOWrapper over a BytesIO buffer.  TextIOWrapper  
does not support relative seeks because it is difficult to map the  
concept of a current position between bytes and the text that it  
encodes, especially with variable-width encodings and other  
considerations.  Furthermore, the value returned from  
TextIOWrapper.tell isn't just a file position but a cookie that  
contains other data necessary to restore the decoding mechanism to the  
same state.  However, for the default encoding (utf-8), the current  
position is equivalent to that of the underlying bytes buffer.


In Python 3, StringIO is implemented using an internal buffer of  
Unicode characters.  There is no technical reason why it can't support  
relative seeks; I assume it does not for compatibility with the  
original Python TextIOWrapper implementation (which is present in  
3.1's _pyio, but not in 3.0).


Note that because of the different implementations, StringIO.tell()  
(and seek) behaves differently for the C and Python implementations:


$ python3.1
 import io, _pyio
 s = io.StringIO('\u263A'); s.read(1), s.tell()
('☺', 1)
 s = _pyio.StringIO('\u263A'); s.read(1), s.tell()
('☺', 3)

The end result seems to be that, for text streams (including  
StreamIO), you *should* treat the value returned by tell() as an  
opaque magic cookie, and *only* pass values to seek() that you have  
obtained from a previous tell() call.  However, in practice, it  
appears that you *may* seek StringIO objects relatively by characters  
using s.seek(s.tell() + n), so long as you do not use the  
_pyio.StringIO implementation.


If what you actually want is a stream of bytes, use BytesIO, which may  
be seeked (sought?) however you please.


I'm basing this all on my reading of the Python source (and svn  
history), since it doesn't seem to be documented, so take it with a  
grain of salt.


-Miles

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


Re: trouble with minidom

2009-07-22 Thread Miles Kaufmann

On Jul 21, 2009, at 8:08 PM, Ronn Ross wrote:

Hello I'm trying to read an xml file using minidome. The xml looks  
like:

rootNode
   project
  namemyProj/name
  path/here//path
   /project
/rootNode

My code looks like so:
from xml.dom.minidom import parse

dom = parse(myfile.xml)

for node in dom.getElementsByTagName(project'):
   print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue,  
node.childNodes[1])


Unfortunately, it returns 'nodeValue as none. I'm trying to read the  
value out of the node fir example name: myProj. I haven't found much  
help in the documentation. Can someone point me in the right  
direction?


Two issues:

In your example XML file, the first child node of the project Element  
is the Text node containing the whitespace between the project and  
name tags.  node.childNodes[0] will select that whitespace node.


The nodeValue of an Element is null (None).  In order to get the text  
contents of an element, you must get the nodeValue of the Text child  
node of the Element.


Like Gabriel, I would recommend using an XML library with a more  
concise API than the W3C DOM (I like lxml.objectify).  But if you  
stick with xml.dom, use the specification as a reference:


http://www.w3.org/TR/REC-DOM-Level-1/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why not enforce four space indentations in version 3.x?

2009-07-16 Thread Miles Kaufmann

On Jul 15, 2009, at 4:26 PM, David Bolen wrote:


Miles Kaufmann mile...@umich.edu writes:


On Jul 14, 2009, at 5:06 PM, David Bolen wrote:
Are you sure?  It seems to restrict them in the same block, but  
not in

the entire file.  At least I was able to use both space and tab
indented blocks in the same file with Python 3.0 and 3.1.


It seems to me that, within an indented block, Python 3.1 requires
that you are consistent in your use of indentation characters *for
that indentation level*.  For example, the following code seems to be
allowed:


Um, right - in other words, what I said :-)


I wasn't trying to correct you, just being more explicit. :)  After  
reading your post, I still wasn't sure if the restriction on mixing  
spaces and tabs applied to nested blocks--I was surprised that the  
code sample I included was allowed.


-Miles

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


Re: missing 'xor' Boolean operator

2009-07-15 Thread Miles Kaufmann


On Jul 15, 2009, at 1:43 PM, Jean-Michel Pichavant wrote:


Hrvoje Niksic wrote:
[snip]

Note that in Python A or B is in fact not equivalent to not(not A and
not B).


 l = [(True, True), (True, False), (False, True), (False, False)]
 for p in l:
... p[0] or p[1]
[snip]
Did I make twice the same obvious error ?



Try again with:

l = [('foo','bar'), ('foo', ''), ('', 'bar'), ('', '')]

-Miles

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


Re: missing 'xor' Boolean operator

2009-07-15 Thread Miles Kaufmann

On Jul 15, 2009, at 1:55 PM, Emile van Sebille wrote:

On 7/15/2009 10:43 AM Jean-Michel Pichavant said...

Hrvoje Niksic wrote:
[snip]
Note that in Python A or B is in fact not equivalent to not(not A  
and

not B).


Did I make twice the same obvious error ?


No -- but in the not(not... example it doesn't short-circuit.


No; like 'A or B', 'not (not A and not B)' does in fact short-circuit  
if A is True.  (The 'and' condition does not have to evaluate the  
right operand when 'not A' is False.)


-Miles

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


Re: python first assignment of a global variable

2009-07-15 Thread Miles Kaufmann

On Jul 15, 2009, at 1:55 PM, Rodrigue wrote:


Basically, I was very surprised to discover that e() raises an
exception, but even more that e_raise() points to
if not MY_GLOBAL Is the problem not really when I assign?

My assumption is that some reordering is happening behind the scenes
that creates a situation similar to the += which assigns hence expects
to be at the local level.


The determination of whether a name is a reference to a local or  
global variable is made at compile time.  When a function contains a  
single assignment (or augmented assignment) to a name, the compiler  
generates bytecode such that all references to that name within the  
function will be looked up in the local scope only, including those  
before the assignment statement.


-Miles

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


Re: Why not enforce four space indentations in version 3.x?

2009-07-15 Thread Miles Kaufmann

On Jul 14, 2009, at 5:06 PM, David Bolen wrote:

Are you sure?  It seems to restrict them in the same block, but not in
the entire file.  At least I was able to use both space and tab
indented blocks in the same file with Python 3.0 and 3.1.


It seems to me that, within an indented block, Python 3.1 requires  
that you are consistent in your use of indentation characters *for  
that indentation level*.  For example, the following code seems to be  
allowed:


def foo():
TABif True:
TABSPSPx = 1
TABelse:
TABTABx = 2
TABreturn x

But replacing any of the first tabs on each line with 8 spaces  
(without replacing them all), which previously would have been  
allowed, is now an error.


-Miles

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


Re: missing 'xor' Boolean operator

2009-07-14 Thread Miles Kaufmann

On Jul 15, 2009, at 12:07 AM, Dr. Phillip M. Feldman wrote:

I appreciate the effort that people have made, but I'm not impressed  
with any
of the answers.  For one thing, xor should be able to accept an  
arbitrary

number of input arguments (not just two)


You originally proposed this in the context of the existing short- 
circuit boolean operators.  Those operators (being infix) take only  
two operands.



and should return True if and only
if the number of input arguments that evaluate to True is odd


The existing and/or operators always return the value of one of the  
operands--not necessarily True or False--which is another important  
property, but one that can't be translated fully to xor.  Given the  
lack of context in your original post, hopefully you'll forgive me  
being unimpressed by your not being impressed. :)



Here's my code:

def xor(*args):
  xor accepts an arbitrary number of input arguments, returning  
True
  if and only if bool() evaluates to True for an odd number of the  
input

  arguments.

  result= False

  for arg in args:
 if bool(arg): result= not result

  return result


If all you want is a True or False result, I'd write it like this:

import operator
def xor(*args):
return reduce(operator.xor, map(bool, args)) # or imap

In order to make it act more like the other logical operators, I'd use  
MRAB's 2-argument xor as the reducer--though I can't really see the  
utility.


def xor2(a, b):
return (not b and a) or (not a and b)

def xor(*args):
return reduce(xor2, args)

You may also find this question interesting:

http://stackoverflow.com/questions/432842/

-Miles

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


Re: Catching control-C

2009-07-09 Thread Miles Kaufmann

On Jul 9, 2009, at 9:20 AM, Lie Ryan wrote:


Michael Mossey wrote:

I want to understand better what the secret is to responding to a
ctrl-C in any shape or form.


Are you asking: when would the python interpreter process
KeyboardInterrupt?
...
In single threaded python program, the currently running thread is
always the main thread (which can handle KeyboardInterrupt). I believe
SIGINT is checked at every ticks. But SIGINT cannot interrupt atomic
operations (i.e. it cannot interrupt long operations that takes a  
single

tick).


Some otherwise atomic single-bytecode operations (like large integer  
arithmetic) do manual checks for whether signals were raised (though  
that won't help at all if the operation isn't on the main thread).



I believe a tick in python is equivalent to a single bytecode, but
please correct me if I'm wrong.


Not all opcodes qualify as a tick.  In general, those opcodes that  
cause control to remain in the eval loop (and not make calls to other  
Python or C functions) don't qualify as ticks (but there are  
exceptions, e.g. so that while True: pass is interruptible).  In  
Python/ceval.c: PyEval_EvalFrameEx(), those opcodes that don't end in  
goto fast_next_opcode are ticks.


Please correct me if _I'm_ wrong! :)
-Miles

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


Re: function local namespace question

2009-07-08 Thread Miles Kaufmann

On Jul 8, 2009, at 1:35 PM, Paul LaFollette wrote:


I cannot figure out any way to get a hook into the local namespace of
a user defined function.  I have tried making a wrapper class that
grabs the function call and then uses exec to invoke
myfunction.__code__ with my own dictionaries.  This runs the (no
argument) function properly (losing the return value, but I can deal
with that) but never accesses the local logging dictionary that I
specify in the exec() call. Perhaps the local namespace of a function
is not a dict at all?


Right.  Within functions, local variable name accesses and assignments  
are compiled into bytecode instructions (LOAD_FAST, STORE_FAST) that  
manipulate pointers to objects by indexing directly into a C array in  
the frame.  The locals dictionary of a frame is generated on-demand  
from that array when accessed.  There is no way that I'm aware of to  
directly hook into function namespace access and manipulation.


-Miles

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


Re: Passing parameters for a C program in Linux.

2009-06-30 Thread Miles Kaufmann


On Jun 30, 2009, at 6:46 AM, venutaurus...@gmail.com wrote:


  I have to write an automted script which will test my c
program. That program when run will ask for the commands.


Keep in mind that, if your test script checks the program's output  
before giving it input, you can run into problems with buffering.  The  
standard C library uses line-based buffering when a program is using a  
terminal for output, but when it's outputting to a pipe it uses block  
buffering.  This can be a problem when running a process using  
subprocess--your program will buffer the prompt, and your test script  
won't see it, so the test will deadlock.  The problem can also exist  
in the opposite direction.


Possible solutions:
- Explicitly set both your test script and your program to have line- 
buffered output.
- Add a flush statement whenever you finish writing output and expect  
input.
- Use pexpect, which uses a pseudo-tty and will make C stdio default  
to line buffering.
- Use pdpi's solution, which, since it doesn't wait for a prompt  
before supplying input, doesn't have this issue.

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


Re: Regular Expression Non Capturing Grouping Does Not Work.

2009-06-27 Thread Miles Kaufmann

On Jun 27, 2009, at 3:28 AM, Virtual Buddha wrote:


Hello all,

I am having some difficulties with the non-capturing grouping in
python regular expression module.

Even the code from the online documentation (http://docs.python.org/
howto/regex.html#non-capturing-and-named-groups) does not seem to
work.

...


Notice that you are calling .group() on the match object instead  
of .groups().  Without any arguments, .group() is equivalent  
to .group(0), which means return the entire matching string.


http://docs.python.org/library/re.html#re.MatchObject.group

-Miles

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


Re: No trees in the stdlib?

2009-06-27 Thread Miles Kaufmann

João Valverde wrote:
To answer the question of what I need the BSTs for, without getting  
into too many boring details it is to merge and sort IP blocklists,  
that is, large datasets of ranges in the form of (IP address, IP  
address, string). Originally I was also serializing them in a binary  
format (but no more after a redesign). I kept the merge and sort  
part as a helper script, but that is considerably simpler to  
implement.


...

As an anecdotal data point (honestly not trying to raise the Python  
is slow strawman), I implemented the same algorithm in C and  
Python, using pyavl. Round numbers were 4 mins vs 4 seconds, against  
Python (plus pyavl). Even considering I'm a worse Python programmer  
than C programmer, it's a lot. I know many will probably think I  
tried to do C in Python but that's not the case, at least I don' t  
think so. Anyway like I said, not really relevant to this discussion.


What format were you using to represent the IP addresses?  (Is it a  
Python class?)  And why wouldn't you use a network address/subnet mask  
pair to represent block ranges?  (It seems like being able to  
represent ranges that don't fit into a subnet's 2^n block wouldn't be  
that common of an occurrence, and that it might be more useful to make  
those ranges easier to manipulate.)


One of the major disadvantages of using a tree container is that  
usually multiple comparisons must be done for every tree operation.   
When that comparison involves a call into Python bytecode (for custom  
cmp/lt methods) the cost can be substantial.  Compare that to Python's  
hash-based containers, which only need to call comparison methods in  
the event of hash collisions (and that's hash collisions, not hash  
table bucket collisions, since the containers cache each object's hash  
value).  I would imagine that tree-based containers would only be  
worth using with objects with comparison methods implemented in C.


Not that I'm trying to be an apologist, or reject your arguments; I  
can definitely see the use case for a well-implemented, fast tree- 
based container for Python.  And so much the better if, when you need  
one, there was a clear consensus about what package to use (like PIL  
for image manipulation--it won't meet every need, and there are others  
out there, but it's usually the first recommended), rather than having  
to search out and evaluate a dozen different ones.


-Miles

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


Re: No trees in the stdlib?

2009-06-26 Thread Miles Kaufmann

On Jun 26, 2009, at 2:23 AM, Chris Rebert wrote:

On Thu, Jun 25, 2009 at 10:55 PM, João Valverdebacku...@netcabo.pt  
wrote:

Aahz wrote:


In article mailman.2139.1245994218.8015.python-l...@python.org,
Tom Reed  tomree...@gmail.com wrote:



Why no trees in the standard library, if not as a built in? I  
searched
the archive but couldn't find a relevant discussion. Seems like a  
glaring
omission considering the batteries included philosophy,  
particularly

balanced binary search trees. No interest, no good implementations,
something other reason? Seems like a good fit for the collections  
module.

Can anyone shed some light?



What do you want such a tree for?  Why are dicts and the bisect  
module
inadequate?  Note that there are plenty of different tree  
implementations

available from either PyPI or the Cookbook.



Simple example usage case: Insert string into data structure in  
sorted order

if it doesn't exist, else retrieve it.


That's pretty much the bisect module in a nutshell. It manipulates a
sorted list using binary search.


With O(n) insertions and removals, though.  A decent self-balancing  
binary tree will generally do those in O(log n).


-Miles

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


Re: urllib2.urlopen issue

2009-06-24 Thread Miles Kaufmann

On Jun 24, 2009, at 2:59 PM, David wrote:

On Jun 24, 11:27 am, Chris Rebert wrote:

On Wed, Jun 24, 2009 at 10:50 AM, David wrote:

hello,

I have a url that is http://query.directrdr.com/ptrack?
pid=225v_url=http://
www.plentyoffish.comkeyword=flowersfeed=1ip=12.2.2.2said=$said.
If I open it on a browser, I can get its contents without any
problem.
However, if I use following code,

import urllib2

url = 'http://query.directrdr.com/ptrack?pid=225v_url=http://
www.plentyoffish.comkeyword=flowersfeed=1ip=12.2.2.2said=$said'

xml = urllib2.urlopen(url).read()

then I get an exception of

 File /usr/lib/python2.5/urllib2.py, line 1082, in do_open
   raise URLError(err)
urllib2.URLError: urlopen error (-2, 'Name or service not known')


Unable to reproduce with either urllib or urllib2's urlopen(). I get
some XML back without error both ways. Using Python 2.6.2 on Mac OS  
X.




Thanks Aahz. And thanks Chris. The XML content is what I am looking
for. I use Python 2.5. Maybe I should update to 2.6.2? Python version
problem?


No, it also works for me on Python 2.5.1.  A wild guess: does this  
code work?


import socket
socket.gethostbyname(socket.gethostname())

If it throws a similar exception (Name or service not known), the root  
problem may be a misconfiguration in your /etc/hosts or /etc/ 
resolv.conf files.


-Miles

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


Re: [Mac] file copy

2009-06-23 Thread Miles Kaufmann

On Jun 23, 2009, at 9:52 AM, Tobias Weber wrote:


Hi,
which is the best way to copy files on OS X? I want to preserve  
resource

forks and extended attributes.

...

bin/cp -p


This.  cp -p, mv, rsync -E, tar, and other utilities will use the  
copyfile(3) API to preserve extended attributes, resource forks, and  
ACLs.  cp -Rp should be just as safe as a Finder copy--moreso if you  
run it as root--with the exception of preserving creation dates.  Or  
if you're worried about hard links, check out ditto(1).


You presumably already know this, but avoid shutil at all costs.

BackupBouncer (http://www.n8gray.org/code/backup-bouncer/) makes  
testing what gets preserved by various methods of copying quick and  
easy.


The results for a Finder copy:

Verifying:basic-permissions ... FAIL (Critical)
Verifying:   timestamps ... ok (Critical)
Verifying: symlinks ... ok (Critical)
Verifying:symlink-ownership ... FAIL
Verifying:hardlinks ... FAIL (Important)
Verifying:   resource-forks ...
   Sub-test: on files ... ok (Critical)
   Sub-test:  on hardlinked files ... FAIL (Important)
Verifying: finder-flags ... ok (Critical)
Verifying: finder-locks ... ok
Verifying:creation-date ... ok
Verifying:bsd-flags ... ok
Verifying:   extended-attrs ...
   Sub-test: on files ... ok (Important)
   Sub-test:   on directories ... ok (Important)
   Sub-test:  on symlinks ... ok
Verifying: access-control-lists ...
   Sub-test: on files ... ok (Important)
   Sub-test:  on dirs ... ok (Important)
Verifying: fifo ... FAIL
Verifying:  devices ... FAIL
Verifying:  combo-tests ...
   Sub-test:  xattrs + rsrc forks ... ok
   Sub-test: lots of metadata ... FAIL

sudo cp -Rp:

Verifying:basic-permissions ... ok (Critical)
Verifying:   timestamps ... ok (Critical)
Verifying: symlinks ... ok (Critical)
Verifying:symlink-ownership ... ok
Verifying:hardlinks ... FAIL (Important)
Verifying:   resource-forks ...
   Sub-test: on files ... ok (Critical)
   Sub-test:  on hardlinked files ... FAIL (Important)
Verifying: finder-flags ... ok (Critical)
Verifying: finder-locks ... ok
Verifying:creation-date ... FAIL
Verifying:bsd-flags ... ok
Verifying:   extended-attrs ...
   Sub-test: on files ... ok (Important)
   Sub-test:   on directories ... ok (Important)
   Sub-test:  on symlinks ... ok
Verifying: access-control-lists ...
   Sub-test: on files ... ok (Important)
   Sub-test:  on dirs ... ok (Important)
Verifying: fifo ... ok
Verifying:  devices ... ok
Verifying:  combo-tests ...
   Sub-test:  xattrs + rsrc forks ... ok
   Sub-test: lots of metadata ... ok

-Miles

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


Re: urllib2 content-type headers

2009-06-21 Thread Miles Kaufmann

On Jun 21, 2009, at 12:01 PM, TYR wrote:


Unfortunately, I'm getting nothing but 400 Bad Requests. I suspect
this is due to an unfeature of urllib2. Notably, although you can use
urllib2.Request's add_header method to append a header, the
documentation (http://docs.python.org/library/urllib2.html) says that:

remember that a few standard headers (Content-Length, Content-Type and
Host) are added when the Request is passed to urlopen() (or
OpenerDirector.open()).

And:

Note that there cannot be more than one header with the same name, and
later calls will overwrite previous calls in case the key collides.

To put it another way, you cannot rely on Content-Type being correct
because whatever you set it to explicitly, urllib2 will silently
change it to something else which may be wrong, and there is no way to
stop it. What happened to explicit is better than implicit?


Those headers are added (by AbstractHTTPHandler.do_request_) only if  
they are missing.


-Miles

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


Re: generator expression works in shell, NameError in script

2009-06-21 Thread Miles Kaufmann

On Jun 19, 2009, at 8:45 AM, Bruno Desthuilliers wrote:

 class Foo(object):
... bar = ['a', 'b', 'c']
... baaz = list((b, b) for b in bar)

but it indeed looks like using bar.index *in a generator expression*  
fails (at least in 2.5.2) :


 class Foo(object):
... bar = ['a', 'b', 'c']
... baaz = list((bar.index(b), b) for b in bar)
...
Traceback (most recent call last):
 File stdin, line 1, in module
 File stdin, line 3, in Foo
 File stdin, line 3, in genexpr
NameError: global name 'bar' is not defined


The reason that the first one works but the second fails is clearer if  
you translate each generator expression to the approximately  
equivalent generator function:


class Foo(object):
bar = ['a', 'b', 'c']
def _gen(_0):
for b in _0:
yield (b, b)
baaz = list(_gen(iter(bar))

# PEP 227: the name bindings that occur in the class block
# are not visible to enclosed functions
class Foo(object):
bar = ['a', 'b', 'c']
def _gen(_0):
for b in _0:
yield (bar.index(b), b)
baaz = list(_gen(iter(bar))

-Miles

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


Re: class or instance method

2009-06-21 Thread Miles Kaufmann

On Jun 21, 2009, at 5:23 PM, Scott David Daniels wrote:

Hrvoje Niksic wrote:

...
class class_or_instance(object):
   def __init__(self, fn):
   self.fn = fn
   def __get__(self, obj, cls):
   if obj is not None:
   return lambda *args, **kwds: self.fn(obj, *args, **kwds)
   else:
   return lambda *args, **kwds: self.fn(cls, *args, **kwds)
...


Just to polish a bit:

   import functools

   class ClassOrInstance(object):
   def __init__(self, fn):
   self._function = fn
   self._wrapper = functools.wraps(fn)

   def __get__(self, obj, cls):
   return self._wrapper(functools.partial(self._function,
  cls if obj is None else obj))




from types import MethodType

class ClassOrInstance(object):
def __init__(self, func):
self._func = func

def __get__(self, obj, cls):
return MethodType(self._func, cls if obj is None else obj, cls)


-Miles

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


[issue6234] cgi.FieldStorage is broken when given POST data

2009-06-15 Thread Miles Kaufmann

Changes by Miles Kaufmann mile...@umich.edu:


--
nosy: +milesck

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



[issue5468] urlencode does not handle bytes, and could easily handle alternate encodings

2009-06-15 Thread Miles Kaufmann

Miles Kaufmann mile...@umich.edu added the comment:

parse_qs and parse_qsl should also grow encoding and errors parameters to 
pass to the underlying unquote().

--
nosy: +milesck

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



Re: Function/method returning list of chars in string?

2009-06-09 Thread Miles Kaufmann

On Jun 9, 2009, at 6:05 AM, Diez B. Roggisch wrote:


Also as list-comps are going away and are replaced by

list(generator-expression)


Where did you hear that?

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


Re: random number including 1 - i.e. [0,1]

2009-06-09 Thread Miles Kaufmann

On Jun 9, 2009, at 7:05 PM, Mensanator wrote:


On Jun 9, 4:33 pm, Esmail wrote:

Hi,

random.random() will generate a random value in the range [0, 1).

Is there an easy way to generate random values in the range [0, 1]?
I.e., including 1?

I am implementing an algorithm and want to stay as true to the
original design specifications as possible though I suppose the
difference between the two max values might be minimal.


I'm curious what algorithm calls for random numbers on a closed  
interval.



ps: I'm confused by the docs for uniform():

random.uniform(a, b)
 Return a random floating point number N such that a = N = b  
for a = b


That's wrong. Where did you get it?


http://docs.python.org/library/random.html

-Miles

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


Re: Winter Madness - Passing Python objects as Strings

2009-06-06 Thread Miles Kaufmann

On Jun 4, 2009, at 3:25 AM, Hendrik van Rooyen wrote:


A can is like a pickle, in that it is a string, but anything
can be canned.
Unlike a pickle, a can cannot leave the process, though,
unless the object it points to lives in shared memory.

If you have any interest, contact me and I will
send you the source.


Sounds like di(), which can be written:

import _ctypes
di = _ctypes.PyObj_FromPtr

def can(o): return str(id(o))
def uncan(s): return di(int(s))

http://www.friday.com/bbum/2007/08/24/python-di/

-Miles

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