0

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.

  1. 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.

  1. 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.

martineau
  • 119,623
  • 25
  • 170
  • 301
pow
  • 415
  • 3
  • 8
  • 18
  • 1
    Sounds to me like you need a `class Bike` or a number of `bike` dictionaries to hold all the attributes (bike, model, colour, price). – jDo Mar 25 '17 at 14:26
  • 1
    The [Pyro4 documentation](https://pythonhosted.org/Pyro4/intro.html#id1) says it support anything that can be serialized with `serpent`, `pickle`, or `dill`, so @jDo's advice sounds like the right plan of attack. – martineau Mar 25 '17 at 15:11
  • How would I then incorporate the new class of instances into in the warehouse? Please be explicit, I am very amateur at python @jDo – pow Mar 25 '17 at 15:24
  • @mystifier You probably want to write your `Bike` class in a way that enables you to change `self.contents = ["bike1", "bike2", "bike3", "bike4", "bike5"]` in the `Warehouse` initializer to something like `self.contents = [Bike(name="bike1"), Bike(name="bike2"), Bike(name="bike3"), Bike(name="bike4"), Bike(name="bike5")]` – jDo Mar 25 '17 at 15:42
  • Thing is, the bike names are gonna be dynamic since bikes can be both removed and added so i can't really explicitly name them like that. is there anyway to directly link the self.contents to all current instances of Bike? @jDo – pow Mar 25 '17 at 15:49
  • 1
    Define a `Bike` class whose instances have the desired attributes. i,e, `class Bike(object):`, `def __init__(self, name, model, colour, price)`, `self.name = name`, `self.model = model`, `self.colour = colour`, etc. Then create instances of it with the desired values and put them in the `Warehouse` instance's `self.contents` similar to what jDo says. – martineau Mar 25 '17 at 15:53
  • @mystifier Once you have a `Bike` class similar to what martineau is describing, you can add bikes to the warehouse by changing `warehouse.store(self.name, bike)` to `warehouse.store(self.name, Bike(name=bike))` (maybe you also need to supply values for `model`, `colour` and `price`, depending on your implementation) – jDo Mar 25 '17 at 16:00

0 Answers0