Print Page | Close Window

Call Python random number generator from MERL

Printed From: MetaCase
Category:
Forum Name: MetaEdit+
Forum Description: All topics relating to MetaEdit+ or DSM
URL: http://www.metacase.com/forums/forum_posts.asp?TID=950
Printed Date: 19.Apr.2024 at 11:46
Software Version: Web Wiz Forums 12.05 - http://www.webwizforums.com


Topic: Call Python random number generator from MERL
Posted By: edward22243
Subject: Call Python random number generator from MERL
Date 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 */




Replies:
Posted By: stevek
Date Posted: 12.Sep.2022 at 12:55
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.

https://www.metacase.com/forums/forum_posts.asp?TID=915&PID=2321&title=python-c-text-not-correct#2321" rel="nofollow - 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 ( https://www.metacase.com/support/55/manuals/mwb/Mw.html#Mw-10_2.html" rel="nofollow - getVar: / setVar:value: , e.g. internal 'setVar:value: seed 3721' execute.


Posted By: stevek
Date Posted: 28.Sep.2022 at 19:32
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):
external 
  '
    powershell 
      "
        [Console]::OutputEncoding = [System.Text.Encoding]::Unicode; 
        get-date -format {dddd d MMMM yyyy HH:mm} | write-host -nonewline
      "
  ' 
read
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:
external 
'
set PYTHONIOENCODING=utf_16 && python -c
"
from datetime import datetime; 
n = datetime.now(); 
print(n.strftime(''%A %d %B %Y %H:%M''), end='''');
"
read
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. 


Posted By: edward22243
Date Posted: 29.Sep.2022 at 06:43
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.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.05 - http://www.webwizforums.com
Copyright ©2001-2022 Web Wiz Ltd. - https://www.webwiz.net