Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:23:36+00:00 2026-06-03T23:23:36+00:00

I was curious on how decorators worked so was seeing if it would work

  • 0

I was curious on how decorators worked so was seeing if it would work for placing generic functions inside walking a path. I wanted to return the longest segment of integers in space seperated files below the path (and then return this value or possibly print it). Is this possible?

I tried to return things in countL with no success. Then was curious if I placed outside out of the function how this would work. Are Decorators only for printing or can you return from them to get this example to operate properly??

def walkPath(fn):
    def wrapper(*args, **kwargs):
        outside = None
        for dirname, dirnames, filenames in os.walk(args[0]):
            for filename in filenames:
                fn(filename, dirname, outside, *args, **kwargs)
    return wrapper

@walkPath
def countL(filename, dirname, outside, path, extension = '.wrd'):
    if (filename[-4:]  == extension):
        with open(os.path.join(dirname, filename), 'r') as input:
            data = input.readlines()
        for i in range(len(data)):
            temp = data[i].split()
            if (int(temp[1]) - int(temp[0]) > longest):                
                outside = int(temp[1]) - int(temp[0])

This explains what I was doing without the decorator and then was just seeing if you could get the same functionality but making it generic. This means where it wasn’t necessarily the longest word in a set of files but maybe a set of files with the word ‘s’ in the filename for example. Just don’t know how to handle the external variable longest properly or if that is possible with decorators.

def countLength(path, extension = '.wrd'):
    longest = 0
    for dirname, dirnames, filenames in os.walk(path):
        for filename in filenames:
            if (filename[-4:]  == extension):
                with open(os.path.join(dirname, filename), 'r') as input:
                    data = input.readlines()
                for i in range(len(data)):
                    temp = data[i].split()
                    if (int(temp[1]) - int(temp[0]) > longest):
                        longest = int(temp[1]) - int(temp[0])
    return longest
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:23:38+00:00Added an answer on June 3, 2026 at 11:23 pm

    You problem is, you are trying to split into the outer loop and its body, but within the loop you had a local variable (longest) to compare the numbers. So what you would need to do is to make your inner function aggregate the results. That means the walkPath loop would need to save the result of every call to fn and hand it over to the next call, so you can do the comparison within fn. At the end it should spit out what the last call returned. I guess this is what you were trying to do with outside. I also removed the path argument from countL, because it didn’t do anything. You example should work like this:

    def walkPath(fn):
        def wrapper(outside, *args, **kwargs):
            for dirname, dirnames, filenames in os.walk(args[0]):
                for filename in filenames:
                    outside = fn(outside, filename, dirname, *args, **kwargs)
            return outside
        return wrapper
    
    @walkPath
    def countL(outside, filename, dirname, extension = '.wrd'):
        if (filename[-4:]  == extension):
            with open(os.path.join(dirname, filename), 'r') as input:
                data = input.readlines()
            for i in range(len(data)):
                temp = data[i].split()
                if (int(temp[1]) - int(temp[0]) > outside):                
                    outside = int(temp[1]) - int(temp[0])
        return outside
    

    You would then have to call something like countL(0).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Curious, what happens when you return keyword this from a struct in C#? For
Just curious. How does actually the function Sleep() work (declared in windows.h)? Maybe not
Just curious, why does the following code import sys class F(Exception): sys.stderr.write('Inside exception\n') sys.stderr.flush()
Curious if anyone has opinions on which method would be better suited for asp.net
Just curious how are these implemented. I can't see where I would start. Do
Curious - why doesn't stat_summary produce any new variables? Would seems very useful to
Was curious, but are any NoSQL DBMS written in Ruby? And if not, would
curious if anyone might have some insight in how I would do the following
Curious to know how people set up their personal and/or work development environment, in
Just curious if there's an easy way to add functions to the $(window).load() event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.