ANTLR 4 Python Documentation - python-2.7

I am very new to antlr 4 and my target language is PYTHON2.
I am not able to understand CommonTokenStream in python and how I can access tokens in antlr 4.
What I require is to access tokens present in Hidden Channel ?
Please someone point me to some proper documentation where I can understand how to access tokens and manipulate them in python.
I am sorry if the question is vague, I am new here.

The ANTLR book is one.
https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference
In the chapter 12 "Wielding Lexical Black Magic", it has "Accessing hidden channel" section. Use the TokenStreamRewriter to rewrite the token.
You need to mentally convert Java code in the book to Python code. The runtime libraries have subtle differences but they are virtually the same.
It is not the only way. You can override lexer's emit() function (which I usually do). Then you have total control over the token routing.

If you are on python 3 it is all wonderfully done cooked and ready
https://github.com/jszheng/py3antlr4book
For Some Python start hints try
https://github.com/antlr/antlr4/blob/master/doc/python-target.md
If you are using Anaconda3 try egrep of class def import comments(#) of all *.py
Anaconda3\Lib\site-packages\antlr4_python3_runtime-4.7.1-py3.6.egg\antlr4
Or even write a ANTLR script to create the python docs and share with me and the world
Also at run time this helps to see what methods and properties are in say a CTX object
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
print("-------------------------------------------")
dump(ctx)
print("===========================================")

Related

How to check the methods and attributes of pyomo SolverFactory

I am using the following code to work with pyomo model.
opt = pe.SolverFactory('gurobi')
res = opt.solve(model, tee=False,options=solver_opt)
walltime = res.solver.time
I tried several times to get the code snippet "res.solver.time" for obtaining the "walltime". Hence, I want to know where I can get the full list of methods and attributes in "opt". Then I can obtain other useful information. Thank you for help.
Anytime I'm poking around for a particular feature, I keep a terminal window open with ipython and use the code introspection feature to poke around (input 4 below). Then you can use ? or ?? (deep info, typically the source code) to get more info as in input 3 below.
Just type the command, '.', tab and poke around
This is the same stuff you would get from any IDE that supports introspection based code completion. I like PyCharm for same reasons.
Realize of course that what is shown below are the methods associated with pyomo's opt object and NOT the solver (glpk in my case, gurobi in yours) options!

'TypeError at /api/chunked_upload/ Unicode-objects must be encoded before hashing' errorwhen using botocore in Django project

I have hit a dead end with this problem. My code works perfectly in development but when I deploy my project and configure DigitalOcean Spaces & S3 bucket I get the following error when uploading media:
TypeError at /api/chunked_upload/
Unicode-objects must be encoded before hashing
I'm using django-chucked-uploads and it doesn't play well with Botocore
I'm using Python 3.7
My code is taken from this demo: https://github.com/juliomalegria/django-chunked-upload-demo
Any help will be massively helpful
This library was implemented for Python 2, so there might be a couple of things that don't work out of the box with Python 3.
This issue that you're facing is one of them since files in Python 3 are read directly as Unicode (since now py3's str is py2's unicode). The md5 hashing is the part of the code triggering this exception (this line) because it doesn't expect Unicode strings.
If you have created your own model inheriting from AbstractChunkedUpload, you can override the md5 property to encode the chunks before updating the hash. See this other SO question on how to solve this specific.
Hopefully this helped!
Disclaimer: I'm the creator of this library. However, I haven't maintained it in a long time to the point that it might be no longer usable.

Is there a one line text input box in python 2.7?

Java has a method that opens a text box and pulls the answer:
JOptionPane.showInputDialog(frame, "What's your name?");
Is there a similar easy solution for pulling input text?
Edit: I understand that packages exist and what they are, I'm asking if you know of a package that has a one line solution
In python if you are using Tkinter then you can use
Entry()
Check this link out for more clear guidelines
Vanilla python has no methods for UI interfaces (I believe as any other programming language - Java, C#, C++, %any_other_language% use separate libraries or frameworks to create UI). There is number of libraries for python which are used to draw different user interfaces and which would allow you to do this. You could check this list, pick up the one you like more and read how you could do it there:
https://wiki.python.org/moin/GuiProgramming
On the other hand, the most commonly used one is tkinter. For tkinter, you could check examples here (there is example of creating whole python program - a loan calculator - with a UI)
http://www.python-course.eu/tkinter_entry_widgets.php

Building a Native Client app from nothing

What does it take to build a Native Client app from scratch? I have looked into the documentation, and fiddled with several apps, however, I am now moving onto making my own app and I don't see anything related to creating the foundation of a native client app.
Depending on the version of the SDK you want to use, you have a couple of options.
Pepper 16 and 17: use init_project.py or use an example as a starting point
If you are using pepper_16 or pepper_17, you will find a Python script init_project.py in the project_templates in the SDK. It will setup up a complete set of files (.cc, .html, .nmf) with comments indicating where you need to add code. Run python init_project.py -h to see what options it accepts. Additional documentation can be found at https://developers.google.com/native-client/pepper17/devguide/tutorial.
Pepper 18 and newer: use an example as the starting point
If you are using pepper_18 or newer, init_project.py is no longer included. Instead you can copy a very small example from the examples directory (e.g., hello_world_glibc or hello_world_newlib for C or hello_world_interactive for C++) and use that as a starting point.
Writing completely from scratch
If you want to write your app completely from scratch, first ensure that the SDK is working by compiling and running a few of the examples. Then a good next step is to look at the classes pp::Module and pp:Instance, which your app will need to implement.
On the HTML side, write a simple page with the EMBED element for the Native Client module. Then add the JavaScript event handlers for loadstart, progress, error, abort, load, loadend, and message and have the handlers write the event data to, e.g., the JavaScript console, so that it's possible to tell what went wrong if the Native Client module didn't load. The load_progress example shows how to do this.
Next, create the manifest file (.nmf). From pepper_18 and onwards you can use the generate_nmf.py script found in the tools/ directory for this. If you want to write it from scratch, the examples provide examples both for using newlib and glibc (the two Standard C librares currently supported). See hello_world_newlib/ and hello_world_glibc/, respectively.
If you haven't used a gcc-family compiler before, it is also a good idea to look at the Makefile for some of the examples to see what compiler and linker flags to use. Compiling both for 32-bit and 64-bit right from the beginning is recommended.
Easiest way is to follow the quick start doc at https://developers.google.com/native-client/pepper18/quick-start, in particular steps 5-7 of the tutorial ( https://developers.google.com/native-client/pepper18/devguide/tutorial ) which seems to be what you are asking about.

How to get Python code to work with C++ App?

I have the following python 3 file:
import base64
import xxx
str = xxx.GetString()
str2 = base64.b64encode(str.encode())
str3 = str2.decode()
print str3
xxx is a module exported by some C++ code. This script does not work because calling Py_InitModule on this script returns NULL. The weird thing is if I create a stub xxx.py in the same directory
def GetString() :
return "test"
and run the original script under python.exe, it works and outputs the base64 string. My question is why doesn't it like the return value of xxx.GetString? In the C++ code, it returns a string object. I hope I have explained my question well enough... this is a strange error.
I know everybody says this...but:
Boost has an awesome library for exposing classes to python and getting data to and fro. If you're having problems, and looking for alternatives is an option I'd highly recommend the boost python library of the C interface. I've used them both, boost wins hands down imo.
Py_InitModule() is for initializing extension modules written in C, which is not what you are looking for here. If you want to import a module from C, there is a wealth of functions available in the C API: http://docs.python.org/c-api/import.html
But if your aim is really to run a script rather than import a module, you could also use one of the PyRun_XXX() functions described here: http://docs.python.org/c-api/veryhigh.html
Er... You have to investigate why Py_InitModule returns NULL. Posting the Python code using that module won't help.