Note that there are some explanatory texts on larger screens.

plurals
  1. POReusing variable between SocketServer requests
    primarykey
    data
    text
    <p>I want to create temporary variable for method inside class. and update the variable inside method. and i want to reuse <code>self.last_l</code> inside loop. but it doesn't work. </p> <p>here it is my code :</p> <pre><code>import socket, mouseapi, mouseinput from sys import stdout, exit from decimal import Decimal from math import fabs from datetime import datetime import time import SocketServer UDP_IP = "192.168.1.100" UDP_PORT = 5005 class MyUDPHandler(SocketServer.BaseRequestHandler): def setup(self): self.before = 0 self.noise = 1.5 self.noise_f = 0.8 self.last_l = 0 # i want this temporary and updated on handle() def handle(self): data = self.request[0].strip() socket = self.request[1] start = time.clock() ndata = data.replace("[","") ndata = data.replace("]","") ndata = ndata.split(", ") try: ndata[1] = ("%.2f" % float(ndata[1])) atas = ndata[1] atas_bawah = int(int(float(atas)*100)) selisih = fabs(float(atas)-float(self.last_l)) # used here if selisih &gt; self.noise_f: print "Selisih -&gt; %.2f" % float(selisih) print "Sensor -&gt; %.2f" % float(atas) self.last_l = atas # and updated here atas_bawah = int(int(float(atas)*100)) end = time.clock() print "Latency -&gt; %.2gs" % (end-start) if self.last_l == 0: self.last_l = atas # or updated here except KeyboardInterrupt: sys.exit(1) if __name__ == "__main__": HOST, PORT = UDP_IP, UDP_PORT server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler) server.serve_forever() </code></pre> <p>So i expect to print selisih value is less than 1 or more. but it give me more than 1. </p> <pre><code>Selisih -&gt; 6.53 Sensor -&gt; 6.53 Latency -&gt; 3.1e-05s Selisih -&gt; 6.70 Sensor -&gt; 6.70 Latency -&gt; 2.8e-05s Selisih -&gt; 6.97 Sensor -&gt; 6.97 Latency -&gt; 4.1e-05s Selisih -&gt; 7.15 Sensor -&gt; 7.15 Latency -&gt; 2.1e-05s Selisih -&gt; 7.14 Sensor -&gt; 7.14 Latency -&gt; 2.2e-05s Selisih -&gt; 7.14 Sensor -&gt; 7.14 Latency -&gt; 2.1e-05s Selisih -&gt; 7.05 Sensor -&gt; 7.05 Latency -&gt; 2.2e-05s Selisih -&gt; 7.02 Sensor -&gt; 7.02 Latency -&gt; 2.2e-05s </code></pre> <p><strong>I've tried to make last_l with global scope. still won't work.</strong></p> <p>and I get <code>UnboundLocalError: local variable 'last_l' referenced before assignment</code> when i try to place <code>global last_l</code> followed by <code>last_l = 0</code> anywhere and change <code>self.last_l</code> to <code>last_l</code> inside <code>handle</code> method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload