473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need help with OOP

Object oriented programming is not a new term to me, but when I tried
to do some of it... uhh... it sure looked (and felt) like Hell :) I
won't bother about things like "help me learn it" (but wouldn't mind
if someone recommends a good fresh-start tutorial), instead I'll ask
for help...

I did my share of programming several years ago... 6 or 7 to be more
specific, and as I'm pretty proud of my achievements ;)
I continued on the same path. Several weeks ago, Python showed up.
I got the 'assignment' to start learning it... almost professionally :)

OK, I did my part, but now the tests are becoming more than fair :(

Next part (even though I said I know *nothing* about OOP) is:

"Make class for the IP address. It should be initialized from the
string. Later on, we can add functions to it."

[1] What in the world I have to do here?
[1a] How to make class for the IP address? What does it mean?
[2] "Initialization from the string" means something like passing
arguments from the command line...?

Of course, I don't expect you to do the job for me, but I would sure
appreciate help. I know this is easy, but as stated before, I don't
know how to handle objects... yet :)

I informed my 'mentor' that the training on OOP subject has begun and
I started looking for good literature.

In the mean time, I'm trying to buy some time, and I was hoping that
your suggestions could help me.

Thanks for the help, and if I was unclear at some point, just shoot :)

Ciao,
Sinisa
P.S. Can somebody please explain how can I.... how to avoid all these
"I"'s from being used as often as I'm doing it? :( It really goes on
my nerves as I feel like illiterate idiot :(

P.P.S. Speaking of being illiterate, no, English is not my native
language. Just in case someone wanders... :o)
Jul 18 '05 #1
5 1666
On Fri, 7 Nov 2003 05:36:57 +0100, sinisam <X.***************@mail.ru>
wrote:
Next part (even though I said I know *nothing* about OOP) is:

"Make class for the IP address. It should be initialized from the
string. Later on, we can add functions to it."

[1] What in the world I have to do here?
[1a] How to make class for the IP address? What does it mean?
One feature you will often see in OOP is the idea of 'wrapping' more
primitive concepts into objects and then giving those objects more
functionality as a means of convenience. For example, while it is
possible to trate a phone number as a simple string such as

phoneNumber = '102.555.1234'

You can also wrapper the simple string in a class and then give it
extra functionality

phoneNumber = PhoneNumber()
phoneNumber._number = "102.555.1234"
pre = phoneNumber.prefix()

the function "prefix()" is defined by the PhoneNumber class as an
easier way of dealing with the phone number. To create this wrapper
class in Python is easy

class PhoneNumber:
def __init__(self):
self._number = ''

def prefix (self):
return self._number[4:7]

So the question being asked is to write a similar wrapper class that
will do the same for IP addresses

[2] "Initialization from the string" means something like passing
arguments from the command line...?

No, it means that when you create the object, you will provide some
information that will set the default state of the new object

person = Person ("O'Connor", "James")

this is done by providing paramaters for your initialization function
for the class

class Person:

def __init__(self, lastName, firstName):
self._lastName = lastName
self._firstName = firstName

This sets the lastName and firstName of the person to whatever you
tell it to be when you create the object
Of course, I don't expect you to do the job for me, but I would sure
appreciate help. I know this is easy, but as stated before, I don't
know how to handle objects... yet :)


The above explanation and examples are sparse, but should be
sufficient to move you in the right direction to solve your problem

Take care,
Jay
Jul 18 '05 #2
sinisam wrote:
Object oriented programming is not a new term to me, but when I tried
to do some of it... uhh... it sure looked (and felt) like Hell :) I
won't bother about things like "help me learn it" (but wouldn't mind
if someone recommends a good fresh-start tutorial), instead I'll ask
for help... I did my share of programming several years ago... 6 or 7 to be more
specific, and as I'm pretty proud of my achievements ;)
I continued on the same path. Several weeks ago, Python showed up.
I got the 'assignment' to start learning it... almost professionally :)

OK, I did my part, but now the tests are becoming more than fair :(

Next part (even though I said I know *nothing* about OOP) is:

"Make class for the IP address. It should be initialized from the
string. Later on, we can add functions to it."

[1] What in the world I have to do here?
[1a] How to make class for the IP address? What does it mean?
[2] "Initialization from the string" means something like passing
arguments from the command line...?


Warning - oversimplification ahead!

With OOP, the idea is to make "Objects". In this case you want to make an
object which represents an IP address. You will want (eventually) to have a
list of things that you want to do to an IP address object, like create
one, convert one to a name via the DNS, destroy one, pretend that CIDR
doesn't exist and get the netmask for one and so on. In order to do these
things you will need to have some data associated with the object. So all
we need to do is package these things together (the functions you want to
use with the object and the persistant data you need). This packaged
"thing" is called a "class".

In python, you define a function called __init__ which makes the objects,
this is the initialization.

So you fire up a copy of python. and get a prompt. You then type

class ipaddress:
def __init__(self,str):
self.string_rep=str

and press return some more times until you get the >>>> prompt back.
This has defined a class called 'ipaddress', and made an initialization
routine for it. This routine takes 2 parameters, the first is traditionally
called 'self' and refers to the object itself. The second, named 'str',
represents the string parameter. The action of the initialization routine
is to store this string into an internal variable (called string_rep).

You can now create an ipaddress object by typeing

an_ip=ipaddress("192.168.1.1")

and you can do things like

print an_ip.string_rep

Later on you will want to define other functions, and maybe add some more
data. For instance you may want to define __str__(self) so you can just
print out an object.

It is worth working through the python tutorial if you have not already done
so.

To make life more interesting, OOP uses different words, like "method" to
refer to one of the functions defined in the class. Above we created a
variable called 'an_ip' which is an "instance" of the ipaddress class, we
could make a second instance

another_ip=ipaddress("192.168.1.2")

Normally the __init__ method would do rather better error checking, so it
made sure that it was being given a sensible string value,

When you have more methods defined, you can use tham as

an_ip.netmask()

which would be written as
netmask(an_ip)
in most languages which are not OOP languages. Hope this very brief and
superficial introduction helps.
Jul 18 '05 #3
On Fri, 7 Nov 2003 05:36:57 +0100, sinisam
<X.***************@mail.ru> wrote:
Object oriented programming is not a new term to me, but ...
I did my share of programming several years ago...
Don't worry if OOP takes aq while to sink in, thats pretty
common, especially if you've been trained in procedural styule
programming - you have to learn to change your approach to
problem solving.

It might be worth going through some of the beginners
tutors on the Python web site, most of which - including
mine! ;-) - have a section on OOP.
"Make class for the IP address. It should be initialized from the
string. Later on, we can add functions to it."

[1] What in the world I have to do here?
Turn an IP address into an OOP object.
[1a] How to make class for the IP address? What does it mean?
Read the tutorials, they will take you step by step through the
comcepts.
[2] "Initialization from the string" means something like passing
arguments from the command line...?
A little bit, but as part of the initialisation call of your
object. Python contains lots of objects. For example files are
objects.

You create a file object by passing the filename as a parameter:

f = file("foo.txt")

f is now a file object that you can maniplulate. You are being
asked to provide a similar facility for IP addresses:

ip = IPaddress("123.234,321.34")
I started looking for good literature.
Try the beginners tutors and also for the theoretical
understanding visit www.cetus-links.org which is a huge
OOP portal site.
P.P.S. Speaking of being illiterate, no, English is not my native
language. Just in case someone wanders... :o)


You're doing just fine on that front. Better than some who do ave
English as their native tongue! :-)

Alan G.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
Jul 18 '05 #4
On Fri, 07 Nov 2003 06:47:20 GMT, Icarus Sparry
<us****@icarus.freeuk.com> wrote:

class ipaddress:
def __init__(self,str):
self.string_rep=str


However, using str as (even a local) variable name is not that good an
idea, since it's the name of the built-in type representing strings,
which could not be used in this __init__ method because of that. An
example which does just that could be:

class ipaddress:
def __init__(self, address):
self.adress_string = str(address)

to convert whatever parameter gets passed to a string. Using built-in
names for variables is a habit that shouldn't be started ;-)

--
Christopher
Jul 18 '05 #5
On Fri, 7 Nov 2003 05:36:57 +0100, sinisam <X.***************@mail.ru>
wrote:

....
"Make class for the IP address. It should be initialized from the
string. Later on, we can add functions to it."

[1] What in the world I have to do here?
[1a] How to make class for the IP address? What does it mean?
[2] "Initialization from the string" means something like passing
arguments from the command line...?

Of course, I don't expect you to do the job for me, but I would sure

....
This won't directly help with learning OOP, but be sure to read the
Python module documentation for the socket module, and the
higher-level protocol support (e.g., httplib, ftplib, smtplib, etc).
I'm no socket expert, and didn't see anything in it that was an
actual "ip class", but there are a lot of methods for manipulating IP
addresses in string and binary form.
--dang
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.