473,224 Members | 1,401 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,224 software developers and data experts.

ruby %w equivalent

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
Sep 24 '06 #1
24 7511
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

Sep 24 '06 #2
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

Sep 24 '06 #3

p.*******@ieee.org 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".

Sep 24 '06 #4
>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


Sep 24 '06 #5
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

Sep 25 '06 #6
hg
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 ?!!!!)
Sep 25 '06 #7
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

Sep 25 '06 #8
hg
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

Sep 25 '06 #9
* John Machin (24 Sep 2006 15:32:20 -0700)
>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.
Something wrong with "list('abc')"? Or is it too simple?!

Thorsten
Sep 25 '06 #10
hg
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")
Sep 25 '06 #11
Thorsten Kampe wrote:
* John Machin (24 Sep 2006 15:32:20 -0700)
>>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.

Something wrong with "list('abc')"? Or is it too simple?!
It is quite unreliable for strings consisting of more than one char... ;)

Georg
Sep 25 '06 #12
hg wrote:
But today ? what is the cost of replacing %w("blah blah") by
Hi_I_Want_To_Split_The_String_That_Follows( "blah blah")
How about r'blah', u'blah', """blah""", and '''blah'''. :)

Regards,
Jordan

Sep 25 '06 #13
MonkeeSage wrote:
hg wrote:
>But today ? what is the cost of replacing %w("blah blah") by
Hi_I_Want_To_Split_The_String_That_Follows( "blah blah")

How about r'blah', u'blah', """blah""", and '''blah'''. :)
C'mon, the last two really don't count.

wildemar
Sep 25 '06 #14
hg
MonkeeSage wrote:
hg wrote:
>But today ? what is the cost of replacing %w("blah blah") by
Hi_I_Want_To_Split_The_String_That_Follows( "blah blah")

How about r'blah', u'blah', """blah""", and '''blah'''. :)

Regards,
Jordan

Some truth to that !
Sep 25 '06 #15
Thorsten Kampe wrote:
* John Machin (24 Sep 2006 15:32:20 -0700)
>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.

Something wrong with "list('abc')"? Or is it too simple?!

Thorsten
As far as I can tell this works for single characters only. You're not
able to split words, as in "one two three".split().

Regards,
antoine
Sep 26 '06 #16
Antoine De Groote wrote:
Thorsten Kampe wrote:
>* John Machin (24 Sep 2006 15:32:20 -0700)
>>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.

Something wrong with "list('abc')"? Or is it too simple?!

Thorsten

As far as I can tell this works for single characters only. You're not
able to split words, as in "one two three".split().

Regards,
antoine
And this is what Georg Brandl already posted. Sorry!
Sep 26 '06 #17
* Antoine De Groote (Tue, 26 Sep 2006 12:06:38 +0200)
>Thorsten Kampe wrote:
>* John Machin (24 Sep 2006 15:32:20 -0700)
>>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.

Something wrong with "list('abc')"? Or is it too simple?!

Thorsten

As far as I can tell this works for single characters only. You're not
able to split words, as in "one two three".split().
It does satisfy your example in your first posting nevertheless.

Thorsten
Sep 26 '06 #18
MonkeeSage <Mo********@gmail.comwrote:
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`
These are snatched straight from perl. In perl they are spelt
slightly differently

q{blah} r"""blah""" # not identical but similar
qq{blah} """blah""" # no interpolation in python so no direct concept
qw{blah blah} "blah blah".split()
qr{blah} re.compile(r"blah")
qx{ls} commands.getoutput("ls")

In perl (and maybe in ruby I don't know) the { } can be replaced with
any two identical chars, or the matching pair if bracketty, so q/blah/
or q(blah).

As a perl refugee, the only one I miss at all is qw{}, ie %w{} in ruby
the subject of this post.

In python when making __slots__ or module.__all__ you end up typing
lists of objects or methods and they turn out like this which is quite
a lot of extra typing

__slots__ = ["method1", "method2", "method3", "method4", "method5"]

You can of course write it like this

__slots__ = "method1 method2 method3 method4 method5".split()

which is nearly as neat as qw//, but not quite since the split() bit
comes at the end so it doesn't notify you that you have an array of
strings rather than a string.

I don't expect a replacement for %w{}, qw// to ever be added to
python, it is not the python way. And the python way is why I am now
a python programmer not a perl programmer!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Sep 27 '06 #19
Nick Craig-Wood <ni**@craig-wood.comwrote:
In python when making __slots__ or module.__all__ you end up typing
lists of objects or methods and they turn out like this which is quite
a lot of extra typing

__slots__ = ["method1", "method2", "method3", "method4", "method5"]

For __all__ you can use a decorator to avoid retyping the function name at
all. e.g.

def public(f):
all = f.func_globals.setdefault('__all__', [])
all.append(f.__name__)
return f

@public
def foo(): pass

I don't use __slots__ much at all, and if you'd said "attribute1" etc. I'd
have understood, but I'm really curious why would you be listing any
methods in __slots__?
Sep 27 '06 #20
>>>>hg <hg@nospam.com(A) wrote:
>AAntoine 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
>AWhy would they want to make such an obscure API ? ... didn't they have
APython to learn from (I am truly amazed - nothing cynical ...just ...
Awhy ?!!!!)
I think it is modeled after Perl.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Sep 27 '06 #21
Thorsten Kampe wrote:
* Antoine De Groote (Tue, 26 Sep 2006 12:06:38 +0200)
>Thorsten Kampe wrote:
>>* John Machin (24 Sep 2006 15:32:20 -0700)
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.
Something wrong with "list('abc')"? Or is it too simple?!

Thorsten
As far as I can tell this works for single characters only. You're not
able to split words, as in "one two three".split().

It does satisfy your example in your first posting nevertheless.

Thorsten
Absolutely :-)
Sep 27 '06 #22
Duncan Booth <du**********@invalid.invalidwrote:
Nick Craig-Wood <ni**@craig-wood.comwrote:
In python when making __slots__ or module.__all__ you end up typing
lists of objects or methods and they turn out like this which is quite
a lot of extra typing

__slots__ = ["method1", "method2", "method3", "method4", "method5"]

For __all__ you can use a decorator to avoid retyping the function name at
all. e.g.

def public(f):
all = f.func_globals.setdefault('__all__', [])
all.append(f.__name__)
return f

@public
def foo(): pass
Nice one!
I don't use __slots__ much at all, and if you'd said "attribute1" etc. I'd
have understood, but I'm really curious why would you be listing any
methods in __slots__?
Those should of course have been attributes - I noticed immediately
after posting ;-)

Aside: __slots__ is only really useful when you've created so many
objects you are running out of memory and you need to optimise memory
usage a bit. We got our app down to 1/3 of the memory usage by
putting in three __slots__

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Sep 27 '06 #23
Nick Craig-Wood wrote:
These are snatched straight from perl. In perl they are spelt
slightly differently
Yup, they are; perl had _some_ good ideas; no doubt. ;)
In perl (and maybe in ruby I don't know) the { } can be replaced with
any two identical chars, or the matching pair if bracketty, so q/blah/
or q(blah).
Yes; same thing in ruby.

Regards,
Jordan

Sep 30 '06 #24
hg wrote:
But today ? what is the cost of replacing %w("blah blah") by
Hi_I_Want_To_Split_The_String_That_Follows( "blah blah")
The latter is beginning to look like the Cocoa/NextStep framework.
Perhaps we should give up scripting languages for ObjC?

James
Sep 30 '06 #25

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
5
by: F Jamitzky | last post by:
It is rather easy to define functions in python that mimic the special ruby syntactic sugar like: 5.times { print "Hello World!" } or .each { |food| eat food } In python these fragments...
12
by: Gary Nutbeam | last post by:
I've noticed that the Perl camp has a very nice web/database environment called Maypole. Ruby has the Rails environment which on the surface seems similar to Maypole. I can't find anything in...
65
by: Amol Vaidya | last post by:
Hi. I am interested in learning a new programming language, and have been debating whether to learn Ruby or Python. How do these compare and contrast with one another, and what advantages does one...
22
by: Francois | last post by:
I discovered Python a few months ago and soon decided to invest time in learning it well. While surfing the net for Python, I also saw the hype over Ruby and tried to find out more about it, before...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.