Re: exec and traceback

2018-01-23 Thread dieter
ken...@gameofy.com writes:

> I'm using exec() to run a (multi-line) string of python code. If an
> exception occurs, I get a traceback containing a stack frame for the
> string. I've labeled the code object with a "file name" so I can
> identify it easily, and when I debug, I find that I can interact with
> the context of that stack frame, which is pretty handy.
>
> What I would like to also be able to do is make the code string
> visible to the debugger so I can look at and step through the code in
> the string as if it were from a python file.

The debugger may not be prepared for this kind of source; thus, you
might need a much more intelligent debugger.

The "exec" itself works with arbitary strings, not only with string
constants coming from a source file. It has no way to reliably
associate a filename and a line number in the respective file with
the code lines. The debugger (at least currently) expects to locate
the source code in a file (maybe an archive) given a line number in that
file.

Based on this, I expect that your wish will be difficult to fulfill.

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


Java Programming in Delhi | Java Training Institute in Delhi

2018-01-23 Thread Anu. SSDN
Learn All The Essential Java Keywords, Operators, Statements, And Expressions. 
SSDN Technologies is the premier Java training in Delhi, India. Highly 
Qualified Trainers are available to assist you. Enroll Now!
Read More:- http://www.ssdntech.com/Java-training.aspx
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-23 Thread kushal bhattacharya
On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
wrote:
> Hi,
> Is there any python framework or any tool as  which can generate C code from 
> python code as it is .
> 
> Thanks,
> Kushal

hi,
I have found nuitka as asuitable candidate but it seems that nuitka doesnt 
generate a simple C code which could be included as a C file in another 
program.Is there any alternative easier way regarding this?

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


Re: python to C code generator

2018-01-23 Thread paulina . zuniga

What about Cython?

On 01/23/2018 01:25 PM, kushal bhattacharya wrote:

On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
wrote:

Hi,
Is there any python framework or any tool as  which can generate C code from 
python code as it is .

Thanks,
Kushal


hi,
I have found nuitka as asuitable candidate but it seems that nuitka doesnt 
generate a simple C code which could be included as a C file in another 
program.Is there any alternative easier way regarding this?

Thanks


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


Re: python to C code generator

2018-01-23 Thread kushal bhattacharya
On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
wrote:
> Hi,
> Is there any python framework or any tool as  which can generate C code from 
> python code as it is .
> 
> Thanks,
> Kushal

yes i have but it generates a complex C code with python dependencies.I want to 
call the generated function from another C code but i Cant figure out how to do 
that
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-23 Thread bartc

On 23/01/2018 13:23, kushal bhattacharya wrote:

On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
wrote:

Hi,
Is there any python framework or any tool as  which can generate C code from 
python code as it is .

Thanks,
Kushal


yes i have but it generates a complex C code with python dependencies.I want to 
call the generated function from another C code but i Cant figure out how to do 
that


Because the translation isn't simply defined.

I've just tried nuitka on the Python code 'a=b+c', and it generates 2400 
lines of C. The main purpose seems to be to generate a self-contained 
executable corresponding to the Python, but generating first a C 
equivalent then using a C compiler and linker.


This equivalent code may just contain all the bits in CPython needed to 
do the job, but bypassing all the stuff to do with executing actual 
byte-code. But it also seems to do some optimisations (in the generated 
C before it uses C compiler optimisations), so that if static types can 
be inferred it might make use of that info.


Perhaps you simply want to use Python syntax to write C code? That would 
be a different kind of translator. And a simpler one, as 'a=b+c' 
translates to 'a+b+c;' in C.


--
bartc

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


Re: python to C code generator

2018-01-23 Thread kushal bhattacharya
On Tuesday, January 23, 2018 at 7:05:02 PM UTC+5:30, bartc wrote:
> On 23/01/2018 13:23, kushal bhattacharya wrote:
> > On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
> > wrote:
> >> Hi,
> >> Is there any python framework or any tool as  which can generate C code 
> >> from python code as it is .
> >>
> >> Thanks,
> >> Kushal
> > 
> > yes i have but it generates a complex C code with python dependencies.I 
> > want to call the generated function from another C code but i Cant figure 
> > out how to do that
> 
> Because the translation isn't simply defined.
> 
> I've just tried nuitka on the Python code 'a=b+c', and it generates 2400 
> lines of C. The main purpose seems to be to generate a self-contained 
> executable corresponding to the Python, but generating first a C 
> equivalent then using a C compiler and linker.
> 
> This equivalent code may just contain all the bits in CPython needed to 
> do the job, but bypassing all the stuff to do with executing actual 
> byte-code. But it also seems to do some optimisations (in the generated 
> C before it uses C compiler optimisations), so that if static types can 
> be inferred it might make use of that info.
> 
> Perhaps you simply want to use Python syntax to write C code? That would 
> be a different kind of translator. And a simpler one, as 'a=b+c' 
> translates to 'a+b+c;' in C.
> 
> -- 
> bartc


This is exactly what i meant to say.My goal is to translate the python code 
into its C equivalent with function name as it is.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-23 Thread Ned Batchelder

On 1/23/18 8:48 AM, kushal bhattacharya wrote:

On Tuesday, January 23, 2018 at 7:05:02 PM UTC+5:30, bartc wrote:

On 23/01/2018 13:23, kushal bhattacharya wrote:

On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
wrote:

Hi,
Is there any python framework or any tool as  which can generate C code from 
python code as it is .

Thanks,
Kushal

yes i have but it generates a complex C code with python dependencies.I want to 
call the generated function from another C code but i Cant figure out how to do 
that

Because the translation isn't simply defined.

I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
lines of C. The main purpose seems to be to generate a self-contained
executable corresponding to the Python, but generating first a C
equivalent then using a C compiler and linker.

This equivalent code may just contain all the bits in CPython needed to
do the job, but bypassing all the stuff to do with executing actual
byte-code. But it also seems to do some optimisations (in the generated
C before it uses C compiler optimisations), so that if static types can
be inferred it might make use of that info.

Perhaps you simply want to use Python syntax to write C code? That would
be a different kind of translator. And a simpler one, as 'a=b+c'
translates to 'a+b+c;' in C.

--
bartc


This is exactly what i meant to say.My goal is to translate the python code 
into its C equivalent with function name as it is.


The best way to do that is to read the Python code, understand what it 
does, and re-write it in C.  You won't find an automatic tool that can 
do the job you want.  The semantics of Python and C are too different.


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


Re: python to C code generator

2018-01-23 Thread Kirill Balunov
You can look at SymPy code generator
http://docs.sympy.org/latest/modules/utilities/codegen.html
Perhaps this is exactly what you need.

With kind regards,
-gdg

2018-01-23 17:00 GMT+03:00 Ned Batchelder :

> On 1/23/18 8:48 AM, kushal bhattacharya wrote:
>
>> On Tuesday, January 23, 2018 at 7:05:02 PM UTC+5:30, bartc wrote:
>>
>>> On 23/01/2018 13:23, kushal bhattacharya wrote:
>>>
 On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal
 bhattacharya wrote:

> Hi,
> Is there any python framework or any tool as  which can generate C
> code from python code as it is .
>
> Thanks,
> Kushal
>
 yes i have but it generates a complex C code with python dependencies.I
 want to call the generated function from another C code but i Cant figure
 out how to do that

>>> Because the translation isn't simply defined.
>>>
>>> I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
>>> lines of C. The main purpose seems to be to generate a self-contained
>>> executable corresponding to the Python, but generating first a C
>>> equivalent then using a C compiler and linker.
>>>
>>> This equivalent code may just contain all the bits in CPython needed to
>>> do the job, but bypassing all the stuff to do with executing actual
>>> byte-code. But it also seems to do some optimisations (in the generated
>>> C before it uses C compiler optimisations), so that if static types can
>>> be inferred it might make use of that info.
>>>
>>> Perhaps you simply want to use Python syntax to write C code? That would
>>> be a different kind of translator. And a simpler one, as 'a=b+c'
>>> translates to 'a+b+c;' in C.
>>>
>>> --
>>> bartc
>>>
>>
>> This is exactly what i meant to say.My goal is to translate the python
>> code into its C equivalent with function name as it is.
>>
>
> The best way to do that is to read the Python code, understand what it
> does, and re-write it in C.  You won't find an automatic tool that can do
> the job you want.  The semantics of Python and C are too different.
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-23 Thread Andrew Z
Id go this way too. Basic C is straightforward.  I usually consider
learning a new "thing " if the time to support potwntially combersome
solution using existing methods  justifies the effort.

On Jan 23, 2018 09:01, "Ned Batchelder"  wrote:

> On 1/23/18 8:48 AM, kushal bhattacharya wrote:
>
>> On Tuesday, January 23, 2018 at 7:05:02 PM UTC+5:30, bartc wrote:
>>
>>> On 23/01/2018 13:23, kushal bhattacharya wrote:
>>>
 On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal
 bhattacharya wrote:

> Hi,
> Is there any python framework or any tool as  which can generate C
> code from python code as it is .
>
> Thanks,
> Kushal
>
 yes i have but it generates a complex C code with python dependencies.I
 want to call the generated function from another C code but i Cant figure
 out how to do that

>>> Because the translation isn't simply defined.
>>>
>>> I've just tried nuitka on the Python code 'a=b+c', and it generates 2400
>>> lines of C. The main purpose seems to be to generate a self-contained
>>> executable corresponding to the Python, but generating first a C
>>> equivalent then using a C compiler and linker.
>>>
>>> This equivalent code may just contain all the bits in CPython needed to
>>> do the job, but bypassing all the stuff to do with executing actual
>>> byte-code. But it also seems to do some optimisations (in the generated
>>> C before it uses C compiler optimisations), so that if static types can
>>> be inferred it might make use of that info.
>>>
>>> Perhaps you simply want to use Python syntax to write C code? That would
>>> be a different kind of translator. And a simpler one, as 'a=b+c'
>>> translates to 'a+b+c;' in C.
>>>
>>> --
>>> bartc
>>>
>>
>> This is exactly what i meant to say.My goal is to translate the python
>> code into its C equivalent with function name as it is.
>>
>
> The best way to do that is to read the Python code, understand what it
> does, and re-write it in C.  You won't find an automatic tool that can do
> the job you want.  The semantics of Python and C are too different.
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-23 Thread kushal bhattacharya
On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal bhattacharya 
wrote:
> Hi,
> Is there any python framework or any tool as  which can generate C code from 
> python code as it is .
> 
> Thanks,
> Kushal

ok so which python tool would be the best one which can be included and 
parameters can be passed to from another C code file
-- 
https://mail.python.org/mailman/listinfo/python-list


[RELEASED] Python 3.4.8rc1 and Python 3.5.5rc1 are now available

2018-01-23 Thread Larry Hastings


On behalf of the Python development community, I'm pleased to announce 
the availability of Python 3.4.8rc1 and Python 3.5.5rc1.


Both Python 3.4 and 3.5 are in "security fixes only" mode. Both versions 
only accept security fixes, not conventional bug fixes, and both 
releases are source-only.



You can find Python 3.4.8rc1 here:

   https://www.python.org/downloads/release/python-348rc1/


And you can find Python 3.5.5rc1 here:

   https://www.python.org/downloads/release/python-355rc1/



Happy Pythoning,


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


Re: Why is there no functional xml?

2018-01-23 Thread Peter Otten
Rustom Mody wrote:

> Its obviously easier in python to put optional/vararg parameters on the
> right side rather than on the left of a parameter list.
> But its not impossible to get it in the desired order — one just has to
> 'hand-parse' the parameter list received as a *param
> Thusly:

> appointments = E.appointments(
>{"reminder":"15"},
>E.appointment(
> E.begin(1181251680),
> E.uid("04008200E000"),
> E.alarmTime(1181572063),
> E.state(),
> E.location(),
> E.duration(1800),
> E.subject("Bring pizza home")
> )
> )

Let's not waste too much effort on minor aesthic improvements ;)

>> Personally I'd probably avoid the extra layer and write a function that
>> directly maps dataclasses or database records to xml using the
>> conventional elementtree API.
> 
> I dont understand…
> [I find the OO/imperative style of making a half-done node and then
> [throwing
> piece-by-piece of contents in/at it highly aggravating]

What I meant is that once you throw a bit of introspection at it much of the 
tedium vanishes. Here's what might become of the DOM-creation as part of an 
actual script:

import xml.etree.ElementTree as xml
from collections import namedtuple

Appointment = namedtuple(
"Appointment",
"begin uid alarmTime state location duration subject"
)

appointments = [
Appointment(
begin=1181251680,
uid="04008200E000",
alarmTime=1181572063,
state=None,
location=None,
duration=1800,
subject="Bring pizza home"
)
]

def create_dom(appointments, reminder):
node = xml.Element("zAppointments", reminder=str(reminder))
for appointment in appointments:
for name, value in zip(appointment._fields, appointment):
child = xml.SubElement(node, name)
if value is not None:
child.text = str(value)
return node

with open("appt.xml", "wb") as outstream:
root = create_dom(appointments, 15)
xml.ElementTree(root).write(outstream)

To generalize that to handle arbitrarily nested lists and namedtuples a bit 
more effort is needed, but I can't see where lxml.objectify could make that 
much easier.

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


Re: Why is there no functional xml?

2018-01-23 Thread Peter Otten
Rustom Mody wrote:

> On Sunday, January 21, 2018 at 4:51:34 PM UTC+5:30, Peter Otten wrote:
>> Personally I'd probably avoid the extra layer and write a function that
>> directly maps dataclasses or database records to xml using the
>> conventional elementtree API.
> 
> Would appreciate your thoughts/comments Peter!
> 
> I find that you can get 'E' from lxml.objectify as well as lxml.builder
> builder seems better in that its at least sparsely documented
> objectify seems to have almost nothing beyond the original David Mertz'
> docs
> 
> builder.E seems to do what objectify.E does modulo namespaces
> 
> builder.E and objectify.E produce types that are different and look
> backwards (at least to me — Elementbase is less base than _Element)
> 
> You seem to have some reservation against objectify, preferring the
> default Element — I'd like to know what

While I don't have any actual experience with it, my gut feeling is that it 
simplifies something that is superfluous to begin with.

> Insofar as builder seems to produce the same type as Element unlike
> objectify which seems to be producing a grandchild type, do you have the
> same reservations against builder.E?

If I understand you correctly you are talking about implementation details.
Unfortunately I cannot comment on these -- I really just remembered 
objectify because of the catchy name...

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


Re: python to C code generator

2018-01-23 Thread theodore . leblanc

Hey Ally,

Cython adds a big chunk of complexity to simple things. That's the problem.

Greetings.

On 01/23/2018 01:54 PM, ally.m...@bankmail.host wrote:

Have you tried cython ?

On 01/23/2018 01:25 PM, kushal bhattacharya wrote:
On Wednesday, January 17, 2018 at 4:34:23 PM UTC+5:30, kushal 
bhattacharya wrote:

Hi,
Is there any python framework or any tool as  which can generate C 
code from python code as it is .


Thanks,
Kushal


hi,
I have found nuitka as asuitable candidate but it seems that nuitka 
doesnt generate a simple C code which could be included as a C file in 
another program.Is there any alternative easier way regarding this?


Thanks


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


Re: python to C code generator

2018-01-23 Thread Chris Angelico
On Wed, Jan 24, 2018 at 1:45 AM,   wrote:
> Hey Ally,
>
> Cython adds a big chunk of complexity to simple things. That's the problem.

That's like saying "Unicode adds a big chunk of complexity to the
simple task of translating a word from Japanese into Russian". No, it
doesn't; the complexity is inherent in the problem. You cannot
translate Python code into C code without either (a) reimplementing
all of Python's semantics, as Cython does; or (b) drastically changing
the semantics, such that even the very simplest of code might behave
quite differently; or (c) manually reading through the code and
writing equivalent C, which is what you might call "porting" or
"rewriting". (Or possibly "prototyping", if the intention was always
to transform it into C.) There is fundamentally NO easy way to
translate code from one language into another and get readable,
idiomatic code at the other end.

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


Re: python to C code generator

2018-01-23 Thread bartc

On 23/01/2018 13:34, bartc wrote:
Perhaps you simply want to use Python syntax to write C code? That would  > be a different kind of translator. And a simpler one, as 'a=b+c' > 

translates to 'a+b+c;' in C.
Or rather, 'a=b+c;'

(I've written source to source translators, some of which could target 
C, but not Python to C.


It would be feasible to write C code in a syntax that looks rather like 
Python, but it won't be real Python, and you can't run it as Python.


It wouldn't be a satisfactory way of writing C programs. So, although 
I'm not that big a fan of C syntax, it might be better to write C as C, 
and Python as Python, to avoid confusion.)


--
bartc

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


Processing a key pressed in Python 3.6

2018-01-23 Thread Virgil Stokes
I would appreciate help on finding a solution to following problem. This 
is the general structure of the "problem" code:



while True
    # Get some data from the web and process it
    ...
    ...
    # Write these data to a file
    ...
    ...
    # Check if a_key has been pressed in the command window
    ...
    ...
    if a_key_pressed:
    # Perform some pre-termination tasks
    ...
    ...
        # Close the output data file
        ...
        ...
        raise SystemExit('Exit')


I am running the code with Python 3.6 on a windows 10 platform. I have 
tried many approaches (mainly those posted on stackoverflow) but I have 
yet to find an approach that works for this structure.


Note:
  1) The code is executed in the windows 10 command window
  2) I am not using wxPython, IDLE, or pyGame in this application.
  3) The time to get the data, process it and write it to a file can
 take from 0.5 sec to 1.5 sec
  4) The key hit need not be echoed to the command window


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


Re: Processing a key pressed in Python 3.6

2018-01-23 Thread Chris Angelico
On Wed, Jan 24, 2018 at 5:50 AM, Virgil Stokes  wrote:
> I would appreciate help on finding a solution to following problem. This is
> the general structure of the "problem" code:
>
>> while True
>> # Get some data from the web and process it
>> ...
>> ...
>> # Write these data to a file
>> ...
>> ...
>> # Check if a_key has been pressed in the command window
>> ...
>> ...
>> if a_key_pressed:
>> # Perform some pre-termination tasks
>> ...
>> ...
>> # Close the output data file
>> ...
>> ...
>> raise SystemExit('Exit')
>
>
> I am running the code with Python 3.6 on a windows 10 platform. I have tried
> many approaches (mainly those posted on stackoverflow) but I have yet to
> find an approach that works for this structure.
>
> Note:
>   1) The code is executed in the windows 10 command window
>   2) I am not using wxPython, IDLE, or pyGame in this application.
>   3) The time to get the data, process it and write it to a file can
>  take from 0.5 sec to 1.5 sec
>   4) The key hit need not be echoed to the command window
>

Are you okay with demanding a specific key, rather than simply "press
any key"? Even better, key combination? Handle Ctrl-C by catching
KeyboardInterrupt and you can take advantage of Python's existing
cross-platform handling of the standard interrupt signal.

If Ctrl-C won't work for you, how about stipulating that it be Enter?
"Press Enter to quit" isn't too much worse than "Press any key to
quit" (plus you have less chance of accidentally terminating the
program when you don't want to). Spin off a thread to wait for enter.
I've tested this only on Linux, but it ought to work:

import threading
import time

shutdown = False
def wait_for_enter():
print("Hit Enter to quit.")
input()
global shutdown; shutdown = True

threading.Thread(target=wait_for_enter).start()

while "more work to do":
print("Getting data...")
time.sleep(1)
print("Saving data to file...")
time.sleep(1)
if shutdown:
print("Pre-termination...")
time.sleep(1)
raise SystemExit("exit")

If it doesn't, try switching around which is the secondary thread and
which is the primary - spin off a thread to do the work, then call
input() in the main thread.

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


Re: Processing a key pressed in Python 3.6 🦉

2018-01-23 Thread Virgil Stokes

Thanks very much Chris,

This code worked perfectly for "Enter". Your knowledge of  Python and 
more specifically this elegant solution are greatly appreciated. I now 
know that I need to learn more about threads. :-)


On 2018-01-23 20:15, Chris Angelico wrote:

On Wed, Jan 24, 2018 at 5:50 AM, Virgil Stokes  wrote:

I would appreciate help on finding a solution to following problem. This is
the general structure of the "problem" code:


while True
 # Get some data from the web and process it
 ...
 ...
 # Write these data to a file
 ...
 ...
 # Check if a_key has been pressed in the command window
 ...
 ...
 if a_key_pressed:
 # Perform some pre-termination tasks
 ...
 ...
 # Close the output data file
 ...
 ...
 raise SystemExit('Exit')


I am running the code with Python 3.6 on a windows 10 platform. I have tried
many approaches (mainly those posted on stackoverflow) but I have yet to
find an approach that works for this structure.

Note:
   1) The code is executed in the windows 10 command window
   2) I am not using wxPython, IDLE, or pyGame in this application.
   3) The time to get the data, process it and write it to a file can
  take from 0.5 sec to 1.5 sec
   4) The key hit need not be echoed to the command window


Are you okay with demanding a specific key, rather than simply "press
any key"? Even better, key combination? Handle Ctrl-C by catching
KeyboardInterrupt and you can take advantage of Python's existing
cross-platform handling of the standard interrupt signal.

If Ctrl-C won't work for you, how about stipulating that it be Enter?
"Press Enter to quit" isn't too much worse than "Press any key to
quit" (plus you have less chance of accidentally terminating the
program when you don't want to). Spin off a thread to wait for enter.
I've tested this only on Linux, but it ought to work:

import threading
import time

shutdown = False
def wait_for_enter():
 print("Hit Enter to quit.")
 input()
 global shutdown; shutdown = True

threading.Thread(target=wait_for_enter).start()

while "more work to do":
 print("Getting data...")
 time.sleep(1)
 print("Saving data to file...")
 time.sleep(1)
 if shutdown:
 print("Pre-termination...")
 time.sleep(1)
 raise SystemExit("exit")

If it doesn't, try switching around which is the secondary thread and
which is the primary - spin off a thread to do the work, then call
input() in the main thread.

ChrisA


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


plot / graph connecting re ordered lists

2018-01-23 Thread Vincent Davis
Looking for suggestions. I have an ordered list of names these names will
be reordered. I am looking to make a plot, graph, with the two origins of
the names in separate columns and a line connecting them to visually
represent how much they have moved in the reordering.
Surely there is some great example code for this on the net an am not
finding a clean example.

Thanks
Vincent Davis
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Processing a key pressed in Python 3.6

2018-01-23 Thread Virgil Stokes

Another follow-up question:

How would this code be modified to handle using the "Esc" key instead of 
the "Enter" key?



On 2018-01-23 20:15, Chris Angelico wrote:

On Wed, Jan 24, 2018 at 5:50 AM, Virgil Stokes  wrote:

I would appreciate help on finding a solution to following problem. This is
the general structure of the "problem" code:


while True
 # Get some data from the web and process it
 ...
 ...
 # Write these data to a file
 ...
 ...
 # Check if a_key has been pressed in the command window
 ...
 ...
 if a_key_pressed:
 # Perform some pre-termination tasks
 ...
 ...
 # Close the output data file
 ...
 ...
 raise SystemExit('Exit')


I am running the code with Python 3.6 on a windows 10 platform. I have tried
many approaches (mainly those posted on stackoverflow) but I have yet to
find an approach that works for this structure.

Note:
   1) The code is executed in the windows 10 command window
   2) I am not using wxPython, IDLE, or pyGame in this application.
   3) The time to get the data, process it and write it to a file can
  take from 0.5 sec to 1.5 sec
   4) The key hit need not be echoed to the command window


Are you okay with demanding a specific key, rather than simply "press
any key"? Even better, key combination? Handle Ctrl-C by catching
KeyboardInterrupt and you can take advantage of Python's existing
cross-platform handling of the standard interrupt signal.

If Ctrl-C won't work for you, how about stipulating that it be Enter?
"Press Enter to quit" isn't too much worse than "Press any key to
quit" (plus you have less chance of accidentally terminating the
program when you don't want to). Spin off a thread to wait for enter.
I've tested this only on Linux, but it ought to work:

import threading
import time

shutdown = False
def wait_for_enter():
 print("Hit Enter to quit.")
 input()
 global shutdown; shutdown = True

threading.Thread(target=wait_for_enter).start()

while "more work to do":
 print("Getting data...")
 time.sleep(1)
 print("Saving data to file...")
 time.sleep(1)
 if shutdown:
 print("Pre-termination...")
 time.sleep(1)
 raise SystemExit("exit")

If it doesn't, try switching around which is the secondary thread and
which is the primary - spin off a thread to do the work, then call
input() in the main thread.

ChrisA


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


Re: plot / graph connecting re ordered lists

2018-01-23 Thread Vincent Davis
On Tue, Jan 23, 2018 at 4:15 PM Dennis Lee Bieber 
wrote:

> On Tue, 23 Jan 2018 13:51:55 -0700, Vincent Davis
>  declaimed the following:
>
> >Looking for suggestions. I have an ordered list of names these names will
> >be reordered. I am looking to make a plot, graph, with the two origins of
>
> IE: you have two lists with the same items in different orders...
>
> >the names in separate columns and a line connecting them to visually
> >represent how much they have moved in the reordering.
> >Surely there is some great example code for this on the net an am not
> >finding a clean example.
> >
>
> Determine positions:
>
> pos = []
> for p, name in enumerate(first_list):
> np = second_list.index(name)
> pos.append( (name, p, np) )
>
> for (name, p, np) in pos:
> draw_line((1,p) , (2, np))
> label( (1, p), name)
>
> Exact details of graphics package and scaling left as an exercise


Actualy, it’s recomendations for a graphing package And an example using it
for such a graph that I am most interested in. I know how to relate the
names on the 2 lists.


> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
Sent from mobile app. Vincent Davis 720-301-3003
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: plot / graph connecting re ordered lists

2018-01-23 Thread duncan smith
On 23/01/18 23:42, Vincent Davis wrote:
> On Tue, Jan 23, 2018 at 4:15 PM Dennis Lee Bieber 
> wrote:
> 
>> On Tue, 23 Jan 2018 13:51:55 -0700, Vincent Davis
>>  declaimed the following:
>>
>>> Looking for suggestions. I have an ordered list of names these names will
>>> be reordered. I am looking to make a plot, graph, with the two origins of
>>
>> IE: you have two lists with the same items in different orders...
>>
>>> the names in separate columns and a line connecting them to visually
>>> represent how much they have moved in the reordering.
>>> Surely there is some great example code for this on the net an am not
>>> finding a clean example.
>>>
>>
>> Determine positions:
>>
>> pos = []
>> for p, name in enumerate(first_list):
>> np = second_list.index(name)
>> pos.append( (name, p, np) )
>>
>> for (name, p, np) in pos:
>> draw_line((1,p) , (2, np))
>> label( (1, p), name)
>>
>> Exact details of graphics package and scaling left as an exercise
> 
> 
> Actualy, it’s recomendations for a graphing package And an example using it
> for such a graph that I am most interested in. I know how to relate the
> names on the 2 lists.
> 
> 
>> --
>> Wulfraed Dennis Lee Bieber AF6VN
>> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>

Maybe http://graphviz.org/ and http://matthiaseisen.com/articles/graphviz/

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


Re: Processing a key pressed in Python 3.6

2018-01-23 Thread eryk sun
On Tue, Jan 23, 2018 at 11:29 PM, Virgil Stokes  wrote:
>
> How would this code be modified to handle using the "Esc" key instead of the
> "Enter" key?

The input() function depends on the console or terminal to read a line
of input. If you're using the Windows console, it calls the high-level
ReadConsole (or ReadFile) function, which performs a 'cooked' read. In
this case some keys are reserved. Escape is consumed to clear/flush
the input buffer. Function keys and arrows are consumed for line
editing and history navigation. And for some reason line-feed (^J) is
always ignored. Additionally, normal operation requires the following
input modes to be enabled:

ENABLE_PROCESSED_INPUT
process Ctrl+C as CTRL_C_EVENT and carriage return (^M)
as carriage return plus line feed (CRLF). consume
backspace (^H) to delete the previous character.

ENABLE_LINE_INPUT
return only when a carriage return is read.

ENABLE_ECHO_INPUT
echo read characters to the active screen.

Reading the escape key from the console requires the low-level
ReadConsoleInput, GetNumberOfConsoleInputEvents, and
FlushConsoleInputBuffer functions. These access the console's raw
stream of input event records, which includes key events, mouse
events, and window/buffer resize events. It's a bit complicated to use
this API, but fortunately the C runtime wraps it with convenient
functions such as _kbhit, _getwch, and _getwche (w/ echo). On Windows
only, you'll find these functions in the msvcrt module, but named
without the leading underscore. Here's an example of reading the
escape character without echo:

>>> msvcrt.getwch()
'\x1b'

Simple, no?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Processing a key pressed in Python 3.6

2018-01-23 Thread MRAB

On 2018-01-23 23:29, Virgil Stokes wrote:

Another follow-up question:

How would this code be modified to handle using the "Esc" key instead of
the "Enter" key?


This version uses msvcrt on Windows:

import msvcrt
import threading
import time

shutdown = False

def wait_for_enter():
global shutdown

print("Hit Enter to quit.")
# b'\x0D' is the bytestring produced by the Enter key.
# b'\x1B' is the bytestring produced by the Esc key.
if msvcrt.getch() == b'\x1B':
shutdown = True

threading.Thread(target=wait_for_enter).start()

while "more work to do":
 print("Getting data...")
 time.sleep(1)
 print("Saving data to file...")
 time.sleep(1)

 if shutdown:
 print("Pre-termination...")
 time.sleep(1)
 raise SystemExit("exit")


Another function of note is "msvcrt.kbhit()", which will tell you if 
there's a key waiting to be read.


This is all in the docs for the msvcrt module.


On 2018-01-23 20:15, Chris Angelico wrote:

On Wed, Jan 24, 2018 at 5:50 AM, Virgil Stokes  wrote:

I would appreciate help on finding a solution to following problem. This is
the general structure of the "problem" code:


while True
 # Get some data from the web and process it
 ...
 ...
 # Write these data to a file
 ...
 ...
 # Check if a_key has been pressed in the command window
 ...
 ...
 if a_key_pressed:
 # Perform some pre-termination tasks
 ...
 ...
 # Close the output data file
 ...
 ...
 raise SystemExit('Exit')


I am running the code with Python 3.6 on a windows 10 platform. I have tried
many approaches (mainly those posted on stackoverflow) but I have yet to
find an approach that works for this structure.

Note:
   1) The code is executed in the windows 10 command window
   2) I am not using wxPython, IDLE, or pyGame in this application.
   3) The time to get the data, process it and write it to a file can
  take from 0.5 sec to 1.5 sec
   4) The key hit need not be echoed to the command window


Are you okay with demanding a specific key, rather than simply "press
any key"? Even better, key combination? Handle Ctrl-C by catching
KeyboardInterrupt and you can take advantage of Python's existing
cross-platform handling of the standard interrupt signal.

If Ctrl-C won't work for you, how about stipulating that it be Enter?
"Press Enter to quit" isn't too much worse than "Press any key to
quit" (plus you have less chance of accidentally terminating the
program when you don't want to). Spin off a thread to wait for enter.
I've tested this only on Linux, but it ought to work:

import threading
import time

shutdown = False
def wait_for_enter():
 print("Hit Enter to quit.")
 input()
 global shutdown; shutdown = True

threading.Thread(target=wait_for_enter).start()

while "more work to do":
 print("Getting data...")
 time.sleep(1)
 print("Saving data to file...")
 time.sleep(1)
 if shutdown:
 print("Pre-termination...")
 time.sleep(1)
 raise SystemExit("exit")

If it doesn't, try switching around which is the secondary thread and
which is the primary - spin off a thread to do the work, then call
input() in the main thread.

ChrisA




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


Re: Processing a key pressed in Python 3.6

2018-01-23 Thread Virgil Stokes

Ok Dennis,

You were correct. The following also works in the Windows 10 command window.


import time
import msvcrt

while "more work to do":
    print("Getting data...")
    time.sleep(1)
    print("Saving data to file...")
    time.sleep(1)
    key = msvcrt.getwch()
    #print('key: %s'%key)  # just to show the result
    if key == chr(27):
    print("Pre-termination...")
    time.sleep(1)
    raise SystemExit("exit") 
Note, I am using the "Esc" key to exit, which is one answer to my last 
posting on this topic.



On 2018-01-23 20:37, Dennis Lee Bieber wrote:

On Tue, 23 Jan 2018 19:50:57 +0100, Virgil Stokes  declaimed
the following:


I am running the code with Python 3.6 on a windows 10 platform. I have
tried many approaches (mainly those posted on stackoverflow) but I have
yet to find an approach that works for this structure.

Note:
   1) The code is executed in the windows 10 command window
   2) I am not using wxPython, IDLE, or pyGame in this application.
   3) The time to get the data, process it and write it to a file can
  take from 0.5 sec to 1.5 sec
   4) The key hit need not be echoed to the command window

And none of your searching found
https://docs.python.org/3/library/msvcrt.html
which is part of the standard library (and documented in the library
manual). The module IS Windows specific, you'd have to rewrite the code to
run on Linux.

Now, if your requirement was to detect a keypress WHILE the data
fetch/processing was happening, the solution will be different.


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


Re: Why is there no functional xml?

2018-01-23 Thread Rustom Mody
On Tuesday, January 23, 2018 at 8:23:43 PM UTC+5:30, Peter Otten wrote:
> Rustom Mody wrote:
> > [I find the OO/imperative style of making a half-done node and then
> > [throwing
> > piece-by-piece of contents in/at it highly aggravating]
> 
> What I meant is that once you throw a bit of introspection at it much of the 
> tedium vanishes. Here's what might become of the DOM-creation as part of an 
> actual script:


«snipped named-tuple magic»

> To generalize that to handle arbitrarily nested lists and namedtuples a bit 
> more effort is needed, but I can't see where lxml.objectify could make that 
> much easier.

You really mean that??
Well sure in the programming world and even more so in the python world
“Flat is better than nested” is a maxim

But equally programmers need to satisfy requirements…

And right now I am seeing things like this
---
http://schemas.xmlsoap.org/soap/envelope/";>

http://example.com/";>

  
AGGREGATION_ANALYSIS
  
  

  
  

  
  



  
  

  «Big base64 blob»

  





---
Thats 7 levels of nesting (assuming I can count right!)


Speaking of which another followup question:


With
# Read above xml
>>> with open('soap_response.xml') as f: inp = etree.parse(f)
# namespace dict
>>> nsd = {'soap': "http://schemas.xmlsoap.org/soap/envelope/";, 'locns': 
>>> "http://example.com/"}

The following behavior is observed — actual responses elided in the interest of
brevity

>>> inp.xpath('//soap:Body', namespaces = nsd)
finds/reaches the node

>>> inp.xpath('//locns:blobRetrieveResponse', namespaces = nsd)
finds

>>> inp.xpath('//locns:dtCreationDate', namespaces = nsd)
does not find

>>> inp.xpath('//dtCreationDate', namespaces = nsd)
finds

>>> inp.xpath('//dtCreationDate')
also finds


Doesnt this contradict the fact that dtCreationDate is under the locns 
namespace??

Any explanations??
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python to C code generator

2018-01-23 Thread Steven D'Aprano
On Tue, 23 Jan 2018 17:43:18 +, bartc wrote:

> It wouldn't be a satisfactory way of writing C programs. So, although
> I'm not that big a fan of C syntax, it might be better to write C as C,
> and Python as Python, to avoid confusion.)

This.

The fundamental reality is that `a + b` means different things in C and 
Python. Even if you limit yourself to integers and not arbitrary values 
(fractions, lists, strings, etc) the semantics are different:

- in C, ints have a fixed number of bits and any addition which
  ends up out of range is undefined behaviour[1];

- while Python uses BigInts, overflow is impossible, and the
  only possible error is that you run out of memory and an
  exception is raised (although the addition can take an 
  indefinite long amount of time).


Often the difference doesn't matter... but when it does matter, it 
*really* matters.




[1] If anyone thinks that it is addition with overflow, you are wrong. 
Some C compilers *may* use overflow, but the language strictly defines it 
as undefined behaviour, so the compiler can equally choose to set your 
computer on fire[2] if it prefers.

https://blog.regehr.org/archives/213



[2] http://www.catb.org/jargon/html/H/HCF.html


-- 
Steve

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