ValueError: time inversion found - python-3.x

I am using datetimerange to check if a date is in between two dates. Unfortunetaly I somehow got a strange error message without quitting the program or showing anything. Only when I ctrl+c it I get this error:
ValueError: time inversion found: 2021-09-02 14:48:34.796000+00:00 > 2021-08-25 12:27:20.603000+00:00
So this is the lines causing it:
try:
inRange = time_in_range(start_date, end_date, timestamp)
except:
inRange = time_in_range(end_date, start_date, timestamp)
I got the dates from elasticsearch logs and didn't get this before, so I don't know what has it caused. I even don't understand this error message.
Do you know what the problem is? THere is literally no information on the internet, so I think I ran into a bug or it is very obvious.
Thanks

I guess the issue is that range is incorrect. I can easily trigger this message - see below code:
#!/usr/bin/python3.9
from datetimerange import DateTimeRange
d1="2015-03-22T10:00:00+0900";
d2="2015-03-22T10:10:00+0900";
print(DateTimeRange(d1,d2).validate_time_inversion())
print(DateTimeRange(d2,d1).validate_time_inversion())
Output:
None
Traceback (most recent call last):
File "/home/username/py/time_inversion_ex.py", line 6, in <module>
print(DateTimeRange(d2,d1).validate_time_inversion())
File "/usr/local/lib/python3.9/dist-packages/datetimerange/__init__.py", line 272, in validate_time_inversion
raise ValueError(
ValueError: time inversion found: 2015-03-22 10:10:00+09:00 > 2015-03-22 10:00:00+09:00

Related

ete3 error : could not be translated into taxids! - Bioinformatics

I am using ete3(http://etetoolkit.org/) package in Python within a bioinformatics pipeline I wrote myself.
While running this script, I get the following error. I have used this script a lot for other datasets which don't have any issues and have not given any errors. I am using Python3.5 and miniconda. Any fixes/insights to resolve this error will be appreciated.
[Error]
Traceback (most recent call last):
File "/Users/d/miniconda2/envs/py35/bin/ete3", line 11, in <module>
load_entry_point('ete3==3.1.1', 'console_scripts', 'ete3')()
File "/Users/d/miniconda2/envs/py35/lib/python3.5/site-packages/ete3/tools/ete.py", line 95, in main
_main(sys.argv)
File "/Users/d/miniconda2/envs/py35/lib/python3.5/site-packages/ete3/tools/ete.py", line 268, in _main
args.func(args)
File "/Users/d/miniconda2/envs/py35/lib/python3.5/site-packages/ete3/tools/ete_ncbiquery.py", line 168, in run
collapse_subspecies=args.collapse_subspecies)
File "/Users/d/miniconda2/envs/py35/lib/python3.5/site-packages/ete3/ncbi_taxonomy/ncbiquery.py", line 434, in get_topology
lineage = id2lineage[sp]
KeyError: 3
Continuing from the comment section for better formatting.
Assuming that the sp contains 3 as suggested by the error message (do check this yourself). You can inspect the ete3 code (current version) following its definition, you can trace it to line:
def get_lineage_translator(self, taxids):
"""Given a valid taxid number, return its corresponding lineage track as a
hierarchically sorted list of parent taxids.
So I went to https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi
and checked if 3 is valid taxid and it appears that it is not.
# relevant section from ncbi taxonomy browser
No result found in the Taxonomy database for taxonomy id
3
It appears to me that your only option is to trace how the 3 gets computed. Because the root cause is simply that taxid 3 is not valid taxid number as required by the function.

Owlready2 dynamic class generation

I am trying to dynamically create a class for an owlready2 ontology. The documentation suggests the following line of code:
NewClass = types.new_class("NewClassName", (SuperClass,), kwds = { "namespace" : my_ontology })
In my case this equals
types.new_class("NewClassName", (onto["ParentClass"],), kwds={'namespace' : onto})
However, when I run the above code, I get the following exception:
Traceback (most recent call last):
(onto[object.get('owl_dataProperty_parent')],), kwds={'namespace' : onto})
File "/usr/lib/python3.6/types.py", line 62, in new_class
return meta(name, bases, ns, **kwds)
TypeError: __new__() got an unexpected keyword argument 'namespace
I have no idea what went wrong there and after hours of debugging I am still clueless.
I am using Python 3.6.6 and version 0.11 of owlready2
I figured out that the "namespace" attribute is not needed at all. So the following works just fine:
types.new_class("NewClassName", (onto["ParentClass"],))
Even though this does not solve the overall issue, it answered my question.
Additionally, it seams like this is the way to do it in general, cf.owlready forum. So maybe it is time to update the documentation.

I/O operation on closed file using input()

I have code that sets up an environment for running and logging scientific experiments. Some of the initial setup involves using the built in input() method to query the user for values. I keep getting a I/O operation on closed file error whenever I try to call input however.
Code flow: Control.py calls Analyzer.py which calls a specific method in Prompts.py (the code for which is below).
def prompt_instruments(message):
res = input(message) # query user with arg message
print("done")
if '.' in res:
print("User input not cool. Use comma-separated values.")
return None # to continue prompting
...
I have searched all over the internet and have been unable to find anything remotely related. Thank you so much!!
The code you posted seems ok, and the error is probably in one of your other files.
The input() function uses sys.stdout to display the prompt text, and sys.stdin to get the user's input text.
The error message you get is probably caused by one of these files being closed, e.g.:
>>> import sys
>>> input('test: ')
test: hello
'hello'
>>> sys.stdin.close()
>>> input('test: ')
test: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
or:
>>> import sys
>>> input('test: ')
test: hi
'hi'
>>> sys.stdout.close()
>>> input('test: ')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
I can't tell you exactly where to fix this issue, but look for things that might close one of these file, either directly or indirectly (e.g. context manager).

Python Boto3 OpsWorks KeyError by getting CustomJson

I try to get the custom json from my OpsWorks stacks with python and boto3. Getting the name is ok but if I want to get the CustomJson - KeyError. Don't have a clue why.
import boto3
import traceback
client = boto3.client('opsworks')
response = client.describe_stacks()
max_elements = len(response['Stacks'])
for i in range(max_elements):
stack_Name = response['Stacks'][i]['Name'] # works
try:
stack_CustomJson = response['Stacks'][i]['CustomJson'] # KeyError
except:
traceback.print_exc()
That's the console output:
$ python3 get_custom_json.py
Traceback (most recent call last):
File "get_custom_json.py", line 27, in get_opsworks_details
stack_CustomJson = response['Stacks'][i]['CustomJson']
KeyError: 'CustomJson'
Reading the docs from http://boto3.readthedocs.org/en/latest/reference/services/opsworks.html#OpsWorks.Client.describe_stacks I don't see a difference between 'Name' and 'CustomJson' except that CustomJson is a JSON object. Do I have to transform it?
Thx in advance
You are getting a KeyError occasionally because the CustomStack element in the response is optional. If a custom stack is specified for the stack, it will be returned. Otherwise, the CustomStack key will not be there at all. You should do something like:
if 'CustomStack' in stack:
# do your thing
Had a quick chat with a developer in my company. Got some basic introductions for getting better in coding and python and whatever (must loose some of my admin thinking).
Don't iterate about max_elements, better iterate above 'stack in stacks'.
for stack in response['Stacks']:
print(stack['CustomJson'])
Now it works - I'll get the custom json from the OpsWorks stacks. But still there is the KeyError.
Traceback (most recent call last):
File "get_custom_json.py", line 22, in <module>
get_opsworks_details()
File "get_custom_json.py", line 18, in get_opsworks_details
print(stack['CustomJson'])
KeyError: 'CustomJson'
I'll check if I can fetch him again to see why that's happening.
[EDIT] Blind spot - if a stack has no custom json then the KeyError will occur.

why cassandra throws exception in select query?

I am using cassandra db ,while i use select at some times i get this exception?
Traceback (most recent call last):
File "bin/cqlsh", line 1001, in perform_statement_untraced
self.cursor.execute(statement, decoder=decoder)
File "bin/../lib/cql-internal-only-1.4.0.zip/cql-1.4.0/cql/cursor.py", line 81, in execute
return self.process_execution_results(response, decoder=decoder)
File "bin/../lib/cql-internal-only-1.4.0.zip/cql-1.4.0/cql/thrifteries.py", line 131, in process_execution_results
raise Exception('unknown result type %s' % response.type)
Exception: unknown result type None
can any one explain why this exceptions occur and also i get Internal application error.
what this error message actually means?
EDIT: I get this error for the first time, next time onwards its running correctly.I dont get why it is so?
//cql query via cqlsh
select * from event_logging limit 5;

Resources