Json to string and NameError: name 'HamiltonServerHandler' is not defined

[Background Context]
Hello I’m relatively new to PyHamilton but I’ve taken a crack at automating checksum validation .hsl files. As I’m deploying updated methods onto different hamiltons I want to replace the .hsl file’s layout with the proper local version with its own teaching. While I’m able to do this through python I get the dreaded checksum validation error. To work around this you can use the hamilton util library and call a add checksum function. So I wanted to add this to pyhamilton to make this a seamless process of when I deploy to a new hamilton I can then immediately apply a check sum to this new modified .hsl but I’ve run into issues.

[Issue]
I’ve followed the tutorial thread on how to implement new modules [attached photos] however it seem I’m having an issue with the GetStringPropery of the HSLJsonLib as when I call and the trace my output I get nothing back. I don’t know if this is a python code side problem or my setup in method editor I’ve attached snippets of each code. If anyone knows what I might be missing please let me know.




image
image

Here is the traceback :slight_smile:

C:/Users/sgellner/Desktop/RepoHam/ProDevHamilton/Ham-08 Mothra/Methods/Pooling/Pooling_From_Plates_384toTrough_withStepsJULY23.hsl
Traceback (most recent call last):
File “P:\GHam\trace recovery\robot_method.py”, line 55, in
main()
File “P:\GHam\trace recovery\robot_method.py”, line 49, in main
pyrepo_CheckSum(ham_int, input_file_path)
File “C:\Users\sgellner\AppData\Roaming\Python\Python39\site-packages\pyhamilton\liquid_handling_wrappers.py”, line 21, in pyrepo_CheckSum
cmd = ham.send_command(PR_CHECKSUM, hslFile)
File “C:\Users\sgellner\AppData\Roaming\Python\Python39\site-packages\pyhamilton\interface.py”, line 613, in send_command
self._block_until_sq_clear()
File “C:\Users\sgellner\AppData\Roaming\Python\Python39\site-packages\pyhamilton\interface.py”, line 681, in _block_until_sq_clear
while HamiltonServerHandler.has_queued_cmds():
NameError: name ‘HamiltonServerHandler’ is not defined

I’m looking at the source code now and it seems like interface.py has an error where the ‘HamiltonServerHandler’ isn’t defined in the correct scope:
image
@Stefan can probably speak to this issue better.

In the meantime in your code try running:
cmd = ham.send_command(PR_CHECKSUM, hslFile, block_until_sent=False)

That added argument is what determines if the problem line of code runs. It’s supposed to default to False though and already shouldn’t be running in your current code, so there might be something wrong with your build if this doesn’t fix it.

2 Likes

I’ll check this out, thanks for the heads up.

@Gellator There’s almost no reason to change the hsl file directly unless you’re changing the API itself or some core PyHamilton functionality. In that case there is little reason for the checksum to change from robot to robot.

If you are just trying to change the layfile, you don’t have to modify the pyhamilton library. Instead, run this inside your robot method:

lmgr = LayoutManager('deck.lay')

That will actually result in deck.lay becoming the layfile referenced by the universal method.

Otherwise, you have done an excellent job breaking down the data flow of PyHamilton and almost everything is in place for a working API call. The only thing missing is in your wrapper function, you are not calling wait_on_response. Check out the other wrappers for how this is implemented.

Final thought: I am not entirely sure having a checksum update function in the API is appropriate given that I think your use case should be handled in a different way ie with LayoutManager. Let me know if you have thoughts on other scenarios that could use it.

1 Like

To clarify I’m not trying to change the layfile on the current run. What I’m trying to do is use this robot_method.py to add a checksum to a unrelated .hsl file. As when I deploy to our manufacturing dept. hamiltons they want to maintain checksum verification.

I’m able to do this by just running a method that just adds a checksum to a file I’m just trying to integrate this with pyhamilton so when someone deploys they can automatically have pyhamilton run and add the checksum.

1 Like

Ahhhh ok. This is a totally new use-case then.

In that case I think you have the exact right approach and just need to add the wait_on_response call.

Let me know if you have any questions!

Unfortunately I think this might be a deeper issue.

I tried @STomp method of adding the block_until_sent=False that resulted in a
TypeError: send_command() got multiple values for argument ‘block_until_sent’

Then I tried out adding the wait_on_response with and without @STomp recommendation and I either get the same
NameError: name ‘HamiltonServerHandler’ is not defined
or I get the
TypeError: send_command() got multiple values for argument ‘block_until_sent’

Here is my wrapper with the changes you recommended @Stefan

Hi,

Sorry this took me a bit to puzzle out. It’s just a weird little syntax problem that is not easy to spot.

You just have to change
cmd = ham.send_command(PR_CHECKSUM, hslFile)

to

cmd = ham.send_command(PR_CHECKSUM, FileHSL = hslFile)

This is because send_command processes keyword arguments differently from non-keyword arguments. Basically hslFile in your example is getting mapped onto the block_until_sent argument, which is interpreted as True. Another issue is that the block_until_sent logic doesn’t even work because HamiltonServerHandler is out of scope. I’ll try to figure out how to correctly rewrite this but for now it shouldn’t be used.

Sorry for the confusion!

1 Like

Thank your for spending the time to sus out this issue. I’ll find out Monday if this solves the issue.

sadpepe
Unfortunately, that has not solved the issue as now there is this new error within the interface.py
File “C:\Users\sgellner\AppData\Roaming\Python\Python39\site-packages\pyhamilton\interface.py”, line 114, in assert_valid_cmd
raise ValueError(‘\n’.join(prints))
ValueError: Assert valid command “pyRepo_CheckSum” failed: template parameter keys (left) do not match given keys (right)

FileHSL       FileHSL
id            command
              id

my code:

I really do appreciate all the help sometimes things just be funky.

1 Like

(He-Man I Have The Power - YouTube)
Well in classic coding fashion I have terrible spelling ability and had typed in the wrong thing. As the code side did need the changes you provided the other issue was with my json string in the method editor command. As prior I had “Input_HSL” and FileHSL on the code side. When I converted “FileHSL” to “Input_HSL” all was good and worked great. Unfrotunately I still get HamiltonStepError issues however the .hsl file is now being checksummed using pyhamilton.

1 Like

Sweet! Yeah that’s a common error. Fixing HamiltonStepError will probably entail fixing how the response JSON is formatted for your new command.

One more note: I strongly recommend not modifying pip installed code in site-packages down the line. If you want to take an existing Python package, modify it, and then redeploy the new code on other machines (or even just use version control) do this instead:

From the command line
git clone https://github.com/dgretton/pyhamilton
cd pyhamilton
pip install -e .

The last line with -e is a project install, which links the given directory to your site-packages. Now you can modify that repo and the changes will instantly be reflected in your Python packages without any extra work, similar to how you’ve been doing it. But additionally you will also be able to commit these changes to your own fork of the repo and potentially merge them back into the main repo. This will help you massively as you change and redeploy code.

To instantiate the changes you’ve already made within the project install context I just described, clone the repo as described above and then take the pyhamilton folder from site-packages and paste this into the pyhamilton repo you cloned. Your directory from site-packages is going to become the directory named pyhamilton inside the repo, not the repo itself (also named pyhamilton). Finally, run pip uninstall pyhamilton to uninstall from site-packages and then pip install -e . inside the repo.

Let me know if you have any questions about this.

Thank you for the advice I dont think down the line we plan on having multiple computers with this script. As we will probably just run a single server that we remote into to run this.

1 Like

When I do get everything ironed out with a nice GUI I’ll be sure to share this in the #equipment-integration and the #pylabrobot-pyhamilton categories. As this solves an issue many were talking about when wanting to roll out updates to methods across multiple Hamiltons at once.

1 Like