Jump to content

Recommended Posts

Posted

Step 1. Install Python

Step 2. Open CMD, run "pip install pywin32"

Step 3. Go to your_python_directory\Lib\site-packages\win32com\servers

Step 4. Register python COM server by running script "interp.py"

 

Example 1:

$py = ObjCreate("Python.Interpreter")

$py.exec("import sys")
$py_version = $py.eval("sys.version")

MsgBox(64, "Hello World!", "Hello World from Python " & $py_version)

HW_Python.png.f1ac9eb2c54e19d2bce6f42df8290510.png

 

Example 2:

$py = ObjCreate("Python.Interpreter")

$py.exec("import customtkinter")
$py.exec("import sys")

$py.exec("gui = customtkinter.CTk()")

$py.exec("GUI_WIDTH = 640")
$py.exec("GUI_HEIGHT = 115")
$py.exec("GUI_HORIZONTAL = int((gui.winfo_screenwidth() - GUI_WIDTH) / 2)")
$py.exec("GUI_VERTICAL = int((gui.winfo_screenheight() - GUI_HEIGHT) / 2)")

$py.exec("gui.geometry(f'{GUI_WIDTH}x{GUI_HEIGHT}+{GUI_HORIZONTAL}+{GUI_VERTICAL}')")
$py.exec("gui.resizable(False, False)")

$py.exec("gui.title('Hello World!')")

$py.exec("customtkinter.CTkLabel(gui, text=f'Hello World from Python {sys.version}').pack(expand=1)")

$py.exec("gui.mainloop()")

HW_Python-2.png.8e4d59d42b1685435c4226778558d976.png

Posted (edited)

Thanks @Nisteo,

but can you please explain why do you want to mix the programming languages? Do you have any proper use case in mind?
I see a lack of reasons 🤔 . Because in case I am well experienced with Python, I can do (all) it directly in Python. The same for AutoIt.

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

  Reveal hidden contents
Posted
  On 2/14/2023 at 11:09 AM, SOLVE-SMART said:

Do you have any proper use case in mind?

Expand  

Python has dictionaries, and you can convert JSON with a billion lines into a dictionary with a flick of your finger. Just "json.loads()" and that's it.

In the case of AutoIt, you have to spend time writing your own parser, and then SUDDENLY discover that AutoIt's execution speed does not allow you to write really good parsers for huge JSON at all.

Even for small JSON, perhaps. 🤣

And this is a disaster, because JSON is everywhere these days.

Posted (edited)
  On 2/14/2023 at 12:36 PM, Nisteo said:

In the case of AutoIt, you have to spend time writing your own parser, and then SUDDENLY discover that AutoIt's execution speed does not allow you to write really good parsers for huge JSON at all.

Expand  

I guess you didn't do a research about what is achievable with AutoIt regarding JSON handling, am I right? There are several UDFs which support working with JSONs¹ very well and make it easy. Please have a look at the this UDF wiki page for more information about it.

Don't get me wrong, I am open minded and I like different ways of doing things, but at least in my opinion it's not a reason to use Python within AutoIt.
Hint: I assume that Python is not installed already on the computer of the AutoIt users. That's why I critically ask for use cases. But all fine @Nisteo 😀 .

I still don't see the point why not just using Python for all actions instead of mixing the languages?

  On 2/14/2023 at 2:10 PM, Kovacic said:

Python can run on Windows, Mac, Linux, Android, iOS etc... Also its close to Autoit in language. 

Expand  

AutoIt is only available under Windows that's clear. So what's the point? In case I want to develop cross platform driven, I would not use AutoIt at all.

Anyway, I guess this would lead to a more off topic discussion. That's not the right place as @Nisteo opened the thread in "AutoIt General Help and Support".

Best regards
Sven

¹ @AspirinJunkie, @Jefrey and more JSON enthusiasts.

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

  Reveal hidden contents
Posted

As for me this is interesting as an "proof on concept" that we can do things like this in AutoIt.

Thanks @Nisteo

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 2/14/2023 at 2:43 PM, mLipok said:

As for me this is interesting as an "proof on concept" that we can do things like this in AutoIt.

Expand  

I share your point of view @mLipok regarding the POC 👍 . I try to understand the use cases, nothing more.

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

  Reveal hidden contents
Posted
  On 2/14/2023 at 12:36 PM, Nisteo said:

Python has dictionaries

Expand  

What do you see as the key criteria that distinguish Python dictionaries from AutoIt maps?
 

  On 2/14/2023 at 12:36 PM, Nisteo said:

In the case of AutoIt, you have to spend time writing your own parser, and then SUDDENLY discover that AutoIt's execution speed does not allow you to write really good parsers for huge JSON at all.

Expand  

Yup - for huge data you reach the reasonableness limit for AutoIT-based JSON parsers.
In principle, of course, you can also parse a JSON file in AutoIt with a single function call like json.loads() in Python.
However, with correspondingly less performance.

I made a quick comparison: a 250kb JSON file parsed by Python in ~150-200ms, AutoIt needs ~850ms for this.
With a 2MB file it looks already different: Python needs ~300ms and AutoIt already ~4s.
In short: Yes, for large JSON files you should look for other tools.

Whether it brings you much further to call Python within AutoIt instead of directly executing a Python script, would certainly be worth discussing.
Because I assume that although you can run Python from AutoIt, it does not mean that you can process the JSON data parsed by Python in AutoIt - right?
So you have to process the data in Python anyway.
Whether you write this directly in a Python script or take the detour via AutoIt.
I don't know - I don't really see the use case yet. Maybe a few examples will enlighten my mind a bit more.

Otherwise it is nice to see that Python can play the Uno-Reverse card and use the performance argument against others.
Normally Python-users are forced to listen to this argumentation from the respective supporters of compiled languages.
But even there, I think it's a tedious discussion, since it only looks at a single aspect out of 1000.

In the end, it remains as always: Choose the tool based on the task and therefore I agree with the argument that AutoIt is not very suitable for large JSON data.
With the script here, we now have even more options for AutoIt and I think a fundamental discussion about the pros and cons of Python versus AutoIt would be harming the intent of the thread.
By the way, I really enjoy programming in both.

So thanks for the script - it will definitely prove a usefulness to some.

Posted
  On 2/14/2023 at 5:38 PM, AspirinJunkie said:

What do you see as the key criteria that distinguish Python dictionaries from AutoIt maps?

Expand  

This? 🤣

autoit_warning.thumb.png.df0eaf581b1da682e25eb8cfd0a8acf6.png

Joking aside, the most important thing for me so far is converting JSON to a dictionary.

 

  On 2/14/2023 at 5:38 PM, AspirinJunkie said:

I assume that although you can run Python from AutoIt, it does not mean that you can process the JSON data parsed by Python in AutoIt - right?

Expand  

You can get data from Python (by using "eval" method), but you can't send the result of executing a function or variable from AutoIt to Python for some reason.

 

  On 2/14/2023 at 2:34 PM, Zedna said:

Can you please post some simple/small example for JSON?

Expand  

Let's get some values from the beginning of this json https://api.invidious.io/instances.json?pretty=1

$py = ObjCreate("Python.Interpreter")

$py.exec("from requests import get")
$py.exec("from json import loads")

$py.exec("INVIDIOUS_API = 'https://api.invidious.io/instances.json'")

$py.exec("json_data = loads(get(INVIDIOUS_API).content)")

;Print "yewtu.be"
ConsoleWrite($py.eval("json_data[0][0]") & @CRLF)

;Attempt to print Netherlands flag icon :D
ConsoleWrite($py.eval('json_data[0][1]["flag"]') & @CRLF)

;Print "NL"
ConsoleWrite($py.eval('json_data[0][1]["region"]') & @CRLF)

;Print "2.0"
ConsoleWrite($py.eval('json_data[0][1]["stats"]["version"]') & @CRLF)

;Print "invidious"
ConsoleWrite($py.eval('json_data[0][1]["stats"]["software"]["name"]') & @CRLF)

;Print "2023.02.14-d24b60c"
ConsoleWrite($py.eval('json_data[0][1]["stats"]["software"]["version"]') & @CRLF)

;Print "master"
ConsoleWrite($py.eval('json_data[0][1]["stats"]["software"]["branch"]') & @CRLF)

 

Posted
  On 2/14/2023 at 2:34 PM, SOLVE-SMART said:

I still don't see the point why not just using Python for all actions instead of mixing the languages?

Expand  

I still don't see the point why not just using machine code for all actions instead of creating compilers and interpreters and billion programming languages and transpilers from one language to another and interpreted versions of compiled languages and compiled versions of interpreted languages and more and more... 🤣

For example, people using IronPython instead of C#, Cython instead of C, Brython instead of JavaScript, Jython instead of Java, etc, etc, etc... Because python is language mainly for non-qualified programmers like scientists or home linux users or something.  Mixing easier and harder languages allows people to do complex things right now, instead of wasting 100 years for learning C and another 100 for debugging "Hello World" program.

By the same logic, learning few functions from Python and calling them from Autoit is easier task, than learning whole Python. Rapid development, you know 😎

Posted
  On 2/14/2023 at 7:10 PM, Nisteo said:

Let's get some values from the beginning of this json https://api.invidious.io/instances.json?pretty=1

Expand  

I have placed the pure AutoIt solution (with this UDF) for this next to it for comparison:

#include "JSON.au3"

$sString = FileRead("instances.json")
$aJson_Data = _JSON_Parse($sString)

ConsoleWrite(($aJson_Data[0])[0] & @CRLF)
ConsoleWrite(($aJson_Data[0])[1].flag & @CRLF)
ConsoleWrite(($aJson_Data[0])[1].region & @CRLF)
ConsoleWrite(($aJson_Data[0])[1].stats.version & @CRLF)
ConsoleWrite(($aJson_Data[0])[1].stats.software.name & @CRLF)
ConsoleWrite(($aJson_Data[0])[1].stats.software.version & @CRLF)
ConsoleWrite(($aJson_Data[0])[1].stats.software.branch & @CRLF)

The required time for the script in my case: ~550ms
For comparison: Your modified (so that it reads from the file instead of downloading first) script needs only ~120ms for me.

That means almost a 5x increase in execution speed with your approach!

Posted
  On 2/15/2023 at 6:10 AM, AspirinJunkie said:

That means almost a 5x increase in execution speed with your approach!

Expand  

I have a crazy idea. Python has alternative implementations, right? What if we will use something like PyPy instead of standard Python? PyPy supports JIT compilation and runs multiple times faster, but I've never used it before 🤔

Posted (edited)

I don't expect extreme improvements there, since only few commands have to be parsed here in the concrete case.
The situation is different if you have a loop, for example, and the commands there have to be chased through the (byte code) interpreter again and again.
These are the cases where Pypy can show its strengths.

Anyway, I ran it through Pypy and got an execution speed of ~90ms.
So no major improvement.

If it is concretely only about json, then you can also fall back on other special tools such as jq and get similar performance but save the whole Python overhead::

ConsoleWrite(_jq_run('instances.json', _
  '.[0][0], ' _
& '.[0][1].flag, ' _
& '.[0][1].region, ' _
& '.[0][1].stats.version, ' _
& '.[0][1].stats.software.name, ' _
& '.[0][1].stats.software.version, ' _
& '.[0][1].stats.software.branch'))


Func _jq_run($sFileName, $sJqCommand)
    Local $iPID = Run('jq.exe -r "' & $sJqCommand & '" "' & $sFileName & '"', @ScriptDir, @SW_Hide, 6)
    ProcessWaitClose($iPID)
    Return DllCall('user32.dll', 'BOOL', 'OemToChar', 'str', StdoutRead($iPID), 'str', '')[2]
EndFunc

 

Edited by AspirinJunkie
Posted

I continued my silly experiments. I compiled a python extension (PYD) using the Nim language (Nim has a Python-like syntax, which is good). Then I called python from autoit and imported this compiled extension.

 

mymodule.nim:

import nimpy

proc hello: string {.exportpy.} =
    return "Hello World from Python compiled extension"

autoit:

$py = ObjCreate("Python.Interpreter")

$py.exec("import sys")
$py.exec("sys.path.insert(0, 'D:\\AutoIt Scripts\\Examples\\Nimpy_Demonstration')")

$py.exec("import mymodule")

$py.exec("hello = mymodule.hello()")

$hello = $py.eval("hello")

MsgBox(64, "", $hello)

hello_from_Nim.png.cd48d356c57b3b8f80f14039b0918626.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...