How can I fix my module system for my coding language? - python

I am making a coding language with Python. I am working on a module system for custom made modules.
The full code is at https://repl.it/#UCYT5040/koolCode and interp.py but the issue I am having is with this code:
for i in keywords:
keyword = i.lower()
if keyword == "print":
for i1 in quotes:
print(i1)
elif keyword == "add":
for i1 in quotes:
quote = i1.lower()
if quote == "python":
modulesInfo["python"] = {"runLang":"python","runpy":"{quote}"}
modules.append("python")
else:
print("koolCode WARN: custom modules not supported/module not found.")
else:
for i1 in modules:
if modulesInfo[i1]["runLang"] == "python":
try:
eval("f\"" + modulesInfo[i1][keyword] + "\"")
except KeyError:
print(f"koolCode ERROR: Module")
I get this error:
Welcome to koolCode console.
Would you like to open a file? y/n
n
A file will not be opened. Type any commands below.
koolCode>add "python"
['add'] ['python']
koolCode>runpy "help()"
Traceback (most recent call last):
File "main.py", line 8, in <module>
noFile()
File "main.py", line 3, in noFile
runString(input("koolCode>"))
File "/home/runner/koolCode/interp.py", line 46, in runString
eval("f\"" + modulesInfo[i1][keyword] + "\"")
File "<string>", line 1, in <module>
NameError: name 'quote' is not defined
How can I do this?

Related

Syntax error with exec call to an object in Python 3

I really don't understand what causes the problem, could someone point it out for me please?
with shelve.open(obj_path) as obj:
for as_num in obj['as_number_list']: # ignore warning, obj['as_number_list'] is a list
temp = charge_as(obj['as_' + str(as_num)]) # temp is an object
as_test = temp # doing like this is ok
print(type(as_test))
exec("as_{}_obj = {}".format(as_num, temp)) # **error here**
And it gives syntax error like this:
<class 'instruments.AS'>
Traceback (most recent call last):
File "...", line 45, in <module>
exec("as_{}_obj = {}".format(as_num, temp))
File "<string>", line 1
as_1_obj = <instruments.AS object at 0x000002A86732E290>
^
SyntaxError: invalid syntax
I tried
exec("as_{}_obj = {}".format(as_num, temp.__dict__))
no error is shown but now as_{}_obj is of class 'dict' instead of class 'instruments.AS'
line 45:
exec("as_{}_obj = temp".format(as_num))

how to export WBlock by using pyautocad

hi all i have this problem when i try to export object through WBlock what is wrong there i'm try to do simple python work (note: i'm beginners :D) .. any help
from pyautocad import Autocad, APoint,utils
import win32com.client
AutoCAD = win32com.client.dynamic.Dispatch("AutoCAD.Application")
acad = Autocad(create_if_not_exists = False)
acad.Visible=True
doc = AutoCAD.ActiveDocument
layersList = doc.Layers
for l in layersList:
object = acad.iter_objects()
if l.name == "0":
pass
else:
for o in object:
if o.ObjectName == "AcDbText":
SelectionSet = doc.SelectionSets.Item(o.ObjectName).Name
directoryN = "C:\\Temp\\{}_{}.dwg".format(l.name,o.TextString)
doc.WBlock(directoryN,SelectionSet)
here what i Get
AcDbText
Traceback (most recent call last):
File "C:/Temp/Exporter.py", line 23, in <module>
doc.WBlock(directoryN,SelectionSet)
File "<COMObject <unknown>>", line 2, in WBlock
pywintypes.com_error: (-2147352571, 'Type mismatch.', None, 2)
thanks
i'm try to export every text as WBlock

Javascript grammar in antlr4 and python

I have used the following link for JavaScript grammar .
https://github.com/antlr/grammars-v4/tree/master/javascript/javascript/Python
i have used antlr4.8 and ntlr4-python3-runtime==4.8. when i use the following code it gives following error .
error:
PS N:\antlr4\sample\py4.8> python main.py test.js
Running
Test started for: test.js
Created parsers
Traceback (most recent call last):
File "main.py", line 25, in <module>
main(sys.argv)
File "main.py", line 20, in main
tree = parser.program()
File "N:\antlr4\sample\py4.8\JavaScriptParser.py", line 880, in program
self.enterRule(localctx, 0, self.RULE_program)
File "C:\Users\root\AppData\Local\Programs\Python\Python38\lib\site-packages\antlr4\Parser.py", line 366, in enterRule
self._ctx.start = self._input.LT(1)
.
.
.
File "N:\antlr4\sample\py4.8\JavaScriptLexer.py", line 919, in sempred
return pred(localctx, predIndex)
File "N:\antlr4\sample\py4.8\JavaScriptLexer.py", line 925, in HashBangLine_sempred
return this.IsStartOfFile()
NameError: name 'this' is not defined
code:
import sys
from antlr4 import *
import JavaScriptLexer
import JavaScriptParser
JSL = JavaScriptLexer.JavaScriptLexer
JSP = JavaScriptParser.JavaScriptParser
class WriteTreeListener(ParseTreeListener):
def visitTerminal(self, node:TerminalNode):
print ("Visit Terminal: " + str(node) + " - " + repr(node))
def main(argv):
input_stream = FileStream(argv[1])
print("Test started for: " + argv[1])
lexer = JSL(input_stream)
stream = CommonTokenStream(lexer)
parser = JSP(stream)
print("Created parsers")
tree = parser.program()
ParseTreeWalker.DEFAULT.walk(WriteTreeListener(), tree)
if __name__ == '__main__':
print("Running")
main(sys.argv)
print("Hello")
What have I done wrong ?
Replace this with self
(ref here). Example:
Change
return this.IsStartOfFile()
to
return self.IsStartOfFile().
Run the script transformGrammar.py, as indicated in documentation.

python sys.exit() error builtins.SystemExit:

import sys
def main():
keystr = input("Enter the Key: ")
key = int(keystr)
if (key <=0) or (key>=25):
print("The key is out of range")
sys.exit()
When I want to terminate with input of key (<= 0 or >= 25), there is an error messgae.
The key is out of rangeTraceback (most recent call last):
File "C:/Users/USER/Desktop/caesarRefactored.py", line 38, in <module>
main()
File "C:/Users/USER/Desktop/caesarRefactored.py", line 11, in <module>
sys.exit()
builtins.SystemExit:
How can I fix it?
I'm running this on wing idle, the error shows. If on the terminal, it works just fine.

open function not returning file descriptor

Python open function does not return file descriptor. Why could this be? I am using python 3.3.
test_file_open.py
#!/usr/bin/env python
import sys
if __name__ == '__main__':
html = ""
hmtl_fd = open("ns_temp.html",'r')
for line in html_fd:
html += line
Output:
Traceback (most recent call last):
File "./test_file_open.py", line 7, in <module>
for line in html_fd:
NameError: name 'html_fd' is not defined
Permissions:
-rwxr-xr-x 1 michael michael 160 Mar 16 23:03 test_file_open.py
There is a typo in a variable name, see hmtl_fd and html_fd.
Replace:
hmtl_fd = open("ns_temp.html",'r')
with:
html_fd = open("ns_temp.html",'r')
Typo: hmtl_fd in place of html_fd

Categories

Resources