Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added hero_analysis/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions hero_analysis/heroanalyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json # for work with json_files
import glob # module for finding files by pattern
from pathlib import Path

class HeroAnalyzer:
"""A class to analyze superhero data based on gender and occupation
"""

BASE_DIR = Path(__file__).parent.parent # lift up by 2 levels
DEFAULT_DATA_PATH = str(BASE_DIR / 'api' / 'id' / '*.json') #search json files

def __init__(self,gender:str,occupation:bool):
"""Put arguments
Args:
gender (str): Heroes gender to filter ('Male'/'Female')
occupation (bool): Heroes work to filter
"""
self.gender:str = gender.lower()
self.occupation:bool = occupation
self.height_max:int = 0 #stores the maximum height

def main(self) -> str:
"""Find the maximum height of superhero
Work with JSON file in directory and checks each hero:
- Gender
- Occupation
- Height

Returns:
str: Max height of Hero or "No data!"
"""
json_files = glob.glob(self.DEFAULT_DATA_PATH)
if type(self.occupation)!= bool:
return "Not bool type!"
for file in json_files:
try:
with open(file, 'r', encoding='utf-8') as file:
datas = json.load(file)

hero_Gender=datas['appearance']['gender'].lower()
hero_Occupation = datas['work']['occupation']
hero_Height = datas['appearance']['height'][1] # example "180 cm"

hero_Height = float(hero_Height.split()[0]) #take str value to flout
hero_Height = int(round(hero_Height)) #round float value to int

if self._check_string(hero_Occupation) and self._check_gender(hero_Gender) and hero_Height > self.height_max:
self.height_max = hero_Height

except (KeyError, ValueError, IndexError):
continue

return str(self.height_max) if self.height_max !=0 else "No datas!"

def _check_string(self, hero_Occupation:str) -> bool:
"""Check string hero occupation in str
Args:
hero_Occupation (str): Heros occupation
Returns:
bool: If len of str occupation has got symbols return True else False
"""

return bool(len(hero_Occupation)>1) == self.occupation

def _check_gender(self, hero_Gender: str) -> bool:
"""Check hero gender in str
Args:
hero_Gender (str): put gender of hero
Returns:
bool: If gender of hero the same what would put return bool True
"""
return hero_Gender == self.gender

Empty file added hero_analysis/tests/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions hero_analysis/tests/test_heroanalyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from hero_analysis.heroanalyzer import HeroAnalyzer
import pytest

@pytest.mark.parametrize("values1, values2, expected_result", [
('Female', False, '193'), #test by valid datas
('Male', True, '876'), #test by valid datas
('Female', True, '366'), #test by valid datas
('Male', False, '975'), #test by valid datas
('female', True, '366'), #test by register
('FEMALE', False, '193'), #test by register
('MALE', True, '876'), #test by register
('male', False, '975'), #test by register
('Male', "True", 'Not bool type!'), #test data type by work
('Male', "False", 'Not bool type!'), #test data type by work
('Female', "True", 'Not bool type!'), #test data type by work
('Female', "False", 'Not bool type!'), #test data type by work
('asd', False, 'No datas!'), #test invalid datas
('FemaleMale', True, 'No datas!'), #test invalid datas
('MaleFemale', False, 'No datas!'), #test invalid datas
('123', False, 'No datas!'), #test invalid datas
])

def test_class(values1,values2,expected_result):
test = HeroAnalyzer(values1,values2)
assert test.main() == expected_result
49 changes: 7 additions & 42 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,18 @@
# superhero-api
# Superhero Analysis

Multiple universes superheroes open-source REST API

> original source from [superheroapi.com](http://superheroapi.com) edited, cleaned up & made available to [contribution](#contribute)

## API
- [documentation](api)
- [glossary](api/glossary.md)

## Builder

> ! api folder is generated, it's useless editing it, check builder sources instead

#### Clone project
#### change directory
```
git clone https://github.com/akabab/superhero-api.git
cd superhero-api
cd hero_test
```

#### Install dependencies
#### install pytest
```
npm install
pip install -r requirements.txt
```

#### Build API
#### Start test
```
npm run build
python -m pytest
```

#### Release a new version

Edit package.json version then run `release` script
```
npm run release
```

## Contribute

You can contribute by:
- add new or edit images of heroes in the [`builder/sources/images`](https://github.com/akabab/superhero-api/tree/master/builder/sources/images) folder
- submit `powerstats` values for heroes with `null` ones
- suggest new heroes with a complete JSON object
- ..

Check the [Care Center](api/carecenter.md) for needed contributions

**Fork & PR**

---

#### Message for original owners from TwentyEight10
Contact me :)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==8.4.1