Skip to content

Commit 4c812c4

Browse files
authored
Small fixes (#7)
* Renamed variables to not conflict with Python reserved words. * Removed wildcard import. * Added Tests passing in README. * Code refactor.
1 parent c0613af commit 4c812c4

File tree

8 files changed

+140
-129
lines changed

8 files changed

+140
-129
lines changed

.github/workflows/codestyle.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ on:
44
push:
55
branches:
66
- master
7+
- develop
78
pull_request:
89
branches:
910
- master
11+
- develop
1012

1113
jobs:
1214
build:

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ on:
44
push:
55
branches:
66
- master
7+
- develop
78
pull_request:
89
branches:
910
- master
11+
- develop
1012

1113
jobs:
1214
build:

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Codeforces API
33
[![Stars](https://img.shields.io/github/stars/VadVergasov/CodeforcesApiPy)](https://github.com/VadVergasov/CodeforcesApiPy/stargazers)
44
[![Forks](https://img.shields.io/github/forks/VadVergasov/CodeforcesApiPy)](https://github.com/VadVergasov/CodeforcesApiPy/network/members)
55
[![Issues](https://img.shields.io/github/issues/VadVergasov/CodeforcesApiPy)](https://github.com/VadVergasov/CodeforcesApiPy/issues)
6-
[![Publish to PyPI and TestPyPI](https://github.com/VadVergasov/CodeforcesApiPy/workflows/Publish%20to%20PyPI%20and%20TestPyPI/badge.svg?branch=master)](https://pypi.org/project/CodeforcesApiPy/)
6+
[![Publish to PyPI and creating Release](https://github.com/VadVergasov/CodeforcesApiPy/workflows/Publish%20to%20PyPI%20and%20TestPyPI/badge.svg?branch=master)](https://pypi.org/project/CodeforcesApiPy/)
77
[![Generate wiki](https://github.com/VadVergasov/CodeforcesApiPy/workflows/Generate%20wiki/badge.svg?branch=master)](https://github.com/VadVergasov/CodeforcesApiPy/wiki)
8+
[![Testing](https://github.com/VadVergasov/CodeforcesApiPy/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/VadVergasov/CodeforcesApiPy/actions/workflows/test.yml)
89
[![Downloads](https://static.pepy.tech/personalized-badge/codeforcesapipy?period=total&units=international_system&left_color=black&right_color=blue&left_text=Total%20downloads)](https://pepy.tech/project/codeforcesapipy)
910
![Codestyle](https://img.shields.io/badge/code%20style-black-000000.svg)
1011
==========

codeforces_api/api_request_maker.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
class CodeforcesApiRequestMaker:
2525

26-
api_key = ""
27-
secret = ""
28-
rand = 0
26+
_api_key = ""
27+
_secret = ""
28+
_rand = 0
2929
assigned_rand = False
3030
anonimus = False
3131

@@ -46,10 +46,10 @@ def __init__(self, api_key=None, secret=None, random_number=1000000):
4646
if api_key is None and secret is None:
4747
self.anonimus = True
4848
else:
49-
self.api_key = api_key
50-
self.secret = secret
49+
self._api_key = api_key
50+
self._secret = secret
5151
self.anonimus = False
52-
self.rand = random_number
52+
self._rand = random_number
5353

5454
def generate_request(self, method_name, **fields):
5555
"""
@@ -65,9 +65,9 @@ def generate_request(self, method_name, **fields):
6565
self.renew_rand()
6666

6767
current_time = time.time()
68-
fields["apiKey"] = str(self.api_key)
68+
fields["apiKey"] = str(self._api_key)
6969
fields["time"] = str(int(current_time))
70-
api_signature = str(self.rand) + "/" + method_name + "?"
70+
api_signature = str(self._rand) + "/" + method_name + "?"
7171
fields = collections.OrderedDict(sorted(fields.items()))
7272
for i in fields:
7373
api_signature += str(i) + "="
@@ -78,9 +78,9 @@ def generate_request(self, method_name, **fields):
7878
api_signature += str(fields[i])
7979
api_signature += "&"
8080
api_signature = api_signature[:-1]
81-
api_signature += "#" + str(self.secret)
81+
api_signature += "#" + str(self._secret)
8282
hashed_signature = hashlib.sha512(api_signature.encode("utf-8"))
83-
fields["apiSig"] = str(self.rand) + str(hashed_signature.hexdigest())
83+
fields["apiSig"] = str(self._rand) + str(hashed_signature.hexdigest())
8484
return {"request_url": request_url, "data": fields}
8585

8686
def check_return_code(self, response):

codeforces_api/api_requests.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@
1717
import requests
1818

1919
from codeforces_api.api_request_maker import CodeforcesApiRequestMaker
20-
from codeforces_api.types import *
20+
from codeforces_api.types import (
21+
User,
22+
BlogEntry,
23+
Comment,
24+
RecentAction,
25+
RatingChange,
26+
Contest,
27+
Problem,
28+
ProblemStatistic,
29+
Submission,
30+
Hack,
31+
RanklistRow,
32+
)
2133

2234

2335
class CodeforcesApi(CodeforcesApiRequestMaker):
@@ -155,9 +167,6 @@ def contest_standings(
155167
handles_str = ""
156168
for handle in handles:
157169
handles_str += str(handle) + ";"
158-
request_data = self.generate_request(
159-
"user.info", **{"handles": handles_str}
160-
)
161170
parameters["handles"] = handles_str
162171
if room != -1:
163172
parameters["room"] = str(room)
@@ -185,7 +194,7 @@ def contest_status(self, contest_id, handle="", start=-1, count=-1):
185194
186195
Returns parsed response from codeforces.com.
187196
"""
188-
if contest_id == None:
197+
if contest_id is None:
189198
raise TypeError("Contest_id is required")
190199
parameters = {"contestId": str(contest_id)}
191200
if handle != "":

codeforces_api/types.py

+22-25
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
1414
You should have received a copy of the GNU General Public License
1515
along with this program. If not, see <https://www.gnu.org/licenses/>.
16-
"""
17-
"""
1816
Source of inspiration: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/telebot/types.py
1917
"""
2018
import json
@@ -60,10 +58,9 @@ def check_json(json_type):
6058
"""
6159
if isinstance(json_type, dict):
6260
return json_type
63-
elif isinstance(json_type, str):
61+
if isinstance(json_type, str):
6462
return json.loads(json_type)
65-
else:
66-
raise ValueError("json_type should be a json dict or string.")
63+
raise ValueError("json_type should be a json dict or string.")
6764

6865
def __str__(self):
6966
d = {}
@@ -194,7 +191,7 @@ def de_json(cls, json_string):
194191
if json_string is None:
195192
return None
196193
obj = cls.check_json(json_string)
197-
id = obj["id"]
194+
identifier = obj["id"]
198195
original_locale = obj["originalLocale"]
199196
creation_time_seconds = obj["creationTimeSeconds"]
200197
author_handle = obj["authorHandle"]
@@ -206,7 +203,7 @@ def de_json(cls, json_string):
206203
rating = obj["rating"]
207204
content = obj.get("content")
208205
return cls(
209-
id,
206+
identifier,
210207
original_locale,
211208
creation_time_seconds,
212209
author_handle,
@@ -221,7 +218,7 @@ def de_json(cls, json_string):
221218

222219
def __init__(
223220
self,
224-
id,
221+
identifier,
225222
original_locale,
226223
creation_time_seconds,
227224
author_handle,
@@ -233,7 +230,7 @@ def __init__(
233230
rating,
234231
content=None,
235232
):
236-
self.id = id
233+
self.id = identifier
237234
self.original_locale = original_locale
238235
self.creation_time_seconds = creation_time_seconds
239236
self.author_handle = author_handle
@@ -267,15 +264,15 @@ def de_json(cls, json_string):
267264
if json_string is None:
268265
return None
269266
obj = cls.check_json(json_string)
270-
id = obj["id"]
267+
identifier = obj["id"]
271268
creation_time_seconds = obj["creationTimeSeconds"]
272269
commentator_handle = obj["commentatorHandle"]
273270
locale = obj["locale"]
274271
text = obj["text"]
275272
rating = obj["rating"]
276273
parent_comment_id = obj.get("parentCommentId")
277274
return cls(
278-
id,
275+
identifier,
279276
creation_time_seconds,
280277
commentator_handle,
281278
locale,
@@ -286,15 +283,15 @@ def de_json(cls, json_string):
286283

287284
def __init__(
288285
self,
289-
id,
286+
identifier,
290287
creation_time_seconds,
291288
commentator_handle,
292289
locale,
293290
text,
294291
rating,
295292
parent_comment_id=None,
296293
):
297-
self.id = id
294+
self.id = identifier
298295
self.creation_time_seconds = creation_time_seconds
299296
self.commentator_handle = commentator_handle
300297
self.locale = locale
@@ -401,7 +398,7 @@ def de_json(cls, json_string):
401398
if json_string is None:
402399
return None
403400
obj = cls.check_json(json_string)
404-
id = obj["id"]
401+
identifier = obj["id"]
405402
name = obj["name"]
406403
contest_type = obj["type"]
407404
phase = obj["phase"]
@@ -419,7 +416,7 @@ def de_json(cls, json_string):
419416
city = obj.get("city")
420417
season = obj.get("season")
421418
return cls(
422-
id,
419+
identifier,
423420
name,
424421
contest_type,
425422
phase,
@@ -440,7 +437,7 @@ def de_json(cls, json_string):
440437

441438
def __init__(
442439
self,
443-
id,
440+
identifier,
444441
name,
445442
contest_type,
446443
phase,
@@ -458,7 +455,7 @@ def __init__(
458455
city=None,
459456
season=None,
460457
):
461-
self.id = id
458+
self.id = identifier
462459
self.name = name
463460
self.contest_type = contest_type
464461
self.phase = phase
@@ -650,7 +647,7 @@ def de_json(cls, json_string):
650647
if json_string is None:
651648
return None
652649
obj = cls.check_json(json_string)
653-
id = obj["id"]
650+
identifier = obj["id"]
654651
creation_time_seconds = obj["creationTimeSeconds"]
655652
relative_time_seconds = obj["relativeTimeSeconds"]
656653
problem = Problem.de_json(obj["problem"])
@@ -664,7 +661,7 @@ def de_json(cls, json_string):
664661
verdict = obj.get("verdict")
665662
points = obj.get("points")
666663
return cls(
667-
id,
664+
identifier,
668665
creation_time_seconds,
669666
relative_time_seconds,
670667
problem,
@@ -681,7 +678,7 @@ def de_json(cls, json_string):
681678

682679
def __init__(
683680
self,
684-
id,
681+
identifier,
685682
creation_time_seconds,
686683
relative_time_seconds,
687684
problem,
@@ -695,7 +692,7 @@ def __init__(
695692
verdict=None,
696693
points=None,
697694
):
698-
self.id = id
695+
self.id = identifier
699696
self.creation_time_seconds = creation_time_seconds
700697
self.relative_time_seconds = relative_time_seconds
701698
self.problem = problem
@@ -733,7 +730,7 @@ def de_json(cls, json_string):
733730
if json_string is None:
734731
return None
735732
obj = cls.check_json(json_string)
736-
id = obj["id"]
733+
identifier = obj["id"]
737734
creation_time_seconds = obj["creationTimeSeconds"]
738735
hacker = Party.de_json(obj["hacker"])
739736
defender = Party.de_json(obj["defender"])
@@ -742,7 +739,7 @@ def de_json(cls, json_string):
742739
test = obj.get("test")
743740
judge_protocol = obj.get("judgeProtocol")
744741
return cls(
745-
id,
742+
identifier,
746743
creation_time_seconds,
747744
hacker,
748745
defender,
@@ -754,7 +751,7 @@ def de_json(cls, json_string):
754751

755752
def __init__(
756753
self,
757-
id,
754+
identifier,
758755
creation_time_seconds,
759756
hacker,
760757
defender,
@@ -763,7 +760,7 @@ def __init__(
763760
test=None,
764761
judge_protocol=None,
765762
):
766-
self.id = id
763+
self.id = identifier
767764
self.creation_time_seconds = creation_time_seconds
768765
self.hacker = hacker
769766
self.defender = defender

codeforces_api/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.0.5"
1+
__version__ = "2.0.6"

0 commit comments

Comments
 (0)