I wanted to make sure that hotel_name
doesn't exist in Hotel.hotels
list.
It seems that when I start feeding every time for loop look in an empty list.
Note if I am not using for loop and only do
Hotel.hotels.append([number,hotel_name,city,total_number,empty_rooms])
it prints hotels list
[[1, 'Crown Plaza', 'alex', 20, 2], [1, 'Crown Plaza', 'alex', 20, 2], [2, 'Radisson Blu', 'cairo', 24, 22], [3, 'Paradise Inn', 'dubai', 390, 200], [4, 'Four Seasons', 'alex', 1000, 400], [5, 'Address', 'dubai', 500, 200], [6, 'Fairmont', 'dubai', 1000, 100], [7, 'Rotana', 'dubai', 5000, 300]]
[Finished in 0.1s]
This is the file
class Hotel():
"""""""""
this is hotel class file
"""
hotels = []
def __init__(self,number,hotel_name,city,total_number,empty_rooms):
self.number = number
self.hotel_name = hotel_name
self.city = city
self.total_number = total_number
self.empty_rooms = empty_rooms
for i in Hotel.hotels:
if self.hotel_name not in i:
Hotel.hotels.append([number,hotel_name,city,total_number,empty_rooms])
# else:
# print "Hotel already exists!"
feed_dataBase_with_hotel = Hotel(1,"Crown Plaza","alex",20,2)
feed_dataBase_with_hotel = Hotel(1,"Crown Plaza","alex",20,2)# cull repeated
feed_dataBase_with_hotel = Hotel(2,"Radisson Blu","cairo",24,22)
feed_dataBase_with_hotel = Hotel(3,"Paradise Inn","dubai",390,200)
feed_dataBase_with_hotel = Hotel(4,"Four Seasons","alex",1000,400)
feed_dataBase_with_hotel = Hotel(5,"Address","dubai",500,200)
feed_dataBase_with_hotel = Hotel(6,"Fairmont","dubai",1000,100)
feed_dataBase_with_hotel = Hotel(7,"Rotana","dubai",5000,300)
print Hotel.hotels
Many Thanks to Patrick for his answer Update What if i want to build a list from the dict, as i want to access empty-rooms and change its value with another class
class Hotel():
"""
this is hotel class file
"""
hotels = {}
hotelList = []
def __init__(self,number,hotel_name,city,total_number,empty_rooms):
self.number = number
self.hotel_name = hotel_name
self.city = city
self.total_number = total_number
self.empty_rooms = empty_rooms
# edited: no for needed
if self.hotel_name in Hotel.hotels:
print('Hotel {} Already exists!'.format(self.hotel_name))
return # or raise ValueError & handle it
Hotel.hotels[self.hotel_name] = self
tempList = Hotel.hotels.items()
for i in tempList:
x = Hotel.hotels.items()[i][1]
Hotel.hotelList.append(x)
Update
another class for reservation will going to use the instance variable hotel_name which we are using in hotel class
from hotel import Hotel
from customer import Customer
from notification import Notification
class Reservation():
reservations =[]
def reserve_room(self,hotel_name, customer_name):
x = Hotel.hotels.values()
for i in x:
if Hotel.hotel_name in i:
Reservation.reservations.append([hotel_name,customer_name])
i[4] -=1
AttributeError: class Hotel has no attribute 'hotel_name'
Update from Understanding getitem method using
def __getitem__(self, hotel_name):
return self.hotel_name
issue solved!
Special Thanks to Patrick