3

I am just writing some code for a Raspberry Pi robotics project. Since I don't have a Pi set up as a workstation, I am using a Google Drive notepad to do write it in, which recognises Python syntax but doesn't support running of the code (RPi.GPIO wouldn't work even if I used Python on PC anyways).

It crossed my mind that instead of writing:

import RPi.GPIO as GPIO

I could write

import RPi.GPIO as g

and save me typing it, which I'm gonna have to do about 7,000 times.

Every single tutorial I have seen uses GPIO, so is it a required thing or can I just import it as whatever I want?

On a side note, is there any way to simulate running code on a Raspi, or any way to simulate running of the GPIO pins?

  • "Google Drive notepad" -> I imagine this doesn't have many coding features beyond syntax highlighting. As Kevin implies in his answer, using single lowercase letters for globals will become a bullet-in-the-foot eventually -- at which point doing a search and replace on g to improve the situation will be lemony salt in the wound. For fooling around, don't worry, but sooner or later you will want to investigate an editor/IDE with stuff like completion for variable names (similar to what a smartphone does for words), making it easy to be lazy and not paint into a corner. – goldilocks Jun 23 '15 at 18:14
  • At the end of writing your program you could just do a regex search and replace g. with gpio. As a perl regex, s/g./gpio./g It might help with maintainability in the long run. I've done this multiple times when I am re factoring a small program with a lot of use of a variable. – HSchmale Jun 23 '15 at 22:05
8

To answer your main question, no there is nothing syntactically wrong with writing:

import RPi.GPIO as g
g.setmode(GPIO.BCM)
#etc...

However, I would seriously evaluate what you are trying to do if you think you will be typing gpio 7000 times. In my opinion the clarity of gpio is much more important than the 3 characters you are saving each time.

As for not having the raspberry pi as a workstation, if you have it connected to the internet you can easily ssh into it and write code that way using vi or nano. You can also use sftp plugins for Atom, Sublime, etc to edit code on the pi remotely. Using one of these editors will get you tab completion, thereby saving you some valuable key strokes.

  • Also, a web IDE like Codiad is another option. – Jacobm001 Jun 23 '15 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.