How to use the python-pyodata module in Python - python

I am having trouble getting my code to retrieve data from an endpoint using pyodata. I created a project in visual studio 2017 and an example url that works on my browser is http://localhost:51701/Striker.svc/Answers I get the correct output even testing using the request library, however my code below to prints out an empty list:
import requests
import pyodata
SERVICE_URL = 'http://localhost:51701/Striker.svc/'
# Create instance of OData client
client = pyodata.Client(SERVICE_URL, requests.Session())
print(client.schema.entity_sets)
I tested other sample urls hosted online and they all print out the entity set so not sure what I'm missing

I maintain the package and we had problems with XML namespaces which we fixed few weeks ago:
https://github.com/SAP/python-pyodata/commit/dd467e6ad58588d8c6edaae6b99967eb8cfe4c7c
I would recommend you to upgrade to the latest version of pyodata. If you still have the problems, you can instruct pyodata to use your namespaces:
https://pyodata.readthedocs.io/en/latest/usage/initialization.html#dealing-with-errors-during-parsing-metadata
You can the namespace URIs at the begging of $metadata
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0" m:MaxDataServiceVersion="3.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="NorthwindModel">
The XML fragment above was obtained from the Microsoft's Northwind service which is supported by default and servers here only the demonstration purpose. In your python code, you would write the following:
from pyodata.v2.model import Config
namespaces = {
'edmx': 'http://schemas.microsoft.com/ado/2007/06/edmx',
'edm': 'http://schemas.microsoft.com/ado/2008/09/edm'
}
custom_config = Config(xml_namespaces=namespaces)
northwind = pyodata.Client(SERVICE_URL, requests.Session(), config=custom_config)

Related

Python ''PRAW'' api library crashes script

I have been working on a python script intended to help moderate a subreddi using 'warnings'. However, i am unable to get it to work,as when i get to the point in the script where it it supposed to utilise the api, the python window just closes. I have tried using more simple scripts,but non of them work. Here is my simpler file(i used it for testing):
import prawcore
import praw
from os.path import isfile
import praw
import pandas as pd
from time import sleep
# Get credentials from DEFAULT instance in praw.ini
reddit = praw.Reddit()
# begining of script
#login
reddit = praw.Reddit(client_id='this is where i put my user agent',
client_secret='this is where i put my secret',
password='this is wher i put my password',
user_agent='PARS (Python-based Advanced Reddit Reprimand System) by u/veryinterestingnut',
username='PicoModBot')
print(reddit.user.me())
And here is my praw.ini file:
# The URL prefix for regular requests.
reddit_url=https://www.reddit.com
# The URL prefix for short URLs.
short_url=https://redd.it
[DEFAULT]
client_id=my id was here
client_secret=this is where i put my pass
user_agent=PARS (Python-based Advanced Reddit Reprimand System) by u/veryinterestingnut
username=PicoModBot
password=my account pass was here
What can i do to try and fix this? Any help would be much appreciated.

Python - IBM Watson Language Translator v3 - uploading content of a file and downloading the result

I'm trying to use the Python SDK for IBM Watson Language Translator v3, testing the beta functionality of translating actual documents. Below is my code:-
from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
API = "1234567890abcdefg"
GATEWAY = 'https://gateway-lon.watsonplatform.net/language-translator/api'
document_list = []
"""The below authenticates to the IBM Watson service and initiates an instance"""
authenticator = IAMAuthenticator(API)
language_translator = LanguageTranslatorV3(
version='2018-05-01',
authenticator=authenticator
)
language_translator.set_service_url(GATEWAY)
submission = language_translator.translate_document(file="myfile.txt", filename="myfile.txt", file_content_type='text/plain', model_id=None, source='en', target='es', document_id=None)
document_list.append(submission.result['document_id'])
while len(document_list) > 0:
for document in document_list:
document_status = language_translator.get_document_status(document)
if document_status.result['status'] == "available":
translated_document = language_translator.get_translated_document(document)
document_list.remove(document)
language_translator.delete_document(document)
A few questions on this:-
When I check the content of 'translated_document', it doesn't actually contain any content. It contains the headers and the HTTP status of the response but no actually translated content
I decided to use CURL to download my uploaded document and instead of the actual content of the .txt file being uploaded for translation, when downloading the translated file via CURL, it appears that the content is the actual file name (myfile.txt) that is being submitted for translation as opposed to the content of the file.
Researching this and looking at the actual IBM Watson Github respository, it appears that I may have to read the content of 'myfile.txt' to a variable and then pass this variable as 'file={my_variable}' when submitting the translation but doesn't this defeat the object of being able to submit the actual documents for translation? How is this different to the conventional service offered?
Can anybody advise me as to what I'm doing wrong? I've tried multiple approaches (writing the value of 'translated_content' to a file) for example but I just don't seem to be able to grab the translated content nor can I seem to actually upload the content of the file to the service, instead I simply appear to submit the filename.
Thanks all
The file parameter of translate_document is supposed to be the actual content to be translated. I realize that's not clear from the documentation, but that's how the service works. So try passing the actual content you want translated in the file parameter.

Unable to load torrent file using raw base64 form and python xmlrpc client

I am trying to load a torrent file into rtorrent using xmlrpc with the following python3 code:
import xmlrpc.client
server_url = "https://%s:%s#%s/xmlrpc" % ('[REDACTED]', '[REDACTED]', '[REDACTED]');
server = xmlrpc.client.Server(server_url);
with open("test.torrent", "rb") as torrent:
server.load.raw_verbose(xmlrpc.client.Binary(torrent.read()),"d.delete_tied=","d.custom1.set=Test","d.directory.set=/home/[REDACTED]/files")
The load_raw command returns without an error (return code 0), but the torrent does not appear in the rutorrent UI. I seem to be experiencing the same thing as from this reddit post, but I am using the Binary class without any luck.
I am using a Whatbox seedbox.
EDIT:
After enabling logging I am seeing
1572765194 E Could not create download, the input is not a valid torrent.
when trying to load the torrent file, however manually loading the torrent file through the rutorrent UI works fine.
I needed to add "" as the first argument:
server.load.raw_verbose("",xmlrpc.client.Binary(torrent.read()),"d.delete_tied=","d.custom1.set=Test","d.directory.set=/home/[REDACTED]/files")
Not sure why, the docs don't seem to show this is needed.

How to download a report as a CSV directly from Salesforce Lightning?

I am creating a Python script for downloading a report from Salesforce as a CSV.
My script is working perfectly fine for Salesforce Classic. However, I need to get it working for Lightning Experience. I'm using the simple-salesforce Python package to access our org. For SF Classic I enter a link that is structured like this: https://my-company.my.salesforce.com/my_report_id?view=d&snip&export=1&enc=UTF-8&xf=csv
The script is basically like this:
from simple-salesforce import Salesforce
import requests
import pandas as pd
import csv
from io import StringIO
sf = Salesforce(username="my_username", password="my_password",
security_token="my_token")
sf_org = "https://my_company.my.salesforce.com/"
report_id = "0000" # Some report id
sf_report_loc = "{0}{1}?view=d&snip&export=1&enc=UTF-8&xf=csv".format(sf_org, report_id)
response = requests.get(sf_report_loc, headers=sf.headers, cookies={"sid": sf.session_id})
new_report = response.content.decode("utf-8")
df = pd.read_csv(StringIO(new_report)) # Save the report to a DataFrame.
Whenever I switch to Lightning, the link is invalid and I get redirected. Is there a way to make this work in Lightning?
Try with isdtp parameter. In classic it was used to force view pages without sidebar or header, for example add isdtp=vw to a random page and see what happens.
https://my_company.my.salesforce.com/00O.....?isdtp=p1&export=1&enc=UTF-8&xf=csv ?
(no idea what's 'p1' but that's what I see in Chrome's download history as part of the report's source URL)

Openstack Rest API send a file with python to a openstack container

I am trying to upload a video file to Openstack container using REST API.
This is my Python code to upload to the server.
res = requests.put(publicURL+'/'+output_container_name+'/'+toUpload,
headers={'X-Auth-Token': token},
files={'file': open(toUpload,'rb')})
All the variables that you see in code are defined. In fact I can see my file uploaded to the container but when I download it, I cannot play the video.
When I open the video file with a text editor I see these headers at the beginning and at the end of the file.
--0b2e78b52dad4b45a43575c1c42b0b9d
Content-Disposition: form-data; name="file"; filename="input_output.mp4"
.
.
. Normal video content
.
.
--0b2e78b52dad4b45a43575c1c42b0b9d--
How can I get rid of these header in the file?
EDIT: Even when I remove the headers manually there is still some differences in the files when I check them with diff. The Differences are not visible visually the number of lines are the same and everything look the same.
Give the Python OpenStack SDK a try.
pip install openstacksdk
The code for uploading a file.
import sys
from openstack import connection
from openstack import profile
from openstack import utils
utils.enable_logging(True, stream=sys.stdout)
prof = profile.Profile()
prof.set_region(prof.ALL, "RegionOne")
conn = connection.Connection(
auth_url='https://my.identity.endpoint/v2.0/',
profile=prof,
username="my_username",
password="my_password")
c = conn.object_store.create_container(name='videos')
obj = conn.object_store.create_object(container=c.name,
name='input_output.mp4',
data=open('input_output.mp4', 'r'),
content_type='video/mp4')
print(obj)
More info that might be helpful too:
http://python-openstacksdk.readthedocs.org/en/latest/
https://pypi.python.org/pypi/openstacksdk

Categories

Resources