Datetime is not equal to itself in Python [closed] - python-3.x

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I created a simple program to python that check if this X is expired.
import datetime
expired_on = datetime.datetime.now() + datetime.timedelta(0, 20) # add 20 seconds for expiration time
while True:
X = datetime.datetime.now()
if expired_on == X:
print(f"this {X} is expired.")
break
but it didn't break after 20 seconds.

This will be much cleaner.
import datetime
expired_on = datetime.datetime.now() + datetime.timedelta(seconds=20) # Add 20 seconds for expiration time
while True:
now = datetime.datetime.now()
if expired_on <= now:
print('Expired.')
break

Related

In name == 'main', why is variables shared in class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Well, I just noticed that variables in if name == 'main': are shared to the classes in the same file - why is this? I don't recall having this issue in python2...
class A:
def __init__(self, b):
self.b = b
def func_a(self):
return d
if __name__ == '__main__':
classA = A(1)
d = 2
print(classA.func_a())
prints out 2.
What's the reasoning?
this definitely also happens in python2
and is very simple: declaring variables outside of functions/classes makes them global and when python searches for variables with the name d it doesn't find it in the local scope but it does find the global variable

when i write the command this error i see [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
cur.execute(command, tuple(values))
if (fetch := cur.fatchone()) is not None:
return fetch[0]
#bot_has_permissions(manage_roles=True)
#has_permissions(manage_roles=True, manage_guild=True)
async def unmute_members(self, ctx, members: Greedy[Member], *, reason: Optional[str] = "لا يكثر هرجك"):
if not len(members):
await ctx.send("!unmute #member [reason]")
else:
await self.unmute(ctx, members, reason=reason)
AttributeError: 'sqlite3.Cursor' object has no attribute 'fatchone'
Typo.
if (fetch := cur.fatchone()) is not None:
should be
if (fetch := cur.fetchone()) is not None:

AttributeError: 'DataFrame' object has no attribute 'groupyby' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Having used pandas for a long time, this is the first time I got an error as shown in the title of this question and I'm stuck because I don't see any reason why the DataFrame would not have the groupby function "available." Already reinstalled pandas, even looked in the code that is being used (groupby is defined in the core modules).
This is what I'm doing, the error is shown below:
def _bin_by_answer(row):
collocated_answer = row['colname']
if collocated_answer <= -1:
return -1
elif collocated_answer >= 1:
return 1
else:
return 0
df = pd.read_pickle(somepath)
df['binned'] = df.apply(func=lambda row: _bin_by_answer(row), axis=1)
df_sampled = df.groupyby(by='binned', group_keys=False).apply(lambda grp: grp.sample(n=50))
Here is the error:
Traceback (most recent call last):
File "/Users/felix/IdeaProjects/cope/ann4class/exportfromaestoretomturk.py", line 858, in <module>
process_and_export_ps2_inductive()
File "/Users/felix/IdeaProjects/cope/ann4class/exportfromaestoretomturk.py", line 786, in process_and_export_ps2_inductive
df_sampled = df_results.groupyby(by=COL_ANSWER1_COLLOCATED_MAJORITY, group_keys=False).apply(
File "/Users/felix/anaconda3/envs/cope/lib/python3.7/site-packages/pandas/core/generic.py", line 5179, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'groupyby'
As suggested by ddoGas, the cause of this error was a typo. So, I guess the general answer would be: In case you're reading this question because you're running into a similar problem, double check that you wrote the function name correctly.

unexpected results while copying file using shutil [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to copy one file to another. From the accepted answer of this thread, I have done:
def fcopy(src):
dst = os.path.splitext(src)[0] + "a.pot"
try:
shutil.copy(src, dst)
except:
print("Error in copying " + src)
sys.exit(0)
and using it as:
print(atoms)
for q in range(0, len(atoms), 2):
print(type(atoms[q]))
print(atoms[q], fcopy(atoms[q]))
This is quite a few check down inside the code, but I expect that does not matter as long as it finds atoms[q]. But the result I am getting is:
['Mn1.pot', 'Mn2.pot'] <= result of print(atoms)
<class 'str'> <= result of type(atoms)
Mn1.pot None <= result of print(atoms,fcopy(atoms)).
['Mn3.pot', 'Mn4.pot']
<class 'str'>
Mn3.pot None
['Mn5.pot', 'Mn6.pot']
<class 'str'>
Mn5.pot None
['Mn7.pot', 'Mn8.pot']
<class 'str'>
Mn7.pot None
Where I was expecting the print(atoms[q], fcopy(atoms[q])) to give me Mn1.pot Mn1a.pot
I am still a beginner in python, so it will be great if someone can show me what's going wrong here.
You are not getting an error -- if you did you would see the Error in copying message printed.
The part you need to know is that every Python function returns a value. If you do not tell Python what value to return, Python returns None.
So if you want the destination file to be returned to the caller, you have to do it yourself:
def fcopy(src):
dst = os.path.splitext(src)[0] + "a.pot"
try:
shutil.copy(src, dst)
return dst # this line should be added
except:
print("Error in copying " + src)
sys.exit(0)

the python problems about strings [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
def shut_down(s):
s = s.Lower()
if s == 'yes' :
return "Shutting down..."
elif s == 'no':
return "Shutdown aborted!"
else :
return "Sorry, I didn't understand you."
the computer tell me that Your shut_down function threw the following error: 'str' object has no attribute 'Lower'
Your .Lower() is not available in python because it's case sensitive language use .lower()
def shut_down(s):
s = s.lower()
if s == 'yes' :
return "Shutting down..."
elif s == 'no':
return "Shutdown aborted!"
else :
return "Sorry, I didn't understand you."

Resources