Access a hidden library function in Python? - function

So when I was doing coding I came across this:
from hidden_lib import train_classifier
Out of curiosity, is there a way to access the function using the terminal and see what's inside there?

You can use "inspect" library to do that, but it will work only if you have the source code of the "hidden_lib" somewhere on your machine:
>>> import hidden_lib
>>> import inspect
>>> print inspect.getsource(hidden_lib.train_classifier)
Otherwise library will throw the exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\inspect.py", line 701, in getsource
lines, lnum = getsourcelines(object)
File "C:\Python27\lib\inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "C:\Python27\lib\inspect.py", line 529, in findsource
raise IOError('source code not available')
IOError: source code not available
In such a case you need to decompile .pyc file first. To do that you need to go to the:
https://github.com/wibiti/uncompyle2
then download the package, go to the package folder and install it:
C:\package_location> C:\Python27\python.exe setup.py install
Now you can easily find location of the library by typing [1]:
>>> hidden_lib.__file__
Then go to the pointed directory and unpyc the file:
>C:\Python27\python.exe C:\Python27\Scripts\uncompyle2 -o C:\path_pointed_by_[1]\hidden_lib.py C:\path_pointed_by_[1]\hidden_lib.pyc
Sources should be decompiled seccessfully:
# 2016.05.07 17:47:36 Central European Daylight Time
+++ okay decompyling hidden_lib.pyc
# decompiled 1 files: 1 okay, 0 failed, 0 verify faile
# 2016.05.07 17:47:36 Central European Daylight Time
And now you can display sources of functions exposed by hidden_lib in a way I described at the beginning of the post. If you are using iPython you can use also embedded function help(hidden_lib.train_classifier) to do exactly the same.
IMPORTANT NOTE: uncompyle2 library (that I used) works only with Python 2.7, if you want to do the same for Python 3.x you need to find other similar library.

Related

No module named _caffe

_caffe.so is present in the caffe/python/caffe folder
Have set the path variable as export PYTHONPATH=/home/itstudent1/caffe/python:$PYTHONPATH.
make pycaffe was also successful.
I am not understanding what else might be the cause for this error. I am able to import caffe in python.
File
"/home/itstudent1/MajorProject/densecap-master/lib/tools/../../python/caffe/pycaffe.py",
line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \ ImportError: No module named _caffe
It seems like you have two versions of caffe:
one in /home/itstudent1/caffe and another in /home/itstudent1/MajorProject/densecap-master.
While the first version is built and compiled, the later is not and your import looks for _caffe.so in the later.

NLTK Python Extract .txt files from a local folder, file recall issue

I'm writing a simple program on Python/NLTK (Windows PC) for a university exam, I'm frankly new in the world of coding.
I have a folder on my computer called "Reviews" where there are 50 .txt files.
My objective is to extract these files from the folder and recall it; thereafter to create some lists with the files and compare it with some techniques like (as example) FreqDist.
Firstly I did the "import" of nltk, os, PlaintextCorpusReader.
import nltk
from nltk import os
from nltk.corpus import PlaintextCorpusReader
All works. Then I tried to see the content of the folder.
foldercontent = PlaintextCorpusReader("C:\\Users\\Mgmura\\Desktop\\Reviews", '.*', encoding='latin1')
print(foldercontent.fileids())
Also here, all works. The output shows all the 50 .txt files in the folder. So I tried to do something (show sents) with the content of a single .txt file.
foldercontent.sents('it_quattroruote_giulia.txt')
The output shows some sents so it works fine.
Now there's the real issue. If I tried to recall a single file there's a "name error" like below.
> NameError Traceback (most recent call
> last) <ipython-input-1-3dd9ed6446c9> in <module>()
> ----> 1 it_quattroruote_giulia
>
> NameError: name 'it_quattroruote_giulia' is not defined
So the real question is: how I can assign a name to every .txt files and recall it?
Thanks in advance
Marco

Open a html file inside a zip file. Python 3.6. TypeError: 'ZipFile' object is not callable.

I am learning about python and I want to open a html file inside a zip file. I'm reading tutorials and following documentation I found online for python 3.6 but I can't print the content of the news.html document which is inside the John.zip folder
import zipfile
file = zipfile.ZipFile("John.zip", "r")
with file('John.zip') as myzip:
with myzip.open("news.html") as myfile:
print(myfile.read())
When I start without debugging it shows "TypeError: 'ZipFile' object is not callable
But I can't fix this. I tried simpler things such as trying to open it the way you open a .txt file
file=open("John.zip/news.html")
print(file.read())
this didn't work either
What you are doing is storing a ZipFile as an object named it "file". Then, in the line beginning with "with", you're calling it as if it were a function. That is why you received the TypeError, because that object you named "file" is not a callable function.
Get rid of the line where you stored it as "file", and try something like this:
with zipfile.ZipFile('John.zip', 'r') as myzip:
Source:
Python Docs: https://docs.python.org/3/library/zipfile.html#zipfile-objects
I guess you have overridden the builtin function "file" by setting the global variable "file".
just print the file see whats in it.
TypeError: 'ZipFile' object is not callable
I had the similar problem, in my case i was working on google colab and i was getting this error but i updated the tensorflow version to v2.0.0 from v1.5.0 and it worked.
If the problem is not resoöved even after updatíng the tensorflow then try update to higher python version or lower python version sometimes, hope it would be solved.

AttributeERROR:'module' object has no attribute 'labelmap'

I am using ubuntu 14.04.I gited the ssd branch caffe and met the problem below when I bash caffe-ssd/data/VOC0712/create_data.sh.(I named the ssd branch caffe caffe-ssd)
Traceback (most recent call last):
File "/home/lab/caffe-ssd/data/VOC0712/../../scripts/create_annoset.py", line 107, in
label_map = caffe_pb2.LabelMap()
AttributeError: 'module' object has no attribute 'LabelMap'
this is my PYTHONPATH:
lab#lab:~$ echo $PYTHONPATH
/home/lab/caffe-ssd/python
I have also added below words in the file create_annoset.py. But it doesn't seem to work.
sys.path.append("/home/lab/caffe-ssd/python")
I guess maybe problem is that the ssd-branch I've git is original version ssd. I d So I download the ssd-branch zip from github
https://github.com/weiliu89/caffe/tree/ssd
and unzip it.Then I met a new problem when I bash create_data.sh like below:
no module named _caffe
though that I could import caffe in the python shell. I have referred that this problem came up due to the $PYTHONPATH confusion,Then I add the below words in the document caffe_root/scripts/create_annosets.py:
sys.path.insert(0,"YOUR_SSD_BRANCH_CAFFE/python")
all done.

In the NLTK, how to interface to Boxer?

I want to be able to use Boxer as a semantic extractor inside NLTK.
I am testing with the following code:
#!/bin/env python
import nltk
x = nltk.sem.boxer.Boxer()
x.interpret("The capital of Spain is Madrid .")
The failure is the following:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/nltk/sem/boxer.py", line 83, in interpret
d, = self.batch_interpret_multisentence([[input]], discourse_ids, question, verbose)
File "/usr/lib/python2.7/site-packages/nltk/sem/boxer.py", line 140, in batch_interpret_multisentence
drs_dict = self._parse_to_drs_dict(boxer_out, use_disc_id)
File "/usr/lib/python2.7/site-packages/nltk/sem/boxer.py", line 241, in _parse_to_drs_dict
line = lines[i]
IndexError: list index out of range
From the nltk code, I found at http://nltk.org/_modules/nltk/sem/boxer.html#Boxer that in the _parse_to_drs_dict(self, boxer_out, use_disc_id) function, it does a i += 4 that I haven't been able to understand.
Am I feeding something bad to the Boxer?
Did anyone manage to make it work?
Manually debugging step-by-step, the NLTK actually gets the output from candc and boxer.
It seems that the newer version available in GitHub works seamlessly.
In the 2.0.4 code the i += 4 line is probably a bug.
In order to get NLTK working, download the source code from GitHub and python setup.py install it.
Be sure to set CANDCHOME variable to the bin/ dir of your candc and boxer tools, and the models at the previous folder (the path should be $CANDCHOME/../models).