How to import from an encrypted file? - python

I'm trying to get a little bit familiar with code obfuscating and package building. For this I made a simple file named "addCode.py" with a function that adds two numbers called "add". I published it to PyPI and to use it I just need to install the package and import the function:
from addCode import add
The next step I would like to make is to obfuscate the code in addCode.py using pyarmor so that someone who imports the package can use it but can't read the source code. I know this does not fully protect the code but I just want it to be good enough to hide it for the regular person. So I got the encrypted code and uploaded it to PyPI but the problem is that now I cannot import the "add" function because it is not possible to find the function in the file (eventhough the function is there, but encrypted).
To resume: is there anyway I can import a function from an encrypted with pyarmor? If not, is there any other encrypting tool that lets me both import functions and run the code without any errors?

Related

'somewhere' gives error : No module named 'where' for my django app

When I import ( like below after doing pip install somewhere ) handle_uploaded_file from 'somewhere library on my linux
machine.)*
from somewhere import handle_uploaded_file
Getting following error when trying to run my django application.)-
Seems I scared previous person that provided a correct answer as it was deleted, but
I'm gonna guess you are following this example. As you can see, there is a clear comment that it is supposed to be imaginary function, i.e. one that you need to implement yourself if you want to use it in the actual code. It's not supposed to refer to somewhere module which does not offer any function of that name.
# Imaginary function to handle an uploaded file.
from somewhere import handle_uploaded_file

Using Python Libraries in Django

![invoice][1]
![python script][2]
[1]:
[2]: https://i.stack.imgur.com/Y6Ebm.png
Hello! I would appreciate if someone could help me with a doubt I have about using Python libraries in Django. I will try to be as clear as possible so here it goes:
In my job I work with invoices that are all saved in a specific directory (PDF files) and they all have the same structure. In my job I am interested in only one specific value in a row which is a number. My job is to extract that value from all the invoices and sum them all. So, I made a python script in which I use the libraries Pandas, os and PDFplumber and it works great. In code.png, you can see the loop I use to extract the row and value that I want by using PDFplumber and then sum all these values. In invoice.png you can see how PDFplumber divides the invoice in rows and in columns.
So, here is the thing: I want to deploy a Django App so that other people in the enterprise can use the python script I use ( they don't know anything about Python programming). So, I would want to deploy a Django app in which they can upload the directory with all the PDF files and then use the python script I show in code.png. But I'm having problems with the logic behind it. My questions are:
Can we use any python libraries in Django ( such as PDFplumber, pandas, etc) ?
Would I put my python script in views.py? (Something like this)
app/views.py
import os
import pdfplumber
import pandas as pd
from django.shortcuts import render
def function(request):
#Use the directory with the images uploaded by the user.
#Python script shown in code.png
# data = response obtained from the python script.
return render(request, 'app/response.html',{"data":data})
Thank you beforehand!
Can we use any python libraries in Django ( such as PDFplumber, pandas, etc) ?
Yes, a Django app is just Python code. You can add whatever python code you want, including import pandas or whatever.
Would I put my python script in views.py? (Something like this)
While you can write the code in the way you describe, I suggest you organize it more logically. The view function should only handle the request and return a response. Any business logic, such as parsing PDF files, should be in a separate file. Organize your code in a way that makes sense for the task you are trying to complete.
Django is just another Python library so you can definitely use other libs together with Django. Adding your code in views.py is a very good start your project. The downside of views.py is that it will block the call until a response is received. If it takes too lonk you may get HTTP timeouts so other solutions can be explored but I would for sure start with the code in views.py.

Jupyter Notebook: Newly added functions not accessible

similar to this issue and this topic related article, I have created my own
package within Jupyter Notebook. I can successfully access module content after the first import of a python file.
However, whenever I'd like to add a new function to the python file, I am unable to access it then within my notebook.
I've tried the following:
- adjust and save the python file online
- delete the old python version and upload a new one
The only thing working was to upload the python file with a different name. But this is not really what I want to achieve :D
Anyone here having an idea how to add new functions with direct access ability?
This is how I import my modules:
import os
import sys
sys.path.insert(0, os.path.abspath('/home/ubuntu/jupyter/src/..'))
from src.parsing import general
general. <-- function list popping up
When you are in a cell with the code/function you want to use, press Shift + Enter to be able to access that code in the next cell.
I solved the issue. It was rather obvious. You need to restart the kernel, so that the newly added methods can be accessed.
Just rerunning the import cell is not sufficient.

Is it possible to import python scripts in Azure?

I have a python script that is written in different files (one for importing, one for calculations, et cetera). These are all in the same folder, and when I need a function from another function I do something like
import file_import
file_import.do_something_usefull()
where, of course, in the file_import there is a function do_something_usefull() that, uhm, does something usefull. How can I accomplish the same in Azure?
I found it out myself. It is documenten on Microsoft's site here.
The steps, very short, are:
Include all the python you want in a .zip
Upload that zip as a dataset
Drag the dataset as the third option parameter in the 'execute python'-block (example below)
execute said function by importing import Hello (the name of the file, not the zip) and running Hello.do_something_usefull()
As reference, there is a similiar answered thread you can refer to, please see Access Azure blog storage from within an Azure ML experiment.

Can a developed Ansible module include or extend an Ansible Core module?

I am developing an Ansible module that generates a url, fetches (like get_url) the tarball at that url from my internal artifactory and then extracts it. I am wondering if there is a way to include or extend the get_url Ansible core module in my module. I can't have this in multiple steps because the url being used is generated from a git hash and requires a multi-step search.
If there isn't a way, I will probably just copy the whole get_url module and use it in my module, but I would like to avoid that.
I'd like to do something like:
module_json_response = module.get_module('get_url').issue_command('url=http://myartifactory.com/my_artifact.tar.gz dest=/path/to/local/my_artifact.tar.gz');
My understanding of Ansible is that it uploads the module in use and executes it, including another module isn't supported or isn't documented.
Thanks in advance for any help.
To quote Michael DeHaan's post here:
Generally speaking, Ansible allows sharing code through
"lib/ansible/module_common.py" to make writing functionality easier.
It does not, however, make it possible for one module to call another,
which has not, to date, really been needed -- that's not entirely
true, we used to have something like this for file and copy until we
got smart and moved the file attribute code into common :)
It seems like since url access is frequent enough we could make a
common function in module common for url downloads -- IF we modify the
get_url code to also use it so we aren't repeating ourselves.
He later followed up with:
You can access the way template works by writing an action
plugin, but it's more involved than writing a simple client module.
+1 to moving get_url code into common, that's come up a few times.

Categories

Resources