Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10_05_solution.py code doesn't work for PlotScribe #7

Open
jacksonhunter opened this issue Jan 14, 2023 · 2 comments
Open

10_05_solution.py code doesn't work for PlotScribe #7

jacksonhunter opened this issue Jan 14, 2023 · 2 comments

Comments

@jacksonhunter
Copy link

Issue Overview

toFile() method from 10_05_solution.py doesn't work with extended classes like PlotScribe

Describe your environment

toDict() for PlotScribe looks like this:

{
'classname': 'PlotScribe',
'color': 'cyan',
'mark': '*',
'trail': '.',
'pos': (0, 0),
'moves': [
['_setDegrees', [135]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]],
['_plotX', [ < function sine at 0x7fc78a4aa4d0 > ]]
],
'x': 0,
'domain': [0, 30]
}

Steps to Reproduce

  1. Added these lines to end of chapter solution:
    scribe3 = PlotScribe(domain=[0, 30], color='cyan')
    scribe3.plotX(sine)
    canvas = CanvasAxis(30, 30, scribes=[scribe, robotScribe, scribe3])

canvas.toFile('solution_file')
2. $ python ./exercise_files/10_05_solution.py

Expected Behavior

Example code should work with example extended scribe class

Current Behavior

TypeError: Object of type function is not JSON serializable

@jacksonhunter
Copy link
Author

jacksonhunter commented Jan 15, 2023

adding a custom JSON encoder class and argument cls=ScribeEncoder to json.dumps fixes it.

class ScribeEncoder(json.JSONEncoder):

def default(self, obj):
    if callable(obj):
        return type(obj).__name__
    # Let the base class default method raise the TypeError
    return json.JSONEncoder.default(self, obj)

@jacksonhunter
Copy link
Author

It's also not discussed that all the local methods have to be made global methods for the getmembers function to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant