Reusable Custom Logic

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 2

Reusing Custom Logic

•Why?
• Writing any complex logic in the editor within IQ Bot is going
to be complicated. There is nothing like having your favorite To do this there are a few simple steps
editor to do the heavy lifting.
 Choose where on your server you want to have all your
python files
• You also might want a bunch of logic you have written used
mutiple times e.g. in each form field so after a bit it makes  Make a directory e.g."c:\somewhere\mydir"
sense to write a package, import it and then use in the  put an empty file called "__init__.py" in the directory.
custom logic. This will make python treat this folder as package folder
 Go into control panel
• This has the added benefit that the code in IQ Bot is reduced  Find environment variables and the option to edit them
to the bare minimum, which makes it easier to maintain
for the system (not user)
- for example if you have put logic in a bunch of fields for
100 groups, it won't be fun editing and testing it again.  Add the environment variable PYTHONPATH and set it to
your directory e.g..c:\somewhere\mydir
 Restart IQ Bot services (using
• So we can write our required logic in a separate “.py” and
then import it and use any of the functions i.e. a package. stopanduninstallservices.bat and
installandstartservices.bat just to be sure)

Confidential – do not distribute.


Reusing Custom Logic – Contd.,
Now you are ready to create a package e.g. Making it easy to code your package
create a python file, e.g.helloworld.py in the directory There is one last step to make it so you can run your
create a function or two e.g python file standalone while you are writing it but still
have it importable. Note you will have to set up
def hw():
    print('hello world')
variables you have in IQ Bot in your main function (or
  elsewhere) so you can test similar operation.
def hw2():
    print('hello world again!')
def hw():
    print('hello world')
 
def hw2():
    print('hello world again!')
 
#add new function called main with whatever logic you want to
test and any variables you need to set e.g. field_value
In IQ Bot in a form field (or table settings) go to the logic tab and then def main():
you can do the following:     field_value = 'a test value'
    hw()
    hw2()
import helloworld  
  #this final logic makes sure the code runs standalone but not
helloworld.hw() when you import it into another script or in IQ Bot
helloworld.hw2() if __name__ == '__main__':
      main()
# or if you only need one function
  Also any imports you do in this file will be
from helloworld import hw
hw() available when you import in IQ Bot .

Confidential – do not distribute.

You might also like