Add parameter to __init__ in function - python

Here is the original main.py code
kwargs = {
"ffmpeg": args.ffmpeg,
"rtmpdump": args.rtmpdump,
"swfrender": args.swfrender,
}
with presentation.Downloader(pres, **kwargs) as builder:
that initializes this Downloader class
class Downloader(object):
def __init__(self, presentation, overwrite="-n", ffmpeg="ffmpeg", rtmpdump="rtmpdump", swfrender="swfrender"):
self.presentation = presentation
self.ffmpeg = ffmpeg
self.rtmpdump = rtmpdump
self.swfrender = swfrender
When I add an extra argument overwrite
kwargs = {
"ffmpeg": args.ffmpeg,
"rtmpdump": args.rtmpdump,
"swfrender": args.swfrender,
"overwrite": "-y" if args.overwrite else "-n",
}
with presentation.Downloader(pres, **kwargs) as builder:
class Downloader(object):
def __init__(self, presentation, overwrite="-n", ffmpeg="ffmpeg", rtmpdump="rtmpdump", swfrender="swfrender", overwrite="-n"):
self.presentation = presentation
self.ffmpeg = ffmpeg
self.rtmpdump = rtmpdump
self.swfrender = swfrender
self.overwrite = overwrite
python produces this error
Traceback (most recent call last):
File "infoqscraper/main.py", line 374, in <module>
sys.exit(main())
File "infoqscraper/main.py", line 369, in main
module.main(infoq_client, args.module_args)
File "infoqscraper/main.py", line 191, in main
return command.main(infoq_client, args.command_args)
File "infoqscraper/main.py", line 307, in main
with presentation.Downloader(pres, **kwargs) as builder:
TypeError: __init__() got an unexpected keyword argument 'overwrite'
How do I correctly add an argument?

Related

cdk deployment error: RuntimeError: Cannot read properties of undefined (reading 'bindToGraph')

I am trying to do a cdk deployment, where I am just copying the example from the documentation. See example below (https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_stepfunctions/StateMachineFragment.html#aws_cdk.aws_stepfunctions.StateMachineFragment)
from aws_cdk import (
# Duration,
Stack,
aws_stepfunctions as sfn,
aws_stepfunctions_tasks as tasks,
)
from constructs import Construct
class MyJob(sfn.StateMachineFragment):
def __init__(self, parent, id, *, job_flavor):
super().__init__(parent, id)
choice = sfn.Choice(self, "Choice").when(sfn.Condition.string_equals("$.branch", "left"), sfn.Pass(self, "Left Branch")).when(sfn.Condition.string_equals("$.branch", "right"), sfn.Pass(self, "Right Branch"))
# ...
self.start_state = choice
self.end_states = choice.afterwards().end_states
def start_state(self):
return self._start_state
def end_states(self):
return self._end_states
class CdkStateMachineFragmentStack(Stack):
def __init__(self, scope, id):
super().__init__(scope, id)
# Do 3 different variants of MyJob in parallel
parallel = sfn.Parallel(self, "All jobs").branch(MyJob(self, "Quick", job_flavor="quick").prefix_states()).branch(MyJob(self, "Medium", job_flavor="medium").prefix_states()).branch(MyJob(self, "Slow", job_flavor="slow").prefix_states())
sfn.StateMachine(self, "MyStateMachineFragmentTest",
definition=parallel
)
When deploying, I get the following error:
Traceback (most recent call last):
File "app.py", line 10, in
CdkStateMachineFragmentStack(app, "CdkStateMachineFragmentStack",
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call
inst = super().call(*args, **kwargs)
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/cdk_state_machine_fragment/cdk_state_machine_fragment_stack.py", line 34, in init
sfn.StateMachine(self, "MyStateMachineFragmentTest",
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call
inst = super().call(*args, **kwargs)
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/aws_cdk/aws_stepfunctions/init.py", line 6871, in init
jsii.create(self.class, self, [scope, id, props])
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 336, in create
response = self.provider.create(
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 363, in create
return self._process.send(request, CreateResponse)
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 340, in send
raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
RuntimeError: Cannot read properties of undefined (reading 'bindToGraph')
I tried already what is suggested in the following stack overflow post [https://stackoverflow.com/questions/70553737/cannot-read-properties-of-undefined-reading-bindtograph] getting a different error:
Traceback (most recent call last):
File "app.py", line 10, in
CdkStateMachineFragmentStack(app, "CdkStateMachineFragmentStack",
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call
inst = super().call(*args, **kwargs)
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/cdk_state_machine_fragment/cdk_state_machine_fragment_stack.py", line 34, in init
parallel = sfn.Parallel(self, "All jobs").branch(MyJob(self, "Quick", job_flavor="quick").prefix_states()).branch(MyJob(self, "Medium", job_flavor="medium").prefix_states()).branch(MyJob(self, "Slow", job_flavor="slow").prefix_states())
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call
inst = super().call(*args, **kwargs)
File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/cdk_state_machine_fragment/cdk_state_machine_fragment_stack.py", line 18, in init
self.start_state = choice
AttributeError: can't set attribute
Could you support me?
Thanks,
Pablo

Class inheritance and instantiation - confusion between internal calls to __init__() and __new__() methods

This is a follow-up question to a problem I previously posted that was partially resolved with the answer I received.
The scenario is that I have 1 class that is defined at run time, that inherits from a class that is defined based on a type: MasterData or Transaction, which on its turn, inherits from the BusinessDocument.
The BusinessDocument class needs to inherit from a class Thing which is available through an external module.
The following code has been implemented to create all the classes in the chain:
from owlready2 import *
with onto:
class BusinessDocument(Thing):
#staticmethod
def get_class(doc_type):
switch = {
'MasterData': MasterData,
'Transactional': Transactional
}
cls = switch.get(doc_type, lambda: "Invalid Noun Type")
return cls
def __init__(self, doc_id, location, doc_type, color, size):
self.doc_id = doc_id
self.location = location
self.doc_type = doc_type
self.color = color
self.size = size
#property
def get_location(self):
return self.location
#property
def get_doc_id(self):
return self.doc_id
with onto:
class MasterData(BusinessDocument):
def __init__(self, doc_id, location, color, size):
BusinessDocument.__init__(self, doc_id, location, color, size, 'MasterData')
with onto:
class Transactional(BusinessDocument):
def __init__(self, doc_id, location, color, size):
BusinessDocument.__init__(self, doc_id, location, color, size, 'Transactional')
with onto:
class NounClass():
#staticmethod
def get_class(doc_name, doc_type):
return type(doc_name, (BusinessDocument.get_class(doc_type),
BusinessDocument, ),dict.fromkeys(['doc_id', 'location', 'color', 'size',]))
At run time when I get the doc_name and I get a new class, but when I try to instantiate.
invoice_cls = NounClass.get_class('Invoice', 'Transactional')
my_invoice = invoice_cls('IN-1234', 'New York', 'blue', 'big')
Calls to the type() and mro() methods for the invoice_cls gives me the following information:
DEBUG:app.__main__:Type of from_noun is [(bod_ontology.Invoice, bod_ontology.MasterData, bod_ontology.BusinessDocument, owl.Thing, <class 'object'>)]
DEBUG:app.__main__:Class type is [<class 'owlready2.entity.ThingClass'>]
But then I get an exception thrown related to the __new__() method:
DEBUG:app.__main__:Type of from_noun is [(bod_ontology.BillFromPartyMaster, bod_ontology.MasterData, bod_ontology.BusinessObjectDocument, owl.Thing, <class 'object'>)]
DEBUG:app.__main__:Class type is [<class 'owlready2.entity.ThingClass'>]
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 167, in parse_xml
my_invoice = invoice_cls('IN-1234', 'New York', 'blue', 'big')
--- Logging error ---
Traceback (most recent call last):
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 167, in parse_xml
my_invoice = invoice_cls('IN-1234', 'New York', 'blue', 'big')
TypeError: __new__() takes from 1 to 3 positional arguments but 5 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 1081, in emit
msg = self.format(record)
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 925, in format
return fmt.format(record)
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 664, in format
record.message = record.getMessage()
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 369, in getMessage
msg = msg % self.args
TypeError: must be real number, not TypeError
Call stack:
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1758, in <module>
main()
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 520, in <module>
exit(main())
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 515, in main
query_database()
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 103, in query_database
csv_record = parse_xml(bod_type, date_time, from_lid, tenant, xml_string)
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 174, in parse_xml
log.debug("Got an error [%e]", e)
Arguments: (TypeError('__new__() takes from 1 to 3 positional arguments but 5 were given'),)
Then if I try to make the instantiation call without the arguments, I get a different exception related to the __init__() method, and here is the full :
DEBUG:app.__main__:Type of from_noun is [(bod_ontology.BillFromPartyMaster, bod_ontology.MasterData, bod_ontology.BusinessObjectDocument, owl.Thing, <class 'object'>)]
DEBUG:app.__main__:Class type is [<class 'owlready2.entity.ThingClass'>]
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 167, in parse_xml
my_invoice = invoice_cls()
--- Logging error ---
Traceback (most recent call last):
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 167, in parse_xml
my_obj = MyCls()
TypeError: __init__() missing 4 required positional arguments: 'doc_id', 'location', 'color', and 'size'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 1081, in emit
msg = self.format(record)
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 925, in format
return fmt.format(record)
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 664, in format
record.message = record.getMessage()
File "/Users/cracoras/.pyenv/versions/3.8.1/lib/python3.8/logging/__init__.py", line 369, in getMessage
msg = msg % self.args
TypeError: must be real number, not TypeError
Call stack:
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1758, in <module>
main()
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1752, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 520, in <module>
exit(main())
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 515, in main
query_database()
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 103, in query_database
csv_record = parse_xml(bod_type, date_time, from_lid, tenant, xml_string)
File "/Users/cracoras/PycharmProjects/bod2owl/app/convert_data.py", line 174, in parse_xml
log.debug("Got an error [%e]", e)
Arguments: (TypeError("__init__() missing 4 required positional arguments: 'doc_id', 'location', 'color', and 'size'"),)
This only happens when I inherit from the 'Thing' class. If I remove the inheritance the code runs just fine.
It looks like I am messing up with the instantiation sequence in the class hierarchy.
The problem seems to be caused by the Thing class as I suspected and also confirmed by #quamrana.

APScheduler ValueError: The target callable does not accept the following keyword arguments:

I am running into an error calling add_job with kwargs for a function:
I created a function to create scheduled jobs:
def initial_data_pull():
q.enqueue(initial_pull, timout='3h')
def initial_data_load():
dates_list = date_between_list(start=date(2010, 1, 1), stop=date.today())
naics = ['541611', '541618', '541613',
'541511', '541512', '541513',
'541519', '518210', '541612']
fundings = ['3600', '97DH', '7504', '7505',
'7522', '7523', '7524', '7526',
'7527', '7528', '7529', '7530',
'7570']
for date_item in dates_list:
for fund in fundings:
for naic in naics:
sched.add_job(initial_data_pull,
kwargs={'naics_code': naic,
'funding_agency_id': fund,
'date_signed': date_item})
The function initial_pull looks like:
def initial_pull(naics_code=None, funding_agency_id=None, date_signed=None):
print('Gathering Contracts...')
df = fpds_generic(naics_code=naics_code,
funding_agency_id=funding_agency_id,
date_signed=date_signed)
# Code to Process and load data
The function fpds_generic is a function that goes to fpds and gather's data. I am getting the following error when I run the inital_data_load function.
Traceback (most recent call last):
File "clock.py", line 55, in <module>
initial_data_load()
File "clock.py", line 52, in initial_data_load
sched.add_job(initil_data_pull, kwargs=kw)
File "/home/spitfiredd/anaconda3/envs/g2x_flask/lib/python3.6/site-packages/apscheduler/schedulers/base.py", line 425, in add_job
job = Job(self, **job_kwargs)
File "/home/spitfiredd/anaconda3/envs/g2x_flask/lib/python3.6/site-packages/apscheduler/job.py", line 44, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "/home/spitfiredd/anaconda3/envs/g2x_flask/lib/python3.6/site-packages/apscheduler/job.py", line 175, in _modify
check_callable_args(func, args, kwargs)
File "/home/spitfiredd/anaconda3/envs/g2x_flask/lib/python3.6/site-packages/apscheduler/util.py", line 385, in check_callable_args
', '.join(unmatched_kwargs))
ValueError: The target callable does not accept the following keyword arguments: naics_code, funding_agency_id, date_signed
Why is it saying that the function does not accept those keyword args when it does, and how do I fix this?

Serializing twisted.protocols.amp.AmpList for testing

I have a command as follows:
class AddChatMessages(Command):
arguments = [
('messages', AmpList([('message', Unicode()), ('type', Integer())]))]
And I have a responder for it in a controller:
def add_chat_messages(self, messages):
for i, m in enumerate(messages):
messages[i] = (m['message'], m['type'])
self.main.add_chat_messages(messages)
return {}
commands.AddChatMessages.responder(add_chat_messages)
I am writing a unit test for it. This is my code:
class AddChatMessagesTest(ProtocolTestMixin, unittest.TestCase):
command = commands.AddChatMessages
data = {'messages': [{'message': 'hi', 'type': 'None'}]}
def assert_callback(self, unused):
pass
Where ProtocolMixin is as follows:
class ProtocolTestMixin(object):
def setUp(self):
self.protocol = client.CommandProtocol()
def assert_callback(self, unused):
raise NotImplementedError("Has to be implemented!")
def test_responder(self):
responder = self.protocol.lookupFunction(
self.command.commandName)
d = responder(self.data)
d.addCallback(self.assert_callback)
return d
It works if AmpList is not involved, but when it is - I get following error:
======================================================================
ERROR: test_responder
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/internet/defer.py", line 139, in maybeDeferred
result = f(*args, **kw)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/internet/utils.py", line 203, in runWithWarningsSuppressed
reraise(exc_info[1], exc_info[2])
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/internet/utils.py", line 199, in runWithWarningsSuppressed
result = f(*a, **kw)
File "/Users/<username>/Projects/space/tests/client_test.py", line 32, in test_responder
d = responder(self.data)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1016, in doit
kw = command.parseArguments(box, self)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1717, in parseArguments
return _stringsToObjects(box, cls.arguments, protocol)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 2510, in _stringsToObjects
argparser.fromBox(argname, myStrings, objects, proto)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1209, in fromBox
objects[nk] = self.fromStringProto(st, proto)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1465, in fromStringProto
boxes = parseString(inString)
File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 2485, in parseString
return cls.parse(StringIO(data))
TypeError: must be string or buffer, not list
Which makes sense, but how do I serialize a list in AddChatMessagesTest.data?
The responder expects to be called with a serialized box. It will then deserialize it, dispatch the objects to application code, take the object the application code returns, serialize it, and then return that serialized form.
For a few AMP types. most notably String, the serialized form is the same as the deserialized form, so it's easy to overlook this.
I think that you'll want to pass your data through Command.makeArguments in order to produce an object suitable to pass to a responder.
For example:
>>> from twisted.protocols.amp import Command, Integer
>>> class Foo(Command):
... arguments = [("bar", Integer())]
...
>>> Foo.makeArguments({"bar": 17}, None)
AmpBox({'bar': '17'})
>>>
If you do this with a Command that uses AmpList I think you'll find makeArguments returns an encoded string for the value of that argument and that the responder is happy to accept and parse that kind of string.

SpringPython error following the book: AttributeError: 'module' object has no attribute 'ObjBase'

Well, I bought the book Spring Python 1.1 and I have been facing some problems that I cannot solve. I am going to write the code of each file in order to make sure everything is clear. If some of you know what is the problem, please let me know because I am desperate.
simple_service.py
class Service(object):
def happy_birthday(self, name):
results = []
for i in range(4):
if i <= 2:
results.append("Happy birthday dear %s!" % name)
else:
results.append("Happy birthday to you!")
return results
simple_service_server_ctx.py
from springpython.config import *
from springpython.remoting.pyro import *
from simple_service import *
class HappyBirthdayContext(PythonConfig):
def __init__(self):
PythonConfig.__init__(self)
#Object
def target_service(self):
return Service()
#Object
def service_exporter(self):
exporter = PyroServiceExporter()
exporter.service = self.target_service()
exporter.service_name = "service"
exporter.service_host = "127.0.0.1"
exporter.service_port = "7766"
exporter.after_properties_set()
return exporter
simple_server.py
from springpython.context import *
from simple_service_server_ctx import *
if __name__ == "__main__":
ctx = ApplicationContext(HappyBirthdayContext())
ctx.get_object("service_exporter")
I run on a terminal: python simple_server
and then I got the following error:
(spring)kiko#kiko-laptop:~/examples/spring$ python simple_server.py
Traceback (most recent call last):
File "simple_server.py", line 6, in <module>
ctx = ApplicationContext(HappyBirthdayContext())
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/context/__init__.py", line 45, in __init__
self.get_object(object_def.id, ignore_abstract=True)
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/container/__init__.py", line 80, in get_object
comp = self._create_object(object_def)
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/container/__init__.py", line 129, in _create_object
self._get_constructors_kw(object_def.named_constr))
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/factory/__init__.py", line 62, in create_object
return self.method()
File "<string>", line 2, in service_exporter
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/config/__init__.py", line 1370, in object_wrapper
return _object_wrapper(f, theScope, parent, log_func_name, *args, **kwargs)
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/config/__init__.py", line 1350, in _object_wrapper
return _deco(f, scope, parent, log_func_name, *args, **kwargs)
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/config/__init__.py", line 1345, in _deco
results = f(*args, **kwargs)
File "/home/kiko/examples/spring/simple_service_server_ctx.py", line 22, in service_exporter
exporter.after_properties_set()
File "/home/kiko/.virtualenvs/spring/lib/python2.6/site-packages/springpython/remoting/pyro/__init__.py", line 58, in after_properties_set
pyro_obj = Pyro.core.ObjBase()
AttributeError: 'module' object has no attribute 'ObjBase'
I have added on my own the line (file:simple_service_server_ctx.py):
exporter.after_properties_set()
since I read that it must be declared (line 19, link to source code).
Thanks in advance.
I wonder what your Pyro version is. Here using Pyro 3.9.1-1 from Ubuntu 10.04 I have no problems with running your code. Could it be that you're using Pyro 4.x which if I recall correctly was released after the book had been published?

Categories