Re: [Tutor] Question about subprocess

2014-01-10 Thread Steven D'Aprano
On Sat, Jan 11, 2014 at 12:48:13PM +0800, daedae11 wrote:
> p = subprocess.Popen(['E:/EntTools/360EntSignHelper.exe', 
> 'E:/build/temp/RemoteAssistSetup.exe'],
>  stdout=subprocess.PIPE, shell=True).stdout
> temp = p.readline();
> print temp
> p.close()
> 
> When I run the above code in command line, it works formally.

What do you mean, "the above code"? Do you mean the *Python* code? Or do 
you mean calling the 360EntSignHelper.exe application from the command 
line? I'm going to assume you mean calling the .exe.


> However, when I writed it into a .py file and execute the .py file, it 
> blocked at "temp=p.readline()".

Of course it does. You haven't actually called the .exe file, all you 
have done is created a Popen instance and then grabbed a reference to 
it's stdout. Then you sit and wait for stdout to contain data, which it 
never does.

Instead, you can do this:

p = subprocess.Popen(['E:/EntTools/360EntSignHelper.exe', 
  'E:/build/temp/RemoteAssistSetup.exe'],
   stdout=subprocess.PIPE, shell=True)
output = p.communicate()[0]
print output


I think that should do what you expect.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about subprocess

2014-01-10 Thread Danny Yoo
There is a warning in the documentation on subprocess that might be
relevant to your situation:

Warning:
Use communicate() rather than .stdin.write, .stdout.read or
.stderr.read to avoid deadlocks due to any of the other OS pipe
buffers filling up and blocking the child process.

Reference: http://docs.python.org/2/library/subprocess.html


It's possible that the process is deadlocking due to this situation.
Do you run into this issue if you use communicate()?



Good luck!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Question about subprocess

2014-01-10 Thread daedae11
p = subprocess.Popen(['E:/EntTools/360EntSignHelper.exe', 
'E:/build/temp/RemoteAssistSetup.exe'],
 stdout=subprocess.PIPE, shell=True).stdout
temp = p.readline();
print temp
p.close()

When I run the above code in command line, it works formally.

However, when I writed it into a .py file and execute the .py file, it blocked 
at "temp=p.readline()".

What's the problem?

Please help.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Danny Yoo
On Fri, Jan 10, 2014 at 5:57 PM, Amy Davidson  wrote:
> Hey Danny,
>
> I just started taking the course (introduction to Computer Science) on last 
> Tuesday, so I am not to familiar. I have been doing my best to understand  
> the material by reading the text book, Learn Python the hard way.
>
> In my quest to answer the question given to me, I have searched the internet 
> high and low of other functions thus, I am familiar with the basic knowledge 
> of them (i.e. starting with def) as well as examples.

Great, ok.  So you're going through Learn Python the Hard Way.


I'm looking at the beginning of:

http://learnpythonthehardway.org/book/ex19.html

Have you gotten that far yet?  I see a function at the beginning of
that section called cheese_and_crackers.

##
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
print "Man that's enough for a party!"
print "Get a blanket.\n"
##

Can you tell me a little bit of how this function definition works?
What is the "%d" thing there?  What is cheese_count, and what is
box_of_crackers?

Have you been able to effectively type in this function and use it in
your Python programming environment?

Are there any similarities between what this function definition does,
and what your problem statement asks?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Amy Davidson
Hey Danny,

I just started taking the course (introduction to Computer Science) on last 
Tuesday, so I am not to familiar. I have been doing my best to understand  the 
material by reading the text book, Learn Python the hard way.

In my quest to answer the question given to me, I have searched the internet 
high and low of other functions thus, I am familiar with the basic knowledge of 
them (i.e. starting with def) as well as examples.

We can attempt the approach to the method that you prefer.

Thans for helping me, by the way. 
On Jan 10, 2014, at 5:25 PM, Danny Yoo  wrote:

> On Fri, Jan 10, 2014 at 2:00 PM, Keith Winston  wrote:
>> Amy, judging from Danny's replies, you may be emailing him and not the
>> list. If you want others to help, or to report on your progress,
>> you'll need to make sure the tutor email is in your reply to:
> 
> Hi Amy,
> 
> Very much so.  Please try to use "Reply to All" if you can.
> 
> If you're wondering why I'm asking for you to try to recall any other
> example function definitions, I'm doing so specifically because it is
> a general problem-solving technique.  Try to see if the problem that's
> stumping you is similar to things you've seen before.  Several of the
> heuristics from Polya's "How to Solve It" refer to this:
> 
>http://en.wikipedia.org/wiki/How_to_Solve_It
> 
> If you haven't ever seen any function definition ever before, then we
> do have to start from square one.  But this would be a very strange
> scenario, to be asked to write a function definition without having
> seen any previous definitions before.
> 
> If you have seen a function before, then one approach we might take is
> try to make analogies to those previous examples.  That's an approach
> I'd prefer.
> 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can a CGI program get the URL that called it?

2014-01-10 Thread Terry Carroll

On Sat, 11 Jan 2014, Steven D'Aprano wrote:


However, if you pass a path using \ to posixpath, it treats them as
non-separators:


That's apparenbtly what's happening.  I didn't investigate much, once I 
found out that using posixpath didn't address the issue I was having; 
using replace() was pretty straightforward.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python & Django

2014-01-10 Thread Lee Harr
> I am very interested to hear your opinion on which version of Python
> to use in conjunction with Django. Currently, I am taking a class at
> Udemy and they recommend using Python 2.7 with Django 1.6. because
> both versions work well with each other.
>
> Over the last few months I got pretty much used to Python 3.3.0 which
> some of you guys recommended to me on this mailing list. Hence, I
> would prefer to keep using Python 3x but I am not sure if that's a
> good idea. I heard of a couple folks using Python 3.3.0 with Django
> 1.6 that they ran into issues, and most of them switched back to
> Python 2.7.


I am using django 1.6 with python 3.3 and have had no problems.

It depends on what other modules you depend on, but for me
everything I need is working well.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can a CGI program get the URL that called it?

2014-01-10 Thread Steven D'Aprano
On Fri, Jan 10, 2014 at 02:44:35PM -0800, Terry Carroll wrote:

> As it turns out, since I was testing on a Windows box, os.path.relpath was 
> (reasonably) using a '\' as the separator character (surprisingly, so does 
> posixpath.relpath).

Are you sure about that? If it did, that would be an astonishing bug. I 
cannot replicate the behaviour you describe:

py> posixpath.relpath("/a/b/c")
'../../a/b/c'


However, if you pass a path using \ to posixpath, it treats them as 
non-separators:

py> posixpath.relpath("\\a\\b\\c")
'\\a\\b\\c'


That's because the given path \a\b\c under POSIX represents a file named 
"backslash a backslash b backslash c" in the current directory, not a 
file named c in a directory b in a directory a.


If you still think this is an issue, can you Can you post a minimal set 
of code that demonstrates that problem? Also, please specify the Python 
version.


Thanks,



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can a CGI program get the URL that called it?

2014-01-10 Thread Terry Carroll

Ah, I discovered what my problem was...

On Fri, 10 Jan 2014, Alan Gauld wrote:


its calling your file. You should know where your file is?


My problem was that, I know where the file is in the host's file system, 
and relative to my CGI program.  I do not have a URL to that file.


If you want to create a png file and display it to the user then you 
just store the file somewhere in your web site and create an html file 
that has an img tag referencing that location.


Right; I am producing HTML output (using the print command, not as a 
file), with an img tag.  The img tag has a src attribute, which must 
provide the URL of the referenced image file.


But I do not have that URL.  I know where that file is in the file system, 
and relative to my CGI program.  But I do not have a URL to the file.  My 
thinking was that, if I have the URL to my program, it's pretty trivial to 
construct the URL to the file.


And here's where my real problem was:  I had tried specifying just a 
relative path in the src tag, and that did not work consistently; 
specifically, I found that it worked in Chrome, but not Firefox.


As it turns out, since I was testing on a Windows box, os.path.relpath was 
(reasonably) using a '\' as the separator character (surprisingly, so does 
posixpath.relpath).  By simply adding:


   relative_path = relative_path.replace('\\', '/')

It uses the '/' required in a URL (even a relative-path URL) and works. 
Chrome was forgiving of the '\'; other browsers not so much.


It was not until I posted the  tag into a draft of this reply that I 
noticed the '\' characters.


I swear I looked at this for hours without noticing this before.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Danny Yoo
On Fri, Jan 10, 2014 at 2:00 PM, Keith Winston  wrote:
> Amy, judging from Danny's replies, you may be emailing him and not the
> list. If you want others to help, or to report on your progress,
> you'll need to make sure the tutor email is in your reply to:

Hi Amy,

Very much so.  Please try to use "Reply to All" if you can.

If you're wondering why I'm asking for you to try to recall any other
example function definitions, I'm doing so specifically because it is
a general problem-solving technique.  Try to see if the problem that's
stumping you is similar to things you've seen before.  Several of the
heuristics from Polya's "How to Solve It" refer to this:

http://en.wikipedia.org/wiki/How_to_Solve_It

If you haven't ever seen any function definition ever before, then we
do have to start from square one.  But this would be a very strange
scenario, to be asked to write a function definition without having
seen any previous definitions before.

If you have seen a function before, then one approach we might take is
try to make analogies to those previous examples.  That's an approach
I'd prefer.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can a CGI program get the URL that called it?

2014-01-10 Thread Danny Yoo
On Thu, Jan 9, 2014 at 2:30 PM, Terry Carroll  wrote:
> How can my Python 2.7 CGI program find the URL that caused the program to be
> called?

Hi Terry,

According to the description of CGI:

http://en.wikipedia.org/wiki/Common_Gateway_Interface

there should be a set of environmental variables that your program can
observe to reconstruct that portion of the request string.

In particular, you should be seeing SERVER_NAME, SERVER_PORT, and
other environmental variables if CGIHTTPServer from the Python
standard library is implementing the CGI protocol.

Checking...

http://hg.python.org/cpython/file/0e5df5b62488/Lib/CGIHTTPServer.py#l157

Yes, you should be seeing SERVER_NAME and SERVER_PORT, unless
something strange is happening.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Keith Winston
Amy, judging from Danny's replies, you may be emailing him and not the
list. If you want others to help, or to report on your progress,
you'll need to make sure the tutor email is in your reply to:

Often, people prefer you to respond to the list, if there isn't
something particularly personal in your response. Good luck with
learning Python, it's great language, and this is a very helpful
group. I do realize that learning your first computer language can be
disorienting.

Keith

On Fri, Jan 10, 2014 at 2:57 PM, Danny Yoo  wrote:
> I repeat my question in the hopes that you read it.  Do you have other
> examples of functions you have written or seen?
>
> I ask this because if you have never seen a function definition, our advice
> is radically different than if you have.
>
> Just giving us the homework statement is fairly useless to us: how does that
> help us figure out what part **you** are having difficult with?
>
> On Jan 10, 2014 11:25 AM, "Amy Davidson"  wrote:
>>
>> hi Danny,
>>
>> Below are the instructions.
>>
>> Write a function called printID that takes a name and student number as
>> parameters and prints them to the screen.
>>
>> E.g.:
>> >>> printID("Andrew Joe", 10055) #note the second parameter must be of
>> >>> type int
>>   "Student Name: Andrew Joe"
>>   "Student Number: 10055"
>> On Jan 10, 2014, at 2:14 PM, Danny Yoo  wrote:
>>
>> Hi Amy,
>>
>> Have you seen any other examples of functions in your instruction, either
>> in your books or notes?
>>
>>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Danny Yoo
I repeat my question in the hopes that you read it.  Do you have other
examples of functions you have written or seen?

I ask this because if you have never seen a function definition, our advice
is radically different than if you have.

Just giving us the homework statement is fairly useless to us: how does
that help us figure out what part **you** are having difficult with?
On Jan 10, 2014 11:25 AM, "Amy Davidson"  wrote:

> hi Danny,
>
> Below are the instructions.
>
> Write a function called printID that takes a name and student number as
> parameters and prints them to the screen.
> E.g.:
> >>> printID("Andrew Joe", 10055) #note the second parameter *must* be
> of type int
>   "Student Name: Andrew Joe"
>   "Student Number: 10055"
> On Jan 10, 2014, at 2:14 PM, Danny Yoo  wrote:
>
> Hi Amy,
>
> Have you seen any other examples of functions in your instruction, either
> in your books or notes?
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Danny Yoo
Hi Amy,

Have you seen any other examples of functions in your instruction, either
in your books or notes?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] arrangement of datafile

2014-01-10 Thread Albert-Jan Roskam

Ok, it's clear already that the OP has a csv file so the following is 
OFF-TOPIC. I was reading Python Cookbook and I saw a recipe to read fixed width 
files using struct.unpack. Much shorter and faster (esp. if you use compiled 
structs) than indexing. I thought this is a pretty cool approach: 
http://code.activestate.com/recipes/65224-accessing-substrings/.

regards,
Albert-Jan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread spir

On 01/10/2014 01:11 AM, Amy Davidson wrote:

Hi,

I am a university student who is struggling with writing functions in Python. 
I’ve been attempting to write a function in Python for over 2 hours with no 
progress. The function must be called, printID and take a name and student 
number as parameter and prints them to the screen.

This is my closest guess:

def print_ID(“Amy Davidson”, 1)
Student = “Amy Davidson”
StudentN = 1
print (“StudentName:”, Student)
print (“StudentNumber:”, StudentN)

If you could help correct my work and explain, that would be great!

Thanks,

Amy Davidson


You are confusing *defining* a function and *executing* it. Below a trial at 
explaining. You may have a part of program that does this:


name = "toto"
ident = 123

print(name, ident)  # the performing part

Now, say instead of a single line, the "performance" is bigger or more 
complicated and, most importantly, forms a whole conceptually. For instance, it 
may be a piece of code that (1) reads and properly structures input data or (2) 
processes them or (3) outputs results in a format convenient to the user. If you 
structure program application that way, then it looks like


data = read_input(where)
results = process(data)
write_report(results)

which is quite clear, isn't it? Functions (procedures, routines, etc...) are 
used first to provide such a clear structure to a program, which otherwise would 
be a huge mess. Another reason why functions (etc...) are necessary is that one 
often uses pieces of code multiple times, maybe millions of times even when 
applying the same process to a big store of data. Yet another reason is to build 
a toolkit for standard or common tasks, that one can reuse at will when needed 
--and that's what a language comes with: standard / primitive / builtin routines 
for the most common tasks. And then we construct more sophisticated processes 
from these building blocks, according to our app's needs.


Now, back to concrete. To define and use a function, one proceeds as follows 
(example):


# define function:
def average (numbers):
count = len(numbers)# len means length"
total = sum(numbers)
return total / count

# use function:
nums = [1,3,9]
x = average(nums)
print(x)# ==> 4.333

You first define the function using a "def" instruction. The function nearly 
always takes some input variable(s) called "paramaters", here 'numbers'. And 
often returns a result [*].


I think and hope you have enough information to do it in your case; and in a 
variety of other cases you may like to try and play with :-)


Denis

[*] Else, rather than a function properly, it is an action that *does* something 
but usually does not return any result. For instance, 'write_report' above is an 
action.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Keith Winston
Amy, be aware that there are slightly different versions of Python
floating around, and the example Dayo gave you uses a slightly
different print statement (no parens) than the example you provided
(which probably indicates that your Python requires them).

Good luck, you're on your way!

Keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can a CGI program get the URL that called it?

2014-01-10 Thread Alan Gauld

On 09/01/14 22:30, Terry Carroll wrote:

How can my Python 2.7 CGI program find the URL that caused the program
to be called?


You don't say what modules or toolkits you are using but I'll assume for 
now its the standard library cgi module?


I'm not sure what you are looking for. Is it the url that the user 
clicked on in their browser? Why would you need that dynamically,

its calling your file. You should know where your file is?
Or is your file being called from many different page links and you want 
the specific link? Usually that's done by adding a marker to the 
submitted data on the form?


If you want to create a png file and display it to the user
then you just store the file somewhere in your web site and
create an html file that has an img tag referencing that location.
You shouldn't need the calling url to do that unless you
are working across multiple web sites or somesuch.

Can you explain a bit more about the use case that requires you
to know the incoming url?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Alan Gauld

On 10/01/14 00:11, Amy Davidson wrote:

Hi,

I am a university student who is struggling with writing functions in Python.


You could try reading the functions and modules topic in
my tutorial.(see below)


The function must be called, printID and take a name and student number

> as parameter and prints them to the screen.


This is my closest guess:

def print_ID(“Amy Davidson”, 1)


The def printID bit is fine but you were supposeed to have *parameers* 
fore name and student number. A parameter is like a variable, its a 
generic name that you later assign a value to when you call the function.


Thus here is a simple function that adds 5 to the number
passed to it

def add5(n):
   ...

First, note the colon after the function definition, that's important.
Now, n is a parameter, a place marker that we can use inside
our function. When we call add5() we provide a value, called
the argument:

result = add5(3)

Now inside the add5() function n has the value 3.

So for your function you need to provide two parameters called,
maybe, say, 'student' and 'studentN'? (It's traditional to have
parameter names start with lower case letters. Python doesn't
care but it makes it easier for other programmers - or you
later on - to recognise what they are)


Student = “Amy Davidson”
StudentN = 1
print (“StudentName:”, Student)
print (“StudentNumber:”, StudentN)


The lines that make up a function body must be indented under
the def line. For example in my add5() example it would look
like:

def add5(n):
return n+5

So your four lines above need to be indented, and once
you create parameters the first two will not be needed.

Once you have that down you can then test your function by calling it 
with whatever values you want printed:


printID("Amy Davidson", 1)
printID("Bill Gates", 12345678)
etc...


hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Keith Winston
Amy, you may want to get a little clearer on the difference between
defining a function, and calling one. The definition is sort of a
generic process, it's when you are calling it that you really fill in
the blanks, and the function does what it's designed for (whether you
like it or not!).

You might even try breaking it down a little further, maybe write a
function with just one parameter, and call it a few times to get the
hang of it. In fact, try calling some of the built-in functions (for
example, you can get the length of a string s = "hello" by calling
'len(s)'). You can do all this at the python prompt, but as things get
more complicated it's less frustrating to do it saved to a file (are
you doing all this in IDLE?).

keith
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread jargon
On 01/10/2014 02:11 AM, Amy Davidson wrote:
> Hi,
>
> I am a university student who is struggling with writing functions in Python. 
> I’ve been attempting to write a function in Python for over 2 hours with no 
> progress. The function must be called, printID and take a name and student 
> number as parameter and prints them to the screen.
>
> This is my closest guess:
>
> def print_ID(“Amy Davidson”, 1)
> Student = “Amy Davidson”
> StudentN = 1
> print (“StudentName:”, Student)
> print (“StudentNumber:”, StudentN)
>
> If you could help correct my work and explain, that would be great! 
>
> Thanks,
>
> Amy Davidson
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
Hi Amy

Your function will need function parameters, to which values will be
passed to:

def printID(studentName, studentNumber):
print "Student Name: %s. Student Number: %d" %(studentName,
studentNumber)

Then you invoke the function like this:

printID("Amy Davidson", 1)

Hope this helps

Dayo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Question

2014-01-10 Thread Mark Lawrence

On 10/01/2014 00:11, Amy Davidson wrote:

Hi,

I am a university student who is struggling with writing functions in Python. 
I’ve been attempting to write a function in Python for over 2 hours with no 
progress. The function must be called, printID and take a name and student 
number as parameter and prints them to the screen.

This is my closest guess:

def print_ID(“Amy Davidson”, 1)


The def line should finish with a colon.  How do you expect to call this 
function for "Mark Lawrence", 1?  Research something like:- python 
functions parameters arguments.



Student = “Amy Davidson”
StudentN = 1
print (“StudentName:”, Student)
print (“StudentNumber:”, StudentN)


All four of your lines above should be indented, or have they been lost 
by your email client?  You simply assign to Student and StudentN which 
you then print.  Look closely at this, look back to my earlier comment, 
add them and hopefully the answer is 4 :)  Incidentally you'd usually 
spell them student and studentN.




If you could help correct my work and explain, that would be great!

Thanks,

Amy Davidson



--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How can a CGI program get the URL that called it?

2014-01-10 Thread Terry Carroll
How can my Python 2.7 CGI program find the URL that caused the program to 
be called?


I have a program that creates a JPG or PNG file on the fly, and needs to 
construct a URL to it. I know the path relative to my program is, for 
example, "../temp/tmpiicack.png" (the filename generated by 
tempfile.NamedTemporaryFile).  From this, I want to generate a URL for the 
image so it can be displayed.


I invoke My CGI program (in testing) with the URL 
http://localhost:8000/cgi-bin/query.py?tmtype=s&tmnumber=76044902. It is 
usually invoked using a form at http://localhost:8000/cgi-bin/query.py, 
which generates the URL, but can also be invoked by directly going to the 
URL with parameters specified (I want to be able to email a complete URL, 
for example).  In this instance, the URL I want to generate would be 
http://localhost:8000/temp/tmpiicack.png.  The problem is, my program does 
not know the "http://localhost:8000"; part.


Module urlparse has facilities for generating a URL from relative parts, 
but requires that I know a base URL to begin with. I've looked in 
os.environ to see if anything is presented, but the only thing close is 
os.environ['HTTP_REFERER'], which is only populated if the program is 
invoked from the form-click, not if directly entered (e.g. copy/paste).


(That's my fall-back solution; but it will mean the image-support will 
fail if a URL is entered directly, and will work only if invoked from a 
form.)


I've also checked os.environ['PATH_INFO'] as suggested in a post on 
stackoverflow,[1] but that is not populated.  (I can't recall whether it's 
a zero-length string or None, but it has nothing useful).


I'm testing using CGIHTTPServer as my server, if it matters.


[1] 
http://stackoverflow.com/questions/4939067/catching-the-url-path-following-a-python-cgi-script/4939137#4939137


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Question

2014-01-10 Thread Amy Davidson
Hi,

I am a university student who is struggling with writing functions in Python. 
I’ve been attempting to write a function in Python for over 2 hours with no 
progress. The function must be called, printID and take a name and student 
number as parameter and prints them to the screen.

This is my closest guess:

def print_ID(“Amy Davidson”, 1)
Student = “Amy Davidson”
StudentN = 1
print (“StudentName:”, Student)
print (“StudentNumber:”, StudentN)

If you could help correct my work and explain, that would be great! 

Thanks,

Amy Davidson
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] arrangement of datafile

2014-01-10 Thread Amrita Kumari
Hi Peter,

Thankyou very much for your kind help. I got the output like the way I
wanted (which you have also shown in your output). I really appreciate your
effort.

Thanks for your time.
Amrita


On Thu, Jan 9, 2014 at 8:41 PM, Peter Otten <__pete...@web.de> wrote:

> Amrita Kumari wrote:
>
> > On 17th Dec. I posted one question, how to arrange datafile in a
> > particular fashion so that I can have only residue no. and chemical
> > shift value of the atom as:
> > 1  H=nil
> > 2  H=8.8500
> > 3  H=8.7530
> > 4  H=7.9100
> > 5  H=7.4450
> > 
> > Peter has replied to this mail but since I haven't subscribe to the
> > tutor mailing list earlier hence I didn't receive the reply, I
> > apologize for my mistake, today I checked his reply and he asked me to
> > do few things:
>
> I'm sorry, I'm currently lacking the patience to tune into your problem
> again, but maybe the script that I wrote (but did not post) back then is of
> help.
>
> The data sample:
>
> $ cat residues.txt
> 1 GLY HA2=3.7850 HA3=3.9130
> 2 SER H=8.8500 HA=4.3370 N=115.7570
> 3 LYS H=8.7530 HA=4.0340 HB2=1.8080 N=123.2380
> 4 LYS H=7.9100 HA=3.8620 HB2=1.7440 HG2=1.4410 N=117.9810
> 5 LYS H=7.4450 HA=4.0770 HB2=1.7650 HG2=1.4130 N=115.4790
> 6 LEU H=7.6870 HA=4.2100 HB2=1.3860 HB3=1.6050 HG=1.5130 HD11=0.7690
> HD12=0.7690 HD13=0.7690 N=117.3260
> 7 PHE H=7.8190 HA=4.5540 HB2=3.1360 N=117.0800
> 8 PRO HD2=3.7450
> 9 GLN H=8.2350 HA=4.0120 HB2=2.1370 N=116.3660
> 10 ILE H=7.9790 HA=3.6970 HB=1.8800 HG21=0.8470 HG22=0.8470 HG23=0.8470
> HG12=1.6010 HG13=2.1670 N=119.0300
> 11 ASN H=7.9470 HA=4.3690 HB3=2.5140 N=117.8620
> 12 PHE H=8.1910 HA=4.1920 HB2=3.1560 N=121.2640
> 13 LEU H=8.1330 HA=3.8170 HB3=1.7880 HG=1.5810 HD11=0.8620 HD12=0.8620
> HD13=0.8620 N=119.1360
>
> The script:
>
> $ cat residues.py
> def process(filename):
> residues = {}
> with open(filename) as infile:
> for line in infile:
> parts = line.split()# split line at whitespace
> residue = int(parts.pop(0)) # convert first item to integer
> if residue in residues:
> raise ValueError("duplicate residue {}".format(residue))
> parts.pop(0)# discard second item
>
> # split remaining items at "=" and put them in a dict,
> # e. g. {"HA2": 3.7, "HA3": 3.9}
> pairs = (pair.split("=") for pair in parts)
> lookup = {atom: float(value) for atom, value in pairs}
>
> # put previous lookup dict in residues dict
> # e. g. {1: {"HA2": 3.7, "HA3": 3.9}}
> residues[residue] = lookup
>
> return residues
>
> def show(residues):
> atoms = set().union(*(r.keys() for r in residues.values()))
> residues = sorted(residues.items())
> for atom in sorted(atoms):
> for residue, lookup in residues:
> print "{} {}={}".format(residue, atom, lookup.get(atom, "nil"))
> print
> print "---"
> print
>
> if __name__ == "__main__":
> r = process("residues.txt")
> show(r)
>
> Note that converting the values to float can be omitted if all you want to
> do is print them. Finally the output of the script:
>
> $ python residues.py
> 1 H=nil
> 2 H=8.85
> 3 H=8.753
> 4 H=7.91
> 5 H=7.445
> 6 H=7.687
> 7 H=7.819
> 8 H=nil
> 9 H=8.235
> 10 H=7.979
> 11 H=7.947
> 12 H=8.191
> 13 H=8.133
>
> ---
>
> 1 HA=nil
> 2 HA=4.337
> 3 HA=4.034
> 4 HA=3.862
> 5 HA=4.077
> 6 HA=4.21
> 7 HA=4.554
> 8 HA=nil
> 9 HA=4.012
> 10 HA=3.697
> 11 HA=4.369
> 12 HA=4.192
> 13 HA=3.817
>
> ---
>
> [snip]
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor