showing all elements on Array/list/ without a loop

This is the place for queries that don't fit in any of the other categories.

showing all elements on Array/list/ without a loop

Postby fosiul » Tue Apr 22, 2014 7:33 pm

Hi
I am quite new to python, i just need a help to fix a script ( i have done work on ruby)
Basically,
in ruby I can show full element of Array by putting like this,

Code: Select all
myarray=Array.new
myarray="test1"
myarray="test2"

print #{myarray}


I just wanted to do same concept by python, but its only show me last elements,

Code: Select all
_msg_line=[]
for key in DATA.keys():

        _msg_line="%s %s %s - %s\n\n\n%s\n"  \
                 %( color[key], hobbittime, status[key], status_info[key], DATA[key])

print _msg_line


but this only shows the last elements.

how can i show the full element of Array or list or .. without any loops ??

reason, I need to pass the content of that _msg_line to monitoring system like this..

Code: Select all
 command = "%s %s 'status %s.%s  %s'" \
        %( os.environ['BB'], os.environ['BBDISP'],os.environ['MACHINE'],_test,_msg_line)

         os.system(command)


Thanks for your kind help.
Last edited by micseydel on Tue Apr 22, 2014 8:00 pm, edited 1 time in total.
Reason: Code tags, lock.
fosiul
 
Posts: 6
Joined: Tue Apr 22, 2014 7:23 pm

Re: showing all elements on Array/list/ without a loop

Postby stranac » Tue Apr 22, 2014 8:18 pm

fosiul wrote:I just wanted to do same concept by python, but its only show me last elements,

Code: Select all
_msg_line=[]
for key in DATA.keys():

        _msg_line="%s %s %s - %s\n\n\n%s\n"  \
                 %( color[key], hobbittime, status[key], status_info[key], DATA[key])

print _msg_line


_msg_line is a string when you're printing it, not a list.
I'm not exactly sure what result you're looking for, but maybe you wanted _msg_line.append(...)
fosiul wrote:reason, I need to pass the content of that _msg_line to monitoring system like this..

Code: Select all
 command = "%s %s 'status %s.%s  %s'" \
        %( os.environ['BB'], os.environ['BBDISP'],os.environ['MACHINE'],_test,_msg_line)

         os.system(command)

You don't want to do that...
Take a look at subprocess.
Last edited by micseydel on Tue Apr 22, 2014 9:29 pm, edited 1 time in total.
Reason: Fixed the quote formatting.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: showing all elements on Array/list/ without a loop

Postby micseydel » Tue Apr 22, 2014 9:30 pm

On top of stranac's comments, I'm not sure what you mean about Ruby
Code: Select all
$ irb
irb(main):001:0> myarray=Array.new
=> []
irb(main):002:0> myarray="test1"
=> "test1"
irb(main):003:0> myarray="test2"
=> "test2"
irb(main):004:0>
irb(main):005:0* print #{myarray}
nil=> nil
irb(main):006:0>
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1368
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: showing all elements on Array/list/ without a loop

Postby fosiul » Tue Apr 22, 2014 9:45 pm

Hi, its like this

micseydel wrote:On top of stranac's comments, I'm not sure what you mean about Ruby



Code: Select all
[root@171-DP-A ~]# irb
irb(main):001:0> myarray=Array.new
=> []
irb(main):002:0> myarray<<"test1"
=> ["test1"]
irb(main):003:0> myarray<<"test2"
=> ["test1", "test2"]
irb(main):004:0>


So in scirpt if i put like this, it will give all values .
puts #{myarray}
Last edited by micseydel on Tue Apr 22, 2014 10:31 pm, edited 1 time in total.
Reason: Fixed formatting.
fosiul
 
Posts: 6
Joined: Tue Apr 22, 2014 7:23 pm

Re: showing all elements on Array/list/ without a loop

Postby fosiul » Tue Apr 22, 2014 9:51 pm

_msg_line is a string when you're printing it, not a list.
I'm not exactly sure what result you're looking for, but maybe you wanted _msg_line.append(...)
[/quote]

Hi ,

i have tryed that
but its gives me syntax error

Code: Select all
_msg_line=[]
for key in DATA.keys():

        _msg_line.append("%s %s %s - %s\n\n\n%s\n"  \
                 %( color[key], hobbittime, status[key], status_info[key], DATA[key]))

print _msg_line


Basically the output should be like this

Name: vm1
State: Running
HostServerMemUsed: 0.71%
MaxMemSet: 512 MB
CurMemSet: 512 MB
VCPUs: 1

Name: vm2
State: Running
HostServerMemUsed: 0.71%
MaxMemSet: 512 MB
CurMemSet: 512 MB
VCPUs: 1


but , i only see the lat output .. i guess its gets over written
fosiul
 
Posts: 6
Joined: Tue Apr 22, 2014 7:23 pm

Re: showing all elements on Array/list/ without a loop

Postby fosiul » Tue Apr 22, 2014 10:23 pm

Hi
Bellow code works
msg_line=[]
for key in DATA.keys():

_msg_line.append("%s %s %s - %s\n\n\n%s\n" \
%( color[key], hobbittime, status[key], status_info[key], DATA[key]))

print _msg_line


only problem is :
its not putting Line break ...!!
So i get output like this

['green Tue Apr 22 23:16:53 BST 2014 kvm_Status OK - \n\n\nName: vm1\nState: Running\nHostServerMemUsed: 1.41%\nMaxMemSet: 1024 MB\nCurMemSet : 1024 MB\nVCPUs: 2\n\n\n', 'green Tue Apr 22 23:16:53 BST 2014 kvm_Status OK - \n\n\nName: vm2\nState: Running\nHostServerMemUsed: 1.41%\nMaxMemSet: 24 MB\nCurMemSet: 1024 MB\nVCPUs: 1\n\n\n']

What shall i do to get line break ??

Thanks
fosiul
 
Posts: 6
Joined: Tue Apr 22, 2014 7:23 pm

Re: showing all elements on Array/list/ without a loop

Postby micseydel » Tue Apr 22, 2014 10:35 pm

Code: Select all
for thing in _msg_line:
    print thing

or
Code: Select all
print '\n'.join(_msg_line)


Tips for the future:
* if you're going to make three posts in the same thread in a row... don't. Just do the one. We'll totally see it.
* post the right code, in code tags (not quote tags). When I say "right code" I mean that if you want your Ruby to have << then don't put = there instead. Seriously. Also, don't post Python code with undefined variables like that. Post runnable code. If the variable are important, include them. If they're not (and they probably aren't) then use a simpler example. After simplifying things, double check the code still runs.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1368
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: showing all elements on Array/list/ without a loop

Postby fosiul » Tue Apr 22, 2014 10:53 pm

micseydel wrote:
Code: Select all
for thing in _msg_line:
    print thing

or
Code: Select all
print '\n'.join(_msg_line)




Hi
Thanks,

I just need a last and small fix., ( i am really sorry for asking too much as I am not a python guys.and i need this fix like a blind , hope you will understand)

Code: Select all
_msg_line=[]
for key in DATA.keys():

        _msg_line.append("%s %s %s - %s\n\n\n%s\n"  \
                 %( color[key], hobbittime, status[key], status_info[key], DATA[key]))

print '\n'.join(_msg_line)

#Launching xymon client
_cmd_line="%s %s \"status %s.%s %s %s\n\n%s\"" %(os.environ['BB'], os.environ['BBDISP'], os.environ['MACHINE'], _test, _status, _date, _msg_line)
os.system(_cmd_line)



Print is fine, But only issue is, when i am sending this to monitoring system via _cmd_line.
its sending same way , with \n\n\

Can i do some thing like bellow ?? obviously bellow code fails.
Code: Select all
_cmd_line="%s %s \"status %s.%s %s %s\n\n%s\"" %(os.environ['BB'], os.environ['BBDISP'], os.environ['MACHINE'], _test, _status, _date, '\n'.join._msg_line)
fosiul
 
Posts: 6
Joined: Tue Apr 22, 2014 7:23 pm

Re: showing all elements on Array/list/ without a loop

Postby fosiul » Tue Apr 22, 2014 11:00 pm

Hi
not sure if this is the right way
but bellow works

Code: Select all
_msg_line=[]
for key in DATA.keys():

        _msg_line.append("%s %s %s - %s\n\n\n%s\n"  \
                 %( color[key], hobbittime, status[key], status_info[key], DATA[key]))

_msg_line1= '\n'.join(_msg_line)

#Launching xymon client
_cmd_line="%s %s \"status %s.%s %s %s\n\n%s\"" %(os.environ['BB'], os.environ['BBDISP'], os.environ['MACHINE'], _test, _status, _date, _msg_line1)
os.system(_cmd_line)


sys.exit(0)

fosiul
 
Posts: 6
Joined: Tue Apr 22, 2014 7:23 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests