Problems with PLY LEX and YACC - python

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!

Related

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

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)?

Word2Vec error when loading in GoogleNews data

I am following a tutorial here: https://towardsdatascience.com/multi-class-text-classification-model-comparison-and-selection-5eb066197568
I am at the part "Word2vec and Logistic Regression". I have downloaded the "GoogleNews-vectors-negative300.bin.gz" file and I am tyring to apply it to my own text data. However when I get to the following code:
%%time
from gensim.models import Word2Vec
wv = gensim.models.KeyedVectors.load_word2vec_format("/data/users/USERS/File_path/classifier/GoogleNews_Embedding/GoogleNews-vectors-negative300.bin.gz", binary=True)
wv.init_sims(replace=True)
I run into the following error:
/data/users/msmith/env/lib64/python3.6/site-packages/smart_open/smart_open_lib.py:398: UserWarning: This function is deprecated, use smart_open.open instead. See the migration notes for details: https://github.com/RaRe-Technologies/smart_open/blob/master/README.rst#migrating-to-the-new-open-function
'See the migration notes for details: %s' % _MIGRATION_NOTES_URL
---------------------------------------------------------------------------
EOFError Traceback (most recent call last)
<timed exec> in <module>
~/env/lib64/python3.6/site-packages/gensim/models/keyedvectors.py in load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors, limit, datatype)
1492 return _load_word2vec_format(
1493 cls, fname, fvocab=fvocab, binary=binary, encoding=encoding, unicode_errors=unicode_errors,
-> 1494 limit=limit, datatype=datatype)
1495
1496 def get_keras_embedding(self, train_embeddings=False):
~/env/lib64/python3.6/site-packages/gensim/models/utils_any2vec.py in _load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors, limit, datatype)
383 with utils.ignore_deprecation_warning():
384 # TODO use frombuffer or something similar
--> 385 weights = fromstring(fin.read(binary_len), dtype=REAL).astype(datatype)
386 add_word(word, weights)
387 else:
/usr/lib64/python3.6/gzip.py in read(self, size)
274 import errno
275 raise OSError(errno.EBADF, "read() on write-only GzipFile object")
--> 276 return self._buffer.read(size)
277
278 def read1(self, size=-1):
/usr/lib64/python3.6/_compression.py in readinto(self, b)
66 def readinto(self, b):
67 with memoryview(b) as view, view.cast("B") as byte_view:
---> 68 data = self.read(len(byte_view))
69 byte_view[:len(data)] = data
70 return len(data)
/usr/lib64/python3.6/gzip.py in read(self, size)
480 break
481 if buf == b"":
--> 482 raise EOFError("Compressed file ended before the "
483 "end-of-stream marker was reached")
484
EOFError: Compressed file ended before the end-of-stream marker was reached
Any idea whats gone wrong/ how to overcome this issue?
Thanks in advance!

Error in NLTK file

I have installed Anaconda3-4.2.0 for Windows (64 bit) and nltk-3.2.1. While i am running the following code in Jupyter Notebook
`para = "Hello World. It's good to see you. Thanks for buying this book."
import nltk.data tokenizer = nltk.data.load('tokenizers/punkt/PY3/english.pickle') tokenizer.tokenize(para)'
I am getting the following error:
'OSError Traceback (most recent call last)
<ipython-input-1-a87e01558cc4> in <module>()
1 para = "Hello World. It's good to see you. Thanks for buying this book."
2 import nltk.data
----> 3 tokenizer = nltk.data.load('tokenizers/punkt/PY3/english.pickle')
4 tokenizer.tokenize(para)
C:\Anaconda3\lib\site-packages\nltk\data.py in load(resource_url, format, cache, verbose, logic_parser, fstruct_reader, encoding)
799
800 # Load the resource.
--> 801 opened_resource = _open(resource_url)
802
803 if format == 'raw':
C:\Anaconda3\lib\site-packages\nltk\data.py in _open(resource_url)
917
918 if protocol is None or protocol.lower() == 'nltk':
--> 919 return find(path_, path + ['']).open()
920 elif protocol.lower() == 'file':
921 # urllib might not use mode='rb', so handle this one ourselves:
C:\Anaconda3\lib\site-packages\nltk\data.py in find(resource_name, paths)
607 return GzipFileSystemPathPointer(p)
608 else:
--> 609 return FileSystemPathPointer(p)
610 else:
611 p = os.path.join(path_, url2pathname(zipfile))
C:\Anaconda3\lib\site-packages\nltk\compat.py in _decorator(*args, **kwargs)
559 def _decorator(*args, **kwargs):
560 args = (args[0], add_py3_data(args[1])) + args[2:]
--> 561 return init_func(*args, **kwargs)
562 return wraps(init_func)(_decorator)
563
C:\Anaconda3\lib\site-packages\nltk\data.py in __init__(self, _path)
298 _path = os.path.abspath(_path)
299 if not os.path.exists(_path):
--> 300 raise IOError('No such file or directory: %r' % _path)
301 self._path = _path
302
OSError: No such file or directory: 'C:\\nltk_data\\tokenizers\\punkt\\PY3\\PY3\\english.pickle'`
I have downloaded punktword tokenizer in nltk. Why I am seeing this error?Please give me an answer.
It seems tokenizers/punkt/PY3/english.pickle file not exists. You need check it.
NLTK can download pickle file use download function:
import nltk
nltk.download()

Python 3.5 Jupyter Notebook ggplot "name error"

I am getting NameError: name 'unicode' is not defined when using yHat's ggplot library for Python (see full error below) in a Jupyter Notebook, running Python 3.5.2. with the following import statements:
%matplotlib inline
from ggplot import *
The graph still renders, but I would like to eliminate the error, or hide it if its not causing a major conflict.
The full error is below. Many thanks in advance :)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
//anaconda/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
697 type_pprinters=self.type_printers,
698 deferred_pprinters=self.deferred_printers)
--> 699 printer.pretty(obj)
700 printer.flush()
701 return stream.getvalue()
//anaconda/lib/python3.5/site-packages/IPython/lib/pretty.py in pretty(self, obj)
381 if callable(meth):
382 return meth(obj, self, cycle)
--> 383 return _default_pprint(obj, self, cycle)
384 finally:
385 self.end_group()
//anaconda/lib/python3.5/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
501 if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs:
502 # A user-provided repr. Find newlines and replace them with p.break_()
--> 503 _repr_pprint(obj, p, cycle)
504 return
505 p.begin_group(1, '<')
//anaconda/lib/python3.5/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
692 """A pprint that just redirects to the normal repr function."""
693 # Find newlines and replace them with p.break_()
--> 694 output = repr(obj)
695 for idx,output_line in enumerate(output.splitlines()):
696 if idx:
//anaconda/lib/python3.5/site-packages/ggplot/ggplot.py in __repr__(self)
113 Evaluates patsy expressions within the aesthetics. For example, 'x + 1'
114 , 'factor(x)', or 'pd.cut(price, bins=10)')
--> 115 """
116 for key, item in self._aes.items():
117 if item not in self.data:
//anaconda/lib/python3.5/site-packages/ggplot/ggplot.py in make(self)
//anaconda/lib/python3.5/site-packages/ggplot/ggplot.py in apply_axis_labels(self)
269 i, j = self.subplots.shape
270 i, j = int((i - 1) / 2), int(j - 1)
--> 271 ax = self.subplots[i][j]
272 make_legend(ax, legend)
273 elif self.facets.rowvar:
NameError: name 'unicode' is not defined
If you have anaconda installed try installing the code-forge version. I was able to fix this same problem by switching to that version for python 3.5
conda install -c conda-forge ggplot
Hope that helps

Sage hangs when calling Mathematica

I recently installed Sage 6.3 on my Fedora 21 machine. I'm using version 6.3, which is slightly outdated, because it is the most recent thing available in yum's repositories. I also installed Mathematica on the same computer in the hope of being able to call it from within Sage.
Mathematica's terminal interface using the math command works, which according to this reference page should be all that I need. However, when I tell Sage to use mathematica either from the Sage command line or the notebook, Sage hangs. Here's a sample of my interaction with the terminal (the traceback is horrible, as is frequently the case in Sage):
sage: mathematica('hello world')
^CInterrupting Mathematica...
^C---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-1-9208bb19d841> in <module>()
----> 1 mathematica('hello world')
/usr/lib64/python2.7/site-packages/sage/interfaces/interface.pyc in __call__(self, x, name)
197
198 if isinstance(x, basestring):
--> 199 return cls(self, x, name=name)
200 try:
201 return self._coerce_from_special_method(x)
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in __init__(self, parent, value, is_name, name)
1310 else:
1311 try:
-> 1312 self._name = parent._create(value, name=name)
1313 # Convert ValueError and RuntimeError to TypeError for
1314 # coercion to work properly.
/usr/lib64/python2.7/site-packages/sage/interfaces/interface.pyc in _create(self, value, name)
387 def _create(self, value, name=None):
388 name = self._next_var_name() if name is None else name
--> 389 self.set(name, value)
390 return name
391
/usr/lib64/python2.7/site-packages/sage/interfaces/mathematica.pyc in set(self, var, value)
508 cmd = '%s=%s;'%(var,value)
509 #out = self.eval(cmd)
--> 510 out = self._eval_line(cmd, allow_use_file=True)
511 if len(out) > 8:
512 raise TypeError("Error executing code in Mathematica\nCODE:\n\t%s\nMathematica ERROR:\n\t%s"%(cmd, out))
/usr/lib64/python2.7/site-packages/sage/interfaces/mathematica.pyc in _eval_line(self, line, allow_use_file, wait_for_prompt, restart_if_needed)
535 def _eval_line(self, line, allow_use_file=True, wait_for_prompt=True, restart_if_needed=False):
536 s = Expect._eval_line(self, line,
--> 537 allow_use_file=allow_use_file, wait_for_prompt=wait_for_prompt)
538 return str(s).strip('\n')
539
/usr/lib64/python2.7/site-packages/sage/interfaces/expect.pyc in _eval_line(self, line, allow_use_file, wait_for_prompt, restart_if_needed)
890 out = ''
891 except KeyboardInterrupt:
--> 892 self._keyboard_interrupt()
893 raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self)
894 if self._terminal_echo:
/usr/lib64/python2.7/site-packages/sage/interfaces/mathematica.pyc in _keyboard_interrupt(self)
420 e = self._expect
421 e.sendline(chr(3)) # send ctrl-c
--> 422 e.expect('Interrupt> ')
423 e.sendline("a") # a -- abort
424 e.expect(self._prompt)
/usr/lib64/sagemath/site-packages/pexpect.pyc in expect(self, pattern, timeout, searchwindowsize)
914 """
915 compiled_pattern_list = self.compile_pattern_list(pattern)
--> 916 return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
917
918 def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
/usr/lib64/sagemath/site-packages/pexpect.pyc in expect_list(self, pattern_list, timeout, searchwindowsize)
965 raise TIMEOUT ('Timeout exceeded in expect_list().')
966 # Still have time left, so read more data
--> 967 c = self.read_nonblocking (self.maxread, timeout)
968 incoming = incoming + c
969 if timeout is not None:
/usr/lib64/sagemath/site-packages/pexpect.pyc in read_nonblocking(self, size, timeout)
546 raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.')
547
--> 548 r, w, e = select.select([self.child_fd], [], [], timeout)
549 if not r:
550 if not self.isalive():
/usr/lib64/python2.7/site-packages/sage/ext/c_lib.so in sage.ext.c_lib.sage_python_check_interrupt (build/cythonized/sage/ext/c_lib.c:1683)()
/usr/lib64/python2.7/site-packages/sage/ext/c_lib.so in sage.ext.c_lib.sig_raise_exception (build/cythonized/sage/ext/c_lib.c:769)()
KeyboardInterrupt:
sage:
The equivalent in Mathematica's own interface works just fine:
Mathematica 10.1.0 for Linux x86 (64-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= hello world
Out[1]= hello world
In[2]:=
Am I doing something wrong, or is this a bug in Sage? Would downloading and building the latest version (6.7) manually fix the problem?
EDIT:
I am using Mathematica 10.1.0. Could it be that be my problem, i.e. the older version of Sage doesn't know how to handle the newer version of Mathematica?
According to Trac 16703, this problem should be fixed in the latest Sage. I don't personally have a copy of Mathematica to test this on.

Categories

Resources