How to write a simple shell loop in python?
Hi, I am trying to write a simple shell loop in Python. My simple approach works fine - but the first output line after entering something is always indented by one blank. Is there any logic explanation for this? How can I get rid of the blank? Is there a smarter way to write a simple shell loop which would work better? Thanks, Dietrich Here my approach: $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> while (1): ... print "$ ", ... input = sys.stdin.readline() ... input = input.strip() ... print input ... print input ... print input ... $ one one one one $ two two two two $ three three three three $ Traceback (most recent call last): File "", line 3, in KeyboardInterrupt >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write a simple shell loop in python?
[Sorry for top posting - I had a HD problem and lost the original mails] Hi Saul, Steve, Ben, James, Scott David and James! Thank you all very much for your help! I finally got rid of the extra space and also understood why the space was printed :) After using Steve's 'input = raw_input("$ ")' solution for a while - it does exactly what I want and is the fastest fix also - I wanted command line editing and switched to the cmd.Cmd module. Special thanks to James Mills for writing his own solution! I would like to use your component - but cmd.Cmd is just perfect for my purpose and I want to run my program without the need to install any new module (I am writing the program for somebody else). I got interested in circuits though - but the homepage http://trac.softcircuit.com.au/circuits/ (currently?) seems not to be available. Thanks again for your answers :) Dietrich PS: There is a little tutorial for cmd.Cmd here: - http://www.doughellmann.com/PyMOTW/cmd/index.html The documentation is here: - http://docs.python.org/library/cmd.html (got it from Ben Finney's post) On Wed, 2009-01-21 at 08:37 -0500, Steve Holden wrote: > Dietrich Bollmann wrote: > > Hi, > > > > I am trying to write a simple shell loop in Python. > > > > My simple approach works fine - but the first output line after entering > > something is always indented by one blank. > > > > Is there any logic explanation for this? > > How can I get rid of the blank? > > Is there a smarter way to write a simple shell loop which would work > > better? > > > > Thanks, Dietrich > > > > > > Here my approach: > > > > $ python > > Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) > > [GCC 4.3.2] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > >>>> import sys > >>>> while (1): > > ... print "$ ", > > ... input = sys.stdin.readline() > Just replace the lines above with > > input = raw_input("$ ") > > and you'll be fine. The "," in the print statement causes the > interpreter to set a flag to emit a space before the next output unless > it has just printed a newline. The "newline", of course, is provided by > the input, so the next print emits a space since it *hasn't* just > emitted a newline. > > regards > Steve > > > ... input = input.strip() > > ... print input > > ... print input > > ... print input > > ... > > $ one > > one > > one > > one > > $ two > > two > > two > > two > > $ three > > three > > three > > three > > $ > > Traceback (most recent call last): > > File "", line 3, in > > KeyboardInterrupt > > > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > > -- http://mail.python.org/mailman/listinfo/python-list
How to convert between Japanese coding systems?
Hi, Are there any functions in python to convert between different Japanese coding systems? I would like to convert between (at least) ISO-2022-JP, UTF-8, EUC-JP and SJIS. I also need some function to encode / decode base64 encoded strings. I get the strings (which actually are emails) from a server on the internet with: import urllib server = urllib.urlopen(serverURL, parameters) email = server.read() The coding systems are given in the response string: Example: email = '''[...] Subject: =?UTF-8?Q?romaji=E3=81=B2=E3=82=89=E3=81=8C=E3=81=AA=E3=82=AB=E3=82=BF?= =?UTF-8?Q?=E3=82=AB=E3=83=8A=E6=BC=A2=E5=AD=97?= [...] Content-Type: text/plain; charset=EUC-JP [...] Content-Transfer-Encoding: base64 [...] cm9tYWpppNKk6aSspMqlq6W/paulyrTBu/oNCg0K ''' My idea is to first parse the 'email' string and to extract the email body as well as the values of the 'Subject: ', the 'Content-Type: ' and the 'Content-Transfer-Encoding: ' attributes and to after use them to convert them to some other coding system: Something in the lines of: (subject, contentType, contentTransferEncoding, content) = parseEmail(email) to = 'utf-8' subjectUtf8 = decodeSubject(subject, to) from = contentType to = 'utf-8' contentUtf8 = convertCodingSystem(decodeBase64(content), from, to) The only problem is that I could not find any standard functionality to convert between different Japanese coding systems. Thanks, Dietrich Bollmann -- http://mail.python.org/mailman/listinfo/python-list
segmentation fault when executing PyImport_ImportModule("sys")
Hi, Since some time I get the following segmentation fault in an application which used to work fine until recently. I made a backtrace but couldn't find the reason for the segmentaion fault until now. In the hope that somebody might have encountered a similar problem or does understand the backtrace better than me and can explain it I posted the backtrace here... At the end of the backtrace I appended some more context concerning the involved code. Thanks for your help :) Dietrich Here comes the backtrace: Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb046ab90 (LWP 9854)] threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154 154 ../Python/pystate.c: No such file or directory. in ../Python/pystate.c (gdb) bt full #0 threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154 No locals. #1 0xb7e8a897 in PyEval_GetGlobals () at ../Python/ceval.c:3340 current_frame = #2 0xb7eaeb67 in PyImport_Import (module_name=0xb7119480) at ../Python/import.c:2400 globals = import = builtins = r = silly_list = (PyObject *) 0xb738fb0c builtins_str = (PyObject *) 0xb7391c50 import_str = (PyObject *) 0xb7391fc0 #3 0xb7eaede5 in PyImport_ImportModule (name=0x901504b "sys") at ../Python/import.c:1903 pname = (PyObject *) 0xb7119480 result = (PyObject *) 0x0 #4 0x08996c9f in py_stdouterr_buffer_new () at source/blender/commandport/blender/src/py_stdouterr_buffer.c:75 buffer = (py_stdouterr_buffer) 0x94fd428 func_StringIO = (PyObject *) 0x9152a48 args_StringIO = (PyObject *) 0x0 #5 0x089967e1 in bcp_blender_handler_new () at source/blender/commandport/blender/src/bcp_blender.c:130 handler = (bcp_blender_handler) 0x9810420 #6 0x08998db3 in bcp_handle_client (client_socket=8) at source/blender/commandport/blender/src/bcp_handle_client.c:73 debug = 0 debug3 = 0 debug4 = 0 message_handler = (message_handler) 0x97eba10 blender_handler = (bcp_blender_handler) 0x0 command = 0x0 result = 0x9152a48 "[EMAIL PROTECTED]@@[EMAIL PROTECTED] \"[EMAIL PROTECTED]@[EMAIL PROTECTED] [EMAIL PROTECTED]@[EMAIL PROTECTED] \b\"[EMAIL PROTECTED]@w# [EMAIL PROTECTED]" package_number = -1216545219 #7 0x08998d60 in bcp_client_thread (args=0x0) at source/blender/commandport/blender/src/bcp_server.c:164 targs = (struct bcp_client_thread_args *) 0x0 client_socket = 8 client_thread = 2957421456 #8 0xb76b04fb in start_thread () from /lib/i686/cmov/libpthread.so.0 No symbol table info available. #9 0xb77c2d7e in clone () from /lib/i686/cmov/libc.so.6 No symbol table info available. (gdb) q The program is running. Exit anyway? (y or n) y --- and here some informations about its context: Python-2.4.4/Python/ceval.c line 3340: PyFrameObject *current_frame = PyEval_GetFrame(); context: --- PyObject * PyEval_GetGlobals(void) { PyFrameObject *current_frame = PyEval_GetFrame(); if (current_frame == NULL) return NULL; else return current_frame->f_globals; } --- Python-2.4.4/Python/pystate.c lign 154: { context: --- /* Default implementation for _PyThreadState_GetFrame */ static struct _frame * threadstate_getframe(PyThreadState *self) { return self->frame; } --- Python-2.4.4/Python/import.c lign 2400: globals = PyEval_GetGlobals(); context: --- PyObject * PyImport_Import(PyObject *module_name) { ... /* Get the builtins from current globals */ globals = PyEval_GetGlobals(); if (globals != NULL) { Py_INCREF(globals); builtins = PyObject_GetItem(globals, builtins_str); if (builtins == NULL) goto err; } ... } --- Python-2.4.4/Python/import.c lign 1903: result = PyImport_Import(pname); context: --- PyObject * PyImport_ImportModule(char *name) { PyObject *pname; PyObject *result; pname = PyString_FromString(name); if (pname == NULL) return NULL; result = PyImport_Import(pname); Py_DECREF(pname); return result; } --- source/blender/commandport/blender/src/py_stdouterr_buffer.c lign 75:buffer->mod_sys = PyImport_ImportModule("sys"); context: --- /** Make a new python io buffer. */ py_stdouterr_buffer py_stdouterr_buffer_new() { py_stdouterr_buffer buffer; buffer = (py_stdouterr_buffer) malloc(sizeof(py_stdouterr_buffer_struct)); if (buffer == NULL) { fprintf(stderr, "Couldn't allocate memory for new py_stdouterr_buffer! \n"); exit(ERROR_MEMORY); } buffer->mod_sys = PyImport_ImportModule("sys"); buffer->mod_cStringIO = PyImport_ImportModule("cStringIO"); /* store stdout and stderr */ buffer->stdout_obj = PyObject
segmentation fault when executing PyImport_ImportModule("sys")
Hi, Since some time I get the following segmentation fault in an application which used to work fine until recently. I made a backtrace but couldn't find the reason for the segmentaion fault until now. In the hope that somebody might have encountered a similar problem or does understand the backtrace better than me and can explain it I posted the backtrace here... At the end of the backtrace I appended some more context concerning the involved code. Thanks for your help :) Dietrich Here comes the backtrace: Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb046ab90 (LWP 9854)] threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154 154 ../Python/pystate.c: No such file or directory. in ../Python/pystate.c (gdb) bt full #0 threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154 No locals. #1 0xb7e8a897 in PyEval_GetGlobals () at ../Python/ceval.c:3340 current_frame = #2 0xb7eaeb67 in PyImport_Import (module_name=0xb7119480) at ../Python/import.c:2400 globals = import = builtins = r = silly_list = (PyObject *) 0xb738fb0c builtins_str = (PyObject *) 0xb7391c50 import_str = (PyObject *) 0xb7391fc0 #3 0xb7eaede5 in PyImport_ImportModule (name=0x901504b "sys") at ../Python/import.c:1903 pname = (PyObject *) 0xb7119480 result = (PyObject *) 0x0 #4 0x08996c9f in py_stdouterr_buffer_new () at source/blender/commandport/blender/src/py_stdouterr_buffer.c:75 buffer = (py_stdouterr_buffer) 0x94fd428 func_StringIO = (PyObject *) 0x9152a48 args_StringIO = (PyObject *) 0x0 #5 0x089967e1 in bcp_blender_handler_new () at source/blender/commandport/blender/src/bcp_blender.c:130 handler = (bcp_blender_handler) 0x9810420 #6 0x08998db3 in bcp_handle_client (client_socket=8) at source/blender/commandport/blender/src/bcp_handle_client.c:73 debug = 0 debug3 = 0 debug4 = 0 message_handler = (message_handler) 0x97eba10 blender_handler = (bcp_blender_handler) 0x0 command = 0x0 result = 0x9152a48 "[EMAIL PROTECTED]@@[EMAIL PROTECTED] \"[EMAIL PROTECTED]@[EMAIL PROTECTED] [EMAIL PROTECTED]@[EMAIL PROTECTED] \b\"[EMAIL PROTECTED]@w# [EMAIL PROTECTED]" package_number = -1216545219 #7 0x08998d60 in bcp_client_thread (args=0x0) at source/blender/commandport/blender/src/bcp_server.c:164 targs = (struct bcp_client_thread_args *) 0x0 client_socket = 8 client_thread = 2957421456 #8 0xb76b04fb in start_thread () from /lib/i686/cmov/libpthread.so.0 No symbol table info available. #9 0xb77c2d7e in clone () from /lib/i686/cmov/libc.so.6 No symbol table info available. (gdb) q The program is running. Exit anyway? (y or n) y --- and here some informations about its context: Python-2.4.4/Python/ceval.c line 3340: PyFrameObject *current_frame = PyEval_GetFrame(); context: --- PyObject * PyEval_GetGlobals(void) { PyFrameObject *current_frame = PyEval_GetFrame(); if (current_frame == NULL) return NULL; else return current_frame->f_globals; } --- Python-2.4.4/Python/pystate.c lign 154: { context: --- /* Default implementation for _PyThreadState_GetFrame */ static struct _frame * threadstate_getframe(PyThreadState *self) { return self->frame; } --- Python-2.4.4/Python/import.c lign 2400: globals = PyEval_GetGlobals(); context: --- PyObject * PyImport_Import(PyObject *module_name) { ... /* Get the builtins from current globals */ globals = PyEval_GetGlobals(); if (globals != NULL) { Py_INCREF(globals); builtins = PyObject_GetItem(globals, builtins_str); if (builtins == NULL) goto err; } ... } --- Python-2.4.4/Python/import.c lign 1903: result = PyImport_Import(pname); context: --- PyObject * PyImport_ImportModule(char *name) { PyObject *pname; PyObject *result; pname = PyString_FromString(name); if (pname == NULL) return NULL; result = PyImport_Import(pname); Py_DECREF(pname); return result; } --- source/blender/commandport/blender/src/py_stdouterr_buffer.c lign 75:buffer->mod_sys = PyImport_ImportModule("sys"); context: --- /** Make a new python io buffer. */ py_stdouterr_buffer py_stdouterr_buffer_new() { py_stdouterr_buffer buffer; buffer = (py_stdouterr_buffer) malloc(sizeof(py_stdouterr_buffer_struct)); if (buffer == NULL) { fprintf(stderr, "Couldn't allocate memory for new py_stdouterr_buffer! \n"); exit(ERROR_MEMORY); } buffer->mod_sys = PyImport_ImportModule("sys"); buffer->mod_cStringIO = PyImport_ImportModule("cStringIO"); /* store stdout and stderr */ buffer->stdout_obj = PyObject
Re: segmentation fault when executing PyImport_ImportModule("sys")
Hi, I found the reason for the segmentation fault and hope that my solution might be helpful for somebody else some day :) On Tue, 2008-04-08 at 19:31 +0900, Dietrich Bollmann wrote: Program received signal SIGSEGV, Segmentation fault. > [Switching to Thread 0xb046ab90 (LWP 9854)] > threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154 > 154 ../Python/pystate.c: No such file or directory. > in ../Python/pystate.c > (gdb) bt full > #0 threadstate_getframe (self=0xb7e8a889) at ../Python/pystate.c:154 > No locals. > #1 0xb7e8a897 in PyEval_GetGlobals () at ../Python/ceval.c:3340%G% @ > ...snip... > #3 0xb7eaede5 in PyImport_ImportModule (name=0x901504b "sys")%G%@ > ...snip... The segmentation fault was caused by the interaction of my own code with some changes made to the code base recently which where intended to make the python functions thread save: Here come the new additions to the function which starts the Python interpreter: void BPY_start_python( int argc, char **argv ) { PyThreadState *py_tstate = NULL; ... /* Initialize thread support (also acquires lock) */ PyEval_InitThreads(); /* Don'\''t allow the Python Interpreter to release the GIL on * its own, to guarantee PyNodes work properly. For Blender this * is currently the best default behavior. * The following code in C is equivalent in Python to: * "import sys; sys.setcheckinterval(sys.maxint)" */ _Py_CheckInterval = PyInt_GetMax(); ... py_tstate = PyGILState_GetThisThreadState(); PyEval_ReleaseThread(py_tstate); ... } Until recently only one thread accessed python objects and there was no need for any code making Python thread-save. This didn't cause any problem with my own code as I only inserted events in form of python code to execute into a thread save event queue which was worked off by the only thread accessing python objects. With the new python thread code added I had to surround all my code which accesses python objects with PyGILState_*() in order to solve the problem: PyGILState_STATE gstate; /* aquire python thread */ gstate = PyGILState_Ensure(); ...access python objects... /* release python thread */ PyGILState_Release(gstate); The PyGILState_*() functions ensure that a global Python thread lock is held by the current thread, protecting the Python objects from being accessed by other threads in the same time. For more detailed explanations see the chapter "8.1 Thread State and the Global Interpreter Lock" (http://docs.python.org/api/threads.html) in the Python/C API Reference Manual. Thanks for your help, Dietrich -- http://mail.python.org/mailman/listinfo/python-list
Error in Extending/Embedding FAQ, point 16: How do I tell "incomplete input" from "invalid input"?
Hi, Both code examples from paragraph 16 from the Python Extending / Embedding FAQ - 'How do I tell "incomplete input" from "invalid input"?' - ( http://www.python.org/doc/faq/extending/#how-do-i-tell-incomplete-input-from-invalid-input ) do not work with the current state of Python anymore. In the second code example, the error message returned by Python is checked in order to differentiate errors caused by an incomplete input from other syntax errors: if (PyArg_ParseTuple (val, "sO", &msg, &obj) && !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ In the current Python version there are more error messages indicating an incomplete Python input and I could make the code work for a while by adding the following strings to the condition: /* error messages indicating an incomplete input */ if (PyArg_ParseTuple(error, "sO", &message, &obj) && (!strcmp(message, "unexpected EOF while parsing") || !strcmp(message, "expected an indented block") || !strcmp(message, "EOF while scanning triple-quoted string") ) ) { /* E_EOF */ but recently there are also cases which generate error messages which are too general to be added to this list. The following code for example: >>> eins = [1, ... 2, ... 3] >>> is accepted without any problem by the Python shell. When using the code from the FAQ and entering it line by line already the second line causes a simple "invalid syntax" error: >>> eins = [1, ... 2, File "", line 2 2, ^ SyntaxError: invalid syntax which is to general to be integrated into the list of tested error messages as it might be caused also by code like: >>> one two File "", line 1 one two ^ SyntaxError: invalid syntax which generates an "invalid syntax" error even in the Python shell. I also tried the first code example of paragraph '16 How do I tell "incomplete input" from "invalid input"?' of the FAQ in order to see if it could be used to make the difference between syntax errors and incomplete code errors. But - as in the case before - the returned error code is E_SYNTAX (14 = Syntax error) and not E_EOF (11 = End Of File) as should be expected. Is there anybody who has an idea how to differentiate the first case from the second in order to mimic the behaviour of the Python shell from c code? If this shouldn't be possible lists split into different lines couldn't be accepted anymore or the feature of the Python shell to described in paragraph 16 of the faq: Sometimes you want to emulate the Python interactive interpreter's behavior, where it gives you a continuation prompt when the input is incomplete (e.g. you typed the start of an "if" statement or you didn't close your parentheses or triple string quotes), but it gives you a syntax error message immediately when the input is invalid. would have to be given up and every entered line of code would have to be terminated by an empty line before evaluation :( Thanks for any help, Dietrich -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in Extending/Embedding FAQ, point 16: How do I tell "incomplete input" from "invalid input"?
On Wed, 2008-04-23 at 00:12 +0900, Dietrich Bollmann wrote: > The following code for example: > > >>> eins = [1, > ... 2, > ... 3] > >>> > > is accepted without any problem by the Python shell. > > When using the code from the FAQ and entering it line by line > already the second line causes a simple "invalid syntax" error: > > >>> eins = [1, > ... 2, > File "", line 2 > 2, >^ > SyntaxError: invalid syntax By the way - isn't this error message / error code just "wrong" in the given situation and therefor kind of a "bug"? An "end of file" or "incomplete input" error at least would describe the situation much better - and be a better base for functionality which is based the error code also. --- I also thought that I should explain a little bit more exactly, what I am intending to do with the code based on paragraph 16 (How do I tell "incomplete input" from "invalid input"?) of the Extending/Embedding FAQ: I am using Python as scripting language in an application (blender). In order to interface this application from other programs I programmed a python command port / command socket for this application. Between other clients I also wrote a shell client which connects via the command port to the application. My goal is to make it as similar to a normal python shell as possible - and therefor I try to also mimic the "intelligent" way of the Python shell to react to Python input: - when entering a line which is a complete input, it is immediately evaluated by the shell and the result is printed. - when the last entered line is erroneous, an error message is printed immediately - when the input is incomplete, Python waits for other lines to complete the input - when the line is part of a function definition etc. python waits until an empty line is entered before accepting the input as complete. My problem is to understand when an input is erroneous and when it is incomplete - which is impossible with an error message like "invalid syntax"... So here again my question: How can I make the difference between an incomplete and an erroneous input? The code examples in the FAQ worked fine until now - but do not anymore for the current Python implementation. Thanks, Dietrich By the way: Does anybody know who is responsible for the FAQ and could adapt the examples to the current Python version by changing the code / annotating it? On Wed, 2008-04-23 at 00:12 +0900, Dietrich Bollmann wrote: Hi, > > Both code examples from paragraph 16 from the Python Extending / > Embedding FAQ - 'How do I tell "incomplete input" from "invalid input"?' > - > ( http://www.python.org/doc/faq/extending/#how-do-i-tell-incomplete-input-from-invalid-input ) do not work with the current state of Python anymore. > > In the second code example, the error message returned by Python is > checked in order to differentiate errors caused by an incomplete input > from other syntax errors: > >if (PyArg_ParseTuple (val, "sO", &msg, &obj) && > !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ > > In the current Python version there are more error messages indicating an > incomplete Python input and I could make the code work for a while > by adding the following strings to the condition: > > /* error messages indicating an incomplete input */ > if (PyArg_ParseTuple(error, "sO", &message, &obj) && > (!strcmp(message, "unexpected EOF while parsing") || >!strcmp(message, "expected an indented block") || >!strcmp(message, "EOF while scanning triple-quoted > string") >) > ) { /* E_EOF */ > > but recently there are also cases which generate error messages > which are too general to be added to this list. > > The following code for example: > > >>> eins = [1, > ... 2, > ... 3] > >>> > > is accepted without any problem by the Python shell. > > When using the code from the FAQ and entering it line by line > already the second line causes a simple "invalid syntax" error: > > >>> eins = [1, > ... 2, > File "", line 2 > 2, >^ > SyntaxError: invalid syntax > > which is to general to be integrated into the list of tested > error messages as it might be caused also by code like: > > >>> one two > File "
Re: Error in Extending/Embedding FAQ, point 16: How do I tell "incomplete input" from "invalid input"?
Hi, I found a solution thanks to another posting on c++-sig and an answer by Andreas Klöckner :) Thank you, Andreas! The thread is here: http://mail.python.org/pipermail/c++-sig/2008-April/thread.html#13470 I would like to inform the responsible of the Python Extending/Embedding FAQ, http://www.python.org/doc/faq/extending/ about the broken code in the FAQ and the solution I found. I hope this might prevent other people from the frustration I found myself in this morning (...but unfortunately also, at least partly, from the joy I am experiencing now, after finding the new solution :). Does anybody know how to contact the person in charge? Thanks, Dietrich PS: Of course, I still wonder about the "invalid syntax" error message / code I wrote about. But ok, I hope there will be some more adequate error message / code some day in the future :) On Wed, 2008-04-23 at 01:09 +0900, Dietrich Bollmann wrote: > On Wed, 2008-04-23 at 00:12 +0900, Dietrich Bollmann wrote: > > The following code for example: > > > > >>> eins = [1, > > ... 2, > > ... 3] > > >>> > > > > is accepted without any problem by the Python shell. > > > > When using the code from the FAQ and entering it line by line > > already the second line causes a simple "invalid syntax" error: > > > > >>> eins = [1, > > ... 2, > > File "", line 2 > > 2, > >^ > > SyntaxError: invalid syntax > > By the way - isn't this error message / error code just "wrong" in > the given situation and therefor kind of a "bug"? > > An "end of file" or "incomplete input" error at least would > describe the situation much better - and be a better base for > functionality which is based the error code also. > > --- > > I also thought that I should explain a little bit more exactly, > what I am intending to do with the code based on > paragraph 16 (How do I tell "incomplete input" from "invalid input"?) > of the Extending/Embedding FAQ: > > I am using Python as scripting language in an application (blender). > In order to interface this application from other programs > I programmed a python command port / command socket > for this application. > > Between other clients I also wrote a shell client which connects via > the command port to the application. My goal is to make it as similar > to a normal python shell as possible - and therefor I try to also mimic > the "intelligent" way of the Python shell to react to Python input: > > - when entering a line which is a complete input, > it is immediately evaluated by the shell and the > result is printed. > > - when the last entered line is erroneous, > an error message is printed immediately > > - when the input is incomplete, Python waits > for other lines to complete the input > > - when the line is part of a function definition etc. > python waits until an empty line is entered > before accepting the input as complete. > > My problem is to understand when an input is erroneous and > when it is incomplete - which is impossible with an error message > like "invalid syntax"... > > So here again my question: How can I make the difference > between an incomplete and an erroneous input? > > The code examples in the FAQ worked fine until now - but do not > anymore for the current Python implementation. > > Thanks, Dietrich > > By the way: Does anybody know who is responsible for the FAQ > and could adapt the examples to the current Python version > by changing the code / annotating it? > > > On Wed, 2008-04-23 at 00:12 +0900, Dietrich Bollmann wrote: > Hi, > > > > Both code examples from paragraph 16 from the Python Extending / > > Embedding FAQ - 'How do I tell "incomplete input" from "invalid > input"?' > > - > > > ( > http://www.python.org/doc/faq/extending/#how-do-i-tell-incomplete-input-from-invalid-input > ) do not work with the current state of Python anymore. > > > > In the second code example, the error message returned by Python is > > checked in order to differentiate errors caused by an incomplete input > > from other syntax errors: > > > >if (PyArg_ParseTuple (val, "sO", &msg, &obj) && > > !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ > > > > In the current Python version there are more error messages indicating > an > > incomplete Python input and I could make the code
How to pretty-print cyclic dictionaries?
Hi, I would like to represent graphs as cyclic dictionaries in Python. The python code for the graphs is generated by some other program (written in lisp) and I wonder what would be the best syntax for writing the cycles in Python? The following works: >>> a = {} >>> a['a'] = a As can be seen here: >>> a {'a': {...}} >>> a['a'] {'a': {...}} >>> a['a']['a'] {'a': {...}} >>> a['a']['a']['a'] {'a': {...}} >>> but I wonder if there is some easier syntax to represent the cycles? "Tags" like the following would be ideal: [0] {'a': [0]} but is there something like this in Python? Thanks, Dietrich PS: If there is such a representation, how could I make Python print it out? The normal printing is not very informative: >>> a {'a': {...}} And Pythons pretty-print functionality is not better either: >>> pprint.pprint(a) {'a': } Any idea? Thanks again :) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pretty-print cyclic dictionaries?
Hi Chris and Garrick :) On Thu, 2010-04-29 at 18:09 +, Garrick P wrote: > Chris Rebert rebertia.com> writes: > > ... > > > If you want a prettier print, you could try serializing it to YAML and > > printing the result out; YAML has syntax for "tags". > > > > Cheers, > > Chris > > -- > > http://blog.rebertia.com > > Works fairly well. > > $ python > Python 2.6.4 (r264:75706, Mar 1 2010, 14:28:00) > [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import yaml > >>> a = {} > >>> a['a'] = a > >>> a > {'a': {...}} > >>> print yaml.dump(a) > &id001 > a: *id001 > >>> b = {} > >>> b['b'] = b > >>> b['a'] = a > >>> a['b'] = b > >>> print yaml.dump(a) > &id001 > a: *id001 > b: &id002 > a: *id001 > b: *id002 Great! This is exactly what I was looking for (...even if I still prefer parentheses to indentation - yaml and python with (the option to use) parentheses would be my dream :). Thank you very much! > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pretty-print cyclic dictionaries?
Hi Robert, On Thu, 2010-04-29 at 11:56 -0500, Robert Kern wrote: > On 4/29/10 11:23 AM, Dietrich Bollmann wrote: > > Hi, > > > > I would like to represent graphs as cyclic dictionaries in Python. > > > > The python code for the graphs is generated by some other program > > (written in lisp) and I wonder what would be the best syntax for writing > > the cycles in Python? > > You can implement your ideas using Armin Ronacher's pretty.py: > >http://pypi.python.org/pypi/pretty > > The default pretty printer for dicts looks like this: > > def dict_pprinter(obj, p, cycle): > if cycle: > return p.text('{...}') > p.begin_group(1, '{') > keys = obj.keys() > try: > keys.sort() > except Exception, e: > # Sometimes the keys don't sort. > pass > for idx, key in enumerate(keys): > if idx: > p.text(',') > p.breakable() > p.pretty(key) > p.text(': ') > p.pretty(obj[key]) > p.end_group(1, '}') > > You could conceivably subclass RepresentationPrinter (the variable p above is > an > instance of this) that will assign increasing ID numbers to repeated objects > so > you can tag them. Thanks, this is what I was looking for! ...But I would have to write a parser also, so I'll first try Chris' and Garrick's yaml solution and if I still find some time implement your approach later :) Thanks for your help, Dietrich > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it > had > an underlying truth." >-- Umberto Eco Maybe the mad and terrible part of the problem is Eco's definition of 'truth' ? :) > -- http://mail.python.org/mailman/listinfo/python-list