0

I have edited using some of your comments below. I am just looking to have a default value for a parameter where one is not provided. I still do not get any values when I run the following two lines. There are now default values for the variables that will be used if the class is called without data

newPatient = patient()

print(newPatient.firstname)


import os
from sikuli import *

class patient():

    firstname = ""
    middlename = ""
    Surname = ""
    title = ""
    ppsno = ""
    gender = ""
    testID = ""
    birthDate = ""
    address1 = ""
    address2 = ""
    address3 = ""
    address4 = ""
    town = ""
    county = ""
    country = ""
    email = ""
    mobile = ""

#Default Constructor for the class patient

#Use this to create a patient with more detailed information

    def __init__(
        self,
        testID,
        firstname = 'Sample',
        middlename = 'Mary',
        surname = 'Patient',
        gender = 'Female',
        birthDate = '03091959',
        title = 'Mrs',            
        ppsno = '7445213P',
        address1 = '100',
        address2 = 'Green Glade',
        address3 = 'Pleasant Way',
        address4 = 'Ballybehy',
        town = 'Abbyfeale',
        county = 'Limerick',
        country = 'Ireland',
        email = '[email protected]',
        mobile = '0870563229'):

        self.testID = testID

        self.firstname = firstname
        self.middlename = middlename
        self.surname = surname
        self.gender = gender
        self.birthDate = birthDate
        self.title = title        
        self.ppsno = ppsno
        self.address1 = address1
        self.address2 = address2
        self.address3 = address3
        self.address4 = address4
        self.town = town
        self.county = county
        self.country = country
        self.email = email
        self.mobile = mobile

    def getfirstname(self):
         return self.firstname

    class schemeDetails():
        cardNumber = ""
        scheme = ""
        cardNumber = ""
        month = ""
        year = ""
        setSchemeAsDefault = ""

    def __init__(
        self,
        scheme = None,
        cardNumber = None,
        month = None,
        year = None,
        setSchemeAsDefault = None ):

        if (scheme is None):
            scheme = "GM"
            self.scheme = scheme
        if (cardNumber is None):
            month = "1231456A"
            self.cardNumber = cardNumber
        if (month is None):
            month = "September"
            self.month = month
        if (year is None):
            year = "2015"
            self.year = year
        if (setSchemeAsDefault is None):
            setSchemeAsDefault = "true"
            self.setSchemeAsDefault = setSchemeAsDefault

    def getStuff(self):
        return self.stuff

#Inner class for creating basic dispenses
    class basicDispense():
        drug = ""
        packSize = ""
        dosageSystem = ""
        scheme = ""
        #Constructor for the class basicDispense
        def __init__(
            self,
            drug = None,
            packSize = None,
            dosageSystem = None,
            scheme = None):

            if (drug is None):
                drug = "ABBOTT THIN LANCET TYPE C GMS"
                self.drug = drug
            if (packSize is None):
                packSize = "28"
                self.packSize = packSize
            if (dosageSystem is None):
                dosageSystem = "MD"
                self.dosageSystem = dosageSystem
            if (scheme is None):
                scheme = "GM"
                self.scheme = scheme

            def getStuff(self):
                return self.stuff

        #Inner class of basicDispenses for printing Labels
        #Constructor for the class Labels
        class labels():
            def __init__(
                self,
                testID,
                printBagLabel = None,
                printDrugLabel = None):

                self.testID = testID
                if (printBagLabel is None):
                    printBagLabel = "false"
                    self.drug = drug
                if (printDrugLabel is None):
                    printDrugLabel = "false"
                    self.printDrugLabel = printDrugLabel
3
  • 1
    You are not using the class variables so you don't really need them (the stuff above the init) Commented Jul 22, 2014 at 15:10
  • 1
    Why do you use something like firstname = "Sample"followed by self.firstname = firstname. Just self.firstname = 'Sample is enough. On the other hand even this is strange in this context. Just use the solution from Martin Konecny's answer. Commented Jul 22, 2014 at 15:13
  • What are you trying to do? Create a patient object with some test data? Commented Jul 22, 2014 at 15:17

3 Answers 3

3

Your class attributes should be set if you aren't passing in an argument for that specific parameter. Show us some proof that this isn't the case - you are probably printing the class variable instead of the instance variable

p = patient()
p.firstname # should print the default firstname
patient.firstname # should print an empty string

On a side note, why don't you initialize the following way (it's much more compact, and easy to read):

def __init__(
    self,
    testID,
    firstname = "Sample",
    middlename = "Patient",
    surname = "Mary",
    gender = "Female",
    birthDate = "03091959",
    title = "Mrs",            
    ppsno = "7445213P",
    ...
):

EDIT:

OK it makes sense what else is wrong now - your indentation. Calling newPatient = patient() would fail if your indentation was correct (because you have one mandatory parameter, testID). Make sure you indent your entire __init__ function to the right by 4 spaces.

Sign up to request clarification or add additional context in comments.

5 Comments

You missed the instantiation of the class. Use p = patient(). Better still: Mention the naming conventions from PEP-8 and use p = Patient().
I tried setting the values in the constructor as you have done but it still printed blank when I tried the following newPatient = patient(), print(newPatient.firstname). I tried using a get function but that also didn't do the trick
what does newPatient.getfirstname() do? Code please.
OK it makes sense what is wrong now - your indentation. Calling newPatient = patient() would fail if your indentation was correct. Make sure you indent your __init__ function to the right be 4 spaces.
I made sure there were 4 spaces used for indentation :)
1

I dont get your use of the default values. They are obviously test data and should therefore not be in the class. What about something like this to construct a class with test data:

class Patient():
    def __init__(
                self,
                test_id,
                firstname,
                middlename):

        self.test_id = test_id
        self.firstname = firstname
        self.middlename = middlename

test_data = {"test_id":123, "firstname":"foo", "middlename":"bar"}
p = Patient(**test_data)

print(p.test_id)
print(p.firstname)
print(p.middlename)

Gives

>>>123
>>>foo
>>>bar

Comments

1

The whole class should look like this - (using the tips before)

import os
from sikuli import *

class patient():
    def __init__(
        self,
        testID,
        firstname = 'Sample',
        middlename = 'Mary',
        surname = 'Patient',
        gender = 'Female',
        birthDate = '03091959',
        title = 'Mrs',            
        ppsno = '7445213P',
        address1 = '100',
        address2 = 'Green Glade',
        address3 = 'Pleasant Way',
        address4 = 'Ballybehy',
        town = 'Abbyfeale',
        county = 'Limerick',
        country = 'Ireland',
        email = '[email protected]',
        mobile = '0870563229'):

        self.testID = testID

        self.firstname = firstname
        self.middlename = middlename
        self.surname = surname
        self.gender = gender
        self.birthDate = birthDate
        self.title = title        
        self.ppsno = ppsno
        self.address1 = address1
        self.address2 = address2
        self.address3 = address3
        self.address4 = address4
        self.town = town
        self.county = county
        self.country = country
        self.email = email
        self.mobile = mobile

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.