Re: Invoking return through a function?

2017-10-31 Thread Chris Angelico
On Tue, Oct 31, 2017 at 4:32 PM, Steve D'Aprano
 wrote:
> On Tue, 31 Oct 2017 02:34 pm, Chris Angelico wrote:
>
>> On Tue, Oct 31, 2017 at 2:00 PM, Steve D'Aprano
>>  wrote:
>>> Python has no GOTO, fortunately, but C has at least two, GOTO and LONGJMP.
>>> A C macro could, if I understand correctly, jump into the middle of another
>>> function. (Yay for spaghetti code!)
>>
>> No, I don't think you do understand them correctly - or at least, I
>> don't know of any way for a C macro to jump into the middle of a
>> function.
>
> I presume a macro could contain a call to longjmp, yes? Since longjmp can jump
> into another function (albeit only one which has prepared for it in advance),
> so can the macro.

I'd still consider longjmp to be jumping OUT, not IN - you can't skip
the beginning of a function and jump half way into it. For instance:

void foo() {
printf("Start of function foo\n");
SOME_EVIL_MACRO
printf("End of function foo\n");
}

void bar() {
SOME_OTHER_EVIL_MACRO
}

int main() {
bar();
return 0;
}

To my knowledge, there's no way to have bar() run just the second half
of foo(). The only way to have bar "jump into" foo is to first call
foo, which would look like this:

jmp_buf buf;
void foo() {
printf("Start of function foo\n");
if (!setjmp(buf)) {
printf("Middle of function foo\n");
bar();
printf("This won't happen");
} else {
printf("After longjmp\n");
}
printf("End of function foo\n");
}

void bar() {
printf("Start of function bar\n");
longjmp(buf, 1);
printf("This won't happen\n");
}

The Python equivalent would be:

def foo():
print("Start of function foo")
try:
print("Middle of function foo")
bar()
print("This won't happen")
except Exception:
print("After longjmp/exception")
print("End of function foo")

def bar():
print("Start of function bar")
raise Exception
print("This won't happen")

So if longjmp can jump into a function, so can Python's raise
statement. Yes, it's nonlocal flow control; but it's not an
uncontrolled goto that lets you jump into functions in any normal
sense of that expression.

> https://stackoverflow.com/questions/21355110/how-to-goto-into-different-function-in-c
>
> And what about assembly? Couldn't you jump into a function from assembly? Of
> course the stack will be all wrong, but if you're using assembly you have to
> manage that yourself.

Oh sure, but that's like saying "I can change the value of a Python
string using ctypes, ergo Python strings aren't immutable". When you
peel away an abstraction layer, you get to see the underlying code.
What you do with that is your responsibility.

>> There are three quite different things mentioned here.
>>
>> 1) The 'goto' statement, which unconditionally jumps you to another
>> location *in the same function*
>
> Yes. And a large enough function can contain everything. If you wanted to
> write BASIC-style unstructured code, you could dump everything into one
> function and use GOTO.

Well sure. C certainly won't  stop you from being an idiot :) And
Python (or at least CPython) would let you alter the bytecode and
actually insert jump opcodes. And, of course, you can hide the abuse
behind a decorator, which is as much "action at a distance" as CPP
macros.

The trouble with trying to make yourself stupider than you really are
is that you very often succeed. CS Lewis wasn't talking about software
design, but he was pretty much spot on... :D

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


enum

2017-10-31 Thread ast

Hi

Below an example of enum which defines an __init__
method. 


https://docs.python.org/3.5/library/enum.html#planet

Documentation says that the value of the enum 
members will be passed to this method.


But in that case __init__ waits for two arguments, mass
and radius, while enum member's value is a tuple. 


It seems that there is a tuple unpacking, but it is
not documented, that's not clear 


class Planet(Enum):
... MERCURY = (3.303e+23, 2.4397e6)
... VENUS   = (4.869e+24, 6.0518e6)
... EARTH   = (5.976e+24, 6.37814e6)
... MARS= (6.421e+23, 3.3972e6)
... JUPITER = (1.9e+27,   7.1492e7)
... SATURN  = (5.688e+26, 6.0268e7)
... URANUS  = (8.686e+25, 2.5559e7)
... NEPTUNE = (1.024e+26, 2.4746e7)
...
... def __init__(self, mass, radius):
... self.mass = mass   # in kilograms
... self.radius = radius# in meters
...
... @property
... def surface_gravity(self):
... # universal gravitational constant  (m3 kg-1 s-2)
... G = 6.67300E-11
... return G * self.mass / (self.radius * self.radius)
--
https://mail.python.org/mailman/listinfo/python-list


Re: Invoking return through a function?

2017-10-31 Thread Alberto Berti
> "Lele" == Lele Gaifax  writes:

Lele> r...@zedat.fu-berlin.de (Stefan Ram) writes:
Stefan> There are many macro processors available, like the C macro
Stefan> preprocessor, gpp, M4, or funnelweb. You can always use them
Stefan> for your Python source (which, in this case, isn't exactly Python
Stefan> source anymore).

Lele> Even more to the point, MacroPy! See 
https://github.com/azazel75/macropy for a
Lele> 3.5+ version.

I share the opinion of Stefan and others, it's a bad practice. But just
to have some fun I implemented  it with MacroPy...

This is the module with the test code::

  # test.py
  from test_macros import macros, check_macro

  def checkKey(k, m):
  return k in m

  @check_macro
  def test():

  m = {1: 'a', 2: 'b'}
  print('before')
  checkKey(3, m)
  print('after')


here the test function is wrapped by  the macro, that is defined as:

  # test_macros.py
  from macropy.core.macros import Macros
  from macropy.core.quotes import macros, q, u, ast_literal
  from macropy.core.walkers import Walker
  from macropy.experimental.pattern import macros, switch, _matching, 
ClassMatcher

  from ast import Call, Name, Expr

  macros = Macros()

  @macros.decorator
  def check_macro(tree, **kw):

  @Walker
  def transform_checkKey(tree, stop, **kw):
  with switch(tree):
  if Expr(value=Call(func=Name(id='checkKey'))):
  with q as result:
  if not ast_literal[tree.value]:
  print('exiting!')
  return
  stop()
  else:
  result = tree
  return result

  return transform_checkKey.recurse(tree)

The macro is run with all the decorated code of the ``test()`` method
passed as the ``tree`` parameter in the form of ast tree. A tree walker
is then run to navigate the tree and augment the occurrence of checkKey
with the conditional return

And finally, a ``main`` module is needed to activate macro parsing and
substitution:

  # test_main.py
  import macropy.activate

  import test

  test.test()

cheers,

Alberto

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


Re: Invoking return through a function?

2017-10-31 Thread bartc

On 31/10/2017 05:32, Steve D'Aprano wrote:

On Tue, 31 Oct 2017 02:34 pm, Chris Angelico wrote:



No, I don't think you do understand them correctly - or at least, I
don't know of any way for a C macro to jump into the middle of a
function.


I presume a macro could contain a call to longjmp, yes? Since longjmp can jump
into another function (albeit only one which has prepared for it in advance),
so can the macro.

https://stackoverflow.com/questions/21355110/how-to-goto-into-different-function-in-c

And what about assembly? Couldn't you jump into a function from assembly? Of
course the stack will be all wrong, but if you're using assembly you have to
manage that yourself.


I've used jumping-into-functions quite a bit, mainly to implement 
threaded code as used to code fast byte-code dispatchers.


The actual jump is done in inline assembly (and usually via a function 
pointer; no actual named label is used), but it needs HLL support to 
ensure the functions you're jumping into have special entry and exit 
code (no stack frames or anything).


The main problem with a HLL goto into a HLL function is that the label 
names in the function will not be visible from the goto site. Otherwise 
it might be possible, with some provisos (eg. any stack frame the 
function uses may not be properly set up).


gcc-C probably has some extensions to do something similar (label 
pointers for example which can be stored in global data, although you 
have to execute the function normally first to set them up).


None of this would be routine however. Python thinks it's a cut above 
other languages as it manages without gotos, forgetting that gotos are 
most likely needed in its implementation, so that someone has done the 
dirty work!


(CPython compiled with gcc almost certainly uses label pointers in its 
dispatcher, although I believe the gotos are still into the same function.)



2) setjmp/longjmp, which is not actually a "goto", but more of a
"multi-level return"


Right-oh. So sort of like an exception then.


I've implemented setjmp/longjmp, but find them unintuitive to use. When 
I had to use arbitrary jumps from one function to another in the past 
(error recovery for example, what people use exceptions for now), I just 
used inline assembly. It was a lot simpler:


 assem
 mov [spvalue],sp# set up recovery data in globals
 mov [bpvalue],bp
 mov [recoverylabel],recoverypt
 end
  do normal work.

 recoverypt: # here when something has gone wrong


When you want to get to the recovery point from anywhere (probably you'd 
put this in a function):


 assem
 mov sp,[spvalue]
 mov bp,[bpvalue]
 jmp [recoverylabel]
 end

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


How to join elements at the beginning and end of the list

2017-10-31 Thread Ganesh Pal
How to join each elements with a delimiter at (1) beginning and end of the
list and (2) connecting all elements  of the list

Example :

>>> value_list =  [1, 2, 3, 4, 56, 's']   I want this to be converted in
this  from '||1||2||3||4||56||s||'

Here is my solution

>>> values = '||' + '||'.join(map(str, value_list)) + '||'
>>> values

'||1||2||3||4||56||s||'


Iam joining the elements at the beginning and end of the list using '+'
operator any other solution, this is not looking neater


I am a Linux user using python 2.7


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


Re: Invoking return through a function?

2017-10-31 Thread Rhodri James

On 31/10/17 02:06, Alberto Riva wrote:

Steve D'Aprano gave you a pretty full answer, I just wanted to add:


 The kind of
statement I was trying to add would at least have made that explicit: 
return-if-so-and-so-happens.


That's only obvious in the function that's doing the returning.  The 
function that's doing the calling that gets its expectation of flow 
control broken has no clue, and that's my problem.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Neil Cerutti
On 2017-10-31, Ganesh Pal  wrote:
> Here is my solution
>
 values = '||' + '||'.join(map(str, value_list)) + '||'
 values
>
> '||1||2||3||4||56||s||'
>
> I am joining the elements at the beginning and end of the list
> using '+' operator any other solution, this is not looking
> neater
>
> I am a Linux user using python 2.7

You can use the % operator instead of +, and a generator
expression instead of map. It's a pretty small improvement,
though.

values = '||%s||' % ('||'.join(str(s) for s in value_list))

At least... I THINK you can use that generator expression in 2.7.

-- 
Neil Cerutti

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


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Ned Batchelder

On 10/31/17 11:29 AM, Neil Cerutti wrote:

On 2017-10-31, Ganesh Pal  wrote:

Here is my solution


values = '||' + '||'.join(map(str, value_list)) + '||'
values

'||1||2||3||4||56||s||'

I am joining the elements at the beginning and end of the list
using '+' operator any other solution, this is not looking
neater

I am a Linux user using python 2.7

You can use the % operator instead of +, and a generator
expression instead of map. It's a pretty small improvement,
though.

values = '||%s||' % ('||'.join(str(s) for s in value_list))

At least... I THINK you can use that generator expression in 2.7.



However you solve it, do yourself a favor and write a function to 
encapsulate it:


    def wrapped_join(values, sep):
    """Join values with sep, and also include sep at the ends."""
    return "{sep}{vals}{sep}".format(sep=sep, vals=sep.join(str(v) 
for v in values))


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


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Neil Cerutti
On 2017-10-31, Stefan Ram  wrote:
> Neil Cerutti  writes:
>>You can use the % operator instead of +, and a generator
>>expression instead of map. It's a pretty small improvement,
>>though.
>
>   "Improvement" in what sense?
>
> C:\>python -m timeit -s "value_list =  [1, 2, 3, 4, 56, 's']" "values = '||' 
> + '||'.join(map(str, value_list)) + '||'"
> 10 loops, best of 3: 4.86 usec per loop
>
> C:\>python -m timeit -s "value_list =  [1, 2, 3, 4, 56, 's']" "values = 
> '||%s||' % ('||'.join(str(s) for s in value_list))"
> 10 loops, best of 3: 7.46 usec per loop

Hmmm minty freshness?

-- 
Neil Cerutti

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


Re: Cooperative class tree filtering

2017-10-31 Thread Alberto Berti
Thanks Ian,

> "Ian" == Ian Kelly  writes:

Ian> On Thu, Oct 12, 2017 at 5:07 PM, Alberto Berti 
 wrote:

Ian> My initial reaction is: is this really worth it? This seems like an
Ian> awful lot of code and added complexity in order to do away with two
Ian> lines. It's a lot easier to reason about "return
Ian> super().filter(element)" and verify that it does the right thing than
Ian> for the complicated descriptor above.


yes this was also my conclusion, that descriptor is sitting on a branch
of its own and I've yet to decide on it.  The fact is that i've many
cases where I need a coperative superclass-subclass method execution and
I was trying to find some other pattern that would fit in ;-)


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


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Ned Batchelder

On 10/31/17 12:29 PM, Stefan Ram wrote:

Ned Batchelder  writes:

However you solve it, do yourself a favor and write a function to
encapsulate it:

   It is always a good solution to encapsulate a pattern into
   a function. So I agree that this is a good suggestion. But
   just for the sole sake of information, I'd like to add that
   this is also the slowest solution so far (about 10.76 usec).

   This might be a case where macros would be fine. As readable
   as a function call, but no runtime-overhead. One can write

value_list =  [1, 2, 3, 4, 56, 's']

#define JOIN_WRAPPED(list,string) \
string + string.join(map(str,list)) + string

values = JOIN_WRAPPED(value_list,'||')

print( values )

   and save it as »source.c« and execute it using

gcc -E source.c -o source.py
python source.py

   . This is also not intended to be a recommendation.



I try to avoid micro-optimization.  My guess is that right after calling 
wrapped_join(), the result will be written to an I/O device of some 
kind.  If that is so, the time spent in wrapped_join will be irrelevant.


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


Re: How to join elements at the beginning and end of the list

2017-10-31 Thread Steve D'Aprano
On Wed, 1 Nov 2017 02:29 am, Neil Cerutti wrote:

> You can use the % operator instead of +, and a generator
> expression instead of map. It's a pretty small improvement,
> though.
> 
> values = '||%s||' % ('||'.join(str(s) for s in value_list))
> 
> At least... I THINK you can use that generator expression in 2.7.

Generator expressions are slightly slower when you call join. If join knows
how many items there are, it can allocate space more efficiently, which is
faster.

So even though it takes a bit of time to build, and throw away, a temporary
list, its actually faster to join a list comp than a generator expression.




-- 
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: Performance of map vs starmap.

2017-10-31 Thread Steve D'Aprano
On Mon, 30 Oct 2017 09:10 pm, Kirill Balunov wrote:

> Sometime ago I asked this question at SO [1], and among the responses
> received was paragraph:
> 
>  - `zip` re-uses the returned `tuple` if it has a reference count of 1 when
> the `__next__` call is made.
>  - `map` build a new `tuple` that is passed to the mapped function every
> time a `__next__` call is made.
> 
> Why can not `map` use the same approach as `zip`?


It possibly could, if somebody could be bothered to make that
micro-optimization.



-- 
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: enum

2017-10-31 Thread Cousin Stanley
ast wrote:

> https://docs.python.org/3.5/library/enum.html#planet
> 
> Documentation says that the value of the enum
> members will be passed to this method.
> 
> But in that case __init__ waits for two arguments, mass
> and radius, while enum member's value is a tuple.
> 
> It seems that there is a tuple unpacking, but it is
> not documented, that's not clear
>  
  
  
I added the following code to your example
to unpack  planet.value  into mass and radius 
after first importing Enum 

from enum import Enum
 

def test_01() :

print( '\n  planet   mass   radius  gravity \n' )

for planet in Planet : 

mass , radius = planet.value

print( '  {:8s}  {:9.4E}  {:9.4E}  {:9.4E} '.format( planet.name , mass 
, radius , 
  planet.surface_gravity ) )


if __name__ == '__main__' :

test_01()


A working copy  

http://csphx.net/python/planets_enum.txt


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona

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


Thread safety issue (I think) with defaultdict

2017-10-31 Thread Israel Brewster
A question that has arisen before (for example, here: 
https://mail.python.org/pipermail/python-list/2010-January/565497.html 
) is 
the question of "is defaultdict thread safe", with the answer generally being a 
conditional "yes", with the condition being what is used as the default value: 
apparently default values of python types, such as list, are thread safe, 
whereas more complicated constructs, such as lambdas, make it not thread safe. 
In my situation, I'm using a lambda, specifically:

lambda: datetime.min

So presumably *not* thread safe.

My goal is to have a dictionary of aircraft and when they were last "seen", 
with datetime.min being effectively "never". When a data point comes in for a 
given aircraft, the data point will be compared with the value in the 
defaultdict for that aircraft, and if the timestamp on that data point is newer 
than what is in the defaultdict, the defaultdict will get updated with the 
value from the datapoint (not necessarily current timestamp, but rather the 
value from the datapoint). Note that data points do not necessarily arrive in 
chronological order (for various reasons not applicable here, it's just the way 
it is), thus the need for the comparison.

When the program first starts up, two things happen:

1) a thread is started that watches for incoming data points and updates the 
dictionary as per above, and
2) the dictionary should get an initial population (in the main thread) from 
hard storage.

The behavior I'm seeing, however, is that when step 2 happens (which generally 
happens before the thread gets any updates), the dictionary gets populated with 
56 entries, as expected. However, none of those entries are visible when the 
thread runs. It's as though the thread is getting a separate copy of the 
dictionary, although debugging says that is not the case - printing the 
variable from each location shows the same address for the object.

So my questions are:

1) Is this what it means to NOT be thread safe? I was thinking of race 
conditions where individual values may get updated wrong, but this apparently 
is overwriting the entire dictionary.
2) How can I fix this?

Note: I really don't care if the "initial" update happens after the thread 
receives a data point or two, and therefore overwrites one or two values. I 
just need the dictionary to be fully populated at some point early in 
execution. In usage, the dictionary is used to see of an aircraft has been seen 
"recently", so if the most recent datapoint gets overwritten with a slightly 
older one from disk storage, that's fine - it's just if it's still showing 
datetime.min because we haven't gotten in any datapoint since we launched the 
program, even though we have "recent" data in disk storage thats a problem. So 
I don't care about the obvious race condition between the two operations, just 
that the end result is a populated dictionary. Note also that as datapoint come 
in, they are being written to disk, so the disk storage doesn't lag 
significantly anyway.

The framework of my code is below:

File: watcher.py

last_points = defaultdict(lambda:datetime.min)

# This function is launched as a thread using the threading module when the 
first client connects
def watch():
while true:

pointtime= 
if last_points[] < pointtime:

last_points[]=pointtime
#DEBUGGING
print("At update:", len(last_points))


File: main.py:

from .watcher import last_points

# This function will be triggered by a web call from a client, so could happen 
at any time
# Client will call this function immediately after connecting, as well as in 
response to various user actions.
def getac():


for record in aclist:
last_points[]=record_timestamp
#DEBUGGING
print("At get AC:", len(last_points))


---
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
---




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


Re: Report on non-breaking spaces in posts (was: How to join elements at the beginning and end of the list)

2017-10-31 Thread Jon Ribbens
On 2017-10-31, Stefan Ram  wrote:
> Ned Batchelder  writes:
>>     def wrapped_join(values, sep):
>
>   Ok, here's a report on me seing non-breaking spaces in 
>   posts in this NG. I have written this report so that you
>   can see that it's not my newsreader that is converting
>   something, because there is no newsreader involved.

Yes Ned's post definitely had non-breaking spaces in; it looks like
Thunderbird is turning strings of spaces into ' \xa0+ ', presumably
because things that expect HTML will squash runs of spaces (even
though the post is not HTML...)

By the way I would commend 'od -tx1z' to you as it is *far* easier to
read:

0012460 20 0a 65 6e 63 61 70 73 75 6c 61 74 65 20 69 74  > .encapsulate it<
0012500 3a 0a 0a 20 c2 a0 c2 a0 c2 a0 20 64 65 66 20 77  >:.. .. def w<
0012520 72 61 70 70 65 64 5f 6a 6f 69 6e 28 76 61 6c 75  >rapped_join(valu<
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Report on non-breaking spaces in posts

2017-10-31 Thread Rhodri James

On 31/10/17 17:23, Stefan Ram wrote:

Ned Batchelder  writes:

    def wrapped_join(values, sep):


   Ok, here's a report on me seing non-breaking spaces in
   posts in this NG. I have written this report so that you
   can see that it's not my newsreader that is converting
   something, because there is no newsreader involved.

   Here are some relevant lines from Ned's above post:

|From: Ned Batchelder 
|Newsgroups: comp.lang.python
|Subject: Re: How to join elements at the beginning and end of the list
|Message-ID: 


Hm.  That suggests the mail-to-news gateway has a hand in things.


|Content-Type: text/plain; charset=utf-8; format=flowed
|Content-Transfer-Encoding: 8bit
|     def wrapped_join(values, sep):


[snippety snip]


|od -c tmp.txt
|...
|0012620   s   u   l   a   t   e   i   t   :  \n  \n   Â       Â
|0012640       Â       d   e   f   w   r   a   p   p   e   d   _
|...
|
|od -x tmp.txt
|...
|0012620 7573 616c 6574 6920 3a74 0a0a c220 c2a0
|0012640 c2a0 20a0 6564 2066 7277 7061 6570 5f64
|...

   And you can see, there are two octet pairs »c220« and
   »c2a0« in the post (directly preceding »def wrapped«).
   (Compare with the Content-Type and Content-Transfer-Encoding
   given above.) (Read table with a monospaced font:)

 corresponding
Codepoint  UTF-8ISO-8859-1  interpretation

U+0020?c2 2020? SPACE?
U+00A0 c2 a0a0  NON-BREAKING SPACE

   This makes it clear that there really are codepoints
   U+00A0 in what I get from the server, i.e., non-breaking
   spaces directly in front of »def wrapped«.


And?  Why does that bother you?  A non-breaking space is a perfectly 
valid thing to put into a UTF-8 encoded message.  The 0xc2 0x20 byte 
pair that you misidentify as a space is another matter entirely.


0xc2 0x20 is not a space in UTF-8.  It is an invalid code sequence.  I 
don't know how or where it was generated, but it really shouldn't have 
been.  It might have been Ned's MUA, or some obscure bug in the 
mail-to-news gateway.  Does anyone in a position to know have any opinions?


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Report on non-breaking spaces in posts

2017-10-31 Thread Ben Bacarisse
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>>|od -c tmp.txt
>>|...
>>|0012620   s   u   l   a   t   e   i   t   :  \n  \n   Â       Â
>>|0012640       Â       d   e   f   w   r   a   p   p   e   d   _
>>|...
>>|
>>|od -x tmp.txt
>>|...
>>|0012620 7573 616c 6574 6920 3a74 0a0a c220 c2a0
>>|0012640 c2a0 20a0 6564 2066 7277 7061 6570 5f64
>>|...
>
>   PS:
>
>   Oh, the byte order of »od -x« did not match my expectations!

You might like

  http://www.bsb.me.uk/software/utf-8-dump/

I use it all the time (but then I would!).


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


Re: The syntax of replacement fields in format strings

2017-10-31 Thread John Smith
If we keep the current implementation as is, perhaps the documentation
should at least be altered ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Report on non-breaking spaces in posts

2017-10-31 Thread Ben Bacarisse
Rhodri James  writes:

> On 31/10/17 17:23, Stefan Ram wrote:
>> Ned Batchelder  writes:
>>>     def wrapped_join(values, sep):
>>
>>Ok, here's a report on me seing non-breaking spaces in
>>posts in this NG. I have written this report so that you
>>can see that it's not my newsreader that is converting
>>something, because there is no newsreader involved.
>>
>>Here are some relevant lines from Ned's above post:
>>
>> |From: Ned Batchelder 
>> |Newsgroups: comp.lang.python
>> |Subject: Re: How to join elements at the beginning and end of the list
>> |Message-ID: 
>
> Hm.  That suggests the mail-to-news gateway has a hand in things.
>
>> |Content-Type: text/plain; charset=utf-8; format=flowed
>> |Content-Transfer-Encoding: 8bit
>> |     def wrapped_join(values, sep):
>
> [snippety snip]
>
>> |od -c tmp.txt
>> |...
>> |0012620   s   u   l   a   t   e   i   t   :  \n  \n   Â       Â
>> |0012640       Â       d   e   f   w   r   a   p   p   e   d   _
>> |...
>> |
>> |od -x tmp.txt
>> |...
>> |0012620 7573 616c 6574 6920 3a74 0a0a c220 c2a0
>> |0012640 c2a0 20a0 6564 2066 7277 7061 6570 5f64
>> |...
>>
>>And you can see, there are two octet pairs »c220« and
>>»c2a0« in the post (directly preceding »def wrapped«).
>>(Compare with the Content-Type and Content-Transfer-Encoding
>>given above.) (Read table with a monospaced font:)
>>
>>  corresponding
>> Codepoint  UTF-8ISO-8859-1  interpretation
>>
>> U+0020?c2 2020? SPACE?
>> U+00A0 c2 a0a0  NON-BREAKING SPACE
>>
>>This makes it clear that there really are codepoints
>>U+00A0 in what I get from the server, i.e., non-breaking
>>spaces directly in front of »def wrapped«.
>
> And?  Why does that bother you?  A non-breaking space is a perfectly
> valid thing to put into a UTF-8 encoded message.

But it's an odd thing to put into Python code (at least there).  If the
Usenet client is doing it that's surely bad as the code won't run
without editing.

> The 0xc2 0x20 byte
> pair that you misidentify as a space is another matter entirely.
>
> 0xc2 0x20 is not a space in UTF-8.  It is an invalid code sequence.  I
> don't know how or where it was generated, but it really shouldn't have
> been.

It wasn't there.  It was down to a misreading of the byte-order in the
hex dump.


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


matplot plot hangs

2017-10-31 Thread Andrew Z
hello,
 learning python's plotting by using matplotlib with python35 on fedora 24
x86.

Installed matplotlib into user's directory.
tk, seemed to work -
http://www.tkdocs.com/tutorial/install.html#installlinux - the window shows
up just fine.
but when trying to run the simple plot (
https://matplotlib.org/examples/pylab_examples/simple_plot.html) the script
is hanging on;

plt.plot(t, s)

attempts to
matplotlib.interactive(True) didn't bring anything,

I do get an error while trying to switch the backend ( which is fine with me) :
plt.switch_backend('Qt4Agg')
>>> plt.switch_backend('Qt4Agg')
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/az/.local/lib/python3.5/site-packages/matplotlib/pyplot.py",
line 231, in switch_backend
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File 
"/home/az/.local/lib/python3.5/site-packages/matplotlib/backends/__init__.py",
line 60, in pylab_setup
[backend_name], 0)
  File 
"/home/az/.local/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py",
line 10, in 
from .backend_qt4 import (
  File 
"/home/az/.local/lib/python3.5/site-packages/matplotlib/backends/backend_qt4.py",
line 18, in 
from .qt_compat import QtCore, QtWidgets, _getSaveFileName, __version__
  File 
"/home/az/.local/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py",
line 150, in 
from PyQt4 import QtCore, QtGui
ImportError: *No module named 'PyQt4'*
>>> plt.switch_backend('Agg')
>>> plt.switch_backend('TkAgg')


summary:

 why i can't use tkAgg, and how can i troubleshoot the issue?

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


Re: The syntax of replacement fields in format strings

2017-10-31 Thread Rick Johnson
On Tuesday, October 31, 2017 at 1:35:33 PM UTC-5, John Smith wrote:
> If we keep the current implementation as is, perhaps the
> documentation should at least be altered ?

You should supply a concise code example that showcases why
_you_ feel the docs are not satisfactory. It would help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Invoking return through a function?

2017-10-31 Thread Alberto Riva

On 10/31/2017 11:01 AM, Rhodri James wrote:

On 31/10/17 02:06, Alberto Riva wrote:

Steve D'Aprano gave you a pretty full answer, I just wanted to add:


 The kind of
statement I was trying to add would at least have made that explicit: 
return-if-so-and-so-happens.


That's only obvious in the function that's doing the returning.  The 
function that's doing the calling that gets its expectation of flow 
control broken has no clue, and that's my problem.


Sorry, I wasn't clear: I meant that the function that's doing the 
returning should be called "return-if-so-and-so-happens", literally ;) 
So when it appears in the body of another function, it's clear that it 
*may* invoke a return. Not saying it's a good idea, but just that when 
you read something like that you can expect evaluation flow to be disrupted.


Again: I'm not saying it's a good idea, please don't take this as a 
serious feature request :)


Alberto



--
E-mail address:
((lambda (s a b c) (map 'string #'code-char (mapcar (lambda (v)
(setq s (+ a (mod (+ s v (- a)) b (map 'list (lambda (v)
(- (char-code v) c)) " 1`-THUZ&+Wh1" 97 46 73 32)
--
https://mail.python.org/mailman/listinfo/python-list


Re: matplot plot hangs

2017-10-31 Thread Andrew Z
Stefan,
I intentionally tried to use the qt backend that i do not have. I wanted to
check i the switch was working (or not).

All i want at this point is to use (seemed to be default) tkagg.

On Oct 31, 2017 20:15, "Stefan Ram"  wrote:

> Andrew Z  writes:
> >ImportError: *No module named 'PyQt4'*
>
>   Matplotlib requires a large number of dependencies,
>   like »NumPy«, »dateutil«, »libpng«, or »pytz«.
>
>   There are also some capabilities provided by optional
>   backends which have their own dependencies.
>
>   If one would like to use the »Qt4Agg« backend, one would
>   also need to have »PyQt4« installed.
>
>   Some Python distributions might already contain many
>   dependencies.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list