Python scripting quick reference.

Following code snippets are discussed.  

Define a function and use ‘for’ loop in Python

Convert string to hexadecimal value.

Stip-off whitespaces from end.

Conversion of 12 bit hexadecimal values to 12 bit
 

LTE - 4G Wireless Technology

Digital fundamentals.

Interview Questions.

~\Documents\fullchip\python\mysite\webpages\templates\webpages\test.py.html
import sys
import os
import string

class file_op:
         def file_read(self, file):
             self.file_to_read = file
             self.rfile = self.file_to_read.readline()
             print self.rfile
             # To calculate length
             self.ln = len(self.rfile)
             print (self.ln)
             # Open file for Writing
             self.temp_write = open('temp_file.txt' , 'a')
             # Strip off white space on end
             self.rfile = string.strip(self.rfile)
             # Read 12 bit vectors as string from the text vectors file
             self.ci = 0
             self.idata = 0
             self.qdata = 0
             for self.ci in range(10, 11, 1):
                self.idata = self.rfile[self.ci] + self.rfile[self.ci+1] + self.rfile[self.ci+2]
                self.qdata = self.rfile[self.ci+5] + self.rfile[self.ci+6] + self.rfile[self.ci+7]
                print self.idata, self.qdata
             # Convert String to a Integer Value
             self.idata_f = int(self.idata, 16)
             self.qdata_f =  int(self.qdata, 16)

             # Convert hexadecimal value to signed integers
             if (self.idata_f > (4096/2 - 1)):
                self.idata_f = self.idata_f - 4096
             if (self.qdata_f > 4096/2 - 1):
                self.qdata_f = self.qdata_f - 4096

             self.iqdata = self.idata_f, self.qdata_f
             print self.iqdata

             # Write file
             self.temp_write.write(self.iqdata)

             # Close file
             self.temp_write.close()

             return

if len(sys.argv) == 1:
    r1 = file_op()
    r1.file_read(sys.stdin)
else:
    r1 = file_op()
    r1.file_read(open(sys.argv[1], 'r'))

Tutorials @fullchipdesign.com

Verilog Tutorial.

LTE Tutorial.

Memory Tutorial.

USAGE of the PROGRAM

From UNIX command prompt use:

>> python python_file1.py input_vectors.txt

Sample input vector text file

1AF00 F0 0E1 1F4E

Following list of programs are discussed in this sec:

¶ File Operations: 

  1. Open and read file from within the python program as a single string. Command line arguments lists as string with ‘sys.argv’.
  2. Use of  glob module in Python code . Read file using classes, global variables and functions. Program to open and write file.
  3. Specify files to be written from the command line. File writes using classes and global variables

¶ Program to read file, filter text, conversion to HEX, conversion from Hexadecimal to Signed Magnitude.S Steps for conditional statements, converting string to hexadecimalvalue, strip of white space at end, converting 12 bit hexadecimal values to 12 bit signed values.

¶ Advanced python code to cover UNIX shell commands, read lines from a text file, ‘sysargv’ command line operations, use of class and multiple functions. 

¶ Write a program to generate diamond pattern.

¶ Implement Python function calls to different classes.

¶ Python read binary files, use of struct.unpack.

¶ Python error’s with complete code examples:-

  1. TypeError: __init__() takes exactly 2 arguments (1 given),
  2. TypeError: 'str' object is not callable, TypeError: not all arguments converted during string formatting,
  3. AttributeError: ** instance has no attribute **

 

 

Hope you liked! this page. Don't forgot to access relevant previous and next sections with links below.