Re: GOTCHA with list comprehension

2015-08-05 Thread Pavel S
It seems this is allowed by the grammar:

list_display::=  "[" [expression_list | list_comprehension] "]"
list_comprehension  ::=  expression list_for
list_for::=  "for" target_list "in" old_expression_list [list_iter]
old_expression_list ::=  old_expression [("," old_expression)+ [","]]
old_expression  ::=  or_test | old_lambda_expr
list_iter   ::=  list_for | list_if
list_if ::=  "if" old_expression [list_iter]

So chaining multiple ifs is fine:

[ i for i in range(10) if True if True if True if True ]

Dne středa 5. srpna 2015 8:49:20 UTC+2 Pavel S napsal(a):
> Hi,
> 
> I recently found interesting GOTCHA while doing list comprehension in python 
> 2.6:
> 
> >>> values = ( True, False, 1, 2, 3, None )
> >>> [ value for value in values if value if not None ]
> [True, 1, 2, 3]
> 
> I was wondering why this list comprehension returns incorrect results and 
> finally found a typo in the condition. The typo wasn't visible at the first 
> look.
> 
> My intention was: if value is not None
> But I wrote: if value if not None
> 
> Is that a language feature of list comprehension that it accepts conditions 
> like: if A if B if C if D ...?

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


Re: GOTCHA with list comprehension

2015-08-05 Thread Chris Angelico
On Wed, Aug 5, 2015 at 4:48 PM, Pavel S  wrote:
> Hi,
>
> I recently found interesting GOTCHA while doing list comprehension in python 
> 2.6:
>
 values = ( True, False, 1, 2, 3, None )
 [ value for value in values if value if not None ]
> [True, 1, 2, 3]
>
> I was wondering why this list comprehension returns incorrect results and 
> finally found a typo in the condition. The typo wasn't visible at the first 
> look.
>
> My intention was: if value is not None
> But I wrote: if value if not None
>
> Is that a language feature of list comprehension that it accepts conditions 
> like: if A if B if C if D ...?

It certainly is. You can chain 'for' and 'if' clauses as much as you
like, and they behave exactly the way you'd expect. You might possibly
get a warning from a linter with your code, though, as it has an
always-true condition ("if not None" can never be false), so it's
possible something might hint at what's going on; but other than that,
all you can do is test stuff and see if it's giving the right result.

Incidentally, why Python 2.6? Python 2.7 has been out for a pretty
long time now, and if you can't move to version 3.x, I would at least
recommend using 2.7. Since the release of 2.6.9 back before Frozen
came out, that branch has been completely unmaintained. Grab yourself
a 2.7 and take advantage of some neat new features (for old values of
"new"), and improved compatibility with 3.x.

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


Re: GOTCHA with list comprehension

2015-08-05 Thread Peter Otten
Pavel S wrote:

> Hi,
> 
> I recently found interesting GOTCHA while doing list comprehension in
> python 2.6:
> 
 values = ( True, False, 1, 2, 3, None )
 [ value for value in values if value if not None ]
> [True, 1, 2, 3]
> 
> I was wondering why this list comprehension returns incorrect results and
> finally found a typo in the condition. The typo wasn't visible at the
> first look.
> 
> My intention was: if value is not None
> But I wrote: if value if not None
> 
> Is that a language feature of list comprehension that it accepts
> conditions like: if A if B if C if D ...?

I think it's just that a condition may be a constant expression. Python 
evaluates (not None) for every item in values. Other variants:

>>> if 42:
... print("branch always taken")
... 
branch always taken
>>> always_yes = "yes" if True else "no"
>>> always_yes
'yes'
>>> [c for c in "foo" if "true in a boolean context"]
['f', 'o', 'o']

An optimizer might detect that (not None) is always True in a boolean 
context, but that would be an implementation detail.



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


Re: GOTCHA with list comprehension

2015-08-05 Thread Pavel S
$ cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.5 (Santiago)
$ python --version
Python 2.6.6

> Incidentally, why Python 2.6?
> 
> ChrisA


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


Re: GOTCHA with list comprehension

2015-08-05 Thread Chris Angelico
On Wed, Aug 5, 2015 at 5:03 PM, Pavel S  wrote:
> It seems this is allowed by the grammar:
>
> list_display::=  "[" [expression_list | list_comprehension] "]"
> list_comprehension  ::=  expression list_for
> list_for::=  "for" target_list "in" old_expression_list 
> [list_iter]
> old_expression_list ::=  old_expression [("," old_expression)+ [","]]
> old_expression  ::=  or_test | old_lambda_expr
> list_iter   ::=  list_for | list_if
> list_if ::=  "if" old_expression [list_iter]
>
> So chaining multiple ifs is fine:
>
> [ i for i in range(10) if True if True if True if True ]

Yep. A chain of 'if' clauses isn't materially different from a single
'if' with a bunch of 'and' checks, but if you alternate 'if' and 'for'
clauses, you get a comprehension that conditionally nests its
iterations.

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


Re: GOTCHA with list comprehension

2015-08-05 Thread Chris Angelico
On Wed, Aug 5, 2015 at 5:10 PM, Pavel S  wrote:
> $ cat /etc/redhat-release
> Red Hat Enterprise Linux Server release 6.5 (Santiago)
> $ python --version
> Python 2.6.6
>
>> Incidentally, why Python 2.6?
>>

I guess that would be why :)

That's probably actually a patched 2.6.6 - from what I understand of
how Red Hat works, the version number is the number of the *oldest*
part of the code, so that quite possibly has a lot of backported
fixes. When I said 2.6 was out of support, I meant from python.org;
Red Hat supports stuff for a lot longer.

So, yeah, perfectly good reason for sticking with 2.6. For now. :)

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


Re: GOTCHA with list comprehension

2015-08-05 Thread Pavel S
Hi Chris, yeah, I have to stick on the software which my employer provides to 
me (we're enterprise company). I'm not root on that system. I'm happy with 2.6 
now, two years ago we were on older RHEL with python 2.4 and it was a real pain 
:)

> > $ cat /etc/redhat-release
> > Red Hat Enterprise Linux Server release 6.5 (Santiago)
> > $ python --version
> > Python 2.6.6
> >
> >> Incidentally, why Python 2.6?
> >>
> 
> I guess that would be why :)
> 
> That's probably actually a patched 2.6.6 - from what I understand of
> how Red Hat works, the version number is the number of the *oldest*
> part of the code, so that quite possibly has a lot of backported
> fixes. When I said 2.6 was out of support, I meant from python.org;
> Red Hat supports stuff for a lot longer.
> 
> So, yeah, perfectly good reason for sticking with 2.6. For now. :)
> 
> ChrisA

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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Steven D'Aprano
On Wednesday 05 August 2015 05:59, Ben Finney wrote:

> marco.naw...@colosso.nl writes:
> 
>> Why not use Python files itself as configuration files?
> 
> Because configuration data will be user-editable. (If it's not
> user-editable, that is itself a poor design choice.)
> 
> If you allow executable code to be user-edited, that opens your program
> to arbitrary injection of executable code. Your program becomes wide
> open for security exploits, whether through malicious or accidental
> bugs, and simple human error can lead to arbitrary-scope damage to the
> user's system.

Yeah... it's almost as if you allowed the user to edit the source code of 
your application, or even write their own code. And nobody would do that! 

*wink*

I'm not entirely disagreeing, just adding some nuance to the discussion.

My own personal feeling is that using code as config is a little 
disquieting. It's a bit of a code smell. Do you really need that much power 
just to allow people to set some configuration settings? Using a Turing 
Complete programming language just to set a bunch of name:value pairs seems 
to be gross overkill.

But on the other hand, config systems do tend to grow in power. People often 
want conditional and computed settings. Rather than invent your own (buggy) 
mini-language to allow conf like this:

if $PLATFORM = 'Windows' set location = 'C:\The Stuff';
if $PLATFORM = 'Linux' set location = $baselocation + '/thestuff';

using Python seems like a much better idea.

Code smell or not, I don't think that Python code as config is that much of 
a problem, for most applications. Yes, the user can insert arbitrary code 
into their config file, and have it run... but it's their user account 
running it, they presumably could just insert that code into a file and run 
it with python regardless. There's (usually) no security implications.

Although I can think of some exceptions. For example, the company I work for 
has a Linux desktop designed for untrusted, hostile users (prisoners in 
jail), and using .py files as config would *definitely* not be allowed for 
that. But I think that's an exceptional case.


> On another dimension, configuration files specifying the behaviour of
> the system are much more useful if their format is easily parsed and
> re-worked by tools the user chooses.
> 
> Your program should not be the only tool (and Python should not be the
> only language) that can read and/or write the configuration data with
> straightfoward data manipulation.

Python is an open standard that anyone can use, or write their own should 
they wish. If you don't like CPython, use Jython or PyPy or 
MyFirstPythonInterpreter to pass the config data. 

A better argument may be that *running arbitrary code* should not be 
required in order to parse a config file. I'm sympathetic to that argument 
too. But if that's your worry, and you absolutely must read a .py config 
file, you could write your own stripped down interpreter of a limited subset 
of the language, and use that. No, it won't be able to determine all the 
config settings from arbitrary .py files, but it may give you a practical 
solution which is good enough for what you need.


> So a complex full-blown programming language like Python is a poor
> choice for configuration data for that reason, too.
> 
> Much better to choose a tightly-defined, limited-scope configuration
> data format that you can be confident any user cannot employ to run
> arbitrary code.

Sure, if your config requirements are met by such a data format.


-- 
Steve

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


Re: GOTCHA with list comprehension

2015-08-05 Thread Marko Rauhamaa
Chris Angelico :

> You can chain 'for' and 'if' clauses as much as you like, and they
> behave exactly the way you'd expect.

How do you know what I'd expect?

I wouldn't know what to expect myself.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GOTCHA with list comprehension

2015-08-05 Thread Chris Angelico
On Wed, Aug 5, 2015 at 7:01 PM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> You can chain 'for' and 'if' clauses as much as you like, and they
>> behave exactly the way you'd expect.
>
> How do you know what I'd expect?
>
> I wouldn't know what to expect myself.

A list comprehension can always be unwound into statement form. [1] For example:

squares = [n*n for n in range(10)]

can be unwound into:

squares = []
for n in range(10):
squares.append(n*n)

You simply take the expression at the beginning and put that into the
append() method call, and then the rest become statements. Here's a
filtered version:

odd_squares = [n*n for n in range(20) if n%2]

Which becomes:

odd_squares = []
for n in range(20):
if n%2:
odd_squares.append(n*n)

So what would you expect nested 'if' clauses to do? Well, they become
nested 'if' statements:

primes = [n for n in range(2,24) if n%2 if n%3]

primes = []
for n in range(2,24):
if n%2:
if n%3:
primes.append(n)

What if we have multiple 'for' loops? Same thing!

ways_to_get_seven = [(a,b) for a in range(1,7) for b in range(1,7) if a+b==7]

ways_to_get_seven = []
for a in range(1,7):
for b in range(1,7):
if a+b==7:
ways_to_get_seven.append((a,b))

No matter what combination of 'if' and 'for' you use, it can be
unwound like this, and it'll always behave the same way. Not sure what
to expect? Follow the simple rules of unwinding, and then read the
code that way.

Of course, if you don't know how to predict what the statement form
will do, then you won't understand what the comprehension will do. But
that's not the comprehension's fault.

peculiar = [a*x*x+b*x+c for a in range(1,10) for b in range(2,30) for
c in range(-3,5) if b*b-4*a*c>=0 if (-b+math.sqrt(b*b-4*a*c))/2/a>0
for x in (x*.01 for x in range(-500,500))]

I suppose you could graph that. Or something. But that's just bad
code, don't blame the list comp for that...

ChrisA

[1] To be technically correct, this unwinding should be done in a
nested function, which you then call. There are some other minor
technicalities too. But it's pretty much this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem in IDLE setup

2015-08-05 Thread Ahsan Chauhan
Respected Sir/Madam,
I would like to bring it to your notice that IDLE's executable file is not
working properly in Python3.5.0. So please look into this.
Regards
Ahsan Chauhan
-- 
https://mail.python.org/mailman/listinfo/python-list


consumer/producer with asyncio

2015-08-05 Thread David Rios
Hello everyone,

I'm trying to implement a producer/consumer using asyncio, but I have
some doubts about the best way to do it. I already have a working
implementation using threads, and want to port it to asyncio for fun,
to learn and also to avoid some problems that I have using threads,
namely that sometimes the script locks and never finish.

What the script have to do is to connect to a server, stream content
from it, and send it for processing as items arrive. The processing
part is to connect to other servers based on the item, send the item
to process and wait for the response.

The threaded version sets a pool of consumer threads that fetch items
from a queue, while on the main thread the queue is populated.

Special requirements that I have are that the script should be
interruptible and should stop cleanly on errors. When I interrupt it
or any part throws an exception for any reason, the script should stop
producing items, the workers should finish the current job and then
stop processing new items, at which point the script should perform
appropriate housekeeping and exit.

I have a mock implementation using asyncio here:
https://gist.github.com/davidrios/011d044b5e7510f085dd

But I think it is quite ugly, particularly all the nesting.

What do you think?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Chris Angelico
On Wed, Aug 5, 2015 at 6:32 PM, Steven D'Aprano
 wrote:
> My own personal feeling is that using code as config is a little
> disquieting. It's a bit of a code smell. Do you really need that much power
> just to allow people to set some configuration settings? Using a Turing
> Complete programming language just to set a bunch of name:value pairs seems
> to be gross overkill.
>
> But on the other hand, config systems do tend to grow in power. People often
> want conditional and computed settings. Rather than invent your own (buggy)
> mini-language to allow conf like this:
>
> if $PLATFORM = 'Windows' set location = 'C:\The Stuff';
> if $PLATFORM = 'Linux' set location = $baselocation + '/thestuff';
>
> using Python seems like a much better idea.

I often have config files where the example is really simple, like this:

https://github.com/Rosuav/LetMeKnow/blob/master/keys_sample.py
https://github.com/Rosuav/Yosemite/blob/master/config.py

but the in-production versions sometimes have a teensy bit more code
in them - maybe an if-expression, maybe pulling something from
os.environ. In both of those cases, any person editing the config file
will also have full power to tamper with the source code, so the
security issue doesn't apply; and apart from requiring quotes around
string values, it's pretty much the level of simplicity you'd get out
of any other config file format. You get comments, you get freedom to
add whitespace anywhere you like (or not add it, if you so desire),
it's great! Right? Well, there is another subtlety, and that's that
there's no error checking. If you duplicate an entry, nobody will
issue even a warning. For small config files, that's fine; but what if
you're editing something the size of postgresql.conf? You can't keep
it all on screen or in your head, and if you simply search the file
for some keyword, would you notice that there are two of them?

So, I'd recommend this if - and ONLY if - the files are normally going
to be small enough to fit on a single page. Otherwise, consider
carefully whether you'd later on want to add error checking like that,
and consider how hard it would be to hack that in.

Drink responsibly.

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


Re: Uninstall

2015-08-05 Thread Mark Lawrence

On 05/08/2015 02:51, Mario Figueiredo wrote:

[chopped to pieces]


On Tue, Aug 4, 2015 at 9:01 PM, Mark Lawrence mailto:breamore...@yahoo.co.uk>> wrote:
On 04/08/2015 19:31, sohcahto...@gmail.com
 wrote:
On Tuesday, August 4, 2015 at 7:29:29 AM UTC-7, Grant Edwards wrote:
On 2015-08-04, milos zorica 

I trust that you also have a good day :)

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Linux script to get most expensive processes

2015-08-05 Thread Thomas 'PointedEars' Lahn
Cecil Westerhof wrote:

> Under Linux I like to get the most expensive processes. The two most
> useful commands are:
> ps -eo pid,user,pcpu,args --sort=-pcpu
> and:
> ps -eo pid,user,pcpu,args --sort=-vsize
> 
> In my case I am only interested in the seven most expensive processes.
> For this I wrote the following script.

Don’t.  Use

  ps -eo pid,user,pcpu,args --sort=-pcpu | head -n 8

or

  ps -eo pid,user,pcpu,args --sort=-pcpu | sed -n '2,8p'

and the like instead.  (procps ps(1) also has an output modifier to omit
the headers, but I could not get that to work with the sorting just now.

[Thanks for pointing out the “--sort” option of *procps* ps(1) 3.3.10.
I was not aware of it, and had used

$ alias cpu
alias cpu='ps -ww aux | sort -nk3 | tail'

instead.]
 
> 
> #!/usr/bin/env python3
> 
> import subprocess
> import sys
> 
> 
> def give_output(param):
> output = subprocess.check_output(([
> 'ps',
> '--columns={0}'   .format(max_line_length),
> '-eo',
> 'pid,user,start_time,{0},args'.format(param),
> '--sort=-{0}' .format(param)
> ])).splitlines()
> […]
> 
> 
> Is this a reasonable way to do this?

No.

> Getting the parameter is done quit[e] simple, but I did not think fancy 
> was necessary here.

It is not.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Uninstall

2015-08-05 Thread Thomas 'PointedEars' Lahn
Mark Lawrence wrote:

> On 04/08/2015 19:31, sohcahto...@gmail.com wrote:
>> I really fucking hate how pedantic some of the people on this mailing
>> list are.
>>
>> milos wasn't wrong.  You just chose to take his message too literally.  I
>> thought it was pretty clear that when milos said "can't", he really meant
>> "shouldn't".
> 
> The simple solution is not to subscribe.

Too late.  The simple solution for them is to _unsubscribe_ now :)

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GOTCHA with list comprehension

2015-08-05 Thread Marko Rauhamaa
Chris Angelico :

> On Wed, Aug 5, 2015 at 7:01 PM, Marko Rauhamaa  wrote:
>> Chris Angelico :
>>
>>> You can chain 'for' and 'if' clauses as much as you like, and they
>>> behave exactly the way you'd expect.
>>
>> How do you know what I'd expect?
>>
>> I wouldn't know what to expect myself.

> [...]

> So what would you expect nested 'if' clauses to do? Well, they become
> nested 'if' statements:
>
> primes = [n for n in range(2,24) if n%2 if n%3]
>
> primes = []
> for n in range(2,24):
> if n%2:
> if n%3:
> primes.append(n)
>
> What if we have multiple 'for' loops? Same thing!

So no need to appeal to my expectations. Better just define it (as you
now did).


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem in IDLE setup

2015-08-05 Thread Mark Lawrence

On 04/08/2015 12:31, Ahsan Chauhan wrote:

Respected Sir/Madam,
I would like to bring it to your notice that IDLE's executable file is
not working properly in Python3.5.0. So please look into this.
Regards
Ahsan Chauhan



Please state exactly what you're tried to do, what you expected to 
happen, and what actually happened.  If you have a traceback please cut 
and paste all of it into your response.  What OS are you using?


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Tim Chase
On 2015-08-02 12:11, Cecil Westerhof wrote:
> There are a lot of ways to store configuration information:
> - conf file
> - xml file
> - database
> - json file
> - and possible a lot of other ways
> 
> I want to write a Python program to display cleaned log files. I do
> not think I need a lot of configuration to be stored:
> - some things relating to the GUI
> - default behaviour
> - default directory
> - log files to display, including some info
>   - At least until where it was displayed
> 
> Because of this I think a human readable file would be best.

Yet another mostly-built-in option is to just have a simple file of
key/value pairs, optionally with comments.  This can be read with
something like

  config = {}
  with open('config.ini') as f:
for row in f:
  row = row.strip()
  if not row or row.startswith(('#', ';')):
continue
  k, _, v = row.partition('=')
  config[k.strip().upper()] = v.lstrip()

which is pretty straight-forward and easy format to edit.

-tkc


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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Rustom Mody
On Wednesday, August 5, 2015 at 6:58:01 PM UTC+5:30, Tim Chase wrote:
> On 2015-08-02 12:11, Cecil Westerhof wrote:
> > There are a lot of ways to store configuration information:
> > - conf file
> > - xml file
> > - database
> > - json file
> > - and possible a lot of other ways
> > 
> > I want to write a Python program to display cleaned log files. I do
> > not think I need a lot of configuration to be stored:
> > - some things relating to the GUI
> > - default behaviour
> > - default directory
> > - log files to display, including some info
> >   - At least until where it was displayed
> > 
> > Because of this I think a human readable file would be best.
> 
> Yet another mostly-built-in option is to just have a simple file of
> key/value pairs, optionally with comments.  This can be read with
> something like
> 
>   config = {}
>   with open('config.ini') as f:
> for row in f:
>   row = row.strip()
>   if not row or row.startswith(('#', ';')):
> continue
>   k, _, v = row.partition('=')
>   config[k.strip().upper()] = v.lstrip()
> 
> which is pretty straight-forward and easy format to edit.
> 
> -tkc

JSON handles basic types like this:
>>> from json import loads
>>> loads("""{"anInt":1, "aString":"2"}""")
{'aString': '2', 'anInt': 1}
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Rustom Mody
On Sunday, August 2, 2015 at 3:44:51 PM UTC+5:30, Cecil Westerhof wrote:
> There are a lot of ways to store configuration information:
> - conf file
> - xml file
> - database
> - json file
> - and possible a lot of other ways

One that I dont think has been mentioned:
ast.literal_eval
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Grant Edwards
On 2015-08-05, Michael Torrie  wrote:
> On 08/04/2015 01:59 PM, Ben Finney wrote:
>> marco.naw...@colosso.nl writes:
>> 
>>> Why not use Python files itself as configuration files?
>> 
>> Because configuration data will be user-editable. (If it's not
>> user-editable, that is itself a poor design choice.)
>> 
>> If you allow executable code to be user-edited, that opens your program
>> to arbitrary injection of executable code. Your program becomes wide
>> open for security exploits, whether through malicious or accidental
>> bugs, and simple human error can lead to arbitrary-scope damage to the
>> user's system.
>
> We need to state the context here.  The only context in which having a
> Python config file is dangerous is when the python program runs as a
> different user/privilege than the owner of the config file.  If the user
> owns the python files as well as the config file then none of this matters.

Yes, it does.

We're not just talking about intentional, malicious damange, we're
also talking about _accidental_ damage caused by an incorrect edit of
a configuration files.

It's much harder to cause damage by mis-editing an "ini" format file
that's parsed with the config file library than it is by mis-editing a
Python file that's imported.

-- 
Grant Edwards   grant.b.edwardsYow! Clear the laundromat!!
  at   This whirl-o-matic just had
  gmail.coma nuclear meltdown!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Steven D'Aprano
On Wed, 5 Aug 2015 11:46 pm, Rustom Mody wrote:

> On Sunday, August 2, 2015 at 3:44:51 PM UTC+5:30, Cecil Westerhof wrote:
>> There are a lot of ways to store configuration information:
>> - conf file
>> - xml file
>> - database
>> - json file
>> - and possible a lot of other ways
> 
> One that I dont think has been mentioned:
> ast.literal_eval


Probably because it doesn't work :-)

py> import ast
py> s = "x = 23"  # pretend I read this line from a file
py> ast.literal_eval(s)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.3/ast.py", line 47, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
  File "/usr/local/lib/python3.3/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
  File "", line 1
x = 23
  ^
SyntaxError: invalid syntax


You might be able to build Yet Another Config Format using literal_eval as a
building block, for evaluating values, but *in and of itself* it isn't a
way to store config information, any more than

int
float

or any other function which takes a string and evaluates it as a Python
primitive or built-in type.


However, there are at least config formats in the standard library which I
believe we've missed: shelve, and plistlib.

help(shelve)

A "shelf" is a persistent, dictionary-like object.  The difference
with dbm databases is that the values (not the keys!) in a shelf can
be essentially arbitrary Python objects -- anything that the "pickle"
module can handle.  This includes most class instances, recursive data
types, and objects containing lots of shared sub-objects.  The keys
are ordinary strings.


help(plistlib)

The property list (.plist) file format is a simple XML pickle supporting
basic object types, like dictionaries, lists, numbers and strings.
Usually the top level object is a dictionary.


-- 
Steven

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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Rustom Mody
On Wednesday, August 5, 2015 at 7:38:46 PM UTC+5:30, Steven D'Aprano wrote:
> On Wed, 5 Aug 2015 11:46 pm, Rustom Mody wrote:
> 
> > On Sunday, August 2, 2015 at 3:44:51 PM UTC+5:30, Cecil Westerhof wrote:
> >> There are a lot of ways to store configuration information:
> >> - conf file
> >> - xml file
> >> - database
> >> - json file
> >> - and possible a lot of other ways
> > 
> > One that I dont think has been mentioned:
> > ast.literal_eval
> 
> 
> Probably because it doesn't work :-)

In the way you describe... yes
Works alright if you give it *literals*

>>> from ast import literal_eval
>>> literal_eval('{"x":"hello", "y":2, "z":3.142}')
{'z': 3.142, 'x': 'hello', 'y': 2}

which is identical the the json.loads behavior
>>> loads('{"x":"hello", "y":2, "z":3.142}')
{'z': 3.142, 'x': 'hello', 'y': 2}

of course the natural method of use would of course get the string from
the open-ing of a config file -- something along the lines

configuration = literal_eval(open("~/.fooconfig"))
-- 
https://mail.python.org/mailman/listinfo/python-list


Is this an example of tail recursion?

2015-08-05 Thread jennyfurtado2
I am trying to learn differences between tail recursion and non tail recursion.

Is the following recursive code tail recursive?
If it is not how to convert it to tail recursion?
If it is how to convert it to non tail recursion?

class CastleDefenseI:
   INFINITY = 9

   def __init__(self):
   self.dpw = 0

   def soldiersVsDefenders(self,soldiers,defenders):
   # soldiers win
   if defenders <=0:
  return 0
   # castle/defenders win
   if soldiers <= 0:
  return self.INFINITY
   
   # do another round of fighting
   # 1. Soldiers kill as many defenders 
   defendersLeft = defenders - soldiers
   # 2. defendersLeft kill as many soldiers
   soldiersLeft = soldiers - defendersLeft   
   return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)

   def oneWave(self,soldiers,defenders,castleHits):
   # castle/defenders wins
   if soldiers <= 0:
   return self.INFINITY
   # castle is dead, let soldiers play against defenders
   if castleHits <= 0:
   defendersLeft = defenders - self.dpw
   return self.soldiersVsDefenders(soldiers,defendersLeft)
   
   # try every possibility:
   # 1) all soldiers hit the castle, none hits the defenders
   # 2) one soldier hits the castle, the others hit the defenders
   # 3) two soldiers hit the castle, the others hit the defenders
   # ...
   # soldiers) no soldier hits the castle, all others hit the 
   # defenders
   mini = self.INFINITY
   for i in range(0,soldiers):
   if i > defenders:
break
   soldiersLeft = soldiers - (defenders -i)
   defendersLeft = defenders - i + self.dpw
   castleHitsLeft = castleHits - (soldiers -i)
   mini = min(mini,1 + 
self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
   return mini
  
   def playGame(self,soldiers,castleHits,defendersPerWave):
   self.dpw = defendersPerWave
   numWaves = self.oneWave(soldiers,0,castleHits)
   if numWaves >= self.INFINITY:
  return -1
   else:
  return numWaves
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an example of tail recursion?

2015-08-05 Thread Rustom Mody
On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com wrote:
> I am trying to learn differences between tail recursion and non tail 
> recursion.
> 
> Is the following recursive code tail recursive?
> If it is not how to convert it to tail recursion?
> If it is how to convert it to non tail recursion?
> 
> class CastleDefenseI:
>INFINITY = 9
> 
>def __init__(self):
>self.dpw = 0
> 
>def soldiersVsDefenders(self,soldiers,defenders):
># soldiers win
>if defenders <=0:
>   return 0
># castle/defenders win
>if soldiers <= 0:
>   return self.INFINITY
>
># do another round of fighting
># 1. Soldiers kill as many defenders 
>defendersLeft = defenders - soldiers
># 2. defendersLeft kill as many soldiers
>soldiersLeft = soldiers - defendersLeft   
>return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)

Yes it *looks* tail recursive
However if you rewrite 1 + x as 1 .__add__(x) you get
return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))

Now you can see its not tail recursive
I guess the same applies to the other functions

> 
>def oneWave(self,soldiers,defenders,castleHits):
># castle/defenders wins
>if soldiers <= 0:
>return self.INFINITY
># castle is dead, let soldiers play against defenders
>if castleHits <= 0:
>defendersLeft = defenders - self.dpw
>return self.soldiersVsDefenders(soldiers,defendersLeft)
>
># try every possibility:
># 1) all soldiers hit the castle, none hits the defenders
># 2) one soldier hits the castle, the others hit the defenders
># 3) two soldiers hit the castle, the others hit the defenders
># ...
># soldiers) no soldier hits the castle, all others hit the 
># defenders
>mini = self.INFINITY
>for i in range(0,soldiers):
>if i > defenders:
> break
>soldiersLeft = soldiers - (defenders -i)
>defendersLeft = defenders - i + self.dpw
>castleHitsLeft = castleHits - (soldiers -i)
>mini = min(mini,1 + 
> self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
>return mini
>   
>def playGame(self,soldiers,castleHits,defendersPerWave):
>self.dpw = defendersPerWave
>numWaves = self.oneWave(soldiers,0,castleHits)
>if numWaves >= self.INFINITY:
>   return -1
>else:
>   return numWaves

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


Re: Is this an example of tail recursion?

2015-08-05 Thread jennyfurtado2
On Wednesday, August 5, 2015 at 9:21:33 AM UTC-6, Rustom Mody wrote:
> On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com 
> wrote:
> > I am trying to learn differences between tail recursion and non tail 
> > recursion.
> > 
> > Is the following recursive code tail recursive?
> > If it is not how to convert it to tail recursion?
> > If it is how to convert it to non tail recursion?
> > 
> > class CastleDefenseI:
> >INFINITY = 9
> > 
> >def __init__(self):
> >self.dpw = 0
> > 
> >def soldiersVsDefenders(self,soldiers,defenders):
> ># soldiers win
> >if defenders <=0:
> >   return 0
> ># castle/defenders win
> >if soldiers <= 0:
> >   return self.INFINITY
> >
> ># do another round of fighting
> ># 1. Soldiers kill as many defenders 
> >defendersLeft = defenders - soldiers
> ># 2. defendersLeft kill as many soldiers
> >soldiersLeft = soldiers - defendersLeft   
> >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> 
> Yes it *looks* tail recursive
> However if you rewrite 1 + x as 1 .__add__(x) you get
> return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))
> 
> Now you can see its not tail recursive
> I guess the same applies to the other functions
> 
> > 
> >def oneWave(self,soldiers,defenders,castleHits):
> ># castle/defenders wins
> >if soldiers <= 0:
> >return self.INFINITY
> ># castle is dead, let soldiers play against defenders
> >if castleHits <= 0:
> >defendersLeft = defenders - self.dpw
> >return self.soldiersVsDefenders(soldiers,defendersLeft)
> >
> ># try every possibility:
> ># 1) all soldiers hit the castle, none hits the defenders
> ># 2) one soldier hits the castle, the others hit the defenders
> ># 3) two soldiers hit the castle, the others hit the defenders
> ># ...
> ># soldiers) no soldier hits the castle, all others hit the 
> ># defenders
> >mini = self.INFINITY
> >for i in range(0,soldiers):
> >if i > defenders:
> > break
> >soldiersLeft = soldiers - (defenders -i)
> >defendersLeft = defenders - i + self.dpw
> >castleHitsLeft = castleHits - (soldiers -i)
> >mini = min(mini,1 + 
> > self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
> >return mini
> >   
> >def playGame(self,soldiers,castleHits,defendersPerWave):
> >self.dpw = defendersPerWave
> >numWaves = self.oneWave(soldiers,0,castleHits)
> >if numWaves >= self.INFINITY:
> >   return -1
> >else:
> >   return numWaves



On Wednesday, August 5, 2015 at 9:21:33 AM UTC-6, Rustom Mody wrote:
> On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com 
> wrote:
> > I am trying to learn differences between tail recursion and non tail 
> > recursion.
> > 
> > Is the following recursive code tail recursive?
> > If it is not how to convert it to tail recursion?
> > If it is how to convert it to non tail recursion?
> > 
> > class CastleDefenseI:
> >INFINITY = 9
> > 
> >def __init__(self):
> >self.dpw = 0
> > 
> >def soldiersVsDefenders(self,soldiers,defenders):
> ># soldiers win
> >if defenders <=0:
> >   return 0
> ># castle/defenders win
> >if soldiers <= 0:
> >   return self.INFINITY
> >
> ># do another round of fighting
> ># 1. Soldiers kill as many defenders 
> >defendersLeft = defenders - soldiers
> ># 2. defendersLeft kill as many soldiers
> >soldiersLeft = soldiers - defendersLeft   
> >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> 
> Yes it *looks* tail recursive
> However if you rewrite 1 + x as 1 .__add__(x) you get
> return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))
> 
> Now you can see its not tail recursive
> I guess the same applies to the other functions
> 
> > 
> >def oneWave(self,soldiers,defenders,castleHits):
> ># castle/defenders wins
> >if soldiers <= 0:
> >return self.INFINITY
> ># castle is dead, let soldiers play against defenders
> >if castleHits <= 0:
> >defendersLeft = defenders - self.dpw
> >return self.soldiersVsDefenders(soldiers,defendersLeft)
> >
> ># try every possibility:
> ># 1) all soldiers hit the castle, none hits the defenders
> ># 2) one soldier hits the castle, the others hit the defenders
> ># 3) two soldiers hit the castle, the others hit the defenders
> ># ...
> ># soldiers) no soldier hits the castle, all others hit the 
> ># defenders
> >mini = self.INFINITY
> >  

Re: Is this an example of tail recursion?

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 1:13 AM,   wrote:
> I am trying to learn differences between tail recursion and non tail 
> recursion.

Tail recursion is where you do exactly this:

return some_function(...)

Absolutely nothing is allowed to happen around or after that function,
and that also means you can't do that inside a try/except/finally
block, nor a with block, etc, etc, etc. It has to be nothing more than
a function call, and you return the exact result of that call.

> Is the following recursive code tail recursive?
> If it is not how to convert it to tail recursion?
> If it is how to convert it to non tail recursion?
>
>def __init__(self):
>self.dpw = 0

Not tail recursive - not recursive - doesn't call anything. Trivial case. :)

>def soldiersVsDefenders(self,soldiers,defenders):
># soldiers win
>if defenders <=0:
>   return 0
># castle/defenders win
>if soldiers <= 0:
>   return self.INFINITY

In these cases, equally trivial - not recursive in any form.

># do another round of fighting
># 1. Soldiers kill as many defenders
>defendersLeft = defenders - soldiers
># 2. defendersLeft kill as many soldiers
>soldiersLeft = soldiers - defendersLeft

(Interesting that the attacking soldiers get the first strike.)

>return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)

This is NOT tail recursion, because you add 1 at the end of it. The
way to make it tail recursive would be to add another argument to the
function, which it would keep adding to; whenever it returns, you
would add the accumulator to the return value:

def soldiersVsDefenders(self,soldiers,defenders, accum=0):
if defenders <= 0:
return 0 + accum
if soldiers <= 0:
return self.INFINITY + accum
# other code goes here
return self.soldiersVsDefenders(soldiersLeft,defendersLeft,1+accum)

Now it's tail recursive. If this looks ugly, it's because it is; tail
recursion often isn't worth the effort.

Note that, as far as Python's concerned, this is a tail call, but
isn't necessarily *recursion* (which implies that you somehow know
you're calling the same function). If someone subclasses your code and
overrides this method, your code will call the subclass's version - if
the subclass calls through to super(), you'll end up with mutual
recursion, but still not a simple case of tail recursion. However, you
could choose to ignore this possibility and manually convert this into
iteration:

def soldiersVsDefenders(self,soldiers,defenders):
rounds = 0
while soldiers and defenders:
# do another round of fighting
# 1. Soldiers kill as many defenders
defendersLeft = defenders - soldiers
# 2. defendersLeft kill as many soldiers
soldiersLeft = soldiers - defendersLeft
rounds += 1
if defenders <= 0:
return rounds
return self.INFINITY + rounds

How's that look? Better? Worse?

On to the next function.

>def oneWave(self,soldiers,defenders,castleHits):
># castle is dead, let soldiers play against defenders
>if castleHits <= 0:
>defendersLeft = defenders - self.dpw
>return self.soldiersVsDefenders(soldiers,defendersLeft)

This is a tail call. It's not tail *recursion* because you're calling
a completely different function, but you are indeed calling another
function and directly returning its return value.

>mini = self.INFINITY
>for i in range(0,soldiers):
>if i > defenders:
> break
>soldiersLeft = soldiers - (defenders -i)
>defendersLeft = defenders - i + self.dpw
>castleHitsLeft = castleHits - (soldiers -i)
>mini = min(mini,1 + 
> self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
>return mini

Not sure what the point of all this is, but sure. This clearly isn't
tail recursion, though, as it's doing a whole lot of other work after
the recursive call. Having a loop and recursion like this is pretty
scary, but in terms of "is this or isn't this tail recursive", it's
pretty clear.

>def playGame(self,soldiers,castleHits,defendersPerWave):
>self.dpw = defendersPerWave
>numWaves = self.oneWave(soldiers,0,castleHits)
>if numWaves >= self.INFINITY:
>   return -1
>else:
>   return numWaves

And this is, again, no tail call. If the trap for INFINITY becoming -1
were inside oneWave(), then this could be turned into a tail call, but
as it is, there's more work to be done after the other function
returns, so it's not a tail call.

Hope that helps!

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


Re: Is this an example of tail recursion?

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 1:37 AM,   wrote:
> Sorry I am missing a subtle point: Isnt 1+ self.soldiersVsDefenders... ending 
> up calling 1.__add__(self.soldiersVsDefenders...)?

I think his point is that it is, in effect, doing that; but honestly,
calling this a tail call into the int+int addition function is pretty
pointless. I mean, sure, it's technically a sort of tail call, but
it's definitely not tail recursion, and it's such a trivial operation
(adding one to a probably-small number) that it's hardly even worth
mentioning. The main point of tail recursion is how it interacts with
the self-call, and that's not the tail call here.

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


Re: Is this an example of tail recursion?

2015-08-05 Thread jenny
On Wednesday, August 5, 2015 at 9:52:14 AM UTC-6, Chris Angelico wrote:
> On Thu, Aug 6, 2015 at 1:13 AM,   wrote:
> > I am trying to learn differences between tail recursion and non tail 
> > recursion.
> 
> Tail recursion is where you do exactly this:
> 
> return some_function(...)
> 
> Absolutely nothing is allowed to happen around or after that function,
> and that also means you can't do that inside a try/except/finally
> block, nor a with block, etc, etc, etc. It has to be nothing more than
> a function call, and you return the exact result of that call.
> 
> > Is the following recursive code tail recursive?
> > If it is not how to convert it to tail recursion?
> > If it is how to convert it to non tail recursion?
> >
> >def __init__(self):
> >self.dpw = 0
> 
> Not tail recursive - not recursive - doesn't call anything. Trivial case. :)
> 
> >def soldiersVsDefenders(self,soldiers,defenders):
> ># soldiers win
> >if defenders <=0:
> >   return 0
> ># castle/defenders win
> >if soldiers <= 0:
> >   return self.INFINITY
> 
> In these cases, equally trivial - not recursive in any form.
> 
> ># do another round of fighting
> ># 1. Soldiers kill as many defenders
> >defendersLeft = defenders - soldiers
> ># 2. defendersLeft kill as many soldiers
> >soldiersLeft = soldiers - defendersLeft
> 
> (Interesting that the attacking soldiers get the first strike.)
> 
> >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> 
> This is NOT tail recursion, because you add 1 at the end of it. The
> way to make it tail recursive would be to add another argument to the
> function, which it would keep adding to; whenever it returns, you
> would add the accumulator to the return value:
> 
> def soldiersVsDefenders(self,soldiers,defenders, accum=0):
> if defenders <= 0:
> return 0 + accum
> if soldiers <= 0:
> return self.INFINITY + accum
> # other code goes here
> return self.soldiersVsDefenders(soldiersLeft,defendersLeft,1+accum)
> 
> Now it's tail recursive. If this looks ugly, it's because it is; tail
> recursion often isn't worth the effort.
> 
> Note that, as far as Python's concerned, this is a tail call, but
> isn't necessarily *recursion* (which implies that you somehow know
> you're calling the same function). If someone subclasses your code and
> overrides this method, your code will call the subclass's version - if
> the subclass calls through to super(), you'll end up with mutual
> recursion, but still not a simple case of tail recursion. However, you
> could choose to ignore this possibility and manually convert this into
> iteration:
> 
> def soldiersVsDefenders(self,soldiers,defenders):
> rounds = 0
> while soldiers and defenders:
> # do another round of fighting
> # 1. Soldiers kill as many defenders
> defendersLeft = defenders - soldiers
> # 2. defendersLeft kill as many soldiers
> soldiersLeft = soldiers - defendersLeft
> rounds += 1
> if defenders <= 0:
> return rounds
> return self.INFINITY + rounds
> 
> How's that look? Better? Worse?
> 
> On to the next function.
> 
> >def oneWave(self,soldiers,defenders,castleHits):
> ># castle is dead, let soldiers play against defenders
> >if castleHits <= 0:
> >defendersLeft = defenders - self.dpw
> >return self.soldiersVsDefenders(soldiers,defendersLeft)
> 
> This is a tail call. It's not tail *recursion* because you're calling
> a completely different function, but you are indeed calling another
> function and directly returning its return value.
> 
> >mini = self.INFINITY
> >for i in range(0,soldiers):
> >if i > defenders:
> > break
> >soldiersLeft = soldiers - (defenders -i)
> >defendersLeft = defenders - i + self.dpw
> >castleHitsLeft = castleHits - (soldiers -i)
> >mini = min(mini,1 + 
> > self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
> >return mini
> 
> Not sure what the point of all this is, but sure. This clearly isn't
> tail recursion, though, as it's doing a whole lot of other work after
> the recursive call. Having a loop and recursion like this is pretty
> scary, but in terms of "is this or isn't this tail recursive", it's
> pretty clear.
> 
> >def playGame(self,soldiers,castleHits,defendersPerWave):
> >self.dpw = defendersPerWave
> >numWaves = self.oneWave(soldiers,0,castleHits)
> >if numWaves >= self.INFINITY:
> >   return -1
> >else:
> >   return numWaves
> 
> And this is, again, no tail call. If the trap for INFINITY becoming -1
> were inside oneWave(), then this could be turned into a tail call, but
> as it is, there's more work to be done after the other function
> returns, so it's not a tail call.
> 
> Hope that h

Re: Is this an example of tail recursion?

2015-08-05 Thread Rustom Mody
On Wednesday, August 5, 2015 at 9:07:52 PM UTC+5:30, jennyf...@gmail.com wrote:
> On Wednesday, August 5, 2015 at 9:21:33 AM UTC-6, Rustom Mody wrote:
> > On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com 
> > wrote:
> > > I am trying to learn differences between tail recursion and non tail 
> > > recursion.
> > > 
> > > Is the following recursive code tail recursive?
> > > If it is not how to convert it to tail recursion?
> > > If it is how to convert it to non tail recursion?
> > > 
> > > class CastleDefenseI:
> > >INFINITY = 9
> > > 
> > >def __init__(self):
> > >self.dpw = 0
> > > 
> > >def soldiersVsDefenders(self,soldiers,defenders):
> > ># soldiers win
> > >if defenders <=0:
> > >   return 0
> > ># castle/defenders win
> > >if soldiers <= 0:
> > >   return self.INFINITY
> > >
> > ># do another round of fighting
> > ># 1. Soldiers kill as many defenders 
> > >defendersLeft = defenders - soldiers
> > ># 2. defendersLeft kill as many soldiers
> > >soldiersLeft = soldiers - defendersLeft   
> > >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> > 
> > Yes it *looks* tail recursive
> > However if you rewrite 1 + x as 1 .__add__(x) you get
> > return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))
> > 
> > Now you can see its not tail recursive
> > I guess the same applies to the other functions
> > 
> > > 
> > >def oneWave(self,soldiers,defenders,castleHits):
> > ># castle/defenders wins
> > >if soldiers <= 0:
> > >return self.INFINITY
> > ># castle is dead, let soldiers play against defenders
> > >if castleHits <= 0:
> > >defendersLeft = defenders - self.dpw
> > >return self.soldiersVsDefenders(soldiers,defendersLeft)
> > >
> > ># try every possibility:
> > ># 1) all soldiers hit the castle, none hits the defenders
> > ># 2) one soldier hits the castle, the others hit the defenders
> > ># 3) two soldiers hit the castle, the others hit the defenders
> > ># ...
> > ># soldiers) no soldier hits the castle, all others hit the 
> > ># defenders
> > >mini = self.INFINITY
> > >for i in range(0,soldiers):
> > >if i > defenders:
> > > break
> > >soldiersLeft = soldiers - (defenders -i)
> > >defendersLeft = defenders - i + self.dpw
> > >castleHitsLeft = castleHits - (soldiers -i)
> > >mini = min(mini,1 + 
> > > self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
> > >return mini
> > >   
> > >def playGame(self,soldiers,castleHits,defendersPerWave):
> > >self.dpw = defendersPerWave
> > >numWaves = self.oneWave(soldiers,0,castleHits)
> > >if numWaves >= self.INFINITY:
> > >   return -1
> > >else:
> > >   return numWaves
> 
> 
> 
> On Wednesday, August 5, 2015 at 9:21:33 AM UTC-6, Rustom Mody wrote:
> > On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com 
> > wrote:
> > > I am trying to learn differences between tail recursion and non tail 
> > > recursion.
> > > 
> > > Is the following recursive code tail recursive?
> > > If it is not how to convert it to tail recursion?
> > > If it is how to convert it to non tail recursion?
> > > 
> > > class CastleDefenseI:
> > >INFINITY = 9
> > > 
> > >def __init__(self):
> > >self.dpw = 0
> > > 
> > >def soldiersVsDefenders(self,soldiers,defenders):
> > ># soldiers win
> > >if defenders <=0:
> > >   return 0
> > ># castle/defenders win
> > >if soldiers <= 0:
> > >   return self.INFINITY
> > >
> > ># do another round of fighting
> > ># 1. Soldiers kill as many defenders 
> > >defendersLeft = defenders - soldiers
> > ># 2. defendersLeft kill as many soldiers
> > >soldiersLeft = soldiers - defendersLeft   
> > >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> > 
> > Yes it *looks* tail recursive
> > However if you rewrite 1 + x as 1 .__add__(x) you get
> > return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))
> > 
> > Now you can see its not tail recursive
> > I guess the same applies to the other functions
> > 
> > > 
> > >def oneWave(self,soldiers,defenders,castleHits):
> > ># castle/defenders wins
> > >if soldiers <= 0:
> > >return self.INFINITY
> > ># castle is dead, let soldiers play against defenders
> > >if castleHits <= 0:
> > >defendersLeft = defenders - self.dpw
> > >return self.soldiersVsDefenders(soldiers,defendersLeft)
> > >
> > ># try every possibility:
> > ># 1) all soldiers hit 

Re: Is this an example of tail recursion?

2015-08-05 Thread jennyfurtado2
On Wednesday, August 5, 2015 at 10:10:22 AM UTC-6, Rustom Mody wrote:
> On Wednesday, August 5, 2015 at 9:07:52 PM UTC+5:30, jennyf...@gmail.com 
> wrote:
> > On Wednesday, August 5, 2015 at 9:21:33 AM UTC-6, Rustom Mody wrote:
> > > On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com 
> > > wrote:
> > > > I am trying to learn differences between tail recursion and non tail 
> > > > recursion.
> > > > 
> > > > Is the following recursive code tail recursive?
> > > > If it is not how to convert it to tail recursion?
> > > > If it is how to convert it to non tail recursion?
> > > > 
> > > > class CastleDefenseI:
> > > >INFINITY = 9
> > > > 
> > > >def __init__(self):
> > > >self.dpw = 0
> > > > 
> > > >def soldiersVsDefenders(self,soldiers,defenders):
> > > ># soldiers win
> > > >if defenders <=0:
> > > >   return 0
> > > ># castle/defenders win
> > > >if soldiers <= 0:
> > > >   return self.INFINITY
> > > >
> > > ># do another round of fighting
> > > ># 1. Soldiers kill as many defenders 
> > > >defendersLeft = defenders - soldiers
> > > ># 2. defendersLeft kill as many soldiers
> > > >soldiersLeft = soldiers - defendersLeft   
> > > >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> > > 
> > > Yes it *looks* tail recursive
> > > However if you rewrite 1 + x as 1 .__add__(x) you get
> > > return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))
> > > 
> > > Now you can see its not tail recursive
> > > I guess the same applies to the other functions
> > > 
> > > > 
> > > >def oneWave(self,soldiers,defenders,castleHits):
> > > ># castle/defenders wins
> > > >if soldiers <= 0:
> > > >return self.INFINITY
> > > ># castle is dead, let soldiers play against defenders
> > > >if castleHits <= 0:
> > > >defendersLeft = defenders - self.dpw
> > > >return self.soldiersVsDefenders(soldiers,defendersLeft)
> > > >
> > > ># try every possibility:
> > > ># 1) all soldiers hit the castle, none hits the defenders
> > > ># 2) one soldier hits the castle, the others hit the defenders
> > > ># 3) two soldiers hit the castle, the others hit the defenders
> > > ># ...
> > > ># soldiers) no soldier hits the castle, all others hit the 
> > > ># defenders
> > > >mini = self.INFINITY
> > > >for i in range(0,soldiers):
> > > >if i > defenders:
> > > > break
> > > >soldiersLeft = soldiers - (defenders -i)
> > > >defendersLeft = defenders - i + self.dpw
> > > >castleHitsLeft = castleHits - (soldiers -i)
> > > >mini = min(mini,1 + 
> > > > self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
> > > >return mini
> > > >   
> > > >def playGame(self,soldiers,castleHits,defendersPerWave):
> > > >self.dpw = defendersPerWave
> > > >numWaves = self.oneWave(soldiers,0,castleHits)
> > > >if numWaves >= self.INFINITY:
> > > >   return -1
> > > >else:
> > > >   return numWaves
> > 
> > 
> > 
> > On Wednesday, August 5, 2015 at 9:21:33 AM UTC-6, Rustom Mody wrote:
> > > On Wednesday, August 5, 2015 at 8:43:31 PM UTC+5:30, jennyf...@gmail.com 
> > > wrote:
> > > > I am trying to learn differences between tail recursion and non tail 
> > > > recursion.
> > > > 
> > > > Is the following recursive code tail recursive?
> > > > If it is not how to convert it to tail recursion?
> > > > If it is how to convert it to non tail recursion?
> > > > 
> > > > class CastleDefenseI:
> > > >INFINITY = 9
> > > > 
> > > >def __init__(self):
> > > >self.dpw = 0
> > > > 
> > > >def soldiersVsDefenders(self,soldiers,defenders):
> > > ># soldiers win
> > > >if defenders <=0:
> > > >   return 0
> > > ># castle/defenders win
> > > >if soldiers <= 0:
> > > >   return self.INFINITY
> > > >
> > > ># do another round of fighting
> > > ># 1. Soldiers kill as many defenders 
> > > >defendersLeft = defenders - soldiers
> > > ># 2. defendersLeft kill as many soldiers
> > > >soldiersLeft = soldiers - defendersLeft   
> > > >return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)
> > > 
> > > Yes it *looks* tail recursive
> > > However if you rewrite 1 + x as 1 .__add__(x) you get
> > > return 1 .__add__(self.soldiersVsDefenders(soldiersLeft,defendersLeft))
> > > 
> > > Now you can see its not tail recursive
> > > I guess the same applies to the other functions
> > > 
> > > > 
> > > >def oneWave(self,soldiers,defenders,castleHits):
> > > ># castle/defenders wins
> > > >if soldiers <= 0:
> > > >return self.INFINITY
>

Re: Is this an example of tail recursion?

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 2:10 AM, Rustom Mody  wrote:
> 1 + x
> does not *call* 1 .__add__(x)
> It *is* that
> [Barring corner cases of radd etc]
> IOW I am desugaring the syntax into explicit method-calls so you can see
> all the calls explicitly
> Then it becomes evident -- visibly and in fact --that the tail call is the
> __add__ method not the solderdiersVsDefenders

Except that it *isn't* that, precisely because of those other cases.
When Python sees an expression like "1 + x" and doesn't yet know what
x is, it can't do anything other than record the fact that there'll be
a BINARY_ADD of the integer 1 and whatever that thing is. That object
might well define __radd__, so the call is most definitely not
equivalent to the operator.

And the ultimate result of that addition might not even be a function
call at all, if it's implemented in C. Or if you're running in PyPy
and the optimizer turned it into machine code. So no, even though you
can define addition for *your own classes* using __add__ or __radd__,
you can't reinterpret every addition as a function call.

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


How to trace the recursive path?

2015-08-05 Thread jennyfurtado2
Consider this code (shown in my previous post)

class CastleDefenseI:
   INFINITY = 9

   def __init__(self):
   self.dpw = 0

   def soldiersVsDefenders(self,soldiers,defenders):
   # soldiers win
   if defenders <=0:
  return 0
   # castle/defenders win
   if soldiers <= 0:
  return self.INFINITY
   
   # do another round of fighting
   # 1. Soldiers kill as many defenders 
   defendersLeft = defenders - soldiers
   # 2. defendersLeft kill as many soldiers
   soldiersLeft = soldiers - defendersLeft   
   return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft)

   def oneWave(self,soldiers,defenders,castleHits):
   # castle/defenders wins
   if soldiers <= 0:
   return self.INFINITY
   # castle is dead, let soldiers play against defenders
   if castleHits <= 0:
   defendersLeft = defenders - self.dpw
   return self.soldiersVsDefenders(soldiers,defendersLeft)
   
   # try every possibility:
   # 1) all soldiers hit the castle, none hits the defenders
   # 2) one soldier hits the castle, the others hit the defenders
   # 3) two soldiers hit the castle, the others hit the defenders
   # ...
   # soldiers) no soldier hits the castle, all others hit the 
   # defenders
   mini = self.INFINITY
   for i in range(0,soldiers):
   if i > defenders:
break
   soldiersLeft = soldiers - (defenders -i)
   defendersLeft = defenders - i + self.dpw
   castleHitsLeft = castleHits - (soldiers -i)
   mini = min(mini,1 + 
self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft))
   return mini
  
   def playGame(self,soldiers,castleHits,defendersPerWave):
   self.dpw = defendersPerWave
   numWaves = self.oneWave(soldiers,0,castleHits)
   if numWaves >= self.INFINITY:
  return -1
   else:
  return numWaves
  
solution = CastleDefenseI()
gameSoldiers = 10
castleHits = 11
defendersPerWave = 15

#prints 4
print solution.playGame(gameSoldiers, castleHits, defendersPerWave)

How would I print the path that leads to   
if soldiers <= 0 (in oneWave and soldiersVsDefenders)
if defenders<=0 (in soldiersVsDefenders)

I have started going down this direction (incorrect)

class CastleDefenseI:
   INFINITY = 9

   def __init__(self):
   self.dpw = 0

   def soldiersVsDefenders(self,soldiers,defenders,path):
   # soldiers win
   if defenders <=0:
  return 0
   # castle/defenders win
   if soldiers <= 0:
  return self.INFINITY
   
   # do another round of fighting
   # 1. Soldiers kill as many defenders 
   defendersLeft = defenders - soldiers
   # 2. defendersLeft kill as many soldiers
   soldiersLeft = soldiers - defendersLeft 
   path.append({"soldiers":soldiersLeft,"defenders":defendersLeft,
   "castleHits":0})
   return 1 + self.soldiersVsDefenders(soldiersLeft,defendersLeft,path)

   def oneWave(self,soldiers,defenders,castleHits,path):
   # castle/defenders wins
   if soldiers <= 0:
   print path
   return self.INFINITY
   # castle is dead, let soldiers play against defenders
   if castleHits <= 0:
   defendersLeft = defenders - self.dpw
   
path.append({"soldiers":soldiers,"defenders":defendersLeft,"castleHits":0})
   return self.soldiersVsDefenders(soldiers,defendersLeft,path)
   
   # try every possibility:
   # 1) all soldiers hit the castle, none hits the defenders
   # 2) one soldier hits the castle, the others hit the defenders
   # 3) two soldiers hit the castle, the others hit the defenders
   # ...
   # soldiers) no soldier hits the castle, all others hit the 
   # defenders
   mini = self.INFINITY
   for i in range(0,soldiers):
   if i > defenders:
break
   soldiersLeft = soldiers - (defenders -i)
   defendersLeft = defenders - i + self.dpw
   castleHitsLeft = castleHits - (soldiers -i)
   path = []
   
path.append({"soldiers":soldiersLeft,"defenders":defendersLeft,"castleHits":castleHitsLeft})
   mini = min(mini,1 + 
self.oneWave(soldiersLeft,defendersLeft,castleHitsLeft,path))
  
   return mini
  
   def playGame(self,soldiers,castleHits,defendersPerWave):
   self.dpw = defendersPerWave
   numWaves = self.oneWave(soldiers,0,castleHits,[])
   if numWaves >= self.INFINITY:
  return -1
   else:
  return numWaves
  
solution = CastleDefenseI()
gameSoldiers = 10
castleHits = 11
defendersPerWave = 15

#prints 4
print solution.playGame(gameSoldiers, castleHits, defendersPerWave)

   


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


Re: Is this an example of tail recursion?

2015-08-05 Thread jennyfurtado2
On Wednesday, August 5, 2015 at 10:29:21 AM UTC-6, Chris Angelico wrote:
> On Thu, Aug 6, 2015 at 2:10 AM, Rustom Mody  wrote:
> > 1 + x
> > does not *call* 1 .__add__(x)
> > It *is* that
> > [Barring corner cases of radd etc]
> > IOW I am desugaring the syntax into explicit method-calls so you can see
> > all the calls explicitly
> > Then it becomes evident -- visibly and in fact --that the tail call is the
> > __add__ method not the solderdiersVsDefenders
> 
> Except that it *isn't* that, precisely because of those other cases.
> When Python sees an expression like "1 + x" and doesn't yet know what
> x is, it can't do anything other than record the fact that there'll be
> a BINARY_ADD of the integer 1 and whatever that thing is. That object
> might well define __radd__, so the call is most definitely not
> equivalent to the operator.
> 
> And the ultimate result of that addition might not even be a function
> call at all, if it's implemented in C. Or if you're running in PyPy
> and the optimizer turned it into machine code. So no, even though you
> can define addition for *your own classes* using __add__ or __radd__,
> you can't reinterpret every addition as a function call.
> 
> ChrisA

Good (intricate) point. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an example of tail recursion?

2015-08-05 Thread Rustom Mody
On Wednesday, August 5, 2015 at 10:11:30 PM UTC+5:30,  wrote:
> On Wednesday, August 5, 2015 at 10:29:21 AM UTC-6, Chris Angelico wrote:
> > On Thu, Aug 6, 2015 at 2:10 AM, Rustom Mody  wrote:
> > > 1 + x
> > > does not *call* 1 .__add__(x)
> > > It *is* that
> > > [Barring corner cases of radd etc]
> > > IOW I am desugaring the syntax into explicit method-calls so you can see
> > > all the calls explicitly
> > > Then it becomes evident -- visibly and in fact --that the tail call is the
> > > __add__ method not the solderdiersVsDefenders
> > 
> > Except that it *isn't* that, precisely because of those other cases.
> > When Python sees an expression like "1 + x" and doesn't yet know what
> > x is, it can't do anything other than record the fact that there'll be
> > a BINARY_ADD of the integer 1 and whatever that thing is. That object
> > might well define __radd__, so the call is most definitely not
> > equivalent to the operator.
> > 
> > And the ultimate result of that addition might not even be a function
> > call at all, if it's implemented in C. Or if you're running in PyPy
> > and the optimizer turned it into machine code. So no, even though you
> > can define addition for *your own classes* using __add__ or __radd__,
> > you can't reinterpret every addition as a function call.
> > 
> > ChrisA
> 
> Good (intricate) point.

And I continue to have no idea what Chris is talking about.
Here is C printf
>>> from ctypes import *
>>> cdll.LoadLibrary("libc.so.6")
>>> libc = CDLL("libc.so.6")
>>> libc.printf(b"%s", b"Hello")
5
Hello>>> 

As far as I can see printf is a C function and its behaving like (an 
ill-behaved) python function as well.
Likewise for anything else written ina C extension module
Or a C-builtin

If its callable from within python its python
That it may also be C seems to me beside the point
[As I said I dont get the point]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is this an example of tail recursion?

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 2:51 AM, Rustom Mody  wrote:
> And I continue to have no idea what Chris is talking about.
> Here is C printf
 from ctypes import *
 cdll.LoadLibrary("libc.so.6")
 libc = CDLL("libc.so.6")
 libc.printf(b"%s", b"Hello")
> 5
> Hello>>>
>
> As far as I can see printf is a C function and its behaving like (an
> ill-behaved) python function as well.
> Likewise for anything else written ina C extension module
> Or a C-builtin
>
> If its callable from within python its python
> That it may also be C seems to me beside the point
> [As I said I dont get the point]

Sure, if it's callable from within Python. Where is this implemented in CPython?

def f(x): return x+2

f(1)

There's PyNumber_Add() in abstract.c, which looks for the nb_add slot.
That contains a pointer to long_add, which is defined in longobject.c.
Is that the same thing as (1).__add__? Not really, but that's kinda
what implements the underlying operation. Also, the function is
declared as 'static', so I don't think you can find it using ctypes.

Adding two Python objects is *not* a function call. It is an
operator-controlled action. It's very similar, in many ways, to a
method call, but it isn't exactly that, and it certainly isn't the
sort of thing that you could tail-call-optimize as the concept applies
only to cases where you can actually replace a stack frame.

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


Installation Successful, but pythonw and idle doesn't function

2015-08-05 Thread Rick Smith


I was able to install various versions of Python (3.5.0b4 32bit being 
the most recent) multiple times (uninstalling in between) and they 
worked ("python --version" at the command line worked).


However pythonw.exe did not and does not work. I was simply returned to 
the command prompt, without ANY interaction or error.


  prompt>pythonw

  prompt>

I also attempted to run "idle", with the following results:

  C:\Users\judy\AppData\Local\Programs\Python\Python35-32\Lib\idlelib>idle.py
  ** IDLE can't import Tkinter.
  Your Python may not be configured for Tk. **

I finally attempted the installation using ONLY the default 
installation choice and got the same results. I had attempted previous 
installations using a "custom" configuration each time, also with the 
SAME results.


There was an older version (2.3.5) installed from a CD from a Python 
book, but this was uninstalled as well. (I believe that version did 
work with pythonw.)


I have checked online for answers and they involve adding paths to the 
"Path" environmental variable, moving libraries or deleting files in 
the ".idlerc" directory. I have attempted ALL of the. One mentioned 
using the ActiveState version, which I will try later.


So, I am at a complete loss -- I am running Windows Visa Home Premium 
32 bit, but I did not see any "incompatibility" with Vista mentioned. I 
did search for all "python" in the registry and did find "residue" from 
every unique version installed. I removed all of them and attempted a 
fresh, default install of the latest version -- same problem.



The path included:

C:\Users\judy\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\judy\AppData\Local\Programs\Python\Python35-32\;C:\Users\judy\AppData\Local\Programs\Python\Python35-32\DLLs;C:\Users\judy\AppData\Local\Programs\Python\Python35-32\LIB;C:\Users\judy\AppData\Local\Programs\Python\Python35-32\LIB\LIB-TK;C:\Users\judy\AppData\Local\Programs\Python\Python35-32\LIB\tkinter;

The long path was due to the default installation. I added the final 
path item.


I also added a "PythonPath" environment variable with the same contents 
as I saw that mentioned as a solution to this issue.


The problem seems rare and may be due to a previous installation and 
Vista -- I can't tell. Am willing to try anything.


I have NO idea of what to do next to install the python.org version on 
this Toshiba laptop.


Please let me know.


Thank you.

Rick Smith

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


QUEST: does HACKING make FOR loop quicker.

2015-08-05 Thread John Doe

Presumption

1. Lists are mutable sequences.
2. There is a subtlety when the sequence is being modified by the FOR 
loop (this can only occur for mutable sequences, i.e. lists)



Preamble
=
To pass by reference or by copy of - that is the question from hamlet. 
("hamlet" - a community of people smaller than a village python3.4-linux64)


xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
for x in xlist:
print(xlist)
print("\txlist[%d] = %d" % (i, x))
if x%2 == 0 :
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1

So, catch the output and help me, PLEASE, improve the answer:
Does it appropriate ALWAYS RE-evaluate the terms of the expression list 
in FOR-scope on each iteration?
But if I want to pass ONCE a copy to FOR instead of a reference (as seen 
from an output) and reduce unreasonable RE-evaluation, what I must to do 
for that?




Assumption. Wind of changes.

Sometimes FOR-loop may do a global work. (depends on size of expression 
list)

Sometimes on external DB. (just a joke)

So, what can FOR-loop do for You, depends what can You do for expression 
list, which can be mutable far outside YOUR NICE LOGIC.



QUEST

Your example of FOR-loop with a simple logic and sensitive information, 
operated by means of external list-array and Your elaborative vision of 
hacking possibilities through mutation of a list's terms.


TIA
RSVP
--
https://mail.python.org/mailman/listinfo/python-list


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Tim Chase
On 2015-08-05 06:37, Rustom Mody wrote:
> >   config = {}
> >   with open('config.ini') as f:
> > for row in f:
> >   row = row.strip()
> >   if not row or row.startswith(('#', ';')):
> > continue
> >   k, _, v = row.partition('=')
> >   config[k.strip().upper()] = v.lstrip()
> > 
> > which is pretty straight-forward and easy format to edit.
> > 
> > -tkc  
> 
> JSON handles basic types like this:
> >>> from json import loads
> >>> loads("""{"anInt":1, "aString":"2"}""")  
> {'aString': '2', 'anInt': 1}

But requires the person hand-editing the file to make sure that
opening braces close, that quoted text is properly opened/closed, has
peculiarities regarding things following back-slashes, etc.

There's a certain simplicity to simply having key/value pairs
separated by an "=" and then letting the application do whatever it
needs/wants with those key/value strings.

-tkc



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


Looking for OpenStack Developer

2015-08-05 Thread Pranesh Srinivasan
Hi,
Hope you are doing well !!!
My name is Siva and I'm a recruiter at TheAppliedthought , a global staffing 
and IT consulting company.
Please find the below job description which may suits any of your consultants 
who are available in market or who are looking for change, please send me 
latest updated resume along with contact details and expected hourly rate to my 
email id s...@theappliedthought.com or you can reach me on 407-574-7610.
Position: Python + Angular JS + Knowledge in Open Stack + Linux
Location: Houston, TX
Duration: Long Term
Job Description
Skills Required
*   Python
*   Angular JS
*   Open Stack
*   Linux
Thanks & Regards
Siva
Executive-Talent  Acquisition & Bench Sales
Email: s...@theappliedthought.com
IM: malladi sivaramakrishna88
Phone: 407-574-7610
Fax: 407-641-8184
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: QUEST: does HACKING make FOR loop quicker.

2015-08-05 Thread Mark Lawrence

On 05/08/2015 21:00, John Doe wrote:

Three strikes and you're out, good bye troll.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Marko Rauhamaa
Tim Chase :

> There's a certain simplicity to simply having key/value pairs
> separated by an "=" and then letting the application do whatever it
> needs/wants with those key/value strings.

That trap has lured in a lot of wildlife.

What to do with lists?

Is whitespace significant?

Case in point, systemd configuration files:

   http://www.freedesktop.org/software/systemd/man/systemd.servic
   e.html#Command%20lines>

It specifies all kinds of application-do-whatever-it-needs syntax:

 * backslash escapes

 * double quotes and single quotes

 * line continuations

 * percent specifiers

 * dollar substitution with or without braces

 * double-dollar escape

 * together with undocumented quirks (for example, how do you specify a
   command whose pathname contains whitespace?)

All of which makes it all but impossible to algorithmically escape a
literal command into the ExecStart= entry.

When you start with something that's too simple, you end up with layers
of ever-increasing complexity but never attain the expressive power of
JSON, S-expressions and the like (I don't have the stomach to mention
XML).


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installation Successful, but pythonw and idle doesn't function

2015-08-05 Thread Thomas 'PointedEars' Lahn
Rick Smith wrote:

> However pythonw.exe did not and does not work. I was simply returned to
> the command prompt, without ANY interaction or error.
> 
>prompt>pythonw
> 
>prompt>

Works as designed.  You are proceeding from a false assumption.  pythonw.exe 
is not meant to provide an interactive Python shell.  A little Web research 
would have showed that to you.



(First hit for “pythonw” on Google with my account.  I have never visited 
that site before or can remember to have searched for “pythonw”.)
 
> I also attempted to run "idle", with the following results:
> 
>C:
\Users\judy\AppData\Local\Programs\Python\Python35-32\Lib\idlelib>idle.py
>** IDLE can't import Tkinter.
>Your Python may not be configured for Tk. **

I do not know IDLE well (if at all after all this time).  Make sure that you 
have installed the prerequisites.  But it strikes me as odd to run a GUI-
based application from the Windows command shell.  Is there not an icon that 
you can use instead to run it?  Presumably that would execute a .bat or .cmd 
script that sets up the PYTHONPATH.  RTFM, STFW.
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installation Successful, but pythonw and idle doesn't function

2015-08-05 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote:

> 
> 
> (First hit for “pythonw” on Google with my account.  I have never visited
> that site before or can remember to have searched for “pythonw”.)

JFTR: s/site/question/.  I am rather active on Stack Overflow (but not much 
regarding Python, IIRC).
  
-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Uninstall

2015-08-05 Thread Emile van Sebille

On 8/4/2015 6:51 PM, Mario Figueiredo wrote:

On Tue, Aug 4, 2015 at 9:01 PM, Mark Lawrence 





The simple solution is not to subscribe.


Yes -- it's about gotten to that point.


Or even better, tell you to fuck off.


Now that's a first to my recollection.  I must say I prefer the pedantry 
to your civility.


But-neither-is-much-to-my-liking-ly y'rs,

Emile


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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Tim Chase
On 2015-08-06 00:47, Marko Rauhamaa wrote:
> > There's a certain simplicity to simply having key/value pairs
> > separated by an "=" and then letting the application do whatever
> > it needs/wants with those key/value strings.  
> 
> That trap has lured in a lot of wildlife.
> 
> What to do with lists?
> 
> Is whitespace significant?

As I said, simple.  Lists?  Not in a simple config format.  Though I
suppose, just like the .ini format, you could use a convention of
comma or space-separated list items. Or quoted white-space-separated
items like shlex handles nicely.  Or if you really need, pass the
value portion of the key/value pair through ast.literal_eval()

Significant whitespace?  Not usually simple (just stuck touching a
project where someone committed with tons of trailing whitespaces.
grumble), so strip 'em off as if they're an error condition.  I've
never had a config-file where I wanted leading/trailing whitespace as
significant.

> Case in point, systemd configuration files:

The antithesis of "simplicity" ;-)

-tkc



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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 9:43 AM, Tim Chase  wrote:
> Significant whitespace?  Not usually simple (just stuck touching a
> project where someone committed with tons of trailing whitespaces.
> grumble), so strip 'em off as if they're an error condition.  I've
> never had a config-file where I wanted leading/trailing whitespace as
> significant.

If you're configuring a prompt, sometimes you need to be able to
include a space at the end of it. Since trailing whitespace on a line
in the file itself is a bad idea, you need some way of marking it.
That might mean quoting the string, or having a Unicode or byte escape
like \x20 that means space, or something like that. If you define that
spaces around your equals sign are insignificant, you need the same
sort of system to cope with the possibility of actual leading
whitespace, too.

>> Case in point, systemd configuration files:
>
> The antithesis of "simplicity" ;-)

Ehh... I reckon they're pretty simple. They're somewhat more powerful
than Upstart config files, and pay some complexity cost for that; but
they're a lot simpler than sysvinit "config files" (which are actually
shell scripts), especially with all the dependency handling cruft that
goes into encoded comments. Frankly, I do my best to avoid ever
touching those. I'm not sure what else to compare them against (never
used any init system other than the three just mentioned), so I don't
see systemd files as being particularly complex.

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


Re: Problem in IDLE setup

2015-08-05 Thread Terry Reedy

On 8/5/2015 9:08 AM, Mark Lawrence wrote:

On 04/08/2015 12:31, Ahsan Chauhan wrote:

Respected Sir/Madam,
I would like to bring it to your notice that IDLE's executable file is
not working properly in Python3.5.0.


3.5.0 has not been released.  3.5.0b4 is the most recent.
Make sure that you are using that. I use it on Win7 daily without 
problems. 3.5.0c1 will be out this weekend.



Please state exactly what you're tried to do, what you expected to
happen, and what actually happened.  If you have a traceback please cut
and paste all of it into your response.  What OS are you using?


If on Windows, find the Command Prompt console and enter
> python -m idlelib
or, if that does not start 3.5,
> py -3.5 -m idlelib


--
Terry Jan Reedy

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


Re: QUEST: does HACKING make FOR loop quicker.

2015-08-05 Thread Michael Torrie
On 08/05/2015 03:39 PM, Mark Lawrence wrote:
> On 05/08/2015 21:00, John Doe wrote:
> 
> Three strikes and you're out, good bye troll.

While the original post is incomprehensible to me, I see only one post.
 What were the other two strikes?

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


Re: QUEST: does HACKING make FOR loop quicker.

2015-08-05 Thread Joel Goldstick
On Wed, Aug 5, 2015 at 8:29 PM, Michael Torrie  wrote:
> On 08/05/2015 03:39 PM, Mark Lawrence wrote:
>> On 05/08/2015 21:00, John Doe wrote:
>>
>> Three strikes and you're out, good bye troll.
>
> While the original post is incomprehensible to me, I see only one post.
>  What were the other two strikes?
>
> --
> https://mail.python.org/mailman/listinfo/python-list

I count 2, but the second is the repeat of the first.

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Rustom Mody
On Thursday, August 6, 2015 at 2:31:52 AM UTC+5:30, Tim Chase wrote:
> On 2015-08-05 06:37, Rustom Mody wrote:
> > >   config = {}
> > >   with open('config.ini') as f:
> > > for row in f:
> > >   row = row.strip()
> > >   if not row or row.startswith(('#', ';')):
> > > continue
> > >   k, _, v = row.partition('=')
> > >   config[k.strip().upper()] = v.lstrip()
> > > 
> > > which is pretty straight-forward and easy format to edit.
> > > 
> > > -tkc  
> > 
> > JSON handles basic types like this:
> > >>> from json import loads
> > >>> loads("""{"anInt":1, "aString":"2"}""")  
> > {'aString': '2', 'anInt': 1}
> 
> But requires the person hand-editing the file to make sure that
> opening braces close, that quoted text is properly opened/closed, has
> peculiarities regarding things following back-slashes, etc.
> 
> There's a certain simplicity to simply having key/value pairs
> separated by an "=" and then letting the application do whatever it
> needs/wants with those key/value strings.
> 
> -tkc

I just checked that literal_eval accepts comments.
So thats one plus for that.
However I must admit that in checking that out I was faced with more than
(I) expected unfriendly error messages like

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.4/ast.py", line 84, in literal_eval
return _convert(node_or_string)
  File "/usr/lib/python3.4/ast.py", line 62, in _convert
in zip(node.keys, node.values))
  File "/usr/lib/python3.4/ast.py", line 61, in 
return dict((_convert(k), _convert(v)) for k, v
  File "/usr/lib/python3.4/ast.py", line 83, in _convert
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x7fe1173ebac8>


By contrast here is a more friendly error message (had put a comma where a colon
required)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.4/ast.py", line 46, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.4/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
  File "", line 2
"i", 1}

So overall whether ast.literal_eval is a good idea is not clear to me
-- 
https://mail.python.org/mailman/listinfo/python-list


Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Terry Reedy
There have been discussions, such as today on Idle-sig , about who uses 
Idle and who we should design it for.  If you use Idle in any way, or 
know of or teach classes using Idle, please answer as many of the 
questions below as you are willing, and as are appropriate


Private answers are welcome. They will be deleted as soon as they are 
tallied (without names).


I realized that this list is a biased sample of the universe of people 
who have studied Python at least, say, a month.  But biased data should 
be better than my current vague impressions.


0. Classes where Idle is used:
Where?
Level?

Idle users:

1. Are you
grade school (1=12)?
undergraduate (Freshman-Senior)?
post-graduate (from whatever)?

2. Are you
beginner (1st class, maybe 2nd depending on intensity of first)?
post-beginner?

3. With respect to programming, are you
amateur (unpaid)
professional (paid for programming)

--
Terry Jan Reedy, Idle maintainer

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


Re: Most Pythonic way to store (small) configuration

2015-08-05 Thread Rustom Mody
On Thursday, August 6, 2015 at 6:32:03 AM UTC+5:30, Rustom Mody wrote:

> By contrast here is a more friendly error message (had put a comma where a 
> colon
> required)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python3.4/ast.py", line 46, in literal_eval
> node_or_string = parse(node_or_string, mode='eval')
>   File "/usr/lib/python3.4/ast.py", line 35, in parse
> return compile(source, filename, mode, PyCF_ONLY_AST)
>   File "", line 2
> "i", 1}

Uh...
The more helpfulness not evident as the most crucial line not cut-pasted

Heres the full bt

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.4/ast.py", line 46, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.4/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
  File "", line 2
"i", 1}
   ^
SyntaxError: invalid syntax
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installation Successful, but pythonw and idle doesn't function

2015-08-05 Thread Terry Reedy

On 8/5/2015 12:12 PM, Rick Smith wrote:


I was able to install various versions of Python (3.5.0b4 32bit being
the most recent) multiple times (uninstalling in between) and they
worked ("python --version" at the command line worked).

However pythonw.exe did not and does not work. I was simply returned to
the command prompt, without ANY interaction or error.


C:\Programs\Python35>pythonw

C:\Programs\Python35>

Normal behavior.

Try
> python -m idlelib
or, if that does not start 3.5,
> py -3.5 -m idlelib
and you should either see Idle or an error message

If Idle is starting but not connecting, as you seems to say, there there 
is a problems with your network socket configuration or another program. 
 Adding -n at the end of the command line will will bypass that, though 
you could have other problems in certain situations.


Please see my Who uses IDLE? thread.

--
Terry Jan Reedy

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


Re: How to trace the recursive path?

2015-08-05 Thread Terry Reedy

trace --trackcalls
Display the calling relationships exposed by running the program.

will give you part of what you want, but only counts.

I would just add print('xyx calledl') at the top of each function you 
want traced.


--
Terry Jan Reedy

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


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Ben Finney
Terry Reedy  writes:

> Private answers are welcome. They will be deleted as soon as they are
> tallied (without names).

Are you also expecting questionnaire answers in this forum? I suspect it
will become a free-ranging discussion; hopefully you're prepared to pick
through and collect the responses if so.

-- 
 \   “The Stones, I love the Stones; I can't believe they're still |
  `\  doing it after all these years. I watch them whenever I can: |
_o__)Fred, Barney, ...” —Steven Wright |
Ben Finney

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


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Rustom Mody
On Thursday, August 6, 2015 at 6:36:56 AM UTC+5:30, Terry Reedy wrote:
> There have been discussions, such as today on Idle-sig , about who uses 
> Idle and who we should design it for.  If you use Idle in any way, or 
> know of or teach classes using Idle, please answer as many of the 
> questions below as you are willing, and as are appropriate
> 
> Private answers are welcome. They will be deleted as soon as they are 
> tallied (without names).
> 
> I realized that this list is a biased sample of the universe of people 
> who have studied Python at least, say, a month.  But biased data should 
> be better than my current vague impressions.
> 
> 0. Classes where Idle is used:
> Where?
> Level?
> 
> Idle users:
> 
> 1. Are you
> grade school (1=12)?
> undergraduate (Freshman-Senior)?
> post-graduate (from whatever)?
> 
> 2. Are you
> beginner (1st class, maybe 2nd depending on intensity of first)?
> post-beginner?
> 
> 3. With respect to programming, are you
> amateur (unpaid)
> professional (paid for programming)
> 
> -- 
> Terry Jan Reedy, Idle maintainer

I used idle to teach a 2nd year engineering course last sem
It was a more pleasant experience than I expected
One feature that would help teachers:
It would be nice to (have setting to) auto-save the interaction window
[Yeah I tried to see if I could do it by hand but could not find where]
Useful for giving as handouts of the class
So students rest easy and dont need to take 'literal' notes of the session

I will now be teaching more advanced students and switching back to emacs
-- python, C, and others -- so really no option to emacs.
Not ideal at all but nothing else remotely comparable
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installation Successful, but pythonw and idle doesn't function

2015-08-05 Thread Terry Reedy

On 8/5/2015 6:09 PM, Thomas 'PointedEars' Lahn wrote:

Rick Smith wrote:



I also attempted to run "idle", with the following results:

C:

\Users\judy\AppData\Local\Programs\Python\Python35-32\Lib\idlelib>idle.py

** IDLE can't import Tkinter.
Your Python may not be configured for Tk. **


Rick, can you run python?  What happens with 'import tkinter'?


I do not know IDLE well (if at all after all this time).  Make sure that you
have installed the prerequisites.


A Windows install should install tkinter and Idle together.

> But it strikes me as odd to run a GUI-

based application from the Windows command shell.


This is the right thing to do when there is a problem, as some error 
messages get delivered to the console.  The prime example is the above. 
 If Idle cannot import tkinter, it cannot use a tkinter message box.


> Is there not an icon that you can use instead to run it?

In the start menu, but that error message would not appear.


--
Terry Jan Reedy

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


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Paul Rubin
Terry Reedy  writes:
> There have been discussions, such as today on Idle-sig , about who
> uses Idle and who we should design it for.  

I use it sometimes.  I mostly use Emacs with Python-mode but find Idle
is nice for quickly experimenting with something or probing an API.  I
know there are fancier IDE's out there but I'm mostly an Emacs user.

> 0. Classes where Idle is used:  N/A

> 1. Are you grade school (1=12)?  
Working programmer.

> 2. Are you  post-beginner?  
Yes

> 3. With respect to programming, are you
> amateur (unpaid)
> professional (paid for programming)

Professional.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 11:06 AM, Terry Reedy  wrote:
> There have been discussions, such as today on Idle-sig , about who uses Idle
> and who we should design it for.  If you use Idle in any way, or know of or
> teach classes using Idle, please answer as many of the questions below as
> you are willing, and as are appropriate

When I'm talking to my students about interactive Python on Windows,
I'll sometimes recommend Idle. It's not any sort of official thing,
but when they're having issues (particularly if they're tinkering with
a 'while' or 'for' loop, as Idle recalls those as coherent units
instead of fetching up individual lines), I'll point them to it as
another way to tackle a problem. Usually on Linux or Mac OS they're
better able to manage with the console interpreter and readline, but
Windows sucks so Idle has a bigger advantage (plus, a lot of Linux
distro-supplied Pythons don't include Idle, whereas I can be fairly
confident that a Windows Python will have it).

> 1. Are you
> grade school (1=12)?
> undergraduate (Freshman-Senior)?
> post-graduate (from whatever)?

I'm a high school dropout; my students probably cover all three of
those categories.

> 2. Are you
> beginner (1st class, maybe 2nd depending on intensity of first)?
> post-beginner?

Usually Idle is mentioned only in the context of the very early
explorations. After that, it's all "use a text editor, and then run
it", and students use whichever editors they like. Most don't ask
about editor options. Possibly I'd recommend Idle's editor mode in
some cases, but it's usually more convenient to have a single editor
which also understands HTML and CSS, as this is a web programming
course.

> 3. With respect to programming, are you
> amateur (unpaid)
> professional (paid for programming)

Pro. A lot of my students are currently amateurs; a decent number are
professional and moving around in skillset. (Do you count as a pro
programmer if you're currently paid to work with Excel macros, and
you're learning Python so you can do better? Line gets blurry.)

If Idle didn't exist, it wouldn't kill what I'm doing, but it is a
convenient option to have around.

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


Re: Installation Successful, but pythonw and idle doesn't function

2015-08-05 Thread Zachary Ware
Hi Rick,

On Wed, Aug 5, 2015 at 11:12 AM, Rick Smith  wrote:
> I was able to install various versions of Python (3.5.0b4 32bit being the
> most recent) multiple times (uninstalling in between) and they worked
> ("python --version" at the command line worked).
>
> However pythonw.exe did not and does not work. I was simply returned to the
> command prompt, without ANY interaction or error.
>
>   prompt>pythonw
>
>   prompt>

As I believe others have mentioned, 'pythonw' is not the one you want
to use 99% of the time.  'pythonw' is a 'win32' app rather than a
'console' app, meaning that it has no attached console, and if you
just run it with no arguments, it will appear to do nothing (though
actually, it starts, finds it has no input, and ends very quickly).

> I also attempted to run "idle", with the following results:
>
>
> C:\Users\judy\AppData\Local\Programs\Python\Python35-32\Lib\idlelib>idle.py
>   ** IDLE can't import Tkinter.
>   Your Python may not be configured for Tk. **

You hit upon a bug in 3.5.0b4, which is that the installer is broken
for tkinter unless you have Microsoft Visual Studio 2015 installed.
See http://bugs.python.org/issue24771 for details, the fix will be
included in 3.5.0rc1.

Your best bet is to avoid using pre-release software, and stick with
Python 3.4.3 until such time as 3.5.0 final is released.

Hope this helps,
-- 
Zach
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Uninstall

2015-08-05 Thread Laura Creighton
In a message of Tue, 04 Aug 2015 11:37:47 +0900, Bill writes:
>How do I uninstall Python from a Mac?
>
>
>-- 
>https://mail.python.org/mailman/listinfo/python-list

How did you get it in the first place?

If you installed it yourself, then you have to retrace what steps you
took to install it in order to remove it.  Did you get it from python.org?
Did you get it from ActiveState?  Did you get it from MacPorts?  From brew?
How you uninstall it is different depending on where it came from.

If you didn't install it yourself, then either somebody else installed
it for you, or it came with your mac, and Apple installed it for you.
If this is the system version of Python that Apple installed for you
that you are talking about, then it may not be a good idea for you
to uninstall it at all.  Apple's own software for internal housekeeping
may require Python in order to work properly.

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Linux script to get most expensive processes

2015-08-05 Thread Laura Creighton
If you are running this script with Python 2 write:

   if sys.platform.startswith('linux'):

to handle the case where you get linux or linux2 (and a few other weird
things some embedded systems give you ...)

Right now I think every linux system returns linux for Python 3, so it
is less of an issue (now, anyway).

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: QUEST: does HACKING make FOR loop quicker.

2015-08-05 Thread Mark Lawrence

On 06/08/2015 01:29, Michael Torrie wrote:

On 08/05/2015 03:39 PM, Mark Lawrence wrote:

On 05/08/2015 21:00, John Doe wrote:

Three strikes and you're out, good bye troll.


While the original post is incomprehensible to me, I see only one post.
  What were the other two strikes?



Same question originally on the tutor mailing list, then the development 
mailing list.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: GOTCHA with list comprehension

2015-08-05 Thread Laura Creighton
In a message of Wed, 05 Aug 2015 17:05:49 +1000, Chris Angelico writes:
>Incidentally, why Python 2.6? Python 2.7 has been out for a pretty
>long time now, and if you can't move to version 3.x, I would at least
>recommend using 2.7. Since the release of 2.6.9 back before Frozen
>came out, that branch has been completely unmaintained. Grab yourself
>a 2.7 and take advantage of some neat new features (for old values of
>"new"), and improved compatibility with 3.x.
>
>ChrisA

Be careful suggesting that people upgrade, at least until you have
found out that they aren't running CentOS enterprise edition. The
CentOS packaging system is utterly dependent on having the Python
version it expects, and if you install a more recent version
(from source, say) the whole packaging system will stop working.
Recovering from this problem is also very, very difficult  (unless
you just restore from a recent full backup).

Been there, done that. :(

Laura
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GOTCHA with list comprehension

2015-08-05 Thread Chris Angelico
On Thu, Aug 6, 2015 at 2:39 PM, Laura Creighton  wrote:
> In a message of Wed, 05 Aug 2015 17:05:49 +1000, Chris Angelico writes:
>>Incidentally, why Python 2.6? Python 2.7 has been out for a pretty
>>long time now, and if you can't move to version 3.x, I would at least
>>recommend using 2.7. Since the release of 2.6.9 back before Frozen
>>came out, that branch has been completely unmaintained. Grab yourself
>>a 2.7 and take advantage of some neat new features (for old values of
>>"new"), and improved compatibility with 3.x.
>>
>>ChrisA
>
> Be careful suggesting that people upgrade, at least until you have
> found out that they aren't running CentOS enterprise edition. The
> CentOS packaging system is utterly dependent on having the Python
> version it expects, and if you install a more recent version
> (from source, say) the whole packaging system will stop working.
> Recovering from this problem is also very, very difficult  (unless
> you just restore from a recent full backup).
>
> Been there, done that. :(

Yeah. Fortunately there are ways to leave the system Python untouched
while adding in another Python for some other purpose... and, if
you're desperate enough, you can even leave the Apache-Python bridge
using an older build of Python while running key parts of your web
application in a newer version,  but that requires a certain measure
of insanity!

There will come a time, though, when every supported Linux distro is
shipping either 2.7 or 3.x. And it isn't far off. When that happens,
advising the upgrade will be simple.

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


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Miki Tebeka
Greetings,

> 0. Classes where Idle is used:
> Where?
At client site. Mostly big companies.

> Level?
>From beginner to advanced.

> Idle users:
> 1. Are you
> grade school (1=12)?
> undergraduate (Freshman-Senior)?
> post-graduate (from whatever)?
post-graduate

> 2. Are you
> beginner (1st class, maybe 2nd depending on intensity of first)?
> post-beginner?
post-beginner
 
> 3. With respect to programming, are you
> amateur (unpaid)
> professional (paid for programming)
professional
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Laura Creighton
In a message of Wed, 05 Aug 2015 21:06:31 -0400, Terry Reedy writes:
>0. Classes where Idle is used:
>Where -- my house or sometimes at the board game society
>Level -- beginners

and there are 8 children right now.

>Idle users:
>
>1. Are you
I am post graduate, but the kids are all grade school.

>2. Are you
>beginner (1st class, maybe 2nd depending on intensity of first)?
>post-beginner?

I am post-beginner, the kids are all beginners.

>3. With respect to programming, are you
>amateur (unpaid)
>professional (paid for programming)

The teaching is unpaid.  I get paid for other things.
>
>-- 
>Terry Jan Reedy, Idle maintainer

Laura

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


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Paul Rubin
Paul Rubin  writes:
> I use it sometimes.  I mostly use Emacs with Python-mode but find Idle
> is nice for quickly experimenting with something or probing an API.

Added: I sometimes used Idle in places where Emacs isn't available,
e.g. client machines running Windows.  It's nice that Idle is there if
the Python environment is there.
-- 
https://mail.python.org/mailman/listinfo/python-list


how to simulate keydown and keyup events using win32api .

2015-08-05 Thread ppk . phanikumar
win32api.keybd_event(code,0,0,0)
time.sleep(2)
win32api.keybd_event(code,0,win32con.KEYEVENTF_KEYUP,0)



the above code is simulating single click on button but not press Key Hold
but i want to hold the until key up event is called
eg: for key 'a' down it have to simulate key continous set  until keyUp is 
called but not like "a" 
please specify any python API to simulate continues keyDown until it receives 
keyUp event
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Who uses IDLE -- please answer if you ever do, know, or teach

2015-08-05 Thread Laura Creighton
Added:  right now most children I know who want to program want to
write games that run on their cell phones and tablets.  So Idle integration
with kivy would be very nice, if Idle developers are looking for
new directions.

Laura

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


Re: QUEST: does HACKING make FOR loop quicker.

2015-08-05 Thread Steven D'Aprano
On Thursday 06 August 2015 10:29, Michael Torrie wrote:

> On 08/05/2015 03:39 PM, Mark Lawrence wrote:
>> On 05/08/2015 21:00, John Doe wrote:
>> 
>> Three strikes and you're out, good bye troll.
> 
> While the original post is incomprehensible to me, I see only one post.
>  What were the other two strikes?

John Doe has already asked this question on the tutor list, where I answered 
it (to the best as I can understand his question), and the Python-Dev list.

I've told him at least twice off-list that if his question hasn't been 
answered, he should explain what he means in more detail on the tutor list.

At least the question isn't off-topic here. I just wish I understood what 
part of my answer doesn't satisfy. Despite John Doe emailing me off list 
four times, I still don't know what he actually wants.

https://mail.python.org/pipermail/tutor/2015-August/106244.html


-- 
Steve

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