Python: source code has attributes that are unaccessible from outside of it - python

So i've been wondering around the other day and stumbled across this piece of code from datetime.datetime:
def timestamp(self):
"Return POSIX timestamp as float"
if self._tzinfo is None:
s = self._mktime()
return s + self.microsecond / 1e6
else:
return (self - _EPOCH).total_seconds()
I decided to check what self._tzinfo and self._mktime() are, so i ran this code in console:
>>> datetime(1970, 1, 1)._tzinfo
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '_tzinfo'
>>> datetime(1970, 1, 1)._mktime()
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '_mktime'
But to my surprise i got AttributeError on both occasions. PyCharm says Cannot find declaration to go to. Yet, there is a normal declaration of _mktime() method right above the timestamp() method.
Despite all this, of course, timestamp() works totally fine.
>>> datetime(1970, 1, 1).timestamp()
-10800.0
So why is this happening? Why are these attributes not accessible from outside and why does interpreter react to them so weirdly?

Related

PyAD- Date based attributes Mostly return None

Copying from Github-
When I try to adquery or pull an attribute that has a date involved (such as lastLogon, lastLogoff, or even a log off method... the attribute always comes up None. However other attributes seem to work. What am I doing wrong?
user1=aduser.ADUser.from_cn("First Last")
date=user1.get_last_login() //comes back as nonetype
Traceback (most recent call last):
File "<pyshell#80>", line 1, in
date=user1.get_last_login()
File "C:\Users\administrator.RESOURCECENTER\AppData\Local\Programs\Python\Python39\lib\site-packages\pyad\adobject.py", line 407, in get_last_login
return pyadutils.convert_datetime(self.get_attribute('lastLogonTimestamp', False))
File "C:\Users\administrator.RESOURCECENTER\AppData\Local\Programs\Python\Python39\lib\site-packages\pyad\pyadutils.py", line 71, in convert_datetime
high_part = int(adsi_time_com_obj.highpart) << 32
AttributeError: 'NoneType' object has no attribute 'highpart'
Not really well documentation on this issue so not sure what I can try.

How to fix TypeError: 'float' object is not callable [duplicate]

As a starting developer in Python I've seen this error message many times appearing in my console but I don't fully understand what does it means.
Could anyone tell me, in a general way, what kind of action produces this error?
That error occurs when you try to call, with (), an object that is not callable.
A callable object can be a function or a class (that implements __call__ method). According to Python Docs:
object.__call__(self[, args...]): Called when the instance is “called” as a function
For example:
x = 1
print x()
x is not a callable object, but you are trying to call it as if it were it. This example produces the error:
TypeError: 'int' object is not callable
For better understaing of what is a callable object read this answer in another SO post.
The other answers detail the reason for the error. A possible cause (to check) may be your class has a variable and method with the same name, which you then call. Python accesses the variable as a callable - with ().
e.g. Class A defines self.a and self.a():
>>> class A:
... def __init__(self, val):
... self.a = val
... def a(self):
... return self.a
...
>>> my_a = A(12)
>>> val = my_a.a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>>
The action occurs when you attempt to call an object which is not a function, as with (). For instance, this will produce the error:
>>> a = 5
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Class instances can also be called if they define a method __call__
One common mistake that causes this error is trying to look up a list or dictionary element, but using parentheses instead of square brackets, i.e. (0) instead of [0]
The exception is raised when you try to call not callable object. Callable objects are (functions, methods, objects with __call__)
>>> f = 1
>>> callable(f)
False
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
I came across this error message through a silly mistake. A classic example of Python giving you plenty of room to make a fool of yourself. Observe:
class DOH(object):
def __init__(self, property=None):
self.property=property
def property():
return property
x = DOH(1)
print(x.property())
Results
$ python3 t.py
Traceback (most recent call last):
File "t.py", line 9, in <module>
print(x.property())
TypeError: 'int' object is not callable
The problem here of course is that the function is overwritten with a property.

Type error in python 2.7

Traceback (most recent call last):
File "C:\Python27\meanshift.py", line 15, in <module>
roi = frame[r:r+h, c:c+w],
TypeError: 'NoneType' object has no attribute '__getitem__'
Please help me to resolve this error, I am trying to make a region of interest on the first frame to track an object.
Usually I get this kind of errors if I forgot to put a return statement at the end of a function:
def create_array(n):
a = np.array((n,5))
b = create_array(10)
print b[5,1]
Without an explicit return a at the end of the function, it will return None.

Pydoc not working in Python 3.3 in Windows

I am trying to generate the help text at runtime and i am not able to use the pydoc command in Windows. When i type
>>> pydoc(atexit)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'pydoc' is not defined
I have already set up the environment variables for pydoc.py file. C:\Python33\Lib\pydoc.py.
This also not works like it works for >>help('atexit')
>>> pydoc('atexit')
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'pydoc' is not defined
Whats the possible reason for it.
Updates:
>>> import pydoc
>>> pydoc(sys)
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'module' object is not callable
>>> pydoc('sys')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'module' object is not callable
Like any library in Python, you need to import it before you can use it.
Edit What exactly are you trying to achieve? Modules are indeed not callable. pydoc.help is the function you want, although I don't really know why you need it, since as you note the standalone help function does the same thing already.

pickle, 'str' does not support buffer interface in python

Ok so I have the following code:
def rate_of_work(self):
global rate
rate = (turtle.textinput("Your Work Rate","What is your hourly work rate in US Dollars?"))
outFile_rate = pickle.dumps(rate)
rate1 = pickle.loads(rate)
rate2 = ((hours*rate1) + (minutes*rate1)*0.0167 + (seconds*rate1)*0.000278) #isnt necessary information
rate3 = round(rate2, 2) #isnt necessary information
im getting the error:
rate1 = pickle.loads(rate)
TypeError: 'str' does not support the buffer interface
please help
I assume you are working under python 3.x
For the specific error, pickle.loads() only accepts bytes, and you are trying to give a plain string to it, that's why it fails.
>>> pickle.loads("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' does not support the buffer interface
>>> pickle.loads(b"")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError

Categories