Re: Python Programing for the Absoulte Beginner
Steve Hayes wrote: > I've got too big an investment in books on Python 2, and there are no > books available on Python 3 (I don't regard downloadable PDFs or other > onlines stuff as "books"). I love Python 3, it's way better than Python 2, and there's less and less reason to stick to Python 2 now. You really should learn Python 3, you won't be sorry. But, if you choose not to, there's nothing to be ashamed of. Python 2.7 has got at least six years of life left in it, and when you're done with it, migrating to Python 3 isn't like learning a new language. It's more like the difference between American and British English. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
--- Original Message - > From: Terry Reedy > To: python-list@python.org > Cc: > Sent: Sunday, August 3, 2014 4:43 AM > Subject: Re: Correct type for a simple "bag of attributes" namespace object > > On 8/2/2014 8:59 PM, Ian Kelly wrote: >> On Sat, Aug 2, 2014 at 2:46 PM, Mark Summerfield > wrote: >>> On Saturday, 2 August 2014 20:58:59 UTC+1, Ben Finney wrote: Steven D'Aprano writes: > If you need instances which carry state, then object is the > wrong > class. >>> >>> Fair enough. >>> Right. The 'types' module provides a SimpleNamespace class > for the common "bag of attributes" use case:: >>> import types >>> foo = types.SimpleNamespace() >>> foo.x = 3 >>> foo namespace(x=3) >>> >>> This is too much for children (& beginners). >>> >>> But perhaps what I should be asking for is for a new built-in that does > what types.SimpleNamespace() does, so that without any import you can write, > say, >>> >>> foo = namespace(a=1, b=2) >>> # or >>> bar = namespace() >>> bar.a = 1 I find the following obscure (to me at least) use of type() useful exactly for this "bag of attributes" use case: >>> employee = type("Employee", (object,), {}) >>> employee.name = "John Doe" >>> employee.position = "Python programmer" >>> employee.name, employee.position, employee ('John Doe', 'Python programmer', ) >>> details = dict(name="John Doe", position="Python programmer") >>> employee = type("Employee", (object,), details) >>> employee.name, employee.position, employee ('John Doe', 'Python programmer', ) regards, Albert-Jan -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Albert-Jan Roskam wrote: > I find the following obscure (to me at least) use of type() useful exactly > for this "bag of attributes" use case: employee = type("Employee", (object,), {}) employee.name = "John Doe" employee.position = "Python programmer" employee.name, employee.position, employee > ('John Doe', 'Python programmer', ) Are you sure you know what you are doing? The above is equivalent to >>> class employee: ... name = "John Doe" ... position = "Python programmer" ... >>> employee.name, employee.position, employee ('John Doe', 'Python programmer', ) >>> type(employee) Basically you are using classes as instances. While there is no fundamental difference between classes and instances in Python you'll surprise readers of your code and waste some space: >>> import sys >>> sys.getsizeof(employee) 976 >>> class Employee: pass ... >>> employee = Employee() >>> employee.name = "John Doe" >>> employee.position = "Python programmer" >>> sys.getsizeof(employee) 64 -- https://mail.python.org/mailman/listinfo/python-list
SQLAlchemy tutorial
Hi, I've been looking at various tutorials on SQLAlchemy as I am planning to do fill a void among the Linux applications (unless someone has seen a diabetic result "analyser" thingy that's common for Android et al). But I need to get a database working. Problem is the Alchemy tutorials I've been looking at all produce various errors when following them (and pretty early on). Anyone got a suggestion for SQLAlchemy and Python 3.4 they can recommend? /Martin S -- Regards, Martin S -- https://mail.python.org/mailman/listinfo/python-list
Re: SQLAlchemy tutorial
Martin S writes: > Problem is the [SQLAlchemy] tutorials I've been looking at all produce > various errors when following them (and pretty early on). SQLAlchemy has been progressively adding support for Python 3, so it matters which version of SQLAlchemy you install. For best results, it seems SQLAlchemy version 0.9 or later http://docs.sqlalchemy.org/en/rel_0_9/changelog/migration_09.html#targeting-python-2-6-and-up-now-python-3-without-2to3> supports Python 3 seamlessly. > Anyone got a suggestion for SQLAlchemy and Python 3.4 they can > recommend? The official documentation http://docs.sqlalchemy.org/> is the right place to start, once you can get compatible versions of SQLAlchemy and Python together. -- \ “It seems intuitively obvious to me, which means that it might | `\ be wrong.” —Chris Torek | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
- Original Message - > From: Peter Otten <__pete...@web.de> > To: python-list@python.org > Cc: > Sent: Sunday, August 3, 2014 11:37 AM > Subject: Re: Correct type for a simple "bag of attributes" namespace object > > Albert-Jan Roskam wrote: > >> I find the following obscure (to me at least) use of type() useful exactly >> for this "bag of attributes" use case: > employee = type("Employee", (object,), {}) > employee.name = "John Doe" > employee.position = "Python programmer" > employee.name, employee.position, employee >> ('John Doe', 'Python programmer', '__main__.Employee'>) > > Are you sure you know what you are doing? The above is equivalent to > class employee: > ... name = "John Doe" > ... position = "Python programmer" > ... employee.name, employee.position, employee > ('John Doe', 'Python programmer', '__main__.employee'>) type(employee) > > > Basically you are using classes as instances. While there is no fundamental > difference between classes and instances in Python you'll surprise readers > of your code and waste some space: Yes, I know that it is equivalent, but I have always found it kind of ugly to use class() just to bundle a number of items. Like you are 'announcing OOP' (not sure how to put this into words), and then it's merely a simple bundle. Or maybe it's just me being silly, because it's even in the Python tutorial: https://docs.python.org/2/tutorial/classes.html#odds-and-ends import sys sys.getsizeof(employee) > 976 class Employee: pass > ... employee = Employee() employee.name = "John Doe" employee.position = "Python programmer" sys.getsizeof(employee) > 64 Wow, I was not aware of that at all. So they are not equivalent after all. -- https://mail.python.org/mailman/listinfo/python-list
How to turn off Python's event log window in the background?
Hi, whenever I run the Leo editor (a Python application) from Windows (8.1), there is always an event log window in the background. I want to turn it off. It was suggested to me on another forum that I use pythonw.exe instead of python.exe to prevent the window from being displayed, but I don't know how to do this, because it is all automatic: I just click on the .py file, and the python interpreter is automatically loaded. Could someone please tell me how to disable the event log window? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
- Original Message - > From: Albert-Jan Roskam > To: Terry Reedy ; "python-list@python.org" > > Cc: > Sent: Sunday, August 3, 2014 11:17 AM > Subject: Re: Correct type for a simple "bag of attributes" namespace object > Right. The 'types' module provides a SimpleNamespace > class >> for the > common "bag of attributes" use case:: > > >>> import types > >>> foo = types.SimpleNamespace() > >>> foo.x = 3 > >>> foo > namespace(x=3) This is too much for children (& beginners). But perhaps what I should be asking for is for a new built-in that > does >> what types.SimpleNamespace() does, so that without any import you can > write, >> say, foo = namespace(a=1, b=2) # or bar = namespace() bar.a = 1 > > I find the following obscure (to me at least) use of type() useful exactly > for > this "bag of attributes" use case: employee = type("Employee", (object,), {}) employee.name = "John Doe" employee.position = "Python programmer" employee.name, employee.position, employee > ('John Doe', 'Python programmer', '__main__.Employee'>) > details = dict(name="John Doe", position="Python > programmer") employee = type("Employee", (object,), details) employee.name, employee.position, employee > ('John Doe', 'Python programmer', '__main__.Employee'>) PS to my previous mail: class() can (should?) be used here to do the exact same thing but it feels a little like "Getting your car [OOP] just because you need an ashtray [bundled items]". :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Albert-Jan Roskam wrote: > > > - Original Message - > >> From: Peter Otten <__pete...@web.de> >> To: python-list@python.org >> Cc: >> Sent: Sunday, August 3, 2014 11:37 AM >> Subject: Re: Correct type for a simple "bag of attributes" namespace >> object >> >> Albert-Jan Roskam wrote: >> >>> I find the following obscure (to me at least) use of type() useful >>> exactly for this "bag of attributes" use case: >> employee = type("Employee", (object,), {}) >> employee.name = "John Doe" >> employee.position = "Python programmer" >> employee.name, employee.position, employee >>> ('John Doe', 'Python programmer', > '__main__.Employee'>) >> >> Are you sure you know what you are doing? The above is equivalent to >> > class employee: >> ... name = "John Doe" >> ... position = "Python programmer" >> ... > employee.name, employee.position, employee >> ('John Doe', 'Python programmer', > '__main__.employee'>) > type(employee) >> >> >> Basically you are using classes as instances. While there is no >> fundamental difference between classes and instances in Python you'll >> surprise readers of your code and waste some space: > > Yes, I know that it is equivalent, but I have always found it kind of ugly > to use class() just to bundle a number of items. But that's what you are doing, you just spell it differently. > Like you are 'announcing > OOP' (not sure how to put this into words), and then it's merely a simple > bundle. Or maybe it's just me being silly, because it's even in the Python > tutorial: https://docs.python.org/2/tutorial/classes.html#odds-and-ends > > import sys > sys.getsizeof(employee) >> 976 > class Employee: pass >> ... > employee = Employee() > employee.name = "John Doe" > employee.position = "Python programmer" > sys.getsizeof(employee) >> 64 > Wow, I was not aware of that at all. So they are not equivalent after all. I think you are misunderstanding what I was trying to say; so to be explicit: >>> class Employee: pass ... >>> sys.getsizeof(Employee) 976 i. e. you have a per-class and per-instance memory consumption. The latter is smaller, so with regards to memory consumption instantiating only pays off when there is more than one employee. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to turn off Python's event log window in the background?
On Sun, Aug 3, 2014 at 9:05 PM, wrote: > It was suggested to me on another forum that I use pythonw.exe instead of > python.exe to prevent the window from being displayed, but I don't know how > to do this, because it is all automatic: I just click on the .py file, and > the python interpreter is automatically loaded. > Assuming you have your installation set up in the normal way, renaming your .py file to .pyw will have it run with pythonw.exe instead of python.exe. That should do what you want. Have fun! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
On 02/08/2014 20:58, Ben Finney wrote: Steven D'Aprano writes: If you need instances which carry state, then object is the wrong class. Right. The ‘types’ module provides a SimpleNamespace class for the common “bag of attributes” use case:: >>> import types >>> foo = types.SimpleNamespace() >>> foo.x = 3 >>> foo namespace(x=3) https://docs.python.org/3/library/types.html#types.SimpleNamespace> What Alex Martelli called a bunch? http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
In article , Mark Lawrence wrote: > On 02/08/2014 20:58, Ben Finney wrote: > > Steven D'Aprano writes: > > > >> If you need instances which carry state, then object is the wrong > >> class. > > > > Right. The âtypesâ module provides a SimpleNamespace class for the > > common âbag of attributesâ use case:: > > > > >>> import types > > >>> foo = types.SimpleNamespace() > > >>> foo.x = 3 > > >>> foo > > namespace(x=3) > > > > https://docs.python.org/3/library/types.html#types.SimpleNamespace> > > > > What Alex Martelli called a bunch? > > http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a- > bunch-of-named/ Still overkill :-) I usually just do: class Data: pass my_obj = Data() That's all you really need. It's annoying that you can't just do: my_obj = object() which would be even simpler, because (for reasons I don't understand), you can't create new attributes on my_obj. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
On Sun, Aug 3, 2014 at 10:40 PM, Roy Smith wrote: > I usually just do: > > class Data: >pass > my_obj = Data() > > That's all you really need. It's annoying that you can't just do: > > my_obj = object() > > which would be even simpler, because (for reasons I don't understand), > you can't create new attributes on my_obj. Python 3.4 on Windows (32-bit): >>> import sys >>> class Data: pass >>> sys.getsizeof(Data()) 32 >>> sys.getsizeof(object()) 8 Even before you add a single attribute, the object is four times the size it needs to be. When you need a sentinel (for a function's default argument, for instance), you don't need any attributes on it - all you need is some object whose identity can be tested. And the excess would be multiplied by a fairly large number of objects in the system (it's not just object() that doesn't take attributes). If you want a bag of attributes, get a bag of attributes, don't expect object() to be it :) The only reason I can think of for expecting a basic object to work this way is because of the parallel with ECMAScript; it's not a fundamental part of the type hierarchy. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
In article , Chris Angelico wrote: > On Sun, Aug 3, 2014 at 10:40 PM, Roy Smith wrote: > > I usually just do: > > > > class Data: > >pass > > my_obj = Data() > > > > That's all you really need. It's annoying that you can't just do: > > > > my_obj = object() > > > > which would be even simpler, because (for reasons I don't understand), > > you can't create new attributes on my_obj. > > [...] > The only reason I can think of for expecting a > basic object to work this way is because of the parallel with > ECMAScript; it's not a fundamental part of the type hierarchy. I don't know about that. I agree that your explanation about object size makes sense for why it wasn't built to work that way, but I don't think the expectation should be surprising. When I write: class Foo(Bar): # stuff I'm saying, "make Foos be just like Bars, except for this stuff". I can do: >>> class Foo(object): ... pass ... >>> f = Foo() >>> f.x = 1 in which case, I've said, "make Foos just like objects, except for, oh, never mind, there aren't any differences". But, in reality, the system bolted on the ability to have user-defined attributes without telling me. I don't think it's unreasonable to be surprised at that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
On Sun, Aug 3, 2014 at 11:25 PM, Roy Smith wrote: > in which case, I've said, "make Foos just like objects, except for, oh, > never mind, there aren't any differences". But, in reality, the system > bolted on the ability to have user-defined attributes without telling > me. I don't think it's unreasonable to be surprised at that. I agree that this is slightly surprising. However, imagine if it were the other way: class Foo(object): x = 1 def __init__(self): self.y = 2 These would throw errors, unless you explicitly disable __slots__ processing. When there's two ways to do things and both would be surprising, you pick the one that's going to be less of a surprise, or surprising less often, and accept it. That doesn't mean it's not a problem, but it's better than the alternative. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Getting a list of all modules
On 01/08/2014 16:41, Steven D'Aprano wrote: On Fri, 01 Aug 2014 14:39:09 +0100, Robert Kern wrote: Take a look at what has already been implemented in IPython: https://github.com/ipython/ipython/blob/master/IPython/core/ completerlib.py#L208 Awesome! Thank you! Is Lib/idlelib/AutoComplete.py of any use? I found it when looking at this http://bugs.python.org/issue18766 -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
asyncio with map&reduce flavor and without flooding the event loop
Hi all I am trying to use asyncio in real applications and it doesn't go that easy, a help of asyncio gurus is needed badly. Consider a task like crawling the web starting from some web-sites. Each site leads to generation of new downloading tasks in exponential(!) progression. However we don't want neither to flood the event loop nor to overload our network. We'd like to control the task flow. This is what I achieve well with modification of nice Maxime's solution proposed here: https://mail.python.org/pipermail/python-list/2014-July/675048.html Well, but I'd need as well a very natural thing, kind of map() & reduce() or functools.reduce() if we are on python3 already. That is, I'd need to call a "summarizing" function for all the downloading tasks completed on links from a page. This is where i fail :( I'd propose an oversimplified but still a nice test to model the use case: Let's use fibonacci function implementation in its ineffective form. That is, let the coro_sum() be our reduce() function and coro_fib be our map(). Something like this: @asyncio.coroutine def coro_sum(x): return sum(x) @asyncio.coroutine def coro_fib(x): if x < 2: return 1 res_coro = executor_pool.spawn_task_when_arg_list_of_coros_ready(coro=coro_sum, arg_coro_list=[coro_fib(x - 1), coro_fib(x - 2)]) return res_coro So that we could run the following tests. Test #1 on one worker: executor_pool = ExecutorPool(workers=1) executor_pool.as_completed( coro_fib(x) for x in range(20) ) Test #2 on two workers: executor_pool = ExecutorPool(workers=2) executor_pool.as_completed( coro_fib(x) for x in range(20) ) It would be very important that both each coro_fib() and coro_sum() invocations are done via a Task on some worker, not just spawned implicitly and unmanaged! It would be cool to find asyncio gurus interested in this very natural goal. Your help and ideas would be very much appreciated. best regards -- Valery -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Albert-Jan Roskam writes: > I find the following obscure (to me at least) use of type() useful > exactly for this "bag of attributes" use case: employee = type("Employee", (object,), {}) employee.name = "John Doe" employee.position = "Python programmer" You could write it as: class Employee: name = "Johh Doe" position = "Python programmer" It also makes it clear that `type()` returns a *class Employee*, not its instance (Employee()) and therefore name, position are class attributes. -- Akira -- https://mail.python.org/mailman/listinfo/python-list
try/exception - error block
Hi. I have a long running process, it generates calls to a separate py app. The py app appears to generate errors, as indicated in the /var/log/messages file for the abrtd daemon.. The errors are intermittent. So, to quickly capture all possible exceptions/errors, I decided to wrap the entire "main" block of the test py func in a try/exception block. This didn't work, as I'm not getting any output in the err file generated in the exception block. I'm posting the test code I'm using. Pointers/comments would be helpful/useful. the if that gets run is the fac1 logic which operates on the input packet/data.. elif (level=='collegeFaculty1'): #getClasses(url, college, termVal,termName,deptName,deptAbbrv) ret=getParseCollegeFacultyList1(url,content) Thanks. if __name__ == "__main__": # main app try: #college="asu" #url="https://webapp4.asu.edu/catalog"; #termurl="https://webapp4.asu.edu/catalog/TooltipTerms.ext"; #termVal=2141 # # get the input struct, parse it, determine the level # #cmd='cat /apps/parseapp2/asuclass1.dat' #print "cmd= "+cmd #proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) #content=proc.communicate()[0].strip() #print content #sys.exit() #s=getClasses(content) #print "arg1 =",sys.argv[0] if(len(sys.argv)<2): print "error\n" sys.exit() a=sys.argv[1] aaa=a # # data is coming from the parentApp.php #data has been rawurlencode(json_encode(t)) #-reverse/split the data.. #-do the fetch, #-save the fetched page/content if any #-create the returned struct #-echo/print/return the struct to the # calling parent/call # ##print urllib.unquote_plus(a).decode('utf8') #print "\n" #print simplejson.loads(urllib.unquote_plus(a)) z=simplejson.loads(urllib.unquote_plus(a)) ##z=simplejson.loads(urllib.unquote(a).decode('utf8')) #z=simplejson.loads(urllib2.unquote(a).decode('utf8')) #print "aa \n" print z #print "\n bb \n" # #-passed in # url=str(z['currentURL']) level=str(z['level']) cname=str(z['parseContentFileName']) # # need to check the contentFname # -should have been checked in the parentApp # -check it anyway, return err if required # -if valid, get/import the content into # the "content" var for the function/parsing # ##cmd='echo ${yolo_clientFetchOutputDir}/' cmd='echo ${yolo_clientParseInputDir}/' #print "cmd= "+cmd proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) cpath=proc.communicate()[0].strip() cname=cpath+cname #print "cn = "+cname+"\n" #sys.exit() cmd='test -e '+cname+' && echo 1' #print "cmd= "+cmd proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) c1=proc.communicate()[0].strip() if(not c1): #got an error - process it, return print "error in parse" # # we're here, no err.. got content # #fff= "sdsu2.dat" with open(cname,"r") as myfile: content=myfile.read() myfile.close() #-passed in #college="louisville" #url="http://htmlaccess.louisville.edu/classSchedule/"; #termVal="4138" #print "term = "+str(termVal)+"\n" #print "url = "+url+"\n" #jtest() #sys.exit() #getTerm(url,college,termVal) ret={} # null it out to start if (level=='rState'): #ret=getTerm(content,termVal) ret=getParseStates(content) elif (level=='stateCollegeList'): #getDepts(url,college, termValue,termName) ret=getParseStateCollegeList(url,content) elif (level=='collegeFaculty1'): #getClasses(url, college, termVal,termName,deptName,deptAbbrv) ret=getParseCollegeFacultyList1(url,content) elif (level=='collegeFaculty2'): #getClasses(url, college, termVal,termName,deptName,deptAbbrv) ret=getParseCollegeFacultyList2(content) # # the idea of this section.. we have the resulting # fetched content/page... # a={} status=False if(ret['status']==True): s=ascii_strip(ret['data']) if(((s.find("-1) or (s.find("-1)) and ((s.find("-1) or (s.find("-1)) and level=='classSectionDay'): status=True #print "herh" #sys.exit() # # build the returned struct # # a['Status']=True a['recCount']=ret['count'] a['data']=ret['data'] a['nextLevel']='' a['timestamp']='' a['macAddress']='' elif(ret['status']==False): a['Status']=False a['recCount']=0 a['data']='' a['nextLevel']='' a['timestamp']='' a['macAddress']='' res=urllib.quote(simplejson.dumps(a)) ##print res name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE) name=name.communicat
Re: Correct type for a simple “bag of attributes” namespace object
In article , Chris Angelico wrote: > On Sun, Aug 3, 2014 at 11:25 PM, Roy Smith wrote: > > in which case, I've said, "make Foos just like objects, except for, oh, > > never mind, there aren't any differences". But, in reality, the system > > bolted on the ability to have user-defined attributes without telling > > me. I don't think it's unreasonable to be surprised at that. > > I agree that this is slightly surprising. However, imagine if it were > the other way: > > class Foo(object): > x = 1 > def __init__(self): self.y = 2 > > These would throw errors, unless you explicitly disable __slots__ > processing. When there's two ways to do things and both would be > surprising, you pick the one that's going to be less of a surprise, or > surprising less often, and accept it. That doesn't mean it's not a > problem, but it's better than the alternative. > > ChrisA I'm not following you at all. What does "the other way" mean, and how would that cause the above to generate errors, and what does this have to do with __slots__? -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
In article , bruce wrote: > I'm posting the test code I'm using. Pointers/comments would be > helpful/useful. It would be really helpful if you could post a minimal code example which demonstrates the problem you're having. Leave out everything (including the commented-out code) which isn't necessary to demonstrate the behavior. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Peter Otten <__pete...@web.de>: > i. e. you have a per-class and per-instance memory consumption. The > latter is smaller, so with regards to memory consumption instantiating > only pays off when there is more than one employee. I've reached a point where I think classes are a superfluous OO concept. You only need objects. Python is close to that reality. The "class" keyword really creates a function that creates objects, and the objects' class membership is ignored in ducktyping. Classes may or may not save RAM, but that is rather a low-brow point of view toward OO. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
On 03/08/2014 15:39, Roy Smith wrote: In article , bruce wrote: I'm posting the test code I'm using. Pointers/comments would be helpful/useful. It would be really helpful if you could post a minimal code example which demonstrates the problem you're having. Leave out everything (including the commented-out code) which isn't necessary to demonstrate the behavior. How to go about this is at "Short, Self Contained, Correct (Compilable), Example" at http://sscce.org/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
On Mon, Aug 4, 2014 at 12:36 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> On Sun, Aug 3, 2014 at 11:25 PM, Roy Smith wrote: >> > in which case, I've said, "make Foos just like objects, except for, oh, >> > never mind, there aren't any differences". But, in reality, the system >> > bolted on the ability to have user-defined attributes without telling >> > me. I don't think it's unreasonable to be surprised at that. >> >> I agree that this is slightly surprising. However, imagine if it were >> the other way: >> >> class Foo(object): >> x = 1 >> def __init__(self): self.y = 2 >> >> These would throw errors, unless you explicitly disable __slots__ >> processing. When there's two ways to do things and both would be >> surprising, you pick the one that's going to be less of a surprise, or >> surprising less often, and accept it. That doesn't mean it's not a >> problem, but it's better than the alternative. >> >> ChrisA > > I'm not following you at all. What does "the other way" mean, and how > would that cause the above to generate errors, and what does this have > to do with __slots__? Fact #1: Some classes, like object, don't have a dictionary for arbitrary attributes. (I'll call this __slots__ mode, although it's not technically __slots__when it's a C-implemented class.) Fact #2: You can subclass such objects. There are two ways that this subclassing could be done. Either it maintains __slots__ mode, which means you can see every change (and going "class Foo(Bar): pass" will make an exact subclass of Bar with identical behaviour), or it drops __slots__ and adds a dictionary unless you explicitly reenable __slots__. >>> class Base(object): __slots__ = ('a', 'b', 'c') >>> class Deriv(Base): pass >>> Base().d = 1 Traceback (most recent call last): File "", line 1, in Base().d = 1 AttributeError: 'Base' object has no attribute 'd' >>> Deriv().d = 1 Python opted to go with the second behaviour: the subclass is not bound to the superclass's __slots__, but gets a dictionary unless it itself specifies __slots__ (in which case it gets all of them, parent's included): >>> class SlottedDeriv(Base): __slots__ = ('d', 'e', 'f') >>> SlottedDeriv().a = 1 >>> SlottedDeriv().d = 1 >>> SlottedDeriv().g = 1 Traceback (most recent call last): File "", line 1, in SlottedDeriv().g = 1 AttributeError: 'SlottedDeriv' object has no attribute 'g' The alternative would be for __slots__ to normally copy down, and to have to be explicitly removed - for "pass" to be actually equivalent to this: >>> class Deriv(Base): __slots__ = Base.__slots__ >>> Deriv().d = 1 Traceback (most recent call last): File "", line 1, in Deriv().d = 1 AttributeError: 'Deriv' object has no attribute 'd' and for some other syntax to do what "pass" currently does. That has the benefit of purity (it's easy to describe what happens, there's no magic going on), but at the expense of practicality (you'd have to explicitly de-slottify your classes before you can add functionality to them). And we know what the Zen of Python says about which of those takes priority. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
In article <87wqaplj8h@elektro.pacujo.net>, Marko Rauhamaa wrote: > I've reached a point where I think classes are a superfluous OO concept. > You only need objects. comp.lang.javascript is over that way --> -- https://mail.python.org/mailman/listinfo/python-list
python-list@python.org
I want to break a PKCS7 signature that contains data + signature into separate: raw data & detached PKCS7 signature in python. I can get the data fro the signature because the verification routine returns it, but how can I get the detached signature ? def verify_pkcs7(data_bio, signature_bio, cacert_bio, format=X509.FORMAT_PEM): sm_obj = SMIME.SMIME() st = X509.X509_Store() st.add_cert(X509.load_cert_string(cacert_bio)) sm_obj.set_x509_store(st) if format == X509.FORMAT_PEM: p7 = SMIME.load_pkcs7_bio(signature_bio) else: p7 = SMIME.PKCS7(m2.pkcs7_read_bio_der(signature_bio._ptr()), 1) sk = p7.get0_signers(X509.X509_Stack()) sm_obj.set_x509_stack(sk) try: v = sm_obj.verify(p7, data_bio) if v: print "Client signature verified." with open('file.rar', 'wb') as ff: ff.write(v) except Exception as e: print str(e) print "*** INVALID CLIENT MESSAGE SIGNATURE ***" My wild guess is that it one of these functions.. I base my assumption on the fact that M2Crypto is a wrapper over OpenSSL. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Roy Smith : > Marko Rauhamaa wrote: > >> I've reached a point where I think classes are a superfluous OO >> concept. You only need objects. > > comp.lang.javascript is over that way --> Thanks for the insight. I'm currently more absorbed by comp.lang.scheme, though. Now, Python is ducktyped. It is (rightly) considered bad taste to consult the datatype (class) of an object. Compare these two definitions: class Point: def __init__(self, x, y): self.x = x self.y = y def x(self): return self.x def y(self): return self.y and: class Object: pass def Point(x, y): self = Object() self.__dict__ = dict(x=lambda: x, y=lambda: y) return self For all practical purposes, the two definitions are identical even though the latter doesn't specify any class. Inheritance needs to be addressed, but that can be taken care of without classes as well. Obviously, Python's syntax makes it convenient to deal with classes, and there's no practical downside to it. However, conceptually, classes are unnecessary baggage and at odds with ducktyping. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
On 03/08/2014 15:29, bruce wrote: [snipped to bits] Please see this http://sscce.org/ as already requested on the main mailing list. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
In article , Mark Lawrence wrote: > How to go about this is at "Short, Self Contained, Correct (Compilable), > Example" at http://sscce.org/ It says there, "most readers will stop reading by 100 lines of code". I guess I have a short attention span relative to "most readers", because my tl;dnr threshold is a lot shorter than that. The other advantage to coming up with a minimal example is that often the exercise of cutting your problem down to a minimal example is enough to allow you to figure it out for yourself :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
On Mon, Aug 4, 2014 at 2:27 AM, Roy Smith wrote: > It says there, "most readers will stop reading by 100 lines of code". I > guess I have a short attention span relative to "most readers", because > my tl;dnr threshold is a lot shorter than that. "by" 100 lines includes everyone who stops at 10 lines, 40 lines, or 80 lines, too. What it means is that if you post >100 lines of code, you've lost most of your audience. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Tcl/Tk alpha channel bug on OSX Mavericks is fixeded, but how/when can I use the fix?
Hello to all, My OSX is 10.9 Mavericks. I use Python 3.4. with ActiveTcl 8.5.15.1. I was struggling with several approaches to using PNG files with alpha channel (partial transparency). Following my question in this newsgroup (see the thread "Python 3.4.1 installer on Mac links Python to old Tcl/Tk" and all the kind suggestions what to do), I have tried several things: - Used the package Img with Tk 8.5 - Used PIL to load the file and then converting it by ImageTk.PhotoImage. - Installed MacPorts' version of Python 3.4 with Tk 8.6 But none of the approaches were able to show corretly my partially transparent image - the image was loaded and shown but the alpha channel was reduced to binary transparency. I think that it is because of this problem in Mavericks: http://core.tcl.tk/tk/tktview?name=99b84e49ff The above link says that it has been solved in Tcl/Tk. But: what does it mean for me - a Python user? Can anyone say when a version containing the above bug fix will be available in a form of an installable package that I can use with Python on a Mac? How can I know that such a package is avaibale? Or can anyone point me to some instructions how could I compile Tcl/Tk for myslef and (more importanty) how to install the result of that compilation so that Python 3.4 recognizes that it should use that package (and not three other installations of Tcl/Tk in my computer)? Thanks, Peter -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
On 02/08/2014 20:58, Ben Finney wrote: Steven D'Aprano writes: If you need instances which carry state, then object is the wrong class. Right. The ‘types’ module provides a SimpleNamespace class for the common “bag of attributes” use case:: >>> import types >>> foo = types.SimpleNamespace() >>> foo.x = 3 >>> foo namespace(x=3) https://docs.python.org/3/library/types.html#types.SimpleNamespace> A slight aside but from the link "SimpleNamespace may be useful as a replacement for class NS: pass." I'm not quite sure how that class definition is meant to read, other than guessing that NS stands for NameSpace, any ideas? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple “bag of attributes” namespace object
In article , Mark Lawrence wrote: > On 02/08/2014 20:58, Ben Finney wrote: > > Steven D'Aprano writes: > > > >> If you need instances which carry state, then object is the wrong > >> class. > > > > Right. The âtypesâ module provides a SimpleNamespace class for the > > common âbag of attributesâ use case:: > > > > >>> import types > > >>> foo = types.SimpleNamespace() > > >>> foo.x = 3 > > >>> foo > > namespace(x=3) > > > > https://docs.python.org/3/library/types.html#types.SimpleNamespace> > > > > A slight aside but from the link "SimpleNamespace may be useful as a > replacement for class NS: pass." I'm not quite sure how that class > definition is meant to read, other than guessing that NS stands for > NameSpace, any ideas? Trivia: argparse.ArgumentParser().parse_args() returns a Namespace. -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
chris.. my bad.. I wasnt intending to mail you personally. Or I wouldn't have inserted the "thanks guys"! > thanks guys... > > but in all that.. no one could tell me .. why i'm not getting any > errs/exceptions in the err file which gets created on the exception!!! > > but thanks for the information on posting test code! Don't email me privately - respond to the list :) Also, please don't top-post. ChrisA On Sun, Aug 3, 2014 at 10:29 AM, bruce wrote: > Hi. > > I have a long running process, it generates calls to a separate py > app. The py app appears to generate errors, as indicated in the > /var/log/messages file for the abrtd daemon.. The errors are > intermittent. > > So, to quickly capture all possible exceptions/errors, I decided to > wrap the entire "main" block of the test py func in a try/exception > block. > > This didn't work, as I'm not getting any output in the err file > generated in the exception block. > > I'm posting the test code I'm using. Pointers/comments would be > helpful/useful. > > > the if that gets run is the fac1 logic which operates on the input > packet/data.. > elif (level=='collegeFaculty1'): > #getClasses(url, college, termVal,termName,deptName,deptAbbrv) > ret=getParseCollegeFacultyList1(url,content) > > > Thanks. > > if __name__ == "__main__": > # main app > > try: > #college="asu" > #url="https://webapp4.asu.edu/catalog"; > #termurl="https://webapp4.asu.edu/catalog/TooltipTerms.ext"; > > > #termVal=2141 > # > # get the input struct, parse it, determine the level > # > > #cmd='cat /apps/parseapp2/asuclass1.dat' > #print "cmd= "+cmd > #proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) > #content=proc.communicate()[0].strip() > #print content > #sys.exit() > > #s=getClasses(content) > > #print "arg1 =",sys.argv[0] > if(len(sys.argv)<2): > print "error\n" > sys.exit() > > a=sys.argv[1] > aaa=a > > # > # data is coming from the parentApp.php > #data has been rawurlencode(json_encode(t)) > #-reverse/split the data.. > #-do the fetch, > #-save the fetched page/content if any > #-create the returned struct > #-echo/print/return the struct to the > # calling parent/call > # > > ##print urllib.unquote_plus(a).decode('utf8') > #print "\n" > #print simplejson.loads(urllib.unquote_plus(a)) > z=simplejson.loads(urllib.unquote_plus(a)) > ##z=simplejson.loads(urllib.unquote(a).decode('utf8')) > #z=simplejson.loads(urllib2.unquote(a).decode('utf8')) > > #print "aa \n" > print z > #print "\n bb \n" > > # > #-passed in > # > url=str(z['currentURL']) > level=str(z['level']) > cname=str(z['parseContentFileName']) > > > # > # need to check the contentFname > # -should have been checked in the parentApp > # -check it anyway, return err if required > # -if valid, get/import the content into > # the "content" var for the function/parsing > # > > ##cmd='echo ${yolo_clientFetchOutputDir}/' > cmd='echo ${yolo_clientParseInputDir}/' > #print "cmd= "+cmd > proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) > cpath=proc.communicate()[0].strip() > > cname=cpath+cname > #print "cn = "+cname+"\n" > #sys.exit() > > > cmd='test -e '+cname+' && echo 1' > #print "cmd= "+cmd > proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) > c1=proc.communicate()[0].strip() > > if(not c1): > #got an error - process it, return > print "error in parse" > > # > # we're here, no err.. got content > # > > #fff= "sdsu2.dat" > with open(cname,"r") as myfile: > content=myfile.read() > myfile.close() > > > #-passed in > #college="louisville" > #url="http://htmlaccess.louisville.edu/classSchedule/"; > #termVal="4138" > > > #print "term = "+str(termVal)+"\n" > #print "url = "+url+"\n" > > #jtest() > #sys.exit() > > #getTerm(url,college,termVal) > > > ret={} # null it out to start > if (level=='rState'): > #ret=getTerm(content,termVal) > ret=getParseStates(content) > > elif (level=='stateCollegeList'): > #getDepts(url,college, termValue,termName) > ret=getParseStateCollegeList(url,content) > > elif (level=='collegeFaculty1'): > #getClasses(url, college, termVal,termName,deptName,deptAbbrv) > ret=getParseCollegeFacultyList1(url,content) > > elif (level=='collegeFaculty2'): > #getClasses(url, college, termVal,termName,deptName,deptAbbrv) > ret=getParseCollegeFacultyList2(content) > > > > # > # the idea of this section.. we have the resulting > # fetched content/page... > # > > a={} > status=False > if(ret['status']==True): > >
Re: try/exception - error block
On Mon, Aug 4, 2014 at 3:52 AM, bruce wrote: > chris.. my bad.. I wasnt intending to mail you personally. > > Or I wouldn't have inserted the "thanks guys"! > > >> thanks guys... >> >> but in all that.. no one could tell me .. why i'm not getting any >> errs/exceptions in the err file which gets created on the exception!!! >> >> but thanks for the information on posting test code! > > Don't email me privately - respond to the list :) > > Also, please don't top-post. > > ChrisA I figured that was an accident :) Now if you could just master the interleaved posting style, that would be a great help. https://en.wikipedia.org/wiki/Posting_style#Interleaved_style ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python and IDEs [was Re: Python 3 is killing Python]
Am 03.08.2014 02:04, schrieb Gregory Ewing: MRAB wrote: RISC OS didn't have a menu bar at the top of each window either; its menus were all pop-up. You didn't have to keep flicking the mouse at all! The main reason for having a menu bar is discoverability. The idea is that you can browse through the menus and get a feel for what commands are potentially available to you. That's not so easy to do when everything is hidden in contextual menus. This was not a problem with the RISC OS menu concept. Only some menu items were depending on the mouse position. Actually the menu items were easier to discover as the non-applicable items were not hidden but greyed out. Regards, Dietmar -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
On 03/08/14 18:52, bruce wrote: but in all that.. no one could tell me .. why i'm not getting any errs/exceptions in the err file which gets created on the exception!!! Does the file actually get created? Do you see the print statement output - are they what you expect? Did you try the things Steven suggested. except Exception, e: print e print "pycolFac1 - error!! \n"; name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE) name=name.communicate()[0].strip() name=name.replace("-","_") This is usually a bad idea. You are using name for the process and its output. Use more names... What about: uuid=subprocess.Popen('uuidgen -t',shell=True,stdout=subprocess.PIPE) output=uuid.communicate()[0].strip() name=output.replace("-","_") name2="/home/ihubuser/parseErrTest/pp_"+name+".dat" This would be a good place to insert a print print name2 ofile1=open(name2,"w+") Why are you using w+ mode? You are only writing. Keep life as simple as possible. ofile1.write(e) e is quite likely to be empty ofile1.write(aaa) Are you sure aaa exists at this point? Remember you are catching all errors so if an error happens prior to aaa being created this will fail. ofile1.close() You used the with form earlier, why not here too. It's considered better style... Some final comments. 1) You call sys.exit() several times inside the try block. sys.exit will not be caught by your except block, is that what you expect?. 2) The combination of confusing naming of variables, reuse of names and poor code layout and excessive commented code makes it very difficult to read your code. That makes it hard to figure out what might be going on. - Use sensible variable names not a,aaa,z, etc - use 3 or 4 level indentation not 2 - use a version control system (RCS,CVS, SVN,...) instead of commenting out big blocks - use consistent code style eg with f as ... or open(f)/close(f) but not both - use the os module (and friends) instead of subprocess if possible 3) Have you tried deleting all the files in the /home/ihubuser/parseErrTest/ folder and starting again, just to be sure that your current code is actually producing the empty files? 4) You use tmpParseDir in a couple of places but I don't see it being set anywhere? That's about the best I can offer based on the information available. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
On Sunday, August 3, 2014 10:39:19 PM UTC+8, Roy Smith wrote: > In article , > > bruce wrote: > > > > > I'm posting the test code I'm using. Pointers/comments would be > > > helpful/useful. > > > > It would be really helpful if you could post a minimal code example > > which demonstrates the problem you're having. Leave out everything > > (including the commented-out code) which isn't necessary to demonstrate > > the behavior. Oh, I remember I was working with a professional programer who agreed with me that a subroutine or function longer than 80 lines in C/C++/PASCAL/Fortan, etc. must explain the reason in the commented preamble claearly for other programer to check the necessity of the unusual length. -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
Hi Alan. Yep, the err file in the exception block gets created. and the weird thing is it matches the time of the abrtd information in the /var/log/messages log.. Just nothing in the file! On Sun, Aug 3, 2014 at 4:01 PM, Alan Gauld wrote: > On 03/08/14 18:52, bruce wrote: > >>> but in all that.. no one could tell me .. why i'm not getting any >>> errs/exceptions in the err file which gets created on the exception!!! > > > Does the file actually get created? > Do you see the print statement output - are they what you expect? > > Did you try the things Steven suggested. > > >>>except Exception, e: >>> print e >>> print "pycolFac1 - error!! \n"; >>> name=subprocess.Popen('uuidgen -t', >>> shell=True,stdout=subprocess.PIPE) >>> name=name.communicate()[0].strip() >>> name=name.replace("-","_") > > > This is usually a bad idea. You are using name for the process and its > output. Use more names... > What about: > > uuid=subprocess.Popen('uuidgen -t',shell=True,stdout=subprocess.PIPE) > output=uuid.communicate()[0].strip() > name=output.replace("-","_") > >>> name2="/home/ihubuser/parseErrTest/pp_"+name+".dat" > > > This would be a good place to insert a print > > print name2 > >>> ofile1=open(name2,"w+") > > > Why are you using w+ mode? You are only writing. > Keep life as simple as possible. > >>> ofile1.write(e) > > > e is quite likely to be empty > >>> ofile1.write(aaa) > > > Are you sure aaa exists at this point? Remember you are catching all errors > so if an error happens prior to aaa being created this will > fail. > >>> ofile1.close() > > > You used the with form earlier, why not here too. > It's considered better style... > > Some final comments. > 1) You call sys.exit() several times inside > the try block. sys.exit will not be caught by your except block, > is that what you expect?. > > 2) The combination of confusing naming of variables, > reuse of names and poor code layout and excessive commented > code makes it very difficult to read your code. > That makes it hard to figure out what might be going on. > - Use sensible variable names not a,aaa,z, etc > - use 3 or 4 level indentation not 2 > - use a version control system (RCS,CVS, SVN,...) instead > of commenting out big blocks > - use consistent code style > eg with f as ... or open(f)/close(f) but not both > - use the os module (and friends) instead of subprocess if possible > > 3) Have you tried deleting all the files in the > /home/ihubuser/parseErrTest/ folder and starting again, > just to be sure that your current code is actually > producing the empty files? > > 4) You use tmpParseDir in a couple of places but I don't > see it being set anywhere? > > > That's about the best I can offer based on the > information available. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does not pprint work?
With the way you have imported, you trying to use the module pprint instead of the function pprint.pprint. You need to use pprint.pprint or you need to import as: from pprint import pprint if you want to use the shorter form. On Tuesday, July 22, 2014 5:42:02 PM UTC-4, fl wrote: > Hi, > > > > I read web tutorial at: > > > > http://nedbatchelder.com/blog/201308/names_and_values_making_a_game_board.html > > > > I enter the example lines of that website: > > > > > > import pprint > > board = [ [0]*8 ] * 8 > > pprint(board) > > > > > > It echos error with Python 2.7: > > > > Traceback (most recent call last): > > File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\framework\scriptutils.py", > > line 323, in RunScript > > debugger.run(codeObject, __main__.__dict__, start_stepping=0) > > File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\debugger\__init__.py", > > line 60, in run > > _GetCurrentDebugger().run(cmd, globals,locals, start_stepping) > > File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\debugger\debugger.py", > > line 655, in run > > exec cmd in globals, locals > > File "C:\cygwin64\home\Jeff\Python_lesson\ppn.py", line 1, in > > import pprint > > TypeError: 'module' object is not callable > > > > It has similar error with Python 3.4.1. > > > > > > Why does pprint not work? > > > > > > Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Tcl/Tk alpha channel bug on OSX Mavericks is fixeded, but how/when can I use the fix?
On 8/3/14, 1:24 PM, Peter Tomcsanyi wrote: I think that it is because of this problem in Mavericks: http://core.tcl.tk/tk/tktview?name=99b84e49ff The above link says that it has been solved in Tcl/Tk. But: what does it mean for me - a Python user? Can anyone say when a version containing the above bug fix will be available in a form of an installable package that I can use with Python on a Mac? How can I know that such a package is avaibale? Or can anyone point me to some instructions how could I compile Tcl/Tk for myslef and (more importanty) how to install the result of that compilation so that Python 3.4 recognizes that it should use that package (and not three other installations of Tcl/Tk in my computer)? New releases of Tcl/Tk 8.5 and 8.6 are due out soon; right now they are undergoing final testing as betas/release candidates. If you are using the standard Python installer from Python.org, then you can wait for ActiveTcl to be updated (not sure of their release schedule) or you can download the source tarballs for Tcl and Tk when they are released, untar them to a specific directory, cd to the directory, and run these commands: make -C $insert_tcl_dirname_here/macosx make -C $insert_tk_dirname_here/macosx and then sudo make -C $insert_tcl_dirname_here/macosx install sudo make -C $insert_tk_dirname_here/macosx install This will install the updated version of Tcl and Tk in /Library/Frameworks, and Python should pick them up. These instructions assume you have Apple's developer tools installed and are at least minimally comfortable using the command line. If you have installed Python via some other process, cf. MacPorts or Homebrew, you'll have to wait until they pick up the new versions of Tcl and Tk and follow their instructions for upgrading. Hope this helps, Kevin -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
bruce wrote: > Hi. > > I have a long running process, it generates calls to a separate py > app. The py app appears to generate errors, as indicated in the > /var/log/messages file for the abrtd daemon.. The errors are > intermittent. > > So, to quickly capture all possible exceptions/errors, I decided to > wrap the entire "main" block of the test py func in a try/exception > block. > > This didn't work, as I'm not getting any output in the err file > generated in the exception block. > > I'm posting the test code I'm using. Pointers/comments would be > helpful/useful. > try: [...] > except Exception, e: > print e > print "pycolFac1 - error!! \n"; > name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE) > name=name.communicate()[0].strip() > name=name.replace("-","_") > name2="/home/ihubuser/parseErrTest/pp_"+name+".dat" > ofile1=open(name2,"w+") > ofile1.write(e) You can't write exceptions to the file, just strings. Try print >> ofile1, e or ofile1.write(str(e) + "\n") instead of the above line. > ofile1.write(aaa) > ofile1.close() > > sys.exit() -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does not pprint work?
On 03/08/2014 22:34, robkote...@gmail.com wrote: [snipped to bits] Please don't top post, further would you read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
cmd.exe on WIndows - problem with displaying some Unicode characters
Hi, as OO programming exercise, I'm trying to port to Python one of my favorite game from early'90 (Atari 65XL/XE) - Kolony (here's video from original version on C64 https://www.youtube.com/watch?v=UFycYOp2cbE, and here's video from modern rewritten (for Atari emulators) version: Kolony 2106 https://www.youtube.com/watch?v=eX20Qqqm5eg - you get the idea? ;-)). OO Design is one thing, but I want to make it look as near as possible to the original (those windows-like menus in console window). I tried to use 'standard' Unicode characters (I can see that most of my Windows monospaced fonts have them) to draw frame around menu. Something like this: ┌──╖ │ Construction ║ │ Production ║ │ Research ║ │ Exploration ║ ├··╢ │ Next turn║ ╘══╝ (I like the look of double lines on right and at the bottom) But when I try to print those characters, I get an error: | Traceback (most recent call last): | File "E:\Moje dokumenty\python\kolony\menu.py", line 14, in | """ | File "C:\Python34\lib\encodings\cp852.py", line 19, in encode | return codecs.charmap_encode(input,self.errors,encoding_map)[0] | UnicodeEncodeError: 'charmap' codec can't encode character '\u2556' in position 1 | 6: character maps to Now I know what that means. Code page that my cmd.exe is using (852) doesn't have "╖", "╘", "╢" and "·" symbols. Changing code page to Unicode (65001) doesn't really help, because all is messed up: ┌──╖ │ Construction ║ │ Production ║ │ Research ║ │ Exploration ║ ├··╢ │ Next turn║ ╘══╝ �·╢ │ Next turn║ ╘══╝ ��╝ ��═╝ ═╝ (I believe that's cmd.exe bug with Unicode support, not Python fault) Before I drop entirely this idea of using double lines on right and bottom edges, and make it look like this ┌──┐ │ Construction │ ├--┤ │ Next turn│ └──┘ I have to ask - is there a way to make that original concept work? I know, that CP437 has symbols "╖", "╢" and "╘", but does not have polish letters - and I need to display them too. I also know, that cmd.exe can display those Unicode characters (by copy/paste them in command line or by listing filenames containing that characters), no matter what CP is set. How does it manage to do it? Can I exploit that writing my Python program? Wiktor -- Best regards, Wiktor Matuszewski 'py{}@wu{}em.pl'.format('wkm', 'ka') -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
On 8/3/2014 10:51 AM, Marko Rauhamaa wrote: Peter Otten <__pete...@web.de>: i. e. you have a per-class and per-instance memory consumption. The latter is smaller, so with regards to memory consumption instantiating only pays off when there is more than one employee. I've reached a point where I think classes are a superfluous OO concept. You only need objects. Python is close to that reality. The "class" keyword really creates a function that creates objects, and the objects' class membership is ignored in ducktyping. The object class is used to implement duck typing. It is the delegation of operations to class instance methods that makes extensible duck typing possible. 'a + b' is equivalent to type(a).__add__(a, b) -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On Mon, Aug 4, 2014 at 8:52 AM, Wiktor wrote: > I have to ask - is there a way to make that original concept work? I know, > that CP437 has symbols "╖", "╢" and "╘", but does not have polish letters - > and I need to display them too. Yeah, that's exactly the problem with codepages :) The best way to do it is to use the Unicode codepage, but cmd.exe just plain has issues. There are underlying Windows APIs for displaying text that have problems with astral characters (I think that's what it is), so ultimately, you're largely stuck. One option would be to render the whole thing graphically, abandoning cmd.exe altogether. That would be how a lot of telnet and SSH clients will do the work. Get a proper Unicode-supporting toolkit (Tkinter has issues with astral characters too, AIUI), and yes, you'll have to do a lot of work yourself. Or maybe, grab an actual telnet client, and write this as a socket server. I'd be delighted to help you with that option - I'm a MUDder and have spent innumerable dev hours on telnet clients! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On 03/08/2014 23:52, Wiktor wrote: Hi, as OO programming exercise, I'm trying to port to Python one of my favorite game from early'90 (Atari 65XL/XE) - Kolony (here's video from original version on C64 https://www.youtube.com/watch?v=UFycYOp2cbE, and here's video from modern rewritten (for Atari emulators) version: Kolony 2106 https://www.youtube.com/watch?v=eX20Qqqm5eg - you get the idea? ;-)). OO Design is one thing, but I want to make it look as near as possible to the original (those windows-like menus in console window). I tried to use 'standard' Unicode characters (I can see that most of my Windows monospaced fonts have them) to draw frame around menu. Something like this: ┌──╖ │ Construction ║ │ Production ║ │ Research ║ │ Exploration ║ ├··╢ │ Next turn║ ╘══╝ (I like the look of double lines on right and at the bottom) But when I try to print those characters, I get an error: | Traceback (most recent call last): | File "E:\Moje dokumenty\python\kolony\menu.py", line 14, in | """ | File "C:\Python34\lib\encodings\cp852.py", line 19, in encode | return codecs.charmap_encode(input,self.errors,encoding_map)[0] | UnicodeEncodeError: 'charmap' codec can't encode character '\u2556' in position 1 | 6: character maps to Now I know what that means. Code page that my cmd.exe is using (852) doesn't have "╖", "╘", "╢" and "·" symbols. Changing code page to Unicode (65001) doesn't really help, because all is messed up: ┌──╖ │ Construction ║ │ Production ║ │ Research ║ │ Exploration ║ ├··╢ │ Next turn║ ╘══╝ �·╢ │ Next turn║ ╘══╝ ��╝ ��═╝ ═╝ (I believe that's cmd.exe bug with Unicode support, not Python fault) Before I drop entirely this idea of using double lines on right and bottom edges, and make it look like this ┌──┐ │ Construction │ ├--┤ │ Next turn│ └──┘ I have to ask - is there a way to make that original concept work? I know, that CP437 has symbols "╖", "╢" and "╘", but does not have polish letters - and I need to display them too. I also know, that cmd.exe can display those Unicode characters (by copy/paste them in command line or by listing filenames containing that characters), no matter what CP is set. How does it manage to do it? Can I exploit that writing my Python program? Wiktor There are multiple known problems with cmd.exe and unicode. A solution might be to use powershell, but windows being windows who really knows? :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On 2014.08.03 18:08, Chris Angelico wrote: > The best way to do it is to use the Unicode codepage, but cmd.exe just > plain has issues. There are underlying Windows APIs for displaying > text that have problems with astral characters (I think that's what it > is), so ultimately, you're largely stuck. That is not quite true. The terminal has these issues, not the shell. Using cp65001 does make Unicode in a Windows terminal possible, but using a better terminal[1] makes it almost perfect (my experience has been that input can be difficult, but output works well). I personally have used an IRC bot written in Python with logging output containing Unicode characters that display just fine (both locally and over SSH). [1] I recommend ConEmu: https://code.google.com/p/conemu-maximus5/ -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On 04/08/2014 00:25, Andrew Berg wrote: On 2014.08.03 18:08, Chris Angelico wrote: The best way to do it is to use the Unicode codepage, but cmd.exe just plain has issues. There are underlying Windows APIs for displaying text that have problems with astral characters (I think that's what it is), so ultimately, you're largely stuck. That is not quite true. The terminal has these issues, not the shell. Using cp65001 does make Unicode in a Windows terminal possible, but using a better terminal[1] makes it almost perfect (my experience has been that input can be difficult, but output works well). I personally have used an IRC bot written in Python with logging output containing Unicode characters that display just fine (both locally and over SSH). [1] I recommend ConEmu: https://code.google.com/p/conemu-maximus5/ *facepalm* forgot all about ConEmu, but then I only use it on a daily basis :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On Mon, Aug 4, 2014 at 9:25 AM, Andrew Berg wrote: > On 2014.08.03 18:08, Chris Angelico wrote: >> The best way to do it is to use the Unicode codepage, but cmd.exe just >> plain has issues. There are underlying Windows APIs for displaying >> text that have problems with astral characters (I think that's what it >> is), so ultimately, you're largely stuck. > That is not quite true. The terminal has these issues, not the shell. Using > cp65001 does make Unicode in a Windows terminal possible, but using a better > terminal[1] makes it almost perfect (my experience has been that input can be > difficult, but output works well). I personally have used an IRC bot written > in > Python with logging output containing Unicode characters that display just > fine > (both locally and over SSH). > > [1] I recommend ConEmu: https://code.google.com/p/conemu-maximus5/ Sorry, yeah, my terminology was sloppy. It's not technically cmd.exe, but the default console. I just played around with a CP-437 decode of everything 128-255, rendered in various different fonts, all using my MUD client on Windows. (For what it's worth, it renders using GTK2 and Pango. But I suspect this is more a font issue than a display engine one.) Most fonts had those little boxes-with-numbers for most of the line drawing characters, but Lucida Sans Unicode, Meiryo, and Tahoma all managed to display all the characters. However, I wasn't able to visually distinguish between the single-line and double-line characters, so you may want to play around with it a bit more. Your simplest solution may still be to abandon those double lines and just go with single. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On Mon, Aug 4, 2014 at 9:39 AM, Chris Angelico wrote: > I just played around with a CP-437 decode of everything 128-255, > rendered in various different fonts, all using my MUD client on > Windows. (For what it's worth, it renders using GTK2 and Pango. But I > suspect this is more a font issue than a display engine one.) Most > fonts had those little boxes-with-numbers for most of the line drawing > characters, but Lucida Sans Unicode, Meiryo, and Tahoma all managed to > display all the characters. However, I wasn't able to visually > distinguish between the single-line and double-line characters, so you > may want to play around with it a bit more. Hmm. Actually... I'd mucked up my CP-437 decode. For instance, I'd represented 0xC8 as U+251D BOX DRAWINGS VERTICAL LIGHT AND RIGHT HEAVY, where it's actually U+255A BOX DRAWINGS DOUBLE UP AND RIGHT. When I get the right characters, the default fonts on Windows work just fine. (There's still the issue that the supposedly "HEAVY" lines are coming out with the exact same thickness as the "LIGHT" lines, but that's unlikely to bother you as you don't need those characters.) So, conclusion: If you do the drawing yourself, all those characters are fine. Go ahead and use them! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Marko Rauhamaa wrote: > I've reached a point where I think classes are a superfluous OO concept. > You only need objects. I don't know whether "superfluous" is correct, but they certainly are *optional*. There are at least two types of object oriented programming: class-bases, and prototype-based. Java, Python, Ruby, etc. are all class-based. The only example of a prototype-based OOP language I know of is Javascript, and even that has recently gained the ability to define classes. I don't know enough about prototyped OOP to really give a definitive answer, but I believe that the popularity of class-based OOP is because there is a huge body of theory on types, which makes it easier for compiler designers to reason about classes than prototypes. For example, if Haskell introduces OOP (and it may already have, I don't know) I would expect it to be class-based. Also, the first OOP languages (Simula and, especially, Smalltalk) are class-based, and people tend to copy what's been done before and what they're familiar with. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On 8/3/2014 4:25 PM, Andrew Berg wrote: On 2014.08.03 18:08, Chris Angelico wrote: The best way to do it is to use the Unicode codepage, but cmd.exe just plain has issues. There are underlying Windows APIs for displaying text that have problems with astral characters (I think that's what it is), so ultimately, you're largely stuck. That is not quite true. The terminal has these issues, not the shell. Using cp65001 does make Unicode in a Windows terminal possible, but using a better terminal[1] makes it almost perfect (my experience has been that input can be difficult, but output works well). I personally have used an IRC bot written in Python with logging output containing Unicode characters that display just fine (both locally and over SSH). [1] I recommend ConEmu: https://code.google.com/p/conemu-maximus5/ I will be reading more about conemu, thanks for the reference. http://bugs.python.org/issue1602 describes 7 years worth of discussion of the problems with the console/terminal used by default by cmd.exe and other Windows command line programs, versus Python. The recent insights in the last couple weeks have given me hope that Python might be able to be fixed to work properly with the default Windows console at long last... at least for non-astral characters (I'm not sure whether or not the Windows console supports non-BMP characters). For this OP problem, it is mostly a matter of finding a fixed-width font that supports the box drawing characters and the Polish characters that are desired. Lucida Console has a fair repertoire, and Consolas has a fair repertoire, in the fixed-width font arena. There may be others, documented on Polish language web sites that I wouldn't know about, and I don't know enough Polish to be sure those I mentioned suffice. And then, the workarounds mentioned in the above-referenced bug or on the GitHub or PyPi sites mentioned should provide any needed additional solutions... and hopefully something along this line finally integrated into Python so that it can finally be said that Python supports Unicode properly on Windows (or at least as properly as Windows allows... but it is pretty clear that Windows supports Unicode, even for the console, using different APIs that Python is presently using, and that mismatch between APIs is really the source of the problems with using Unicode in Python on Windows). Glenn -- https://mail.python.org/mailman/listinfo/python-list
Python crashing when script is running against live system
Hi, ALL, I'm working on the script that should starting from the given directory enumerate all directories and files underneath and calculates the hash value of such file. It works fine when I start it from some particular directory, but when I give the "C:\" it crashes python. The last thing the script processes is the file in cmdcons directory, which is AFAIU from the ComboFix run. I have Windows XP SP3 with python 2.7. Now, I'm confused. My questions are: 1. Am I able to catch such situation and see where the crash occur? 2. If not, what is the proper way to debug such situation? I am getting the window saying that python.exe has encountered a problem and needs to close. The last file processed was "c:\cmdcons\dpti20.sy_". What strange about it that when I run the script under debugger (I'm using winpdb), it is running further and crashing on some other file. Thank you for any help tracking down the issue. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python crashing when script is running against live system
On Mon, Aug 4, 2014 at 10:20 AM, Igor Korot wrote: > I'm working on the script that should starting from the given > directory enumerate all directories and files underneath and > calculates the hash value of such file. > It works fine when I start it from some particular directory, but when > I give the "C:\" it crashes python. > > The last thing the script processes is the file in cmdcons directory, > which is AFAIU from the ComboFix run. > I have Windows XP SP3 with python 2.7. > > Now, I'm confused. > My questions are: > > 1. Am I able to catch such situation and see where the crash occur? > 2. If not, what is the proper way to debug such situation? Great. A Heisenbug. These are so much fun... Okay. Here's how I'd try to track it down. 1) Sort the directory listings before you start iterating, to ensure that you're processing the files in a consistent order. Also, be sure you're flushing to disk every time you produce a line of output - you can probably do this by recording everything to stderr, which is generally unbuffered. 2) Do a dry run. Run the script exactly the same as it does when it crashes, but comment out the actual file reading and hash calculation. This will almost certainly succeed. 3) Using the output of step 2, make yourself an enumerated text file with all the files that the script would hash. 4) Do a half-dry run. Have the script alternate between doing the actual hashing, and just skipping it and pretending it did. 5) Do the other-half-dry run - as above but switch whether it hashes or skips the first one. Ideally, your output from step 2 should be something like this: 1: c:\config.sys 2: c:\msdos.sys 3: c:\hiberfile.sys 4: c:\cmdcons\dpti20.sy_ ... etc etc etc ... Your step 4 and 5 runs should then be doing (either way around) c:\config.sys and c:\hiberfile.sys, and c:\msdos.sys and c:\cmdcons\dpti20.sy_. That'll give you valuable information. I can imagine a few different crash scenarios from the half-and-half runs: * One of them might crash on the same file that the full run crashed on, which suggests that that file has an issue. * They might both crash, roughly twice as far into the list, which suggests that it's a memory leak or something that crashes after doing a certain amount of hashing. * They might crash at some completely different location, in which case you'd have to look for clues there. Another way to get some more info out of it would be to add a few seconds' sleep after each file. You should be able to count off the files by time, which will give additional hints. But try the other way first, as this will potentially take a looong time. There are a few possibilities to look out for - RAM fault, hard drive issue, stuff like that - which would be a REAL pain, but if it's not that kind of thing, it's probably a crash in the hashing code. That would at least be a Mandelbug. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
In article <53ded02e$0$29980$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > Marko Rauhamaa wrote: > > > I've reached a point where I think classes are a superfluous OO concept. > > You only need objects. > > I don't know whether "superfluous" is correct, but they certainly are > *optional*. There are at least two types of object oriented programming: > class-bases, and prototype-based. Java, Python, Ruby, etc. are all > class-based. The only example of a prototype-based OOP language I know of > is Javascript, and even that has recently gained the ability to define > classes. > > I don't know enough about prototyped OOP to really give a definitive answer, > but I believe that the popularity of class-based OOP is because there is a > huge body of theory on types, which makes it easier for compiler designers > to reason about classes than prototypes. For example, if Haskell introduces > OOP (and it may already have, I don't know) I would expect it to be > class-based. Also, the first OOP languages (Simula and, especially, > Smalltalk) are class-based, and people tend to copy what's been done before > and what they're familiar with. The first code I ever saw which had any OO concepts was the Unix kernel I/O drivers. The whole thing was written in C. It essentially implemented something we would recognize today as static class inheritance with polymorphic method dispatch. Each driver was required to implement a number of functions (read, write, open, etc), with specified signatures and contracts. Effectively, the all subclassed a (mythical) BaseIODriver. There was a big table where the rows were the different drivers (indexed by major device number), and the columns were the different methods. That was in the early 70s. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Steven D'Aprano wrote: I don't know enough about prototyped OOP to really give a definitive answer, but I believe that the popularity of class-based OOP is because there is a huge body of theory on types, I think it's more than that. I thought about prototype-based OO systems in some depth a while ago, and I came to the conclusion that the supposed simplification that comes from not having classes is an illusion. It *seems* simpler to have only one kind of object and allow any object to inherit from any other. But actually using it in such an undisciplined way would lead to chaos. In practice, you end up with two groups of objects, with one being used in a class-like way, and the other in an instance-like way. So you effectively have classes anyway, whether you call them that or not. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
CodeSkulptor
I am very new to Python. Right now I am using two tools. I am trying the tutorials at codecademy.com which is walking me through it pretty slow. The second thing I am doing is using codeskulptor to try out a few things I have learned at codecademy. I am getting a mismatch. The example I am working on is: (codecademy) from datetime import datetime now = datetime.now() current_year = now.year current_month = now.month current_day = now.day Putting that in codeskulptor gets Line 4: ImportError: No module named datetime The commands I am used to in codeskulptor is something like import random import math import simplegui import datetime < Changing this to import instead of from datetime import datetime still doesn't work. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Terry Reedy wrote: > The object class is used to implement duck typing. It is the delegation > of operations to class instance methods that makes extensible duck > typing possible. That cannot possibly be true, because Python had duck typing before it had object. object and new-style classes were introduced in Python 2.2, which means that there were a good half-dozen versions of Python, including the very popular 1.5, where people couldn't use object. (Also, I'm not sure what you mean by "class instance methods" -- methods can be class methods, or they can be instance methods, but not both at the same time.) > 'a + b' is equivalent to type(a).__add__(a, b) It's actually more complicated than that, because type(b).__radd__ also gets considered, but either way, I don't think that the *implementation* of + is relevant here. Duck-typing isn't really a mechanism, in the sense that static/dynamic or strong/weak typing are mechanisms: - static typing means that the compiler can tell at compile-time what type a variable will have, and prohibit code which may violate that constraint; - dynamic typing means that types are associated with values, not with variables; - strong typing means that the compiler will do nothing (or very little) to automatically convert values from one type to another; - weak typing means that the compiler will do a lot to automatically convert values from one type to another, including possibly some conversions which are considered by many to be unsafe or silly. Duck-typing is more of a programming philosophy than a mechanism, at least in Python: - you shouldn't care whether a value has a specific type or not, but whether it exposes the interface you care about. Some languages (like Java) try to formalise this, providing a mechanism by which you can implement a particular interface in a way known to the compiler: https://en.wikipedia.org/wiki/Interface_%28Java%29 Python's ABCs (abstract base classes, introduced in version 2.6) are similar. In both cases, they use the type system (in Java's case, at compile-time, in Python's case, at run-time) to check for an interface up-front, i.e. a form of "Look Before You Leap". In Python, duck-typing can also be more ad hoc and informal: if you want to know whether an object provides a certain interface, you typically just try it and see if it breaks, hoping that it will raise an exception sooner rather than later (i.e. "Easier to Ask for Forgiveness than Permission"). That's why I call it more of a philosophy than a mechanism. Duck-typing, of course, is not infallible. Suppose you're expecting an Artist, and call the artist.draw() method, but somebody gives you a Gunfighter instead. There's also the problem of what to do when an object provides only *part* of an interface: sometimes, by the time you have to ask forgiveness, you've already made irreversible changes to something (a file, a database, or launched the missiles). -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On 04/08/2014 02:06, Seymore4Head wrote: I am very new to Python. Right now I am using two tools. I am trying the tutorials at codecademy.com which is walking me through it pretty slow. The second thing I am doing is using codeskulptor to try out a few things I have learned at codecademy. I am getting a mismatch. The example I am working on is: (codecademy) from datetime import datetime now = datetime.now() current_year = now.year current_month = now.month current_day = now.day Putting that in codeskulptor gets Line 4: ImportError: No module named datetime The commands I am used to in codeskulptor is something like import random import math import simplegui import datetime < Changing this to import instead of from datetime import datetime still doesn't work. Please define "still doesn't work" as my crystal ball has broken down and been sent for repair. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3 is killing Python
RIck, On 7/17/14, 2:15 PM, Rick Johnson wrote: Sadly, all of my calls to improve IDLE have been meet with rebukes about me "whining". The "powers that be" would wise to*UTILIZE* and*ENCOURAGE* my participation instead of *IGNORING* valuable talent and*IMPEDING* the expansion of this "private boys club". A bit late to this, I suppose... Where are your patches? Can you point me to anywhere at the Python bug tracker where they can be found? I'll highlight the two major patches I've submitted over the past few years: http://bugs.python.org/issue15853 http://bugs.python.org/issue6075 One fixed a pretty bad crash on the Mac, and the other optimized IDLE's Mac port to adjust to some API changes in Tk because of a switch in the native back end (Carbon to Cocoa). In both cases I posted an e-mail or two to the relevant mailing list (IDLE-dev and MacPython) to provide a head-up about the patch, answer questions, and so on--but that was it. No major "calls to improve IDLE," just some code that DID improve IDLE. The "powers that be" didn't commit the patches right away, and not without some modification and testing, but they eventually did commit them, and the outcome satisfied my intention in submitting the patches in the first place. Both of these patches addressed issues that made IDLE pretty much un-usable for me. Obviously a crash will do this, but also, when I switched my installation of Tk from the Carbon-backed one to the Cocoa-backed one, there were lots of little glitches because of subtle differences in how Cocoa did things. I suppose I simply could have filled the mailing lists with complaints that these things were Big Problems for me and Someone Should Do Something About Them, but there was no guarantee that someone would pick up the challenge. Fortunately, I had the knowledge, skills and time to submit patches that were sufficiently developed that the relevant Python maintainers could take them, apply them, modify slightly as required, test them, and then commit them. This did ensure that Something Would Be Done about my issue, because the Person Who Did Something About It was me. I know you are proficient in both Python and Tkinter, as I've noted from the helpful advice you give Tkinter newbies on the list from time to time, and so I'm sure you have the skill set to put together some patches that address specific points of pain for you. And despite the disagreement that others may register with you in these threads from time to time, I'm quite confident that useful patches will be gratefully accepted, even if not immediately. --Kevin -- Kevin Walzer Code by Kevin/Mobile Code by Kevin http://www.codebykevin.com http://www.wtmobilesoftware.com -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
Seymore4Head wrote: [...] > The second thing I am doing is using codeskulptor to try out a few > things I have learned at codecademy. What's CodeSkulptor? > Putting that in codeskulptor gets > > Line 4: ImportError: No module named datetime Well that's a bug in CodeSkultor. datetime is a standard Python library, if CodeSkulptor doesn't provide it, that's a serious bug. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On Mon, Aug 4, 2014 at 11:29 AM, Steven D'Aprano wrote: >> Putting that in codeskulptor gets >> >> Line 4: ImportError: No module named datetime > > Well that's a bug in CodeSkultor. datetime is a standard Python library, if > CodeSkulptor doesn't provide it, that's a serious bug. I think it's not a bug, but a restriction; since it's letting you run code on their server, and since Python sandboxing is a hard problem, CodeSkulptor cuts down the available modules. From the docs: http://www.codeskulptor.org/docs.html#tabs-Python """ CodeSkulptor implements the following subset of the Python standard library. To use these operations, first import the relevant module with an import statement, such asimport math. """ Solution: Don't try to run Python code in your browser, but download and install an actual interpreter. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: try/exception - error block
You also posted this question to the tu...@python.org mailing list, which is where I gave an answer. This is a much better place, so I'll re-post the most important parts of my answer here. If you read nothing else, scroll down to the end and read the last part of my comment. bruce wrote: > I have a long running process, it generates calls to a separate py > app. The py app appears to generate errors, as indicated in the > /var/log/messages file for the abrtd daemon.. The errors are > intermittent. Well, what do the errors say? [...] > if __name__ == "__main__": > # main app > > try: [deleting lots of commented out code] > if(len(sys.argv)<2): > print "error\n" > sys.exit() You really should raise an exception on errors, but if you insist on doing things this way, you should print to stderr, not stdout, and you should exit with a non-zero status: print >>sys.stdout, "descriptive error messages are better\n" sys.exit(101) > a=sys.argv[1] > aaa=a A minor stylistic thing: you can write this as: a = aaa = sys.argv[1] but of course you really ought to use descriptive variable names rather than cryptic "a" and "aaa" and "z" and other meaningless names. [deleting more fossil code] > z=simplejson.loads(urllib.unquote_plus(a)) > print z > url=str(z['currentURL']) > level=str(z['level']) > cname=str(z['parseContentFileName']) > cmd='echo ${yolo_clientParseInputDir}/' > proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) > cpath=proc.communicate()[0].strip() Hmmm. Are you trying to read the value of an environment variable using subprocess? If so, then try this instead: cpath = os.getenv('yolo_clientParseInputDir') If not, then sorry for the noise. Perhaps you could explain what your call to echo in the shell is meant to do? > cname=cpath+cname > cmd='test -e '+cname+' && echo 1' > proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE) > c1=proc.communicate()[0].strip() And here I think you're trying to test whether a file exists? os.path.exists(cname) > if(not c1): > #got an error - process it, return > print "error in parse" Um, surely not? Surely the error is that the file doesn't exist, not that it is a parsing error? > with open(cname,"r") as myfile: > content=myfile.read() > myfile.close() If you use the "with open" form, there is no need to manually close the file, it will be automatically closed for you. with open(cname,"r") as myfile: content = myfile.read() is all you need (assuming you have permission to open the file, and that it still exists). [lots more fossils deleted] > ret={} # null it out to start > if (level=='rState'): > ret=getParseStates(content) > elif (level=='stateCollegeList'): > ret=getParseStateCollegeList(url,content) > elif (level=='collegeFaculty1'): > ret=getParseCollegeFacultyList1(url,content) > elif (level=='collegeFaculty2'): > ret=getParseCollegeFacultyList2(content) I'm not really sure if any of that code is relevant to the problem you're having. > a={} > status=False > if(ret['status']==True): > s=ascii_strip(ret['data']) > if(((s.find("-1) or (s.find("-1)) and > ((s.find("-1) or (s.find("-1)) and >level=='classSectionDay'): > status=True > a['Status']=True > a['recCount']=ret['count'] > a['data']=ret['data'] > a['nextLevel']='' > a['timestamp']='' > a['macAddress']='' > elif(ret['status']==False): > a['Status']=False > a['recCount']=0 > a['data']='' > a['nextLevel']='' > a['timestamp']='' > a['macAddress']='' > res=urllib.quote(simplejson.dumps(a)) Your code will be much, much, much more readable if you use a reasonable indent between levels. Four spaces rather than two, or a tab. I'm finding it quite difficult to keep track of the levels when they are so close together. > name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE) > name=name.communicate()[0].strip() > name=name.replace("-","_") > name2=tmpParseDir+"/rr_"+name+".dat" > ofile1=open(name2,"w+") > ofile1.write(res) > ofile1.close() > print name2 So does this file get written to? Does name2 get printed? > if status==False: > sname=tmpParseDir+"/serr_"+name+".dat" > ofile1=open(sname,"w+") > ofile1.write(aaa) > ofile1.close() How about this one? Does it get written to? > sys.exit() Since you exit here, the rest of the code in the block is never executed: > print "term = "+str(termVal)+"\n" > print "url = "+url+"\n" > getTerm(url,college,termVal) > print "exit" > sys.exit() That's all dead code. I hope it isn't important. > except Exception, e: > print e > print "pycolFac1 - error!! \n"; Does that get printed? > name=subprocess.Popen('uui
Re: CodeSkulptor
On Mon, 04 Aug 2014 11:29:06 +1000, Steven D'Aprano wrote: >Seymore4Head wrote: > >[...] >> The second thing I am doing is using codeskulptor to try out a few >> things I have learned at codecademy. > >What's CodeSkulptor? > >> Putting that in codeskulptor gets >> >> Line 4: ImportError: No module named datetime > >Well that's a bug in CodeSkultor. datetime is a standard Python library, if >CodeSkulptor doesn't provide it, that's a serious bug. Is codeskulptor only free to users of coursra? I assumed that codeskulptor was free to everyone. Could you verify that datetime is not included in codeskulptor? http://www.codeskulptor.org/ I just tried 3 simple lines of code at codeskulptor import random import math import datetime Line 3: ImportError: No module named datetime I guess I should be using Python 3.3. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On 04/08/2014 02:41, Seymore4Head wrote: On Mon, 04 Aug 2014 11:29:06 +1000, Steven D'Aprano wrote: Seymore4Head wrote: [...] The second thing I am doing is using codeskulptor to try out a few things I have learned at codecademy. What's CodeSkulptor? Putting that in codeskulptor gets Line 4: ImportError: No module named datetime Well that's a bug in CodeSkultor. datetime is a standard Python library, if CodeSkulptor doesn't provide it, that's a serious bug. Is codeskulptor only free to users of coursra? I assumed that codeskulptor was free to everyone. Could you verify that datetime is not included in codeskulptor? http://www.codeskulptor.org/ I just tried 3 simple lines of code at codeskulptor import random import math import datetime Line 3: ImportError: No module named datetime I guess I should be using Python 3.3. Thanks That won't do you any good at all, the datetime module has been around for years. Try your favourite search engine for something like "free online python code tester", there's bound to be something to suit your needs. Alternatively what's stopping you running Python on your own machine? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On Mon, 04 Aug 2014 02:56:34 +0100, Mark Lawrence wrote: >On 04/08/2014 02:41, Seymore4Head wrote: >> On Mon, 04 Aug 2014 11:29:06 +1000, Steven D'Aprano >> wrote: >> >>> Seymore4Head wrote: >>> >>> [...] The second thing I am doing is using codeskulptor to try out a few things I have learned at codecademy. >>> >>> What's CodeSkulptor? >>> Putting that in codeskulptor gets Line 4: ImportError: No module named datetime >>> >>> Well that's a bug in CodeSkultor. datetime is a standard Python library, if >>> CodeSkulptor doesn't provide it, that's a serious bug. >> >> Is codeskulptor only free to users of coursra? I assumed that >> codeskulptor was free to everyone. >> >> Could you verify that datetime is not included in codeskulptor? >> http://www.codeskulptor.org/ >> >> I just tried 3 simple lines of code at codeskulptor >> import random >> import math >> import datetime >> >> Line 3: ImportError: No module named datetime >> >> I guess I should be using Python 3.3. >> >> Thanks >> > >That won't do you any good at all, the datetime module has been around >for years. Try your favourite search engine for something like "free >online python code tester", there's bound to be something to suit your >needs. Alternatively what's stopping you running Python on your own >machine? Wellit is just a small thing, but I am going to have to do it. I run Win7 and like to keep it lean so I also have an XP machine that I use for experimenting. I log into the XP machine remotely. I don't like to have to keep it running 24/7 like I use to. I guess I will have to get over that, won't I? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On Mon, 4 Aug 2014 11:43:48 +1000, Chris Angelico wrote: >On Mon, Aug 4, 2014 at 11:29 AM, Steven D'Aprano > wrote: >>> Putting that in codeskulptor gets >>> >>> Line 4: ImportError: No module named datetime >> >> Well that's a bug in CodeSkultor. datetime is a standard Python library, if >> CodeSkulptor doesn't provide it, that's a serious bug. > >I think it's not a bug, but a restriction; since it's letting you run >code on their server, and since Python sandboxing is a hard problem, >CodeSkulptor cuts down the available modules. From the docs: > >http://www.codeskulptor.org/docs.html#tabs-Python >""" >CodeSkulptor implements the following subset of the Python standard >library. To use these operations, first import the relevant module >with an import statement, such asimport math. >""" > >Solution: Don't try to run Python code in your browser, but download >and install an actual interpreter. > >ChrisA I am just going to run 3.3 remotely. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re:Python crashing when script is running against live system
Igor Korot Wrote in message: > Hi, ALL, > I'm working on the script that should starting from the given > directory enumerate all directories and files underneath and > calculates the hash value of such file. > It works fine when I start it from some particular directory, but when > I give the "C:\" it crashes python. How are you 'giving' that string? It's not a valid literal. How are you running this script? Are you starting it from a cmd prompt (dos box) or some other way? And is that 'other way' eating your error information? Do you get any messages at all? Nearly every problem that aborts a python program will also display some form of error messages to stderr. > > The last thing the script processes is the file in cmdcons directory, > which is AFAIU from the ComboFix run. > I have Windows XP SP3 with python 2.7. > I haven't needed to use Windows for a long time, but I seem to recall that directory is special to Windows, containing backup copies of certain critical files. So Windows may be protecting it from corruption. What file mode are you using to open those files? I would expect 'rb'. > Now, I'm confused. So are we, you include no source code, nor do you copy/paste the error tracebacks. > My questions are: > > 1. Am I able to catch such situation and see where the crash occur? > 2. If not, what is the proper way to debug such situation? > > I am getting the window saying that python.exe has encountered a > problem and needs to close. > > The last file processed was "c:\cmdcons\dpti20.sy_". > > What strange about it that when I run the script under debugger (I'm > using winpdb), it is running further and crashing on some other file. What's the path to that file? Is it in the same directory? Are you using stderr for your messages so you'll see them all even if the program crashes? Does the program run long enough that you can watch memory usage with task manager or equivalent? Can you run to completion if you skip the part where you open the file and calculate the checksums? How about if you skip that particular directory? Or if you sort the directories in forward or reverse order? If you do change the sorts, does it affect where the crash occurs? This program should be only about 25 lines, so please show it. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
how to call back a method ?
I want to call back a function which is the method of a class . def callback(self.do,x): return(self.do(x)) That is what i want to write,when i input def callback(self.do,x): error message: File "", line 1 def callback(self.do,x): ^ SyntaxError: invalid syntax `do` is a method in my class ,how to write the code? -- https://mail.python.org/mailman/listinfo/python-list
Re: how to call back a method ?
On Mon, Aug 4, 2014 at 12:33 PM, elearn wrote: > I want to call back a function which is the method of a class . > > def callback(self.do,x): > return(self.do(x)) > > That is what i want to write,when i input > > def callback(self.do,x): > > error message: > > > File "", line 1 > def callback(self.do,x): > ^ > SyntaxError: invalid syntax > > > `do` is a method in my class ,how to write the code? You don't want to define it with the dot in there. Give it a name like 'func' (or preferably, something more descriptive): def callback(func,x): return func(x) (And I took the superfluous parentheses off 'return'; it's a statement, not a function.) If self is an object, then self.do is an attribute of that object, which in your case is a callable function. You can assign that to anything, pass it around, and so on, and you don't need "self.do" as the name. Try it like this, see if it works... but have a careful look at your design, too. You may well be overthinking this. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On Sun, 03 Aug 2014 22:08:21 -0400, Seymore4Head wrote: >On Mon, 4 Aug 2014 11:43:48 +1000, Chris Angelico >wrote: > >>On Mon, Aug 4, 2014 at 11:29 AM, Steven D'Aprano >> wrote: Putting that in codeskulptor gets Line 4: ImportError: No module named datetime >>> >>> Well that's a bug in CodeSkultor. datetime is a standard Python library, if >>> CodeSkulptor doesn't provide it, that's a serious bug. >> >>I think it's not a bug, but a restriction; since it's letting you run >>code on their server, and since Python sandboxing is a hard problem, >>CodeSkulptor cuts down the available modules. From the docs: >> >>http://www.codeskulptor.org/docs.html#tabs-Python >> BTW Just read the instructions seems like a daunting task at the moment. You knew what instructions you were looking for. I am clueless. Like running Doom doom.exe -noidea :) >>CodeSkulptor implements the following subset of the Python standard >>library. To use these operations, first import the relevant module >>with an import statement, such asimport math. >>""" >> >>Solution: Don't try to run Python code in your browser, but download >>and install an actual interpreter. >> >>ChrisA > >I am just going to run 3.3 remotely. >Thanks -- https://mail.python.org/mailman/listinfo/python-list
how to call back a method in python3?
I want to call back a function which is the method of a class . def callback(self.do,x): return(self.do(x)) That is what i want to write,when i input def callback(self.do,x): error message: File "", line 1 def callback(self.do,x): ^ SyntaxError: invalid syntax `do` is a method in my class ,how to write the code?-- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
Seymore4Head wrote: > On Sun, 03 Aug 2014 22:08:21 -0400, Seymore4Head > wrote: > >>On Mon, 4 Aug 2014 11:43:48 +1000, Chris Angelico >>wrote: >> >>>On Mon, Aug 4, 2014 at 11:29 AM, Steven D'Aprano >>> wrote: > Putting that in codeskulptor gets > > Line 4: ImportError: No module named datetime Well that's a bug in CodeSkultor. datetime is a standard Python library, if CodeSkulptor doesn't provide it, that's a serious bug. >>> >>>I think it's not a bug, but a restriction; since it's letting you run >>>code on their server, and since Python sandboxing is a hard problem, >>>CodeSkulptor cuts down the available modules. From the docs: >>> >>>http://www.codeskulptor.org/docs.html#tabs-Python Excluding datetime seems rather extreme to me. > BTW Just read the instructions seems like a daunting task at the > moment. You knew what instructions you were looking for. I am > clueless. So am I, because I don't know what instructions you're referring to. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On Mon, Aug 4, 2014 at 12:38 PM, Seymore4Head wrote: > BTW Just read the instructions seems like a daunting task at the > moment. You knew what instructions you were looking for. I am > clueless. Yeah, that's called experience :) Part of that experience is the rather painful one of spending a good few dev hours trying to sandbox Python inside a C++ process, only to find that the sandbox got busted wide open pretty quickly as soon as I asked people to try to. (It was in a safe environment, firewalled off from everything. It was deliberately done as a security test... and the system failed so spectacularly that we had to make a complete change to the core model, among other things not using Python. Which made me sad. We had to go with ECMAScript and its flaws.) But that means that I know straight away what to look for. This is why we have these kinds of mailing lists / newsgroups. Every one of us has some experience; when you put a question out to the collective, there's a high probability that someone will know the answer. It's not that there are "smart people" and "dumb people", and that dumb people ask questions that smart people answer; it's that the whole group is smarter than any one of us. And then the answer gets posted to the whole group, and we all become that bit smarter for it :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: CodeSkulptor
On Mon, Aug 4, 2014 at 1:03 PM, Steven D'Aprano wrote: I think it's not a bug, but a restriction; since it's letting you run code on their server, and since Python sandboxing is a hard problem, CodeSkulptor cuts down the available modules. From the docs: http://www.codeskulptor.org/docs.html#tabs-Python > > Excluding datetime seems rather extreme to me. By the look of their docs, they've actually gone the other way: it's not that they've excluded datetime, but that they've carefully vetted a specific set of modules (and maybe not all functionality in them) and that's all they support. In any case, I think that as soon as you hit an ImportError on the sandbox, you should go and download Python for your desktop and start working there. (Idea, for anyone who runs a sandbox like that: Enumerate all packages and modules in the stdlib, and create a little stub for each of them. "import blahblah" will still produce ImportError, but "import datetime" could report back "This interpreter is working with a small subset of the Python standard library" rather than leaving us wondering if there was some weird copy/paste error in the import line. And yes, I did test for that.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On 8/3/2014 5:17 PM, Glenn Linderman wrote: On 8/3/2014 4:25 PM, Andrew Berg wrote: On 2014.08.03 18:08, Chris Angelico wrote: The best way to do it is to use the Unicode codepage, but cmd.exe just plain has issues. There are underlying Windows APIs for displaying text that have problems with astral characters (I think that's what it is), so ultimately, you're largely stuck. That is not quite true. The terminal has these issues, not the shell. Using cp65001 does make Unicode in a Windows terminal possible, but using a better terminal[1] makes it almost perfect (my experience has been that input can be difficult, but output works well). I personally have used an IRC bot written in Python with logging output containing Unicode characters that display just fine (both locally and over SSH). [1] I recommend ConEmu:https://code.google.com/p/conemu-maximus5/ I will be reading more about conemu, thanks for the reference. Having read a bit about ConEmu, it seems that it is a "pretty face" built on top of Windows Console, by screen scraping the real (but hidden) Windows Console, and providing a number of interesting display features and modes. So while it adds functionality to the Windows Console interface, it doesn't seem like it is likely to fix bugs or resolve issues with code pages, font selection, or Unicode character repertoires, which are the issues of this thread and the bug I referenced earlier. Can anyone with ConEmu installed refute this interpretation of its functionality? -- https://mail.python.org/mailman/listinfo/python-list
Re: cmd.exe on WIndows - problem with displaying some Unicode characters
On 2014.08.03 23:14, Glenn Linderman wrote: > Having read a bit about ConEmu, it seems that it is a "pretty face" built on > top of Windows Console, by screen scraping the real (but hidden) Windows > Console, and providing a number of interesting display features and modes. So > while it adds functionality to the Windows Console interface, it doesn't seem > like it is likely to fix bugs or resolve issues with code pages, font > selection, or Unicode character repertoires, which are the issues of this > thread and the bug I referenced earlier. > > Can anyone with ConEmu installed refute this interpretation of its > functionality? > If you run cmd in it, you will still need to use cp65001. This is not necessary for (or applicable to) other applications (such as a Python interpreter) run directly. ConEmu can use any arbitrary font available on the system. As I have said, I have been able to display Unicode output on it from an application written in Python. No mojibake, no replacement characters, just the exact characters one would expect. I do not know the internals of ConEmu and how it interacts with conhost and whatever else, but I have not found a need to since it has just worked for me. -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
On 8/3/2014 9:19 PM, Steven D'Aprano wrote: stuff based on an understandable misunderstanding of what I wrote. Terry Reedy wrote: The object class is used to implement duck typing. It is the delegation of operations to class instance methods that makes extensible duck typing possible. I left off "'s". The object's class ... 'class instance methods' = class attributes that are instance methods. I was alluding to the fact that magic methods are looked up directly on the class and not the instance. Sorry for the confusion. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
Steven D'Aprano : > Marko Rauhamaa wrote: > >> I've reached a point where I think classes are a superfluous OO concept. >> You only need objects. > > I don't know whether "superfluous" is correct, but they certainly are > *optional*. There are at least two types of object oriented programming: > class-bases, and prototype-based. And I'm talking about a third kind: object-based. It is in active (albeit limited) use in scheme: http://irreal.org/blog/?p=40>. I'm currently using the principle in a project of mine. In Java, you use anonymous classes for the same thing. In Python, you can think of the principle as one-time classes. So instead of writing: class A: def __init__(self, x, y, z): self.x = x self.d = y * y + z * z def f(self): return self.x - self.d you write: def A(x, y, z): d = y * y + z * z class Anonymous: def f(self): return x - d return Anonymous() Now, if you always did this, you would notice that classes are unnecessary clutter and would call for syntax like this: def A(x, y, z): d = y * y + z * z return object: def f(): return x - d Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: how to call back a method in python3?
"水静流深" <1248283...@qq.com>: > I want to call back a function which is the method of a class . > > def callback(self.do,x): > return(self.do(x)) > > That is what i want to write,when i input > > def callback(self.do,x): > > error message: Do this: class MyClass: def my_method(self): def callback(x): return self.do(x) return callback def do(self, x): print("done: {}".format(x)) then call: m = MyClass() callback = m.my_method() callback(7) Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: how to call back a method in python3?
Marko Rauhamaa wrote: Do this: class MyClass: def my_method(self): def callback(x): return self.do(x) return callback def do(self, x): print("done: {}".format(x)) Or more simply: def my_method(self): return self.do -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Programing for the Absoulte Beginner
in 726123 20140803 090919 Steven D'Aprano wrote: >Steve Hayes wrote: > >> I've got too big an investment in books on Python 2, and there are no >> books available on Python 3 (I don't regard downloadable PDFs or other >> onlines stuff as "books"). > >I love Python 3, it's way better than Python 2, and there's less and less >reason to stick to Python 2 now. You really should learn Python 3, you >won't be sorry. > >But, if you choose not to, there's nothing to be ashamed of. Python 2.7 has >got at least six years of life left in it, and when you're done with it, >migrating to Python 3 isn't like learning a new language. It's more like >the difference between American and British English. With American English being 2.7 ?? Sorry, but someone had to ask :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Correct type for a simple "bag of attributes" namespace object
On Sun, Aug 3, 2014 at 11:41 PM, Marko Rauhamaa wrote: > Steven D'Aprano : > And I'm talking about a third kind: object-based. It is in active > (albeit limited) use in scheme: http://irreal.org/blog/?p=40>. I'm > currently using the principle in a project of mine. > > In Java, you use anonymous classes for the same thing. In Python, you > can think of the principle as one-time classes. So instead of writing: In my experience, 99% of the time when anonymous classes are used, they only contain one method. That's because they're not really being used to implement some interface abstractly; they mostly exist to provide a means of specifying a callback function in a language where functions aren't first-class. > > class A: > def __init__(self, x, y, z): > self.x = x > self.d = y * y + z * z > > def f(self): > return self.x - self.d > > you write: > > def A(x, y, z): > d = y * y + z * z > > class Anonymous: > def f(self): > return x - d > > return Anonymous() And it's the same thing here. This isn't an interface. It's a function, so just return a function and be done with it. And in the rare event that you actually mean to implement more than one method, then you have enough functionality localized that it's probably worthwhile to just name it and make it an actual class. > Now, if you always did this, you would notice that classes are > unnecessary clutter and would call for syntax like this: > > def A(x, y, z): > d = y * y + z * z > return object: > def f(): > return x - d The presence of "object" here feels totally unnecessary to me. If the point is to avoid having a class, then why create a class? In this case you could easily just return a function. To take a more complex case, say the Scheme make-queue constructor that you linked to, you could still just return a function; the same could be implemented in Python 3 as: def make_queue(): front = [] back = [] def queue_command(command, data=None): def exchange(): nonlocal front, back front = back; front.reverse() back = [] if command == 'push': back.append(data) elif command == 'pop': if not front: exchange() return front.pop() elif command == 'peek': if not front: exchange() return front[-1] elif command == 'show': return str(list(reversed(front)) + back) else: raise ValueError('Illegal command to queue object ' + command) queue_command.push = lambda data: queue_command('push', data) queue_command.pop = lambda: queue_command('pop') queue_command.peek = lambda: queue_command('peek') return queue_command >>> queue = make_queue() >>> queue.push(1) >>> queue.push(2) >>> queue.pop() 1 >>> queue.push(3) >>> queue.push(4) >>> queue.pop() 2 >>> queue.peek() 3 >>> queue.pop() 3 >>> queue.pop() 4 -- https://mail.python.org/mailman/listinfo/python-list