Tuesday, December 6, 2016

Know Python (Mostly Syntax)



  • Define function:        
    • Syntax: 
      • def <function_name>(<values_of_parameters>):    
      •        <statements>
      •        return <values>

  • Print: 
    • Script:
      • print "\n\n======================================================================================================================================================"
    • print is inbuilt method that accepts string  or variable, or both sometimes.
    • print msg_text //The simplest way to produce output is using the print statement where you can pass zero or more expressions separated by commas.
  • try:   // colon(: ) is used after many inbuilt keywords like try, finally, for loop,if, predefined functions….
  • for row in request_data:  // for loop syntax – keywords include ‘for’, ‘in’ and ‘:’  
  • request_data = csv.reader(req_file)  // csv was the inbuilt function imported and reader is the method
  • req_file = open('UK_Line_Inp.csv','rb') // Nothing related to open is imported not sure how open is working
  • import urllib, urllib2 // Open arbitrary resources by URL and extensible library for opening URLs
  • req = urllib2.Request(url, req_data, headers) // Same as above
  • rsp_response_code = response['response']['responseCode'] // ‘response’ is the object of load function. Here it means we are reading the value each element in json.
  • rsp_optimal_line = response['response']['line']['optimalLine'] // same as above
  • results_file_writer.writerow((row[0],row[1],row[2],row[3],rsp_optimal_line,'Pass' if row[3] == rsp_optimal_line else
  • except HTTPError as e:
  • except Exception as e:
  • except URLError as e:
  • results_file = open('UK_Line_API_Test_Results_'+timestamp +'.csv','wt')  //what is ‘wt’ mode?

file object = open(file_name [, access_mode][, buffering]) //without file keyword code is being written. 
  • -        results_file_writer.writerow((row[0],row[1],row[2],row[3],'Error Response','Fail',req_data,pretty_resp))
  • -        from urllib2 import Request, urlopen, URLError, HTTPError //To use the module bar, we can import it in two ways:

1.  import foo.bar
2.  from foo import bar
In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don't, because we import the module to our module's namespace.
-        headers = {
                                        'Content-Type':'application/json',
                                        'client_app_id':'PZN',
                                        'request_timestamp':'2016-09-06 13:53:59.030',
                                        'channel':'WEB',
                                        'request_id':'9999999802',
                                        'country_code':'GB',
                                        'client_platform':'ORCA'
                        }

  • -        import subprocess
  • -        Python is case sensitive (P and p are not same here)  -        “”(double quotes) and ‘’(single quote) are read same here

No comments:

Post a Comment