r/madeinpython • u/westo48 • May 12 '20
Clash of Clans API framework
I still have a lot to work on, but I made a framework for the Clash of Clans API. I run a discord bot that I also designed for our clan and realized I was doing way too much work keeping up with the mess of code. I made the framework using OOP principles and trying to focus on using clean code (as much as I could lol). Again I still have a lot to work on and it may not be much, but I designed it from scratch and I love it! I would love lots of feedback if possible.
2
u/nandahkrishna May 12 '20
Looks nice for a WIP! You seem to have adhered to PEP8 at first glance, neat code is always a plus :)
2
u/westo48 May 12 '20
Thank you! I tried to make sure that everything was in the class that it should be in, and had a reason to be there. Forced myself to try to make lots of comments lol.
2
u/YoelkiToelki May 12 '20
Except for the 80 characters per line limit ... who cares though that guideline is ridiculous anyway!
1
u/westo48 May 12 '20
If I’m being honest I haven’t gone through the ACTUAL guidelines. I just focused on the OOP portion of it. A lot of the code uses a ton of characters because of the api access (json iteration and what not), so it gets a little hairy in the get methods. I’m hoping to update it as time goes on, and make sure that I use good version control, start using PEP8 guidelines, and try not to clutter everything lol. It’s my first larger project, so I’ve been excited/obsessed. Whichever you want to call it haha
3
u/YoelkiToelki May 12 '20
You could cut down on some space using the unpacking operator * on the jsons, for example, when u make a new clan instance. Just make sure the json values and clan parameters are in order
1
u/westo48 May 12 '20
I haven’t seen that, I’ll have to look into it!! My only concern (without reading anything about it mind you) is if the API return values vary at all then it could prove challenging. Sometimes values are there and sometimes they just don’t exist (if the player doesn’t have the troop, bldg, etc then it just won’t be included in the json response, which IMO it should return 0 or something like that for uniformity, but I’m inexperienced, so idk)
1
u/westo48 May 14 '20
I made some of those guideline changes (it is in the only non-master branch). If you have a moment I would appreciate your input!
2
u/YoelkiToelki May 14 '20
Link to github page for Clash-Frame
I looked and it seems like you have docstrings at the top of files, outside of classes/functions/methods. Refer to this to get simple help on how to put docstrings in your code.
I also saw that you would put each parameter of an init function on its own line which isn't necessary and, in my opinion, harder to read and understand. If you want to adhere strictly to PEP8, break the line once you get as far as you can to 80 chars.
Also, I still recommend that you try to use the unpacking operator, for instance:
clan_json = clan_json = json_response(clan_tag, header)
//do whatever here to extract your other info
return Clan(*clan_json, donation_upgrade, members)this of course means you have to put donation_upgrade and members at the end of the Clan init function and make sure all the clan_json values line up accordingly in the init function so each value is stored where it should be
1
u/westo48 May 14 '20
Oooooh I see now, so just break the lines at 80 for the most part. I gotcha. I was looking at the PEP8 guidelines, but the examples weren’t very long. I had to try and decipher for the most part lol.
As for the unpack operator, it really is difficult since I don’t add all the information that the json response uses some of them get split into different classes and others just don’t get used. I’ll definitely look into implementing it though!!
Again I appreciate all the advice and time!!
1
u/YoelkiToelki May 14 '20
What kind of data is stored in the json response that you don’t use ?
1
u/westo48 May 14 '20
Things like labels, achievements, icon urls, things like that, so some information gets skipped over. Some information doesn’t get pulled at all either.
For example: if someone is a town hall 12 they have a weapon on their town hall, and the api will return a json response that includes “townHallWeaponLevel” and it will be an int. But if they aren’t at 12 yet it will not return that at all. I would have thought it would have returned 0, but it just doesn’t exist. Same goes for troops that haven’t been unlocked yet. Honestly it is kind of a mess to work with since it doesn’t return the same thing all the time. Is that normal (genuine question)?
Either way I definitely need to learn how to better make use of the unpack operator!
4
u/westo48 May 12 '20
The big thing I am going to try to work on next is error validation and things like that. I am not very good at error handling other than writing code to work around it (which doesn't always turn out very pretty haha), so it will be good to learn it!