Python Forum
Global accessing of parameter to class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global accessing of parameter to class
#1
I need to access the variable from main.py to global_rrt.py. I need to access the variable serial_rrt_read in global_rrt.py under the calss frame_101.
global_rrt.py
global serial_rrt,serial_rrt_read           #Read data from RS232 port of fast charger
global serial_length                        #Determine the length of serial read from fast charger


from main import*
print serial_rrt_read
class frame_101:
        frame_101_serial =  "1111111100000000"
        frame_101_start  =  frame_101_serial[0:8]
        frame_101_f_id   =  frame_101_serial[8:16] 
        frame_101_end    =  frame_101_serial[632:640]

frame_101_data = frame_101()
#print frame_101_data.frame_101_serial
main.py
import time                     #Deals with date and time
#import serial                   #Acess for Serial Port
import subprocess               #Allows us to spawn processes
import os                       #Provides a way of using operating system dependent functionality
import sys                      #Access variables used or maintained by the interpreter
from global_rrt import*         #Access Global Variables in Main File

#Start Reading Serial Port Data
#serial_rrt = serial.Serial('/dev/ttyACM1', 9600, timeout=1)

while 1:
    #serial_rrt_read = ser.readline()
    serial_rrt_read = "1111111100000000"
    #print serial_rrt_read
    serial_length = len(serial_rrt_read)
    #print serial_length
    if(serial_length == 16):
        frame_101_data = frame_101()
        print "Hi"
        print frame_101_data.frame_101_serial
  
     
Reply
#2
You should take a tutorial on classes
see: https://docs.python.org/3/tutorial/classes.html
Reply
#3
There's also one on this forum, see the link in my signature.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
If you use the if __name__ == "__main__": check, you should be able to just import it.

# eggs.py

frame_data = 42

if __name__ == "__main__":
    import spam
    print(spam.other_data)
    print(frame_data) # what will this be?
# spam.py

import eggs
other_data = eggs.frame_data * 2
eggs.frame_data = 1
Output:
> python eggs.py 84 42
It's not a great way to do it, since each module would then have it's own concept of what the values are, so a change in one file won't be reflected in the other (as demonstrated).  It works for basic cases, but you're better off restructuring if you want something that works better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,637 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  accessing value in dict_values class CompleteNewb 14 5,019 Mar-31-2022, 04:02 AM
Last Post: deanhystad
  read a parameter after updating it in another class MKS2020 3 2,203 Jul-25-2020, 08:27 AM
Last Post: MKS2020
  accessing local variable outside class priyanka08 3 2,130 Sep-24-2019, 10:00 AM
Last Post: buran
  Global variable does not seem to be global. Columbo 6 3,615 Jul-15-2019, 11:00 PM
Last Post: Columbo
  The derivate class dosn't behave like the base when i pass parameter to them drudox 4 3,125 Aug-05-2018, 06:42 PM
Last Post: drudox
  How to print a global dictionary class properly 3dimensions 2 3,180 Apr-18-2018, 05:45 PM
Last Post: nilamo
  [Python Class] save method output to global file/list Vijay 3 4,979 Dec-23-2017, 03:20 AM
Last Post: Vijay

Forum Jump:

User Panel Messages

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