Python (seleniumrequests)Class, Constructor, Method Not Working


 
Thread Tools Search this Thread
Top Forums Programming Python (seleniumrequests)Class, Constructor, Method Not Working
# 1  
Old 08-26-2015
Python (seleniumrequests)Class, Constructor, Method Not Working

Newbie question. I created a class:
Code:
class WP(seleniumrequests.PhantomJS):
    
                def __init__(self, wpurl='https://blah.org/download/release-archive/', bwppurl='https://blah.org/plugins/browse/popular/'):
                                self.wp=wpurl
                                self.bwpp=bwppurl
                def wpscaper(self):
                                wpjfetch=seleniumrequests.PhantomJS('../ms-tuesday/phantomjs/bin/phantomjs')
                                wpjfetch.get(self.wp)
                                wpsoup=BeautifulSoup(wpjfetch.page_source, 'lxml')
                def wppscaper(self):
                                wppjfetch=seleniumrequests.PhantomJS('../ms-tuesday/phantomjs/bin/phantomjs')
                                wppjfetch.get(self.bwpp)
                                wppsoup=BeautifulSoup(wpjfetch.page_source, 'lxml')

I am trying to understand why I cannot see any of my methods in where I would expect it to be:
Code:
goodies=WP()
goodies.wpscaper()
goodies.wpscaper.wpjfetch.page_source
goodies.wpscaper.wpsoup

What I see is:
Code:
goodies.wpscaper.im_class  goodies.wpscaper.im_func   goodies.wpscaper.im_self

and the same applies for:
Code:
goodies=WP()
goodies.wppscaper()
goodies.wppscaper.im_class  goodies.wppscaper.im_func   goodies.wppscaper.im_self

What gives ??

---------- Post updated 08-26-15 at 03:35 PM ---------- Previous update was 08-25-15 at 05:37 PM ----------

I can see that the stuff works. For example, if I add a print statement to the functions such as:
Code:
class WP(seleniumrequests.PhantomJS):
    
                def __init__(self, wpurl='https://blah.org/download/release-archive/', bwppurl='https://blah.org/plugins/browse/popular/'):
                                self.wp=wpurl
                                self.bwpp=bwppurl
                def wpscaper(self):
                                wpjfetch=seleniumrequests.PhantomJS('../ms-tuesday/phantomjs/bin/phantomjs')
                                wpjfetch.get(self.wp)
                                wpsoup=BeautifulSoup(wpjfetch.page_source, 'lxml')
                                print (wpsoup)
                def wppscaper(self):
                                wppjfetch=seleniumrequests.PhantomJS('../ms-tuesday/phantomjs/bin/phantomjs')
                                wppjfetch.get(self.bwpp)
                                wppsoup=BeautifulSoup(wpjfetch.page_source, 'lxml')
                                print (wppsoup)

it prints exactly whats in wpsoup or wppsoup:
Code:
goodies=WP()
goodies.wpscaper()
<!DOCTYPE html>
<html dir="ltr" lang="en" xmlns="http://www.w3.org/1999/xhtml"><head profile="http://gmpg.org/xfn/11">
<meta charset="utf-8"/>
<!--
<meta property="fb:page_id" content="6427302910" />
-->
<meta content="7VWES_-rcHBcmaQis9mSYamPfNwE03f4vyTj4pfuAw0" name="google-site-verification"/>
blah blah blah blah..
goodies.wppscaper()
<!DOCTYPE html>
<html dir="ltr" lang="en" xmlns="http://www.w3.org/1999/xhtml"><head profile="http://gmpg.org/xfn/11">
<meta charset="utf-8"/>
<!--
<meta property="fb:page_id" content="6427302910" />
-->
<meta content="7VWES_-rcHBcmaQis9mSYamPfNwE03f4vyTj4pfuAw0" name="google-site-verification"/>
blah blah blah blah..

Additonal Info:
Code:
In [180]: dir(goodies.wpscaper)
Out[180]: 
['__call__',
 '__class__',
 '__cmp__',
 '__delattr__',
 '__doc__',
 '__format__',
 '__func__',
 '__get__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__self__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'im_class',
 'im_func',
 'im_self']
In [181]: dir(goodies.wppscaper)
Out[181]: 
['__call__',
 '__class__',
 '__cmp__',
 '__delattr__',
 '__doc__',
 '__format__',
 '__func__',
 '__get__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__self__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'im_class',
 'im_func',
 'im_self']

So I simple cant understand why I dont have access to:

goodies.wpscaper.wpjfetch
goodies.wpscaper.wpsoup

or

goodies.wppscaper.wppjfetch
goodies.wppscaper.wppsoup

why cant I see them?? I am truly confused.

Last edited by metallica1973; 08-26-2015 at 04:43 PM..
# 2  
Old 08-26-2015
Quote:
Originally Posted by metallica1973
So I simple cant understand why I dont have access to:

goodies.wpscaper.wpjfetch
goodies.wpscaper.wpsoup

or

goodies.wppscaper.wppjfetch
goodies.wppscaper.wppsoup

why cant I see them?? I am truly confused.
Python does not work as such.

In relatation to it:
Code:
wpjfetch=seleniumrequests.PhantomJS('../ms-tuesday/phantomjs/bin/phantomjs')

wpjfetch is a local binding to whatever object is returned from calling the method seleniumrequests.PhantomJS() and it is out of scope as soon as the goodies.wpscaper() method is finished.
You can return it inside the method, or you can make it an instance class attribute and then you can access to it as goodies.wppjfetch, but that breaks OOP encapsulation.

Last edited by Aia; 08-26-2015 at 09:12 PM..
# 3  
Old 08-26-2015
awesome explanation. When you say return back to inside the method, can you show me an example? What would I return it back too?
# 4  
Old 08-26-2015
Code:
$ cat model.py
class ModelNumber(object):

    def serial(self):
        num = "X12631"
        return num

Now in the REPL

Code:
>>> from model import *
>>> product = ModelNumber()
>>> type(product)
<class 'model.ModelNumber'>
>>> type(product.serial)
<type 'instancemethod'>
>>> product.serial()
'X12631'
>>>

However, that's not very useful. The following might be a bit more interesting.

Code:
$ cat model.py
class ModelNumber(object):

    def __init__(self, num):
        self.num = num

    def serial(self):
        return self.num

    def change_serial(self, num):
        self.num = num

In the REPL
Code:
>>>
>>>
>>> from model import *
>>>
>>> product = ModelNumber("X12631")
>>> product.serial()
'X12631'
>>> product.change_serial("X12632")
>>> product.serial()
'X12632'
>>> product.num
'X12632'
>>>

As you see, product.num is accessible, which is nice for debugging purposes, but it should be manipulated and handled by a method in the class.

Last edited by Aia; 08-27-2015 at 12:13 PM.. Reason: mispelling
# 5  
Old 08-28-2015
Aia, I still cant unhide the methods under wppscaper or wpscaper with any return or restructuring this in anyway:
Code:
class WP(seleniumrequests.PhantomJS):
        
                      def __init__(self):
                                                        self.wpurl='https://blah.org/download/release-archive/'
                                                        self.wppurl='https://blah.org/plugins/browse/popular/'
                      def wpscaper(self):
                                                        wpinit=seleniumrequests.PhantomJS(executable_path='phantomjs/bin/phantomjs')
                                                        wpinit.get(self.wpurl)
                                                        wpsoup=BeautifulSoup(wpinit.page_source, 'lxml')
                                                        print (wpsoup)
                                                        return wpsoup
                      def wppscaper(self):
                                                        wppinit=seleniumrequests.PhantomJS(executable_path = "phantomjs/bin/phantomjs")
                                                        wppinit.get(self.wppurl)
                                                        wppsoup=BeautifulSoup(wppinit.page_source, 'lxml')
                                                        print (wppsoup)
                                                        return wppsoup

In [85]: dir(WP)
Out[85]: 
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 '_unwrap_value',
 '_wrap_value',
 'add_cookie',
 'application_cache',
 'back',
 'close',
 'create_web_element',
 'current_url',
 'current_window_handle',
 'delete_all_cookies',
 'delete_cookie',
 'desired_capabilities',
 'execute',
 'execute_async_script',
 'execute_script',
 'file_detector',
 'find_element',
 'find_element_by_class_name',
 'find_element_by_css_selector',
 'find_element_by_id',
 'find_element_by_link_text',
 'find_element_by_name',
 'find_element_by_partial_link_text',
 'find_element_by_tag_name',
 'find_element_by_xpath',
 'find_elements',
 'find_elements_by_class_name',
 'find_elements_by_css_selector',
 'find_elements_by_id',
 'find_elements_by_link_text',
 'find_elements_by_name',
 'find_elements_by_partial_link_text',
 'find_elements_by_tag_name',
 'find_elements_by_xpath',
 'forward',
 'get',
 'get_cookie',
 'get_cookies',
 'get_log',
 'get_screenshot_as_base64',
 'get_screenshot_as_file',
 'get_screenshot_as_png',
 'get_window_position',
 'get_window_size',
 'implicitly_wait',
 'log_types',
 'maximize_window',
 'mobile',
 'name',
 'orientation',
 'page_source',
 'quit',
 'refresh',
 'request',
 'save_screenshot',
 'set_page_load_timeout',
 'set_script_timeout',
 'set_window_position',
 'set_window_size',
 'start_client',
 'start_session',
 'stop_client',
 'switch_to',
 'switch_to_active_element',
 'switch_to_alert',
 'switch_to_default_content',
 'switch_to_frame',
 'switch_to_window',
 'title',
 'window_handles',
 'wppscaper',
 'wpscaper']

In [86]: dir(WP.wpscaper)
Out[86]: 
['__call__',
 '__class__',
 '__cmp__',
 '__delattr__',
 '__doc__',
 '__format__',
 '__func__',
 '__get__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__self__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'im_class',
 'im_func',
 'im_self']

In [87]: dir(WP.wppscaper)
Out[87]: 
['__call__',
 '__class__',
 '__cmp__',
 '__delattr__',
 '__doc__',
 '__format__',
 '__func__',
 '__get__',
 '__getattribute__',
 '__hash__',
 '__init__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__self__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'im_class',
 'im_func',
 'im_self']

I think I understand code encapulation but in this instance I dont want it but for the life of me I cannot understand why I can see my methods and or anything under wpscaper and or wppscaper ? also why cannot I see wpsoup and or wppsoup under wpscaper or wppscaper? Please shed further light upon meSmilie
# 6  
Old 08-28-2015
Since wpsoup is still local to wpscaper() the only way of access is to bind a name to it, outside the class.

Code:
wp_instance = WP()
result = wp_instance.wpscaper()
print(result)

you can make wpsoup a class instance attribute and then it can be manipulated as part of the class object.

Code:
def wpscaper(self):
    wpinit=seleniumrequests.PhantomJS(executable_path='phantomjs/bin/phantomjs')
    wpinit.get(self.wpurl)
    self.wpsoup=BeautifulSoup(wpinit.page_source, 'lxml')

Which will allow you to allow you to access the attribute directly for debugging.
Code:
wp_instance = WP()
wp_instance.wpscaper()
print(wp_instance.wpsoup)

Note that wpsoup does not exist until there's a call to wp_instance.wpscaper(), since it is not coded to be created in the __init__()
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Challenge in finding listing class method and its number of code lines

there are about 300 objectivec .m files and I need to print each file name and its method and number of lines inside the method there is a sample perl files that do perl brace matching... (0 Replies)
Discussion started by: steve32001
0 Replies

2. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

3. Shell Programming and Scripting

Python: Comparison is not working for me

Guys it is very simple script , but for some reason my IF condition is not matching. from sys import argv import os filename='testdata.txt' txt=open(filename) while 1: line= txt.readline() if not line: break line=line.strip('\n') sp=line.split(" ") command =sp+ " " +sp+ " "... (4 Replies)
Discussion started by: baker
4 Replies

4. Shell Programming and Scripting

Date Format not working in python script

Good Morning, I am working on a pyhton script, where it has to fetch a file from our vendor's server over an ftp. The file is having a date format of "MMDDYYYY" (i have included in my python script), but when the script is executed it is unable to fetch the file. At the same time, if i... (1 Reply)
Discussion started by: AReddy
1 Replies

5. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

6. Solaris

Python script not working in batch process

Good morning, I have a python 2.3 script that runs stand alone as intended when tested, then it was put into a ksh script. when running the ksh script it runs as intended. The problem is that my script does not run when the ksh script is called by another user who runs a batch process (as of right... (1 Reply)
Discussion started by: mhahe
1 Replies

7. Shell Programming and Scripting

[Python]StringVar() Class String processing

I have a requirement where I collect inputs from entry widget in gui(via variables a,b,c) and then output a string like this: -a a -b b -c c. Hence if I have given a=1 b=2 c=3, The output string is --> -a 1 -b 2 -c 3 or if I have given a=1 b=2 (c left blank) then the output is --> -a 1... (1 Reply)
Discussion started by: animesharma
1 Replies

8. Programming

Pointer for class not working that well. Syntax I think.

I'll be gratefull for any help. Thanks. :) This is the non class type error: # g++ -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlpp -L/usr/lib/mysql -L/usr/local/lib/mysql++ loaddsgsports.cpp -o loaddsgsports loaddsgsports.cpp: In function âint outputToImport(const char*, const char*,... (1 Reply)
Discussion started by: sepoto
1 Replies

9. Shell Programming and Scripting

Method isSuccess not working right perl

Good morning all.... I have been learning Perl for about 2 months now and I guess I am getting there as much as I can however I am really stuck. I have a Perl script called postEvent.pl which uses a package called event.pm. PostEvent.pl depends on a meithod inside event.pm called isSuccess to... (0 Replies)
Discussion started by: LRoberts
0 Replies

10. Shell Programming and Scripting

call constructor of java class in script

Hi, Is it possible to call the constructur of a java class in a shell script? I know you can call static methods, but can you also call the constructor? tnx. (1 Reply)
Discussion started by: thebladerunner
1 Replies
Login or Register to Ask a Question