python代写: Level 4: I/O and String Manipulation

4.1: Python Strings

1) Use the following string for each of the below exercises: “ that I have ever taken. “:

The Python course is the best course

Exercises

Level 4: I/O and String Manipulation

  1. Display the length of the string.
  2. Find the index of the first ‘o’ in the string.
  3. Trim off the leading spaces only.
  4. Trim off the trailing spaces only.
  5. Trim off both the leading and trailing spaces (use this trimmed string for all the remaining

    parts below).

  6. Fully capitalize the string.
  7. Fully lowercase the string.
  8. Display the number of occurrence of the letter ‘d’ and of the word ‘the’.
  9. Display the first 15 characters of the string.
  10. Display the last 10 characters of the string.
  11. Display characters 5-23 of the string.
  12. Find the index of the first occurrence of the word ‘course’.
  13. Findtheindexofthesecondoccurrenceoftheword‘course’.
  14. Find the index of the second to last occurrence of the letter ‘t’, between the 7th and 33rd

    characters in the string.

  15. Replace the period (.) with an exclamation point (!).
  16. Replace all occurrences of the word ‘course’ with ‘class’.
  1. 2)  Save the following Windows file-path into a string variable: C:\Users\Me\Desktop\MyTable.csv. Perform the following operations:
    1. Extract the filename with extension from the path.
    2. Extract the file extension only.
    3. Add another folder (can name it whatever you’d like) between ‘Desktop’ and the filename.
  2. 3)  Create a list as follows: [‘C:’, ‘Users’, ‘Me’, ‘Desktop’, ‘MyTable.csv’]. Perform the following:
    1. Join the list together to create a valid pathname.
    2. Insert another folder into the list, between ‘Desktop’ and ‘MyTable.csv’ and join the

      resulting list to create a valid pathname.

  1. 4)  Create a program that does the following:
    a. Prompts the user for name, age (integer), and country of residence. Display the information

    as follows: <name> is <age> years old and lives in <country>.
    b. Do the same as above, but using a decimal number for the age. Display the number with one

    decimal place.

    Write a separate version of the above using format flags, a version using the format function with numeric placeholders, and a version using the format function with keyword placeholders – which is cleaner?

  2. 5)  Convert your Timer class’ print statement to use format flags or the format function, instead of concatenating strings.

Note that you are expected to use either format flags or the format function in all your code from here on out. You may choose whichever you prefer (though the format function is now more commonly used, especially in Python 3), but you should be consistent with your choice.

4.2: Logging

For all of the below exercises, be sure to use the previously-learned string formatting to log well-formed messages. Log messages should be informative and explicit as possible. For example, if logging an error for an input value, the message should contain the actual input value, why it’s bad, and what the code expects instead.

  1. 1)  Modify your Timer class to use a logging statement (info level) instead of a print statement.
  2. 2)  Modify your Timer class as follows:
    1. Add a class-level warnThreshold variable, which defaults to 1 minute.
    2. When printing the time taken, use a warn-level log statement instead of info-level if the

      time taken exceeds the warn threshold.

  3. 3)  Add logging statements to your Loan class. This should consist of the following:
    1. Anytime an exception is thrown (i.e., when an incorrect Asset type is passed-into the

      initialization function), log an error prior to raising the exception.

    2. Debug-level logs which display interim steps of calculations and return values for the Loan

      functions.

    3. Info-level logs to display things like ‘t is greater than term’ in the loan functions.
    4. At the point the exception is caught (in your main function) use logging.exception to display

      the exception in addition to a custom message.

    5. Add a warn log to the recursive versions of the waterfall functions (that they are expected to

      take a long time, so the explicit versions are recommended).

  4. 4)  Play around with your Loan and Timer classes to trigger logging statements. Switch logging levels in your main code to demonstrate how to turn on/off different levels of logging.

4.3: File I/O

  1. 1)  To practice file management, do the following in order (using Python code):
    1. Create a new directory.
    2. Rename the above directory.
    3. Delete the above directory.
    4. Create another directory and create two text files in this directory.
    5. Delete one of the text files from the above directory.
    6. Rename the remaining text file.
    7. Create a subdirectory within the above created directory.
    8. Move the remaining text file into the subdirectory.
    9. Remove the top level directory with all its contents (using a single function call). Be careful!
  2. 2)  Write code that searches your entire computer for all files of extension py. Hint: Use os.walk and any necessary string manipulation functions.
  3. 3)  Write code that searches your entire computer for all pdf files which reside in any directory (at any level) that contains the string ‘gr’ in its name.
  4. 4)  Open a brand-new file and write to it (should write several lines).
  5. 5)  Open the file from 1) and read it. Display each line.
  6. 6)  Open the file from 1) and append to it.
  7. 7)  Create a program which does the following:
    1. Gives the user a choice of two options: (1) Add Loan, (2) Write file and exit.
      1. If user enters ‘1’, prompt the user for the type of Loan, its asset name/value, its face amount, rate, and term. Each prompt should occur one after the other. After the last prompt, save the entry into a Loan object, notify the user that the loan has been recorded, and return to the main menu.
      2. If user enters ‘2’, loop through all the entered loans and write them to a file. The file should be in extension .csv. To do this properly, each sub-entry (loan type, asset name, asset value, amount, rate, and term) should be separate by a comma. Each loan should be separated by a newline.
    2. To verify that your generated .csv is a valid .csv file, try opening it in Excel once it has been generated. You should see four columns and the number of rows should reflect the number of loans.

8) As a follow-up, create a third option: (3) Read loan .csv file. This option should:

  1. Ask the user to enter a file path of the loan .csv file.
  2. Load the .csv file into Loan objects.
  3. Add an additional (fourth) option to display the WAR and WAM of all the loans.

Note that this is going to be very useful to read in actual Loan data in our ABS model in the case study. Additionally, we will be writing our ABS model results into CSV files.