How can I parse this correctly?

Ben Finney ben+python at benfinney.id.au
Sun Apr 6 04:16:22 EDT 2014


Anthony Papillion <papillion at gmail.com> writes:

> On Apr 5, 2014, at 23:21, Ben Finney <ben+python at benfinney.id.au> wrote:
> > Alternatively, if you just want to do integer arithmetic on the
> > year, you don't need the ‘datetime’ module at all.
>
> True. But I do actually need to some date based calculations.
> Basically I'm processing a large data set and calculating time
> intervals between entries

Okay. So, it seems that some entries have (some of?) the date components
blank.

There is no sensible general-purpose default for ‘datetime.date’, so you
will need to decide what “empty date” means for your data.

> > Python doesn't have “cast”; instead, you request the creation of a
> > new object by calling the type.
>
> Hmm, interesting. I need to think on that for a moment.  I may well
> have completely misunderstood a major part of Python all this time.

Yes, it's important to recognise that all Python's built-in types are
callable just as user-defined types are; and by calling them, you are
requesting a new instance.

> >> print int(row['YEAR'])
> >
> > What do you expect this to return when ‘row['YEAR']’ is ‘""’ (empty
> > string)?
>
> I expected a defaut value to be returned, perhaps "0".

You'll need to be aware that the Gregorian calendar (which is what
‘datetime.date’ uses) has no year zero. 1 BCE is immediately succeeded
by 1 CE.

    <URL:https://en.wikipedia.org/wiki/Year_Zero>

So, again, there's no good general-purpose default year in our calendar.
Any system will need an explicit decision for the most sensible default
for its purpose.

> I see now from another response that this is not the case and so I've
> fixed it to read
>
> print int(row['YEAR'] or 0000)

“foo or bar” is not a Pythonic way to get a default value; it relies on
quirks of implementation and is not expressive as to the meaning you
intend.

Rather, be explicit:

    # Default to the beginning of the project.
    year = 1969
    if row['YEAR']:
        # Use the value as a text representation of a year.
        year = int(row['YEAR'])

Also, be aware that Python allows leading-zero integer literals, but
<URL:http://catb.org/jargon/html/H/hysterical-reasons.html> interprets
them not as decimal (base ten), but as octal (base eight).

If “this integer is base-eight for a good reason explained nearby” is
not your intent, don't put a leading-zero integer literal in the code.

> Thank you! I actually like the fact that [Python's ‘int’ initialiser]
> won't simply "fill something in". It makes things more predictable and
> stable.

Welcome to a dependable language :-)

-- 
 \       “Firmness in decision is often merely a form of stupidity. It |
  `\        indicates an inability to think the same thing out twice.” |
_o__)                                                —Henry L. Mencken |
Ben Finney




More information about the Python-list mailing list