'''
Python version : 3.6
Python program to input a filename of python source and print a
cross
reference of the python names found within the source file.
'''
def get_python_names(line):
''' Get a set of python names found within a line
'''
found_names = set()
python_name = ""
quote = None
for c in line:
if quote:
if c ==
quote:
quote = None
continue
if c == '"' or c == "'":
quote = c
continue
if c == '#':
break
if c.isalpha() or c == '_' or
(len(python_name) > 0 and c.isdigit()):
python_name +=
c
elif len(python_name) > 0:
found_names.add(python_name)
python_name =
""
if len(python_name) > 0:
found_names.add(python_name)
return found_names
if __name__ == "__main__":
# create a dictionary, with keys be the names and
values be the list of line numbers where it is found
cross_reference_dict = {}
# input the filename
filename = input('Enter the filename: ')
# open the file in read mode
file = open(filename)
line_no = 1 # set line_no to start line
# read a line from file
line = file.readline()
# loop till the end of file
while line:
# get the names from the line
found_names =
get_python_names(line)
# loo pover the names and add it to
cross_reference_dict
for name in found_names:
# name present
in dictionary, append the line_no to the list
if name in
cross_reference_dict:
cross_reference_dict[name].append(line_no)
else: # name not
found in dictionary, create a new record with list containing the
line_no
cross_reference_dict[name] = [line_no]
line_no += 1 # increment the line
number
# read the next line
line = file.readline()
# get the keys from the dictionary
names = cross_reference_dict.keys()
names.sort() # sort the names in ascending order
# loop over the sorted names, to display the name and
the line numbers
for name in names:
# display the name
print('%-20s'%(name), end='')
# get the list of line
numbers
line_nums =
cross_reference_dict[name]
# loop over the line numbers, to
display them
for i in
range(len(line_nums)-1):
print('%d,
'%(line_nums[i]), end='')
print(line_nums[len(line_nums)-1])
#end of program
Code Screenshot:
Output:
Input file:
Output:
python 3 8.12 LAB: Python cross reference In addition to editors and compilers, a software developer...
Please mad in dev c++ software That is example. But it is wrong Write a simple checkbook balancing program Input will contain either the word "withdraw" or "deposit". The next line will contain the amount of the check or deposit. After reading each pair of lines, the program should compute and print the new balance. You can declare the variable to hold the running balance to be type float, and you can use the function atof (also in the standard...
In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to read from an input file, perform string manipulation on each line of the file, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward Provided output file: A single output file...
Python 3 Question: All I need is for someone to edit the program with comments that explains what the program is doing (for the entire program) def main(): developerInfo() #i left this part out retail = Retail_Item() test = Cash_Register() test.data(retail.get_item_number(), retail.get_description(), retail.get_unit_in_inventory(), retail.get_price()) answer = 'n' while answer == 'n' or answer == 'N': test.Menu() print() print() Number = int(input("Enter the menu number of the item " "you would like to purchase: ")) if Number == 8: test.show_items() else:...
anyString.substr(x, n) - Returns a copy of a substring. The substring is n characters long and begins at position x of anyString. Write a program that reads a string from the user containing a date in the format mm/dd/yyyy. You have to use above substring method to extract the various fields from the format. It should print the date in the form Month Date, Year. Validate: Exit the program with error message as “Invalid date format” if length of...
Questin 1 (CO 2) Which of the following is not a valid name in python? last_name lastName last name lname Question 2 (CO 3) How many times will “Hello World” be displayed after the following code executes? num=2 while num<12: print("Hello world") num+=2 2 12 5 4 Question 3 (CO 1) The following code contains an error, what type of error is it and what line number is it? 1 count=1 2 while count<4 3 print("count = ",...
python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...
Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...
Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...
use python IDEL Please highlight the answer Problem 1 Errors and Exceptions. There are two main types of errors: syntax errors and runtime errors. Syntax errors are detected by the Python interpreter before the program execution during the parsing stage (parsing is a process, during which the Python interpreter analyzes the grammatical structure of the program). Runtime errors are errors in a program that are not detected by the Python interpreter during its parsing stage. They are further divided into...
NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...