Re: copy and paste with a program called clipboard

2018-07-20 Thread Terry Reedy

On 7/19/2018 9:29 PM, eryk sun wrote:

On Thu, Jul 19, 2018 at 5:42 PM, Terry Reedy  wrote:


What happens when you right click depends on the program you are interacting
with.  When you select and right-click in the current Windows 10 console,
used for Command Prompt, PowerShell, Python, and other programs, the
selection disappears.  I just discovered that it also copies to the
clipboard.  I also just discovered that right click without a selection
pastes the current clipboard contents.  (I usually use ^X, ^C, and ^V.)
Since the console is in the process of being improved, I have no idea how
long this has been true.


Prior to Windows 10, the console uses either "quick-edit" or "mark"
mode for text selection.

If "QuickEdit Mode" is selected in the console properties, mouse
events are consumed internally by the console to support text
selection. When text is selected, the enter key or right-click copies
it. Other keys, including Ctrl+C, cancel the selection. If no text is
selected, then right-click pastes the contents of the clipboard. You
can also select "Copy" and "Paste" from the edit menu. Quick-edit mode
can cause problems since it's easy to accidentally select text, which
causes most console functions (e.g. reading from the input buffer) to
block until the selection is copied or cancelled. Quick-edit mode also
incompatible with text-interface applications that require mouse
input.

If quick-edit mode is disabled, by default right-click should display
the edit menu, which has a "Mark" command. In mark mode you can use
the cursor keys (arrows, home, end, page up/down) to position the
cursor, and hold shift plus the cursor keys to select text. You can
also use the mouse to select and copy text in mark mode.

Windows 10

In the new console, selecting "enable Ctrl key shortcuts" enables
special support for control characters. In this mode, the old control
characters can still be entered by including the shift key (e.g.
Ctrl+Shift+V to get ^V). Without shift, we have, for example, Ctrl+F
to find text, Ctrl+M to enter mark mode, Ctrl+A to select all text,
and Ctrl+V to paste from the clipboard. Regardless of this setting, if
text is selected, Ctrl+C copies it to the clipboard. Otherwise, with
the default settings and no text selected, Ctrl+C sends a CTRL_C_EVENT
to all processes attached to the console.

If "extended text selection keys" is selected, text can be selected
starting at the current cursor position using the cursor keys while
holding shift. Mouse selection is also supported after you start
selecting text with the keyboard. It's like mark mode, except only
from the current cursor position.

If "enable line wrapping selection" is enabled, text is selected by
line and wraps as if the screen buffer were a continuous line. Hold
Alt to get the classic rectangle selection. The Alt key actually acts
like a toggle here. If "enable line wrapping selection" is not
selected, then the default is rectangle mode and holding Alt switches
to line mode.


It would be nice if this info could be access from within the Console 
itself.



--
Terry Jan Reedy

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


Re: Edit with IDLE pull down menu

2018-07-20 Thread Terry Reedy

On 7/19/2018 5:40 PM, [email protected] wrote:


The option for Edit with IDLE has an expanding menu.  The only item in
the pull down menu is..Edit with IDLE.


What I see is Edit with IDLE 3.7.  There should be an entry for each 
Python installed.  Since that is the reason for the 2nd menu, and used 
to be the case, it is a bug that there is not.  I will later check the 
tracker and maybe open an issue.



So to run a .py program, you have to right click and select the pull
down menu Edit with IDLE and then inside that menu you pick Edit with
IDLE.  Seems a little redundant.




--
Terry Jan Reedy

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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Brian Oney via Python-list
On Fri, 2018-07-20 at 06:37 +, Steven D'Aprano wrote:
> On Fri, 20 Jul 2018 08:25:04 +0200, Brian Oney via Python-list wrote:
> 
> > PS: Can I twiddle bits in Python?
> 
> Yes.
> 
> These operators work on ints:
> 
>   bitwise AND:  &
>   bitwise OR:   |
>   bitwise XOR:  ^
> 
That's right I had forgotten about that. Thank you for the quick answer.Some 
fun:$ ipythonPython 2.7.13 (default, Nov 24 2017, 17:33:09) ...In [1]: j = 16; 
i = 1
In [2]: print(i+j); print(i|j)1717
In [3]: %timeit i+j1000 loops, best of 3: 65.8 ns per loop
In [4]: %timeit i|j1000 loops, best of 3: 73 ns per loop
In [5]: %timeit 16|11000 loops, best of 3: 28.8 ns per loop
In [6]: %timeit 16+11000 loops, best of 3: 28.8 ns per loop
I wonder why the difference between [3] and [4]. My mental ranking of speed of 
operations tells me it should be the other way around.
Are 16|1 and 16+1 internally the same operation (for integers)? 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Bit twiddling homework

2018-07-20 Thread xffox
On Fri, Jul 20, 2018 at 08:25:04AM +0200, Brian Oney via Python-list wrote:
> Therefore, what book or learning course do you recommend? I imagine something 
> that tours or skims
> the fundamentals of Boolean algebra and digital logic, and then goes to C and 
> some fun homework
> problems. It may seem to you that the emphasis there is wrongly placed. 

"Hacker's Delight" (please don't judge by the name):
https://en.wikipedia.org/wiki/Hacker's_Delight . It's a collection of
bit-level hacks. Figuring out each trick before reading the solution can
provide a list of fun homework problems. Hope it helps.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Bit twiddling homework

2018-07-20 Thread Chris Angelico
On Fri, Jul 20, 2018 at 7:00 PM, Brian Oney via Python-list
 wrote:
> On Fri, 2018-07-20 at 06:37 +, Steven D'Aprano wrote:
>> On Fri, 20 Jul 2018 08:25:04 +0200, Brian Oney via Python-list wrote:
>>
>> > PS: Can I twiddle bits in Python?
>>
>> Yes.
>>
>> These operators work on ints:
>>
>>   bitwise AND:  &
>>   bitwise OR:   |
>>   bitwise XOR:  ^
>>
> That's right I had forgotten about that. Thank you for the quick answer.Some 
> fun:$ ipythonPython 2.7.13 (default, Nov 24 2017, 17:33:09) ...In [1]: j = 
> 16; i = 1
> In [2]: print(i+j); print(i|j)1717
> In [3]: %timeit i+j1000 loops, best of 3: 65.8 ns per loop
> In [4]: %timeit i|j1000 loops, best of 3: 73 ns per loop
> In [5]: %timeit 16|11000 loops, best of 3: 28.8 ns per loop
> In [6]: %timeit 16+11000 loops, best of 3: 28.8 ns per loop
> I wonder why the difference between [3] and [4]. My mental ranking of speed 
> of operations tells me it should be the other way around.
> Are 16|1 and 16+1 internally the same operation (for integers)?

What you're seeing is nothing but noise. With numbers this low, you
can't really judge anything. In fact, operations 5 and 6 are probably
just looking up constants that got calculated once, so they're
literally proving nothing at all.

Even at the CPU level, you'll generally find that adding two numbers
takes the same amount of time as bitwise Or, mainly because both of
them take a single clock cycle. (Proving that they actually DON'T take
the same amount of time requires a fairly deep understanding of the
internal workings of the chip.) Definitely at the Python level, the
costs are virtually identical. Don't do bitwise operations for the
sake of performance; do them because they clearly and adequately
describe what you're doing. :)

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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Ben Bacarisse
Brian Oney  writes:

> On Fri, 2018-07-20 at 06:37 +, Steven D'Aprano wrote:
>> On Fri, 20 Jul 2018 08:25:04 +0200, Brian Oney via Python-list wrote:
>> 
>> > PS: Can I twiddle bits in Python?
>> 
>> Yes.
>> 
>> These operators work on ints:
>> 
>>   bitwise AND:  &
>>   bitwise OR:   |
>>   bitwise XOR:  ^
>> 
> That's right I had forgotten about that.

There's also ~ for bitwise negation.  Rather than explain what this
means in a language with an unbounded integer type, you might like to
experiment, er, a bit.  (Excuse the pun.)

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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Brian Oney via Python-list
On Fri, 2018-07-20 at 18:07 +0900, xffox wrote:
> On Fri, Jul 20, 2018 at 08:25:04AM +0200, Brian Oney via Python-list wrote:
> > Therefore, what book or learning course do you recommend? I imagine 
> > something that tours or skims
> > the fundamentals of Boolean algebra and digital logic, and then goes to C 
> > and some fun homework
> > problems. It may seem to you that the emphasis there is wrongly placed. 
> 
> "Hacker's Delight" (please don't judge by the name):
> https://en.wikipedia.org/wiki/Hacker's_Delight . It's a collection of
> bit-level hacks. Figuring out each trick before reading the solution can
> provide a list of fun homework problems. Hope it helps.
That's what I am looking for. Thank you so much. Have a nice weekend!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Bit twiddling homework

2018-07-20 Thread Grant Edwards
On 2018-07-20, Dennis Lee Bieber  wrote:

> While I suspect Python isn't micro-optimizing, take into account
> that most processors do have an "increment"/"decrement" operation --
> since that is done so much at the low-level. Also, just general
> integer addition is common, so the hardware may be optimized for
> doing them fast. Boolean operations may not be as well optimized.

Boolean operations are also very common at the lowest level, and they
involve far simpler logic than does addition.  I refuse to believe
there's an extant processor in common use where an ADD is faster than
an OR unless somebody shows me the processor spec sheet.

-- 
Grant Edwards   grant.b.edwardsYow! These PRESERVES should
  at   be FORCE-FED to PENTAGON
  gmail.comOFFICIALS!!

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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Chris Angelico
On Sat, Jul 21, 2018 at 1:14 AM, Grant Edwards
 wrote:
> On 2018-07-20, Dennis Lee Bieber  wrote:
>
>> While I suspect Python isn't micro-optimizing, take into account
>> that most processors do have an "increment"/"decrement" operation --
>> since that is done so much at the low-level. Also, just general
>> integer addition is common, so the hardware may be optimized for
>> doing them fast. Boolean operations may not be as well optimized.
>
> Boolean operations are also very common at the lowest level, and they
> involve far simpler logic than does addition.  I refuse to believe
> there's an extant processor in common use where an ADD is faster than
> an OR unless somebody shows me the processor spec sheet.
>

"Faster than"? I'd agree with you. But "as fast as"? I believe that's
how most modern CPUs already operate. (Well, mostly.) There are
sophisticated methods of daisy-chaining the carry bit that mean the
overall addition can be performed remarkably quickly, and the end
result is a one-clock ADD operation, same as OR. For most data, most
code, and most situations, integer addition is exactly as fast as
integer bit shift.

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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Brian Oney via Python-list
On Fri, 2018-07-20 at 10:38 -0400, Dennis Lee Bieber wrote:
> On Fri, 20 Jul 2018 11:00:09 +0200, Brian Oney via Python-list
>  declaimed the following:
> 
> > Are 16|1 and 16+1 internally the same operation (for integers)? 
> 
>   For those integers the EFFECT/RESULT will be the same. But...
> 
> > > > 17 + 1
> 
> 18
> > > > 17 | 1
> 
> 17
Of course. There was a reason I chose those integers and timed them. I am 
ignorant of how Python stores an integer internally. Either way (2^x bit) the 
result would be the same regardless.
In retrospect, I seem to suggest that Python would have different sets of 
operations for an integer based on it's value; that's poor wording. Anyways.
 
> {From the original post}
> > For example, bitwise operators are neat (here a shift):
> > C:
> >  int res = 1 << 4 
> >  printf("%d\n", res)
> > 16
> > 
> > Translates to pseudocrap:
> >  0001 -> leftwards bitshift of 4 places -> 0001 
> > 
> 
>   Python also supports shift...
> 
> > > > 17 << 2
> 
> 68
> > > > 17 >> 2
> 
> 4
Nice! Once again Python shines as a prototyping language!
> [Side comment, even in C bitwise operators aren't really considered playing
> "directly with memory". That would be more something like:
> 
>   int * i;// i is a pointer (address) to an 
> integer
>   i = 0x01FF; // set i to access address 511
>   printf("%d\n", *i); // print (as integer) whatever is stored at 511
> 
> THAT you can not do in Python (at least, not without using something like
> the ctypes library).]
> 
Thank you for the explanation. That is a nice example.
>   Hex is basically just a means of shortening a binary string by
> representing runs of 4 bits using a single symbol. Same with Octal (runs of
> 3 bits). In both cases, each symbol translates to an integral number of
> bits, and position in a multi-symbol value can be translated directly.
> 
>   This is not possible when representing binary values in Decimal, as
> each place in the decimal representation does not map to an integral number
> of bits.
> 
Yup! Wikipedia has the same info scattered about the hexadecimal page. You 
describe it nicely from the perspective of a computer scientist.
> > Therefore, what book or learning course do you recommend? I imagine 
> > something that tours or skims
> > the fundamentals of Boolean algebra and digital logic, and then goes to C 
> > and some fun homework
> > problems. It may seem to you that the emphasis there is wrongly placed. 
> 
>   While Boolean algebra and digital logic are core concepts for
> computers, neither really appear in books that teach programming. Boolean
> logic is more in the realms of pure math and then feeds to digital logic
> (at the gate level -- NAND, NOR, AND, OR gates and combinations to create
> things like adders). Consider de Morgan's theorem:
> 
> A & B =>  not(not A | not B)
> A | B =>  not(not A & not B)
> 
> > > > b = 24
> > > > a = 19
> > > > a & b
> 
> 16
> > > > a | b
> 
> 27
> > > > a ^ b
> 
> 11
> > > > ~(~a & ~b)
> 
> 27
> > > > ~(~a | ~b)
> 
> 16
Thank you for the advice. Those are also some nice examples. Cool, I can do 
this stuff in Python. I guess this is also why Python is so popular; an 
instructor can take one language and cover a ton of material without having to 
saddle students with learning another language.
>   Can't really help with suggesting books -- The ones from my college
> days are likely out-of-print; I haven't really had a need to buy newer
> books for such basic concepts. Even simple discrete 74xx chips are so rare
> these days that the idea of creating a four-bit ADDER from gates is hard to
> find.
> 
>   Parallax used to have a slew of educational stuff based on BASIC Stamps
> and a logic simulator (and project board), but neither appear to still be
> in their catalog (the books used to be available as PDF for most of their
> educational stuff). I did find a copy on a third-party site for the Digital
> Logic intro
> http://rambal.com/index.php?controller=attachment&id_attachment=686&usg=AOvVaw3YI9p_blIFAgardqgB2SBq
> 
> Can't find a download for their logical simulator, but there is something
> that might be usable at 
> https://sourceforge.net/projects/cedarlogic/
> 
> 
>   Just to put this back on topic for the group...
> 
>   Pretty much all of what you ask can be learned in Python (and using the
> interactive interpreter is a lot faster than writing a C program, compiling
> it, etc. -- as can be seen by the cut&paste examples above). It is only
> direct hardware access that is not something Python is really suited for
> (although AdaFruit has a few boards which run CircuitPython
> [MicroPython+special libraries] if you really want to play with LEDs and
> such -- though they still don't access raw memory). If you really want
> hardware access -- as in the processor registers, memory, etc. -- you are
> probably talking about something like Arduino (AVR for Uno/Mega; ARM

Re: [OT] Bit twiddling homework

2018-07-20 Thread Marko Rauhamaa
Chris Angelico :

> On Sat, Jul 21, 2018 at 1:14 AM, Grant Edwards
>  wrote:
>> I refuse to believe there's an extant processor in common use where
>> an ADD is faster than an OR unless somebody shows me the processor
>> spec sheet.
>
> "Faster than"? I'd agree with you. But "as fast as"? I believe that's
> how most modern CPUs already operate. (Well, mostly.) There are
> sophisticated methods of daisy-chaining the carry bit that mean the
> overall addition can be performed remarkably quickly, and the end
> result is a one-clock ADD operation, same as OR. For most data, most
> code, and most situations, integer addition is exactly as fast as
> integer bit shift.

I'm guessing the clock speed is adjusted for the longest propagation
delays. According to

   https://en.wikipedia.org/wiki/Carry-lookahead_adder#Implementa
   tion_details>

the maximal gate delay of a 16-bit carry-lookahead-adder is 8 gate
delays. A 64-bit addition results in some more delay:

   https://en.wikipedia.org/wiki/Lookahead_carry_unit#64-bit_adder>


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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Chris Angelico
On Sat, Jul 21, 2018 at 2:39 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> On Sat, Jul 21, 2018 at 1:14 AM, Grant Edwards
>>  wrote:
>>> I refuse to believe there's an extant processor in common use where
>>> an ADD is faster than an OR unless somebody shows me the processor
>>> spec sheet.
>>
>> "Faster than"? I'd agree with you. But "as fast as"? I believe that's
>> how most modern CPUs already operate. (Well, mostly.) There are
>> sophisticated methods of daisy-chaining the carry bit that mean the
>> overall addition can be performed remarkably quickly, and the end
>> result is a one-clock ADD operation, same as OR. For most data, most
>> code, and most situations, integer addition is exactly as fast as
>> integer bit shift.
>
> I'm guessing the clock speed is adjusted for the longest propagation
> delays. According to
>
>https://en.wikipedia.org/wiki/Carry-lookahead_adder#Implementa
>tion_details>
>
> the maximal gate delay of a 16-bit carry-lookahead-adder is 8 gate
> delays. A 64-bit addition results in some more delay:
>
>https://en.wikipedia.org/wiki/Lookahead_carry_unit#64-bit_adder>

Right; and since "one clock" is sufficient to propagate through all
the gates of a 64-bit snake (err, I mean adder), and since "one clock"
is also the shortest time from one instruction to the next, the
bitwise operation and the arithmetic operation will generally take the
same amount of time. Generally.

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


Re: [OT] Bit twiddling homework

2018-07-20 Thread Grant Edwards
On 2018-07-20, Chris Angelico  wrote:
> On Sat, Jul 21, 2018 at 1:14 AM, Grant Edwards  
> wrote:
>> On 2018-07-20, Dennis Lee Bieber  wrote:
>>
>>> While I suspect Python isn't micro-optimizing, take into account
>>> that most processors do have an "increment"/"decrement" operation --
>>> since that is done so much at the low-level. Also, just general
>>> integer addition is common, so the hardware may be optimized for
>>> doing them fast. Boolean operations may not be as well optimized.
>>
>> Boolean operations are also very common at the lowest level, and they
>> involve far simpler logic than does addition.  I refuse to believe
>> there's an extant processor in common use where an ADD is faster than
>> an OR unless somebody shows me the processor spec sheet.
>
> "Faster than"? I'd agree with you.

My understanding was that the hypothesis was the Python benchmark
discrepency might be due to the CPU ADD operation being faster than
the OR operator.

> But "as fast as"?  I believe that's how most modern CPUs already
> operate. (Well, mostly.) There are sophisticated methods of
> daisy-chaining the carry bit that mean the overall addition can be
> performed remarkably quickly, and the end result is a one-clock ADD
> operation, same as OR.

Definitely.  Any modern CPU capable of running Python will have single
cycle add/subtract/or/and/not/xor.  Addition is so important for so
many things that they will have jumped through the hoops required to
build a single cycle word-width adder.  And/or/not is so simple
there's just no sane way to make it take more than one cycle.

> For most data, most code, and most situations, integer addition is
> exactly as fast as integer bit shift.

Yes.

-- 
Grant Edwards   grant.b.edwardsYow! Was my SOY LOAF left
  at   out in th'RAIN?  It tastes
  gmail.comREAL GOOD!!

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


try except inside a with open

2018-07-20 Thread Ganesh Pal
Dear python Friends,


I need a quick suggestion on the below code.

def modify_various_line(f):
""" Try modifiying various line """
try:
f.write('0123456789abcdef')
f.seek(5) # Go to the 6th byte in the file
print f.read(1)
f.seek(-3, 2) # Go to the 3rd byte before the end
print f.read(1)
f.write('END')
except IOError as e:
   raise
return True

def h():
try:
with open('/tmp/file.txt', 'r+') as f:
 try:
 modify_various_line(f)
 except Exception as e:
   print e
except IOError as e:
print(e)

h()

gpal-cwerzvd-1# python ganesh1.py
5
d
gpal-cwerzvd-1# cat /tmp/file.txt
0123456789abcdefEND#



(1) Can we  use try and expect  in  a 'with open' function as shown in the
below example code .

(2)  If I hit any other exceptions  say Value-error can I catch them as
show below

try:
with open('/tmp/file.txt', 'r+') as f:
 try:
 modify_various_line(f)
 value_list = [str(value) for value in somedict.values()]]
 except (ValueError,IOError) as e:
   print e
except IOError as e:

PS:  I don't want to use other way of opening file say file =
open(“testfile.txt”,”w”)  and also want to retain modify_various_line(f)
function ,


Regards,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] Bit twiddling homework

2018-07-20 Thread jladasky
On Friday, July 20, 2018 at 2:00:26 AM UTC-7, Brian Oney wrote:

> Are 16|1 and 16+1 internally the same operation (for integers)?

For 16 and 1, the output of the two operations happen to be the same, but 
generally a bitwise OR is not the same are addition.  There are no carry bits 
in the bitwise operation!  1 + 1 = 2.  But 1 | 1 = 1.
-- 
https://mail.python.org/mailman/listinfo/python-list


Unable to install latest version of a package via pip from PyPI

2018-07-20 Thread Iwo Herka
Hello,

I do not wish to bother you with boring installation problems; however,
I would be obliged if someone could spare a minute or two to take a look at
my problem.

I've recently uploaded a package on PyPI: pypi.org/project/sexpr/. Latest
version is 0.1.6 (for Python 3+). Unfortunately, I cannot install it via pip:

$ pip install sexpr==0.1.6

Could not find a version that satisfies the requirement sexpr==0.1.6 (from
versions: 0.1.6.linux-x86_64, 0.1.2, 0.1.3, 0.1.4, 0.1.5)
No matching distribution found for sexpr==0.1.6

However, package is obviously there:

$ pip search sexpr
sexpr (0.1.6)  - S-expression toolkit for Python
INSTALLED: 0.1.5
LATEST:0.1.6

Package was compiled and uploaded in a standard way:

$ python3 setup.py sdist bdist_wheel
...
$ twine upload dist/*
...

setuptools version is 39.0.1. twine version is 1.11.0. wheel version is 0.31.1.
I've tried installing with pip version 8.1.1 and 9.0.1. I'm also able to
install the package directly from GitHub without problems. setup.py install
works too. I'm using venv and pyenv.

Am I missing something obvious? Thank you.

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


Re: Unable to install latest version of a package via pip from PyPI

2018-07-20 Thread Iwo Herka
Oh. It took a while for the message to go through.
In the meantime I've discovered that the source-code archive was badly 
formatted. Probably something with venv. I've bumped the patch version and 
re-uploaded. Sorry and thank you.

Iwo Herka

 Original Message 
On 20 Jul 2018, 7:05 pm, Iwo Herka wrote:

> Hello,
>
> I do not wish to bother you with boring installation problems; however,
> I would be obliged if someone could spare a minute or two to take a look at
> my problem.
>
> I've recently uploaded a package on PyPI: pypi.org/project/sexpr/. Latest
> version is 0.1.6 (for Python 3+). Unfortunately, I cannot install it via pip:
>
> $ pip install sexpr==0.1.6
>
> Could not find a version that satisfies the requirement sexpr==0.1.6 (from
> versions: 0.1.6.linux-x86_64, 0.1.2, 0.1.3, 0.1.4, 0.1.5)
> No matching distribution found for sexpr==0.1.6
>
> However, package is obviously there:
>
> $ pip search sexpr
> sexpr (0.1.6) - S-expression toolkit for Python
> INSTALLED: 0.1.5
> LATEST: 0.1.6
>
> Package was compiled and uploaded in a standard way:
>
> $ python3 setup.py sdist bdist_wheel
> ...
> $ twine upload dist/*
> ...
>
> setuptools version is 39.0.1. twine version is 1.11.0. wheel version is 
> 0.31.1.
> I've tried installing with pip version 8.1.1 and 9.0.1. I'm also able to
> install the package directly from GitHub without problems. setup.py install
> works too. I'm using venv and pyenv.
>
> Am I missing something obvious? Thank you.
>
> Iwo Herka
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Cookies not showing up in environ

2018-07-20 Thread abc abc
I am trying the simplest of examples below to set and read a cookie. I am using 
the Django framework, but I have also tried with vanilla python cookies and 
os.environ. I have posted elsewhere, but this hasn't gotten much attention, so 
I'd really appreciate any help.

Thinking at this point it may be something to with the server, not python. I 
have tried uwsgi/nginx, gunicorn/nginx, django development server with no 
success.

view.py:

def index(environ, start_response, request):
if not 'HTTP_COOKIE' in environ:
response = HttpResponse("hello")
response.set_cookie('user_agreement', 'cookie text', 
domain='.mysite.com')
return response
else:
print environ['HTTP_COOKIE']

The webpage just prints 'hello' and never reaches the else not matter how many 
times I refresh the page. There are no cookies in the browser ever either.

Am I missing some middleware? I remember some answers to cookie problems 
related to the order of their middleware configurations. Here's mine:

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-20 Thread Calvin Spealman
You can read cookies from the request via the request.COOKIES dictionary.
See the documentation here:
https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.COOKIES

You won't find them in an environment variable, which is shared
process-wide and across all requests, because they are bound to each
request. This is not a CGI script, so those HTTP_* environment variables
you seem to expect won't be here.

On Fri, Jul 20, 2018 at 3:39 PM, abc abc  wrote:

> I am trying the simplest of examples below to set and read a cookie. I am
> using the Django framework, but I have also tried with vanilla python
> cookies and os.environ. I have posted elsewhere, but this hasn't gotten
> much attention, so I'd really appreciate any help.
>
> Thinking at this point it may be something to with the server, not python.
> I have tried uwsgi/nginx, gunicorn/nginx, django development server with no
> success.
>
> view.py:
>
> def index(environ, start_response, request):
> if not 'HTTP_COOKIE' in environ:
> response = HttpResponse("hello")
> response.set_cookie('user_agreement', 'cookie text', domain='.
> mysite.com')
> return response
> else:
> print environ['HTTP_COOKIE']
>
> The webpage just prints 'hello' and never reaches the else not matter how
> many times I refresh the page. There are no cookies in the browser ever
> either.
>
> Am I missing some middleware? I remember some answers to cookie problems
> related to the order of their middleware configurations. Here's mine:
>
> MIDDLEWARE = [
> 'django.middleware.security.SecurityMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware',
> ]
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try except inside a with open

2018-07-20 Thread MRAB

On 2018-07-20 18:59, Ganesh Pal wrote:

Dear python Friends,


I need a quick suggestion on the below code.

def modify_various_line(f):
 """ Try modifiying various line """
 try:
 f.write('0123456789abcdef')
 f.seek(5) # Go to the 6th byte in the file
 print f.read(1)
 f.seek(-3, 2) # Go to the 3rd byte before the end
 print f.read(1)
 f.write('END')
 except IOError as e:
raise
 return True

def h():
 try:
 with open('/tmp/file.txt', 'r+') as f:
  try:
  modify_various_line(f)
  except Exception as e:
print e
 except IOError as e:
 print(e)

h()

gpal-cwerzvd-1# python ganesh1.py
5
d
gpal-cwerzvd-1# cat /tmp/file.txt
0123456789abcdefEND#



(1) Can we  use try and expect  in  a 'with open' function as shown in the
below example code .


Yes.


(2)  If I hit any other exceptions  say Value-error can I catch them as
show below

 try:
 with open('/tmp/file.txt', 'r+') as f:
  try:
  modify_various_line(f)
  value_list = [str(value) for value in somedict.values()]]
  except (ValueError,IOError) as e:
print e
 except IOError as e:

PS:  I don't want to use other way of opening file say file =
open(“testfile.txt”,”w”)  and also want to retain modify_various_line(f)
function ,


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


Re: Cookies not showing up in environ

2018-07-20 Thread Peter J. Holzer
On 2018-07-20 12:39:38 -0700, abc abc wrote:
> I am trying the simplest of examples below to set and read a cookie. I
> am using the Django framework, but I have also tried with vanilla
> python cookies and os.environ. I have posted elsewhere, but this
> hasn't gotten much attention, so I'd really appreciate any help.
> 
> Thinking at this point it may be something to with the server, not
> python. I have tried uwsgi/nginx, gunicorn/nginx, django development
> server with no success.
> 
> view.py:
> 
> def index(environ, start_response, request):
> if not 'HTTP_COOKIE' in environ:

The first argument to a Django view function is the request object,
not anything one could plausibly name "environ". See the django docs on
how to access the fields of a request.

hp

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | [email protected] | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-20 Thread abc abc
Well, I'm so messed up between so many sources and tutorials I don't know which 
way is up. 

References to environ variable:
https://www.python.org/dev/peps/pep-0333/
https://stackoverflow.com/questions/16774952/wsgi-whats-the-purpose-of-start-response-function

I read that I have to have a file project/project/wsgi.py that has a function 
called 'application' that gets called; still a little fuzzy on whose calling it 
and passing these parameters. I understood that the cookies should be in 
HTTP_COOKIES inside the environ hash... they're not. From wsgi.py I call a 
function in my view which returns a response object. Sorry if my parameters 
don't quite match up, I've been rearranging things while trying to get this to 
work; it should just be environ and start_response in wsgi:application().

If I try to access the request object, it is None, and I'm not sure how I would 
get it if it's not coming form wsgi.py:application...

My previous example is bad since they are passing 'status' to the 
start_response which I don't know how got created. TBH: I've spent days and 
days on this in different forums and am not getting much help or progress. I 
think I have made some sort of bad assumption in the beginning which is 
blocking me now.

I don't normally like to ask for handouts, but can you please point me to a 
dead simple setup with wsgi.py and view.py that can handle cookies... just 
simply setting them and reading them?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: try except inside a with open

2018-07-20 Thread Steven D'Aprano
On Fri, 20 Jul 2018 23:29:21 +0530, Ganesh Pal wrote:

> Dear python Friends,
> 
> 
> I need a quick suggestion on the below code.
> 
> def modify_various_line(f):
> """ Try modifiying various line """
> try:
> f.write('0123456789abcdef')
> f.seek(5) # Go to the 6th byte in the file
> print f.read(1)
> f.seek(-3, 2) # Go to the 3rd byte before the end
> print f.read(1)
> f.write('END')
> except IOError as e:
>raise
> return True

(1) Since this function always returns True (if it returns at all), what 
is the point? There's no point checking the return result, since it's 
always true, so why bother returning anything?

(2) What's the point of catching an exception only to immediately, and 
always, re-raise it?

It seems to me that your code above is better written like this:

def modify_various_line(f):
""" Try modifying various line """
f.write('0123456789abcdef')
f.seek(5) # Go to the 6th byte in the file
print f.read(1)
f.seek(-3, 2) # Go to the 3rd byte before the end
print f.read(1)
f.write('END')



> def h():
> try:
> with open('/tmp/file.txt', 'r+') as f:
>  try:
>  modify_various_line(f)
>  except Exception as e:
>print e
> except IOError as e:
> print(e)

Debugging is hard enough without destroying useful debugging information. 
Tracebacks are not your enemy to be hidden and suppressed (at least not 
during development) but your greatest friend in the world, one who tells 
you the embarrassing errors you have made (bugs) so you can fix them.

https://realpython.com/the-most-diabolical-python-antipattern/


def h():
with open('/tmp/file.txt', 'r+') as f:
modify_various_line(f)


is much shorter, easier to read, and if an error occurs, you get the 
benefit of the full traceback not just the abbreviated error message.

Tracebacks are printed to standard error, not standard out, so they can 
be redirected to a log file more easily. Or you can set an error handler 
for your entire application, so that in production any uncaught exception 
can be logged without having to fill your application with boilerplate 
"try...except...print >>sys.stderr, err".

But if you *really* have to catch the exception and suppress the 
traceback, try this:

def h():
try:
with open('/tmp/file.txt', 'r+') as f:
modify_various_line(f)
except IOError as e:
print(e)

There's no need to nest two except clauses, both of which do the same 
thing with an exception, and one of which will cover up bugs in your code 
as well as expected failures.



> (1) Can we  use try and expect  in  a 'with open' function as shown in
> the below example code .

Yes.


> (2)  If I hit any other exceptions  say Value-error can I catch them as
> show below

If you hit ValueError, that is almost always a bug in your code. That's 
exactly the sort of thing you *shouldn't* be covering up with an except 
clause unless you really know what you are doing.



-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson

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


Re: Edit with IDLE pull down menu

2018-07-20 Thread no
On Fri, 20 Jul 2018 03:19:16 -0400, Terry Reedy 
wrote:

>On 7/19/2018 5:40 PM, [email protected] wrote:
>
>> The option for Edit with IDLE has an expanding menu.  The only item in
>> the pull down menu is..Edit with IDLE.
>
>What I see is Edit with IDLE 3.7.  There should be an entry for each 
>Python installed.  Since that is the reason for the 2nd menu, and used 
>to be the case, it is a bug that there is not.  I will later check the 
>tracker and maybe open an issue.
>
>> So to run a .py program, you have to right click and select the pull
>> down menu Edit with IDLE and then inside that menu you pick Edit with
>> IDLE.  Seems a little redundant.

You seem to be able to directly affect Python development.  If that is
true, maybe you could remove the pull down redundancy from one menu
and add a pull down option to another pull down menu.

If Python can modify the pull down menus, it would be nice to be able
to right click in a folder (Windows) and have an option to create a
new python file with a .py extension like you can create a new text
document with a .txt extension.

OpenOffice adds this option to create new OO files.

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


PyCharm

2018-07-20 Thread no
Win7

I was watching some tutorial videos on Python that recommended I use
PyCharm and it worked pretty well until I tried to use input.

I found this:
https://youtrack.jetbrains.com/issue/PY-27891

It says..
Pavel Karateev   commented 10 Jan 2018 11:57
Hi Calvin Broadus! I am sorry for the inconvenience, this is the
problem on PyCharm side, the fix is in progress, should be included in
the next minor update 2017.3.3, release candidate build is planned on
this week.

Since I just installed the program yesterday, I assume that the fix
has not been implemented yet.

Most of my practice programs I always just enter a set value for
inputs instead of taking inputs from the keyboard, but I would like to
pause so find out why my program is broken.

Is there another way to insert a pause that will work with PyCharm?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyCharm

2018-07-20 Thread Michael Vilain
I'm running PyCharm Edu (to go through their great tutorial). It's version is 
2018.1.3, which I got from the web site.  Unless you mistyped the version, this 
is the current release and yours is very old.
--
Michael Vilain
650-322-6755

> On 20-Jul-2018, at 8:11 PM 🌙, [email protected] wrote:
> 
> Win7
> 
> I was watching some tutorial videos on Python that recommended I use
> PyCharm and it worked pretty well until I tried to use input.
> 
> I found this:
> https://youtrack.jetbrains.com/issue/PY-27891
> 
> It says..
> Pavel Karateev   commented 10 Jan 2018 11:57
> Hi Calvin Broadus! I am sorry for the inconvenience, this is the
> problem on PyCharm side, the fix is in progress, should be included in
> the next minor update 2017.3.3, release candidate build is planned on
> this week.
> 
> Since I just installed the program yesterday, I assume that the fix
> has not been implemented yet.
> 
> Most of my practice programs I always just enter a set value for
> inputs instead of taking inputs from the keyboard, but I would like to
> pause so find out why my program is broken.
> 
> Is there another way to insert a pause that will work with PyCharm?
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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