Python Forum
AttributeError: 'top_block_sptr' object has no attribute 'change_param_1'
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:

[Image: uuC0h7.png]
Reply
#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.
Reply
#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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 719 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,532 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,385 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 704 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,667 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 686 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,215 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,301 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,780 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,352 Jul-29-2022, 09:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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