Skip to content

Web programming contribution #2436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 21, 2020

Conversation

niranjan195
Copy link
Contributor

@niranjan195 niranjan195 commented Sep 16, 2020

Describe your change:

Added Currency Converter Program to web_programming directory.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@TravisBuddy
Copy link

Hey @niranjanhegde144,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 62e11780-f812-11ea-8f9f-a900ee46d44b

@niranjan195 niranjan195 reopened this Sep 16, 2020
@niranjan195
Copy link
Contributor Author

Clauss Can you take a look at this

Comment on lines 5 to 7
import requests

API_KEY = "" # <-- Put your API Key here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to change this. For security reasons, we do not want to encourage users to put security credentials into Python files. Instead, we will get the key at runtime from an environment variable.

The user will run the program with this command:
AMDOREN_API_KEY="This is my key" python3 web_programming/currency_converter.py

Suggested change
import requests
API_KEY = "" # <-- Put your API Key here
import os
import requests
assert API_KEY := os.environ["AMDOREN_API_KEY"], (
"Please put your API key in an environment variable."
)

Comment on lines 173 to 186
def convert_currency(
baseCurrency: str = "USD",
targetCurrency: str = "INR",
amount: float = 1.0,
apiKey: str = API_KEY,
) -> str:
"""https://www.amdoren.com/currency-api/"""
res = requests.get(
f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\
amount={amount}"
).json()
if res["error"] == 0:
return str(res["amount"])
return res["error_message"]
Copy link
Member

@cclauss cclauss Sep 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def convert_currency(
baseCurrency: str = "USD",
targetCurrency: str = "INR",
amount: float = 1.0,
apiKey: str = API_KEY,
) -> str:
"""https://www.amdoren.com/currency-api/"""
res = requests.get(
f"{URL_BASE}?api_key={API_KEY}&from={baseCurrency}&to={targetCurrency}&\
amount={amount}"
).json()
if res["error"] == 0:
return str(res["amount"])
return res["error_message"]
def convert_currency(
from_: str = "USD",
to: str = "INR",
amount: float = 1.0,
api_key: str = API_KEY
) -> str:
"""https://www.amdoren.com/currency-api/"""
params = locals()
params["from"] = params.pop("from_") # 'from' is a Python keyword
res = requests.get(URL_BASE, params=params).json()
return str(res["amount"] if res["error"] == 0 else res["error_message"])

There are nice advantages in requests and locals() if our function parameter names exactly match the API.

Comment on lines 190 to 194
base_currency = input("Enter base currency: ").strip()
target_currency = input("Enter target currency: ").strip()
amount = float(input("Enter the amount: ").strip())
print(
convert_currency(
baseCurrency=base_currency, targetCurrency=target_currency, amount=amount
)
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
base_currency = input("Enter base currency: ").strip()
target_currency = input("Enter target currency: ").strip()
amount = float(input("Enter the amount: ").strip())
print(
convert_currency(
baseCurrency=base_currency, targetCurrency=target_currency, amount=amount
)
)
print(
convert_currency(
input("Enter from currency: ").strip(),
input("Enter to currency: ").strip(),
float(input("Enter the amount: ").strip())
)
)

@niranjan195 niranjan195 force-pushed the web_programming_contribution branch from 6f96b53 to bef8610 Compare September 26, 2020 09:58
@TravisBuddy
Copy link

Hey @niranjanhegde144,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 61ca28d0-ffdf-11ea-9882-0125ab0f75bf

@TravisBuddy
Copy link

Travis tests have failed

Hey @niranjanhegde144,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 268e7950-ffe0-11ea-9882-0125ab0f75bf

@cclauss
Copy link
Member

cclauss commented Sep 26, 2020

@stale
Copy link

stale bot commented Nov 21, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Used to mark an issue or pull request stale. label Nov 21, 2020
@ghost ghost added the require tests Tests [doctest/unittest/pytest] are required label Nov 21, 2020
@ghost ghost added the tests are failing Do not merge until tests pass label Nov 21, 2020
@ghost ghost removed the tests are failing Do not merge until tests pass label Nov 21, 2020
@cclauss cclauss merged commit f036b9f into TheAlgorithms:master Nov 21, 2020
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Currency Converter

* currency converter

* Currency Converter

* currency converter

* implemented changes

* Implemented changes requested

* TESTING = os.getenv("CONTINUOUS_INTEGRATION", False)

* Update currency_converter.py

* Update currency_converter.py

Co-authored-by: Christian Clauss <[email protected]>
peRFectBeliever pushed a commit to peRFectBeliever/Python that referenced this pull request Apr 1, 2021
* Currency Converter

* currency converter

* Currency Converter

* currency converter

* implemented changes

* Implemented changes requested

* TESTING = os.getenv("CONTINUOUS_INTEGRATION", False)

* Update currency_converter.py

* Update currency_converter.py

Co-authored-by: Christian Clauss <[email protected]>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
* Currency Converter

* currency converter

* Currency Converter

* currency converter

* implemented changes

* Implemented changes requested

* TESTING = os.getenv("CONTINUOUS_INTEGRATION", False)

* Update currency_converter.py

* Update currency_converter.py

Co-authored-by: Christian Clauss <[email protected]>
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Currency Converter

* currency converter

* Currency Converter

* currency converter

* implemented changes

* Implemented changes requested

* TESTING = os.getenv("CONTINUOUS_INTEGRATION", False)

* Update currency_converter.py

* Update currency_converter.py

Co-authored-by: Christian Clauss <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
require tests Tests [doctest/unittest/pytest] are required stale Used to mark an issue or pull request stale.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants