Python AttributeError: 'module' object has no attribute 'atoi' - python-3.x

I tried to run following program of using python 3.2 , there is error:
'module' object has no attribute 'atoi'
Can anybody tell me what should I do to fix this?
i really appreciate it !
import string
def converttoint(str):
try:
value = string.atoi(str)
return value
except ValueError:
return None

string.atoi has been deprecated for a very long time. Since Python 2.0, in fact, and it doesn't exist in Python 3.
Simply use
value = int(s)
instead, and don't call your variable str. That's a bad habit, as it shadows the builtin string type str.

Related

why I'm getting AttributeError: 'NoneType' object has no attribute 'value' when printing values without loop

I've worked on data structures in C++, now I've switched to python and facing an issue here.
When I try to access values of node without loop it always through an error
AttributeError: 'NoneType' object has no attribute 'value'
def solution(l):
head=ListNode(None)
head.next=l
current=head.next #also tried head.next.next gets error on this one too
print(current.value)
return True
error output
But when I try to print values in loop all the values are printed and I don't understand the science behind it
def solution(l):
head=ListNode(None)
head.next=l
current=head
while(current):
print(current.value)
current=current.next
return True
and here is the output of it
printed output
Given that rest of the syntax is correct
Defined Node format is as follow:
# def __init__(self, x):
# self.value = x
# self.next = None
Now my question is why it prints values in loop and not in single line statment
Assuming that l is an instance of ListNode here.
You must always verify that a variable is not None before accessing one of its attributes (such as next).
The second solution does this every time a an assignment is made to current. It will exit the loop as soon as current is None.
The first solution will often work, except when an empty list is passed as argument, i.e. when l is None. Then it will trigger the error you mentioned. In that case also current will be None, and then accessing current.next is invalid.

calling datetime.strftime inside Python string.format fails

Why does calling datetime.strftime fails inside Python's string.format?
In an f-string it works:
from datetime import datetime, timezone
now = datetime.now(timezone.utc)
print(f'{now.strftime("%Y%m%d%H%M%S")}')
try:
print('{now.strftime("%Y%m%d%H%M%S")}'.format(now=now))
except AttributeError as e:
print(e)
# 'datetime.datetime' object has no attribute 'strftime("%Y%m%d%H%M%S")'
Found the answer in the docs:
The syntax is related to that of formatted string literals, but it is less sophisticated and, in particular, does not support arbitrary expressions.

Code incompatibility issues - Python 2.x/ Python 3.x

I have this code:
from abc import ABCMeta, abstractmethod
class Instruction (object):
__metaclass__ = ABCMeta
def __init__(self, identifier_byte):
#type: (int) ->
self.identifier_byte = identifier_byte
#abstractmethod
def process (self):
print ("Identifier byte: ()".format(self.identifier_byte))
class LDAInstruction (Instruction):
def process (self):
super(Instruction,self).process()
with works fine with Python 3.2 but not with 2.6. Then based on this topic: TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
I changed the last line to:
super(Instruction,self).process()
which causes this error message on this precise line:
AttributeError: 'super' object has no attribute 'process'
For me it seems that there is a "process" method for the super invocation. Is Python saying that "super" is an independent object, unrelated to instruction? If yes, how can I tell it that super shall only invoke the base class constructor?
If not, how I shall proceed? Thanks for any ideas.
You're passing the wrong class to super in your call. You need to pass the class you're making the call from, not the base class. Change it to this and it should work:
super(LDAInstruction, self).process()
It's unrelated to your main error, but I'd further note that the base-class implementation of process probably has an error with its attempt at string formatting. You probably want {0} instead of () in the format string. In Python 2.7 and later, you could omit the 0, and just use {}, but for Python 2.6 you have to be explicit.

Calling a method <name> I get AttributeError: 'int' object has no attribute <name>

I have a callback method being triggered on a GPIO pin that when fired calls for a read of a input devices register, however when called I get -
Traceback (most recent call last):
File "/home/coder/Try2.py", line 35, in NewPins
x = self.mcp.readGPIO()
AttributeError: 'int' object has no attribute 'mcp'
If I enter-
a.mcp.readGPIO() <- from python shell I get
`BitArray('0xffff')` <-expected result
>>>
where the RotaryEncoder class is instantiated as 'a'
`class RotaryEncoder:`
`def __init__(self,IntPin, Start, Id):`
.... initialise some variables then open the device as 'mcp' -
`self.mcp = MCP23S17(bus=0x00, pin_cs=0x00, device_id=Id)`
`self.mcp.open()`
`def NewPins(self):`
` global OldPins`
` x = self.mcp.readGPIO()`
..... irrelevant code
callback line is
gpio.add_event_detect(IntPin, gpio.FALLING, callback=NewPins)
expected read value is a BitArray
actual result is
AttributeError: 'int' object has no attribute 'mcp'
Ok my dumb.... - the RPiGPIO library I'm using returns the GPIO channel number in the callback, hence the callback function requires two arguments and not one. I had thought I'd messed up my namespaces and so was trying to code around this when I encountered the error above - I'm a novice python programmer and didn't know where to look!
That'll teach me to not to read the documentation thoroughly...

Cannot access theano shared variable

I created a number of shared variables and placed them in an array like so:
self.params = [self.We, self.Wr, self.Wv, self.b]
When I tried to get their value in another part of the code, something like this:
self.h = [theano.shared(value=p.get_value()*0.) for p in self.params]
I get this error:
AttributeError: 'TensorVariable' object has no attribute 'get_value'
Any help really appreciated.
The problem was that eventhough I used the shared api, I also cast to a float32 with .astype(theano.config.floatX) , and this was causing the conversion from sharedVariable to just tensorVariable

Resources