Python scripting
quick reference.
Following code snippets are discussed.
Define a function and use ‘for’
loop in Python
Convert string to hexadecimal value.
Stip-
hexadecimal
values to 12 bitscripting
quick reference.Following code snippets are discussed.
Define a function and use ‘for’
loop in Python
Convert string to hexadecimal value.
Stip-
hexadecimal
values to 12 bitimport 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'))
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
programs
are discussed in this sec:¶ File Operations:
¶ 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:-
Hope you liked! this page. Don't forgot to access relevant previous and next sections
with links below.