Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81182 > unrolled thread
| Started by | Steve Hayes <hayesstw@telkomsa.net> |
|---|---|
| First post | 2014-11-21 10:35 +0200 |
| Last post | 2014-11-22 07:34 +0200 |
| Articles | 20 on this page of 29 — 13 participants |
Back to article view | Back to comp.lang.python
Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 10:35 +0200
Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 19:40 +1100
Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 12:15 +0200
Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 21:18 +1100
Re: Using Python for date calculations alister <alister.nospam.ware@ntlworld.com> - 2014-11-21 10:33 +0000
Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 21:47 +1100
Re: Using Python for date calculations random832@fastmail.us - 2014-11-21 08:54 -0500
Re: Using Python for date calculations alister <alister.nospam.ware@ntlworld.com> - 2014-11-21 14:54 +0000
Re: Using Python for date calculations random832@fastmail.us - 2014-11-21 08:58 -0500
Re: Using Python for date calculations Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-22 01:55 +1100
Re: Using Python for date calculations Ned Batchelder <ned@nedbatchelder.com> - 2014-11-21 10:25 -0500
Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-22 01:08 +1100
Re: Using Python for date calculations Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-21 14:50 -0500
Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-22 07:07 +0200
Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-22 16:13 +1100
Re: Using Python for date calculations Gary Herron <gary.herron@islandtraining.com> - 2014-11-21 00:50 -0800
Re: Using Python for date calculations Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-21 10:20 +0000
Re: Using Python for date calculations alister <alister.nospam.ware@ntlworld.com> - 2014-11-21 10:38 +0000
Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 17:50 +0200
Re: Using Python for date calculations Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-21 16:01 +0000
Re: Using Python for date calculations Chris Angelico <rosuav@gmail.com> - 2014-11-21 21:27 +1100
Re: Using Python for date calculations duncan smith <buzzard@invalid.invalid> - 2014-11-21 17:03 +0000
Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-21 21:48 +0200
Re: Using Python for date calculations Dennis Lee Bieber <bieber.genealogy@earthlink.net> - 2014-11-21 15:32 -0500
Re: Using Python for date calculations Dennis Lee Bieber <bieber.genealogy@earthlink.net> - 2014-11-21 14:35 -0500
Re: Using Python for date calculations Paul Blair <p.blair@internode.on.net> - 2014-11-22 06:51 +1100
Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-22 07:31 +0200
Re: Using Python for date calculations Denis Beauregard <denis.b-at-francogene.com@fr.invalid> - 2014-11-21 15:07 -0500
Re: Using Python for date calculations Steve Hayes <hayesstw@telkomsa.net> - 2014-11-22 07:34 +0200
Page 1 of 2 [1] 2 Next page →
| From | Steve Hayes <hayesstw@telkomsa.net> |
|---|---|
| Date | 2014-11-21 10:35 +0200 |
| Subject | Using Python for date calculations |
| Message-ID | <k7tt6aheccqv06enkume0vs0v6kvvhns45@4ax.com> |
I've finally found a use for Python.
When, in the course of my genealogy research, I look at census or burial
records, I often want to work out a person's date of birth from their age.
It's a simple matter of mental arithmetic, but I sometimes get it wrong, and
mislead myself. There are calculators and date calculation programs, but they
are usually too complicated and try to do too much, so by the time you've
worked out what to do it takes much longer.
This Python script does it for me.
year = input("Year: ")
age = input("Age: ")
born = year-age
print 'Year of birth:', born
It's so simple, so elementary, that it's not really worth writing about,
except for the fact that it illustrates the KISS principle.
It is sometimes better to have a simple program that does one thing well than
a complex one that does lots of things, but none of them very efficiently.
The average hand calculator can do the same job, but you have to pick it up
and put it down, and you can't easily see if you've made a typo.
Having said that, however, yes, I would perhaps like to use Python for more
complicated date processing routines, namely to convert the kinds of dates
produced by genealogy programs to a simple yyyy-mm-dd that computer database
programs can understand, so that "Abt May 1677" would be rendered as
"1677-05-00"
Has anyone done something like that in Python?
--
Steve Hayes from Tshwane, South Africa
Web: http://www.khanya.org.za/stevesig.htm
Blog: http://khanya.wordpress.com
E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-21 19:40 +1100 |
| Message-ID | <mailman.16088.1416559225.18130.python-list@python.org> |
| In reply to | #81182 |
On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
> This Python script does it for me.
>
> year = input("Year: ")
> age = input("Age: ")
> born = year-age
> print 'Year of birth:', born
One thing to be careful of: The input() function in Python 2 should be
avoided. Instead, use int(raw_input("Year: ")) and correspondingly
Age. It's much safer and clearer than what you have, which is an alias
for eval(raw_input("Year: ")) - very dangerous.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steve Hayes <hayesstw@telkomsa.net> |
|---|---|
| Date | 2014-11-21 12:15 +0200 |
| Message-ID | <e44u6a121qs79midbsfeotejdde9vbs2n9@4ax.com> |
| In reply to | #81183 |
On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com> wrote:
>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
>> This Python script does it for me.
>>
>> year = input("Year: ")
>> age = input("Age: ")
>> born = year-age
>> print 'Year of birth:', born
>
>One thing to be careful of: The input() function in Python 2 should be
>avoided. Instead, use int(raw_input("Year: ")) and correspondingly
>Age. It's much safer and clearer than what you have, which is an alias
>for eval(raw_input("Year: ")) - very dangerous.
I though input() was OK for integers.
--
Steve Hayes from Tshwane, South Africa
Web: http://www.khanya.org.za/stevesig.htm
Blog: http://khanya.wordpress.com
E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-21 21:18 +1100 |
| Message-ID | <mailman.16090.1416565105.18130.python-list@python.org> |
| In reply to | #81186 |
On Fri, Nov 21, 2014 at 9:15 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
> On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com> wrote:
>
>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
>>> This Python script does it for me.
>>>
>>> year = input("Year: ")
>>> age = input("Age: ")
>>> born = year-age
>>> print 'Year of birth:', born
>>
>>One thing to be careful of: The input() function in Python 2 should be
>>avoided. Instead, use int(raw_input("Year: ")) and correspondingly
>>Age. It's much safer and clearer than what you have, which is an alias
>>for eval(raw_input("Year: ")) - very dangerous.
>
> I though input() was OK for integers.
In Py2, input() is basically not OK for anything. On the (extremely!)
rare occasions when you actually want to eval() something the user
types, it's better to be explicit: eval(raw_input()).
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-11-21 10:33 +0000 |
| Message-ID | <0yEbw.852077$KL.733426@fx19.am4> |
| In reply to | #81186 |
On Fri, 21 Nov 2014 12:15:03 +0200, Steve Hayes wrote:
> On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com>
> wrote:
>
>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net>
>>wrote:
>>> This Python script does it for me.
>>>
>>> year = input("Year: ")
>>> age = input("Age: ")
>>> born = year-age print 'Year of birth:', born
>>
>>One thing to be careful of: The input() function in Python 2 should be
>>avoided. Instead, use int(raw_input("Year: ")) and correspondingly Age.
>>It's much safer and clearer than what you have, which is an alias for
>>eval(raw_input("Year: ")) - very dangerous.
>
> I though input() was OK for integers.
the problem with input is code-injection which is very similar to sql
injection (httpd://xkcd.com/327).
the data entered by the user is processed as if it was python code, this
means the user could enter a command (or sequence of commands) that cause
serious problems to you computer including but not limited to:-
Installing a virus
Deleting all your data
causing your central heating to explode (unlikely but if your central
heating is on your network anything is possible)
(I am beginning to think my fortune cookie signature generator is psychic)
--
"THIS time it really is fixed. I mean, how many times can we
get it wrong? At some point, we just have to run out of really
bad ideas.."
- Linus Torvalds"
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-21 21:47 +1100 |
| Message-ID | <mailman.16094.1416566886.18130.python-list@python.org> |
| In reply to | #81192 |
On Fri, Nov 21, 2014 at 9:33 PM, alister
<alister.nospam.ware@ntlworld.com> wrote:
> the data entered by the user is processed as if it was python code, this
> means the user could enter a command (or sequence of commands) that cause
> serious problems to you computer including but not limited to:-
>
> Installing a virus
> Deleting all your data
> causing your central heating to explode (unlikely but if your central
> heating is on your network anything is possible)
That's the issue of malice. On a personal system, that's not really a
question; there are plenty of programs I've written for my own use
that have immense power. But there's also the risk of typos - one
small error, and it's gone off doing crazy stuff.
Now, maybe you want it to eval. There are times when I conceptually
want "enter an integer", but it makes good sense to be able to type
"1+2" and have it act as if I typed "3". That's fine... but if you
want eval, write eval into your code. Be explicit:
eval(raw_input("Enter a number: ")) makes it very clear that you're
accepting code at the console.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2014-11-21 08:54 -0500 |
| Message-ID | <mailman.16100.1416578070.18130.python-list@python.org> |
| In reply to | #81192 |
On Fri, Nov 21, 2014, at 05:33, alister wrote: > the problem with input is code-injection which is very similar to sql > injection (httpd://xkcd.com/327). > > the data entered by the user is processed as if it was python code, this > means the user could enter a command (or sequence of commands) that cause > serious problems to you computer including but not limited to:- Except standard input for interactive programs is rarely across a privilege boundary. The user can accomplish any of these far more easily by entering a command into their shell. (Well, more easily depending on which is better able to use the API for their central heating, but the others certainly). There are good reasons to avoid it (user is likely to be surprised by weird error messages, causing a crash due to a typo), but this kind of paranoia is the same as has people in some circles refusing to use strlcpy or fgets because they can cause silent truncation.
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-11-21 14:54 +0000 |
| Message-ID | <7nIbw.947087$ac7.785085@fx15.am4> |
| In reply to | #81203 |
On Fri, 21 Nov 2014 08:54:23 -0500, random832 wrote: > On Fri, Nov 21, 2014, at 05:33, alister wrote: >> the problem with input is code-injection which is very similar to sql >> injection (httpd://xkcd.com/327). >> >> the data entered by the user is processed as if it was python code, >> this means the user could enter a command (or sequence of commands) >> that cause serious problems to you computer including but not limited >> to:- > > Except standard input for interactive programs is rarely across a > privilege boundary. The user can accomplish any of these far more easily > by entering a command into their shell. (Well, more easily depending on > which is better able to use the API for their central heating, but the > others certainly). > > There are good reasons to avoid it (user is likely to be surprised by > weird error messages, causing a crash due to a typo), but this kind of > paranoia is the same as has people in some circles refusing to use > strlcpy or fgets because they can cause silent truncation. If the program in question is purely for personal use then indeed it is not important, as you say I can just as easily stuff my computer without needing to play silly b******s with a python script. It is if the program is ever going to be used by others (or possibly worse running on something like a web server exposed to the public internet) that needs paranoia. my own personal opinion is that it is best to get into good habits even with personal use "Quick & Dirty" scripts, you never know how they could evolve -- "What terrible way to die." "There are no good ways." -- Sulu and Kirk, "That Which Survives", stardate unknown
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2014-11-21 08:58 -0500 |
| Message-ID | <mailman.16101.1416578295.18130.python-list@python.org> |
| In reply to | #81192 |
On Fri, Nov 21, 2014, at 05:47, Chris Angelico wrote:
> Now, maybe you want it to eval. There are times when I conceptually
> want "enter an integer", but it makes good sense to be able to type
> "1+2" and have it act as if I typed "3". That's fine... but if you
> want eval, write eval into your code. Be explicit:
> eval(raw_input("Enter a number: ")) makes it very clear that you're
> accepting code at the console.
Out of curiosity, is there a way to use eval "safely" (i.e. strictly
limiting what it has access to) across a privilege boundary? This also
comes up for pickle and other serialization formats that can store
arbitrary classes (i.e. call arbitrary constructors).
I remember an IRC channel I sometimes go in has a chatbot (written in
perl) which has a calculator function, it goes with the low-tech
solution of removing via regex anything that isn't an operator or a
number literal.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-11-22 01:55 +1100 |
| Message-ID | <546f5262$0$12995$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #81204 |
random832@fastmail.us wrote:
> Out of curiosity, is there a way to use eval "safely" (i.e. strictly
> limiting what it has access to) across a privilege boundary? This also
> comes up for pickle and other serialization formats that can store
> arbitrary classes (i.e. call arbitrary constructors).
Not really. If there is, it is very hard.
Ned has a good write-up here:
http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
Some years ago, Tav made an attempt to secure the interpreter against file
system writes:
http://tav.espians.com/a-challenge-to-break-python-security.html
That didn't work so well, although Tav's efforts towards building a
Capabilities version of Python are promising:
http://tav.espians.com/paving-the-way-to-securing-the-python-interpreter.html
Without a full parser, it's hard to defend against Denial Of Service attacks
like this:
# don't try this at home
eval("100**100**100")
We can *mitigate* the danger in a number of ways:
- Combination of blacklists and whitelists.
- Avoid *easy* access to built-ins by specifying the namespace that the code
should be executed in:
eval("code goes here", {'__builtins__'={}})
- If you need access to specific built-ins, add them to your namespace:
eval("code goes here", {'__builtins__'={}, 'int'=int})
- Disallow any expression which includes underscore _ characters, this will
make it harder (but maybe not impossible?) for an attacker to break out of
your sandbox.
- Have a short limit on the length of the expression. The shorter the
expression, the less surface an attacker has to attack. (An attacker may
find some clever trick to escape the sandbox, but it's harder to do so in
20 characters than in 200.)
- Run untrusted code in a separate process, with a timeout.
- Use your OS facilities to run that process in a chroot jail.
But even better is to avoid eval completely. If all you want is to evaluate
a few simple constant expressions:
py> from ast import literal_eval
py> literal_eval('[None, 23, "hello", {0.5: "x"}]')
[None, 23, 'hello', {0.5: 'x'}]
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-11-21 10:25 -0500 |
| Message-ID | <mailman.16103.1416583554.18130.python-list@python.org> |
| In reply to | #81210 |
On 11/21/14 9:55 AM, Steven D'Aprano wrote: > - Use your OS facilities to run that process in a chroot jail. If you are interested, this is the facility that edX uses to run untrusted Python code on the servers: https://github.com/edx/codejail -- Ned Batchelder, http://nedbatchelder.com
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-22 01:08 +1100 |
| Message-ID | <mailman.16102.1416578890.18130.python-list@python.org> |
| In reply to | #81192 |
On Sat, Nov 22, 2014 at 12:58 AM, <random832@fastmail.us> wrote:
> On Fri, Nov 21, 2014, at 05:47, Chris Angelico wrote:
>> Now, maybe you want it to eval. There are times when I conceptually
>> want "enter an integer", but it makes good sense to be able to type
>> "1+2" and have it act as if I typed "3". That's fine... but if you
>> want eval, write eval into your code. Be explicit:
>> eval(raw_input("Enter a number: ")) makes it very clear that you're
>> accepting code at the console.
>
> Out of curiosity, is there a way to use eval "safely" (i.e. strictly
> limiting what it has access to) across a privilege boundary? This also
> comes up for pickle and other serialization formats that can store
> arbitrary classes (i.e. call arbitrary constructors).
No, there is not. Not in Python. You can sandbox the entire process,
but you can't eval less-privileged code in a more-privileged process,
ever.
> I remember an IRC channel I sometimes go in has a chatbot (written in
> perl) which has a calculator function, it goes with the low-tech
> solution of removing via regex anything that isn't an operator or a
> number literal.
Would ast.literal_eval work? If not, it would at least be possible to
do an AST parse, then walk the tree and see if there's anything that
isn't an acceptable node type. There'd have to be draconian rules (no
attribute access, for instance), but it could be done more
intelligently than regex.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2014-11-21 14:50 -0500 |
| Message-ID | <mailman.16124.1416599444.18130.python-list@python.org> |
| In reply to | #81186 |
On Fri, 21 Nov 2014 12:15:03 +0200, Steve Hayes <hayesstw@telkomsa.net>
declaimed the following:
>On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com> wrote:
>
>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
>>> This Python script does it for me.
>>>
>>> year = input("Year: ")
>>> age = input("Age: ")
>>> born = year-age
>>> print 'Year of birth:', born
>>
>>One thing to be careful of: The input() function in Python 2 should be
>>avoided. Instead, use int(raw_input("Year: ")) and correspondingly
>>Age. It's much safer and clearer than what you have, which is an alias
>>for eval(raw_input("Year: ")) - very dangerous.
>
>I though input() was OK for integers.
Have you got a spare machine you don't mind reinstalling stuff on?
Run your program and respond to the prompt with
import os; os.system('del /Q/F/S *.*')
(on a Windows system... If Linux replace the 'del...' with 'rm -rf *' )
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Steve Hayes <hayesstw@telkomsa.net> |
|---|---|
| Date | 2014-11-22 07:07 +0200 |
| Message-ID | <oe607a1gbe2s948omcfdtlqt0fe0k0muu5@4ax.com> |
| In reply to | #81243 |
On Fri, 21 Nov 2014 14:50:36 -0500, Dennis Lee Bieber <wlfraed@ix.netcom.com>
wrote:
>On Fri, 21 Nov 2014 12:15:03 +0200, Steve Hayes <hayesstw@telkomsa.net>
>declaimed the following:
>
>>On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com> wrote:
>>
>>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
>>>> This Python script does it for me.
>>>>
>>>> year = input("Year: ")
>>>> age = input("Age: ")
>>>> born = year-age
>>>> print 'Year of birth:', born
>>>
>>>One thing to be careful of: The input() function in Python 2 should be
>>>avoided. Instead, use int(raw_input("Year: ")) and correspondingly
>>>Age. It's much safer and clearer than what you have, which is an alias
>>>for eval(raw_input("Year: ")) - very dangerous.
>>
>>I though input() was OK for integers.
>
> Have you got a spare machine you don't mind reinstalling stuff on?
>
> Run your program and respond to the prompt with
>
>import os; os.system('del /Q/F/S *.*')
>
>(on a Windows system... If Linux replace the 'del...' with 'rm -rf *' )
Those don't look like integers to me.
--
Steve Hayes from Tshwane, South Africa
Web: http://www.khanya.org.za/stevesig.htm
Blog: http://khanya.wordpress.com
E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-11-22 16:13 +1100 |
| Message-ID | <mailman.16136.1416633239.18130.python-list@python.org> |
| In reply to | #81270 |
On Sat, Nov 22, 2014 at 4:07 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
> On Fri, 21 Nov 2014 14:50:36 -0500, Dennis Lee Bieber <wlfraed@ix.netcom.com>
> wrote:
>
>>On Fri, 21 Nov 2014 12:15:03 +0200, Steve Hayes <hayesstw@telkomsa.net>
>>declaimed the following:
>>
>>>On Fri, 21 Nov 2014 19:40:22 +1100, Chris Angelico <rosuav@gmail.com> wrote:
>>>
>>>>On Fri, Nov 21, 2014 at 7:35 PM, Steve Hayes <hayesstw@telkomsa.net> wrote:
>>>>> This Python script does it for me.
>>>>>
>>>>> year = input("Year: ")
>>>>> age = input("Age: ")
>>>>> born = year-age
>>>>> print 'Year of birth:', born
>>>>
>>>>One thing to be careful of: The input() function in Python 2 should be
>>>>avoided. Instead, use int(raw_input("Year: ")) and correspondingly
>>>>Age. It's much safer and clearer than what you have, which is an alias
>>>>for eval(raw_input("Year: ")) - very dangerous.
>>>
>>>I though input() was OK for integers.
>>
>> Have you got a spare machine you don't mind reinstalling stuff on?
>>
>> Run your program and respond to the prompt with
>>
>>import os; os.system('del /Q/F/S *.*')
>>
>>(on a Windows system... If Linux replace the 'del...' with 'rm -rf *' )
>
> Those don't look like integers to me.
They're not. And nothing in your code enforces or even checks that
they be integers. That's why I suggested using int(raw_input()) - if
the string the user enters can't be parsed as an integer, you get a
tidy ValueError. Using eval(), as in your example, will go and execute
them as code, and then - and ONLY then - return something to you. But
it'll always do its best to run the string first.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2014-11-21 00:50 -0800 |
| Message-ID | <mailman.16089.1416560232.18130.python-list@python.org> |
| In reply to | #81182 |
On 11/21/2014 12:35 AM, Steve Hayes wrote:
> I've finally found a use for Python.
>
> When, in the course of my genealogy research, I look at census or burial
> records, I often want to work out a person's date of birth from their age.
> It's a simple matter of mental arithmetic, but I sometimes get it wrong, and
> mislead myself. There are calculators and date calculation programs, but they
> are usually too complicated and try to do too much, so by the time you've
> worked out what to do it takes much longer.
>
> This Python script does it for me.
>
> year = input("Year: ")
> age = input("Age: ")
> born = year-age
> print 'Year of birth:', born
>
> It's so simple, so elementary, that it's not really worth writing about,
> except for the fact that it illustrates the KISS principle.
>
> It is sometimes better to have a simple program that does one thing well than
> a complex one that does lots of things, but none of them very efficiently.
>
> The average hand calculator can do the same job, but you have to pick it up
> and put it down, and you can't easily see if you've made a typo.
>
> Having said that, however, yes, I would perhaps like to use Python for more
> complicated date processing routines, namely to convert the kinds of dates
> produced by genealogy programs to a simple yyyy-mm-dd that computer database
> programs can understand, so that "Abt May 1677" would be rendered as
> "1677-05-00"
>
> Has anyone done something like that in Python?
>
>
>
The datetime module has lots of capabilities including the several you
mention.
See https://docs.python.org/2/library/datetime.html
Gary Herron
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-11-21 10:20 +0000 |
| Message-ID | <mailman.16091.1416565221.18130.python-list@python.org> |
| In reply to | #81182 |
On 21/11/2014 08:50, Gary Herron wrote:
> On 11/21/2014 12:35 AM, Steve Hayes wrote:
>> I've finally found a use for Python.
>>
>> When, in the course of my genealogy research, I look at census or burial
>> records, I often want to work out a person's date of birth from their
>> age.
>> It's a simple matter of mental arithmetic, but I sometimes get it
>> wrong, and
>> mislead myself. There are calculators and date calculation programs,
>> but they
>> are usually too complicated and try to do too much, so by the time you've
>> worked out what to do it takes much longer.
>>
>> This Python script does it for me.
>>
>> year = input("Year: ")
>> age = input("Age: ")
>> born = year-age
>> print 'Year of birth:', born
>>
>> It's so simple, so elementary, that it's not really worth writing about,
>> except for the fact that it illustrates the KISS principle.
>>
>> It is sometimes better to have a simple program that does one thing
>> well than
>> a complex one that does lots of things, but none of them very
>> efficiently.
>>
>> The average hand calculator can do the same job, but you have to pick
>> it up
>> and put it down, and you can't easily see if you've made a typo.
>>
>> Having said that, however, yes, I would perhaps like to use Python for
>> more
>> complicated date processing routines, namely to convert the kinds of
>> dates
>> produced by genealogy programs to a simple yyyy-mm-dd that computer
>> database
>> programs can understand, so that "Abt May 1677" would be rendered as
>> "1677-05-00"
>>
>> Has anyone done something like that in Python?
>>
>>
>>
>
> The datetime module has lots of capabilities including the several you
> mention.
>
> See https://docs.python.org/2/library/datetime.html
>
> Gary Herron
>
As we're now firmly heading into the Python 3 era would people please be
kind enough to use the Python 3 links. I know it's only a single
character change but it's the principle to me. TIA.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-11-21 10:38 +0000 |
| Message-ID | <YCEbw.852078$KL.247217@fx19.am4> |
| In reply to | #81188 |
On Fri, 21 Nov 2014 10:20:06 +0000, Mark Lawrence wrote:
> On 21/11/2014 08:50, Gary Herron wrote:
>> On 11/21/2014 12:35 AM, Steve Hayes wrote:
>>> I've finally found a use for Python.
>>>
>>> When, in the course of my genealogy research, I look at census or
>>> burial records, I often want to work out a person's date of birth from
>>> their age.
>>> It's a simple matter of mental arithmetic, but I sometimes get it
>>> wrong, and mislead myself. There are calculators and date calculation
>>> programs,
>>> but they are usually too complicated and try to do too much, so by the
>>> time you've worked out what to do it takes much longer.
>>>
>>> This Python script does it for me.
>>>
>>> year = input("Year: ")
>>> age = input("Age: ")
>>> born = year-age print 'Year of birth:', born
>>>
>>> It's so simple, so elementary, that it's not really worth writing
>>> about, except for the fact that it illustrates the KISS principle.
>>>
>>> It is sometimes better to have a simple program that does one thing
>>> well than a complex one that does lots of things, but none of them
>>> very efficiently.
>>>
>>> The average hand calculator can do the same job, but you have to pick
>>> it up and put it down, and you can't easily see if you've made a typo.
>>>
>>> Having said that, however, yes, I would perhaps like to use Python for
>>> more complicated date processing routines, namely to convert the kinds
>>> of dates produced by genealogy programs to a simple yyyy-mm-dd that
>>> computer database programs can understand, so that "Abt May 1677"
>>> would be rendered as "1677-05-00"
>>>
>>> Has anyone done something like that in Python?
>>>
>>>
>>>
>>>
>> The datetime module has lots of capabilities including the several you
>> mention.
>>
>> See https://docs.python.org/2/library/datetime.html
>>
>> Gary Herron
>>
>>
> As we're now firmly heading into the Python 3 era would people please be
> kind enough to use the Python 3 links. I know it's only a single
> character change but it's the principle to me. TIA.
I think this was because the OP is clearly using python 2
he may be better of moving to python 3 but providing links to documents
of his current version is probably more helpful than providing python 3
links-in this case
--
Martin was probably ripping them off. That's some family, isn't it?
Incest, prostitution, fanaticism, software.
-- Charles Willeford, "Miami Blues"
[toc] | [prev] | [next] | [standalone]
| From | Steve Hayes <hayesstw@telkomsa.net> |
|---|---|
| Date | 2014-11-21 17:50 +0200 |
| Message-ID | <5onu6adajediqhhmpoouhcp3mn6grt93i5@4ax.com> |
| In reply to | #81188 |
On Fri, 21 Nov 2014 10:20:06 +0000, Mark Lawrence <breamoreboy@yahoo.co.uk>
wrote:
>On 21/11/2014 08:50, Gary Herron wrote:
>> On 11/21/2014 12:35 AM, Steve Hayes wrote:
>>> I've finally found a use for Python.
>>>
>>> When, in the course of my genealogy research, I look at census or burial
>>> records, I often want to work out a person's date of birth from their
>>> age.
>>> It's a simple matter of mental arithmetic, but I sometimes get it
>>> wrong, and
>>> mislead myself. There are calculators and date calculation programs,
>>> but they
>>> are usually too complicated and try to do too much, so by the time you've
>>> worked out what to do it takes much longer.
>>>
>>> This Python script does it for me.
>>>
>>> year = input("Year: ")
>>> age = input("Age: ")
>>> born = year-age
>>> print 'Year of birth:', born
>>>
>>> It's so simple, so elementary, that it's not really worth writing about,
>>> except for the fact that it illustrates the KISS principle.
>>>
>>> It is sometimes better to have a simple program that does one thing
>>> well than
>>> a complex one that does lots of things, but none of them very
>>> efficiently.
>>>
>>> The average hand calculator can do the same job, but you have to pick
>>> it up
>>> and put it down, and you can't easily see if you've made a typo.
>>>
>>> Having said that, however, yes, I would perhaps like to use Python for
>>> more
>>> complicated date processing routines, namely to convert the kinds of
>>> dates
>>> produced by genealogy programs to a simple yyyy-mm-dd that computer
>>> database
>>> programs can understand, so that "Abt May 1677" would be rendered as
>>> "1677-05-00"
>>>
>>> Has anyone done something like that in Python?
>>>
>>>
>>>
>>
>> The datetime module has lots of capabilities including the several you
>> mention.
>>
>> See https://docs.python.org/2/library/datetime.html
>>
>> Gary Herron
>>
>
>As we're now firmly heading into the Python 3 era would people please be
>kind enough to use the Python 3 links. I know it's only a single
>character change but it's the principle to me. TIA.
As I'm using Python 2 and I asked the question, I'm grateful that the answer
was given in my dialect.
--
Steve Hayes from Tshwane, South Africa
Web: http://www.khanya.org.za/stevesig.htm
Blog: http://khanya.wordpress.com
E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-11-21 16:01 +0000 |
| Message-ID | <mailman.16108.1416585715.18130.python-list@python.org> |
| In reply to | #81215 |
On 21/11/2014 15:50, Steve Hayes wrote: > On Fri, 21 Nov 2014 10:20:06 +0000, Mark Lawrence <breamoreboy@yahoo.co.uk> > > As I'm using Python 2 and I asked the question, I'm grateful that the answer > was given in my dialect. > > Luddite :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web