Enumeration attribute not recognized - python

This is the code I am running:
import Qubit
from Z import Z
q = Qubit(Z.V)
Qubit code looks like this:
from Z import Z
class Qubit:
def __init__(self, spin):
if isinstance(spin, Z):
print ('success')
Z code looks like this:
from enum import Enum
class Z(Enum):
H = 0
V = 1
When I run the code, I get this error:
Traceback (most recent call last):
File "main.py", line 4, in <module>
q = Qubit(Z.V)
TypeError: 'module' object is not callable
Am I doing something wrong?

Yes, the enum Z is a member of the module Z, which means you need to refer to it as Z.Z, both in main.py and Qubit. Alternatively, you can replace import Z with from Z import Z in both files.

Related

'tuple' object has no attribute 'split' when getting input works with debugger but not with vsCode terminal

Having some issues with my code, would be great if you can help me out
Function of code:.
Calculate the dot product between two vectors
def dot_product():
vector1 = input("Enter your vector in the form of: x, y, z: ").split(",")
vector2 = input("Enter your vector in the form of: x, y, z: ").split(",")
result = 0
for item in zip(vector1, vector2):
result += (int(item[0])*int(item[1]))
return vector1, vector2, result
print(dot_product())
Problem: Every time I run this code I get an error:
Traceback (most recent call last):
File "/premium.py/Chapter1/work.py", line 34, in <module>
print(dot_product())
File "/premium.py/Chapter1/work.py", line 26, in dot_product
vector1 = input("Enter your vector in the form of: x, y, z: ").split(",")
AttributeError: 'tuple' object has no attribute 'split'
Now it was working fine before but now it isn't. I have tried doing:
vector1 = str(input("Enter your vector in the form of: x, y, z: ")).split(",")
vector2 = str(input("Enter your vector in the form of: x, y, z: ")).split(",")
But I am still getting same error.
When I run it with the python debugger console it works fine and I don't get any issues.
But when I run it with the normal code It doesn't work.
Btw I just installed code-runner extension, don't know if that has anything to do with it
an error of
File "<string>", line 1
python -u "/premium.py/Chapter1/work.py"^
SyntaxError: invalid syntax
every other time I try to run it.
Please can you help me understand thanks

TypeError: cannot unpack non-iterable int object, Plus Minus problem in HackerRank

I am stuck on what to do on this problem, tried to execute it on vscode and the hackerrank IDE, both are giving errors even though all solutions on web are same as mine
import math
import os
import random
import re
import sys
#
# Complete the 'plusMinus' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#
def plusMinus(arr):
# Write your code here
neg,pos,zero=0
for i in range(0,len(arr)):
if(arr[i]<0):
neg+=0
elif(arr[i]>0):
pos+=0
else:
zero+=0
print(pos/len(arr))
print(neg/len(arr))
print(zero/len(arr))
return 0
if __name__ == '__main__':
n = int(input().strip())
arr = list(map(int, input().rstrip().split()))
plusMinus(arr)
Traceback (most recent call last):
File "/tmp/submission/20211128/06/29/hackerrank-a7793862d075fcff390bb368bc113c47/code/Solution.py", line 35, in <module>
plusMinus(arr)
File "/tmp/submission/20211128/06/29/hackerrank-a7793862d075fcff390bb368bc113c47/code/Solution.py", line 17, in plusMinus
neg,pos,zero=0
TypeError: cannot unpack non-iterable int object
Reading the traceback reveals the cause of the error you're getting:
Traceback (most recent call last):
File "/tmp/submission/20211128/06/29/hackerrank-a7793862d075fcff390bb368bc113c47/code/Solution.py", line 35, in <module>
plusMinus(arr)
File "/tmp/submission/20211128/06/29/hackerrank-a7793862d075fcff390bb368bc113c47/code/Solution.py", line 17, in plusMinus
neg,pos,zero=0
TypeError: cannot unpack non-iterable int object
The correct syntax would be either
# map the elements of the iterable on the right-hand side to the
# declared variable names
neg, pos, zero = 0, 0, 0
or
# assign the same value to all declared variables
neg = pos = zero = 0
As-written, it's trying to unpack the integer 0 into three separate values neg, pos, zero. Since 0 is not an iterable object like a tuple (as, for example, 0, 0, 0 is), and thus cannot be unpacked into multiple values, python throws an error.

Cannot find mpmath package in python

I am using Python35. I would like to use the mpmath.invertlaplace function used in this question. Unfortunately I am having some trouble:
>>> import mpmath as mp
>>> import numpy as np
>>> def f(s):
return(1/s)
>>> t = np.linspace(0.01,0.5,10)
>>> G = []
>>> for i in range(0,10):
G.append(mp.invertlaplace(f, t[i], method = 'dehoog', degree = 18))
Traceback (most recent call last):
File "<pyshell#254>", line 2, in <module>
G.append(mp.invertlaplace(f, t[i], method = 'dehoog', degree = 18))
AttributeError: module 'mpmath' has no attribute 'invertlaplace'
Has this function been added too recently for Python35 to pick up? Am I missing something here? I feel like this should work...

How to parallel a Theano function using multiprocessing?

I have a function returned by theano.function(), and I want to use it within multiprocessing for speedup. The following is a simplified demo script to show where I run into problem:
import numpy as np
from multiprocessing import Pool
from functools import partial
import theano
from theano import tensor
def get_theano_func():
x = tensor.dscalar()
y = x + 0.1
f = theano.function([x], [y])
return f
def func1(func, x):
return func(x)
def MPjob(xlist):
f = get_theano_func()
fp = partial(func1, func=f)
pool = Pool(processes=5)
Results = pool.imap(fp, xlist)
Y = []
for y in Results:
Y.append(y[0])
pool.close()
return Y
if __name__ == '__main__':
xlist = np.arange(0, 5, 1)
Y = MPjob(xlist)
print(Y)
In the above codes, the theano function 'f' is fed to 'func1()' as input argument. If MPjob() runs correctly, it should return [0.1, 1.1, 2.1, 3.1, 4.1]. However, an exception "TypeError: func1() got multiple values for argument 'func'" raised.
The full trackback log is as follows:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Python35\lib\multiprocessing\pool.py", line 119, in worker
result = (True, func(*args, **kwds))
TypeError: func1() got multiple values for argument 'func'
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "F:/DaweiLeng/Code/workspace/Python/General/theano_multiprocess_debug.py", line 36, in <module>
Y = MPjob(xlist)
File "F:/DaweiLeng/Code/workspace/Python/General/theano_multiprocess_debug.py", line 29, in MPjob
for y in Results:
File "C:\Python35\lib\multiprocessing\pool.py", line 695, in next
raise value
TypeError: func1() got multiple values for argument 'func'
Anyone got a hint?
Turns out it's related with the partial() function. The full explanation is here https://github.com/Theano/Theano/issues/4720#issuecomment-232029702

How come there is a TypeError in my Python code?

from celery.task import Task
class Decayer(Task):
def calc_decay_value(self, x):
y = (1.0/(2^x))
return y
def calc_decay_time(self, x):
y = 2^x
return y
def run(self, d, **kwargs):
#do stuff.
return 0
>>> decayer = tasks.Decayer(r)
Traceback (most recent call last):
File "scanDecay.py", line 31, in <module>
decayer = tasks.Decayer(r)
TypeError: object.__new__() takes no parameters
Two errors
1) Your class doesn't have an __init__ function. Either add one, or use this instead:
decayer = tasks.Decayer()
2) You are trying to raise an integer to the power of a float, but ^ means xor and cannot be used on floats. Use ** instead of ^:
y = 2 ** x
The problem seems due to decayer = tasks.Decayer(r) call and tasks.Decayer is not designed to take a argument, because Task does not define a __init__ method which can take one.

Categories