New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added functionality to run hedy files locally #120
Conversation
Now changed to defining level in code file ( |
Hi @Sven-Developer. Thank you for your contribution! We'll be reviewing the PR shortly :). |
@Sven-Developer I tried it locally and it works well! Thanks for this contribution! Could you add a small paragraph here (https://github.com/Felienne/hedy/blob/master/CONTRIBUTING.md#run-hedy-code-on-your-machine) in your PR to describe how to use this functionality? It'd be similar to what you specified in the PR (pass the path to the hedy file as an argument to the command, and specify the level at the top of the file with a line of the form #level N). Am not sure why the tests are not passing. Will discuss with @Felienne how to figure this out and as soon as that's cleared out we can proceed to merging. Will keep you updated! |
I dont know either why the test are not passing. I will make a description how to use it. |
I have updated the contributing file with the new feature |
Thank you for the docs! I'll see if I can fix the test issue - I have the same error now locally so I should be able to get to the bottom of it. |
So I thought i saw the problem, because i edited the excecute function with a extra argument, I thought it ran into a problem there (for example other files that use that same function will fail the excecution because there is a extra argument.). So I copied the function and made it a seperate one so it would not complain about that. Still not fixed the error... |
@Sven-Developer I tested again and it's not the naming. The strange thing is that As soon as I clear up some urgent UI fixes, I'll be back to this to see how we can make the tests pass. I'll keep you updated. |
@fpereiro @Sven-Developer, ik heb uitgevonden waarom de code het niet doet. Het komt omdat je in if len(sys.argv) > 1:
LEVEL = 1
FILE = sys.argv[1]
...enzovoort... Die code wordt uitgevoerd als je
Intypt. Maar diezelfde code wordt óók uitgevoerd als iemand anders een ander bestand uitvoert, waarin
staat. Bijvoorbeeld in https://github.com/Felienne/hedy/blob/b4359bf202c952fd1d946e5a91a57abcbe2991b7/app.py#L5 En ook in https://github.com/Felienne/hedy/blob/b4359bf202c952fd1d946e5a91a57abcbe2991b7/tests.py#L2 Dus als je gewoon code typt in de linkerkantlijn, en iemand doet dan:
Dan wordt Jouw In dit geval zit daar het volgende in:
En wat gebeurt er nu? Jouw stukje code denkt dat Dus gaat die nu vrolijk Lange uitleg, maar hoe fixen we dat nu? Het problem is dat Dus je wil eigenlijk dat jouw code ALLEEN uitgevoerd wordt in het eerste geval, en niet in het tweede. Dat doe je door het volgende voor je code te zetten: if __name__ == '__main__':
# hier komt de code die uitgevoerd moet worden als Hedy als los programma draait
... Zoals je ziet is dit nogal foutgevoelig, en heel verwarrend als het misgaat. Het kán dus wel, maar is misschien niet zo handig om te doen. Ik zou je aanraden om een nieuw script te maken om lokaal Hedy programma's uit te voeren. Misschien noem je dat bijvoorbeeld En dan laat je |
Ah kijk, dat verklaart een hele hoop, ik zal morgen een nieuwe commit doen waarin ik het zal oplossen. Dank voor het meedenken |
@Sven-Developer ik heb ook even de vrijheid genomen om je code een beetje te herschrijven. Ik hoop dat je dat goed vindt. Ik zal je meteen uitleggen waarom ik dat gedaan heb: # 1️⃣
def main():
# 2️⃣ argv0 is the name of the script, so let's cut that off to
# make the indices easier to reason about.
args = sys.argv[1:]
# 3️⃣ Let's be nice to the user and explain how this program is supposed
# to be used if they're doing it wrong.
if len(args) == 0 or args == ['-h'] or args == ['--help']:
sys.stderr.write('Run a Hedy source file.\n')
sys.stderr.write('\n')
sys.stderr.write('Usage: hedy.py FILE\n')
sys.stderr.write('\n')
sys.stderr.write('Select a level by putting "#level <number>" on the first line by itself.\n')
# 8️⃣
sys.exit(1)
# 4️⃣
level = 1
filename = args[0]
# 5️⃣ Use the 'with' block to close the file as soon as we don't need
# it anymore.
with open(filename, 'r') as f:
lines = f.readlines()
# Look for '#level <number>' on the first line
if lines[0].lower().startswith('#level'):
parts = lines[0].split(' ')
if len(parts) >= 2:
level = int(parts[1])
# 6️⃣ Keep only all lines that do NOT start with '#' (those are comments),
# then join them back to a big string.
program = '\n'.join([line
for line in lines
if not line.startswith('#')])
try:
execute_locally(program, level)
except HedyException as e:
# 7️⃣ If we have a Hedy parsing exception, print it slightly nicer than
# before.
sys.stderr.write('An error occurred: %s\n' % e.error_code)
sys.stderr.write('Error attributes:\n')
for key, value in e.arguments.items():
sys.stderr.write(f'{key}={value}\n')
# 8️⃣
sys.exit(1)
if __name__ == '__main__':
main() 1️⃣ Ja, je kan je code meteen in het 2️⃣ Omdat ik 3️⃣ Het is wel zo handig dat als je vergeten bent hoe je een programma moet gebruiken, het programma het je vertelt! Laten we aardig zijn voor de gebruiker (dat ben je zelf ook over een maand!) 4️⃣ Ik zag dat al jouw variabelen met 5️⃣ Een programma mag maximaal een bepaald aantal bestanden tegelijk open hebben. Het is dus verstandig om ze te sluiten als je ze niet meer nodig hebt, zodat je programma niet opeens stopt met een fout omdat je over de grens heen gaat! (Ja, voor dit programma maakt het niet uit want het is er maar ééntje, en Python helpt je ook nog eens met automatisch sluiten zodat het niet per sé nodig is. Doe het toch maar, want het is een goede gewoonte om aan te wennen. Van die gewoonte ga je later nog veel lol hebben). 6️⃣ Ik heb je lus omgeschreven naar een "list comprehension" met een "join". De list comprehension: 7️⃣ Het viel me op dat 8️⃣ Als er iets misgaat stoppen we het programma altijd met een foutcode. Dat is handig als mensen eromheen willen programmeren in een script (bijvoorbeeld in Batch of bash), zodat ze kunnen controleren of alles goed gegaan is. En foutuitvoer gaat altijd naar |
Ah top, ik zal er naar kijken en het morgen proberen het zo goed mogelijk te implementeren in de code! |
Je kan de code van hierboven copy/pasten 😉 |
Maar je leert natuurlijk meer als je het zelf probeert :) |
@rix0rrr Hoezo gebruik je |
Om te beginnen kleine grammaticacorrectie: je bedoelt "waarom" in plaats van "hoezo" 😛. Tekstprogramma's hebben twee stromen van uitvoer: die heten "standard output" ( Google er maar eens naar. Hier is bijvoorbeeld een hit die er wel nuttig uit ziet: https://retrocomputing.stackexchange.com/questions/11499/what-was-the-point-of-separating-stdout-and-stderr In Python schrijf je naar sys.stdout.write('hallo\n')
sys.stderr.write('doei\n') Omdat je printen heel vaak doet en je uitvoer MEESTAL naar def print(uitvoer):
sys.stdout.write(uitvoer + '\n') |
Ik heb de code zelf een beetje anders gemaakt zodat je ook het level als argument kan meegeven. Nu is het dus geworden: |
Goed werk Sven! De tests passen nu netjes. Het is wel iets veiliger en overzichtelijker als we jouw logica voor het offline runnen in een andere file zetten, zoals @rix0rrr hier helemaal boven voorstelt:
Lukt het jou om dat te maken? |
Zeker, ik zal dat nu gaan doen |
Het is gelukt 😄 |
Was vergeten de nieuwe versie van de normale hedy.py te pushen, nu alsnog gedaan |
Ik zal trouwens nu ook even de contributing.md file aanpassen dat alles daar ook klopt 😄 |
Nu zou als het goed is alles gedaan moeten zijn wat nodig is. |
Gemerged! Goed werk Sven!!! |
@rix0rrr Thank you for the amazingly detailed feedback and for getting to the root of the issue!! @Sven-Developer Thanks again for your contribution! |
Added functionality to run your hedy files locally. You can install the dependencies using
pip install -r requirements.txt
.Then use
python3 hedy.py LEVEL PATH_TO_FILE
to launch your file