Awkward format string

Discussion in 'Python' started by beginner, Aug 1, 2007.

  1. beginner

    beginner Guest

    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
    beginner, Aug 1, 2007
    #1
    1. Advertising

  2. beginner

    Chris Mellon Guest

    On 8/1/07, beginner <> wrote:
    > 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?
    Chris Mellon, Aug 1, 2007
    #2
    1. Advertising

  3. beginner

    beginner Guest

    On Aug 1, 11:31 am, "Chris Mellon" <> wrote:
    > On 8/1/07, beginner <> wrote:
    >
    > > 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?


    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.
    beginner, Aug 1, 2007
    #3
  4. beginner

    Neil Cerutti Guest

    On 2007-08-01, beginner <> wrote:
    > 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.

    --
    Neil Cerutti
    Neil Cerutti, Aug 1, 2007
    #4
  5. beginner

    Ian Clark Guest

    beginner wrote:
    > 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
    >


    >>> import datetime
    >>> old_tuple = (

    .... 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
    Ian Clark, Aug 1, 2007
    #5
  6. beginner

    Chris Mellon Guest

    On 8/1/07, beginner <> wrote:
    > On Aug 1, 11:31 am, "Chris Mellon" <> wrote:
    > > On 8/1/07, beginner <> wrote:
    > >
    > > > 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?

    >
    > 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.
    Chris Mellon, Aug 1, 2007
    #6
  7. beginner

    Guest

    On Aug 1, 9:42 am, beginner <> wrote:


    (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;

    --
    Hope this helps,
    Steven
    , Aug 1, 2007
    #7
  8. On Aug 1, 6:11 pm, beginner <> wrote:
    > 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:]
    Gerard Flanagan, Aug 1, 2007
    #8
  9. beginner

    Ian Clark Guest

    Gerard Flanagan wrote:
    > (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
    Ian Clark, Aug 1, 2007
    #9
  10. On Aug 1, 11:52 pm, Ian Clark <> wrote:
    > Gerard Flanagan wrote:
    > > (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. ;)
    >


    oops! SAD (Solipsistic API Design)... ;-)

    Gerard
    Gerard Flanagan, Aug 2, 2007
    #10
  11. 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?




    >>> from datetime import datetime
    >>> dt = datetime(2007,8,2)
    >>> dt

    datetime.datetime(2007, 8, 2, 0, 0)
    >>> str(dt)

    '2007-08-02 00:00:00'
    >>> "%s" % dt

    '2007-08-02 00:00:00'
    >>> dt.date()

    datetime.date(2007, 8, 2)
    >>> str(dt.date())

    '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))
    Bruno Desthuilliers, Aug 2, 2007
    #11
  12. beginner

    beginner Guest

    On Aug 2, 3:32 am, Bruno Desthuilliers <bruno.
    > wrote:
    > 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?

    >
    > >>> from datetime import datetime
    > >>> dt = datetime(2007,8,2)
    > >>> dt

    > datetime.datetime(2007, 8, 2, 0, 0)
    > >>> str(dt)

    > '2007-08-02 00:00:00'
    > >>> "%s" % dt

    > '2007-08-02 00:00:00'
    > >>> dt.date()

    > datetime.date(2007, 8, 2)
    > >>> str(dt.date())

    > '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. :)
    beginner, Aug 2, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Woody
    Replies:
    3
    Views:
    358
    James Kanze
    Jul 11, 2008
  2. Ben Phillips
    Replies:
    6
    Views:
    866
    Roger Lindsjö
    Aug 23, 2008
  3. rickman

    Awkward Arithmetic

    rickman, Mar 15, 2010, in forum: VHDL
    Replies:
    9
    Views:
    585
    rickman
    Mar 22, 2010
  4. Chris Angelico
    Replies:
    3
    Views:
    149
    Mark Lawrence
    Mar 1, 2013
  5. Joe Snodgrass

    Compilation of Awkward Syntax

    Joe Snodgrass, Apr 27, 2013, in forum: C++
    Replies:
    25
    Views:
    462
    Stuart
    May 7, 2013
Loading...

Share This Page