Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   The fastest way to convert a long list of date (http://www.velocityreviews.com/forums/t669507-the-fastest-way-to-convert-a-long-list-of-date.html)

loredana.pier@gmail.com 02-08-2009 12:46 PM

The fastest way to convert a long list of date
 
If I want to convert a single date format I can do:

import datetime, dateutil.parser
d = dateutil.parser.parse('2008-09-26)
print d.strftime('%A %d, %b %y' )

but if I want convert a long list of time how can do it in the fastest
way?
for example:
from ['2008-09-26’, '2008-09-28’, '2008-09-29’,.............]
to [’26 sep’,’28 sep’,’29 sep’,................]

Thank you

Loredana

Chris Rebert 02-08-2009 01:05 PM

Re: The fastest way to convert a long list of date
 
On Sun, Feb 8, 2009 at 4:46 AM, <loredana.pier@gmail.com> wrote:
> If I want to convert a single date format I can do:
>
> import datetime, dateutil.parser
> d = dateutil.parser.parse('2008-09-26)
> print d.strftime('%A %d, %b %y' )
>
> but if I want convert a long list of time how can do it in the fastest
> way?
> for example:
> from ['2008-09-26', '2008-09-28', '2008-09-29',.............]
> to ['26 sep','28 sep','29 sep',................]


import datetime
from dateutil.parser import parse
orig_dates = ['2008-09-26', '2008-09-28', ...]#etc
converted_dates = [parse(datestr).strftime('%A %d, %b %y' ) for
datestr in orig_dates]

Or if an iterator would be okay instead of a list, you can substitute
the list comprehension for itertools.imap():

#setup same as before
from itertools import imap
converted_dates = imap(lambda datestr: parse(datestr).strftime('%A %d,
%b %y' ), orig_dates)

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

Peter Otten 02-08-2009 01:52 PM

Re: The fastest way to convert a long list of date
 
loredana.pier@gmail.com wrote:

> If I want to convert a single date format I can do:
>
> import datetime, dateutil.parser
> d = dateutil.parser.parse('2008-09-26)
> print d.strftime('%A %d, %b %y' )
>
> but if I want convert a long list of time how can do it in the fastest
> way?
> for example:
> from ['2008-09-26’, '2008-09-28’, '2008-09-29’,.............]
> to [’26 sep’,’28 sep’,’29 sep’,................]


For large source lists the following should be quite fast...

months = "jan feb mar apr may jun jul aug sep oct dec".split()
lookup = dict(("%02d-%02d" % (mi+1, d), "%02d-%s" % (d, m)) for mi, m in
enumerate(months) for d in range(1, 32))

source = ["2008-09-26", "2008-09-28", "2008-09-29"]
print [lookup[d[-5:]] for d in source]


.... but it is also very brittle. Various compromises are possible, but to
find the best would require that you tell us a bit more about your use
case. In my experience people are a bit too fast to demand "fast".

Peter

loredana.pier@gmail.com 02-08-2009 04:30 PM

Re: The fastest way to convert a long list of date
 
First of all: Thanks for your reply
My use case is the following:
I perform a query to DB and the result of query is this;

>>> print rows

{‘Label’: {'2009:01:30': 9, '2009:01:28': 13, '2009:01:29': 19,
'2009:01:26': 1, '2009:01:27': 3, '2009:01:20': 86, '2009:01:22': 3,
'2009:01:23': 12, '2009:02:07': 3, '2009:02:06': 12, '2009:02:05': 15,
'2009:02:04': 12, '2009:02:02': 2}}

I want to make a chart with this data using JSON format where:
• the key ‘Label’ is the title of chart,
• rows['Label'].keys() is x values,
• rows[‘Label’].values() is the y values

So my goal is:

1) to fill the bin of chart with no data (for example in this tupla..
[.., '2009:01:28: 9, '2009:01:30: 13,..].. the date '2009:01:29’ is
missing and I want to append the value ['2009:01:29’,0] to rows
[‘Label’])

2) convert the date format (for example in '%A %d, %b %y')

3) transform rows dictionary in JSON code

The point 3) is easy to implement but I’m wondering which is the best
way to implement the point 1) and 2) ...considering it will be used
for a web application (CherryPy)?

thanks,

Loredana


All times are GMT. The time now is 07:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.