Hi, I am trying to learn how to setup a workflow for my Python3 script.
In my code I have a dict variable named Parameter[“Mode1”].
Anyone that can help me how to be able to add this variable in the yml-file.
Thanks
Hi, I am trying to learn how to setup a workflow for my Python3 script.
In my code I have a dict variable named Parameter[“Mode1”].
Anyone that can help me how to be able to add this variable in the yml-file.
Thanks
Could you describe with a bit more detail (ideally with code) what you’re trying to achieve? I guess something along the lines of creating an output or environment variable from a variable in your Python code?
Hi
This the yml file that I have created.
.github/workflows/python-app.yml
I have some input from a system that I want to have static values in the test.
Parameters is a dictionary, I think, these variables exists
Parameters[“Mode1”]
Parameters[“Mode2”]
Parameters[“Mode3”]
Parameters[“Mode4”]
I’m afraid I can’t see the content of the file here, you’d need to copy or link it.
How to do that depends on how your code receives the input from that other system.
Sorry, try this link
It is Domoticz software.
Do you know how to create a Dict as variable for workflow, then I think it can work.
The error from workflow is that it is missing the variable “Parameters”
Thanks for your help
This is the result for the latest test
OK now I see, I have totally misunderstood workflow.
I need to build a setup file for pytest, it was crashing on flake8, so I removed that one and now I need to understand how to collect output
This looks like a misunderstanding about how environment variables work:
env:
Parameters["Mode1"]: 123
What are you trying to achieve with that? Do you want to set a dict element within your Python code?
Yes, that is what I want to do
That’s not (directly) possible from the workflow. The env
keyword lets you set environment variables, that’s what shows up in Python in os.environ
.
If you do something like this in the workflow:
steps:
- run: python3 test.py
env:
MODE_1: 123
And the test.py
script looks like this:
import os
print(os.environ['MODE_1'])
You’ll see “123” as the output. If you want to set variables via the environment your code needs to read them from there.
ok thanks.
a basic question:
if I have a py-file like this:
"
b=a[“a1”]+a[“a2”]
print(b)
"
Do you know how to write a workflow test for this?
dict a is getting data from the system(Domoticz) and look like this when it has access to the system e.g. {‘a1’: 1, ‘a2’: 2}
now I want to set the dict inside my workflow, to be used during the test and want to see the output, which shall be 3 if I set a[“a1”]=1 and a[“a2”]=2
do you know how to do that?
thanks a lot so far
Sounds like the Domoticz system injects the a
(or Parameters
) variable into your module? If that’s the case you’ll have to do the same during your test setup.
Here’s a minimal (two files) example using unittest
, I assume you can do something similar with pytest if you prefer. unittest
has some nicer ways to do this with unittest.mock.patch()
, but I want to show the principle.
# plugin.py
def func():
return a['x'] + a['y']
# test_plugin.py
import unittest
import plugin
class PluginTest(unittest.TestCase):
def setUp(self):
# this is where the magic happens
plugin.a = {'x': 1, 'y': 2}
def test_func(self):
b = plugin.func()
self.assertEqual(b, 3)
That said, injecting variables like that seems like a questionable design choice to me. It would probably be better to define a class that represents your plugin and pass the parameters to the constructor.