Parse command line output using textfsm

2012-06-21 Thread Ferdinand Sousa
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"?

2009-02-10 Thread Ferdinand Sousa
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

2009-02-03 Thread Ferdinand Sousa
 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

2009-02-02 Thread Ferdinand Sousa
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

2009-01-28 Thread Ferdinand Sousa
>
> 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_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

2009-01-12 Thread Ferdinand Sousa
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

2008-12-17 Thread Ferdinand Sousa
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