Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 AttributeError: 'top_block_sptr' object has no attribute 'change_param_1'
#1
In this exercise, I am using a python webserver to change the parameters' values inside my configuration file (.ini file). To do that I have a few widgets (roundsliders) that I am using to change the values of the parameters. So, once I call/import the file then start dragging the sliders and hit submit then the values should change based on the values I assigned.

The error I am facing with now is with changing the value of parameter # 1. It tells me that "object has no attribute 'change_param_1".

Any idea on how to solve this problem!

Also, if you think there is an easier approach to edit the params values then I would appreciate your suggestions.

python code:

from __future__ import print_function
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import time
from flask import Flask, flash, request, redirect, jsonify, render_template_string, g, current_app
import ConfigParser



config = ConfigParser.RawConfigParser()
config.add_section('Params')



class Top_Block(gr.top_block):

    def __init__(self):

        gr.top_block.__init__(self, "Top Block 22")





    def getfile(self, filename, param_1=0, param_2=0, param_3=0):


        self.param_1 = param_1
        print('[Top_Block] __init__: param_1:', self.param_1)

        self.param_2 = param_2
        print('[Top_Block] __init__: param_2:', self.param_2)

        self.param_3 = param_3
        print('[Top_Block] __init__: param_3:', self.param_3)



    with open(filename, 'wb') as configfile:
        config.write(configfile)



    def change_param_1(self, value=None):
            if value is not None:
                    self.param_1 = value
                    print('[Top_Block] change: param_1:', value)
            config.set_param_1('Params', 'param_1', value)

    def change_param_2(self, value=None):
        if value is not None:
            self.param_2 = value
            print('[Top_Block] change: param_2:', value)
            config.set('Params', 'param_2', value)

    def change_param_3(self, value=None):
        if value is not None:
            self.param_3 = value
            print('[Top_Block] change: param_3:', value)
            config.set('Params', 'param_3', value)


    def change(self,  param_1=None, param_2=None, param_3=None):

            self.change_param_1(param_1)

            self.change_param_2(param_2)

            self.change_param_3(param_3)

    def turn_off(self):
                self.change(0, 0, 0)


#===============================================================

app = Flask(__name__) 




@app.route("/") 
def index():

    current_app.tb = Top_Block()
    current_app.tb.start()

    args_filename = request.args.get('filename', '')    

        # 
    return render_template_string('''<!DOCTYPE html> 
<html>
<head>
<meta charset="utf-8">
<title>Project</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.js"></script>
<style type="text/css">

form {
   display: flex;
   top: 5px;
   margin:20px;  
   border-width: 2px;
   border-bottom-color:#3300ff;
   border-bottom-style: solid;
   width: 1900px;
   border: 5px solid green;
   border-radius: .5em;
   padding: 5px;
   align-items: center;

}

</style>
<h1>
<form action="getfile" method="POST" enctype="multipart/form-data">
     <font size="10"><pre  class="tab"> Import INI File:    </pre></font>
     <input type="file" name="file">
     <input type="submit" value="Submit">
</form>


<style>
  .container {
    display: flex;
  }
  .child
  {
  margin-left:100px;
  }
</style>

<body>
  <div class="container">
    <div class="child">
      <div id="slider1" class='slider row1 col1'></div>

      <center>
        <p>param_1</p>
      </center>
    </div>
    <div class="child">
      <div id="slider2" class='slider row1 col2'></div>
      <center>
        <p>param_2</p>
      </center>
    </div>

    <div class="child">
      <div id="slider3" class='slider row1 col3'></div>

      <center>
        <p>param_3</p>
      </center>
    </div>
  </div>
  <script>
    // create sliders

    $("#slider1").roundSlider({
      sliderType: "min-range",
      radius: 150,
      min: 0,
      max: 10000000000,
      value: 0, // default value at start
      //change: function(event) { $.getJSON('/valueofslider', {slide1_val: event.value}); }
      change: function(event) {
        $.getJSON('/set_param_1/' + event.value);
      }
    });


    $("#slider2").roundSlider({
      sliderType: "min-range",
      radius: 150,
      min: 0,
      max: 10000000000,
      value: 0, // default value at start
      //change: function(event) { $.getJSON('/valueofslider', {slide2_val: event.value}); }
      change: function(event) {
        $.getJSON('/set_param_2/' + event.value);
      }
    });

    $("#slider3").roundSlider({
      sliderType: "min-range",
      radius: 150,
      min: 0,
      max: 10000000000,
      value: 0, // default value at start
      //change: function(event) { $.getJSON('/valueofslider', {slide3_val: event.value}); }
      change: function(event) {
        $.getJSON('/set_param_3/' + event.value);
      }
    });


  </script>

</body>

</html>''', filename=args_filename)


@app.route('/getfile', methods=['GET','POST'])
def getfile():

    result = request.files.get('file')
    print('[/getfile] result:', result.filename)

    if result:

        result.save(result.filename) 

        current_app.tb.getfile(result.filename)




    return redirect('/')

@app.route('/off')
def off():
    current_app.tb.turn_off()
    return 'off'

@app.route('/set_param_1/<int:value>')
def set_param_1(value):
    current_app.tb.change_param_1(value)
    return str(value)

@app.route('/set_param_2/<int:value>')
def set_param_2(value):
    current_app.tb.change_param_2(value)
    return str(value)

@app.route('/set_param_3/<int:value>')
def set_param_3(value):
    current_app.tb.change_param_3(value)
    return str(value)



@app.route('/valueofslider')
def slide():
    param_1 = request.args.get('slide1_val', None)
    param_2   = request.args.get('slide2_val', None)
    param_3   = request.args.get('slide3_val', None)



    return 'param_1: {}, param_2: {}, param_3: {}'.format(param_1, param_2, param_3)


def main():


    app.run(host='0.0.0.0', debug=True)

if __name__ == '__main__':
    main()
Traceback:

* Serving Flask app "writeToConfigFile" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 138-727-058
127.0.0.1 - - [01/Oct/2019 11:16:22] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [01/Oct/2019 11:16:23] "GET /set_param_1/719618996 HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/fit-pc/Desktop/random exercises/INI/writeToConfigFile.py", line 235, in set_param_1
    current_app.tb.change_param_1(value)
  File "/usr/lib/python2.7/dist-packages/gnuradio/gr/hier_block2.py", line 92, in __getattr__
    return getattr(self._impl, name)
AttributeError: 'top_block_sptr' object has no attribute 'change_param_1'
Location of error:

İmage
Quote
#2
It could be the indenting on lines 43 and 44. Since they aren't under get_file(), the interpreter could think change_param_1() is a function instead of a method.
Hadad likes this post
Quote
#3
(Oct-01-2019, 05:30 PM)stullis Wrote: could be the indenting on lines 43 and 44

Thats how the problem started. I got many notifications about the indenting of these lines and tried different ways and none of them worked. I just ended up having them there as they are now! Thanks for the observation though!


Update:

Well, thanks for bringing that to my attention again. It works like charm now. I re-wrote it like this and it worked:

    def getfile(self, filename):


    	with open(filename, 'wb') as configfile:
    		config.write(configfile)

Something that I wasn't aware of: I am not sure of the following but it helped correct the indenting issue of the def change_param_1 too, which is the unseen spaces!!! I noticed that I had many spaces between lines, once I deleted these spaces I got no more indenting notifications!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  'tuple' object has no attribute 'data' gregpederseng 0 57 10 hours ago
Last Post: gregpederseng
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 71 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn
  AttributeError: 'NoneType' object has no attribute 'label' : What's wrong ? Etienne17 1 329 Dec-13-2019, 06:57 PM
Last Post: ibreeden
  AttributeError: 'Register' object has no attribute 'bit_7' colt 1 202 Dec-12-2019, 09:39 PM
Last Post: micseydel
  Object cannot find its attribute colt 6 496 Oct-09-2019, 12:30 AM
Last Post: ichabod801
  'Client' object has no attribute 'send_message Bambob01 1 774 Sep-27-2019, 01:40 AM
Last Post: Bambob01
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 297 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  AttributeError: module 'asyncio' has no attribute 'get_event_loop Susmitha 5 8,823 Sep-03-2019, 02:33 AM
Last Post: chengonghao
  AttributeError: 'area' object has no attribute 'areaofcircle' shane1236 5 515 Aug-02-2019, 06:44 AM
Last Post: shane1236
  Plotly error - AttributeError: module 'plotly.plotly' has no attribute 'iplot' fernando_santos 0 1,198 Jul-24-2019, 02:35 PM
Last Post: fernando_santos

Forum Jump:


Users browsing this thread: 1 Guest(s)