I am using click-configfile (an extension of Click) to have it read command line args straight from a config file:
class ConfigSectionSchema(object):
"""Describes all config sections of this configuration file."""
#matches_section("POOL_CONFIG") # Matches multiple sections
class Pool(SectionSchema):
pooluser = Param(type=str)
pool = Param(type=str)
poolpassword = Param(type=str)
class ConfigFileProcessor(ConfigFileReader):
config_files = ["xenbuilder.config"]
config_section_schemas = [
ConfigSectionSchema.Pool
]
CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor.read_config())
class ZenConnection(object):
def __init__(self, pool, pooluser, poolpassword):
self.pool = pool
self.pooluser = pooluser
self.poolpassword = poolpassword
# Pool Connection param args
#click.group()
#click.command(context_settings=CONTEXT_SETTINGS)
#click.option(
'--pool',
help='Pool Server to connect to'
)
#click.option(
'--pooluser',
help='Connecting as Pool User'
)
#click.option(
'--poolpassword',
hide_input=True,
help='Password to authenticate in the Xen Pool'
)
#click.pass_context
def cli(ctx,pool,pooluser,poolpassword):
""" BSD Xen VM Builder """
ctx.obj = ZenConnection(pool, pooluser, poolpassword)
#click.pass_obj
def xen_session(ctx):
logging.info('INFO: Establishing Connection...')
try:
session = XenAPI.Session(str(ctx.pool))
session.xenapi.login_with_password(str(ctx.pooluser),str(ctx.poolpassword))
print("Connection Successful!")
logging.info('INFO: Connection Successful!!')
except:
logging.error("ERROR: Unexpected Error - ", sys.exc_info()[0])
raise
return session
I am making sure to follow as closely to the tutorial found here. However, when I run the code, I constantly get this stack trace:
Traceback (most recent call last):
File "/usr/local/bin/bsdxenvmbuilder", line 9, in <module>
load_entry_point('bsdxenvmbuilder==0.1', 'console_scripts', 'bsdxenvmbuilder')()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 565, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2697, in load_entry_point
return ep.load()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2370, in load
return self.resolve()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2376, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Users/ryankahil/Xen-Builder/bsdxenbuilder.py", line 52, in <module>
#click.pass_context
File "/Library/Python/2.7/site-packages/click/decorators.py", line 115, in decorator
cmd = _make_command(f, name, attrs, cls)
File "/Library/Python/2.7/site-packages/click/decorators.py", line 71, in _make_command
raise TypeError('Attempted to convert a callback into a '
TypeError: Attempted to convert a callback into a command twice.
I know that error is when you are using a decorator incorrectly. But I am not sure how it is incorrect. Can anyone with knowledge of click-configfile be able to provide some guidance on this?
A click function can either be a group (a collection of commands) or a command, it cannot be both. So the problem is in these two lines:
#click.group()
#click.command(context_settings=CONTEXT_SETTINGS)
You will need to remove one of these decorators.
Related
I am trying to create a python script file with multiple functions and pass 3 arguments to the main functions. These parameters are also passed onto other functions with the script. My code looks somewhat like this:
import benepar
import spacy
import json
import gc
import typer
from spacy.tokens import Doc,DocBin
from typing import Tuple, List
from pathlib import Path
def main(nlp_dir: Path,inp_File:Path,out_File:Path):
try:
seg_obj=load_nlp_object(nlp_dir)
print('Loaded nlp obj')
doc_train=DocBin().from_disk(inp_File)
docs=get_list_of_docs(doc_train,nlp_dir)
data=[]
except Exception as e:
print(e)
chunk_count=0
for d in docs:
try:
temp=seg_obj(d)
chunk_count=chunk_count+1
if chunk_count%5 == 0:
seg_obj=load_nlp_object(nlp_dir)
gc.collect()
#Other code
except Exception as e:
print(e)
#Saving linker data
for val in data:
with open(out_File,'a',encoding='utf8') as f:
json.dump(val,f)
f.write(",\n")
print('Data created successfully.')
def load_nlp_object(nlp_dir):
#Code
return nlp
def get_list_of_docs(bin_obj,nl):
n=load_nlp_object(nl)
dcs=list(bin_obj.get_docs(n.vocab))
return dcs
def benepar_split(doc: Doc) -> List[Tuple]:
#Code
return split_indices
if __name__=='__main__':
typer.run(main)
While running I get the following error:
Traceback (most recent call last):
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\Project\create_linker_data.py", line 96, in <module>
typer.run(main)
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\typer\main.py", line 864, in run
app()
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\typer\main.py", line 214, in __call__
return get_command(self)(*args, **kwargs)
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\click\core.py", line 1128, in __call__
return self.main(*args, **kwargs)
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\click\core.py", line 1053, in main
rv = self.invoke(ctx)
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\click\core.py", line 1395, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\click\core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "C:\Users\Shrinidhi\Desktop\Sentiment Analysis\sentiment\lib\site-packages\typer\main.py", line 500, in wrapper
return callback(**use_params) # type: ignore
TypeError: main() got an unexpected keyword argument 'inp_file'
I have debugged by code but I am unable to find the root cause. My command line command is python create_linker_data.py "Entity Linker\\ner_obj\\" "Named Entity Recognition\\sentiment_data\\3\\3.spacy" "Entity Linker\\data\\entity_linker_3.txt"
I'm trying to write a simple application that communicates using RPCs. I'm using python 3.7's xmlrpc.
This is my server code
MY_ADDR = ("localhost", int(sys.argv[1]))
HOST_ADDR = ("localhost", int(sys.argv[2]))
class RpcServer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.port = MY_ADDR[1]
self.addr = MY_ADDR[0]
# serve other hosts using this
self.server = SimpleXMLRPCServer((self.addr, self.port))
self.server.register_function(self.recv_ops)
def run(self):
self.server.serve_forever()
def recv_ops(self, sender, op):
print("Sender ", sender, " sent: ", op)
pass
And this is what I'm using as my client's code
def send_ops(host_addr, op):
# contact the other host using this
proxy_addr = "http://{addr}:{port}/".format(addr=host_addr[0], port=host_addr[1])
client_proxy = xmlrpc.client.ServerProxy(proxy_addr, allow_none=True)
resp = client_proxy.recv_ops(MY_ADDR, op)
...
send_ops(HOST_ADDR, ("d", ii, last_line[ii])) # THE RPC CALL I MAKE
Despite setting allow_none=True, I keep getting this:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "nb.py", line 102, in editor
send_ops(HOST_ADDR, ("d", ii, last_line[ii]))
File "nb.py", line 63, in send_ops
resp = client_proxy.recv_ops(MY_ADDR, op)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xmlrpc/client.py", line 1112, in __call__
return self.__send(self.__name, args)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xmlrpc/client.py", line 1452, in __request
verbose=self.__verbose
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xmlrpc/client.py", line 1154, in request
return self.single_request(host, handler, request_body, verbose)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xmlrpc/client.py", line 1170, in single_request
return self.parse_response(resp)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xmlrpc/client.py", line 1342, in parse_response
return u.close()
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xmlrpc/client.py", line 656, in close
raise Fault(**self._stack[0])
xmlrpc.client.Fault: <Fault 1: "<class 'TypeError'>:cannot marshal None unless allow_none is enabled">
What's tripping my is that the server on the other side actually receives the message (without any None)
Sender ['localhost', 8001] sent: ['d', 4, 'o']
What am I missing here? Any help would be appreciated.
Thanks!
In your server class, add allow_none=True to your SimpleXMLRPCServer instantiation.
self.server = SimpleXMLRPCServer((self.addr, self.port), allow_none=True)
The allow_none and encoding parameters are passed on to xmlrpc.client and control the XML-RPC responses that will be returned from the server.
Flask RESTApi newbie here
I am trying to build a RESTapi service in Flask (and I am trying to save the output as a .txt file) using flask_restful for a code of mine using the pydifact module as follows:
import datetime
from pydifact.message import Message
from pydifact.segments import Segment
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class RestAPI(Resource):
def get(self, ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE):
MSCONS = Message()
def erstellung_datum_zeit(dt_time):
# Needed for the UNB segment
dt_time = dt_time.strftime('%Y%m%d%H%M')
return dt_time
def UNA_UNB_segment(absender_id, empfänger_id, erst_datum, ref):
MSCONS.add_segment(Segment('UNA', ":+.? '"))
MSCONS.add_segment(Segment('UNB', ['UNOC', '3'], [absender_id, '14'], [
empfänger_id, '500'], [erst_datum[2:8], erst_datum[8:]], ref, '', 'TL'))
ERSTELLUNG_DATUM_ZEIT = str(
erstellung_datum_zeit(datetime.datetime.now()))
UNA_UNB_segment(ABSENDER_ID, EMPFÄNGER_ID,
ERSTELLUNG_DATUM_ZEIT, REFERENCE)
result = MSCONS.serialize()
final_result = result
PATH_FOR_TXT = r'C:\Users\kashy\OneDrive\Desktop\Codes\mscons.txt'
textfile = open(PATH_FOR_TXT, 'w')
textfile.write(result)
textfile.close()
return {'result': final_result}
api.add_resource(
RestAPI,
'/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>')
if __name__ == '__main__':
app.run(debug=True)
The ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE should all be user inputs and they should be all in string format.
When I do /RestAPI/<str:ABSENDER_ID>/<str:EMPFÄNGER_ID/<str:ERSTELLUNG_DATUM_ZEIT/<str:REFERENCE>, i get the following error:
C:\Users\kashy\OneDrive\Desktop\Codes\pydifact> & C:/Users/kashy/Anaconda3/envs/py36/python.exe c:/Users/kashy/OneDrive/Desktop/Codes/api.py
Traceback (most recent call last):
File "c:/Users/kashy/OneDrive/Desktop/Codes/api.py", line 44, in <module>
self.url_map.add(rule)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 1401, in add
rule.bind(self)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 730, in bind
self.compile()
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 790, in compile
_build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 779, in _build_regex
convobj = self.get_converter(variable, converter, c_args, c_kwargs)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 738, in get_converter
raise LookupError("the converter %r does not exist" % converter_name)
LookupError: the converter 'str' does not exist
and when I do
/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>, I get the following error:
PS C:\Users\kashy\OneDrive\Desktop\Codes\pydifact> & C:/Users/kashy/Anaconda3/envs/py36/python.exe c:/Users/kashy/OneDrive/Desktop/Codes/api.py
Traceback (most recent call last):
File "c:/Users/kashy/OneDrive/Desktop/Codes/api.py", line 44, in <module>
'/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>')
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask_restful\__init__.py", line 382, in add_resource
self._register_view(self.app, resource, *urls, **kwargs)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask_restful\__init__.py", line 448, in _register_view
app.add_url_rule(rule, view_func=resource_func, **kwargs)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask\app.py", line 98, in wrapper_func
return f(self, *args, **kwargs)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask\app.py", line 1277, in add_url_rule
self.url_map.add(rule)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 1401, in add
rule.bind(self)
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 730, in bind
self.compile()
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 790, in compile
_build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 766, in _build_regex
for converter, arguments, variable in parse_rule(rule):
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 226, in parse_rule
raise ValueError("malformed url rule: %r" % rule)
ValueError: malformed url rule: '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>'
I am totally new to this and just started learning it using the Building a REST API using Python and Flask | Flask-RESTful tutorial.
Can anyone please tell me what is the mistake I am doing?
Your url routes have problem. In the first one, it should be string instead of str and in the second one you have a missing > at the end of int:EMPFÄNGER_ID and int:ERSTELLUNG_DATUM_ZEIT
Correct ones should be:
/RestAPI/<string:ABSENDER_ID>/<string:EMPFANGER_ID>/<string:ERSTELLUNG_DATUM_ZEIT>/<string:REFERENCE>
and
/RestAPI/<int:ABSENDER_ID>/<int:EMPFANGER_ID>/<int:ERSTELLUNG_DATUM_ZEIT>/<int:REFERENCE>
Note: I replaced Ä with A in urls above because that might also cause malformed url rule issue.
I am making a class to handle operations for a DynamoDB database. It is going to be simple database with only one table.
I am getting a NoRegionError() when running my script as python dynamo.db. However, when I just call the class in a jupyter notebook or qtconsole environment or run each of the commands in the script separately in a python shell, I get no such error.
I have added my credentials to the local .aws folder so I'm not sure why this error is occurring and how to debug it. Here is the class I'm working on:
import boto3
class Dynamo(object):
def __init__(self, **kwargs):
self._db_api = 'dynamodb'
self._session = None
self._client = None
self._database = None
self._region_name = kwargs.get('region_name', 'us-west-2')
self._endpoint_url = kwargs.get('endpoint_url', 'http://localhost:8000')
self.cached_table = {}
self.table = None
def connect(self):
self._session = boto3.Session()
print(self._region_name)
print(self._db_api)
if self._session:
self._database = self._session.resource(self._db_api)
def main():
dynamo = Dynamo()
dynamo.connect()
return dynamo
if __name__ == '__main__':
dynodb = main()
And this is the error I'm getting:
Traceback (most recent call last):
File "test.py", line 39, in <module>
dynodb = main()
File "test.py", line 34, in main
dynamo.connect()
File "test.py", line 28, in connect
self._database = self._session.resource(self._db_api)
File "python2.7/site-packages/boto3/session.py", line 389, in resource
aws_session_token=aws_session_token, config=config)
File "python2.7/site-packages/boto3/session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
File "python2.7/site-packages/botocore/session.py", line 824, in create_client
client_config=config, api_version=api_version)
File "python2.7/site-packages/botocore/client.py", line 69, in create_client
verify, credentials, scoped_config, client_config, endpoint_bridge)
File "python2.7/site-packages/botocore/client.py", line 222, in _get_client_args
verify, credentials, scoped_config, client_config, endpoint_bridge)
File "python2.7/site-packages/botocore/args.py", line 44, in get_client_args
endpoint_url, is_secure, scoped_config)
File "python2.7/site-packages/botocore/args.py", line 101, in compute_client_args
service_name, region_name, endpoint_url, is_secure)
File "python2.7/site-packages/botocore/client.py", line 295, in resolve
service_name, region_name)
File "python2.7/site-packages/botocore/regions.py", line 122, in construct_endpoint
partition, service_name, region_name)
File "python2.7/site-packages/botocore/regions.py", line 135, in _endpoint_for_partition
raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.
You should create a DynamoDB client using boto3 templates:
boto3.client('dynamodb', region_name='us-west-2', endpoint_url='http://localhost:8000')
Also see this question and answer.
boto3.session('dynamodb', region_name='us-west-2', endpoint_url='http://localhost:8000')
This will may helpful for you.
I get an exception on both client and server side when I run the first example at http://buildbot.twistedmatrix.com/builds/sphinx-html/291-15849/projects/web/howto/xmlrpc.html. The server code I used is below:
from twisted.web import xmlrpc, server
class Example(xmlrpc.XMLRPC):
"""An example object to be published."""
def xmlrpc_echo(self, x):
"""
Return all passed args.
"""
return x
def xmlrpc_add(self, a, b):
"""
Return sum of arguments.
"""
return a + b
def xmlrpc_fault(self):
"""
Raise a Fault indicating that the procedure should not be used.
"""
raise xmlrpc.Fault(123, "The fault procedure is faulty.")
if __name__ == '__main__':
from twisted.internet import reactor
r = Example()
reactor.listenTCP(7080, server.Site(r))
reactor.run()
Client side is below:
import xmlrpclib
s = xmlrpclib.Server('http://localhost:7080/')
print s.echo('Hello world')
The server side exception is:
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/twisted/web/xmlrpc.py", line 150, in render_POST
d.addCallback(self._cbRender, request, responseFailed)
File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 260, in addCallback
callbackKeywords=kw)
File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 249, in addCallbacks
self._runCallbacks()
File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 441, in _runCallbacks
self.result = callback(self.result, *args, **kw)
--- <exception caught here> ---
File "/usr/lib/python2.6/dist-packages/twisted/web/xmlrpc.py", line 170, in _cbRender
allow_none=self.allowNone)
exceptions.TypeError: dumps() got an unexpected keyword argument 'allow_none'
Client side exception is:
Traceback (most recent call last):
File "./client.py", line 6, in <module>
print s.echo('Hello world')
File "/usr/local/lib/python2.6/dist-packages/xmlrpclib-1.0.1-py2.6.egg/xmlrpclib.py", line 986, in __call__
return self.__send(self.__name, args)
File "/usr/local/lib/python2.6/dist-packages/xmlrpclib-1.0.1-py2.6.egg/xmlrpclib.py", line 1239, in __request
verbose=self.__verbose
File "/usr/local/lib/python2.6/dist-packages/xmlrpclib-1.0.1-py2.6.egg/xmlrpclib.py", line 1037, in request
return self._parse_response(h.getfile(), sock)
File "/usr/local/lib/python2.6/dist-packages/xmlrpclib-1.0.1-py2.6.egg/xmlrpclib.py", line 1136, in _parse_response
p.close()
File "/usr/local/lib/python2.6/dist-packages/xmlrpclib-1.0.1-py2.6.egg/xmlrpclib.py", line 508, in close
self._parser.Parse("", 1) # end of data
xml.parsers.expat.ExpatError: no element found: line 1, column 0
Looks like you have an old version of xmlrpclib?
What version of python are you using?
Where is the xmlrpclib coming from that your xmlrpc server is using, and what version is it?
$ python -v
>>> import xmlrpclib
# /usr/lib/python2.6/xmlrpclib.pyc matches /usr/lib/python2.6/xmlrpclib.py
>>> xmlrpclib.__version__
'1.0.1'
>>> xmlrpclib.dumps((None,), allow_none=True)
'<params>\n<param>\n<value><nil/></value></param>\n</params>\n
i.e. this works for me. Perhaps you are somehow using an old version of xmlrpclib?