Converting Python scripts to APIs - python

I have a Python script that extracts certain consumer product aspects from customer reviews using LinearSVC, but I am trying to convert this script into some sort of API to use for new reviews. Is there an easy way to do this? I am very new to the whole concept of APIs.

An API is just a library you import once it's reachable by the interpreter in your case. So any import in python is you calling on an library/API.
So if you're script is called foobar.py for example, if it is in the same directory as other python files using
import foobar
at the top of your python file should allow you to reference any functions made in your original python script.

Related

ADO how to read build information from python script started from ADO-agent

I am building a pipeline in Azure Devops. I have a Yaml file which is starting a python script on a self-hosted agent. Is there any way for me to read information about the pipeline which started the python script inside of the python script without sending it as arguments?
The information i need is information as:
build.definitionName,
system.teamProjectId,
system.pipelineStartTime
Python can access these variables using os.environ['Name']. As per the documentation, predefined variables are converted into uppercase and any '.' are replace with '_'
For example, to access these variables on a windows OS it should be as simple as:
import os
os.environ['BUILD_DEFINITIONNAME']
os.environ['SYSTEM_TEAMPROJECTID']
os.environ['SYSTEM_PIPELINESTARTTIME']

How to get output data from shell redirection inside a Python script?

Actually, I am creating several utilities to deal with AWS CLI responses, which responds with json.
My whole scripts ecosystem are based in Python, and usually I create tiny utilities that fetches the json data and parses to readable/organized data.
But I have noticed that I am taking so much time to creates scripts, besides hiding the data structure responses, something that may also be useful.
A much more interesting alternative would create a script that just receives the json data from aws cli and with a map as argument, filters down the information to what I need. And I pretend do this by a simple shell redirect (pipe).
Right now, I can do something like that in Node.js script. Following illustrates exactly how I would like to operates in Python:
Unless someone says that does not have a way to redirect shell command output to a python script, I prefer not to maintain just this piece of code in a different language than all the others.
How do I do the shell output redirection to a python script?
You could read sys.stdin in Python. My understanding of the above javascript code is translates to this in Python:
import sys
import json
object_data = json.loads(sys.stdin.read())
print(object_data['description'])
However do note that the AWS API has a Python SDK, so you probably don't have to pipe around these JSON outputs at all.

How to save classes and functions for universal usage in python

I want save some classes and functions that I have wrote for Neural Networks, which I want to use in the future whenever I need them. Is there a way to save functions and classes in some library?
So more precisely, I'm looking for a library (let's call it tools), so that I can do:
save my_function in tool
...
from tool import my_function
The way to do that in Python is to simply save your functions in a separate python file (also called a module, see the official docs).
In your case the custom function code could be saved as the file tool.py.
You can then use the syntax you mentioned:
from tool import my_function
To import this specific function, but only if the file tool.py is actually in the same directory as the session you are importing it to (this is an easy way to add the module to your Module Search path, see the offical documentation).
If you want to use the module in another directory, you can append the path where you saved tool.py to your sys.paths:
import sys
sys.path.append('/usr/dir/customcode/')
Then you can from tool import my_function in the same session, if you have tool.py saved in the directory /usr/dir/customcode/.

When I import a package in my python script, does Python also import the sub package?

I am doing an online Python course and currently learning about modules and packages, but can't seem to get my head around the logic. I have used modules (or maybe they were packages who knows?) before in current job but without really knowing what was happening when I would do import Pandas for example.
By following the course I have created a folder called "MyMainPackage" and in this folder there is another folder called MySubPackage.
In "MyMainPackage":
init.py and my_main_script.py; in my_main_script.py there is a function called main_func()
in "MySubPackage":
init.py and my_sub_script.py; in my_sub_script.py there is a function called sub_func()
In Sublime Text editor I wrote a script called myprogram2.py:
import MyMainPackage
MyMainPackage.my_main_script.main_func()
However, this hasn't worked; when I call python myprogram2.py in Windows Command Prompt I get the following message
Attribute Error: module MyMainPackage has no attribute my_main_script
However, as per the online lecture, what does work is this:
from MyMainPackage import my_main_script
from MyMainPackage.MySubPackage import my_sub_script
my_main_script.main_func()
my_sub_script.sub_func()
Why can't I just import the entire package and access the modules like I have tried to above instead of the way the online lecture does it? I thought it would be the same thing. I am just struggling to understand the logic.

Python how to learn about importing files etc

I'm fairly new to python programming, i'm familiar with the very basic stuff and i'm currently learning about creating function definitions in scripts.
In particular i'm a mac user and i'm using text wrangler to write and run my python programmes.
Now that i have learned how to define basic functions in scripts i have questions which my notes do not seem to answer.
How do i import my definition which is saved in a file on my desktop to use on IDLE? I've tried
import fileaname
in IDLE and it does not work.
Secondly Suppose i create a function A in one script and then another function B in a separate script that depends on A, do i have to import A in the script for B first? Do they need to be saved in the same file?
I appreciate any advice and useful tips.
With import filename , you want to make sure its in the same directory as your original file. Try using this too
import sys
sys.path.append(directory_path)
It seems to be a common issue.

Categories