-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyBins.py
182 lines (140 loc) Β· 6.25 KB
/
PyBins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import yaml
# Creates a new container in the specified file by appending it to the existing data.
def create_new_container(container_dict, filename):
with open(filename, 'a') as f:
yaml.dump(container_dict, f)
# Adds an item to an existing container.
def add_item_to_container(container_name, item_name, quantity, filename="storage.yaml"):
try:
with open(filename, 'r') as f:
# Load the data from the file, or use an empty dictionary if the file is empty.
data = yaml.safe_load(f) or {}
except FileNotFoundError:
data = {}
if container_name not in data:
print(f" Container π¦ '{container_name}' does not exist!")
return False
# Initialize the container's data if it's empty.
if data[container_name] is None:
data[container_name] = {}
# Update the quantity of the item in the container.
data[container_name][item_name] = quantity
with open(filename, 'w') as f:
yaml.dump(data, f)
print(f"Added π {quantity} {item_name}(s) to π¦ {container_name}")
return True
# Lists all the containers in the specified file.
def list_containers(filename="storage.yaml"):
try:
with open(filename, 'r') as f:
# Load the data from the file, or use an empty dictionary if the file is empty.
data = yaml.safe_load(f) or {}
if not data:
print("No containers found! π")
return
# Print the names of all containers.
print("\nAvailable Containers π¦:")
print("--------------------")
for container_name in data.keys():
print(f"- {container_name}")
print("--------------------")
except FileNotFoundError:
print("No storage file found! No containers exist yet. π«")
# Lists all items in a specific container.
def list_items_in_container(container_name, filename="storage.yaml"):
try:
with open(filename, 'r') as f:
# Load the data from the file, or use an empty dictionary if the file is empty.
data = yaml.safe_load(f) or {}
if container_name not in data:
print(f" Container π¦ '{container_name}' does not exist!")
return
# Check if the container has any items.
if not data[container_name]:
print(f"The container π¦ '{container_name}' is empty. π")
return
print(f"\nItems in Container π¦ '{container_name}':")
print("-----------------------------")
for item_name, quantity in data[container_name].items():
print(f"- {item_name}: {quantity}")
print("-----------------------------")
except FileNotFoundError:
print("No storage file found! No containers exist yet. π«")
# Deletes a container from the specified file.
def delete_container(container_name, filename="storage.yaml"):
try:
with open(filename, 'r') as f:
# Load the data from the file, or use an empty dictionary if the file is empty.
data = yaml.safe_load(f) or {}
if container_name not in data:
print(f" Container π¦ '{container_name}' does not exist!")
return False
# Remove the container.
del data[container_name]
with open(filename, 'w') as f:
yaml.dump(data, f)
print(f" Container π¦ '{container_name}' has been deleted successfully! π")
return True
except FileNotFoundError:
print("No storage file found!")
return False
# Deletes an item from a specific container.
def delete_item_from_container(container_name, item_name, filename="storage.yaml"):
try:
with open(filename, 'r') as f:
# Load the data from the file, or use an empty dictionary if the file is empty.
data = yaml.safe_load(f) or {}
if container_name not in data:
print(f" Container π¦ '{container_name}' does not exist!")
return False
if data[container_name] is None or item_name not in data[container_name]:
print(f"Item '{item_name}' does not exist in container π¦ '{container_name}'!")
return False
# Remove the item from the container.
del data[container_name][item_name]
with open(filename, 'w') as f:
yaml.dump(data, f)
print(f"Item '{item_name}' has been deleted from '{container_name}' successfully!")
return True
except FileNotFoundError:
print("No storage file found!")
return False
# Main function to run the application.
def main():
while True:
print("\n1. π¦ Create new container")
print("2. β Add item to container")
print("3. β Delete item from container")
print("4. ποΈ List all containers")
print("5. π List all items in a container")
print("6. β Delete container")
print("7. π Exit")
choice = input("Choose an option (1-7): ")
if choice == "1":
container_name = input("Please enter the name for your new container π¦: ")
container = {container_name: {}}
create_new_container(container, "storage.yaml")
print(f" Container π¦ '{container_name}' created successfully! π")
elif choice == "2":
container_name = input("Enter container name π¦: ")
item_name = input("Enter item name: ")
quantity = int(input("Enter quantity: "))
add_item_to_container(container_name, item_name, quantity)
elif choice == "3":
container_name = input("Enter container name π¦: ")
item_name = input("Enter item name to delete: ")
delete_item_from_container(container_name, item_name)
elif choice == "4":
list_containers()
elif choice == "5":
container_name = input("Enter container name π¦: ")
list_items_in_container(container_name)
elif choice == "6":
container_name = input("Enter container name π¦ to delete: ")
delete_container(container_name)
elif choice == "7":
print("Goodbye! π")
break
else:
print("Invalid option! Please enter a number ranging from 1 to 7 to select an option. π€")
main()