MetaCase Homepage
Forum Home Forum Home > > MetaEdit+
  New Posts New Posts RSS Feed - Call Python random number generator from MERL
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Call Python random number generator from MERL

 Post Reply Post Reply
Author
Message
edward22243 View Drop Down
Major Contributor
Major Contributor
Avatar

Joined: 19.Apr.2019
Points: 41
Post Options Post Options   Thanks (0) Thanks(0)   Quote edward22243 Quote  Post ReplyReply Direct Link To This Post Topic: Call Python random number generator from MERL
    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
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 641
Post Options Post Options   Thanks (0) Thanks(0)   Quote stevek Quote  Post ReplyReply Direct Link To This Post 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.

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
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 641
Post Options Post Options   Thanks (2) Thanks(2)   Quote stevek Quote  Post ReplyReply Direct Link To This Post 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. 
Back to Top
edward22243 View Drop Down
Major Contributor
Major Contributor
Avatar

Joined: 19.Apr.2019
Points: 41
Post Options Post Options   Thanks (1) Thanks(1)   Quote edward22243 Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.05
Copyright ©2001-2022 Web Wiz Ltd.

This page was generated in 0.094 seconds.