4

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 which looks like this:

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

My aim is to get a class, where I can save the information about the company's name, the founding year, and the number of employees instead of using the dictionary which also contains a list.

I read several tutorials but I really do not know how to do that. It was really confusing what this "self" is what __init__ and __del__ does and so on. How do I go about doing this?

dbr
  • 165,801
  • 69
  • 278
  • 343
  • [Here's an explanation of `self`](http://stackoverflow.com/questions/2709821/python-self-explained). It should help. – thegrinner Sep 11 '13 at 14:20

4 Answers4

2

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).

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • yeh ! I did it the exact same way you did ! but then I added some stuff to it and thought that it would not work :P I just did not know how to define a method to read the information from the text and save it in these variables like I did in the example with the list :/ –  Sep 11 '13 at 14:27
1

Here's 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's an example of how you might 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's an example of how you might use it:

print "The company's info is:", str(comp_info)
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • looks cool too and makes sense ^^ but it seems like I do not have some information you folks have :P because I really do not know how you guy include that I get the Info from a text file :/ –  Sep 11 '13 at 14:34
  • The scope of your class can be whatever you want it to be and it's not clear from your question that you want it to include "file parsing behavior". It's not even 100% clear that it's what you want from this comment, really. So if you want a class that deduces its identity from a file-like object, the `__init__()` method would likely be different. – Brian Cain Sep 11 '13 at 14:38
  • I am sorry that I did not write my question so clearly :/ I am from Germany and my English is not the best so I sometimes do not know exactly how I can say what I actually want to say, soz :S –  Sep 11 '13 at 14:42
  • cool that you edited it for me :D it works to a certain limit, it just shows me last company and its information - it just shows the gardener when it says print"The company's info is:", str(comp_info) how do I make it work that the program shows every company name and the user can choose a company he wants to have more information about ? –  Sep 12 '13 at 09:20
  • Again, I think that your question underscores the importance of scope. The metaphor that we're using is "`CompanyInfo`", so it should probably only know about one and only one Company. But let's also talk about `CompanyList` or `CompanySet` if you wanted to create an object that behaved like a list yet had some special behavior regarding `CompanyInfo`s. In that case you might want to inherit from `list` or `set`. – Brian Cain Sep 12 '13 at 22:46
1
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')
Dharman
  • 30,962
  • 25
  • 85
  • 135
-1
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 create an instance of the class, and use the get methods to fetch 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
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • thank you for your answer ! :) but I just do not know how you get the info from the text I wrote at the moment - I get the information from a text data, that was the reason I used the split method and so on –  Sep 11 '13 at 14:32
  • Microsoft was not funded in 1964. FYI – Ethan Sep 11 '13 at 14:33
  • 1
    I downvote this because of the useless use of getter. Simply use the attributes directly. If you would ever require a getter you could transparently swap them our with the use of the property decorator: http://docs.python.org/2/library/functions.html#property – Martin Thurau Sep 11 '13 at 14:43
  • @MartinThurau I disagree. What if at a later time he decides that all instances where the property is used, there needs to be some transformation done beforehand? – Ethan Sep 11 '13 at 16:48
  • @Ethan then he replaces the attribute with a property. Unlike some other languages, it doesn't change the code that accesses the attribute in any way. – Duncan Sep 11 '13 at 19:30
  • @MartinThurau is right. I appreciate his comment, because I hadn't heard about property before. – Roman Rdgz Sep 12 '13 at 07:00