Getting error when trying to print class definition with inspect.getsource()) - python

I am defining a class:
class MyFirstClass:
pass
After, I am trying to print the definition of MyFirstClass class:
import inspect
print(inspect.getsource(MyFirstClass))
But I am getting error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_22132/2338486789.py in <module>
1 import inspect
----> 2 print(inspect.getsource(MyFirstClass))
C:\ProgramData\Anaconda3\lib\inspect.py in getsource(object)
971 or code object. The source code is returned as a single string. An
972 OSError is raised if the source code cannot be retrieved."""
--> 973 lines, lnum = getsourcelines(object)
974 return ''.join(lines)
975
C:\ProgramData\Anaconda3\lib\inspect.py in getsourcelines(object)
953 raised if the source code cannot be retrieved."""
954 object = unwrap(object)
--> 955 lines, lnum = findsource(object)
956
957 if istraceback(object):
C:\ProgramData\Anaconda3\lib\inspect.py in findsource(object)
766 is raised if the source code cannot be retrieved."""
767
--> 768 file = getsourcefile(object)
769 if file:
770 # Invalidate cache if needed.
C:\ProgramData\Anaconda3\lib\inspect.py in getsourcefile(object)
682 Return None if no way can be identified to get the source.
683 """
--> 684 filename = getfile(object)
685 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
686 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
C:\ProgramData\Anaconda3\lib\inspect.py in getfile(object)
651 if getattr(module, '__file__', None):
652 return module.__file__
--> 653 raise TypeError('{!r} is a built-in class'.format(object))
654 if ismethod(object):
655 object = object.__func__
TypeError: <class '__main__.MyFirstClass'> is a built-in class
I expected oputput is:
class MyFirstClass:
pass
How to correctly use inspect.getsource()) to get my expected output (definition of MyFirstClass class)?

Related

AttributeError: 'IntervalArray' object has no attribute '_dtype' while reading pickle file to dataframe

I want to read a pickle file into a data frame. However, I get the following error message and I don't know how to solve the issue.
df_batches = pd.read_pickle(folder_in / "batches_and_phases.p")
Error Message:
AttributeError Traceback (most recent call last)
File ~\Master_Thesis\mypython\lib\site-packages\pandas\io\pickle.py:205, in read_pickle(filepath_or_buffer, compression, storage_options)
204 warnings.simplefilter("ignore", Warning)
--> 205 return pickle.load(handles.handle)
206 except excs_to_catch:
207 # e.g.
208 # "No module named 'pandas.core.sparse.series'"
209 # "Can't get attribute '__nat_unpickle' on <module 'pandas._libs.tslib"
File ~\Master_Thesis\mypython\lib\site-packages\pandas\_libs\internals.pyx:750, in pandas._libs.internals.BlockManager.__setstate__()
File ~\Master_Thesis\mypython\lib\site-packages\pandas\_libs\internals.pyx:767, in pandas._libs.internals.BlockManager.__setstate__()
File ~\Master_Thesis\mypython\lib\site-packages\pandas\core\internals\blocks.py:2143, in ensure_block_shape(values, ndim)
2142 if values.ndim < ndim:
-> 2143 if not is_1d_only_ea_dtype(values.dtype):
2144 # TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
2145 # block.shape is incorrect for "2D" ExtensionArrays
2146 # We can't, and don't need to, reshape.
2147 values = cast("np.ndarray | DatetimeArray | TimedeltaArray", values)
File ~\Master_Thesis\mypython\lib\site-packages\pandas\core\arrays\interval.py:624, in IntervalArray.dtype(self)
622 #property
623 def dtype(self) -> IntervalDtype:
--> 624 return self._dtype
AttributeError: 'IntervalArray' object has no attribute '_dtype'
Thank you!

I can't seem to find a fix for the "ValueError: Unknown subheader signature" raised while reading sas file using pd.read_sas?

I am trying to load a sasbdat file in python using pd.read_sas() and I fail to load the data due to the below error.
ValueError Traceback (most recent call last)
<ipython-input-148-64f915da8256> in <module>
----> 1 df_sas = pd.read_sas('input_sasfile.sas7bdat', format='sas7bdat')
~\.conda\envs\overloaded-new\lib\site-packages\pandas\io\sas\sasreader.py in read_sas(filepath_or_buffer, format, index, encoding, chunksize, iterator)
121
122 reader = SAS7BDATReader(
--> 123 filepath_or_buffer, index=index, encoding=encoding, chunksize=chunksize
124 )
125 else:
~\.conda\envs\overloaded-new\lib\site-packages\pandas\io\sas\sas7bdat.py in __init__(self, path_or_buf, index, convert_dates, blank_missing, chunksize, encoding, convert_text, convert_header_text)
144
145 self._get_properties()
--> 146 self._parse_metadata()
147
148 def column_data_lengths(self):
~\.conda\envs\overloaded-new\lib\site-packages\pandas\io\sas\sas7bdat.py in _parse_metadata(self)
349 self.close()
350 raise ValueError("Failed to read a meta data page from the SAS file.")
--> 351 done = self._process_page_meta()
352
353 def _process_page_meta(self):
~\.conda\envs\overloaded-new\lib\site-packages\pandas\io\sas\sas7bdat.py in _process_page_meta(self)
355 pt = [const.page_meta_type, const.page_amd_type] + const.page_mix_types
356 if self._current_page_type in pt:
--> 357 self._process_page_metadata()
358 is_data_page = self._current_page_type & const.page_data_type
359 is_mix_page = self._current_page_type in const.page_mix_types
~\.conda\envs\overloaded-new\lib\site-packages\pandas\io\sas\sas7bdat.py in _process_page_metadata(self)
388 subheader_signature = self._read_subheader_signature(pointer.offset)
389 subheader_index = self._get_subheader_index(
--> 390 subheader_signature, pointer.compression, pointer.ptype
391 )
392 self._process_subheader(subheader_index, pointer)
~\.conda\envs\overloaded-new\lib\site-packages\pandas\io\sas\sas7bdat.py in _get_subheader_index(self, signature, compression, ptype)
401 else:
402 self.close()
--> 403 raise ValueError("Unknown subheader signature")
404 return index
405
ValueError: Unknown subheader signature
Though I found relevant github issue (https://github.com/pandas-dev/pandas/issues/24794), but it was closed because the issue got resolved by updating the pandas.
Any help is greatly appreciated.

Node2vec and networkx

I am attempting to run node2vec on a directed networkx network I have created. The network looks like this:
OutEdgeDataView([(7, 1, {'senderId': 7, 'weight': 273}), (7, 8, {'senderId': 7, 'weight': 319}), (7, 9, {'senderId': 7, 'weight': 137})....
With each node having an integer ID and a weight linking one node to another.
I am trying to use the node2vec module on this network as:
from node2vec import Node2Vec
node2vec = Node2Vec(mail_n_basic, dimensions=64, walk_length=30, num_walks=200, workers=4)
And am returned with this error, any help explaining the error would be much appreciated:
---------------------------------------------------------------------------
_RemoteTraceback Traceback (most recent call last)
_RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Users\Andrew\Anaconda3\lib\site-packages\joblib\externals\loky\process_executor.py", line 398, in _process_worker
r = call_item.fn(*call_item.args, **call_item.kwargs)
File "C:\Users\Andrew\Anaconda3\lib\site-packages\joblib\_parallel_backends.py", line 561, in __call__
return self.func(*args, **kwargs)
File "C:\Users\Andrew\Anaconda3\lib\site-packages\joblib\parallel.py", line 224, in __call__
for func, args, kwargs in self.items]
File "C:\Users\Andrew\Anaconda3\lib\site-packages\joblib\parallel.py", line 224, in <listcomp>
for func, args, kwargs in self.items]
File "C:\Users\Andrew\Anaconda3\lib\site-packages\node2vec\node2vec.py", line 51, in parallel_generate_walks
walk_to = np.random.choice(walk_options, size=1)[0]
File "mtrand.pyx", line 1126, in mtrand.RandomState.choice
ValueError: a must be non-empty
"""
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-58-3ac160061528> in <module>()
1
----> 2 node2vec = Node2Vec(mail_n_basic, dimensions=64, walk_length=30, num_walks=200, workers=4)
~\Anaconda3\lib\site-packages\node2vec\node2vec.py in __init__(self, graph, dimensions, walk_length, num_walks, p, q, weight_key, workers, sampling_strategy)
111
112 self.d_graph = self._precompute_probabilities()
--> 113 self.walks = self._generate_walks()
114
115 def _precompute_probabilities(self):
~\Anaconda3\lib\site-packages\node2vec\node2vec.py in _generate_walks(self)
178 self.NEIGHBORS_KEY,
179 self.PROBABILITIES_KEY) for idx, num_walks
--> 180 in enumerate(num_walks_lists, 1))
181
182 walks = flatten(walk_results)
~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
960
961 with self._backend.retrieval_context():
--> 962 self.retrieve()
963 # Make sure that we get a last message telling us we are done
964 elapsed_time = time.time() - self._start_time
~\Anaconda3\lib\site-packages\joblib\parallel.py in retrieve(self)
863 try:
864 if getattr(self._backend, 'supports_timeout', False):
--> 865 self._output.extend(job.get(timeout=self.timeout))
866 else:
867 self._output.extend(job.get())
~\Anaconda3\lib\site-packages\joblib\_parallel_backends.py in wrap_future_result(future, timeout)
513 AsyncResults.get from multiprocessing."""
514 try:
--> 515 return future.result(timeout=timeout)
516 except LokyTimeoutError:
517 raise TimeoutError()
~\Anaconda3\lib\site-packages\joblib\externals\loky\_base.py in result(self, timeout)
429 raise CancelledError()
430 elif self._state == FINISHED:
--> 431 return self.__get_result()
432 else:
433 raise TimeoutError()
~\Anaconda3\lib\site-packages\joblib\externals\loky\_base.py in __get_result(self)
380 def __get_result(self):
381 if self._exception:
--> 382 raise self._exception
383 else:
384 return self._result
ValueError: a must be non-empty
I'm the author of this library.
If you are using Windows, parallel execution won't work because joblib and Windows issues.
Run the same code with the updated version pip install -U node2vec and when constructing the Node2Vec class, pass workers=1

"TypeError: write() argument must be str, not bytes" when trying to write an ElementTree to an XML file

I'm trying to write some XML as a byte string (to wit, a response from the Zillow API) to an XML file using the ElementTree.write method. Here is an example of what I'm trying to do:
from xml.etree import ElementTree
content = b'<?xml version="1.0" encoding="utf-8"?><Comps:comps xsi:schemaLocation="http://www.zillow.com/static/xsd/Comps.xsd https://www.zillowstatic.com/vstatic/b20c067/static/xsd/Comps.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:Comps="http://www.zillow.com/static/xsd/Comps.xsd"><request><zpid>48789177</zpid><count>1</count></request><message><text>Request successfully processed</text><code>0</code></message><response><properties><principal><zpid>48789177</zpid><links><homedetails>https://www.zillow.com/homedetails/1614-E-Columbia-St-Seattle-WA-98122/48789177_zpid/</homedetails><graphsanddata>http://www.zillow.com/homedetails/1614-E-Columbia-St-Seattle-WA-98122/48789177_zpid/#charts-and-data</graphsanddata><mapthishome>http://www.zillow.com/homes/48789177_zpid/</mapthishome><comparables>http://www.zillow.com/homes/comps/48789177_zpid/</comparables></links><address><street>1614 E Columbia St</street><zipcode>98122</zipcode><city>SEATTLE</city><state>WA</state><latitude>47.609388</latitude><longitude>-122.310834</longitude></address><taxAssessmentYear>2016</taxAssessmentYear><taxAssessment>528000.0</taxAssessment><yearBuilt>1907</yearBuilt><lotSizeSqFt>2613</lotSizeSqFt><finishedSqFt>2430</finishedSqFt><bathrooms>2.0</bathrooms><bedrooms>4</bedrooms><zestimate><amount currency="USD">900186</amount><last-updated>04/13/2018</last-updated><oneWeekChange deprecated="true"></oneWeekChange><valueChange duration="30" currency="USD">112285</valueChange><valuationRange><low currency="USD">783162</low><high currency="USD">1053218</high></valuationRange><percentile>90</percentile></zestimate><localRealEstate><region name="Minor" id="271921" type="neighborhood"><zindexValue>588,800</zindexValue><links><overview>http://www.zillow.com/local-info/WA-Seattle/Minor/r_271921/</overview><forSaleByOwner>http://www.zillow.com/minor-seattle-wa/fsbo/</forSaleByOwner><forSale>http://www.zillow.com/minor-seattle-wa/</forSale></links></region></localRealEstate></principal><comparables><comp score="7.0"><zpid>89822967</zpid><links><homedetails>https://www.zillow.com/homedetails/1711-E-Fir-St-Seattle-WA-98122/89822967_zpid/</homedetails><graphsanddata>http://www.zillow.com/homedetails/1711-E-Fir-St-Seattle-WA-98122/89822967_zpid/#charts-and-data</graphsanddata><mapthishome>http://www.zillow.com/homes/89822967_zpid/</mapthishome><comparables>http://www.zillow.com/homes/comps/89822967_zpid/</comparables></links><address><street>1711 E Fir St</street><zipcode>98122</zipcode><city>SEATTLE</city><state>WA</state><latitude>47.602463</latitude><longitude>-122.309743</longitude></address><taxAssessmentYear>2016</taxAssessmentYear><taxAssessment>513000.0</taxAssessment><yearBuilt>2008</yearBuilt><lotSizeSqFt>1306</lotSizeSqFt><finishedSqFt>1490</finishedSqFt><bathrooms>3.0</bathrooms><bedrooms>3</bedrooms><lastSoldDate>03/15/2018</lastSoldDate><lastSoldPrice currency="USD">783000</lastSoldPrice><zestimate><amount currency="USD">791686</amount><last-updated>04/13/2018</last-updated><oneWeekChange deprecated="true"></oneWeekChange><valueChange duration="30" currency="USD">-8370</valueChange><valuationRange><low currency="USD">752102</low><high currency="USD">831270</high></valuationRange><percentile>87</percentile></zestimate><localRealEstate><region name="Minor" id="271921" type="neighborhood"><zindexValue>588,800</zindexValue><links><overview>http://www.zillow.com/local-info/WA-Seattle/Minor/r_271921/</overview><forSaleByOwner>http://www.zillow.com/minor-seattle-wa/fsbo/</forSaleByOwner><forSale>http://www.zillow.com/minor-seattle-wa/</forSale></links></region></localRealEstate></comp></comparables></properties></response></Comps:comps><!-- H:042 T:99ms S:2011 R:Sun Apr 15 09:01:05 PDT 2018 B:5.0.53029.1-hotfix_ADAT-2519.69881bc~hotfix-platform-for-2018-04-11.319b04e -->'
root = ElementTree.fromstring(content)
tree = ElementTree.ElementTree(root)
with open(f"comparables.xml", 'w+') as fp:
tree.write(fp)
However, if I run this (in iPython) I get a TypeError:
In [3]: exec(open('zillow_api.py').read())
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-23b53830497c> in <module>()
----> 1 exec(open('zillow_api.py').read())
<string> in <module>()
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/etree/ElementTree.py in write(self, file_or_filename, encoding, xml_declaration, default_namespace, method, short_empty_elements)
774 serialize = _serialize[method]
775 serialize(write, self._root, qnames, namespaces,
--> 776 short_empty_elements=short_empty_elements)
777
778 def write_c14n(self, file):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
86 if type is None:
87 try:
---> 88 next(self.gen)
89 except StopIteration:
90 return False
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/etree/ElementTree.py in _get_writer(file_or_filename, encoding)
833 # destroyed
834 stack.callback(file.detach)
--> 835 yield file.write
836
837 def _namespaces(elem, default_namespace=None):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__(self, *exc_details)
378 # set-up context
379 fixed_ctx = exc_details[1].__context__
--> 380 raise exc_details[1]
381 except BaseException:
382 exc_details[1].__context__ = fixed_ctx
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in __exit__(self, *exc_details)
363 cb = self._exit_callbacks.pop()
364 try:
--> 365 if cb(*exc_details):
366 suppressed_exc = True
367 pending_raise = False
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py in _exit_wrapper(exc_type, exc, tb)
312 """
313 def _exit_wrapper(exc_type, exc, tb):
--> 314 callback(*args, **kwds)
315 # We changed the signature, so using #wraps is not appropriate, but
316 # setting __wrapped__ may still help with introspection
TypeError: write() argument must be str, not bytes
Is what I'm passing in not a "file object opened for writing" as specified in the docs?
I've tried following the solution in ElementTree TypeError "write() argument must be str, not bytes" in Python3, but the tostring method appears to no longer be contained in the xml.etree module.
It turns out that the argument to tree.write() should just be the name of the file you want to create:
tree.write("comparables.xml")
which creates comparables.xml in my current directory.

Problems with PLY LEX and YACC

I am trying to run the first part of a simple example of the PLY but I encounter a strange error. When I run the following code, it gives me an error regarding lex.lex()
Anyone knows what the problem is?
import ply.lex as lex
tokens = [ 'NAME','NUMBER','PLUS','MINUS','TIMES', 'DIVIDE', 'EQUALS' ]
t_ignore = '\t'
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_EQUALS = r'='
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
lex.lex() # Build the lexer
This is the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-e527bd224769> in <module>()
14 return t
15
---> 16 ply.lex.lex() # Build the lexer
c:\python27\lib\site-packages\ply\lex.pyc in lex(module, object, debug, optimize, lextab, reflags, nowarn, outputdir, debuglog, errorlog)
904 linfo.get_all()
905 if not optimize:
--> 906 if linfo.validate_all():
907 raise SyntaxError("Can't build lexer")
908
c:\python27\lib\site-packages\ply\lex.pyc in validate_all(self)
578 self.validate_tokens()
579 self.validate_literals()
--> 580 self.validate_rules()
581 return self.error
582
c:\python27\lib\site-packages\ply\lex.pyc in validate_rules(self)
820
821 for module in self.modules:
--> 822 self.validate_module(module)
823
824 # -----------------------------------------------------------------------------
c:\python27\lib\site-packages\ply\lex.pyc in validate_module(self, module)
831
832 def validate_module(self, module):
--> 833 lines, linen = inspect.getsourcelines(module)
834
835 fre = re.compile(r'\s*def\s+(t_[a-zA-Z_0-9]*)\(')
c:\python27\lib\inspect.pyc in getsourcelines(object)
688 original source file the first line of code was found. An IOError is
689 raised if the source code cannot be retrieved."""
--> 690 lines, lnum = findsource(object)
691
692 if ismodule(object): return lines, 0
c:\python27\lib\inspect.pyc in findsource(object)
524 is raised if the source code cannot be retrieved."""
525
--> 526 file = getfile(object)
527 sourcefile = getsourcefile(object)
528 if not sourcefile and file[:1] + file[-1:] != '<>':
c:\python27\lib\inspect.pyc in getfile(object)
401 if hasattr(object, '__file__'):
402 return object.__file__
--> 403 raise TypeError('{!r} is a built-in module'.format(object))
404 if isclass(object):
405 object = sys.modules.get(object.__module__)
TypeError: <module '__main__' (built-in)> is a built-in module
You are trying to run ply from some kind of REPL (ipython, at a guess).
For whatever reason, that won't work. Ply insists that the grammar be a module, which means it must be in a file. The error precisely indicates that there was no file associated with the grammar source.
It turned out that the issue is that I was running the code via iPython Notebook and it didn't like it for some reason. Saved the code as a regular .py file and ran it through a command prompt and no errors occurred!
P.S. I appreciate it if anyone can elaborate on why the code doesn't run in an iPython Notebook environment!

Categories

Resources