calling a variable from outside the function using python - python

I have this script that allow the user to enter the file name by an argument and then it updates the file version:
#!/usr/bin/env python
import os
import sys
class versionBumper:
def __init__(self):
self.version = None
self.SavedBefore = ""
self.SavedAfter = ""
def change_version(self, file_to_be_modded, packageVersion):
for line in file_to_be_modded:
if packageVersion in line:
print "VERSION FOUND: ", line
self.VersionNumber = line
elif self.VersionNumber is None:
self.SavedBefore += line
else:
self.SavedAfter += line
file_to_be_modded.close()
print "Version: ", self.VersionNumber
return self.VersionNumber
if __name__ == '__main__':
print "sys.argv[1:]:", sys.argv[0:]
versionBumper123 = versionBumper()
filename = sys.argv[1]
path = "/home/Desktop/Crate/Crate/" + filename + "/build/CMakeLists.txt"
if os.path.exists:
inputFile = open(path, 'r')
else:
print "no match found"
sys.exit()
print "Which version number to bump ?"
print "1) major"
print "2) minor."
print "3) patch."
Choose_version = raw_input("Choose version: ")
if Choose_version == "1":
version = versionBumper123.change_version(inputFile, "_MAJOR ")
elif Choose_version == "2":
version = versionBumper123.change_version(inputFile, "_MINOR ")
elif Choose_version == "3":
version = versionBumper123.change_version(inputFile, "_PATCH ")
else:
print "Invalid input. Exiting gracefully..."
sys.exit()
outputFile = open (path, 'w')
splitted_version_line_substrings = version.split('"')
Version_Name = splitted_version_line_substrings[0]
Version_Number = int(splitted_version_line_substrings[1]) + 1
parenthesis = splitted_version_line_substrings[2]
new_version = (str(Version_Name) + '"'
+ str(Version_Number) + '"'
+ str(parenthesis))
print "new_version: ", new_version
outputFile.write(str(versionBumper123.SavedBefore))
outputFile.write(str(new_version))
outputFile.write(str(versionBumper123.SavedAfter))
But I keep getting this error:
Traceback (most recent call last):
File "untitled.py", line 57, in <module>
splitted_version_line_substrings = version.split('"')
NameError: name 'version' is not defined.
I also tried to define version as a global variable but that also did not work, I can't really figure out how to call version from outside the function it is defined in.

Me thinks that you are dealing with a single instance of your class versionBumper
versionBumper123 = versionBumper()
so I guess that what you want to do with the statement
splitted_version_line_substrings = version.split('"')
should be expressed as
splitted_version_line_substrings = versionBumper123.version.split('"')
Just because you use a single instance of your class, imho you could write simpler code if you don't use a class.

Related

How do I clear positional arguments in my function(s) in Python?

There was a looping error with add, but now that I can add data to my txt file, there is this error using 'see' to list my data. Below is the erro:
TypeError:list_champs() takes 0 positional arguments but 1 was given.
I cannot see where I added a parameter unknowingly that doesn't need one in my current code.
# file = open("champs.txt", "w+")
FILENAME = "champs.txt"
def write_champs(champs):
with open(FILENAME, "w") as file:
for champ in champs:
file.write(champ + "\n")
def read_champs():
champs = []
with open(FILENAME) as file:
for line in file:
line = line.replace("\n", "")
champs.append(line)
return champs
def list_champs():
for i in range(len(champs)):
champ = champs[i]
print(str(i+1) + " - " + champs)
print()
def add_champ(champs):
champ = input("Champion: ")
#year = input("Season: ")
#champ = []
champs.append(champ)
#champ.append(year)
write_champs(champs)
print(champ + " was added.\n")
def display_menu():
print("Premier League Champions")
print("++++++++++++++++++++++++")
print("COMMANDS")
print("see - See the list of Champions")
print("add - Add a Champion to the list")
print("exit - Exit program")
print()
def main():
display_menu()
champs = read_champs()
while True:
command = input("Enter command: ")
if command == "see":
list_champs(champs)
elif command == "add":
add_champ(champs)
elif command == "exit":
print("Later!")
break
else:
print("Input not valid. Try again.")
if __name__ == "__main__":
main()
As always help is much appreciated!
You need to change your def list_champs to support arguments:
def list_champs(champs):
for i in range(len(champs)):
champ = champs[i]
print(str(i+1) + " - " + champs)
print()
function definition
def list_champs():
function call
list_champs(champs)
Do you want the function to take an argument, or not?
Fix one or the other, depending on your intended design.

No attribute present

I am quite new to python and I am getting an attribute error.
import FileHandeling as fh;
import os;
CounterFilePath = os.path.dirname(os.path.realpath(__file__))+"/counter.txt";
FilePath = os.path.dirname(os.path.realpath(__file__))+"/FileIO.txt";
class Employee:
def createEmployee(self):
numOfEmployees = int(input("Enter number of employees: "));
empDetails = [];
for i in range(numOfEmployees):
empFName, empLName, empSalary, empEmailId = raw_input("Enter employee first name: "), raw_input("Enter employee last name: "), raw_input("Enter employee salary: "), raw_input("Enter employee Email ID: ");
string = str(i)+" "+empFName+" "+empLName+" "+empSalary+" "+empEmailId+"\n";
empDetails.append(string);
with open(FilePath,"a+") as fo:
fo.seek(0);
fh.createFile(fo,numOfEmployees,empDetails,CounterFilePath);
def searchEmployee(self):
choice = int(input("Press:\n1 to search by First Name\n2 to search by Last Name\n3 to search by Salary\n4 to search by Email ID\n"));
print "Enter the",;
if(choice == 1):
print "First Name:",;
elif(choice == 2):
print "Last Name:",;
elif(choice == 3):
print "Salary:",;
elif(choice == 4):
print "Email ID:",;
searchStr = raw_input();
with open(FilePath,"r") as fo:
string = fh.readFile(fo,searchStr,choice-1);
while line in string:
print line;
def updateEmployee(self):
print "Leave the entries empty if you dont want to update that entry.";
lineNum = input("Enter the line number of the entry you want to update: ");
with open(FilePath,"r") as fo:
empFName, empLName, empSalary, empEmailId = raw_input("Enter employee first name: "), raw_input("Enter employee last name: "), raw_input("Enter employee salary: "), raw_input("Enter employee Email ID: ");
if(empFName == ""):
record = fh.readFile(fo,lineNum-1,0);
empDetails = record[0].split();
empFName = empDetails[1];
if(empLName == ""):
record = fh.readFile(fo,lineNum-1,0);
empDetails = record[0].split();
empLName = empDetails[2];
if(empSalary == ""):
record = fh.readFile(fo,lineNum-1,0);
empDetails = record[0].split();
empSalary = empDetails[3];
if(empEmailId == ""):
record = fh.readFile(fo,lineNum-1,0);
empDetails = record[0].split();
empEmailId = empDetails[4];
updateStr = str(lineNum-1)+" "+empFName+" "+empLName+" "+empSalary+" "+empEmailId+"\n";
fh.updateRecord(fo,FilePath,updateStr,lineNum-1);
def deleteEmployee(self):
lineNum = input("Enter the line number of the entry you want to delete: ");
with open(FilePath,"r") as fo:
fh.deleteRecord(fo,FilePath,lineNum-1);
def main(self):
goOn = True;
employee = Employee();
while goOn:
choice = input("Press:\n1 to enter a new employee\n2 to search employee\n3 to update employee\n4 to delete employee\n0 to exit\n");
if(choice == 1):
employee.createEmployee();
elif(choice == 2):
employee.searchEmployee();
elif(choice == 3):
employee.updateEmployee();
elif(choice == 4):
employee.deleteEmployee();
elif(choice == 0):
goOn = False;
else:
print "Wrong Choice!!!";
emp = Employee();
emp.main();
Here I am importing this class:
class FileHandeling:
def createFile(fo,numOfRecords,data,counterFile):
#Getting the value of counter
frc = open(counterFile,"r");
counter = int(frc.read());
frc.close();
#Taking input and writing to the file
for i in range(counter,numOfRecords+counter):
string = str(i)+data[i];
fo.write(string);
counter += 1;
#Writing back to counter the updated value.
fwc = open(counterFile,"w");
fwc.write(str(counter)+"\n");
fwc.close();
def readFile(fo,searchStr,criteria):
line = fo.readline();
string = [];
while line:
entries = line.split();
if(searchStr == entries[criteria]):
string.append(line);
line = fo.readline();
return string;
def printFile(fo):
fo.seek(0);
lines = fo.readlines();
print "The File: "
for line in lines:
print line;
def updateRecord(fo,fileLoc,updateStr,lineNum):
#Replacing the given record with he updated record and writing back to file
lines = fo.readlines();
fwu = open(fileLoc, "w");
lines[lineNum]= updateStr;
for line in lines:
fwu.write(line);
fwu.close();
def deleteRecord(fo,fileLoc,lineNum):
#Deleting the record
lines = fo.readlines();
fwu = open(fileLoc, "w");
lines.pop(lineNum);
#Correcting the Employee Ids and Writing Back to File
for line in lines:
entry1, entry2, entry3, entry4, entry5 = line.split();
entry1 = str(lines.index(line));
line = entry1+" "+entry2+" "+entry3+" "+entry4+" "+entry5+"\n";
fwu.write(line);
fwu.close();
#Reducing Counter value
frc = open(counterFile,"r");
counter = int(frc.read());
frc.close();
fwc = open(counterFile,"w");
fwc.write(str(counter-1)+"\n");
fwc.close();
In this code I am trying to replicate a database with the help of file but my code gives error saying that 'module' object has no attribute 'createFile'. I also tried creating packages and doing like in java but then it started saying that ImportError: No module named src.fileManipulation. they were just my folders in which I was working and wanted them as packages so I put an __init__.py in them and the tutorials said that this will help in making packages but that didn't happen and since both my files were in same directory I imported it directly but now it gives attribute error and I don't know what that means. Please Help.
I have executed the code in Python default IDLE after correcting some print statements. I am using Python3 so used print (" "). And the result was an endless loop of
Wrong Choice!!!
Press:
1 to enter a new employee
2 to search employee
3 to update employee
4 to delete employee
0 to exit
My kind requtest to you is to revise Python once again.

Using str.replace gives me a TypeError: 'str' object cannot be interpreted as an integer

I have a homework about making a phone book. And I did a code.
import sys
print("Type 'help' to learn commands ")
command = ("show_list, add_person, delete_person, search_person, exit")
command1 = ()
list1 = {}
while True:
command1 = input("Command: ")
saved = open("PhoneBook.txt", "a")
if command1 == "help":
print(command)
print()
elif command1 == "show_list":
saved = open("PhoneBook.txt", "r")
print(saved.read())
saved.close()
elif command1 == "add_person":
name = str(input("Name: "))
number = int(input("Number: "))
list1[name] = number
saved = open("PhoneBook.txt", "w")
saved.write("\n-------------")
saved.write(str(name))
saved.write(":")
saved.write(str(number))
saved.write("-------------\n")
saved.close()
elif command1 == "search_person":
search = open("PhoneBook.txt", "r")
search1 = input("Name:")
for line in search:
if search1 in line:
print("\n")
print(search)
elif command1 == "delete_person":
del0 = open("PhoneBook.txt", "r+")
del1 = str(input("Name: "))
for line in del0:
if del1 in line:
del2 = line.replace(" ", " ", " ")
del0.write(silinecek3)
del0.close()
elif command1 == "exit":
sys.exit()`
And everything is good (I think), except deleting a name. Because when I try to delete a name, it gives me an output:
Traceback (most recent call last):
File "C:/Users/oyuni/Desktop/ödeving.py", line 47, in <module>
del2 = line.replace(" ", " ", " ")
TypeError: 'str' object cannot be interpreted as an integer
And I don't know what to do. Can anyone help me to fix the code?
str.replace() can take either 2 or 3 arguments, but that 3rd argument must be an integer (it limits the number of replacements that take place).
You passed in a string as the third argument:
line.replace(" ", " ", " ")
Pass in just two strings, and make those different strings if you actually want to replace anything:
line.replace(" ", "")
However, this won't delete the name. The rest of that block of code is going to give you more problems:
You didn't define the name silinecek3
You can't safely read and write to the same file, at the same time. Read all of the file into memory first, or write to a new file that then is moved back into place.
You want to remove names entirely; do so by writing out all names you keep (so filter out the lines, write each one that doesn't have the name).

Python - Random baby name generator issues - (duplicating input, calling variables)

I've been looking at this all afternoon and can't figure out why the gender input is repeating itself despite only appearing to be called once. It's not part of a loop that I can see either.
I've tried adding variables to act as a counter and tried using an if statement to only run the input if the counter variable is less than 1, but can't figure it out.
Edit: Thanks to the great feedback here, I found out that get_full_name was causing the duplicating gender input in get_first_name - but now I'm running into issues when trying to output the randomly generated first & middle names.
I figured setting the setFirst, setMiddle and setLast variables as globals, but then I get a NameError. I also tried creating a new function to display them, but that wasn't working either. I tried adding "self." (without the quotes) either directly in the function() or one of the indents beneath it.
I'll display the error first, then the full code.
Error:
Traceback (most recent call last):
File "init.py", line 100, in
main()
File "init.py", line 92, in main
print displayName(setFirst, setMiddle, setLast)
NameError: global name 'setFirst' is not defined
I also get name errors trying to concatenate setFirst, setMiddle and setLast into another variable for the full name.
Here's the code:
from os.path import abspath, join, dirname
import random
full_path = lambda filename: abspath(join(dirname(__file__), filename))
FILES = {
'first:male': full_path('dist.male.first'),
'first:female': full_path('dist.female.first'),
'last': full_path('dist.all.last'),
}
def get_name(filename):
selected = random.random() * 90
with open(filename) as name_file:
for line in name_file:
name, _, cummulative, _ = line.split()
if float(cummulative) > selected:
return name
def get_first_name(gender=None):
global determine
global setFirst
print ("First name... Enter 1 for Male, 2 for Female or 3 to be surprised! ")
determine = input()
if determine == 1:
gender = 'male'
if determine == 2:
gender = 'female'
if determine == 3:
print ("You want to be surprised!")
gender = random.choice(('male', 'female'))
return get_name(FILES['first:%s' % gender]).capitalize()
setFirst = get_first_name()
print setFirst + " "
def get_middle_name(gender=None):
global setMiddle
if determine == 1:
gender = 'male'
if determine == 2:
gender = 'female'
if determine == 3:
gender = random.choice(('male', 'female'))
return get_name(FILES['first:%s' % gender]).capitalize()
setMiddle = get_middle_name()
print setMiddle + " "
def get_last_name():
global setLast
#We will implicitly pass a Last Name until other issues are fixed
return “Smith”
setLast = get_last_name()
print setLast
def get_full_name(gender=None):
return u"%s %s %s" % (get_first_name(gender), get_middle_name(gender), get_last_name())
#def displayName(setFirst, setMiddle, setLast):
# print setFirst + " " + setMiddle + " " + setLast
def main():
#print u"%s %s %s" % (setFirst, setMiddle, setLast)
#print displayName(setFirst, setMiddle, setLast)
f = open('output', 'a') #append output to filename output
f.write(get_full_name() + '\n') #and add a line break after each run
f.close()
if __name__ == "__main__":
main()
Even if I try passing the variables to main() like:
def main(setFirst, setMiddle, setLast):
It still gives the NameError about not being defined. What am I doing wrong?
I added this right under "import random", but now I'm getting some rogue "None" displays - which leads me to believe there is a leak in the code somewhere. Thoughts?
setFirst = None
setMiddle = None
setLast = None
Here is the function I created to try to track it:
def displayName(setFirst, setMiddle, setLast):
if setFirst == None:
print ("Random Baby Name Generator")
else:
print setFirst
print setMiddle
print setLast
if setMiddle == None:
print ("Double check the middle name variable.")
if setLast == None:
print ("Double check the last name variable.")
You are calling get_full_name() twice, you need to save the results:
def main():
full_name = get_full_name()
print(full_name)
f = open('output', 'a') #append output to filename output
f.write(full_name + '\n') #and add a line break after each run
f.close()
You also have a few indentation issues as well, plus your use of globals is a bit inefficient. Ideally, functions should do one - and only one - task; this makes them easier to debug.
Try this different version of your code:
from os.path import abspath, join, dirname
import random
full_path = lambda filename: abspath(join(dirname(__file__), filename))
FILES = {
'first:male': full_path('dist.male.first'),
'first:female': full_path('dist.female.first'),
'last': full_path('dist.all.last'),
}
GENDER_MAP = {'1': 'male', '2': 'female'}
def get_gender():
result = input('Select a gender: 1 for Male, 2 for Female or 3 to be surprised')
if result not in ('1', '2', '3'):
print('{} is not a valid choice, please try again'.format(result))
return get_gender()
if result == '3':
return random.choice(('1', '2'))
return result
def get_name(filename):
selected = random.random() * 90
with open(filename) as name_file:
for line in name_file:
name, _, cummulative, _ = line.split()
if float(cummulative) > selected:
return name
def get_name_from_file(name_type='first', gender='male'):
if name_type in ('first','middle',):
name = get_name(FILES['{}:{}'.format(name_type, gender)]).capitalize()
else:
name = get_name(FILES['last']).capitalize()
return name
def get_full_name():
gender = get_gender()
gender_file = GENDER_MAP.get(gender, '')
first_name = get_name_from_file('first', gender_file)
middle_name = get_name_from_file('middle', gender_file)
last_name = get_name_from_file('last')
return '{} {} {}'.format(first_name, middle_name, last_name)
if __name__ == '__main__':
name = get_full_name()
print(full_name)
with open('output', 'a') as f:
f.write('{}\n'.format(full_name))
print('Done')
get_full_name is being called twice, probably, and this result in other functions referenced by get_full_name being called twice as well. Unfortunately this means double input and confusing the user.

python unboundLocalError why

import os
def createFile():
if os.path.exists("highscores.txt") == False:
myFile = open("highscores.txt","w")
myFile.close()
def inputInt():
number = input(str("please input your score "))
try:
return int(number)
except:
print ("this is not an acceptable input, please try again")
inputInt()
def addScore():
name = input("Please enter the name you wish to add")
score = inputInt()
messages = "thank you"
'''open the highscores line and read in all the lines to a list called scorelist.
then close the file'''
scoresFile = open("highscores.txt","r")
scoresList = scoresFile.readlines()
scoresFile.close()
#check each line in the scoresList list
for i in range(0,len(scoresList) ):
#check to see if the name is in the line
if name in scoresList[i]:
#if it is then strip the name from the text. this should leave theb score
tempscore = scoresList[i].replace(name,"")
#if the score is higher then add it to the list
if int(tempscore)<score:
message = "Score updated"
scoresList[i] = (name + str(score))
#write the scores back to the file
scoresFile = open("highscores.txt","w")
for line in scoresList:
scoresFile.write(line + "\n")
scoresFile.close()
#no need to continue so break
break
else:
#message as score too low
message= "score too low. not updated"
if message("Score updated"):
message = "New score added"
scoresFile = open("highscores.txt","a")
scoresFile.write(name + str(score) + "\n")
scoresFile.close()
print (message)
addScore()
Above is the code. The error shown is:
Traceback (most recent call last):
File "E:\Computer Science\python code\highScores\highscores1.py", line 66, in <module>
addScore()
File "E:\Computer Science\python code\highScores\highscores1.py", line 60, in addScore
File "E:\Computer Science\python code\highScores\highscores1.py", line 60, in addScore
if message("Score updated"):
UnboundLocalError: local variable 'message' referenced before assignment
(1) You are referring to message which is undefined when no names are found in scoresList[i]. Put a
message = ""
line before your for loop. I don't know your actual intent, so this will make the error go away but check if the logic is still correct.
(2) You are doing the comparison incorrectly. You should write
if message == "Score updated":
instead of
if message("Score updated"):

Categories