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 7782701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:37:22+00:00 2026-06-01T19:37:22+00:00

My assignment calls for 3 modules- fileutility, choices, and selectiveFileCopy, the last of which

  • 0

My assignment calls for 3 modules- fileutility, choices, and selectiveFileCopy, the last of which imports the first two.

The purpose is to be able to selectively copy pieces of text from an input file, then write it to an output file, determined by the “predicate” in the choices module. As in, either copy everything (choices.always), if a specific string is present(choices.contains(x)), or by length (choices.shorterThan(x)).

So far, I have only the always() working, but it must take in one parameter, but my professor specifically stated the parameter could be anything, even nothing(?). Is this possible? If so, how do I write my definition so that it works?

The second part of this very long question is why my other two predicates don’t work. When I tested them with docstests(another part of the assignment), they all passed.

Here’s some code:

fileutility(I’ve been told this function is meaningless, but its part of the assignment so…)-

def safeOpen(prompt:str, openMode:str, errorMessage:str ):
   while True:
      try:
         return open(input(prompt),openMode)            
       except IOError:
           return(errorMessage)

choices-

def always(x):
    """
    always(x) always returns True
    >>> always(2)
    True
    >>> always("hello")
    True
    >>> always(False)
    True
    >>> always(2.1)
    True
    """
    return True

def shorterThan(x:int):
    """
    shorterThan(x) returns True if the specified string 
    is shorter than the specified integer, False is it is not

    >>> shorterThan(3)("sadasda")
    False
    >>> shorterThan(5)("abc")
    True

    """
    def string (y:str): 
        return (len(y)<x)
    return string

def contains(pattern:str):
    """
    contains(pattern) returns True if the pattern specified is in the
    string specified, and false if it is not.

    >>> contains("really")("Do you really think so?")
    True
    >>> contains("5")("Five dogs lived in the park")
    False

    """
    def checker(line:str):
        return(pattern in line)
    return checker

selectiveFileCopy-

import fileutility
import choices

def selectivelyCopy(inputFile,outputFile,predicate):
    linesCopied = 0
    for line in inputFile:
        if predicate == True:
            outputFile.write(line)
            linesCopied+=1
    inputFile.close()
    return linesCopied


inputFile = fileutility.safeOpen("Input file name: ",  "r", "  Can't find that file")
outputFile = fileutility.safeOpen("Output file name: ", "w", "  Can't create that file")
predicate = eval(input("Function to use as a predicate: "))   
print("Lines copied =",selectivelyCopy(inputFile,outputFile,predicate))
  • 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-01T19:37:24+00:00Added an answer on June 1, 2026 at 7:37 pm

    So far, I have only the always() working, but it must take in one
    parameter, but my professor specifically stated the parameter could be
    anything, even nothing(?). Is this possible? If so, how do I write my
    definition so that it works?

    You can use a default argument:

    def always(x=None): # x=None when you don't give a argument
        return True
    

    The second part of this very long question is why my other two
    predicates don’t work. When I tested them with docstests(another part
    of the assignment), they all passed.

    Your predicates do work, but they are functions that need to be called:

    def selectivelyCopy(inputFile,outputFile,predicate):
        linesCopied = 0
        for line in inputFile:
            if predicate(line): # test each line with the predicate function
                outputFile.write(line)
                linesCopied+=1
        inputFile.close()
        return linesCopied
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assignment: Write a C++ program to read the menu information from menu.txt. First letter
The assignment at my first year uni computing course says my program should read
I'm doing an assignment on fork(),exec() and related UNIX calls where I need to
I have some Perl modules which exports various functions. (We haven't used @EXPORT in
I am doing a batch scripting assignment where I have to call one script
/************************** Assignment Statment Module **************************/ assgnStmnt(Clist) :- Clist = [H|T], id(H), conc(Exp, [';'|AnotherStmnt], T),
Assignment of NSFileOwnerAccountName and NSFileGroupOwnerAccountName does not work? The directory created defaults to my
My assignment is to write an overloaded version of iquote() , a method that
Subscripted assignment dimension mismatch.' when running a linprog coding. My code is for M
My assignment is to create a simple graph that has both Nodes and Edges.

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.