[Tutor] Help me Be Pythonic
Andrei Kulakov
ak@silmarill.org
Tue, 28 May 2002 21:54:02 -0400
On Tue, May 28, 2002 at 04:38:00PM -0800, Chris Lott wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I have started to learn Python and written my first little app
> that takes a file of names (or a list on the command line) and
> checks to see which of the .com/.org/.net/.info domains are
> available using urllib to grab data from web forms.
>
> However, my code is very procedural. No functions and no objects
> of my own.
>
> In pseudo code, how can I think about this task like a python
> programmer. Would I have a urlcheck object that was passed the
> data and checked for different types? What's the pythonic way
> to:
>
> open file or command line arguments
> check url for .com
> if text found print "succes"
> else print "unavailable"
> check url for .org
> if text found print "succes"
> else print "unavailable"
>
You could do:
for top_lev_domain in (".com", ".org" ...):
print top_lev_domain
if text found print "success"
else print "unavailable"
Furthermore, you could wrap some code in a function. For
instance:
def is_available(domain):
[check if domain is avaiable]
if yes: return 1
else: return 0
Once you do that, you may use this function from a different
program simply by doing: import module;
module.is_available(domain)
- Andrei
> etc?
>
> c
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.7 (MingW32) - GPGshell v2.30
> Comment: Key ID: 0x51046CFD
>
> iD8DBQE89CLk1oth6FEEbP0RAmMAAJ9n6hFJsBIo7K12ct5mwHAG8tlpMACff5BI
> rZr6OR2br+PeSAayUtYucL0=
> =0Fyc
> -----END PGP SIGNATURE-----
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org