Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi line Python code prevent escape
#1
In the code of the following post, because the Python code is only a few lines, it can be executed successfully

https://www.quickmacros.com/forum/showth...4#pid34764

But if there are many lines of Python code, there will be many symbols to escape, which will be very troublesome. For example, the following code:

C# code:
// script "ipy_no.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;

ScriptEngine engine = Python.CreateEngine();

string code = @"# -*- coding: UTF-8 -*-
import httplib
import urllib
import socket
import time
 
params = dict(
    login_token="31e250c75", # replace with your API token
    format="json",
    domain_id=79051040, # replace with your domain_id, can get it by API Domain.List
    record_id=603739834, # replace with your record_id, can get it by API Record.List
    sub_domain="help", # replace with your sub_domain
    record_line="默认"
)
current_ip = None

def ddns(ip):
    params.update(dict(value=ip))
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
    conn = httplib.HTTPSConnection("dnsapi.cn")
    conn.request("POST", "/Record.Ddns", urllib.urlencode(params), headers)
    
    response = conn.getresponse()
    print(response.status, response.reason)
    data = response.read()
    #encode_utf8(data)
    print(data)
    conn.close()
    return response.status == 200

def getip():
    sock = socket.create_connection(('ns1.dnspod.net', 6666))
    ip = sock.recv(16)
    sock.close()
    return ip

ip = getip() 
print(ip)
ddns(ip)"

var paths = engine.GetSearchPaths();
paths.Add(@"C:\Users\Administrator\Desktop\IPY\Lib");
engine.SetSearchPaths(paths);

engine.Execute(code);


}
}

In qm2, it is very simple to prevent escape multiple lines of code,  For example, the following code, in qm3, Is there any good solution? Thanks in advance

Macro Macro3
Code:
Copy      Help
str code = 
;# -*- coding: UTF-8 -*-
;import httplib
;import urllib
;import socket
;import time
;;
;params = dict(
;;;;;login_token="31e250c75", # replace with your API token
;;;;;format="json",
;;;;;domain_id=79051040, # replace with your domain_id, can get it by API Domain.List
;;;;;record_id=603739834, # replace with your record_id, can get it by API Record.List
;;;;;sub_domain="help", # replace with your sub_domain
;;;;;record_line="默认"
;)
;current_ip = None
;
;def ddns(ip):
;;;;;params.update(dict(value=ip))
;;;;;headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
;;;;;conn = httplib.HTTPSConnection("dnsapi.cn")
;;;;;conn.request("POST", "/Record.Ddns", urllib.urlencode(params), headers)
;;;;;
;;;;;response = conn.getresponse()
;;;;;print(response.status, response.reason)
;;;;;data = response.read()
;;;;;#encode_utf8(data)
;;;;;print(data)
;;;;;conn.close()
;;;;;return response.status == 200
;
;def getip():
;;;;;sock = socket.create_connection(('ns1.dnspod.net', 6666))
;;;;;ip = sock.recv(16)
;;;;;sock.close()
;;;;;return ip
;
;ip = getip()
;print(ip)
;ddns(ip)
#2
Maybe C# 10 will have this or similar proposal implemented.
https://github.com/dotnet/csharplang/issues/4304

Now, if the string is big, maybe better let it be in a file, which can be added to the QM3 workspace and edited in QM3. Also then it can be added to resources and loaded from there as string.

Or create a script to replace all " with "" or '.


Forum Jump:


Users browsing this thread: 1 Guest(s)