-1
\$\begingroup\$

I have run the code and it's error free. As this is partial code which only defines one class of the many more to come, there is no output involved yet, therefore I am looking for testing whether the way I have used classes is logically correct without having to wait to complete the whole program and then find problems with it.

import os

#creating a Subdirectory for modules
here=os.path.dirname(os.path.realpath(__file__))
subdir=("TBG_module")
filename=("__init__.py")
filepath=os.path.join(here,subdir,filename)
os.mkdir(os.path.join(here,subdir))

#creating a new empty file in the module subdirectory
try:
   f=open(filepath,'w')
   f.close()
except IOError:
   print"Wrong path provided"


#Parent Class ITEMS
class Items():
      def __init__(self,name,Type,description,value):
          self.name=name
          self.description=description
          self.value=value
          self.Type=Type
      def __str__(self):
          return "{}\n===\n{}value: {}\n".format(self.name,  self.Type,self.description, self.value)

#Child class Key of Items
class key(Items):
      def __init__(self, type_):
      super().__init__(name="key", Type=self.type_ )

def  set_type_(self,type_,description):
     type_ = self.type_
     super().__init__(Type=self.type_ )

#Child class wooden_key of child class Key from parent class Items
class wooden_key(key):
     def set_type_(self,type_,description):
     wooden=type_
     super().set_type_(type_=self.type_ , description="This is a {} key.It opens class 1 door's.".format(str(self.type_)))

#Child class gold_key of child class Key from parent class Items
class gold_key(key):
      def set_type_(self,type_ , description):
      gold=type_
      super().set_type_(type_=self.type_ ,description="This is a {} key.It opens class 2 door's.".format(str(self.type_)))

#Child class silver_key of child class Key from parent class Items
class silver_key(key):
      def set_type_(self,type_ ,description):
      silver=type_
      super().set_type_(type_=self.type_ ,description="This is a {} key.It opens class 3 door's.".format(str(self.type_)))
\$\endgroup\$

closed as off-topic by syb0rg, t3chb0t, Marc-Andre, Mast, 200_success Jul 26 '16 at 17:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – syb0rg, t3chb0t, Marc-Andre, Mast, 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ While I believe that the code was error-free when you ran it, it is no longer error-free due to a flaw in this website. In short, it mangles whitespace. For posting code in general, and Python code in particular, it's important to ensure that you don't use tabs. \$\endgroup\$ – Peter Taylor Jul 26 '16 at 7:16
  • 2
    \$\begingroup\$ I think your code so far is already broken in quite a few places (apart from the indentation due to mixing tabs and spaces)... \$\endgroup\$ – Graipher Jul 26 '16 at 11:55
3
\$\begingroup\$

The description of the keys sounds like a good place to prevent some code redundancy:

super().set_type_(type_=self.type_ ,description="This is a {} key.It opens class 3 door's.".format(str(self.type_)))

The only difference between the keys is (apart from the type, which you already have as a variable) its class.

I was thinking of having this string already in the Key class.

However, I realized your code is quite broken here (fixed copy&paste indentation errors due to mixing of tabs and spaces, which you should avoid):

class key(Items):
    def __init__(self, type_):
        super().__init__(name="key", Type=self.type_ )

    def  set_type_(self,type_, description):
        type_ = self.type_
        super().__init__(Type=self.type_)

key.set_type sets type_ = self.type_, overriding the passed value, instead of self.type_ = type_. The description is also never passed on, so is never set either. In __init__, self.type_ is not yet set, so this will also raise an error.


You seem to be confused about assignments in general. In the following assignment, the value on the right is assigned to the variable on the left:

variable = value

Throughout your code you write value = variable:

gold = type_

Note that type_ = gold would still not work, because the variable called gold does not exist. You'd have to use type_ = "gold" or something similar.


For the Key class, the only thing distinguishing it from a general item seem to be two things:

  1. A key has a class, which determines which doors it can open
  2. A key has an almost generic description, passing in its type of key (wooden, gold, ...) and its class.

Therefore I would propose something like this:

class Key(Item):
    # This could be put into different derived classes,
    # if it becomes too large to maintain:
    classes = {'wooden': 1, 'silver': 2, 'gold': 3}
    values = {'wooden': 2, 'silver': 10, 'gold': 50}

    def __init__(self, name, _type):
        self.class = Key.classes[_type]
        description = "This is a {} key.It opens class {} doors.".format(_type, Key.classes[_type])
        super().__init__("Key", _type, description, self.class)

    def opens(self, door):
        return self.class == door.class

You should also specify default values for some parameters of the baseclass:

class Items():
      def __init__(self, name="", Type="", description="", value=0):

As of right now, the script can only be run once, because after that the file exists already. Use os.makedirs(path, exist_ok=True).


Instead of

try:
   f=open(filepath,'w')
   f.close()
except IOError:
   print"Wrong path provided"

Just use:

if not os.path.isfile(name):
    raise IOError("file {} does not exist".format(name))
\$\endgroup\$
0
\$\begingroup\$

Classes

classes in Python normally adhere to the CapWords convention (see pep8) (https://www.python.org/dev/peps/pep-0008/#class-names)

So

class wooden_key(key):

would become:

class WoodenKey(Key)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.