Skip to content

shaikh-ma/swapi_assignment

Repository files navigation

Task Objectives:

  1. To get the data from the The Star Wars API server.
  2. Extract the 10 characters appearing in most films.
  3. Sort the data by height.
  4. Create a csv file of the sorted data.
  5. Upload the data on the Httpbin.org server.

How to prepare the enviroment :

To clone the project use the below:

git clone https://github.com/shaikh-ma/swapi_assignment/

How to execute the program:

To execute the program, run the script 'main.py'

python main.py

How to execute the test:

To execute the tests, run the script 'test_swapi_assignment.py'

python test_swapi_assignment.py


About the script :

The script has been designed using the below two approaches:

  • Approach 1 : Using Python Standard libraries & builtin functions if in case the 3rd party packages are not installed.
  • Approach 2 : Using 3rd party libraries (like pandas, request) in case if these packages are already installed on the user system.



There are total 3 scripts,

Script name What it does
main.py This is the main script, it checks if the pandas, request libraries are available or not and selects the script based on it, to run the program.
assignment_with_std_libraries.py This script uses the standard modules (urllib, json, csv) with builtin functions.
assignment_with_non_std_libraries.py This script uses the 3rd party modules (pandas and requests).



Functions defined in the script(s)

  • get_data(url)
  • Retrieves data from the server.
  • parameter: url -> url of the server from which to get the data.
  • returns the data if received, otherwise None.
  • get_top_characters(json_data, max_characters=10)
  • Extractes the requred data into a json object.
  • Parameters :
  • json_data : The json file containing the data from server.
  • max_characters : Number of characters for which the data should be filtered.
  • By default, extracts data for 10 characters if no value is passed.
  • Returns json format for the number of characters appearing in most films.
  • write_data(json_data, filename=None)
  • Stores the extracted data in a csv file.
  • If the file name is passed, otherwise it's named as "Exported.csv".
  • The file is saved in the same folder as the script.
  • Returns the filename.
  • show_data(selected_characters, max_characters=None)
  • Prints out the json formatted data for exported data for max_characters passed.
  • post_data(url, filename='')
  • Uploading the files to the server.
  • url -> server URL.
  • filename -> Name of the file to be uploaded.
  • Returns the tuple containing response object from the request and the status code.

Output :

Exported data

generated_csv

Generated CSV file content :

Sr.no Character name Species Height Appearances
1 Darth Vader - 202 4
2 Biggs Darklighter - 183 1
3 Obi-Wan Kenobi - 182 6
4 Owen Lars - 178 3
5 Luke Skywalker - 172 4
6 C-3PO Droid 167 6
7 Beru Whitesun lars - 165 3
8 Leia Organa - 150 4
9 R5-D4 Droid 97 1
10 R2-D2 Droid 96 6

Response from HttpBin.org for POST request:

results

Test Results :

test_Result



Date Created : 13-08-2022

Project Type : Test Assignment

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages