PDA

View Full Version : some code.


inkedmn
03-05-2002, 09:38 PM
this... is python... :D


from socket import gethostbyname, gethostbyaddr, error
import string

#!/usr/bin/env python

print '\n'
print "Welcome to PyResolve!"
quit = 1
try:
while quit == 1:
print '\n'
print "1. Get a domain name for an IP address."
print "2. Get an IP address for a host."
print "3. Buggar off..."
x = int(raw_input())
if x == 1:
IP = raw_input("Enter IP address: ")
host = gethostbyaddr(IP)[0]
print "The domain name for %s is %s" % (IP, host)
quit = 0
elif x == 2:
addr = raw_input("Enter domain name: ")
IP = gethostbyname(addr)
print "The IP for %s is %s" % (addr, IP)
quit = 0
elif x == 3:
print "Fine, didn't want to help you anyway..."
quit = 0
else:
print "1, 2, or 3, buddy. This ain't rocket science..."
print '\n'
quit = 1
except ValueError:
print "ValueError: these aren't hard questions, really..."
except error:
print "Couldn't resolve that host"
except:
print "something pretty bad happened, but i have no idea what it is..."


this script resolves hosts to IP's and vice versa

i coded it last night when i should've been in class :)

any thoughts? who's got a snippet to share?

Psychosis
03-05-2002, 11:45 PM
Looks like Basic to me. ;)

kmj
03-06-2002, 12:21 AM
Trust me, python may look like basic at a glance, but python is not basic. VB is the only language I refuse to learn, lest someone ask me to use it. Python, on the other hand, is the language I use when I code on my own time, i.e. for the sheer enjoyment of it.

Psychosis
03-06-2002, 01:00 AM
I've never used Python, maybe I'll check it out soon.

kmj
03-06-2002, 10:19 AM
If you're already a skilled programmer, the

Python Tutorial (http://www.python.org/doc/current/tut/), written by Guido van Rossum (the language creator and "director") is the place to start. If you're learning to program, then How To Think Like A Computer Scientist (http://www.ibiblio.org/obp/thinkCSpy/) (an "open source" online book written originally with java, then converted to python) is the place to start. Also, bruce eckel is working on "thinking in python" (http://www.mindview.net/Books/TIPython), which you can download.

Don't look at me that way; we all get a little fanatical occasionally.

inkedmn
03-06-2002, 12:12 PM
i also found that O'Reilly's 'Learning Python' is pretty good for beginners.

:smoke: python :smoke:

Ludootje
03-06-2002, 01:42 PM
There wouldn't be any movies which are tutorials for python, no?
(Yes I know some describe this as lazy) ;)

Thanks for the links kmj.
inkedmn: I heard O'Reilly's books are HUGE, so that's nothing for me, since my English is far from perfect, as you LNO'ers probably already saw :o

Dru Lee Parsec
03-06-2002, 02:28 PM
Hey Python guys:

What are your impressions of Python regarding the lack of curly braces (like C or Java)? This is not a flame, I'm actually quite interested.

Also, a few jobs back one of the coders knocked out some quick and dirty test programs in JPython. Could one of you guys explain to me what the differences are between Python and JPython? And how does JPython relate to Java (I think it's somehow related but I'm not sure how).

Thanks. I'm kind of interested in finding out a bit more about this language.

Ludootje
03-06-2002, 02:33 PM
I've seen something about jpython on http://www.python.org, but don't really know something about it so I can't help you out, but I suppose you'll find all the information you need on http://www.python.org

kmj
03-07-2002, 08:17 PM
re: lack of braces

It's a godsend. :) trouble is I forget my parens and braces constantly now when I write C(++) and Java (both of which I use at work).

I guess you already looked into jython, so you probably know more about it than me.

Only thing I can say is: just like with any other language, look into it, try out a few projects in it and see how you feel. Give it a chance. Of course no language is right for everyone, you know the whole "different strokes for different folks" thing. In most "contemporary" cases, I think you'll find that python is just as viable a language as any other.

If you'll allow a bit of advocacy, here's Eric S. Raymond's accound of why he now prefers python over perl:
Why Python? (http://www.linuxjournal.com/article.php?sid=3882).

If you want more advocacy, just ask. :)

Fireman-x
03-07-2002, 09:41 PM
So how does Python perform speed wise, say as opposed to Java?

GnuVince
03-07-2002, 09:54 PM
Slower. But developpement with Python is faster according to many.

kmj
03-07-2002, 10:43 PM
One thing to note is that python generally takes much less time to "get going" (i.e. load and initialize the interpreter, VM, whatever) which I think would give it the advantage for the little scripts.
Here's the whole scoop:

http://www.twistedmatrix.com/users/glyph/rant/python-vs-java.html


and for comparisons with other languages:

http://www.python.org/doc/Comparisons.html