LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2022, 05:01 AM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 285

Rep: Reputation: 6
python: passing parameters through a functions tree


Hi,
I have a functions tree in which a requests session should be opened at the top of the tree, but the session itself should be used by the functions in the bottom of the tree.

please consider this simple example:
Code:
def scrape(sites_list):
    for site in sites_list:
        with requests.Session() as req_session:
            funcA(site, req_session)

def funcA(site, req_session):
    funcB(req_session)

def funcB(req_session):
    req_session.get('site.com')

To let funcB() use req_session should I pass it as a parameter through the whole functions tree as show above? Or is there a smarter way to do it?
 
Old 03-09-2022, 06:07 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
depends on your code (if it is OOP or not). You can use global variable, a class member (for example).
 
Old 03-09-2022, 09:08 AM   #3
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
i've not used OOP so far.

i thought i could use something like this:
Code:
$ cat main.py
import sys

def main():
    for site in ['site1', 'site2']:
        site_module = importlib.import_module(site)
        site_scraper = getattr(site_module, f'{site}_scraper')
        site_scraper()
        site_session = getattr(site_module, f'{site}_session')
        site_session.close()

$ cat site1.py
import requests

site1_session = requests.Session()


def site1_scraper():
    post_scraper()
    comment_scraper()


def post_scraper():
    response = site1_session.get(post_url)


def comment_scraper():
    response = site1_session.get(comment_url)
for what i understand, the with construct would automatically close each session in case of error.
could i get something similar with a try/except construct inside main()?

Last edited by masavini; 03-09-2022 at 09:56 AM.
 
Old 03-09-2022, 02:42 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
I'd just pass it up.
 
Old 03-10-2022, 12:06 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
I don't really understand the question. And also I don't understand why don't you try your idea. Would be better to try to implement what you want and discuss the practical details.
 
1 members found this post helpful.
Old 03-11-2022, 02:01 AM   #6
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by pan64 View Post
I don't really understand the question. And also I don't understand why don't you try your idea. Would be better to try to implement what you want and discuss the practical details.
of course i tried my idea and it works, but i'm a total newbie and:
1) i don't want to reinvent the wheel
2) even if it seems to works, it doesn't mean it's the right way
 
Old 03-11-2022, 02:30 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by masavini View Post
of course i tried my idea and it works, but i'm a total newbie and:
1) i don't want to reinvent the wheel
Actually I don't know which wheel is it
Quote:
Originally Posted by masavini View Post
2) even if it seems to works, it doesn't mean it's the right way
there is no "right way", there are several different ways available to implement anything. So your solution can be acceptable especially because you understand how does that work.
From the other hand you can show us that code and we can help you to improve it.
 
Old 03-11-2022, 03:03 AM   #8
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by pan64 View Post
Actually I don't know which wheel is it
well, i don't know either. but as i wrote i'm a newbie and i just know there are A LOT of wheels: i didn't want to miss an important one.


Quote:
Originally Posted by pan64 View Post
there is no "right way", there are several different ways available to implement anything. So your solution can be acceptable especially because you understand how does that work.
From the other hand you can show us that code and we can help you to improve it.
thanks, i usually post code samples to avoid bothering people with not relevant (frequently badly written) code...
 
Old 03-11-2022, 08:16 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557
Quote:
Originally Posted by masavini View Post
of course i tried my idea and it works, but i'm a total newbie and:
1) i don't want to reinvent the wheel
2) even if it seems to works, it doesn't mean it's the right way
Both good attitudes to have, but as Pan says there isn't a single right answer, sometimes you have to make a choice and it'll either be the right one or you'll learn something.

Have a read of this article: https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms

Different approaches can work better is different situations, but sometimes they work equally well and it doesn't matter which one you use - what's more important is consistency, correctness, and readability of the code.

 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing pointers through multiple functions. yaplej Programming 6 10-28-2016 06:12 PM
Run fast export with passing parameters through ksh renuk Linux - Newbie 4 12-27-2013 04:05 AM
python: passing functions or unevaluated code with multiple lines or expressions stateless Programming 1 11-30-2013 10:38 PM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 02:40 PM
Passing Functions through Socket sunny_krishna Linux - Software 1 04-15-2004 08:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration