Python Forum
Help using class methods
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help using class methods
#1
I have setup the class methods I would like to use instead of the code below the class. I am new to classes and still learning, how to make them work effectively.

Any help or tips would be greatly appreciated!
The code should print the numerical value of the letter according to its location in the alphabet, from the stored dictionary. The text file read in is just the alphabet a-z.

import numpy as np
import string
import collections.abc

class Alphabet(collections.abc.Mapping):
   
   def __len__(self):
       pass
   def __iter__(self):
       pass
   def __getitem__(self,key):
       pass

filename= "alphabet.txt" # Assigns the file to a variable
alphabet= np.genfromtxt(filename, dtype= str) # reads in alphabet from file

my_dict= dict.fromkeys(alphabet,0) # Sets the imported Alphabet letters to keys


i=0 # Initialize the incrementation
alphabet_loc= [] # Initialize the alphabets value storage
for i in range(len(alphabet)): # Iterates as many times as there are characters 
   alph_value= ord(alphabet[i])-96 # finds the ordinal value and minus 96 to get to 1
   alphabet_loc.append(alph_value) # Fills the value array
   my_dict[alphabet[i]]= alphabet_loc[i] # Builds the dictionary Key values

def get_key(): # Function for getting the key from the user.
   input_char=input("Please enter a single letter of the alphabet: ").lower() # User input, converts in lowercase
   allowed_char= string.ascii_letters # used to verify the user input is a letter
   
   while(len(input_char) != 1 or input_char not in allowed_char): # Loop for checking length and character entered
       input_char= input("Please enter a single letter of the alphabet: ").lower() # user input, converts in lowercase
   return input_char # Ends the while loop and return the value

key_char= get_key() # Calls on the function and receives the user input Key

print(my_dict[key_char]) # Print out the the ordinal value for the user inputed Key
Help
Reply
#2
To use the class you have to create an instance so you code should have something like:

alphabet=Alphabet()

alphabet['k'] # will call alphabet.__getitem__('k')
Note that nothing says how you initialize the mapping with data, that's up to you. And __getitem__ can just as well compute the answer instead of
looking it up:

import collections
 
class Alphabet(collections.Mapping):
    
    def __len__(self):
       pass

    def __iter__(self):
       pass

    def __getitem__(self,key):
        return ord((key.lower()[0]))-96
   
alphabet=Alphabet()
print (alphabet['a'])
print (alphabet['A'])
print (alphabet['z'])
Btw, using numpy just to read a file is gross.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020