[issue22750] xmlapp.py display bug when validate XML by DTD
Ferdinand added the comment: I found a solution : from xml.sax import make_parser from xml.sax.handler import feature_namespaces, feature_validation from xml.sax.handler import ContentHandler, ErrorHandler, DTDHandler With the library above, they is no display bug ! -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22750 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22750] xmlapp.py display bug when validate XML by DTD
New submission from Ferdinand: Code is working, but the TEXT variable is not showing the correct line of the XML file but the the line after correct one : Exemple : [u6v7mr@vl-a-txx-05 Python]$ ./validateXML.py DTD/herve.xml DTD/Tomcat.dtd ERROR: 'I' is not an allowed value for the 'value' attribute at DTD/herve.xml:3:19 TEXT: ' tomcat' XML file is KO [u6v7mr@vl-a-txx-05 Python]$ The line expected is : env value=I/ The corresponding line in the DTD file is this one : !ATTLIST env value (D|F|R|P|Q|A) #REQUIRED Here is the python's content : [u6v7mr@vl-a-txx-05 Python]$ cat validateXML.py #!/usr/bin/python from xml.parsers.xmlproc import xmlproc from xml.parsers.xmlproc import xmlval from xml.parsers.xmlproc import xmldtd def validate_xml(xml_filename, dtd_filename): Validate a given XML file with a given external DTD. If the XML file is not valid, an exception will be printed with an error message. dtd = xmldtd.load_dtd(dtd_filename) parser = xmlproc.XMLProcessor() parser.set_application(xmlval.ValidatingApp(dtd, parser)) parser.dtd = dtd parser.ent = dtd try : parser.parse_resource(xml_filename) except : print XML file is KO return 0 print XML file is OK if __name__ == __main__: import sys xml_filename, dtd_filename = sys.argv[1], sys.argv[2] validate_xml(xml_filename, dtd_filename) [u6v7mr@vl-a-txx-05 Python]$ Here is the XML file with the error for the element env : [u6v7mr@vl-a-txx-05 Python]$ cat DTD/herve.xml ?xml version='1.0' encoding='utf-8'? config env value=I/ tomcat count=1 start=1 step=1 adresseIP=10.108.250.72 offset=10 name=HERVE_R_NM_GEN shutdown_port=40010 jvm memory verbosegc=true logrotategc=true min=512 max=1024 /memory permgen max=128 min=128 /permgen /jvm /tomcat tomcat count=1 start=2 step=1 adresseIP=10.108.250.72 offset=20 name=HERVE_R_NM_GEN shutdown_port=40010 jvm memory verbosegc=true logrotategc=true min=512 max=1024 /memory permgen max=128 min=128 /permgen /jvm /tomcat /config [u6v7mr@vl-a-txx-05 Python]$ Here is the stdout expected : [u6v7mr@vl-a-txx-05 Python]$ ./validateXML.py DTD/herve.xml DTD/Tomcat.dtd ERROR: 'I' is not an allowed value for the 'value' attribute at DTD/herve.xml:3:19 TEXT: ' env value=I/' XML file is KO -- components: XML files: Tomcat.dtd messages: 230135 nosy: Spider06 priority: normal severity: normal status: open title: xmlapp.py display bug when validate XML by DTD type: behavior versions: Python 2.7 Added file: http://bugs.python.org/file37049/Tomcat.dtd ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22750 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17225] JSON decoder reports wrong column number on first line
Ferdinand Beyer added the comment: Line and column number are included in the formatted error message (raise ValueError(errormsg(...))). They are currently not accessible separately as exception arguments. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17225 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17225] JSON decoder reports wrong column number on first line
New submission from Ferdinand Beyer: The linecol() function in json/decoder.py computes the line and column numbers for a byte offset in a string. Both numbers are expected to start with 1 (as in text editors). If the position is in the first line, the returned column is off by one (or starting with zero): from json.decoder import linecol linecol('spam', 0) # Should be (1, 1) (1, 0) linecol('\nspam', 1) (2, 1) The problem is the line: if lineno == 1: colno = pos that should read if lineno == 1: colno = pos + 1 -- components: Library (Lib) messages: 182320 nosy: fbeyer priority: normal severity: normal status: open title: JSON decoder reports wrong column number on first line type: behavior versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17225 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Parse command line output using textfsm
Hi List, I am using python to run command line utilities on Linux machine. I came across textfsm and feel that it fits my requirements. Here is the structure of the output from the 2 utilities Command 1: Id AddressPort Location State Tenant count Max tenants Description -- -- -- --- 1 10.0.32.20 8443 -ACTIVE712 - 2 10.0.32.21 8443 -ACTIVE712 - 3 10.0.32.22 8443 -ACTIVE712 - Command 2: 12012-04-04 19:54 00DD000q3di GOLDCompany 1 CA360 ACTIVE Yes https://10.0.32.26:7000 8 50 17 2012-04-09 23:01 00Dd000efRF SILVER Company 2 CA360 ACTIVE Yes https://10.0.32.20:7014 1 440 27 2012-04-10 03:42 00Dd000efdS TRIAL Company 3 CA360 ACTIVE Yes https://10.0.32.23:7021 4 10 32 2012-04-11 11:24 00Dd000eiGb TRIAL Company 4 CA360 INACTIVE Yes https://10.0.32.21:7000 2 25 I found this link documenting how to parse tabular data: http://code.google.com/p/textfsm/wiki/TextFSMCodeLab#Parsing_tabular_data My question is, what is the syntax of the pattern matching used. Is it the same as python regexp? Any other documentation/examples available for this library? I'm also open to suggestions of using other libraries. Basically, I would like this to be semantically as close as possible to working with data extracted from a database. Thanks and regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list
COM and threading, CoInitialize?
Greetings to list members, I am trying to use an Acrobat COM object in a class that is subclassed from threading.Thread. Since threading.Thread is subclassed, the __init__ method of the inheriting class must explicitly call the threading.Thread.__init__ method of the parent. I guess I'm missing something similar for the COM object. Threading code and traceback below. Any pointers? CODE: = class process_file(threading.Thread): def __init__(self,connection): threading.Thread.__init__(self) self.connection=connection def run(self): irrelevant code here # Acrobat COM starts AVD = win32com.client.Dispatch('AcroExch.AVDoc') # Traceback refers to this line (89) pdfname=pdfname.replace('.pdf',PDFNAME_SUFFIX) AVD.Open(pdf.name, pdfname) # print 'opened avdoc' == TRACEBACK: == Exception in thread Thread-1: Traceback (most recent call last): File C:\Python25\lib\threading.py, line 486, in __bootstrap_inner self.run() File D:\Ferdinand\#Storage\Ferdi-python\Acrobat Automation\temp testing.py, line 89, in run AVD = win32com.client.Dispatch('AcroExch.AVDoc') File C:\Python25\Lib\site-packages\win32com\client\__init__.py, line 95, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) File C:\Python25\Lib\site-packages\win32com\client\dynamic.py, line 98, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File C:\Python25\Lib\site-packages\win32com\client\dynamic.py, line 78, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) com_error: (-2147221008, 'CoInitialize has not been called.', None, None) == TIA. Best regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list
Re: Varibles -- copies and references
People get a confused because if you pass a mutable object inside a def function and mutate that object the changes /are/ propagated outside-- because now you have a name inside the function and a name outside the object both pointing to the same object. Since tuples are immutable, I guess passing tuples to functions would achieve the result desired by me: result = func ( (anobject) ) If you want a function to work on a copy of a mutable object passed to it, and not the real object itself, you must explicitly make a copy of it. Or using deepcopy within the function definition. This would be better as I could simply pass an object to the function. I don't think there is another way to make an object mutable by something in the class definition. Is there? class AA: a=111 x=AA() x.a 111 y=x y.a 111 y.a=222 x.a 222 # undesirable? z=copy.deepcopy(x) z.a 222 z.a=444 x.a 222 y.a 222 The reason you see the undesirable behavior of a change to y.a showing the same result of x.a... is because those are the *exact* same instance. You didn't make a copy of the instance, you just made a new name and bound it to the same instance. If you want to copy in Python you have to explicitly do so, via the 'copy' module or any copy methods an object provides. Could someone please give me an example of how to implement this copy method for the AA class. Going the other way: a=555 d=copy.copy(a) # Supposed to be a shallow copy d 555 d=444 a # But, it doesn't change the original 555 Is there any way to get ints/strings to refer to the same object? Thanks and Regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list
Varibles -- copies and references
Hi Some weeks back I had been following the thread Why can't assign to function call. Today, I saw the function scope thread, and decided I should ask about the behaviour below: # Simple variables p=55 q=p q 55 q=44 p 55 # In a function def dd(d): del d['key'] return d adict={'pramod':'goodboy', 'ferdi':'badboy', 'key':'to be deleted'} dd(adict) {'ferdi': 'badboy', 'pramod': 'goodboy'} adict {'ferdi': 'badboy', 'pramod': 'goodboy'} # undesirable? # In a class class AA: a=111 x=AA() x.a 111 y=x y.a 111 y.a=222 x.a 222 # undesirable? z=copy.deepcopy(x) z.a 222 z.a=444 x.a 222 y.a 222 Guess the simple types show the expected behaviour (even though they are technically instances of existing classes). The user defined classes seem to be references/shallow copies. The function scope thread refers to http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objectsto explain why default parameters should not be modified. But, in the function example above, the dictionary passed as a variable *in the calling scope* gets modified. How can you avoid the dictionary being changed? Assigning one object to another always creates references? Can you modify anything in the class def to get a copy? [I have a tingling feeling this is how the default types (integers, strings etc) are defined] Thanks and best regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 64, Issue 617
Secondly, it has no way to display the image drawn on. Is it possible, or do I have to pass the image off to another module's methods? im.show() this will display the image (and any modification(s) made to it) Example: Draw a Grey Cross Over an Image import Image, ImageDraw im = Image.open(lena.pgm) draw = ImageDraw.Draw(im) draw.line((0, 0) + im.size, fill=128) draw.line((0, im.size[1], im.size[0], 0), fill=128) del draw # write to stdout im.save(sys.stdout, PNG) Hope that helps That's pretty much the code I used. In fact, I borrowed it from the pdf. I just tried it, and it output %PNG. im.save(picture1.png) OR im.save(picture1 png) # not sure if it has to be PNG What was happening earlier was that the binary data was being directed to the standard output, which is where all your text is printed by a print statement (print func in Py 3000). If you open a png in notepad, you will notice that the 1st four characters are indeed %PNG, which is the magic number for a PNG file. For further info, see: http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_fileshttp://en.wikipedia.org/wiki/Magic_number_%28programming%29#Magic_numbers_in_files http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header Regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list
Re: strange dict issue
James First off, the computer is always right :-) {'value': 'Route66', 'key': 'NAME'} Yet when the second line of my code throws an error saying the key 'NAME' doesn't exist. If you look carefully the key NAME indeed does not exist. The dictionary contains 2 key-value pairs. This should clear things out: *key * *value *'value' 'Route66' 'key' 'NAME' Get it? NAME is a value, not a key. You have used the string 'key' itself as a dictionary key. My advice would be to not use such conflicting names. Regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list
Transferring a file over sockets
I am using sockets to transfer a file over LAN. There are 2 scripts, the server opens a listens for connection and the client is run on another machine. I always make sure the server is run first. The strange thing is that if the the server script is double-clicked and executed (run in a console with title %Python path%python.exe) the output file saved on the server is truncated. It works just fine if you open the server script in IDLE and then run it. The client script can be run in either way, it still works. You could try using any arbitrary file to test this behaviour after changing the file name in both the scripts. == # file receiver # work in progress import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = '192.168.1.17' PORT = 31400 s.bind((HOST, PORT)) s.listen(3) conn, addr = s.accept() print 'conn at address',addr conn.send('READY') f = open('C:\\Documents and Settings\\USER\\Desktop\\test.pdf','wb') fsize=int(conn.recv(8)) print 'File size',fsize f.write(conn.recv(fsize)) f.close() conn.close() s.close() raw_input('Press any key to exit') === # file sender !!! # Work in progress import socket, os from stat import ST_SIZE HOST = '192.168.1.17' PORT = 31400 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST,PORT)) if s.recv(5)!='READY': raw_input('Unable to connect \n\n Press any key to exit ...') s.close() exit() f=open('C:\\Documents and Settings\\USER\\Desktop\\t.pdf', 'rb') fsize=os.stat(f.name)[ST_SIZE] s.send(str(fsize)) s.send(f.read()) s.close() f.close() === Thanks for reading!! Best regards, Ferdi -- http://mail.python.org/mailman/listinfo/python-list