Trying to decide between PHP and Python

G

Google Poster

About once a year, I have to learn yet another programming language.
Given all the recommendations (an outstanding accolade from Bruce
Eckel, author of "Thinking in Java") I have set my aim to Python.
Sounds kinda cool.

The indentation-as-block is unique, but that is how I always indent,
anyway.

Can any of you nice folks post a snippet of how to perform a listing
of the current directory and save it in a string?

Something like this:

$ setenv FILES = `ls`

Bonus: Let's say that I want to convert the names of the files to
lowercase? As 'tolower()'

TIA,

-Ramon
 
G

Google Poster

1)
import os
files = ' '.join(os.listdir('/home/dan'))

2)
import os
import string
files = string.lower(' '.join(os.listdir('/home/dan')))

As to choice between Python and PHP, I would say learn anything but PHP.
> Even Perl has fewer tentacles than PHP.

Not to mention that it took me 9 minutes to get a reply from you...
Quite speedy community support.

That is a very important parameter in my technology decisions these
days.

Thanks!

-Ramon
 
G

Google Poster

> This Usenet group is a truly awesome resource.

My kinda place. :)

Now, I will embark on my learning journey the proper way: reading some
Internet tutorials, instead of cheating like I just did.

-Ramon
 
B

Benjamin Kaplan

About once a year, I have to learn yet another programming language.
Given all the recommendations (an outstanding accolade from Bruce
Eckel, author of "Thinking in Java") I have set my aim to Python.
Sounds kinda cool.

The indentation-as-block is unique, but that is how I always indent,
anyway.

Can any of you nice folks post a snippet of how to perform a listing
of the current directory and save it in a string?

Something like this:

$ setenv FILES = `ls`


import os
FILES = os.listdir('.')

will give you a list of all the files in the current directory. If you
wanted that as a single string, just join all of the file names with
whatever you want as a separator.

files_str = '\n'.join(FILES)

It's a bit different than what your original command does- it doesn't
use the external "ls" command, it just gives you the list of files
already parsed.
Bonus: Let's say that I want to convert the names of the files to
lowercase? As 'tolower()'

Strings in Python have a lower() method.
files_str = files_str.lower()
 
A

Alan Meyer

I confess that I haven't used php so someone correct me if I'm wrong.

Looking at the history of the two languages, it is my impression that
php originated as a language for web/CGI development which eventually
added features enabling it to be used (sparingly) as a general purpose
language. Python on the other hand is a general purpose language that
has added features, in the form of library modules, that enable it to be
used as a web/CGI development.

Given these histories, one might ask, do you want to learn a new
language for general purposes including web development, or do you want
to learn a new language that is specifically focused on web/CGI? If the
later, php will be an excellent choice, if the former, Python.

If you only want to learn one language, or at least only want to learn
one at this time, Python seems like a more useful choice - but only if
you work (or also work) on some applications other than web/CGI.

Alan
 
C

Chris Rebert

import os
import string
files = string.lower(' '.join(os.listdir('/home/dan')))

Most of the string module has been deprecated for quite a while now;
use string methods instead:

files = ' '.join(os.listdir('/home/dan')).lower()

Cheers,
Chris
 
A

Alex Willmer

Can any of you nice folks post a snippet of how to perform a listing
of the current directory and save it in a string?

Something like this:

$ setenv FILES = `ls`

Bonus: Let's say that I want to convert the names of the files to
lowercase? As 'tolower()'

I'd just like to mention one more python nicety: list comprehension.
If you wanted the filenames as a list of strings, with each made
lowercase then the following would serve well:

import os
filenames = os.listdir('.')
filenames_lower = [fn.lower() for fn in filenames]

You could also combine this into one line:

import os
filenames_lower = [fn.lower() for fn in os.listdir('.')]

Regards, Alex
 
T

Tomasz Rola

As to choice between Python and PHP, I would say learn anything but PHP.
Even Perl has fewer tentacles than PHP.

However, the quality of code depends heavily on who writes it. My
impression is that more folks of "I did it and it works so it is good,
right?" attitude can be found among Perl/PHP crowd (compared to Python or
Ruby or...). The reason is probably the "easyness" of those languages
(mostly because of tons of readymade code on the net) which - wrongly -
suggests they are already "there", no need to learn anymore.

But I find, from time to time, nice code written in, say, PHP and from
this I know it is possible to use it without screwing up. I guess it
requires just some learning, some books (leaning towards theory rather
than teaching dummies whatever in 2 and 1/10 days) and about five years
(or maybe ten, or maybe only two) - after that, you can learn PHP and Perl
if you really want to :).

I guess stackoverflow can give some pointers about it.

Myself, I see neither of the two as promising for me, so I deflect them.

Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature. **
** As the answer, master did "rm -rif" on the programmer's home **
** directory. And then the C programmer became enlightened... **
** **
** Tomasz Rola mailto:[email protected] **
 
G

Google Poster

Can any of you nice folks post a snippet of how to perform a listing
of the current directory and save it in a string?
Something like this:
$ setenv FILES = `ls`
Bonus: Let's say that I want to convert the names of the files to
lowercase? As 'tolower()'

I'd just like to mention one more python nicety: list comprehension.
If you wanted the filenames as a list of strings, with each made
lowercase then the following would serve well:

import os
filenames = os.listdir('.')
filenames_lower = [fn.lower() for fn in filenames]

You could also combine this into one line:

import os
filenames_lower = [fn.lower() for fn in os.listdir('.')]

Regards, Alex


The syntax reminds me of Lots of Interspersed Silly Parentheses
(L.I.S.P.), but without the parentheses.

:)

-Ramon
 
S

Steven D'Aprano

You could also combine this into one line:

import os
filenames_lower = [fn.lower() for fn in os.listdir('.')]

That's two lines :)

Here are a couple of nicely obfuscated one-liners:

[name.lower() for name in __import__('os').listdir('.')]

map(str.lower, __import__('os').listdir('.'))
 
A

Alan Meyer

The syntax reminds me of Lots of Interspersed Silly Parentheses
(L.I.S.P.), but without the parentheses.

I haven't heard that version before. The one I heard was:

"Lots of Irritating Single Parentheses".

Alan
 
T

Tomasz Rola

Long Involved Stupid Parentheses.

Heh. One day, guys, when you have nothing better to do, try writing a
parser for Lisp-like language (Common Lisp, Scheme, whatever). After that,
do the same with some other language of your preference (Python, Java,
whatever). Compare time and code spent...

Regards :)
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature. **
** As the answer, master did "rm -rif" on the programmer's home **
** directory. And then the C programmer became enlightened... **
** **
** Tomasz Rola mailto:[email protected] **
 
R

Roy Smith

Tomasz Rola said:
Heh. One day, guys, when you have nothing better to do, try writing a
parser for Lisp-like language (Common Lisp, Scheme, whatever). After that,
do the same with some other language of your preference (Python, Java,
whatever). Compare time and code spent...

There is no doubt that lisp is easy to parse. Even I can increment a
counter every time I see '(' and decrement it every time I see ')'.
But, computers are there to make life easy on people, not the other way
around.

There. Now that I've tossed some gasoline on the language wars fire,
I'll duck and run in the other direction :)
 
T

Tomasz Rola

Perhaps Lisp is a simpler language to parse than Python.
Perhaps a machine with only one instruction
<URL:http://en.wikipedia.org/wiki/One_instruction_set_computer> is
simpler to implement than one with a broader instruction set.

So... nothing at all. My intention was to point out that parentheses (or
rather, simple syntax that is enabled when using them) can give a boost in
some situations. BTW, my own experience tells me, they are not really as
bad as some folk tales imply. Of course, there is also esthetic reason -
some people don't like parentheses, period. I am ok with this. Me, OTOH, I
have esthetic incompatibility with Perl and to some extent with PHP. No
problem for me :).

Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature. **
** As the answer, master did "rm -rif" on the programmer's home **
** directory. And then the C programmer became enlightened... **
** **
** Tomasz Rola mailto:[email protected] **
 
O

Octavian Rasnita

From: "Tomasz Rola said:
However, the quality of code depends heavily on who writes it. My
impression is that more folks of "I did it and it works so it is good,
right?" attitude can be found among Perl/PHP crowd (compared to Python or
Ruby or...). The reason is probably the "easyness" of those languages
(mostly because of tons of readymade code on the net) which - wrongly -
suggests they are already "there", no need to learn anymore.


Yes you are right. Perl is much flexible than all other languages and there
was written a lot of bad code in the past that can now be found on the net,
beeing very hard for a newbie to find only the good examples and tutorials.

But Perl offers many helpful modules for testing the apps so for good
programmers there is not true the idea of "it works so it's ok".

Usually we compare the languages although we always think to all aditional
modules and libraries we can use with them for creating apps.
Thinking this way, Perl is better than Python for creating web apps, because
Catalyst framework is more advanced than the frameworks for Python and it is
more flexible even than Ruby on Rails, DBIx::Class ORM is a very advanced
ORM and a very clean one and Perl web apps can use strong templating systems
and form processors, unicode is a native code for Perl for a long time and
so on.

So how good is the language depends on what you need to use it for.

(But I hope that this won't start a language war, because I have just done
the same on a Perl mailing list telling about some Perl disadvantages
towards Python :)

Octavian
 
F

flebber

Yes you are right. Perl is much flexible than all other languages and there
was written a lot of bad code in the past that can now be found on the net,
beeing very hard for a newbie to find only the good examples and tutorials.

But Perl offers many helpful modules for testing the apps so for good
programmers there is not true the idea of "it works so it's ok".

Usually we compare the languages although we always think to all aditional
modules and libraries we can use with them for creating apps.
Thinking this way, Perl is better than Python for creating web apps, because
Catalyst framework is more advanced than the frameworks for Python and it is
more flexible even than Ruby on Rails, DBIx::Class ORM is a very advanced
ORM and a very clean one and Perl web apps can use strong templating systems
and form processors, unicode is a native code for Perl for a long time and
so on.

So how good is the language depends on what you need to use it for.

(But I hope that this won't start a language war, because I have just done
the same on a Perl mailing list telling about some Perl disadvantages
towards Python :)

Octavian

My two cents, I am understanding python far better by learning scheme.
Didn't intentionally set out to achieve that as a goal just a by
product. An excelent resource http://htdp.org and using the racket
scheme ide(as much of an ide as idle), simple thorough well explained
concepts via worked examples and a very encouraging and enthusiastic
mail group, much like this list.

I would so love a book like that for python but after i complete htdp
I may not need it.

Regards

Sayth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top