Python Forum
Learning to have Class and doing stuff with it...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning to have Class and doing stuff with it...
#1
PSA: Python noob trying to learn the whole module import, Class usage, and function creation stuff.

Error:
Traceback (most recent call last): File "waveshare.py", line 3, in <module> cmd(AT) NameError: name 'cmd' is not defined
=== Waveshare.py ===
import SIM800

cmd(AT)
=== SIM800/V25ter.py ===
import serial
import time

class V25ter():
    def __init__(command):
        self.cmd = command

    def cmd():
        print(command)
=== SIM800/__init__.py ===
import V25ter
"AT" is an example of a command that I want to send.
Reply
#2
Not sure what you are trying to do, it seems that you want just to print the command to stdout;
To do this by the sophisticated way you chose, you need to do the following changes:

#=== SIM800/__init__.py ===
from .V25ter import V25ter

#=== Waveshare.py ===
import SIM800

cmd_at = SIM800.V25ter("AT")
cmd_at.cmd()


#=== SIM800/V25ter.py ===
import serial
import time
 
class V25ter():
    def __init__(self, command):
        self.cmd = command
 
    def cmd(self):
        print(self.cmd)
BTW, method cmd runs the command,
so its appropriate name should be, e.g. run?!

Hope that helps...
Reply
#3
(Apr-29-2020, 10:05 AM)scidam Wrote: BTW, method cmd runs the command,
so its appropriate name should be, e.g. run?!
Ok, didn't know that. I changed it to run.

(Apr-29-2020, 10:05 AM)scidam Wrote: To do this by the sophisticated way you chose, you need to do the following changes:

#=== SIM800/__init__.py ===
from .V25ter import V25ter

#=== Waveshare.py ===
import SIM800

cmd_at = SIM800.V25ter("AT")
cmd_at.cmd()


#=== SIM800/V25ter.py ===
import serial
import time
 
class V25ter():
    def __init__(self, command):
        self.cmd = command
 
    def cmd(self):
        print(self.cmd)
Did the changes, but now I have a new error...
Error:
Traceback (most recent call last): File "waveshare.py", line 4, in <module> run_at.run()
(Apr-29-2020, 10:05 AM)scidam Wrote: Not sure what you are trying to do, it seems that you want just to print the command to stdout;

Well... The end project is to convert/map a bunch of serial "AT" commands for the SIM800 chip to python along with simpler easy to use functions to do some of the common stuff. The print line is intended to be a serial.write() that uses the arguments of the defined function for variable options for the AT command. As far as I know there's two PIP packages for what I'm trying to do, but they seem dead and are not complete.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A class of machine learning programs Led_Zeppelin 0 481 Jul-13-2023, 01:17 PM
Last Post: Led_Zeppelin
  Run the code for some stuff it does not return me why Anldra12 3 2,838 Apr-19-2021, 02:01 PM
Last Post: Anldra12
  Unable to print stuff from while loop Nick1507 4 2,318 Sep-17-2020, 02:26 PM
Last Post: Nick1507
  How Do I Install Stuff for Python? CopBlaster 6 3,179 May-08-2020, 12:27 PM
Last Post: hussainmujtaba
  Automate the boring stuff : the tic tac toe game DJ_Qu 7 6,640 Apr-24-2019, 12:22 PM
Last Post: ichabod801
  More List Stuff Zman350x 3 3,417 Mar-11-2018, 12:20 AM
Last Post: Zman350x
  Need help importing stuff that python says it cant open Tiaan 2 2,831 Nov-30-2017, 09:54 PM
Last Post: Tiaan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020