Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ruby %w equivalent

Reply
Thread Tools

ruby %w equivalent

 
 
Antoine De Groote
Guest
Posts: n/a
 
      09-24-2006
Hi everybody,

is there a python equivalent for the ruby %w operator?
%w{a b c} creates an array with strings "a", "b", and "c" in ruby...

Thanks a lot
Regards,
antoine
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      09-24-2006
Antoine De Groote wrote:

>
> is there a python equivalent for the ruby %w operator?
> %w{a b c} creates an array with strings "a", "b", and "c" in ruby...
>


| >>> "a b c".split()
| ['a', 'b', 'c']

.... appears to match your single example.

HTH,
John

 
Reply With Quote
 
 
 
 
p.lavarre@ieee.org
Guest
Posts: n/a
 
      09-24-2006
> is there a python equivalent for the ruby %w operator?
> %w{a b c} creates an array with strings "a", "b", and "c" in ruby...


The expression 'a b c'.split() creates the ['a', 'b', 'c'] list of str,
if that helps.

Also dir('a b c') briefly lists much of what
http://docs.python.org/lib/string-methods.html explains.

Also Google was curiously resistant to telling me where Ruby's %w is
documented.

Pat LaVarre

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      09-24-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Also Google was curiously resistant to telling me where Ruby's %w is
> documented.
>


You would need to dig into your Google toolbar config and un-tick
"YAGNI filter".

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-24-2006
>> is there a python equivalent for the ruby %w operator?
>> %w{a b c} creates an array with strings "a", "b", and "c" in ruby...
>>

>
> | >>> "a b c".split()
> | ['a', 'b', 'c']
>
> ... appears to match your single example.


bah, far to easy to understand...add a little line-noise, man,
and it will be closer to ruby/perl.

Maybe something like

>>> from string import split as _
>>> _("a b c")

['a', 'b', 'c']

or perhaps

>>> "a b c".split
>>> _()

['a', 'b', 'c']

to give it that perl/ruby-ish feel of terseness and obscurity.

And people wonder why I like python...

-tkc




 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      09-25-2006
Tim Chase wrote:
> to give it that perl/ruby-ish feel of terseness and obscurity.


Don't feel bad, you always have things like r'%s\%s' % (u'blah',
u'blah') and so on. But of course, it's only the other guys who are
evil / ugly / stupid. As the human torch says, "Flame On".

[Full disclosure: I like ruby _and_ I like python (gasp!), and see no
need to artificially criticize one or the other; I try rather to
utilize the strengths of both.]

Regards,
Jordan

 
Reply With Quote
 
hg
Guest
Posts: n/a
 
      09-25-2006
Antoine De Groote wrote:
> Hi everybody,
>
> is there a python equivalent for the ruby %w operator?
> %w{a b c} creates an array with strings "a", "b", and "c" in ruby...
>
> Thanks a lot
> Regards,
> antoine


Why would they want to make such an obscure API ? ... didn't they have
Python to learn from (I am truly amazed - nothing cynical ...just ...
why ?!!!!)
 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      09-25-2006
hg wrote:
> Why would they want to make such an obscure API ? ... didn't they have
> Python to learn from (I am truly amazed - nothing cynical ...just ...
> why ?!!!!)


In ruby there are several special literal notations, just like python.
In ruby it goes like this:

%{blah} / %Q{blah} # same as "blah" but igornes " and '
%q{blah} # same as 'blah' but no interpolation
%w{blah blah} # same as "blah blah".split
%r{blah} # same as /blah/
%x{ls} # same as `ls`

Sometimes they are very useful, and sometimes they are cumbersome. It's
up to the programmer to implement them effectively.

Regards,
Jordan

 
Reply With Quote
 
hg
Guest
Posts: n/a
 
      09-25-2006
MonkeeSage wrote:
> hg wrote:
>> Why would they want to make such an obscure API ? ... didn't they have
>> Python to learn from (I am truly amazed - nothing cynical ...just ...
>> why ?!!!!)

>
> In ruby there are several special literal notations, just like python.
> In ruby it goes like this:
>
> %{blah} / %Q{blah} # same as "blah" but igornes " and '
> %q{blah} # same as 'blah' but no interpolation
> %w{blah blah} # same as "blah blah".split
> %r{blah} # same as /blah/
> %x{ls} # same as `ls`
>
> Sometimes they are very useful, and sometimes they are cumbersome. It's
> up to the programmer to implement them effectively.
>
> Regards,
> Jordan
>

I am certain Ruby is a very effective language (I read much good stuff
about it) ... it's just that I cannot comprehend why a "new" language
would attempt so hard to look like assembly.


Regards,

hg

 
Reply With Quote
 
hg
Guest
Posts: n/a
 
      09-25-2006
hg wrote:
> MonkeeSage wrote:
>> hg wrote:
>>> Why would they want to make such an obscure API ? ... didn't they have
>>> Python to learn from (I am truly amazed - nothing cynical ...just ...
>>> why ?!!!!)

>> In ruby there are several special literal notations, just like python.
>> In ruby it goes like this:
>>
>> %{blah} / %Q{blah} # same as "blah" but igornes " and '
>> %q{blah} # same as 'blah' but no interpolation
>> %w{blah blah} # same as "blah blah".split
>> %r{blah} # same as /blah/
>> %x{ls} # same as `ls`
>>
>> Sometimes they are very useful, and sometimes they are cumbersome. It's
>> up to the programmer to implement them effectively.
>>
>> Regards,
>> Jordan
>>

> I am certain Ruby is a very effective language (I read much good stuff
> about it) ... it's just that I cannot comprehend why a "new" language
> would attempt so hard to look like assembly.
>
>
> Regards,
>
> hg
>

To further comment: back to the PDP11 and such guys, there was a true
need to "terse" the language and give the computer a break ... "what
I've already calculated, the computer needs not to calculate ... plus
I'm avoiding potential software(assembler/compiler) bugs"

But today ? what is the cost of replacing %w("blah blah") by
Hi_I_Want_To_Split_The_String_That_Follows( "blah blah")
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python API Functions equivalent to ruby's rb_big2str() andrb_str2cstr() rahul Python 3 05-12-2009 04:03 PM
Any equivalent to Ruby's 'hpricot' html/xpath/css selector package? Kenneth McDonald Python 6 12-30-2008 01:38 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Ruby on Rails Job Site -- Is there a Python equivalent? Ray Python 3 12-02-2005 03:13 PM
Ruby on Rails or Perl's Maypole..is there a Python equivalent Gary Nutbeam Python 12 03-07-2005 04:40 PM



Advertisments