Awkward format string

B

beginner

Hi,

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.

x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x

e is a tuple. x is my new tuple.

Does anyone know better ways of handling this?

Thanks,
Geoffrey
 
C

Chris Mellon

Hi,

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.

x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x

e is a tuple. x is my new tuple.

Does anyone know better ways of handling this?

You seem to be doing quite complicated things with your magical e
tuple. Do you have some specific aversion to classes?
 
B

beginner

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.
x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x
e is a tuple. x is my new tuple.
Does anyone know better ways of handling this?

You seem to be doing quite complicated things with your magical e
tuple. Do you have some specific aversion to classes?

e is not complicated. It is a record that have 7 fields. In my program
a function outputs a list of tuples, each is of type e, and now I just
need to send them to a text file.

I have no problem using classes and I do use them everywhere. But
using classes does not solve my problem here. I will probably find
myself doing:

print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % (x.field1..strftime("%Y-%m-
%d"), x.field2..strftime("%Y-%m-%d"), x.field3, x.field4, x.field5,
x.field.6, x.field7)

This is also tedious and error-prone.
 
N

Neil Cerutti

print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" %
(x.field1..strftime("%Y-%m- %d"),
x.field2..strftime("%Y-%m-%d"), x.field3, x.field4, x.field5,
x.field.6, x.field7)

This is also tedious and error-prone.

Providing a suitable .str or .__repr__ method for your class
may make that problem disappear.
 
I

Ian Clark

beginner said:
Hi,

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.

x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x

e is a tuple. x is my new tuple.

Does anyone know better ways of handling this?

Thanks,
Geoffrey
.... datetime.datetime(2007, 8, 1),
.... datetime.datetime(2007, 8, 2),
.... 1,
.... 2.0,
.... 3.0,
.... 4
.... )
first_date = old_tuple[0].strftime('%Y-%m-%d')
second_date = old_tuple[1].strftime('%Y-%m-%d')
new_tuple = (first_date, second_date) + old_tuple[2:]
print '\t'.join(str(i) for i in new_tuple)
2007-08-01 2007-08-02 1 2.0 3.0 4

Without more information that's the best I can think of.

Ian
 
C

Chris Mellon

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.
x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x
e is a tuple. x is my new tuple.
Does anyone know better ways of handling this?

You seem to be doing quite complicated things with your magical e
tuple. Do you have some specific aversion to classes?

e is not complicated. It is a record that have 7 fields. In my program
a function outputs a list of tuples, each is of type e, and now I just
need to send them to a text file.

I have no problem using classes and I do use them everywhere. But
using classes does not solve my problem here. I will probably find
myself doing:

print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % (x.field1..strftime("%Y-%m-
%d"), x.field2..strftime("%Y-%m-%d"), x.field3, x.field4, x.field5,
x.field.6, x.field7)

This is also tedious and error-prone.

If you ever need to write this more than once you're doing it wrong.
I'm not sure what's "tedious and error prone" about specifying the
format of your data file.
 
A

attn.steven.kuo

(snipped)
e is not complicated. It is a record that have 7 fields. In my program
a function outputs a list of tuples, each is of type e, and now I just
need to send them to a text file.

I have no problem using classes and I do use them everywhere. But
using classes does not solve my problem here. I will probably find
myself doing:

print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % (x.field1..strftime("%Y-%m-
%d"), x.field2..strftime("%Y-%m-%d"), x.field3, x.field4, x.field5,
x.field.6, x.field7)

This is also tedious and error-prone.



You can implement a __str__ special method in a class. You can
use 'type' to examine, well, the type of an object. So:

from datetime import datetime

class PrettyDT(datetime):
def __str__(self):
return self.strftime('%Y-%m-%d')


e = (PrettyDT(2007, 8, 1), PrettyDT(2007, 8, 2),
1, 2.0, 3.0, 4)

print '\t'.join(str(each) for each in e)


# Or even

format = { int: '%d', float: '%f', PrettyDT: '%s' }
format_string = '\t'.join(format[type(each)] for each in e)
print format_string % e;
 
G

Gerard Flanagan

Hi,

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.

x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x

e is a tuple. x is my new tuple.

Does anyone know better ways of handling this?

YEARMONTHDAY = "%Y-%m-%d"

def strftime(dt):
return dt.strftime(YEARMONTHDAY)

def tostring(data):
return tuple(strftime(x) for x in data[:2]) + data[2:]
 
I

Ian Clark

Gerard said:
(snip)

def tostring(data):
return tuple(strftime(x) for x in data[:2]) + data[2:]

Hrmm, not sure that having a function named tostring() that returns a
tuple is the best idea. ;)

Ian
 
B

Bruno Desthuilliers

beginner a écrit :
Hi,

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list,
s/list/tuple/

and then
send it to print. The following is an example.

x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x

e is a tuple. x is my new tuple.

Does anyone know better ways of handling this?

'2007-08-02'


Do you really need datetime objects ? If not, using date objects instead
would JustWork(tm) - at least until someone ask you to use another date
format !-)


Else, and since you seem to have a taste for functional programming:

from datetime import datetime
from functools import partial

def iformat(e):
fake = lambda obj, dummy: obj
for item in e:
yield getattr(item, 'strftime', partial(fake, item))('%Y-%m-%d')


e = (datetime(2007,8,1),datetime(2007,8,2) ,42, 0.1, 0.2, 0.3, 1138)
print tuple(iformat(e))
print "%s\t%s\t%d\t%f\t%f\t%f\t%d" % tuple(iformat(e))
 
B

beginner

beginner a écrit :
In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list,
s/list/tuple/

and then
send it to print. The following is an example.
x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x
e is a tuple. x is my new tuple.
Does anyone know better ways of handling this?
'2007-08-02'

Do you really need datetime objects ? If not, using date objects instead
would JustWork(tm) - at least until someone ask you to use another date
format !-)

Else, and since you seem to have a taste for functional programming:

from datetime import datetime
from functools import partial

def iformat(e):
fake = lambda obj, dummy: obj
for item in e:
yield getattr(item, 'strftime', partial(fake, item))('%Y-%m-%d')

e = (datetime(2007,8,1),datetime(2007,8,2) ,42, 0.1, 0.2, 0.3, 1138)
print tuple(iformat(e))
print "%s\t%s\t%d\t%f\t%f\t%f\t%d" % tuple(iformat(e))

Thanks.

The 'functional' taste is still under development. It hasn't reached
production quality yet. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top