Hi to everyone. I am new here and i try to learn python3. For this reason i try to implement a problem with the following characteristics: A class Room with atrributes(building,floor,number) and a Class occupant with attributes(familyname,givenname,room). Next the program will read a text file line by line. each line has 1 or 2 first,given names and a room like "A.15.10". So i complete line will be "familyname, givenname room". Between family and given name exist a ',' and after givenname a 'space'. I implement this in perl and if anyone know something how to do something similar in python i ll appriciate it. My perl imlementetion is

   package Room;

  use overload
  '""' => sub { $_[0]->str(); }
 ;

 sub new
 {
my ($class, $b, $f, $n) = @_;
my $self = {};    # empty hash
$self->{B} = defined($b) ? $b : "A";
$self->{F} = defined($f) ? $f : "01";
$self->{N} = defined($n) ? $n : "01";
bless $self, $class;
return $self;
}

sub str
{
my $self = $_[0];
return $self->{B} . "." . $self->{F} . "." . $self->{N};
}

1;

the class Person or Occupant

 package Occupant;

 use Room;
use overload
'""' => sub { $_[0]->str(); }
;

sub new
{
my ($class, $f, $g, $r) = @_;
my $self = {};    # empty hash
$self->{F} = defined($f) ? $f : " " ;
$self->{G} = defined($g) ? $g : " " ;
$self->{R} = defined($r) ? $r : Room->new() ;
bless $self, $class;
return $self;
}

sub str
{
my $self = $_[0];
return $self->{F} . ", " . $self->{G} . " " . $self->{R};
}

1;

the main program

use Room; 
use Occupant;

open(INFILE, '<file.txt') || die "Could not open fbc_math.txt \n";

$i = 0;
while ($str = <INFILE>)
{
@word = split(/\s|\,\s/, $str);
$rword = \@word;
if ($#$rword == 2) #This person has one given name.
{ 
  @room = split(/\./, $word[2]);
  $Occupants[$i] = Occupant->new($word[0],$word[1],Room->new($room[0],$room[1],$room[2]));
}
if ($#$rword == 3) #This person has two given names.
{ 
  @room = split(/\./, $word[3]);
  $Occupants[$i] = Occupant->new($word[0],$word[1] . " " . $word[2],Room->new($room[0],$room[1],$room[2]));
}
$i = $i + 1;
}

seek(INFILE, 0, 0);

foreach $temp (@Occupants) {
print "$temp\n";
}

sub seniority
{     
$a->{R}->{B} cmp $b->{R}->{B}
or $a->{R}->{F} cmp $b->{R}->{F}
or $a->{R}->{N} cmp $b->{R}->{N}
}

@Sorted = sort seniority @Occupants;

foreach $temp (@Sorted) {
 print "$temp\n";
}

I have started until know with the implementation of the first class Room

 Class Room:
      def __init__(self.building,floor,number):
         self.building=building
         self.floor=floor
         self.number=number

      def storeall(self):
          return self.building,'.',self.floor,'.',self.number

Recommended Answers

All 2 Replies

I cannot understand how the inheritance work in pyhton. Is it correct something like the following

  class Occupant(Room):
       def __init__(self,familyname,givenname,room):
           self.familyname=familyname
           self.givenname=givenname
           self.room=Room.__init__(self,building,floor,number)

How is Occupant similar to Room, I do not understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.