Using class instead of python list

I have the following code:

#!/usr/bin/python # -*- coding: utf-8 -*- import sys import re companies = {} for line in open('/home/ibrahim/Desktop/Test.list'): company, founding_year, number_of_employee = line.split(',') number, name = company.split(")") companies[name] = [name, founding_year, number_of_employee] print "Company: %s" % company CompanyIndex = raw_input('\n<Choose a company you want to know more about.>\n\n<Insert a companyspecific-number and press "Enter" .>\n') if CompanyIndex in companies: name, founding_year, number_of_employee = companies[CompanyIndex] print 'The companys name is: ',name,'\nThe founding year is: ', founding_year,'\nThe amount of employees is: ', number_of_employee else: print"Your input is wrong." 

This program reads some information from a text file that looks like this:

 (1)Chef,1956,10 (2)Fisher,1995,20 (3)Gardener,1998,50 

My goal is to get a class where I can save information about the company name, year of foundation and number of employees instead of using a dictionary that also contains a list.

I read a few tutorials, but I really don't know how to do this. It was really confusing what ā€œIā€ is - what __init__ and __del__ , etc. How should I do it?

+4
source share
4 answers

Here is an example of how you could create a CompanyInfo class.

 class CompanyInfo(object): def __init__(self, name, founded_yr, empl_count): self.name = name self.founded_yr = founded_yr self.empl_count = empl_count def __str__(self): return 'Name: {}, Founded: {}, Employee Count: {}'.format(self.name, self.founded_yr, self.empl_count) 

And here is an example of how you can create it:

 # ... for line in open('/home/ibrahim/Desktop/Test.list'): company, founding_year, number_of_employee = line.split(',') comp_info = CompanyInfo(company, founding_year, number_of_employee) 

And here is an example of how you can use it:

 print "The company info is:", str(comp_info) 
+1
source

You can do:

 class Company(object): def __init__(self, name, founding_year, number_of_employee): self.name = name self.founding_year = founding_year self.number_of_employee = number_of_employee 

After that, you can create a Company object by writing company = Company('Chef', 1956, 10) .

+2
source
 class companies(object): def __init__(self,text_name): text_file = open(text_name,'r') companies = {} all_text = text_file.read() line = all_text.split('\n') #line is a list for element in line: name,year,number = element.split(',') companies[name] = [year,number] self.companies = companies def get_information(self,index): print self.companies[index] #an instance of the class defined above my_company = companies(r'company.txt') #call function of my_company my_company.get_information(r'Gardener') 

I am a new nutritionist - I hope someone can help :)

+1
source
 class Company: def __init__(self, name, year_of_funding, num_of_employees): ''' This is the constructor for the class. We pass the info as arguments, and save them as class member variables ''' self.name = name self.year_of_funding = year_of_funding self.num_of_employees = num_of_employees def get_name(self): ''' This method returns the company name ''' return self.name def get_year_of_funding(self): ''' This method returns the year the company was funded ''' return self.year_of_funding def get_num_of_employees(self): ''' This method returns the number of employees this company has ''' return self.num_of_employees 

Then you can instantiate the class and use get methods to retrieve the data:

 my_company = Company('Microsoft', 1964, 30000) print my_company.get_name() + ' was funded in ' + str(my_company.get_year_of_funding()) + ' and has ' + str(my_company.get_num_of_employees()) + ' employees' # OUTPUT: Microsoft was funded in 1964 and has 30000 employees 
-one
source

Source: https://habr.com/ru/post/1501671/


All Articles