-1

I have this code

def testing1(terms, request):
    dat = datetime.now(pytz.timezone(geo_timezone(request)))
    __start = terms['year']+'-'+terms['month']+'-'+terms['day']+'T'+'00:00:00'+dat.strftime('%z')[:-2]+':'+dat.strftime('%z')[-2:]
    __end = terms['year']+'-'+terms['month']+'-'+terms['day']+'T'+'23:59:59'+dat.strftime('%z')[:-2]+':'+dat.strftime('%z')[-2:]
    return __start, __end

testing({"month":12,"day":1, "year":"2015"}, request)

But I have a interrogant, whats is the best way to write this code, readable and friendly for the others programers?

Any suggestion for write code extensive in one line like this?

This suggestion is readable?

def testing2(terms, request):
        dat = datetime.now(pytz.timezone(geo_timezone(request)))
        __start = terms['year'] + '-' + terms['month'] + '-' + terms['day'] + \
                  'T' + '00:00:00' + dat.strftime('%z')[:-2] + ':' + dat.strftime('%z')[-2:]
        __end = terms['year'] + '-' + terms['month'] + '-' + terms['day'] + \
                'T' + '23:59:59' + dat.strftime('%z')[:-2] + ':' + dat.strftime('%z')[-2:]
        return __start, __end
Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Carlos Rodriguez
  • 833
  • 8
  • 12

3 Answers3

0

The only hard part to read is where you build your string so I would use .format(). This way you can see the resulting layout and then all of the corresponding entries.

__start = '{}-{}-{}T00:00:00{}:{}'.format(terms['year'],
                                          terms['month'],
                                          terms['day'],
                                          dat.strftime('%z')[:-2],
                                          dat.strftime('%z')[-2:])

__end = '{}-{}-{}T23:59:59{}:{}'.format(terms['year'],
                                        terms['month'],
                                        terms['day'],
                                        dat.strftime('%z')[:-2],
                                        dat.strftime('%z')[-2:])
SirParselot
  • 2,640
  • 2
  • 20
  • 31
0

You can try something like :

__start = ''.join([terms['year'], '-',
                   terms['month'], '-',
                   terms['day'], 'T',
                   '00:00:00',
                   dat.strftime('%z')[:-2], ':',
                   dat.strftime('%z')[-2:]
                   ])

Parenthesis, brackets and braces will help you to keep your lines of code < 80 characters (and here the join method of string objects is more efficient than operator +)

As your post is about coding style it's hard to not mention the PEP8 if you don't know it already.

mgc
  • 5,223
  • 1
  • 24
  • 37
0

Personally, I'd go with that second block and call it a day. If you want, you could try aligning it into groups, or messing with str.format():

__start = terms['year'] + '-' + terms['month'] + '-' + terms['day'] + \
          'T00:00:00' + \
          dat.strftime('%z')[:-2] + ':' + dat.strftime('%z')[-2:]
__end =   terms['year'] + '-' + terms['month'] + '-' + terms['day'] + \
          'T23:59:59' + \
          dat.strftime('%z')[:-2] + ':' + dat.strftime('%z')[-2:]

 

__start = ('{}-{}-{}'.format(terms['year'], terms['month'], terms['day']) +
           'T00:00:00' +
           '{}:{}'.format(dat.strftime('%z')[:-2], dat.strftime('%z')[-2:]))
__end =   ('{}-{}-{}'.format(terms['year'], terms['month'], + terms['day']) +
           'T23:59:59' +
           '{}:{}'.format(dat.strftime('%z')[:-2], dat.strftime('%z')[-2:]))
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97