Im a bit of a beginner when it comes to classes.
I have a class defined as follows (simplified for purposes of this post)
class worksheet:
def __init__(self, filename):
self.filename = (filename).strip().replace(" ","")
def idMaker(self):
number = '{:05d}'.format(random.randrange(1,99999))
sheetId = self.filename+str(number)
I want to be able to get the 'sheetID' for each instance by saying something like the following (again, this might be completely incorrect):
newSheet = worksheet('testsheet')
id = newSheet.sheetID
This of course does not work, but I am not sure what I need to do to make it work.
I want to make sure the ID stays constant and doesnt recreate itself with new random numbers.
Thank you in advance
Just generate and assign the id in __init__. In general, as a user of the class, you don't want to care about generating the id yourself. As far as you're concerned, instantiating Worksheet gives you a complete, usable object.
import random
class Worksheet(object):
def __init__(self, filename):
self.filename = filename.strip().replace(' ','')
number = '{:05d}'.format(random.randrange(1,99999))
self.sheet_id = self.filename + str(number)
sheet = Worksheet(' some filename with spaces ')
print(sheet.filename)
print(sheet.sheet_id)
will output
somefilenamewithspaces
somefilenamewithspaces68237
The sheetId variable lives within that class's idMaker method, so you cannot access it with a dot operator. If you are trying to create custom IDs for instances of your class, I would recommend doing that in the class constructor __init__ method so it gets assigned on object creation. Maybe consider the following:
class worksheet:
def __init__(self, filename):
self.filename = (filename).strip().replace(" ","")
number = '{:05d}'.format(random.randrange(1,99999))
self.sheetID = self.filename+str(number)
Related
Here is an example code of what I am trying to do. My end goal is creating a JSON object for class real estate agent, but I am struggling because I don't know what to do about self.housesSold.
class realEstateAgent:
def __init__(self, name, salary, houses):
self.name = name
self.sal = salary
self.housesSold = #a list of **HOUSE** objects
class houseSold:
def __init__(openPrice, closePrice, closeDate):
self.openPrice = openPrice
self.closePrice = closePrice
self.closeDate = closeDate
You mention that housesSold is a list. In your last comment, it appears that it's not the case and it is actually an object.
However, in both cases, printing the object (or the list of objects) will not get you anywhere unless you edit the __str__ or __repr__ method of the corresponding class. Instead you should try to get the attributes of that object (because you care mainly about them).
For example if housesSold is an object, then try this:
housesSold = data.housesSold
print(housesSold.openPrice) #get the price
If it's a list, then try this :
housesSold = data.housesSold
print(housesSold[0].openPrice) #get the price of the first house
First of all, excuse my ignorance if this is a fairly easy question. What I would like to achieve is to create an attribute for every instance of the class (i.e. filepath), change it for an instance (i.e. in the first case, where I change the value of filepath for the a instance, but if I create a new instance, e.g. b I would like to keep the original filepath value.
filepath = '/path/to/original/file'
class A(object):
#classmethod
def _set_path(cls, filepath):
cls.filepath = filepath
return cls.filepath
def __init__(self, name):
self.name = name
A._set_path(filepath) # Set filepath for all instances: /path/to/original/file
a = A("Alice")
print(a.filepath)
a._set_path("/path/to/another/file") # Set filepath for instance a, but also for every new instance. This is what needs to be corrected.
print(a.filepath)
b = A("Bob")
print(b.filepath) # I would like to keep /path/to/original/file
Is there a way to improve this code and/or have a smart design pattern for this case?
Please, correct me, if I did not understand what you're looking for correctly and I can adjust the answer accordingly, but from what I got, you're looking for a class and instance attributes and distinction between them:
class A:
filepath = None
def __init__(self, name):
self.name = name
A.filepath = "/path/to/original/file"
a = A("Alice")
print(a.filepath)
a.filepath = "/path/to/another/file"
print(a.filepath)
b = A("Bob")
print(b.filepath)
Defining class A (note: in python 3 all classes are new-style which I presume is what inheritance from object was meant to be for as a hold out of python 2 habits) we define a class attribute filepath. This is strictly speaking not necessary, but if this is intended part of the interface... You could of course also specify the first initial default value directly in the class definition.
Then we assign our first value '/path/to/original/file' to it. At this point we create and instance a of class A and when we access its filepath attribute, we get value of the class attribute. Then we assign a different value to an instance attribute (a.filepath) and accessing it again we get its value back, while we have not modified class attribute A.filepath which is also what we see for newly created instance b.
Be ware though, mixing assignments to both class and instance attribute (of the same name could cause confusion and possibly unintended behavior. Consider this:
A.filepath = "/path/to/original/file"
a = A("Alice")
a.filepath = "/path/to/another/file"
b = A("Bob") # b.filepath is "/path/to/original/file"
A.filepath = "/third/file"
c = A("Carl")
Now accessing a.filepath yields "/path/to/another/file", but for both b.filepath and c.filepath are "/third/file" which may or may not be what we wanted esp. for b.filepath to be the case.
Hence for similar use case I would prefer something like:
class A:
default_filepath = "/path/to/original/file"
def __init__(self, name):
self.filepath = self.default_filepath
self.name = name
a = A("Alice")
a.filepath = "/path/to/another/file"
b = A("Bob")
A.default_filepath = "/third/file"
c = A("Carl")
Class has a default_filepath attributed which is used to assign to each instances self.filepath attribute. That should help reduce risk of mistakes. In the above example these:
a.filepath
b.filepath
c.filepath
Now have values of:
/path/to/another/file
/path/to/original/file
/third/file
I have the following problem and I need advice on how to solve it the best technically in Python. As I am new to programming I would like to have some advice.
So I will have the following object and they should store something. Here is an example:
object 1: cash dividends (they will have the following properties)
exdate (will store a list of dates)
recorddate (will store a list of dates)
paydate (will store a list of dates)
ISIN (will store a list of text)
object 2: stocksplits (they will have the following prpoerties)
stockplitratio (will be some ration)
exdate(list of dates)
...
I have tried to solve it like this:
class cashDividends(object):
def __init__(self, _gross,_net,_ISIN, _paydate, _exdate, _recorddate, _frequency, _type, _announceddate, _currency):
self.gross = _gross
self.net = _net
self.ISIN = _ISIN
self.paydate = _paydate
self.exdate = _exdate
self.recorddate = _recorddate
self.frequency = _frequency
self.type = _type
self.announceddate = _announceddate
self.currency = _currency
So if I have this I would have to create another class named stockplits and then define an __init__ function again.
However is there a way where I can have one class like "Corporate Actions" and then have stock splits and cashdividends in there ?
Sure you can! In python you can pass classes to other classes.
Here a simple example:
class A():
def __init__(self):
self.x = 0
class B():
def __init__(self):
self.x = 1
class Container():
def __init__(self, objects):
self.x = [obj.x for obj in objects]
a = A()
b = B()
c = Container([a,b])
c.x
[0,1]
If I understood correctly what you want is an object that has other objects from a class you created as property?
class CorporateActions(object):
def __init__(self, aCashDividend, aStockSplit):
self.cashDividend = aCashDividend
self.stockSplit = aStockSplit
myCashDividends = CashDividends(...) #corresponding parameters here
myStockSplit = StockSplit(...)
myCorporateActions = CorporateActions(myCashDividends, myStockSplit)
Strictly speaking this answer isn't an answer for the final question. However, it is a way to make your life slightly easier.
Consider creating a sort-of template class (I'm using this term loosely; there's no such thing in Python) that does the __init__ work for you. Like this:
class KwargAttrs():
def __init__(self, **kwargs):
for k,v in kwargs.items():
setattr(self, k, v)
def _update(self, **kwargs):
args_dict = {k:(kwargs[k] if k in kwargs else self.__dict__[k]) for k in self.__dict__}
self.__dict__.update(args_dict)
This class uses every supplied keyword argument as an object attribute. Use it this way:
class CashDividends(KwargAttrs):
def __init__(self, gross, net, ISIN, paydate, exdate, recorddate, frequency, type, announceddate, currency):
# save the namespace before it gets polluted
super().__init__(**locals())
# work that might pollute local namespace goes here
# OPTIONAL: update the argument values in case they were modified:
super()._update(**locals())
Using a method like this, you don't have to go through the argument list and assign every single object attribute; it happens automatically.
We bookend everything you need to accomplish in the __init__ method with method calls to the parent-class via super(). We do this because locals() returns a dict every variable in the function's current namespace, so you need to 1.) capture that namespace before any other work pollutes it and 2.) update the namespace in case any work changes the argument values.
The call to update is optional, but the values of the supplied arguments will not be updated if something is done to them after the call to super().__init__() (that is, unless you change the values using setattr(self, 'argname, value)`, which is not a bad idea).
You can continue using this class like so:
class StockSplits(KwargAttrs):
def __init__(self, stocksplitratio, gross, net, ISIN, paydate, exdate, recorddate, frequency, type, announceddate, currency):
super().__init__(**locals())
As mentioned in the other answers you can create a container for our other classes, but you can even do that using this same template class:
class CorporateActions(KwargAttrs):
def __init__(self, stock_splits , cash_dividends):
super().__init__(**locals())
ca = CorporateActions(stock_splits = StockSplits(<arguments>), cash_dividends = CashDividends(<arguments>) )
I'm trying to use a function of a class object to create a new class object and running into problems. Here's the code I have so far:
class Room(object):
def __init__(self, name):
self.name = name
self.N = None
self.E = None
self.S = None
self.W = None
'''relevant code'''
def north(self,room):
self.N = Room(room)
self.N.S = self
def south(self,room):
self.S = Room(room)
self.S.N = self
So I want at least one of these print statements
room1 = Room('room1')
room1.north('room2')
print(room2.S)
print(Room(room2).S)
print(Room('room2').S)
to spit out 'room1', but the first two don't work because room2 as a variable is yet to be defined, and the last one doesn't work because it seems to be creating a new object instead of referencing the existing one, so it just prints the default 'None'.
Does there actually exist a way to reference an existing object with no variable set to it? Or is my only option to do something like this?
def north(self,room):
roomDict[room] = Room(room)
self.N = roomDict[room]
self.N.S = self
Edit: I realize I should probably be calling the new Room's south() function instead of directly changing the S variable, but that seems intuitively like it would cause a loop so I haven't touched it yet.
* Edited based on OP's clarification *
If you have a large number of objects you want to refer to without binding them to variables, dict is the way to go.
You can use #Berci's solution. But note that with that solution, if you already have a room named foo, you can't overwrite it by simply calling Room('foo') again -- doing that will just return the original foo room. To overwrite an existing room you must first do del Room.roomDict['foo'], and then call Room('foo'). This may be something you want, but maybe not.
The implementation below is less fanciful and doesn't require __new__ (in fact, Berci's solution doesn't need __new__ either and can be all done in __init__):
class Room:
registry = {}
def __init__(self, name):
self.registry[name] = self
# the rest of your __init__ code
If you want rooms to be non-overwritable, as they are in Berci's solution, just add two lines:
class Room:
registry = {}
def __init__(self, name):
if name in self.registry:
raise ValueError('room named "{}" already exists'.format(name))
self.registry[name] = self
It's not necessary to nest registry inside Room. You can make it an external dict if you want. The advantage of having the registry as a class attribute is that your Room object can access it as self.registry without knowing its global name. The (slight) disadvantage is that you need to type Room.registry or someroom.registry instead of just, say, registry, every time you access it.
Your dict solution can be brought to work. Use a class level roomDict and a new constructor not to create an already existing object referred by its name:
class Room(object):
roomDict = {}
def __new__(cls, name):
if name in cls.roomDict:
return cls.roomDict[name]
self = object.__new__(cls, name) # here the object is created
cls.roomDict[name] = self
return self
def __init__(self, name):
...
So that you can refer to room2 as Room('room2') afterwards.
For a program that creates a timetable for a doctor(specialist) I want to use certain attributes of an object created by a different class to be used in the class that makes the timetable for the doctor.
class makePatient(object):
def __init__(self,name,room):
self.name = name
self.room = room
def getPatient(self):
print(self.name)
print(self.room)
class makeSpecialist(object):
def __init__(self,name,specialization,timetable):
self.name = name
self.specialization = specialization
self.timetable = timetable
def getSpecialist(self):
print(self.name)
print(self.specialization)
print(self.timetable)
class makeAgenda(object):
def addAgenda(self):
self.timetable.append()
#I want to append the name of the patient I have defined here.
print(self.timetable)
patient1 = makePatient("Michael","101")
specialist1 = makeSpecialist("Dr. John","Hematology",[])
What do I do now, to make sure that the name "Michael" gets added to the list [] of specialist Dr. John?
Thanks in advance, I will provide further details if necessary!
I think another approach would be better; you can put the whole makePatient object into the timetable for the specialist:
specialist1 = makeSpecialist("Dr. John", "Hematology", [patient1])
Now you can access the names and other attributes of the patients in a specialist's timetable:
for patient in specialist1.timetable:
print(patient.name)
You can also define a __repr__ method to tell Python how to display an object, rather than the current getPatient:
class makePatient(object):
# ...
def __repr__(self):
return "{0} (room {1})".format(self.name, self.room)
Now when you print the whole timetable:
>>> print(specialist1.timetable)
You get the necessary information:
[Michael (room 101)]
Note also that the classes should probably be called, simply, Patient, Specialist and Agenda; the make is implied.
Finally, you will get errors in makeAgenda.addAgenda as, without an __init__, self.timetable doesn't exist for a makeAgenda object, and an empty append() doesn't do anything anyway.
Classes are often used to represent entities and operations allowed on them, include constructing, or making, new instances of them. Therefore, your classes would be better named simplyPatient, Specialist, andAgenda. The name of the method that constructs a new instance of any class in Python is always__init__().
That said, after creating aPatientand aSpecialistyou could then add patient instances to the specialist's timetable/agenda by passing it to aSpecialistmethod specifically designed for that purpose. In other words, a Specialist "has-a" Agenda instance namedtimetableand to which patients can be added via an appropriately namedadd_to_timetable()method.
Here's what I mean -- note I've modified your code to follow PEP 8 -- Style Guide for Python Code guidelines which I also suggest that you follow:
class Agenda(object):
def __init__(self):
self.events = []
def append(self, event):
self.events.append(event)
class Patient(object):
def __init__(self, name, room):
self.name = name
self.room = room
def get_patient(self):
print(self.name)
print(self.room)
class Specialist(object):
def __init__(self, name, specialization):
self.name = name
self.specialization = specialization
self.timetable = Agenda()
def add_to_timetable(self, patient):
self.timetable.append(patient)
def get_specialist(self):
print(self.name)
print(self.specialization)
print(self.timetable)
specialist1 = Specialist("Dr. John", "Hematology")
patient1 = Patient("Michael", "101")
specialist1.add_to_timetable(patient1)
I'm not too sure what you're trying to accomplish here with method that just print values or with the makeAgenda class, but here's how you can get Michael in Dr. John's list:
specialist1.timetable.append(patient1.name)