Help with python code!

J

jojo

Hi - I am a newbie to python and was wondering can someone tell me what thefollowing 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.
 
C

Chris Angelico

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
 
J

jojo

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)
 
J

jojo

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)
 
R

Roy Smith

jojo said:
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 :)
 
R

Roy Smith

jojo said:
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.
 
C

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
 
J

jojo

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)
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"

m = q.search(line)
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 testcases. I don't need to code the tests, just give some high level tests. Asfar 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'tlike this!!
 
G

gerrymcgovern

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.


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.


m = q.search(line)
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!!
 
G

gerrymcgovern

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.


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.


m = q.search(line)
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!!
 
R

Roy Smith

jojo said:
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.
 
G

gerrymcgovern

jojo 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.

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 outwhat the code does, then I think me, been a newbie to new language (with horrible syntax) is entitled to ask for help.
 
C

Chris Angelico

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
 
G

gerrymcgovern

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.
 
G

gerrymcgovern

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.
 
R

rurpy

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?
 
M

Mark Lawrence

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.
 
C

Chris Angelico

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
 
J

Jason Friedman

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top