5
\$\begingroup\$

I would like to improve the following code:

    result = database.execute(script, fetch="0") # (6, 2016) for example
    month = str(result[0])
    year = str(result[1])
    if len(month) == 1:
        month = "0" + month
    return "%s/%s" % (month, year)

I'm feeling that my code is dumb, and could be more intelligent.

Python version 2.6.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Could you update your post to explain what your code is supposed to do? It is hard to review something without knowing its purpose! \$\endgroup\$ Jan 12, 2017 at 20:16

3 Answers 3

9
\$\begingroup\$

You can use the datetime module:

import datetime as dt
result = (2, 2017)
date = dt.date(year=result[1], month=result[0], day=1)
date_str = date.strftime("%m/%Y")
\$\endgroup\$
7
\$\begingroup\$
>>> result = (1, 17)
>>> f'{result[0]:02}/{result[1]}'
'01/17'
>>> '{result[0]:02}/{result[1]}'.format(result=result)
'01/17'
\$\endgroup\$
2
2
\$\begingroup\$

Is it necessary to be ints ? well as i see it , and it may be too complicated for your program you can make a dictionary table, and use it.

monthDict= {
  1: "january",
  2: "february",
  #etc
}

# and then just use
month = montDict(integerRepresentationOfMont)
# and you can do that for year, month, day etc and format it accordingly with strings manipulation.

As i understood from colleagues above, you are either using long way outdated version of python ( which i find unlikely as when you download python IDE it offers you a newest version ) or you are a novice or you are trying to figure out how str.format is done.

So lets assume you are novice. You can format integers in python as string ,rerp, hex,oct,bin etc etc with str.format as colleague above said.

String formating can be done in several ways:

#string concatination
somestirng = "somestring"+ str(someinteger)

#string joining, use some string to join some other string = somestring.join(someotherstirng) ofcorse "" is valid string
somestirng = "-".join(listofItems) # if listofItems = [1,2,3] result would be 1-2-3

#formating
somestirng = "{0:02}".format(integer)

#or using % sign
a = "somestring %s" %( 4)

As you can see there are to many ways you can use and mess up string, the way you use it is mostly defined by your preferences and program requirement.

Using an dictionary to switch between values: Part of the solution i have given to you above, but using string manipulation shown just now, we can spice the dictionary with some formating. research lambda functions or you can just put normal functions as value in dictionary

If you know your way inside python programming language, and you are trying to figure out how to do str.format on your own you can see this link for source code

Lets see what we have as normal startup tools, we have use of chr or ord as characters and integers that can represent characters, and most of built_in data types have str method that can convert the output value to string.

If program doesn't need complex binary/hex/oct/int conversion and searching for characters, we can just convert it to string and concatenate it to another string. I could reproduce class Format from source code, as for that we have THE SOURCE CODE, i could reproduce the requested formating for datetime string.

def mineDate(dateTuple):
    year,month,day = dateTuple # you should format dateTuple as (year,month,day)
    # result for (2000,12,4) 12/4/2000
    return "{0:02}/{1:02}/{2:04}".format(month,day,year)

If you want to change months from integer to names or abbreviations you can use dictionary for that and some other

This should work for your case. For further research google string formating or string concatenation

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.