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

Need better way to unpack a list

Nx
Is there a better solution to following problem :

I want to unpack a list into variables
the list is created at runtime and I do not know
how many items there will be , but I know not more than 25.
The list unpacks into variables which are prev. defined as
line1,line2....line25 which are actually the names of lineedit fields in
a Qt gui application and which need to be populated.
I currently use following to achieve that :

c=0
while c < len(mylinelist):
c = c + 1
if c==1:
self.line1.setText(mylinelist[c])
if c==2 :
self.line2.setText(mylinelist[c])
Aug 17 '05 #1
3 1786
Nx wrote:
Is there a better solution to following problem :

I want to unpack a list into variables
the list is created at runtime and I do not know
how many items there will be , but I know not more than 25.
The list unpacks into variables which are prev. defined as
line1,line2....line25 which are actually the names of lineedit fields in
a Qt gui application and which need to be populated.
I currently use following to achieve that :

c=0
while c < len(mylinelist):
c = c + 1
if c==1:
self.line1.setText(mylinelist[c])
if c==2 :
self.line2.setText(mylinelist[c])
.
.
.
if c==25:
self.line25.setText(mylinelist[c])

I rather have someting like
pseudo code follows:

self.line+"c"+.setText(mylinelist[c])

How to do that ?


for index, value in enumerate(mylinelist):
getattr(self, "line%d" % (index + 1)).setText(value)

A better approach would be to create a list of lineedit fields in the
__init__() method instead of (or in addition to) the individual lineNNN
attributes and then just write

for edit, value in zip(self.lineedits, mylinelist):
edit.setText(value)

If you want to play it safe you might also want to clear the extra
lineedits:

from itertools import repeat, chain, izip
for edit, value in izip(self.lineedits, chain(mylinelist, repeat(""))):
edit.setText(value)

Peter

Aug 17 '05 #2
On 8/17/05, Nx <no****@nomail.com> wrote:
I want to unpack a list into variables
the list is created at runtime and I do not know
how many items there will be , but I know not more than 25.
The list unpacks into variables which are prev. defined as
line1,line2....line25 which are actually the names of lineedit fields in
a Qt gui application and which need to be populated.
I currently use following to achieve that :

c=0
while c < len(mylinelist):
c = c + 1
if c==1:
self.line1.setText(mylinelist[c])
if c==2 :
self.line2.setText(mylinelist[c])
.
.
.
if c==25:
self.line25.setText(mylinelist[c])

I rather have someting like
pseudo code follows:

self.line+"c"+.setText(mylinelist[c])

How to do that ?


Lift would be much easier if you held your 'linenn' lineedit fields in
a list instead. Is that possible? If so, you could do something like
(untested):

for index, line in enumerate(mylinelist):
self.lines[index].setText(line)

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Aug 17 '05 #3
Nx
> for index, value in enumerate(mylinelist):
getattr(self,Â*"line%d"Â*%Â*(indexÂ*+Â*1)).setText (value)


Thanks ! This was the line which did it ....
The correct code snippet now reads :

# set the lineedits of the QT gui
for index,value in enumerate(self.mylinelist):
getattr(self, "lineEdit%d" % (index + 1)).setText(self.tr(value))
Nx
Aug 17 '05 #4

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

Similar topics

4
by: ahsan Imam | last post by:
Hello All, I am trying to move an application from python 1.5.2 to 2.3. The code works fine in 1.5.2 but gives the exception (exceptions.TypeError unpack non-sequence) in python 2.3. I did not...
6
by: aurora | last post by:
I find that I use some list unpacking construct very often: name, value = s.split('=',1) So that 'a=1' unpack as name='a' and value='1' and 'a=b=c' unpack as name='a' and value='b=c'. The...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
3
by: Elezar Simeon Papo | last post by:
Hello All, I have a tab separated input file (data.txt) in text format - the file looks like this SCHOOL DEPART1 DEPART2 DEPART3 Harvard Economics Mathematics Physics...
0
by: Steve | last post by:
Are there any gurus of the pack/unpack world out there that can help me with this one? I have an encrypted string which I'm reading from a zipped file. It was written using pack ( "la" ....
4
by: jimgardener | last post by:
hi am a beginner in python and PIL .I need to read an RGB 8 bit image (am using jpeg )and pack the rgb color values into a double value so i can store the image as a list of pixelvalues.From my...
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
1
by: joe shoemaker | last post by:
If you have the following: data = unpack('>L', sock.recv(4)) Does this line of code means that incoming data is big endian and unpack it to endianess of local machine? If local machine is...
17
by: JRough | last post by:
I have used this function to create a string called $headers: function GetHeaders($file_name){ return "<th><a href='".$file_name."&order_by=l_e'>L_E</a></th> <th><a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.