Skip to content
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

All modules should automatically be from module import * #88

Open
mobluse opened this issue Jun 16, 2024 · 11 comments
Open

All modules should automatically be from module import * #88

mobluse opened this issue Jun 16, 2024 · 11 comments

Comments

@mobluse
Copy link

mobluse commented Jun 16, 2024

It would be easier if from math import * was used because it would save people from having to type math. before every math function. All modules are auto imported, and I suggest all modules should also be auto from-imported. That means you could use all functions without a module name prefix. You could still use the fully qualified name if you want or need to. If one name shadows another you have to use the fully qualified name except for the last from-imported module.

The modules could be auto from-imported in an order so that the most used modules are imported last.

from ANYTHING import * should be ignored like import ANYTHING is ignored now.

I think this would be helpful for beginners, since you don't have to type many module prefixes. This means Snek would be more competitive as a first computer language. This would also make Snek programs shorter in flash memory.

E.g. stdscr.refresh() would still need stdscr. since stdscr is an object and not a module.

@keith-packard
Copy link
Owner

snek doesn't actually support python modules, the whole hack with the math functions is that they really are named math.sin et al. . is part of the name, it's not separate at all. Maybe those functions should also be available without the math. prefix as well, so that you could pretend that from math import * did what you expected?

@mobluse
Copy link
Author

mobluse commented Jun 22, 2024

I understand that the modules are faked, but I think it should work as if this had been in a Python3 file before the rest of the program:

import eeprom
import tone
import curses
import time
import random
import math
from eeprom import *
from tone import *
from curses import *
from time import *
from random import *
from math import *

I.e. the most common modules should be from-imported last in order to override earlier names.

stdscr is an auto created object in the Snek case.

GPIO functions, e.g. talkto() and on(), are moduleless and temperature() also.
Is it reset() or eeprom.reset()? See https://sneklang.org/doc/snek.html#_reset .

@merwok
Copy link

merwok commented Jun 22, 2024

62 names (from the math module) seem a bit much, IMHO it’s cleaner and closer to Python to have only math.x names

@mobluse
Copy link
Author

mobluse commented Jun 26, 2024

It's standard on graphing calculators with Python to from-import math and other modules. It makes programming easier and programs shorter.

@mobluse
Copy link
Author

mobluse commented Oct 24, 2024

An alternative could be to allow shorter module names. It would be as if the following had been done:

import eeprom as e
import tone as to
import curses as c
import time as t
import random as r
import math as m

Then one wouldn't need to type a lot and one could fit larger programs in flash.

$ python
Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> m=math
>>> m.atan(1)*4
3.141592653589793
>>> 
$ snek
Welcome to Snek version 1.9
> import math
> m=math
<stdin>:2 undefined: math
> m.atan(1)*4
<stdin>:3 undefined: m.atan
> atan=math.atan
> atan(1)*4
3.14159274
>

@keith-packard
Copy link
Owner

I've just posted PR #90 which would allow us to include 'from math import *' in source files. With that, we could
bring the math functions into the main namespace and get rid of the math. versions entirely. This would save a tonne of ROM space as well as making source code shorter, while still providing source-code compatibility with python. Thoughts?

@keith-packard
Copy link
Owner

I'd probably remove time. and random. as well. Unsure about the others.

@mobluse
Copy link
Author

mobluse commented Oct 25, 2024

I believe this would be an improvement since math, random, and time mostly uses from module import functions. It would also be easier to type and store in eeprom. This change would break existing Snek programs, but one could write a sed script to convert them.

@keith-packard
Copy link
Owner

Check out PR #99 and see what you think. I've left curses, stdscr and eeprom out of this patch because they're much less common. The eeprom functions can't be changed without also fixing the mu editor upstream.

@mobluse
Copy link
Author

mobluse commented Oct 29, 2024

It works as expected from my small tests e.g. using this program:
https://github.com/mobluse/python82-scripts/blob/main/flappbat_s.py
Those ending in _s.py works in the coming Snek.
I compiled using (since I only compile for Linux and Arduino UNO):

sudo apt install lola gcc-avr avr-libc python3-serial gawk libreadline-dev asciidoctor ruby-asciidoctor-pdf
cd
git clone https://github.com/keith-packard/snek/
cd snek/ports/posix/
make clean
git fetch origin pull/99/head:BRANCH_NAME
git switch BRANCH_NAME
make
sudo make install

I have not yet tested on Arduino UNO.

@keith-packard
Copy link
Owner

Thanks for testing. I think I'll go see if I can add an option to leave the old names in place while also providing the new names on targets large enough for that. Tiny targets will want to save the space, I suspect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants