Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69754

Re: How can I parse this correctly?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'explicit': 0.07; 'purpose.': 0.07; '8bit%:30': 0.09; 'calculating': 0.09; 'indicates': 0.09; 'instance.': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; '(empty': 0.16; '1969': 0.16; '8bit%:32': 0.16; 'calendar.': 0.16; 'callable': 0.16; 'finney': 0.16; 'inability': 0.16; 'literals,': 0.16; 'merely': 0.16; 'octal': 0.16; 'pythonic': 0.16; 'quirks': 0.16; 'rather,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recognise': 0.16; 'returned,': 0.16; 'stable.': 0.16; 'url:catb': 0.16; 'user-defined': 0.16; 'value;': 0.16; 'zero.': 0.16; ':-)': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'year,': 0.18; 'module': 0.19; 'basically': 0.19; "python's": 0.19; 'seems': 0.21; 'print': 0.22; 'header:User- Agent:1': 0.23; 'entries': 0.24; 'integer': 0.24; 'decide': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'fixed': 0.29; "doesn't": 0.30; "i'm": 0.30; '(which': 0.31; 'url:wiki': 0.31; 'decimal': 0.31; 'explained': 0.31; 'relies': 0.31; 'requesting': 0.31; 'url:wikipedia': 0.31; 'writes:': 0.31; 'allows': 0.31; 'another': 0.32; 'text': 0.33; 'beginning': 0.33; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'so,': 0.37; 'expected': 0.38; 'thank': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'expect': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'major': 0.40; 'read': 0.60; 'most': 0.60; 'year.': 0.61; 'new': 0.61; 'simply': 0.61; "you'll": 0.62; 'our': 0.64; 'more': 0.64; 'skip:\xe2 10': 0.65; 'between': 0.67; 'default': 0.69; '8bit%:43': 0.74; 'subject:this': 0.83; '2014,': 0.84; 'expressive': 0.84; 'received:125': 0.84; '\xe2\x80\x9cthis': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: How can I parse this correctly?
Date Sun, 06 Apr 2014 18:16:22 +1000
References <CAJUMiQsoNbNzDgUOkaQxFLGptTqKriD7DcXSeFwwu_-v4TKJKQ@mail.gmail.com> <85zjjz141k.fsf@benfinney.id.au> <985B907F-8EC7-4B5C-8A9F-64AEC3E5803D@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xBD41714B
X-Public-Key-Fingerprint 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:Q/UEx+hNlFgeBzMj3mTvoJAmXWI=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8941.1396772200.18130.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396772200 news.xs4all.nl 2876 [2001:888:2000:d::a6]:37322
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69754

Show key headers only | View raw


Anthony Papillion <papillion@gmail.com> writes:

> On Apr 5, 2014, at 23:21, Ben Finney <ben+python@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How can I parse this correctly? Ben Finney <ben+python@benfinney.id.au> - 2014-04-06 18:16 +1000

csiph-web