Re: Bitwise Operations

2013-07-30 Thread Ulrich Eckhardt

Am 30.07.2013 01:34, schrieb Devyn Collier Johnson:

Typing "101 & 010" or "x = (int(101, 2) & int(010, 2))" only gives errors.


What errors? Check out Eric Raymond's essay on asking smart questions, 
it's a real eye-opener! ;)


That said, use "0b" as prefix for binary number literals (0b1000 is 
eight, for example).


Cheers!

Uli


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


Modeling life on Earth –- an object-oriented (Python?) challenge

2013-07-30 Thread quartese
Dear List,

I have to start this email by saying that I have recently attended EuroPython 
in Florence, and it was the best and better organized conference I have ever 
attended in 14 years of international meetings.

I apologize if this is off topic, but I read in the list's description that 
“[p]retty much anything Python-related is fair game for discussion”.

Although I am not a Python developer, I decided to attend EuroPython in search 
for a programmer interested in collaborating in the Python project I briefly 
describe below.

I use ecosystem models implemented with a procedural paradigm in a language 
different from Python (Pascal, for the records). I would like to migrate these 
ecosystem models (and code) to an object-oriented paradigm using Python, as I 
have come to believe its expressiveness would help a lot get the semantics 
right, rather than simply split procedural code into objects corresponding to 
ecological elements. What's more, our models use physiological analogies among 
the different levels of the food chain or web, and this makes them amenable to 
an even higher level of object-oriented abstraction given adequate 
expressiveness.

The goal is to go beyond the currently (mostly) formal implementation of the 
object-oriented paradigm in ecological models. To do that, I would need help 
from an expert Python programmer (who also has some math skills, knows English, 
and can work in the Rome area, or at least central Italy). I need help because 
I am a Python beginner with limited programming experience in general, and 
hence my contribution will mainly be the ecosystem modeling insight.

At EuroPython, I gave a lightning talk about the project that can be found on 
YouTube
http://youtu.be/iUNbgNuN0qY?t=31m50s

As I already made some very promising contacts at EuroPyton with developers 
that are interested and willing to help, and many people shared their views and 
provided useful insight into the issue (thanks!), this post is meant to get 
further feedback on my idea and possibly reach other interested developers.

Kindly contact me if you have any interest in the idea and time to devote it, 
as it is becoming a funded project.

Kind regards, thanks for any hint, and apologies for the many inaccuracies,

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


How to parse the starting and ending of a loop statements in python

2013-07-30 Thread karthik sridhar
My objective is to find the line numbers of the start and the end of a loop 
statement in python.

Example scenario

#A.py
Line1: a=0  
Line2: while a<5:   
Line3:print a  
Line4:a=a+1 

Desired output:
Start of a loop Line2 
End of a loop   Line4
 
Current parser code
#parser.py
with open(a) as f:
tree = ast.parse(f.read())
taskline=[]
for node in ast.walk(tree):
if isinstance(node, (ast.For)) or isinstance(node,(ast.While)): 
   
print node.lineno-1  <-- This give line number on for the start of a 
loop

I wanted to achieve the above output. I use AST to parse a given file and 
determine the occurrence of loops. With AST parsing i am able to find line 
number for the start of the loop but the line number for ending of the loop is 
yet to be determined. Is there any way i could parse an entire loop statement 
and determine its starting and ending line number ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Prime number generator

2013-07-30 Thread Albert van der Horst
In article ,
Chris Angelico   wrote:
>And now for something completely different.
>
>I knocked together a prime number generator, just for the fun of it,
>that works like a Sieve of Eratosthenes but unbounded. It keeps track
>of all known primes and the "next composite" that it will produce -
>for instance, after yielding 13, the prime map will be {2: 20, 3: 18,
>5: 20, 7: 21, 11: 22, 13: 26}, each one mapped to the first multiple
>greater than 13.
>
>Notable in the algorithm is an entire lack of division, or even
>multiplication. Everything is done with addition.
>
>So, a few questions. Firstly, is there a stdlib way to find the key
>with the lowest corresponding value? In the above map, it would return
>3, because 18 is the lowest value in the list. I want to do this with
>a single pass over the dictionary. Secondly, can the "while
>iit, but not quite there. Thirdly, is there any sort of half-sane
>benchmark that I can compare this code to? And finally, whose wheel
>did I reinvent here? What name would this algorithm have?

Notice that all values from i on are possibly present.
So you are better off with a list indexed by forthcoming i's and
each item containing a list of primes. What you do then, more or less,
is keep track of all dividers of primes to be.
This promises to be reasonable efficient.
I've done a similar thing in Forth.

I've also done a slightly different but related parallel program on a
multi-processor Forth machine where each processor takes care of one
prime.

There is an unpleasant fact about this kind of generators.
If you want to go unbounded, you've no choice but remember all
primes. If you go bounded, you need to remember 168 up til 1M,
say sqrt(limit)/log(limit). This dramatic difference (and the lack
of processors) leads one quickly to decide for some upper bound.

>
>Code tested on Python 3.3, would probably run fine on pretty much any
>Python that supports yield, though I don't have a Py2.2 to test from
>__future__ import generators on!

I had problems with the print statement on a 2 version, fixed easy
enough.

>
>ChrisA
>
># -- start --
>def primes():
>   """Generate an infinite series of prime numbers."""
>   i=2
>   yield 2
>   prime={2:2} # Map a prime number to its next composite (but bootstrap 
> with 2:2)
>   while True:
>   # Find the smallest value in prime[] and its key.
>   # Is there a standard library way to do this??
>   # (If two values are equal smallest, either can be returned.)
>   prm=None
>   for p,val in prime.items():
>   if prm is None or val   prm,smallest=p,val
>   prime[prm]+=prm
>   while i   yield i
>   prime[i]=i+i
>   i+=1
>   if i==smallest: i+=1
>
>gen=primes()
>for i in range(30):
>   print(next(gen),end="\t") # Star Trek?
>print()
># -- end --
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: Modeling life on Earth –- an object-oriented (Python?) challenge

2013-07-30 Thread David Hutto
Never used pascal, and python might not be the fastest way to implement a
program such as this.

In a previous discussion, this was taken place by someone using a predator
prey brain class..

The simulation will vary, until a full refinement of forecast is above a
certainty percentage level.

Visualization is needed as well.

Collaboration is, of course
, the best possible route. However you need to start with certain
statistics, and know there will be an Uncerrtainty Principle rule applied.

The algorithm for such massive amounts of data analysis in a simulation
forecast, will involve HD space and RAM
.

You will also want to collaborate with certain databases in order to refine
the accuracy of your models.

This is kind of what I would consider being a Dune(Frank Herbert) planetary
engineer. It also takes in other db data such as tagging marks of animals
percentiles of bacterias/viruses/etcSO it's not as simple as it sounds,
and python would be more of a prototyping language, and later translated
into another language for faster maneuvering of data.



On Tue, Jul 30, 2013 at 4:57 AM,  wrote:

> Dear List,
>
> I have to start this email by saying that I have recently attended
> EuroPython in Florence, and it was the best and better organized conference
> I have ever attended in 14 years of international meetings.
>
> I apologize if this is off topic, but I read in the list's description
> that “[p]retty much anything Python-related is fair game for discussion”.
>
> Although I am not a Python developer, I decided to attend EuroPython in
> search for a programmer interested in collaborating in the Python project I
> briefly describe below.
>
> I use ecosystem models implemented with a procedural paradigm in a
> language different from Python (Pascal, for the records). I would like to
> migrate these ecosystem models (and code) to an object-oriented paradigm
> using Python, as I have come to believe its expressiveness would help a lot
> get the semantics right, rather than simply split procedural code into
> objects corresponding to ecological elements. What's more, our models use
> physiological analogies among the different levels of the food chain or
> web, and this makes them amenable to an even higher level of
> object-oriented abstraction given adequate expressiveness.
>
> The goal is to go beyond the currently (mostly) formal implementation of
> the object-oriented paradigm in ecological models. To do that, I would need
> help from an expert Python programmer (who also has some math skills, knows
> English, and can work in the Rome area, or at least central Italy). I need
> help because I am a Python beginner with limited programming experience in
> general, and hence my contribution will mainly be the ecosystem modeling
> insight.
>
> At EuroPython, I gave a lightning talk about the project that can be found
> on YouTube
> http://youtu.be/iUNbgNuN0qY?t=31m50s
>
> As I already made some very promising contacts at EuroPyton with
> developers that are interested and willing to help, and many people shared
> their views and provided useful insight into the issue (thanks!), this post
> is meant to get further feedback on my idea and possibly reach other
> interested developers.
>
> Kindly contact me if you have any interest in the idea and time to devote
> it, as it is becoming a funded project.
>
> Kind regards, thanks for any hint, and apologies for the many inaccuracies,
>
> Luigi
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com*
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: collections.Counter surprisingly slow

2013-07-30 Thread Serhiy Storchaka

29.07.13 14:49, Joshua Landau написав(ла):

I find it hard to agree that counter should be optimised for the
unique-data case, as surely it's much more oft used when there's a point
to counting?


Different methods are faster for different data. LBYL approach is best 
for the mostly unique data case, while EAFP approach is best for the 
mostly repeated data case. In general case a performance of particular 
method is a function of its performances in this two extreme cases. When 
it slow for one of extreme case it can be slow in a number of 
intermediate cases.



Also, couldn't Counter just extend from defaultdict?


Unfortunately this only will slowdown it.

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


Re: import syntax

2013-07-30 Thread Neil Cerutti
On 2013-07-29, Joshua Landau  wrote:
> Sure, just as one light is no brighter or dimmer than another
> when disregarding luminosity.
>
> As people have said, it improves diffs as well. It flows
> quicker into the "from module import things" form (which I oft
> prefer), too.
>
> When asking these questions, ask yourself "why would it
> *compile* differently? It wouldn't. Plus, premature
> optimisation is the root of all evil.
>
> 1) Write your code
> 2) If it's slow:
> 2a) Do you have time? If so:
> 2b) Is it important to speed up, or is the slowness not worth spending the
> hours fixing?
> 2c) Profile it to see what's actually slow
> 2d) Realise that the slow part is not what you thought it was
> 2e) Fix the bit that's slow (and nothing else)
> 2f) Repeat from 2
> 3) Write some more code

1a) Does it work?
1b) Can you prove it?

It's best to at least have some regression tests before you start
refactoring and optimizing.

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


Python descriptor protocol (for more or less structured data)

2013-07-30 Thread CWr
Hi together,

Some years ago I started a small WSGI project at my university. Since then the 
project was grown up every year. Some classes have more than 600 lines of code 
with (incl. boiler-plates mostly in descriptors/properties). 

Many of these properties are similar or have depencies among themselves.
The idea is to grouping similar properties like:

new style:
--
>>>m = MyClass(...)
>>>m.attr = 'some; complex:data#string'

>>>m.attr.value
'some'
>>>m.attr.extras
{'complex':('data','string')}

I wrote this descriptor:

class Descr:

def __init__(self, value):
self.attribute = self.__class__.__name__
self.__set__(None, value)

def __get__(self, obj, Type=None):
return getattr(obj, self.attribute, self)

def __set__(self, obj, value):
if obj is None: # descripting yourself
# do something here ...
self.value = value
else:
if hasattr(obj, self.attribute):
self.__get__(obj).__set__(None, value)
else:
setattr(obj, self.attribute, type(self)(value))

This works fine as long as the value attribute of Descr is read-only and the 
user have to use the descriptor interface e.g. __get__/__set__. Because
it's not guaranteed that the user gets a seperated instance of Descr which
will be living in obj.__dict__. If obj is None the descriptor will be returned
themselves.

But I would like that the user can use the following statement:

>>>m = MyClass(...)
>>>m.attr = 'some; complex:data#string'
>>>m.attr.value
'some'
>>>m.attr.value = 'some other'
>>>m.attr.value
'some other'

But this usage will be problematic. If the descriptor returned themselves 
(default case) and the user modified the value, he modified the default
value without to create a seperated instance attribute.

>>>class C:
>>>def __init__(self, value):
>>>if not value is None:
>>>self.attr = value
>>>attr = Descr('default value')

>>># explicit default usage (no problem): 
>>>C.attr.value
'default value'

>>>a = C()
>>>a.attr.value
'default value'

The following is the main Problem:

>>>a.attr.value = 'other'
>>>C.attr.value
'other'

The user could think that a new instance based value will be created. But it
isn't.

It will works fine only if I assign a value explicitly.

>>>m = MyClass(value='test')
>>>m.__dict__
>>>{'Descr':}

Has anyone had a similar problem in the past? Or I am on the wrong way.

Kind Regards,
Chris

Sorry for my terrible english ...





 





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


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Tim
On Monday, July 29, 2013 7:52:36 PM UTC-4, Chris Angelico wrote:
> On Mon, Jul 29, 2013 at 8:16 PM, Tim wrote:
> > My intent is to pass it a directory name or path and if it exists, use 
> > shutil.rmtree to remove whatever is there (if it isn't a directory, try to 
> > unlink it); then use os.makedirs to create a new directory or path:

> > def make_clean_dir(directory):
> > if os.path.exists(directory):
> > if os.path.isdir(directory):
> > shutil.rmtree(directory)
> > else:
> > os.unlink(directory)
> > os.makedirs(directory)
> >
> > The last bit of the traceback is:
> > File "/develop/myproject/helpers/__init__.py", line 35, in make_clean_dir
> > os.makedirs(directory)
> >   File "/usr/local/lib/python2.7/os.py", line 157, in makedirs
> > mkdir(name, mode)
> > OSError: [Errno 17] File exists: '/users/tim/testing/testing_html'
> >
> > The directory 'testing_html' existed when I executed the function;
>  
> First thing I'd check is: Did rmtree succeed? Try removing the
> makedirs and test it again; then, when your process has completely
> finished, see if the directory is there. If it is, the problem is in
> rmtree - for instance:

> * You might not have permission to remove everything
> * There might be a messed-up object in the file system
> * If the directory is a remote share mount point, the other end might
> have lied about the removal
> * Something might have been created inside the directory during the removal
> * Myriad other possibilities
> As I understand rmtree's docs, any errors *that it detects* will be
> raised as exceptions (since you haven't told it to suppress or handle
> them), but possibly there's an error that it isn't able to detect.
> Worth a test, anyhow.
> 
> ChrisA

Thanks Chris, but the directory was actually removed on the first run in spite 
of the traceback; when I run it a second time (immediately after the first 
time), it runs fine. That's why I thought it was a timing issue. I thought 
about just putting a sleep in there, but that made me feel dirty. 

hmm, now that you mention it, this is executing on a remote box with access to 
the same file system my local calling program is on. That is, there is a local 
call to an intermediate script that connects to a socket on the remote where 
the above program actually runs, but the file system is the same place for both 
local and remote.

But even so, since the script that does the rmtree and mkdir is running on the 
same machine (even though it's remote), I would think the mkdir couldn't 
execute until the rmtree was completely finished.

thanks,
--Tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 2:10 PM, Tim  wrote:
> hmm, now that you mention it, this is executing on a remote box with access 
> to the same file system my local calling program is on. That is, there is a 
> local call to an intermediate script that connects to a socket on the remote 
> where the above program actually runs, but the file system is the same place 
> for both local and remote.
>
> But even so, since the script that does the rmtree and mkdir is running on 
> the same machine (even though it's remote), I would think the mkdir couldn't 
> execute until the rmtree was completely finished.

Hmm. What system is used for the file system sharing? I know quite a
few of them lie about whether something's been completely done or not.

Can you use inotify to tell you when the directory's been deleted?
Seems stupid though.

Worst case, all you need is a quick loop at the bottom, eg:

for delay in 100,300,600,1000,3000,5000,1:
  if not os.path.exists(directory): break
  sleep(delay)

That'll sleep a maximum of 20 seconds, tune as required. Of course, if
there's a way to tune the FS to guarantee that the removal blocks
correctly, that would be way better than sleep()!

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


Unexpected results comparing float to Fraction

2013-07-30 Thread Oscar Benjamin
On 29 July 2013 17:09, MRAB  wrote:
> On 29/07/2013 16:43, Steven D'Aprano wrote:
>>
>> Comparing floats to Fractions gives unexpected results:

You may not have expected these results but as someone who regularly
uses the fractions module I do expect them.

>> # Python 3.3
>> py> from fractions import Fraction
>> py> 1/3 == Fraction(1, 3)
>> False
>>
>> but:
>>
>> py> 1/3 == float(Fraction(1, 3))
>> True

Why would you do the above? You're deliberately trying to create a
float with a value that you know is not representable by the float
type. The purpose of Fractions is precisely that they can represent
all rational values, hence avoiding these problems.

When I use Fractions my intention is to perform exact computation. I
am very careful to avoid allowing floating point imprecision to sneak
into my calculations. Mixing floats and fractions in computation is
not IMO a good use of duck-typing.

>> I expected that float-to-Fraction comparisons would convert the Fraction
>> to a float, but apparently they do the opposite: they convert the float
>> to a Fraction:
>>
>> py> Fraction(1/3)
>> Fraction(6004799503160661, 18014398509481984)
>>
>> Am I the only one who is surprised by this? Is there a general rule for
>> which way numeric coercions should go when doing such comparisons?

I would say that if type A is a strict superset of type B then the
coercion should be to type A. This is the case for float and Fraction
since any float can be represented exactly as a Fraction but the
converse is not true.

> I'm surprised that Fraction(1/3) != Fraction(1, 3); after all, floats
> are approximate anyway, and the float value 1/3 is more likely to be
> Fraction(1, 3) than Fraction(6004799503160661, 18014398509481984).

Refuse the temptation to guess: Fraction(float) should give the exact
value of the float. It should not give one of the countably infinite
number of other possible rational numbers that would (under a
particular rounding scheme and the floating point format in question)
round to the same float. If that is the kind of equality you would
like to test for in some particular situation then you can do so by
coercing to float explicitly.

Calling Fraction(1/3) is a misunderstanding of what the fractions
module is for and how to use it. The point is to guarantee avoiding
floating point errors; this is impossible if you use floating point
computations to initialise Fractions.

Writing Fraction(1, 3) does look a bit ugly so my preferred way to
reduce the boiler-plate in a script that uses lots of Fraction
"literals" is to do:

from fractions import Fraction as F

# 1/3 + 1/9 + 1/27 + ...
limit = F('1/3') / (1 - F('1/3'))

That's not as good as dedicated syntax but with code highlighting it's
still quite readable.


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


Re: RE Module Performance

2013-07-30 Thread wxjmfauth
Le dimanche 28 juillet 2013 05:53:22 UTC+2, Ian a écrit :
> On Sat, Jul 27, 2013 at 12:21 PM,   wrote:
> 
> > Back to utf. utfs are not only elements of a unique set of encoded
> 
> > code points. They have an interesting feature. Each "utf chunk"
> 
> > holds intrisically the character (in fact the code point) it is
> 
> > supposed to represent. In utf-32, the obvious case, it is just
> 
> > the code point. In utf-8, that's the first chunk which helps and
> 
> > utf-16 is a mixed case (utf-8 / utf-32). In other words, in an
> 
> > implementation using bytes, for any pointer position it is always
> 
> > possible to find the corresponding encoded code point and from this
> 
> > the corresponding character without any "programmed" information. See
> 
> > my editor example, how to find the char under the caret? In fact,
> 
> > a silly example, how can the caret can be positioned or moved, if
> 
> > the underlying corresponding encoded code point can not be
> 
> > dicerned!
> 
> 
> 
> Yes, given a pointer location into a utf-8 or utf-16 string, it is
> 
> easy to determine the identity of the code point at that location.
> 
> But this is not often a useful operation, save for resynchronization
> 
> in the case that the string data is corrupted.  The caret of an editor
> 
> does not conceptually correspond to a pointer location, but to a
> 
> character index.  Given a particular character index (e.g. 127504), an
> 
> editor must be able to determine the identity and/or the memory
> 
> location of the character at that index, and for UTF-8 and UTF-16
> 
> without an auxiliary data structure that is a O(n) operation.
> 
> 
--

Same conceptual mistake as Steven's example with its buffers,
the buffer does not know it holds characters.
This is not the point to discuss.

-

I am pretty sure that once you have typed your 127504
ascii characters, you are very happy the buffer of your
editor does not waste time in reencoding the buffer as
soon as you enter an €, the 125505th char. Sorry, I wanted
to say z instead of euro, just to show that backspacing the
last char and reentering a new char implies twice a reencoding.

Somebody wrote "FSR" is just an optimization. Yes, but in case
of an editor à la FSR, this optimization take place everytime you
enter a char. Your poor editor, in fact the FSR, is finally
spending its time in optimizing and finally it optimizes nothing.
(It is even worse).

If you type correctly a z instead of an €, it is not necessary
to reencode the buffer. Problem, you do you know that you do
not have to reencode? simple just check it, and by just checking
it wastes time to test it you have to optimized or not and hurt
a little bit more what is supposed to be an optimization.

Do not confuse the process of optimisation and the result of
optimization (funny, it's like the utf's).

There is a trick to make the editor to know if it has
to be "optimized". Just put some flag somewhere. Then
you fall on the "Houston" syndrome. Houston, we got a
problem, our buffer consumes much more bytes than expected.

>>> sys.getsizeof('€')
40
>>> sys.getsizeof('a')
26

Now the good news. In an editor à la FSR, the
"composition" is not so important. You know,
"practicality beats purity". The hard job
is the text rendering engine and the handling
of the font (even in a raw unicode editor).
And as these tools are luckily not woking à la FSR
(probably because they understand the coding
of the characters), your editor is still working
not so badly.

jmf

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


Re: PEP8 79 char max

2013-07-30 Thread Skip Montanaro
> In that gauge I would exclude indentation (you don't count the
> number of characters the margin takes) 

I don't think anyone reads the margins. :-)

That said, I agree that code and prose are fundamentally different
beasts.  Still, when reading either and you get to the end of the
line, you need to shift your gaze down a line and back to the left
margin (or the left margin plus any indentation).  That task becomes
more difficult as line length increases.

As programmers/software engineers, we need to read and write both code
and text. I think 80 columns is still a decent compromise.

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


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Steven D'Aprano
On Tue, 30 Jul 2013 14:27:10 +0100, Chris Angelico wrote:

> for delay in 100,300,600,1000,3000,5000,1:
>   if not os.path.exists(directory): break
>   sleep(delay)
> 
> That'll sleep a maximum of 20 seconds, tune as required.

Actually, that will sleep a maximum of 5.55 hours, and a minimum of 1.7 
minutes (assuming the directory doesn't get deleted instantaneously).

time.sleep() takes an argument in seconds.



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


Re: Python descriptor protocol (for more or less structured data)

2013-07-30 Thread Peter Otten
CWr wrote:

> Some years ago I started a small WSGI project at my university. Since then
> the project was grown up every year. Some classes have more than 600 lines
> of code with (incl. boiler-plates mostly in descriptors/properties).
> 
> Many of these properties are similar or have depencies among themselves.
> The idea is to grouping similar properties like:
> 
> new style:
> --
m = MyClass(...)
m.attr = 'some; complex:data#string'
> 
m.attr.value
> 'some'
m.attr.extras
> {'complex':('data','string')}
> 
> I wrote this descriptor:
> 
> class Descr:
> 
> def __init__(self, value):
> self.attribute = self.__class__.__name__
> self.__set__(None, value)
> 
> def __get__(self, obj, Type=None):
> return getattr(obj, self.attribute, self)
> 
> def __set__(self, obj, value):
> if obj is None: # descripting yourself
> # do something here ...
> self.value = value
> else:
> if hasattr(obj, self.attribute):
> self.__get__(obj).__set__(None, value)
> else:
> setattr(obj, self.attribute, type(self)(value))

You must not store per-object data in the descriptor. I suggest a naming 
convention (the internal data for obj.attr is stored in obj._attr) together 
with a value class that handles breaking of the string into attributes of an 
instance of itself:

class StructuredAttribute:
def __init__(self, name, make_default):
self.name = name
self.make_default = make_default

def __get__(self, obj, type=None):
if obj is None:
return self
_name = "_" + self.name
try:
return getattr(obj, _name)
except AttributeError:
setattr(obj, _name, self.make_default())
return getattr(obj, _name)

def __set__(self, obj, value):
self.__get__(obj).update(value)


class Value:
def __init__(self, value):
self.update(value)
def update(self, value):
if isinstance(value, str):
self.value, sep, rest = value.partition(";")
self.extras = dict(item.partition("#")[::2] for item in 
rest.split())
else:
self.value = value.value
self.extras = value.extras
def __repr__(self):
return repr("{}; {}".format(self.value, " ".join("{}:
{}".format(*item) for item in self.extras.items(

def make_default_value():
return Value("some; complex:data#string")

class A:
attr = StructuredAttribute("alpha", make_default_value)

def show(obj):
print("attr:", obj.attr)
print("attr.value:", obj.attr.value)
print("attr.extras:", obj.attr.extras)

a = A()
show(a)
newvalue = "whatever"
print("updating value to", newvalue)
a.attr.value = newvalue
show(a)

That's the general idea if you want "setattr polymorphism". Personally I 
would go with simpler standard attributes:

class A:
def __init__(self):
self.attr = Value(...)

a = A()
a.value = Value(...)
a.value.extras = ...



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


Re: RE Module Performance

2013-07-30 Thread Antoon Pardon
Op 30-07-13 16:01, wxjmfa...@gmail.com schreef:
> 
> I am pretty sure that once you have typed your 127504
> ascii characters, you are very happy the buffer of your
> editor does not waste time in reencoding the buffer as
> soon as you enter an €, the 125505th char. Sorry, I wanted
> to say z instead of euro, just to show that backspacing the
> last char and reentering a new char implies twice a reencoding.

Using a single string as an editor buffer is a bad idea in python
for the simple reason that strings are immutable. So adding
characters would mean continuously copying the string buffer
into a new string with the next character added. Copying
127504 characters into a new string will not make that much
of a difference whether the octets are just copied to octets
or are unpacked into 32 bit words.

> Somebody wrote "FSR" is just an optimization. Yes, but in case
> of an editor à la FSR, this optimization take place everytime you
> enter a char. Your poor editor, in fact the FSR, is finally
> spending its time in optimizing and finally it optimizes nothing.
> (It is even worse).

Even if you would do it this way, it would *not* take place
every time you enter a char. Once your buffer would contain
a wide character, it would just need to convert the single
character that is added after each keystroke. It would not
need to convert the whole buffer after each key stroke.

> If you type correctly a z instead of an €, it is not necessary
> to reencode the buffer. Problem, you do you know that you do
> not have to reencode? simple just check it, and by just checking
> it wastes time to test it you have to optimized or not and hurt
> a little bit more what is supposed to be an optimization.

Your scenario is totally unrealistic. First of all because of
the immutable nature of python strings, second because you
suggest that real time usage would result in frequent conversions
which is highly unlikely.

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


Re: RE Module Performance

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 3:01 PM,   wrote:
> I am pretty sure that once you have typed your 127504
> ascii characters, you are very happy the buffer of your
> editor does not waste time in reencoding the buffer as
> soon as you enter an €, the 125505th char. Sorry, I wanted
> to say z instead of euro, just to show that backspacing the
> last char and reentering a new char implies twice a reencoding.

You're still thinking that the editor's buffer is a Python string. As
I've shown earlier, this is a really bad idea, and that has nothing to
do with FSR/PEP 393. An immutable string is *horribly* inefficient at
this; if you want to keep concatenating onto a string, the recommended
method is a list of strings that gets join()d at the end, and the same
technique works well here. Here's a little demo class that could make
the basis for such a system:

class EditorBuffer:
def __init__(self,fn):
self.fn=fn
self.buffer=[open(fn).read()]
def insert(self,pos,char):
if pos==0:
# Special case: insertion at beginning of buffer
if len(self.buffer[0])>1024: self.buffer.insert(0,char)
else: self.buffer[0]=char+self.buffer[0]
return
for idx,part in enumerate(self.buffer):
l=len(part)
if pos>l:
pos-=l
continue
if pos1024: self.buffer[idx:idx+1]=self.buffer[idx],char
else: self.buffer[idx]+=char
return
raise ValueError("Cannot insert past end of buffer")
def __str__(self):
return ''.join(self.buffer)
def save(self):
open(fn,"w").write(str(self))

It guarantees that inserts will never need to resize more than 1KB of
text. As a real basis for an editor, it still sucks, but it's purely
to prove this one point.

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


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 3:07 PM, Steven D'Aprano
 wrote:
> On Tue, 30 Jul 2013 14:27:10 +0100, Chris Angelico wrote:
>
>> for delay in 100,300,600,1000,3000,5000,1:
>>   if not os.path.exists(directory): break
>>   sleep(delay)
>>
>> That'll sleep a maximum of 20 seconds, tune as required.
>
> Actually, that will sleep a maximum of 5.55 hours, and a minimum of 1.7
> minutes (assuming the directory doesn't get deleted instantaneously).
>
> time.sleep() takes an argument in seconds.

LOL! Whoops. That's what I get for not checking my docs. This is why
we have public responses, my errors can be caught by someone else.

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


Python script help

2013-07-30 Thread cool1574
Hello, I am looking for a script that will be able to search an online document 
(by giving the script the URL) and find all the downloadable links in the 
document and then download them automatically.
I appreciate your help,
Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Neil Cerutti
On 2013-07-30, Skip Montanaro  wrote:
>> In that gauge I would exclude indentation (you don't count the
>> number of characters the margin takes) 
>
> I don't think anyone reads the margins. :-)
>
> That said, I agree that code and prose are fundamentally
> different beasts.  Still, when reading either and you get to
> the end of the line, you need to shift your gaze down a line
> and back to the left margin (or the left margin plus any
> indentation).  That task becomes more difficult as line length
> increases.

Most research about speed of comprehension of different line
lengths was based on subjects reading prose. The effect of code
line length hasn't been studied extensively.

> As programmers/software engineers, we need to read and write
> both code and text. I think 80 columns is still a decent
> compromise.

So rules of thumb, standardizations, and personal preferences are
mostly what we have to go by.

When code that looks very similar to code you've seen before
really *is* similar to code you've seen before, comprehension
speed can increase. A study of chess masters' ability to memorize
chess positions showed that they were powerfully accurate when
shown positions from real games, but no better than the average
schmoe when shown randomly positioned pieces. So if everyone
basically follows PEP8 we all benefit from playing by the same
game rules, as it were.

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


Re: importing modules

2013-07-30 Thread Dave Angel

On 07/29/2013 05:57 PM, syed khalid wrote:

I am attempting to import modules from Shogun to python from a non-standard
python directory ie from my /home/xxx directory. is there a way on ubuntu
to selectively some modules, scripts, data  from one directory and others
modules, scripts from another directory. In other words, is there a file(s)
that provide pointers to where these modules are located.



Your question is confusing, but the short answer is that import will 
search the sys.path list for you.  So in your main script, you can add a 
directory to that list,  before doing the imports.


It can also be done with an environment variable, or with the site file, 
but I don't bother.



--
DaveA

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


Re: Python script help

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 3:49 PM,   wrote:
> Hello, I am looking for a script that will be able to search an online 
> document (by giving the script the URL) and find all the downloadable links 
> in the document and then download them automatically.
> I appreciate your help,
> Thank you.

baseurl = "http://";
options = "."
os.system("wget "+options+" "+baseurl)

Sometimes the right tool for the job isn't Python.

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


Re: PEP8 79 char max

2013-07-30 Thread Skip Montanaro
> So if everyone basically follows PEP8 we all benefit from playing by
> the same game rules, as it were.

(I think I'm agreeing with you, but nonetheless, I will forge ahead.)

To the extent that 80-column window widths have been common for so
long, PEP 8 or not (and Python or not), there is a ton of code out
there which abides by that convention.  More-or-less unilaterally
increasing the recommended max line width to 100 (or 99?) columns
isn't likely to improve things.  People like me (who prefer the status
quo) will complain about all the new-fangled code written to a wider
standard (and will be tempted to reformat).  People who like the new
standard will complain about old code wasting all that white space
(and will be tempted to reformat). :-)

Finally (I promise this is my last word on the topic), most lines
don't need to be wrapped as they stand today.  See the attached graph
for the distribution of line lengths for the current project where I
spend most of my time these days (just Python code, blank lines
elided, comment lines included).  Stretching the max out to 100
columns when most lines are less than 60 columns just wastes screen
real estate.

Skip
<>-- 
http://mail.python.org/mailman/listinfo/python-list


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Tim
On Tuesday, July 30, 2013 9:27:10 AM UTC-4, Chris Angelico wrote:
> On Tue, Jul 30, 2013 at 2:10 PM, Tim wrote:
> > hmm, now that you mention it, this is executing on a remote box with access 
> > to the same file system my local calling program is on. That is, there is a 
> > local call to an intermediate script that connects to a socket on the 
> > remote where the above program actually runs, but the file system is the 
> > same place for both local and remote.
> >
> > But even so, since the script that does the rmtree and mkdir is running on 
> > the same machine (even though it's remote), I would think the mkdir 
> > couldn't execute until the rmtree was completely finished.
> 
> Hmm. What system is used for the file system sharing? I know quite a
> few of them lie about whether something's been completely done or not.  
> Can you use inotify to tell you when the directory's been deleted? 
> Seems stupid though.   
> Worst case, all you need is a quick loop at the bottom, eg:
> 
<>  
> ChrisA

Argg, this isn't the first time I've had troubles with the file system. This is 
FreeBSD and NFS. I will code up a progressive delay as you mentioned (with 
Steve's correction).

thanks much!
--Tim


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


Re: Python script help

2013-07-30 Thread cool1574
I know but I think using Python in this situation is good...is that the full 
script?
-- 
http://mail.python.org/mailman/listinfo/python-list


Share Code: Laptop Lid State

2013-07-30 Thread Devyn Collier Johnson

Aloha everyone!

   I attached a script that I thought I could share with everyone for 
your help. This Python3 script only works on Unix systems. It prints the 
current state of the lid. This can be used to make a script that 
performs some action when the lid is closed or open. The script is 
licensed under LGPLv3 and I will soon upload it to my Launchpad account. 
Enjoy!


Mahalo,

devyncjohn...@gmail.com
#!/usr/bin/env python3
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS 


#   LGPLv3 - 2013. (Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS)©
#This program is free software: you can redistribute it and/or modify it under 
the terms of the GNU General Public License as
#published by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.
#   This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU 
General Public License for more details.
#You should have received a copy of the GNU General Public License along with 
this program. If not, see .

import subprocess; print(subprocess.getoutput('cat 
/proc/acpi/button/lid/LID/state | awk \'{ print $2 }\''))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script help

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 4:49 PM,   wrote:
> I know but I think using Python in this situation is good...is that the full 
> script?

That script just drops out to the system and lets wget do it. So don't
bother with it.

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


Re: Share Code: Laptop Lid State

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 3:06 PM, Devyn Collier Johnson
 wrote:
> Aloha everyone!
>
>I attached a script that I thought I could share with everyone for your
> help. This Python3 script only works on Unix systems. It prints the current
> state of the lid. This can be used to make a script that performs some
> action when the lid is closed or open. The script is licensed under LGPLv3
> and I will soon upload it to my Launchpad account. Enjoy!

There's... no Python code in that. Why not simply
open("/proc/acpi/button/lid/LID/state") and read from it, instead of
using cat and awk?

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


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 4:37 PM, Tim  wrote:
> Argg, this isn't the first time I've had troubles with the file system. This 
> is FreeBSD and NFS. I will code up a progressive delay as you mentioned (with 
> Steve's correction).

I've used several different networked file systems, including
NetBIOS/NetBEUI/SMB/Samba/etc, sshfs/cifs, and nfs. Not one of them
"feels" as clean as I'd like, though sshfs comes closest. There always
seem to be hacks around.

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


Re: RE Module Performance

2013-07-30 Thread MRAB

On 30/07/2013 15:38, Antoon Pardon wrote:

Op 30-07-13 16:01, wxjmfa...@gmail.com schreef:


I am pretty sure that once you have typed your 127504 ascii
characters, you are very happy the buffer of your editor does not
waste time in reencoding the buffer as soon as you enter an €, the
125505th char. Sorry, I wanted to say z instead of euro, just to
show that backspacing the last char and reentering a new char
implies twice a reencoding.


Using a single string as an editor buffer is a bad idea in python for
the simple reason that strings are immutable.


Using a single string as an editor buffer is a bad idea in _any_
language because an insertion would require all the following
characters to be moved.


So adding characters would mean continuously copying the string
buffer into a new string with the next character added. Copying
127504 characters into a new string will not make that much of a
difference whether the octets are just copied to octets or are
unpacked into 32 bit words.


Somebody wrote "FSR" is just an optimization. Yes, but in case of
an editor à la FSR, this optimization take place everytime you
enter a char. Your poor editor, in fact the FSR, is finally
spending its time in optimizing and finally it optimizes nothing.
(It is even worse).


Even if you would do it this way, it would *not* take place every
time you enter a char. Once your buffer would contain a wide
character, it would just need to convert the single character that is
added after each keystroke. It would not need to convert the whole
buffer after each key stroke.


If you type correctly a z instead of an €, it is not necessary to
reencode the buffer. Problem, you do you know that you do not have
to reencode? simple just check it, and by just checking it wastes
time to test it you have to optimized or not and hurt a little bit
more what is supposed to be an optimization.


Your scenario is totally unrealistic. First of all because of the
immutable nature of python strings, second because you suggest that
real time usage would result in frequent conversions which is highly
unlikely.


What you would have is a list of mutable chunks.

Inserting into a chunk would be fast, and a chunk would be split if
it's already full. Also, small adjacent chunks would be joined together.

Finally, a chunk could use FSR to reduce memory usage.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python script help

2013-07-30 Thread cool1574
** urlib, urlib2

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


Re: Python script help

2013-07-30 Thread Ulrich Eckhardt

Am 30.07.2013 16:49, schrieb cool1...@gmail.com:

Hello, I am looking for a script that will be able to search an
online document (by giving the script the URL) and find all the
downloadable links in the document and then download them
automatically.


Well, that's actually pretty simple. Using the URL, download the 
document. Then, parse it in order to extract embedded URLs and finally 
download the resulting URLs.


If you have specific problems, please provide more info which part 
exactly you're having problems with, along with what you already tried 
etc. In short, show some effort yourself. In the meantime, I'd suggest 
reading a Python tutorial and Eric Raymonds essay on asking smart questions.


Greetings!

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


Re: Python script help

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 5:10 PM,   wrote:
> What if I want to use only Python? is that possible? using lib and lib2?
> --
> http://mail.python.org/mailman/listinfo/python-list

Sure, anything's possible. And a lot easier if you quote context in
your posts. But why do it? wget is exactly what you need.

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


Re: Python script help

2013-07-30 Thread cool1574
What if I want to use only Python? is that possible? using lib and lib2? 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE Module Performance

2013-07-30 Thread Antoon Pardon

Op 30-07-13 18:13, MRAB schreef:

On 30/07/2013 15:38, Antoon Pardon wrote:

Op 30-07-13 16:01, wxjmfa...@gmail.com schreef:


I am pretty sure that once you have typed your 127504 ascii
characters, you are very happy the buffer of your editor does not
waste time in reencoding the buffer as soon as you enter an €, the
125505th char. Sorry, I wanted to say z instead of euro, just to
show that backspacing the last char and reentering a new char
implies twice a reencoding.


Using a single string as an editor buffer is a bad idea in python for
the simple reason that strings are immutable.


Using a single string as an editor buffer is a bad idea in _any_
language because an insertion would require all the following
characters to be moved.


Not if you use a gap buffer.

--
Antoon Pardon.

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


Re: PEP8 79 char max

2013-07-30 Thread Joshua Landau
On 30 July 2013 16:44, Skip Montanaro  wrote:

> > So if everyone basically follows PEP8 we all benefit from playing by
> > the same game rules, as it were.
>
> (I think I'm agreeing with you, but nonetheless, I will forge ahead.)
>
> To the extent that 80-column window widths have been common for so
> long, PEP 8 or not (and Python or not), there is a ton of code out
> there which abides by that convention.  More-or-less unilaterally
> increasing the recommended max line width to 100 (or 99?) columns
> isn't likely to improve things.  People like me (who prefer the status
> quo) will complain about all the new-fangled code written to a wider
> standard (and will be tempted to reformat).  People who like the new
> standard will complain about old code wasting all that white space
> (and will be tempted to reformat). :-)
>
> Finally (I promise this is my last word on the topic), most lines
> don't need to be wrapped as they stand today.  See the attached graph
> for the distribution of line lengths for the current project where I
> spend most of my time these days (just Python code, blank lines
> elided, comment lines included).  Stretching the max out to 100
> columns when most lines are less than 60 columns just wastes screen
> real estate.
>

Your graph doesn't convince me.

Take this line from earlier, which is currently 102 characters and nearing
my personal limit.

completer = completer.Completer(bindings=[r'"\C-xo": overwrite-mode',
r'"\C-xd": dump-functions'])

Under rules to wrap to 80 characters (and in this case I'd probably do it
anyway), I'd normally wrap to this:

completer = completer.Completer(bindings=[
  r'"\C-xo": overwrite-mode',
  r'"\C-xd": dump-functions'
])

of line lengths 46, 36, 35 and 6 respectively. Thus it's impossible to so
easily tell how many lines would be "unwrapped" from your graph.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script help

2013-07-30 Thread Vincent Vande Vyvre

Le 30/07/2013 18:10, cool1...@gmail.com a écrit :

What if I want to use only Python? is that possible? using lib and lib2?


Have a look here:

http://bazaar.launchpad.net/~vincent-vandevyvre/qarte/trunk/view/head:/parsers.py

This script get a web page and parse it to find downloadable objects.
--
Vincent V.V.
Oqapy  . Qarte 
 . PaQager 

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


Re: PEP8 79 char max

2013-07-30 Thread Vito De Tullio
Ed Leafe wrote:

> I had read about a developer who switched to using proportional fonts for
> coding, and somewhat skeptically, tried it out. After a day or so it
> stopped looking strange, and after a week it seemed so much easier to
> read.

By my (limited) experience with proportional fonts, they can be useful only 
with something like elastic tabstops[0]. But, as a general rule, I simply 
found more "squared" to just use a fixed-width font.


[0] http://nickgravgaard.com/elastictabstops/

-- 
ZeD

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


Re: timing issue: shutil.rmtree and os.makedirs

2013-07-30 Thread Göktuğ Kayaalp
On Jul 30, 2013 3:29 PM, "Chris Angelico"  wrote:
>
> On Tue, Jul 30, 2013 at 2:10 PM, Tim  wrote:
> > hmm, now that you mention it, this is executing on a remote box with
access to the same file system my local calling program is on. That is,
there is a local call to an intermediate script that connects to a socket
on the remote where the above program actually runs, but the file system is
the same place for both local and remote.
> >
> > But even so, since the script that does the rmtree and mkdir is running
on the same machine (even though it's remote), I would think the mkdir
couldn't execute until the rmtree was completely finished.
>
> Hmm. What system is used for the file system sharing? I know quite a
> few of them lie about whether something's been completely done or not.
>
> Can you use inotify to tell you when the directory's been deleted?
> Seems stupid though.

Inotify is a linux thing, but there is kqueue for Free?BSD.  OP can run the
deletion procedure, wait for a NOTE_DELETE event, which would block, and
create the fresh directory afterwards.  It may require some C hacking
though, in case Python lacks a kqueue wrapper.  I think that this kind of
approach would be more sound than a check-wait-loop approach.

(I would elaborate more with pointers to appropriate documentation, but I'm
on a silly tablet, please excuse me for that.)

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


Re: RE Module Performance

2013-07-30 Thread MRAB

On 30/07/2013 17:39, Antoon Pardon wrote:

Op 30-07-13 18:13, MRAB schreef:

On 30/07/2013 15:38, Antoon Pardon wrote:

Op 30-07-13 16:01, wxjmfa...@gmail.com schreef:


I am pretty sure that once you have typed your 127504 ascii
characters, you are very happy the buffer of your editor does not
waste time in reencoding the buffer as soon as you enter an €, the
125505th char. Sorry, I wanted to say z instead of euro, just to
show that backspacing the last char and reentering a new char
implies twice a reencoding.


Using a single string as an editor buffer is a bad idea in python for
the simple reason that strings are immutable.


Using a single string as an editor buffer is a bad idea in _any_
language because an insertion would require all the following
characters to be moved.


Not if you use a gap buffer.


The disadvantage there is that when you move the cursor you must move
characters around. For example, what if the cursor was at the start and
you wanted to move it to the end? Also, when the gap has been filled,
you need to make a new one.

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


Re: RE Module Performance

2013-07-30 Thread Tim Delaney
On 31 July 2013 00:01,  wrote:

>
> I am pretty sure that once you have typed your 127504
> ascii characters, you are very happy the buffer of your
> editor does not waste time in reencoding the buffer as
> soon as you enter an €, the 125505th char. Sorry, I wanted
> to say z instead of euro, just to show that backspacing the
> last char and reentering a new char implies twice a reencoding.
>

And here we come to the root of your complete misunderstanding and
mischaracterisation of the FSR. You don't appear to understand that
strings in Python are immutable and that to add a character to an
existing string requires copying the entire string + new character. In
your hypothetical situation above, you have already performed 127504
copy + new character operations before you ever get to a single widening
operation. The overhead of the copy + new character repeated 127504
times dwarfs the overhead of a single widening operation.

Given your misunderstanding, it's no surprise that you are focused on
microbenchmarks that demonstrate that copying entire strings and adding
a character can be slower in some situations than others. When the only
use case you have is implementing the buffer of an editor using an
immutable string I can fully understand why you would be concerned about
the performance of adding and removing individual characters. However,
in that case *you're focused on the wrong problem*.

Until you can demonstrate an understanding that doing the above in any
language which has immutable strings is completely insane you will have
no credibility and the only interest anyone will pay to your posts is
refuting your FUD so that people new to the language are not driven off
by you.

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


Re: RE Module Performance

2013-07-30 Thread Joshua Landau
On 30 July 2013 17:39, Antoon Pardon  wrote:

> Op 30-07-13 18:13, MRAB schreef:
>
>  On 30/07/2013 15:38, Antoon Pardon wrote:
>>
>>> Op 30-07-13 16:01, wxjmfa...@gmail.com schreef:
>>>

 I am pretty sure that once you have typed your 127504 ascii
 characters, you are very happy the buffer of your editor does not
 waste time in reencoding the buffer as soon as you enter an €, the
 125505th char. Sorry, I wanted to say z instead of euro, just to
 show that backspacing the last char and reentering a new char
 implies twice a reencoding.

>>>
>>> Using a single string as an editor buffer is a bad idea in python for
>>> the simple reason that strings are immutable.
>>>
>>
>> Using a single string as an editor buffer is a bad idea in _any_
>> language because an insertion would require all the following
>> characters to be moved.
>>
>
> Not if you use a gap buffer.


Additionally, who says a language couldn't use, say, B-Trees for all of its
list-like types, including strings?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Joshua Landau
On 30 July 2013 18:08, Vito De Tullio  wrote:

> Ed Leafe wrote:
>
> > I had read about a developer who switched to using proportional fonts for
> > coding, and somewhat skeptically, tried it out. After a day or so it
> > stopped looking strange, and after a week it seemed so much easier to
> > read.
>
> By my (limited) experience with proportional fonts, they can be useful only
> with something like elastic tabstops[0]. But, as a general rule, I simply
> found more "squared" to just use a fixed-width font.
>

Not if you give up on the whole "aligning" thing.


> [0] http://nickgravgaard.com/elastictabstops/
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Grant Edwards
On 2013-07-30, Joshua Landau  wrote:
> On 30 July 2013 18:08, Vito De Tullio  wrote:
>
>> Ed Leafe wrote:
>>
>> > I had read about a developer who switched to using proportional fonts for
>> > coding, and somewhat skeptically, tried it out. After a day or so it
>> > stopped looking strange, and after a week it seemed so much easier to
>> > read.
>>
>> By my (limited) experience with proportional fonts, they can be useful only
>> with something like elastic tabstops[0]. But, as a general rule, I simply
>> found more "squared" to just use a fixed-width font.
>>
>
> Not if you give up on the whole "aligning" thing.

You don't think that Python code at a given level should all be
aligned?  I find it very helpful when a given block of code is
visually left-aligned.

I also find intializers for tables of data to be much more easily read
and maintained if the columns can be aligned.

-- 
Grant Edwards   grant.b.edwardsYow! MMM-MM!!  So THIS is
  at   BIO-NEBULATION!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Vito De Tullio
Joshua Landau wrote:

>> By my (limited) experience with proportional fonts, they can be useful
>> only with something like elastic tabstops[0]. But, as a general rule, I
>> simply found more "squared" to just use a fixed-width font.

> Not if you give up on the whole "aligning" thing.


and this is one of the reason why I come back to fixed-width

-- 
By ZeD

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


Re: Share Code: Laptop Lid State

2013-07-30 Thread Ian Kelly
On Jul 30, 2013 10:06 AM, "Chris Angelico"  wrote:
>
> On Tue, Jul 30, 2013 at 3:06 PM, Devyn Collier Johnson
>  wrote:
> > Aloha everyone!
> >
> >I attached a script that I thought I could share with everyone for
your
> > help. This Python3 script only works on Unix systems. It prints the
current
> > state of the lid. This can be used to make a script that performs some
> > action when the lid is closed or open. The script is licensed under
LGPLv3
> > and I will soon upload it to my Launchpad account. Enjoy!
>
> There's... no Python code in that. Why not simply
> open("/proc/acpi/button/lid/LID/state") and read from it, instead of
> using cat and awk?

Or for that matter, why not just make it a bash script instead of Python?
It's kind of pointless to go to all the trouble of starting a Python
interpreter just to have it start a subprocess.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE Module Performance

2013-07-30 Thread Antoon Pardon

Op 30-07-13 19:14, MRAB schreef:

On 30/07/2013 17:39, Antoon Pardon wrote:

Op 30-07-13 18:13, MRAB schreef:

On 30/07/2013 15:38, Antoon Pardon wrote:

Op 30-07-13 16:01, wxjmfa...@gmail.com schreef:


I am pretty sure that once you have typed your 127504 ascii
characters, you are very happy the buffer of your editor does not
waste time in reencoding the buffer as soon as you enter an €, the
125505th char. Sorry, I wanted to say z instead of euro, just to
show that backspacing the last char and reentering a new char
implies twice a reencoding.


Using a single string as an editor buffer is a bad idea in python for
the simple reason that strings are immutable.


Using a single string as an editor buffer is a bad idea in _any_
language because an insertion would require all the following
characters to be moved.


Not if you use a gap buffer.


The disadvantage there is that when you move the cursor you must move
characters around. For example, what if the cursor was at the start and
you wanted to move it to the end? Also, when the gap has been filled,
you need to make a new one.


So? Why are you making this a point of discussion? I was not aware that
the pro and cons of various editor buffer implemantations was relevant
to the point I was trying to make.

If you prefer an other data structure in the editor you are working on,
I will not dissuade you.

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


OrderedEnum examples

2013-07-30 Thread Bas van der Wulp
Using the enum34 0.9.13 package from PyPi in Python 2.7.3, the examples 
for OrderedEnum seem to be broken.


The example in the package documentation reads:

class OrderedEnum(Enum):
def __ge__(self, other):
if self.__class__ is other.__class__:
return self._value >= other._value
return NotImplemented
def __gt__(self, other):
if self.__class__ is other.__class__:
return self._value > other._value
return NotImplemented
def __le__(self, other):
if self.__class__ is other.__class__:
return self._value <= other._value
return NotImplemented
def __lt__(self, other):
if self.__class__ is other.__class__:
return self._value < other._value
return NotImplemented

class Grade(OrderedEnum):
__ordered__ = 'A B C D F'
A = 5
B = 4
C = 3
D = 2
F = 1

Grade.C < Grade.A

to which Python replies with:

Traceback (most recent call last):
  File "test.py", line 35, in 
print Grade.C < Grade.A
  File "test.py", line 23, in __lt__
return self._value < other._value
AttributeError: 'Grade' object has no attribute '_value'

Also, in the example in the Python 3.4 library documentation (section 
8.17.2) has the __ordered__ attribute removed (presumably because, in 
contrast to Python 2.x, Python 3 will respect the order of attribute 
definition). This example gives the same ValueErrror when using the 
enum34 package in Python 2.7.3. It is the same example, after all.


Replacing each occurrence of self._value with either self._value_ or 
self.value in the examples seems to make them work as expected.


Are both examples incorrect, or not intended to work in Python 2.x?

--
S. van der Wulp
--
http://mail.python.org/mailman/listinfo/python-list


Announcing Picat, the next scripting language after Python

2013-07-30 Thread neng . zhou
We are pleased to announce the launch of the Picat system on

 picat-lang.org. 

Picat is a simple, and yet powerful, logic-based multi-paradigm
programming language aimed for general-purpose applications. Picat is
a rule-based language, in which predicates, functions, and actors are
defined with pattern-matching rules. Picat incorporates many
declarative language features for better productivity of software
development, including explicit non-determinism, explicit
unification, functions, list comprehensions, constraints, and
tabling. Picat also provides imperative language constructs, such as
assignments and loops, for programming everyday things. The Picat
implementation, which is based on a well-designed virtual machine and
incorporates a memory manager that garbage-collects and expands the
stacks and data areas when needed, is efficient and scalable. Picat
can be used for not only symbolic computations, which is a
traditional application domain of declarative languages, but also for
scripting and modeling tasks. 

Example 1: The following predicate, input_data(Tri), reads rows of
integers from the text file "triangle.txt" into an array. This is the
first part of a Picat solution for the Euler Project, problem #67
(picat-lang.org/projects.html).

import util.

input_data(Tri) => 
Lines = read_file_lines("triangle.txt"),
Tri = new_array(Lines.length),
I = 1,
foreach(Line in Lines)
Tri[I] = Line.split().map(to_integer).to_array(),
I := I+1
end.

The function read_file_lines/1, which is imported by default from the
io module, reads all of the lines from a file as a list of strings.
For each Line in Lines, the foreach loop splits Line into tokens
(using the function split/1, which is imported from the util module),
maps the tokens to integers (map(to_integer)), and converts the list
to an array (to_array). As illustrated in this example, Picat, as a
scripting language, is as powerful as Python and Ruby.

Example 2: Given a triangle stored in an array, the following tabled
predicate finds the maximum total sum from top to bottom. This is the
second part of the Picat solution for the Euler Project, problem #67.

table (+,+,max,nt) 
path(Row,Col,Sum,Tri),Row==Tri.length => Sum=Tri[Row,Col].
path(Row,Col,Sum,Tri) ?=> 
path(Row+1,Col,Sum1,Tri),
Sum = Sum1+Tri[Row,Col].
path(Row,Col,Sum,Tri) => 
path(Row+1,Col+1,Sum1,Tri),
Sum = Sum1+Tri[Row,Col].
Sum = Sum1+Tri[Row,Col].

The first line is a table mode declaration that instructs the system
about how to table the calls and answers: '+' means that the argument
is tabled, 'max' means that the argument should be maximized, and
'nt' means that the argument is not tabled. This predicate searches
for a path with the maximum total sum. If the current row is at the
bottom of the triangle, then the leaf value is returned. Otherwise,
it makes a non-deterministic choice between two branches, one going
straight down and the other going down to the adjacent number. This
program is not only compact, but also runs fast. For the 100-row
triangle that is provided by the Euler project, this program finds
the answer in only 0.01 second.

Example 3: The following example models the N-queens problem by using
three all_different constraints. 

import cp.

queens3(N, Q) =>
Q = new_list(N),
Q in 1..N,
all_different(Q),
all_different([$Q[I]-I : I in 1..N]),
all_different([$Q[I]+I : I in 1..N]),
solve([ff],Q).

List comprehensions are used to specify lists. The expressions that
are preceded with a dollar sign denote terms rather than function
calls. This program uses the CP solver. If the sat module is imported
instead of cp, then the SAT solver will be used (and the ff option
will be ignored).

As demonstrated by the three examples, Picat offers many advantages
over other languages. Compared with functional and scripting
languages, the support of explicit unification, explicit
non-determinism, tabling, and constraints makes Picat more suitable
for symbolic computations. Compared with Prolog, Picat is arguably
more expressive and scalable: it is not rare to find problems for
which Picat requires an order of magnitude fewer lines of code to
describe than Prolog and Picat can be significantly faster than
Prolog because pattern-matching facilitates indexing of rules. 

Picat can be used for any fair purpose, including commercial
applications. The C source code will be made available to registered
developers and users free of charge. Please keep tuned.

Sincerely,

Neng-Fa Zhou
Jonathan Fruhman
Hakan Kjellerstrand
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OrderedEnum examples

2013-07-30 Thread Ian Kelly
On Tue, Jul 30, 2013 at 12:18 PM, Bas van der Wulp  wrote:
> Replacing each occurrence of self._value with either self._value_ or
> self.value in the examples seems to make them work as expected.
>
> Are both examples incorrect, or not intended to work in Python 2.x?

The _value attribute was renamed _value_ in:

http://hg.python.org/cpython/rev/511c4daac102

It looks like the example wasn't updated to match.  You should
probably just use self.value here since the name of the private
attribute is an implementation detail.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OrderedEnum examples

2013-07-30 Thread Ethan Furman

On 07/30/2013 11:18 AM, Bas van der Wulp wrote:

Using the enum34 0.9.13 package from PyPi in Python 2.7.3, the examples for 
OrderedEnum seem to be broken.


Thanks for catching that, I'll get it fixed asap.



Also, in the example in the Python 3.4 library documentation (section 8.17.2) 
has the __ordered__ attribute removed
(presumably because, in contrast to Python 2.x, Python 3 will respect the order 
of attribute definition).


Correct.  In 3.4 __ordered__ never came into being as it was not necessary.  I added that purely so that 2.x could be 
ordered if desired.



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


Re: RE Module Performance

2013-07-30 Thread wxjmfauth
Matable, immutable, copyint + xxx, bufferint, O(n) 
Yes, but conceptualy the reencoding happen sometime, somewhere.
The internal "ucs-2" will never automagically be transformed
into "ucs-4" (eg).

>>> timeit.timeit("'a'*1 +'€'")
7.087220684719967
>>> timeit.timeit("'a'*1 +'z'")
1.5685214234430873
>>> timeit.timeit("z = 'a'*1; z = z +'€'")
7.169538866162213
>>> timeit.timeit("z = 'a'*1; z = z +'z'")
1.5815893830557286
>>> timeit.timeit("z = 'a'*1; z += 'z'")
1.606955741596181
>>> timeit.timeit("z = 'a'*1; z += '€'")
7.160483334521416


And do not forget, in a pure utf coding scheme, your
char or a char will *never* be larger than 4 bytes.

>>> sys.getsizeof('a')
26
>>> sys.getsizeof('\U000101000')
48


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


Re: OrderedEnum examples

2013-07-30 Thread Ethan Furman

On 07/30/2013 11:38 AM, Ethan Furman wrote:


Thanks for catching that, I'll get it fixed asap.


Latest code is on PyPI.

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


Re: OrderedEnum examples

2013-07-30 Thread Ethan Furman

On 07/30/2013 11:58 AM, Ian Kelly wrote:

On Tue, Jul 30, 2013 at 12:18 PM, Bas van der Wulp  wrote:

Replacing each occurrence of self._value with either self._value_ or
self.value in the examples seems to make them work as expected.

Are both examples incorrect, or not intended to work in Python 2.x?


The _value attribute was renamed _value_ in:

http://hg.python.org/cpython/rev/511c4daac102

It looks like the example wasn't updated to match.  You should
probably just use self.value here since the name of the private
attribute is an implementation detail.


In `__new__` it has to be `_value_`, but in the other methods `.value` works 
fine.  Updated the 3.4 example with `.value`.

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


Re: embedding: how to create an "idle" handler to allow user to kill scripts?

2013-07-30 Thread David M. Cotter
Okay, i'm really surprised nobody knows how to do this.  and frankly i'm amazed 
at the utter lack of documentation.  but i've figured it out, and it's all 
working beautifully.

if you want the code, go here:
http://karaoke.kjams.com/wiki/Python
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE Module Performance

2013-07-30 Thread Chris Angelico
On Tue, Jul 30, 2013 at 8:09 PM,   wrote:
> Matable, immutable, copyint + xxx, bufferint, O(n) 
> Yes, but conceptualy the reencoding happen sometime, somewhere.
> The internal "ucs-2" will never automagically be transformed
> into "ucs-4" (eg).

But probably not on the entire document. With even a brainless scheme
like I posted code for, no more than 1024 bytes will need to be
recoded at a time (except in some odd edge cases, and even then, no
more than once for any given file).

> And do not forget, in a pure utf coding scheme, your
> char or a char will *never* be larger than 4 bytes.
>
 sys.getsizeof('a')
> 26
 sys.getsizeof('\U000101000')
> 48

Yeah, you have a few odd issues like, oh, I dunno, GC overhead,
reference count, object class, and string length, all stored somewhere
there. Honestly jmf, if you want raw assembly you know where to get
it.

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


Re: how to package embedded python?

2013-07-30 Thread David M. Cotter
yes, i've looked there, and all over google.  i'm quite expert at embedding at 
this point.

however nowhere i have looked has had instructions for "this this is how you 
package up your .exe with all the necessary python modules necessary to 
actually run on a user's system that does not have python installed".

on mac, it's trivial: all macs come with python, there is nothing i need to 
include with my app and it "just works"

on windows: if you don't include the proper DLLs and/or whatnot, then the app 
will complain about missing DLLs on startup.

What DLLs must i include?  where are the instructions?
-- 
http://mail.python.org/mailman/listinfo/python-list


binary key in dictionary

2013-07-30 Thread cerr
Hi,

In my application I have followingf lines:
print curr_mac
print hexlify(buf)
binmac = unhexlify(curr_mac)
tmpgndict[binmac] += buf
curr_mac being a 3Byte MAVC address in ASCII and I want to populate a 
dictionary where the value(buf) is indexed by binary mac.

I get this in my code:

Traceback (most recent call last):
  File "gateway.py", line 2485, in 
main()
  File "gateway.py", line 2459, in main
cloud_check()
  File "gateway.py", line 770, in cloud_check
gnstr_dict[src] = gn_from_cloud(curr_mac)
  File "gateway.py", line 2103, in gn_from_cloud
tmpgndict[binmac] += "HELLO"
KeyError: '\x04\xeeu'

but then again, the following works fine in the python interpreter:
>>> mac = '04ee75'
>>> dat = '2a0001016d03c400040001000a'
>>> mydict = {}
>>> mydict[unhexlify(mac)]=dat
>>> print mydict
{'\x04\xeeu': '2a0001016d03c400040001000a'}

I really seem to do something wrong and can't see what it is. Can anyone help 
me further here?

Thank you very much!
Ron
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: binary key in dictionary

2013-07-30 Thread Gary Herron

On 07/30/2013 01:29 PM, cerr wrote:

Hi,

In my application I have followingf lines:
 print curr_mac
 print hexlify(buf)
 binmac = unhexlify(curr_mac)
 tmpgndict[binmac] += buf
curr_mac being a 3Byte MAVC address in ASCII and I want to populate a 
dictionary where the value(buf) is indexed by binary mac.

I get this in my code:

Traceback (most recent call last):
   File "gateway.py", line 2485, in 
 main()
   File "gateway.py", line 2459, in main
 cloud_check()
   File "gateway.py", line 770, in cloud_check
 gnstr_dict[src] = gn_from_cloud(curr_mac)
   File "gateway.py", line 2103, in gn_from_cloud
 tmpgndict[binmac] += "HELLO"
KeyError: '\x04\xeeu'

but then again, the following works fine in the python interpreter:

mac = '04ee75'
dat = '2a0001016d03c400040001000a'
mydict = {}
mydict[unhexlify(mac)]=dat
print mydict

{'\x04\xeeu': '2a0001016d03c400040001000a'}

I really seem to do something wrong and can't see what it is. Can anyone help 
me further here?

Thank you very much!
Ron



You are confusing the problem with excess code.  Examine the following 
simpler example which illustrates the problem:

>>> d = {}
>>> d[1] = 99
>>> d[2] += 98
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 2
>>>

The  line
  d[1] = 99
creates a key-value pair in the dictionary, but the line
  d[2] += 98
tries to add 98 to an already existing value at d[2],   But there is no 
value at d[2] until you set it:

  d[2] = 0 # for instance

You may want to look at defaultdict from the collections module.

Gary Herron





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


Re: RE Module Performance

2013-07-30 Thread Terry Reedy

On 7/30/2013 1:40 PM, Joshua Landau wrote:


Additionally, who says a language couldn't use, say, B-Trees for all of
its list-like types, including strings?


Tk apparently uses a B-tree in its text widget.

--
Terry Jan Reedy

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


PyPy3 2.1 beta 1 released

2013-07-30 Thread Philip Jenvey

PyPy3 2.1 beta 1


We're pleased to announce the first beta of the upcoming 2.1 release of
PyPy3. This is the first release of PyPy which targets Python 3 (3.2.3)
compatibility.

We would like to thank all of the people who donated_ to the `py3k proposal`_
for supporting the work that went into this and future releases.

You can download the PyPy3 2.1 beta 1 release here:

http://pypy.org/download.html#pypy3-2-1-beta-1

Highlights
==

* The first release of PyPy3: support for Python 3, targetting CPython 3.2.3!

  - There are some `known issues`_ including performance regressions (issues
`#1540`_ & `#1541`_) slated to be resolved before the final release.

What is PyPy?
==

PyPy is a very compliant Python interpreter, almost a drop-in replacement for
CPython 2.7.3 or 3.2.3. It's fast due to its integrated tracing JIT compiler.

This release supports x86 machines running Linux 32/64, Mac OS X 64 or Windows
32. Also this release supports ARM machines running Linux 32bit - anything with
``ARMv6`` (like the Raspberry Pi) or ``ARMv7`` (like Beagleboard,
Chromebook, Cubieboard, etc.) that supports ``VFPv3`` should work.


Cheers,
the PyPy team
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: Python 3 and web applications?

2013-07-30 Thread Wayne Werner

On Fri, 26 Jul 2013, Rui Maciel wrote:


I'm currently learning Python, and I've been focusing on Python3.  To try to
kill two birds with one stone, I would also like to learn the basics of
writing small web applications.

These web applications don't need to do much more than provide an interface
to a small database, and they may not even be required to be accessible
outside of a LAN.

Does anyone have any tips on what's the best way to start off this
adventure?


Take a look at the Python3 branch of Flask: 
https://github.com/mitsuhiko/flask.git


And the werkzeug webserver:
https://github.com/mitsuhiko/werkzeug.git


If you download these you can install them with:


python setup.py install


(werkzeug first, then flask)


Here's the most basic webserver you can create that way:


from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():
return "Hello, Web!"

if __name__ == "__main__":
app.run()



And yet flask is highly extensible with a lot of plugins.


HTH,
W
--
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Cameron Simpson
On 30Jul2013 01:41, Rhodri James  wrote:
| On Tue, 30 Jul 2013 01:11:18 +0100, Joshua Landau  wrote:
| >On 30 July 2013 00:08, Rhodri James  wrote:
| >>I'm working on some shonky C code at the moment that inconsistent
| >>indentation and very long lines.
[...]

Have you tried the indent(1) command?

  DESCRIPTION
 indent is a C program formatter.  It reformats the C program in the
 input_file according to the switches.  The switches which can be speci‐
 fied are described below.  They may appear before or after the file
 names.

Very handy sometimes.

Cheers,
-- 
Cameron Simpson 

The top three answers:  Yes I *am* going to a fire!
Oh! We're using *kilometers* per hour now.
I have to go that fast to get back to my own time.
- Peter Harper 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script help

2013-07-30 Thread Cameron Simpson
On 30Jul2013 09:12, cool1...@gmail.com  wrote:
| ** urlib, urlib2

Sure. And I'd use BeautifulSoup to do the parse. You'll need to fetch that.
So: urllib[2] to fetch the document and BS to parse it for links,
then urllib[2] to fetch the links you want.

http://www.crummy.com/software/BeautifulSoup/bs4/download/4.0/

Cheers,
-- 
Cameron Simpson 

You can be psychotic and still be competent.
- John L. Young, American Academy of Psychiatry and the Law on Ted
  Kaczynski, and probably most internet users
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP8 79 char max

2013-07-30 Thread Cameron Simpson
On 29Jul2013 16:24, Devyn Collier Johnson  wrote:
| So, I can have a script with large lines and not negatively
| influence performance on systems that do not use punch cards?

Well, running anything will negatively impact the performance of a
system for others...o

Please think about what CPython actually executes, and then try to
figure out for yourself whether the line length or the source code
will affect that execution.

Cheers,
-- 
Cameron Simpson 

"How do you know I'm Mad?" asked Alice.
"You must be," said the Cat, "or you wouldn't have come here."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OrderedEnum examples

2013-07-30 Thread Bas van der Wulp

On 30-7-2013 21:30, Ethan Furman wrote:

On 07/30/2013 11:58 AM, Ian Kelly wrote:

On Tue, Jul 30, 2013 at 12:18 PM, Bas van der Wulp
 wrote:

Replacing each occurrence of self._value with either self._value_ or
self.value in the examples seems to make them work as expected.

Are both examples incorrect, or not intended to work in Python 2.x?


The _value attribute was renamed _value_ in:

http://hg.python.org/cpython/rev/511c4daac102

It looks like the example wasn't updated to match.  You should
probably just use self.value here since the name of the private
attribute is an implementation detail.


In `__new__` it has to be `_value_`, but in the other methods `.value`
works fine.  Updated the 3.4 example with `.value`.

--
~Ethan~


That was quick! Thanks Ethan and Ian.

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


Re: Python testing tools

2013-07-30 Thread cutems93
On Tuesday, July 23, 2013 3:51:00 PM UTC-7, Ben Finney wrote:
> cutems93  writes:
> 
> 
> 
> > On Saturday, July 20, 2013 1:11:12 AM UTC-7, Ben Finney wrote:
> 
> > > You will find these discussed at the Python Testing Tools Taxonomy
> 
> > > http://wiki.python.org/moin/PythonTestingToolsTaxonomy>.
> 
> > > 
> 
> > > Hope that helps.
> 
> >
> 
> > Thank you, but I already read this page before I posted this question.
> 
> 
> 
> (You will benefit from also reading and applying
> 
> http://wiki.python.org/moin/GoogleGroupsPython> before using Google
> 
> Groups. My advice: choose a different interface to this forum, Google
> 
> Groups is terrible.)
> 
> 
> 
> > What I want to know is whether you personally use these tools other
> 
> > than unit testing tools.
> 
> 
> 
> Yes, I do :-)
> 
> 
> 
> What are you actually wanting to learn, beyond a collection of “this is
> 
> what I use” stories?
> 
> 
> 
> -- 
> 
>  \  “The way to build large Python applications is to componentize |
> 
>   `\ and loosely-couple the hell out of everything.” —Aahz |
> 
> _o__)  |
> 
> Ben Finney

Sorry, I didn't notice that there are new replies. I want to know why you are 
using other software than unittest. Are those software more like "options" or 
"necessities" for you?

Thank you!
Min S.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to package embedded python?

2013-07-30 Thread CM
On Tuesday, July 30, 2013 4:23:06 PM UTC-4, David M. Cotter wrote:
> yes, i've looked there, and all over google.  i'm quite expert at embedding 
> at this point.
> 
> 
> 
> however nowhere i have looked has had instructions for "this this is how you 
> package up your .exe with all the necessary python modules necessary to 
> actually run on a user's system that does not have python installed".
> 
> 
> 
> on mac, it's trivial: all macs come with python, there is nothing i need to 
> include with my app and it "just works"
> 
> 
> 
> on windows: if you don't include the proper DLLs and/or whatnot, then the app 
> will complain about missing DLLs on startup.
> 
> 
> 
> What DLLs must i include?  where are the instructions?

I know nothing about embedding, but in terms of packaging up a Python 
interpreter with an application that needs it, could you use py2exe to do that? 
 If, so is this helpful (gotten from Google's cache since page doesn't appear 
at the moment...later try embedded Python py2exe search)?:

http://webcache.googleusercontent.com/search?q=cache:x3lrdFT5OF0J:www.py2exe.org/index.cgi/ShippingEmbedded+&cd=2&hl=en&ct=clnk&gl=us&client=firefox-a
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Share Code: Laptop Lid State

2013-07-30 Thread Devyn Collier Johnson


On 07/30/2013 12:00 PM, Chris Angelico wrote:

On Tue, Jul 30, 2013 at 3:06 PM, Devyn Collier Johnson
 wrote:

Aloha everyone!

I attached a script that I thought I could share with everyone for your
help. This Python3 script only works on Unix systems. It prints the current
state of the lid. This can be used to make a script that performs some
action when the lid is closed or open. The script is licensed under LGPLv3
and I will soon upload it to my Launchpad account. Enjoy!

There's... no Python code in that. Why not simply
open("/proc/acpi/button/lid/LID/state") and read from it, instead of
using cat and awk?

ChrisA
The script returns either "open" or "close" instead of printing the 
whole file contents. I thought some people would find it useful (^_^;).


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


Re: RE Module Performance

2013-07-30 Thread Neil Hodgson

MRAB:


The disadvantage there is that when you move the cursor you must move
characters around. For example, what if the cursor was at the start and
you wanted to move it to the end? Also, when the gap has been filled,
you need to make a new one.


   The normal technique is to only move the gap when text is added or 
removed, not when the cursor moves. Code that reads the contents, such 
as for display, handles the gap by checking the requested position and 
using a different offset when the position is after the gap.


   Gap buffers work well because changes are generally close to the 
previous change, so require moving only a relatively small amount of 
text. Even an occasional move of the whole contents won't cause too much 
trouble for interactivity with current processors moving multiple 
megabytes per millisecond.


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


Re: RE Module Performance

2013-07-30 Thread Michael Torrie
On 07/30/2013 12:19 PM, Antoon Pardon wrote:
> So? Why are you making this a point of discussion? I was not aware that
> the pro and cons of various editor buffer implemantations was relevant
> to the point I was trying to make.

I for one found it very interesting.  In fact this thread caused me to
wonder how one actually does create an efficient editor.  Off the
original topic true, but still very interesting.



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


Re: RE Module Performance

2013-07-30 Thread Michael Torrie
On 07/30/2013 01:09 PM, wxjmfa...@gmail.com wrote:
> Matable, immutable, copyint + xxx, bufferint, O(n) 
> Yes, but conceptualy the reencoding happen sometime, somewhere.
> The internal "ucs-2" will never automagically be transformed
> into "ucs-4" (eg).

So what major python project are you working on where you've found FSR
in general to be a problem?  Maybe we can help you work out a more
appropriate data structure and algorithm to use.

But if you're not developing something, and not developing in Python,
perhaps you should withdraw and let us use our horrible FSR in peace,
because it doesn't seem to bother the vast majority of python
programmers, and does not bother some large python projects out there.
In fact I think most of us welcome integrated, correct, full unicode.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Prime number generator

2013-07-30 Thread bryanjugglercryptographer
Chris Angelico wrote:
> Bas wrote:
> > Still trying to figure out your algorithm ...
> 
> It's pretty simple. (That's a bad start, I know!) Like the Sieve of
> Eratosthenes, it locates prime numbers, then deems every multiple of
> them to be composite. Unlike the classic sieve, it does the "deem"
> part in parallel. Instead of marking all the multiples of 2 first,
> then picking three and marking all the multiples of 3, then 5, etc,
> this function records the fact that it's up to (say) 42 in marking
> multiples of 2, and then when it comes to check if 43 is prime or not,
> it moves to the next multiple of 2. This requires memory to store the
> previously-known primes, similarly to other methods, but needs no
> multiplication or division.

Knuth points to the method, using a priority queue, in exercise 15 of section 
5.2.3 of /Sorting and Searching/, and credits it to "B. A. Chartres".

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


Re: Python script help

2013-07-30 Thread Denis McMahon
On Tue, 30 Jul 2013 07:49:04 -0700, cool1574 wrote:

> Hello, I am looking for a script that will be able to search an online
> document (by giving the script the URL) and find all the downloadable
> links in the document and then download them automatically.
> I appreciate your help,

Why use Python? Just:

wget -m url

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best python web framework to build university/academic website

2013-07-30 Thread bryanjugglercryptographer
b.kris...@gmail.com wrote:

> I got a chance to build an university website, within very short period of 
> time.
> I know web2py, little bit of Django, so please suggest me the best to build 
> rapidly.

Web2py rocks like nothing else for getting up fast. If you already know it, 
problem solved.

That said, Django has more available production-quality (free) utility apps. 
You might might want to check whether someone else has already done your work 
for you.

I'm leaning toward Django because it's ahead of Web2py in Python 3 support.

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


Re: Modeling life on Earth –- an object-oriented (Python?) challenge

2013-07-30 Thread Luigi Ponti
[...forgot to reply to the list...]

Dear David,

Thanks for your feedback -- you got right to the point:

...python would be more of a prototyping language, and later translated
> into another language for faster maneuvering of data
>

exactly! I was hoping that, since the modeling framework is conceptually
well developed (i.e., books, papers, analysis, etc. in 35+ years), most of
the work would be towards getting the code up to the same conceptual (i.e.,
abstraction) level. Hence, I was thinking Python would be a good tool for
that. Performance can be taken care of at a later stage, if needed.

Please do not hesitate to drop a further line.

Kind regards,

Luigi


On Tue, Jul 30, 2013 at 1:23 PM, David Hutto  wrote:

> Never used pascal, and python might not be the fastest way to implement a
> program such as this.
>
> In a previous discussion, this was taken place by someone using a predator
> prey brain class..
>
> The simulation will vary, until a full refinement of forecast is above a
> certainty percentage level.
>
> Visualization is needed as well.
>
> Collaboration is, of course
> , the best possible route. However you need to start with certain
> statistics, and know there will be an Uncerrtainty Principle rule applied.
>
> The algorithm for such massive amounts of data analysis in a simulation
> forecast, will involve HD space and RAM
> .
>
> You will also want to collaborate with certain databases in order to
> refine the accuracy of your models.
>
> This is kind of what I would consider being a Dune(Frank Herbert)
> planetary engineer. It also takes in other db data such as tagging marks of
> animals percentiles of bacterias/viruses/etcSO it's not as simple as it
> sounds, and python would be more of a prototyping language, and later
> translated into another language for faster maneuvering of data.
>
>
>
> On Tue, Jul 30, 2013 at 4:57 AM,  wrote:
>
>> Dear List,
>>
>> I have to start this email by saying that I have recently attended
>> EuroPython in Florence, and it was the best and better organized conference
>> I have ever attended in 14 years of international meetings.
>>
>> I apologize if this is off topic, but I read in the list's description
>> that “[p]retty much anything Python-related is fair game for discussion”.
>>
>> Although I am not a Python developer, I decided to attend EuroPython in
>> search for a programmer interested in collaborating in the Python project I
>> briefly describe below.
>>
>> I use ecosystem models implemented with a procedural paradigm in a
>> language different from Python (Pascal, for the records). I would like to
>> migrate these ecosystem models (and code) to an object-oriented paradigm
>> using Python, as I have come to believe its expressiveness would help a lot
>> get the semantics right, rather than simply split procedural code into
>> objects corresponding to ecological elements. What's more, our models use
>> physiological analogies among the different levels of the food chain or
>> web, and this makes them amenable to an even higher level of
>> object-oriented abstraction given adequate expressiveness.
>>
>> The goal is to go beyond the currently (mostly) formal implementation of
>> the object-oriented paradigm in ecological models. To do that, I would need
>> help from an expert Python programmer (who also has some math skills, knows
>> English, and can work in the Rome area, or at least central Italy). I need
>> help because I am a Python beginner with limited programming experience in
>> general, and hence my contribution will mainly be the ecosystem modeling
>> insight.
>>
>> At EuroPython, I gave a lightning talk about the project that can be
>> found on YouTube
>> http://youtu.be/iUNbgNuN0qY?t=31m50s
>>
>> As I already made some very promising contacts at EuroPyton with
>> developers that are interested and willing to help, and many people shared
>> their views and provided useful insight into the issue (thanks!), this post
>> is meant to get further feedback on my idea and possibly reach other
>> interested developers.
>>
>> Kindly contact me if you have any interest in the idea and time to devote
>> it, as it is becoming a funded project.
>>
>> Kind regards, thanks for any hint, and apologies for the many
>> inaccuracies,
>>
>> Luigi
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> Best Regards,
> David Hutto
> *CEO:* *http://www.hitwebdevelopment.com*
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: RE Module Performance

2013-07-30 Thread Steven D'Aprano
On Tue, 30 Jul 2013 12:09:11 -0700, wxjmfauth wrote:

> And do not forget, in a pure utf coding scheme, your char or a char will
> *never* be larger than 4 bytes.
> 
 sys.getsizeof('a')
> 26
 sys.getsizeof('\U000101000')
> 48

Neither character above is larger than 4 bytes. You forgot to deduct the 
size of the object header. Python is a high-level object-oriented 
language, if you care about minimizing every possible byte, you should 
use a low-level language like C. Then you can give every character 21 
bits, and be happy that you don't waste even one bit.


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


Re: PEP8 79 char max

2013-07-30 Thread Joshua Landau
On 30 July 2013 18:52, Grant Edwards  wrote:

> On 2013-07-30, Joshua Landau  wrote:
> > On 30 July 2013 18:08, Vito De Tullio  wrote:
> >
> >> Ed Leafe wrote:
> >>
> >> > I had read about a developer who switched to using proportional fonts
> for
> >> > coding, and somewhat skeptically, tried it out. After a day or so it
> >> > stopped looking strange, and after a week it seemed so much easier to
> >> > read.
> >>
> >> By my (limited) experience with proportional fonts, they can be useful
> only
> >> with something like elastic tabstops[0]. But, as a general rule, I
> simply
> >> found more "squared" to just use a fixed-width font.
> >>
> >
> > Not if you give up on the whole "aligning" thing.
>
> You don't think that Python code at a given level should all be
> aligned?  I find it very helpful when a given block of code is
> visually left-aligned.
>

I don't understand what you mean. My coding practices almost never require
anything more than the initial indentation to have things line up -- any
other form of alignment is in my opinion overrated. Maybe it helps you, but
personally I don't like it.

As I've been saying, the whole thing is personal preference and
proportional fonts for some people, such as I, are fine. Except in that
there are no good proportional fonts at 8px :(.

To explain, I tend to take the "HTML" form of alignment by wrapping:

open stuff stuff stuff close

to

open
stuff
stuff
stuff
close

and thus everything important lines up anyway. Extra non-indentation
indents are a burden for me and look worse (again, personal preference).

I also find intializers for tables of data to be much more easily read
> and maintained if the columns can be aligned.
>

Why do you have tables in your Python code?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python script help

2013-07-30 Thread Joshua Landau
On 30 July 2013 22:47, Cameron Simpson  wrote:

> On 30Jul2013 09:12, cool1...@gmail.com  wrote:
> | ** urlib, urlib2
>
> Sure. And I'd use BeautifulSoup to do the parse. You'll need to fetch that.
> So: urllib[2] to fetch the document and BS to parse it for links,
> then urllib[2] to fetch the links you want.
>
> http://www.crummy.com/software/BeautifulSoup/bs4/download/4.0/


Personally BeautifulSoup + requests is a great combination. Maybe I'm just
lazy ;).
-- 
http://mail.python.org/mailman/listinfo/python-list