Re: How do I display unicode value stored in a string variable using ord()

2012-08-21 Thread Neil Hodgson

Steven D'Aprano:


Using variable-sized strings like UTF-8 and UTF-16 for in-memory
representations is a terrible idea because you can't assume that people
will only every want to index the first or last character. On average,
you need to scan half the string, one character at a time. In Big-Oh, we
can ignore the factor of 1/2 and just say we scan the string, O(N).


   In the majority of cases you can remove excessive scanning by 
caching the most recent index->offset result. If the next index request 
is nearer the cached index than to the beginning then iterate from that 
offset. This converts many operations from quadratic to linear. Locality 
of reference is common and can often be reasonably exploited.


   However, exposing the variable length nature of UTF-8 allows the 
application to choose efficient techniques for more cases.


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


Re: [ANNC] pybotwar-0.8

2012-08-21 Thread Jamie Paul Griffin
[ Ramchandra Apte wrote on Sat 18.Aug'12 at 19:21:03 +0530 ]

> I'll be using Google Groups (hopefully it won't top-post by default) to
> post stuff.

You are encouraged to get used to it i'm afraid as any mailing list you use, 
its users will prefer you to use the correct formatting of responses. 

You can change the settings in Gmail web interface or use an MUA and configure 
that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro 
 wrote:

Consider this code:




class SlowStorage(dict):
def __getattr__(self,key):
  return self[key]
def __setattr__(self,key):
  self[key]=value




class FastStorage(dict):
def __init__(self, __d__=None, **kwargs):
self.update(__d__,**kwargs)
def __getitem__(self,key):
return self.__dict__.get(key,None)
def __setitem__(self,key,value):
self.__dict__[key] = value
def __delitem__(self,key):
delattr(self,key)
def __copy__(self):
return Storage(self)
def __nonzero__(self):
return len(self.__dict__)>0
def pop(self,key,default=None):
if key in self:
default = getattr(self,key)
delattr(self,key)
return default
def clear(self):
self.__dict__.clear()
def __repr__(self):
return repr(self.__dict__)
def keys(self):
return self.__dict__.keys()
def values(self):
return self.__dict__.values()
def items(self):
return self.__dict__.items()
  def iterkeys(self):
return self.__dict__.iterkeys()
def itervalues(self):
return self.__dict__.itervalues()
def iteritems(self):
return self.__dict__.iteritems()
def viewkeys(self):
return self.__dict__.viewkeys()
def viewvalues(self):
return self.__dict__.viewvalues()
def viewitems(self):
return self.__dict__.viewitems()
def fromkeys(self,S,v=None):
return self.__dict__.fromkeys(S,v)
def setdefault(self, key, default=None):
try:
return getattr(self,key)
except AttributeError:
setattr(self,key,default)
return default
def clear(self):
self.__dict__.clear()
def len(self):
return len(self.__dict__)
def __iter__(self):
return self.__dict__.__iter__()
def has_key(self,key):
return key in self.__dict__
def __contains__(self,key):
return key in self.__dict__
def update(self,__d__=None,**kwargs):
if __d__:
for key in __d__:
kwargs[key] = __d__[key]
self.__dict__.update(**kwargs)
def get(self,key,default=None):
return getattr(self,key) if key in self else default




>>> s=SlowStorage()
>>> a.x=1  ### (1)
>>> a.x### (2)
1 # ok
>>> isinstance(a,dict)
True # ok
>>> print dict(a)
{'x':1} # ok (3)


Try:

a.items()


What does that show?





>>> s=FastStorage()
>>> a.x=1  ### (4)
>>> a.x### (5)
1 # ok
>>> isinstance(a,dict)
True # ok
>>> print dict(a)
{} # not ok (6)



Lines (4) and (5) are about 10x faster then lines (1) and (2). I 

like
FastStorage better but while (3) behaves ok, (6) does not behave as 

I

want.




I intuitively understand why FastStorage is cannot cast into dict
properly.




What I do not know is how to make it do the casting properly without
losing the 10x speedup of FastStorage over SlowStorage.




Any idea?


I don't really understand what your trying to do but since you didn't 
add the __setattr__ method to FastStorage the item is not added to 
the dictionary when you do a.x = 1


Oscar

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


Abuse of subject, was Re: Abuse of Big Oh notation

2012-08-21 Thread Peter Otten
[email protected] wrote:

> By chance and luckily, first attempt.
 
> c:\python32\python -m timeit "('€'*100+'€'*100).replace('€'
> , 'œ')"
> 100 loops, best of 3: 1.48 usec per loop
> c:\python33\python -m timeit "('€'*100+'€'*100).replace('€'
> , 'œ')"
> 10 loops, best of 3: 7.62 usec per loop

OK, that is roughly factor 5. Let's see what I get:

$ python3.2 -m timeit '("€"*100+"€"*100).replace("€", "œ")'
10 loops, best of 3: 1.8 usec per loop
$ python3.3 -m timeit '("€"*100+"€"*100).replace("€", "œ")'
1 loops, best of 3: 9.11 usec per loop

That is factor 5, too. So I can replicate your measurement on an AMD64 Linux 
system with self-built 3.3 versus system 3.2.

> Note
> The used characters are not members of the latin-1 coding
> scheme (btw an *unusable* coding).
> They are however charaters in cp1252 and mac-roman.

You seem to imply that the slowdown is connected to the inability of latin-1 
to encode "œ" and "€" (to take the examples relevant to the above 
microbench). So let's repeat with latin-1 characters:

$ python3.2 -m timeit '("ä"*100+"ä"*100).replace("ä", "ß")'
10 loops, best of 3: 1.76 usec per loop
$ python3.3 -m timeit '("ä"*100+"ä"*100).replace("ä", "ß")'
1 loops, best of 3: 10.3 usec per loop

Hm, the slowdown is even a tad bigger. So we can safely dismiss your theory 
that an unfortunate choice of the 8 bit encoding is causing it. Do you 
agree?

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


Re: Top-posting &c. (was Re: [ANNC] pybotwar-0.8)

2012-08-21 Thread Steven D'Aprano
On Tue, 21 Aug 2012 08:07:33 +0200, Alex Strickland wrote:

> On 2012/08/17 12:42 AM, Madison May wrote:
> 
>> As a lurker, I agree completely with Chris's sentiments.
> 
> I too, but I'd prefer something top-posted than have to skip through 38
> pages of quoted e-mail to get to a (generally) 1 liner at the bottom.

+1000

People with Hotmail accounts used to be known as "Metoobees" because of 
their tendency to reply to long posts with a single line at the very end, 
"Me too!".


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


Re: Does Polymorphism mean python can create object?

2012-08-21 Thread Chris Angelico
On Tue, Aug 21, 2012 at 10:20 AM, alex23  wrote:
> On Tue, Aug 21, 2012 at 12:01 AM, Levi Nie wrote:
>> Does Polymorphism mean python can create object?
>
> No. This isn't D&D. Polymorphism has a distinct meaning in computer science, 
> one which you would've found in less time searching Wikipedia than asking 
> this question here.

That said, though, I wouldn't be against someone casting Baleful
Polymorph on the odd troll...

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


unittest - sort cases to be run

2012-08-21 Thread Kevin Zhang
Hi all,

I want to sort the order of the unittest cases to be run, but found such
statement in Python doc,
"Note that the order in which the various test cases will be run is
determined by sorting the test function names with respect to the built-in
ordering for strings."

s.addTest(BTest())
s.addTest(ATest())
TextTestRunner().run(ts)

I need BTest() to be run prior to ATest(), is there any natural/beautiful
way to achieve this? Thanks,
-- 
http://mail.python.org/mailman/listinfo/python-list


[email protected]

2012-08-21 Thread Ulrich Eckhardt

Am 21.08.2012 00:49, schrieb Prasad, Ramit:

I also tend to blame M$ (Outlook and variants) for this tendency to
quote everything and top-post -- Outlook makes it almost impossible
to do a trim&interleave response style.


I [think] that Outlook & Co are guilty. That and the fact that few
people even think about this.


Nonsense, I post only from Outlook. You can do it and it is not hard.
It is just requires a little effort.


A good tool would reduce the effort and guide users, like e.g. giving 
them a hint if they leave the whole mail they're replying to as copy. 
Several corporate email solutions (like MS Outlook/Exchange) put very 
little emphasis on communication efficiency but only on eye-candy 
features. Their popularity and the resulting influence on people has 
caused decay in average communication culture, and that is what I blame 
them for.



BTW: You omitted the attribution line for the text you quoted, whom do 
you blame for that? That said, "Nonsense" is a strong enough word to 
start a flamewar... not nice.


;^)

Uli

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


Re: unittest - sort cases to be run

2012-08-21 Thread Terry Reedy

On 8/21/2012 5:09 AM, Kevin Zhang wrote:

Hi all,

I want to sort the order of the unittest cases to be run, but found such
statement in Python doc,
"Note that the order in which the various test cases will be run is
determined by sorting the test function names with respect to the
built-in ordering for strings."

 s.addTest(BTest())
 s.addTest(ATest())
 TextTestRunner().run(ts)

I need BTest() to be run prior to ATest(), is there any
natural/beautiful way to achieve this? Thanks,


Rename it @BTest.

--
Terry Jan Reedy

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


Re: Abuse of Big Oh notation

2012-08-21 Thread Steven D'Aprano
On Mon, 20 Aug 2012 11:12:22 -0600, Ian Kelly wrote:

> On Mon, Aug 20, 2012 at 10:09 AM, Chris Angelico 
> wrote:
>> On Tue, Aug 21, 2012 at 2:01 AM, Paul Rubin 
>> wrote:
>>> Analogy: how big a box is required to hold a pair of shoes?  In a
>>> purely theoretical sense we might say O(S) where S is the shoe size,
>>> treating shoe size as an arbitrary independent variable.  But in the
>>> real world, shoe size is controlled by the size of human feet, which
>>> is bounded by a constant for biological reasons.  You don't have to
>>> consider shoes the size of Jupiter.  So it is O(1).
>>
>> By that argument, everything is amortized O(1), because there's a limit
>> on every variable. You can't possibly be working with a data set
>> greater than the total sum of storage space in the entire world. That
>> still doesn't mean that bubble sort and heap sort are equivalently
>> efficient.
> 
> The difference between the two is that the former is bounded by a
> constant that is fundamental to the algorithm at hand, whereas the
> latter is bounded only by available resources.  By any practical
> consideration the latter must be considered a variable, but the former
> need not be.
> 
> Paul discusses above the asymptotic growth of a variable as O(S) where S
> is shoe size, but really this measure makes no sense in the first place.
>  The question that this notation seeks to answer is, "What is the big-Oh
> behaviour of this variable as shoe size increases indefinitely (i.e. to
> infinity)?" and the answer in this case is "Shoe size does not increase
> indefinitely"; the question is invalid.
> 
> A more logical question might be, "How much material do I need to
> construct N shoes of size S?"  The answer to this question would
> presumably be some constant factor of N * S**2, which is O(N * S**2).
> Although N can be assumed to vary freely (up to nonsensical quantities
> like the mass of the entire universe), S is clearly bounded by the
> constraints of actual shoes, so we can safely treat S as a constant and
> call it O(N).

Why the hell are we talking about shoe sizes?

Let's not lose sight of the actual question in hand: how expensive is it 
to fetch a character at some arbitrary index in a string?

With fixed-width characters, average time is O(1), best-case time is O(1) 
and worst-case time is O(1).

With non-fixed width characters, average time and worst-case time are 
both O(N), and best-case ("give me the character at index 0") is O(1). 
But that's a degenerate case that gives us absolutely no insight into the 
cost of the operation. It's like the observation that "sorting a list of 
one item takes constant time, regardless of the algorithm". Um, yes, it 
does. So what?

Invented constraints like "people only care about indexes really close to 
zero, or to the end of the string", effectively confuse the best possible 
case for the average case. In real life, people do care about random 
access to strings and the average case is more important than the best 
case. That's why strings are typically implemented as arrays of fixed-
width characters rather than linked lists, and Haskell (which doesn't) 
explicitly warns you that they don't and that your string-handling code 
is likely to be slow.

Of course, you can swap space and complexity for time, e.g. ropes, or use 
cached pointers to character locations in the hope that indexes will be 
clustered rather than scattered randomly. These more complex 
implementations typically use as much memory than, and performance is 
often no better than, a dead simple implementation that uses a simple 
array of fixed width characters. Most harmful, it means that a simple 
indexing operation may be fast on average, and occasionally degenerate to 
slow. The only thing worse than "This program is slow" is "This program 
is *occasionally* slow, and I can't predict when".

Not surprising, almost all programming languages in common usage use 
arrays of fixed-width characters as their native string type. Python 3.3 
will now do the same thing, with the memory optimization that the fixed-
width will be per string and selected from 1, 2, or 4 bytes according to 
the minimum width needed, rather than choosing between 2 and 4 bytes when 
you build the Python compiler.

This is a big positive step forward with a pretty simple algorithm and 
will strongly help encourage the use of Unicode by taking much of the 
sting out of the perennial complaints that "Unicode wastes space". Not so 
wasteful any more.



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


Re: color coding for numbers

2012-08-21 Thread Ulrich Eckhardt

Am 21.08.2012 10:38, schrieb [email protected]:

what is the best way


Define "best" before asking such questions. ;)



using color/shading on a tkinter canvas as a visualization for a
two-dimensional grid of numbers? so far my best idea is to use the
same value for R,G and B (fill = '#xyxyxy'), which gives shades of
gray. if possible i'd like to have a larger number of visually
distinct values.


The basic idea behind this is that you first normalize the values to a 
value between zero and one and then use that to look up an according 
color in an array. Of course you can also do both in one step or compute 
the colors in the array on the fly (like you did), but it helps keeping 
things simple at least for a start, and it also allows testing different 
approaches separately.


If the different number of resulting colors isn't good enough then, it 
could be that the array is too small (its size determines the maximum 
number of different colours), that the normalization only uses a small 
range between zero and one (reducing the effectively used number of 
colours) or simply that your screen doesn't support that many different 
colors.



> i've seen visualizations that seem to use some kind
> of hot-versus-cold color coding. does anybody know how to do this?

The colour-coding is just the way that above mentioned array is filled. 
For the hot/cold coding, you could define a dark blue for low values and 
a bright red for high values and then simply interpolate the RGB triple 
for values in between.


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


Re: New internal string format in 3.3

2012-08-21 Thread Roy Smith
In article ,
 Michael Torrie  wrote:

> > And if you want the "fudge it somehow" behavior (which is often very 
> > useful!), there's always http://pypi.python.org/pypi/Unidecode/
> 
> Sweet tip, thanks!  I often want to process text that has smart quotes,
> emdashes, etc, and convert them to plain old ascii quotes, dashes,
> ticks, etc.  This will do that for me without resorting to a bunch of
> regexes.  Bravo.

Yup, that's one of the things it's good for.  We mostly use it to help 
map search terms, i.e. if you search for "beyonce", you're probably 
expecting it to match "Beyoncé".

We also special-case some weird stuff like "kesha" matching "ke$ha", but 
we have to hand-code those.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does Polymorphism mean python can create object?

2012-08-21 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Tue, Aug 21, 2012 at 10:20 AM, alex23  wrote:
> > On Tue, Aug 21, 2012 at 12:01 AM, Levi Nie wrote:
> >> Does Polymorphism mean python can create object?
> >
> > No. This isn't D&D. Polymorphism has a distinct meaning in computer 
> > science, one which you would've found in less time searching Wikipedia than 
> > asking this question here.
> 
> That said, though, I wouldn't be against someone casting Baleful
> Polymorph on the odd troll...

Won't work.  Trolls always make their random.saving_throw().
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python 6 compilation failure on RHEL

2012-08-21 Thread Jerry Hill
On Tue, Aug 21, 2012 at 12:34 AM, John Nagle  wrote:
> After a thread of clueless replies, it's clear that nobody
> responding actually read the build log.  Here's the problem:

The very first reply, Emile's, pointed out that these were optional
modules, and that python did, in fact build successfully.

>   Failed to find the necessary bits to build these modules:
> bsddb185
> dl
> imageop
> sunaudiodev
>
> What's wrong is that the Python 2.6 build script is looking for
> some antiquated packages that aren't in a current RHEL.  Those
> need to be turned off.

They don't need to be turned off.  They can either be ignored (because
they aren't needed, and did not cause the build to fail), or the
development libraries for those pieces can be installed and python
recompiled.

The rest of the thread has been commenting on the OP's choice of
python version and operating system.  That's not exactly on topic, but
the original question was answered in the very first response.

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


Re: Class.__class__ magic trick help

2012-08-21 Thread Massimo Di Pierro
On Aug 21, 2:40 am, Oscar Benjamin  wrote:
> On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro
>
>
>
>
>
>
>
>
>
>  wrote:
> > Consider this code:
> > class SlowStorage(dict):
> >     def __getattr__(self,key):
> >           return self[key]
> >     def __setattr__(self,key):
> >           self[key]=value
> > class FastStorage(dict):
> >     def __init__(self, __d__=None, **kwargs):
> >         self.update(__d__,**kwargs)
> >     def __getitem__(self,key):
> >         return self.__dict__.get(key,None)
> >     def __setitem__(self,key,value):
> >         self.__dict__[key] = value
> >     def __delitem__(self,key):
> >         delattr(self,key)
> >     def __copy__(self):
> >         return Storage(self)
> >     def __nonzero__(self):
> >         return len(self.__dict__)>0
> >     def pop(self,key,default=None):
> >         if key in self:
> >             default = getattr(self,key)
> >             delattr(self,key)
> >         return default
> >     def clear(self):
> >         self.__dict__.clear()
> >     def __repr__(self):
> >         return repr(self.__dict__)
> >     def keys(self):
> >         return self.__dict__.keys()
> >     def values(self):
> >         return self.__dict__.values()
> >     def items(self):
> >         return self.__dict__.items()
> >       def iterkeys(self):
> >         return self.__dict__.iterkeys()
> >     def itervalues(self):
> >         return self.__dict__.itervalues()
> >     def iteritems(self):
> >         return self.__dict__.iteritems()
> >     def viewkeys(self):
> >         return self.__dict__.viewkeys()
> >     def viewvalues(self):
> >         return self.__dict__.viewvalues()
> >     def viewitems(self):
> >         return self.__dict__.viewitems()
> >     def fromkeys(self,S,v=None):
> >         return self.__dict__.fromkeys(S,v)
> >     def setdefault(self, key, default=None):
> >         try:
> >             return getattr(self,key)
> >         except AttributeError:
> >             setattr(self,key,default)
> >             return default
> >     def clear(self):
> >         self.__dict__.clear()
> >     def len(self):
> >         return len(self.__dict__)
> >     def __iter__(self):
> >         return self.__dict__.__iter__()
> >     def has_key(self,key):
> >         return key in self.__dict__
> >     def __contains__(self,key):
> >         return key in self.__dict__
> >     def update(self,__d__=None,**kwargs):
> >         if __d__:
> >             for key in __d__:
> >                 kwargs[key] = __d__[key]
> >         self.__dict__.update(**kwargs)
> >     def get(self,key,default=None):
> >         return getattr(self,key) if key in self else default
> > >>> s=SlowStorage()
> > >>> a.x=1  ### (1)
> > >>> a.x    ### (2)
> > 1 # ok
> > >>> isinstance(a,dict)
> > True # ok
> > >>> print dict(a)
> > {'x':1} # ok (3)
>
> Try:
>
> >>> a.items()
>
> What does that show?
>
>
>
>
>
>
>
>
>
>
>
> > >>> s=FastStorage()
> > >>> a.x=1  ### (4)
> > >>> a.x    ### (5)
> > 1 # ok
> > >>> isinstance(a,dict)
> > True # ok
> > >>> print dict(a)
> > {} # not ok (6)
> > Lines (4) and (5) are about 10x faster then lines (1) and (2). I
> like
> > FastStorage better but while (3) behaves ok, (6) does not behave as
> I
> > want.
> > I intuitively understand why FastStorage is cannot cast into dict
> > properly.
> > What I do not know is how to make it do the casting properly without
> > losing the 10x speedup of FastStorage over SlowStorage.
> > Any idea?
>
> I don't really understand what your trying to do but since you didn't
> add the __setattr__ method to FastStorage the item is not added to
> the dictionary when you do a.x = 1
>
> Oscar

>>> a.items()
[('x',1')]

all the APIs work as expected except casting to dict.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On 21 August 2012 13:52, Massimo Di Pierro wrote:

> On Aug 21, 2:40 am, Oscar Benjamin  wrote:
> > On Mon, 20 Aug 2012 21:17:15 -0700 (PDT), Massimo Di Pierro
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >  wrote:
> > > Consider this code:
> > > class SlowStorage(dict):
> > > def __getattr__(self,key):
> > >   return self[key]
> > > def __setattr__(self,key):
> > >   self[key]=value
> > > class FastStorage(dict):
> > > def __init__(self, __d__=None, **kwargs):
> > > self.update(__d__,**kwargs)
> > > def __getitem__(self,key):
> > > return self.__dict__.get(key,None)
> > > def __setitem__(self,key,value):
> > > self.__dict__[key] = value
> > > def __delitem__(self,key):
> > > delattr(self,key)
> > > def __copy__(self):
> > > return Storage(self)
> > > def __nonzero__(self):
> > > return len(self.__dict__)>0
> > > def pop(self,key,default=None):
> > > if key in self:
> > > default = getattr(self,key)
> > > delattr(self,key)
> > > return default
> > > def clear(self):
> > > self.__dict__.clear()
> > > def __repr__(self):
> > > return repr(self.__dict__)
> > > def keys(self):
> > > return self.__dict__.keys()
> > > def values(self):
> > > return self.__dict__.values()
> > > def items(self):
> > > return self.__dict__.items()
> > >   def iterkeys(self):
> > > return self.__dict__.iterkeys()
> > > def itervalues(self):
> > > return self.__dict__.itervalues()
> > > def iteritems(self):
> > > return self.__dict__.iteritems()
> > > def viewkeys(self):
> > > return self.__dict__.viewkeys()
> > > def viewvalues(self):
> > > return self.__dict__.viewvalues()
> > > def viewitems(self):
> > > return self.__dict__.viewitems()
> > > def fromkeys(self,S,v=None):
> > > return self.__dict__.fromkeys(S,v)
> > > def setdefault(self, key, default=None):
> > > try:
> > > return getattr(self,key)
> > > except AttributeError:
> > > setattr(self,key,default)
> > > return default
> > > def clear(self):
> > > self.__dict__.clear()
> > > def len(self):
> > > return len(self.__dict__)
> > > def __iter__(self):
> > > return self.__dict__.__iter__()
> > > def has_key(self,key):
> > > return key in self.__dict__
> > > def __contains__(self,key):
> > > return key in self.__dict__
> > > def update(self,__d__=None,**kwargs):
> > > if __d__:
> > > for key in __d__:
> > > kwargs[key] = __d__[key]
> > > self.__dict__.update(**kwargs)
> > > def get(self,key,default=None):
> > > return getattr(self,key) if key in self else default
> > > >>> s=SlowStorage()
> > > >>> a.x=1  ### (1)
> > > >>> a.x### (2)
> > > 1 # ok
> > > >>> isinstance(a,dict)
> > > True # ok
> > > >>> print dict(a)
> > > {'x':1} # ok (3)
> >
> > Try:
> >
> > >>> a.items()
> >
> > What does that show?
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > >>> s=FastStorage()
> > > >>> a.x=1  ### (4)
> > > >>> a.x### (5)
> > > 1 # ok
> > > >>> isinstance(a,dict)
> > > True # ok
> > > >>> print dict(a)
> > > {} # not ok (6)
> > > Lines (4) and (5) are about 10x faster then lines (1) and (2). I
> > like
> > > FastStorage better but while (3) behaves ok, (6) does not behave as
> > I
> > > want.
> > > I intuitively understand why FastStorage is cannot cast into dict
> > > properly.
> > > What I do not know is how to make it do the casting properly without
> > > losing the 10x speedup of FastStorage over SlowStorage.
> > > Any idea?
> >
> > I don't really understand what your trying to do but since you didn't
> > add the __setattr__ method to FastStorage the item is not added to
> > the dictionary when you do a.x = 1
> >
> > Oscar
>
> >>> a.items()
> [('x',1')]
>
> all the APIs work as expected except casting to dict.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Sorry, I see what you're doing now. Because you've subclassed dict the dict
constructor is not using any of the python methods you have defined to
create a new dict. It is copying directly from the builtin instance that
your instance is wrapping, but you haven't actually passed your values on
to the superclass so it hasn't stored them in the builtin data structure.

Either subclass object so that your methods are called, or do something
like:

def __setitem__(self,key,value):
self.__dict__[key] = value
dict.__setitem__(self, key, value)

I still don't see the point of this but that should work.

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


Re: remote read eval print loop

2012-08-21 Thread Eric Frederich
This isn't really for users.  It is for developers like me.
Yes it is a security hole but again, it is a debugger.

The people who will be using it can all ssh into the server machine with
the same ID that the server process is running on.
In fact, this is quite normal.

As it is right now, we log into these machines and start an interactive
Python and use the bindings to debug things.
This works well most of the time but when you do that you need to start
another session of the application.
It is useful to run code interactively from within an actual client session
where something has gone wrong.

In any case I got this working with a rudimentary SWT Java client
(yuck, but the application is based on Eclipse).

Below is the code I used.  It has a singleton interactive console object.
I sub-classed it and defined another method "process" which simply calls
the "push" method after wrapping stdout and stderr.
It returns anything that was printed to stdout, stderr, and the return
value of the "push" method.

So now from the client I can process one line at a time and it behaves much
like the real interactive console... you wouldn't even realize there is all
this client / server / WSDL / xml / https junk going on.

 BEGIN CODE

import sys
from code import InteractiveConsole

class MyBuffer(object):
def __init__(self):
self.buffer = []
def write(self, data):
self.buffer.append(data)
def get(self):
ret = ''.join(self.buffer)
self.buffer = []
return ret

class MyInteractiveConsole(InteractiveConsole):

def __init__(self, *args, **kwargs):
InteractiveConsole.__init__(self, *args, **kwargs)
self.mb_out = MyBuffer()
self.mb_err = MyBuffer()

def process(self, s):
sys.stdout, sys.stderr = self.mb_out, self.mb_err
more = self.push(s)
sys.stdout, sys.stderr = sys.__stdout__, sys.__stderr__
return self.mb_out.get(), self.mb_err.get(), more

print 'creating new interactive console'
mic = MyInteractiveConsole()


On Fri, Aug 17, 2012 at 10:06 AM, Chris Angelico  wrote:

> On Fri, Aug 17, 2012 at 11:28 PM, Eric Frederich
>  wrote:
> > Within the debugging console, after importing all of the bindings, there
> > would be no reason to import anything whatsoever.
> > With just the bindings I created and the Python language we could do
> > meaningful debugging.
> > So if I block the ability to do any imports and calls to eval I should be
> > safe right?
>
> Nope. Python isn't a secured language in that way. I tried the same
> sort of thing a while back, but found it effectively impossible. (And
> this after people told me "It's not possible, don't bother trying". I
> tried anyway. It wasn't possible.)
>
> If you really want to do that, consider it equivalent to putting an
> open SSH session into your debugging console. Would you give that much
> power to your application's users? And if you would, is it worth
> reinventing SSH?
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class.__class__ magic trick help

2012-08-21 Thread Massimo Di Pierro
Hello Oscar,

thanks for your help but your proposal of adding:

def __setitem__(self,key,value):
   self.__dict__[key] = value
   dict.__setitem__(self, key, value)

does not help me.

What I have today is a class that works like SlowStorage. I want to
replace it with NewStorage because it is 10x faster. That is the only
reason. NewStorage does everything I want and all the APIs work like
SlowStorage except casting to dict.

By defining __setitem__ as you propose, you solve the casting to dict
issue but you have two unwanted effects: each key,value is store twice
(in different places), accessing the elements becomes slower the
SlowStprage which is my problem in the first place.

The issue for me is understanding how the casting dict(obj) works and
how to change its behavior so that is uses methods exposed by obj to
do the casting, if all possible.

Massimo

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


Re: Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On 21 August 2012 14:50, Massimo Di Pierro wrote:

> Hello Oscar,
>
> thanks for your help but your proposal of adding:
>
> def __setitem__(self,key,value):
>self.__dict__[key] = value
>dict.__setitem__(self, key, value)
>
> does not help me.
>
> What I have today is a class that works like SlowStorage. I want to
> replace it with NewStorage because it is 10x faster. That is the only
> reason. NewStorage does everything I want and all the APIs work like
> SlowStorage except casting to dict.
>
> By defining __setitem__ as you propose, you solve the casting to dict
> issue but you have two unwanted effects: each key,value is store twice
> (in different places), accessing the elements becomes slower the
> SlowStprage which is my problem in the first place.
>
> The issue for me is understanding how the casting dict(obj) works and
> how to change its behavior so that is uses methods exposed by obj to
> do the casting, if all possible.
>

Then you have two options:
1) subclass object instead of dict - you're not using any of the features
of the dict superclass and the fact that it is a superclass is confusing
the dict() constructor.
2) use a different "cast" e.g. d = dict(a.items())

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


Re: Class.__class__ magic trick help

2012-08-21 Thread Massimo DiPierro
Thanks again Oscar. I cannot do that. I have tight constraints. I am not at 
liberty to modify the code that uses the class. The exposed API cannot change 
including a.x, dict(a), is isinstance(a,dict).

My goal it so change the definition of this class to make it faster.

Where is in the Python source code is the casting to dict defined? Where can I 
try understand what it does?

Massimo


On Aug 21, 2012, at 9:14 AM, Oscar Benjamin wrote:

> On 21 August 2012 14:50, Massimo Di Pierro  wrote:
> Hello Oscar,
> 
> thanks for your help but your proposal of adding:
> 
> def __setitem__(self,key,value):
>self.__dict__[key] = value
>dict.__setitem__(self, key, value)
> 
> does not help me.
> 
> What I have today is a class that works like SlowStorage. I want to
> replace it with NewStorage because it is 10x faster. That is the only
> reason. NewStorage does everything I want and all the APIs work like
> SlowStorage except casting to dict.
> 
> By defining __setitem__ as you propose, you solve the casting to dict
> issue but you have two unwanted effects: each key,value is store twice
> (in different places), accessing the elements becomes slower the
> SlowStprage which is my problem in the first place.
> 
> The issue for me is understanding how the casting dict(obj) works and
> how to change its behavior so that is uses methods exposed by obj to
> do the casting, if all possible.
> 
> Then you have two options:
> 1) subclass object instead of dict - you're not using any of the features of 
> the dict superclass and the fact that it is a superclass is confusing the 
> dict() constructor.
> 2) use a different "cast" e.g. d = dict(a.items())
> 
> Oscar

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


Class.__class__ magic trick help

2012-08-21 Thread Oscar Benjamin
On 21 August 2012 16:19, Oscar Benjamin wrote:

>
> On Aug 21, 2012 3:42 PM, "Massimo DiPierro" 
> wrote:
> >
> > Thanks again Oscar. I cannot do that. I have tight constraints. I am not
> at liberty to modify the code that uses the class. The exposed API cannot
> change including a.x, dict(a), is isinstance(a,dict).
> >
> > My goal it so change the definition of this class to make it faster.
> >
> > Where is in the Python source code is the casting to dict defined? Where
> can I try understand what it does?
>
> help(dict)
>
> There is no cast, there is only the dict constructor. If the dict
> constructor finds a dict instance (including from a subclass) then it will
> efficiently create the new dict from the old dict's underlying data without
> calling your methods. If you want dict(a) and isinstance(dict) to work them
> you need to tell the dict superclass to store the data using
> dict.__setitem__.
>
> You have three options:
> 1) use SlowStorage
> 2) duplicate the data in self and self.__dict__ (this will probably end up
> slowing down FastStorage)
> 3) change the requirement to isinstance(Mapping)
>
> Oscar
>
Okay, there is a way to solve your problem:

>>> class Storage(dict):
... def __init__(self, *args, **kwargs):
... dict.__init__(self, *args, **kwargs)
... self.__dict__ = self
...
>>> s = Storage()
>>> s
{}
>>> s.x = 1
>>> s
{'x': 1}
>>> dict(s)
{'x': 1}
>>> isinstance(s, dict)
True

But it's a stupid idea unless you know that the keys of your dict will
*never* have any of the following values:

>>> dir({})
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items',
'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem',
'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']

To see what goes wrong:

>>> s['items'] = [1,2,3]
>>> s.items()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'list' object is not callable

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


Re: [ANNC] pybotwar-0.8

2012-08-21 Thread Ramchandra Apte
On Tuesday, 21 August 2012 12:41:39 UTC+5:30, Jamie Paul Griffin  wrote:
> [ Ramchandra Apte wrote on Sat 18.Aug'12 at 19:21:03 +0530 ]
> 
> 
> 
> > I'll be using Google Groups (hopefully it won't top-post by default) to
> 
> > post stuff.
> 
> 
> 
> You are encouraged to get used to it i'm afraid as any mailing list you use, 
> its users will prefer you to use the correct formatting of responses. 
> 
> 
> 
> You can change the settings in Gmail web interface or use an MUA and 
> configure that.

Enough of OT.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert base 10 to base 2?

2012-08-21 Thread Miki Tebeka
> You get the binary by doing bin(x), where x is an integer.
Note that Python also support binary number literals (prefixed with 0b):
In [1]: 0b101
Out[1]: 5
-- 
http://mail.python.org/mailman/listinfo/python-list


Why does dynamic doc string do not work?

2012-08-21 Thread Miki Tebeka
Greetings,

>>> class A:
... '''a doc string'''
... 
>>> A.__doc__
'a doc string'
>>> class B:
... '''a {} string'''.format('doc')
... 
>>> B.__doc__
>>> 

Is there's a reason for this?

I know I can do:
>>> class B:
...__doc__ = '''a {} string'''.format('doc')

And it'll work, but I wonder why the first B docstring is empty.

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


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-21 Thread Guillaume Comte
Unfortunatly, my_socket.bind((src_addr, 1)) doesn't work. I get the error 
message: "socket.error: [Errno 49] Can't assign requested address"...

I've tried to change the protocol to IPPROTO_RAW. Here is a simple example of 
the code:



import socket
import os
import struct
import time
import select

ICMP_ECHO_REQUEST = 8
PACKET_SIZE = 64 # Bytes
TIMEOUT = 0.5 # Seconds

def do_one(src_addr, dest_addr):
my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, 
socket.IPPROTO_RAW)

if src_addr != None:
src_addr = socket.gethostbyname(src_addr)
my_socket.bind((src_addr, 1))

my_id = os.getpid() & 0x

print "id: " + str(my_id)

print "sending..."
send_one(dest_addr, my_socket, my_id)

print "receiving..."
id = receive_one(my_socket)
if id == None:
print "nothing received !"
else:
print "received id: " + str(id)

my_socket.close()

def checksum(source_string):
...

def send_one(addr, my_socket, id):
# Header: type (8), code (8), checksum (16), id (16), sequence number (16)
cs = 0
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, cs, socket.htons(id), 0)
data = PACKET_SIZE * "G"

cs = checksum(header + data)

header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(cs), 
socket.htons(id), 0)
packet = header + data

my_socket.sendto(packet, (socket.gethostbyname(addr), 1))

def receive_one(my_socket):
while True:
what_ready = select.select([my_socket], [], [], TIMEOUT)
if what_ready[0] == []:
return None

received_packet = my_socket.recvfrom(1024)[0]
header = received_packet[20:28]
id = struct.unpack("bbHHh", header)[3]
return socket.ntohs(id)

if __name__ == "__main__":

import sys
dst = sys.argv[1]
print "dst: " + dst
try:
src = sys.argv[2]
print "src: " + src
except IndexError:
src = None
do_one(src, dst)


But when I try to set a source address, I still get the same error message...

Does anyone know how I could build the IP header (I don't even know if it can 
be the solution but it's worth a try...)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: color coding for numbers

2012-08-21 Thread DJC

On 21/08/12 12:55, Ulrich Eckhardt wrote:

Am 21.08.2012 10:38, schrieb [email protected]:

what is the best way


Define "best" before asking such questions. ;)




matplotlib.colors

A module for converting numbers or color arguments to RGB or RGBA

RGB and RGBA are sequences of, respectively, 3 or 4 floats in the range 0-1.

This module includes functions and classes for color specification 
conversions, and for mapping numbers to colors in a 1-D array of colors 
called a colormap.


see








using color/shading on a tkinter canvas as a visualization for a
two-dimensional grid of numbers? so far my best idea is to use the
same value for R,G and B (fill = '#xyxyxy'), which gives shades of
gray. if possible i'd like to have a larger number of visually
distinct values.


The basic idea behind this is that you first normalize the values to a
value between zero and one and then use that to look up an according
color in an array. Of course you can also do both in one step or compute
the colors in the array on the fly (like you did), but it helps keeping
things simple at least for a start, and it also allows testing different
approaches separately.

If the different number of resulting colors isn't good enough then, it
could be that the array is too small (its size determines the maximum
number of different colours), that the normalization only uses a small
range between zero and one (reducing the effectively used number of
colours) or simply that your screen doesn't support that many different
colors.


 > i've seen visualizations that seem to use some kind
 > of hot-versus-cold color coding. does anybody know how to do this?

The colour-coding is just the way that above mentioned array is filled.
For the hot/cold coding, you could define a dark blue for low values and
a bright red for high values and then simply interpolate the RGB triple
for values in between.

Uli


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


Re: Why does dynamic doc string do not work?

2012-08-21 Thread Ian Kelly
On Tue, Aug 21, 2012 at 10:44 AM, Miki Tebeka  wrote:
> Greetings,
>
 class A:
> ... '''a doc string'''
> ...
 A.__doc__
> 'a doc string'
 class B:
> ... '''a {} string'''.format('doc')
> ...
 B.__doc__

>
> Is there's a reason for this?
>
> I know I can do:
 class B:
> ...__doc__ = '''a {} string'''.format('doc')
>
> And it'll work, but I wonder why the first B docstring is empty.

The docstring is built at compile-time, not at run-time, so it must be
a literal, not an arbitrary expression.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Abuse of subject, was Re: Abuse of Big Oh notation

2012-08-21 Thread wxjmfauth
Le mardi 21 août 2012 09:52:09 UTC+2, Peter Otten a écrit :
> [email protected] wrote:
> 
> 
> 
> > By chance and luckily, first attempt.
> 
>  
> 
> > c:\python32\python -m timeit "('€'*100+'€'*100).replace('€'
> 
> > , 'œ')"
> 
> > 100 loops, best of 3: 1.48 usec per loop
> 
> > c:\python33\python -m timeit "('€'*100+'€'*100).replace('€'
> 
> > , 'œ')"
> 
> > 10 loops, best of 3: 7.62 usec per loop
> 
> 
> 
> OK, that is roughly factor 5. Let's see what I get:
> 
> 
> 
> $ python3.2 -m timeit '("€"*100+"€"*100).replace("€", "œ")'
> 
> 10 loops, best of 3: 1.8 usec per loop
> 
> $ python3.3 -m timeit '("€"*100+"€"*100).replace("€", "œ")'
> 
> 1 loops, best of 3: 9.11 usec per loop
> 
> 
> 
> That is factor 5, too. So I can replicate your measurement on an AMD64 Linux 
> 
> system with self-built 3.3 versus system 3.2.
> 
> 
> 
> > Note
> 
> > The used characters are not members of the latin-1 coding
> 
> > scheme (btw an *unusable* coding).
> 
> > They are however charaters in cp1252 and mac-roman.
> 
> 
> 
> You seem to imply that the slowdown is connected to the inability of latin-1 
> 
> to encode "œ" and "€" (to take the examples relevant to the above 
> 
> microbench). So let's repeat with latin-1 characters:
> 
> 
> 
> $ python3.2 -m timeit '("ä"*100+"ä"*100).replace("ä", "ß")'
> 
> 10 loops, best of 3: 1.76 usec per loop
> 
> $ python3.3 -m timeit '("ä"*100+"ä"*100).replace("ä", "ß")'
> 
> 1 loops, best of 3: 10.3 usec per loop
> 
> 
> 
> Hm, the slowdown is even a tad bigger. So we can safely dismiss your theory 
> 
> that an unfortunate choice of the 8 bit encoding is causing it. Do you 
> 
> agree?

- I do not care too much about the numbers. It's
an attempt to show the principles.

- The fact, considering latin-1 as a bad coding,
lies on the point that is is simply unsuable
for some scripts / languages. It has mainly to do
with source/text files coding. This is not really
the point here.

- Now, the technical aspect. This "coding" (latin-1)
may be considered somehow as the pseudo-coding covering
the unicode code points range 128..255. Unfortunatelly,
this "coding" is not very optimal (or can be see as) when
you work with a full range of Unicode, but is is fine
when one works only in pure latin-1, with only 256
characters.
This range 128..255 is always the critical part
(all codings considered). And probably represents
the most used characters.

I hope, it was not too confused.

I have no proof for my theory. With my experience on that
field, I highly suspect this as the bottleneck.

Some os as before.

Py 3.2.3
>>> timeit.repeat("('€'*100+'€'*100).replace('€', 'œ')")
[1.5384088242603358, 1.532421642233382, 1.5327445924545433]
>>> timeit.repeat("('ä'*100+'ä'*100).replace('ä', 'ß')")
[1.561762063667686, 1.5443503206462594, 1.5458670051605168]


3.3.0b2
>>> timeit.repeat("('€'*100+'€'*100).replace('€', 'œ')")
[7.701523104134512, 7.720358191179441, 7.614549852683501]>>> 
>>> timeit.repeat("('ä'*100+'ä'*100).replace('ä', 'ß')")
[4.887939423990709, 4.868787294350611, 4.865697999795991]

Quite mysterious!

In any way it is a regression.

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


Re: Why does dynamic doc string do not work?

2012-08-21 Thread MRAB

On 21/08/2012 17:44, Miki Tebeka wrote:

Greetings,


class A:

... '''a doc string'''
...

A.__doc__

'a doc string'

class B:

... '''a {} string'''.format('doc')
...

B.__doc__



Is there's a reason for this?

I know I can do:

class B:

...__doc__ = '''a {} string'''.format('doc')

And it'll work, but I wonder why the first B docstring is empty.


I think what's happening is that it's being parsed and then checked to
see whether it's a string literal.

This:

"doc string"

is OK, as is this:

"doc" "string"

(implied concatenation) and this:

   ("doc string")

but this isn't:

"doc" + " string"

because its syntax is:

ADD(STRING_LITERAL, STRING_LITERAL)

In other words, it's looking at the syntax, not the resulting value.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does dynamic doc string do not work?

2012-08-21 Thread Ian Kelly
On Tue, Aug 21, 2012 at 11:09 AM, Ian Kelly  wrote:
> On Tue, Aug 21, 2012 at 10:44 AM, Miki Tebeka  wrote:
>> Greetings,
>>
> class A:
>> ... '''a doc string'''
>> ...
> A.__doc__
>> 'a doc string'
> class B:
>> ... '''a {} string'''.format('doc')
>> ...
> B.__doc__
>
>>
>> Is there's a reason for this?
>>
>> I know I can do:
> class B:
>> ...__doc__ = '''a {} string'''.format('doc')
>>
>> And it'll work, but I wonder why the first B docstring is empty.
>
> The docstring is built at compile-time, not at run-time, so it must be
> a literal, not an arbitrary expression.

Also, if it did allow arbitrary expressions, then the syntax would be ambiguous.

def a():
foo()
do_stuff()

Is "foo()" intended to return a doc string?  If so, then it should be
called when the function object is built, not when the function is
called.  On the other hand, maybe it's intended to be part of the
function's code, in which case it should be called only when the
function itself is called.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unittest - sort cases to be run

2012-08-21 Thread goon12
On Tuesday, August 21, 2012 5:34:33 AM UTC-4, Terry Reedy wrote:
> On 8/21/2012 5:09 AM, Kevin Zhang wrote:
> 
> > Hi all,
> 
> >
> 
> > I want to sort the order of the unittest cases to be run, but found such
> 
> > statement in Python doc,
> 
> > "Note that the order in which the various test cases will be run is
> 
> > determined by sorting the test function names with respect to the
> 
> > built-in ordering for strings."
> 
> >
> 
> >  s.addTest(BTest())
> 
> >  s.addTest(ATest())
> 
> >  TextTestRunner().run(ts)
> 
> >
> 
> > I need BTest() to be run prior to ATest(), is there any
> 
> > natural/beautiful way to achieve this? Thanks,

If BTest *has* to run prior to ATest, it could be a code smell.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does dynamic doc string do not work?

2012-08-21 Thread Steven D'Aprano
On Tue, 21 Aug 2012 09:44:10 -0700, Miki Tebeka wrote:

> Greetings,
> 
 class A:
> ... '''a doc string'''
> ...
 A.__doc__
> 'a doc string'
 class B:
> ... '''a {} string'''.format('doc') ...
 B.__doc__
 
 
> Is there's a reason for this?

Yes. Python only generates docstrings automatically from string literals, 
not arbitrary expressions.

Arbitrary expressions are evaluated and then garbage collected, so:

class B:
'''a {} string'''.format('doc')

is equivalent to:

class B:
__doc__ = None
_tmp = '''a {} string'''.format('doc')
del _tmp

except that the name "_tmp" doesn't actually get used.



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


Re: Why does dynamic doc string do not work?

2012-08-21 Thread Dave Angel
On 08/21/2012 12:44 PM, Miki Tebeka wrote:
> 
>>> class B:
> ... '''a {} string'''.format('doc')
> ... 
 B.__doc__

>   I wonder why the first B docstring is empty. Thanks, -- Miki

According to some early documentation:

"convenient initialization of the |__doc__| attribute of modules,
classes and functions by placing a string literal by itself as the first
statement in the suite. It must be a literal -- an expression yielding a
string object is not accepted as a documentation string, since future
tools may need to derive documentation from source by parsing."

-- 

DaveA

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


Re: Why does dynamic doc string do not work?

2012-08-21 Thread Miki Tebeka
Thanks!

> On 08/21/2012 12:44 PM, Miki Tebeka wrote:
> 
> > 
> 
> >>> class B:
> 
> > ... '''a {} string'''.format('doc')
> 
> > ... 
> 
>  B.__doc__
> 
> 
> 
> >   I wonder why the first B docstring is empty. Thanks, -- Miki
> 
> 
> 
> According to some early documentation:
> 
> 
> 
> "convenient initialization of the |__doc__| attribute of modules,
> 
> classes and functions by placing a string literal by itself as the first
> 
> statement in the suite. It must be a literal -- an expression yielding a
> 
> string object is not accepted as a documentation string, since future
> 
> tools may need to derive documentation from source by parsing."
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

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


Re: unittest - sort cases to be run

2012-08-21 Thread Peter Otten
Kevin Zhang wrote:

> I want to sort the order of the unittest cases to be run, but found such
> statement in Python doc,
> "Note that the order in which the various test cases will be run is
> determined by sorting the test function names with respect to the built-in
> ordering for strings."
> 
> s.addTest(BTest())
> s.addTest(ATest())
> TextTestRunner().run(ts)
> 
> I need BTest() to be run prior to ATest(), is there any natural/beautiful
> way to achieve this? Thanks,

Did you try the above? I think BTest *will* run before ATest. The sorting is 
performed by the TestLoader if there is one, i. e. if you don't build a test 
suite manually. If you *do* use a TestLoader you can still influence the 
sort order by defining a sortTestMethodsUsing static method. Here's a fairly 
complex example:

[Ordering tests in a testsuite]
http://mail.python.org/pipermail/python-list/2010-October/589058.html

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


Re: Why doesn't Python remember the initial directory?

2012-08-21 Thread John Roth
On Sunday, August 19, 2012 7:57:46 PM UTC-6, kj wrote:

> This means that no library code can ever count on, for example,
> 
> being able to reliably find the path to the file that contains the
> 
> definition of __main__. 

If you want to find that, look up __main__ in sys.modules and then look at the 
__file__ attribute. You need to be a bit careful with this; the import 
machinery was rewritten for the upcoming 3.3 release, and there were several 
changes to the module information.

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


Reimporting modules, and sandboxing?

2012-08-21 Thread Dan Stromberg
I know I've seen this discussed before, and I came away from observing the
discussion thinking "Python doesn't do that very well...", but we have some
people here who really would like to do this, and I need to better
understand the pros and cons now.

Is there a good way of reimporting an _independent_ set of CPython modules?

I'm aware of these issues:
http://stackoverflow.com/questions/1254370/reimport-a-module-in-python-while-interactive
http://docs.python.org/library/functions.html#reload

Are there 
more?

Does sandboxing Python code help in some way?  If yes, then what kind of
sandbox?  Note that this isn't sandboxing to prevent execution of malicious
code, it's sandboxing to preserve reimportability, which may be an easier
constraint to satisfy.  The modules are friendly to each other, they aren't
trying to break into each other, we just have to be careful that they don't
step on each other's ability to reimport accidentally.

Although I love Pypy, I don't think we'll be able to use Pypy sandboxes
this time around.

Also, if we need to reload 3 modules at once, can reload() reimport them
atomically, as a set?  Or would it need to do them one at a time?

In short, we're talking about having some code that allows automatically
re-importing changed modules.  These modules will be pretty independent of
each other in the Python world, but might interact with each other in the
REST world even though they're written in CPython.

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


Re: protobuf + pypy

2012-08-21 Thread Mark Lawrence

On 21/08/2012 22:55, Pedro Larroy wrote:

Hi

Anyone knows if it's possible to use protobuffers with pypy?   Seems
there isn't much info on the web about this.

Pedro.



Did you mean this, in which case there loads on the web 
http://code.google.com/p/protobuf/ ?


--
Cheers.

Mark Lawrence.

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


Re: Books?

2012-08-21 Thread Roy Smith
In article 
<5203ee16-5a80-4cd9-9434-ee2efb645...@kg10g2000pbc.googlegroups.com>,
 Anonymous Group  wrote:

> What books do you recomend for learning python? Preferably free and/or
> online.

I would start with http://docs.python.org/tutorial/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Books?

2012-08-21 Thread Chris Angelico
On Wed, Aug 22, 2012 at 11:39 AM, Roy Smith  wrote:
> In article
> <5203ee16-5a80-4cd9-9434-ee2efb645...@kg10g2000pbc.googlegroups.com>,
>  Anonymous Group  wrote:
>
>> What books do you recomend for learning python? Preferably free and/or
>> online.
>
> I would start with http://docs.python.org/tutorial/

Agreed. And for anything beyond that, I would recommend the anonymous
OP offer some more information, such as current programming skill
level and languages known.

But the tutorial is a good start regardless.

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


Re: Books?

2012-08-21 Thread Anonymous Group
On Aug 21, 7:04 pm, Chris Angelico  wrote:
> On Wed, Aug 22, 2012 at 11:39 AM, Roy Smith  wrote:
> > In article
> > <5203ee16-5a80-4cd9-9434-ee2efb645...@kg10g2000pbc.googlegroups.com>,
> >  Anonymous Group  wrote:
>
> >> What books do you recomend for learning python? Preferably free and/or
> >> online.
>
> > I would start withhttp://docs.python.org/tutorial/
>
> Agreed. And for anything beyond that, I would recommend the anonymous
> OP offer some more information, such as current programming skill
> level and languages known.
>
> But the tutorial is a good start regardless.
>
> ChrisA

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


Re: something about split()???

2012-08-21 Thread mingqiang hu
why filter is bad when use lambda ?actually I think I can use lambda like
this: filter(lambda x:x==None,"|",split("|"))

On Wed, Aug 15, 2012 at 1:33 PM, Ramchandra Apte wrote:

> filter is bad when you use lambda with it
> there are (good) cases for filter
>
>
> On 14 August 2012 22:39, Jean-Michel Pichavant wrote:
>
>> Ramchandra Apte wrote:
>>
>>> (Much) more Pythonic solution:
>>> >>> filter(None,"|".split("|"))
>>>
>>> On 14 August 2012 15:14, Andreas Tawn >> andreas.tawn@ubisoft.**com >> wrote:
>>>
>>> > I have a question about the split function? surpose a = "|",and
>>> when I use a.split("|") , I got the list
>>> > ['"",""] ,but I want to get the empty list,what should I do ?
>>>
>>> Something like...
>>>
>>> >>> [x for x in "|".split("|") if x]
>>> []
>>>
>>> Cheers,
>>>
>>> Drea
>>> --
>>> 
>>> http://mail.python.org/**mailman/listinfo/python-list
>>>
>>>
>>>  A pythonic answer would be bottom-posted :p
>>
>> JM
>>
>>
>> PS : pylint raises a low warning about *filter* being non pythonic,
>> http://pylint-messages.**wikidot.com/messages:w0141
>> "les goûts et les couleurs ne se discutent pas"
>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asking

2012-08-21 Thread Ian Foote

On 22/08/12 03:57, mingqiang hu wrote:
can I use just one statement to figure out if substring “a” ,"b" "c" 
are in string "adfbdfc" ? not use the statement like


("a" in "adfbdfc") or ( "b" in "adfbdfc") or ("c" in "adfbdfc" ) 
,because if I have lots of substring, this could sucks


This might not be the most efficient way, but:

>>> set("abc") <= set("adfbdfc")
True
>>> set("abce") <= set("adfbdfc")
False

If you want to check for substrings longer than one character, this 
won't work. A solution then is to define a custom function:


def all_in(string, substrings):
for substring in substrings:
if substring not in string:
return False
return True

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


Re: asking

2012-08-21 Thread Ian Foote

Oops, hopefully this with indent correctly:

def all_in(string, substrings):
for substring in substrings:
if substring not in string:
return False
return True
--
http://mail.python.org/mailman/listinfo/python-list


Re: asking

2012-08-21 Thread Dave Angel
On 08/22/2012 12:17 AM, Ian Foote wrote:
> Oops, hopefully this with indent correctly:
>
> def all_in(string, substrings):
> for substring in substrings:
> if substring not in string:
> return False
> return True

The POP's question was ambiguous (did he want to match any of the
substrings, or all of the substrings), but his example code:
   

("a" in "adfbdfc")  or ( "b" in "adfbdfc") or ("c" in "adfbdfc" )

implements the opposite sense of what you have.  So perhaps he'd want:


def any_in(string, substrings):
for substring in substrings:
if substring in string:
  return True:
return False


-- 

DaveA

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


Re: asking

2012-08-21 Thread Terry Reedy

On 8/21/2012 10:57 PM, mingqiang hu wrote:

  can I use just one statement to figure out if  substring “a” ,"b" "c"
are in  string "adfbdfc" ?  not use the statement like

("a" in "adfbdfc")  or ( "b" in "adfbdfc") or ("c" in "adfbdfc" )
,because if I have lots of substring, this could sucks


>>> import re
# single chars
>>> print(re.search('[abc]', 'defgh'))
None
>>> print(re.search('[abc]', 'defgha'))
<_sre.SRE_Match object at 0x03251098>
# multichar strings
>>> print(re.search('ab|ha]', 'defgha'))
None
>>> print(re.search('ab|ha', 'defgha'))
<_sre.SRE_Match object at 0x03251098>

--
Terry Jan Reedy


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


Re: something about split()???

2012-08-21 Thread Terry Reedy

On 8/21/2012 11:43 PM, mingqiang hu wrote:

why filter is bad when use lambda ?


Inefficient, not 'bad'. Because the equivalent comprehension or 
generator expression does not require a function call.


--
Terry Jan Reedy

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


Re: asking

2012-08-21 Thread alex23
On 22/08/12 03:57, mingqiang hu wrote:
> can I use just one statement to figure out if substring “a” ,"b" "c"
> are in string "adfbdfc" ? not use the statement like
> ("a" in "adfbdfc") or ( "b" in "adfbdfc") or ("c" in "adfbdfc" )
> ,because if I have lots of substring, this could sucks

subs = ['a', 'b', 'c']
string = 'abcdef'

def all_in(string, substrings):
return all(map(string.__contains__, subs))
-- 
http://mail.python.org/mailman/listinfo/python-list


help me debug my "word capitalizer" script

2012-08-21 Thread Santosh Kumar
Here is the script I am using:

from os import linesep
from string import punctuation
from sys import argv

script, givenfile = argv

with open(givenfile) as file:
# List to store the capitalised lines.
lines = []
for line in file:
# Split words by spaces.
words = line.split(' ')
for i, word in enumerate(words):
if len(word.strip(punctuation)) > 3:
# Capitalise and replace words longer than 3 (without
punctuation)
words[i] = word.capitalize()
# Join the capitalised words with spaces.
lines.append(' '.join(words))
# Join the capitalised lines by the line separator
capitalised = linesep.join(lines)
# Optionally, write the capitalised words back to the file.

print(capitalised)


Purpose of the script:
To capitalize the first letter of any word in a given file, leaving
words which have 3 or less letters.

Bugs:
I know it has many bugs or/and it can be improved by cutting down the
code, but my current focus is to fix this bug:
  1. When I pass it any file, it does it stuff but inserts a blank
line everytime it processes a new line. (Please notice that I don't
want the output in an another file, I want it on screen).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Top-posting &c. (was Re: [ANNC] pybotwar-0.8)

2012-08-21 Thread Bob Martin
in 679182 20120821 181439 Dennis Lee Bieber  wrote:
>On Tue, 21 Aug 2012 08:07:33 +0200, Alex Strickland 
>declaimed the following in gmane.comp.python.general:
>
>> On 2012/08/17 12:42 AM, Madison May wrote:
>>
>> > As a lurker, I agree completely with Chris's sentiments.
>>
>> I too, but I'd prefer something top-posted than have to skip through 38
>> pages of quoted e-mail to get to a (generally) 1 liner at the bottom.
>
>Doesn't help me though... Agent shows quoted material as blue, fresh
>text as black.
>
>I tend to not see a one-liner at the top (since it is next to the
>attribution line) and if the rest of the page is all blue text I hit
>page down... and down, down, down... looking for black text... Then end
>up going "Wha', where's the new stuff?" and having to scroll back up to
>find a one-liner cuddling up with the attribution line.

Yep, and the only solution is for everyone to top-post.
-- 
http://mail.python.org/mailman/listinfo/python-list