Re: python3 - set '\n\n' as the terminator when writing a formatted LogRecord

2016-12-30 Thread Peter Otten
iMath wrote:

> Is it possible to set '\n\n' as the terminator when writing a formatted
> LogRecord to a stream by changing the format parameter of
> logging.basicConfig?
> 
> I know it is possible using the terminator attribute of StreamHandler
> class to implement this, I just wonder Is it possible to achieve this
> feature by changing the format parameter? I am not familiar with the
> format string language

How about altering an existing format?

>>> logging.basicConfig(format=logging.BASIC_FORMAT + "\n", 
level=logging.INFO)
>>> g = logging.getLogger()
>>> g.info("hi")
INFO:root:hi

>>> 


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


Re: ctypes, memory mapped files and context manager

2016-12-30 Thread Peter Otten
eryk sun wrote:

> On Thu, Dec 29, 2016 at 12:18 PM, Hans-Peter Jansen  wrote:
>>> >>> import weakref, ctypes
>>> >>> T = ctypes.c_ubyte * 3
>>> >>> t = T()
>>> >>> bytes(t) == b"\0" * 3
>>>
>>> True
>>>
>>> >>> bytes(weakref.proxy(t)) == b"\0" * 3
>>>
>>> Traceback (most recent call last):
>>>   File "", line 1, in 
>>> AttributeError: 'c_ubyte_Array_3' object has no attribute '__bytes__'
>>>
>>> That looks like a leaky abstraction. While I found a workaround
>>>
>>> >>> bytes(weakref.proxy(t)[:]) == b"\0" * 3
>>>
>>> True
>>
>> I found a couple of other rough corners already, when working with the
>> ctypes module. Obviously, this module is lacking some love.
> 
> That's not the fault of ctypes. 

By adding a Structure.close() method that would release the underlying 
memoryview reference and make all further attribute access fail ctypes could 
support Hans-Peter's use case quite easily. In that sense I think it is the 
"fault" or rather a limitation of ctypes. I'm not sure though whether ctypes 
is the right tool here in the first place. 

Anyway, as long as he gets enough advantages from his current approach a few 
odd del statements might be OK.

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


Re: [OT] Security question

2016-12-30 Thread Anssi Saari
"Frank Millman"  writes:

> Hi all
>
> This is off-topic, but I would appreciate a comment on this matter.
>
> I have just upgraded my internet connection from ADSL to Fibre.
>
> As part of the process, my ISP sent a text message to my cell phone
> with the username and password I must use to connect.
>
> To my surprise, they sent me my existing username *and* my existing
> password, all in clear text.

I'd say it depends on what the password is actually used for. You seem
to indicate it's just so you can access the internet? To me it seems
abusing that password is hard to impossible since it's your fibre to
your home. If the password is used for access control for anything then
it's an awful practise.

In my case, I have one password for the email account my ISP provides
and another for their web management pages where I can buy more or get
rid of services and see my bills and such.

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


Re: [OT] Security question

2016-12-30 Thread Marko Rauhamaa
Anssi Saari :

> "Frank Millman"  writes:
>> To my surprise, they sent me my existing username *and* my existing
>> password, all in clear text.
>
> I'd say it depends on what the password is actually used for. You seem
> to indicate it's just so you can access the internet? To me it seems
> abusing that password is hard to impossible since it's your fibre to
> your home. If the password is used for access control for anything
> then it's an awful practise.

The message to take home is that whenever you are faced with a password
prompt, the recipient can do with the password whatever they want. You
should assume the worst. The password will be stored in the clear and
all employees of the recipient have free access to it. Also, there's a
high likelihood that the credentials will leak outside the organization.


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


Re: [OT] Security question

2016-12-30 Thread Chris Angelico
On Fri, Dec 30, 2016 at 10:47 PM, Anssi Saari  wrote:
> I'd say it depends on what the password is actually used for. You seem
> to indicate it's just so you can access the internet? To me it seems
> abusing that password is hard to impossible since it's your fibre to
> your home. If the password is used for access control for anything then
> it's an awful practise.

"Just" so he can access the internet? That's no small deal. If someone
else can sign in with the same password, s/he can do any sort of abuse
and it'll be registered to someone else. What spammer wouldn't jump at
the chance to blame someone else for the traffic?

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


Simulating int arithmetic with wrap-around

2016-12-30 Thread Steve D'Aprano
It's easy to simulate integer arithmetic with wrap-around for *unsigned*
ints. For example, imagine a four-bit integer (0 through 15):

# Addition
py> (7 + 8) & 0xF
15
py> (7 + 9) & 0xF
0
py> (7 + 10) & 0xF
1

# Multiplication
py> (7 * 2) & 0xF
14
py> (7 * 3) & 0xF
5
py> (7 * 4) & 0xF
12


And in general, for any operator ⊗, and a and b both in the range 0 through
2**N-1, we can simulate unsigned N-bit integer arithmetic with:

a⊗b & (2**N - 1)

(I think this works for all the arithmetic operators, and the bitwise
operations. The bitmask & 2**N-1 is not needed for the bitwise operations
except for left-shift.)


How about *signed* integers?

7 + 1 => -8
7 + 2 => -7
7 + 7 => -2

7 * 2 => -2
7 * 3 => 5
7 * 4 => -4


Again, assume both operands are in range for an N-bit signed integer. What's
a good way to efficiently, or at least not too inefficiently, do the
calculations in Python?


Signed arithmetic also has some gotchas. For example, -x is not necessarily
defined, nor is abs(x).


Thanks,





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: [OT] Security question

2016-12-30 Thread Marko Rauhamaa
Chris Angelico :

> On Fri, Dec 30, 2016 at 10:47 PM, Anssi Saari  wrote:
>> I'd say it depends on what the password is actually used for. You seem
>> to indicate it's just so you can access the internet? To me it seems
>> abusing that password is hard to impossible since it's your fibre to
>> your home. If the password is used for access control for anything then
>> it's an awful practise.
>
> "Just" so he can access the internet? That's no small deal. If someone
> else can sign in with the same password, s/he can do any sort of abuse
> and it'll be registered to someone else. What spammer wouldn't jump at
> the chance to blame someone else for the traffic?

That's called plausible deniability.


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


Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Serhiy Storchaka

On 30.12.16 16:47, Steve D'Aprano wrote:

Again, assume both operands are in range for an N-bit signed integer. What's
a good way to efficiently, or at least not too inefficiently, do the
calculations in Python?


def to_unsigned(bits, x):
return x & ((1

Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Chris Angelico
On Sat, Dec 31, 2016 at 1:47 AM, Steve D'Aprano
 wrote:
> How about *signed* integers?
>
> 7 + 1 => -8
> 7 + 2 => -7
> 7 + 7 => -2
>
> 7 * 2 => -2
> 7 * 3 => 5
> 7 * 4 => -4
>
>
> Again, assume both operands are in range for an N-bit signed integer. What's
> a good way to efficiently, or at least not too inefficiently, do the
> calculations in Python?

One way would be to work with the integers as unsigned numbers, and
then render them signed for display only. The neat thing about two's
complement (as opposed to two's compliment, which is a really sweet
thing to say to someone) is that a lot of operations work exactly the
same way. You add 7+2 and get 9, and then display 9 as -7. You take
that 9 (really -7) and add 5 to it, and you get 14, which you display
as -2. Add another 4 and you get 18, mask that off and you get 2. You
may have to handle multiplication and division differently, I think,
and you might need a sign-extend right shift operator (as opposed to a
zero-fill right shift), but they should be able to be defined in terms
of the other operations.

7 * 2 => 14, displayed as -2
7 * 3 => 21, mask to 5
7 * 4 => 28, mask to 12, display as -4
7 * 9 => 63, mask to 15, display as -1. Conceptually 7*-7 => -49.

Actually you might be able to get away with multiplication perfectly.
I'm thinking of the Intel 80x86 opcodes, where MUL and IMUL are
unsigned and signed multiplication, respectively, but they differ
because the result is twice as wide as the operands (eg if you
multiply two 16-bit numbers, you get a 32-bit result). The Intel chips
do the same with division (eg you divide a 32-bit number by a 16-bit
and get back a 16-bit quotient and 16-bit remainder). You may be able
to take the exact same short-cut.

> Signed arithmetic also has some gotchas. For example, -x is not necessarily
> defined, nor is abs(x).

AIUI -x is always defined, but might be equal to x in two
circumstances (zero and MAXINT+1). Not sure about abs(x), but probably
would be the same.

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


Re: Parse a Wireshark pcap file

2016-12-30 Thread Grant Edwards
On 2016-12-27, [email protected] <[email protected]> wrote:
>
> I have a pcap file, I want to parse that file & fetch some
> information like Timestamp, Packet Size, Source/Dest IP Address,
> Source/Dest Port, Source/ Dest MAC address.

I've been using pylibpcap for ages.  It's a bit old, but still works
fine for me:

 https://sourceforge.net/projects/pylibpcap/

There's also pypcap:

 https://github.com/pynetwork/pypcap

-- 
Grant Edwards   grant.b.edwardsYow! Now KEN and BARBIE
  at   are PERMANENTLY ADDICTED to
  gmail.comMIND-ALTERING DRUGS ...

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


learning and experimenting python.

2016-12-30 Thread einstein1410
Hello everyone,
I am the new comer and learner of python.
I have a doubt that when I type python and press enter it shows a prompt like
>>>
But why it is >>> ?
Is there any special reason?
Why it is not setted as @,& or any other special characters?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Ian Kelly
On Fri, Dec 30, 2016 at 11:50 AM,   wrote:
> Hello everyone,
> I am the new comer and learner of python.
> I have a doubt that when I type python and press enter it shows a prompt like

> But why it is >>> ?
> Is there any special reason?
> Why it is not setted as @,& or any other special characters?

Because it's easy to recognize as a prompt, I would guess. You can
customize it by importing the 'sys' module and changing the values of
sys.ps1 and sys.ps2.

>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> def foo():
... pass
...
>>> sys.ps1 = 'EGGS '
EGGS sys.ps2 = 'BACON '
EGGS def foo():
BACON pass
BACON
EGGS
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread BartC

On 30/12/2016 18:44, Ian Kelly wrote:

On Fri, Dec 30, 2016 at 11:50 AM,   wrote:

Hello everyone,
I am the new comer and learner of python.
I have a doubt that when I type python and press enter it shows a prompt like



But why it is >>> ?
Is there any special reason?
Why it is not setted as @,& or any other special characters?


Because it's easy to recognize as a prompt, I would guess. You can
customize it by importing the 'sys' module and changing the values of
sys.ps1 and sys.ps2.


import sys


In usenet posts >>> gets confused for quoted material 3 levels deep 
(which on my newsreader is shown as coloured vertical bars on the left).


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


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
LAN you are right. I am agree with you that it's easy to recognise.

But look
$ for normal user
# for special user/root
% for other shell
>>> For python 
And so on...
Why?
Why their developer selected that?
Is there any special reason?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
So what bartc?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread mm0fmf

On 30/12/2016 19:08, [email protected] wrote:

LAN you are right. I am agree with you that it's easy to recognise.

But look
$ for normal user
# for special user/root
% for other shell

For python

And so on...
Why?
Why their developer selected that?
Is there any special reason?

It is what it is. Change it if you don't like it. But you will only 
confuse people who have lots of experience looking for ">>>" when you 
post some output and ask for help.

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


Re: learning and experimenting python.

2016-12-30 Thread BartC

On 30/12/2016 19:09, [email protected] wrote:

So what bartc?


What until you have to try and figure out which is which!

It's a disadvantage of using >>>


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


Re: learning and experimenting python.

2016-12-30 Thread Wildman via Python-list
On Fri, 30 Dec 2016 11:08:30 -0800, einstein1410 wrote:

> LAN you are right. I am agree with you that it's easy to recognise.
> 
> But look
> $ for normal user
> # for special user/root
> % for other shell
 For python 
> And so on...
> Why?
> Why their developer selected that?
> Is there any special reason?

That would be questions for the developer(s).  As an end-user,
the only answer I can give is, it is that way because that is
the way it is.

-- 
 GNU/Linux user #557453
Keyboard not detected! Press any key to continue...
-- 
https://mail.python.org/mailman/listinfo/python-list


List comprehension

2016-12-30 Thread Jason Friedman
$ python
Python 3.6.0 (default, Dec 26 2016, 18:23:08)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = (
... (1,2),
... (3,4),
... )
>>> [a for a in data]
[(1, 2), (3, 4)]

Now, this puzzles me:

>>> [x,y for a in data]
  File "", line 1
[x,y for a in data]
   ^
SyntaxError: invalid syntax

I expected:
[(1, 2), (3, 4)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Michael Torrie
On 12/30/2016 12:08 PM, [email protected] wrote:
> LAN you are right. I am agree with you that it's easy to recognise.
> 
> But look
> $ for normal user
> # for special user/root
> % for other shell
 For python 
> And so on...
> Why?
> Why their developer selected that?
> Is there any special reason?

Is there a special reason bourne shell uses $ and #?  Coming from an old
DOS background (>) I found that rather jarring at first.  There's no
particular reason for any of those shell prompts.  You say "%" is for
"other shell."  Which shells?  *Any* other shell?  These are all just
arbitrary.

Furthermore, Python is not a shell, so why would you expect an
interactive python prompt to look like bourne shell?  Wouldn't that just
be confusing?

$ python3
Python 3.4.3 (default, Aug  9 2016, 17:10:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
$

No thanks.  I think using > as a prompt character work very well. I
suppose Python could have made the prompt a bit more like ruby:

$ irb
irb(main):001:0>

But that's a bit busy.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Chris Angelico
On Sat, Dec 31, 2016 at 6:49 AM, Michael Torrie  wrote:
> Is there a special reason bourne shell uses $ and #?  Coming from an old
> DOS background (>) I found that rather jarring at first.  There's no
> particular reason for any of those shell prompts.  You say "%" is for
> "other shell."  Which shells?  *Any* other shell?  These are all just
> arbitrary.
>
> Furthermore, Python is not a shell, so why would you expect an
> interactive python prompt to look like bourne shell?  Wouldn't that just
> be confusing?
>
> $ python3
> Python 3.4.3 (default, Aug  9 2016, 17:10:39)
> [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> $
>
> No thanks.  I think using > as a prompt character work very well. I
> suppose Python could have made the prompt a bit more like ruby:

Quickly running through the interactive shells and command
interpreters I have available yields this data:

rosuav@sikorsky:~$ bash (my normal terminal prompt)
$ sh, dash
sikorsky% zsh
>>> python, python3, jython
 pypy
> pike, node, threshold, vlc
rosuav=> psql
irb(main):001:0> irb (ruby)
sqlite> sqlite3
(no prompt) php, telnet, bc

And that's not counting the continuation prompts or other-mode prompts
for each of them (eg # for superuser). There are a lot of prompts, but
as you see, ">" is somewhat overused. Without even trying hard, I
found *four* programs that use it unadorned (plus I know bash uses
that as a continuation prompt), yet with the same effort, I found only
*three* with no prompt whatsoever, and they're all in "batch mode" or
similar. (Telnet, for instance, just passes everything on to the
server, so if you're talking to an SMTP server, you get no prompt.) So
there's plenty of good reason to avoid ">", just so people know what's
going on. Can you tell, for instance, what this is?

> 1+2;
3

At least this one has some adornment on its results (since you can
retrieve prior results by index):

> 1+2;
(1) Result: 3

Those are the Node.js and Pike REPLs, respectively. Python has an
advantage because you can instantly see that a transcript comes from
Python. :)

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


Re: List comprehension

2016-12-30 Thread Joel Goldstick
On Fri, Dec 30, 2016 at 2:37 PM, Jason Friedman  wrote:
> $ python
> Python 3.6.0 (default, Dec 26 2016, 18:23:08)
> [GCC 4.8.4] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 data = (
> ... (1,2),
> ... (3,4),
> ... )
 [a for a in data]
> [(1, 2), (3, 4)]
>
> Now, this puzzles me:
>
 [x,y for a in data]
>   File "", line 1
> [x,y for a in data]
>^
> SyntaxError: invalid syntax
>
> I expected:
> [(1, 2), (3, 4)]
> --
> https://mail.python.org/mailman/listinfo/python-list

Thy this:

>>> [(a[0], a[1]) for a in data]
[(1, 2), (3, 4)]


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread David Froger
Quoting [email protected] (2016-12-30 18:50:19)
> Hello everyone,
> I am the new comer and learner of python.
> I have a doubt that when I type python and press enter it shows a prompt like
> >>>
> But why it is >>> ?
> Is there any special reason?
> Why it is not setted as @,& or any other special characters?
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Seems that it is inherited from the ABC programming langage:
http://homepages.cwi.nl/~steven/abc/types.html

The question is now why ABC choose >>> as prompt...
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: List comprehension

2016-12-30 Thread Joaquin Alzola


>Now, this puzzles me:

 [x,y for a in data]
>  File "", line 1
>[x,y for a in data]
 >  ^
>SyntaxError: invalid syntax

>I expected:
>[(1, 2), (3, 4)]

You can try [(x,z) for x,z in data].
In your situation a takes the values (1,2) or (3,4) in the one that I put x and 
z take the tupple values (x first one z second one).

>>> [(x,z) for x,z in data]
[(1, 2), (3, 4)]
This email is confidential and may be subject to privilege. If you are not the 
intended recipient, please do not copy or disclose its content but contact the 
sender immediately upon receipt.
-- 
https://mail.python.org/mailman/listinfo/python-list


Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
I've already learned one neat trick to collapse a conditional:

a = expression1 if condition else expression2

Here I have a real mess, in my opinion:

if len(l1[st]) == 0:
  if len(l2[st]) > 0:
  l1[st] = l2[st]
  elif len(l2[st]) == 0:
  if len(l1[st]) > 0:
  l2[st] = l1[st]

  (Basically, if one field from two adjacent rows is empty and the other
is 
  not, copy the non-empty field to the empty one. I use this for rental 
  listings that are identical but posted on different dates, to copy the

  data from an older one to the new one. Or, if I look up the data for a
new  
  listing, to copy it back to the older ones.)

Anybody know or see an easier (more pythonic) way to do this? I need to
do it for four fields, and needless to say, that's a really long block
of ugly code.

Any other insights into tightening up complex conditionals?

Thanks in advance,
Deborah 

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


Re: Cleaning up conditionals

2016-12-30 Thread BartC

On 30/12/2016 21:20, Deborah Swanson wrote:

I've already learned one neat trick to collapse a conditional:

a = expression1 if condition else expression2

Here I have a real mess, in my opinion:

if len(l1[st]) == 0:
  if len(l2[st]) > 0:
  l1[st] = l2[st]
  elif len(l2[st]) == 0:
  if len(l1[st]) > 0:
  l2[st] = l1[st]


This doesn't make sense. The main block is executed when len(l1[st]) is 
0, but you're testing for len(l1[st])>0 in the last if statement (which 
can't see the assignment to l1[st], so can never be true).


Try writing it out on paper using A and B instead l1[st] and l2[st] as 
they look confusing.


You might also be evaluating len(l2[st]) twice.

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


Re: List comprehension

2016-12-30 Thread Joel Goldstick
On Fri, Dec 30, 2016 at 2:58 PM, Joaquin Alzola
 wrote:
>
>
>>Now, this puzzles me:
>
> [x,y for a in data]
>>  File "", line 1
>>[x,y for a in data]
>  >  ^
>>SyntaxError: invalid syntax
>
>>I expected:
>>[(1, 2), (3, 4)]
>
> You can try [(x,z) for x,z in data].
> In your situation a takes the values (1,2) or (3,4) in the one that I put x 
> and z take the tupple values (x first one z second one).
>
 [(x,z) for x,z in data]
> [(1, 2), (3, 4)]

I like yours better than mine.


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
BartC wrote:
> Sent: Friday, December 30, 2016 2:11 PM
> 
> On 30/12/2016 21:20, Deborah Swanson wrote:
> > I've already learned one neat trick to collapse a conditional:
> >
> > a = expression1 if condition else expression2
> >
> > Here I have a real mess, in my opinion:
> >
> > if len(l1[st]) == 0:
> >   if len(l2[st]) > 0:
> >   l1[st] = l2[st]
> >   elif len(l2[st]) == 0:
> >   if len(l1[st]) > 0:
> >   l2[st] = l1[st]
> 
> This doesn't make sense. The main block is executed when 
> len(l1[st]) is 
> 0, but you're testing for len(l1[st])>0 in the last if 
> statement (which 
> can't see the assignment to l1[st], so can never be true).
> Try writing it out on paper using A and B instead l1[st] and 
> l2[st] as 
> they look confusing.
> 
> You might also be evaluating len(l2[st]) twice.
> 
> -- 
> Bartc

Oops, indentation was messed up when I copied it into the email. Should
be this:

if len(l1[st]) == 0:
if len(l2[st]) > 0:
l1[st] = l2[st]
elif len(l2[st]) == 0:
if len(l1[st]) > 0:
l2[st] = l1[st]

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


Re: Cleaning up conditionals

2016-12-30 Thread David
On 31 December 2016 at 10:00, Deborah Swanson  wrote:
>
> Oops, indentation was messed up when I copied it into the email.

The indentation of your latest message looks completely broken  now,
you can see it here:
https://mail.python.org/pipermail/python-list/2016-December/717758.html
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On Fri, 30 Dec 2016 13:20:15 -0800, "Deborah Swanson" 
>  declaimed the following:
> 
> >I've already learned one neat trick to collapse a conditional:
> >
> > a = expression1 if condition else expression2
> >
> >Here I have a real mess, in my opinion:
> >
> > if len(l1[st]) == 0:
> >  if len(l2[st]) > 0:
> >  l1[st] = l2[st]
> >  elif len(l2[st]) == 0:
> >  if len(l1[st]) > 0:
> 
>   You will never reach here. The above "elif" is only 
> reachable if "len(l1[st])" IS EQUAL TO 0, which means that 
> the later "if" can not be true.
> 
> >  l2[st] = l1[st]
> >
> >  (Basically, if one field from two adjacent rows is empty and the 
> >other is
> >  not, copy the non-empty field to the empty one. I use this 
> for rental
> >  listings that are identical but posted on different dates, 
> to copy the
> >
> >  data from an older one to the new one. Or, if I look up 
> the data for 
> >a new
> >  listing, to copy it back to the older ones.)
> >
> >Anybody know or see an easier (more pythonic) way to do 
> this? I need to 
> >do it for four fields, and needless to say, that's a really 
> long block 
> >of ugly code.
> >
> 
>   Ever consider using conjunctions?
> 
>   if len(l1[st]) and not len(l2[st]):
>   #0 is considered a false -- no need to test for "==0"
>   #non-0 is considered true -- no need to test for ">0"
>   #copy l1 to l2
>   elif not len(l1[st]) and len(l2[st]):
>   #copy l2 to l1
> 
> -- 
>   Wulfraed Dennis Lee Bieber AF6VN
> [email protected]://wlfraed.home.netcom.com/

That's a neat shortcut, len(a) instead of len(a)!= 0.  Thanks!

Yes, 4 lines is an improvement on 6 lines, but I was hoping for
something more radical.

Is it possible to use some version of the "a = expression1 if condition
else expression2" syntax with an elif? And for expression1 and
expression2 to be single statements?  That's the kind of shortcutting
I'd like to do, and it seems like python might be able to do something
like this.

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


Re: Cleaning up conditionals

2016-12-30 Thread Erik

On 30/12/16 23:00, Deborah Swanson wrote:

Oops, indentation was messed up when I copied it into the email. Should
be this:

if len(l1[st]) == 0:
if len(l2[st]) > 0:
l1[st] = l2[st]
elif len(l2[st]) == 0:
if len(l1[st]) > 0:
l2[st] = l1[st]


That's even worse!

Anyway, ignoring all that, if what you are trying to do is just do some 
action based on a set of fixed comparisons that are known at the top of 
the function (and do not mutate depending on the path through the 
function), then you can just cache the comparisons and then compare the 
resulting set of boolean results (note that my examples are NOT based on 
your use-case, it's just pseudo-code which happens to use your expressions):


state = (len(l1[st]) == 0, len(l2[st]) > 0)
if state == (True, False):
  pass
elif state == (False, True):
  pass

... etc.

If the len() comparisons are tri-state (i.e., in some cases you want to 
know if the length is <, ==, or > 0, depending on one of the other 
comparisons) then you can do something like:


def clamp(foo):
  return min(max(-1, foo), 1)

state = (clamp(cmp(len(l1[st]), 0), cmp(len(l2[st]), 0))
if state == (0, -1):
  pass
elif state == (1, -1):
  pass

... etc.

I'm not sure this makes it much more readable though - but if you make 
the RHS of those comparisons a symbolic name you might be getting 
somewhere -


ACTION1 = (0, -1)
ACTION2 = (1, -1)
if state == ACTION1:
  pass
elif state == ACTION2:
  pass

Hope that helps, E.
--
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
I am not getting you.
Please simplify it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
That's not the answer.
If you don't have answer, please don't answer like this, because that will 
confuse others also.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
You are also confusing me.
But there mustbe  some reason.
What happens if your student questions you like this.?
And may be those who questions like this will surely be the developer of its 
successor language.
Because out of thousands, only one may asks this, whom you all will consider 
fool, but he's the only genius.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Yes, I am not confusing you all, rather I thought that this is the best place 
to solve any doubts, soy only question for you is

Why python uses >>> instead of >, or any other special characters?
Do you know about this, if yes then please answer it.
I will be so much thankful of you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Erik

On 30/12/16 23:34, [email protected] wrote:

You are also confusing me.
But there mustbe  some reason.
What happens if your student questions you like this.?
And may be those who questions like this will surely be the developer of its 
successor language.
Because out of thousands, only one may asks this, whom you all will consider 
fool, but he's the only genius


Do not feed the troll.

E.

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


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Then again the question is same for ABC.
Why >>>?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
No I have a question not answer, but if I got the answer then I will tell you.
Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Ian Kelly
On Dec 30, 2016 4:42 PM,  wrote:

Yes, I am not confusing you all, rather I thought that this is the best
place to solve any doubts, soy only question for you is

Why python uses >>> instead of >, or any other special characters?
Do you know about this, if yes then please answer it.
I will be so much thankful of you.


The answers you've already received are the best you're likely to get here.
Nobody knows exactly why because the question is about unimportant trivia.
If you really want to know you could ask Guido (if he even remembers the
reason) and then you can be the expert.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Im not feeding the troll.
If you don't have answer to this, then there would be no question that you are 
the python developer.
And if this is troll, leave it.
But respect every question, they are the gift of God. And if there is no 
question, there will be no answer. And may the question is the answer to this 
giant development.
If no question arises in the person who is now recognised as the developer, 
then now a days he will not be that what he is...
-- 
https://mail.python.org/mailman/listinfo/python-list



Re: learning and experimenting python.

2016-12-30 Thread einstein1410
But in his website, he recommended that post your questions here, he will 
answer it.
But still as you told me I will send him an personal e-mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
And if this is unimportant, as you thought, then you must not be the part of 
this group. This group is to share knowledge, nott for deciding what is trivia 
and what's not.
If you think it's it, simply leave it, any else will answer.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 31 December 2016 at 10:00, Deborah Swanson 
>  wrote:
> >
> > Oops, indentation was messed up when I copied it into the email.
> 
> The indentation of your latest message looks completely 
> broken  now, you can see it here: 
> https://mail.python.org/pipermail/python-list/2016-December/71
7758.html

Hm, I don't know what pipermail does with whitespace but the formatting
in the message at
https://mail.python.org/pipermail/python-list/2016-December/717758.html
is not the same as the message I sent from Outlook.

Again, it should have been:

if len(l1[st]) == 0:
if len(l2[st]) > 0:
l1[st] = l2[st]
elif len(l2[st]) == 0:
if len(l1[st]) > 0:
l2[st] = l1[st]

(Maybe keeping the left edge flush with the left margin will help keep
the mail processors straight. I've never tried emailing python code
before.)

As Mr. Bieber points out, what I had above greatly benefits from the use
of conjunctions. It now reads:

if not len(l1[st]) and len(l2[st]):
l1[st] = l2[st]
elif not len(l2[st]) and len(l1[st]):
l2[st] = l1[st]

(I plead being cross-eyed from rewriting this section so many times for
not seeing this.)

I'm still wondering if these 4 lines can be collapsed to one or two
lines.

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


Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Steve D'Aprano
On Sat, 31 Dec 2016 02:14 am, Serhiy Storchaka wrote:

> On 30.12.16 16:47, Steve D'Aprano wrote:
>> Again, assume both operands are in range for an N-bit signed integer.
>> What's a good way to efficiently, or at least not too inefficiently, do
>> the calculations in Python?
> 
> def to_unsigned(bits, x):
>  return x & ((1< 
> def to_signed(bits, x):
>  offset = 1<<(bits-1)
>  return to_unsigned(bits, x + offset) - offset

Thanks.

Are you saying that the best way of doing this is this?

(1) convert signed Python ints to unsigned;

(2) perform operation and bitmask;

(3) convert unsigned back to signed.


Here's an example. I want to multiply 7*3 using a signed 4-bit int, getting
5 as the answer, and 7*4 getting -4 as the answer:


py> N = 4
py> to_signed(N, to_unsigned(N, 7) * to_unsigned(N, 3) & (2**N - 1))
5
py> to_signed(N, to_unsigned(N, 7) * to_unsigned(N, 4) & (2**N - 1))
-4



It works, but I'm glad I won't be doing anything that requires great
speed :-)




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: List comprehension

2016-12-30 Thread Steve D'Aprano
On Sat, 31 Dec 2016 06:37 am, Jason Friedman wrote:

> $ python
> Python 3.6.0 (default, Dec 26 2016, 18:23:08)
> [GCC 4.8.4] on linux
> Type "help", "copyright", "credits" or "license" for more information.
 data = (
> ... (1,2),
> ... (3,4),
> ... )
 [a for a in data]
> [(1, 2), (3, 4)]
> 
> Now, this puzzles me:
> 
 [x,y for a in data]
>   File "", line 1
> [x,y for a in data]
>^
> SyntaxError: invalid syntax
> 
> I expected:
> [(1, 2), (3, 4)]


Why would you expect that? I would expect the global variables x and y, or
if they don't exist, a NameError:

py> a = (1, 2)
py> x, y
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'x' is not defined

Python has no magic that "x and y mean the first two items of a".


Instead, you get a syntax error because the syntax is ambiguous:

[x,y for a in data]

looks like a list

[x, y]

except the second item is a syntax error "y for a in data". So let's fix the
syntax error:



py> data = [(1, 2), (3, 4)]
py> [a for a in data]
[(1, 2), (3, 4)]
py> [(x, y) for a in data]
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name 'x' is not defined


Okay, let's create global variables x and y:

py> x = 98
py> y = 99
py> [(x, y) for a in data]
[(98, 99), (98, 99)]


No surprises there. How about this instead:

py> [(x, y) for (x, y) in data]
[(1, 2), (3, 4)]


That's better!




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Cleaning up conditionals

2016-12-30 Thread Erik

On 31/12/16 00:26, Deborah Swanson wrote:

As Mr. Bieber points out, what I had above greatly benefits from the use
of conjunctions. It now reads:

if not len(l1[st]) and len(l2[st]):


IMHO, "if not len(l)" is a _terrible_ way of spelling "if len(l) == 0" 
(mentally, I have to read that as "if length of 'l' is not not equal to 
0" - and a double negative won't never cause problems ( ;) )).


Also, in that particular expression, having to know off the top of their 
head the precedence of 'not' and 'and' will cause at least some 
percentage of your maintenance audience in the future to get it wrong.


What's wrong with:

if len(l1[st]) == 0 and len(l2[st]) != 0:
  ...

?

There is _no way_ someone could read that and get the wrong idea.

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


RE: Re: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 30/12/16 23:00, Deborah Swanson wrote:
> > Oops, indentation was messed up when I copied it into the email. 
> > Should be this:
> >
> > if len(l1[st]) == 0:
> > if len(l2[st]) > 0:
> > l1[st] = l2[st]
> > elif len(l2[st]) == 0:
> > if len(l1[st]) > 0:
> > l2[st] = l1[st]
> 
> That's even worse!
> 
> Anyway, ignoring all that, if what you are trying to do is 
> just do some 
> action based on a set of fixed comparisons that are known at 
> the top of 
> the function (and do not mutate depending on the path through the 
> function), then you can just cache the comparisons and then 
> compare the 
> resulting set of boolean results (note that my examples are 
> NOT based on 
> your use-case, it's just pseudo-code which happens to use 
> your expressions):
> 
> state = (len(l1[st]) == 0, len(l2[st]) > 0)
> if state == (True, False):
>pass
> elif state == (False, True):
>pass
> 
> ... etc.
> 
> If the len() comparisons are tri-state (i.e., in some cases 
> you want to 
> know if the length is <, ==, or > 0, depending on one of the other 
> comparisons) then you can do something like:
> 
> def clamp(foo):
>return min(max(-1, foo), 1)
> 
> state = (clamp(cmp(len(l1[st]), 0), cmp(len(l2[st]), 0))
> if state == (0, -1):
>pass
> elif state == (1, -1):
>pass
> 
> ... etc.
> 
> I'm not sure this makes it much more readable though - but if 
> you make 
> the RHS of those comparisons a symbolic name you might be getting 
> somewhere -
> 
> ACTION1 = (0, -1)
> ACTION2 = (1, -1)
> if state == ACTION1:
>pass
> elif state == ACTION2:
>pass
> 
> Hope that helps, E.
> 

Thanks Erik, I really like your method of capturing the state and then
reusing it. I'm sure I can use it in other situations. In this case,
since I now have it down to:

if not len(l1[st]) and len(l2[st]):
l1[st] = l2[st]
elif not len(l2[st]) and len(l1[st]):
l2[st] = l1[st]

I'm not sure caching the state really simplifies that much, although I'm
going to play around with caching the state of all four fields for both
rows, or maybe for all rows to be compared. The code I've been posting
is only for one field and 2 rows, but there are 4 fields to be compared
and there can be up to 10 rows (or more) that are identical except for
the date. Would be nifty to do them all in one swipe. 

It's possible that there are some efficiencies to be found in treating
the 4 field comparisons as one problem. Or maybe I should write a little
function, since the only thing that changes in the comparisons for a set
of rows is the 4 fields. I'm going to try that now, and quite possibly
caching the state would really be useful in that function. Just gobble
them all up in one bite!

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


Re: learning and experimenting python.

2016-12-30 Thread Steve D'Aprano
On Sat, 31 Dec 2016 05:51 am, BartC wrote:

> In usenet posts >>> gets confused for quoted material 3 levels deep
> (which on my newsreader is shown as coloured vertical bars on the left).


Indeed, which is why my Python startup file contains this:



# Change the main prompt. Consider using '≻≻≻ '?
# This is non-portable and may not work everywhere.
if (sys.version_info[0] >= 3 and os.name == 'posix'
and os.environ['TERM'] in ['xterm', 'vt100']):
# Make the prompt bold in Python 3.
sys.ps1 = '\001\x1b[1m\002py> \001\x1b[0m\002'
sys.ps2 = '\001\x1b[1m\002... \001\x1b[0m\002'
else:
sys.ps1 = 'py> '


That gives me a "py>" prompt, bolded in Python 3 and plain in Python 2.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 31/12/16 00:26, Deborah Swanson wrote:
> > As Mr. Bieber points out, what I had above greatly benefits 
> from the 
> > use of conjunctions. It now reads:
> >
> > if not len(l1[st]) and len(l2[st]):
> 
> IMHO, "if not len(l)" is a _terrible_ way of spelling "if 
> len(l) == 0" 
> (mentally, I have to read that as "if length of 'l' is not 
> not equal to 
> 0" - and a double negative won't never cause problems ( ;) )).
> 
> Also, in that particular expression, having to know off the 
> top of their 
> head the precedence of 'not' and 'and' will cause at least some 
> percentage of your maintenance audience in the future to get it wrong.
> 
> What's wrong with:
> 
> if len(l1[st]) == 0 and len(l2[st]) != 0:
>...
> 
> ?

Absolutely nothing wrong, and you're right that it's more readable. I
just think it's cool that Python will do the right thing with not len(a)
and len(a).
 
> There is _no way_ someone could read that and get the wrong idea.
> 
> E.

Quite true.

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


Re: Simulating int arithmetic with wrap-around

2016-12-30 Thread Serhiy Storchaka

On 31.12.16 02:30, Steve D'Aprano wrote:

Are you saying that the best way of doing this is this?

(1) convert signed Python ints to unsigned;

(2) perform operation and bitmask;

(3) convert unsigned back to signed.


Here's an example. I want to multiply 7*3 using a signed 4-bit int, getting
5 as the answer, and 7*4 getting -4 as the answer:


py> N = 4
py> to_signed(N, to_unsigned(N, 7) * to_unsigned(N, 3) & (2**N - 1))
5
py> to_signed(N, to_unsigned(N, 7) * to_unsigned(N, 4) & (2**N - 1))
-4


Step 1 is not needed. And you can inline to_unsigned() in to_signed(). I 
introduced it just for clearness.


py> to_signed(N, 7 * 3)
5
py> to_signed(N, 7 * 4)
-4


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


Re: learning and experimenting python.

2016-12-30 Thread eryk sun
On Fri, Dec 30, 2016 at 7:49 PM, Michael Torrie  wrote:
>
> Is there a special reason bourne shell uses $ and #?

To me, "$"  is for the [$]tandard shell prompt, and "#" noticeably
distinguishes root shells.

> Coming from an old DOS background (>) I found that rather jarring at first.

DOS is a single-user system with no security, so it doesn't need to
distinguish standard and root prompts.

In modern Windows, cmd and PowerShell change the console title instead
of the prompt. The title starts with "Administrator: " if the user is
an administrator (i.e. the BUILTIN\Administrators group is present and
enabled in the process token).

> You say "%" is for "other shell."  Which shells?  *Any* other shell?

"%" is the standard prompt for csh. I think the legacy Thompson shell
also used it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Wildman via Python-list
On Fri, 30 Dec 2016 15:34:16 -0800, einstein1410 wrote:

> You are also confusing me.
> But there mustbe  some reason.
> What happens if your student questions you like this.?

I am not a teacher.

> And may be those who questions like this will surely be the developer of its 
> successor language.
> Because out of thousands, only one may asks this, whom you all will consider 
> fool, but he's the only genius.

To get an answer to your questions you need to ask the developer.  I don't
think I can get any clearer.

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Wildman via Python-list
On Fri, 30 Dec 2016 23:39:43 +, Erik wrote:

> On 30/12/16 23:34, [email protected] wrote:
>> You are also confusing me.
>> But there mustbe  some reason.
>> What happens if your student questions you like this.?
>> And may be those who questions like this will surely be the developer of its 
>> successor language.
>> Because out of thousands, only one may asks this, whom you all will consider 
>> fool, but he's the only genius
> 
> Do not feed the troll.
> 
> E.

Please explain how what I said is trolling.  Perhaps it was a little
snide but I tend to get that way when trying to explain the obvious.

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Michael Torrie
On 12/30/2016 06:46 PM, eryk sun wrote:
> On Fri, Dec 30, 2016 at 7:49 PM, Michael Torrie  wrote:
>>
>> Is there a special reason bourne shell uses $ and #?
> 
> To me, "$"  is for the [$]tandard shell prompt, and "#" noticeably
> distinguishes root shells.

Yes of course. You missed my point completely.  These prompts have
meaning only because they were picked arbitrarily.  Bourne shell could
well have used # for user prompt and $ for the root prompt.

>> Coming from an old DOS background (>) I found that rather jarring at first.
> 
> DOS is a single-user system with no security, so it doesn't need to
> distinguish standard and root prompts.

Again I think you missed my point. Or you read more into it than I gave.
I was not confused by the unix prompts. I understood the difference
between root and normal user.  Rather I liked the appearance of the
Windows-style prompt simply because I was used to it.

Now I prefer the bash prompt and I greatly value the different root
prompt character.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Michael Torrie
On 12/30/2016 04:26 PM, [email protected] wrote:
> That's not the answer.
> If you don't have answer, please don't answer like this, because that will 
> confuse others also.

I don't believe anyone will be confused.

Clearly there's no answer that you understand, or one that would satisfy
you.  There could be good reasons for choosing >>>.  Or there could be
no reason at all.  Maybe Guido liked the > but to make it a bit more
unique, made it >>>.  Maybe he had a dream and saw >>> in the sky.  Or
maybe he just got the idea from xkcd.  Why does this matter?  We've
given you a plethora of rationale and possible ideas why it is this way.
 Just pick one, any one, and there's your answer.

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


Re: learning and experimenting python.

2016-12-30 Thread Michael Torrie
On 12/30/2016 07:05 PM, Wildman via Python-list wrote:
> On Fri, 30 Dec 2016 23:39:43 +, Erik wrote:
> 
>> On 30/12/16 23:34, [email protected] wrote:
>>> You are also confusing me.
>>> But there mustbe  some reason.
>>> What happens if your student questions you like this.?
>>> And may be those who questions like this will surely be the developer of 
>>> its successor language.
>>> Because out of thousands, only one may asks this, whom you all will 
>>> consider fool, but he's the only genius
>>
>> Do not feed the troll.
>>
>> E.
> 
> Please explain how what I said is trolling.  Perhaps it was a little
> snide but I tend to get that way when trying to explain the obvious.

Hmm. I thought he was referring to einstein1410...  It was his message
he was replying to, not yours, and I took it as a request to the rest of
us (sorry, Erik--couldn't resist posting).

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


Re: learning and experimenting python.

2016-12-30 Thread Ethan Furman

On 12/30/2016 06:05 PM, Wildman via Python-list wrote:

On Fri, 30 Dec 2016 23:39:43 +, Erik wrote:



Do not feed the troll.


Please explain how what I said is trolling.  Perhaps it was a little
snide but I tend to get that way when trying to explain the obvious.


I suspect Erik was referring to "einstein1410", who seems to be wasting time 
and bandwidth on silly questions.

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


Re: Cleaning up conditionals

2016-12-30 Thread Michael Torrie
On 12/30/2016 05:26 PM, Deborah Swanson wrote:
> I'm still wondering if these 4 lines can be collapsed to one or two
> lines.

If the logic is clearly expressed in the if blocks that you have, I
don't see why collapsing an if block into one or two lines would even be
desirable.  Making a clever one-liner out of something isn't always a
good thing.  In fact some programmers don't like to use the ternary
operator or conditional expressions, preferring to use explicit if block
logic.

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


Re: Cleaning up conditionals

2016-12-30 Thread MRAB

On 2016-12-31 00:48, Deborah Swanson wrote:

On 30/12/16 23:00, Deborah Swanson wrote:
> Oops, indentation was messed up when I copied it into the email.
> Should be this:
>
>if len(l1[st]) == 0:
> if len(l2[st]) > 0:
> l1[st] = l2[st]
> elif len(l2[st]) == 0:
> if len(l1[st]) > 0:
> l2[st] = l1[st]

That's even worse!

Anyway, ignoring all that, if what you are trying to do is
just do some
action based on a set of fixed comparisons that are known at
the top of
the function (and do not mutate depending on the path through the
function), then you can just cache the comparisons and then
compare the
resulting set of boolean results (note that my examples are
NOT based on
your use-case, it's just pseudo-code which happens to use
your expressions):

state = (len(l1[st]) == 0, len(l2[st]) > 0)
if state == (True, False):
   pass
elif state == (False, True):
   pass

... etc.

If the len() comparisons are tri-state (i.e., in some cases
you want to
know if the length is <, ==, or > 0, depending on one of the other
comparisons) then you can do something like:

def clamp(foo):
   return min(max(-1, foo), 1)

state = (clamp(cmp(len(l1[st]), 0), cmp(len(l2[st]), 0))
if state == (0, -1):
   pass
elif state == (1, -1):
   pass

... etc.

I'm not sure this makes it much more readable though - but if
you make
the RHS of those comparisons a symbolic name you might be getting
somewhere -

ACTION1 = (0, -1)
ACTION2 = (1, -1)
if state == ACTION1:
   pass
elif state == ACTION2:
   pass

Hope that helps, E.



Thanks Erik, I really like your method of capturing the state and then
reusing it. I'm sure I can use it in other situations. In this case,
since I now have it down to:

if not len(l1[st]) and len(l2[st]):
l1[st] = l2[st]
elif not len(l2[st]) and len(l1[st]):
l2[st] = l1[st]


[snip]

An empty list is considered falsey and a non-empty list is considered truey:

>>> bool([])
False
>>> bool([0])
True

You don't care what the list's length is, only whether it's 0 or not, so:

if not l1[st] and l2[st]:
l1[st] = l2[st]
elif not l2[st] and l1[st]:
l2[st] = l1[st]

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


Re: Cleaning up conditionals

2016-12-30 Thread Cameron Simpson

On 30Dec2016 15:17, Deborah Swanson  wrote:

Ever consider using conjunctions?

if len(l1[st]) and not len(l2[st]):
#0 is considered a false -- no need to test for "==0"
#non-0 is considered true -- no need to test for ">0"
#copy l1 to l2
elif not len(l1[st]) and len(l2[st]):
#copy l2 to l1
--
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.home.netcom.com/


That's a neat shortcut, len(a) instead of len(a)!= 0.  Thanks!


Also, for almost every python collection (lists, tuples, sets etc), python 
boolean logic tests __nonzero__, which works off len() by default.


So:

 if a:
   # a is not empty: len(a) > 0
 else:
   # a is empty: len(a) == 0

I find this far more readable, presuming the reader knows that "empty" things 
test as false. Of course, you need to ensure that any "collection"-ish classes 
you use or write have this property, but the builtin ones do.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Ethan,
If you think I am wasting time, don't read my posts.
Why wasting your time in reading my post.

Are you having the answer of my question?
Till now no one is able to give answer, everybody just assuming something and 
answering. No answer is perfect that satisfy why it uses >>>. Except lan's 
answer.


Lan told that it is successor of ABC that's why it uses the >>> style.
Now I got my answer. But next question is Why it for ABC?
I know this is python group but it's reasonable doubt. If no doubts like this 
arises in your mind then you are just doing programming like Robot does and 
compiler converts the code in to machine language, it does not understand.
Same you are doing man.


Once again thanks a lot LAN for satisfiable answer.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Thanks once again.
Its only you who gave me the satisfied answer.
Now it's my task to identify the reason why ABC used >>> ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Sorry everyone it's not LAN, its David Rogers.
Please accept my apology.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread einstein1410
Thanks everyone. 
My all posted messages will be deleted within 1 hour.
I got my answer, its time to good bye.
If new questions arise then I will put it by creating new topic.
Good Bye all.
Once again thanks David Froger for your answer.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Chris Angelico
On Sat, Dec 31, 2016 at 12:46 PM, eryk sun  wrote:
> In modern Windows, cmd and PowerShell change the console title instead
> of the prompt. The title starts with "Administrator: " if the user is
> an administrator (i.e. the BUILTIN\Administrators group is present and
> enabled in the process token).

I'd much rather the prompt change. The title isn't always visible, and
even if it is, it isn't always obvious. When I'm ssh'ing around the
network, changing the title is a small convenience (since that means
the "running programs" list shows who I'm logged in as), but changing
the prompt is critical, because I see that right there as I start
typing. Fortunately, every Unix shell I've ever used lets me have both
- title AND prompt - so there need be no mistake.

(Of course, that still doesn't stop me fat-fingering and bringing
services down on the live system instead of the test...)

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


Re: Re: Cleaning up conditionals

2016-12-30 Thread MRAB

On 2016-12-31 01:59, Cameron Simpson wrote:

On 30Dec2016 15:17, Deborah Swanson  wrote:

Ever consider using conjunctions?

if len(l1[st]) and not len(l2[st]):
#0 is considered a false -- no need to test for "==0"
#non-0 is considered true -- no need to test for ">0"
#copy l1 to l2
elif not len(l1[st]) and len(l2[st]):
#copy l2 to l1
--
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.home.netcom.com/


That's a neat shortcut, len(a) instead of len(a)!= 0.  Thanks!


Also, for almost every python collection (lists, tuples, sets etc), python
boolean logic tests __nonzero__, which works off len() by default.


[snip]

For Python 2, it tries __nonzero__, or __len__ if that fails.

For Python 3, it tries __bool__, or __len__ if that fails.

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


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 30Dec2016 15:17, Deborah Swanson  wrote:
> >>Ever consider using conjunctions?
> >>
> >>if len(l1[st]) and not len(l2[st]):
> >>#0 is considered a false -- no need to test for "==0"
> >>#non-0 is considered true -- no need to test for ">0"
> >>#copy l1 to l2
> >>elif not len(l1[st]) and len(l2[st]):
> >>#copy l2 to l1
> >> --
> >>Wulfraed Dennis Lee Bieber AF6VN
> >> [email protected]://wlfraed.home.netcom.com/
> >
> >That's a neat shortcut, len(a) instead of len(a)!= 0.  Thanks!
> 
> Also, for almost every python collection (lists, tuples, sets 
> etc), python 
> boolean logic tests __nonzero__, which works off len() by default.
> 
> So:
> 
>   if a:
> # a is not empty: len(a) > 0
>   else:
> # a is empty: len(a) == 0
> 
> I find this far more readable, presuming the reader knows 
> that "empty" things 
> test as false. Of course, you need to ensure that any 
> "collection"-ish classes 
> you use or write have this property, but the builtin ones do.
> 
> Cheers,
> Cameron Simpson 

Another neat thing to know, and yes, that's much more readable. I've run
into trouble testing for empty (tests failed when they shouldn't have),
but I can't remember where I've had that problem, and since it happened
early in my learning python, chances are pretty good I screwed something
else up.  Thanks, I'll remember to try using it again  and see if I can
get it right.
 

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


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
> On 2016-12-31 00:48, Deborah Swanson wrote:
> >> On 30/12/16 23:00, Deborah Swanson wrote:
> >> > Oops, indentation was messed up when I copied it into the email. 
> >> > Should be this:
> >> >
> >> >  if len(l1[st]) == 0:
> >> > if len(l2[st]) > 0:
> >> > l1[st] = l2[st]
> >> > elif len(l2[st]) == 0:
> >> > if len(l1[st]) > 0:
> >> > l2[st] = l1[st]
> >>
> >> That's even worse!
> >>
> >> Anyway, ignoring all that, if what you are trying to do is just do 
> >> some action based on a set of fixed comparisons that are known at
> >> the top of
> >> the function (and do not mutate depending on the path through the
> >> function), then you can just cache the comparisons and then
> >> compare the
> >> resulting set of boolean results (note that my examples are
> >> NOT based on
> >> your use-case, it's just pseudo-code which happens to use
> >> your expressions):
> >>
> >> state = (len(l1[st]) == 0, len(l2[st]) > 0)
> >> if state == (True, False):
> >>pass
> >> elif state == (False, True):
> >>pass
> >>
> >> ... etc.
> >>
> >> If the len() comparisons are tri-state (i.e., in some 
> cases you want 
> >> to know if the length is <, ==, or > 0, depending on one 
> of the other
> >> comparisons) then you can do something like:
> >>
> >> def clamp(foo):
> >>return min(max(-1, foo), 1)
> >>
> >> state = (clamp(cmp(len(l1[st]), 0), cmp(len(l2[st]), 0))
> >> if state == (0, -1):
> >>pass
> >> elif state == (1, -1):
> >>pass
> >>
> >> ... etc.
> >>
> >> I'm not sure this makes it much more readable though - but if you 
> >> make the RHS of those comparisons a symbolic name you might be 
> >> getting somewhere -
> >>
> >> ACTION1 = (0, -1)
> >> ACTION2 = (1, -1)
> >> if state == ACTION1:
> >>pass
> >> elif state == ACTION2:
> >>pass
> >>
> >> Hope that helps, E.
> >>
> >
> > Thanks Erik, I really like your method of capturing the 
> state and then 
> > reusing it. I'm sure I can use it in other situations. In 
> this case, 
> > since I now have it down to:
> >
> > if not len(l1[st]) and len(l2[st]):
> > l1[st] = l2[st]
> > elif not len(l2[st]) and len(l1[st]):
> > l2[st] = l1[st]
> >
> [snip]
> 
> An empty list is considered falsey and a non-empty list is 
> considered truey:
> 
>  >>> bool([])
> False
>  >>> bool([0])
> True
> 
> You don't care what the list's length is, only whether it's 0 
> or not, so:
> 
> if not l1[st] and l2[st]:
>  l1[st] = l2[st]
> elif not l2[st] and l1[st]:
>  l2[st] = l1[st]
> 

Similar, actually the same as Cameron suggested. I really need to
revisit testing for empty. I probably rejected it early on for some bad
reason (you don't understand everything that goes wrong when you're
learning).

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


RE: Cleaning up conditionals

2016-12-30 Thread Deborah Swanson
Michael Torrie wrote:
> On 12/30/2016 05:26 PM, Deborah Swanson wrote:
> > I'm still wondering if these 4 lines can be collapsed to one or two 
> > lines.
> 
> If the logic is clearly expressed in the if blocks that you 
> have, I don't see why collapsing an if block into one or two 
> lines would even be desirable.  Making a clever one-liner out 
> of something isn't always a good thing.  In fact some 
> programmers don't like to use the ternary operator or 
> conditional expressions, preferring to use explicit if block logic.
> 

Maybe it isn't always a good thing, but learning the capabilities of
python is. Besides, if the concern is future maintenance, a lot would
depend on the proficiency of those expected to maintain the code.

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


Re: learning and experimenting python.

2016-12-30 Thread Ian Kelly
On Fri, Dec 30, 2016 at 6:05 PM,   wrote:
> But in his website, he recommended that post your questions here, he will 
> answer it.
> But still as you told me I will send him an personal e-mail.

This is a good place for asking questions about Python, but you should
know that Guido himself doesn't usually post here.

> And if this is unimportant, as you thought, then you must not be the part of 
> this group. This group is to share knowledge, nott for deciding what is 
> trivia and what's not.

The information that you're asking for has no practical utility, hence
trivia. I wasn't passing judgment on your question; I was trying to
explain why it's unlikely that anybody would know the answer.

> My all posted messages will be deleted within 1 hour.

What makes you think that? This is an unmoderated Usenet group. The
only reason anything ever gets deleted from the archives is if it's
libelous spam. This is also distributed as a mailing list, and once
the email goes out, there's no unsending it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: List comprehension

2016-12-30 Thread Jason Friedman
> data = (
>> ... (1,2),
>> ... (3,4),
>> ... )
>>
> [x,y for a in data]
>>   File "", line 1
>> [x,y for a in data]
>>^
>> SyntaxError: invalid syntax
>>
>> I expected:
>> [(1, 2), (3, 4)]
>
>
> Why would you expect that? I would expect the global variables x and y, or
> if they don't exist, a NameError:

Thank you Steve (and everyone) for your answers.

In answer to Steve's question above, it's because I can do this:
>>> for x,y in data:
... pass
...
>>>

But, anyway, I see my error now, and I'm good to go.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: List comprehension

2016-12-30 Thread Terry Reedy

On 12/30/2016 2:37 PM, Jason Friedman wrote:


Now, this puzzles me:


[x,y for a in data]

  File "", line 1
[x,y for a in data]
   ^
SyntaxError: invalid syntax


I believe that python begins to parse this as
[x, (y for a in data)], a list of 2 items,
except that the required () are missing.
Notice that the ^ is under the r of 'for'.
"y for" is not a legal beginning of a list item.
Most tuples need ()s for proper grouping.



--
Terry Jan Reedy

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


Re: learning and experimenting python.

2016-12-30 Thread Steve D'Aprano
On Sat, 31 Dec 2016 10:26 am, [email protected] wrote:

> That's not the answer.
> If you don't have answer, please don't answer like this, because that will
> confuse others also.

What's not the answer? What's the question?


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Problem with running python 3.6.0 on a 32 bit windows 7 ultimate operating system.

2016-12-30 Thread Sagar Utlas
I am new to python, I've been using C++ as as a student till last 3 years.
To expand my knowledge, I installed Python 3.6.0 and when tried to open it,
a pop up window appeared saying- "The program can't start because
api-ms-win-crt-runtime-|1-1-0.dll is missing from your computer. try
reinstalling the program to fix this problem."
I tried repairing the software using the setup, but again it was the same.
What else can I do to run Python on my system.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Wildman via Python-list
On Fri, 30 Dec 2016 19:23:17 -0700, Michael Torrie wrote:

> On 12/30/2016 07:05 PM, Wildman via Python-list wrote:
>> On Fri, 30 Dec 2016 23:39:43 +, Erik wrote:
>> 
>>> On 30/12/16 23:34, [email protected] wrote:
 You are also confusing me.
 But there mustbe  some reason.
 What happens if your student questions you like this.?
 And may be those who questions like this will surely be the developer of 
 its successor language.
 Because out of thousands, only one may asks this, whom you all will 
 consider fool, but he's the only genius
>>>
>>> Do not feed the troll.
>>>
>>> E.
>> 
>> Please explain how what I said is trolling.  Perhaps it was a little
>> snide but I tend to get that way when trying to explain the obvious.
> 
> Hmm. I thought he was referring to einstein1410...  It was his message
> he was replying to, not yours, and I took it as a request to the rest of
> us (sorry, Erik--couldn't resist posting).

I took it to mean that he was telling einstein1410 to not feed
the trolls.  If I am wrong then my apologies to Erik.

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: learning and experimenting python.

2016-12-30 Thread Steve D'Aprano
On Sat, 31 Dec 2016 02:24 pm, [email protected] wrote:

> Ethan,
> If you think I am wasting time, don't read my posts.
> Why wasting your time in reading my post.
> 
> Are you having the answer of my question?
> Till now no one is able to give answer, everybody just assuming something
> and answering. No answer is perfect that satisfy why it uses >>>. 

Guido picked >>> because he likes the look of it. When you create your own
programming language, you can pick any prompt you like. Different programs,
shells and languages use many different styles of prompts:

Python: >>>
iPython:In [1]:
Ruby (irb): irb(main):001:0>
bash:   [steve@ando ~]$
bash (root):[steve@ando ~]#
DOS:>
rhino:  js>
lua:>
julia:  julia>
xion:   >
mutt:   :
ed: (no prompt)
zsh:[steve@ando]~%


It is completely a matter of personal taste.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Problem with running python 3.6.0 on a 32 bit windows 7 ultimate operating system.

2016-12-30 Thread Ian Kelly
On Fri, Dec 30, 2016 at 10:53 PM, Sagar Utlas  wrote:
> I am new to python, I've been using C++ as as a student till last 3 years.
> To expand my knowledge, I installed Python 3.6.0 and when tried to open it,
> a pop up window appeared saying- "The program can't start because
> api-ms-win-crt-runtime-|1-1-0.dll is missing from your computer. try
> reinstalling the program to fix this problem."
> I tried repairing the software using the setup, but again it was the same.
> What else can I do to run Python on my system.

http://stackoverflow.com/questions/33265663/api-ms-win-crt-runtime-l1-1-0-dll-is-missing-when-opening-microsoft-office-file
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cleaning up conditionals

2016-12-30 Thread Jussi Piitulainen
"Deborah Swanson"  writes:

> Michael Torrie wrote:
>> On 12/30/2016 05:26 PM, Deborah Swanson wrote:
>> > I'm still wondering if these 4 lines can be collapsed to one or two 
>> > lines.
>> 
>> If the logic is clearly expressed in the if blocks that you 
>> have, I don't see why collapsing an if block into one or two 
>> lines would even be desirable.  Making a clever one-liner out 
>> of something isn't always a good thing.  In fact some 
>> programmers don't like to use the ternary operator or 
>> conditional expressions, preferring to use explicit if block logic.
>> 
>
> Maybe it isn't always a good thing, but learning the capabilities of
> python is. Besides, if the concern is future maintenance, a lot would
> depend on the proficiency of those expected to maintain the code.

One line:

l1[st], l2[st] = (l1[st] or l2[st]), (l2[st] or l1[st])

(The parentheses are redundant.)

Two lines:

l1[st] = l1[st] or l2[st]
l2[st] = l2[st] or l1[st]

Both of these store the same value back in the given field if it's
considered true (non-empty), else they store the corresponding value
from the other record in the hope that it would be considered true
(non-empty).
-- 
https://mail.python.org/mailman/listinfo/python-list