Re: [Tutor] is there a better way to do this?

2016-05-10 Thread Chris Roy-Smith

On 10/05/16 07:03, Ondřej Rusek wrote:

Dne 9.5.2016 v 10:13 Chris Roy-Smith napsal(a):

Hi
Python 3.4 Linux (ubuntu)

This code does what I want.
curs is the result of a mysql query


data = [[" " for x in range(9)] for y in range(count)]
for (ddate, mood, walk, lag, sleep) in curs:
data[row][0]=ddate
data[row][1]=mood
data[row][2]=walk
data[row][3]=lag
data[row][4]=sleep
row +=1


if you want 'lists in list' (like your solution):

data = []
for ddate, mood, walk, lag, sleep in curs:
data += [ [ddate, mood, walk, lag, sleep] ]

or 'tuples in list':

data = []
for ddate, mood, walk, lag, sleep in curs:
  data += [ (ddate, mood, walk, lag, sleep) ]

but for 'tuples in list'... simple:

data = []
for record in curs:
  data += [record]



Thanks,
I hadn't considered having a second list of lists for my calculations,
Your solution is the sort of thing I was looking for.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-10 Thread Chris Roy-Smith

On 10/05/16 12:01, Steven D'Aprano wrote:

On Mon, May 09, 2016 at 06:13:32PM +1000, Chris Roy-Smith wrote:


data = [[" " for x in range(9)] for y in range(count)]
for (ddate, mood, walk, lag, sleep) in curs:
 data[row][0]=ddate
 data[row][1]=mood
 data[row][2]=walk
 data[row][3]=lag
 data[row][4]=sleep
 row +=1

While I don't know a better way to do this, it seems a bit awkward, is
there a better way?

Hmmm, it's hard to be sure because we don't really know what count is.
Do you want a bunch of empty rows at the end? My guess is No.

In your code above, you initialise each row with ten spaces, and only
replace five of them. So assuming you need the extra five spaces:

data = [record + [" "]*5 for record in curs]

provided curs returns lists, rather than tuples. (If not, it's
easy to just convert using `list(record)`.

If you don't need the extra five columns, the code is even simpler:

data = list(curs)

Thank you,
that's much better
I thought I needed the extra columns, but I changed things to use 2 
lists of lists
(one generated with the above line and another to hold my calculated 
results)




What if you do want extra blank rows? Easiest to just add them at the
end:

# initialise data as above, then add blanks
for i in range(how_many_extra_rows):
 data.append([" "]*10)

which can be simplified to:

data.extend([[" "]*10 for i in range(how_many_extra_rows)])





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-09 Thread Steven D'Aprano
On Mon, May 09, 2016 at 06:13:32PM +1000, Chris Roy-Smith wrote:

> data = [[" " for x in range(9)] for y in range(count)]
> for (ddate, mood, walk, lag, sleep) in curs:
> data[row][0]=ddate
> data[row][1]=mood
> data[row][2]=walk
> data[row][3]=lag
> data[row][4]=sleep
> row +=1
> 
> While I don't know a better way to do this, it seems a bit awkward, is 
> there a better way?

Hmmm, it's hard to be sure because we don't really know what count is. 
Do you want a bunch of empty rows at the end? My guess is No. 

In your code above, you initialise each row with ten spaces, and only 
replace five of them. So assuming you need the extra five spaces:

data = [record + [" "]*5 for record in curs]

provided curs returns lists, rather than tuples. (If not, it's 
easy to just convert using `list(record)`.

If you don't need the extra five columns, the code is even simpler:

data = list(curs)


What if you do want extra blank rows? Easiest to just add them at the 
end:

# initialise data as above, then add blanks
for i in range(how_many_extra_rows):
data.append([" "]*10)

which can be simplified to:

data.extend([[" "]*10 for i in range(how_many_extra_rows)])



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-09 Thread Joel Goldstick
On Mon, May 9, 2016 at 6:49 PM, Alan Gauld via Tutor  wrote:
> On 09/05/16 22:03, Ondřej Rusek wrote:
>
>> but for 'tuples in list'... simple:
>>
>> data = []
>> for record in curs:
>>data += [record]
>
> Couldn't that be abbreviated to:
>
> date = list(curs)
>
I thought I nailed it earlier (and others) but this is great.  An
expressive language, python
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-09 Thread Alan Gauld via Tutor
On 09/05/16 22:03, Ondřej Rusek wrote:

> but for 'tuples in list'... simple:
> 
> data = []
> for record in curs:
>data += [record]

Couldn't that be abbreviated to:

date = list(curs)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-09 Thread Ondřej Rusek

Dne 9.5.2016 v 10:13 Chris Roy-Smith napsal(a):

Hi
Python 3.4 Linux (ubuntu)

This code does what I want.
curs is the result of a mysql query


data = [[" " for x in range(9)] for y in range(count)]
for (ddate, mood, walk, lag, sleep) in curs:
data[row][0]=ddate
data[row][1]=mood
data[row][2]=walk
data[row][3]=lag
data[row][4]=sleep
row +=1


if you want 'lists in list' (like your solution):

data = []
for ddate, mood, walk, lag, sleep in curs:
data += [ [ddate, mood, walk, lag, sleep] ]

or 'tuples in list':

data = []
for ddate, mood, walk, lag, sleep in curs:
  data += [ (ddate, mood, walk, lag, sleep) ]

but for 'tuples in list'... simple:

data = []
for record in curs:
  data += [record]


--
S pozdravem

--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Ondrej Rusek
GYmnazium BOzeny Nemcove, Hradec Kralove, Czech
ru...@gybon.cz, http://www.gybon.cz/~rusek
ICQ: 150366991
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


--
Tato zprava byla prohledana na vyskyt viru
a nebezpecneho obsahu antivirovym systemem
MailScanner a zda se byt cista.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-09 Thread Peter Otten
Chris Roy-Smith wrote:

> Hi
> Python 3.4 Linux (ubuntu)
> 
> This code does what I want.
> curs is the result of a mysql query
> 
> 
> data = [[" " for x in range(9)] for y in range(count)]
> for (ddate, mood, walk, lag, sleep) in curs:
>  data[row][0]=ddate
>  data[row][1]=mood
>  data[row][2]=walk
>  data[row][3]=lag
>  data[row][4]=sleep
>  row +=1
> 
> 
> While I don't know a better way to do this, it seems a bit awkward, is
> there a better way?

Does `curs` give `count` records? If so:

data = [
   list(row) + [" "] * 4
   for row in curs
]

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is there a better way to do this?

2016-05-09 Thread Joel Goldstick
On Mon, May 9, 2016 at 4:13 AM, Chris Roy-Smith
 wrote:
> Hi
> Python 3.4 Linux (ubuntu)
>
> This code does what I want.
> curs is the result of a mysql query
>
>
does this work (untested)?

data = []
for stuff in curs:
   data.append(stuff)
>
> While I don't know a better way to do this, it seems a bit awkward, is there
> a better way?
>
>
> Thank you
> Chris Roy-Smith
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] is there a better way to do this?

2016-05-09 Thread Chris Roy-Smith

Hi
Python 3.4 Linux (ubuntu)

This code does what I want.
curs is the result of a mysql query


data = [[" " for x in range(9)] for y in range(count)]
for (ddate, mood, walk, lag, sleep) in curs:
data[row][0]=ddate
data[row][1]=mood
data[row][2]=walk
data[row][3]=lag
data[row][4]=sleep
row +=1


While I don't know a better way to do this, it seems a bit awkward, is 
there a better way?



Thank you
Chris Roy-Smith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a "better" way to do this?

2004-12-22 Thread Kent Johnson
John,
In this case I think using a class is overkill. You can write a simple 
procedural version like this:
def getOption():
while True:
print """
Please choose from the following options.
1) - Normal Unit test with static data.
2) - Normal Unit test with missing data.
3) - Integration test with current DTG.
4) - Integration test with missing data.
5) - Clean directory
6) - Exit
"""
choice = raw_input("Choice(1-6) ")
try:
choice = int(choice)
except ValueError:
print "Please enter a number from 1 to 6"
if 1 <= choice <= 6:
return choice
print "You have entered an invalid entry. Please try again"
option = getOption()
print "You chose", option
If you have multiple clients you could parameterize it so it becomes
def getOption(prompt, low, high)
I try to do the simplest thing that could possibly work. Sometimes that is just straight line code, 
with maybe a few functions mixed in. Sometimes a procedural approach works fine. Sometimes classes 
and objects are the right way to go, but I don't introduce classes until I need them.

Don't get me wrong, I am a huge fan of OOP, but sometimes it is a bigger hammer 
than you need.
Kent
Ertl, John wrote:
I am trying to do the usual thing of asking for an input and then checking
it to see if it is valid.  If the entry is not valid then ask again until
you get the correct answer.
I have come up with this class.  I am trying to make a transition from
procedural programming to object oriented.   Is this a good approach for
such a check?  It seems to me this is more work then needed. (I can put in a
counter also to break out if you try too many times).
Please comment if you have the time. 

class greating:
def __init__(self):
self.OK = False
self.lowValue = 1
self.highValue = 6
def opening(self):
print """
Please choose from the following options.
1) - Normal Unit test with static data.
2) - Normal Unit test with missing data.
3) - Integration test with current DTG.
4) - Integration test with missing data.
5) - Clean directory
6) - Exit
"""
self.choice = raw_input("Choice(1-6) ")
def check(self):
try:
self.choice = int(self.choice)
except ValueError:
print "Please enter a number from ",self.lowValue," to
",self.highValue
pass

if self.choice > self.highValue or self.choice < self.lowValue:
print "You have entered an invalid entry. Please try again"
else:
self.OK = True

a = greating()
while a.OK != True:
a.opening()
a.check()
print a.choice
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a "better" way to do this?

2004-12-22 Thread Alan Gauld
> procedural programming to object oriented.   Is this a good approach
for
> such a check?  It seems to me this is more work then needed.

Its a valid approach but whether its the best approach depends on
what else you intend to do. For example will there be multiple
types of greeting? or several instances of greeting? Or will
you use this same greeting in many programs? I suspect not
because the menu is embedded, but if you could parameterise
the init method, then you could pass the menu string in along
with high and low values and then you might have a reusable
text menu class?

class Menu:
   def __init__(self, menu, prompt, high, low=1):
  # lots of self assignments here

   def opening(self):
  print self.menu
  self.choice = raw_input(self.prompt)

   def validateChoice(self):
  # this method overriden in subbclasses if need be...
  return self.low < int(self.choice) < self.high

But thats just soome personal musings. Comments on
your code follow:

> class greating:

Its conventional to make class names start with a capital letter,
instances with a lower. Python doesn't case but its easier on
the reader...

> def __init__(self):
> self.OK = False
> self.lowValue = 1
> self.highValue = 6
>
> def opening(self):
> print """
> Please choose from the following options.
> 1) - Normal Unit test with static data.
> 2) - Normal Unit test with missing data.
> 3) - Integration test with current DTG.
> 4) - Integration test with missing data.
> 5) - Clean directory
> 6) - Exit
> """
> self.choice = raw_input("Choice(1-6) ")

See comments above about making menu a configurable item.

> def check(self):
> try:
> self.choice = int(self.choice)
> except ValueError:
> print "Please enter a number from ",self.lowValue," to
> ",self.highValue
> pass

pass does nothing. It is only used to indicate that something
should go here sometime. But since you are doing something,
you don't need pass here. Maybe you meant return?

> if self.choice > self.highValue or self.choice <
self.lowValue:
> print "You have entered an invalid entry. Please try
again"

python allows a shortened version of this like

if x < value < y:   # checks that value is between x and y.

> else:
> self.OK = True

As well as printing the errors you probably want to set self.OK
to False in the other cases. Just because you did it in the init()
is not enough, if you call opening() more than once and the user
types valid input first time but not second time OK will be set
to True when it should be False.

The way you use it in the test case is OK but in other scenarios...

> a = greating()
>
> while a.OK != True:
> a.opening()
> a.check()

Try this test loop instead:

while a.choice not in '123456':   # ie an invalid value
   a.opening()
   a.check()

OOPS! IT won;t work because a.choice doesn't exist until
after calling a.opening().

This is one reason you are better to initialise all
instance variables in the init method IMHO.

HTH

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Is there a "better" way to do this?

2004-12-22 Thread Ertl, John
I am trying to do the usual thing of asking for an input and then checking
it to see if it is valid.  If the entry is not valid then ask again until
you get the correct answer.

I have come up with this class.  I am trying to make a transition from
procedural programming to object oriented.   Is this a good approach for
such a check?  It seems to me this is more work then needed. (I can put in a
counter also to break out if you try too many times).

Please comment if you have the time. 

class greating:

def __init__(self):
self.OK = False
self.lowValue = 1
self.highValue = 6

def opening(self):
print """
Please choose from the following options.
1) - Normal Unit test with static data.
2) - Normal Unit test with missing data.
3) - Integration test with current DTG.
4) - Integration test with missing data.
5) - Clean directory
6) - Exit
"""
self.choice = raw_input("Choice(1-6) ")

def check(self):
try:
self.choice = int(self.choice)
except ValueError:
print "Please enter a number from ",self.lowValue," to
",self.highValue
pass

if self.choice > self.highValue or self.choice < self.lowValue:
print "You have entered an invalid entry. Please try again"
else:
self.OK = True


a = greating()

while a.OK != True:
a.opening()
a.check()

print a.choice


___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor