Created
June 8, 2020 21:09
-
-
Save tshabatyn/02558d2bdfc6d80752e1eb03a68acdb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import shelve | |
# Loading data fom dataname.db | |
def loadD(): | |
db = shelve.open('dataname') | |
dbItems = [] | |
for i in range(0, db['last_index'] + 1): | |
dbItems.append(db[str(i)]) | |
return dbItems | |
# Saving data to dataname.db | |
def saveD(D): | |
db = shelve.open('dataname') | |
db.clear() | |
for i, persone in enumerate(D): | |
db[str(i)] = persone | |
db['last_index'] = i | |
db.sync() | |
db.close() | |
# Displaying all the persons from dataname.db | |
def outputPersons(D): | |
for i, person in enumerate(D): | |
print(str(i) + ' => ' + ', '.join(map(str, person))) | |
return D | |
# adding new person to dataname.db | |
def addPerson(D): | |
a = str(input('Name: ')) | |
b = str(input('Surname: ')) | |
c = int(input('Age: ')) | |
d = str(input('City: ')) | |
D.append([a, b, c, d]) | |
return D | |
def removePerson(D): | |
outputPersons(D) | |
keyToDelete = int(input('Enter the index number to delete: ')) | |
try: | |
D.remove(D[keyToDelete]) | |
except: | |
print('Cannot find index ' + str(keyToDelete)) | |
return D | |
def changePersonData(D): | |
outputPersons(D) | |
changePersonWithIndex = int(input(f'Enter the person index to change (0-{len(D) - 1}): ')) | |
print('Changing ' + ', '.join(map(str, D[changePersonWithIndex]))) | |
print('Enter new person data') | |
a = str(input('Name: ')) | |
b = str(input('Surname: ')) | |
c = int(input('Age: ')) | |
d = str(input('City: ')) | |
try: | |
D[changePersonWithIndex] = [a, b, c, d] | |
except: | |
print('There are no index ' + str(changePersonWithIndex) + ' in the db.') | |
return D | |
def filterPersonBy(M): | |
b = str(input('Filter by name, surname, age, city? Your answer: ')) | |
if b == ('name' or 'surname' or 'city'): | |
a = str(input('enter name/surname/city : ')) | |
i = 0 | |
L = [] | |
while i < len(M): | |
for j in range(len(M[i])): | |
if a == M[i][j]: | |
L = L + M[i] | |
i += 1 | |
return L | |
if b == 'age': | |
a = str(input('Wanna enter a specific age? Your answer: ')) | |
if a == 'yes': | |
c = int(input('Enter the age of a person you are looking for: ')) | |
i = 0 | |
L = [] | |
while i < len(M): | |
for j in range(len(M[i])): | |
if c == M[i][j]: | |
L += M[i] | |
i += 1 | |
else: | |
d_1 = int(input('Enter an aproximated age: ')) | |
d_2 = str(input('Are the people younger or older than the entered age? ')) | |
if d_2 == 'yonger': | |
i = 0 | |
L = [] | |
while i < len(M): | |
for j in range(len(M[i])): | |
if type(M[i][j]) == int and M[i][j] < d_1: | |
L += M[i] | |
i += 1 | |
else: | |
i = 0 | |
L = [] | |
while i < len(M): | |
for j in range(len(M[i])): | |
if type(M[i][j]) == int and M[i][j] > d_1: | |
L += M[i] | |
i += 1 | |
return L | |
def sort(i): | |
return i[n] | |
def close(L): | |
sys.exit(D) | |
def recreate(Persons): | |
db = shelve.open('dataname') | |
db.clear() | |
for i, persone in enumerate(Persons): | |
db[str(i)] = persone | |
db['last_index'] = i | |
db.sync() | |
db.close() | |
def menu(): | |
print('input - Enter new data') | |
print('delete - Delete the entered data') | |
print('change - Change the entered data') | |
print('output - Output all stored persons') | |
print('Filter') | |
print('Sort all the data') | |
print('Close the database') | |
print(f'There are {len(D) - 1} persons in the DB!') | |
try: | |
D = loadD() | |
except: | |
D = [['Donald', 'Trump', 77, 'Washington'],['Angela', 'Merkel', 63, 'Berlin'],['Boris', 'Johnson', 49, 'London'], ['John', 'Kennedy', 45, 'Washington'], ['Frank', 'Abignail', 35, 'New York']] | |
recreate(D) | |
menu() | |
userChoice = str(input('What would you like to do? : ')) | |
if userChoice == 'output': | |
outputPersons(D) | |
if userChoice == 'input': | |
D = addPerson(D) | |
saveD(D) | |
print(D) | |
if userChoice == 'delete': | |
D = removePerson(D) | |
saveD(D) | |
print(D) | |
if userChoice == 'change': | |
D = changePersonData(D) | |
saveD(D) | |
print(D) | |
# if userChoice == 'filter': | |
# f = filterPersonBy(D) | |
# db = shelve.open('dataname') | |
# db.close() | |
# print(f) | |
# | |
# if userChoice == 'sort': | |
# n = input('Sort by 1.Name, 2.Surname, 3.Age, 4.City. Enter the number: ') | |
# n = int(n)-1 | |
# db = shelve.open('dataname') | |
# db.close() | |
# print(sorted(D, key = sort)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment