Python Coding - hqzhang/cloudtestbed GitHub Wiki

Python Types

Python vs groovy
1. List is the same
   list=[] vs list=[] ie, [ 'abc','xyz']
2. Length
   Len() vs size()
3. List appending
   append/Insert vs add
4. List removing
   Remove(val)/pop vs remove(idx)
5. For loop:
   Item in list vs item in list
6. List
   []List is a collection which is ordered and changeable. 
7. Tuple
   ()Tuple is a collection which is ordered and unchangeable.
8. Set
   {}Set is a collection which is unordered, unchangeable*, and unindexed.add() remove()
9. Data Mapping ie. { 'key':'abc','age': 'xyz'}
   {}Dictionary vs [:]   while {} is closure defination in groovy.
10. Mapping asignment is the same 
   [key]=val vs [key]=val
11. dict to tuple list:
    dictionary.items()
12. Groovy closure
   def greet = {  "Hello, $it"  }
   println greet("John") // Output: Hello, John

YAML File Parsing and Dump

#!/usr/bin/env python3
import sys
import yaml
import ruamel.yaml
from pathlib import Path
#conf = yaml.safe_load(Path('data.yml').read_text())
yaml_str = """\
steps:
- !<!entry>
  id: Entry-1
  actions: []
- !<!replybuttons>
  id: ReplyButtons-langcheck
  footer: ''
"""
with open("myfile.yml", "r") as myfile:
    mydic=yaml.safe_load(myfile)
    print(mydic)
    
myyaml = ruamel.yaml.YAML()
yaml_str=Path('myfile.yml').read_text()
data = myyaml.load(yaml_str)
data['locations'][0]['ip Addr']='myip8.8.8.8999'
#print('tag', data['steps'][1].tag.value)


with open("structure.yml", "w") as file:
        # yaml.dump need a dict and a file handler as parameter
        myyaml = ruamel.yaml.YAML()
        myyaml.indent(sequence=4, offset=2)
        myyaml.dump(data, file)

run command

import subprocess
def run_process(cmd, stdin=subprocess.PIPE, print_out=False, shell=True, stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT, universal_newlines=True, check=True):
    result=None
    try:
      result=subprocess.run(cmd, stdin=stdin, shell=shell, stdout=stdout,
                                 stderr=stderr, universal_newlines=universal_newlines, check=check)
    except subprocess.CalledProcessError as cpe:
        print(f"ERROR -- {cpe.output}",flush=True)
        raise cpe
    if print_out:
        print(result.stdout, flush=True)
  return result

cmd1 result=subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE
cmd2 result=run_process(cmd2, stdin=result.stdout)

some notes

A lambda function
a small anonymous function and can take any number of arguments, but can only have one expression.
x = lambda a, b : a * b
print(x(5, 6))

install package
python3 -m pip install requests for python3

__init__.py file
In Python the definition of package is very simple. Like Java the hierarchical structure and
the directory structure are the same. But you have to have __init__.py in a package. 
__init__.py can be empty or put __all__ = ['module_n1','module_n2', 'module_n3'] if use import *:

__init__() method
The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. 
The __init__ function is called every time an object is created from a class. 
The __init__ method lets the class initialize the object's attributes and serves no other purpose.
 It is only used within classes

#!/usr/bin/env python3

#"public relations" is an anagram of "crap built on lies."

str1="publicrelations"
str2="crapbuiltonlies"
def findAnagram(str1,str2):
     print(type(str1))
     str1=sorted(str1)
     str2=sorted(str2) 
     len1=len(str1)
     len2=len(str2)
     if len1 != len2:
         return False
     print(type(str1))
     for i, var in enumerate(str1):
         if var != str2[i]:
            return False
     return True
print(findAnagram(str1,str2))

def findDuplication(Array ):
  uni= []
  dup=[]
  for v in Array :
    print(v)
    if v not in uni:
       uni.append(v)
    else :
       dup.append(v)
  print (uni)
  return dup
input= [1,2,3, 4,3,2,1]
print( findDuplication(input) )
#PYthon Notes:
#Standard Data type
Numbers: int/long/float/complex
String: name = "John"
List[]: list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] #list=square bracket
Tuple():  tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  ) #round bracket const cannot be changed
for var in list/tuple:
    print var
Dictionary:
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}. #flower bracket for dict(map) 
dict = {}
dict['one'] = "This is one"
dict[2]     = "This is two"
delete dict['one']
#Basic type
counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string
#advanced type

#!/usr/bin/python
import time;
import logging
logger = logging.getLogger()
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(filename)s:%(lineno)s:%(funcName)2s(): %(message)s', level=logging.INFO)

def myfoo(a,b):
   print "Hello, Python!",a,b
class Person:
   def __init__(age,sex):
      self.age = name
      self.sex = sex 
class Employee(Person):
   'Common base class for all employees'
   empCount = 0
   def __init__(self, name, sex, salary):
      self.name = name
      self.sex =sex
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):
     print "Total Employee %d" % Employee.empCount

   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary

"This would create first object of Employee class"
emp1 = Employee("Zara","male" 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", "female",5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
myfoo(2,3)

var = 100
if  var  == 100 : 
   print "Value of expression is 100"
if var:
   print "if var: expect"
 #show list and loop
fruits = ['banana', 'apple']
tuples = ('banana', 'apple')
dicts = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
for fruit in fruits:        # Second Example
   print 'Current fruit :', fruit

 #show time
ticks = time.localtime(time.time())
print "ticks from 1970:", ticks
 #show input
str = raw_input("Enter your input: ");
print "Received input is : ", str

# Open a file
try:
    with open("foo.txt", "wbr+") as fo:
       print "Name of the file: ", fo.name
       fo.write( "Python is a great language.\nYeah its great!!\n");
    
except Exception as ex:
    print ex.message()
with open("foo.txt", "r+") as fo:
   str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close() # no need if use with

#python unit test

import unittest
import testedpackage
class baseClass(unittest.TestCase):
    env={}
class TestClassC(baseClass):
    def testOne1(self):
        print "#1111 testclassC:test one code"
        self.assertEqual('foo'.upper(), 'FOO')

    def testOne2(self):
        print "#2222 testclassC:test one code"
        self.assertTrue('FOO'.isupper())
       
   if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestClassC)
    results = unittest.TextTestRunner().run(suite)

#python error checking

find $@ -name "*.py" | xargs pylint '--rcfile=./jenkins/buildsystem/scripts/pylint.rc' '--msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}' --disable=E0611,E1101,E0401,E1136,C,W

#python format 
autopep8 --in-place --aggressive --aggressive file.py

#python get and set path
import inspect
# set module path
sys.path.append('/home/hongqi/pyenv/5.9.0/pypy/site-packages/')
import dialog
#get module path
print inspect.getfile(dialog)
#get module class/function
print dir(dialog)

#ssh python coding

#!/usr/bin/env python
import pexpect
import logging
logger = logging.getLogger()
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(filename)s:%(lineno)s:%(funcName)2s(): %(message)s', level=logging.DEBUG)
import sys
import time
# my ssh command line
def sshlogin(username,ip,password,main_menu,newpass="Admin123",port="22"):
    logger.info("ssh login with {}:{}@{}".format(password,username,ip))
    ssh_command="ssh "+username+"@"+str(ip)+" -p "+str(port)
    ssh_newkey = 'Are you sure you want to continue connecting'
    p=0;i=0;ii=0
    try:
        logger.info("exec {}".format(ssh_command))
        p=pexpect.spawn(ssh_command)
        p.logfile = open("/tmp/mylog", "w")
        i=p.expect([ssh_newkey,'Password:',main_menu],timeout=5)
        if i==2:
           logger.info("LOGIN SUCESS with SSHKEY")
        elif i==0:
           logger.info("send yesno")
           p.sendline('yes')
           ii=p.expect(['something','Password:'],timeout=5)
        if i==1 or ii==1:
           logger.info("send password and wait response...")
           p.sendline(password)
           ii=p.expect(['UNIX password:','Password:',main_menu],timeout=5)
           if ii==0:
               logger.debug("password need changed!!!")
               p.sendline(password)
               p.expect(['Enter new UNIX password:'],timeout=5)
               p.sendline(newpassword)
               p.expect(['Retype new UNIX password:'],timeout=5)
               p.sendline(newpassword)
               p.expect(main_menu,timeout=5)
               print "login successful with PASS"
           elif ii==1:
              logger.fatal("Wrong USER or PASS")
           elif ii==2:
              logger.info("Login main menu UCCESS with PASS")
    except:
        logger.fatal( "IP Address is wrong!!!")
        sys.exit(0)
ip="192.168.1.26"
username="coreos"
password="coreos"
main_menu="coreos@localhost"
sshlogin(username,ip,password,main_menu)

#python dialog

#!/home/hongqi/pyenv/5.9.0/bin/python
import sys
import dialog
import locale
from dialog import Dialog
import inspect

locale.setlocale(locale.LC_ALL, '')
d = Dialog(dialog="dialog")
d.set_background_title("My little program")

if d.yesno("Are you REALLY sure you want to see this?") == d.OK:
    d.msgbox("You have been warned...")
    hostname="myhost"
    domain="mydomain"
    display="mydiplay"
    d.form("Current Configuration", [("Hostname",1,0,hostname,1,15,64,64),
                                         ("Domain",  2,0,domain,2,15,64,64),
                                         ("NTP Server",3,0,display,3,15,64,64)],
                   form_height=6,timeout = 10,
                   no_cancel=False,ok_label="Save")
    code, tags = d.checklist("What sandwich toppings do you like?",
                             choices=[("Catsup", "",             False),
                                      ("Pesto", "",              False),
                                      ("Mayonnaise", "",         True),
                                      ("Sun-dried tomatoes", "", True)],
                             title="Do you prefer ham or spam?",
                             backtitle="And now, for something "
                             "completely different...")
    if code == d.OK:
        # 'tags' now contains a list of the toppings chosen by the user
        pass
else:
    code, tag = d.menu("OK, then you have two options:",
                       choices=[("(1)", "Leave this fascinating example"),
                                ("(2)", "Leave this fascinating example")])
    if code == d.OK:
        # 'tag' is now either "(1)" or "(2)"
        pass

#How to reverse sentence,easy!

sentence = "Hello World!"
#reverse char
print(sentence[::-1])
#reverse words
words = sentence.split()
revs=reversed(words)
#insert space to reversed sentence.
sentence_rev = " ".join(revs) 
print(sentence_rev)
⚠️ **GitHub.com Fallback** ⚠️