Re: What's the neatest way of getting dictionary entries in a specified order?

2017-03-08 Thread gvmcmt
On Thursday, March 9, 2017 at 1:03:31 AM UTC+5:30, Chris Green wrote:
> I have a fairly simple application that populates a GUI window with
> fields from a database table.  The fields are defined/configured by a
> dictionary as follows:-
> 
> # 
> # 
> # Address Book field details, dictionary key is the database column 
> # 
> dbcol = {}
> dbcol['firstname'] = col('First Name', True, False)
> dbcol['lastname'] = col('Last Name', True, False)
> dbcol['email'] = col('E-Mail', True, True)
> dbcol['phone'] = col('Phone', True, True)
> dbcol['mobile'] = col('Mobile', True, True)
> dbcol['address'] = col('Address', True, False)
> dbcol['town'] = col('Town/City', True, False)
> dbcol['county'] = col('County/Region', True, False)
> dbcol['postcode'] = col('PostCode', True, False)
> dbcol['country'] = col('Country', True, False)
> dbcol['notes'] = col('Notes', True, False)
> dbcol['www'] = col('Web Page', True, True)
> dbcol['categories'] = col('Categories', True, True)
> 
> How can I get the fields in the GUI window in the order I want rather
> than the fairly random order that they appear in at the moment?
> 
> Currently the GUI fields are populated by a for loop as follows:-
> 
> # 
> # 
> # Put values into the fields 
> # 
> i = 0
> for col, field in abookdb.dbcol.items():
> print(field.heading)
> if (i > numkeys/2):
> self.addfieldentry(col, address, field, self.rtable, i-numkeys/2)
> else:
> self.addfieldentry(col, address, field, self.ltable, i)
> i = i + 1
> 
> The for loop gets the items from the dictionary in an order that isn't
> what I want.  How can I configure things so they're in the order I want?
> 
> -- 
> Chris Green
> ·

There is OrderedDict.

https://docs.python.org/2/library/collections.html#collections.OrderedDict

https://docs.python.org/3/library/collections.html#collections.OrderedDict
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: str.title() fails with words containing apostrophes

2017-03-06 Thread gvmcmt
On Monday, March 6, 2017 at 2:37:11 PM UTC+5:30, Jussi Piitulainen wrote:
> gvm...@gmail.com writes:
> 
> > On Sunday, March 5, 2017 at 11:25:04 PM UTC+5:30, Steve D'Aprano wrote:
> >> I'm trying to convert strings to Title Case, but getting ugly results
> >> if the words contain an apostrophe:
> >> 
> >> 
> >> py> 'hello world'.title()  # okay
> >> 'Hello World'
> >> py> "i can't be having with this".title()  # not okay
> >> "I Can'T Be Having With This"
> >> 
> >> 
> >> Anyone have any suggestions for working around this?
> 
> [snip sig]
> 
> > import string
> >
> > txt = "i can't be having with this"
> > string.capwords(txt)
> >
> > That gives you "I Can't Be Having With This"
> >
> > Hope that helps.
> 
> Won't Steve D'aprano And D'arcy Cain Be Happy Now :)


I found it at https://docs.python.org/3/library/string.html#string.capwords :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: str.title() fails with words containing apostrophes

2017-03-06 Thread gvmcmt
On Sunday, March 5, 2017 at 11:25:04 PM UTC+5:30, Steve D'Aprano wrote:
> I'm trying to convert strings to Title Case, but getting ugly results if the
> words contain an apostrophe:
> 
> 
> py> 'hello world'.title()  # okay
> 'Hello World'
> py> "i can't be having with this".title()  # not okay
> "I Can'T Be Having With This"
> 
> 
> Anyone have any suggestions for working around this?
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.

import string 

txt = "i can't be having with this" 
string.capwords(txt) 

That gives you "I Can't Be Having With This" 

Alternatively

txt = "i can't be having with this"

' '.join([word.capitalize() for word in txt.split()])

will result in: "I Can't Be Having With This"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: str.title() fails with words containing apostrophes

2017-03-06 Thread gvmcmt
On Sunday, March 5, 2017 at 11:25:04 PM UTC+5:30, Steve D'Aprano wrote:
> I'm trying to convert strings to Title Case, but getting ugly results if the
> words contain an apostrophe:
> 
> 
> py> 'hello world'.title()  # okay
> 'Hello World'
> py> "i can't be having with this".title()  # not okay
> "I Can'T Be Having With This"
> 
> 
> Anyone have any suggestions for working around this?
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.


import string

txt = "i can't be having with this"
string.capwords(txt)

That gives you "I Can't Be Having With This"

Hope that helps.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: list of the lists - append after search

2017-03-05 Thread gvmcmt
On Thursday, March 2, 2017 at 9:33:14 PM UTC+5:30, Andrew Zyman wrote:
> Hello,
>  please advise.
> 
>  I'd like search and append the internal list in the list-of-the-lists.
> 
> Example:
>  ll =[ [a,1], [b,2], [c,3], [blah, 1000] ]
> 
>  i want to search for the internal [] based on the string field and, if 
> matches, append that list with a value.
> 
>   if internal_list[0] == 'blah':
>   ll[ internal_list].append = [ 'new value']
> 
> End result:
>  ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ]
> 
> 
> I came up with the following, but the second stmnt is not correct:
> 
> print [x for x in ll if x[0]== 'blah']
> print ll.index([x for x in ll if x[0]=='blah'])
> 
> output:
> ValueError: [['blah', 1]] is not in list
> 
> 
> 
> 
> thank you
> AZ


list_of_lists = [['a', 1], ['b', 2], ['c', 3], ['blah', 1000]]

for sublist in list_of_lists:
if sublist[0] == 'blah':
sublist.append('new value')


Hope that helps.
-- 
https://mail.python.org/mailman/listinfo/python-list