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


Groups > comp.lang.python > #42402 > unrolled thread

Help with python code!

Started byjojo <gerrymcgovern@gmail.com>
First post2013-03-31 13:10 -0700
Last post2013-03-31 17:08 -0400
Articles 20 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Help with python code! jojo <gerrymcgovern@gmail.com> - 2013-03-31 13:10 -0700
    Re: Help with python code! Chris Angelico <rosuav@gmail.com> - 2013-04-01 07:39 +1100
      Re: Help with python code! jojo <gerrymcgovern@gmail.com> - 2013-03-31 14:06 -0700
        Re: Help with python code! Roy Smith <roy@panix.com> - 2013-03-31 17:13 -0400
          Re: Help with python code! jojo <gerrymcgovern@gmail.com> - 2013-03-31 14:21 -0700
            Re: Help with python code! Roy Smith <roy@panix.com> - 2013-03-31 17:27 -0400
              Re: Help with python code! gerrymcgovern@gmail.com - 2013-03-31 14:32 -0700
                Re: Help with python code! Alister <alister.ware@ntlworld.com> - 2013-04-01 17:54 +0000
              Re: Help with python code! rurpy@yahoo.com - 2013-03-31 14:46 -0700
            Re: Help with python code! Chris Angelico <rosuav@gmail.com> - 2013-04-01 08:35 +1100
              Re: Help with python code! gerrymcgovern@gmail.com - 2013-03-31 14:41 -0700
              Re: Help with python code! gerrymcgovern@gmail.com - 2013-03-31 14:41 -0700
            Re: Help with python code! Jason Friedman <jsf80238@gmail.com> - 2013-04-02 21:59 -0600
        Re: Help with python code! Chris Angelico <rosuav@gmail.com> - 2013-04-01 08:21 +1100
          Re: Help with python code! gerrymcgovern@gmail.com - 2013-03-31 14:24 -0700
          Re: Help with python code! gerrymcgovern@gmail.com - 2013-03-31 14:24 -0700
        Re: Help with python code! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-31 23:02 +0100
        Re: Help with python code! Chris Angelico <rosuav@gmail.com> - 2013-04-01 09:04 +1100
      Re: Help with python code! jojo <gerrymcgovern@gmail.com> - 2013-03-31 14:06 -0700
    Re: Help with python code! Roy Smith <roy@panix.com> - 2013-03-31 17:08 -0400

#42402 — Help with python code!

Fromjojo <gerrymcgovern@gmail.com>
Date2013-03-31 13:10 -0700
SubjectHelp with python code!
Message-ID<37f23623-8bf5-421a-ab6a-34ff622c69f1@googlegroups.com>
Hi - I am a newbie to python and was wondering can someone tell me what the following code does. I need to figure out how to test it

import time
import glob
import re
import os

current_time = time.time() + 60*60+24*30

dirList = glob.glob('\content\paytek\ejbProperties\cybersource\*.crt')

q = re.compile('^Owner:.*CN=([^\s\,]+)')
p = re.compile('until: (\w+) (\w+) (\d+) (\d+):(\d+):(\d+) \w+ (\d+)')
cert_name = ""
days = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
months = {"Jan":1, "Feb":2, "Mar":3, "Apr":4,
"May":5, "Jun":6, "Jul":7, "Aug":8,
"Sep":9, "Oct":10, "Nov":11, "Dec":12}

for fname in dirList:
cmd = "keytool ­printcert ­file " + fname
for line in os.popen(cmd).readlines():
line = line.rstrip()
m = p.search(line)
if m:
sue = time.mktime(
(int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
int(m.group(4)), int(m.group(5)), int(m.group(6)),
int(days[m.group(1)]), 0, 0)
)
expire_time = (sue ­ current_time)/60/60/24
if expire_time < 0:
print cert_name + " has already expired!"
elif expire_time < 31:
print cert_name + " expires in " +str(int(expire_time)) + " days"
else:
m = q.search(line)
if m:
cert_name = m.group(1)


Im used to C# so the syntax looks bizarre to me! Any help would be great.

[toc] | [next] | [standalone]


#42406

FromChris Angelico <rosuav@gmail.com>
Date2013-04-01 07:39 +1100
Message-ID<mailman.4027.1364762359.2939.python-list@python.org>
In reply to#42402
On Mon, Apr 1, 2013 at 7:10 AM, jojo <gerrymcgovern@gmail.com> wrote:
> Im used to C# so the syntax looks bizarre to me! Any help would be great.

The first thing you'll need to understand about Python syntax is that
indentation is important. By posting this code flush-left, you've
actually destroyed its block structure. Could you post it again, with
indentation, please? We'd then be in a much better position to help.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#42407

Fromjojo <gerrymcgovern@gmail.com>
Date2013-03-31 14:06 -0700
Message-ID<2912c674-e30b-4339-9344-1f460cb96b23@googlegroups.com>
In reply to#42406
On Sunday, March 31, 2013 4:39:11 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 7:10 AM, jojo wrote:
> 
> > Im used to C# so the syntax looks bizarre to me! Any help would be great.
> 
> 
> 
> The first thing you'll need to understand about Python syntax is that
> 
> indentation is important. By posting this code flush-left, you've
> 
> actually destroyed its block structure. Could you post it again, with
> 
> indentation, please? We'd then be in a much better position to help.
> 
> 
> 
> Chris Angelico


Hi Chris, thanks for your reply. See code below...

import time
import glob
import re
import os
current_time = time.time() + 60*60+24*30
dirList = glob.glob('\content\paytek\ejbProperties\cybersource\*.crt')
q = re.compile('^Owner:.*CN=([^\s\,]+)')
p = re.compile('until: (\w+) (\w+) (\d+) (\d+):(\d+):(\d+) \w+ (\d+)')
cert_name = ""
days = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
months = {"Jan":1, "Feb":2, "Mar":3, "Apr":4,
  "May":5, "Jun":6, "Jul":7, "Aug":8,
  "Sep":9, "Oct":10, "Nov":11, "Dec":12}
for fname in dirList:
 cmd = "keytool ­printcert ­file " + fname
 for line in os.popen(cmd).readlines():
   line = line.rstrip()
   m = p.search(line)
   if m:
      sue = time.mktime(
        (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
          int(m.group(4)), int(m.group(5)), int(m.group(6)),
          int(days[m.group(1)]), 0, 0)
        )
        expire_time = (sue ­ current_time)/60/60/24
        if expire_time < 0:
          print cert_name + " has already expired!"
        elif expire_time < 31:
          print cert_name + " expires in " +str(int(expire_time)) + " days"
       else:
        m = q.search(line)
        if m:
        cert_name = m.group(1)

[toc] | [prev] | [next] | [standalone]


#42410

FromRoy Smith <roy@panix.com>
Date2013-03-31 17:13 -0400
Message-ID<roy-AA2630.17134931032013@news.panix.com>
In reply to#42407
In article <2912c674-e30b-4339-9344-1f460cb96b23@googlegroups.com>,
 jojo <gerrymcgovern@gmail.com> wrote:

> for fname in dirList:
>  cmd = "keytool ­printcert ­file " + fname
>  for line in os.popen(cmd).readlines():
>    line = line.rstrip()
>    m = p.search(line)
>    if m:
>       sue = time.mktime(
>         (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
>           int(m.group(4)), int(m.group(5)), int(m.group(6)),
>           int(days[m.group(1)]), 0, 0)
>         )
>         expire_time = (sue ­ current_time)/60/60/24
>         if expire_time < 0:
>           print cert_name + " has already expired!"
>         elif expire_time < 31:
>           print cert_name + " expires in " +str(int(expire_time)) + " days"
>        else:
>         m = q.search(line)
>         if m:
>         cert_name = m.group(1)

Was this code really indented like this when you got it?  You've got (at 
least) three different indent sizes.  I see 1, 2, and 3 space indents in 
different places in the code.

I'm not even sure if this is legal, but even if it is, it's really bad 
form.  Pick an indent, and stick with it uniformly.  4 spaces seems to 
be pretty standard.

That being said, I'm going to return to my previous statement that until 
you know what the code is *supposed* to do, trying to test it is 
meaningless.

[toc] | [prev] | [next] | [standalone]


#42412

Fromjojo <gerrymcgovern@gmail.com>
Date2013-03-31 14:21 -0700
Message-ID<4455829d-5b4a-44ee-b65f-5f72d429ba9c@googlegroups.com>
In reply to#42410
On Sunday, March 31, 2013 5:13:49 PM UTC-4, Roy Smith wrote:
> In article <2912c674-e30b-4339-9344-1f460cb96b23@googlegroups.com>,
> 
>  jojo  wrote:
> 
> 
> 
> > for fname in dirList:
> 
> >  cmd = "keytool �printcert �file " + fname
> 
> >  for line in os.popen(cmd).readlines():
> 
> >    line = line.rstrip()
> 
> >    m = p.search(line)
> 
> >    if m:
> 
> >       sue = time.mktime(
> 
> >         (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
> 
> >           int(m.group(4)), int(m.group(5)), int(m.group(6)),
> 
> >           int(days[m.group(1)]), 0, 0)
> 
> >         )
> 
> >         expire_time = (sue � current_time)/60/60/24
> 
> >         if expire_time < 0:
> 
> >           print cert_name + " has already expired!"
> 
> >         elif expire_time < 31:
> 
> >           print cert_name + " expires in " +str(int(expire_time)) + " days"
> 
> >        else:
> 
> >         m = q.search(line)
> 
> >         if m:
> 
> >         cert_name = m.group(1)
> 
> 
> 
> Was this code really indented like this when you got it?  You've got (at 
> 
> least) three different indent sizes.  I see 1, 2, and 3 space indents in 
> 
> different places in the code.
> 
> 
> 
> I'm not even sure if this is legal, but even if it is, it's really bad 
> 
> form.  Pick an indent, and stick with it uniformly.  4 spaces seems to 
> 
> be pretty standard.
> 
> 
> 
> That being said, I'm going to return to my previous statement that until 
> 
> you know what the code is *supposed* to do, trying to test it is 
> 
> meaningless.


Hi Rob.

Thanks for your replies. Just to be clear this is for a interview and they would like me to figure out what the code does and come back with some test cases. I don't need to code the tests, just give some high level tests. As far as I can make out it is some system where you input your name and it will bring back your details plus how much time you have left on your card. Have to say I find the code extremely confusing, hopefully all python isn't like this!!

[toc] | [prev] | [next] | [standalone]


#42415

FromRoy Smith <roy@panix.com>
Date2013-03-31 17:27 -0400
Message-ID<roy-309B56.17270631032013@news.panix.com>
In reply to#42412
In article <4455829d-5b4a-44ee-b65f-5f72d429ba9c@googlegroups.com>,
 jojo <gerrymcgovern@gmail.com> wrote:

> Thanks for your replies. Just to be clear this is for a interview and they 
> would like me to figure out what the code does and come back with some test 
> cases. I don't need to code the tests, just give some high level tests. As 
> far as I can make out it is some system where you input your name and it will 
> bring back your details plus how much time you have left on your card. Have 
> to say I find the code extremely confusing, hopefully all python isn't like 
> this!!

If this is for an interview, you really should be doing this on your 
own.  I assume the point of the interview is to see how well you know 
Python.  Please don't expect people here to take your interview for you.

[toc] | [prev] | [next] | [standalone]


#42416

Fromgerrymcgovern@gmail.com
Date2013-03-31 14:32 -0700
Message-ID<40463d1c-013e-46c2-823a-787c1cca450f@googlegroups.com>
In reply to#42415
On Sunday, March 31, 2013 5:27:06 PM UTC-4, Roy Smith wrote:
> In article <4455829d-5b4a-44ee-b65f-5f72d429ba9c@googlegroups.com>,
> 
>  jojo  wrote:
> 
> 
> 
> > Thanks for your replies. Just to be clear this is for a interview and they 
> 
> > would like me to figure out what the code does and come back with some test 
> 
> > cases. I don't need to code the tests, just give some high level tests. As 
> 
> > far as I can make out it is some system where you input your name and it will 
> 
> > bring back your details plus how much time you have left on your card. Have 
> 
> > to say I find the code extremely confusing, hopefully all python isn't like 
> 
> > this!!
> 
> 
> 
> If this is for an interview, you really should be doing this on your 
> 
> own.  I assume the point of the interview is to see how well you know 
> 
> Python.  Please don't expect people here to take your interview for you.

Where did I ask people to take the interview for me? I asked for some tips on interpreting the code something which you have been unable to give me. If a senior dev (I am assuming you are a python dev) is unable to figure out what the code does, then I think me, been a newbie to new language (with horrible syntax) is entitled to ask for help. 

[toc] | [prev] | [next] | [standalone]


#42476

FromAlister <alister.ware@ntlworld.com>
Date2013-04-01 17:54 +0000
Message-ID<GRj6t.266868$fB.160901@fx27.fr7>
In reply to#42416
On Sun, 31 Mar 2013 14:32:21 -0700, gerrymcgovern wrote:

> On Sunday, March 31, 2013 5:27:06 PM UTC-4, Roy Smith wrote:
>> In article <4455829d-5b4a-44ee-b65f-5f72d429ba9c@googlegroups.com>,
>> 
>>  jojo  wrote:
>> 
>> 
>> 
>> > Thanks for your replies. Just to be clear this is for a interview and
>> > they
>> 
>> > would like me to figure out what the code does and come back with
>> > some test
>> 
>> > cases. I don't need to code the tests, just give some high level
>> > tests. As
>> 
>> > far as I can make out it is some system where you input your name and
>> > it will
>> 
>> > bring back your details plus how much time you have left on your
>> > card. Have
>> 
>> > to say I find the code extremely confusing, hopefully all python
>> > isn't like
>> 
>> > this!!
>> 
>> 
>> 
>> If this is for an interview, you really should be doing this on your
>> 
>> own.  I assume the point of the interview is to see how well you know
>> 
>> Python.  Please don't expect people here to take your interview for
>> you.
> 
> Where did I ask people to take the interview for me? I asked for some
> tips on interpreting the code something which you have been unable to
> give me. If a senior dev (I am assuming you are a python dev) is unable
> to figure out what the code does, then I think me, been a newbie to new
> language (with horrible syntax) is entitled to ask for help.

I think Roy's point here is if you don't know python (as you admit) why 
are you applying for a job as a Python Developer.

the code provided appears to be pretty poor quality to me & probably 
buggy as well but without the specification I can't be sure.




-- 
I like work; it fascinates me; I can sit and look at it for hours.

[toc] | [prev] | [next] | [standalone]


#42420

Fromrurpy@yahoo.com
Date2013-03-31 14:46 -0700
Message-ID<95056399-bafd-43c4-a261-c41f6943f5c5@googlegroups.com>
In reply to#42415
On Sunday, March 31, 2013 3:27:06 PM UTC-6, Roy Smith wrote:

> If this is for an interview, you really should be doing this on your 
> own.  I assume the point of the interview is to see how well you know 
> Python.  Please don't expect people here to take your interview for you.

Maybe the interviewer gives higher ratings to someone who knows 
how to take advantage of all available resources than someone who
goes off in a corner and tries to do it alone?

[toc] | [prev] | [next] | [standalone]


#42417

FromChris Angelico <rosuav@gmail.com>
Date2013-04-01 08:35 +1100
Message-ID<mailman.4031.1364765746.2939.python-list@python.org>
In reply to#42412
On Mon, Apr 1, 2013 at 8:21 AM, jojo <gerrymcgovern@gmail.com> wrote:
> Thanks for your replies. Just to be clear this is for a interview and they would like me to figure out what the code does and come back with some test cases

That explains the utter lack of comments, then. In well-maintained
code, you would simply read through the comments to get an idea of
what it does.

A couple of key things to look up: glob.glob and os.popen. When you
know what they do, you should be able to get a broad understanding of
the whole program.

ChrisA

[toc] | [prev] | [next] | [standalone]


#42419

Fromgerrymcgovern@gmail.com
Date2013-03-31 14:41 -0700
Message-ID<6285f193-5e3d-4696-a164-12bea6da3903@googlegroups.com>
In reply to#42417
On Sunday, March 31, 2013 5:35:38 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 8:21 AM, jojo  wrote:
> 
> > Thanks for your replies. Just to be clear this is for a interview and they would like me to figure out what the code does and come back with some test cases
> 
> 
> 
> That explains the utter lack of comments, then. In well-maintained
> 
> code, you would simply read through the comments to get an idea of
> 
> what it does.
> 
> 
> 
> A couple of key things to look up: glob.glob and os.popen. When you
> 
> know what they do, you should be able to get a broad understanding of
> 
> the whole program.
> 
> 
> 
> ChrisA

OK perfect Chris. Thanks for your reply.

[toc] | [prev] | [next] | [standalone]


#42421

Fromgerrymcgovern@gmail.com
Date2013-03-31 14:41 -0700
Message-ID<mailman.4033.1364766631.2939.python-list@python.org>
In reply to#42417
On Sunday, March 31, 2013 5:35:38 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 8:21 AM, jojo  wrote:
> 
> > Thanks for your replies. Just to be clear this is for a interview and they would like me to figure out what the code does and come back with some test cases
> 
> 
> 
> That explains the utter lack of comments, then. In well-maintained
> 
> code, you would simply read through the comments to get an idea of
> 
> what it does.
> 
> 
> 
> A couple of key things to look up: glob.glob and os.popen. When you
> 
> know what they do, you should be able to get a broad understanding of
> 
> the whole program.
> 
> 
> 
> ChrisA

OK perfect Chris. Thanks for your reply.

[toc] | [prev] | [next] | [standalone]


#42623

FromJason Friedman <jsf80238@gmail.com>
Date2013-04-02 21:59 -0600
Message-ID<mailman.33.1364961569.3114.python-list@python.org>
In reply to#42412

[Multipart message — attachments visible in raw view] — view raw

> Thanks for your replies. Just to be clear this is for a interview and they
> would like me to figure out what the code does and come back with some test
> cases. I don't need to code the tests, just give some high level tests. As
> far as I can make out it is some system where you input your name and it
> will bring back your details plus how much time you have left on your card.
> Have to say I find the code extremely confusing, hopefully all python isn't
> like this!!
>

This question causes me to recall this story.  I was interviewing college
student candidates for a Summer internship. The interview consisted of a
30-minute phone conversation followed by a 60-minute programming exercise
(performed offsite).  One candidate listed only VB and HTML as programming
languages, but the candidate was local and thought I'd give the person a
chance and some interviewing practice.  Only two of nine candidates "aced"
the programming exercise, and this candidate was one of them!  The
candidates could choose whatever programming language they liked, this
candidate chose C#.  I wrote back to acknowledge receiving the answers and
asked why C# was not listed on the resume.  The candidate replied that
three friends helped compose the answer!

I should have asked whether the candidate would, if offered the position,
be able to bring those three friends to work every day.

[toc] | [prev] | [next] | [standalone]


#42411

FromChris Angelico <rosuav@gmail.com>
Date2013-04-01 08:21 +1100
Message-ID<mailman.4029.1364764868.2939.python-list@python.org>
In reply to#42407
On Mon, Apr 1, 2013 at 8:06 AM, jojo <gerrymcgovern@gmail.com> wrote:
> On Sunday, March 31, 2013 4:39:11 PM UTC-4, Chris Angelico wrote:
>> On Mon, Apr 1, 2013 at 7:10 AM, jojo wrote:
>>
>> > Im used to C# so the syntax looks bizarre to me! Any help would be great.
>>
>>
>>
>> The first thing you'll need to understand about Python syntax is that
>>
>> indentation is important. By posting this code flush-left, you've
>>
>> actually destroyed its block structure. Could you post it again, with
>>
>> indentation, please? We'd then be in a much better position to help.
>>
>>
>>
>> Chris Angelico
>
>
> Hi Chris, thanks for your reply. See code below...

Ah, you appear to be posting from Google Groups. You may want to check
this page out, as a lot of people rather dislike GG posts.

http://wiki.python.org/moin/GoogleGroupsPython

The best method is simply to avoid Google Groups altogether.

Anyway, some code comments. (Though the biggest comment to make about
the code is its utter lack of comments. Not a good idea in any
language, for anything more than the most trivial script.)

> current_time = time.time() + 60*60+24*30

This line doesn't, quite frankly, make a lot of sense; time.time()
returns the current time already, but then an offset of one hour and
twelve minutes is added.

>    if m:
>       sue = time.mktime(
>         (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
>           int(m.group(4)), int(m.group(5)), int(m.group(6)),
>           int(days[m.group(1)]), 0, 0)
>         )
>         expire_time = (sue ­ current_time)/60/60/24

Here's a likely problem. There's supposed to be an operator - probably
a plus sign - between sue and current_time.

>        else:
>         m = q.search(line)
>         if m:
>         cert_name = m.group(1)

And this last line needs indentation.

The very easiest way to debug Python code is to run it. If it runs,
great! See what output it made and whether it's correct or not. If it
doesn't, Python will give you an exception traceback that points you
to the failing line. Get familiar with them, as you'll be seeing them
a lot :)

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#42413

Fromgerrymcgovern@gmail.com
Date2013-03-31 14:24 -0700
Message-ID<6a2b4d3e-b218-4e09-9ee1-c3aed1f454d3@googlegroups.com>
In reply to#42411
On Sunday, March 31, 2013 5:21:00 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 8:06 AM, jojo  wrote:
> 
> > On Sunday, March 31, 2013 4:39:11 PM UTC-4, Chris Angelico wrote:
> 
> >> On Mon, Apr 1, 2013 at 7:10 AM, jojo wrote:
> 
> >>
> 
> >> > Im used to C# so the syntax looks bizarre to me! Any help would be great.
> 
> >>
> 
> >>
> 
> >>
> 
> >> The first thing you'll need to understand about Python syntax is that
> 
> >>
> 
> >> indentation is important. By posting this code flush-left, you've
> 
> >>
> 
> >> actually destroyed its block structure. Could you post it again, with
> 
> >>
> 
> >> indentation, please? We'd then be in a much better position to help.
> 
> >>
> 
> >>
> 
> >>
> 
> >> Chris Angelico
> 
> >
> 
> >
> 
> > Hi Chris, thanks for your reply. See code below...
> 
> 
> 
> Ah, you appear to be posting from Google Groups. You may want to check
> 
> this page out, as a lot of people rather dislike GG posts.
> 
> 
> 
> http://wiki.python.org/moin/GoogleGroupsPython
> 
> 
> 
> The best method is simply to avoid Google Groups altogether.
> 
> 
> 
> Anyway, some code comments. (Though the biggest comment to make about
> 
> the code is its utter lack of comments. Not a good idea in any
> 
> language, for anything more than the most trivial script.)
> 
> 
> 
> > current_time = time.time() + 60*60+24*30
> 
> 
> 
> This line doesn't, quite frankly, make a lot of sense; time.time()
> 
> returns the current time already, but then an offset of one hour and
> 
> twelve minutes is added.
> 
> 
> 
> >    if m:
> 
> >       sue = time.mktime(
> 
> >         (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
> 
> >           int(m.group(4)), int(m.group(5)), int(m.group(6)),
> 
> >           int(days[m.group(1)]), 0, 0)
> 
> >         )
> 
> >         expire_time = (sue ­ current_time)/60/60/24
> 
> 
> 
> Here's a likely problem. There's supposed to be an operator - probably
> 
> a plus sign - between sue and current_time.
> 
> 
> 
> >        else:
> 
> >         m = q.search(line)
> 
> >         if m:
> 
> >         cert_name = m.group(1)
> 
> 
> 
> And this last line needs indentation.
> 
> 
> 
> The very easiest way to debug Python code is to run it. If it runs,
> 
> great! See what output it made and whether it's correct or not. If it
> 
> doesn't, Python will give you an exception traceback that points you
> 
> to the failing line. Get familiar with them, as you'll be seeing them
> 
> a lot :)
> 
> 
> 
> Chris Angelico


Ok, thanks Chris!!

[toc] | [prev] | [next] | [standalone]


#42414

Fromgerrymcgovern@gmail.com
Date2013-03-31 14:24 -0700
Message-ID<mailman.4030.1364765084.2939.python-list@python.org>
In reply to#42411
On Sunday, March 31, 2013 5:21:00 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 8:06 AM, jojo  wrote:
> 
> > On Sunday, March 31, 2013 4:39:11 PM UTC-4, Chris Angelico wrote:
> 
> >> On Mon, Apr 1, 2013 at 7:10 AM, jojo wrote:
> 
> >>
> 
> >> > Im used to C# so the syntax looks bizarre to me! Any help would be great.
> 
> >>
> 
> >>
> 
> >>
> 
> >> The first thing you'll need to understand about Python syntax is that
> 
> >>
> 
> >> indentation is important. By posting this code flush-left, you've
> 
> >>
> 
> >> actually destroyed its block structure. Could you post it again, with
> 
> >>
> 
> >> indentation, please? We'd then be in a much better position to help.
> 
> >>
> 
> >>
> 
> >>
> 
> >> Chris Angelico
> 
> >
> 
> >
> 
> > Hi Chris, thanks for your reply. See code below...
> 
> 
> 
> Ah, you appear to be posting from Google Groups. You may want to check
> 
> this page out, as a lot of people rather dislike GG posts.
> 
> 
> 
> http://wiki.python.org/moin/GoogleGroupsPython
> 
> 
> 
> The best method is simply to avoid Google Groups altogether.
> 
> 
> 
> Anyway, some code comments. (Though the biggest comment to make about
> 
> the code is its utter lack of comments. Not a good idea in any
> 
> language, for anything more than the most trivial script.)
> 
> 
> 
> > current_time = time.time() + 60*60+24*30
> 
> 
> 
> This line doesn't, quite frankly, make a lot of sense; time.time()
> 
> returns the current time already, but then an offset of one hour and
> 
> twelve minutes is added.
> 
> 
> 
> >    if m:
> 
> >       sue = time.mktime(
> 
> >         (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
> 
> >           int(m.group(4)), int(m.group(5)), int(m.group(6)),
> 
> >           int(days[m.group(1)]), 0, 0)
> 
> >         )
> 
> >         expire_time = (sue ­ current_time)/60/60/24
> 
> 
> 
> Here's a likely problem. There's supposed to be an operator - probably
> 
> a plus sign - between sue and current_time.
> 
> 
> 
> >        else:
> 
> >         m = q.search(line)
> 
> >         if m:
> 
> >         cert_name = m.group(1)
> 
> 
> 
> And this last line needs indentation.
> 
> 
> 
> The very easiest way to debug Python code is to run it. If it runs,
> 
> great! See what output it made and whether it's correct or not. If it
> 
> doesn't, Python will give you an exception traceback that points you
> 
> to the failing line. Get familiar with them, as you'll be seeing them
> 
> a lot :)
> 
> 
> 
> Chris Angelico


Ok, thanks Chris!!

[toc] | [prev] | [next] | [standalone]


#42422

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-03-31 23:02 +0100
Message-ID<mailman.4034.1364767305.2939.python-list@python.org>
In reply to#42407
On 31/03/2013 22:21, Chris Angelico wrote:
>>        sue = time.mktime(
>>          (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
>>            int(m.group(4)), int(m.group(5)), int(m.group(6)),
>>            int(days[m.group(1)]), 0, 0)
>>          )
>>          expire_time = (sue ­ current_time)/60/60/24
>
> Here's a likely problem. There's supposed to be an operator - probably
> a plus sign - between sue and current_time.
>

There is actually a minus sign there which showed up when I pasted the 
code into the Eclipse/Pydev editor.  That sadly doesn't fix the problem 
with the call to mktime, 12 open round brackets to 14 close if I've 
counted correctly.

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#42425

FromChris Angelico <rosuav@gmail.com>
Date2013-04-01 09:04 +1100
Message-ID<mailman.4035.1364767957.2939.python-list@python.org>
In reply to#42407
On Mon, Apr 1, 2013 at 9:02 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 31/03/2013 22:21, Chris Angelico wrote:
>>>
>>>        sue = time.mktime(
>>>          (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
>>>            int(m.group(4)), int(m.group(5)), int(m.group(6)),
>>>            int(days[m.group(1)]), 0, 0)
>>>          )
>>>          expire_time = (sue ­ current_time)/60/60/24
>>
>>
>> Here's a likely problem. There's supposed to be an operator - probably
>> a plus sign - between sue and current_time.
>>
>
> There is actually a minus sign there which showed up when I pasted the code
> into the Eclipse/Pydev editor.  That sadly doesn't fix the problem with the
> call to mktime, 12 open round brackets to 14 close if I've counted
> correctly.

Oh, of course, since sue comes from mktime. But yeah, definitely needs
an operator there.

ChrisA

[toc] | [prev] | [next] | [standalone]


#42408

Fromjojo <gerrymcgovern@gmail.com>
Date2013-03-31 14:06 -0700
Message-ID<mailman.4028.1364763983.2939.python-list@python.org>
In reply to#42406
On Sunday, March 31, 2013 4:39:11 PM UTC-4, Chris Angelico wrote:
> On Mon, Apr 1, 2013 at 7:10 AM, jojo wrote:
> 
> > Im used to C# so the syntax looks bizarre to me! Any help would be great.
> 
> 
> 
> The first thing you'll need to understand about Python syntax is that
> 
> indentation is important. By posting this code flush-left, you've
> 
> actually destroyed its block structure. Could you post it again, with
> 
> indentation, please? We'd then be in a much better position to help.
> 
> 
> 
> Chris Angelico


Hi Chris, thanks for your reply. See code below...

import time
import glob
import re
import os
current_time = time.time() + 60*60+24*30
dirList = glob.glob('\content\paytek\ejbProperties\cybersource\*.crt')
q = re.compile('^Owner:.*CN=([^\s\,]+)')
p = re.compile('until: (\w+) (\w+) (\d+) (\d+):(\d+):(\d+) \w+ (\d+)')
cert_name = ""
days = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
months = {"Jan":1, "Feb":2, "Mar":3, "Apr":4,
  "May":5, "Jun":6, "Jul":7, "Aug":8,
  "Sep":9, "Oct":10, "Nov":11, "Dec":12}
for fname in dirList:
 cmd = "keytool ­printcert ­file " + fname
 for line in os.popen(cmd).readlines():
   line = line.rstrip()
   m = p.search(line)
   if m:
      sue = time.mktime(
        (int(m.group(7)), int(months[m.group(2)]), int(m.group(3)),
          int(m.group(4)), int(m.group(5)), int(m.group(6)),
          int(days[m.group(1)]), 0, 0)
        )
        expire_time = (sue ­ current_time)/60/60/24
        if expire_time < 0:
          print cert_name + " has already expired!"
        elif expire_time < 31:
          print cert_name + " expires in " +str(int(expire_time)) + " days"
       else:
        m = q.search(line)
        if m:
        cert_name = m.group(1)

[toc] | [prev] | [next] | [standalone]


#42409

FromRoy Smith <roy@panix.com>
Date2013-03-31 17:08 -0400
Message-ID<roy-72EC60.17080931032013@news.panix.com>
In reply to#42402
In article <37f23623-8bf5-421a-ab6a-34ff622c69f1@googlegroups.com>,
 jojo <gerrymcgovern@gmail.com> wrote:

> Hi - I am a newbie to python and was wondering can someone tell me what the 
> following code does. I need to figure out how to test it

I know this is going to sound unhelpful, but if your task is to test the 
code, what good does it do to know what it does?  Clearly, it does what 
it does.  What you really want to know is "What is it *supposed* to do?"  
Until you know what it's supposed to do, it is meaningless to even think 
about testing it.

Seriously.  I'm not trying to make life difficult for you.  I've seen 
too much time and effort wasted on useless testing because there wasn't 
any specification for what the code was supposed to do.

I will give you one hint, however.  You've got stuff like:

for line in os.popen(cmd).readlines():
line = line.rstrip()

This is a syntax error because the indenting is wrong.  Unlike C#, 
indenting is significant in Python.  I don't know if the code you've got 
really is indented wrong, or it's just an artifact of how you posted it.  
But before anything else, you need to sort that out.

Unless, of course, the specification for this code is, "It is supposed 
to raise IndentationError", in which case it passes the test :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web