How to declare the __init__ variables that is not necessarily required - python

class Gathering(object):
def __init__(self, date, spent1, spent2, spent3, spent4):
"""Return a gathering object whose date is declared """
self.date = date
self.spent1 = spent1
self.spent2 = spent2
self.spent3 = spent3
self.spent4 = spent4
self.spent_total = spent1+spent2+spent3+spent4
def per_person(self):
return self.spent_total/3
I had made short script that I can easily calculate the portion of one person when me and my friend had some gatehring. We usally move the spots and spent different amount of money, but how many places we visit that night is always different.
So I"d like to make spent1,2,3,4 variables not necessarily required, how can I do that?

You may use a variable number of arguments:
class Gathering(object):
def __init__(self, date, *args):
"""Return a gathering object whose date is declared """
self.date = date
self.spent = args
self.spent_total = sum(args, 0)
def per_person(self):
return self.spent_total / 3.0
sent contains a tuple of values. You can use your class like:
g1 = Gathering(date1, 1, 2, 3)
g2 = Gathering(date2, 2, 3)
and so on.

Related

Class that tracks data of all its active instantiations?

I have a class Foo with its instances having a "balance" attribute. I'm designing it in such a way that Foo can track all the balances of its active instances. By active I mean instances that are currently assigned to a declared variable, of part of a List that is a declared variable.
a = Foo(50) # Track this
b = [ Foo(20) for _ in range(5) ] # Track this
Foo(20) # Not assigned to any variable. Do not track this.
Another feature of Foo is that is has an overloaded "add" operator, where you can add two Foo's balances together or add to a Foo's balance by adding it with an int or float.
Example:
x = Foo(200)
x = x + 50
y = x + Foo(30)
Here is my code so far:
from typing import List
class Foo:
foo_active_instances: List = []
def __init__(self, balance: float = 0):
Foo.foo_active_instances.append(self)
self.local_balance: float = balance
#property
def balance(self):
"""
The balance of only this instance.
"""
return self.local_balance
def __add__(self, addend):
"""
Overloading the add operator
so we can add Foo instances together.
We can also add more to a Foo's balance
by just passing a float/int
"""
if isinstance(addend, Foo):
return Foo(self.local_balance + addend.local_balance)
elif isinstance(addend, float | int):
return Foo(self.local_balance + addend)
#classmethod
#property
def global_balance(cls):
"""
Sum up balance of all active Foo instances.
"""
return sum([instance.balance for instance in Foo.foo_active_instances])
But my code has several issues. One problem is when I try to add a balance to an already existing instance, like:
x = Foo(200)
x = x + 50 # Problem: This instantiates another Foo with 200 balance.
y = Foo(100)
# Expected result is 350, because 250 + 100 = 350.
# Result is 550
# even though we just added 50 to x.
print(Foo.global_balance)
Another problem is replacing a Foo instance with None doesn't remove it from Foo.foo_active_instances.
k = Foo(125)
k = None
# Expected global balance is 0,
# but the balance of the now non-existing Foo still persists
# So result is 125.
print(Foo.global_balance)
I tried to make an internal method that loops through foo_active_instances and counts how many references an instance has. The method then pops the instance from foo_active_instance if it doesn't have enough. This is very inefficient because it's a loop and it's called each time a Foo instance is made and when the add operator is used.
How do I rethink my approach? Is there a design pattern just for this problem? I'm all out of ideas.
The weakref module is perfect for this design pattern. Instead of making foo_active_instances a list, you can make it a weakref.WeakSet. This way, when a Foo object's reference count falls to zero (e.g., because it wasn't bound to a variable), it will be automatically removed from the set.
class Foo:
foo_active_instances = weakref.WeakSet()
def __init__(self, balance: float = 0) -> None:
Foo.foo_active_instances.add(self)
...
In order to add Foo objects to a set, you'll have to make them hashable. Maybe something like
class Foo:
...
def __hash__(self) -> int:
return hash(self.local_balance)
You can use inspect to check if the __init__ or __add__ methods have been called as part of an assignment statement. Additionally, you can keep a default parameter in __init__ to prevent increasing your global sum by the value passed to it when creating a new Foo object from __add__:
import inspect, re
def from_assignment(frame):
return re.findall('[^\=]\=[^\=]', inspect.getframeinfo(frame).code_context[0])
class Foo:
global_balance = 0
def __init__(self, balance, block=False):
if not block and from_assignment(inspect.currentframe().f_back):
Foo.global_balance += balance
self.local_balance = balance
def __add__(self, obj):
if from_assignment(inspect.currentframe().f_back) and not hasattr(obj, 'local_balance'):
Foo.global_balance += obj
return Foo(getattr(obj, 'local_balance', obj), True)
a = Foo(50)
b = [Foo(20) for _ in range(5)]
Foo(20)
print(Foo.global_balance) #150
x = Foo(200)
x = x + 50
y = Foo(100)
print(Foo.global_balance) #350

How can I use a function to create a persistant list correlated to the given arguments?

I'm writing a script to find the moving average of different stocks. This script runs continuously, looping through my API call to add the current price to a list before averaging it. This works fine, however I'd like to be able to put this into a function to where the only input I need to give it is the name of the stock. I'd like this script to work for as many stocks as I want to specify, at the same time. That's where I run into issues because for each stock I have I need to have an empty list predefined that can hold the pricing information.
I've been trying to use the name of the stock to then create a related list, but as I now understand it it's not a great idea using one variable name to create another variable, so I'm not sure what to do. I believe the usual solution here would be to use a dictionary, but I'm a beginner to programming in general so I haven't figured out how to fit that into my situation. Any help would be greatly appreciated.
def sma(stock_name):
list_exists = stock_name + "_list" in locals() or stock_name + "_list" in globals()
if list_exists:
print()
else:
stock_name + "_list" = [] # Problem line, I would like for this to create a list called stock_name_list
stock_price = requests.get("myapi.com", params={"stock_name": stock_name, "bla bla": "blah"})
stock_name_list.append(stock_price)
When you have an operation based on a version of the data specific to that operation, that is usually a good time to think about using classes. This particular proposed class will encapsulate the name of a stock, the list of data specific to it, and perform sma on it:
class Stock:
n = 10
def __init__(self, name):
self.name = name
self.data = []
def sma(self):
stock_price = requests.get("myapi.com", params={"stock_name": self.stock_name, "bla bla": "blah"})
self.data.append(stock_price)
window = self.data[-n:]
return sum(window) / len(window)
Now you can maintain a dictionary of these objects. Any time you encounter a new stock, you just add an item to the dictionary:
stocks = {}
def sma(name):
stock = stocks.get(name)
if name is None: # None is what get returns when the key is missing
stock = Stock(name)
stocks[name] = stock
return stock.sma()
The nice thing is that you now have a dictionary of named datasets. If you want to add a different statistic, just add a method to the Stock class that implements it.
I defined a global sma function here that calls the eponymous method on the object it finds in your dictionary. You can carry encapsulation to an exterme by making the method perform the action of the function if called statically with a name instead of an instance. For example:
class Stock:
n = 10
named_stocks = {} # This is a class variable that replaces the global stocks
def __init__(self, name):
self.name = name
self.data = []
def sma(self):
if isinstance(self, str):
self = Stock.get_stock(self)
stock_price = requests.get("myapi.com", params={"stock_name": self.stock_name, "bla bla": "blah"})
self.data.append(stock_price)
window = self.data[-n:]
return sum(window) / len(window)
#classmethod
def get_stock(cls, name):
stock = cls.named_stocks.get(name)
if stock is None:
stock = cls(name)
cls.named_stocks[name] = stock
return stock
Now that there is a check for isinstance(self, str), you can call the sma method in one of two ways. You can all it directly on an instance, which knows its own name:
aapl = Stock('AAPL')
aapl.sma()
OR
Stock.get_stock('AAPL').sma()
Alternatively, you can call it on the class, and pass in a name:
Stock.sma('AAPL')
use defaultdict
from collections import defaultdict
stock_name_to_stock_prices = defaultdict(list)
stock_name_to_stock_prices['STOCK_NAME'].append(123.45)

Python AssertionError creating a class

I have a class User, and a class Theme. The user class can create a Theme, add a Theme to the Theme's dictionary, and should be able to return the dictionary of themes. I'm really new to python so I'm having trouble with the python logic/syntax
class User:
def __init__(self, name):
self.themes = {}
def createTheme(self, name, themeType, numWorkouts, themeID, timesUsed):
newTheme = Theme(name, themeType, numWorkouts, themeID, timesUsed)
return newTheme
and my Theme class:
class Theme:
def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
#themeType: 1 = genre, 2 = artist, 3 = song
self.name = name
self.themeType = themeType
self.numWorkouts = numWorkouts
self.themeID = themeID
self.timesUsed = timesUsed
I run the test in testUser:
## test createTheme
theme1 = Theme("theme", 2, 5, 1, 0)
self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1)
but I get -
Traceback (most recent call last):
File "/Tests/testUser.py", line 52, in test
self.assertEqual(usr1.createTheme("theme", 2, 5, 1, 0), theme1)
AssertionError: !=
I am not sure what I'm doing wrong, can anyone please help?
(Also, I have the following methods in User, but haven't been able to test them yet since my createTheme doesn't work, but I could use some help to see if there are errors in my logic/syntax:
# returns dict
# def getThemes(self):
# return self.themes
#
# def addTheme(self, themeID, theme):
# if theme not in self.themes:
# themes[themeID] = theme
#
# def removeTheme(self, _theme):
# if _theme.timesUsed == _theme.numWorkouts:
# del themes[_theme.themeID]
What is happening
When attempting to determine if two objects are equal, say obj1 == obj2, Python will do the following.
It will first attempt to call obj1.__eq__(obj2), that is a method
defined in the class of obj1 which should determine the logic for
equality.
If this method does not exist, or return NotImplemented, then
Python falls back on calling obj2.__eq__(obj1).
If this is still not conclusive, Python will return id(obj1) == id(obj2),
i.e. it will tell you if both values are the same object in memory.
In your test, Python has to fall back to the third option and your object are two different instances of the class Theme.
What you want to happen
If you expect objects Theme("theme", 2, 5, 1, 0) and usr1.createTheme("theme", 2, 5, 1, 0) to be equal because they have the same attributes, you have to define the Theme.__eq__ method like so.
class Theme:
def __init__(self, name, themeType, numWorkouts, themeID, timesUsed):
#themeType: 1 = genre, 2 = artist, 3 = song
self.name = name
self.themeType = themeType
self.numWorkouts = numWorkouts
self.themeID = themeID
self.timesUsed = timesUsed
def __eq__(self, other)
# You can implement the logic for equality here
return (self.name, self.themeType, self.numWorkouts, self.themeID) ==\
(other.name, other.themeType, other.numWorkouts, other.themeID)
Note that I am wrapping the attributes in tuples and I then compare the tuples for readability, but you could also compare attributes one by one.

Running functions defined within classes

I have a python class which houses some info. I have another file which some of these functions refer to. My get_date , is working fine however, none of my other functions seem to be working. I am getting the error AttributeError: PVData instance has no attribute 'time' when calling the time function.
class PVData:
def __init__(self):
self.date = yesterday()
self.data = load_data(self.date)
def change_date(self, date):
if self.date != date:
self.date = date
## self.refresh()
else:
self.date = date
self.date = load_data(self.date)
#time, temp, sun
self.time = []
self.temperature = []
self.sunlight = []
for minute in self.date:
self.time.append(minute[0])
self.temperature.append(minute[1])
self.sunlight.append(minute[2])
#power
self.dictonary[a] = []
for a in ARRAYS:
self.dictionary[ARRAYS[i]].append(power)
def get_date(self):
return self.date
def get_time(self, time_index):
return self.time[time_index]
def get_temperature(self):
return self.temperature
def get_sunlight(self):
return self.sunlight
def get_power(self, array):
return self.dictionary[array]
pvd = PVData()
The load_data function is (in another file):
def load_data(dateStr):
text = get_data_for_date(dateStr)
data = []
for line in text.splitlines():
time, temp, sun, powerStr = line.split(',', 3)
power = []
for p in powerStr.split(','):
power.append(int(p))
data.append((time, float(temp), float(sun), tuple(power)))
return data
which returns something such as:
[('19:00', 20.0, 0.0, (0, 0, 0, 0, 0, 0, 0, 21, 31, 52)), (tuple 2), etc etc]
The error seems to be resulting because time is not a valid parameter for self, but I thought that was defined where self.time = [].
Excuse my lack of knowledge, python is quite new to me. Any ideas of why this is not doing as required?
Move time as well as other variables that should be accessible from outside to def init(self). Please keep in mind, that python creates variables in runtime, so if you want variable has been accessible in any place - they should be created on class initialization.
Added:
From your code it looks like, you should move temperature and sunlight to def init(self) as well.

Python: referencing class object list of lists

I am fairly new to python. I have tried to define a class, I then want to create an instance from a file, then refer to specific pieces of it, but cannot seem to. This is Python 3.3.0
Here's the class....
class Teams():
def __init__(self, ID = None, Team = None, R = None, W = None, L = None):
self._items = [ [] for i in range(5) ]
self.Count = 0
def addTeam(self, ID, Team, R=None, W = 0, L = 0):
self._items[0].append(ID)
self._items[1].append(Team)
self._items[2].append(R)
self._items[3].append(W)
self._items[4].append(L)
self.Count += 1
def addTeamsFromFile(self, filename):
inputFile = open(filename, 'r')
for line in inputFile:
words = line.split(',')
self.addTeam(words[0], words[1], words[2], words[3], words[4])
def __len__(self):
return self.Count
Here's the code in Main
startFileName = 'file_test.txt'
filename = startFileName
###########
myTestData = Teams()
myTestData.addTeamsFromFile(startFileName)
sample data in file
100,AAAA,106,5,0
200,BBBB,88,3,2
300,CCCC,45,1,4
400,DDDD,67,3,2
500,EEEE,90,4,1
I think I am good to here (not 100% sure), but now how do I reference this data to see... am i not creating the class correctly? How do I see if one instance is larger than another...
ie, myTestData[2][2] > myTestData[3][2] <----- this is where I get confused, as this doesn't work
Why don't you create a Team class like this :
class Team():
def __init__(self, ID, Team, R=None, W = 0, L = 0)
# set up fields here
Then in Teams
class Teams():
def __init__(self):
self._teams = []
def addTeam (self, ID, Team, R=None, W = 0, L = 0)
team = Team (ID, Team, R=None, W = 0, L = 0)
self._teams.append (team)
Now If i got it right you want to overwrite the > operator's behaviour.
To do that overload __gt__(self, other) [link]
So it will be
class Team ():
# init code from above for Team
def __gt__ (self, otherTeam):
return self.ID > otherTeam.ID # for example
Also be sure to convert those strings to numbers because you compare strings not numbers. Use int function for that.
The immediate problem you're running into is that your code to access the team data doesn't account for your myTestData value being an object rather than a list. You can fix it by doing:
myTestData._items[2][2] > myTestData._items[3][2]
Though, if you plan on doing that much, I'd suggest renaming _items to something that's obviously supposed to be public. You might also want to make the addTeamsFromFile method convert some of the values it reads to integers (rather than leaving them as strings) before passing them to the addTeam method.
An alternative would be to make your Teams class support direct member access. You can do that by creating a method named __getitem__ (and __setitem__ if you want to be able to assign values directly). Something like:
def __getitem__(self, index):
return self._items[index]
#Aleksandar's answer about making a class for the team data items is also a good one. In fact, it might be more useful to have a class for the individual teams than it is to have a class containing several. You could replace the Teams class with a list of Team instances. It depends on what you're going to be doing with it I guess.

Categories