Пример #1
0
    def handle(self, *args, **options):

        with open('pokemon.json', 'r') as file:
            text = file.read()
            data = json.loads(text)

            for pokemans in data['pokemon']:
                number = int(pokemans['number'])
                name = pokemans['name']
                height = int(pokemans['height'])
                weight = int(pokemans['weight'])
                image_front = pokemans['image_front']
                image_back = pokemans['image_back']
                types = pokemans['types']

                add_pokemon = Pokemon(
                    number=number,
                    name=name,
                    height=height,
                    weight=weight,
                    image_front=image_front,
                    image_back=image_back,
                )
                add_pokemon.save()
                for typ in types:
                    add_type, other = PokemonType.objects.get_or_create(name=typ)
                    add_pokemon.types.add(add_type)
                    
            # print(pokemans)
Пример #2
0
    def handle(self, *args, **options):
        # delete everything at the start
        #this was also tested successfully
        Pokemon.objects.all().delete()

        # Open the file with the pokemon in json in reacing mode
        # The with statement guarantees the file will be closed after the with statement ends.
        with open('./pokedex/management/commands/pokemon.json', 'r') as file:
            # Read the contents of the file
            contents = file.read()
            # print(contents ) ##this worked

            # Parse the json string
        data = json.loads(contents)
        # loop over the json string, and get the data from it
        for monster in data['pokemon']:
            number = monster['number']
            name = monster['name']
            height = monster['height']
            weight = monster['weight']
            image_front = monster['image_front']
            image_back = monster['image_back']
            types = monster['types']
            url = monster['url']
            monster = Pokemon(number=number, name=name, height=height,
                              weight=weight, image_front=image_front, image_back=image_back,
                              types=types, url=url)

            monster.save()
Пример #3
0
    def handle(self, *args, **options):
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()
        with open('./pokedex/management/commands/pokemon.json', 'r') as file:
            text = file.read()
        data = json.loads(text)
        for pokemon in data['pokemon']:
            number = pokemon['number']
            name = pokemon['name']
            height = pokemon['height']
            weight = pokemon['weight']
            image_front = pokemon['image_front']
            image_back = pokemon['image_back']
            types = pokemon['types']

            new_pokemon = Pokemon(
                number=number,
                name=name,
                height=height,
                weight=weight,
                image_front=image_front,
                image_back=image_back,
            )
            new_pokemon.save()
            for ptype in types:
                p_types1, created = PokemonType.objects.get_or_create(
                    name=ptype)
                new_pokemon.types.add(p_types1)
Пример #4
0
    def handle(self, *args, **options):

        #erases current tables from database if need to redo load
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()

        with open('./pokedex/management/commands/pokemon.json', 'r') as file: # open file via relative path
            data = json.loads(file.read()) # read the file and convert json to dictionary

        # looping over the contact data inside that dictionary
        for pokemon_data in data['pokemon']:
            number = pokemon_data['number']
            name = pokemon_data['name']
            height = pokemon_data['height']
            weight = pokemon_data['weight']
            image_front = pokemon_data['image_front']
            image_back = pokemon_data['image_back']
            url = pokemon_data['url']
        
            pokemon = Pokemon(number=number,
                                name=name,
                                height=height,
                                weight=weight,
                                image_front=image_front, 
                                image_back=image_back,
                                url=url)
            pokemon.save()

            for typ_data in pokemon_data['types']:
                pok_typ, created = PokemonType.objects.get_or_create(name=typ_data)
                pokemon.types.add(pok_typ)
Пример #5
0
    def handle(self, *args, **options):
        # Pete: 'now it's the recipe, not the meal'
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()
        # write the code here
        with open('pokemon.json', 'r') as file:
            text = file.read()
        # the ENTIRE dictionary
        data = json.loads(text)
        # how to get these things per pokemon
        for pokemon_data in data['pokemon']:
            pokemon = Pokemon(name=pokemon_data['name'],
                              number=pokemon_data['number'],
                              height=pokemon_data['height'],
                              weight=pokemon_data['weight'],
                              image_front=pokemon_data['image_front'],
                              image_back=pokemon_data['image_back'],
                              url=pokemon_data['url'])

            # doing it the KWARG shortcut way
            # pokemon = Pokemon(**pokemon_data)

            pokemon.save()

            # list of strings, iterating
            for type_str in pokemon_data['types']:
                # unpacking 'get_or_create' not the above
                types, created = PokemonType.objects.get_or_create(
                    name=type_str)
                # appending it
                pokemon.types.add(types)
Пример #6
0
    def handle(self, *args, **options):
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()
        response = requests.get(
            'https://raw.githubusercontent.com/PdxCodeGuild/class_mountain_goat/master/4%20Django/labs/pokemon.json'
        )
        data = response.json()
        # print(data)
        for pokemon_data in data['pokemon']:
            number = pokemon_data['number']
            name = pokemon_data['name']
            height = pokemon_data['height']
            weight = pokemon_data['weight']
            image_front = pokemon_data['image_front']
            image_back = pokemon_data['image_back']
            type_names = pokemon_data['types']

            pokemon = Pokemon(number=number,
                              name=name,
                              height=height,
                              weight=weight,
                              image_front=image_front,
                              image_back=image_back)
            pokemon.save()

            # print(type_names)
            for type_name in type_names:
                # print(type_name)
                pokemon_type, created = PokemonType.objects.get_or_create(
                    name=type_name)
                pokemon.types.add(pokemon_type)
Пример #7
0
 def handle(self, *args, **options):
     # Naively assume everything works always
     (pokemon_list,
      links) = getEvolutionChain(options["evolution_chain_id"])
     for info in pokemon_list:
         self.stdout.write("saving pokemon {}".format(info))
         p = Pokemon(**info)
         p.save()
     for (p_from, p_to) in links:
         self.stdout.write("saving link ({}, {})".format(p_from, p_to))
         evolution = Pokemon.objects.filter(id=p_to)[0]
         pre_evolution = Pokemon.objects.filter(id=p_from)[0]
         evolution.pre_evolution = pre_evolution
         evolution.save()
Пример #8
0
    def handle(self, *args, **options):
        # This is to delete all the rows out of the table each time I run the python3 manage.py load_pokemon it will load a fresh data without dupplicating on the database.
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()
        # open the file containing the json and  we need to add the relative path with open below: './pokedex/management/commands/pokemon.json'
        with open('./pokedex/management/commands/pokemon.json', 'r') as file:
            # reading the text in the file
            text = file.read()
            # turning that json string into a python dictionary
            data = json.loads(text)  # bring the variable data to the for loop
            print(data)
            pokemon = data['pokemon']
            # return ''
            print(pokemon)

            # looping over the contact data inside that dictionary
        for pika_data in data['pokemon']:

            # getting out the contact data into local variables
            number = pika_data['number']
            # .title() TO MAKE THE FIRST NAME CAPITAL
            name = pika_data['name'].title()
            height = pika_data['height']
            weight = pika_data['weight']
            image_front = pika_data['image_front']
            image_back = pika_data['image_back']
            types = pika_data['types']

            # create a pokemon from our data
            pokemon = Pokemon(number=number,
                              name=name,
                              height=height,
                              weight=weight,
                              image_front=image_front,
                              image_back=image_back)
            # save the contact to the database
            pokemon.save()

            # loop through each type and associate go through each items and if the type is not there then create it.
            # here we are looping over the dictionary and looking for the data with the key types
            # looping poke_type in the dictionary call pika_data selecting the types ['types']
            for poke_type in pika_data['types']:
                types, created = PokemonType.objects.get_or_create(
                    name=poke_type)
                pokemon.types.add(types)
Пример #9
0
def data_pokemon(id, chain_id):
	url = "http://pokeapi.co/api/v2/pokemon/" + id

	response = requests.request("GET", url)
	data_base = response.json()

	poke_ball = Pokemon(id_pokemon = data_base['id'],
		name = data_base['name'],
		height = float(data_base['height']),
		weight = float(data_base['weight']),
		hp_base = int(data_base['stats'][0]['base_stat']),
		attack_base = int(data_base['stats'][1]['base_stat']),
		attack_sp_base = int(data_base['stats'][3]['base_stat']),
		defense_base = int(data_base['stats'][2]['base_stat']),
		defense_sp_base = int(data_base['stats'][4]['base_stat']),
		speed_base = int(data_base['stats'][5]['base_stat']),
		chain_evol_id = int(chain_id)
	)
	poke_ball.save()
Пример #10
0
def add_pokemon(pokemon):
    all_types = Type.objects.all()
    new_type = ''
    new_pokemon = Pokemon(number=pokemon['number'],
                          name=pokemon['name'],
                          height=pokemon['height'],
                          weight=pokemon['weight'],
                          image_front=pokemon['image_front'],
                          image_back=pokemon['image_back'])
    new_pokemon.save()
    for i in range(len(pokemon['types'])):
        if not Type.objects.filter(name=pokemon['types'][i]):
            print(pokemon['types'][i])
            new_type = Type(name=pokemon['types'][i])
            new_type.save()
        else:
            new_type = Type.objects.get(name=pokemon['types'][i])
        new_pokemon.types.add(new_type)
    print(all_types)
    print(new_pokemon)
    def handle(self, *args, **options):
        with open('pokedex/management/commands/pokemon.json', 'r') as poketext:
            pokejson = poketext.read()
        pokedata = json.loads(pokejson)
        for pokemon_data in pokedata['pokemon']:
            number = pokemon_data['number']
            name = pokemon_data['name']
            height = pokemon_data['height']
            weight = pokemon_data['weight']
            image_front = pokemon_data['image_front']
            image_back = pokemon_data['image_back']
            type_names = pokemon_data['types']

            pokemon = Pokemon(number=number,
                              name=name,
                              height=height,
                              weight=weight,
                              image_front=image_front,
                              image_back=image_back)
            pokemon.save()

            for type_name in type_names:
                pokemon_type, created = PokemonType.objects.get_or_create(
                    name=type_name)
                pokemon.types.add([pokemon_type])

            pokemon.save()
Пример #12
0
    def handle(self, *args, **options):
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()
        with open('./pokedex/management/commands/pokemon.json', 'r') as file:
            text = file.read()
        data = json.loads(text)
        # print(data)
        pokemon = data['pokemon']
        print(pokemon)

        for pokemon_data in data['pokemon']:
            pokemon = Pokemon(number=pokemon_data['number'],
                              name=pokemon_data['name'],
                              height=pokemon_data['height'],
                              weight=pokemon_data['weight'],
                              image_front=pokemon_data['image_front'],
                              image_back=pokemon_data['image_back'])

            pokemon.save()

            for type_str in pokemon_data['types']:
                type, created = PokemonType.objects.get_or_create(
                    name=type_str)
                pokemon.types.add(type)

            pokemon.save()
Пример #13
0
    def handle(self, *args, **options):

        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()

        # response = requests.get('https://raw.githubusercontent.com/PdxCodeGuild/class_armadillo/master/4%20Django/labs/pokemon.json')
        # print(response.text)

        with open('./pokedex/management/commands/pokemon.json', 'r') as file:
            text = file.read()
        data = json.loads(text)
        for pokemon_data in data['pokemon']:
            # pokemon = Pokemon(**pokemon_data)
            # pokemon.types=...

            pokemon = Pokemon(number=pokemon_data['number'],
                              name=pokemon_data['name'],
                              height=pokemon_data['height'],
                              weight=pokemon_data['weight'],
                              image_front=pokemon_data['image_front'],
                              image_back=pokemon_data['image_back'],
                              url=pokemon_data['url'])
            pokemon.save()

            for type_str in pokemon_data['types']:
                type, created = PokemonType.objects.get_or_create(
                    name=type_str)
                # type = PokemonType(name=type_str)
                # type.save()

                pokemon.types.add(type)


# def kwargs(**data):
#     print(data)
# kwargs(a=1,b=2,c=3)
# kwargs(**{'a':1, 'b':2, 'c':3})
Пример #14
0
    def handle(self, *args, **options):
        with open('pokemon.json', 'r') as file:
            data = file.read()

        all_pokemon = json.loads(data)['pokemon']
        Pokemon.objects.all().delete()
        PokemonType.objects.all().delete()

        for pokemon in all_pokemon:
            db_types = []
            for type in pokemon['types']:
                obj, created = PokemonType.objects.get_or_create(name=type)
                db_types.append(obj)
            mon = Pokemon()
            mon.number = pokemon['number']
            mon.name = pokemon['name']
            mon.height = pokemon['height']
            mon.weight = pokemon['weight']
            mon.image_front = pokemon['image_front']
            mon.image_back = pokemon['image_back']
            mon.save()

            for type in db_types:
                print(type)
                mon.types.add(type)

            mon.save()
Пример #15
0
    def handle(self, *args, **kwargs):
        Pokemon.objects.all().delete()
        Skutecznosc.objects.all().delete()

        max_numer = 151
        # Interesują nas tylko pokemony pierwszej generacji, tj. 1-151
        print 'Tworzenie rekordów pokemonów...'
        pokemony = {i: Pokemon(numer=i) for i in range(1, max_numer + 1)}

        print 'Dodawanie nazw pokemonów...'
        handle = open(
            os.path.join(settings.BASE_DIR, 'csv',
                         'pokemon_species_names.csv'), 'r')
        csvr = csv.reader(handle, delimiter=',')
        next(csvr, None)  # pomijamy header
        for row in csvr:
            numer = int(row[0])
            if numer > max_numer:
                break
            if row[1] == '9':  # nazwy angielskie
                pokemony[numer].nazwa = row[2]

        print 'Dodawanie ewolucji pokemonów...'
        handle = open(
            os.path.join(settings.BASE_DIR, 'csv', 'pokemon_species.csv'), 'r')
        csvr = csv.reader(handle, delimiter=',')
        next(csvr, None)  # pomijamy header
        for row in csvr:
            numer = int(row[0])
            if numer > max_numer:
                break
            if row[3] != '' and int(row[3]) <= max_numer:
                pokemony[int(row[3])].ewolucja = pokemony[numer].nazwa

        print 'Przygotowywanie nazw rodzajów...'
        rodzaje = dict()
        handle = open(
            os.path.join(settings.BASE_DIR, 'csv', 'type_names_pl.csv'), 'r')
        csvr = csv.reader(handle, delimiter=',')
        next(csvr, None)  # pomijamy header
        for row in csvr:
            rodzaje[int(row[0])] = row[1]

        print 'Dodawanie rodzajów pokemonów...'
        handle = open(
            os.path.join(settings.BASE_DIR, 'csv', 'pokemon_types.csv'), 'r')
        csvr = csv.reader(handle, delimiter=',')
        next(csvr, None)  # pomijamy header
        for row in csvr:
            numer = int(row[0])
            if numer > max_numer:
                break
            if row[2] == '1':
                pokemony[numer].rodzaj1 = rodzaje[int(row[1])]
            elif row[2] == '2':
                pokemony[numer].rodzaj2 = rodzaje[int(row[1])]

        print 'Zapisywanie rekordów pokemonów...'
        for p in pokemony:
            pokemony[p].save()

        print 'Tworzenie tabeli skuteczności ataków...'
        handle = open(
            os.path.join(settings.BASE_DIR, 'csv', 'type_efficacy.csv'), 'r')
        csvr = csv.reader(handle, delimiter=',')
        next(csvr, None)  # pomijamy header
        for row in csvr:
            if int(row[2]) != 100:
                s = Skutecznosc(atak=rodzaje[int(row[0])],
                                obrona=rodzaje[int(row[1])],
                                mnoznik=float(row[2]) / 100)
                s.save()

        print 'Gotowe!'
Пример #16
0
from pokedex.models import Pokemon

pokemon_list = [
    Pokemon(name="Pikachu", is_yellow=True),
    Pokemon(name="Bulbasaur", is_yellow=False),
    Pokemon(name="Eevee", is_yellow=False),
    Pokemon(name="Psyduck", is_yellow=True),
    Pokemon(name="Squirtle", is_yellow=False)
]

[p.save() for p in pokemon_list]

######

speed = models.IntegerField(null=True)
series = models.IntegerField(default=1)
date_added = models.DateField(null=True)

######

from datetime import datetime

pokemon_list = [
    Pokemon(name="Magikarp", is_yellow=False, date_added=datetime(2015, 7,
                                                                  13)),
    Pokemon(name="Pidgey", is_yellow=False, date_added=datetime(2017, 1, 24))
]

[p.save() for p in pokemon_list]

######