Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Grabbing a value from one python script into another
Very new to Python and trying to figure out how I can get the value from a variable in one script into another.

I have a python script called "Accelerator" which gets a constantly changing value from a UDP packet and then prints that value to the screen

-> print("Revs = " + str(int(revsPC)))

What I want to do is grab that value in revsPC pass it to another script and display it

I have tried the import i.e import Accelerator but this I understand executes the whole Accelerator script which throws up an error. I have also tried import revsPC from Accelerator but that doesn't seem to do anything.

Am I missing something really obvious? I am very new to python so my ignorance is high!

Thanks in advance for any help.
revs_pc = 999
if __name__ == '__main__':
    print(f"Revs = {revs_pc}")
import Accelerator
import math

def foo(arg):
    value = math.factorial(arg) + Accelerator.revs_pc
    return value

if __name__ == '__main__':
So it's a good practice to use if __name__ == '__main__',then dos code after that do not execute when import it.
Have to call Accelerator.revs_pc to get 999.
Also look that i use f-string to avoid the not so nice way print("Revs = " + str(int(revsPC))).
That's great and works a treat but the revsPC data is generated from a value inside a UDP packet where the connection is established at the start of the script and the the revsPC variable is inside a loop. I know my syntax is not great or neat but it does do the basic job of determining my revsPC value.
import sys
import socket
import time

UDP_IP = ""
UDP_PORT1 = 40002
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock1.bind((UDP_IP, UDP_PORT1))

#definition of the function for mapping a range values to another
def translate(value, inputMin, inputMax, outputMin, outputMax):
	# Figure out how 'wide' each range is
	inputSpan = inputMax - inputMin
	outputSpan = outputMax - outputMin

	# Convert the input range into a 0-1 range (float)
	valueScaled = float(value - inputMin) / float(inputSpan)

	# Convert the 0-1 range into a value in the output range.	
	return outputMin + (valueScaled * outputSpan)

counter = 0
while True:

	#Beginning of the program

	data, addr = sock1.recvfrom(1024) # length of the buffer : 1024 bytes
	hiValRPM = data1[0]
	loValRPM = data1[1]

	print("Data Received : " + str(hiValRPM) + "/" + str(loValRPM))
	print(" ")

	#Revs : Mapping from 0-1023 to 0-6000
	if(hiValRPM == 0):
		revsPC = translate(loValRPM, 0, 255, 0, 1503)
	elif(hiValRPM == 1):
		revsPC = translate(loValRPM, 0, 255, 1504, 3004)
	elif(hiValRPM == 2):
		revsPC = translate(loValRPM, 0, 255, 3005, 4503)
	elif(hiValRPM == 3):
		revsPC = translate(loValRPM, 0, 255, 4504, 6000)

	print("Revs = " + str(int(revsPC)))

	#Calculation of the program execution time
	print ("Execution time = %f ms" %tmps2)

	counter += 1
	print("Packet number : " + str(counter))
Yoriz wrote May-24-2019, 04:27 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Now you run a infinity loop(while True) with no break out of the loop.
So need a way to get value out,one way can by to rewire it so it's a generator.
import random
import time

def foo():
    while True:
        revs_pc = random.randint(1,10)
        yield revs_pc
import Accelerator

for i in range(10):
    revs_pc = next(
    revs_pc += 9  # do something with revs_pc value
12 16 13 11 10 10 15 10 19 14

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 1 40 Yesterday, 02:16 PM
Last Post: buran
  Grabbing a Subset of a String acemurdoc 3 401 Jun-18-2019, 04:57 PM
Last Post: perfringo
  Package python script which has different libraries as a single executable or script tej7gandhi 1 466 May-11-2019, 08:12 PM
Last Post: keames
  Grabbing questions from a quiz app CodingUnicorn 0 402 Mar-05-2019, 11:26 PM
Last Post: CodingUnicorn
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 6,228 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 2,786 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)