ruby %w equivalent

Discussion in 'Python' started by Antoine De Groote, Sep 24, 2006.

  1. 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
     
    Antoine De Groote, Sep 24, 2006
    #1
    1. Advertising

  2. Antoine De Groote

    John Machin Guest

    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
     
    John Machin, Sep 24, 2006
    #2
    1. Advertising

  3. Antoine De Groote

    Guest

    > 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, 2006
    #3
  4. Antoine De Groote

    John Machin Guest

    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".
     
    John Machin, Sep 24, 2006
    #4
  5. Antoine De Groote

    Tim Chase Guest

    >> 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
     
    Tim Chase, Sep 24, 2006
    #5
  6. Antoine De Groote

    MonkeeSage Guest

    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
     
    MonkeeSage, Sep 25, 2006
    #6
  7. Antoine De Groote

    hg Guest

    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 ?!!!!)
     
    hg, Sep 25, 2006
    #7
  8. Antoine De Groote

    MonkeeSage Guest

    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
     
    MonkeeSage, Sep 25, 2006
    #8
  9. Antoine De Groote

    hg Guest

    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
     
    hg, Sep 25, 2006
    #9
  10. Antoine De Groote

    hg Guest

    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")
     
    hg, Sep 25, 2006
    #10
  11. * 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
     
    Thorsten Kampe, Sep 25, 2006
    #11
  12. Antoine De Groote

    Georg Brandl Guest

    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
     
    Georg Brandl, Sep 25, 2006
    #12
  13. Antoine De Groote

    MonkeeSage Guest

    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
     
    MonkeeSage, Sep 25, 2006
    #13
  14. Antoine De Groote

    hg Guest

    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 !
     
    hg, Sep 25, 2006
    #14
  15. 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
     
    Wildemar Wildenburger, Sep 25, 2006
    #15
  16. 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
     
    Antoine De Groote, Sep 26, 2006
    #16
  17. 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!
     
    Antoine De Groote, Sep 26, 2006
    #17
  18. * 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
     
    Thorsten Kampe, Sep 26, 2006
    #18
  19. MonkeeSage <> wrote:
    > 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 <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Sep 27, 2006
    #19
  20. Antoine De Groote

    Duncan Booth Guest

    Nick Craig-Wood <> wrote:

    > 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__?
     
    Duncan Booth, Sep 27, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Nutbeam
    Replies:
    12
    Views:
    772
    Gary Nutbeam
    Mar 7, 2005
  2. Ray
    Replies:
    3
    Views:
    546
    Paul Boddie
    Dec 2, 2005
  3. Kenneth McDonald
    Replies:
    6
    Views:
    1,800
    Mark Thomas
    Dec 30, 2008
  4. rahul
    Replies:
    3
    Views:
    336
    John Machin
    May 12, 2009
  5. anne001
    Replies:
    1
    Views:
    550
Loading...

Share This Page