Call Python random number generator from MERL |
Post Reply |
Author | ||
edward22243
Major Contributor Joined: 19.Apr.2019 Points: 41 |
Post Options
Thanks(0)
Posted: 12.Sep.2022 at 12:41 |
|
I need a random generator for generating IDs in an specific format. I made a python program because I think there is no random-generator in MERL (please correct me if I am wrong).
I generate a python script and run it, but it crashes. The C:\Program is not recognised ('the Files (x86)' part of the directory is ignored) so I have to use "" brackets. In my windows commandwindow "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\python.exe" "C:\Users\hage0\Documents\MetaEdit+ 5.5\rand_oid.py" works fine but the same command in MERL not. So when I wrap it in cmd /x /c "" then MetaEdit crashes. How to continue with this ? Script: generate_oid_python() $pythonexe = 'C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\python.exe' $save_random = 'C:\Users\hage0\Documents\MetaEdit+ 5.5\rand_oid.py' filename $save_random write 'import random r = lambda x: random.randint(0,x) print("%08x-%04x-%04x-%012x" % (r(16**8), r(16**4), r(16**4), r(16**12))) ' close external 'cmd /x /c ""' $pythonexe '" "' $save_random '""' read /* this CRASHES */ Edited by stevek - 21.Sep.2022 at 11:26 |
||
stevek
MetaCase Joined: 11.Mar.2008 Points: 641 |
Post Options
Thanks(0)
|
|
Define "crashes" - an error message, program just disappears...? If there's an error message, the relevant entry(ies) in meplus0.err would be useful.
Looks like on your last line you have doubled the double quotes before $pythonexe and at the end of the line. That's not correct use of cmd /x /c, which expects each parameter to be separate: just enclose the exe and .py each in double quotes. Python is a pain in how it interacts with the 'DOS' command line (double-byte encoding etc. - normal characters are returned as random Chinese characters). Do you need to use cmd /x /c, or could you just call the python exe? Or have the python program output to a file and read the file, which would allow you to see what encoding python uses and use that encoding in the filename..encoding..read command. You could make a (pseudo-)random number generator in MERL quite easily I think. The algorithms are mostly just modular arithmetic. To keep the seed between requests you could use an internal variable (getVar: / setVar:value:, e.g. internal 'setVar:value: seed 3721' execute.
Edited by stevek - 21.Sep.2022 at 11:25 |
||
stevek
MetaCase Joined: 11.Mar.2008 Points: 641 |
Post Options
Thanks(2)
|
|
So that we can capture all kinds of characters, external..read uses cmd /u /c on Windows to run the command. The /u causes the output of internal commands to a pipe (e.g. back to MetaEdit+) or file to be in Unicode - UTF-16 (little endian, apparently with no BOM). Accordingly, MetaEdit+ expects that format when it reads the result.
External programs may need to be told explicitly to output in UTF-16. How to do that varies widely, but I've underlined the command for that in the examples below. To get the date with Windows PowerShell (5.1):
The black is MERL, blue is cmd, red is the PowerShell command to set output to UTF-16, green is the PowerShell date command, and purple pipes the date to be output as a string without an extra newline at the end. Or to get the date with Python (3.8) on Windows:
The black is MERL, blue is cmd to set Python to use UTF-16 and run Python, green is the Python to output the date and time (note the doubled single quotes on the last green line, since we are already inside a single-quoted MERL string), and purple makes the print command output without an extra newline at the end. |
||
edward22243
Major Contributor Joined: 19.Apr.2019 Points: 41 |
Post Options
Thanks(1)
|
|
Thanks.
For now I made a pseudo-randomgenerator in MERL itself, but this answer is very valuable for any output of a program I may want to use in MERL. This allows for more smart tools to be part of workflow. |
||
Post Reply | |
Tweet |
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |