Skip to content

Commit

Permalink
Fixes and improvements (#364)
Browse files Browse the repository at this point in the history
* Fixed incorrect tags when requested with\without rating after opposite request
* Improved testing
* Fixed some mistyping
* Fixed variable name from anonimus to anonymous
  • Loading branch information
VadVergasov authored Sep 9, 2022
1 parent c7c4a9f commit 2a5f8e8
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 85 deletions.
2 changes: 0 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ updates:
allow:
- dependency-type: direct
- dependency-type: indirect
ignore:
- dependency-name: "idna"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
12 changes: 6 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- develop
tags:
- "*"
pull_request:
pull_request_target:
branches:
- master
- develop
Expand All @@ -23,11 +23,11 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip
uses: actions/cache@v2.1.7
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
Expand All @@ -47,11 +47,11 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Cache pip
uses: actions/cache@v2.1.7
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
Expand All @@ -65,7 +65,7 @@ jobs:
run: |
python setup.py sdist
- name: Publish distribution 📦 to PyPI
uses: pypa/[email protected].0
uses: pypa/[email protected].1
with:
password: ${{ secrets.pypi_password }}
release:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wiki.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install Python 3.8
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install dependencies
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
tests/*
!tests/test_api.py
!tests/test_api_auth.py
!tests/test_parser.py
!tests/conftest.py

!codeforces_api/
Expand Down
8 changes: 4 additions & 4 deletions codeforces_api/api_request_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CodeforcesApiRequestMaker:
_secret = None
_rand = 0
assigned_rand = False
anonimus = False
anonymous = False

def __init__(self, api_key=None, secret=None, random_number=1000000):
"""
Expand All @@ -46,19 +46,19 @@ def __init__(self, api_key=None, secret=None, random_number=1000000):
random_number,
)
if api_key is None and secret is None:
self.anonimus = True
self.anonymous = True
else:
self._api_key = api_key
self._secret = secret
self.anonimus = False
self.anonymous = False
self._rand = random_number

def generate_request(self, method_name, **fields):
"""
Generates request URL and data for API.
"""
request_url = "https://codeforces.com/api/" + str(method_name)
if not self.anonimus:
if not self.anonymous:
# Renew Rand
if not self.assigned_rand:
self.renew_rand()
Expand Down
4 changes: 2 additions & 2 deletions codeforces_api/api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, api_key=None, secret=None, random_number=1000000, method="POS

def blog_entry_comments(self, blog_entry_id):
"""
Get blogEntry.commnets for blog, blog_entry_id required.
Get blogEntry.comments for blog, blog_entry_id required.
Returns parsed response from codeforces.com.
"""
Expand Down Expand Up @@ -309,7 +309,7 @@ def user_friends(self, only_online=False):
Returns parsed response from codeforces.com.
"""
if self.anonimus:
if self.anonymous:
raise TypeError("Auth is required.")
return self._make_request(
"user.friends", **{"onlyOnline": str(only_online).lower()}
Expand Down
28 changes: 17 additions & 11 deletions codeforces_api/parse_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_solution(self, contest_id, submit_id):
contest_id is the id of contest.
submit_id is the id of sumbission.
submit_id is the id of submission.
"""
solutionPage = self.session.get(
"https://codeforces.com/contest/"
Expand All @@ -55,14 +55,15 @@ def get_solution(self, contest_id, submit_id):

def get_tags(self, contest_id, index, include_rating=False):
"""
Get tags of the given problem.
Get tags in lexicographical order of the given problem.
contest_id is the number of the contest.
index is the number of the problem, better to be a capital letter. Also could be an integer or lowercase letter.
include_rating is bool which indicates include or not task rating.
"""
# If we don't have tags we should get them.
if self.problem_tags == dict():
cf_api = CodeforcesApi()
for problem in cf_api.problemset_problems()["problems"]:
Expand All @@ -71,20 +72,25 @@ def get_tags(self, contest_id, index, include_rating=False):
self.problem_tags[str(problem.contest_id)][
str(problem.index)
] = problem.tags
if include_rating:
try:
self.problem_tags[str(problem.problem_id)][
str(problem.index)
].append("*" + str(problem.rating))
except KeyError:
pass
try:
self.problem_tags[str(problem.contest_id)][
str(problem.index)
].append("*" + str(problem.rating))
except KeyError:
pass
self.problem_tags[str(problem.contest_id)][str(problem.index)].sort()
if isinstance(index, int):
index = chr(ord("A") + index)
elif isinstance(index, str):
if index.isnumeric():
index = chr(ord("A") + int(index))
index = index.capitalize()
try:
return self.problem_tags[str(contest_id)][index]
if include_rating:
return self.problem_tags[str(contest_id)][index]
return self.problem_tags[str(contest_id)][index][1:]
except KeyError:
return self.problem_tags[str(int(contest_id) - 1)][index]
# If problem included in more than one division.
if include_rating:
return self.problem_tags[str(int(contest_id) - 1)][index]
return self.problem_tags[str(int(contest_id) - 1)][index][1:]
2 changes: 1 addition & 1 deletion codeforces_api/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.10"
__version__ = "2.0.11"
63 changes: 34 additions & 29 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
appdirs==1.4.4
astroid==2.9.3
atomicwrites==1.4.0
attrs==21.4.0
black==22.1.0
bleach==4.1.0
certifi==2021.10.8
chardet==4.0.0
charset-normalizer==2.0.12
click==8.0.4
colorama==0.4.4
docutils==0.18.1
astroid==2.12.9
atomicwrites==1.4.1
attrs==22.1.0
black==22.8.0
bleach==5.0.1
certifi==2022.6.15
chardet==5.0.0
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.5
commonmark==0.9.1
dill==0.3.5.1
docutils==0.19
idna==2.10
importlib-metadata==4.11.2
importlib-metadata==4.12.0
iniconfig==1.1.1
isort==5.10.1
keyring==23.3.0
keyring==23.9.1
lazy-object-proxy==1.7.1
lxml==4.9.1
mccabe==0.6.1
mypy-extensions==0.4.3
packaging==21.3
pathspec==0.9.0
pkginfo==1.8.2
pathspec==0.10.1
pkginfo==1.8.3
platformdirs==2.5.2
pluggy==1.0.0
py==1.11.0
Pygments==2.11.2
pylint==2.12.2
pyparsing==3.0.7
pytest==7.0.1
Pygments==2.13.0
pylint==2.15.2
pyparsing==3.0.9
pytest==7.1.3
pywin32-ctypes==0.2.0
readme-renderer==33.0
regex==2022.3.2
requests==2.27.1
readme-renderer==37.1
regex==2022.8.17
requests==2.28.1
requests-toolbelt==0.9.1
rfc3986==2.0.0
rich==12.5.1
six==1.16.0
toml==0.10.2
tomli==2.0.1
tqdm==4.63.0
twine==3.8.0
typed-ast==1.5.2
typing-extensions==4.1.1
urllib3==1.26.8
tomlkit==0.11.4
tqdm==4.64.1
twine==4.0.1
typed-ast==1.5.4
typing_extensions==4.3.0
urllib3==1.26.12
webencodings==0.5.1
wrapt==1.13.3
zipp==3.7.0
wrapt==1.14.1
zipp==3.8.1
46 changes: 20 additions & 26 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
from codeforces_api import CodeforcesApi


def test_blog_entry_comments(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_blog_entry_comments():
api = CodeforcesApi()
comments = api.blog_entry_comments(74291)
for comment in comments:
if comment.id == 584151:
assert comment.creation_time_seconds == 1582795345


def test_blog_entry_view(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_blog_entry_view():
api = CodeforcesApi()
blog_entry = api.blog_entry_view(74291)
assert blog_entry.author_handle == "VadVergasov"
assert blog_entry.original_locale == "ru"
Expand All @@ -32,8 +32,8 @@ def test_blog_entry_view(api_key, api_secret):
assert blog_entry.content is None


def test_contest_hacks(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_contest_hacks():
api = CodeforcesApi()
hacks = api.contest_hacks(1311)
for hack in hacks:
if hack.id == 615666:
Expand Down Expand Up @@ -93,8 +93,8 @@ def test_contest_list():
break


def test_contest_rating_changes(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_contest_rating_changes():
api = CodeforcesApi()
changes = api.contest_rating_changes(1313)
for change in changes:
if change.handle == "VadVergasov":
Expand All @@ -107,8 +107,8 @@ def test_contest_rating_changes(api_key, api_secret):
break


def test_contest_standings(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_contest_standings():
api = CodeforcesApi()
standings = api.contest_standings(1313, handles=["VadVergasov"])
assert standings["contest"].id == 1313
assert standings["contest"].name == "Codeforces Round #622 (Div. 2)"
Expand Down Expand Up @@ -197,8 +197,8 @@ def test_contest_status():
assert row.points is None


def test_problemset_problems(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_problemset_problems():
api = CodeforcesApi()
problemset = api.problemset_problems()
for problem in problemset["problems"]:
if problem.name == "Single Push":
Expand All @@ -215,8 +215,8 @@ def test_problemset_problems(api_key, api_secret):
assert isinstance(statistic.contest_id, int)


def test_recent_status(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_recent_status():
api = CodeforcesApi()
status = api.problemset_recent_status(1)[0]
assert isinstance(status.id, int)
assert isinstance(status.creation_time_seconds, int)
Expand All @@ -233,8 +233,8 @@ def test_recent_status(api_key, api_secret):
assert status.points is None or isinstance(status.points, float)


def test_recent_actions(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_recent_actions():
api = CodeforcesApi()
action = api.recent_actions()[0]
assert isinstance(action.time_seconds, int)
if hasattr(action, "blog_entry"):
Expand All @@ -243,8 +243,8 @@ def test_recent_actions(api_key, api_secret):
assert isinstance(action.comment, Comment)


def test_user_blog_entries(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
def test_user_blog_entries():
api = CodeforcesApi()
entries = api.user_blog_entries("VadVergasov")
for entry in entries:
if entry.id == 74291:
Expand All @@ -264,14 +264,8 @@ def test_user_blog_entries(api_key, api_secret):
assert entry.content is None


def test_user_friends(api_key, api_secret):
api = CodeforcesApi(api_key, api_secret)
friends = api.user_friends()
assert "aropan" in friends or "gepardo" in friends


def test_user_info(api_key, api_secret, check_user):
api = CodeforcesApi(api_key, api_secret)
def test_user_info(check_user):
api = CodeforcesApi()
info = api.user_info(["VadVergasov", "tourist"])
for user in info:
check_user(user)
Expand Down
Loading

0 comments on commit 2a5f8e8

Please sign in to comment.