Question on keyword arguments

2016-02-18 Thread grsmith

Would this be the correct way to return
a list as a default result.

Also, would the list be the preferable result (to a python programmer) ?

def test(command, return_type='LIST'):
""" Go to database and return data"""
if return_type == 'LIST':
result = ['ONE', 'TWO', 'THREE']
else:
result = r'0xfeONE\0exfeTWO\0xfeTHREE'
return result

if __name__ == '__main__':
print(test('cmd'))
print(test('cmd', 'LIST'))
print(test('cmd', None))
print(test('cmd', 'string'))


thanks,
george
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question on keyword arguments

2016-02-18 Thread grsmith
Thanks to all who responded, it is a big help. 


Tim, the 'crazy-other-result format' is the
result returned by the database, nothing
I can do about that :)

Thanks again
George

-Original Message- 
From: Chris Angelico 
Sent: Thursday, February 18, 2016 10:45 AM 
Cc: [email protected] 
Subject: Re: Question on keyword arguments 


On Fri, Feb 19, 2016 at 2:39 AM, Dan Strohl  wrote:

So, define a return object like:

from collections import UserList
class TestResponse(UserList):
def __str__(self):
return '\0xfe%s' % '\0xfe'.join(self.data)

...and return that object.


Out of interest, why UserList rather than simply subclassing list?

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list


Nested List question

2016-02-24 Thread grsmith
All,

Can you have a phython list like:
['George',
'Soros',
['99 First Street',
  '33 Broadway Avenue', ['Apt 303'],
  '1 Park Avenue'],
  'New York', 'NY']

In other words how do you correctly nest the
['Apt 303'] so it goes with 33 Broadway Avenue.

Also, I tried several ways and could not figure out
how to get the ['Apt 303'] out of the list. How can 
you do that. It is the ['Apt 303'] that is the problem,
I know how to do the other elements.

thanks
George 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested List question

2016-02-24 Thread grsmith

Erik,

Works perfectly, thanks much !!!
Mark, I am tied to data structure.

for address in addresses:
   if isinstance(address, str):
   apt = None
   print(address, apt)
   else:
   address, apt = address
   print(address, apt)


99 First Street None
33 Broadway Avenue Apt 303
1 Park Avenue None


-Original Message- 
From: Erik

Sent: Wednesday, February 24, 2016 4:28 PM
To: [email protected] ; python-list
Subject: Re: Nested List question

On 24/02/16 20:59, [email protected] wrote:

Can you have a phython list like:
['George',
'Soros',
['99 First Street',
   '33 Broadway Avenue', ['Apt 303'],
   '1 Park Avenue'],
   'New York', 'NY']

In other words how do you correctly nest the
['Apt 303'] so it goes with 33 Broadway Avenue.


The way to keep those particular items together is to have something like:

data = \
['George', 'Soros',
  [ '99 First Street',
['33 Broadway Avenue', 'Apt 303'],
'1 Park Avenue'
  ],
  'New York', 'NY'
]

And then, guessing from the structure of your list, you'd do something like:

forename, surname, addresses, city, state = data

And then, something like:

for address in addresses:
  if type(address) == StringType:
apt = None
  else:
address, apt = address

That should work to pick apart with what you have presented above, but I
think you'd be better off presenting the data with a bit more structure
in the first place.


Also, I tried several ways and could not figure out
how to get the ['Apt 303'] out of the list. How can
you do that.


It would be much easier to work out what you mean by this if you'd have
actually said what you'd tried and what you had expected to happen and
why what did happen wasn't right for you.

E. 


--
https://mail.python.org/mailman/listinfo/python-list