Archive for September, 2014

dictionary

dictionary is coded in curly braces, “key : value”

we can index the dictionary, the dictionary index operation uses the same syntax as that used for sequences, but the intem in the square brackets is a key, not a relative position.

space in the “key ” is counted.

>>> d = {“food”:’spam’, ‘quantity’ : 4 , ‘color ‘ : ‘pink’}
>>> d
{‘food’: ‘spam’, ‘color ‘: ‘pink’, ‘quantity’: 4}
>>>
>>>
>>> d [“food”]
‘spam’
>>>
>>>
>>> d [“color”]
Traceback (most recent call last):
File “<pyshell#65>”, line 1, in <module>
d [“color”]
KeyError: ‘color’
>>> d [“color “]

Advertisements

Leave a comment

list append and pop

list []

list. prepend (“x”), x will be added to the end of the list

list.pop (x), the x element in the list will pop out for ever!

>>> l=[3,4,5]
>>> l + [1,2,3]
[3, 4, 5, 1, 2, 3]
>>>
>>>
>>> l.append(‘NI’)
>>>
>>> l
[3, 4, 5, ‘NI’]
>>> l.pop (2)
5
>>>
>>>
>>>
>>> l
[3, 4, ‘NI’]

Leave a comment

ECDSA –ImportError: No module named

paramiko模块安装(win)与使用

Nov 22, 2013 19:28 / Python

116–> 0 Comments

安装

.安装Paramiko模块。下载Paramiko。解压并进入目录,输入python setup.py install 安装。

若提示 No module named ecdsa。下载https://pypi.python.org/pypi/ecdsa/ 。解压并进入目录,输入python setup.py install安装。

C:\Python27>cd ecdsa-0.11
C:\Python27\ecdsa-0.11>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\ecdsa
copying ecdsa\curves.py -> build\lib\ecdsa
copying ecdsa\der.py -> build\lib\ecdsa
copying ecdsa\ecdsa.py -> build\lib\ecdsa
copying ecdsa\ellipticcurve.py -> build\lib\ecdsa
copying ecdsa\keys.py -> build\lib\ecdsa
copying ecdsa\numbertheory.py -> build\lib\ecdsa
copying ecdsa\rfc6979.py -> build\lib\ecdsa
copying ecdsa\six.py -> build\lib\ecdsa
copying ecdsa\test_pyecdsa.py -> build\lib\ecdsa
copying ecdsa\util.py -> build\lib\ecdsa
copying ecdsa\_version.py -> build\lib\ecdsa
copying ecdsa\__init__.py -> build\lib\ecdsa
running install_lib
creating C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\curves.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\der.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\ecdsa.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\ellipticcurve.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\keys.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\numbertheory.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\rfc6979.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\six.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\test_pyecdsa.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\util.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\_version.py -> C:\Python27\Lib\site-packages\ecdsa
copying build\lib\ecdsa\__init__.py -> C:\Python27\Lib\site-packages\ecdsa
byte-compiling C:\Python27\Lib\site-packages\ecdsa\curves.py to curves.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\der.py to der.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\ecdsa.py to ecdsa.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\ellipticcurve.py to ellipticc
urve.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\keys.py to keys.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\numbertheory.py to numbertheo
ry.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\rfc6979.py to rfc6979.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\six.py to six.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\test_pyecdsa.py to test_pyecd
sa.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\util.py to util.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\_version.py to _version.pyc
byte-compiling C:\Python27\Lib\site-packages\ecdsa\__init__.py to __init__.pyc
running install_egg_info
Writing C:\Python27\Lib\site-packages\ecdsa-0.11-py2.7.egg-info
C:\Python27\ecdsa-0.11>
C:\Python27\ecdsa-0.11>
C:\Python27\ecdsa-0.11>

Leave a comment

re.match

>>> help (re.match)
Help on function match in module re:
match(pattern, string, flags=0)
Try to apply the pattern at the start of the string, returning
a match object, or None if no match was found.
>>>
>>>
>>>

re.match (‘what you want to find ‘, ‘from where you want to find’)

name_a=re.match

Leave a comment

easy way to find “Active” BGP peers

string = “””
dm2-arista7280-01#show int counters rates | nz
Port Name Intvl In Mbps % In Kpps Out Mbps % Out Kpps
dm2-arista7280-01#show ip bgp summary
BGP summary information for VRF default
Router identifier 10.236.2.82, local AS number 8076
Neighbor V AS MsgRcvd MsgSent InQ OutQ Up/Down State PfxRcd PfxAcc
10.236.0.96 4 8075 0 0 0 0 23:06:00 Active
10.236.0.98 4 8075 3065 2768 0 0 23:01:44 Estab 2 1
10.236.0.100 4 8075 0 0 0 0 23:06:00 Active
10.236.0.104 4 8075 0 0 0 0 23:06:00 Active
10.236.0.108 4 8075 0 0 0 0 23:06:00 Active
“””

for i in string.split (“\n”): ############ this is used to split the lines by “enter” operator
if i.find (‘Activ’) !=-1:#############==-1 means there is no matching in this line!
print i.find (‘Activ’)
print i

>>> ================================ RESTART ================================
>>>
65
10.236.0.96 4 8075 0 0 0 0 23:06:00 Active
65
10.236.0.100 4 8075 0 0 0 0 23:06:00 Active
65
10.236.0.104 4 8075 0 0 0 0 23:06:00 Active
65
10.236.0.108 4 8075 0 0 0 0 23:06:00 Active

string.split (‘ this can be a space, a coma, or an enter’)

if the operator is an enter, the string.split equal to string.splitlines()

for example,
string.split (‘\n’) equal to string.splitlines()

string.find (‘ xx’) == -1 means there is no find!
if it != -1, then it means there is a match!!

Leave a comment

string. find: find the offset of the string; string.replace(“a”,”b”)

>>> s = “this is a string”
>>>
>>> s.find (“is”)
2
>>> s.find (“tr”)
11
>>> s[11]
‘t’
>>> s.replace (“tr”, “TR”)
‘this is a sTRing’
>>> s
‘this is a string’
>>>

string.find is used to find the offset of a specific part_of_string
s.replace is only to replace the part , but does not change s

we can put the replace value to a name, to save the replacement:
>>> test=s.replace (“tr”, “TR”)
>>> test
‘this is a sTRing’
>>>

Leave a comment

Python String and list

we can put a string in a list:
for example:

>>> s = “this is a string”
>>>
>>>
>>> L= list(s)
>>>
>>> L
[‘t’, ‘h’, ‘i’, ‘s’, ‘ ‘, ‘i’, ‘s’, ‘ ‘, ‘a’, ‘ ‘, ‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’]
>>>
>>> ”.join (L)
‘this is a string’

list (string_name), can change a string to list,

“”.join (List_name ) can be used to convert a list to a string!

Leave a comment