Error wont dissapear, even when deleting/commenting the faulty lines - python

im am using Linux Ubuntu on a Virtual machine on Windows 10.
I have downloaded a IPython Notebook from dms_tools
Now when I try to run certain parts of the code I become the following error:
/usr/lib/python3.8/distutils/version.py in _cmp(self, other)
335 if self.version == other.version:
336 return 0
--> 337 if self.version < other.version:
338 return -1
339 if self.version > other.version:
TypeError: '<' not supported between instances of 'str' and 'int'
Since I did not know, hot to solve this problem I decided to just eddit this version.py file (perhaps not so smart, but I did not know what else to do...)
I just decided to Comment the faulty lines and return 0 everytime.
Now the weird part, I still get the same error pointing on the comments:
/usr/lib/python3.8/distutils/version.py in _cmp(self, other)
335 # if self.version == other.version:
336 # return 0
--> 337 # if self.version < other.version:
338 # return -1
339 # if self.version > other.version:
TypeError: '<' not supported between instances of 'str' and 'int'
Now I tested what would happen if I just added some empty new lines and the error looks like this
(pointing at the same line, where nothing even is):
/usr/lib/python3.8/distutils/version.py in _cmp(self, other)
335
336
--> 337
338
339
TypeError: '<' not supported between instances of 'str' and 'int'
I just can not explain what is happening here and I hope someone has an idea.
The complete Traceback is:
TypeError Traceback (most recent call last)
/tmp/ipykernel_2888/2193680867.py in <module>
1 fastqdir = os.path.join(resultsdir, './FASTQ_files/')
2 print("Downloading FASTQ files from the SRA...")
----> 3 dms_tools2.sra.fastqFromSRA(
4 samples=samples,
5 fastq_dump='/home/andreas/sratoolkit.2.11.0-ubuntu64/bin/fastq-dump',
~/.local/lib/python3.8/site-packages/dms_tools2/sra.py in fastqFromSRA(samples, fastq_dump, fastqdir, aspera, overwrite, passonly, no_downloads, ncpus)
91 .decode('utf-8').split(':')[-1].strip())
92 fastq_dump_minversion = '2.8'
---> 93 if not (distutils.version.LooseVersion(fastq_dump_version) >=
94 distutils.version.LooseVersion(fastq_dump_minversion)):
95 raise RuntimeError("fastq-dump version {0} is installed. You need "
/usr/lib/python3.8/distutils/version.py in __ge__(self, other)
68
69 def __ge__(self, other):
---> 70 c = self._cmp(other)
71 if c is NotImplemented:
72 return c
/usr/lib/python3.8/distutils/version.py in _cmp(self, other)
335 if self.version == other.version:
336 return 0
--> 337 if self.version < other.version:
338 return -1
339 if self.version > other.version:
TypeError: '<' not supported between instances of 'str' and 'int'

The issue is with your fastq-dump version. Looking at the source code that generates the error from sra.py:
fastq_dump_version = (subprocess.check_output([fastq_dump, '--version'])
.decode('utf-8')
.replace('"fastq-dump" version', '').split(':'))
if len(fastq_dump_version) == 1:
fastq_dump_version = fastq_dump_version[0].strip()
elif len(fastq_dump_version) == 2:
fastq_dump_version = fastq_dump_version[1].strip()
else:
fastq_dump_version = (subprocess.check_output([fastq_dump, '--help'])
.decode('utf-8').split(':')[-1].strip())
fastq_dump_minversion = '2.8'
if not (distutils.version.LooseVersion(fastq_dump_version) >=
distutils.version.LooseVersion(fastq_dump_minversion)):
raise RuntimeError("fastq-dump version {0} is installed. You need "
"at least version {1}".format(fastq_dump_version,
fastq_dump_minversion))
There is an assumption about the output of fastq-dump --version, i.e. that there is a : right before the version being output. This is not the case for 2.11 though and the subprocess call results in this:
>>> (subprocess.check_output(['sratoolkit.2.11.0-ubuntu64/bin/fastq-dump', '--version']).decode('utf-8').replace('"fastq-dump" version', '').split(':'))
['\n"sratoolkit.2.11.0-ubuntu64/bin/fastq-dump" version 2.11.0\n\n']
this string is then used for the version comparison further down and distutils complains about being unable to compare it to the version 2.8 saved in fastq_dump_minversion.
The easiest way to fix this is to use another version of the sra toolkit. Version 2.9 should work, as the version output seems to match the expectation:
>>> (subprocess.check_output(['sratoolkit.2.9.0-ubuntu64/bin/fastq-dump', '--version']).decode('utf-8').replace('"fastq-dump" version', '').split(':'))
['\nsratoolkit.2.9.0-ubuntu64/bin/fastq-dump ', ' 2.9.0\n\n']
Additional Info
Why did changing lib/python3.7/distutils/version.py not do the trick? There is a precompiled file in lib/python3.7/distutils/__pycache__ that is being read instead or the actual lib/python3.7/distutils/version.py. If you edit version.py, you should delete the coresponding file in the __pycache__ dir. Note though, that I strongly recommend to not mess with these files, as you can easily break your python if you don't know what you are doing.
P.S.
This should be fixed in dms_tools version 2.6.11

First of all, the error TypeError: '<' not supported between instances of 'str' and 'int' means that one of the operands you are using in condition checking is string data type and another is integer data type.
You can check what's what by using type() function.
Next what you can do is rename the file using mv command and run again using:
python <filename.py>

In short: you are trying to compare two different data types.
If you're sure that both values are number, you can convert the value before compare:
if int(self.version) < int(other.version):

Related

SageMath: Why doesn't sagemath give line number in case of TypeErrors? Is there a way to trace the actual line number?

Using Sagemath 9.2 on Windows 10
a.sage
i = 10
print("hello " + i)
sage: load("a.sage")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)
in
----> 1 load("a.sage")
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/misc/persist.pyx
in sage.misc.persist.load
(build/cythonized/sage/misc/persist.c:2558)()
141
142 if sage.repl.load.is_loadable_filename(filename):
--> 143 sage.repl.load.load(filename, globals())
144 return
145
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/repl/load.py
in load(filename, globals, attach)
270 add_attached_file(fpath)
271 with open(fpath) as f:
--> 272 exec(preparse_file(f.read()) + "\n", globals)
273 elif ext == '.spyx' or ext == '.pyx':
274 if attach:
in
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/rings/integer.pyx
in sage.rings.integer.Integer.add
(build/cythonized/sage/rings/integer.c:12447)() 1785
return y 1786
-> 1787 return coercion_model.bin_op(left, right, operator.add) 1788 1789 cpdef add(self, right):
/opt/sagemath-9.2/local/lib/python3.7/site-packages/sage/structure/coerce.pyx
in sage.structure.coerce.CoercionModel.bin_op
(build/cythonized/sage/structure/coerce.c:11304)() 1246 #
We should really include the underlying error. 1247 # This
causes so much headache.
-> 1248 raise bin_op_exception(op, x, y) 1249 1250 cpdef canonical_coercion(self, x, y):
TypeError: unsupported operand parent(s) for +: '<class 'str'>' and
'Integer Ring'
In many other types of errors, sage math does give line number where the error happened, but usually in TypeErrors, I don't see that happening
So,
This is a big problem in longer programs & especially in more complicated datatypes. It's quite difficult to track the line giving the problem.
What the different kinds of errors where this happens?
Is there a simple way to track the line number (I use a rather long way).
If you use %attach a.sage instead, it will print line numbers. The line numbers are for the preparsed version of the file, but you can perhaps extract enough information from that. Here is what I see:
sage: %attach /Users/palmieri/Desktop/a.sage
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-a6e4524362f6> in <module>
----> 1 get_ipython().run_line_magic('attach', '/Users/palmieri/Desktop/a.sage')
[snip]
~/.sage/temp/John-iMac-2017.local/34847/a.sage5dnlgxa9.py in <module>
5 _sage_const_10 = Integer(10)
6 i = _sage_const_10
----> 7 print("hello " + i)
[snip]
TypeError: unsupported operand parent(s) for +: '<class 'str'>' and 'Integer Ring'
%attach also has the feature that whenever the file is changed, it automatically gets reloaded.

How to use str.replace in Python 3 [duplicate]

This question already has an answer here:
TypeError: Type aliases cannot be used with isinstance()
(1 answer)
Closed 4 years ago.
I am trying to replace patterns by strings in a column of my dataframe. In Python 2 this works fine.
df = df['Transaction Description'].str.replace("apple", "pear")
But in Python 3 it gives me:
TypeError Traceback (most recent call last)
<ipython-input-10-1f1e7cb2faf3> in <module>()
----> 1 df = df['Transaction Description'].str.replace("apple", "pear")
~/.local/lib/python3.5/site-packages/pandas/core/strings.py in replace(self, pat, repl, n, case, flags, regex)
2427 def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
2428 result = str_replace(self._data, pat, repl, n=n, case=case,
-> 2429 flags=flags, regex=regex)
2430 return self._wrap_result(result)
2431
~/.local/lib/python3.5/site-packages/pandas/core/strings.py in str_replace(arr, pat, repl, n, case, flags, regex)
637 raise TypeError("repl must be a string or callable")
638
--> 639 is_compiled_re = is_re(pat)
640 if regex:
641 if is_compiled_re:
~/.local/lib/python3.5/site-packages/pandas/core/dtypes/inference.py in is_re(obj)
217 """
218
--> 219 return isinstance(obj, re_type)
220
221
/usr/lib/python3.5/typing.py in __instancecheck__(self, obj)
258
259 def __instancecheck__(self, obj):
--> 260 raise TypeError("Type aliases cannot be used with isinstance().")
261
262 def __subclasscheck__(self, cls):
TypeError: Type aliases cannot be used with isinstance().
What's the right way to do this in python 3?
I am using pandas 0.23.0 in both cases.
That's a bug in Python 3.5.2 and lower
What is your python version?
Source
0.23.1 saved the day
#Anush
Note:
You should have first searched google for "TypeError: Type aliases cannot be used with isinstance()" and you would have arrived at the answer immediately

graphviz_layout: AttributeError: 'NoneType' object has no attribute 'get_node'

For some reason when i try to implement this function in my dynamically created graph, I get this weird error. I can run the function examples given online but its fails when I run it.
stacktrace:
122 #pos = nx.spectral_layout(G)
123 #write_dot(G,'test.dot')
--> 124 pos= graphviz_layout(G,prog='twopi',args='')
125 nx.draw_networkx(G, node_color=nodeColors, arrows=False, alpha=.8, labels=nodeLabels, font_size=8)
126 print(nx.info(G))
246 This is a wrapper for pydot_layout.
247 """
--> 248 return pydot_layout(G=G,prog=prog,root=root,**kwds)
249
250
281 for n in G.nodes():
282 pydot_node = pydotplus.Node(make_str(n)).get_name()
--> 283 node=Q.get_node(pydot_node)
284
285 if isinstance(node,list):
AttributeError: 'NoneType' object has no attribute 'get_node'
It looks to me like this is a simple typo. You wrote Q where you probably meant G. There is nothing called Q, and so Q.get_node makes no sense. However G.get_node would make sense.
Without understanding more about the code, it is hard to say what caused the problem, but...
Based on the error message and the stacktrace, the object 'Q' is set to None. For some reason, when the object Q was made, the code assigned the value None instead of the desired value. Troubleshooting will require backtracking to find out when Q was made and why it was set to None.
--> 283 node=Q.get_node(pydot_node)
The answer from Joel may not be correct, since from the source, it shows that
P=to_pydot(G)
if root is not None :
P.set("root",make_str(root))
D=P.create_dot(prog=prog)
if D=="": # no data returned
print("Graphviz layout with %s failed"%(prog))
print()
print("To debug what happened try:")
print("P=pydot_from_networkx(G)")
print("P.write_dot(\"file.dot\")")
print("And then run %s on file.dot"%(prog))
return
Q=pydotplus.graph_from_dot_data(D)
node_pos={}
for n in G.nodes():
pydot_node = pydotplus.Node(make_str(n)).get_name()
node=Q.get_node(pydot_node)
So Q plays no direct role with the eternal

using pool.map to apply function to list of strings in parallel?

I have a large list of http user agent strings (taken from a pandas dataframe) that I am trying to parse using the python implementation of ua-parser. I can parse the list fine when only using a single thread, but based on some preliminary speed testing, it'd take me well over 10 hours to run the whole dataset.
I am trying to use pool.map() to decrease processing time but can't quite seem to figure out how to get it to work. I've read about a dozen 'tutorials' that I found online and have searched SO (likely a duplicate of some sort, as there are a lot of similar questions), but none of the dozens of attempts have worked for one reason or another. I'm assuming/hoping it's an easy fix.
Here is what I have so far:
from ua_parser import user_agent_parser
http_str = df['user_agents'].tolist()
def uaparse(http_str):
for i, item in enumerate(http_str):
return user_agent_parser.Parse(http_str[i])
pool = mp.Pool(processes=10)
parsed = pool.map(uaparse, range(0,len(http_str))
Right now I'm seeing the following error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-25-701fbf58d263> in <module>()
7
8 pool = mp.Pool(processes=10)
----> 9 results = pool.map(uaparse, range(0,len(http_str)))
/home/ubuntu/anaconda/lib/python2.7/multiprocessing/pool.pyc in map(self, func, iterable, chunksize)
249 '''
250 assert self._state == RUN
--> 251 return self.map_async(func, iterable, chunksize).get()
252
253 def imap(self, func, iterable, chunksize=1):
/home/ubuntu/anaconda/lib/python2.7/multiprocessing/pool.pyc in get(self, timeout)
565 return self._value
566 else:
--> 567 raise self._value
568
569 def _set(self, i, obj):
TypeError: 'int' object is not iterable
Thanks in advance for any assistance/direction you can provide.
It seems like all you need is:
http_str = df['user_agents'].tolist()
pool = mp.Pool(processes=10)
parsed = pool.map(user_agent_parser.Parse, http_str)

Python memory error in sympy.simplify

Using 64-bit Python 3.3.1 and 32GB RAM and this function to generate target expression 1+1/(2+1/(2+1/...)):
def sqrt2Expansion(limit):
term = "1+1/2"
for _ in range(limit):
i = term.rfind('2')
term = term[:i] + '(2+1/2)' + term[i+1:]
return term
I'm getting MemoryError when calling:
simplify(sqrt2Expansion(100))
Shorter expressions work fine, e.g:
simplify(sqrt2Expansion(50))
Is there a way to configure SymPy to complete this calculation? Below is the error message:
MemoryError Traceback (most recent call last)
<ipython-input-90-07c1e2de29d1> in <module>()
----> 1 simplify(sqrt2Expansion(100))
C:\Python33\lib\site-packages\sympy\simplify\simplify.py in simplify(expr, ratio, measure)
2878 from sympy.functions.special.bessel import BesselBase
2879
-> 2880 original_expr = expr = sympify(expr)
2881
2882 expr = signsimp(expr)
C:\Python33\lib\site-packages\sympy\core\sympify.py in sympify(a, locals, convert_xor, strict, rational)
176 try:
177 a = a.replace('\n', '')
--> 178 expr = parse_expr(a, locals or {}, rational, convert_xor)
179 except (TokenError, SyntaxError):
180 raise SympifyError('could not parse %r' % a)
C:\Python33\lib\site-packages\sympy\parsing\sympy_parser.py in parse_expr(s, local_dict, rationalize, convert_xor)
161
162 code = _transform(s.strip(), local_dict, global_dict, rationalize, convert_xor)
--> 163 expr = eval(code, global_dict, local_dict) # take local objects in preference
164
165 if not hit:
MemoryError:
EDIT:
I wrote a version using sympy expressions instead of strings:
def sqrt2Expansion(limit):
x = Symbol('x')
term = 1+1/x
for _ in range(limit):
term = term.subs({x: (2+1/x)})
return term.subs({x: 2})
It runs better: sqrt2Expansion(100) returns valid result, but sqrt2Expansion(200) produces RuntimeError with many pages of traceback and hangs up IPython interpreter with plenty of system memory left unused. I created new question Long expression crashes SymPy with this issue.
SymPy is using eval along the path to turn your string into a SymPy object, and eval uses the built-in Python parser, which has a maximum limit. This isn't really a SymPy issue.
For example, for me:
>>> eval("("*100+'3'+")"*100)
s_push: parser stack overflow
Traceback (most recent call last):
File "<ipython-input-46-1ce3bf24ce9d>", line 1, in <module>
eval("("*100+'3'+")"*100)
MemoryError
Short of modifying MAXSTACK in Parser.h and recompiling Python with a different limit, probably the best way to get where you're headed is to avoid using strings in the first place. [I should mention that the PyPy interpreter can make it up to ~1100 for me.]

Categories

Resources