Examples of data type conversions in Python and script statistics server memory instances

Source: Internet
Author: User
Tags iterable server memory

counting the remaining memory of the system

In [1]: S1 = ' abc '

In [2]: Help (S1.startswith)

Help on built-in function StartsWith:

StartsWith (...)

S.startswith (prefix[, start[, end]), BOOL

Return True If S starts with the specified prefix, False otherwise.

With optional start, test S beginning on that position.

With optional end, stop comparing S on that position.

Prefix can also is a tuple of strings to try.

(END)

Cat/proc/meminfo


#!/usr/bin/python

With open ('/proc/meminfo ') as FD:

For line in FD:

If Line.startswith (' Memtotal: '):

Total = Line.split () [1]

Continue

If Line.startswith (' Memfree: '):

Free = Line.split () [1]

Break

Print "%.2f"% (int (free)/1024.0) + ' M '


Data type conversion calculation (Compute MAC address)

10 binary converted to 16 binary:

In [9]: Hex (10)

OUT[9]: ' 0xa '


16 binary converted to 10 binary:

In [8]: Int (' 0xa ', 16)

OUT[8]: 10

In [7]: Int (' AA ', 16)

OUT[7]: 170


A string of pure numbers is converted into 10 binary:

In [ten]: Int (' 10 ')

OUT[10]: 10


10 binary converted to string:

In [All]: STR (10)

OUT[11]: ' 10 '

Example:

Original server mac:02:42:0e:31:98:0b, write script to generate the next Nic Mac, automatically add 1.

#!/usr/bin/python

macaddr = ' 02:42:0e:31:98:0b '

Prefix_mac = macaddr[:-3]

Last_two = macaddr[-2:]

plus_one = Int (last_two,16) +1

If Plus_one in range (10):

New_last_two = Hex (Plus_one) [2:]

New_last_two = ' 0 ' + new_last_two

Else

New_last_two = Hex (Plus_one) [2:]

If Len (new_last_two) = = 1:

New_last_two = ' 0 ' +new_last_two

New_mac = Prefix_mac + ': ' + new_last_two

Print New_mac.upper ()

Data type conversions (lists and dictionaries convert each other)

View Help Join ()

Help on built-in function join:

Join (...)

S.join (iterable), string

Return a string which is the concatenation of the strings in the

Iterable. The separator between elements is S.

(END)


String-To-Lists: list (string)

In [approx]: a = list (' AAA ')

In [PNS]: type (a)

OUT[37]: List

In []: a

OUT[38]: [' A ', ' a ', ' a ']


List to string: '. Join (list)

In []: a

OUT[38]: [' A ', ' a ', ' a ']

in [39]:l = a

in []: l

OUT[18]: [' A ', ' a ', ' a ']

in [+]: '. Join (L)

OUT[19]: ' AAA '

in []: ', '. Join (L)

OUT[20]: ' A,a,a '

In [21]: '. '. Join (L)

OUT[21]: ' A.A.A '

In []: A= ' a '

In [All]: Help (A.join)


String tuples: Tuple (String)

In []: s

OUT[24]: [' A ', ' a ', ' a ']

in [+]: tuple (s)

OUT[26]: (' A ', ' a ', ' a ')


Tuple to string: '. Join (tuple)

In [SI]: type (a)

OUT[54]: Tuple

in [+]: a = str (a)

in [+]: A

OUT[56]: "(' A ', ' B ', ' C ', 111)"

In [the]: type (a)

OUT[57]: Str


Dictionary goto list:

in [+]: dic = {' A ': 1, ' B ': 2}

in [+]: dic

OUT[29]: {' A ': 1, ' B ': 2}

in [+]: Dic.items ()

OUT[30]: [(' A ', 1), (' B ', 2)]


List to Dictionary:

In [to]: L1 = Dic.items ()

in [+]: L1

OUT[32]: [(' A ', 1), (' B ', 2)]

In [All]: type (L1)

OUT[33]: List

In [the]: dict (L1)

OUT[34]: {' A ': 1, ' B ': 2}


Examples of data type conversions in Python and script statistics server memory instances

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.