[Tutor] help on raw_input()
Hi, i wanted to get a string from raw_input like this raw_input('>') > \n\nsomestring but the problem is raw input will return the string '\\n\\nsomestring' My question is Are there any function to convert back those string to '\n\nsomestring' ? Thanks - Ahhh...imagining that irresistible "new car" smell? Check outnew cars at Yahoo! Autos.___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reassign
I wonder how to use colors from gray to black to represent a group of values from small to large? Thanks, Linda ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Command line args
"Teresa Stanton" <[EMAIL PROTECTED]> wrote > No one suggested this. That's great! Wish I had seen it sooner. > Thanks, > I'll put that in my notebook for further use later. Note that Fileinput is used to iterate over a (set of) file line by line, it doesn't read the entire file into a string as in your original question. You can get round that by using join() to join the lines togvether after reading. Also there are no prompts to stdin if that matters. HTH, Alan G. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
"ammar azif" <[EMAIL PROTECTED]> wrote > i wanted to get a string from raw_input like this > > raw_input('>') >> \n\nsomestring OK, Can you explain precisely what you want the string to contain. \n is the string representation of a newline. Do you want to enter something that starts with two newlines? Or do you literally want the sequence \,n,\,n? If its the latter thats what Python has stored. The double slash is only there when python displays the result (Try using len and comparing if you aren't convinced) If you want to actually capture newline characters from raw_input, thats more tricky. But before we get to that can you clarify what you actually want? Alan G. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
Actually i wanted to write a http client using just he low level socket module. The program will prompt the user asking for input and the use will type http commands like 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n' when i use the raw_input function , the string that i get is 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n' is there any easy way other than modify this string ? Perhaps regular expression? Alan Gauld <[EMAIL PROTECTED]> wrote: "ammar azif" wrote > i wanted to get a string from raw_input like this > > raw_input('>') >> \n\nsomestring OK, Can you explain precisely what you want the string to contain. \n is the string representation of a newline. Do you want to enter something that starts with two newlines? Or do you literally want the sequence \,n,\,n? If its the latter thats what Python has stored. The double slash is only there when python displays the result (Try using len and comparing if you aren't convinced) If you want to actually capture newline characters from raw_input, thats more tricky. But before we get to that can you clarify what you actually want? Alan G. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor - Ahhh...imagining that irresistible "new car" smell? Check outnew cars at Yahoo! Autos.___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading.currentThread() always same across request ?
Arun Kumar PG wrote: > Guys, > > I have a web application and I want to store an object per request so > thta that is available across all classes till end of request. > > I am planning to write the below code in my entry program which is > executed as soon as a request comes: > > entry.py > import threading > > th = threading.currentThread() > th.service = somemod.Service() > > then in other parts of program whereever I want to use service: > > th = threading.currentThread () > if hasattr(th, 'service'): > th.service.call_whatever() Rather than store your attributes directly in the thread, it would probably be better to use a threading.local() object as the container. http://docs.python.org/lib/module-threading.html#l2h-3416 I don't really know if it makes any difference but this is the supported mechanism for thread-local storage. > I am wondering if I will ever face a problem in this case ? I want to > make sure that since the request till the end the same "service" object > is available and it should not collide with any other request. Since > it's a web application multilple request may come simutaneously. You have to be careful that you don't get stale data, as threads are often reused in web servers. At the end of the request processing you should clear out your local data. If you are using an existing web framework it probably has some kind of support for sessions. Using session objects might be another way to do this. Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
ammar azif wrote: > Hi, > > i wanted to get a string from raw_input like this > > raw_input('>') > > \n\nsomestring > > but the problem is raw input will return the string > '\\n\\nsomestring' This is a bit confusing to talk about because the actual contents of the string differ from what is printed. I don't know if you realize that or not so I'll start at the beginning. In [3]: s= raw_input('> ') > one\r\ntwo In [4]: s Out[4]: 'one\\r\\ntwo' When the interpreter outputs a string, it outputs it in the form of a string constant. Any actual \ in the string is escaped with an extra \. The string s contains single \ characters. You can verify this by getting the length: In [5]: len(s) Out[5]: 10 or by outputting it with print, which just outputs the actual characters: In [6]: print s one\r\ntwo So s does contain the same characters as typed. > My question is > Are there any function to convert back those string to '\n\nsomestring' ? You don't want the four literal characters \, r, \, n, you want a carriage return / newline combination. raw_input() doesn't interpret escape characters but there is a way to convert them: In [7]: t=s.decode('string_escape') Now t contains a carriage return / newline instead of the escape characters: In [8]: t Out[8]: 'one\r\ntwo' In [9]: len(t) Out[9]: 8 In [10]: print t one two Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reassign
"linda.s" <[EMAIL PROTECTED]> wrote >I wonder how to use colors from gray to black to represent a group of > values from small to large? Can you explain what you mean? Normally colours are represented in code as a tuple of three numbers for the RGB values. If its shades of grey then all three numbers are the same so you only need a single number. But you don't store colours in a program (except maybe as string representations, eg 'cyan', 'blue', 'azure'). I'm not sure what you mean by using colours to represent values. Usually its the other way around? If you do mean turning a range of values into shades of grey then thats faitrly easy, just scale your values between 0 and 255, where black is (0,0,0) and white is (255,255,255). If you have N values then the scaling factor is 255/N. Then simply multiply the value by the factor... Alan G. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
"ammar azif" <[EMAIL PROTECTED]> wrote > Actually i wanted to write a http client using just he low level > socket module. I won;t ask why! But the httplib module probably does what you want just in case you dodn't realize it existed... > The program will prompt the user asking for input and the > use will type http commands like > 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n' OK, So you want the user to acrtually type \,n,\,n and you will then send that string to be interpreted as newlines? > when i use the raw_input function , the string that i get is > 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n' The double \\ doesn''t actually exist its just Python telling you that it is a literal \ character not an escaped sequence. As I said earlier if you check the len() of the string it will only have one character per backslash. I think it's already doing what you want! You just need to turn the \n's that the user entered into newline characters, Kent has shown you how to do that with the decode() method... -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
Thanks the encode method really helps. Alan Gauld <[EMAIL PROTECTED]> wrote: "ammar azif" wrote > Actually i wanted to write a http client using just he low level > socket module. I won;t ask why! But the httplib module probably does what you want just in case you dodn't realize it existed... > The program will prompt the user asking for input and the > use will type http commands like > 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n' OK, So you want the user to acrtually type \,n,\,n and you will then send that string to be interpreted as newlines? > when i use the raw_input function , the string that i get is > 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n' The double \\ doesn''t actually exist its just Python telling you that it is a literal \ character not an escaped sequence. As I said earlier if you check the len() of the string it will only have one character per backslash. I think it's already doing what you want! You just need to turn the \n's that the user entered into newline characters, Kent has shown you how to do that with the decode() method... -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor - Ahhh...imagining that irresistible "new car" smell? Check outnew cars at Yahoo! Autos.___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading.currentThread() always same across request ?
* Kent Johnson <[EMAIL PROTECTED]> [070414 19:30]: > Arun Kumar PG wrote: > > Guys, > > > > I have a web application and I want to store an object per request so > > thta that is available across all classes till end of request. > > > > I am planning to write the below code in my entry program which is > > executed as soon as a request comes: > > > > entry.py > > import threading > > > > th = threading.currentThread() > > th.service = somemod.Service() > > > > then in other parts of program whereever I want to use service: > > > > th = threading.currentThread () > > if hasattr(th, 'service'): > > th.service.call_whatever() > > Rather than store your attributes directly in the thread, it would > probably be better to use a threading.local() object as the container. > http://docs.python.org/lib/module-threading.html#l2h-3416 > > I don't really know if it makes any difference but this is the supported > mechanism for thread-local storage. Well, the other mechanism is also supported, because subclassing threads is allowed, and subclasses need to keep their attributes, even if accessed via threading.currentThread(). But as Kent has stated, stick with the Session mechanism of your framework. > If you are using an existing web framework it probably has some kind of > support for sessions. Using session objects might be another way to do this. Andreas ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading.currentThread() always same across request ?
Andreas Kostyrka wrote: > * Kent Johnson <[EMAIL PROTECTED]> [070414 19:30]: >> Rather than store your attributes directly in the thread, it would >> probably be better to use a threading.local() object as the container. >> http://docs.python.org/lib/module-threading.html#l2h-3416 >> >> I don't really know if it makes any difference but this is the supported >> mechanism for thread-local storage. > > Well, the other mechanism is also supported, because subclassing > threads is allowed, and subclasses need to keep their attributes, even > if accessed via threading.currentThread(). That's a good point. Does anyone know when to prefer threading.local() vs thread attributes? Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading.currentThread() always same across request ?
* Kent Johnson <[EMAIL PROTECTED]> [070414 19:53]: > Andreas Kostyrka wrote: > > * Kent Johnson <[EMAIL PROTECTED]> [070414 19:30]: > >> Rather than store your attributes directly in the thread, it would > >> probably be better to use a threading.local() object as the container. > >> http://docs.python.org/lib/module-threading.html#l2h-3416 > >> > >> I don't really know if it makes any difference but this is the supported > >> mechanism for thread-local storage. > > > > Well, the other mechanism is also supported, because subclassing > > threads is allowed, and subclasses need to keep their attributes, even > > if accessed via threading.currentThread(). > > That's a good point. Does anyone know when to prefer threading.local() > vs thread attributes? It's design question, I guess: *) if you have thread subclasses, then use thread attributes. *) if you have standard threads, then use thread.local(). The idea is, that it's "rude" to stick attributes on an object that is not owned by you. Rationale: *) Somebody might decide to make threading.Thread be a new style object with __slots__ => your code breaks. I know, it's unprobably, but if you derive a subclass, you can be at least sure that the object will have a __dict__ ;) Andreas ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Seeking python projects
Hi folks, I want to practice Python programming by developing complete applications. Where can I get such problems, which can improve my Python programming skills. Thanks in anticipation. Regards, Asrar ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading.currentThread() always same across request ?
Andreas Kostyrka wrote: > * Kent Johnson <[EMAIL PROTECTED]> [070414 19:53]: >> That's a good point. Does anyone know when to prefer threading.local() >> vs thread attributes? > It's design question, I guess: > > *) if you have thread subclasses, then use thread attributes. > *) if you have standard threads, then use thread.local(). > > The idea is, that it's "rude" to stick attributes on an object that is > not owned by you. > > Rationale: > *) Somebody might decide to make threading.Thread be a new style > object with __slots__ => your code breaks. > > I know, it's unprobably, but if you derive a subclass, you can be at > least sure that the object will have a __dict__ ;) If you use threading.local() you can be sure the names you use don't conflict with any attributes of the thread. Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] threading.currentThread() always same across request ?
Thx guys. now quick question on the usage of thread local storage. In my case I have the below object model: class Base(object):' def __init__(self): self.__service = None def _GetService(): if not hasattr(threading.currentThread(), 'service'): threading.currentThread().service = Service() self.__service = threading.currentThread().service return self.__service class Child1(Base):' def DoSomething(): service = self._GetService() # use service class Child2(Base):' def DoSomething(): service = self._GetService() # use service The above Child classes are used by a controller: class Controller(object): def process(self): c1 = Child1() c1.DoSomething() ... c2 = Child2() c2.DoSomething() Using the above technique the "service" is instantiated only one time i.e. as soon as I create the first instance of the Child class abd associated with the current thread for future instantiation of any Child class. Now in this scenario how can I use thread local ? Where do I keep the thread local object as in my case I am instantiating the Child classes ? Using currentThread() always gives me the same thread instance for a given request and I can bypass instantiating the Service class by simply returning the "service" attribute already attached to the current thread. Any suggestion appreciated! - A On 4/15/07, Kent Johnson <[EMAIL PROTECTED]> wrote: Andreas Kostyrka wrote: > * Kent Johnson <[EMAIL PROTECTED]> [070414 19:53]: >> That's a good point. Does anyone know when to prefer threading.local() >> vs thread attributes? > It's design question, I guess: > > *) if you have thread subclasses, then use thread attributes. > *) if you have standard threads, then use thread.local(). > > The idea is, that it's "rude" to stick attributes on an object that is > not owned by you. > > Rationale: > *) Somebody might decide to make threading.Thread be a new style > object with __slots__ => your code breaks. > > I know, it's unprobably, but if you derive a subclass, you can be at > least sure that the object will have a __dict__ ;) If you use threading.local() you can be sure the names you use don't conflict with any attributes of the thread. Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor