So I am trying to use class instances to create a remote database which is accessible as remote objects. But my issue is something simple, i.e. being able to store objects (with multiple attributes) as opposed to strings (which I have currently coded).
So I have a bike warehouse and different employees can access it so as to take or store bikes.
from __future__ import print_function
import Pyro4
@Pyro4.expose
class Warehouse(object):
def __init__(self):
self.contents = ["bike1", "bike2", "bike3", "bike4", "bike5"]
def list_contents(self):
return self.contents
def take(self, name, bike):
self.contents.remove(bike)
print("{0} took the {1}.".format(name, bike))
def store(self, name, bike):
self.contents.append(bike)
print("{0} stored the {1}.".format(name, bike))
def main():
warehouse = Warehouse()
Pyro4.Daemon.serveSimple(
{
warehouse: "example.warehouse"
},
ns=True)
if __name__ == "__main__":
main()
this is how the different employees access said bike warehouse
from __future__ import print_function
import sys
if sys.version_info < (3, 0):
input = raw_input
class Person(object):
def __init__(self, name):
self.name = name
def visit(self, warehouse):
print("This is {0}.".format(self.name))
self.deposit(warehouse)
self.retrieve(warehouse)
print("Thank you, come again!")
def deposit(self, warehouse):
print("The warehouse contains:", warehouse.list_contents())
bike = input("Type the bike you want to store (or empty): ").strip()
if bike:
warehouse.store(self.name, bike)
def retrieve(self, warehouse):
print("The warehouse contains:", warehouse.list_contents())
bike = input("Type the bike you want to take (or empty): ").strip()
if bike:
warehouse.take(self.name, bike)
and the actual access is executed with a few lines of code in a different script.
What I am trying to do is instead of having single string instances like bike1, bike2 etc. I want to have and want to input several attributes per instance in an object, namely:
"bike, model, colour, price"
Later these will be made searchable by price range or model.
- Then I want to be able to remove the bike (and all its attributes) from the warehouse by only specifying the bike name.
I have tried several things but have been struggling for days.