Python: Unable to call function within a seperate function? (undefined name 'getItemClassiness') - python

For some reason the getClassiness Function does not work as it is not able to call the helper function getItemClassiness. Is there any reason this might be? Thanks!
class Classy(object):
def __init__(self):
self.items = []
def addItem(self, item):
self.items.append(item)
def getItemClassiness(item):
if item == "tophat":
return 2
if item == "bowtie":
return 4
if item == "monocle":
return 5
return 0
def getClassiness(self):
total = 0
for item in self.items:
x = getItemClassiness(item)
total += x
return total
# Test cases
me = Classy()
# Should be 0
print(me.getClassiness())
# Should be 2
me.addItem("tophat")
print(me.getClassiness())
me.addItem("bowtie")
me.addItem("jacket")
me.addItem("monocle")
print(me.getClassiness())
# Should be 11
me.addItem("bowtie\n")
print(me.getClassiness())
# Should be 15
You can use this class to represent how classy someone or something is. "Classy" is interchangable with "fancy". If you add fancy-looking items, you will increase your "classiness". Create a function in "Classy" that takes a string as input and adds it to the "items" list. Another method should calculate the "classiness" value based on the items. The following items have classiness points associated with them: "tophat" = 2 "bowtie" = 4 "monocle" = 5 Everything else has 0 points. Use the test cases below to guide you!

class Classy(object):
def __init__(self):
self.items = []
def addItem(self, string):
self.items.append(string)
def getClassiness(self):
sum = 0
for item in self.items:
if (item == "tophat"):
sum += 2
elif (item == "bowtie"):
sum += 4
elif (item == "monocle"):
sum += 5
else:
sum += 0
return sum

You should declare getItemClassiness as a static method because it doesn't require a specific instance. Then you can call the function as you would an instance method.
#staticmethod
def getItemClassiness(item):
...
def getClassiness(self):
...
for item in self.items:
x = self.getItemClassiness(item)
But still it won't give you 15 for the last test case, because "bowtie" != "bowtie\n". If you intend to ignore white space at the start or the end of the string, use str.strip().

In line 21 call for a class method is made without using the self keyword.
x = self.getItemClassiness(item)
Similarly on line 8 in self keyword is required with as parameter for function definition of getItemClassiness
def getItemClassiness(self, item):

Here is what I did using Static Method. Got the right output in Test Cases.
class Classy(object):
def __init__(self):
self.items = []
def addItem(self, item):
self.items.append(item)
#staticmethod
def getItemClassiness(item):
if item == "tophat":
return 2
if item == "bowtie":
return 4
if item == "monocle":
return 5
return 0
def getClassiness(self):
total = 0
for item in self.items:
x = self.getItemClassiness(item)
total += x
return total

Related

How to update a value after it is returned in python

Consider this piece of code, wondering would it be possible to return a value before it gets updated.
class A:
def __init__(self):
self.n = 0
def get_next(self):
return self.n++ # Return its current value. after it gets returned, update n.
a = A()
a.get_next() # return 0
a.get_next() # return 1
This'll work:
def get_next(self):
old_value = self.n
self.n += 1
return old_value

How come I see 'None' in output

Learning python from Udacity. Exercise is mentioned below. I cannot see where output 'None' is coming from. Is there something about classes that I am missing ? Thx in advance
Output is always
0
None
======= CODE BEGIN ==============
"""You can use this class to represent how classy someone
or something is.
"Classy" is interchangable with "fancy".
If you add fancy-looking items, you will increase
your "classiness".
Create a function in "Classy" that takes a string as
input and adds it to the "items" list.
Another method should calculate the "classiness"
value based on the items.
The following items have classiness points associated
with them:
"tophat" = 2
"bowtie" = 4
"monocle" = 5
Everything else has 0 points.
Use the test cases below to guide you!"""
class Classy(object):
def __init__(self):
self.items = []
self.classiness = 0
def getClassiness(self):
print(self.classiness)
def createList(self):
self.items.append(item)
def addItem(self, item):
if item=="tophat":
self.classiness+=2
elif item=="bowtie":
self.classiness+=4
elif item=="monocle":
self.classiness+=5
else:
self.classiness+=0
return self.classiness
# Test cases
me = Classy()
# Should be 0
print(me.getClassiness())
Your method getClassiness() is printing and the caller is also printing.
Maybe you meant to return a value rather than printing?
def getClassiness(self):
return self.classiness
class Classy(object):
def __init__(self):
self.items = []
self.classiness = 0
def getClassiness(self):
return self.classiness
def createList(self):
self.items.append(item)
def addItem(self, item):
if item=="tophat":
self.classiness+=2
elif item=="bowtie":
self.classiness+=4
elif item=="monocle":
self.classiness+=5
else:
self.classiness=0
Test cases
me = Classy()
Should be 0
print(me.getClassiness())

How can I create a running average of the last N items in a time series?

My basic idea was to create a linked list, and as each new value comes in, add 1/N times the new value and subtract 1/N times the first value, then move the pointer to first along by one and free the memory that had been associated with first.
This won't ultimately be implemented in Python but just to get the process clear in my head, I tried to write it in Python, but my implementation is flawed. Do I need a doubly linked list for this? Is there an alternative approach (not linked-list based) that would be better?
Here's my attempt so far:
class Link:
def __init__(self,val):
self.next = None
self.value = val
class LinkedList:
def __init__(self,maxlength):
self.current_link = None
self.maxlength = maxlength
self.sum = 0.
self.average = None
self.length = 0
self._first_link = None
def add_link(self,val):
new_link = Link(val)
new_link.next = self.current_link
self.current_link = new_link
if self._first_link is None:
self._first_link = self.current_link
self.sum += val
if self.length < self.maxlength:
self.length += 1
else:
self.sum -= self._first_link.value
self._first_link = self._first_link.next # this line is flawed
self.average = self.sum/self.length
def get_first(self):
return self._first_link.value
# Main
ll = LinkedList(5)
for ii in xrange(10):
ll.add_link(ii)
print ii,ll.get_first(),ll.average
The problem is that _first_link gets set to a value that doesn’t have a next. That is, _first_link gets set to the first item that's added, but its next is None, so I don't see how to move it along by 1 as I want to. This is what makes me wonder if a doubly linked list is needed.
I'd appreciate any advice.
I think the simplest implementation is to use a circular linked list (a.k.a. a ring):
class Link(object):
def __init__(self, value=0.0):
self.next = None
self.value = value
class LinkedRing(object):
def __init__(self, length):
self.sum = 0.0
self.length = length
self.current = Link()
# Initialize all the nodes:
last = self.current
for i in xrange(length-1): # one link is already created
last.next = Link()
last = last.next
last.next = self.current # close the ring
def add_val(self, val):
self.sum -= current.value
self.sum += val
self.current.value = val
self.current = self.current.next
def average(self):
return self.sum / self.length
# Test example:
rolling_sum = LinkedRing(5)
while True:
x = float(raw_input())
rolling_sum.add_val(x)
print(">> Average: %f" % rolling_sum.average())
You can implement this using collections.deque and the numerically stable math for maintaining running averages:
import collections
class AveragingBuffer(object):
def __init__(self, maxlen):
assert( maxlen>1)
self.q=collections.deque(maxlen=maxlen)
self.xbar=0.0
def append(self, x):
if len(self.q)==self.q.maxlen:
# remove first item, update running average
d=self.q.popleft()
self.xbar=self.xbar+(self.xbar-d)/float(len(self.q))
# append new item, update running average
self.q.append(x)
self.xbar=self.xbar+(x-self.xbar)/float(len(self.q))
if __name__=="__main__":
import scipy
ab=AveragingBuffer(10)
for i in xrange(32):
ab.append(scipy.rand())
print ab.xbar, scipy.average(ab.q), len(ab.q)
Okay, I thought of a solution that works in O[1] time. I'm still curious if anyone has a linked-list-based solution, but this solution avoids the LL entirely:
class Recent:
def __init__(self,maxlength):
self.maxlength = maxlength
self.length = 0
self.values = [0 for ii in xrange(maxlength)]
self.index = 0
self.total = 0.
self.average = 0.
def add_val(self,val):
last = self.values[self.index%self.maxlength]
self.values[self.index%self.maxlength] = val
self.total += val
self.total -= last
if self.length < self.maxlength:
self.length += 1
self.average = self.total / self.length
self.index += 1
def print_vals(self):
print ""
for ii in xrange(self.length):
print ii,self.values[ii%self.maxlength]
print "average:",self.average
# Example to show it works
rr = Recent(5)
for ii in xrange(3):
rr.add_val(ii)
rr.print_vals()
for ii in xrange(13):
rr.add_val(ii)
rr.print_vals()

Python Priority Queue checking to see if item exists without looping

import heapq
class PriorityQueue:
def __init__(self):
self.heap = []
def push(self, item, priority):
pair = (priority,item)
heapq.heappush(self.heap,pair)
def pop(self):
return heapq.heappop(self.heap)
def isEmpty(self):
return len(self.heap) == 0
def clear(self):
while not (self.isEmpty()):
self.heap.pop()
def getHeap(self):
return self.heap
def getLeng(self):
return len(self.heap)
def exists(self, item):
return len(list(set(self.heap) & set(item)))
pq = PriorityQueue()
x = "test"
pq.push(x,1)
print pq.exists(x)
it printed 0 when it should print 1 since intersection of a set with x and another set with x should be 1
am i overlooking things?
why is it printing 0 instead of 1?
You are pushing tuples of (priority,value) to the heap but want the exist method to work only on values, so you should get a value-only list/iterator out of your heap, something like this:
def exists(self, item):
return item in (x[1] for x in self.heap)

Can iterator be restored and can its value/status be assigned?

I have below snippet which use the generator to give the new ID
...
def __init__(self, id_generator = None):
if id_generator is None: id_generator = 0
if isinstance(id_generator, int):
import itertools
self._generator = itertools.count(id_generator)
else:
self._generator = id_generator
...
id = self._generator.next() #0
id = self._generator.next() #1
id = self._generator.next() #2
id = self._generator.next() #3
my question is, suppose I have an existing id number such as int(99) need to be accepted
during the runtime and need the _generator to generate the ID starting from 99.
looks like:
0
1
2
3
4
<---- new id number 99 be given in somehow
99
100
101
how I can feed it back to the _generator?
Just use self._generator = itertools.count(99) or there's a better way?
More, sucn a confuse can be to be more generalized: how I can memo/assign a generator's
status/value if it is not for an int type?
Thanks!
No, generators just generate items, you cannot set or save their state once they have been created. So self._generator = itertools.count(99) is really the best way to go.
What you can do is duplicate a generator with itertools.tee, which memorizes the output sequence from the first iterable and passes it to the new generators.
You can also write a generator that draws from a source you can change:
class counter(object):
def __init__(self, current=0):
self.current = current
def __iter__(self):
def iter():
while True:
yield self.current
self.current += 1 #
return iter()
def set(self,x):
self.current = x
s = counter()
t = iter(s)
print t.next() # 0
s.set(20)
print t.next() # 21

Categories

Resources