I am making a class but when I test it I get very weird error messages,
This is my code:
class Kaart:
def __init__(self, rows = 10, colls = 10):
self.rows = rows
self.colls = colls
self.matrixRC = []
for _ in range(rows):
self.matrixRC.append([2 for _ in range(colls)])
def __str__(self):
retVal = '\n'.join(''.join(map(str, row)) for row in self.matrixRC)
for index, new in enumerate((' ', '#', '?')):
retVal = retVal.replace(str(index), new)
return retVal
def __getitem__(self, key):
r, c = key
return self.matrixRC[r][c]
def __setitem__(self, key, value):
r, c = key
self.matrixRC[r][c] = value
def __iter__(self):
self.matrixRC.__iter__()
And my test code is here:
import math
import kaart
map = kaart.Kaart()
print(map[1, 2])
but I get the error message AttributeError: Kaart instance has no attribute '__getitem__'
What am I doing wrong? I do have a __getitem__ function so why doesn't he recognize it?
I am having the same problems with __setitem__ and __iter__ but not with __str__
I think that your bug is a syntactic one, meaning that you do not indent all those functions that you are defining. They are not belonging to the class, but to the module's namespace. Try adding indentation for all of them and please let me know if that was the problem.
The problem was in indentation, i used tabs and quad spaces together, this caused problems which made it so that python couldn't identify the code.
Related
I'm trying to use ray module to on an existing code based on if an env variable is true or not.
This is what I've done so far. this code structure is similar to mine but not exactly due to it's size.
import os
if os.getenv("PARALLEL"):
import ray
ray.init()
class A(object):
def __init__(self, attr):
self.attr = attr
def may_be_remote(func):
return ray.remote(func) if os.getenv("PARALLEL") else func
#may_be_remote
def do_work(self):
#work code
def execute(self, n):
for _ in range(n):
do_work.remote()
Then, I call the execute function of class A :
a = A()
a.execute(7)
I get AttributeError : 'function' has no attribute 'remote' on that line.
Where did I go wrong with this code please?
You are accessing remote() on the function do_work, which is not defined.
Did you mean to just call do_work()?
Unfortunately ray makes it hard to get transparent code to switch easily as you intend.
Following https://docs.ray.io/en/latest/ray-overview/index.html#parallelizing-python-classes-with-ray-actors the quite strange insert-.remote syntax is like...
import os
use_ray = os.getenv("PARALLEL") is not None
if use_ray:
import ray
ray.init()
def maybe_remote(cls):
return ray.remote(cls) if use_ray else cls
#maybe_remote
class A:
def __init__(self, attr):
self.attr = attr
def do_work(self, foo): # do something
self.attr += foo
def get_attr(self): # return value maybe from remote worker
return self.attr
if __name__ == '__main__':
n = 7
if use_ray:
a = A.remote(0)
for i in range(1, n + 1):
a.do_work.remote(i)
result = ray.get(a.get_attr.remote())
else:
a = A(0)
for i in range(1, n + 1):
a.do_work(i)
result = a.get_attr()
expect = int((n / 2) * (n + 1))
assert expect == result
Not sure there is also an easy (decorator) solution for the differences in the method calls.
I am having some problems adding two class objects together.
This is the code given to me, which will run MY file, the HyperLogLog and a sample text file:
import HyperLogLog
import sys
hlls = [HyperLogLog.HyperLogLog() for _ in range(5)]
with open(sys.argv[1], "r") as file:
for line in file:
cleanLine = line.replace("\n", "")
(cmd, set, value) = cleanLine.split(" ")[:3]
# See if this was an add, count, or merge command
if cmd == "A":
hlls[int(set)].add(value)
elif cmd == "C":
estimate = hlls[int(set)].count()
print("Estimate:", estimate, "Real count:", value)
elif cmd == "M":
(cmd, m1, m2, m3) = cleanLine.split(" ")
hlls[int(m3)] = hlls[int(m1)] + hlls[int(m2)]
The bottom most line is to merge hlls(set m1) and hlls(set m2). hlls(set x) stores a single parameter M, which is my HyperLogLog vector. I need to make an add function to make the addition line above work. This I have done as follows:
class HyperLogLog:
def __init__(self):
self.M = [0 for x in range(m)]
##############
Code altering the self.M
##############
def __add__(self, other):
Sum=other.M
for i,value in enumerate(other.M):
if value<self.M[i]:
Sum[i]=self.M[i]
self.M=Sum
return self
This will return the correct value for the m3 set. But it will also alter the self.M value of set m1. How can I return something other than self, which will make hlls[int(m3)] and instance of the HyperLogLog class, with the merged self.M value?
If I just return the Sum function, hlls[int(m3)] is no longer an instance of the HyperLogLog class.
If I change self.M as I do, I alter the self.M value of hlls[int(m1)].
If I do something like:
def __add__(self, other):
Sum=other.M
for i,value in enumerate(other.M):
if value<self.M[i]:
Sum[i]=self.M[i]
self2=self
self2.M=Sum
return self2
The value of self.M of instance hlls[int(m1)] is still changed. I don't understand why.
When you do this:
self2=self
Both self and self2 point to the same object, so when one is changed the other one is changed as well. The easiest fix would be to create a new HyperLogLog object, so you would replace the line above with:
self2=HyperLogLog()
This doesn't create a new object instance. It just assigns another name to the same object.
self2=self
You should create a new HyperLogLog object in the __add__ method.
Something like this:
def __add__(self, other):
retval = HyperLogLog()
retval.M = [max(a, b) for a, b in zip(self.M, other.M)]
return retval
In Clojure I can do something like this:
(-> path
clojure.java.io/resource
slurp
read-string)
instead of doing this:
(read-string (slurp (clojure.java.io/resource path)))
This is called threading in Clojure terminology and helps getting rid of a lot of parentheses.
In Python if I try to use functional constructs like map, any, or filter I have to nest them to each other. Is there a construct in Python with which I can do something similar to threading (or piping) in Clojure?
I'm not looking for a fully featured version since there are no macros in Python, I just want to do away with a lot of parentheses when I'm doing functional programming in Python.
Edit: I ended up using toolz which supports pipeing.
Here is a simple implementation of #deceze's idea (although, as #Carcigenicate points out, it is at best a partial solution):
import functools
def apply(x,f): return f(x)
def thread(*args):
return functools.reduce(apply,args)
For example:
def f(x): return 2*x+1
def g(x): return x**2
thread(5,f,g) #evaluates to 121
I wanted to take this to the extreme and do it all dynamically.
Basically, the below Chain class lets you chain functions together similar to Clojure's -> and ->> macros. It supports both threading into the first and last arguments.
Functions are resolved in this order:
Object method
Local defined variable
Built-in variable
The code:
class Chain(object):
def __init__(self, value, index=0):
self.value = value
self.index = index
def __getattr__(self, item):
append_arg = True
try:
prop = getattr(self.value, item)
append_arg = False
except AttributeError:
try:
prop = locals()[item]
except KeyError:
prop = getattr(__builtins__, item)
if callable(prop):
def fn(*args, **kwargs):
orig = list(args)
if append_arg:
if self.index == -1:
orig.append(self.value)
else:
orig.insert(self.index, self.value)
return Chain(prop(*orig, **kwargs), index=self.index)
return fn
else:
return Chain(prop, index=self.index)
Thread each result as first arg
file = Chain(__file__).open('r').readlines().value
Thread each result as last arg
result = Chain(range(0, 100), index=-1).map(lambda x: x * x).reduce(lambda x, y: x + y).value
I am trying to implement the Scipy script from section "Simplifying the syntax" here: http://scipy-cookbook.readthedocs.io/items/FittingData.html
My code is quite long, so I'll post only the parts that seem to be the problem.
I get the following error message: TypeError: unsupported operand type(s) for *: 'int' and 'Parameter', which I understand why it happens: it's the product in this part: return self.amplitude() * np.exp(-1*self.decay_const()*x)
class Plot():
def __init__(self,slice_and_echo,first_plot,right_frame):
self.slice_and_echo = slice_and_echo
self.first_plot = first_plot
self.right_frame = right_frame
self.amplitude = Parameter(1)
self.decay_const = Parameter(1)
def function(self,x):
print(self.amplitude)
print(self.amplitude())
return self.amplitude() * np.exp(-1*self.decay_const()*x)
def create_plot(self):
plot_figure = Figure(figsize=(10,10), dpi=100)
self.the_plot = plot_figure.add_subplot(111)
self.the_plot.plot(self.echoes,self.average,'ro')
print(self.amplitude())
self.fit_parameters = self.fit(self.function,[self.amplitude,self.decay_const],self.average)
print(self.fit_parameters)
def fit(self,function, parameters, y, x=None):
def f(params):
i = 0
for p in parameters:
p.set(params[i])
i += 1
return y - function(x)
if x is None: x = np.arange(y.shape[0])
p = [param for param in parameters]
return optimize.leastsq(f, p)
and the Parameter() class is the same as in the link:
class Parameter:
def __init__(self, value):
self.value = value
def set(self, value):
self.value = value
def __call__(self):
return self.value
The issue seems to be that, when I call self.amplitude() inside of the create_plot(self): method, the value it returns is an integer (which is what I want!). But that doesn't happen when I call it inside of the function(self,x) method; when I print it inside this method I get: <__main__.Parameter object at 0x1162845c0> instead of the integer 1.
Why would it return different values when called from different methods in the same class? What am I missing here?
Thank you!
You got a typo in list comprehension. Your code states:
p = [param for param in parameters]
and the example code states:
p = [param() for param in parameters]
Note that in your case you are generating a list of objects of type Parameter instead of a list of numbers.
By the way, check out module called lmfit - it simplifies fitting routines by great deal.
I wrote the following program:
def split_and_add(invoer):
rij = invoer.split('=')
rows = []
for line in rij:
rows.append(process_row(line))
return rows
def process_row(line):
temp_coordinate_row = CoordinatRow()
rij = line.split()
for coordinate in rij:
coor = process_coordinate(coordinate)
temp_coordinate_row.add_coordinaterow(coor)
return temp_coordinate_row
def process_coordinate(coordinate):
cords = coordinate.split(',')
return Coordinate(int(cords[0]),int(cords[1]))
bestand = file_input()
rows = split_and_add(bestand)
for row in range(0,len(rows)-1):
rij = rows[row].weave(rows[row+1])
print rij
With this class:
class CoordinatRow(object):
def __init__(self):
self.coordinaterow = []
def add_coordinaterow(self, coordinate):
self.coordinaterow.append(coordinate)
def weave(self,other):
lijst = []
for i in range(len(self.coordinaterow)):
lijst.append(self.coordinaterow[i])
try:
lijst.append(other.coordinaterow[i])
except IndexError:
pass
self.coordinaterow = lijst
return self.coordinaterow
However there is an error in
for row in range(0,len(rows)-1):
rij = rows[row].weave(rows[row+1])
print rij
The outcome of the print statement is as follows:
[<Coordinates.Coordinate object at 0x021F5630>, <Coordinates.Coordinate object at 0x021F56D0>]
It seems as if the program doesn't acces the actual object and printing it. What am i doing wrong here ?
This isn't an error. This is exactly what it means for Python to "access the actual object and print it". This is what the default string representation for a class looks like.
If you want to customize the string representation of your class, you do that by defining a __repr__ method. The typical way to do it is to write a method that returns something that looks like a constructor call for your class.
Since you haven't shown us the definition of Coordinate, I'll make some assumptions here:
class Coordinate(object):
def __init__(self, x, y):
self.x, self.y = x, y
# your other existing methods
def __repr__(self):
return '{}({}, {})'.format(type(self).__name__, self.x, self.y)
If you don't define this yourself, you end up inheriting __repr__ from object, which looks something like:
return '<{} object at {:#010x}>'.format(type(self).__qualname__, id(self))
Sometimes you also want a more human-readable version of your objects. In that case, you also want to define a __str__ method:
def __str__(self):
return '<{}, {}>'.format(self.x, self.y)
Now:
>>> c = Coordinate(1, 2)
>>> c
Coordinate(1, 2)
>>> print(c)
<1, 2>
But notice that the __str__ of a list calls __repr__ on all of its members:
>>> cs = [c]
>>> print(cs)
[Coordinate(1, 2)]