BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

计算机代写|COMP 202 Fall 2022 Assignment 4

计算机代写|COMP 202 Fall 2022 Assignment 4

这是一篇来自加拿大的关于解决Paying It Forward Making Melodies两个问题的计算机代写

 

Question 1: Paying It Forward (40 points)

An Application Programming Interface, or API for short, is a way for two or more computer programs to communicate with each other. An API is comprised of a formal specification, describing how the communication is done, and an implementation, which implements the communication process in code.

APIs are not designed for regular users of a computer program. They are designed for use by programmers, who want their computer program to be able to interface with another.

A common type of API is called a web API. A web API is used by a computer program to retrieve information from a website. Most popular websites have their own web API. For example, YouTube,Facebook, Reddit, and Twitter (while it still exists) all have a web API. These APIs allow a computer program to connect to the website and retrieve data, e.g., tweets from Twitter, or posts from Reddit.

The data can then be used in the computer program.

Here are some examples of APIs:

  • With the YouTube API, we can search for videos, update playlists, make comments, add captions and many other things just by writing code (basically, almost anything that could be done by users of the website itself).
  • NASA provides a set of APIs that allow you to retrieve all kinds of data. One simple API allows a computer program to retrieve NASA’s Astronomy Picture of the Day.
  • Some weather websites have APIs that allow a computer program to retrieve the current weather for a given city. That is how, in the Virtual World, the weather instruments in the Observatory can give you the current weather for Montreal: because the Virtual World server uses a weather website’s API to retrieve that information.

Using an API

It is relatively simple to use a web API. Our code must connect to a website at a special API URL and retrieve data from it.

First, we need to know how to retrieve data from a website in Python. To do this, we will need to install the requests module into Thonny, following the usual steps. (You may already have it installed if you have tried out the Virtual World.)

With the requests module installed, we can use the following code to retrieve data from a given URL.

This example assumes that the website will send the information to us in dictionary form (which is not always the case – it depends on the particular API).

result_dict = requests.get(url=‘url-here’).json()

The resulting dictionary will contain the data requested from the API and/or other information about the status of the request.

Each API has several endpoints. An endpoint is a ‘point of entry’ into the API. Each different request you can make (e.g., posting a comment on a YouTube video, searching for a video, liking or disliking a video) will have its own endpoint. Each endpoint also has its own URL (just like each different webpage on a website). For example, the URL for the YouTube API endpoint that allows for getting a user’s like or dislike of a video is ‘https://www.googleapis.com/youtube/v3/videos/getRating’.

After the URL, we must also include certain data to make our request. To continue with the above example, we would need to specify a video ID for the video that we want to check. We do this by adding a question mark (‘?’) to the end of the URL, followed by (in this case) ‘id=VIDEOID’, where VIDEOID is the video ID (i.e., the part after ‘https://www.youtube.com/watch?v=’ for a YouTube video URL). So the full URL we would connect to would be ‘https://www.googleapis.com/youtube/v3/videos/getRating?id=VIDEOID’1 .

The COMP202COIN API

In this question, we are going to write some code to interface with a web API. Not just any web API – the COMP202COIN API, which was designed specifically for this question.

The COMP202COIN API will let you perform certain operations with respect to your coin balance. So that only you can access your own balance, we introduce a security measure called a token, which you can think of as a password to your account.

To obtain your token, DM CoinsBot over Slack and enter the command @CoinsBot token. Keep your token secure and do not share nor post it anywhere. We will not be able to resolve any issues for

you that are the result of a lost or stolen token.

The COMP202COIN API, located at ‘https://coinsbot202.herokuapp.com/api/’, has the following endpoints. The data required for each endpoint is also listed.

  • balance

email: your McGill email address used to sign up to our Slack workspace

token: your coin account token

  • transfer

withdrawal_email: your McGill email address used to sign up to our Slack workspace

token: your coin account token

deposit_email: the McGill email address of the student to which you want to transfer coins

amount: the number of coins you want to transfer to the other student

To construct an API request given an endpoint and the needed data, we write the API URL, followed by the endpoint name and a ‘?’, then each piece of data in the form ‘name=value’, each separated with an ampersand ‘&’.

E.g., if I wanted to get my balance and my token was ABC, then I would construct the following URL:

‘https://coinsbot202.herokuapp.com/api/balance?email=jonathan.campbell@mcgill.ca&token=ABC’

If we then pass this URL into the requests.get function as described on the previous page, we will obtain a dictionary with a key ‘status’ and a key ‘message’. The former will indicate if our request was successful, and the latter will give us the requested data or other relevant information. For example2 :

>>> API_URL = ‘https://coinsbot202.herokuapp.com/api/’

>>> request_url = API_URL + ‘balance?email=jonathan.campbell@mcgill.ca&token=ABC’

>>> result = requests.get(url=request_url).json()

>>> result

{‘message’: ‘The token in the API request did not match the token that was sent over Slack.’,

‘status’: ‘error’}

Through the code that you will write for this question, specified on the next page, you will be able to connect to these two endpoints with your Python program. What will you do with your newfound power? Will you make deals with other students to amass the most COMP202COIN and obtain a coveted sticker? Or will you generously donate your COMP202COIN to those who may need it more than you? The possibilities are endless!

Please use your judgement if and when you make deals with COMP202COIN. That is, don’t do anything silly. Don’t lend your brand new Tesla to someone just because they give you 5,000 COMP202COIN.

Don’t exchange real money for COMP202COIN. But if you want to offer your friend some home-made cookies in exchange for some coins, that’s great!

For this question, define the following in a file called coins.py.

  • a function dict_to_query that takes a dictionary as input, and returns a string containing the keys and values of the dictionary with the format ‘key=value’, and ampersands (‘&’) separating each.

>>> dict_to_query({’email’: ‘jonathan.campbell@mcgill.ca’, ‘token’: ‘ABC’})

’email=jonathan.campbell@mcgill.ca&token=ABC’

  • a class Account, with a class attribute API_URL equal to ‘https://coinsbot202.herokuapp.com/api/’.

The Account class will store all relevant data about a user’s COMP202COIN account and also contain methods that operate on that data. Your class should include the following methods:

A constructor that takes an email (string) and token (string) as input. Raise an AssertionError if the types of the inputs are incorrect or if the email does not end in ‘mcgill.ca’. Creates the following instance attributes:

∗ email: set to the input of the same name

∗ token: set to the input of the same name

∗ balance: set to -1

∗ request_log: set to an empty list

>>> my_acct = Account(“jonathan.campbell@mcgill.ca”, “ABC”)

>>> my_acct.balance

-1

An __str__ method that returns a string of the format ‘EMAIL has balance BALANCE’ where

EMAIL and BALANCE refer to the appropriate instance attributes.

>>> my_acct = Account(“jonathan.campbell@mcgill.ca”, “ABC”)

>>> print(my_acct)

jonathan.campbell@mcgill.ca has balance -1

An instance method call_api that takes an endpoint (string) and request dictionary as explicit inputs. If the types of the inputs are incorrect or the endpoint is not valid, raise an AssertionError. The method should add the key ‘token’ into the dictionary with value given by the instance attribute of the same name. It should then construct an API request URL and send the request as indicated on the previous page. If the ‘status’ in the result dictionary is not ‘OK’, raise an AssertionError with the value for the ‘message’ key as error message.

Otherwise, return the result dictionary.

>>> my_acct = Account(“jonathan.campbell@mcgill.ca”, “ABC”)

>>> my_acct.call_api(“balance”, {’email’: my_acct.email})

Traceback (most recent call last):

AssertionError: The token in the API request did not match the token that was sent over Slack.

An instance method retrieve_balance which takes no explicit inputs. Calls the API to retrieve the balance for the current user email. Updates the balance attribute of the current user to the given value (after converting to integer), and returns the integer.

(Note: This example assumes that the correct token was provided.)

>>> my_acct = Account(“jonathan.campbell@mcgill.ca”, “ABC”)

>>> my_acct.retrieve_balance()

1

Question 2: Making Melodies (60 points)

We’ve dealt with images in the Assignment 3. Now it’s time to make music!

A music melody is made up of a series of notes. Each note has a pitch (a letter ranging from A to G) and a duration. Each note is also located in a particular octave from 1 to 7. Higher octaves correspond to a higher frequency. For example, C4 has a higher frequency than C3. Finally, notes may also have accidentals (sharps or flats) that make them sound different than regular notes. Music also has silences which are called rests.

To play our melody, we will use a third-party Python library called musicalbeeps, which we will need to install using the regular steps in Thonny. Depending on your OS and installed software, you may have to download additional software before you can install the library:

  • If you are on Windows, you must download the Microsoft C++ Build Tools. When running the installer, select Workloads – Desktop Development with C++, then for Individual Components,select only Windows SDK and C++ x64/x86 build tools. Wait for the download and installation to complete, then install the library in Thonny.
  • If you are on Mac, then it may work to install the library right away. Or, you may need to go to the Terminal (in Applications > Utilities) and type in xcode-select –install then press return.

Wait for the download and installation to complete, then install the library in Thonny.

Note that the installation of the library in Thonny may take several minutes.

Once installed, you can then import the library and use it as follows:

import musicalbeeps

player = musicalbeeps.Player()

player.play_note(“C4”, 1.0)

The play_note method takes two objects: a note string (with pitch and octave), and a duration (as float). It then plays the specified note through your speakers. The note string can also optionally have a third character: a ‘#’ to specify a sharp note, or a ‘b’ to specify a flat note.

We will write code in this question to read in song files and play them out of the computer speakers using the musicalbeeps library. We will represent notes using scientific pitch notation. In this notation,each note is represented by a letter and a number for its pitch and octave. For example, C4 is a note with pitch C and octave 4. A rest note has pitch R.

Here is an example of a song file. It contains the melody to the popular ‘Happy Birthday to you’ tune.

Happy Birthday

Patty and Mildred J. Hill

.25 D 4 NATURAL false

.25 D 4 NATURAL false

.5 E 4 NATURAL false

.5 D 4 NATURAL false

.5 G 4 NATURAL false

1 F 4 SHARP false

.25 D 4 NATURAL false

.25 D 4 NATURAL false

.5 E 4 NATURAL false

.5 D 4 NATURAL false

.5 A 4 NATURAL false

1 G 4 NATURAL false

.25 D 4 NATURAL false

.25 D 4 NATURAL false

.5 D 5 NATURAL false

.5 B 4 NATURAL false

.5 G 4 NATURAL false

.5 F 4 SHARP false

1 E 4 NATURAL false

.25 C 5 NATURAL false

.25 C 5 NATURAL false

.5 B 4 NATURAL false

.5 G 4 NATURAL false

.5 A 4 NATURAL false

1.5 G 4 NATURAL false

You will observe that the first two lines contain the title and author of the song. Each subsequent line represents a single note. Each note line has five values: the duration of the note (as a float), the pitch,the octave, the accidental value (SHARP, FLAT or NATURAL) and a boolean at the end.

The boolean at the end is known as the repeat value. If a sequence of notes repeats consecutively in the melody, then the repeated notes are not stored in the file. Instead, the sequence that repeats has the repeat value ‘true’ for the first and last of the repeated sequence, to indicate that the entire sequence should be repeated. For example, given the following lines:

0.5 B 4 NATURAL true

0.5 A 4 NATURAL false

1 G 4 NATURAL true

then these three notes form a sequence that should be repeated, because they start and end with a ‘true’ repeat value. If we were loading these lines into our program (as described further below), we would load in six notes: these three, and then the same three repeated right after.

The only special note is a rest note (pitch R). A rest note will not have a value for the octave or accidental. It will only have a duration, then R, then a repeat value.

bestdaixie

评论已关闭。