From fomcl at yahoo.com Wed Aug 1 10:37:00 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 1 Aug 2012 01:37:00 -0700 (PDT) Subject: [Tutor] int("10**6") Message-ID: <1343810220.31119.YahooMailNeo@web110716.mail.gq1.yahoo.com> Hi I want the user to be able to specify "10**6" as arguments. How can I cast this string value?(it's from sys.argv) to an int value?. Simply doing int("10**6") won't work. The code below works, but seems overly complicated. ? import re POW = lambda x, y=1: pow(x, y) convert = lambda n: POW(*map(int, re.split(r"[*]*", str(n)))) nrows, ncols = map(convert, (sys.argv[1], sys.argv[2]) Any ideas how to improve this? Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 1 10:55:34 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 1 Aug 2012 18:55:34 +1000 Subject: [Tutor] int("10**6") In-Reply-To: <1343810220.31119.YahooMailNeo@web110716.mail.gq1.yahoo.com> References: <1343810220.31119.YahooMailNeo@web110716.mail.gq1.yahoo.com> Message-ID: <20120801085534.GA11958@ando> On Wed, Aug 01, 2012 at 01:37:00AM -0700, Albert-Jan Roskam wrote: > Hi > I want the user to be able to specify "10**6" as arguments. How can I cast this string value?(it's from sys.argv) to an int value?. > Simply doing int("10**6") won't work. The code below works, but seems overly complicated. Is your intention to support arbitrary arithmetic? E.g. what happens if the user provides "23.45 + -17.9**5.3e2 * log(0.023 + 9.31)/3" ? If you want a full blown arithmetic parser, you will need to write one. It's not as hard as you might think, but neither is it trivial. If you're satisfied with a *simple* arithmetic parser, you can try these: http://effbot.org/zone/simple-iterator-parser.htm http://effbot.org/zone/simple-top-down-parsing.htm As an alternative, if you like living dangerously and don't mind having other people shouting at you, you can use eval. But BEWARE: you are opening yourself up to all sorts of nasty security vulnerabilities if you do so. But if this script is for your own personal use, it is worth considering. If all you want is to support arguments of the form "INT ** INT", that's easy: def eval_power(argument): if "**" in argument: a, b = argument.split("**", 1) a = a.strip() # ignore whitespace b = b.strip() a = int(a) # or float if you prefer b = int(b) return a**b else: return int(argument) # or raise an error? Now just call that function on each of your arguments, and you're done. -- Steven From fomcl at yahoo.com Wed Aug 1 11:17:56 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 1 Aug 2012 02:17:56 -0700 (PDT) Subject: [Tutor] int("10**6") In-Reply-To: <20120801085534.GA11958@ando> References: <1343810220.31119.YahooMailNeo@web110716.mail.gq1.yahoo.com> <20120801085534.GA11958@ando> Message-ID: <1343812676.2907.YahooMailNeo@web110716.mail.gq1.yahoo.com> Aaaahhh *slap against forehead*, of course eval! Thanks!! This is perfect for my needs. Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? >________________________________ >From: Steven D'Aprano >To: tutor at python.org >Sent: Wednesday, August 1, 2012 10:55 AM >Subject: Re: [Tutor] int("10**6") > >On Wed, Aug 01, 2012 at 01:37:00AM -0700, Albert-Jan Roskam wrote: >> Hi >> I want the user to be able to specify "10**6" as arguments. How can I cast this string value?(it's from sys.argv) to an int value?. >> Simply doing int("10**6") won't work. The code below works, but seems overly complicated. > >Is your intention to support arbitrary arithmetic? E.g. what happens if >the user provides "23.45 + -17.9**5.3e2 * log(0.023 + 9.31)/3" ? > >If you want a full blown arithmetic parser, you will need to write one. >It's not as hard as you might think, but neither is it trivial. > >If you're satisfied with a *simple* arithmetic parser, you can try >these: > >http://effbot.org/zone/simple-iterator-parser.htm >http://effbot.org/zone/simple-top-down-parsing.htm > >As an alternative, if you like living dangerously and don't mind having >other people shouting at you, you can use eval. But BEWARE: you are >opening yourself up to all sorts of nasty security vulnerabilities if >you do so. But if this script is for your own personal use, it is worth >considering. > > >If all you want is to support arguments of the form "INT ** INT", that's >easy: > >def eval_power(argument): >? ? if "**" in argument: >? ? ? ? a, b = argument.split("**", 1) >? ? ? ? a = a.strip()? # ignore whitespace >? ? ? ? b = b.strip() >? ? ? ? a = int(a)? # or float if you prefer >? ? ? ? b = int(b) >? ? ? ? return a**b >? ? else: >? ? ? ? return int(argument)? # or raise an error? > >Now just call that function on each of your arguments, and you're done. > > > >-- >Steven >_______________________________________________ >Tutor maillist? -? Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rail.shafigulin at gmail.com Wed Aug 1 16:28:53 2012 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Wed, 1 Aug 2012 10:28:53 -0400 Subject: [Tutor] __new__ and __init__ Message-ID: Hello everyone. I'm trying to understand how to use the two methods. I know that __new__ is used to create an object, while __init__ to initialize. But I'm not sure what happens when I create an object. I found the following code in the book (Python 3 Object Oriented Programming by Dusty Phillips) import weakref class CarModel: _models = weakref.WeakValueDictionary() def __new__(cls, model_name, *args, **kwargs): model = cls._models.get(model_name) if not model: model = super().__new__(cls) cls._models[model_name] = model return model def __init__(self, model_name, air = False, tilt = False, cruise_control = False, power_locks = False, alloy_wheels = False, usb_charger = False): if not hasattr(self, "initted"): self.model_name = model_name self.air = air self.tilt = tilt self.cruise_control = cruise_control self.power_locks = power_locks self.alloy_wheels = alloy_wheels self.usb_charger = usb_charger self.intted = True dx = CarModel("Fit DX") lx = CarModel("Fit LX", air = True, tilt = True, cruise_control = True, power_locks = True, alloy_wheels = True, usb_charger = True) I assume when I call CarModel() __new__is being called first and then __init__ after it. 1) Does it mean that __new__ and __init__ must have the same parameters? In this particular case __new__ and __init__ both have model_name and if I understand correctly when __new__ is called the rest of the parameters (air, tilt, cruise_control, etc) are absorbed by the *args argument. Please correct me if I am wrong. 2) What happens if I don't use the same parameters, say in the case of __init__ I will remove model_name, will I still be able to call dx = CarModel("Fix DX") I realize questions might seem a bit strange or simplistic but I just want to make sure I have a correct understanding of things. Any help is appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Wed Aug 1 17:10:06 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 1 Aug 2012 17:10:06 +0200 Subject: [Tutor] __new__ and __init__ In-Reply-To: References: Message-ID: On Wed, Aug 1, 2012 at 4:28 PM, rail shafigulin wrote: > Hello everyone. > > I'm trying to understand how to use the two methods. I know that __new__ > is used to create an object, while __init__ to initialize. But I'm not sure > what happens when I create an object. > > I found the following code in the book (Python 3 Object Oriented > Programming by Dusty Phillips) > > import weakref > > class CarModel: > _models = weakref.WeakValueDictionary() > > def __new__(cls, model_name, *args, **kwargs): > model = cls._models.get(model_name) > if not model: > model = super().__new__(cls) > cls._models[model_name] = model > return model > > def __init__(self, model_name, air = False, tilt = False, cruise_control > = False, power_locks = False, alloy_wheels = False, usb_charger = False): > if not hasattr(self, "initted"): > self.model_name = model_name > self.air = air > self.tilt = tilt > self.cruise_control = cruise_control > self.power_locks = power_locks > self.alloy_wheels = alloy_wheels > self.usb_charger = usb_charger > self.intted = True > > > dx = CarModel("Fit DX") > lx = CarModel("Fit LX", air = True, tilt = True, cruise_control = True, > power_locks = True, alloy_wheels = True, usb_charger = True) > > I assume when I call CarModel() __new__is being called first > and then __init__ after it. > 1) Does it mean that __new__ and __init__ must have the same parameters? > In this particular case __new__ and __init__ both have model_name and if I > understand correctly when __new__ is called the rest of the parameters > (air, tilt, cruise_control, etc) are absorbed by the *args argument. Please > correct me if I am wrong. > This is exactly right. > 2) What happens if I don't use the same parameters, say in the case of > __init__ I will remove model_name, will I still be able to call dx = > CarModel("Fix DX") > > Why didn't you try it yourself? It's only a few modifications. If you remove all references to model_name in __init__, the first CarModel call will still seem to work, but with a little poking you'll realize that it really doesn't. The string "Fix DX" is assigned to model_name in __new__, and since __init__ is called with the exact same arguments always* "Fix DX" will simply end up assigned to the air argument of __init__, and that really isn't what you want in almost every situation. * small caveat: I'm entirely unsure of this, but I *think* if you create CarModel with a metaclass that overrides __call__ you can change the way __new__ and __init__ work? If anyone can confirm this, be my guest. HTH, Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 1 17:27:01 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 02 Aug 2012 01:27:01 +1000 Subject: [Tutor] __new__ and __init__ In-Reply-To: References: Message-ID: <50194AC5.40407@pearwood.info> On 02/08/12 00:28, rail shafigulin wrote: > Hello everyone. > > I'm trying to understand how to use the two methods. I know that __new__ is > used to create an object, while __init__ to initialize. But I'm not sure > what happens when I create an object. > > I found the following code in the book (Python 3 Object Oriented > Programming by Dusty Phillips) That's a rather complex example to be teaching the basics. Here's a simple toy class: class MyClass(object): def __new__(cls, x, y, z): print("constructing instance with arguments:") print(" ", cls, x, y, z) instance = super(MyClass, cls).__new__(cls, x, y, z) return instance def __init__(self, a, b, c): print("initializing instance with arguments:") print(" ", self, a, b, c) self.a = a self.b = b self.c = c If you copy and paste that into the Python interactive interpreter, and then test it, you should see something like this: py> inst = MyClass(23, 42, 'spam') constructing instance with arguments: 23 42 spam initializing instance with arguments: <__main__.MyClass object at 0xb7c85a6c> 23 42 spam So what's going on? When you call MyClass(...), this creates a new instance of the class. Python first calls the constructor, __new__, to actually build an instance in memory. Notice how the first argument to __new__ is not "self", as methods usually have, but "cls" instead? That's because no instance exists yet, so "self" can't exist! Instead, the first argument to the method is not the instance, but the class. The other arguments, x y and z, are whatever arguments you give when you call MyClass(...). The __new__ method then builds a MyClass instance. How does it know how to build an instance? Because MyClass inherits from object, the built-in special class. Everything[1] inherits from object, and object's __new__ knows how to build instances. MyClass.__new__ uses super() to delegate the work of actually building an instance to the superclass (hence the name!), object. The call to super() is a bit tricky, mostly because __new__ is special, so for now just take this as a magic incantation: super(MyClass, cls).__new__(cls, x, y, z) You have to write it like that in Python 2; in Python 3, there is a magic shortcut that you can use: super().__new__(cls, x, y, z) Once the instance is created, your __new__ method can change it, add attributes, turn it inside out and paint it pink if you like. But normally you don't do that from the constructor, you leave that to the initializer, __init__. So the constructor, __new__, will normally just return the instance, and then Python will automatically call __init__. Note: __new__ doesn't have to return the instance. It can return anything you like. But if it is *not* an instance of the class, Python will not call __init__. Unlike __new__, __init__ is just an ordinary method that sees the instance as the first argument, "self". Notice that I have deliberately called the arguments by different names in __new__ and __init__? x, y, z versus a, b, c. I've done that to make a point, but normally you would use the same argument names, for clarity and simplicity. Inside __init__, you do whatever you need do to the instance -- paint it pink, attach attributes, whatever. And then you're done. Unlike __new__, which has to return something (normally the instance), __init__ doesn't have to return anything. It can if you like, but there is no point: Python will just ignore it. So when should you use __new__ and when should you use __init__? The short answer is: you nearly always should use __init__ and not __new__. The longer answer is: if you are subclassing built-in types like int, float, str or tuple, then you have to use __new__. There's usually not much point to trying to change the value in __init__, because it's too late. Here's an example: suppose you want a subclass of int that is always positive or zero. This won't work: py> class PositiveInt(int): # WRONG! ... def __init__(self, arg): ... if arg < 0: ... self = -arg ... py> n = PositiveInt(-1) py> n -1 Trying to change the instance after it is created doesn't do anything. All you do is change the object referred to by the name "self". You have to change the value *before* the instance is created, because ints are immutable and can't be changed once they exist: py> class PositiveInt(int): # RIGHT! ... def __new__(cls, arg): ... if arg < 0: ... arg = -arg ... return super().__new__(cls, arg) ... py> n = PositiveInt(-1) py> n 1 [1] This is true in Python 3. It isn't *quite* true in Python 2. If you care about the difference, ask about "classic classes" and "new-style classes". To answer your questions: > I assume when I call CarModel() __new__is being called first > and then __init__ after it. Correct. > 1) Does it mean that __new__ and __init__ must have the same parameters? In > this particular case __new__ and __init__ both have model_name and if I > understand correctly when __new__ is called the rest of the parameters > (air, tilt, cruise_control, etc) are absorbed by the *args argument. Please > correct me if I am wrong. Correct. Apart from the special first argument (cls and self), yes, __new__ and __init__ will receive the same arguments. That doesn't mean that you have to give those arguments the same names, although you usually should. > 2) What happens if I don't use the same parameters, say in the case of > __init__ I will remove model_name, will I still be able to call dx = > CarModel("Fix DX") If your parameters are different, Python will blindly line up the arguments supplied with the given parameters. If something doesn't match, Python will raise a TypeError. -- Steven From steve at pearwood.info Wed Aug 1 17:53:46 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 02 Aug 2012 01:53:46 +1000 Subject: [Tutor] __new__ and __init__ In-Reply-To: References: Message-ID: <5019510A.5090101@pearwood.info> On 02/08/12 01:10, Hugo Arts wrote: > * small caveat: I'm entirely unsure of this, but I *think* if you create > CarModel with a metaclass that overrides __call__ you can change the way > __new__ and __init__ work? If anyone can confirm this, be my guest. Correct. Metaclasses can essentially change *nearly* everything about how classes and instances are created. py> class MyMeta(type): ... def __call__(self, *args): ... instance = self.__new__(self, "magic") ... instance.__init__("happens") ... instance.colour = "sparkly" ... return instance ... py> py> class MyClass(object, metaclass=MyMeta): ... def __new__(cls, arg): ... print("received argument", arg) ... return super().__new__(cls, arg) ... def __init__(self, arg): ... print("received argument", arg) ... self.arg = arg ... py> py> inst = MyClass("these", "args", "are", "ignored") received argument magic received argument happens py> inst.colour 'sparkly' -- Steven From eryksun at gmail.com Wed Aug 1 18:00:21 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 1 Aug 2012 12:00:21 -0400 Subject: [Tutor] __new__ and __init__ In-Reply-To: References: Message-ID: On Wed, Aug 1, 2012 at 11:10 AM, Hugo Arts wrote: > On Wed, Aug 1, 2012 at 4:28 PM, rail shafigulin > > * small caveat: I'm entirely unsure of this, but I *think* if you create > CarModel with a metaclass that overrides __call__ you can change the way > __new__ and __init__ work? If anyone can confirm this, be my guest. > That works, but if you're going to use a metaclass (i.e. a type subclass of which the CarModel class is an instance), you may as well skip overriding __new__. You don't want to mess around with metaclasses, however, unless absolutely necessary. Don't needlessly complicate your code to make it harder to understand and maintain. From alan.gauld at btinternet.com Thu Aug 2 10:36:23 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 02 Aug 2012 09:36:23 +0100 Subject: [Tutor] __new__ and __init__ In-Reply-To: References: Message-ID: On 01/08/12 15:28, rail shafigulin wrote: > I'm trying to understand how to use the two methods. I know that __new__ > is used to create an object, while __init__ to initialize. But I'm not > sure what happens when I create an object. Use print statements to find out... >>> class C(object): ... def __new__(cls): ... print 'in new' ... return object.__new__(cls) ... def __init__(slf): print 'in init' ... >>> c = C() in new in init >>> > 1) Does it mean that __new__ and __init__ must have the same parameters? > In this particular case __new__ and __init__ both have model_name and if > I understand correctly when __new__ is called the rest of the parameters > (air, tilt, cruise_control, etc) are absorbed by the *args argument. That's right and I've never tried doing it differently - but again the >>> prompt and print are your friends > 2) What happens if I don't use the same parameters, say in the case of > __init__ I will remove model_name, will I still be able to call dx = > CarModel("Fix DX") > The best way to be sure is to try it. That's the joy of an interactive prompt... you never need to guess. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From jobattle at caltech.edu Fri Aug 3 01:38:01 2012 From: jobattle at caltech.edu (John Battle) Date: Thu, 02 Aug 2012 16:38:01 -0700 Subject: [Tutor] PYFTDI Library for FT232H Message-ID: <501B0F59.1050707@caltech.edu> I am relatively new to Pyton and am trying to use a library called pyftdi which is used to establish communication with USB chips made by FTDI. I have been able to install the library and write a simple piece of code to discover my interfaces (I have two FT232H devices connected). The following code seems to work to accomplish that: #!/usr/bin/python from pyftdi.pyftdi.ftdi import * vps=[(0x0403,0x6014)] devs=Ftdi.find_all(vps) print devs However I cannot figure out what to do next. The devices in question are programmed n the FT245 Syncronous mode and I need to send a short string to one of them to start a data download and then receive straming data on the other one. I have a C program that will do this but I need to do it in Python. Any help would be gretly appreciated. Thanks John Battle From alan.gauld at btinternet.com Fri Aug 3 02:02:53 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Aug 2012 01:02:53 +0100 Subject: [Tutor] PYFTDI Library for FT232H In-Reply-To: <501B0F59.1050707@caltech.edu> References: <501B0F59.1050707@caltech.edu> Message-ID: On 03/08/12 00:38, John Battle wrote: > I am relatively new to Pyton and am trying to use a library called > pyftdi which is used to establish communication with USB chips This list is really for those learning the core Python language and standard library. Although we do cover some common third party libraries too it's not really our purpose. You might be very lucky and find someone who knows this particular library but you will generally have better success on a support forum for the library or by writing the author directly. Failing that you could try the main Python mailing list/newsgroup. > I have a C program that will do this but I need to do it in Python. If it's simply how to translate the C to Python we might be able to help. If you can post a short example of the C that you want to emulate we may be able to help with that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Fri Aug 3 03:02:44 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 03 Aug 2012 11:02:44 +1000 Subject: [Tutor] PYFTDI Library for FT232H In-Reply-To: <501B0F59.1050707@caltech.edu> References: <501B0F59.1050707@caltech.edu> Message-ID: <501B2334.3030000@pearwood.info> On 03/08/12 09:38, John Battle wrote: > I am relatively new to Pyton and am trying to use a library called pyftdi > which is used to establish communication with USB chips made by FTDI. I have > been able to install the library and write a simple piece of code to discover > my interfaces (I have two FT232H devices connected). The following code seems > to work to accomplish that: > > #!/usr/bin/python > from pyftdi.pyftdi.ftdi import * > vps=[(0x0403,0x6014)] > devs=Ftdi.find_all(vps) > print devs > > However I cannot figure out what to do next. If you are talking about this library: http://pypi.python.org/pypi/pyftdi have you tried reading the documentation, such as it is? I've had a quick look, and it appears to be extremely light on documentation, but I didn't dig too deeply, maybe you will have better luck. The GitHub page claims to have some examples, but I couldn't get it to work in my browser. You can also try this at the Python interactive interpreter: import pyftdi help(pyftdi) and see if the library has any useful embedded documentation. It may not. Do you know how to start the interactive interpreter? You need to open a terminal window, then at the prompt, you need to launch Python. Under Linux, I just type python and hit the ENTER key. If you are using Windows, it may be more complicated. -- Steven From breamoreboy at yahoo.co.uk Fri Aug 3 15:09:16 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 03 Aug 2012 14:09:16 +0100 Subject: [Tutor] PYFTDI Library for FT232H In-Reply-To: <501B2334.3030000@pearwood.info> References: <501B0F59.1050707@caltech.edu> <501B2334.3030000@pearwood.info> Message-ID: On 03/08/2012 02:02, Steven D'Aprano wrote: > On 03/08/12 09:38, John Battle wrote: >> I am relatively new to Pyton and am trying to use a library called pyftdi >> which is used to establish communication with USB chips made by FTDI. >> I have >> been able to install the library and write a simple piece of code to >> discover >> my interfaces (I have two FT232H devices connected). The following >> code seems >> to work to accomplish that: >> >> #!/usr/bin/python >> from pyftdi.pyftdi.ftdi import * >> vps=[(0x0403,0x6014)] >> devs=Ftdi.find_all(vps) >> print devs >> >> However I cannot figure out what to do next. > > > If you are talking about this library: > > http://pypi.python.org/pypi/pyftdi > > have you tried reading the documentation, such as it is? I've had a > quick look, and it appears to be extremely light on documentation, but I > didn't dig too deeply, maybe you will have better luck. The GitHub page > claims to have some examples, but I couldn't get it to work in my browser. > > You can also try this at the Python interactive interpreter: > > import pyftdi > help(pyftdi) > > > and see if the library has any useful embedded documentation. It may not. > > > Do you know how to start the interactive interpreter? You need to open a > terminal window, then at the prompt, you need to launch Python. Under > Linux, I just type > > python > > and hit the ENTER key. If you are using Windows, it may be more > complicated. > > > Just the same for Windows, although I tend to use PythonWin myself. -- Cheers. Mark Lawrence. From fomcl at yahoo.com Fri Aug 3 16:39:35 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 3 Aug 2012 07:39:35 -0700 (PDT) Subject: [Tutor] adding a windows registry value Message-ID: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Hi, ? I am trying to change a registry value (Windows 7, Python 2.7) but it won't work when I try to do this using Python: ? import os # 1 using this from Python won't work, but double-clicking the file works. os.system(r"regedit /s C:\Users\Desktop\set_temp.reg") # 2: using this from Python won't work, but from the commandline or with a batch file works os.system("reg add HKEY_CURRENT_USER\Software .....(etc)") Why is this not working using Python? Is there a built-in way to do this (I don't have win32api)? Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Aug 3 18:14:43 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 04 Aug 2012 02:14:43 +1000 Subject: [Tutor] adding a windows registry value In-Reply-To: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: <501BF8F3.4030908@pearwood.info> On 04/08/12 00:39, Albert-Jan Roskam wrote: > Hi, > > I am trying to change a registry value (Windows 7, Python 2.7) but it won't > work when I try to do this using Python: Define "won't work". Does it: - change the registry entry, but no effect is apparent? - raise an exception? - crash? - silently fail to change the registry entry? - something else? -- Steven From alan.gauld at btinternet.com Fri Aug 3 18:49:38 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Aug 2012 17:49:38 +0100 Subject: [Tutor] adding a windows registry value In-Reply-To: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On 03/08/12 15:39, Albert-Jan Roskam wrote: > ...Is there a built-in way to do this > (I don't have win32api)? Yes you do, because ctypes is part of the standard Python library. And ctypes lets you access Win32api. See: http://docs.python.org/library/ctypes.html#module-ctypes for examples. As to your problem, it sounds like a permissions thing, are you sure you are running it as the same user in Python and from the DOS box? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From smaran.harihar at gmail.com Fri Aug 3 18:58:32 2012 From: smaran.harihar at gmail.com (Smaran Harihar) Date: Fri, 3 Aug 2012 09:58:32 -0700 Subject: [Tutor] Creating a basic CGI script Message-ID: Hi, I wish to create a basic cgi script which will interact with the ajax call from my Ext-JS, modify it and then return it back to the ext-js. I just completed this tutorial which tells me how I should create a cgi-script but it was interacting with the py script. How do I make a simple cgi for receiving the ajax call? -- Thanks & Regards Smaran Harihar -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Fri Aug 3 19:42:08 2012 From: kwpolska at gmail.com (Kwpolska) Date: Fri, 3 Aug 2012 19:42:08 +0200 Subject: [Tutor] adding a windows registry value In-Reply-To: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: Anyone has an idea why this list wants me to send mail to the OP rather than the ML itself? Anyways, On Fri, Aug 3, 2012 at 4:39 PM, Albert-Jan Roskam wrote: > Hi, > > I am trying to change a registry value (Windows 7, Python 2.7) but it won't > work when I try to do this using Python: > > import os > # 1 using this from Python won't work, but double-clicking the file works. > os.system(r"regedit /s C:\Users\Desktop\set_temp.reg") > # 2: using this from Python won't work, but from the commandline or with a > batch file works > os.system("reg add HKEY_CURRENT_USER\Software .....(etc)") > > Why is this not working using Python? Is there a built-in way to do this (I > don't have win32api)? > > Regards, > Albert-Jan > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > All right, but apart from the sanitation, the medicine, education, wine, > public order, irrigation, roads, a > fresh water system, and public health, what have the Romans ever done for > us? > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Please, do not do stuff like that. There is a library for that: http://docs.python.org/library/_winreg.html Also, using registry keys is not a great idea if you would want someone to use your code on a different platform. (originally sent to the OP @ 2012-08-03T15:14:00Z) -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From alan.gauld at btinternet.com Fri Aug 3 19:43:29 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Aug 2012 18:43:29 +0100 Subject: [Tutor] Creating a basic CGI script In-Reply-To: References: Message-ID: On 03/08/12 17:58, Smaran Harihar wrote: > I wish to create a basic cgi script which will interact with the ajax > call from my Ext-JS, modify it and then return it back to the ext-js. This forum is for people learning the Python language and the standard library. CGI is a standard module but not often used these days for web programming since much better frameworks exist that make things much easier. jax is not really a beginners topic nor directly related to the Python language. Since most Python web development frameworks include standard ways of writing Ajax calls so the answer will depend on which framework you choose, and then support for that will be best given via the framework forum. Having said all that, if are using the standard cgi module and you can show us a very simple example of what you have tried and explain what happened we might be able to help out. > I just completed this tutorial Which tutorial, there are many. Don't make us guess! > ...which tells me how I should create a cgi-script but it > was interacting with the py script. That's the usual way to program in CGI its all done at the server end. Do you understand how the http/CGI mechanism works? Can you write a traditional CGI web application? If you have not already done so I recommend that you read this: http://docs.python.org/py3k/howto/webservers.html and consider using one of the web frameworks mentioned there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Aug 3 20:35:55 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Aug 2012 19:35:55 +0100 Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On 03/08/12 18:42, Kwpolska wrote: > Anyone has an idea why this list wants me to send mail to the OP > rather than the ML itself? Anyways, The list doesn't care, you probably did it by hitting Reply instead of Reply All. Reply replies to the person who posted. Reply All replies to all on the list. Just like regular email. That's just how its set up, to mimic normal email. It's not the only list set up that way, most of the ones I use do it like that. You get used to it :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From redacted@example.com Fri Aug 3 22:18:20 2012 From: redacted@example.com (Alexander Q.) Date: Fri, 3 Aug 2012 13:18:20 -0700 Subject: [Tutor] Newline question Message-ID: I'm following the tutorial from python.org ( http://docs.python.org/tutorial/introduction.html) and am having a few indiscrepancies regarding the new line command. The tutorial says that this code hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ * *Note that whitespace at the beginning of the line is\ significant." should yield this output: This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant. When I run it in the interpreter however, I get the following output: 'This is a rather long string containing\nseveral lines of text just as you would do in C.\n Note that whitespace at the beginning of the line is significant.' The interpreter is not reading the "\n" as new lines but is instead printing them. If I just type something like hello = "This is a rather long string containing \ several lines of text." the output is all on one line, like this: "This is a rather long string containing several lines of text." So I know how to combine code that spans multiple lines to be outputted on one line, but I do not know how to make a newline appear in the output upon command (on account of the interpreter not reading the \n). Any suggestions as to why my output varies from the output in the tutorial? I am running a 2.7 version of Python, btw, and the tutorial is running a higher version I believe (3. something). Thank you. -Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Fri Aug 3 22:32:49 2012 From: wprins at gmail.com (Walter Prins) Date: Fri, 3 Aug 2012 21:32:49 +0100 Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On 3 August 2012 19:35, Alan Gauld wrote: > The list doesn't care, you probably did it by hitting Reply > instead of Reply All. > > Reply replies to the person who posted. Reply All replies to all > on the list. Just like regular email. > That's just how its set up, to mimic normal email. Well normally I expect emails from a list (being the direct sender), to go back to the sender, e.g. the list, and emails directly from a person to go back to that person. (Differently put, I expect *any* email to by default go back to the sender, in general, unless I specify otherwise. So if a mailing list sends me an email, my default expectation is that the mail goes back to the list, unless I specify otherwise. This seems perfectly intuitive to me, but hey ho what the hey. :) ) > It's not the only list set up that way, most of the ones I use do it like > that. You get used to it :-) Yes... most of the ones *I* use don't. Snap. Darn. :) (More seriously, I must admit this "feature" remains an nuisance in my book as well. I might add that regardless of current setup, historical precedents or whatever other rationalization one might cite, it nevertheless seems a less than ideal setup choice for this list given that the default behaviour caters for the minority use-case; The majority of the time most people will want their replies to posts on the list to go back to the list, and not back to the individual who posted to the list, since the whole point of the list is to encourage collective discussion. Taking tutor problems off list is generally counter productive both for the people being helped and others who may benefit from the conversation. Therefore the default setup should IMHO be that replies go back to the list, not to the OP.) Anyway, I'll go back to leaving this issue alone again now. Have a good weekend everyone :) Walter From malaclypse2 at gmail.com Fri Aug 3 22:40:54 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 3 Aug 2012 16:40:54 -0400 Subject: [Tutor] Newline question In-Reply-To: References: Message-ID: On Fri, Aug 3, 2012 at 4:18 PM, Alexander Q. wrote: > I'm following the tutorial from python.org > (http://docs.python.org/tutorial/introduction.html) and am having a few > indiscrepancies regarding the new line command. > > The tutorial says that this code > > hello = "This is a rather long string containing\n\ > several lines of text just as you would do in C.\n\ > Note that whitespace at the beginning of the line is\ > significant." > > should yield this output: > > This is a rather long string containing > several lines of text just as you would do in C. > Note that whitespace at the beginning of the line is significant. You left out the other line of code in the tutorial, which says you need to do print(hello) to the the output that is described. Did you do that? If so, it should work fine. If not, what did you do instead? If you just typed: >>>hello at the interpreter prompt, then you are actually seeing the equivalent of print(repr(hello)), instead of print(hello). Can you copy and paste your session for us? Jerry From eryksun at gmail.com Fri Aug 3 22:58:17 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 3 Aug 2012 16:58:17 -0400 Subject: [Tutor] adding a windows registry value In-Reply-To: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On Fri, Aug 3, 2012 at 10:39 AM, Albert-Jan Roskam wrote: > Hi, > > I am trying to change a registry value (Windows 7, Python 2.7) but it won't > work when I try to do this using Python: > > import os > # 1 using this from Python won't work, but double-clicking the file works. > os.system(r"regedit /s C:\Users\Desktop\set_temp.reg") > # 2: using this from Python won't work, but from the commandline or with a > batch file works > os.system("reg add HKEY_CURRENT_USER\Software .....(etc)") > > Why is this not working using Python? Is there a built-in way to do this (I > don't have win32api)? Wouldn't it be simpler to use the _winreg module? http://docs.python.org/library/_winreg.html From redacted@example.com Fri Aug 3 23:09:34 2012 From: redacted@example.com (Alexander Q.) Date: Fri, 3 Aug 2012 14:09:34 -0700 Subject: [Tutor] Newline question In-Reply-To: References: Message-ID: On Fri, Aug 3, 2012 at 1:40 PM, Jerry Hill wrote: > On Fri, Aug 3, 2012 at 4:18 PM, Alexander Q. wrote: > > I'm following the tutorial from python.org > > (http://docs.python.org/tutorial/introduction.html) and am having a few > > indiscrepancies regarding the new line command. > > > > The tutorial says that this code > > > > hello = "This is a rather long string containing\n\ > > several lines of text just as you would do in C.\n\ > > Note that whitespace at the beginning of the line is\ > > significant." > > > > should yield this output: > > > > This is a rather long string containing > > several lines of text just as you would do in C. > > Note that whitespace at the beginning of the line is significant. > > You left out the other line of code in the tutorial, which says you > need to do print(hello) to the the output that is described. Did you > do that? If so, it should work fine. If not, what did you do > instead? If you just typed: > > >>>hello > > at the interpreter prompt, then you are actually seeing the equivalent > of print(repr(hello)), instead of print(hello). > > Can you copy and paste your session for us? > > Jerry > That was it Jerry- when I typed in "print hello" instead of just "hello", the output was exactly like the one in the tutorial. Alternatively, I could accomplish the same type of output by using triple quotes around the same text, except that I would have to format it manually if I want it to come out looking the same way as it did when using "\n" in the previous example? Thanks again for your help. The way I understand it from your explanation is that "hello" does a literal output of everything typed without processing the escape backslashes, while "print hello" does process them. -Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Aug 4 00:12:03 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Aug 2012 23:12:03 +0100 Subject: [Tutor] Newline question In-Reply-To: References: Message-ID: On 03/08/12 22:09, Alexander Q. wrote: > That was it Jerry- when I typed in "print hello" instead of just > "hello", the output was exactly like the one in the tutorial. Yes, the interactive interpreter shows the representation (repr()) of the data while print shows the normal output. There are several places where this difference is significant. > Alternatively, I could accomplish the same type of output by using > triple quotes around the same text, except that I would have to format > it manually if I want it to come out looking the same Nope, that won;t work either, try it: >>> s = """Here is ... a string ... with ... line ... breaks""" >>> s 'Here is \na string\nwith\nline\nbreaks' >>> print s Here is a string with line breaks >>> Again repr() just prints the newline characters. > I understand it from your explanation is that "hello" does a literal > output of everything typed without processing the escape backslashes, > while "print hello" does process them. Yes, and realise that >>> hello only works at the interactive >>> prompt. If you write a program in a file and run it typing hello on a line by itself with have no result. Python will quietly evaluate the string but not display it. The ability to display a value is a debugging aid only available in the interpreter prompt. In a program you need to explicitly call repr(hello) This is why in my tutorial I never actually tell the users about the hello form but always ask them to type the print... More typing but more consistent output, so ultimately less confusion for beginners. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Aug 4 00:28:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Aug 2012 23:28:25 +0100 Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On 03/08/12 21:32, Walter Prins wrote: > Well normally I expect emails from a list (being the direct sender), But there is no concept of 'direct sender' in email. All emails bounce around the internet via innumerable mail relays before arriving. The only thing email protocols care about is the original sender. Reply sends it back to the originator. Reply All sends it to all recipients plus the original sender. > So if a mailing list sends me an email, The list never sends you mail. The list only forwards mail it receives. It's only a slightly specialised form of mail relay or proxy. > Yes... most of the ones *I* use don't. :-) > nevertheless seems a less than ideal setup choice for this list given > that the default behaviour caters for the minority use-case; But my default behaviour is to always use ReplyAll unless I specifically want to talk to the sender only. It's how I reply to all of the emails I receive both at home and at work. And that's how all the people I work with operate too. So the list works exactly like the mails I receive from my friends, my colleagues and lots of spam too! :-( > collective discussion. Taking tutor problems off list is generally > counter productive both for the people being helped and others who may True but I often want to personally thank somebody who has answered a question well or maybe solved one of my issues by explaining something in a new way. It's not relevant to the OP or discussion and would simply clutter up the list. In those cases I use reply. In every other case I use ReplyAll, because that's what I always do for all email unless there's a reason not to. The problem with the reply goes to list approach is that replying to a poster individually becomes very tedious - you have to manually remove the unwanted addresses. Of course the best solution is to have a client that recognises lists and automatically responds appropriately, mutt and Thunderbird both seem to do that well. > Anyway, I'll go back to leaving this issue alone again now. Have a > good weekend everyone :) Its a debate that's been raging for as long as I've been on the list - nearly 15 years :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Sat Aug 4 01:10:02 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 3 Aug 2012 19:10:02 -0400 Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On Fri, Aug 3, 2012 at 6:28 PM, Alan Gauld wrote: > > The problem with the reply goes to list approach is that replying to a > poster individually becomes very tedious - you have to manually remove the > unwanted addresses. > > Of course the best solution is to have a client that recognises lists and > automatically responds appropriately, mutt and Thunderbird both seem to do > that well. I have the Gmail lab enabled that defaults to "reply all", but the default [reasonable] behavior in that case is to reply to the sender and CC everyone else (i.e. the list). I generally don't want that on a list, but I often forget to manually fix it. The list server adds a "List-Post" field. I wish Gmail could use that as the default for reply all, or make it an available option. From bayespokerguy at gmail.com Sat Aug 4 01:13:38 2012 From: bayespokerguy at gmail.com (Fred G) Date: Fri, 3 Aug 2012 19:13:38 -0400 Subject: [Tutor] I-Phone App in Python? Message-ID: I just googled whether it is possible to write an i-phone app in Python and got very confusing, and not super good results. Is it possible? And if so, what module(s) do I need to install? Much thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Sat Aug 4 04:01:40 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 3 Aug 2012 22:01:40 -0400 Subject: [Tutor] adding a windows registry value In-Reply-To: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: On Fri, Aug 3, 2012 at 10:39 AM, Albert-Jan Roskam wrote: > > import os > # 1 using this from Python won't work, but double-clicking the file works. > os.system(r"regedit /s C:\Users\Desktop\set_temp.reg") Do you have a user named Desktop, or was this supposed to be "C:\Users\USER_NAME\Desktop\set_temp.reg"? From steve at pearwood.info Sat Aug 4 04:30:09 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 04 Aug 2012 12:30:09 +1000 Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: <501C8931.5040308@pearwood.info> On 04/08/12 06:32, Walter Prins wrote: > On 3 August 2012 19:35, Alan Gauld wrote: >> The list doesn't care, you probably did it by hitting Reply >> instead of Reply All. >> >> Reply replies to the person who posted. Reply All replies to all >> on the list. Just like regular email. > >> That's just how its set up, to mimic normal email. > > Well normally I expect emails from a list (being the direct sender), > to go back to the sender, e.g. the list, and emails directly from a > person to go back to that person. (Differently put, I expect *any* > email to by default go back to the sender, in general, unless I > specify otherwise. So if a mailing list sends me an email, my default > expectation is that the mail goes back to the list, unless I specify > otherwise. This seems perfectly intuitive to me, but hey ho what the > hey. :) ) The problem with that reasoning is that the list is *not* the sender. It's just a rely that handles list management and delivery. If you reply to a paper letter from Aunt Tilly, would you expect it to be delivered to the postman who delivered it to you? There is a long, acrimonious debate about the behaviour of mailing lists. Some people, like you, want the mailing list to hack the "Reply To" address so that replies go back to the list instead of the sender. The biggest argument in favour of that is simplicity: you just hit "Reply" on any email and the reply goes to the appropriate place: the list for list mail, and the actual sender for private mail. The biggest argument against is that it encourages a particular failure mode, where the recipient goes to make a private reply, says something personal or embarrassing, but forgets to change the address away from the public list. (My personal answer to that is, carelessness is not the mailing list's fault. If you are writing something private and can't be bothered checking who you are sending too, that's your problem.) Others consider that mangling the Reply To address is an abomination, and insist that it is a horrible abuse of Internet standards, and that it's no big deal to just hit Reply All. Which is wrong because it's a pain in the arse to get two copies of every nearly every email. (Some mailing list software is smart enough to not send you a second copy, but most isn't. Some mail clients are smart enough to detect duplicate emails and throw one away, but most don't, and even those that do only do so *after* the email has been downloaded from the server. Also, the problem with the "purity" behaviour is that it encourages n00bs and the careless to take conversations off-list. It's an imperfect world, and neither solution is right all the time. I have gradually moved away from the "lists should change the Reply To address" camp to a third camp, which is to insist on better tools. If your mail client doesn't give you a simple "Reply To List" command, then your mail client is crap. Some non-crap mail programs include Thunderbird, mutt, and Kmail. One crap one is apparently Gmail. See: http://woozle.org/~neale/papers/reply-to-still-harmful.html which I don't entirely agree with -- the author makes claims about what people want, apparently without realising that the reason for the debate is that not all people want the same thing. In my opinion, he panders too much to the careless and stupid -- I have negative sympathy for anyone who sends private mail to a public address because they didn't bother to glance at where it was going before hitting Send. And he fails to consider the obvious answer that if the Reply To address is for the sender to set to whatever they like, all a mailing list need do is make "you agree that replies will go to the list" a condition of joining a list, and then the mailing list software, acting as your agent, is entitled to mangled the Reply To address. And of course, we still have the problem of human laziness and stupidity. It is *astonishing* how many people apparently have problems with the concept: "Before you reply to an email, decide whether you want to reply to the sender alone, the group, or the mailing list." They'll insist on having a choice between 45 different coffees at Starbucks, but can't cope with the choice between 3 different types of reply. -- Steven From maniandram01 at gmail.com Sat Aug 4 05:20:27 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 4 Aug 2012 08:50:27 +0530 Subject: [Tutor] adding a windows registry value In-Reply-To: <501C8931.5040308@pearwood.info> References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> <501C8931.5040308@pearwood.info> Message-ID: You can use the _winreg module. I posted this message earlier but I replied to OP, not replied to all. On 4 August 2012 08:00, Steven D'Aprano wrote: > On 04/08/12 06:32, Walter Prins wrote: > >> On 3 August 2012 19:35, Alan Gauld> >> wrote: >> >>> The list doesn't care, you probably did it by hitting Reply >>> instead of Reply All. >>> >>> Reply replies to the person who posted. Reply All replies to all >>> on the list. Just like regular email. >>> >> >> That's just how its set up, to mimic normal email. >>> >> >> Well normally I expect emails from a list (being the direct sender), >> to go back to the sender, e.g. the list, and emails directly from a >> person to go back to that person. (Differently put, I expect *any* >> email to by default go back to the sender, in general, unless I >> specify otherwise. So if a mailing list sends me an email, my default >> expectation is that the mail goes back to the list, unless I specify >> otherwise. This seems perfectly intuitive to me, but hey ho what the >> hey. :) ) >> > > > The problem with that reasoning is that the list is *not* the sender. It's > just a rely that handles list management and delivery. If you reply to a > paper letter from Aunt Tilly, would you expect it to be delivered to the > postman who delivered it to you? > > There is a long, acrimonious debate about the behaviour of mailing lists. > Some people, like you, want the mailing list to hack the "Reply To" address > so that replies go back to the list instead of the sender. The biggest > argument in favour of that is simplicity: you just hit "Reply" on any email > and the reply goes to the appropriate place: the list for list mail, and > the actual sender for private mail. > > The biggest argument against is that it encourages a particular failure > mode, where the recipient goes to make a private reply, says something > personal or embarrassing, but forgets to change the address away from the > public list. > > (My personal answer to that is, carelessness is not the mailing list's > fault. If you are writing something private and can't be bothered checking > who you are sending too, that's your problem.) > > Others consider that mangling the Reply To address is an abomination, and > insist that it is a horrible abuse of Internet standards, and that it's no > big deal to just hit Reply All. Which is wrong because it's a pain in the > arse to get two copies of every nearly every email. (Some mailing list > software is smart enough to not send you a second copy, but most isn't. > Some mail clients are smart enough to detect duplicate emails and throw one > away, but most don't, and even those that do only do so *after* the email > has been downloaded from the server. > > Also, the problem with the "purity" behaviour is that it encourages n00bs > and the careless to take conversations off-list. > > It's an imperfect world, and neither solution is right all the time. I > have gradually moved away from the "lists should change the Reply To > address" camp to a third camp, which is to insist on better tools. If your > mail client doesn't give you a simple "Reply To List" command, then your > mail client is crap. Some non-crap mail programs include Thunderbird, mutt, > and Kmail. One crap one is apparently Gmail. > > > See: > > http://woozle.org/~neale/**papers/reply-to-still-harmful.**html > > which I don't entirely agree with -- the author makes claims about what > people want, apparently without realising that the reason for the debate is > that not all people want the same thing. In my opinion, he panders too much > to the careless and stupid -- I have negative sympathy for anyone who sends > private mail to a public address because they didn't bother to glance at > where it was going before hitting Send. And he fails to consider the > obvious answer that if the Reply To address is for the sender to set to > whatever they like, all a mailing list need do is make "you agree that > replies will go to the list" a condition of joining a list, and then the > mailing list software, acting as your agent, is entitled to mangled the > Reply To address. > > And of course, we still have the problem of human laziness and stupidity. > It is *astonishing* how many people apparently have problems with the > concept: > > "Before you reply to an email, decide whether you want to reply to the > sender alone, the group, or the mailing list." > > They'll insist on having a choice between 45 different coffees at > Starbucks, but can't cope with the choice between 3 different types of > reply. > > > > -- > Steven > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From maniandram01 at gmail.com Sat Aug 4 05:20:49 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 4 Aug 2012 08:50:49 +0530 Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> <501C8931.5040308@pearwood.info> Message-ID: I was the first person to respond actually. On 4 August 2012 08:50, Ramchandra Apte wrote: > You can use the _winreg > module. > I posted this message earlier but I replied to OP, not replied to all. > > > On 4 August 2012 08:00, Steven D'Aprano wrote: > >> On 04/08/12 06:32, Walter Prins wrote: >> >>> On 3 August 2012 19:35, Alan Gauld> >>> wrote: >>> >>>> The list doesn't care, you probably did it by hitting Reply >>>> instead of Reply All. >>>> >>>> Reply replies to the person who posted. Reply All replies to all >>>> on the list. Just like regular email. >>>> >>> >>> That's just how its set up, to mimic normal email. >>>> >>> >>> Well normally I expect emails from a list (being the direct sender), >>> to go back to the sender, e.g. the list, and emails directly from a >>> person to go back to that person. (Differently put, I expect *any* >>> email to by default go back to the sender, in general, unless I >>> specify otherwise. So if a mailing list sends me an email, my default >>> expectation is that the mail goes back to the list, unless I specify >>> otherwise. This seems perfectly intuitive to me, but hey ho what the >>> hey. :) ) >>> >> >> >> The problem with that reasoning is that the list is *not* the sender. >> It's just a rely that handles list management and delivery. If you reply to >> a paper letter from Aunt Tilly, would you expect it to be delivered to the >> postman who delivered it to you? >> >> There is a long, acrimonious debate about the behaviour of mailing lists. >> Some people, like you, want the mailing list to hack the "Reply To" address >> so that replies go back to the list instead of the sender. The biggest >> argument in favour of that is simplicity: you just hit "Reply" on any email >> and the reply goes to the appropriate place: the list for list mail, and >> the actual sender for private mail. >> >> The biggest argument against is that it encourages a particular failure >> mode, where the recipient goes to make a private reply, says something >> personal or embarrassing, but forgets to change the address away from the >> public list. >> >> (My personal answer to that is, carelessness is not the mailing list's >> fault. If you are writing something private and can't be bothered checking >> who you are sending too, that's your problem.) >> >> Others consider that mangling the Reply To address is an abomination, and >> insist that it is a horrible abuse of Internet standards, and that it's no >> big deal to just hit Reply All. Which is wrong because it's a pain in the >> arse to get two copies of every nearly every email. (Some mailing list >> software is smart enough to not send you a second copy, but most isn't. >> Some mail clients are smart enough to detect duplicate emails and throw one >> away, but most don't, and even those that do only do so *after* the email >> has been downloaded from the server. >> >> Also, the problem with the "purity" behaviour is that it encourages n00bs >> and the careless to take conversations off-list. >> >> It's an imperfect world, and neither solution is right all the time. I >> have gradually moved away from the "lists should change the Reply To >> address" camp to a third camp, which is to insist on better tools. If your >> mail client doesn't give you a simple "Reply To List" command, then your >> mail client is crap. Some non-crap mail programs include Thunderbird, mutt, >> and Kmail. One crap one is apparently Gmail. >> >> >> See: >> >> http://woozle.org/~neale/**papers/reply-to-still-harmful.**html >> >> which I don't entirely agree with -- the author makes claims about what >> people want, apparently without realising that the reason for the debate is >> that not all people want the same thing. In my opinion, he panders too much >> to the careless and stupid -- I have negative sympathy for anyone who sends >> private mail to a public address because they didn't bother to glance at >> where it was going before hitting Send. And he fails to consider the >> obvious answer that if the Reply To address is for the sender to set to >> whatever they like, all a mailing list need do is make "you agree that >> replies will go to the list" a condition of joining a list, and then the >> mailing list software, acting as your agent, is entitled to mangled the >> Reply To address. >> >> And of course, we still have the problem of human laziness and stupidity. >> It is *astonishing* how many people apparently have problems with the >> concept: >> >> "Before you reply to an email, decide whether you want to reply to the >> sender alone, the group, or the mailing list." >> >> They'll insist on having a choice between 45 different coffees at >> Starbucks, but can't cope with the choice between 3 different types of >> reply. >> >> >> >> -- >> Steven >> >> ______________________________**_________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/**mailman/listinfo/tutor >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From redacted@example.com Sat Aug 4 08:47:42 2012 From: redacted@example.com (Alexander Q.) Date: Fri, 3 Aug 2012 23:47:42 -0700 Subject: [Tutor] Newline question In-Reply-To: References: Message-ID: On Fri, Aug 3, 2012 at 2:09 PM, Alexander Q. wrote: > > > On Fri, Aug 3, 2012 at 1:40 PM, Jerry Hill wrote: > >> On Fri, Aug 3, 2012 at 4:18 PM, Alexander Q. >> wrote: >> > I'm following the tutorial from python.org >> > (http://docs.python.org/tutorial/introduction.html) and am having a few >> > indiscrepancies regarding the new line command. >> > >> > The tutorial says that this code >> > >> > hello = "This is a rather long string containing\n\ >> > several lines of text just as you would do in C.\n\ >> > Note that whitespace at the beginning of the line is\ >> > significant." >> > >> > should yield this output: >> > >> > This is a rather long string containing >> > several lines of text just as you would do in C. >> > Note that whitespace at the beginning of the line is significant. >> >> You left out the other line of code in the tutorial, which says you >> need to do print(hello) to the the output that is described. Did you >> do that? If so, it should work fine. If not, what did you do >> instead? If you just typed: >> >> >>>hello >> >> at the interpreter prompt, then you are actually seeing the equivalent >> of print(repr(hello)), instead of print(hello). >> >> Can you copy and paste your session for us? >> >> Jerry >> > > That was it Jerry- when I typed in "print hello" instead of just "hello", > the output was exactly like the one in the tutorial. Alternatively, I could > accomplish the same type of output by using triple quotes around the same > text, except that I would have to format it manually if I want it to come > out looking the same way as it did when using "\n" in the previous example? > Thanks again for your help. The way I understand it from your explanation > is that "hello" does a literal output of everything typed without > processing the escape backslashes, while "print hello" does process them. > > -Alex > > Yes, thanks Matt- I realized my mistake soon after sending the email. -Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From alonzo.quijote at gmail.com Sat Aug 4 08:58:26 2012 From: alonzo.quijote at gmail.com (Alonzo Quijote) Date: Fri, 3 Aug 2012 23:58:26 -0700 Subject: [Tutor] Recursive assignment in nested lists Message-ID: Is there a way to define a function which takes a list (of lists), a position specified by a list of integers [i0,i1,...,in], and a value and returns the result of setting list[i0][i1]...[in]=value The following function works for positions up to length 3 only. Is it possible to write a general function that does this? def setValueAtPosition(list,pos,value): if len(pos)==1: list[pos[0]]=value elif len(pos)==2: list[pos[0]][pos[1]]=value elif len(pos)==3: list[pos[0]][pos[1]][pos[2]]=value return list For example >>> aa=[1,2,[3,4]] >>> setValueAtPosition(aa,[2,0],5) [1, 2, [5, 4]] >>> aa [1, 2, [5, 4]] From punchagan at gmail.com Sat Aug 4 09:20:03 2012 From: punchagan at gmail.com (Puneeth Chaganti) Date: Sat, 4 Aug 2012 12:50:03 +0530 Subject: [Tutor] Recursive assignment in nested lists In-Reply-To: References: Message-ID: On Sat, Aug 4, 2012 at 12:28 PM, Alonzo Quijote wrote: > Is there a way to define a function which takes > a list (of lists), > a position specified by a list of integers [i0,i1,...,in], and > a value > and returns the result of setting > list[i0][i1]...[in]=value > > The following function works for positions up to length 3 only. > Is it possible to write a general function that does this? > > def setValueAtPosition(list,pos,value): > if len(pos)==1: > list[pos[0]]=value > elif len(pos)==2: > list[pos[0]][pos[1]]=value > elif len(pos)==3: > list[pos[0]][pos[1]][pos[2]]=value > return list Something like this, should work : def setValueAtPosition(my_list, pos, value): sub_list = my_list for i in pos[:-1]: sub_list = sub_list[i] sub_list[pos[-1]] = value return my_list -- Puneeth From steve at pearwood.info Sat Aug 4 09:27:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 04 Aug 2012 17:27:54 +1000 Subject: [Tutor] Recursive assignment in nested lists In-Reply-To: References: Message-ID: <501CCEFA.1000502@pearwood.info> On 04/08/12 16:58, Alonzo Quijote wrote: > Is there a way to define a function which takes > a list (of lists), > a position specified by a list of integers [i0,i1,...,in], and > a value > and returns the result of setting > list[i0][i1]...[in]=value Yes it is possible, but if you need this, you should strongly consider rearranging your data so it is easier to work with. But, for what it's worth, try this: def setValueAtPosition(list, pos, value): tmp = list for i in pos[:-1]: tmp = tmp[i] tmp[pos[-1]] = value There's no need to return the list argument, because it changes it in-place. And here is your example: py> aa=[1, 2, [3, 4]] py> setValueAtPosition(aa, [2, 0], 5) py> aa [1, 2, [5, 4]] And a more complicated example: py> L = [0, 1, 2, [3, 4, [5, 6, [7, [8, 9, 10], 11], 12], 13, 14], 15] py> setValueAtPosition(L, [3, 2, 2, 1, 0], "Surprise!") py> L [0, 1, 2, [3, 4, [5, 6, [7, ['Surprise!', 9, 10], 11], 12], 13, 14], 15] But really, don't do this. Such a thing is hard to understand, hard to use, hard to maintain, and hard to debug when you have a problem. Try to avoid having deeply nested lists like that, your life will be much simpler. -- Steven From __peter__ at web.de Sat Aug 4 09:43:03 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 04 Aug 2012 09:43:03 +0200 Subject: [Tutor] Recursive assignment in nested lists References: Message-ID: Alonzo Quijote wrote: > Is there a way to define a function which takes > a list (of lists), > a position specified by a list of integers [i0,i1,...,in], and > a value > and returns the result of setting > list[i0][i1]...[in]=value > > The following function works for positions up to length 3 only. > Is it possible to write a general function that does this? > > def setValueAtPosition(list,pos,value): > if len(pos)==1: > list[pos[0]]=value > elif len(pos)==2: > list[pos[0]][pos[1]]=value > elif len(pos)==3: > list[pos[0]][pos[1]][pos[2]]=value > return list > > For example >>>> aa=[1,2,[3,4]] > >>>> setValueAtPosition(aa,[2,0],5) > [1, 2, [5, 4]] > >>>> aa > [1, 2, [5, 4]] You have been shown working solutions, but for the learning experience it will be instructive to try and come up with a recursive solution. Here's a sketch: def setValueAtPosition(list, pos, value): if len(pos) == 1: list[pos[0]] = value else: inner_list = ... new_pos = ... setValueAtPosition(inner_list, new_pos, value) Can you fill in the blanks? From pasokan at talentsprint.com Sat Aug 4 09:44:09 2012 From: pasokan at talentsprint.com (Asokan Pichai) Date: Sat, 4 Aug 2012 13:14:09 +0530 Subject: [Tutor] Recursive assignment in nested lists In-Reply-To: References: Message-ID: On Sat, Aug 4, 2012 at 12:28 PM, Alonzo Quijote wrote: > Is there a way to define a function which takes > a list (of lists), > a position specified by a list of integers [i0,i1,...,in], and > a value > and returns the result of setting > list[i0][i1]...[in]=value > > The following function works for positions up to length 3 only. > Is it possible to write a general function that does this? > > def setValueAtPosition(list,pos,value): > if len(pos)==1: > list[pos[0]]=value > elif len(pos)==2: > list[pos[0]][pos[1]]=value > elif len(pos)==3: > list[pos[0]][pos[1]][pos[2]]=value > return list > > For example >>>> aa=[1,2,[3,4]] > >>>> setValueAtPosition(aa,[2,0],5) > [1, 2, [5, 4]] > >>>> aa > [1, 2, [5, 4]] > > _______________________________________________ Seems very odd requirement. Anyway here is one way: --------------- def setValue(NestedList, pos, value): nl = NestedList for p in pos[:-1]: nl = nl[p] nl[pos[-1]] = value return NestedList ------------------------- Asokan Pichai If a language is designed for non-programmers, soon only non-programs get written in it. --- Anonymouse From kwpolska at gmail.com Sat Aug 4 10:57:30 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 4 Aug 2012 10:57:30 +0200 Subject: [Tutor] I-Phone App in Python? In-Reply-To: References: Message-ID: On Sat, Aug 4, 2012 at 1:13 AM, Fred G wrote: > I just googled whether it is possible to write an i-phone app in Python and > got very confusing, and not super good results. > > Is it possible? And if so, what module(s) do I need to install? > > Much thanks! > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > iPhone* and it isn't very likely. There is RubyMotion for Ruby (a different language, which is also popular nowadays), but it costs $199.99. But you also need to pay the iPhone Developer fee of $99. And, of course, you need a Mac, no matter what. Thus, it?s cheaper to learn and write in Objective-C. Or for Android. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From fomcl at yahoo.com Sat Aug 4 12:55:51 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 4 Aug 2012 03:55:51 -0700 (PDT) Subject: [Tutor] adding a windows registry value In-Reply-To: References: <1344004775.90789.YahooMailNeo@web110710.mail.gq1.yahoo.com> Message-ID: <1344077751.47038.YahooMailNeo@web110701.mail.gq1.yahoo.com> Hi, Thanks to all who replied, also those who replied off-list. Somebody else confirmed that the code was working as expected on another machine, so it's probably a rights issue. Still nice to have a look at _winreg though. ? Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? >________________________________ > From: eryksun >To: Albert-Jan Roskam >Cc: Python Mailing List >Sent: Friday, August 3, 2012 10:58 PM >Subject: Re: [Tutor] adding a windows registry value > >On Fri, Aug 3, 2012 at 10:39 AM, Albert-Jan Roskam wrote: >> Hi, >> >> I am trying to change a registry value (Windows 7, Python 2.7) but it won't >> work when I try to do this using Python: >> >> import os >> # 1 using this from Python won't work, but double-clicking the file works. >> os.system(r"regedit /s C:\Users\Desktop\set_temp.reg") >> # 2: using this from Python won't work, but from the commandline or with a >> batch file works >> os.system("reg add HKEY_CURRENT_USER\Software .....(etc)") >> >> Why is this not working using Python? Is there a built-in way to do this (I >> don't have win32api)? > >Wouldn't it be simpler to use the? _winreg module? > >http://docs.python.org/library/_winreg.html > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eire1130 at gmail.com Sat Aug 4 20:18:53 2012 From: eire1130 at gmail.com (James Reynolds) Date: Sat, 4 Aug 2012 14:18:53 -0400 Subject: [Tutor] I-Phone App in Python? In-Reply-To: References: Message-ID: <63E00C4F-3E75-44D4-BE96-841D157F835B@gmail.com> Instagram is written in python django. Sent from my iPad On Aug 3, 2012, at 7:13 PM, Fred G wrote: > I just googled whether it is possible to write an i-phone app in Python and got very confusing, and not super good results. > > Is it possible? And if so, what module(s) do I need to install? > > Much thanks! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From eire1130 at gmail.com Sat Aug 4 20:26:26 2012 From: eire1130 at gmail.com (James Reynolds) Date: Sat, 4 Aug 2012 14:26:26 -0400 Subject: [Tutor] I-Phone App in Python? In-Reply-To: References: Message-ID: <7EA1BB55-4203-48E4-BC9D-0F3DE3033787@gmail.com> To clarify, that is their server side arc. The native app itself... I'm not sure what is written in. Probably java if I had to guess. In making an app, most of your work is going to be backend in any event. Also, what happens when an android user wants to download your app? Sent from my iPad On Aug 3, 2012, at 7:13 PM, Fred G wrote: > I just googled whether it is possible to write an i-phone app in Python and got very confusing, and not super good results. > > Is it possible? And if so, what module(s) do I need to install? > > Much thanks! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From chris at robotninja.net Sat Aug 4 20:28:07 2012 From: chris at robotninja.net (Chris Fox) Date: Sat, 04 Aug 2012 19:28:07 +0100 Subject: [Tutor] I-Phone App in Python? In-Reply-To: <63E00C4F-3E75-44D4-BE96-841D157F835B@gmail.com> References: <63E00C4F-3E75-44D4-BE96-841D157F835B@gmail.com> Message-ID: <501D69B7.9000705@robotninja.net> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 04/08/2012 19:18, James Reynolds wrote: > Instagram is written in python django. > Even if that were true (Django is, as I understand it, a web framework) - how does that information help the original poster? Chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.17 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAlAdabcACgkQ/UgLKfoJxI5f9QCgwA1LR2S0EqUpxWqlFaGE9Dl6 yGEAoLqYlTDn6kZWrZ+PN20uqpmsSbPJ =Oeru -----END PGP SIGNATURE----- From kwpolska at gmail.com Sat Aug 4 20:28:58 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 4 Aug 2012 20:28:58 +0200 Subject: [Tutor] I-Phone App in Python? In-Reply-To: <63E00C4F-3E75-44D4-BE96-841D157F835B@gmail.com> References: <63E00C4F-3E75-44D4-BE96-841D157F835B@gmail.com> Message-ID: On Sat, Aug 4, 2012 at 8:18 PM, James Reynolds wrote: > Instagram is written in python django. > > Sent from my iPad > > On Aug 3, 2012, at 7:13 PM, Fred G wrote: > >> I just googled whether it is possible to write an i-phone app in Python and got very confusing, and not super good results. >> >> Is it possible? And if so, what module(s) do I need to install? >> >> Much thanks! >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Sure, the website can be written using Python/Django. But a native app cannot. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From kwpolska at gmail.com Sat Aug 4 20:57:02 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 4 Aug 2012 20:57:02 +0200 Subject: [Tutor] I-Phone App in Python? In-Reply-To: <7EA1BB55-4203-48E4-BC9D-0F3DE3033787@gmail.com> References: <7EA1BB55-4203-48E4-BC9D-0F3DE3033787@gmail.com> Message-ID: On Sat, Aug 4, 2012 at 8:26 PM, James Reynolds wrote: > To clarify, that is their server side arc. The native app itself... I'm not sure what is written in. Probably java if I had to guess. > > In making an app, most of your work is going to be backend in any event. > > Also, what happens when an android user wants to download your app? > > Sent from my iPad > > On Aug 3, 2012, at 7:13 PM, Fred G wrote: > >> I just googled whether it is possible to write an i-phone app in Python and got very confusing, and not super good results. >> >> Is it possible? And if so, what module(s) do I need to install? >> >> Much thanks! >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor 1. Objective-C. You are forced to use it in iOS. 2. Android? You need to write another version of your app and $25*! How ?bout Windows Phone (a piece of crap)? Another one, and you need Windows and $99! MeeGo? Another version and ?1*. Symbian? One more, but you paid your euro when you made the MeeGo version? Blackberry? Another version, this time for free. But wait, there?s more! You also need some phones to test your apps, and they aren?t free! Of course, you can learn from the web for free, but materials for some programming languages are mediocre and you need some books! Order right now, and we?ll? oh wait, this is not Billy Mays. Anyways, you should pass at least five grand by now. And sorry for a bit of OT, but I had to. * Android and Nokia/Ovi are per lifetime AFAIK, others are per year, including iOS. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 | Arch Linux x86_64, zsh, mutt, vim. # vim:set textwidth=70: From alonzo.quijote at gmail.com Sat Aug 4 21:44:11 2012 From: alonzo.quijote at gmail.com (Alonzo Quijote) Date: Sat, 4 Aug 2012 12:44:11 -0700 Subject: [Tutor] Recursive assignment in nested lists In-Reply-To: References: Message-ID: Thanks for all the help with this. I have 2 very quick follow-up questions: --- 1. Several responses proposed code like this: def setValueAtPosition(list, pos, value): tmp = list for i in pos[:-1]: tmp = tmp[i] tmp[pos[-1]] = value There must be a good reason that the responders use a tmp variable like this? But I notice that the same effects can be obtained with: def setValueAtPosition2(list, pos, value): for i in pos[:-1]: list = list[i] list[pos[-1]] = value Is there something wrong with this latter approach? --- 2. Another response observes that the function can be defined recursively like this: def setValueAtPosition3(list, pos, value): if len(pos) == 1: list[pos[0]] = value else: inner_list = list[pos[0]] new_pos = pos[1:] setValueAtPosition3(inner_list, new_pos, value) Are the non-recursive solutions better? --- Thanks very much again, in advance, Alonzo On Aug 4, 2012, at 12:43 AM, Peter Otten <__peter__ at web.de> wrote: > Alonzo Quijote wrote: > >> Is there a way to define a function which takes >> a list (of lists), >> a position specified by a list of integers [i0,i1,...,in], and >> a value >> and returns the result of setting >> list[i0][i1]...[in]=value >> >> The following function works for positions up to length 3 only. >> Is it possible to write a general function that does this? >> >> def setValueAtPosition(list,pos,value): >> if len(pos)==1: >> list[pos[0]]=value >> elif len(pos)==2: >> list[pos[0]][pos[1]]=value >> elif len(pos)==3: >> list[pos[0]][pos[1]][pos[2]]=value >> return list >> >> For example >>>>> aa=[1,2,[3,4]] >> >>>>> setValueAtPosition(aa,[2,0],5) >> [1, 2, [5, 4]] >> >>>>> aa >> [1, 2, [5, 4]] And a more complicated example: py> L = [0, 1, 2, [3, 4, [5, 6, [7, [8, 9, 10], 11], 12], 13, 14], 15] py> setValueAtPosition(L, [3, 2, 2, 1, 0], "Surprise!") py> L [0, 1, 2, [3, 4, [5, 6, [7, ['Surprise!', 9, 10], 11], 12], 13, 14], 15] > > You have been shown working solutions, but for the learning experience it > will be instructive to try and come up with a recursive solution. Here's a > sketch: > > def setValueAtPosition(list, pos, value): > if len(pos) == 1: > list[pos[0]] = value > else: > inner_list = ... > new_pos = ... > setValueAtPosition(inner_list, new_pos, value) > > Can you fill in the blanks? > From eire1130 at gmail.com Sat Aug 4 21:57:12 2012 From: eire1130 at gmail.com (James Reynolds) Date: Sat, 4 Aug 2012 15:57:12 -0400 Subject: [Tutor] I-Phone App in Python? In-Reply-To: <501D69B7.9000705@robotninja.net> References: <63E00C4F-3E75-44D4-BE96-841D157F835B@gmail.com> <501D69B7.9000705@robotninja.net> Message-ID: the "native app" part of building an "app" is somewhat trivial in comparison to the backend needs. Read this article here on what instagram uses for their architecture: http://instagram-engineering.tumblr.com/post/13649370142/what-powers-instagram-hundreds-of-instances-dozens-of As you can see, most of it is Python libraries, some of which are django specific, some of which aren't (note, theya are using custom written task system instead of using Celery - not sure why this is). As I understand it, interpreted languages on phones simply take up too much space and memory. I know some companies have used http://www.appcelerator.com/ with good success That said, unless all you want to your app to do is only interact with the user from within the phone and never get information from outside of it or send information outside of it, then I suppose you won't be able to use Python to any degree. But, if you do, then you can write fantastic apps using python libraries for the backend needs. On Sat, Aug 4, 2012 at 2:28 PM, Chris Fox wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 04/08/2012 19:18, James Reynolds wrote: > > Instagram is written in python django. > > > > Even if that were true (Django is, as I understand it, a web > framework) - how does that information help the original poster? > > Chris > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v2.0.17 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAlAdabcACgkQ/UgLKfoJxI5f9QCgwA1LR2S0EqUpxWqlFaGE9Dl6 > yGEAoLqYlTDn6kZWrZ+PN20uqpmsSbPJ > =Oeru > -----END PGP SIGNATURE----- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Aug 4 22:48:06 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 04 Aug 2012 21:48:06 +0100 Subject: [Tutor] Recursive assignment in nested lists In-Reply-To: References: Message-ID: On 04/08/12 20:44, Alonzo Quijote wrote: > There must be a good reason that the responders use a tmp variable like this? > But I notice that the same effects can be obtained with: > > def setValueAtPosition2(list, pos, value): > for i in pos[:-1]: > list = list[i] > list[pos[-1]] = value > > Is there something wrong with this latter approach? Not for the specific case but the problem comes because you lost the reference to the original list which is usually a bad idea, especially if you decide you need to do anything with it. > 2. Another response observes that the function can be defined recursively like this: > Are the non-recursive solutions better? Define "better". Recursive functions in Python have a limit on the recursion depth so for a very long list it will break. Recursion also tends to use more memory. Use recursion where you have shallow data structures or very complex structures which are themselves recursive - then rewrite it without recursion, but only if necessary due to the above limitations. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fsalamero at gmail.com Sat Aug 4 23:02:46 2012 From: fsalamero at gmail.com (Fernando Salamero Pelay) Date: Sat, 4 Aug 2012 23:02:46 +0200 Subject: [Tutor] I-Phone App in Python? In-Reply-To: References: Message-ID: Codea is an iOS app for program in Lua. There is a framewok to port to Objective-C. For python there is the app Pythonista. May be the programmer will follow this way. On the other hand, there is multiplatform framework (iOS included) that supports python: kivy.org , I believe. (Enviado desde un dispositivo m?vil) From richkappler at gmail.com Sun Aug 5 03:11:52 2012 From: richkappler at gmail.com (richard kappler) Date: Sat, 4 Aug 2012 21:11:52 -0400 Subject: [Tutor] filelist Message-ID: Starting to work through "Programming Computer Vision with Python" in my -summer of learning python- quest. As I read through the intro to the PIL library, I came across the below code. When I read it, I said to my self "I don't see how that calls a set of files, there's no specificity. How does that know where to call from?" Then I convinced myself that, because I'm a beginner, I must be missing something, and ran it through the interpreter. Of course, I got an error message telling me filelist isn't defined. But now I'm kinda lost. If the directory holding the pics I want to work on is called practicephotos, should I declare something along the lines of filelist = ~/practicephotos/ or what? regards, Richard [code] from PIL import Image import os for infile in filelist: outfile - os.path.splitext(infile)[0] + ".jpg" if infile != outfile: try: Image.open(infile).save(outfile) except IOError: print "cannot convert", infile [/code] -- "Treat all disasters as if they were trivialities but never treat a triviality as if it were a disaster." -- *Quentin Crisp * -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sun Aug 5 03:37:21 2012 From: d at davea.name (Dave Angel) Date: Sat, 04 Aug 2012 21:37:21 -0400 Subject: [Tutor] filelist In-Reply-To: References: Message-ID: <501DCE51.7040304@davea.name> On 08/04/2012 09:11 PM, richard kappler wrote: > Starting to work through "Programming Computer Vision with Python" in my > -summer of learning python- quest. Congratulations; that's a worthy goal. > As I read through the intro to the PIL > library, I came across the below code. When I read it, I said to my self > "I don't see how that calls a set of files, there's no specificity. How > does that know where to call from?" Then I convinced myself that, because > I'm a beginner, I must be missing something, and ran it through the > interpreter. Of course, I got an error message telling me filelist isn't > defined. But now I'm kinda lost. > > If the directory holding the pics I want to work on is called > practicephotos, should I declare something along the lines of filelist = > ~/practicephotos/ or what? > > regards, Richard > > [code] > from PIL import Image > import os > > for infile in filelist: > outfile - os.path.splitext(infile)[0] + ".jpg" > if infile != outfile: > try: > Image.open(infile).save(outfile) > except IOError: > print "cannot convert", infile > [/code] > > As you have realized, the code is incomplete. The loop is expecting filelist to be already initialized to a list of strings, with each string being a filename. One way to get such a list (from the commandline) would be to use import sys filelist = sys.argv[1:] While I've got your attention, please send your messages here as plain text. The indentation of your html message is lost in the plain text version used in this forum. -- DaveA From bayespokerguy at gmail.com Sun Aug 5 05:15:59 2012 From: bayespokerguy at gmail.com (Fred G) Date: Sat, 4 Aug 2012 23:15:59 -0400 Subject: [Tutor] I-Phone App in Python? Message-ID: Thanks guys. Sounds like I'll be learning Objective C in the near future! On Sat, Aug 4, 2012 at 2:57 PM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: adding a windows registry value (Albert-Jan Roskam) > 2. Re: I-Phone App in Python? (James Reynolds) > 3. Re: I-Phone App in Python? (James Reynolds) > 4. Re: I-Phone App in Python? (Chris Fox) > 5. Re: I-Phone App in Python? (Kwpolska) > 6. Re: I-Phone App in Python? (Kwpolska) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sat, 4 Aug 2012 03:55:51 -0700 (PDT) > From: Albert-Jan Roskam > To: eryksun > Cc: Python Mailing List > Subject: Re: [Tutor] adding a windows registry value > Message-ID: > <1344077751.47038.YahooMailNeo at web110701.mail.gq1.yahoo.com> > Content-Type: text/plain; charset="iso-8859-1" > > Hi, > > Thanks to all who replied, also those who replied off-list. Somebody else > confirmed that the code was working as expected on another machine, so it's > probably a rights issue. Still nice to have a look at _winreg though. > > ? > Regards, > Albert-Jan > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > All right, but apart from the sanitation, the medicine, education, wine, > public order, irrigation, roads, a > fresh water system, and public health, what have the Romans ever done for > us? > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? > > > >________________________________ > > From: eryksun > >To: Albert-Jan Roskam > >Cc: Python Mailing List > >Sent: Friday, August 3, 2012 10:58 PM > >Subject: Re: [Tutor] adding a windows registry value > > > >On Fri, Aug 3, 2012 at 10:39 AM, Albert-Jan Roskam > wrote: > >> Hi, > >> > >> I am trying to change a registry value (Windows 7, Python 2.7) but it > won't > >> work when I try to do this using Python: > >> > >> import os > >> # 1 using this from Python won't work, but double-clicking the file > works. > >> os.system(r"regedit /s C:\Users\Desktop\set_temp.reg") > >> # 2: using this from Python won't work, but from the commandline or > with a > >> batch file works > >> os.system("reg add HKEY_CURRENT_USER\Software .....(etc)") > >> > >> Why is this not working using Python? Is there a built-in way to do > this (I > >> don't have win32api)? > > > >Wouldn't it be simpler to use the? _winreg module? > > > >http://docs.python.org/library/_winreg.html > > > > > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20120804/44369c90/attachment-0001.html > > > > ------------------------------ > > Message: 2 > Date: Sat, 4 Aug 2012 14:18:53 -0400 > From: James Reynolds > To: Fred G > Cc: "tutor at python.org" > Subject: Re: [Tutor] I-Phone App in Python? > Message-ID: <63E00C4F-3E75-44D4-BE96-841D157F835B at gmail.com> > Content-Type: text/plain; charset=us-ascii > > Instagram is written in python django. > > Sent from my iPad > > On Aug 3, 2012, at 7:13 PM, Fred G wrote: > > > I just googled whether it is possible to write an i-phone app in Python > and got very confusing, and not super good results. > > > > Is it possible? And if so, what module(s) do I need to install? > > > > Much thanks! > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > Message: 3 > Date: Sat, 4 Aug 2012 14:26:26 -0400 > From: James Reynolds > To: Fred G > Cc: "tutor at python.org" > Subject: Re: [Tutor] I-Phone App in Python? > Message-ID: <7EA1BB55-4203-48E4-BC9D-0F3DE3033787 at gmail.com> > Content-Type: text/plain; charset=us-ascii > > To clarify, that is their server side arc. The native app itself... I'm > not sure what is written in. Probably java if I had to guess. > > In making an app, most of your work is going to be backend in any event. > > Also, what happens when an android user wants to download your app? > > Sent from my iPad > > On Aug 3, 2012, at 7:13 PM, Fred G wrote: > > > I just googled whether it is possible to write an i-phone app in Python > and got very confusing, and not super good results. > > > > Is it possible? And if so, what module(s) do I need to install? > > > > Much thanks! > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > Message: 4 > Date: Sat, 04 Aug 2012 19:28:07 +0100 > From: Chris Fox > To: tutor at python.org > Subject: Re: [Tutor] I-Phone App in Python? > Message-ID: <501D69B7.9000705 at robotninja.net> > Content-Type: text/plain; charset=ISO-8859-1 > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 04/08/2012 19:18, James Reynolds wrote: > > Instagram is written in python django. > > > > Even if that were true (Django is, as I understand it, a web > framework) - how does that information help the original poster? > > Chris > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v2.0.17 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAlAdabcACgkQ/UgLKfoJxI5f9QCgwA1LR2S0EqUpxWqlFaGE9Dl6 > yGEAoLqYlTDn6kZWrZ+PN20uqpmsSbPJ > =Oeru > -----END PGP SIGNATURE----- > > > ------------------------------ > > Message: 5 > Date: Sat, 4 Aug 2012 20:28:58 +0200 > From: Kwpolska > To: tutor at python.org > Subject: Re: [Tutor] I-Phone App in Python? > Message-ID: > < > CAMw+j7Kvw3dy+1jA_wR5+ENePiM4e3Dpk_7idZ08C1dCRu2Qtg at mail.gmail.com> > Content-Type: text/plain; charset=UTF-8 > > On Sat, Aug 4, 2012 at 8:18 PM, James Reynolds wrote: > > Instagram is written in python django. > > > > Sent from my iPad > > > > On Aug 3, 2012, at 7:13 PM, Fred G wrote: > > > >> I just googled whether it is possible to write an i-phone app in Python > and got very confusing, and not super good results. > >> > >> Is it possible? And if so, what module(s) do I need to install? > >> > >> Much thanks! > >> _______________________________________________ > >> Tutor maillist - Tutor at python.org > >> To unsubscribe or change subscription options: > >> http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > Sure, the website can be written using Python/Django. But a native app > cannot. > > -- > Kwpolska > stop html mail | always bottom-post > www.asciiribbon.org | www.netmeister.org/news/learn2quote.html > GPG KEY: 5EAAEA16 | Arch Linux x86_64, zsh, mutt, vim. > # vim:set textwidth=70: > > > ------------------------------ > > Message: 6 > Date: Sat, 4 Aug 2012 20:57:02 +0200 > From: Kwpolska > To: tutor at python.org > Subject: Re: [Tutor] I-Phone App in Python? > Message-ID: > < > CAMw+j7KyxcU20vE41iWKcRuGKSUv-fOrBEsC-QgSVEogdhR06g at mail.gmail.com> > Content-Type: text/plain; charset=UTF-8 > > On Sat, Aug 4, 2012 at 8:26 PM, James Reynolds wrote: > > To clarify, that is their server side arc. The native app itself... I'm > not sure what is written in. Probably java if I had to guess. > > > > In making an app, most of your work is going to be backend in any event. > > > > Also, what happens when an android user wants to download your app? > > > > Sent from my iPad > > > > On Aug 3, 2012, at 7:13 PM, Fred G wrote: > > > >> I just googled whether it is possible to write an i-phone app in Python > and got very confusing, and not super good results. > >> > >> Is it possible? And if so, what module(s) do I need to install? > >> > >> Much thanks! > >> _______________________________________________ > >> Tutor maillist - Tutor at python.org > >> To unsubscribe or change subscription options: > >> http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > 1. Objective-C. You are forced to use it in iOS. > 2. Android? You need to write another version of your app and $25*! > How ?bout Windows Phone (a piece of crap)? Another one, and you need > Windows and $99! MeeGo? Another version and ?1*. Symbian? One > more, but you paid your euro when you made the MeeGo version? > Blackberry? Another version, this time for free. But wait, there?s > more! You also need some phones to test your apps, and they aren?t > free! Of course, you can learn from the web for free, but materials > for some programming languages are mediocre and you need some books! > Order right now, and we?ll? oh wait, this is not Billy Mays. Anyways, > you should pass at least five grand by now. > > And sorry for a bit of OT, but I had to. > > * Android and Nokia/Ovi are per lifetime AFAIK, others are per year, > including iOS. > -- > Kwpolska > stop html mail | always bottom-post > www.asciiribbon.org | www.netmeister.org/news/learn2quote.html > GPG KEY: 5EAAEA16 | Arch Linux x86_64, zsh, mutt, vim. > # vim:set textwidth=70: > > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 102, Issue 11 > ************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Aug 5 11:38:19 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 05 Aug 2012 11:38:19 +0200 Subject: [Tutor] filelist References: Message-ID: richard kappler wrote: > Starting to work through "Programming Computer Vision with Python" in my > -summer of learning python- quest. As I read through the intro to the PIL > library, I came across the below code. When I read it, I said to my self > "I don't see how that calls a set of files, there's no specificity. How > does that know where to call from?" Then I convinced myself that, because > I'm a beginner, I must be missing something, and ran it through the > interpreter. Of course, I got an error message telling me filelist isn't > defined. But now I'm kinda lost. > > If the directory holding the pics I want to work on is called > practicephotos, should I declare something along the lines of filelist = > ~/practicephotos/ or what? In the draft of the book the author shows a way to create a list of images in the next example import os def get_imlist(path): """ Returns a list of filenames for all jpg images in a directory. """ return [os.path.join(path,f) for f in os.listdir(path) if f.endswith(".jpg")] I you follow his instructions the snippet you provided could become > [code] with indentation and outfile assignment fixed > from PIL import Image > import os import imtools filelist = imtools.get_imlist(".") > for infile in filelist: > outfile = os.path.splitext(infile)[0] + ".jpg" > if infile != outfile: > try: > Image.open(infile).save(outfile) > except IOError: > print "cannot convert", infile > [/code] However, he was a bit careless and the two examples don't exactly dovetail. get_imlist() only returns files ending with .jpg -- and these are skipped by the code you posted. You can fix that by changing get_imlist() as follows: IMAGE_SUFFIXES = (".jpg", ".png", ".gif") # add more as needed def get_imlist(path): """ Returns a list of filenames for images in a directory. """ return [os.path.join(path,f) for f in os.listdir(path) if f.endswith(IMAGE_SUFFIXES)] This is still not very robust (what if the filename of an image ends with ".JPG" or ".jpeg" or ".something_completely_different"?) but at least you have something that you can invoke with the python interpreter and that will have an effect. After that experience you should have learned to take the proviso in the introduction seriously: """ What you need to know - Basic programming experience. You need to know how to use an editor and run scripts, how to structure code as well as basic data types. Familiarity with Python [...] will help. """ Not only will it help, you won't have much fun with the book without it. I suggest that you work through a general python tutorial before you go back to the book. http://wiki.python.org/moin/BeginnersGuide From bcarpio at thetek.net Mon Aug 6 15:29:35 2012 From: bcarpio at thetek.net (Brian Carpio) Date: Mon, 6 Aug 2012 07:29:35 -0600 Subject: [Tutor] Code Review? Message-ID: Hi, Hopefully I am allowed to ask this here. I am pretty new to python I've only been writing code for about 6 months now strictly for system administration purposes; however I have now decided to write something "real" that others might benefit from but I am looking for someone to take a look at my code and give me an idea on some areas where I need improvement. I certainly don't expect anyone to rewrite my code but if someone is willing to give it a quick look and tell me things like focus more on XX or you really need to learn YY because its obvious you don't have a clue (lol) those types of comments would be more then welcome. I've ran my code through pylint and am getting a score of 7-9 with all of my scripts, but a program can only tell you so much having a real person point out some of my shortcomings would be amazing. Sometimes you don't know you don't know something until someone tells you "Hey you don't know XX or YY go learn it". https://github.com/bcarpio/mongodb-enc Thanks, Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: From maniandram01 at gmail.com Mon Aug 6 15:38:28 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Mon, 6 Aug 2012 19:08:28 +0530 Subject: [Tutor] Code Review? In-Reply-To: References: Message-ID: In scripts/add_node.py GPL Licene should be GPL License On 6 August 2012 18:59, Brian Carpio wrote: > Hi, > > Hopefully I am allowed to ask this here. I am pretty new to python I've > only been writing code for about 6 months now strictly for system > administration purposes; however I have now decided to write something > "real" that others might benefit from but I am looking for someone to take > a look at my code and give me an idea on some areas where I need > improvement. I certainly don't expect anyone to rewrite my code but if > someone is willing to give it a quick look and tell me things like focus > more on XX or you really need to learn YY because its obvious you don't > have a clue (lol) those types of comments would be more then welcome. > > I've ran my code through pylint and am getting a score of 7-9 with all of > my scripts, but a program can only tell you so much having a real person > point out some of my shortcomings would be amazing. Sometimes you don't > know you don't know something until someone tells you "Hey you don't know > XX or YY go learn it". > > https://github.com/bcarpio/mongodb-enc > > Thanks, > Brian > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From maniandram01 at gmail.com Mon Aug 6 15:39:45 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Mon, 6 Aug 2012 19:09:45 +0530 Subject: [Tutor] Code Review? In-Reply-To: References: Message-ID: Another practice is make __license__ = "GPL license..." and __author__ = "Brian Carpio " I do so. On 6 August 2012 19:08, Ramchandra Apte wrote: > In scripts/add_node.py GPL Licene should be GPL License > > On 6 August 2012 18:59, Brian Carpio wrote: > >> Hi, >> >> Hopefully I am allowed to ask this here. I am pretty new to python I've >> only been writing code for about 6 months now strictly for system >> administration purposes; however I have now decided to write something >> "real" that others might benefit from but I am looking for someone to take >> a look at my code and give me an idea on some areas where I need >> improvement. I certainly don't expect anyone to rewrite my code but if >> someone is willing to give it a quick look and tell me things like focus >> more on XX or you really need to learn YY because its obvious you don't >> have a clue (lol) those types of comments would be more then welcome. >> >> I've ran my code through pylint and am getting a score of 7-9 with all of >> my scripts, but a program can only tell you so much having a real person >> point out some of my shortcomings would be amazing. Sometimes you don't >> know you don't know something until someone tells you "Hey you don't know >> XX or YY go learn it". >> >> https://github.com/bcarpio/mongodb-enc >> >> Thanks, >> Brian >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From oberoc at gmail.com Mon Aug 6 15:45:20 2012 From: oberoc at gmail.com (Tino Dai) Date: Mon, 6 Aug 2012 09:45:20 -0400 Subject: [Tutor] Code Review? In-Reply-To: References: Message-ID: On Mon, Aug 6, 2012 at 9:39 AM, Ramchandra Apte wrote: > Another practice is make __license__ = "GPL license..." > and __author__ = "Brian Carpio " > I do so. > > > On 6 August 2012 19:08, Ramchandra Apte wrote: > >> In scripts/add_node.py GPL Licene should be GPL License >> >> >> Brian, I glanced at your code, and I saw a lot of duplication between the scripts. Is there a reason for this? -Tino -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Aug 6 15:53:37 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 Aug 2012 14:53:37 +0100 Subject: [Tutor] Code Review? In-Reply-To: References: Message-ID: On 06/08/2012 14:38, Ramchandra Apte wrote: [top posting fixed] > On 6 August 2012 18:59, Brian Carpio wrote: > >> Hi, >> >> Hopefully I am allowed to ask this here. I am pretty new to python I've >> only been writing code for about 6 months now strictly for system >> administration purposes; however I have now decided to write something >> "real" that others might benefit from but I am looking for someone to take >> a look at my code and give me an idea on some areas where I need >> improvement. I certainly don't expect anyone to rewrite my code but if >> someone is willing to give it a quick look and tell me things like focus >> more on XX or you really need to learn YY because its obvious you don't >> have a clue (lol) those types of comments would be more then welcome. >> >> I've ran my code through pylint and am getting a score of 7-9 with all of >> my scripts, but a program can only tell you so much having a real person >> point out some of my shortcomings would be amazing. Sometimes you don't >> know you don't know something until someone tells you "Hey you don't know >> XX or YY go learn it". >> >> https://github.com/bcarpio/mongodb-enc >> >> Thanks, >> Brian >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > In scripts/add_node.py GPL Licene should be GPL License > Please don't top post, it makes it very difficult to follow threads. Thanks. -- Cheers. Mark Lawrence. From __peter__ at web.de Mon Aug 6 17:21:51 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 06 Aug 2012 17:21:51 +0200 Subject: [Tutor] Code Review? References: Message-ID: Brian Carpio wrote: > Hi, > > Hopefully I am allowed to ask this here. I am pretty new to python I've > only been writing code for about 6 months now strictly for system > administration purposes; however I have now decided to write something > "real" that others might benefit from but I am looking for someone to take > a look at my code and give me an idea on some areas where I need > improvement. I certainly don't expect anyone to rewrite my code but if > someone is willing to give it a quick look and tell me things like focus > more on XX or you really need to learn YY because its obvious you don't > have a clue (lol) those types of comments would be more then welcome. > > I've ran my code through pylint and am getting a score of 7-9 with all of > my scripts, but a program can only tell you so much having a real person > point out some of my shortcomings would be amazing. Sometimes you don't > know you don't know something until someone tells you "Hey you don't know > XX or YY go learn it". > > https://github.com/bcarpio/mongodb-enc [I took a look at mongodb_node_classifier.py] Random remarks: - pylint probably "told" you: use self-explanatory names rather than i, d, or n. - Split your code in smaller dedicated functions. This simplifies writing unittests, allows reuse and keeps control flow manageable as your script grows. - Add a comment that explains what the script does; repeat for each function. - Use main() to read the configuration/commandline and put the real meat into a dedicated function. - Prefer raising exceptions over sys.exit() in the code that does the real work (main() may convert these into sys.exit). Here's a sketch for a revised main(): def main(): node_name = sys.argv[1] filename = ... conn_spec = read_config(filename) connection = open_connection(con_spec) try: process(connection, node_name) except InheritanceError as err: sys.exit(str(err)) (You should choose a name that is more descriptive than process()) - Use 'expr', not 'expr == True' in boolean contexts - Show that you know None is a singleton and prefer 'expr is None' over 'expr == None' - I found the while loop hard to understand. I have a hunch that it can be simplified. From bcarpio at thetek.net Tue Aug 7 00:27:24 2012 From: bcarpio at thetek.net (Brian Carpio) Date: Mon, 6 Aug 2012 16:27:24 -0600 Subject: [Tutor] Code Review? In-Reply-To: References: Message-ID: Thanks to everyone who has replied! This is some good information for me to go learn with!. I greatly appreciate it. Brian On Mon, Aug 6, 2012 at 9:21 AM, Peter Otten <__peter__ at web.de> wrote: > Brian Carpio wrote: > > > Hi, > > > > Hopefully I am allowed to ask this here. I am pretty new to python I've > > only been writing code for about 6 months now strictly for system > > administration purposes; however I have now decided to write something > > "real" that others might benefit from but I am looking for someone to > take > > a look at my code and give me an idea on some areas where I need > > improvement. I certainly don't expect anyone to rewrite my code but if > > someone is willing to give it a quick look and tell me things like focus > > more on XX or you really need to learn YY because its obvious you don't > > have a clue (lol) those types of comments would be more then welcome. > > > > I've ran my code through pylint and am getting a score of 7-9 with all of > > my scripts, but a program can only tell you so much having a real person > > point out some of my shortcomings would be amazing. Sometimes you don't > > know you don't know something until someone tells you "Hey you don't know > > XX or YY go learn it". > > > > https://github.com/bcarpio/mongodb-enc > > [I took a look at mongodb_node_classifier.py] > > Random remarks: > > - pylint probably "told" you: use self-explanatory names rather than i, d, > or n. > - Split your code in smaller dedicated functions. This simplifies writing > unittests, allows reuse and keeps control flow manageable as your script > grows. > - Add a comment that explains what the script does; repeat for each > function. > - Use main() to read the configuration/commandline and put the real meat > into a dedicated function. > - Prefer raising exceptions over sys.exit() in the code that does the real > work (main() may convert these into sys.exit). > > Here's a sketch for a revised main(): > > def main(): > node_name = sys.argv[1] > filename = ... > conn_spec = read_config(filename) > connection = open_connection(con_spec) > try: > process(connection, node_name) > except InheritanceError as err: > sys.exit(str(err)) > > (You should choose a name that is more descriptive than process()) > > - Use 'expr', not 'expr == True' in boolean contexts > - Show that you know None is a singleton and prefer 'expr is None' over > 'expr == None' > - I found the while loop hard to understand. I have a hunch that it can be > simplified. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From abhishek.vit at gmail.com Tue Aug 7 01:12:52 2012 From: abhishek.vit at gmail.com (Abhishek Pratap) Date: Mon, 6 Aug 2012 16:12:52 -0700 Subject: [Tutor] *large* python dictionary with persistence storage for quick look-ups Message-ID: Hey Guys I have asked a question on stackoverflow and thought I would post it here as it has some learning flavor attached to it...atleast I feel that. http://stackoverflow.com/questions/11837229/large-python-dictionary-with-persistence-storage-for-quick-look-ups -Abhi From sntshkmr60 at gmail.com Tue Aug 7 05:39:02 2012 From: sntshkmr60 at gmail.com (Santosh Kumar) Date: Tue, 7 Aug 2012 09:09:02 +0530 Subject: [Tutor] How to import a few modules when I enter python? Message-ID: Hello there, I have a few scripts that I made to experiment with, I have to import them everytime I enter the Python shell. The scripts are in `/home/username/workshop/` (this directory has also some non .py files) directory. Is there a way I can import them as soon as I enter Python? Also I am exploring the OS module, can I import them as well? Thanks From __peter__ at web.de Tue Aug 7 09:05:45 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Aug 2012 09:05:45 +0200 Subject: [Tutor] How to import a few modules when I enter python? References: Message-ID: Santosh Kumar wrote: > Hello there, > > I have a few scripts that I made to experiment with, I have to import > them everytime I enter the Python shell. The scripts are in > `/home/username/workshop/` (this directory has also some non .py > files) directory. Is there a way I can import them as soon as I enter > Python? > > Also I am exploring the OS module, can I import them as well? You can specify a python module that will be executed at startup by setting the PYTHONSTARTUP environment variable: $ echo 'print "Preparing..." import os import sys foo = 42 > print "...done"' > startup.py $ PYTHONSTARTUP=startup.py python Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. Preparing... ...done >>> foo 42 >>> os From steve at pearwood.info Tue Aug 7 09:07:32 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 7 Aug 2012 17:07:32 +1000 Subject: [Tutor] How to import a few modules when I enter python? In-Reply-To: References: Message-ID: <20120807070732.GA1981@ando> On Tue, Aug 07, 2012 at 09:09:02AM +0530, Santosh Kumar wrote: > Hello there, > > I have a few scripts that I made to experiment with, I have to import > them everytime I enter the Python shell. The scripts are in > `/home/username/workshop/` (this directory has also some non .py > files) directory. Is there a way I can import them as soon as I enter > Python? > > Also I am exploring the OS module, can I import them as well? The way to do this is using Python's optional "startup file". Using your shell, define an environment variable PYTHONSTARTUP set to the path to a Python script. The way you do this depends on your operating system, and shell. I use Linux with the bash shell, and I have this in my .bashrc file: export PYTHONSTARTUP=/home/steve/python/startup.py Then, in your startup.py file, you can add any Python code you like, and it will be automatically run when you use the Python interactive interpreter. In your case, you can keep a list of your modules, and just import them: import module1 import module2 # etc. but that assumes that your /home/username/workshop/ directory is in the PYTHONPATH. How do you do that? Here are two ways: 1) In your startup file, start with these lines: import sys sys.path.append('/home/username/workshop/') 2) Add this line to your .bashrc: export PYTHONPATH=/home/username/workshop/ Some further thoughts: The startup file will NOT run when you run a script directly, or if you pass the -E command line switch: python myscript.py python -E and other Python implementations like IronPython and Jython may not honour the PYTHONSTARTUP variable. Attached is my current startup file, to give you some ideas of what you can do. -- Steven -------------- next part -------------- ############################################################### # P Y T H O N S T A R T U P S C R I P T # ############################################################### # Keep this module compatible with Python 2.4 and better. from __future__ import division # === Basic functionality === # Pre-import useful modules. import math, os, sys # Change the main prompt. sys.ps1 = 'py> ' # Include special values. Prior to Python 2.6, this was messy, platform- # dependent, and not guaranteed to work. try: INF = float('inf') NAN = float('nan') except ValueError: # Possibly Windows prior to Python 2.6. try: INF = float('1.#INF') NAN = float('1.#IND') # Indeterminate. except ValueError: # Desperate times require desperate measures... try: INF = 1e3000 # Hopefully, this should overflow to INF. NAN = INF-INF # And this hopefully will give a NaN. except (ValueError, OverflowError): pass # Just give up. # Bring back reload in Python 3. try: reload except NameError: from imp import reload # Monkey-patch the math module *wicked grin* try: math.tau except AttributeError: math.tau = 2*math.pi # === Add globbing to dir() === try: sys._getframe() except (AttributeError, NotImplementedError): # Not all implementations support _getframe; in particular, # IronPython does not support it by default. print('*** warning: no frame support; globbing dir not available ***') else: try: from enhanced_dir import globbing_dir as dir except ImportError: print('*** warning: globbing dir not available ***') # === Simple benchmarking === try: from timer import Timer as timer except ImportError: print('*** warning: timer not available ***') # === Command line completion and history === try: from completer import completer except ImportError: print('*** warning: command line completion not available ***') print("=== startup script executed ===") From brad.dutton7 at gmail.com Sat Aug 4 23:51:19 2012 From: brad.dutton7 at gmail.com (Brad Dutton) Date: Sat, 4 Aug 2012 17:51:19 -0400 Subject: [Tutor] Question about reading from a file. Message-ID: I recently discovered how to read from a file but I've having some trouble with it. I made a short example program that goes like this: 1. fob = open('c:/python27/a.txt', 'r') 2. 3. print fob.read() 4. print fob.read() When it runs it returns this: 1. Hey now brown cow 2. It's like it forgets how to read after the first time. This has left me very confused. Could you offer any reason why this might be happening? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dane at saltzman.net Sun Aug 5 00:41:24 2012 From: dane at saltzman.net (Dane Saltzman) Date: Sat, 4 Aug 2012 15:41:24 -0700 (PDT) Subject: [Tutor] Built in functions Message-ID: <1344120084.39745.YahooMailNeo@web140401.mail.bf1.yahoo.com> I'm new to python and I was wondering if you could tell me how I would: first, define a function,distance_from_zero, with one parameter (choose any parameter name you like). Second, have that function do the following: 1. Check the?type?of the input it receives. 2. If the?type?is?int?or?float, the function should?return?the?absolute value of the function input. 3. If the?type?is any other?type, the function should?return?"This isn't an integer or a float!" ---DJ -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Tue Aug 7 10:22:13 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 7 Aug 2012 01:22:13 -0700 (PDT) Subject: [Tutor] *large* python dictionary with persistence storage for quick look-ups In-Reply-To: References: Message-ID: <1344327733.49552.YahooMailNeo@web110709.mail.gq1.yahoo.com> ________________________________ From: Abhishek Pratap To: tutor at python.org Sent: Tuesday, August 7, 2012 1:12 AM Subject: [Tutor] *large* python dictionary with persistence storage for quick look-ups Hey Guys > >I have asked a question on stackoverflow and thought I would post it >here as it has some learning flavor attached to it...atleast I feel >that. > >http://stackoverflow.com/questions/11837229/large-python-dictionary-with-persistence-storage-for-quick-look-ups > >-Abhi > >===>> Interesting. The first thing that came to my mind was the shelve module, but this not?the most highly rated solution. Another thing I used before is the marshall module, which is disgustingly fast, but the persistent file is not portable to other Python (sub)versions. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Aug 7 10:37:25 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Aug 2012 10:37:25 +0200 Subject: [Tutor] Built in functions References: <1344120084.39745.YahooMailNeo@web140401.mail.bf1.yahoo.com> Message-ID: Dane Saltzman wrote: > I'm new to python and I was wondering if you could tell me how I would: > > first, define a function,distance_from_zero, with one parameter (choose > any parameter name you like). Second, have that function do the following: > 1. Check the type of the input it receives. > > 2. If the type is int or float, the function should return the absolute > value of the function input. > > 3. If the type is any other type, the function should return "This isn't > an integer or a float!" I'm assuming that the subject line is a hint from your professor where you should look for functions that (a) check if a value is of a particular type (b) calculate an absolute value I suggest that you scan the table at http://docs.python.org/library/functions.html for candidates and try to come up with a suggestion for the implementation of distance_from_zero() yourself. Come back with the code you have if you run into problems. We will be glad to help you fix them. From __peter__ at web.de Tue Aug 7 10:45:46 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Aug 2012 10:45:46 +0200 Subject: [Tutor] Question about reading from a file. References: Message-ID: Brad Dutton wrote: > I recently discovered how to read from a file but I've having some trouble > with it. I made a short example program that goes like this: > > > 1. fob = open('c:/python27/a.txt', 'r') > 2. > 3. print fob.read() > 4. print fob.read() > > When it runs it returns this: > > > 1. Hey now brown cow > 2. > > It's like it forgets how to read after the first time. This has left me > very confused. Could you offer any reason why this might be happening? The open file, 'fob' maintains a pointer into the file that is moved by read operations: >>> fob = open("a.txt") >>> fob.read(3) 'Hey' >>> fob.read(3) ' no' >>> fob.read(3) 'w b' If you call the read method without argument the complete (remaining) file is read and following read calls will not give any data to read: >>> fob.read() 'rown cow\n' >>> fob.read() '' Most of the time the best approach is to open a new file >>> for i in range(3): ... with open("a.txt") as f: print f.read().strip() ... Hey now brown cow Hey now brown cow Hey now brown cow but it is also possible to move the pointer back to the beginning to the file (or elsewhere): >>> fob.seek(4) >>> fob.read() 'now brown cow\n' >>> fob.seek(0) >>> fob.read() 'Hey now brown cow\n' From breamoreboy at yahoo.co.uk Tue Aug 7 11:12:36 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 07 Aug 2012 10:12:36 +0100 Subject: [Tutor] Question about reading from a file. In-Reply-To: References: Message-ID: On 04/08/2012 22:51, Brad Dutton wrote: > I recently discovered how to read from a file but I've having some trouble > with it. I made a short example program that goes like this: > > > 1. fob = open('c:/python27/a.txt', 'r') > 2. > 3. print fob.read() > 4. print fob.read() > > When it runs it returns this: > > > 1. Hey now brown cow > 2. > > It's like it forgets how to read after the first time. This has left me > very confused. Could you offer any reason why this might be happening? > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Further to Peter Otten's answer you make want to take a look at readline or readlines, or check into file iteration. Depends on what you're trying to achieve :) -- Cheers. Mark Lawrence. From joel.goldstick at gmail.com Tue Aug 7 11:33:02 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 7 Aug 2012 05:33:02 -0400 Subject: [Tutor] Question about reading from a file. In-Reply-To: References: Message-ID: On Tue, Aug 7, 2012 at 5:12 AM, Mark Lawrence wrote: > On 04/08/2012 22:51, Brad Dutton wrote: >> >> I recently discovered how to read from a file but I've having some trouble >> with it. I made a short example program that goes like this: >> >> >> 1. fob = open('c:/python27/a.txt', 'r') >> 2. >> 3. print fob.read() >> 4. print fob.read() >> >> >> When it runs it returns this: >> >> >> 1. Hey now brown cow >> 2. >> >> It's like it forgets how to read after the first time. This has left me >> very confused. Could you offer any reason why this might be happening? You need to understand some basic concepts about what a file is in computer programming. The other posters have helped you out. You can find out more here: http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects or spend an hour or so (at least) with the results from googling 'python file operation example' good luck >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > Further to Peter Otten's answer you make want to take a look at readline or > readlines, or check into file iteration. Depends on what you're trying to > achieve :) > > -- > Cheers. > > Mark Lawrence. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick From joel.goldstick at gmail.com Tue Aug 7 11:48:30 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 7 Aug 2012 05:48:30 -0400 Subject: [Tutor] Built in functions In-Reply-To: References: <1344120084.39745.YahooMailNeo@web140401.mail.bf1.yahoo.com> Message-ID: On Tue, Aug 7, 2012 at 4:37 AM, Peter Otten <__peter__ at web.de> wrote: > Dane Saltzman wrote: > >> I'm new to python and I was wondering if you could tell me how I would: >> >> first, define a function,distance_from_zero, with one parameter (choose >> any parameter name you like). Second, have that function do the following: >> 1. Check the type of the input it receives. >> >> 2. If the type is int or float, the function should return the absolute >> value of the function input. >> >> 3. If the type is any other type, the function should return "This isn't >> an integer or a float!" > > I'm assuming that the subject line is a hint from your professor where you > should look for functions that > > (a) check if a value is of a particular type > (b) calculate an absolute value > > I suggest that you scan the table at > > http://docs.python.org/library/functions.html > > for candidates and try to come up with a suggestion for the implementation > of distance_from_zero() yourself. Come back with the code you have if you > run into problems. We will be glad to help you fix them. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor This looks like homework. So, I'll let Peter's response stand as what looks like good advice to answer your question. While you can do it that way, and that way seems to be what your instructor wants you to learn, among python programmers there is a saying that its better to ask forgiveness than permission. Try to do something, and if it raises an error, take some other action. If you open up a python shell you can experiment: >>> abs(-3) 3 4.5 >>> abs(-5) 5 >>> abs (1, 3) Traceback (most recent call last): File "", line 1, in TypeError: abs() takes exactly one argument (2 given) >>> abs('b0b') Traceback (most recent call last): File "", line 1, in TypeError: bad operand type for abs(): 'str' >>> With this knowledge you can do something like this: def distance_from_zero(value): try: return abs(value) except TypeError: return "Sorry, can't find an abs of a type like that" Rather than testing if you can find an absolute value of the type you pass (there maybe other types that you want to use later) for instance complex numbers also have distance values: >>> abs(complex(3,4)) 5.0 -- Joel Goldstick From maniandram01 at gmail.com Tue Aug 7 14:37:37 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Tue, 7 Aug 2012 18:07:37 +0530 Subject: [Tutor] Question about reading from a file. In-Reply-To: References: Message-ID: > > Imagine a book. > .read(num) reads a page of the book Now if you call again it goes to the next page. > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From oberoc at gmail.com Tue Aug 7 15:56:34 2012 From: oberoc at gmail.com (Tino Dai) Date: Tue, 7 Aug 2012 09:56:34 -0400 Subject: [Tutor] Code Review? In-Reply-To: References: Message-ID: On Mon, Aug 6, 2012 at 6:27 PM, Brian Carpio wrote: > Thanks to everyone who has replied! This is some good information for me > to go learn with!. > > I greatly appreciate it. > > When you refactor your code, let us know. I, for one, would like to see it. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue Aug 7 16:12:48 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 07 Aug 2012 15:12:48 +0100 Subject: [Tutor] Question about reading from a file. In-Reply-To: References: Message-ID: On 07/08/2012 13:37, Ramchandra Apte wrote: >> >> Imagine a book. >> > .read(num) reads a page of the book > Now if you call again it goes to the next page. > Please don't top post. This is now the fourth time you've been asked. -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Tue Aug 7 16:16:08 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 07 Aug 2012 15:16:08 +0100 Subject: [Tutor] Built in functions In-Reply-To: References: <1344120084.39745.YahooMailNeo@web140401.mail.bf1.yahoo.com> Message-ID: On 07/08/2012 10:48, Joel Goldstick wrote: > On Tue, Aug 7, 2012 at 4:37 AM, Peter Otten <__peter__ at web.de> wrote: >> Dane Saltzman wrote: >> >>> I'm new to python and I was wondering if you could tell me how I would: >>> >>> first, define a function,distance_from_zero, with one parameter (choose >>> any parameter name you like). Second, have that function do the following: >>> 1. Check the type of the input it receives. >>> >>> 2. If the type is int or float, the function should return the absolute >>> value of the function input. >>> >>> 3. If the type is any other type, the function should return "This isn't >>> an integer or a float!" >> >> I'm assuming that the subject line is a hint from your professor where you >> should look for functions that >> >> (a) check if a value is of a particular type >> (b) calculate an absolute value >> >> I suggest that you scan the table at >> >> http://docs.python.org/library/functions.html >> >> for candidates and try to come up with a suggestion for the implementation >> of distance_from_zero() yourself. Come back with the code you have if you >> run into problems. We will be glad to help you fix them. >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > This looks like homework. So, I'll let Peter's response stand as what > looks like good advice to answer your question. > > While you can do it that way, and that way seems to be what your > instructor wants you to learn, among python programmers there is a > saying that its better to ask forgiveness than permission. Try to do > something, and if it raises an error, take some other action. > > If you open up a python shell you can experiment: > >>>> abs(-3) > 3 > 4.5 >>>> abs(-5) > 5 >>>> abs (1, 3) > Traceback (most recent call last): > File "", line 1, in > TypeError: abs() takes exactly one argument (2 given) >>>> abs('b0b') > Traceback (most recent call last): > File "", line 1, in > TypeError: bad operand type for abs(): 'str' >>>> > > With this knowledge you can do something like this: > > def distance_from_zero(value): > try: > return abs(value) > except TypeError: > return "Sorry, can't find an abs of a type like that" > > Rather than testing if you can find an absolute value of the type you > pass (there maybe other types that you want to use later) > > for instance complex numbers also have distance values: > >>>> abs(complex(3,4)) > 5.0 > > > For the benefit of the OP and possibly others research the contrast in approach between LBYL and EAFP. Should keep you off of the streets for a few minutes :) -- Cheers. Mark Lawrence. From gnj091405 at gmail.com Tue Aug 7 19:10:54 2012 From: gnj091405 at gmail.com (Gregory Lund) Date: Tue, 7 Aug 2012 10:10:54 -0700 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip Message-ID: # neophyte .py/pyTutor user. # I am also a university GIS lecturer. My students submit their lab assignments (zipped up into one zipfile) to a depository, which I download as a single zip containing all 40 students' zipfiles. # Goal: unzip a zipfile of student zipfiles and then unzip each students' zipfile ( to prevent me from having to unzip each students' zipfile by hand. ) # eventually I'll turn this into a tool in ArcGIS, that will grade some of their assignment, but for now, just want to get these extract processes running in python. # using (and want to use) 2.6 because I'm working with ArcGIS # this is what I've got thus far: #1 Unzip a single starting zipfile "initial_Zipfile" (that contains a series of 40 individual zipfiles) into a single folder named whatever it was originally named. #2 Unzip all of the zipfiles within the 'initial_Zipfile' into their own folder. ## Actual Code: import zipfile, arcpy, os.path, os #1 Defining a function to Unzip the initial (.zip) folder. (this worked fine) def unzip_Orig(oZ): z = zipfile.ZipFile(oZ) z.extractall() return q = "unzip_Unzip_Data_Test.zip" unzip_Orig(q) #2 my attempts to unzip each zipfile within the new folder below (failed thus far) mywd = os.getcwd() zipList = os.listdir(mywd) print zipList #this didn't work, it printed the directory of where the .py was stored. #Zipfile.printdir() # returned "'Zipfile' is not defined. #thoughts? # Thanks # Greg From __peter__ at web.de Tue Aug 7 19:55:35 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Aug 2012 19:55:35 +0200 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip References: Message-ID: Gregory Lund wrote: [For improved readability please avoid # prefixes for the parts of your post that are not comments in snippets of python code] > neophyte .py/pyTutor user. Welcome. > I am also a university GIS lecturer. My students submit their lab > assignments (zipped up into one zipfile) to a depository, which I > download as a single zip containing all 40 students' zipfiles. > > Goal: unzip a zipfile of student zipfiles and then unzip each > students' zipfile ( to prevent me from having to unzip each students' > zipfile by hand. ) > > eventually I'll turn this into a tool in ArcGIS, that will grade > some of their assignment, but for now, just want to get these extract > processes running in python. > > using (and want to use) 2.6 because I'm working with ArcGIS > this is what I've got thus far: > > 1 Unzip a single starting zipfile "initial_Zipfile" (that contains a > series of 40 individual zipfiles) into a single folder named whatever > it was originally named. > > 2 Unzip all of the zipfiles within the 'initial_Zipfile' into their own folder. > > ## Actual Code: > > import zipfile, arcpy, os.path, os > > #1 Defining a function to Unzip the initial (.zip) folder. (this worked fine) > > def unzip_Orig(oZ): > z = zipfile.ZipFile(oZ) > z.extractall() > return > > q = "unzip_Unzip_Data_Test.zip" > unzip_Orig(q) > > #2 my attempts to unzip each zipfile within the new folder below > (failed thus far) > > mywd = os.getcwd() > zipList = os.listdir(mywd) > print zipList #this didn't work, it printed the directory of where > the .py was stored. > #Zipfile.printdir() # returned "'Zipfile' is not defined. > > thoughts? Forget about messing with the current working directory. Instead specify the location of the outer zipfile and the destination directory explicitly, e. g.: $ cat unzip_twice.py import glob import os import sys import zipfile source_file = sys.argv[1] dest_folder = sys.argv[2] zipfile.ZipFile(source_file).extractall(dest_folder) inner_zips_pattern = os.path.join(dest_folder, "*.zip") for filename in glob.glob(inner_zips_pattern): inner_folder = filename[:-4] zipfile.ZipFile(filename).extractall(inner_folder) $ tree . ??? all.zip ??? unzip_twice.py 0 directories, 2 files $ python unzip_twice.py all.zip outer $ tree . ??? all.zip ??? outer ? ??? 123 ? ? ??? 1.txt ? ? ??? 2.txt ? ? ??? 3.txt ? ??? 123.zip ? ??? 456 ? ? ??? 4.txt ? ? ??? 5.txt ? ? ??? 6.txt ? ??? 456.zip ? ??? 789 ? ? ??? 7.txt ? ? ??? 8.txt ? ? ??? 9.txt ? ??? 789.zip ??? unzip_twice.py 4 directories, 14 files From breamoreboy at yahoo.co.uk Tue Aug 7 20:10:38 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 07 Aug 2012 19:10:38 +0100 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: On 07/08/2012 18:10, Gregory Lund wrote: > # neophyte .py/pyTutor user. > # I am also a university GIS lecturer. My students submit their lab > assignments (zipped up into one zipfile) to a depository, which I > download as a single zip containing all 40 students' zipfiles. > # Goal: unzip a zipfile of student zipfiles and then unzip each > students' zipfile ( to prevent me from having to unzip each students' > zipfile by hand. ) > # eventually I'll turn this into a tool in ArcGIS, that will grade > some of their assignment, but for now, just want to get these extract > processes running in python. > > # using (and want to use) 2.6 because I'm working with ArcGIS > # this is what I've got thus far: > > #1 Unzip a single starting zipfile "initial_Zipfile" (that contains a > series of 40 individual zipfiles) into a single folder named whatever > it was originally named. > > #2 Unzip all of the zipfiles within the 'initial_Zipfile' into their own folder. > > ## Actual Code: > > import zipfile, arcpy, os.path, os > > #1 Defining a function to Unzip the initial (.zip) folder. (this worked fine) > > def unzip_Orig(oZ): > z = zipfile.ZipFile(oZ) > z.extractall() > return > > q = "unzip_Unzip_Data_Test.zip" > unzip_Orig(q) > > #2 my attempts to unzip each zipfile within the new folder below > (failed thus far) > > mywd = os.getcwd() > zipList = os.listdir(mywd) > print zipList #this didn't work, it printed the directory of where > the .py was stored. Given the help below for the two os functions you've called, what did you expect it to do? >>> help(os.getcwd) Help on built-in function getcwd in module nt: getcwd(...) getcwd() -> path Return a string representing the current working directory. >>> help(os.listdir) Help on built-in function listdir in module nt: listdir(...) listdir(path) -> list_of_strings Return a list containing the names of the entries in the directory. path: path of directory to list The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory. > #Zipfile.printdir() # returned "'Zipfile' is not defined. True indeed, did you mean this to be zipList or what? > > #thoughts? Your assignment for tonight is to go back to the drawing board, let's face it we wouldn't write code for your students :) > > # Thanks > # Greg > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. From maniandram01 at gmail.com Wed Aug 8 05:15:13 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Wed, 8 Aug 2012 08:45:13 +0530 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: I think a shell command (but I think you are using Windows) would be best for this task. You can unzip the file in memory and then unzip the smaller ones. There is no need to create any files. > [snip] > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Wed Aug 8 09:30:47 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 8 Aug 2012 00:30:47 -0700 (PDT) Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: <1344411047.44392.YahooMailNeo@web110707.mail.gq1.yahoo.com> > >I think a shell command (but I think you are using Windows) would be best for this task. >You can unzip the file in memory and then unzip the smaller ones. >There is no need to create any files.? >[snip] >> >>===> This seems to do the trick: http://code.activestate.com/recipes/465649-file-unzip-lite/ >>But how scalable is the zipfile module? I've just wzunzip.exe before because I encountered problems unzipping large zip files using the zipfile module. ________________________________ From: Ramchandra Apte To: Mark Lawrence Cc: tutor at python.org Sent: Wednesday, August 8, 2012 5:15 AM Subject: Re: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Aug 8 10:15:27 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Aug 2012 10:15:27 +0200 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip References: Message-ID: Gregory Lund wrote: > Not too sure on protocol either, sorry if this email is out of line. You have to hit [Reply all] in your email client for your answer to go to the list instead of just me. When you answer a post put your reply after the relevant part of the quoted post (don't "top-post"). Remove the parts of the original message that are not relevant to your follow-up. > Thanks for your assistance. > unfortunately I can't make what you sent, work. > i am not familiar enough to know which are variables and which I must not > touch. > I kept getting list index out of range. That is because you didn't invoke the script from the commandline (DOS box), with two arguments. sys.argv is a list containing the script name and the arguments provided on the commandline. If you invoke the script from Idle or by double-clicking no arguments are passed. > > This is what I attempted: > source_file = sys.argv[1] The error occurs in the line above (the traceback should tell you that), so the following lines including your modification are newer executed. > dest_folder = sys.argv[2] > > zipfile.ZipFile("unzip_Unzip_Data_Test.zip").extractall(r"D: \D_Drive_Documents\test_folder") > > inner_zips_pattern = os.path.join(dest_folder, "*.zip") > for filename in glob.glob(inner_zips_pattern): > inner_folder = filename[:-4] > zipfile.ZipFile(filename).extractall(inner_folder) Let's go back to to my original script and for the moment hard-code the filename and folder. It becomes import glob import os import sys import zipfile source_file = "unzip_Unzip_Data_Test.zip" dest_folder = r"D:\D_Drive_Documents\test_folder" zipfile.ZipFile(source_file).extractall(dest_folder) inner_zips_pattern = os.path.join(dest_folder, "*.zip") for filename in glob.glob(inner_zips_pattern): inner_folder = filename[:-4] zipfile.ZipFile(filename).extractall(inner_folder) That should work provided the source file unzip_Unzip_Data_Test.zip is in the current working directory -- otherwise you must specify the complete path like you did for the destination folder. > I was also thinking I could list the files in the new folder (from the > outer) and then if (:-4) = ".zip" then extract them, but can't figure > that out either. Read up what glob.glob() does in the documentation. If you were to hand code what it does the loop could become for filename in os.listdir(dest_folder): if filename.lower().endswith(".zip"): filename = os.path.join(dest_folder, filename) inner_folder = filename[:-4] zipfile.ZipFile(filename).extractall(inner_folder) From breamoreboy at yahoo.co.uk Wed Aug 8 14:03:06 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 08 Aug 2012 13:03:06 +0100 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: On 08/08/2012 04:15, Ramchandra Apte wrote: > I think a shell command (but I think you are using Windows) would be best > for this task. > You can unzip the file in memory and then unzip the smaller ones. > There is no need to create any files. > >> [snip] >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/**mailman/listinfo/tutor >> > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Why have you sent this to me, copied to the list? Why are you still top posting despite being asked repeatedly not to do so? -- Cheers. Mark Lawrence. From maniandram01 at gmail.com Wed Aug 8 17:34:44 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Wed, 8 Aug 2012 21:04:44 +0530 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: Sorry. This is my first time using a mailing list! Any idea how to send a message without replying? -------------- next part -------------- An HTML attachment was scrubbed... URL: From maniandram01 at gmail.com Wed Aug 8 17:36:12 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Wed, 8 Aug 2012 21:06:12 +0530 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: Sorry for sending the email twice :-( . I just ued 'Reply All' in Gmail. But now I know you shouldn't use it. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Aug 8 17:56:30 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 08 Aug 2012 16:56:30 +0100 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: On 08/08/2012 16:34, Ramchandra Apte wrote: > Sorry. This is my first time using a mailing list! Any idea how to send a > message without replying? > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Thanks for asking :) Sorry I can't comment on gmail as I always read Python mailing lists through Thunderbird on Windows. I just hit reply and it automatically goes to the list. I'm sure others here will provide an answer for you. As for the top posting issue there should be a setting in gmail that allows you to answer at the top or the bottom. If there isn't my advice is to get a decent email reader like, er, Thunderbird :) -- Cheers. Mark Lawrence. From emile at fenx.com Wed Aug 8 17:54:43 2012 From: emile at fenx.com (Emile van Sebille) Date: Wed, 08 Aug 2012 08:54:43 -0700 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: On 8/8/2012 8:34 AM Ramchandra Apte said... > Sorry. This is my first time using a mailing list! Any idea how to send > a message without replying? > > > _______________________________________________ Address an email to Tutor at python.org > Tutor maillist - Tutor at python.org Emile > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Wed Aug 8 17:56:20 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 09 Aug 2012 01:56:20 +1000 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: <50228C24.4030800@pearwood.info> On 09/08/12 01:34, Ramchandra Apte wrote: > Sorry. This is my first time using a mailing list! Any idea how to send a > message without replying? The same way you would send a message to anyone else without replying. In your mail program, click "New Message" or "Write Message" or whatever the command is called. Type your subject line and message. Set the To address to tutor at python.org, and then hit Send. -- Steven From d at davea.name Wed Aug 8 19:45:30 2012 From: d at davea.name (Dave Angel) Date: Wed, 08 Aug 2012 13:45:30 -0400 Subject: [Tutor] Unzip a zipfile, then unzip the zipfiles within the original zip In-Reply-To: References: Message-ID: <5022A5BA.60000@davea.name> On 08/08/2012 11:34 AM, Ramchandra Apte wrote: > Sorry. This is my first time using a mailing list! Any idea how to send a > message without replying? > > There are several ways to send a message to this list by email, depending on how the message relates to others. If you're starting a new topic, you should address a new email ("Compose", or "Write" or "New Email"), give it a meaningful subject line, and send it tutor at python.org If you're replying to a message, you should do one of three things: Reply All - your message is of interest to the person you're replying to, and to the group. i do this most often. Reply - your message is only of private interest to the particular person. Examples might be a special thanks, or some details like a local contact because he happens to be local to you. Reply List - your message is of interest to the list, but only incidentally to the person who wrote the existing message, and you assume he'll get it when he reads the list. Some email programs don't have all 3 choices, so you might end up doing a Reply-All, and deleting one or more names from the To: and/or cc Note that the list works best when everyone follows the same conventions, even though they're using widely different email programs or news readers. One of those conventions is to trim the existing message where possible, and put your own remarks AFTER the parts they're aimed at. Those vertical bars (which are actually "<" characters, but most email programs render them as vertical bars) help to identify who wrote which part. So make sure the parts you write are on lines by themselves, generally by hitting enter before and after the part you add. Note that some people use bogus email addresses, as they don't want to get any private email. There is an RFC spec for what to use as such a bogus address, and I think it involves the 'unknown' domain. Note that most email programs and email readers "thread" the responses, so that they're ordered and indented according to who is replying to whom. Even if you can't see that in your own reader, have consideration for those who do. So, for example, don't address a new email when it really belongs in the thread, as it totally splits the thread up. Hope this was helpful. -- DaveA From lilytran at adobe.com Thu Aug 9 05:26:35 2012 From: lilytran at adobe.com (Lily Tran) Date: Wed, 8 Aug 2012 20:26:35 -0700 Subject: [Tutor] help - SyntaxError: Non-UTF-8 code using python 3 Message-ID: Hello; I am getting the following error when I try to run this python program in eclipse. I am running python 3: File "/Users/lilytran/Desktop/python/Ex_Files_Python_3_EssT/Exercise Files/class_beginner_python/hw3_2_lab6.py", line 30 SyntaxError: Non-UTF-8 code starting with '\xd0' in file on line 30, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details ============================================================ Can you please tell me what I am doing wrong and how do I fix this error? Below is my program. Thanks ?Lily ==================================================== import random def MagicEightBallEmulator(): answers = ["As I see it, yes", "It is certain", "It is decidedly so", "Most likely", "Outlook good", "Signs point to yes", "Without a doubt", "Yes", "Yes ? definitely", "You may rely on it", "Reply hazy, try again", "Ask again later", "Better not tell you now", "Do not count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"] while True: what = random.choice(answers) return print(what) def RunEmulator(): while True: print ('Welcome to Magic 8 Ball!') print ('Please ask your question!') question = input() if question == 'quit': break MagicEightBallEmulator() RunEmulator() -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Aug 9 07:10:00 2012 From: d at davea.name (Dave Angel) Date: Thu, 09 Aug 2012 01:10:00 -0400 Subject: [Tutor] help - SyntaxError: Non-UTF-8 code using python 3 In-Reply-To: References: Message-ID: <50234628.6030201@davea.name> On 08/08/2012 11:26 PM, Lily Tran wrote: > Hello; > > > I am getting the following error when I try to run this python program in eclipse. I am running python 3: > > > File "/Users/lilytran/Desktop/python/Ex_Files_Python_3_EssT/Exercise Files/class_beginner_python/hw3_2_lab6.py", line 30 > > SyntaxError: Non-UTF-8 code starting with '\xd0' in file on line 30, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details > > ============================================================ > > > Can you please tell me what I am doing wrong and how do I fix this error? Below is my program. Thanks ?Lily > > ==================================================== > > > import random > > > > def MagicEightBallEmulator(): > > > what = random.choice(answers) > > return print(what) > > > def RunEmulator(): > > while True: > > print ('Welcome to Magic 8 Ball!') > > print ('Please ask your question!') > > question = input() > > if question == 'quit': > > break > > MagicEightBallEmulator() > > > > RunEmulator() > > Sending us the source without adding a comment to line #30 seems a bit presumptuous. Double spacing by sending it as a non-text message makes it worse. The error message itself seems pretty clear. You have a non-UTF8 sequence in a source file implicitly declared as being UTF8. Simplest solution? Don't use non-ASCII characters. You are probably entering some character (perhaps 0x000000D0) encoded in some other form, and the compiler cannot decode it because it's not in UTF-8. How might you have gotten an non-ASCII character? It might be on your keyboard, like an o with an umlaut. Or you might have pasted it from a word processing document or a pdf file, like a "smart quote." Better solution? Use a text editor that understands encodings, and set it to always use UTF-8. Another solution? Figure out what encoding your editor is stuck in, and declare that in your python file, as the second line. Final solutions? Use a hex viewer to search the file for that D0 mentioned, and figure out just where in your source it is. There's no reason to assume we could even see it here, since your message is encoded in Windows-1252. -- DaveA From lilytran at adobe.com Thu Aug 9 07:12:41 2012 From: lilytran at adobe.com (Lily Tran) Date: Wed, 8 Aug 2012 22:12:41 -0700 Subject: [Tutor] help - SyntaxError: Non-UTF-8 code using python 3 In-Reply-To: <50234628.6030201@davea.name> Message-ID: Thanks. I found the problem character and was able to resolve it. Lily On 8/8/12 10:10 PM, "Dave Angel" wrote: >On 08/08/2012 11:26 PM, Lily Tran wrote: >> Hello; >> >> >> I am getting the following error when I try to run this python program >>in eclipse. I am running python 3: >> >> >> File "/Users/lilytran/Desktop/python/Ex_Files_Python_3_EssT/Exercise >>Files/class_beginner_python/hw3_2_lab6.py", line 30 >> >> SyntaxError: Non-UTF-8 code starting with '\xd0' in file on line 30, >>but no encoding declared; see http://python.org/dev/peps/pep-0263/ for >>details >> >> ============================================================ >> >> >> Can you please tell me what I am doing wrong and how do I fix this >>error? Below is my program. Thanks ?Lily >> >> ==================================================== >> >> >> import random >> >> >> >> def MagicEightBallEmulator(): >> >> >> what = random.choice(answers) >> >> return print(what) >> >> >> def RunEmulator(): >> >> while True: >> >> print ('Welcome to Magic 8 Ball!') >> >> print ('Please ask your question!') >> >> question = input() >> >> if question == 'quit': >> >> break >> >> MagicEightBallEmulator() >> >> >> >> RunEmulator() >> >> > >Sending us the source without adding a comment to line #30 seems a bit >presumptuous. Double spacing by sending it as a non-text message makes >it worse. > >The error message itself seems pretty clear. You have a non-UTF8 >sequence in a source file implicitly declared as being UTF8. > >Simplest solution? Don't use non-ASCII characters. You are probably >entering some character (perhaps 0x000000D0) encoded in some other form, >and the compiler cannot decode it because it's not in UTF-8. How might >you have gotten an non-ASCII character? It might be on your keyboard, >like an o with an umlaut. Or you might have pasted it from a word >processing document or a pdf file, like a "smart quote." > >Better solution? Use a text editor that understands encodings, and set >it to always use UTF-8. > >Another solution? Figure out what encoding your editor is stuck in, and >declare that in your python file, as the second line. > >Final solutions? Use a hex viewer to search the file for that D0 >mentioned, and figure out just where in your source it is. There's no >reason to assume we could even see it here, since your message is >encoded in Windows-1252. > >-- > >DaveA > From alan.gauld at btinternet.com Thu Aug 9 09:38:11 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 09 Aug 2012 08:38:11 +0100 Subject: [Tutor] help - SyntaxError: Non-UTF-8 code using python 3 In-Reply-To: References: Message-ID: On 09/08/12 04:26, Lily Tran wrote: > I am getting the following error when I try to run this python program > in eclipse. I am running python 3: I see you fixed that,but there are other problems: > def MagicEightBallEmulator(): > answers = ["As I see it, yes", .... > "Very doubtful"] > > > while True: > what = random.choice(answers) This loop will go on forever. I don't think you want a loop here you just want to select a choice once. > return print(what) print is a function which returns None. So your function returns None. Youcan therefore eiother dispense with the return statement and Python will return None as default, or, as better practice, "return what" and put the print() call in your main function: > MagicEightBallEmulator() becomes print( MagicEightBallEmulator() ) HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From zaatlob at hotmail.com Thu Aug 9 14:20:15 2012 From: zaatlob at hotmail.com (leon zaat) Date: Thu, 9 Aug 2012 12:20:15 +0000 Subject: [Tutor] help -Global name xxxxxxxx is not definied. In-Reply-To: References: , Message-ID: Hello everyone, Can anybody help me with this problem. Ik have a program that i' am modifying. Ik build a function to export data to a csv file. I tried the functions over different parts, that needed to be extracted. The parts on it self worked fine. Now I put all parts in my function. Because there was a sections that was all alike for the parts, i thought to put it in a second function. This is the code I written: #------------------------------------------------------------------------------ # schrijven van de records #------------------------------------------------------------------------------ def schrijfExportRecord(): sql1=""; sql1="Select huisnummer, huisletter, huisnummertoevoeging, postcode, gerelateerdeopenbareruimte from nummeraanduiding " sql1a= "where aanduidingrecordinactief<> 'J' and" hsql1=" einddatum > '" + dag + "' and identificatie = '" + hoofdadres + "'"; sql1= sql1 + sql1a + hsql1; num= database.select(sql1); for row in num: huisnummer=row[0]; huisletter=row[1]; huisnummertoevoeging=row[2]; postcode=row[3]; gerelateerdeopenbareruimte=row[4]; sql2="Select openbareruimtenaam, gerelateerdewoonplaats from openbareruimte where aanduidingrecordinactief<> 'J'" sql2= sql2 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdeopenbareruimte + "'"; opn=database.select(sql2); for row in database.select(sql2): openbareruimtenaam=row[0]; gerelateerdewoonplaats=row[1]; sql3="Select woonplaatsnaam from woonplaats where aanduidingrecordinactief<> 'J'" sql3= sql3 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdewoonplaats + "'"; wpl=database.select(sql3); for row in wpl: woonplaatsnaam=row[0]; newrow=[identificatie, verblijfobjectgeometrie, huisnummer, huisletter, huisnummertoevoeging, postcode,openbareruimtenaam, woonplaatsnaam]; verblijfhoofd.writerow(newrow); del wpl[:]; del opn[:]; del num[:]; #-------------------------------------------------------------------------------------- # Exporteer benodigde gegevens #-------------------------------------------------------------------------------------- def ExportBestanden(self, event): ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb') verblijfhoofd = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) dag=str(datetime.date.today()); sql4="Select adresseerbaarobjectnevenadres.identificatie, adresseerbaarobjectnevenadres.nevenadres from adresseerbaarobjectnevenadres where aanduidingrecordinactief<> 'J' order by adresseerbaarobjectnevenadres.identificatie" neven= database.select(sql4); for row in neven: nidentificatie=row[0]; nevenadres=row[1]; sql="Select identificatie, hoofdadres, ligplaatsgeometrie from ligplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" lig= database.select(sql); for row in lig: hoofdadres=nevenadres; identificatie=row[0]; verblijfobjectgeometrie=row[2]; schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie) sql="Select identificatie, hoofdadres, verblijfsobjectgeometrie from verblijfsobject where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" vbo= database.select(sql); for row in vbo: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie) sql="Select identificatie, hoofdadres, standplaatsgeometrie from standplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" stand= database.select(sql); for row in stand: hoofdadres=nevenadres; identificatie=row[0]; verblijfobjectgeometrie=row[2]; schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie) del stand[:]; del vbo[:]; del lig[:]; del neven[:]; sql="Select identificatie, hoofdadres, ligplaatsgeometrie from ligplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "'" lig= database.select(sql); for row in lig: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; schrijfExportRecord() del lig[:]; sql="Select identificatie, hoofdadres, standplaatsgeometrie from standplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "'" stand= database.select(sql); for row in stand: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie) del stand[:]; counterVBO=2; identificatie='00000000'; while 1 < counterVBO: hulpIdentificatie= identificatie; sql="Select identificatie, hoofdadres, verblijfsobjectgeometrie from verblijfsobject where "; sql= sql + "identificatie > '" + hulpIdentificatie ; sql= sql + "' and aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' order by identificatie limit 100000"; vbo= database.select(sql); if not vbo: break; else: for row in vbo: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; sql1=""; sql1="Select huisnummer, huisletter, huisnummertoevoeging, postcode, gerelateerdeopenbareruimte from nummeraanduiding " sql1a= "where aanduidingrecordinactief<> 'J' and" hsql1=" einddatum > '" + dag + "' and identificatie = '" + hoofdadres + "'"; sql1= sql1 + sql1a + hsql1; num= database.select(sql1); for row in num: huisnummer=row[0]; huisletter=row[1]; huisnummertoevoeging=row[2]; postcode=row[3]; gerelateerdeopenbareruimte=row[4]; sql2="Select openbareruimtenaam, gerelateerdewoonplaats from openbareruimte where aanduidingrecordinactief<> 'J'" sql2= sql2 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdeopenbareruimte + "'"; opn=database.select(sql2); for row in database.select(sql2): openbareruimtenaam=row[0]; gerelateerdewoonplaats=row[1]; sql3="Select woonplaatsnaam from woonplaats where aanduidingrecordinactief<> 'J'" sql3= sql3 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdewoonplaats + "'"; wpl=database.select(sql3); for row in wpl: woonplaatsnaam=row[0]; newrow=[identificatie, verblijfobjectgeometrie, huisnummer, huisletter, huisnummertoevoeging, postcode,openbareruimtenaam, woonplaatsnaam]; verblijfhoofd.writerow(newrow); del vbo[:]; ofile.close() When i run the program i got the following message: NameError: global name 'schrijfExportRecord' is not found What I am doing wrong and how can i fix it? -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Aug 9 14:45:58 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 09 Aug 2012 14:45:58 +0200 Subject: [Tutor] help -Global name xxxxxxxx is not definied. References: Message-ID: leon zaat wrote: > Hello everyone, > > Can anybody help me with this problem. > Ik have a program that i' am modifying. > > Ik build a function to export data to a csv file. I tried the functions > over different parts, that needed to be extracted. The parts on it self > worked fine. Now I put all parts in my function. Because there was a > sections that was all alike for the parts, i thought to put it in a second > function. > > This is the code I written: > # schrijven van de records > def schrijfExportRecord(): > sql1=""; ... > def ExportBestanden(self, event): > ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb') ... > When i run the program i got the following message: > NameError: global name 'schrijfExportRecord' is not found > > What I am doing wrong and how can i fix it? Judging from the indentation and the 'self' argument the code you quote is inside a class, e. g. class Whatever: def schrijfExportRecord(): ... def ExportBestanden(self, event): ... This layout makes shrijfExportRecord() a method and you have to (1) add a self argument: def schrijfExportRecord(self): ... (2) invoke it in other methods as self.schrijfExportRecord() Alternatively you can leave it as is and move it out of the class (don't forget to fix the indentation accordingly): def schrijfExportRecord(): ... class Whatever: def ExportBestanden(self, event): ... By the way del wpl[:]; is probably "cargo cult" (the code has no effect) because the list is garbage-collected at the end of the function anyway. Also, in Python you don't need to place a ';' at the end of a statement unless you put multiple statements into one line (which is discouraged). From zaatlob at hotmail.com Thu Aug 9 15:09:44 2012 From: zaatlob at hotmail.com (leon zaat) Date: Thu, 9 Aug 2012 13:09:44 +0000 Subject: [Tutor] help -Global name xxxxxxxx is not definied. In-Reply-To: References: , , Message-ID: Thanks, adding the self as suggested did the trick. > To: tutor at python.org > From: __peter__ at web.de > Date: Thu, 9 Aug 2012 14:45:58 +0200 > Subject: Re: [Tutor] help -Global name xxxxxxxx is not definied. > > leon zaat wrote: > > > Hello everyone, > > > > Can anybody help me with this problem. > > Ik have a program that i' am modifying. > > > > Ik build a function to export data to a csv file. I tried the functions > > over different parts, that needed to be extracted. The parts on it self > > worked fine. Now I put all parts in my function. Because there was a > > sections that was all alike for the parts, i thought to put it in a second > > function. > > > > This is the code I written: > > > # schrijven van de records > > > def schrijfExportRecord(): > > sql1=""; > ... > > > def ExportBestanden(self, event): > > ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb') > ... > > > When i run the program i got the following message: > > NameError: global name 'schrijfExportRecord' is not found > > > > What I am doing wrong and how can i fix it? > > Judging from the indentation and the 'self' argument the code you quote is > inside a class, e. g. > > class Whatever: > def schrijfExportRecord(): > ... > def ExportBestanden(self, event): > ... > > This layout makes shrijfExportRecord() a method and you have to > > (1) add a self argument: def schrijfExportRecord(self): ... > (2) invoke it in other methods as self.schrijfExportRecord() > > Alternatively you can leave it as is and move it out of the class (don't > forget to fix the indentation accordingly): > > def schrijfExportRecord(): > ... > > class Whatever: > def ExportBestanden(self, event): > ... > > By the way > > del wpl[:]; > > is probably "cargo cult" (the code has no effect) because the list is > garbage-collected at the end of the function anyway. Also, in Python you > don't need to place a ';' at the end of a statement unless you put multiple > statements into one line (which is discouraged). > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 9 18:03:11 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 09 Aug 2012 17:03:11 +0100 Subject: [Tutor] help -Global name xxxxxxxx is not definied. In-Reply-To: References: , Message-ID: On 09/08/12 13:20, leon zaat wrote: > When i run the program i got the following message: > NameError: global name 'schrijfExportRecord' is not found > > What I am doing wrong and how can i fix it? Looks like you found the problem in this case but for future reference... Please, always post the entire error message not just the last line. It will usually show us which line it is complaining about and the call stack surrounding it. All very helpful in locating the source of the problem. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From richkappler at gmail.com Fri Aug 10 04:24:05 2012 From: richkappler at gmail.com (richard kappler) Date: Thu, 9 Aug 2012 22:24:05 -0400 Subject: [Tutor] receiving string from shell Message-ID: The summer of intensive learning continues. Working on subprocess today. I figured out how to send data out, for example to my festival tts engine: [code]response = k.respond(input, "richard") festivalCmd = '(SayText "%s")' % response subprocess.Popen(['/usr/bin/festival', '-b', festivalCmd])[/code] and that works fine. Now I'm trying to go the other way, starting up my speech recognition engine. The trivial bit is starting it up: [code]subprocess.Popen(['pocketsphinx_continuous -logfn /dev/null'])[/code] I'm at a loss, however, as to how to retrieve the text that pocketsphinx generates from speech into my python program. In other words, the above starts the speech rec engine, and when I say something the recognizer grabs it and prints what I said out as text on the command line. But I'm struggling how to get that text string into my Python program. Should I be looking at pipes, communicate(), Popen.returncode(), something completely different :wink: ? It's probably obvious but I've been working on face recognition all day and now have turned to this, so perhaps I'm a bit burned out. Any guidance would be greatly appreciated so I can get a fresh start in the morning. And now that I think about it, it's even more complex. The speech rec program would be started when my python program starts and keep running. So the python program would have to continuously monitor the terminal for text output to import to python, but would have to ignore the "Ready" or "Listening" prompt within pocketshpinx that shows while waiting for speech input. And I thought this would be easy. Back to the 2.7 docs. Perhaps a few hours of sleep first, Richard -- "Treat all disasters as if they were trivialities but never treat a triviality as if it were a disaster." -- *Quentin Crisp * -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Fri Aug 10 04:44:04 2012 From: d at davea.name (Dave Angel) Date: Thu, 09 Aug 2012 22:44:04 -0400 Subject: [Tutor] receiving string from shell In-Reply-To: References: Message-ID: <50247574.80700@davea.name> On 08/09/2012 10:24 PM, richard kappler wrote: > The summer of intensive learning continues. Working on subprocess today. > I figured out how to send data out, for example to my festival tts engine: > > [code]response = k.respond(input, "richard") > festivalCmd = '(SayText "%s")' % response > subprocess.Popen(['/usr/bin/festival', '-b', festivalCmd])[/code] > > and that works fine. Now I'm trying to go the other way, starting up my > speech recognition engine. The trivial bit is starting it up: > > [code]subprocess.Popen(['pocketsphinx_continuous -logfn /dev/null'])[/code] > > I'm at a loss, however, as to how to retrieve the text that pocketsphinx > generates from speech into my python program. In other words, the above > starts the speech rec engine, and when I say something the recognizer grabs > it and prints what I said out as text on the command line. But I'm > struggling how to get that text string into my Python program. Should I be > looking at pipes, communicate(), Popen.returncode(), something completely > different :wink: ? > > It's probably obvious but I've been working on face recognition all day and > now have turned to this, so perhaps I'm a bit burned out. Any guidance > would be greatly appreciated so I can get a fresh start in the morning. > > And now that I think about it, it's even more complex. The speech rec > program would be started when my python program starts and keep running. So > the python program would have to continuously monitor the terminal for text > output to import to python, but would have to ignore the "Ready" or > "Listening" prompt within pocketshpinx that shows while waiting for speech > input. > > And I thought this would be easy. Back to the 2.7 docs. > > Perhaps a few hours of sleep first, Richard > > Your first question should be to find out the behavior of this speech rec. engine application, assuming you don't have the option of changing it. So try running it from bash, redirecting stdout to a file. Do a tail -f on that file and see if that's exactly what you'd like your python app to see. If it's not, check to see if there are cmdline options for the engine that change its behavior. It's possible that it sends the prompts to stderr. Anyway, you can investigate that way, before trying to get a pipe working from Python. -- DaveA From sntshkmr60 at gmail.com Fri Aug 10 06:01:41 2012 From: sntshkmr60 at gmail.com (Santosh Kumar) Date: Fri, 10 Aug 2012 09:31:41 +0530 Subject: [Tutor] Confusion with Python, Bash and Command Prompt Message-ID: Hello There, We all know that line starting with "#" in Python is a comment. We also know that when the first line of any file (with any extension) has "#!/usr/bin/env bash" and if you make it executable and do ./filename.ext in the terminal then it will be considered as bash file (in this case even if the extension is.py the terminal will take it as bash file.) This same thing also happens with Python (If the first line of the file has #!/usr/bin/env python then even if the extension is .txt, if you make it an executable then terminal will process it with python interpreter.) So far we know two things: 1. "#" makes the line a comment in Python. 2. If processing file with bash, no matter what the file extension is, if the first line of that file hsa "#!/usr/bin/env python" and it is an executable and you run it doing like ./filename.ext, the bash will call python interpreter to process that file. Now let's get back. I'm reading a book "A Byte of Python" (http://www.swaroopch.org/notes/Python). In this page: http://www.swaroopch.org/notes/Python_en:First_Steps, under the section Using a Source File. There is a line saying that "A word of caution on the shebang..". On the next line the book says we can use #!C:\Python32\python.exe to make it executable. My Question: Is it true that doing that is as same as doing #!/usr/bin/env python on Unix? Because I think that the matter of shebang is limited to Bash and Windows don't have a bash, it has a Command Prompt. And I don't think such thing happens in Windows. From steve at pearwood.info Fri Aug 10 06:20:18 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 10 Aug 2012 14:20:18 +1000 Subject: [Tutor] Confusion with Python, Bash and Command Prompt In-Reply-To: References: Message-ID: <50248C02.8070308@pearwood.info> On 10/08/12 14:01, Santosh Kumar wrote: > Hello There, > > We all know that line starting with "#" in Python is a comment. > > We also know that when the first line of any file (with any extension) > has "#!/usr/bin/env bash" and if you make it executable and do > ./filename.ext in the terminal then it will be considered as bash file > (in this case even if the extension is.py the terminal will take it as > bash file.) That is not Python's doing. That is the shell, and so it depends entirely on your choice of operating system and shell. It works on Unix, Linux and probably Mac OS, but not on Windows. As far as Python is concerned, "#!/usr/bin/env python" is just a meaningless comment. > Now let's get back. I'm reading a book "A Byte of Python" > (http://www.swaroopch.org/notes/Python). In this page: > http://www.swaroopch.org/notes/Python_en:First_Steps, under the > section Using a Source File. There is a line saying that "A word of > caution on the shebang..". On the next line the book says we can use > #!C:\Python32\python.exe to make it executable. The book is completely wrong there. Windows does not pay any attention to shebang lines. Although, soon, Python itself will provide a launcher for Windows which understands shebang lines: http://www.python.org/dev/peps/pep-0397/ -- Steven From modulok at gmail.com Fri Aug 10 07:35:08 2012 From: modulok at gmail.com (Modulok) Date: Thu, 9 Aug 2012 23:35:08 -0600 Subject: [Tutor] Confusion with Python, Bash and Command Prompt In-Reply-To: References: Message-ID: ... > My Question: > Is it true that doing that is as same as doing #!/usr/bin/env python > on Unix? Because I think that the matter of shebang is limited to Bash > and Windows don't have a bash, it has a Command Prompt. And I don't > think such thing happens in Windows. It has nothing directly do with bash. It has to do with the operating system's 'program loader'. It's the low level code responsible for setting up an execution environment for a new process. It looks for environment variables, etc. For example on FreeBSD most shebang parsing defined in envopts.c. Basically, whenever you request a file to be executed, the program loader (not your shell) looks at the first line of the file for a shebang. If found, it executes the file the shebang line references and passes the path to the current script file as the first argument. That said, different operating systems' program loaders', regardless of the shell you're using (be it bash, tcsh, sh, etc), will process shebang lines differently. Some ignore them entirely. The syntax can even vary slightly from one OS to another (and not just the file paths). So does it apply to Windows? No. Windows has no concept of shebang lines. File types are determined exclusively their extensions, as is the executability of a file. (There's a registry looking that occurs to determine what program to pass the file to.) If you rename a python script from foo.py to foo.jpg, windows will attempt to open it in an image viewer, regardless of any shebang lines present. (Contrast this with most unix-like flavors where extensions are meaningless. Executability is determined by a file-system level permission bit and the interpreter is determined by the program loader reading shebang lines. The file would be passed to the python interpretter, assuming a correct shebang.) You are right in your assumption; It does not apply to Windows. The tutorial is incorrect. -Modulok- From d at davea.name Fri Aug 10 08:38:03 2012 From: d at davea.name (Dave Angel) Date: Fri, 10 Aug 2012 02:38:03 -0400 Subject: [Tutor] Confusion with Python, Bash and Command Prompt In-Reply-To: References: Message-ID: <5024AC4B.6070800@davea.name> On 08/10/2012 12:01 AM, Santosh Kumar wrote: > Hello There, > > We all know that line starting with "#" in Python is a comment. > > We also know that when the first line of any file (with any extension) > has "#!/usr/bin/env bash" and if you make it executable and do > ./filename.ext in the terminal then it will be considered as bash file > (in this case even if the extension is.py the terminal will take it as > bash file.) > > This same thing also happens with Python (If the first line of the > file has #!/usr/bin/env python then even if the extension is .txt, if > you make it an executable then terminal will process it with python > interpreter.) > > So far we know two things: > 1. "#" makes the line a comment in Python. > 2. If processing file with bash, no matter what the file extension is, > if the first line of that file hsa "#!/usr/bin/env python" and it is > an executable and you run it doing like ./filename.ext, the bash will > call python interpreter to process that file. > > Now let's get back. I'm reading a book "A Byte of Python" > (http://www.swaroopch.org/notes/Python). In this page: > http://www.swaroopch.org/notes/Python_en:First_Steps, under the > section Using a Source File. There is a line saying that "A word of > caution on the shebang..". On the next line the book says we can use > #!C:\Python32\python.exe to make it executable. > > My Question: > Is it true that doing that is as same as doing #!/usr/bin/env python > on Unix? Because I think that the matter of shebang is limited to Bash > and Windows don't have a bash, it has a Command Prompt. And I don't > think such thing happens in Windows. Nothing to do with Windows, or bash really. It's a question of who interprets the line you're typing, and then how that program decides which program to run. Unix-like systems have one set of conventions, but there's no problem breaking them if you don't mind upsetting users. Windows has a different convention, and there's no problem there in breaking it. The standard shell for Windows NT systems is called cmd.exe (It was command.com for MSDOS). But many other shells have been written for Windows, both free and commercial. It's likely that at least one of them processes the shebang line according to Unix conventions. Perhaps your book has available such an alternative shell. Cygwin can be installed into Windows. I would expect that Cygwin comes with its own shell. However, once you're using that one, you probably don't use filenames with colons inside them. I haven't used Windows much in the last few years, but seems to me Cygwin mapped the C: drive into some place like /mnt/c_drive If I have it even close, then the shebang line would look something like: #!/mnt/c_drive/Python32/python.exe -- DaveA From steve at pearwood.info Fri Aug 10 10:44:10 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 10 Aug 2012 18:44:10 +1000 Subject: [Tutor] Confusion with Python, Bash and Command Prompt In-Reply-To: References: Message-ID: <5024C9DA.1040907@pearwood.info> On 10/08/12 15:35, Modulok wrote: > ... >> My Question: >> Is it true that doing that is as same as doing #!/usr/bin/env python >> on Unix? Because I think that the matter of shebang is limited to Bash >> and Windows don't have a bash, it has a Command Prompt. And I don't >> think such thing happens in Windows. > > It has nothing directly do with bash. It has to do with the operating system's > 'program loader'. Correction noted, thank you. -- Steven From zaatlob at hotmail.com Fri Aug 10 12:02:57 2012 From: zaatlob at hotmail.com (leon zaat) Date: Fri, 10 Aug 2012 10:02:57 +0000 Subject: [Tutor] help--- opening csv in different functions Message-ID: I am trying to fill a file. When i am start the leading fuction the file schould be overwriting the , probarly, existing csv file. Accoording from keys from different keys in my sql files, information is collected and written in a other function. I tried something like this class BAGExtractPlus(wx.Frame): .......... #------------------------------------------------------------------------------ # schrijven van de records #------------------------------------------------------------------------------ def schrijfExportRecord(self,identificatie, hoofdadres, verblijfobjectgeometrie, dag): ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wa') verblijfhoofd = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) sql1=""; sql1="Select huisnummer, huisletter, huisnummertoevoeging, postcode, gerelateerdeopenbareruimte from nummeraanduiding " sql1a= "where aanduidingrecordinactief<> 'J' and" hsql1=" einddatum > '" + dag + "' and identificatie = '" + hoofdadres + "'"; sql1= sql1 + sql1a + hsql1; num= database.select(sql1); for row in num: huisnummer=row[0]; huisletter=row[1]; huisnummertoevoeging=row[2]; postcode=row[3]; gerelateerdeopenbareruimte=row[4]; sql2="Select openbareruimtenaam, gerelateerdewoonplaats from openbareruimte where aanduidingrecordinactief<> 'J'" sql2= sql2 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdeopenbareruimte + "'"; opn=database.select(sql2); for row in database.select(sql2): openbareruimtenaam=row[0]; gerelateerdewoonplaats=row[1]; sql3="Select woonplaatsnaam from woonplaats where aanduidingrecordinactief<> 'J'" sql3= sql3 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdewoonplaats + "'"; wpl=database.select(sql3); for row in wpl: woonplaatsnaam=row[0]; newrow=[identificatie, verblijfobjectgeometrie, huisnummer, huisletter, huisnummertoevoeging, postcode,openbareruimtenaam, woonplaatsnaam]; verblijfhoofd.writerow(newrow); del wpl[:]; del opn[:]; del num[:]; #-------------------------------------------------------------------------------------- # Exporteer benodigde gegevens #-------------------------------------------------------------------------------------- def ExportBestanden(self, event): ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb') verblijfhoofd = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) dag=str(datetime.date.today()); sql4="Select adresseerbaarobjectnevenadres.identificatie, adresseerbaarobjectnevenadres.nevenadres from adresseerbaarobjectnevenadres where aanduidingrecordinactief<> 'J' order by adresseerbaarobjectnevenadres.identificatie" neven= database.select(sql4); for row in neven: nidentificatie=row[0]; nevenadres=row[1]; sql="Select identificatie, hoofdadres, ligplaatsgeometrie from ligplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" lig= database.select(sql); for row in lig: hoofdadres=nevenadres; identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) sql="Select identificatie, hoofdadres, verblijfsobjectgeometrie from verblijfsobject where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" vbo= database.select(sql); for row in vbo: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) sql="Select identificatie, hoofdadres, standplaatsgeometrie from standplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" stand= database.select(sql); for row in stand: hoofdadres=nevenadres; identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) del stand[:]; del vbo[:]; del lig[:]; del neven[:]; sql="Select identificatie, hoofdadres, ligplaatsgeometrie from ligplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "'" lig= database.select(sql); for row in lig: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) del lig[:]; sql="Select identificatie, hoofdadres, standplaatsgeometrie from standplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "'" stand= database.select(sql); for row in stand: hoofdadres=row[1]; identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) del stand[:]; The seems to work, but after 200.00 entries i got the message that opening is not allowed. What i am doing wrong? Is there a way to open the file once. When i didn't open it in the first part, it said it couldn't find the file. -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Fri Aug 10 12:35:42 2012 From: d at davea.name (Dave Angel) Date: Fri, 10 Aug 2012 06:35:42 -0400 Subject: [Tutor] help--- opening csv in different functions In-Reply-To: References: Message-ID: <5024E3FE.4040807@davea.name> On 08/10/2012 06:02 AM, leon zaat wrote: > I am trying to fill a file. > When i am start the leading fuction the file schould be overwriting the , probarly, existing csv file. > Accoording from keys from different keys in my sql files, information is collected and written in a other function. > > I tried something like this > > class BAGExtractPlus(wx.Frame): > .......... > #------------------------------------------------------------------------------ > # schrijven van de records > #------------------------------------------------------------------------------ > def schrijfExportRecord(self,identificatie, hoofdadres, verblijfobjectgeometrie, dag): > > ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wa') > > verblijfhoofd = csv.writer(ofile, delimiter=',', > > quotechar='"', quoting=csv.QUOTE_NONNUMERIC) > sql1=""; > sql1="Select huisnummer, huisletter, huisnummertoevoeging, postcode, gerelateerdeopenbareruimte from nummeraanduiding " > sql1a= "where aanduidingrecordinactief<> 'J' and" > hsql1=" einddatum > '" + dag + "' and identificatie = '" + hoofdadres + "'"; > sql1= sql1 + sql1a + hsql1; > num= database.select(sql1); > for row in num: > huisnummer=row[0]; > huisletter=row[1]; > huisnummertoevoeging=row[2]; > postcode=row[3]; > gerelateerdeopenbareruimte=row[4]; > sql2="Select openbareruimtenaam, gerelateerdewoonplaats from openbareruimte where aanduidingrecordinactief<> 'J'" > sql2= sql2 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdeopenbareruimte + "'"; > opn=database.select(sql2); > for row in database.select(sql2): > openbareruimtenaam=row[0]; > gerelateerdewoonplaats=row[1]; > sql3="Select woonplaatsnaam from woonplaats where aanduidingrecordinactief<> 'J'" > sql3= sql3 + "and einddatum > '" + dag + "' and identificatie = '" + gerelateerdewoonplaats + "'"; > wpl=database.select(sql3); > for row in wpl: > woonplaatsnaam=row[0]; > newrow=[identificatie, verblijfobjectgeometrie, huisnummer, huisletter, huisnummertoevoeging, postcode,openbareruimtenaam, woonplaatsnaam]; > verblijfhoofd.writerow(newrow); > > del wpl[:]; > del opn[:]; > del num[:]; > > #-------------------------------------------------------------------------------------- > # Exporteer benodigde gegevens > #-------------------------------------------------------------------------------------- > def ExportBestanden(self, event): > > ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb') > verblijfhoofd = csv.writer(ofile, delimiter=',', > quotechar='"', quoting=csv.QUOTE_NONNUMERIC) > dag=str(datetime.date.today()); > sql4="Select adresseerbaarobjectnevenadres.identificatie, adresseerbaarobjectnevenadres.nevenadres from adresseerbaarobjectnevenadres where aanduidingrecordinactief<> 'J' order by adresseerbaarobjectnevenadres.identificatie" > neven= database.select(sql4); > for row in neven: > nidentificatie=row[0]; > nevenadres=row[1]; > > sql="Select identificatie, hoofdadres, ligplaatsgeometrie from ligplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" > lig= database.select(sql); > for row in lig: > hoofdadres=nevenadres; > identificatie=row[0]; > verblijfobjectgeometrie=row[2]; > self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) > sql="Select identificatie, hoofdadres, verblijfsobjectgeometrie from verblijfsobject where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" > vbo= database.select(sql); > for row in vbo: > hoofdadres=row[1]; > identificatie=row[0]; > verblijfobjectgeometrie=row[2]; > self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) > sql="Select identificatie, hoofdadres, standplaatsgeometrie from standplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "' and identificatie = '"+ nidentificatie + "'" > stand= database.select(sql); > for row in stand: > hoofdadres=nevenadres; > identificatie=row[0]; > verblijfobjectgeometrie=row[2]; > self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) > del stand[:]; > del vbo[:]; > del lig[:]; > del neven[:]; > sql="Select identificatie, hoofdadres, ligplaatsgeometrie from ligplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "'" > lig= database.select(sql); > for row in lig: > hoofdadres=row[1]; > identificatie=row[0]; > verblijfobjectgeometrie=row[2]; > self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) > del lig[:]; > sql="Select identificatie, hoofdadres, standplaatsgeometrie from standplaats where aanduidingrecordinactief<> 'J' and einddatum >'" + dag + "'" > stand= database.select(sql); > for row in stand: > hoofdadres=row[1]; > identificatie=row[0]; > verblijfobjectgeometrie=row[2]; > self.schrijfExportRecord(identificatie, hoofdadres, verblijfobjectgeometrie, dag) > del stand[:]; > > > The seems to work, but after 200.00 entries i got the message that opening is not allowed. > > What i am doing wrong? > Is there a way to open the file once. > When i didn't open it in the first part, it said it couldn't find the file. > > What do you mean by work? Each time through the for row in stand: loop, it calls schrijfExportRecord(), which truncates the file. I'll assume that's what you want, though I can't see the leading() function mentioned in your introductory comments. If that's the case, remove the first two lines of the method ExportBestanden () . You never use the locals ofile and verblixxxhoofd anyway. On the other hand, if you want to create a single csv file for the whole lifetime of the BagExstraxtPlus instance, then create it in the __init__ method, and save the ofile and the verblixxx thing as an instance attribute. And don't re-open it in either of the existing methods. I also see no sign of a close() call. Perhaps the class needs a close() method, whose job it is to close the csv and the actual file. -- DaveA From vashkevichrb at gmail.com Fri Aug 10 13:44:54 2012 From: vashkevichrb at gmail.com (Roman Vashkevich) Date: Fri, 10 Aug 2012 15:44:54 +0400 Subject: [Tutor] Recursive optimization function, binary tree Message-ID: Alright, this may sound like a dumb stupid question. I am testing a recursive optimization function that builds a binary tree. I started a hand simulation but the amount of manual work grows exponentially with the amount of function frames. Is there a graphical testing tool for such a task? RV From bgailer at gmail.com Fri Aug 10 15:12:29 2012 From: bgailer at gmail.com (bob gailer) Date: Fri, 10 Aug 2012 09:12:29 -0400 Subject: [Tutor] Recursive optimization function, binary tree In-Reply-To: References: Message-ID: <502508BD.20407@gmail.com> On 8/10/2012 7:44 AM, Roman Vashkevich wrote: > Alright, this may sound like a dumb stupid question. > I am testing a recursive optimization function that builds a binary tree. > I started a hand simulation but the amount of manual work grows exponentially with the amount of function frames. > Is there a graphical testing tool for such a task? Perhaps - but I'd need more details. -- Bob Gailer 919-636-4239 Chapel Hill NC From wrw at mac.com Fri Aug 10 15:31:37 2012 From: wrw at mac.com (William R. Wing (Bill Wing)) Date: Fri, 10 Aug 2012 09:31:37 -0400 Subject: [Tutor] Confusion with Python, Bash and Command Prompt In-Reply-To: <50248C02.8070308@pearwood.info> References: <50248C02.8070308@pearwood.info> Message-ID: <79DB9E5D-8BFF-47E8-BD24-E8AFA9ACE9A3@mac.com> On Aug 10, 2012, at 12:20 AM, Steven D'Aprano wrote: > [byte] > That is not Python's doing. That is the shell, and so it depends > entirely on your choice of operating system and shell. It works on Unix, > Linux and probably Mac OS, but not on Windows. > Yes, it definitely does work on Mac OS-X as well. -Bill From alan.gauld at btinternet.com Fri Aug 10 18:41:45 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Aug 2012 17:41:45 +0100 Subject: [Tutor] help--- opening csv in different functions In-Reply-To: References: Message-ID: On 10/08/12 11:02, leon zaat wrote: > I am trying to fill a file. I assume you mean create a file? Or overwrite an existing one? Or append to an existing one. There isn't really a concept of filling a file, they just keep getting bigger until you ruin out of space. (There are some filesystems that support creating fixed size files but they are very marginal cases) > The seems to work, but after 200.00 entries i got the message that > opening is not allowed. > What i am doing wrong? You shouldn't be continually opening the file... And when you do you should be sure to close it again as soon as possible. > Is there a way to open the file once. Yes that's usually what you would do. Open it, collect your data and write it out (either as you collect it or all at once as you prefer) then close the file. > When i didn't open it in the first part, it said it couldn't find the > file. Not sure what that means but if you haven't created it any attempt to read it will fail. But your code was way too long for me to be bothered reading through, sorry. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From selbyrowleycannon at ymail.com Fri Aug 10 19:05:07 2012 From: selbyrowleycannon at ymail.com (Selby Rowley Cannon) Date: Fri, 10 Aug 2012 18:05:07 +0100 Subject: [Tutor] Script won't run for no apparent reason Message-ID: <50253F43.8020609@ymail.com> I have written a small application to encrypt some text. The script looks fine to me, but it won't run and I can't figure out why. I have attached it, if anyone knows why it doesn't work please let me know! -------------- next part -------------- A non-text attachment was scrubbed... Name: crypto.py Type: text/x-python Size: 1447 bytes Desc: not available URL: From joel.goldstick at gmail.com Fri Aug 10 19:17:59 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 10 Aug 2012 13:17:59 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <50253F43.8020609@ymail.com> References: <50253F43.8020609@ymail.com> Message-ID: On Fri, Aug 10, 2012 at 1:05 PM, Selby Rowley Cannon wrote: > I have written a small application to encrypt some text. The script looks > fine to me, but it won't run and I can't figure out why. I have attached it, > if anyone knows why it doesn't work please let me know! > What do you mean 'it won't run'? Do you get an error with Traceback? I glanced at your code, and your dictionary ends like this: , 'X':'A', 'Y':'B', 'Z':'C', There is no closing brace -- Joel Goldstick From steve at pearwood.info Fri Aug 10 19:40:38 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 11 Aug 2012 03:40:38 +1000 Subject: [Tutor] Recursive optimization function, binary tree In-Reply-To: References: Message-ID: <50254796.4060202@pearwood.info> On 10/08/12 21:44, Roman Vashkevich wrote: > Alright, this may sound like a dumb stupid question. > I am testing a recursive optimization function that builds a binary tree. > I started a hand simulation but the amount of manual work grows exponentially with the amount of function frames. > Is there a graphical testing tool for such a task? This is not a dumb stupid question. It is an advanced question. This is a mailing list for questions about learning the Python language, not general Python-related questions or questions about advanced code testing tools. If you are lucky, *maybe* somebody can answer the question if you give more detail about what you want -- give as an example of your function, the expected input, and the expected output, and perhaps we can give you some ideas. Otherwise, try on the main Python mailing list, python-list at python.org, also available as a Newsgroup comp.lang.python. -- Steven From joel.goldstick at gmail.com Fri Aug 10 21:07:34 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 10 Aug 2012 15:07:34 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <502559A3.5090502@ymail.com> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> Message-ID: On Fri, Aug 10, 2012 at 2:57 PM, Selby Rowley Cannon wrote: > On 10/08/12 18:17, Joel Goldstick wrote: >> >> On Fri, Aug 10, 2012 at 1:05 PM, Selby Rowley Cannon >> wrote: >>> >>> I have written a small application to encrypt some text. The script looks >>> fine to me, but it won't run and I can't figure out why. I have attached >>> it, >>> if anyone knows why it doesn't work please let me know! >>> >> What do you mean 'it won't run'? Do you get an error with Traceback? >> >> I glanced at your code, and your dictionary ends like this: , 'X':'A', >> 'Y':'B', 'Z':'C', >> >> There is no closing brace > > OK, > > File "./crypto.py", line 6 > def encrypt(): > ^ > SyntaxError: invalid syntax > First, don't reply to me, reply to the group. That might mean choosing reply all. So, your first problem is that the dictionary isn't closed. This is causing the error at line 6 fix that and find your next error. It looks like there are lots of them With such a small file you would do better to just post the code directly. That way if people see problems they can point them out in the body of the reply good luck -- Joel Goldstick From selbyrowleycannon at ymail.com Fri Aug 10 21:33:39 2012 From: selbyrowleycannon at ymail.com (Selby Rowley Cannon) Date: Fri, 10 Aug 2012 20:33:39 +0100 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> Message-ID: <50256213.9070009@ymail.com> On 10/08/12 20:07, Joel Goldstick wrote: > On Fri, Aug 10, 2012 at 2:57 PM, Selby Rowley Cannon > wrote: >> On 10/08/12 18:17, Joel Goldstick wrote: >>> On Fri, Aug 10, 2012 at 1:05 PM, Selby Rowley Cannon >>> wrote: >>>> I have written a small application to encrypt some text. The script looks >>>> fine to me, but it won't run and I can't figure out why. I have attached >>>> it, >>>> if anyone knows why it doesn't work please let me know! >>>> >>> What do you mean 'it won't run'? Do you get an error with Traceback? >>> >>> I glanced at your code, and your dictionary ends like this: , 'X':'A', >>> 'Y':'B', 'Z':'C', >>> >>> There is no closing brace >> OK, >> >> File "./crypto.py", line 6 >> def encrypt(): >> ^ >> SyntaxError: invalid syntax >> > First, don't reply to me, reply to the group. That might mean > choosing reply all. > > So, your first problem is that the dictionary isn't closed. This is > causing the error at line 6 > > fix that and find your next error. It looks like there are lots of them > > With such a small file you would do better to just post the code > directly. That way if people see problems they can point them out in > the body of the reply > > good luck #!/usr/bin/env python3 import random values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j', 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r', 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z', 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H', 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} def encrypt(): textInputE = input('Please enter the text you wish to encrypt: ') textInputE.list() for Eletter in textInputE.list(): try: print (values[Eletter]) except KeyError: print ('Sorry, that input couldn\'t be parsed as text. Try again.') input('Press Enter') def decrypt(): textInputD = input('Please enter the numbertext you wish to decrypt') textInputD.list() for Dletter in textInputD.list(): try: print (values[Dletter]) except KeyError: print ('Sorry, that input couldn\'t be parsed as numbertext from our cipher. Please try again.') input('Press Enter') while True: EorD = input('Encrypt or Decrypt: ') if EorD == 'Encrypt' or EorD == 'encrypt': encrypt() elif EorD == 'Decrypt' or EorD == 'decrypt': decrypt() else: print('Encrypt or Decrypt?') Thanks, I am not quite used to this client yet. The next error is: Traceback (most recent call last): File "crypto.py", line 25, in EorD = input('Encrypt or Decrypt: ') File "", line 1, in NameError: name 'Encrypt' is not defined From d at davea.name Fri Aug 10 21:48:33 2012 From: d at davea.name (Dave Angel) Date: Fri, 10 Aug 2012 15:48:33 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <50256213.9070009@ymail.com> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> Message-ID: <50256591.6030800@davea.name> On 08/10/2012 03:33 PM, Selby Rowley Cannon wrote: > On 10/08/12 20:07, Joel Goldstick wrote: >> On Fri, Aug 10, 2012 at 2:57 PM, Selby Rowley Cannon >> wrote: >>> On 10/08/12 18:17, Joel Goldstick wrote: >>>> On Fri, Aug 10, 2012 at 1:05 PM, Selby Rowley Cannon >>>> wrote: >>>>> I have written a small application to encrypt some text. The >>>>> script looks >>>>> fine to me, but it won't run and I can't figure out why. I have >>>>> attached >>>>> it, >>>>> if anyone knows why it doesn't work please let me know! >>>>> >>>> What do you mean 'it won't run'? Do you get an error with Traceback? >>>> >>>> I glanced at your code, and your dictionary ends like this: , 'X':'A', >>>> 'Y':'B', 'Z':'C', >>>> >>>> There is no closing brace >>> OK, >>> >>> File "./crypto.py", line 6 >>> def encrypt(): >>> ^ >>> SyntaxError: invalid syntax >>> >> First, don't reply to me, reply to the group. That might mean >> choosing reply all. >> >> So, your first problem is that the dictionary isn't closed. This is >> causing the error at line 6 >> >> fix that and find your next error. It looks like there are lots of them >> >> With such a small file you would do better to just post the code >> directly. That way if people see problems they can point them out in >> the body of the reply >> >> good luck > #!/usr/bin/env python3 > > import random > values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', > 'g':'j', 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', > 'n':'q', 'o':'r', 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', > 'u':'x', 'v':'y', 'w':'z', 'x':'a', 'y':'b', 'z':'c', 'A':'D', > 'B':'E', 'C':'F', 'D':'G', 'E':'H', 'F':'I', 'G':'J', 'H':'K', > 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', 'N':'Q', 'O':'R', > 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', 'V':'Y', > 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} > def encrypt(): > textInputE = input('Please enter the text you wish to encrypt: ') > textInputE.list() > for Eletter in textInputE.list(): > try: > print (values[Eletter]) > except KeyError: > print ('Sorry, that input couldn\'t be parsed as text. Try > again.') > input('Press Enter') > def decrypt(): > textInputD = input('Please enter the numbertext you wish to decrypt') > textInputD.list() > for Dletter in textInputD.list(): > try: > print (values[Dletter]) > except KeyError: > print ('Sorry, that input couldn\'t be parsed as > numbertext from our cipher. Please try again.') > input('Press Enter') > > while True: > EorD = input('Encrypt or Decrypt: ') > if EorD == 'Encrypt' or EorD == 'encrypt': > encrypt() > elif EorD == 'Decrypt' or EorD == 'decrypt': > decrypt() > else: > print('Encrypt or Decrypt?') > > Thanks, I am not quite used to this client yet. The next error is: > > Traceback (most recent call last): > File "crypto.py", line 25, in > EorD = input('Encrypt or Decrypt: ') > File "", line 1, in > NameError: name 'Encrypt' is not defined > Did you play with the script as it stands now? Did you try typing things other than Encrypt at the input prompt? You'll notice that the error message is complaining about what YOU typed as the user. Looks like you're running Python 3 code on a Python 2 system. If you're running Python 2, you want to use raw_input() which gets a string, rather than input(), which tries to evaluate an expression. -- DaveA From joel.goldstick at gmail.com Fri Aug 10 21:53:32 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 10 Aug 2012 15:53:32 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <50256213.9070009@ymail.com> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> Message-ID: On Fri, Aug 10, 2012 at 3:33 PM, Selby Rowley Cannon wrote: > On 10/08/12 20:07, Joel Goldstick wrote: >> >> On Fri, Aug 10, 2012 at 2:57 PM, Selby Rowley Cannon >> wrote: >>> >>> On 10/08/12 18:17, Joel Goldstick wrote: >>>> >>>> On Fri, Aug 10, 2012 at 1:05 PM, Selby Rowley Cannon >>>> wrote: >>>>> >>>>> I have written a small application to encrypt some text. The script >>>>> looks >>>>> fine to me, but it won't run and I can't figure out why. I have >>>>> attached >>>>> it, >>>>> if anyone knows why it doesn't work please let me know! >>>>> >>>> What do you mean 'it won't run'? Do you get an error with Traceback? >>>> >>>> I glanced at your code, and your dictionary ends like this: , 'X':'A', >>>> 'Y':'B', 'Z':'C', >>>> >>>> There is no closing brace >>> >>> OK, >>> >>> File "./crypto.py", line 6 >>> def encrypt(): >>> ^ >>> SyntaxError: invalid syntax >>> >> First, don't reply to me, reply to the group. That might mean >> choosing reply all. >> >> So, your first problem is that the dictionary isn't closed. This is >> causing the error at line 6 >> >> fix that and find your next error. It looks like there are lots of them >> >> With such a small file you would do better to just post the code >> directly. That way if people see problems they can point them out in >> the body of the reply >> >> good luck > > #!/usr/bin/env python3 > > import random > values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j', > 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r', > 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z', > 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H', > 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', > 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', > 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} > def encrypt(): > textInputE = input('Please enter the text you wish to encrypt: ') > textInputE.list() > for Eletter in textInputE.list(): > try: > print (values[Eletter]) > except KeyError: > print ('Sorry, that input couldn\'t be parsed as text. Try > again.') > input('Press Enter') > def decrypt(): > textInputD = input('Please enter the numbertext you wish to decrypt') > textInputD.list() > for Dletter in textInputD.list(): > try: > print (values[Dletter]) > except KeyError: > print ('Sorry, that input couldn\'t be parsed as numbertext from > our cipher. Please try again.') > input('Press Enter') > > while True: > EorD = input('Encrypt or Decrypt: ') so are you sure the line above is really what you have in your code? check the quotes > if EorD == 'Encrypt' or EorD == 'encrypt': > encrypt() > elif EorD == 'Decrypt' or EorD == 'decrypt': > decrypt() > else: > print('Encrypt or Decrypt?') > > Thanks, I am not quite used to this client yet. The next error is: > > Traceback (most recent call last): > File "crypto.py", line 25, in > EorD = input('Encrypt or Decrypt: ') > File "", line 1, in > NameError: name 'Encrypt' is not defined > -- Joel Goldstick From selbyrowleycannon at ymail.com Fri Aug 10 22:02:14 2012 From: selbyrowleycannon at ymail.com (Selby Rowley Cannon) Date: Fri, 10 Aug 2012 21:02:14 +0100 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> Message-ID: <502568C6.7020902@ymail.com> On 10/08/12 20:53, Joel Goldstick wrote: > On Fri, Aug 10, 2012 at 3:33 PM, Selby Rowley Cannon > wrote: >> On 10/08/12 20:07, Joel Goldstick wrote: >>> On Fri, Aug 10, 2012 at 2:57 PM, Selby Rowley Cannon >>> wrote: >>>> On 10/08/12 18:17, Joel Goldstick wrote: >>>>> On Fri, Aug 10, 2012 at 1:05 PM, Selby Rowley Cannon >>>>> wrote: >>>>>> I have written a small application to encrypt some text. The script >>>>>> looks >>>>>> fine to me, but it won't run and I can't figure out why. I have >>>>>> attached >>>>>> it, >>>>>> if anyone knows why it doesn't work please let me know! >>>>>> >>>>> What do you mean 'it won't run'? Do you get an error with Traceback? >>>>> >>>>> I glanced at your code, and your dictionary ends like this: , 'X':'A', >>>>> 'Y':'B', 'Z':'C', >>>>> >>>>> There is no closing brace >>>> OK, >>>> >>>> File "./crypto.py", line 6 >>>> def encrypt(): >>>> ^ >>>> SyntaxError: invalid syntax >>>> >>> First, don't reply to me, reply to the group. That might mean >>> choosing reply all. >>> >>> So, your first problem is that the dictionary isn't closed. This is >>> causing the error at line 6 >>> >>> fix that and find your next error. It looks like there are lots of them >>> >>> With such a small file you would do better to just post the code >>> directly. That way if people see problems they can point them out in >>> the body of the reply >>> >>> good luck >> #!/usr/bin/env python3 >> >> import random >> values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j', >> 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r', >> 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z', >> 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H', >> 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', >> 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', >> 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} >> def encrypt(): >> textInputE = input('Please enter the text you wish to encrypt: ') >> textInputE.list() >> for Eletter in textInputE.list(): >> try: >> print (values[Eletter]) >> except KeyError: >> print ('Sorry, that input couldn\'t be parsed as text. Try >> again.') >> input('Press Enter') >> def decrypt(): >> textInputD = input('Please enter the numbertext you wish to decrypt') >> textInputD.list() >> for Dletter in textInputD.list(): >> try: >> print (values[Dletter]) >> except KeyError: >> print ('Sorry, that input couldn\'t be parsed as numbertext from >> our cipher. Please try again.') >> input('Press Enter') >> >> while True: >> EorD = input('Encrypt or Decrypt: ') > so are you sure the line above is really what you have in your code? > check the quotes > >> if EorD == 'Encrypt' or EorD == 'encrypt': >> encrypt() >> elif EorD == 'Decrypt' or EorD == 'decrypt': >> decrypt() >> else: >> print('Encrypt or Decrypt?') >> >> Thanks, I am not quite used to this client yet. The next error is: >> >> Traceback (most recent call last): >> File "crypto.py", line 25, in >> EorD = input('Encrypt or Decrypt: ') >> File "", line 1, in >> NameError: name 'Encrypt' is not defined >> > > OK, I have put it back into Python 2.7, and now I get: Traceback (most recent call last): File "crypto.py", line 27, in encrypt() File "crypto.py", line 7, in encrypt textInputE.list() AttributeError: 'str' object has no attribute 'list' From stuartv at datalinesys.com Fri Aug 10 22:05:03 2012 From: stuartv at datalinesys.com (Stuart van Zee) Date: Fri, 10 Aug 2012 16:05:03 -0400 Subject: [Tutor] Multipage Tiff to PDF Message-ID: <253qHJueD5920S04.1344629103@web04.cms.usa.net> An HTML attachment was scrubbed... URL: From d at davea.name Fri Aug 10 22:07:50 2012 From: d at davea.name (Dave Angel) Date: Fri, 10 Aug 2012 16:07:50 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> Message-ID: <50256A16.6060909@davea.name> On 08/10/2012 03:53 PM, Joel Goldstick wrote: > On Fri, Aug 10, 2012 at 3:33 PM, Selby Rowley Cannon > wrote: >>> >> #!/usr/bin/env python3 >> >> import random >> values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j', >> 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r', >> 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z', >> 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H', >> 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', >> 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', >> 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} >> def encrypt(): >> textInputE = input('Please enter the text you wish to encrypt: ') >> textInputE.list() >> for Eletter in textInputE.list(): >> try: >> print (values[Eletter]) >> except KeyError: >> print ('Sorry, that input couldn\'t be parsed as text. Try >> again.') >> input('Press Enter') >> def decrypt(): >> textInputD = input('Please enter the numbertext you wish to decrypt') >> textInputD.list() >> for Dletter in textInputD.list(): >> try: >> print (values[Dletter]) >> except KeyError: >> print ('Sorry, that input couldn\'t be parsed as numbertext from >> our cipher. Please try again.') >> input('Press Enter') >> >> while True: >> EorD = input('Encrypt or Decrypt: ') > so are you sure the line above is really what you have in your code? > check the quotes The clue was actually in his code. See his shebang line -- he's using Python 3. So the error is on the data that the user inputs. The other clue, that I noticed, was that his innermost error was on line 1, "called" from input(). Anyway, the cure is to use raw_input() everywhere instead. >> >> >> Thanks, I am not quite used to this client yet. The next error is: >> >> Traceback (most recent call last): >> File "crypto.py", line 25, in >> EorD = input('Encrypt or Decrypt: ') >> File "", line 1, in >> NameError: name 'Encrypt' is not defined >> > > -- DaveA From d at davea.name Fri Aug 10 22:16:47 2012 From: d at davea.name (Dave Angel) Date: Fri, 10 Aug 2012 16:16:47 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <502568C6.7020902@ymail.com> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> <502568C6.7020902@ymail.com> Message-ID: <50256C2F.2060004@davea.name> On 08/10/2012 04:02 PM, Selby Rowley Cannon wrote: > OK, I have put it back into Python 2.7, and now I get: > Traceback (most recent call last): > File "crypto.py", line 27, in > encrypt() > File "crypto.py", line 7, in encrypt > textInputE.list() > AttributeError: 'str' object has no attribute 'list' > What did you intend with that line? Even if there were such a method, it's unlikely that it does anything useful, and you do nothing with its return value. -- DaveA From deshpande.jaidev at gmail.com Fri Aug 10 22:44:49 2012 From: deshpande.jaidev at gmail.com (Jaidev Deshpande) Date: Sat, 11 Aug 2012 02:14:49 +0530 Subject: [Tutor] The exec command and namespaces Message-ID: Hi, Supposed I have a string containing a python script and I exec that script. Is there a way to keep track of the variables that this exec() command creates? Say, >>> s = 'for i in range(10):\n\tprint i\n\n' >>> exec(s) 0 1 2 3 4 5 6 7 8 9 Is there a way to ascertain that the variable 'i' was created through the exec function? I'm looking for a way to identify all python variables that a given exec call creates. Thanks From malaclypse2 at gmail.com Fri Aug 10 23:05:43 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 10 Aug 2012 17:05:43 -0400 Subject: [Tutor] The exec command and namespaces In-Reply-To: References: Message-ID: On Fri, Aug 10, 2012 at 4:44 PM, Jaidev Deshpande wrote: > Hi, > > Supposed I have a string containing a python script and I exec that script. > > Is there a way to keep track of the variables that this exec() command creates? Sure. You can provide the dictionaries that exec will use for globals and locals. So, something like this might get you started: >>> my_globals = {} >>> my_locals = {} >>> exec "favorite_color = 'blue'" in my_globals,my_locals >>> print(my_locals) {'favorite_color': 'blue'} From bodsda at googlemail.com Fri Aug 10 23:11:21 2012 From: bodsda at googlemail.com (Bod Soutar) Date: Fri, 10 Aug 2012 22:11:21 +0100 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <502568C6.7020902@ymail.com> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> <502568C6.7020902@ymail.com> Message-ID: >> > OK, I have put it back into Python 2.7, and now I get: > > Traceback (most recent call last): > File "crypto.py", line 27, in > encrypt() > File "crypto.py", line 7, in encrypt > textInputE.list() > AttributeError: 'str' object has no attribute 'list' Would it be a strange conclusion to come to that perhaps the object type of textInputE doesn't have an attribute 'list'? >>> help(str) -- Bodsda -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at linux-ip.net Sat Aug 11 00:10:23 2012 From: martin at linux-ip.net (Martin A. Brown) Date: Fri, 10 Aug 2012 18:10:23 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <50256213.9070009@ymail.com> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> Message-ID: Hello, : #!/usr/bin/env python3 : : import random : values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j', : 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r', : 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z', : 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H', : 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', : 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', : 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} This sort of thing always catches my eye, and I think to myself.... 'Are there any tools or libraries in this language that I could use to generate this, instead of writing out this repetitive data structure?' Here's what I did for my own amusement and possibly of benefit to you. There are probably better solutions out there for your Caesar cipher enjoyment, but I hope you may find this helpful. # -- This code should create a dictionary that should look like the # one above, but you can create it on the fly with a different # value for the shift. You could also use a different alphabet. # def generate_caesar_cipher(alphabet,shift): offset = shift - len(alphabet) cipheralpha = ''.join((alphabet[offset:], alphabet[0:offset])) return dict(zip(alphabet,cipheralpha)) caesar_shift = 3 values = dict() values.update(generate_caesar_cipher(string.ascii_letters,caesar_shift)) One other thing to consider is that you can use the underutilized function 'translate' from the string module. The 'maketrans' function creates a translation table and the 'translate' function applies that to input. def alt_trans(plain_alpha,shift): offset = shift - len(plain_alpha) cipher_alpha = ''.join((plain_alpha[offset:], plain_alpha[0:offset])) return string.maketrans(plain_alpha,cipher_alpha) plaintext = 'Alea iacta est.' shift_cipher = alt_trans(string.ascii_letters, caesar_shift) ciphertext = string.translate(plaintext,shift_cipher) Enjoy Python! -Martin -- Martin A. Brown http://linux-ip.net/ From steve at pearwood.info Sat Aug 11 04:14:48 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 11 Aug 2012 12:14:48 +1000 Subject: [Tutor] Multipage Tiff to PDF In-Reply-To: <253qHJueD5920S04.1344629103@web04.cms.usa.net> References: <253qHJueD5920S04.1344629103@web04.cms.usa.net> Message-ID: <5025C018.5040909@pearwood.info> On 11/08/12 06:05, Stuart van Zee wrote: > > I need to write a simple app that will convert a folder of multi-page tiff files > to PDFs. I was hoping to be able to write a simple, one button Tkinter app to do > this because the people who need to run this job a few times a day are basically > unskilled. My initial idea was to use PIL, but as far as I can tell PIL doesn't > handle multi-page tiffs and also doesn't handle the G4 encoding. > > I would appreciate if someone were to point me in the right direction. https://duckduckgo.com/html/?q=python%20multi%20page%20tiff http://www.google.com.au/search?q=python+multipage+tiff http://pypi.python.org/pypi/tiffany/ Is that enough of a pointer? If not, you will need to give us more information about your general level of Python knowledge. -- Steven From wayne at waynewerner.com Sat Aug 11 05:35:12 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Fri, 10 Aug 2012 22:35:12 -0500 (CDT) Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: <50256A16.6060909@davea.name> References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> <50256A16.6060909@davea.name> Message-ID: On Fri, 10 Aug 2012, Dave Angel wrote: > On 08/10/2012 03:53 PM, Joel Goldstick wrote: > The clue was actually in his code. See his shebang line -- he's using > Python 3. So the error is on the data that the user inputs. > The other clue, that I noticed, was that his innermost error was on line > 1, "called" from input(). > Anyway, the cure is to use raw_input() everywhere instead. Or alternatively, if you want to write forward-looking code: try: input = raw_input except NameError: pass # since we're using python 3+ -Wayne From eryksun at gmail.com Sat Aug 11 08:02:54 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 11 Aug 2012 02:02:54 -0400 Subject: [Tutor] Script won't run for no apparent reason In-Reply-To: References: <50253F43.8020609@ymail.com> <502559A3.5090502@ymail.com> <50256213.9070009@ymail.com> Message-ID: On Fri, Aug 10, 2012 at 6:10 PM, Martin A. Brown wrote: > > : values = {'a':'d', 'b':'e', 'c':'f', 'd':'g', 'e':'h', 'f':'i', 'g':'j', > : 'h':'k', 'i':'l', 'j':'m', 'k':'n', 'l':'o', 'm':'p', 'n':'q', 'o':'r', > : 'p':'s', 'q':'t', 'r':'u', 's':'v', 't':'w', 'u':'x', 'v':'y', 'w':'z', > : 'x':'a', 'y':'b', 'z':'c', 'A':'D', 'B':'E', 'C':'F', 'D':'G', 'E':'H', > : 'F':'I', 'G':'J', 'H':'K', 'I':'L', 'J':'M', 'K':'N', 'L':'O', 'M':'P', > : 'N':'Q', 'O':'R', 'P':'S', 'Q':'T', 'R':'U', 'S':'V', 'T':'W', 'U':'X', > : 'V':'Y', 'W':'Z', 'X':'A', 'Y':'B', 'Z':'C'} > > This sort of thing always catches my eye, and I think to myself.... > 'Are there any tools or libraries in this language that I could use > to generate this, instead of writing out this repetitive data > structure?' > > Here's what I did for my own amusement and possibly of benefit to > you. There are probably better solutions out there for your Caesar > cipher enjoyment, but I hope you may find this helpful. > > # -- This code should create a dictionary that should look like the > # one above, but you can create it on the fly with a different > # value for the shift. You could also use a different alphabet. > # > def generate_caesar_cipher(alphabet,shift): > offset = shift - len(alphabet) > cipheralpha = ''.join((alphabet[offset:], alphabet[0:offset])) > return dict(zip(alphabet,cipheralpha)) > > caesar_shift = 3 > > values = dict() > values.update(generate_caesar_cipher(string.ascii_letters,caesar_shift)) Close, but you're rotating the lower and uppercase letters together. In the original they're separate. Here's a different approach using itertools: from itertools import islice, cycle from string import ascii_lowercase, ascii_uppercase def make_caesar_cipher(alphabet=ascii_lowercase, shift=3): return dict(zip(alphabet, islice(cycle(alphabet), shift, None))) values = make_caesar_cipher() values.update(make_caesar_cipher(ascii_uppercase)) From rdmoores at gmail.com Sun Aug 12 00:30:22 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 11 Aug 2012 15:30:22 -0700 Subject: [Tutor] pickle problems Message-ID: 64-bit Win 7 Python 3.2.3 (default, Apr 11 2012, 07:12:16) [MSC v.1500 64 bit (AMD64)] I wrote pickle_attempt.py as an exercise to try to learn to use the pickle module. See the version I edited for Tutor, pickle_attempt_for_web.py at . To show the problems, I've pasted relevant outputs below the code. The lines 117-145 output shows the adding of items 3000000-3000009, but also that the 2000000-2000009 items are missing from dictionary D. Similarly for the 147-174 output. Further below I show the adding, one-by-one, items 100, 101, and 103. But after closing the program and restarting it, those items have disappeared from D. Advice, please. Dick Moores From eryksun at gmail.com Sun Aug 12 03:18:54 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 11 Aug 2012 21:18:54 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 6:30 PM, Richard D. Moores wrote: > > I wrote pickle_attempt.py as an exercise to try to learn to use the > pickle module. See the version I edited for Tutor, > pickle_attempt_for_web.py at > . > .... > But after closing the program and restarting it, those items have > disappeared from D. On line 68 you open the file in 'ab' mode. A pickle stream ends with an ASCII period (\x2e). Anything appended after that is ignored. Use 'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary, so you might want a file extension other than .txt, such as .pkl, .dat, .bin, etc. If you're curious about the protocol, you can disassemble a pickle using pickletools.dis(pickle), where pickle is bytes or a file-like object. From eryksun at gmail.com Sun Aug 12 03:58:56 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 11 Aug 2012 21:58:56 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 9:18 PM, eryksun wrote: > > On line 68 you open the file in 'ab' mode. A pickle stream ends with > an ASCII period (\x2e). Anything appended after that is ignored. Use > 'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary, > so you might want a file extension other than .txt, such as .pkl, > .dat, .bin, etc. To clarify, you can store multiple pickles in a file, but each needs its own load. So you'd have to maintain a session dictionary for the factors of new integers. Then append the pickled session to the file when the user quits. When the program starts you'd have to loop through the file to update D with each pickled session. If you want an approach that's simpler and faster, use the shelve module. A shelf is a dictionary-like object that uses pickle to serialize objects stored to a database. The keys have to be strings, so your code would change to `D[str(n)] = factors`. http://docs.python.org/py3k/library/shelve.html#module-shelve From rdmoores at gmail.com Sun Aug 12 04:43:29 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 11 Aug 2012 19:43:29 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 6:18 PM, eryksun wrote: > On Sat, Aug 11, 2012 at 6:30 PM, Richard D. Moores wrote: >> >> I wrote pickle_attempt.py as an exercise to try to learn to use the >> pickle module. See the version I edited for Tutor, >> pickle_attempt_for_web.py at >> . >> .... >> But after closing the program and restarting it, those items have >> disappeared from D. > > On line 68 you open the file in 'ab' mode. A pickle stream ends with > an ASCII period (\x2e). Anything appended after that is ignored. Use > 'wb' mode. Also, Python 3.x defaults to protocol 3, which is binary, > so you might want a file extension other than .txt, such as .pkl, > .dat, .bin, etc. Changing to 'wb' mode and using the file extension .dat completely corrected the problems, it seems. Line 69 now reads, f = open("factors.dat", 'wb') But the reference I have says of 'wb': "Write to a binary file. If the file exists, its contents are overwritten. If the file doesn't exist, it's created. Why doesn't factors.dat get overwritten before the pickle.dump()? Is it because there isn't any new data to be written at that point? And another question, if I might. If factors.dat doesn't exist, to use the program I need to manually create it and rem out lines 49-52 the first time I call the script. I thought I could replace lines 49-52 with =========== if "factors.dat": f = open("factors.dat", 'rb') data = pickle.load(f) f.close D = data else: f = open("factors.dat", 'wb') ========== Doesn't work. What would? > If you're curious about the protocol, you can disassemble a pickle > using pickletools.dis(pickle), where pickle is bytes or a file-like > object. I'll give that a try. Thanks! Dick Moores From rdmoores at gmail.com Sun Aug 12 05:19:34 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sat, 11 Aug 2012 20:19:34 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 6:58 PM, eryksun wrote: > On Sat, Aug 11, 2012 at 9:18 PM, eryksun wrote: >> > To clarify, you can store multiple pickles in a file, but each needs > its own load. So you'd have to maintain a session dictionary for the > factors of new integers. Then append the pickled session to the file > when the user quits. When the program starts you'd have to loop > through the file to update D with each pickled session. Isn't that essentially what my script does? > If you want an approach that's simpler and faster, use the shelve > module. A shelf is a dictionary-like object that uses pickle to > serialize objects stored to a database. The keys have to be strings, > so your code would change to `D[str(n)] = factors`. > > http://docs.python.org/py3k/library/shelve.html#module-shelve Yes, shelve is next, now that I have mastered (ha!) pickle. Thanks again for the terrific help. Dick From eryksun at gmail.com Sun Aug 12 05:50:33 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 11 Aug 2012 23:50:33 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 10:43 PM, Richard D. Moores wrote: > > Changing to 'wb' mode and using the file extension .dat completely > corrected the problems, it seems. Line 69 now reads, > f = open("factors.dat", 'wb') The extension doesn't affect anything about the file. It's just .txt indicates a text file. > But the reference I have says of 'wb': "Write to a binary file. If the > file exists, its contents are overwritten. If the file doesn't exist, > it's created. Why doesn't factors.dat get overwritten before the > pickle.dump()? Is it because there isn't any new data to be written at > that point? Opening in mode 'wb' truncates the file. Then pickle.dump() writes the pickle to it. Before you were appending to the end of the file, so you had multiple pickled dictionaries stored in the file. > And another question, if I might. If factors.dat doesn't exist, to use > the program I need to manually create it and rem out lines 49-52 the > first time I call the script. I thought I could replace lines 49-52 > with > > if "factors.dat": > f = open("factors.dat", 'rb') > data = pickle.load(f) > f.close > D = data > else: > f = open("factors.dat", 'wb') You can catch the IOError if the file doesn't exist or pickle.PickleError if it's corrupted, and just assign an empty dict. try: with open("factors.dat", 'rb') as f: D = pickle.load(f) except (IOError, pickle.PickleError): D = {} There's no reason to open the file for writing at this point. You do that later. From eryksun at gmail.com Sun Aug 12 06:51:49 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 12 Aug 2012 00:51:49 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 11:19 PM, Richard D. Moores wrote: > >> To clarify, you can store multiple pickles in a file, but each needs >> its own load. So you'd have to maintain a session dictionary for the >> factors of new integers. Then append the pickled session to the file >> when the user quits. When the program starts you'd have to loop >> through the file to update D with each pickled session. > > Isn't that essentially what my script does? On line 69 your script (http://pastebin.com/SNwKRuSK) appends the current D to the end. So only the last pickle appended would be complete. My first response was for you to switch to 'wb' mode to truncate the file and only save the latest complete session. Then it occurred to me that you actually wanted to grow the pickle the file. I proposed the above solution to append the new factorizations per session. Then at the start load the pickled sessions in a loop, updating D with each loaded dictionary. For example: D = {} session = {} try: with open('factors.dat', 'rb') as f: while True: D.update(pickle.load(f)) except EOFError: pass except (IOError, pickle.PickleError): D = {} if len(D): print("Loaded", len(D), "entries.") while True: n = int(input("integer: ")) if n == 0: print("D has", len(D), "entries.") with open('factors.dat', 'ab') as f: pickle.dump(session, f) break try: factors = D[n] print("the factors of", n, "are", factors) except KeyError: factors = factorsOfInteger(n) print("the factors of", n, "are", factors) D[n] = factors session[n] = factors From alan.gauld at btinternet.com Sun Aug 12 10:00:36 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Aug 2012 09:00:36 +0100 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On 12/08/12 03:43, Richard D. Moores wrote: > =========== > if "factors.dat": This is testing if the string is True, which it always is. I assume you intended something like if os.path.exists('factors.dat'): > f = open("factors.dat", 'rb') > data = pickle.load(f) > f.close > D = data > else: > f = open("factors.dat", 'wb') Not sure there is any point in opening a new file at this point. you are trying to populate data, but if there's no file there is no data so instead of opening the file you want something like data = {} -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From rdmoores at gmail.com Sun Aug 12 10:44:52 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 12 Aug 2012 01:44:52 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sat, Aug 11, 2012 at 9:51 PM, eryksun wrote: > On Sat, Aug 11, 2012 at 11:19 PM, Richard D. Moores wrote: >> >>> To clarify, you can store multiple pickles in a file, but each needs >>> its own load. So you'd have to maintain a session dictionary for the >>> factors of new integers. Then append the pickled session to the file >>> when the user quits. When the program starts you'd have to loop >>> through the file to update D with each pickled session. >> >> Isn't that essentially what my script does? > > On line 69 your script (http://pastebin.com/SNwKRuSK) appends the > current D to the end. So only the last pickle appended would be > complete. My first response was for you to switch to 'wb' mode to > truncate the file and only save the latest complete session. > > Then it occurred to me that you actually wanted to grow the pickle the > file. I proposed the above solution to append the new factorizations > per session. Then at the start load the pickled sessions in a loop, > updating D with each loaded dictionary. For example: OK, thanks for the code, which I will duly study. However, I just pasted my new version, pickle_attempt_for_web2.py at . I've tested it and tested it, and it does exactly what I wanted (thanks to you!). Yes, you're correct, I want to grow the pickle file, the dictionary. The new version puts every new item into it -- nothing gets lost. One caveat is that I have yet to fix the problem with lines 75-78. One thing I'd like to implement is a monitor of the time factorsOfInteger(n) takes to process some of the 18-digit ints (line 153). Most are processed within a second or two, but some can take several minutes. I'd like to limit the time to 15 or 20 seconds, but is there a way to do this? Just a wild guess, but is this where threading would be useful? Dick From rdmoores at gmail.com Sun Aug 12 11:13:32 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 12 Aug 2012 02:13:32 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sun, Aug 12, 2012 at 1:00 AM, Alan Gauld wrote: > On 12/08/12 03:43, Richard D. Moores wrote: > >> =========== >> if "factors.dat": > > This is testing if the string is True, which it always is. > I assume you intended something like > > if os.path.exists('factors.dat'): > > >> f = open("factors.dat", 'rb') >> data = pickle.load(f) >> f.close >> D = data >> else: >> f = open("factors.dat", 'wb') > > > Not sure there is any point in opening a new file at this point. you are > trying to populate data, but if there's no file there is no data so instead > of opening the file you want something like data = {} Great! OK, I now have ========================= D = {} if os.path.exists('factors.dat'): f = open("factors.dat", 'rb') data = pickle.load(f) f.close D = data else: f = open("factors.dat", 'wb') f.close =========================== Which takes care of the case where factors.dat is missing. But what about case where factors.dat is empty? Is there a test for that? When factors.dat exists, but is empty, I get ============================== Traceback (most recent call last): File "C:\P32Working\pickle_attempt_for_web3a.py", line 78, in data = pickle.load(f) EOFError Process terminated with an exit code of 1 ============================== Dick From rdmoores at gmail.com Sun Aug 12 13:44:39 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 12 Aug 2012 04:44:39 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sun, Aug 12, 2012 at 2:13 AM, Richard D. Moores wrote: . > But what > about case where factors.dat is empty? Is there a test for that? I just discovered os.path.getsize('factors.txt') and that factors.txt has a size of 2 bytes when "empty". (I changed the file extension to .txt so I could delete the contents.) So I now have =============================== D = {} if os.path.getsize('factors.txt') == 2: #The file exists, but is empty: leave it alone pass elif os.path.exists('factors.txt'): #The file exists and is not empty: use it f = open("factors.txt", 'rb') data = pickle.load(f) f.close D = data else: # create the file and close it f = open("factors.txt", 'wb') f.close ========================== which seems to work just fine for all cases. Those lines would replace lines 74-78 in Dick From suryak at live.com Sun Aug 12 14:26:39 2012 From: suryak at live.com (Surya K) Date: Sun, 12 Aug 2012 17:56:39 +0530 Subject: [Tutor] How to deploy Python 2.7, Django in OpenShift Message-ID: I am really fed up with tutorials on web telling how to configure Django & Python 2.7 on OpenShift DIY. So, can write a detailed step-by-step procedure on how to setup Django 1.4 with Python 2.7 on OpenShift using VirtualEnv?? Thanks a lot!! -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Sun Aug 12 14:46:46 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 12 Aug 2012 08:46:46 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sun, Aug 12, 2012 at 4:44 AM, Richard D. Moores wrote: > > OK, thanks for the code, which I will duly study. However, I just > pasted my new version, pickle_attempt_for_web2.py at > . I've tested it and tested it, and it > does exactly what I wanted (thanks to you!). Yes, you're correct, I > want to grow the pickle file, the dictionary. The new version puts > every new item into it -- nothing gets lost. I forgot to separate out the PickleError. It shouldn't append new data to a file that gave an error, so rename the existing file. A new file is created later when factors.dat is opened in append mode ('ab'). You asked about handling an empty file. This version (which grows the existing file per session instead of writing a new file) always sees EOFError since it calls pickle.load(f) until it reaches EOF. But the same applies if you only call pickle.load(f) once on an empty file. Just handle the exception with a simple "pass" statement. I narrowed the IOError handling to only deal with ENOENT (file not found). There are other reasons the file could fail to open for reading (e.g. it's a directory or you don't have permission), none of which you probably want to handle automatically, so it just exits with an error. I restructured the main loop to put everything in the try block and handled the ValueError case as well as exiting with an error if the pickle/save fails. import sys, os, errno import pickle from pickle import PickleError D = {} session = {} try: with open("factors.dat", 'rb') as f: while True: D.update(pickle.load(f)) except EOFError: pass #Empty file or finished loading except IOError as e: if e.errno != errno.ENOENT: sys.exit("Can't open factors.dat for reading.") except PickleError: try: #Rename so as not to append to a bad file os.rename('factors.dat', 'factors.err') except OSError: sys.exit("Renaming damaged factors.dat failed.") if len(D): print("Loaded", len(D), "entries.") while True: try: n = int(input("Enter a non-negative integer (0 to quit): ")) if n < 0: raise ValueError if n == 0: print("D has", len(D), "entries") if len(session): with open('factors.dat', 'ab') as f: pickle.dump(session, f) sys.exit(0) factors = D[n] print("the factors of", n, "are", factors) except ValueError: print("e.g. 0, 1, 2, 3") except KeyError: factors = factorsOfInteger(n) print("the factors of", n, "are", factors) D[n] = session[n] = factors except (IOError, PickleError) as e: sys.exit("Error saving data: {0}".format(e.args[-1])) > One thing I'd like to implement is a monitor of the time > factorsOfInteger(n) takes to process some of the 18-digit ints (line > 153). Most are processed within a second or two, but some can take > several minutes. I'd like to limit the time to 15 or 20 seconds, but > is there a way to do this? Just a wild guess, but is this where > threading would be useful? I'd put the time check in the main loop of factorsOfInteger. threading doesn't have an interface to stop a running thread. If you want to use threads, use queues since they're a simple, thread-safe way to communicate between threads. You can modify factorsOfInteger to monitor a queue.Queue and break when it receives a command to halt. Use a 2nd queue to get the result back from the worker thread. Specifically, only call findFactor in factorsOfInteger if qin.empty() is True. Otherwise, qout.put(False) and break. If the function terminates normally, then qout.put(factors). qin = queue.Queue() qout = queue.Queue() t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)).start() try: factors = qout.get(timeout=20) except queue.Empty: qin.put('halt') t.join() #wait for the thread to terminate factors = qout.get() #possibly False if factorsOfInteger quit early if factors: D[n] = session[n] = factors See the docs: http://docs.python.org/py3k/library/threading.html http://docs.python.org/py3k/library/queue.html You could also use a multiprocessing pool to speed things up if you have multiple cores. You'd have to rewrite the factorization code a bit. Partition the search range (i.e. up to the square root) among N cores and run findFactor in parallel. If you have 4 cores, you'll get up to 4 factors. Divide them out, repartition the new range, and repeat. Or something like that. I'm sure you can find a good reference online for parallel factorization. From eryksun at gmail.com Sun Aug 12 15:06:51 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 12 Aug 2012 09:06:51 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sun, Aug 12, 2012 at 7:44 AM, Richard D. Moores wrote: > > I just discovered > os.path.getsize('factors.txt') > and that factors.txt has a size of 2 bytes when "empty". > (I changed the file extension to .txt so I could delete the contents.) No, an empty file has no data; the size is 0. You must have saved a text file in Windows that added a byte order mark (BOM). Windows adds a BOM for UTF-8 and UTF-16 ('Unicode') files. It sounds silly to have a BOM for UTF-8, which can't have a little endian or big endian byte order, but it's there to distinguish UTF-8 from 8-bit ANSI (e.g. Windows 1252). From eryksun at gmail.com Sun Aug 12 15:11:30 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 12 Aug 2012 09:11:30 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: On Sun, Aug 12, 2012 at 8:46 AM, eryksun wrote: > > t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)).start() Sorry I need to proofread better. That should be the following: t = threading.Thread(target=factorsOfInteger, args=(n, qin, qout)) t.start() From breamoreboy at yahoo.co.uk Sun Aug 12 16:40:14 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 12 Aug 2012 15:40:14 +0100 Subject: [Tutor] How to deploy Python 2.7, Django in OpenShift In-Reply-To: References: Message-ID: On 12/08/2012 13:26, Surya K wrote: > I am really fed up with tutorials on web telling how to configure Django & Python 2.7 on OpenShift DIY. > So, can write a detailed step-by-step procedure on how to setup Django 1.4 with Python 2.7 on OpenShift using VirtualEnv?? > Thanks a lot!! > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Thank you for your offer, I'm sure it will be greatly appreciated. When will you be delivering the first draft? -- Cheers. Mark Lawrence. From d at davea.name Sun Aug 12 16:58:08 2012 From: d at davea.name (Dave Angel) Date: Sun, 12 Aug 2012 10:58:08 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: Message-ID: <5027C480.3080705@davea.name> On 08/12/2012 04:44 AM, Richard D. Moores wrote: > > > One caveat is that I have yet to fix the problem with lines 75-78. > > One thing I'd like to implement is a monitor of the time > factorsOfInteger(n) takes to process some of the 18-digit ints (line > 153). Most are processed within a second or two, but some can take > several minutes. I'd like to limit the time to 15 or 20 seconds, but > is there a way to do this? Just a wild guess, but is this where > threading would be useful? > > Now that you're asking about timing, I have to point out that your algorithm isn't very fast. If you could speed it up, perhaps you wouldn't need to limit the time. 1) You use gmpy2,is_prime(). Seems likely that a library with an is_prime function might also have some other functions to aid in factoring. 2) You wind up with a floating point number. If you're getting floats, then you're limited to their precision, maybe 18 digits, and limited to their speed. Perhaps you need to use the // divide rather than the / one. And perhaps it'd help to use divmod instead of using % and / separately. 3) You're doing a % on all the odd numbers above 1, where you really only need to try the primes. If you build an iterator for the primes, you don't need to precalculate them, just cache them for reuse. Again, gmp2 is likely to have something useful. I'm not sure what the point is of preserving the factorizations, especially since you don't store the number you're factoring for each case. Seems to me it'd be more useful to save a list of primes. But you've got your reasons i'm sure. -- DaveA From alan.gauld at btinternet.com Sun Aug 12 18:44:24 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Aug 2012 17:44:24 +0100 Subject: [Tutor] How to deploy Python 2.7, Django in OpenShift In-Reply-To: References: Message-ID: On 12/08/12 13:26, Surya K wrote: > I am really fed up with tutorials on web telling how to configure Django > & Python 2.7 on OpenShift DIY. I assume you intended to send this to a Django programming forum? This is the python tutor list for people learning the Python language. I suggest you repost in an appropriate place. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Sun Aug 12 19:49:19 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 12 Aug 2012 13:49:19 -0400 Subject: [Tutor] pickle problems In-Reply-To: <5027C480.3080705@davea.name> References: <5027C480.3080705@davea.name> Message-ID: On Sun, Aug 12, 2012 at 10:58 AM, Dave Angel wrote: > > 2) You wind up with a floating point number. If you're getting floats, > then you're limited to their precision, maybe 18 digits, and limited to > their speed. Perhaps you need to use the // divide rather than the / > one. And perhaps it'd help to use divmod instead of using % and / > separately. Good catch in Kent Johnson's code. Maybe he'll search for his name and find this. It should be `r = r // factor`. > 3) You're doing a % on all the odd numbers above 1, where you really > only need to try the primes. If you build an iterator for the primes, > you don't need to precalculate them, just cache them for reuse. Again, > gmp2 is likely to have something useful. @Richard, if you haven't learned generators, see the docs for yield expressions: http://docs.python.org/py3k/reference/expressions.html#yield-expressions http://www.python.org/dev/peps/pep-0255 First yield the cached primes. Then compute the next prime, cache it, and yield it. Use the cache to speed up finding the next prime. You can pickle the cache of primes instead of the factorizations. > I'm not sure what the point is of preserving the factorizations, > especially since you don't store the number you're factoring for each > case. The numbers are the dictionary keys. From __peter__ at web.de Sun Aug 12 22:58:45 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 12 Aug 2012 22:58:45 +0200 Subject: [Tutor] pickle problems References: Message-ID: Richard D. Moores wrote: > f = open("factors.txt", 'rb') > data = pickle.load(f) > f.close f.close looks up the close method but doesn't invoke it; you need f.close(). Alternatively use a with statement: with open("factors.txt", "rb") as f: data = pickle.load(f) This will close the file even if an exception occurs when trying to unpickle the data. From rdmoores at gmail.com Mon Aug 13 01:08:18 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Sun, 12 Aug 2012 16:08:18 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: <5027C480.3080705@davea.name> Message-ID: On Sun, Aug 12, 2012 at 10:49 AM, eryksun wrote: > On Sun, Aug 12, 2012 at 10:58 AM, Dave Angel wrote: > Good catch in Kent Johnson's code. Maybe he'll search for his name and > find this. It should be `r = r // factor`. The fault was probably mine. Kent wrote this in 2004, in Python 2.x. When I moved to 3.x I converted the function to 3.x, but missed that one. See Kent's Tutor post archived at I've had an amazing amount of help today. I haven't had time to attend to all of it yet, but thought I'd get this out. Since posting last I've found that my script () that I thought was perfect (except for lines 74-78), isn't. Obvious errors in the dictionary/pickle, such as even prime factors! I'm hoping that correcting Kent's function will stop the sort of errors I've found. And I've found an obvious test: The product of an integers prime factors must equal the integer itself. And of course the factors must be tested for primacy(?)/prime numberhood(?), which is quickly done with gmpy2's is_prime(). I just now checked one of the erroneous items that had an even prime factor: n = 279918016070854658 a random 18-digit integer the factors of 279,918,016,070,854,658 are [2, 7, 41, 487662048903928] After correcting Kent's function, I correctly get > 279918016070854658 the factors of 279,918,016,070,854,658 are [2, 7, 3011, 6640366657277] >>>487662048903928*2*7*41 279918016070854672 >>>2*7*3011*6640366657277 279918016070854658 >>>is_prime(3011) True >>>is_prime(6640366657277) True I also just pasted the 3rd version of my script, pickle_attempt_for_web3.py, at . Dick From elainahyde at gmail.com Mon Aug 13 09:24:16 2012 From: elainahyde at gmail.com (Elaina Ann Hyde) Date: Mon, 13 Aug 2012 17:24:16 +1000 Subject: [Tutor] Matching 3 tables with almost identical columns Message-ID: Greetings, I have a rather monolithic database I am re-building at the moment, and I want to join 3 tables with semi-matching content, I have several sets of these but they all come in groups of three. Here is the situation: --note all tables are in ascii format, space deliminated, with readable #Header format--- T1_01= Table 1 = 1 + 'stuff1' +additional content (only on some lines) 2 "" 3 +'stuff1'+ a bunch of empty-ness ....400 T1_02= Table 2 = 1 + "different stuff" 2 "" 3 "" ... 400 T1_03 = Table 3 = 5 cols yet other stuff + 001 + stuff 5 cols yet other stuff + 003 "" 5 cols yet other stuff + 007 "" ... 5 cols yet other stuff + 400 some rows are skipped, varies which ones what I want, for each 'group' I have 3 tables, since these are grouped in a handy fashion, ie T1_01, T1_02, T1_03 would be table 1,2,3 for group 1, and again for T2_01,T2_02,T2_03. I need to do this about 60 times in total and the table output I am hoping for is: T1_0123= 1 + 'stuff1' + additional content 1 1 + "different stuff" + 5 cols yet other stuff + 001 + additional content 3 2 + 'stuff1' + additional content 1 2 + "different stuff" + "something to fill in the empty spaces, like a set of -99.9 values" 3 + 'stuff1' + "something to fill in empty spaces as no additional content available for this line" 1 3 + "different stuff" + additional content 2 5 cols yet other stuff + 003 + additional content 3 ... 400 "" now I was hoping to run all 3 tables in a go then do something like a batch job on all the sets. just a note: since all my tables are ascii tables with headers, for my first 'group' I have # T1_1A T1_1B.... # T1_2A T1_2B... # T1_3A T1_3B... ------------------------ x1=open(sys.argv[1]) dat1=asciitable.read(x1,Reader=asciitable.CommentedHeader, fill_values=[('','-99.9'),('...','-99.9')]) ------------------------ The first big problem is: I cannot read file T1_01 with asciitable, I thought that fill_values would take care of the empty space but I get the following error ------------------------------------------------ Traceback (most recent call last): File "RunAElist.py", line 25, in dat1=asciitable.read(x1,Reader=asciitable.CommentedHeader, fill_values=[('','-99.9'),('...','-99.9')]) File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/asciitable-0.8.0-py2.7.egg/asciitable/ui.py", line 131, in read dat = _guess(table, new_kwargs) File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/asciitable-0.8.0-py2.7.egg/asciitable/ui.py", line 205, in _guess raise core.InconsistentTableError('\n'.join(lines)) asciitable.core.InconsistentTableError: ERROR: Unable to guess table for with the guesses listed below: Reader:CommentedHeader fill_values: [('', '-99.9'), ('...', '-99.9')] Reader:CommentedHeader delimiter: '|' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: '"' Reader:CommentedHeader delimiter: '|' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: "'" Reader:CommentedHeader delimiter: ',' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: '"' Reader:CommentedHeader delimiter: ',' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: "'" Reader:CommentedHeader delimiter: ' ' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: '"' Reader:CommentedHeader delimiter: ' ' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: "'" Reader:CommentedHeader delimiter: '\\s' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: '"' Reader:CommentedHeader delimiter: '\\s' fill_values: [('', '-99.9'), ('...', '-99.9')] quotechar: "'" Reader:CommentedHeader fill_values: [('', '-99.9'), ('...', '-99.9')] ERROR: Unable to guess table for with the guesses listed above. Check the table and try with guess=False and appropriate arguments to read() --------------------------------- However, I CAN read file T1_02 and T1_03, if I do this and ignore T01, I can make a joined file, but the problem comes in writing the empty spaces as 'something'. There will be several T1_02 with no match to T1_03, and this is still without being able to read T1_01. In theory if I could read T1_01, since T1_01 and T1_02 will be the same length, I just declare 'fiber' to be the matching variable 1,2,3...400 as shown in the table above and what I am thinking is a variation on: fopen=open('Megalith1.list','w') for i in xrange(len(varT1_02)): if fiber1[i] == fiber2[i]: for j in xrange(len(varT1_03)): if fiber2[i] == fiber3[j]: fopen.write(" ".join([str(k) for k in fiber1])+" "+" ".join([str(k) for k in fiber2])+" "+" ".join([str(k) for k in fiber3])+"\n") else: fopen.write(" ".join([str(k) for k in fiber1])+" "+" ".join([str(k) for k in fiber2])+"\n") However, I don't resolve the problem of the empty spaces, and I still can't find a good way to read my T1_01... and I'm additionally not sure how to make a batch job out of all this table goodness. Any suggestions are welcome. Thanks in advance! ~Elaina -- PhD Candidate Department of Physics and Astronomy Faculty of Science Macquarie University North Ryde, NSW 2109, Australia -------------- next part -------------- An HTML attachment was scrubbed... URL: From zaatlob at hotmail.com Mon Aug 13 17:01:52 2012 From: zaatlob at hotmail.com (leon zaat) Date: Mon, 13 Aug 2012 15:01:52 +0000 Subject: [Tutor] output not in ANSI Message-ID: I wrote a program for creating a csv file. I am using pyton on windows. The output i get is not in ANSI. Is there a way i can get the output in ansi, I imported locale and tried wb and w as type of writing. ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'w(b)') -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Aug 13 19:58:58 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Aug 2012 18:58:58 +0100 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: On 13/08/12 16:01, leon zaat wrote: > I wrote a program for creating a csv file. > I am using pyton on windows. > The output i get is not in ANSI. Can you clarify what you mean by ANSI? According to Wikipedia: -------------------- The Institute administers five standards panels: The ANSI Biofuels Standards Panel (ANSI-BSP) The Healthcare Information Technology Standards Panel The ANSI Homeland Security Standards Panel The ANSI Nanotechnology Standards Panel The Identity Theft Prevention and Identity Management Standards Panel Each of the panels works to identify, coordinate, and harmonize voluntary standards relevant to these areas. ------------------- That covers a pretty wide field of interest, which one are you using and which specific standard are you working to? > Is there a way i can get the output in ansi, Python doesn't know anything about ANSI standards, you will need to do that yourself. Python can format the data pretty much as you want it. > I imported locale and tried wb and w as type of writing. > ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'w(b)') Locale will affect the display of the data in terms of fonts and such. Is that what you are trying to control? Slightly confused. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From joel.goldstick at gmail.com Mon Aug 13 20:04:36 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 13 Aug 2012 14:04:36 -0400 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: On Mon, Aug 13, 2012 at 1:58 PM, Alan Gauld wrote: > On 13/08/12 16:01, leon zaat wrote: >> >> I wrote a program for creating a csv file. >> I am using pyton on windows. >> The output i get is not in ANSI. > > > Can you clarify what you mean by ANSI? According to Wikipedia: > I believe in this context the OP means ASCII. ASCII became an ANSI recognized standard many years ago > >> I imported locale and tried wb and w as type of writing. >> ofile=open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'w(b)') > > > Locale will affect the display of the data in terms of fonts and such. > Is that what you are trying to control? > > Slightly confused. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick From malaclypse2 at gmail.com Mon Aug 13 20:09:24 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Mon, 13 Aug 2012 14:09:24 -0400 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: On Mon, Aug 13, 2012 at 11:01 AM, leon zaat wrote: > I wrote a program for creating a csv file. > I am using pyton on windows. > The output i get is not in ANSI. I'm not familiar with an encoding that would be called ANSI, or any ANSI specification for CSV files. What, exactly, are you looking for? In particular it would be helpful if you provided a few things: 1) Your code, especially a version we can actually run for ourselves. 2) A description of what you wanted to have happen, and 3) A description of how the output that you got from the code provided is different for what you expected. If you provide those things, I am sure people on the list will be able to help you. -- Jerry From eryksun at gmail.com Mon Aug 13 21:12:04 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 13 Aug 2012 15:12:04 -0400 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: On Mon, Aug 13, 2012 at 2:04 PM, Joel Goldstick wrote: > > I believe in this context the OP means ASCII. ASCII became an ANSI > recognized standard many years ago In Windows, ANSI refers to the locale-dependent 8-bit codepage. But there is no ANSI standard for Windows1252. It's a common misnomer in the OS dialog boxes and controls. Another MS misnomer is labeling UTF-16 as 'Unicode'. @leon zaat Process your text with Unicode. Open the file using codecs.open set to your platform's preferred encoding, e.g. 'cp1252' for Western, 'cp1251' for Cyrilic, or locale.getpreferredencoding() in general. From matt.gregory at oregonstate.edu Mon Aug 13 21:13:27 2012 From: matt.gregory at oregonstate.edu (Gregory, Matthew) Date: Mon, 13 Aug 2012 12:13:27 -0700 Subject: [Tutor] overriding instance attributes with keywords Message-ID: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> Hi all, I'm trying to create a new instance from an existing instance with attributes of the new instance allowed to be overwritten by keyword parameters. I'm guessing I'm not doing this in the most efficient manner. class Spam: def __init__(self, a, b): self.a = a self.b = b def __repr__(self): return "%s: (%d,%d)" % (self.__class__.__name__, self.a, self.b) def new_with_overrides(s1, **kwargs): new_params = {'a': s1.a, 'b': s1.b} for (k, v) in kwargs.iteritems(): if k in new_params: new_params[k] = v return Spam(new_params['a'], new_params['b']) s1 = Spam(4, 10) s2 = new_with_overrides(s1) s3 = new_with_overrides(s1, a=3) s4 = new_with_overrides(s1, b=7) This works but it doesn't seem very extendable if new attributes are added to Spam. I know that I can make new_with_overrides a method of Spam and that may simplify things a bit (e.g. using __dict__). thanks, matt From eryksun at gmail.com Mon Aug 13 23:16:13 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 13 Aug 2012 17:16:13 -0400 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> Message-ID: On Mon, Aug 13, 2012 at 3:13 PM, Gregory, Matthew wrote: > > I'm trying to create a new instance from an existing instance > def new_with_overrides(s1, **kwargs): > new_params = {'a': s1.a, 'b': s1.b} > for (k, v) in kwargs.iteritems(): > if k in new_params: > new_params[k] = v > return Spam(new_params['a'], new_params['b']) > This works but it doesn't seem very extendable if new attributes are added to Spam. In general instance attributes won't map to __init__ arguments. You can create a new instance by calling __new__. Then update from the old dict and add in override attributes. def new_with_overrides(obj1, **kwds): obj2 = obj1.__new__(obj1.__class__) obj2.__dict__.update(obj1.__dict__) for k, v in kwds.items(): if k in obj2.__dict__: obj2.__dict__[k] = v return obj2 From wprins at gmail.com Mon Aug 13 23:49:48 2012 From: wprins at gmail.com (Walter Prins) Date: Mon, 13 Aug 2012 22:49:48 +0100 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: On 13 August 2012 20:12, eryksun wrote: > In Windows, ANSI refers to the locale-dependent 8-bit codepage. But > there is no ANSI standard for Windows1252. It's a common misnomer in > the OS dialog boxes and controls. Yes. In case it adds anything to the discussion, here's a page that documents the entire ANSI (Windows-1252, extended ASCII or whatever) set: http://www.alanwood.net/demos/ansi.html Walter From alan.gauld at btinternet.com Tue Aug 14 02:12:13 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Aug 2012 01:12:13 +0100 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: On 13/08/12 18:58, Alan Gauld wrote: > Locale will affect the display of the data in terms of fonts and such. > Is that what you are trying to control? For font read character set, oops! :-( -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Tue Aug 14 04:04:24 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 14 Aug 2012 12:04:24 +1000 Subject: [Tutor] output not in ANSI In-Reply-To: References: Message-ID: <5029B228.20203@pearwood.info> On 14/08/12 04:04, Joel Goldstick wrote: > On Mon, Aug 13, 2012 at 1:58 PM, Alan Gauld wrote: >> On 13/08/12 16:01, leon zaat wrote: >>> >>> I wrote a program for creating a csv file. >>> I am using pyton on windows. >>> The output i get is not in ANSI. >> >> >> Can you clarify what you mean by ANSI? According to Wikipedia: >> > I believe in this context the OP means ASCII. ASCII became an ANSI > recognized standard many years ago I think Leon may be referring to one of the Windows standard character encodings, but which one, I have no idea. Maybe someone else can guess? Leon, if you want us to help you, you have to ask a better question that we can understand. Before you do anything else, please read these two pages: http://www.joelonsoftware.com/articles/Unicode.html http://sscce.org/ then come back with any questions! -- Steven From steve at pearwood.info Tue Aug 14 04:28:44 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 14 Aug 2012 12:28:44 +1000 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> Message-ID: <5029B7DC.90005@pearwood.info> On 14/08/12 07:16, eryksun wrote: > On Mon, Aug 13, 2012 at 3:13 PM, Gregory, Matthew > wrote: >> >> I'm trying to create a new instance from an existing instance > >> def new_with_overrides(s1, **kwargs): >> new_params = {'a': s1.a, 'b': s1.b} >> for (k, v) in kwargs.iteritems(): >> if k in new_params: >> new_params[k] = v >> return Spam(new_params['a'], new_params['b']) > >> This works but it doesn't seem very extendable if new attributes are added to Spam. > > In general instance attributes won't map to __init__ arguments. You > can create a new instance by calling __new__. Then update from the > old dict and add in override attributes. > > def new_with_overrides(obj1, **kwds): > obj2 = obj1.__new__(obj1.__class__) > obj2.__dict__.update(obj1.__dict__) > for k, v in kwds.items(): > if k in obj2.__dict__: > obj2.__dict__[k] = v > return obj2 In general, this can fail too. The instance may have __slots__, it may not have a __dict__ at all, it may be using properties or __getattr__ etc. to implement computed attributes. All sorts of things can go wrong when copying arbitrary instances. That's why there is an entire protocol so that types can make themselves copyable. Matt, if you're trying to make a "copy any instance" utility function, you are re-inventing the wheel. See the copy module instead. If you're looking to make an short-cut for your own class, my recommendation is that you give your class two methods: # Your class MUST inherit from object or some other builtin for this to work class MyClass(object): def to_mapping(self): """Return a dict of whatever arguments created the instance.""" return {"a": self.a, ...} @classmethod def from_mapping(cls, adict, **kwargs): d = dict(adict, **kwargs) return cls(**d) Typical usage: x = MyClass(a=1, b=2, c=3) y = x.from_mapping(x.to_mapping(), a=999) -- Steven From eryksun at gmail.com Tue Aug 14 12:01:52 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 14 Aug 2012 06:01:52 -0400 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: <5029B7DC.90005@pearwood.info> References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> <5029B7DC.90005@pearwood.info> Message-ID: On Mon, Aug 13, 2012 at 10:28 PM, Steven D'Aprano wrote: > >> def new_with_overrides(obj1, **kwds): >> obj2 = obj1.__new__(obj1.__class__) >> obj2.__dict__.update(obj1.__dict__) >> for k, v in kwds.items(): >> if k in obj2.__dict__: >> obj2.__dict__[k] = v >> return obj2 > > In general, this can fail too. The instance may have __slots__, it > may not have a __dict__ at all, it may be using properties or > __getattr__ etc. to implement computed attributes. All sorts of things > can go wrong when copying arbitrary instances. That's why there is an > entire protocol so that types can make themselves copyable. > > Matt, if you're trying to make a "copy any instance" utility function, you > are re-inventing the wheel. See the copy module instead. Right, I overlooked classes with __slots__ and that override __new__ and other special methods. copy() is the better and more customizable solution. This is the same route for customizing the pickle of an object, so the pickle docs should help: http://docs.python.org/library/pickle.html#pickling-and-unpickling-normal-class-instances The default __reduce__(2) will call __getnewargs__ and __getstate__ to create an info tuple used to construct a copy. The args from __getnewargs__ are used to call a __newobj__ function that in turn calls cls.__new__(cls, *args) to create a new instance. The copy reconstructor will update the new instance with the state from __getstate__ if it's non-false. If the state is a tuple, the 2nd item should be the slots state. The reconstructor uses __setstate__ if it exists. Otherwise it updates the instance dict with state[0] if it's non-false, and uses setattr to set slots if state[1] is non-false. class Test(object): def __new__(cls, v): print '__new__', v obj = object.__new__(cls) obj.new_attr = v return obj def __getnewargs__(self): print '__getnewargs__' return (self.new_attr, ) def __getstate__(self): print '__getstate__' return ({'attr': 'value'}, {'slot_attr': 'value'}) def __setstate__(self, state): print '__setstate__', state >>> t1 = Test('test') __new__ test >>> t2 = copy.copy(t1) __getnewargs__ __getstate__ __new__ test __setstate__ ({'attr': 'value'}, {'slot_attr': 'value'}) From eryksun at gmail.com Tue Aug 14 14:28:41 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 14 Aug 2012 08:28:41 -0400 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> <5029B7DC.90005@pearwood.info> Message-ID: On Tue, Aug 14, 2012 at 6:01 AM, eryksun wrote: > > Right, I overlooked classes with __slots__ and that override __new__ > and other special methods. copy() is the better and more customizable > solution. If copying is good enough, then this should work: from copy import copy def copy_with_overrides(obj1, **kwds): obj2 = copy(obj1) for k, v in kwds.items(): if hasattr(obj2, k): setattr(obj2, k, v) return obj2 However, I apologize for sidetracking you if you need __init__ to run (possibly creating new instances of attributes instead of getting copied references). That really should be a classmethod as Steve suggests (or simple a regular method and use cls = self.__class__), and you'll need to keep it in sync with any changes you make to __init__. A generic function can't know how to call the constructor. Even if you use inspect.getfullargspec(), there's no guarantee every argument will have a corresponding object attribute with the same name or that the current value is the one __init__ needs -- not unless you design it that way. From zaatlob at hotmail.com Tue Aug 14 14:59:30 2012 From: zaatlob at hotmail.com (leon zaat) Date: Tue, 14 Aug 2012 12:59:30 +0000 Subject: [Tutor] output not in ANSI, conversing char set to locale.getpreferredencoding() In-Reply-To: References: , , , Message-ID: > From: eryksun at gmail.com > Date: Mon, 13 Aug 2012 15:12:04 -0400 > To: joel.goldstick at gmail.com > CC: alan.gauld at btinternet.com; tutor at python.org > Subject: Re: [Tutor] output not in ANSI > > On Mon, Aug 13, 2012 at 2:04 PM, Joel Goldstick > wrote: > > > > I believe in this context the OP means ASCII. ASCII became an ANSI > > recognized standard many years ago > > In Windows, ANSI refers to the locale-dependent 8-bit codepage. But > there is no ANSI standard for Windows1252. It's a common misnomer in > the OS dialog boxes and controls. Another MS misnomer is labeling > UTF-16 as 'Unicode'. > > @leon zaat > > Process your text with Unicode. Open the file using codecs.open set to > your platform's preferred encoding, e.g. 'cp1252' for Western, > 'cp1251' for Cyrilic, or locale.getpreferredencoding() in general. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor I tried changing my code. I now have this piece of code: import csv import codecs import locale # Globale variabele bagObjecten = [] chartype=locale.getpreferredencoding() #------------------------------------------------------------------------------ # BAGExtractPlus toont het hoofdscherm van de BAG Extract+ tool #------------------------------------------------------------------------------ class BAGExtractPlus(wx.Frame): #------------------------------------------------------------------------------ # schrijven van de records #------------------------------------------------------------------------------ def schrijfExportRecord(self, verblijfhoofd,identificatie): sql1=""; sql1="Select openbareruimtenaam, woonplaatsnaam from nummeraanduiding where identificatie = '" + identificatie "'" num= database.select(sql1); for row in num: openbareruimtenaam1=row[0] openbareruimtenaam=u'' + (openbareruimtenaam1.encode(chartype)) woonplaatsnaam1=(row[0]); woonplaatsnaam=u'' + (woonplaatsnaam1.encode(chartype)) newrow=[openbareruimtenaam, woonplaatsnaam]; verblijfhoofd.writerow(newrow); #-------------------------------------------------------------------------------------- # Exporteer benodigde gegevens #-------------------------------------------------------------------------------------- def ExportBestanden(self, event): ofile=codecs.open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb', chartype) verblijfhoofd = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) counterVBO=2; identificatie='0014010011066771'; while 1 < counterVBO: hulpIdentificatie= identificatie; sql="Select identificatie, hoofdadres, verblijfsobjectgeometrie from verblijfsobject where "; sql= sql + "identificatie > '" + hulpIdentificatie ; vbo= database.select(sql); if not vbo: break; else: for row in vbo: identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(verblijfhoofd, identificatie) I highlighted in red the lines i think that are important. When i try to convert openbareruimtenaam from the data below: "P.J. No?l Bakerstraat";"Groningen" I get the error: UnicodeDecodeError: 'ascii' codecs can't decode byte 0xc3 in position 7: ordinal not in range(128) for the openbareruimtenaam=u'' + (openbareruimtenaam1.encode(chartype)) line. I know that the default system codecs is ascii and chartype=b'cp1252' But how can i get the by pass the ascii encoding? -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Aug 14 16:03:46 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Aug 2012 16:03:46 +0200 Subject: [Tutor] output not in ANSI, conversing char set to locale.getpreferredencoding() References: Message-ID: leon zaat wrote: > I get the error: > UnicodeDecodeError: 'ascii' codecs can't decode byte 0xc3 in position 7: > ordinal not in range(128) for the openbareruimtenaam=u'' + > (openbareruimtenaam1.encode(chartype)) line. The error message means that database.select() returns a byte string. bytestring.encode(encoding) implicitly attempts bytestring.decode("ascii").encode(encoding) and will fail for non-ascii bytestrings no matter what encoding you pass to the encode() method. > I know that the default system codecs is ascii and chartype=b'cp1252' > But how can i get the by pass the ascii encoding? You have to find out the database encoding -- then you can change the failing line to database_encoding = ... # you need to find out yourself, but many use the # UTF-8 -- IMO the only sensible choice these days file_encoding = "cp1252" openbareruimtenaam = openbareruimtenaam1.decode( database_encoding).encode(file_encoding) As you now have a bytestring again you can forget about codecs.open() which won't work anyway as the csv module doesn't support unicode properly in Python 2.x (The csv documentation has the details). PS: the u"..." prefix is a way to write unicode constants in Python sourcecode, you cannot create unicode a variable by tucking it in front of a string. u"" + bytestring will trigger a decode u"" + bytestring.decode("ascii") and is thus an obcure way to spell bytestring.decode("ascii") From eryksun at gmail.com Tue Aug 14 16:42:13 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 14 Aug 2012 10:42:13 -0400 Subject: [Tutor] output not in ANSI, conversing char set to locale.getpreferredencoding() In-Reply-To: References: Message-ID: On Tue, Aug 14, 2012 at 10:03 AM, Peter Otten <__peter__ at web.de> wrote: > > You have to find out the database encoding The character at 7 is '?', which is '\xc3\xab' in UTF-8. So that's likely the db encoding. > As you now have a bytestring again you can forget about codecs.open() which > won't work anyway as the csv module doesn't support unicode properly in > Python 2.x (The csv documentation has the details). Sorry for suggesting codecs.open earlier. I looked at the source for the _csv module, and sure enough it calls PyObject_Str(field). Thankfully csv in 3.x has a new API. From wprins at gmail.com Tue Aug 14 16:46:15 2012 From: wprins at gmail.com (Walter Prins) Date: Tue, 14 Aug 2012 15:46:15 +0100 Subject: [Tutor] output not in ANSI, conversing char set to locale.getpreferredencoding() In-Reply-To: References: Message-ID: Hi Leon, Since Unicode's been pulled into the discussion, I'd like to suggest you watch and read the following presentation "Pragmatic Unicode ~or~ How do I stop the pain?" from Pycon US 2012: http://nedbatchelder.com/text/unipain.html Walter From steve at pearwood.info Tue Aug 14 18:53:18 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 15 Aug 2012 02:53:18 +1000 Subject: [Tutor] Matching 3 tables with almost identical columns In-Reply-To: References: Message-ID: <502A827E.30701@pearwood.info> Hi Elaina, My comments interleaved with yours. If you read nothing else, scroll down and read my last paragraph. Good luck! On 13/08/12 17:24, Elaina Ann Hyde wrote: > Greetings, > I have a rather monolithic database I am re-building at the moment, and > I want to join 3 tables with semi-matching content, I have several sets of > these but they all come in groups of three. Here is the situation: > > --note all tables are in ascii format, space deliminated, with readable > #Header format--- Does this mean that the data is in a great big flat text file? > T1_01= Table 1 = > 1 + 'stuff1' +additional content (only on some lines) > 2 "" > 3 +'stuff1'+ a bunch of empty-ness > ....400 So this is *one* table, correct? And you want to grab three tables at a time? I don't follow which parts are part of the table format and which are your comments to us. This is my *guess* as to the format: Each table starts with a single header line, e.g.: T1_01= Table 1 = followed by a maximum of 400 records, numbered from 1 to 400. Each record is a single line. Empty records look like this: 23 "" (that is, the record number followed by an empty double-quoted string). Non-empty records look like this: 42 + 'content' (that is, the record number followed by a plus-sign followed by a single-quoted string with the data). Some non-empty records have free form text after the field: 42 + 'content' +free form text goes here or "a bunch of empty-ness" (whatever that means???) 42 + 'content' + [what goes here???] Am I correct? What do you mean by "a bunch of empty-ness"? > T1_03 = Table 3 = > 5 cols yet other stuff + 001 + stuff > 5 cols yet other stuff + 003 "" > 5 cols yet other stuff + 007 "" > ... > 5 cols yet other stuff + 400 some rows are skipped, varies which ones Okay, now I'm completely lost. I can't begin to decipher that. Why does every record start with the same record number "5"? What are the 001, 003, etc. parts? > what I want, for each 'group' I have 3 tables, since these are grouped in a > handy fashion, ie T1_01, T1_02, T1_03 would be table 1,2,3 for group 1, and > again for T2_01,T2_02,T2_03. I need to do this about 60 times in total and > the table output I am hoping for is: > > > T1_0123= > > 1 + 'stuff1' + additional content 1 1 + "different stuff" + 5 cols yet > other stuff + 001 + additional content 3 > 2 + 'stuff1' + additional content 1 2 + "different stuff" + "something to > fill in the empty spaces, like a set of -99.9 values" > 3 + 'stuff1' + "something to fill in empty spaces as no additional content > available for this line" 1 3 + "different stuff" + additional content 2 5 > cols yet other stuff + 003 + additional content 3 > ... > 400 "" Can you simplify your explanation? As it stands, it is just a big ball of mud. Please give a SHORT, SIMPLE example of the data you have to work with, using SIMPLE and DISTINCT values as placeholders. E.g.: T1_01= Table 1 = 1 + 'a' 2 + 'b' + foo 3 + 'c' + bar T1_02= Table 2 = 1 + 'A' 2 + 'B' + baz 3 + 'C' T1_03= Table 3 = 1 + 'x' 2 + 'y' 3 + 'z' And the merged table: T1_0123= Table 123 = 1 + 'a' 'A' 'x' 2 + 'b' + foo 'B' + baz 'y' 3 + 'c' + bar 'C' 'z' Am I close? What bits am I missing? > now I was hoping to run all 3 tables in a go then do something like a batch > job on all the sets. > just a note: > since all my tables are ascii tables with headers, for my first 'group' I > have > # T1_1A T1_1B.... > # T1_2A T1_2B... > # T1_3A T1_3B... And I don't understand this either. Are the hash marks part of the file you are working with? > ------------------------ > x1=open(sys.argv[1]) > dat1=asciitable.read(x1,Reader=asciitable.CommentedHeader, > fill_values=[('','-99.9'),('...','-99.9')]) > ------------------------ What is asciitable? If that is not a standard Python library, it is very likely that nobody here will have used it before. You might have to contact the author of the library, or ask on a forum specially for asciitable (if there is one). If all else fails, you could try the mailing list python-list at python.org (also available as a newsgroup, comp.lang.python) and see if anyone there has used asciitable. -- Steven From yam.matt at gmail.com Tue Aug 14 20:24:57 2012 From: yam.matt at gmail.com (Mazhar Hussain) Date: Tue, 14 Aug 2012 23:24:57 +0500 Subject: [Tutor] Confusion regarding the 'from' statement Message-ID: Im new to python and theres something thats bothering me for quite a time. I read in 'Learning Python' by Mark Lutz that when we use a 'from' statement to import a name present in a module, it first imports the module then assigns a new name to it(i.e. the name of the function,class, etc present in the imported module) and then deletes the module object with the del statement. However what happens if I try to import a name using 'from' that references a name in the imported module that itself is not imported. Consider the following example,here there are two modules mod1.py and mod2.py, #mod1.py from mod2 import test test('mod1.py') #mod2.py def countLines(name): print len(open(name).readlines()) def countChars(name): print len(open(name).read()) def test(name): print 'loading...' countLines(name) countChars(name) print '-'*10 Now see what happens when I run or import mod1 >>>import mod1 loading... 3 44 ---------- Here when I imported and ran the 'test' function, it ran successfully although I didn't even import countChars or countLines, and the 'from' statement had already deleted the mod2 module object. SO I basically need to know why does this code work although considering the problems I mentioned it shouldn't. From matt.gregory at oregonstate.edu Tue Aug 14 20:25:31 2012 From: matt.gregory at oregonstate.edu (Matt Gregory) Date: Tue, 14 Aug 2012 11:25:31 -0700 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> <5029B7DC.90005@pearwood.info> Message-ID: On 8/14/2012 5:28 AM, eryksun wrote: > On Tue, Aug 14, 2012 at 6:01 AM, eryksun wrote: >> >> Right, I overlooked classes with __slots__ and that override __new__ >> and other special methods. copy() is the better and more customizable >> solution. > > If copying is good enough, then this should work: > > from copy import copy > > def copy_with_overrides(obj1, **kwds): > obj2 = copy(obj1) > for k, v in kwds.items(): > if hasattr(obj2, k): > setattr(obj2, k, v) > return obj2 Thanks to you both for really helpful advice. A quick follow-up question - would you want to create a deepcopy of obj1 in the above example if your obj1 contained other objects? I think that Steven's class method gets around this? thanks, matt From breamoreboy at yahoo.co.uk Tue Aug 14 20:32:48 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 14 Aug 2012 19:32:48 +0100 Subject: [Tutor] Matching 3 tables with almost identical columns In-Reply-To: <502A827E.30701@pearwood.info> References: <502A827E.30701@pearwood.info> Message-ID: On 14/08/2012 17:53, Steven D'Aprano wrote: > What is asciitable? Just for the record http://pypi.python.org/pypi/asciitable/0.8.0 -- Cheers. Mark Lawrence. From david at graniteweb.com Tue Aug 14 20:43:59 2012 From: david at graniteweb.com (David Rock) Date: Tue, 14 Aug 2012 13:43:59 -0500 Subject: [Tutor] Confusion regarding the 'from' statement In-Reply-To: References: Message-ID: <20120814184359.GA19597@wdfs.gateway.2wire.net> * Mazhar Hussain [2012-08-14 23:24]: > the module object with the del statement. However what happens if I > try to import a name using 'from' that references a name in the > imported module that itself is not imported. Consider the following > example,here there are two modules mod1.py and mod2.py, > > #mod1.py > from mod2 import test > test('mod1.py') > > #mod2.py > def countLines(name): > print len(open(name).readlines()) > > def countChars(name): > print len(open(name).read()) > > def test(name): > print 'loading...' > countLines(name) > countChars(name) > print '-'*10 Loosely speaking, it does import the other methods. In order to successfully import test(), it has to resolve countLines() and countChars(). They won't be directly accessible, as they don't exist in the namespace, but test() knows about them. You are reading too much into the comment that the module is "deleted". -- David Rock david at graniteweb.com From eryksun at gmail.com Tue Aug 14 20:55:08 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 14 Aug 2012 14:55:08 -0400 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> <5029B7DC.90005@pearwood.info> Message-ID: On Tue, Aug 14, 2012 at 2:25 PM, Matt Gregory wrote: > > Thanks to you both for really helpful advice. A quick follow-up question - > would you want to create a deepcopy of obj1 in the above example if your > obj1 contained other objects? I think that Steven's class method gets > around this? Chances are you either want a shallow copy or to use a special constructor as Steven wrote. For example, consider an object with a file attribute based on a filename argument. Say for example it's for an MP3 file and the object reads in the ID3 tags in __init__. If you want a new instance based on the same arguments, except for a different MP3 file, then you don't want a copy or a deepcopy. From joel.goldstick at gmail.com Tue Aug 14 21:02:48 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 14 Aug 2012 15:02:48 -0400 Subject: [Tutor] Confusion regarding the 'from' statement In-Reply-To: <20120814184359.GA19597@wdfs.gateway.2wire.net> References: <20120814184359.GA19597@wdfs.gateway.2wire.net> Message-ID: On Tue, Aug 14, 2012 at 2:43 PM, David Rock wrote: > * Mazhar Hussain [2012-08-14 23:24]: >> the module object with the del statement. However what happens if I >> try to import a name using 'from' that references a name in the >> imported module that itself is not imported. Consider the following >> example,here there are two modules mod1.py and mod2.py, >> >> #mod1.py >> from mod2 import test It is probably better to use: import mod2 and then use mod2.test('mod1.py') instead of your line below >> test('mod1.py') >> >> #mod2.py >> def countLines(name): >> print len(open(name).readlines()) >> >> def countChars(name): >> print len(open(name).read()) >> >> def test(name): >> print 'loading...' >> countLines(name) >> countChars(name) >> print '-'*10 > The reason that import is better than from xxx import has to do with name collisions. If you have various modules and they all have a test function, you won't be able to use them all. The last one will be the only one named test() > Loosely speaking, it does import the other methods. In order to > successfully import test(), it has to resolve countLines() and > countChars(). They won't be directly accessible, as they don't exist > in the namespace, but test() knows about them. You are reading too much > into the comment that the module is "deleted". > > -- > David Rock > david at graniteweb.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick From __peter__ at web.de Tue Aug 14 21:56:34 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Aug 2012 21:56:34 +0200 Subject: [Tutor] Confusion regarding the 'from' statement References: Message-ID: Mazhar Hussain wrote: > Im new to python and theres something thats bothering me for quite a > time. I read in 'Learning Python' by Mark Lutz that when we use a > 'from' statement to import a name present in a module, it first > imports the module then assigns a new name to it(i.e. the name of the > function,class, etc present in the imported module) and then deletes > the module object with the del statement. However what happens if I > try to import a name using 'from' that references a name in the > imported module that itself is not imported. Consider the following > example,here there are two modules mod1.py and mod2.py, > > #mod1.py > from mod2 import test > test('mod1.py') > > #mod2.py > def countLines(name): > print len(open(name).readlines()) > > def countChars(name): > print len(open(name).read()) > > def test(name): > print 'loading...' > countLines(name) > countChars(name) > print '-'*10 > > Now see what happens when I run or import mod1 > >>>>import mod1 > > loading... > 3 > 44 > ---------- > > Here when I imported and ran the 'test' function, it ran successfully > although I didn't even import countChars or countLines, and the 'from' > statement had already deleted the mod2 module object. > > SO I basically need to know why does this code work although > considering the problems I mentioned it shouldn't. A quick look under the hood: A module is an object like every other in Python and it will not be deleted until all references to it are gone: >>> class A: ... def __del__(self): print "I'm gone" ... >>> a = b = A() >>> del a # the object is still referenced by the name 'b' >>> del b # the point of no return I'm gone A reference for every module that was ever imported is kept in a cache called sys.modules (a Python dict): >>> from mod2 import test >>> import sys >>> mod2 Traceback (most recent call last): File "", line 1, in NameError: name 'mod2' is not defined >>> "mod2" in sys.modules True >>> sys.modules["mod2"].countLines >>> test("mod2.py") loading... 11 215 ---------- Once you delete the module from that cache your little test() function is in trouble. While it still keeps a reference to the module's global namespace >>> del sys.modules["mod2"] >>> "countLines" in test.__globals__ True all entries in that namespace were set to None during module cleanup: >>> test("mod2.py") loading... Traceback (most recent call last): File "", line 1, in File "mod2.py", line 9, in test countLines(name) TypeError: 'NoneType' object is not callable >>> From eryksun at gmail.com Tue Aug 14 22:00:10 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 14 Aug 2012 16:00:10 -0400 Subject: [Tutor] Confusion regarding the 'from' statement In-Reply-To: References: Message-ID: On Tue, Aug 14, 2012 at 2:24 PM, Mazhar Hussain wrote: > > #mod1.py > from mod2 import test > test('mod1.py') > > #mod2.py > def countLines(name): > print len(open(name).readlines()) > > def countChars(name): > print len(open(name).read()) > > def test(name): > print 'loading...' > countLines(name) > countChars(name) > print '-'*10 > > Here when I imported and ran the 'test' function, it ran successfully > although I didn't even import countChars or countLines, and the 'from' > statement had already deleted the mod2 module object. > > SO I basically need to know why does this code work although > considering the problems I mentioned it shouldn't. mod1 gets a reference to mod2.test, but the mod2 object isn't garbage collected. A reference exists in sys.modules. After the import, add the following: import sys print sys.modules['mod2'] Also test() can access countChars() and countLines() because it was defined in the mod2 namespace. In other words, test.__globals__ is mod2.__globals__. An example: #mod1.py import mod2 mod2.test_global() print mod2.g #mod2.py def test_global(): global g g = "I'm in mod2." #run python mod1.py Even though test_global is called from mod1, g is created in mod2. More namespace trivia... When you run a module as the main script, its name is '__main__' and other modules can "import __main__". But if you import it by the filename, you get a new module instance with a separate namespace: #mod1 if __name__ == '__main__': import mod2 mod2.imp_main() #create g in __main__ and also in mod1 print g #__main__ import mod1 #self import, but mod1 instance, not __main__ print mod1.g #mod1 #mod2 def imp_main(): import __main__ import mod1 __main__.g = "__main__" mod1.g = "mod1" #run python mod1.py From lilytran at adobe.com Wed Aug 15 00:30:17 2012 From: lilytran at adobe.com (Lily Tran) Date: Tue, 14 Aug 2012 15:30:17 -0700 Subject: [Tutor] How do I fix this? Message-ID: I am trying to read in a string from the user input and if the string is a number then I would print out the numbers + 1. For example if the user enters: I have 320 dollars to buy grocery. My program would print out: I have 321 to buy grocery. However, the code that I have listed below would print out: I have 431 dollars to buy grocery. How do I fix this program so that it print out 321 vs. 431? ===================================================== #!/usr/bin/python3 def WhereAreNumbers(a_list): for index, element in enumerate(a_list): if element.isdigit(): new_number = int(element) print (new_number + 1, end="") else: print(element,end="") def Test(): answer = input("Please enter your strings:") if answer: WhereAreNumbers(answer) Test() ========================================== Please enter your strings:I have 320 dollars to buy grocery I have 431 dollars to buy grocery Thanks; Lily -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Aug 15 00:49:19 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Aug 2012 23:49:19 +0100 Subject: [Tutor] How do I fix this? In-Reply-To: References: Message-ID: On 14/08/12 23:30, Lily Tran wrote: > I have 320 dollars to buy grocery. My program would print out: I have > 321 to buy grocery. However, the code that I have listed below would > print out: I have 431 dollars to buy grocery. How do I fix this program > so that it print out 321 vs. 431? Since this is obviously a homework or tutorial exercise I'll only give some hints. > def WhereAreNumbers(a_list): > > for index, element in enumerate(a_list): note: a_list is really a string not a list plus you aren't using index so you could just use: for char in a_string > if element.isdigit(): > new_number = int(element) You stop with the first digit but you really need to collect all the digits before converting to a number. There are several ways to do this but I'll discuss the most simplistic... You need to store the digit you found into a list then fetch the next character from the string and see if it too is a number, if so add it to the list of current digits. When you get a non-digit convert your list of digits to a number and add one. So how to fetch the next char? Just let the loop "continue"(hint) HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From etanes.rm at gmail.com Wed Aug 15 00:57:46 2012 From: etanes.rm at gmail.com (Scurvy Scott) Date: Tue, 14 Aug 2012 15:57:46 -0700 Subject: [Tutor] Resource question? Message-ID: Hello, I'm totally new to this list. I've been learning python through codecademy.com which has een helping a lot with it's step by step approach. I'm wondering if there are any others like it? I've been looking at some other places that attempt to teach python (google python course, code kata or some such thing, and a couple of others) but I just have an easier time learning with that step by step approach. was wondering if anyone knew of anything even remotely similar. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lilytran at adobe.com Wed Aug 15 01:06:47 2012 From: lilytran at adobe.com (Lily Tran) Date: Tue, 14 Aug 2012 16:06:47 -0700 Subject: [Tutor] How do I fix this? In-Reply-To: Message-ID: I figure out how to do this. Here is the code that would allow me to print 320 to 321: def WhereAreNumbers(a_list): numberString='' for index, element in enumerate(a_list): if element.isdigit(): numberString += element else: if numberString != "": print(int(numberString)+1,end="") numberString = "" print(element,end="") if numberString != "": print(int(numberString)+1,end="") def Test(): answer = input("Please enter your strings:") if answer: WhereAreNumbers(answer) ================== Thanks; Lily From: Lily Tran > Date: Tue, 14 Aug 2012 15:30:17 -0700 To: "tutor at python.org" > Subject: How do I fix this? I am trying to read in a string from the user input and if the string is a number then I would print out the numbers + 1. For example if the user enters: I have 320 dollars to buy grocery. My program would print out: I have 321 to buy grocery. However, the code that I have listed below would print out: I have 431 dollars to buy grocery. How do I fix this program so that it print out 321 vs. 431? ===================================================== #!/usr/bin/python3 def WhereAreNumbers(a_list): for index, element in enumerate(a_list): if element.isdigit(): new_number = int(element) print (new_number + 1, end="") else: print(element,end="") def Test(): answer = input("Please enter your strings:") if answer: WhereAreNumbers(answer) Test() ========================================== Please enter your strings:I have 320 dollars to buy grocery I have 431 dollars to buy grocery Thanks; Lily -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Aug 15 01:17:00 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Aug 2012 00:17:00 +0100 Subject: [Tutor] Resource question? In-Reply-To: References: Message-ID: On 14/08/12 23:57, Scurvy Scott wrote: > I've been learning python through codecademy.com > which has een helping a lot with it's step by step approach. I'm > wondering if there are any others like it? I've been looking at some > other places that attempt to teach python (google python course, code > kata or some such thing, and a couple of others) but I just have an > easier time learning with that step by step approach. was wondering if > anyone knew of anything even remotely similar. It depends what you want. There are lots of tutors 9including mine) that gradually build up code by getting you to enter short examples. Not many are live like codecademy is (which is pretty neat - I once tried to do that for mine but it got way too hard, way too soon!) but many use a "passive interactive" style. Check out the Python beginners pages on the web site. (or my tutor link below!) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Wed Aug 15 01:48:13 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 15 Aug 2012 09:48:13 +1000 Subject: [Tutor] overriding instance attributes with keywords In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D647140DAF4B@EXCH2.nws.oregonstate.edu> <5029B7DC.90005@pearwood.info> Message-ID: <502AE3BD.8060903@pearwood.info> On 15/08/12 04:25, Matt Gregory wrote: > Thanks to you both for really helpful advice. A quick follow-up question - would you want to create a deepcopy of obj1 in the above example if your obj1 contained other objects? Only the person making the copy can answer that question, but in general, shallow copies are more common than deep copies. Making a deep copy can be relatively expensive, because Python will copy all the way down, as many levels deep as there are. But it's the only way to ensure that each copy is truly independent. > I think that Steven's class method gets around this? Only by ignoring it completely :) -- Steven From lisa at delightfullycranky.com Wed Aug 15 01:35:35 2012 From: lisa at delightfullycranky.com (Lisa Jervis) Date: Tue, 14 Aug 2012 16:35:35 -0700 Subject: [Tutor] Resource question? In-Reply-To: References: Message-ID: <502AE0C7.3080907@delightfullycranky.com> On 8/14/12 3:57 PM, Scurvy Scott wrote: > Hello, I'm totally new to this list. > > I've been learning python through codecademy.com > which has een helping a lot with it's step by > step approach. I'm wondering if there are any others like it? I've > been looking at some other places that attempt to teach python (google > python course, code kata or some such thing, and a couple of others) > but I just have an easier time learning with that step by step > approach. was wondering if anyone knew of anything even remotely similar. > > Thanks in advance. I have had a really good experience with http://www.learnpythonthehardway.org/. HTH, Lisa > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Lisa Jervis author/editor, aspiring geek, all-around smarty-pants www.cook-food.org @cook_food @infowranglr -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Aug 15 03:04:20 2012 From: d at davea.name (Dave Angel) Date: Tue, 14 Aug 2012 21:04:20 -0400 Subject: [Tutor] Confusion regarding the 'from' statement In-Reply-To: References: Message-ID: <502AF594.1020800@davea.name> On 08/14/2012 04:00 PM, eryksun wrote: > On Tue, Aug 14, 2012 at 2:24 PM, Mazhar Hussain wrote: >> #mod1.py >> from mod2 import test >> test('mod1.py') >> >> #mod2.py >> def countLines(name): >> print len(open(name).readlines()) >> >> def countChars(name): >> print len(open(name).read()) >> >> def test(name): >> print 'loading...' >> countLines(name) >> countChars(name) >> print '-'*10 >> >> Here when I imported and ran the 'test' function, it ran successfully >> although I didn't even import countChars or countLines, and the 'from' >> statement had already deleted the mod2 module object. >> >> SO I basically need to know why does this code work although >> considering the problems I mentioned it shouldn't. > mod1 gets a reference to mod2.test, but the mod2 object isn't garbage > collected. A reference exists in sys.modules. After the import, add > the following: > > import sys > print sys.modules['mod2'] > > Also test() can access countChars() and countLines() because it was > defined in the mod2 namespace. In other words, test.__globals__ is > mod2.__globals__. An example: > > #mod1.py > import mod2 > mod2.test_global() > print mod2.g > > #mod2.py > def test_global(): > global g > g = "I'm in mod2." > > #run python mod1.py > > Even though test_global is called from mod1, g is created in mod2. > > More namespace trivia... > When you run a module as the main script, its name is '__main__' and > other modules can "import __main__". But if you import it by the > filename, you get a new module instance with a separate namespace: > > #mod1 > if __name__ == '__main__': > import mod2 > mod2.imp_main() #create g in __main__ and also in mod1 > print g #__main__ > import mod1 #self import, but mod1 instance, not __main__ > print mod1.g #mod1 > > #mod2 > def imp_main(): > import __main__ > import mod1 > __main__.g = "__main__" > mod1.g = "mod1" > > #run python mod1.py I can't tell from your wording if you're recommending this "feature" or just pointing it out as an anomaly. There are two "mistakes" happening in this sample code which can cause real problems in real code: 1) recursive imports. If anybody has a non-trivial top-level code, or even certain class-initialization code, this can cause surprising errors. 2) multiple imports under different names. By importing the "same" module under the two names __main__ and mod1, you usually end up with two copies of stuff, and that can break a lot of implicit assumptions in working code. -- DaveA From eryksun at gmail.com Wed Aug 15 04:08:04 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 14 Aug 2012 22:08:04 -0400 Subject: [Tutor] Confusion regarding the 'from' statement In-Reply-To: <502AF594.1020800@davea.name> References: <502AF594.1020800@davea.name> Message-ID: On Tue, Aug 14, 2012 at 9:04 PM, Dave Angel wrote: > > 1) recursive imports. If anybody has a non-trivial top-level code, or > even certain class-initialization code, this can cause surprising errors. Yes, it's a problem if you try to "from" import an object that hasn't been defined yet or use something that hasn't been properly initialized. But AFAIK, just importing the current module won't execute it twice unless it's running as __main__. You get the existing reference from sys.modules. > 2) multiple imports under different names. By importing the "same" > module under the two names __main__ and mod1, you usually end up with > two copies of stuff, and that can break a lot of implicit assumptions in > working code. I wasn't casting any judgment, but personally I agree it's a bad idea. Without the "if" block the code executes twice, and everything gets defined twice. If for some reason you need to import the main script (for whatever reason, but the point is probably academic), import __main__, not its module filename. From alan.gauld at btinternet.com Wed Aug 15 10:02:49 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Aug 2012 09:02:49 +0100 Subject: [Tutor] How do I fix this? In-Reply-To: References: Message-ID: On 15/08/12 00:06, Lily Tran wrote: > I figure out how to do this. > Well done, now for bonus points can you make it handle negative numbers too? :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Wed Aug 15 19:27:38 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 15 Aug 2012 19:27:38 +0200 Subject: [Tutor] Confusion regarding the 'from' statement References: Message-ID: Mazhar Hussain wrote: > thanks alot for the help, was really confused with this. > Well what ure > trying to say is that even when a module object is deleted from the > global namespace of a module, a reference of it is still present in > the sys.modules dict? Yes, adding the reference to the sys.modules cache is an integral part of the import. > that means every object refers to its own module > in the sys.modules dict? No, every function has a reference to the enclosing module's global namespace where it looks up (global) names. As long as the module isn't garbage-collected these names refer to meaningful values. The module isn't garbage-collected as long as there is at least one reference, and for modules that occur only in a from mymodule import ... statement that reference is sys.modules["mymodule"]. From fomcl at yahoo.com Thu Aug 16 10:45:31 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 16 Aug 2012 01:45:31 -0700 (PDT) Subject: [Tutor] sneaky re.compile --is this a bug?? Message-ID: <1345106731.4887.YahooMailNeo@web110704.mail.gq1.yahoo.com> ? Hi, ? Is it intended behavior that regular expression flags are ignored when compiled regexes are used? In the code below, I intend to match path names case-independently, but once the flags (in this case: no flags at all) have been set in re.compile, subsequent flags in re.search are ignored (no pun intended). This is sneaky! Is this because of the old Python version I am using? ? Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 >>> import re >>> s = r"""DATA LIST???????? FILE = '\\besh1f\inkomen3\iis1\iistaak\produktie\dividend\base\j2010\bestand\D1305_01' /""" >>> regex = "(file|name) ?= ?['\"]([^'\"]+)['\"]" >>> m1 = re.search(regex, s, re.I) >>> m1.group(2) '\\\\besh1f\\inkomen3\\iis1\\iistaak\\produktie\\dividend\\base\\j2010\\bestand\\D1305_01' >>> compiledRegex = re.compile(regex, s)??? ## no ignorecase here >>> m2 = re.search(compiledRegex, s, re.I)? ## re.I?(ignorecase) is SILENTLY IGNORED >>> m2 is None True Thanks in advance for your replies! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Thu Aug 16 11:23:42 2012 From: wprins at gmail.com (Walter Prins) Date: Thu, 16 Aug 2012 10:23:42 +0100 Subject: [Tutor] sneaky re.compile --is this a bug?? In-Reply-To: <1345106731.4887.YahooMailNeo@web110704.mail.gq1.yahoo.com> References: <1345106731.4887.YahooMailNeo@web110704.mail.gq1.yahoo.com> Message-ID: Hi Albert, On 16 August 2012 09:45, Albert-Jan Roskam wrote: > > Hi, > > Is it intended behavior that regular expression flags are ignored when > compiled regexes are used? In the code below, I intend to match path names > case-independently, but once the flags (in this case: no flags at all) have > been set in re.compile, subsequent flags in re.search are ignored (no pun > intended). This is sneaky! Is this because of the old Python version I am > using? I don't think so . Firstly I think you're arguably using the compiled regex incorrectly. The normal idiom for a compiled regex is: compiled_regex = re.compile(....) and then: compiled_regex.match(...) compiled_regex.search(...) Note, both the regular expression and flags to be used is "baked" into the compiled regex object. By contrast, you're calling re.search() and then passing a previously compiled regex (instead of a regex pattern as strictly speaking is required) to the re.search() method. I suspect that what's happening is that re.search() is perhaps trying to be being helpful by seeing that it's not been given a regular expression but intead a compiled regex, and is then therefore relaying the re.search() request back to the compiled regex's search() method. But, as mentioned, a compiled regex includes the flags it was compiled with, which may differ from the ones passed to re.search(), which is I think why you're seeing what you're seeing. (Observe also the compile regex object's search() method does not accept a flags parameter.) If you want to use different flags you must compile another copy of the regex expression if you want to use the regex in a compiled form. All of that said, you can actually inspect the flags applicable to a compiled regex by evaluating the "flags" attribute on the compiled regex object. This will report a combination of the flags given to compile() and any flags specified inline inside the regular expression itself. It may be worth enquiring on the Python development list whether the behaviour around your case of re.search( compiled_regex, flags) should be perhaps handled slightly differently if the flags specified do not match the already existing flags in the compiled regex, perhaps by raising an exception. The principle of least surprise would seem to suggest that this might be better than silently giving you something else than asked for. Walter From hugo.yoshi at gmail.com Thu Aug 16 11:43:49 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Thu, 16 Aug 2012 11:43:49 +0200 Subject: [Tutor] sneaky re.compile --is this a bug?? In-Reply-To: References: <1345106731.4887.YahooMailNeo@web110704.mail.gq1.yahoo.com> Message-ID: On Thu, Aug 16, 2012 at 11:23 AM, Walter Prins wrote: > Hi Albert, > > On 16 August 2012 09:45, Albert-Jan Roskam wrote: > > > > Hi, > > > > Is it intended behavior that regular expression flags are ignored when > > compiled regexes are used? In the code below, I intend to match path > names > > case-independently, but once the flags (in this case: no flags at all) > have > > been set in re.compile, subsequent flags in re.search are ignored (no pun > > intended). This is sneaky! Is this because of the old Python version I am > > using? > > I don't think so . Firstly I think you're arguably using the compiled > regex incorrectly. The normal idiom for a compiled regex is: > compiled_regex = re.compile(....) > > and then: > compiled_regex.match(...) > compiled_regex.search(...) > > Note, both the regular expression and flags to be used is "baked" into > the compiled regex object. > > By contrast, you're calling re.search() and then passing a previously > compiled regex (instead of a regex pattern as strictly speaking is > required) to the re.search() method. I suspect that what's happening > is that re.search() is perhaps trying to be being helpful by seeing > that it's not been given a regular expression but intead a compiled > regex, and is then therefore relaying the re.search() request back to > the compiled regex's search() method. But, as mentioned, a compiled > regex includes the flags it was compiled with, which may differ from > the ones passed to re.search(), which is I think why you're seeing > what you're seeing. (Observe also the compile regex object's search() > method does not accept a flags parameter.) If you want to use > different flags you must compile another copy of the regex expression > if you want to use the regex in a compiled form. > > All of that said, you can actually inspect the flags applicable to a > compiled regex by evaluating the "flags" attribute on the compiled > regex object. This will report a combination of the flags given to > compile() and any flags specified inline inside the regular expression > itself. It may be worth enquiring on the Python development list > whether the behaviour around your case of re.search( compiled_regex, > flags) should be perhaps handled slightly differently if the flags > specified do not match the already existing flags in the compiled > regex, perhaps by raising an exception. The principle of least > surprise would seem to suggest that this might be better than silently > giving you something else than asked for. > > To add to this, in python 2.6.5 trying to do this raises an error: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> comp = re.compile('a') >>> re.match(comp, 'A', re.I) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/re.py", line 137, in match return _compile(pattern, flags).match(string) File "/usr/lib/python2.6/re.py", line 238, in _compile raise ValueError('Cannot process flags argument with a compiled pattern') ValueError: Cannot process flags argument with a compiled pattern >>> Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Thu Aug 16 12:07:58 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 16 Aug 2012 03:07:58 -0700 (PDT) Subject: [Tutor] sneaky re.compile --is this a bug?? In-Reply-To: References: <1345106731.4887.YahooMailNeo@web110704.mail.gq1.yahoo.com> Message-ID: <1345111678.37206.YahooMailNeo@web110716.mail.gq1.yahoo.com> To add to this, in python 2.6.5 trying to do this raises an error: > >Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)? >[GCC 4.4.3] on linux2 >Type "help", "copyright", "credits" or "license" for more information. >>>> import re >>>> comp = re.compile('a') >>>> re.match(comp, 'A', re.I) >Traceback (most recent call last): >? File "", line 1, in >? File "/usr/lib/python2.6/re.py", line 137, in match >? ? return _compile(pattern, flags).match(string) >? File "/usr/lib/python2.6/re.py", line 238, in _compile >? ? raise ValueError('Cannot process flags argument with a compiled pattern') >ValueError: Cannot process flags argument with a compiled pattern >>>>? > >Walter, Hugo, > >THANKS for your replies. I am getting my sanity back again. ;-) This is another reason to stay up-to-date with software versions. I wish my company upgraded to 2.7.highest and ditched all the older versions. In the future, I'll also use re.compile in the way Walter describes. > >Albert-Jan > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zaatlob at hotmail.com Thu Aug 16 13:33:13 2012 From: zaatlob at hotmail.com (leon zaat) Date: Thu, 16 Aug 2012 11:33:13 +0000 Subject: [Tutor] FW: output not in ANSI, conversing char set to locale.getpreferredencoding() In-Reply-To: References: , , , , , Message-ID: > To: tutor at python.org > From: __peter__ at web.de > Date: Tue, 14 Aug 2012 16:03:46 +0200 > Subject: Re: [Tutor] output not in ANSI, conversing char set to locale.getpreferredencoding() > > leon zaat wrote: > > > I get the error: > > UnicodeDecodeError: 'ascii' codecs can't decode byte 0xc3 in position 7: > > ordinal not in range(128) for the openbareruimtenaam=u'' + > > (openbareruimtenaam1.encode(chartype)) line. > > > The error message means that database.select() returns a byte string. > > bytestring.encode(encoding) > > implicitly attempts > > bytestring.decode("ascii").encode(encoding) > > and will fail for non-ascii bytestrings no matter what encoding you pass to > the encode() method. > > > I know that the default system codecs is ascii and chartype=b'cp1252' > > But how can i get the by pass the ascii encoding? > > You have to find out the database encoding -- then you can change the > failing line to > > database_encoding = ... # you need to find out yourself, but many use the > # UTF-8 -- IMO the only sensible choice these days > file_encoding = "cp1252" > > openbareruimtenaam = openbareruimtenaam1.decode( > database_encoding).encode(file_encoding) > > As you now have a bytestring again you can forget about codecs.open() which > won't work anyway as the csv module doesn't support unicode properly in > Python 2.x (The csv documentation has the details). > Tried it with: openbareruimtenaam = openbareruimtenaam1.decode("UTF-8").encode("cp1252") but still the complains about the ascii error prior message: import csv import codecs import locale # Globale variabele bagObjecten = [] chartype=locale.getpreferredencoding() #------------------------------------------------------------------------------ # BAGExtractPlus toont het hoofdscherm van de BAG Extract+ tool #------------------------------------------------------------------------------ class BAGExtractPlus(wx.Frame): #------------------------------------------------------------------------------ # schrijven van de records #------------------------------------------------------------------------------ def schrijfExportRecord(self, verblijfhoofd,identificatie): sql1=""; sql1="Select openbareruimtenaam, woonplaatsnaam from nummeraanduiding where identificatie = '" + identificatie "'" num= database.select(sql1); for row in num: openbareruimtenaam1=row[0] openbareruimtenaam=u'' + (openbareruimtenaam1.encode(chartype)) woonplaatsnaam1=(row[0]); woonplaatsnaam=u'' + (woonplaatsnaam1.encode(chartype)) newrow=[openbareruimtenaam, woonplaatsnaam]; verblijfhoofd.writerow(newrow); #-------------------------------------------------------------------------------------- # Exporteer benodigde gegevens #-------------------------------------------------------------------------------------- def ExportBestanden(self, event): ofile=codecs.open(r'D:\bestanden\BAG\adrescoordinaten.csv', 'wb', chartype) verblijfhoofd = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) counterVBO=2; identificatie='0014010011066771'; while 1 < counterVBO: hulpIdentificatie= identificatie; sql="Select identificatie, hoofdadres, verblijfsobjectgeometrie from verblijfsobject where "; sql= sql + "identificatie > '" + hulpIdentificatie ; vbo= database.select(sql); if not vbo: break; else: for row in vbo: identificatie=row[0]; verblijfobjectgeometrie=row[2]; self.schrijfExportRecord(verblijfhoofd, identificatie) I highlighted in red the lines i think that are important. When i try to convert openbareruimtenaam from the data below: "P.J. No?l Bakerstraat";"Groningen" I get the error: UnicodeDecodeError: 'ascii' codecs can't decode byte 0xc3 in position 7: ordinal not in range(128) for the openbareruimtenaam=u'' + (openbareruimtenaam1.encode(chartype)) line. I know that the default system codecs is ascii and chartype=b'cp1252' But how can i get the by pass the ascii encoding? -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Thu Aug 16 15:02:44 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 16 Aug 2012 09:02:44 -0400 Subject: [Tutor] FW: output not in ANSI, conversing char set to locale.getpreferredencoding() In-Reply-To: References: Message-ID: On Thu, Aug 16, 2012 at 7:33 AM, leon zaat wrote: > > Tried it with: > openbareruimtenaam = openbareruimtenaam1.decode("UTF-8").encode("cp1252") > but still the complains about the ascii error Did you also change it for woonplaatsnaam? For example: ~~~~ import locale file_encoding = locale.getpreferredencoding() #e.g. "cp1252" db_encoding = "utf-8" for row in num: openbareruimtenaam1 = row[0].decode(db_encoding) openbareruimtenaam = openbareruimtenaam1.encode(file_encoding) woonplaatsnaam1 = row[0].decode(db_encoding) woonplaatsnaam = woonplaatsnaam1.encode(file_encoding) newrow = [openbareruimtenaam, woonplaatsnaam] verblijfhoofd.writerow(newrow) # Should "woonplaatsnaam1" be assigned to row[1] instead of row[0]? ~~~~ Off -topic comments: Try to avoid cluttering your code with redundant parentheses, unless they make it easier to understand a complex expression. If func() returns a string, you don't need to do "string" + (func()). The extra parentheses are just noise. Use "string" + func(). Also, if you have a long string, use the compiler's implicit string join instead of runtime concatenation. If you have "string1" "string2", the compiler stores "string1string2". You can combine this with a parenthesized multiline expression. For example: ~~~~ sql = ("Select identificatie, hoofdadres, verblijfsobjectgeometrie " "from verblijfsobject where identificatie > '" + hulpIdentificatie) ~~~~ From debby at glance.net Fri Aug 17 16:27:04 2012 From: debby at glance.net (debbym) Date: Fri, 17 Aug 2012 07:27:04 -0700 (PDT) Subject: [Tutor] specifying my "default" python installation Message-ID: <1345213624104-4985459.post@n6.nabble.com> I am new to both freebsd and python. I have python 2.6 and 3.2 both installed on freebsd. "python" runs python 2.6 and I need to use "python3.2" to run python 3.2 Do I need to do something to make python 3.2 the default? -- View this message in context: http://python.6.n6.nabble.com/specifying-my-default-python-installation-tp4985459.html Sent from the Python - tutor mailing list archive at Nabble.com. From ramit.prasad at jpmorgan.com Fri Aug 17 17:05:43 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 17 Aug 2012 15:05:43 +0000 Subject: [Tutor] specifying my "default" python installation In-Reply-To: <1345213624104-4985459.post@n6.nabble.com> References: <1345213624104-4985459.post@n6.nabble.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474165FC936@SCACMX008.exchad.jpmchase.net> > I am new to both freebsd and python. > I have python 2.6 and 3.2 both installed on freebsd. > "python" runs python 2.6 and I need to use "python3.2" to run python 3.2 > Do I need to do something to make python 3.2 the default? I am not really familiar with BSD but *nix has the application update-alternatives. That will do what you want. Otherwise, you could change the name/location in the bin directory. It is likely that python is a symlink to python2.6 and all you need to do is change the symlink to point to python3.2. If no symlink is used you can rename the binaries instead. Apologies in advanced if this is way off base. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From debby at glance.net Fri Aug 17 17:40:42 2012 From: debby at glance.net (Debby Mendez) Date: Fri, 17 Aug 2012 11:40:42 -0400 Subject: [Tutor] specifying my "default" python installation In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474165FC936@SCACMX008.exchad.jpmchase.net> References: <1345213624104-4985459.post@n6.nabble.com> <5B80DD153D7D744689F57F4FB69AF474165FC936@SCACMX008.exchad.jpmchase.net> Message-ID: <005a01cd7c8e$a90c2c80$fb248580$@net> OK thanks. It sounds like maybe this is all normal then, not an indication that something went wrong with my 3.2 install. -----Original Message----- From: tutor-bounces+debby=glance.net at python.org [mailto:tutor-bounces+debby=glance.net at python.org] On Behalf Of Prasad, Ramit Sent: Friday, August 17, 2012 11:06 AM To: tutor at python.org Subject: Re: [Tutor] specifying my "default" python installation > I am new to both freebsd and python. > I have python 2.6 and 3.2 both installed on freebsd. > "python" runs python 2.6 and I need to use "python3.2" to run python > 3.2 Do I need to do something to make python 3.2 the default? I am not really familiar with BSD but *nix has the application update-alternatives. That will do what you want. Otherwise, you could change the name/location in the bin directory. It is likely that python is a symlink to python2.6 and all you need to do is change the symlink to point to python3.2. If no symlink is used you can rename the binaries instead. Apologies in advanced if this is way off base. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From eryksun at gmail.com Fri Aug 17 18:47:20 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 17 Aug 2012 12:47:20 -0400 Subject: [Tutor] specifying my "default" python installation In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474165FC936@SCACMX008.exchad.jpmchase.net> References: <1345213624104-4985459.post@n6.nabble.com> <5B80DD153D7D744689F57F4FB69AF474165FC936@SCACMX008.exchad.jpmchase.net> Message-ID: On Fri, Aug 17, 2012 at 11:05 AM, Prasad, Ramit wrote: > > I am not really familiar with BSD but *nix has the application > update-alternatives. That will do what you want. Otherwise, > you could change the name/location in the bin directory. > It is likely that python is a symlink to python2.6 and all > you need to do is change the symlink to point to python3.2. > If no symlink is used you can rename the binaries instead. Modifying the default to Python 3 sounds like a bad idea. Platforms are still in transition to 3.x. Some scripts might assume /usr/bin/python links to python2.x. On Debian there's a python3 symlink. You can add your own if FreeBSD doesn't have it. First, if ~/bin doesn't exist, run "mkdir ~/bin"; restart your session, and run "echo $PATH" to verify it's on the search path. Then make the link with "ln -s /usr/bin/python3.2 ~/bin/python3". You'll have to update this when 3.3 is released. If ~/bin isn't on the PATH, edit ~/.profile and add the following: if [ -d "$HOME/bin" ] ; then PATH="$HOME/bin:$PATH" fi Also, while it's probably a bit much at first, I recommend using virtualenv to configure environments with different versions of Python/packages: http://www.virtualenv.org From alan.gauld at btinternet.com Fri Aug 17 20:11:46 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 17 Aug 2012 19:11:46 +0100 Subject: [Tutor] specifying my "default" python installation In-Reply-To: <1345213624104-4985459.post@n6.nabble.com> References: <1345213624104-4985459.post@n6.nabble.com> Message-ID: On 17/08/12 15:27, debbym wrote: > I am new to both freebsd and python. > I have python 2.6 and 3.2 both installed on freebsd. > "python" runs python 2.6 and I need to use "python3.2" to run python 3.2 > Do I need to do something to make python 3.2 the default? The generic way to do that on *nix is to create/modify the symbolic links in the directory holding the binaries. You should be able to get that with the which command: which python which python2 which python3 On my system the latest install package seems to create the python2/python3 links itself. I'm not sure what sets up the python link - the mysteries of Synaptic package management. Once you have the directory you can view the links with ls -l In my case its in /usr/bin so: $ ls -l /usr/bin/pyth* lrwxrwxrwx 1 root root 9 2011-08-13 16:04 /usr/bin/python -> python2.6 lrwxrwxrwx 1 root root 9 2011-08-13 16:04 /usr/bin/python2 -> python2.6 -rwxr-xr-x 1 root root 2613296 2010-04-16 15:42 /usr/bin/python2.6 lrwxrwxrwx 1 root root 9 2011-08-13 20:47 /usr/bin/python3 -> python3.1 -rwxr-xr-x 1 root root 2849576 2011-12-09 21:26 /usr/bin/python3.1 You can then create/modify the links as required using the ln -s command. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From modulok at gmail.com Fri Aug 17 21:35:02 2012 From: modulok at gmail.com (Modulok) Date: Fri, 17 Aug 2012 13:35:02 -0600 Subject: [Tutor] specifying my "default" python installation In-Reply-To: <1345213624104-4985459.post@n6.nabble.com> References: <1345213624104-4985459.post@n6.nabble.com> Message-ID: On 8/17/12, debbym wrote: > I am new to both freebsd and python. > I have python 2.6 and 3.2 both installed on freebsd. > "python" runs python 2.6 and I need to use "python3.2" to run python 3.2 > Do I need to do something to make python 3.2 the default? FreeBSD doesn't have an 'alternatives' system like debian flavors. However, there's nothing wrong with your install. I run FreeBSD on several boxes. On this box I have python 2.6, 2.7, and 3.2 installed. My default 'python' executable is a symlink that points to /usr/local/bin/python, which itself is version 2.6. (The box I'm on is FreeBSD 8.1) If I want a program to run in python3.2, I put something like this (or similar) as the shebang line at the top of the file: #!/usr/bin/evn python3.2 Or to get the interpretter just type 'python3.2'. Don't worry about the shebang lines being cross platform either; Once you package up your scripts into python packages, using distutils or similar, all shebang lines will be stripped. Upon re-installation on the target platform they'll be replaced appropriately. (In fact, some platforms even put 'env' in /bin instead of FreeBSD's /usr/bin.) I would also recommend against changing the default 'python' symlink, as some tools may depend on it being at a specific version. One I can think of off the top of my head is the third party `xps` tool for searching ports. This is most notable when talking about python2.x vs. the 3.x branch, as python 3 broke backward compatibility with things like print statements becoming print functions. If you find it irritating to have to type 'python3.2' instead of just 'python', you could create an alias for your shell, so that 'python' is aliased to 'python3.2'. I use tcsh, so I in my case my $HOME/.cshrc file might look something like this: alias python /usr/local/bin/python3.2 Then I would type 'source .cshrc' or logout and back in for changes to take affect. As an alternative, you could create a symlink from your own bin directory instead. For example: cd mkdir bin ln -s /usr/local/bin/python3.2 ./bin/python This should work once you type 'rehash' (in tcsh) or logout and log back in. Most default installs will list the 'bin' directory, found in a given user's home directory, on the default $PATH. You can verify this by typing 'echo $PATH' and making sure '/home//bin' is listed. This isn't as good of an option as simply using the alternate shebangs, as it affectively replaces your version of python with the version you choose, but it will work. Finally, if you want to get carried away, you can install a virtual python as mentioned. For even more options, you might subscribe to questions at freebsd.org ;) -Modulok- From crawlzone at gmail.com Sat Aug 18 18:36:40 2012 From: crawlzone at gmail.com (Ray) Date: Sat, 18 Aug 2012 09:36:40 -0700 Subject: [Tutor] Introduction Message-ID: <502FC498.1050200@gmail.com> Hello. I am new to the mailing list and to Python. My knowledge of Python comes almost strictly from Nick Parlante's classes on YouTube that I've watched over the last week or so. I'm not certain why I'm diving into Python. My only coding experience has been using Bash scripts on my Ubuntu system for the past half dozen years, and so far I'm not particularly convinced that Python has any advantage FOR ME over what I have been using. In my Bash scripts I make generous use of sed and grep, with the occasional use of awk -- batch renaming of files, pulling exif date information from (manually) downloaded jpegs and adding that date to the front of file names, removing url '%' coding on photos and replacing it with "real" text, and the occasional foray into network stuff in conjunction with vlc or netcat. By no means am I a pro with Bash - only in the last six months have I really began to understand how to use arrays rather than temp files - but I can usually accomplish what I set out to do no matter how ugly the coding! The potential cross-platform use of Python is probably one of the major reasons for my interest....tho' I don't think I write anything anyone else would want...and although, as I look at my Python code so far, it's definitely hard-coded for a Linux system :-p. So much for that reasoning.... But here I am - I am looking forward to learning from everyone, but keep the terminology as simple as possible, if you please - I'm only now beginning to understand what objects are....lol From aclark at aclark.net Sat Aug 18 19:08:45 2012 From: aclark at aclark.net (Alex Clark) Date: Sat, 18 Aug 2012 13:08:45 -0400 Subject: [Tutor] Introduction References: <502FC498.1050200@gmail.com> Message-ID: Hi Ray, On 2012-08-18 16:36:40 +0000, Ray said: > Hello. I am new to the mailing list and to Python. My knowledge of > Python comes almost strictly from Nick Parlante's classes on YouTube > that I've watched over the last week or so. > > I'm not certain why I'm diving into Python. My only coding experience > has been using Bash scripts on my Ubuntu system for the past half dozen > years, and so far I'm not particularly convinced that Python has any > advantage FOR ME over what I have been using. > > In my Bash scripts I make generous use of sed and grep, with the > occasional use of awk -- batch renaming of files, pulling exif date > information from (manually) downloaded jpegs and adding that date to the > front of file names, removing url '%' coding on photos and replacing it > with "real" text, and the occasional foray into network stuff in > conjunction with vlc or netcat. By no means am I a pro with Bash - only > in the last six months have I really began to understand how to use > arrays rather than temp files - but I can usually accomplish what I set > out to do no matter how ugly the coding! > > The potential cross-platform use of Python is probably one of the major > reasons for my interest....tho' I don't think I write anything anyone > else would want...and although, as I look at my Python code so far, it's > definitely hard-coded for a Linux system :-p. So much for that reasoning.... > > But here I am - I am looking forward to learning from everyone, but keep > the terminology as simple as possible, if you please - I'm only now > beginning to understand what objects are....lol Welcome! Based on what you describe above, you might enjoy `Text Processing in Python`: - http://gnosis.cx/TPiP/ Alex > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Alex Clark ? http://pythonpackages.com From crawlzone at gmail.com Sat Aug 18 19:13:14 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sat, 18 Aug 2012 10:13:14 -0700 Subject: [Tutor] Introduction In-Reply-To: References: <502FC498.1050200@gmail.com> Message-ID: <502FCD2A.1030608@gmail.com> Thanks for the welcome - I'll take a look at your recommendation. Ray On 08/18/2012 10:08 AM, Alex Clark wrote: > Hi Ray, > > On 2012-08-18 16:36:40 +0000, Ray said: > >> Hello. I am new to the mailing list and to Python. My knowledge of >> Python comes almost strictly from Nick Parlante's classes on YouTube >> that I've watched over the last week or so. >> >> I'm not certain why I'm diving into Python. My only coding experience >> has been using Bash scripts on my Ubuntu system for the past half dozen >> years, and so far I'm not particularly convinced that Python has any >> advantage FOR ME over what I have been using. >> >> In my Bash scripts I make generous use of sed and grep, with the >> occasional use of awk -- batch renaming of files, pulling exif date >> information from (manually) downloaded jpegs and adding that date to the >> front of file names, removing url '%' coding on photos and replacing it >> with "real" text, and the occasional foray into network stuff in >> conjunction with vlc or netcat. By no means am I a pro with Bash - only >> in the last six months have I really began to understand how to use >> arrays rather than temp files - but I can usually accomplish what I set >> out to do no matter how ugly the coding! >> >> The potential cross-platform use of Python is probably one of the major >> reasons for my interest....tho' I don't think I write anything anyone >> else would want...and although, as I look at my Python code so far, it's >> definitely hard-coded for a Linux system :-p. So much for that >> reasoning.... >> >> But here I am - I am looking forward to learning from everyone, but keep >> the terminology as simple as possible, if you please - I'm only now >> beginning to understand what objects are....lol > > > Welcome! Based on what you describe above, you might enjoy `Text > Processing in Python`: > > > - http://gnosis.cx/TPiP/ > > > Alex > > >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > From alan.gauld at btinternet.com Sat Aug 18 19:25:39 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 18 Aug 2012 18:25:39 +0100 Subject: [Tutor] Introduction In-Reply-To: <502FC498.1050200@gmail.com> References: <502FC498.1050200@gmail.com> Message-ID: On 18/08/12 17:36, Ray wrote: > I'm not certain why I'm diving into Python. My only coding experience > has been using Bash scripts on my Ubuntu system for the past half dozen > years, and so far I'm not particularly convinced that Python has any > advantage FOR ME over what I have been using. Python may not give you any new capability for the kinds of things you describe but what you should find is that the code although maybe a smidge longer will be much easier to maintain. It will often run a little bit faster too (occasionally a lot faster) and use less computing resources. As with anything there will be a learning curve where it will feel a lot easier to just "knock something together is bash" but in time the Python approach will become more natural. Of course there will still be plenty of room for OS one liners. I still use bash and awk for short one-off jobs. But for things you do frequently Python is usually a better long term bet. And of course you can overlay a nice GUI to make those tools easier to use... > In my Bash scripts I make generous use of sed and grep, with the > occasional use of awk Remember that python can do all of those jobs natively, so resist the temptation to just use os.system() or the SubProcess module. Thee is a place for those, but its not to do what awk/sed etc can do - thats usually better kept within Python. > else would want...and although, as I look at my Python code so far, it's > definitely hard-coded for a Linux system :-p. So much for that reasoning.... We look forward to seeing some of it in the future when you ask questions. But bear in mind my comments about avoiding os.system() etc unless its the last resort. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From crawlzone at gmail.com Sat Aug 18 19:42:21 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sat, 18 Aug 2012 10:42:21 -0700 Subject: [Tutor] Introduction In-Reply-To: References: <502FC498.1050200@gmail.com> Message-ID: <502FD3FD.3010004@gmail.com> GUI? Moi? Hahaha....well....now that you mention it, I wonder.... Ray On 08/18/2012 10:25 AM, Alan Gauld wrote: > On 18/08/12 17:36, Ray wrote: > >> I'm not certain why I'm diving into Python. My only coding experience >> has been using Bash scripts on my Ubuntu system for the past half dozen >> years, and so far I'm not particularly convinced that Python has any >> advantage FOR ME over what I have been using. > > Python may not give you any new capability for the kinds of things you > describe but what you should find is that the code although maybe a > smidge longer will be much easier to maintain. It will often run a > little bit faster too (occasionally a lot faster) and use less > computing resources. > > As with anything there will be a learning curve where it will feel a > lot easier to just "knock something together is bash" but in time the > Python approach will become more natural. Of course there will still > be plenty of room for OS one liners. I still use bash and awk for > short one-off jobs. But for things you do frequently Python is usually > a better long term bet. And of course you can overlay a nice GUI to > make those tools easier to use... > >> In my Bash scripts I make generous use of sed and grep, with the >> occasional use of awk > > Remember that python can do all of those jobs natively, so resist the > temptation to just use os.system() or the SubProcess module. Thee is a > place for those, but its not to do what awk/sed etc can do - thats > usually better kept within Python. > >> else would want...and although, as I look at my Python code so far, it's >> definitely hard-coded for a Linux system :-p. So much for that >> reasoning.... > > We look forward to seeing some of it in the future when you ask > questions. But bear in mind my comments about avoiding os.system() etc > unless its the last resort. > > HTH, From modulok at gmail.com Sat Aug 18 21:24:49 2012 From: modulok at gmail.com (Modulok) Date: Sat, 18 Aug 2012 13:24:49 -0600 Subject: [Tutor] Genetic module recommendations? Message-ID: List, I'm looking for a good genetic module. (Stable, well documented, pythonic, etc.) I'm writing a breeding simulator where users select parent organisms to breed based on traits they favor, e.g: eye color, height, etc. The human player is the fitness function. I guess this is "artificial selection"? After breeding the user gets an offspring which carries traits from its parents. It's only a game and a crude approximation of nature at best. However, the algorithm has to be stable enough this can go on for several hundred generations without entropy reducing to the point of haulting evolution. On the other hand, I don't want so much entropy that it's reduce to a random search. Before I write my own, I thought I'd ask to see if there was a third party, de-facto standard Python genetic module. Or at least one that is highly recommended. Any suggestions? Thanks! -Modulok- From ryan.waples at gmail.com Sat Aug 18 21:45:16 2012 From: ryan.waples at gmail.com (Ryan Waples) Date: Sat, 18 Aug 2012 12:45:16 -0700 Subject: [Tutor] Genetic module recommendations? In-Reply-To: References: Message-ID: Not sure if it meets your needs, but you should at least check out simuPOP. On Sat, Aug 18, 2012 at 12:24 PM, Modulok wrote: > List, > > I'm looking for a good genetic module. (Stable, well documented, pythonic, > etc.) > > I'm writing a breeding simulator where users select parent organisms to breed > based on traits they favor, e.g: eye color, height, etc. The human player is > the fitness function. I guess this is "artificial selection"? After breeding > the user gets an offspring which carries traits from its parents. > > It's only a game and a crude approximation of nature at best. However, the > algorithm has to be stable enough this can go on for several hundred > generations without entropy reducing to the point of haulting evolution. On the > other hand, I don't want so much entropy that it's reduce to a random search. > > Before I write my own, I thought I'd ask to see if there was a third party, > de-facto standard Python genetic module. Or at least one that is highly > recommended. > > Any suggestions? > > Thanks! > -Modulok- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sat Aug 18 21:55:59 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 18 Aug 2012 20:55:59 +0100 Subject: [Tutor] Genetic module recommendations? In-Reply-To: References: Message-ID: On 18/08/12 20:24, Modulok wrote: > List, > > I'm looking for a good genetic module. (Stable, well documented, pythonic, > etc.) There probably is but asking on the tutor list - for beginners to Python - is less likely to find one than a request on the wider Python community at comp.lang.python. I'd try again there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Sat Aug 18 22:02:35 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 18 Aug 2012 21:02:35 +0100 Subject: [Tutor] Genetic module recommendations? In-Reply-To: References: Message-ID: On 18/08/2012 20:24, Modulok wrote: > List, > > I'm looking for a good genetic module. (Stable, well documented, pythonic, > etc.) > > I'm writing a breeding simulator where users select parent organisms to breed > based on traits they favor, e.g: eye color, height, etc. The human player is > the fitness function. I guess this is "artificial selection"? After breeding > the user gets an offspring which carries traits from its parents. > > It's only a game and a crude approximation of nature at best. However, the > algorithm has to be stable enough this can go on for several hundred > generations without entropy reducing to the point of haulting evolution. On the > other hand, I don't want so much entropy that it's reduce to a random search. > > Before I write my own, I thought I'd ask to see if there was a third party, > de-facto standard Python genetic module. Or at least one that is highly > recommended. > > Any suggestions? > > Thanks! > -Modulok- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Have you looked on pypi? http://pypi.python.org/pypi?%3Aaction=search&term=genetic&submit=search -- Cheers. Mark Lawrence. From douglas168 at yahoo.com Sun Aug 19 07:05:39 2012 From: douglas168 at yahoo.com (Douglas Kuo) Date: Sun, 19 Aug 2012 13:05:39 +0800 Subject: [Tutor] Python cms Message-ID: <2D313591-6D70-4D17-B940-60BF1966D257@yahoo.com> Hi all, I am mainly a php/drupal developer totally new to python.. Any experienced python especially in term of cms developers out there can tell me what are the benefits of python cms over php cms? I want to find out if I should switch to python cms? Thanks in advance, Douglas From steve at pearwood.info Sun Aug 19 09:10:02 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Aug 2012 17:10:02 +1000 Subject: [Tutor] Python cms In-Reply-To: <2D313591-6D70-4D17-B940-60BF1966D257@yahoo.com> References: <2D313591-6D70-4D17-B940-60BF1966D257@yahoo.com> Message-ID: <5030914A.1010308@pearwood.info> On 19/08/12 15:05, Douglas Kuo wrote: > Hi all, > > I am mainly a php/drupal developer totally new to python.. Any experienced >python especially in term of cms developers out there can tell me what are >the benefits of python cms over php cms? This might upset you, but the biggest advantage is that they aren't PHP. http://www.codinghorror.com/blog/2012/06/the-php-singularity.html http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/ I know what it's like to have people dissing your favourite language and telling you it isn't a "proper" programming language. But you know, sometimes there are good reasons why they do that, not just fashion. I found this spirited defense of PHP here: http://blog.ircmaxell.com/2012/04/php-sucks-but-i-like-it.html which raises some good points, but I think when the author finds himself defending PHP's == operator in the comments by saying: "It may not be sane, but it works..." then I think he's just conceded the argument. The biggest disadvantage of using a Python CMS is that if you're not running your own web server, you might have to call up two or three ISPs to find one that will provide you with Python on the server. In the Python ecosystem, we have Zope, which is a big, powerful heavyweight CMS. For a beginner, you might prefer to start with something more lightweight. Perhaps try Django: https://www.djangoproject.com/ See also http://wiki.python.org/moin/ContentManagementSystems For a wiki, try MoinMoin: http://moinmo.in/ For web frameworks, try CherryPy: http://www.cherrypy.org/ -- Steven From eire1130 at gmail.com Sun Aug 19 16:08:04 2012 From: eire1130 at gmail.com (James Reynolds) Date: Sun, 19 Aug 2012 10:08:04 -0400 Subject: [Tutor] Python cms In-Reply-To: <5030914A.1010308@pearwood.info> References: <2D313591-6D70-4D17-B940-60BF1966D257@yahoo.com> <5030914A.1010308@pearwood.info> Message-ID: <00D45040-5F7E-4201-82F1-A572233C9CDD@gmail.com> I agree with Steven. You will notice efficiency gains by moving to python. I'm a django developer myself, but some of the other frameworks work well also. That said the django community is fairly large, and there is an active cms project called django-cms. https://www.django-cms.org/ Sent from my iPad On Aug 19, 2012, at 3:10 AM, Steven D'Aprano wrote: > On 19/08/12 15:05, Douglas Kuo wrote: >> Hi all, >> >> I am mainly a php/drupal developer totally new to python.. Any experienced >> python especially in term of cms developers out there can tell me what are >> the benefits of python cms over php cms? > > This might upset you, but the biggest advantage is that they aren't PHP. > > http://www.codinghorror.com/blog/2012/06/the-php-singularity.html > > http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/ > > I know what it's like to have people dissing your favourite language and > telling you it isn't a "proper" programming language. But you know, sometimes > there are good reasons why they do that, not just fashion. > > I found this spirited defense of PHP here: > > http://blog.ircmaxell.com/2012/04/php-sucks-but-i-like-it.html > > which raises some good points, but I think when the author finds himself > defending PHP's == operator in the comments by saying: > > "It may not be sane, but it works..." > > then I think he's just conceded the argument. > > > The biggest disadvantage of using a Python CMS is that if you're not running > your own web server, you might have to call up two or three ISPs to find one > that will provide you with Python on the server. > > In the Python ecosystem, we have Zope, which is a big, powerful heavyweight > CMS. For a beginner, you might prefer to start with something more > lightweight. Perhaps try Django: > > https://www.djangoproject.com/ > > See also http://wiki.python.org/moin/ContentManagementSystems > > > For a wiki, try MoinMoin: > > http://moinmo.in/ > > For web frameworks, try CherryPy: > > http://www.cherrypy.org/ > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From selbyrowleycannon at ymail.com Sun Aug 19 21:29:22 2012 From: selbyrowleycannon at ymail.com (Selby Rowley Cannon) Date: Sun, 19 Aug 2012 20:29:22 +0100 Subject: [Tutor] (no subject) Message-ID: <50313E92.4070809@ymail.com> OK, I have some code, and it uses glob.glob('*.py') to find all the python files in the current directory. Just thought i'd ask, would: glob.glob('C:\*.py') (Windows), or glob.glob('/*.py') (*nix) find all the python files on the system? Thanks From d at davea.name Sun Aug 19 21:53:31 2012 From: d at davea.name (Dave Angel) Date: Sun, 19 Aug 2012 15:53:31 -0400 Subject: [Tutor] (no subject) In-Reply-To: <50313E92.4070809@ymail.com> References: <50313E92.4070809@ymail.com> Message-ID: <5031443B.9000102@davea.name> On 08/19/2012 03:29 PM, Selby Rowley Cannon wrote: > OK, I have some code, and it uses glob.glob('*.py') to find all the > python files in the current directory. > Just thought i'd ask, would: > > glob.glob('C:\*.py') (Windows), or > > glob.glob('/*.py') (*nix) > > find all the python files on the system? Thanks See http://docs.python.org/library/glob.html I don't see anywhere it implies that more than one directory would be scanned. In any case, it's pretty quick to just try it in the interpreter. I tried using python 2.7 on Linux, and it does not descend recursively into subdirectories. Perhaps you want os.walk, which lets you loop over an entire tree of files/directories. BTW, you forgot to double the backslash in your Windows example. Either double the backslash, or make it a raw string, or use forward slashes. -- DaveA From akleider at sonic.net Sun Aug 19 23:19:47 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Sun, 19 Aug 2012 14:19:47 -0700 Subject: [Tutor] (no subject)- finding/deleting files In-Reply-To: <5031443B.9000102@davea.name> References: <50313E92.4070809@ymail.com> <5031443B.9000102@davea.name> Message-ID: <807d39ebe0c727144b30369d74639678.squirrel@webmail.sonic.net> > On 08/19/2012 03:29 PM, Selby Rowley Cannon wrote: >> OK, I have some code, and it uses glob.glob('*.py') to find all the >> python files in the current directory. >> Just thought i'd ask, would: >> >> glob.glob('C:\*.py') (Windows), or >> >> glob.glob('/*.py') (*nix) >> >> find all the python files on the system? Thanks > > See http://docs.python.org/library/glob.html Selby, Also there is no rule that says that all python files must end in .py. Those that don't of course will not be 'found.' ;-( I just happened to knock out a little script that might be easily adapted to what you want to do. I'll attach it in case you want to make use of it. alex > > I don't see anywhere it implies that more than one directory would be > scanned. In any case, it's pretty quick to just try it in the > interpreter. I tried using python 2.7 on Linux, and it does not descend > recursively into subdirectories. > > Perhaps you want os.walk, which lets you loop over an entire tree of > files/directories. > > BTW, you forgot to double the backslash in your Windows example. Either > double the backslash, or make it a raw string, or use forward slashes. > > > -- > > DaveA > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- A non-text attachment was scrubbed... Name: trash.py Type: text/x-python Size: 870 bytes Desc: not available URL: From steve at pearwood.info Mon Aug 20 00:19:04 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Aug 2012 08:19:04 +1000 Subject: [Tutor] (no subject) In-Reply-To: <50313E92.4070809@ymail.com> References: <50313E92.4070809@ymail.com> Message-ID: <20120819221904.GA19831@ando> On Sun, Aug 19, 2012 at 08:29:22PM +0100, Selby Rowley Cannon wrote: > OK, I have some code, and it uses glob.glob('*.py') to find all the > python files in the current directory. That works because if you don't specify an absolute directory, the current directory is used. It does not look inside subdirectories. > Just thought i'd ask, would: > > glob.glob('C:\*.py') (Windows), or By the way, that only works by accident. Backslashes are special in Python, and should be escaped like this: 'C:\\*.py' or by using a raw string which turns off backslash interpretation: r'C:\*.py' or most easy of all, take advantage of Windows' feature that it allows you to use either backslashes or forward slashes: 'C:/*.py' In your specific case, it works by accident because \* is interpreted as a literal backslash followed by an asterisk, but if you wrote something like 'C:\n*' hoping to find files that start with the letter "n", you would actually be looking for C : NEWLINE star instead. > glob.glob('/*.py') (*nix) > > find all the python files on the system? Thanks No, because that specifies an absolute directory, but does not look inside subdirectories. You can tell glob to look one, two, three... levels deep: glob.glob('/*.py') glob.glob('/*/*.py') glob.glob('/*/*/*.py') but there's no way to tell it to look all the way down, as far as it takes. For that sort of "recursive glob", you can use a combination of os.walk and the fnmatch module to do the same thing. Or look at the various solutions listed here: http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python -- Steven From amonroe at columbus.rr.com Mon Aug 20 20:19:39 2012 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Mon, 20 Aug 2012 14:19:39 -0400 Subject: [Tutor] geekdad-puzzle-of-the-week-extreme-products Message-ID: <1231022782.20120820141939@columbus.rr.com> People hunting for Python projects to work on might get a kick out of this puzzle: http://www.wired.com/geekdad/2012/08/geekdad-puzzle-of-the-week-extreme-products/ From abasiemeka at gmail.com Mon Aug 20 22:24:21 2012 From: abasiemeka at gmail.com (Osemeka Osuagwu) Date: Mon, 20 Aug 2012 21:24:21 +0100 Subject: [Tutor] Error in apparently correct code Message-ID: Dear Pythonistas (I hope you don't mind that), I wrote the following code to find the four adjacent numbers (in any direction in the array) which, when multiplied together, would yield the highest product to be gotten. I finally stumbled on the answer while studying the data itself but I'm still stumped as to why I keep getting an error while running the code. I would like answers to this and also any suggestions as to more elegant ways of solving this problem. THE CODE data = [[ 8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65], [52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91], [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80], [24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50], [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70], [67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21], [24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72], [21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95], [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92], [16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57], [86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58], [19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40], [04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66], [88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69], [04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36], [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16], [20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54], [01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48]] def down_right_product(row, col): if (row>len(data)-3) or (col>len(data[row])-3): return 1 else: return reduce(lambda x,y:x*y, [data[row+each][col+each] for each in range(4)]) def down_left_product(row, col): if (row>len(data)-3) or (col<3): return 1 else: return reduce(lambda x,y:x*y, [data[row-each][col-each] for each in range(4)]) def right_product(row, col): if col>len(data[row])-3: return 0 else: return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) def down_product(row, col): if row>len(data)-3: return 1 else: return reduce(lambda x,y:x*y, [data[i][col] for i in range(row, row+4)]) def main(): prod = 1 source = [] for row in range(len(data)): for col in range(len(data[row])): if right_product(row, col) > prod: prod = right_product(row, col) source = [data[row][i] for i in range(col, col+4)] if down_product(row, col) > prod: prod = down_product(row, col) source = [data[i][col] for i in range(row, row+4)] if down_right_product(row, col) > prod: prod = down_right_product(row, col) source = [data[row+each][col+each] for each in range(4)] if down_left_product(row, col) > prod: prod = down_left_product(row, col) source = [data[row-each][col-each] for each in range(4)] print 'Maximum product is %d from %l' %(prod, source) if __name__ == '__main__': main() THE ERROR Traceback (most recent call last): File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE University\Python\Python Code\MyCode\Project Euler code\Project Euler answer 11.py", line 84, in main() File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE University\Python\Python Code\MyCode\Project Euler code\Project Euler answer 11.py", line 64, in main if right_product(row, col) > prod: File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE University\Python\Python Code\MyCode\Project Euler code\Project Euler answer 11.py", line 49, in right_product return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) IndexError: list index out of range p.s I apologise for the lack of comments Regards, Abasiemeka -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Aug 20 23:06:56 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 20 Aug 2012 22:06:56 +0100 Subject: [Tutor] Error in apparently correct code In-Reply-To: References: Message-ID: On 20/08/2012 21:24, Osemeka Osuagwu wrote: > Dear Pythonistas (I hope you don't mind that), > I wrote the following code to find the four adjacent numbers (in any > direction in the array) which, when multiplied together, would yield the I can't see a Python array anywhere. Do you mean the list of lists? :) > highest product to be gotten. I finally stumbled on the answer while > studying the data itself but I'm still stumped as to why I keep getting an > error while running the code. I would like answers to this and also any > suggestions as to more elegant ways of solving this problem. > > THE CODE > [snipped as it's been mangled, was it posted in html and not plain text or what? I'm too lazy to unmangle it, others might be kinder] > > THE ERROR > > > Traceback (most recent call last): > > File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE > University\Python\Python Code\MyCode\Project Euler code\Project Euler > answer 11.py", line 84, in > > main() > > File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE > University\Python\Python Code\MyCode\Project Euler code\Project Euler > answer 11.py", line 64, in main > > if right_product(row, col) > prod: > > File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE > University\Python\Python Code\MyCode\Project Euler code\Project Euler > answer 11.py", line 49, in right_product > > return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) > > IndexError: list index out of range > > > p.s I apologise for the lack of comments > > Regards, > > Abasiemeka > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The usual way to chase a problem like this is to place print statements in appropriate places in your code. The for loops in your main function look like a good starting point to me. Please try that and see what you come up with. -- Cheers. Mark Lawrence. From robert.day at merton.oxon.org Mon Aug 20 23:06:12 2012 From: robert.day at merton.oxon.org (Rob Day) Date: Mon, 20 Aug 2012 22:06:12 +0100 Subject: [Tutor] Error in apparently correct code In-Reply-To: References: Message-ID: Your problem is in this line, as the traceback shows: > return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) > > So the thing you have to think about is - what values can col be? The answer is then in this line: for col in range(len(data[row])) And since each row of your data has 20 numbers in, col ranges between 0 and 19. What happens when col is 19 and you try to do 'return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)])'? i will vary between 19 and 22, and so at some point you'll be trying to pick the 21st, 22nd and 23rd elements of a list of 20 numbers. Obviously you can't do that - so you get a 'list index out of range'. -- Robert K. Day robert.day at merton.oxon.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Aug 20 23:27:44 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 20 Aug 2012 22:27:44 +0100 Subject: [Tutor] Error in apparently correct code In-Reply-To: References: Message-ID: On 20/08/2012 22:06, Rob Day wrote: > Your problem is in this line, as the traceback shows: > >> return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) >> >> > So the thing you have to think about is - what values can col be? The > answer is then in this line: > > for col in range(len(data[row])) > > And since each row of your data has 20 numbers in, col ranges between 0 and > 19. > > What happens when col is 19 and you try to do 'return reduce(lambda > x,y:x*y, [data[row][i] for i in range(col, col+4)])'? i will vary between > 19 and 22, and so at some point you'll be trying to pick the 21st, 22nd and > 23rd elements of a list of 20 numbers. Obviously you can't do that - so you > get a 'list index out of range'. > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Give a man a fish, and you feed him for a day; show him how to catch fish, and you feed him for a lifetime. -- Cheers. Mark Lawrence. From thudfoo at gmail.com Tue Aug 21 00:15:01 2012 From: thudfoo at gmail.com (xDog Walker) Date: Mon, 20 Aug 2012 15:15:01 -0700 Subject: [Tutor] Error in apparently correct code In-Reply-To: References: Message-ID: <201208201515.01883.thudfoo@gmail.com> On Monday 2012 August 20 14:27, Mark Lawrence wrote: > Give a man a fish, and you feed him for a day; show him how to catch > fish, and you feed him for a lifetime. > > -- > Cheers. > > Mark Lawrence. Teach a man to steal fish and he will live until he dies. -- Yonder nor sorghum stenches shut ladle gulls stopper torque wet strainers. From steve at pearwood.info Tue Aug 21 05:21:06 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 21 Aug 2012 13:21:06 +1000 Subject: [Tutor] Error in apparently correct code In-Reply-To: References: Message-ID: <5032FEA2.2060905@pearwood.info> On 21/08/12 06:24, Osemeka Osuagwu wrote: [...] > p.s I apologise for the lack of comments Don't apologise for lack of comments, apologise for posting in HTML which causes your code to be mangled, leading indentation stripped, and generally turned into an unreadable mess. Indentation has meaning in Python. When you send email, if your email program (possibly GMail?) mangles the indentation, your code will become invalid, unreadable mess. You need to fix this. Often the easiest way to fix this is to *not* post so-called "rich text", actually HTML. Most mail programs will leave plain text emails alone, but if you send HTML, they will mangle *both* the text and the HTML. (Aside: the HTML also increases the size of the email by about 300-400%.) As a programmer, like any technician or craftsman, your tools make all the difference. Email is a tool. If your tool breaks your code, you need a better tool. -- Steven From abasiemeka at gmail.com Tue Aug 21 07:12:05 2012 From: abasiemeka at gmail.com (Osemeka Osuagwu) Date: Tue, 21 Aug 2012 06:12:05 +0100 Subject: [Tutor] Tutor Digest, Vol 102, Issue 48 In-Reply-To: References: Message-ID: Dear Mark, > I can't see a Python array anywhere. Do you mean the list of lists? :) > >> THE CODE > > [snipped as it's been mangled, was it posted in html and not plain text > or what? I'm too lazy to unmangle it, others might be kinder] > Again, my bad. Here's the code in plain text: ''#------------------------------------------------------------------------------------------------------------------------------- # Name:Project Euler Problem 11 # Purpose:Project Euler 11 # # Author: Abasiemeka # # Created:23-07-2012 # Copyright: (c) Abasiemeka 2012 # Licence: #------------------------------------------------------------------------------------------------------------------------------- data = [[ 8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65], [52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91], [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80], [24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50], [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70], [67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21], [24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72], [21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95], [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92], [16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57], [86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58], [19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40], [04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66], [88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69], [04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36], [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16], [20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54], [01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48]] def down_right_product(row, col): if (row>len(data)-3) or (col>len(data[row])-3): return 1 else: return reduce(lambda x,y:x*y, [data[row+each][col+each] for each in range(4)]) def down_left_product(row, col): if (row>len(data)-3) or (col<3): return 1 else: return reduce(lambda x,y:x*y, [data[row-each][col-each] for each in range(4)]) def right_product(row, col): if col>len(data[row])-3: return 0 else: return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) def down_product(row, col): if row>len(data)-3: return 1 else: return reduce(lambda x,y:x*y, [data[i][col] for i in range(row, row+4)]) def main(): prod = 1 source = [] for row in range(len(data)): for col in range(len(data[row])): #print col, row if right_product(row, col) > prod: prod = right_product(row, col) source = [data[row][i] for i in range(col, col+4)] if down_product(row, col) > prod: prod = down_product(row, col) source = [data[i][col] for i in range(row, row+4)] if down_right_product(row, col) > prod: prod = down_right_product(row, col) source = [data[row+each][col+each] for each in range(4)] if down_left_product(row, col) > prod: prod = down_left_product(row, col) source = [data[row-each][col-each] for each in range(4)] print 'Maximum product is %d from %l' %(prod, source) if __name__ == '__main__': main() THE ERROR >> >> >> Traceback (most recent call last): >> >> File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE >> University\Python\Python Code\MyCode\Project Euler code\Project Euler >> answer 11.py", line 84, in >> >> main() >> >> File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE >> University\Python\Python Code\MyCode\Project Euler code\Project Euler >> answer 11.py", line 64, in main >> >> if right_product(row, col) > prod: >> >> File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE >> University\Python\Python Code\MyCode\Project Euler code\Project Euler >> answer 11.py", line 49, in right_product >> >> return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)]) >> >> IndexError: list index out of range >> >> > > The usual way to chase a problem like this is to place print statements > in appropriate places in your code. The for loops in your main function > look like a good starting point to me. Please try that and see what you > come up with. > > -- > Cheers. > > Mark Lawrence. I had inserted the print statements (now commented out in the main() function) and found that the error occurs when it attempts to do right_product(row, col) for (row = 0, col = 18) I had anticipated this and added the "if col>len(data[row])-3:" statement in the right_product(row, col) function. It appears this statement was skipped for some puzzling reason. So the question is why did it not execute the if statement? And if it did why did it not return rather than try to execute the else line (which gave the error). Thanks for your input, Abasiemeka From crawlzone at gmail.com Tue Aug 21 07:42:01 2012 From: crawlzone at gmail.com (Ray Jones) Date: Mon, 20 Aug 2012 22:42:01 -0700 Subject: [Tutor] Escaping globs for glob.iglob() Message-ID: <50331FA9.2020502@gmail.com> The code: curDir = os.getcwd() znDir = shutil.abspath('../') baseDir = shutil.abspath('../../') Files = glob.iglob(os.path.join(znDir, '*')) print Files for moveFile in Files: print moveFile shutil.move(moveFile, curDir) Nothing happens. The 'print...moveFile' never happens, even though print Files shows it to be an iglob generator object. After reading over the glob docs and working the directories backward manually, I realized that one of the directory names in the znDir path begins with '[1st]'. According to the docs, glob is apparently seeing that as a character range. No problem, I'll simply escape the '[' and ']' with backslashes. I used: znDir = znDir.replace('[', '\[') and then znDir = znDir.replace(']', '\]') No such luck. Glob still does not recognize the file glob in znDir/*. Later in the code I will be using creating symbolic links, and I wish to use the absolute path rather than the relative path. Any idea of how to sanitize this path so that I can get glob to work? Ray From __peter__ at web.de Tue Aug 21 08:05:03 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 21 Aug 2012 08:05:03 +0200 Subject: [Tutor] Escaping globs for glob.iglob() References: <50331FA9.2020502@gmail.com> Message-ID: Ray Jones wrote: > The code: > > curDir = os.getcwd() > znDir = shutil.abspath('../') > baseDir = shutil.abspath('../../') > > Files = glob.iglob(os.path.join(znDir, '*')) > print Files > > for moveFile in Files: > print moveFile > shutil.move(moveFile, curDir) > > Nothing happens. The 'print...moveFile' never happens, even though print > Files shows it to be an iglob generator object. > > After reading over the glob docs and working the directories backward > manually, I realized that one of the directory names in the znDir path > begins with '[1st]'. According to the docs, glob is apparently seeing > that as a character range. No problem, I'll simply escape the '[' and > ']' with backslashes. I used: > > znDir = znDir.replace('[', '\[') > and then > znDir = znDir.replace(']', '\]') > > No such luck. Glob still does not recognize the file glob in znDir/*. > Later in the code I will be using creating symbolic links, and I wish to > use the absolute path rather than the relative path. > > Any idea of how to sanitize this path so that I can get glob to work? Try znDir = znDir.replace("[", "[[]") Alternatively use os.listdir(znDir): [os.path.join(znDir, name) for name in os.listdir(znDir)] For a non-trivial file pattern: pattern = "*tmp*.py" files = [os.path.join(znDir, name) for name in fnmatch.filter(os.listdir(znDir), pattern)] From crawlzone at gmail.com Tue Aug 21 08:15:30 2012 From: crawlzone at gmail.com (Ray Jones) Date: Mon, 20 Aug 2012 23:15:30 -0700 Subject: [Tutor] Escaping globs for glob.iglob() In-Reply-To: References: <50331FA9.2020502@gmail.com> Message-ID: <50332782.8000204@gmail.com> On 08/20/2012 11:05 PM, Peter Otten wrote: > Ray Jones wrote: > >> The code: >> >> curDir = os.getcwd() >> znDir = shutil.abspath('../') >> baseDir = shutil.abspath('../../') >> >> Files = glob.iglob(os.path.join(znDir, '*')) >> print Files >> >> for moveFile in Files: >> print moveFile >> shutil.move(moveFile, curDir) >> >> Nothing happens. The 'print...moveFile' never happens, even though print >> Files shows it to be an iglob generator object. >> >> After reading over the glob docs and working the directories backward >> manually, I realized that one of the directory names in the znDir path >> begins with '[1st]'. According to the docs, glob is apparently seeing >> that as a character range. No problem, I'll simply escape the '[' and >> ']' with backslashes. I used: >> >> znDir = znDir.replace('[', '\[') >> and then >> znDir = znDir.replace(']', '\]') >> >> No such luck. Glob still does not recognize the file glob in znDir/*. >> Later in the code I will be using creating symbolic links, and I wish to >> use the absolute path rather than the relative path. >> >> Any idea of how to sanitize this path so that I can get glob to work? > Try > > znDir = znDir.replace("[", "[[]") That never occurred to me. Excellent. > Alternatively use os.listdir(znDir): > > [os.path.join(znDir, name) for name in os.listdir(znDir)] I've actually used this similarly in the past. I don't know why I got hung up on glob! ;) > For a non-trivial file pattern: > > pattern = "*tmp*.py" > files = [os.path.join(znDir, name) for name in > fnmatch.filter(os.listdir(znDir), pattern)] I looked at fnmatch.filter and didn't see how I could use it in this instance - it's crystal clear now. Thanks for the help. I think it's going to work now! Ray From emile at fenx.com Tue Aug 21 08:18:42 2012 From: emile at fenx.com (Emile van Sebille) Date: Mon, 20 Aug 2012 23:18:42 -0700 Subject: [Tutor] Escaping globs for glob.iglob() In-Reply-To: <50331FA9.2020502@gmail.com> References: <50331FA9.2020502@gmail.com> Message-ID: On 8/20/2012 10:42 PM Ray Jones said... > Nothing happens. >plugh!< :) From alan.gauld at btinternet.com Tue Aug 21 09:52:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Aug 2012 08:52:25 +0100 Subject: [Tutor] Tutor Digest, Vol 102, Issue 48 In-Reply-To: References: Message-ID: On 21/08/12 06:12, Osemeka Osuagwu wrote: > right_product(row, col) for (row = 0, col = 18) I had anticipated this > and added the "if col>len(data[row])-3:" statement But because of zero indexing you need this to be either: if col >= len(data[row])-3: or if col > len(data[row])-4: ie. if row length is 10 the indexes go from 0-9 so the highest index you can use is 6. However you have similar problems in other places, such as your main function. Consider: for row in range(len(data)): for col in range(len(data[row])): if right_product(row, col) > prod: source = [data[row][i] for i in range(col, col+4)] what happens when col gets bigger than len(row)-4? if down_right_product(row, col) > prod: source = [data[row+each][col+each] for each in range(4)] And this is even worse because both additions could blow up. It's probably better if you can fix it in the for loops rather than putting if guards in. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Aug 21 09:59:32 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Aug 2012 08:59:32 +0100 Subject: [Tutor] Escaping globs for glob.iglob() In-Reply-To: <50331FA9.2020502@gmail.com> References: <50331FA9.2020502@gmail.com> Message-ID: On 21/08/12 06:42, Ray Jones wrote: > Files = glob.iglob(os.path.join(znDir, '*')) > print Files > > for moveFile in Files: > print moveFile > > Nothing happens. The 'print...moveFile' never happens, even though print > Files shows it to be an iglob generator object. Good but does it contain anything? If not the loop will never execute. Did you try plain glob() which returns a list? Easier to see what the result set looks like. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From crawlzone at gmail.com Tue Aug 21 10:53:07 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 21 Aug 2012 01:53:07 -0700 Subject: [Tutor] Escaping globs for glob.iglob() In-Reply-To: References: <50331FA9.2020502@gmail.com> Message-ID: <50334C73.3090006@gmail.com> On 08/21/2012 12:59 AM, Alan Gauld wrote: > On 21/08/12 06:42, Ray Jones wrote: > >> Files = glob.iglob(os.path.join(znDir, '*')) >> print Files >> >> for moveFile in Files: >> print moveFile >> >> Nothing happens. The 'print...moveFile' never happens, even though print >> Files shows it to be an iglob generator object. > > Good but does it contain anything? > If not the loop will never execute. > Did you try plain glob() which returns a list? Easier to see what the > result set looks like. > Yes, I did try glob in the shell. It returned '[]' with the absolute path or any relative path that included the directory with the '[1st]' in it...which is what drew my focus to that portion of the path name to begin with. cheers. Ray From eryksun at gmail.com Tue Aug 21 12:25:51 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 21 Aug 2012 06:25:51 -0400 Subject: [Tutor] Error in apparently correct code In-Reply-To: <5032FEA2.2060905@pearwood.info> References: <5032FEA2.2060905@pearwood.info> Message-ID: On Mon, Aug 20, 2012 at 11:21 PM, Steven D'Aprano wrote: > > Indentation has meaning in Python. When you send email, if your email > program (possibly GMail?) mangles the indentation, your code will become > invalid, unreadable mess. You need to fix this. Often the easiest way to fix > this is to *not* post so-called "rich text", actually HTML. Most mail > programs will leave plain text emails alone, but if you send HTML, they will > mangle *both* the text and the HTML. Gmail's editor for plain text will hard wrap *some* lines at about 69 characters, but not rigidly (it seems to leave code untouched). There's no live preview (the textarea wraps at the window width). You just click "Send" and hope it doesn't mangle your formatting. Also, the interface displays plain text mail with a proportional font. Depending on your browser you can hack the CSS to use a monospace font. I use Stylish in Firefox: http://userstyles.org/styles/15618/gmail-monospace-font-for-body-messages-textarea From akarsh.iirs at gmail.com Tue Aug 21 14:29:17 2012 From: akarsh.iirs at gmail.com (akarsh a) Date: Tue, 21 Aug 2012 17:59:17 +0530 Subject: [Tutor] Python doubt Message-ID: sir, How to read an excel file row by row and write each row to different text files? ie, if i have an excel file consists of m raw and n columns as follws 1 as mmm 15 25 cv 10.2 2 xv fvbgd 14 11 dc 12.5 .................................................. I want to read each line and write it to separate text file ie, a.txt, b.txt,..............m is there any python code for this purpose . please explain me the python modules required to serve this purpose and code because i am new to python ...... with regards Akarsh -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue Aug 21 14:49:02 2012 From: wprins at gmail.com (Walter Prins) Date: Tue, 21 Aug 2012 13:49:02 +0100 Subject: [Tutor] Python doubt In-Reply-To: References: Message-ID: Hello Akarsh, Welcome to the Python tutor list. On 21 August 2012 13:29, akarsh a wrote: > sir, > How to read an excel file row by row and write each row to different > text files? > ie, if i have an excel file consists of m raw and n columns as follws > 1 as mmm 15 25 cv 10.2 > 2 xv fvbgd 14 11 dc 12.5 > .................................................. > I want to read each line and write it to separate text file ie, a.txt, > b.txt,..............m > is there any python code for this purpose . please explain me the python > modules required to serve this purpose and code because i am new to python > ...... 1) You can read .xls files using the "xlrd" module. It is not included by default with Python but you can install it easily enough on your Python installation. The preferred method to do this will vary by operating system. What operating system are you using? You can find out more about the xlrd module here: http://pypi.python.org/pypi/xlrd/ Note there's a parallel module for writing named "xlwt". 2) You can write text files using the simple file I/O functions e.g. newLine = 'blah blah blah' file = open("datafile.txt", "w") file.write(newLine) file.close() etc. You may also want to look at the "csv" module, for example: import csv somedata = ["value %d" % i for i in range(1,4)] fileobj = open("datafile.csv","w") csvwriter = csv.writer(fileobj, delimiter=',',quoting=csv.QUOTE_ALL) csvwriter.writerow(somedata) HTH, Walter From norman at khine.net Tue Aug 21 15:43:47 2012 From: norman at khine.net (Norman Khine) Date: Tue, 21 Aug 2012 14:43:47 +0100 Subject: [Tutor] extract date and time from string Message-ID: Hello, When I try this, I get the following error: ? python * master 697cedfitools" Python 2.7.2 (default, Jan 28 2012, 14:53:22) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import dateutil.parser as dparser >>> test = '2009/05/25 00:54:45' >>> dparser.parse(test, fuzzy=True) Traceback (most recent call last): File "", line 1, in File "/Users/khinester/.virtualenvs/itools/lib/python2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.5-intel.egg/dateutil/parser.py", line 697, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "/Users/khinester/.virtualenvs/itools/lib/python2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.5-intel.egg/dateutil/parser.py", line 303, in parse raise ValueError, "unknown string format" ValueError: unknown string format i basically have a list, like: TABLE_CONTENT = [['958','','USA','Atmospheric','1945/07/16 11:29:45','33.6753','-106.4747','','-.03','21','','','TRINITY',' ',' ','SourceData',' '],['959','','USA','Atmospheric','1945/08/05 23:16:02','34.395','132.4538','','-.58','15','','','LITTLEBOY',' ',' ','SourceData',' '] ..... ] from which i want to extract the date and time any advice much appreciated. -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From norman at khine.net Tue Aug 21 16:23:03 2012 From: norman at khine.net (Norman Khine) Date: Tue, 21 Aug 2012 15:23:03 +0100 Subject: [Tutor] extract date and time from string In-Reply-To: References: Message-ID: ok, i figured it out: >>> from BeautifulSoup import BeautifulSoup >>> for EVENT in TABLE_CONTENT: ... for index, item in enumerate(EVENT): ... if index == 4: ... soup = BeautifulSoup(item) ... for a in soup.findAll('a'): ... print ''.join(a.findAll(text=True)) ... else: ... pass ... print '=== new record ===' ... 1945/07/16 11:29:45 === new record === 1945/08/05 23:16:02 === new record === On Tue, Aug 21, 2012 at 2:43 PM, Norman Khine wrote: > Hello, > When I try this, I get the following error: > > > ? python > > * master 697cedfitools" > Python 2.7.2 (default, Jan 28 2012, 14:53:22) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. >>>> import dateutil.parser as dparser >>>> test = '2009/05/25 00:54:45' >>>> dparser.parse(test, fuzzy=True) > Traceback (most recent call last): > File "", line 1, in > File "/Users/khinester/.virtualenvs/itools/lib/python2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.5-intel.egg/dateutil/parser.py", > line 697, in parse > return DEFAULTPARSER.parse(timestr, **kwargs) > File "/Users/khinester/.virtualenvs/itools/lib/python2.7/site-packages/matplotlib-1.1.0-py2.7-macosx-10.5-intel.egg/dateutil/parser.py", > line 303, in parse > raise ValueError, "unknown string format" > ValueError: unknown string format > > > i basically have a list, like: > > > TABLE_CONTENT = [['958',' title="go to map"> border="0">','USA','Atmospheric',' href="javascript:c_ol(\'958\')" title="click date time to show > origin_list (evid=958)">1945/07/16 > 11:29:45','33.6753','-106.4747','','-.03','21','','','TRINITY',' ',' ',' href="javascript:c_md(\'958\')" title="click here to show source > data">SourceData',' '],['959',' href="javascript:c_row(\'959\')" title="go to map"> src="/images/c_map.png" border="0">','USA','Atmospheric',' href="javascript:c_ol(\'959\')" title="click date time to show > origin_list (evid=959)">1945/08/05 > 23:16:02','34.395','132.4538','','-.58','15','','','LITTLEBOY',' ',' ',' href="javascript:c_md(\'959\')" title="click here to show source > data">SourceData',' '] ..... ] > > from which i want to extract the date and time > > any advice much appreciated. > > > -- > %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or > chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From crawlzone at gmail.com Wed Aug 22 03:39:24 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 21 Aug 2012 18:39:24 -0700 Subject: [Tutor] subprocess.Popen help Message-ID: <5034384C.9080808@gmail.com> Does anyone know of a link to a really good tutorial that would help me with subprocess.Popen? a tutorial that uses really small words and more examples than explanation? After 15 years of scripting, I'm ashamed to say that I'm still not all that familiar with input, output, pipes, etc. much beyond a simple 'ls | ws -l' or &2>/dev/null scenarios. The docs for Popen have left me completely boggled, and I'm not seeing much available on Google search. Any suggestions? Thanks. Ray From pedrooconnell at gmail.com Wed Aug 22 08:05:24 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Wed, 22 Aug 2012 18:05:24 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions Message-ID: Hi I am trying to parse a text file and create a list of all the lines that don't include: "vn", "vt" or are empty. I want to make this as fast as possible because I will be parsing many files each containing thousands of lines. I though I would give list comprehensions a try. The last 3 lines of the code below have three list comprehensions that I would like to combine into 1 but I am not sure how to do that. Any tips would be greatly appreciated pete #start############################################################ fileName = '/usr/home/poconnell/Desktop/objCube.obj' theFileOpened = open(fileName,'r') theTextAsList = theFileOpened.readlines() theTextAsListStripped = [] for aLine in theTextAsList: theTextAsListStripped.append(aLine.strip("\n")) theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""] #end#################################################################### and here is the simple file I am parsing as a test: #start########################################################################################################## ## OBJ file generated by Nuke ## # vertex list - offset=0 v 0.000000 0.000000 1.000000 v 1.000000 0.000000 1.000000 v 0.000000 1.000000 1.000000 v 1.000000 1.000000 1.000000 v 1.000000 0.000000 0.000000 v 0.000000 0.000000 0.000000 v 1.000000 1.000000 0.000000 v 0.000000 1.000000 0.000000 v 1.000000 0.000000 1.000000 v 1.000000 0.000000 0.000000 v 1.000000 1.000000 1.000000 v 1.000000 1.000000 0.000000 v 0.000000 0.000000 0.000000 v 0.000000 0.000000 1.000000 v 0.000000 1.000000 0.000000 v 0.000000 1.000000 1.000000 v 0.000000 1.000000 1.000000 v 1.000000 1.000000 1.000000 v 0.000000 1.000000 0.000000 v 1.000000 1.000000 0.000000 v 0.000000 0.000000 0.000000 v 1.000000 0.000000 0.000000 v 0.000000 0.000000 1.000000 v 1.000000 0.000000 1.000000 # point texture coordinates - offset=0 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 1.000000 1.000000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 1.000000 1.000000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 1.000000 1.000000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 1.000000 1.000000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 1.000000 1.000000 vt 0.000000 0.000000 vt 1.000000 0.000000 vt 0.000000 1.000000 vt 1.000000 1.000000 # vertex normals - offset=0 vn 0.000000 0.000000 1.000000 vn 0.000000 0.000000 1.000000 vn 0.000000 0.000000 1.000000 vn 0.000000 0.000000 1.000000 vn 0.000000 0.000000 -1.000000 vn 0.000000 0.000000 -1.000000 vn 0.000000 0.000000 -1.000000 vn 0.000000 0.000000 -1.000000 vn 1.000000 0.000000 0.000000 vn 1.000000 0.000000 0.000000 vn 1.000000 0.000000 0.000000 vn 1.000000 0.000000 0.000000 vn -1.000000 0.000000 0.000000 vn -1.000000 0.000000 0.000000 vn -1.000000 0.000000 0.000000 vn -1.000000 0.000000 0.000000 vn 0.000000 1.000000 0.000000 vn 0.000000 1.000000 0.000000 vn 0.000000 1.000000 0.000000 vn 0.000000 1.000000 0.000000 vn 0.000000 -1.000000 0.000000 vn 0.000000 -1.000000 0.000000 vn 0.000000 -1.000000 0.000000 vn 0.000000 -1.000000 0.000000 f 1/1/1 2/2/2 4/4/3 3/3/4 f 5/5/5 6/6/6 8/8/7 7/7/8 f 9/9/9 10/10/10 12/12/11 11/11/12 f 13/13/13 14/14/14 16/16/15 15/15/16 f 17/17/17 18/18/18 20/20/19 19/19/20 f 21/21/21 22/22/22 24/24/23 23/23/24 # end of file From johann.spies at gmail.com Wed Aug 22 08:29:43 2012 From: johann.spies at gmail.com (Johann Spies) Date: Wed, 22 Aug 2012 08:29:43 +0200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: #start############################################################ import re exclude = re.compile('vn|vt|^$|^#') fileName = '/tmp/x' theFileOpened = open(fileName,'r') theTextAsList = theFileOpened.readlines() theTextAsListStripped = [] for aLine in theTextAsList: theTextAsListStripped.append(aLine.strip("\n")) theTextAsListNoVn = [x for x in theTextAsListStripped if not re.search(exclude,x)] print theTextAsListNoVn Regards Johann -- Because experiencing your loyal love is better than life itself, my lips will praise you. (Psalm 63:3) -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Aug 22 09:06:11 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 22 Aug 2012 09:06:11 +0200 Subject: [Tutor] list comprehension, testing for multiple conditions References: Message-ID: Pete O'Connell wrote: > Hi I am trying to parse a text file and create a list of all the lines > that don't include: "vn", "vt" or are empty. I want to make this as > fast as possible because I will be parsing many files each containing > thousands of lines. I though I would give list comprehensions a try. > The last 3 lines of the code below have three list comprehensions that > I would like to combine into 1 but I am not sure how to do that. > Any tips would be greatly appreciated > > pete > > #start############################################################ > fileName = '/usr/home/poconnell/Desktop/objCube.obj' > theFileOpened = open(fileName,'r') > theTextAsList = theFileOpened.readlines() If you have a file with 1,000,000 lines you have now a list of 1,000,000 strings of which perhaps 1,000 match your criteria. You are squandering memory. Rule of thumb: never use readlines(), iterate over the file directly. > theTextAsListStripped = [] > for aLine in theTextAsList: > > theTextAsListStripped.append(aLine.strip("\n")) > > theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] > theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != > ""] I think that should be theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x != ""] You can combine the three if clauses or add them all to one list-comp: with open(filename) as lines: wanted = [line.strip("\n") for line in lines if "vn" not in line and "vt" not in line and line != "\n"] You can even have multiple if clauses in one list-comp (but that is rarely used): with open(filename) as lines: wanted = [line.strip("\n") for line if "vn" not in line if "vt" not in x if line != "\n"] While your problem is simple enough to combine all filters into one list- comp some problems are not. You can then prevent the intermediate lists from materializing by using generator expressions. The result minimizes memory consumption, too, and should be (almost) as fast. For example: with open(filename) as lines: # use gen-exps to remove empty and whitespace-only lines stripped = (line.strip() for line in lines) nonempty = (line for line in stripped if line) wanted = [line for line in nonempty if "vt" not in line and "vn" not in line] From alan.gauld at btinternet.com Wed Aug 22 09:36:56 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Aug 2012 08:36:56 +0100 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: On 22/08/12 08:06, Peter Otten wrote: > You can even have multiple if clauses in one list-comp (but that is rarely > used): > > with open(filename) as lines: > wanted = [line.strip("\n") for line > if "vn" not in line > if "vt" not in x > if line != "\n"] > > While your problem is simple enough to combine all filters into one list- > comp some problems are not. You can then prevent the intermediate lists from > materializing by using generator expressions. The result minimizes memory > consumption, too, and should be (almost) as fast. For example: > > with open(filename) as lines: > # use gen-exps to remove empty and whitespace-only lines > stripped = (line.strip() for line in lines) > nonempty = (line for line in stripped if line) > > wanted = [line for line in nonempty > if "vt" not in line and "vn" not in line] Another option using generators is to roll your own. This would be my recomendation for really complex filtering: def myFilterGenerator(aFile): for line in aFile: if 'vn' not in line: # demo "complexity" not efficiency! if 'vt' not in line: if '\n' != line: yield line.strip() with open filename as myFile: result = [line for line in myFilterGenerator(myFile)] But in this specific case the filter is simple enough that the list comp from Peter is probably the best solution. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Wed Aug 22 09:39:39 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Aug 2012 08:39:39 +0100 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: On 22/08/2012 07:29, Johann Spies wrote: > #start############################################################ > import re > exclude = re.compile('vn|vt|^$|^#') > fileName = '/tmp/x' > theFileOpened = open(fileName,'r') > theTextAsList = theFileOpened.readlines() > > theTextAsListStripped = [] > for aLine in theTextAsList: > > theTextAsListStripped.append(aLine.strip("\n")) > > theTextAsListNoVn = [x for x in theTextAsListStripped if not > re.search(exclude,x)] > > print theTextAsListNoVn > > > > Regards > Johann > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Please no, not a regex for something this simple!!! -- Cheers. Mark Lawrence. From andipersti at gmail.com Wed Aug 22 09:59:15 2012 From: andipersti at gmail.com (Andreas Perstinger) Date: Wed, 22 Aug 2012 09:59:15 +0200 Subject: [Tutor] subprocess.Popen help In-Reply-To: <5034384C.9080808@gmail.com> References: <5034384C.9080808@gmail.com> Message-ID: <50349153.1040901@gmail.com> On 22.08.2012 03:39, Ray Jones wrote: > Does anyone know of a link to a really good tutorial that would help me > with subprocess.Popen? a tutorial that uses really small words and more > examples than explanation? After 15 years of scripting, I'm ashamed to > say that I'm still not all that familiar with input, output, pipes, etc. > much beyond a simple 'ls | ws -l' or &2>/dev/null scenarios. The > docs for Popen have left me completely boggled, and I'm not seeing much > available on Google search. Any suggestions? What about this tutorial: http://jimmyg.org/blog/2009/working-with-python-subprocess.html or Doug Hellmann's PyMOTW page about subprocess: http://www.doughellmann.com/PyMOTW/subprocess/index.html Bye, Andreas From david at pythontoo.com Wed Aug 22 10:37:24 2012 From: david at pythontoo.com (David Abbott) Date: Wed, 22 Aug 2012 04:37:24 -0400 Subject: [Tutor] subprocess.Popen help In-Reply-To: <5034384C.9080808@gmail.com> References: <5034384C.9080808@gmail.com> Message-ID: On Tue, Aug 21, 2012 at 9:39 PM, Ray Jones wrote: > > Does anyone know of a link to a really good tutorial that would help me > with subprocess.Popen? a tutorial that uses really small words and more > examples than explanation? After 15 years of scripting, I'm ashamed to > say that I'm still not all that familiar with input, output, pipes, etc. > much beyond a simple 'ls | ws -l' or &2>/dev/null scenarios. The > docs for Popen have left me completely boggled, and I'm not seeing much > available on Google search. Any suggestions? > > Thanks. > > > Ray Hi Ray, http://www.doughellmann.com/PyMOTW/subprocess/ David From pedrooconnell at gmail.com Wed Aug 22 11:51:07 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Wed, 22 Aug 2012 21:51:07 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: What a great mailing list! Thanks for all the responses. I have a few questions, though, first in regards to Puneeth's code. He writes to use: >theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" not in x and "vt" not in x and x!= ""] It works but what I don't understand about this line is why the ands are nor ors ("or" doesn't work even though I would have expected it to) I am sure I will have a few more questions over the next couple days as I work my way through the responses. Thanks Pete On Wed, Aug 22, 2012 at 6:23 PM, Puneeth Chaganti wrote: > On Wed, Aug 22, 2012 at 11:35 AM, Pete O'Connell > wrote: >> Hi I am trying to parse a text file and create a list of all the lines >> that don't include: "vn", "vt" or are empty. I want to make this as >> fast as possible because I will be parsing many files each containing >> thousands of lines. I though I would give list comprehensions a try. >> The last 3 lines of the code below have three list comprehensions that >> I would like to combine into 1 but I am not sure how to do that. >> Any tips would be greatly appreciated >> >> pete >> >> #start############################################################ >> fileName = '/usr/home/poconnell/Desktop/objCube.obj' >> theFileOpened = open(fileName,'r') >> theTextAsList = theFileOpened.readlines() >> >> theTextAsListStripped = [] >> for aLine in theTextAsList: >> >> theTextAsListStripped.append(aLine.strip("\n")) >> >> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] >> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != ""] > > Something like this should work : > > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped > if "vn" not in x and "vt" not in x and x!= ""] > > HTH, > Puneeth -- - From pedrooconnell at gmail.com Wed Aug 22 11:59:46 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Wed, 22 Aug 2012 21:59:46 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: Thanks Peter. This looks like what I need: with open(fileName) as lines: wanted = [line.strip("\n") for line in lines if "vn" not in line and "vt" not in line and line != "\n"] Cheers And in response to Allan's suggestion. I can see using a generator in a situation where the if statements were more numerous and complex. I am sure that will come in handy. Thanks On Wed, Aug 22, 2012 at 7:06 PM, Peter Otten <__peter__ at web.de> wrote: > Pete O'Connell wrote: > >> Hi I am trying to parse a text file and create a list of all the lines >> that don't include: "vn", "vt" or are empty. I want to make this as >> fast as possible because I will be parsing many files each containing >> thousands of lines. I though I would give list comprehensions a try. >> The last 3 lines of the code below have three list comprehensions that >> I would like to combine into 1 but I am not sure how to do that. >> Any tips would be greatly appreciated >> >> pete >> >> #start############################################################ >> fileName = '/usr/home/poconnell/Desktop/objCube.obj' >> theFileOpened = open(fileName,'r') >> theTextAsList = theFileOpened.readlines() > > If you have a file with 1,000,000 lines you have now a list of 1,000,000 > strings of which perhaps 1,000 match your criteria. You are squandering > memory. Rule of thumb: never use readlines(), iterate over the file > directly. > >> theTextAsListStripped = [] >> for aLine in theTextAsList: >> >> theTextAsListStripped.append(aLine.strip("\n")) >> >> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] >> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != >> ""] > > I think that should be > > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x != > ""] > > You can combine the three if clauses or add them all to one list-comp: > > with open(filename) as lines: > wanted = [line.strip("\n") for line in lines > if "vn" not in line and "vt" not in line and line != "\n"] > > > You can even have multiple if clauses in one list-comp (but that is rarely > used): > > with open(filename) as lines: > wanted = [line.strip("\n") for line > if "vn" not in line > if "vt" not in x > if line != "\n"] > > While your problem is simple enough to combine all filters into one list- > comp some problems are not. You can then prevent the intermediate lists from > materializing by using generator expressions. The result minimizes memory > consumption, too, and should be (almost) as fast. For example: > > with open(filename) as lines: > # use gen-exps to remove empty and whitespace-only lines > stripped = (line.strip() for line in lines) > nonempty = (line for line in stripped if line) > > wanted = [line for line in nonempty > if "vt" not in line and "vn" not in line] > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - From pedrooconnell at gmail.com Wed Aug 22 12:28:20 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Wed, 22 Aug 2012 22:28:20 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: Hi. The next step for me to parse the file as I want to is to change lines that look like this: f 21/21/21 22/22/22 24/24/23 23/23/24 into lines that look like this: f 21 22 23 24 Below is my terribly slow loop for doing this. Any suggestions about how to make this code more efficient would be greatly appreciated ################################################################################ fileName = '/usr/home/poconnell/Desktop/objCube.obj' with open(fileName) as lines: theGoodLines = [line.strip("\n") for line in lines if "vn" not in line and "vt" not in line and line != "\n"] for i in range(len(theGoodLines)): if theGoodLines[i][0] == "f": aGoodLineAsList = theGoodLines[i].split(" ") theGoodLines[i] = aGoodLineAsList[0] + " " + aGoodLineAsList[1].split("/")[-1] + " " + aGoodLineAsList[2].split("/")[-1] + " " + aGoodLineAsList[3].split("/")[-1] + " " + aGoodLineAsList[4].split("/")[-1] for anItem in theGoodLines: print anItem ################################################################################## Thanks! Pete On Wed, Aug 22, 2012 at 9:59 PM, Pete O'Connell wrote: > Thanks Peter. This looks like what I need: > > with open(fileName) as lines: > wanted = [line.strip("\n") for line in lines if "vn" not in line > and "vt" not in line and line != "\n"] > > Cheers > > And in response to Allan's suggestion. I can see using a generator in > a situation where the if statements were more numerous and complex. I > am sure that will come in handy. > > Thanks > > > On Wed, Aug 22, 2012 at 7:06 PM, Peter Otten <__peter__ at web.de> wrote: >> Pete O'Connell wrote: >> >>> Hi I am trying to parse a text file and create a list of all the lines >>> that don't include: "vn", "vt" or are empty. I want to make this as >>> fast as possible because I will be parsing many files each containing >>> thousands of lines. I though I would give list comprehensions a try. >>> The last 3 lines of the code below have three list comprehensions that >>> I would like to combine into 1 but I am not sure how to do that. >>> Any tips would be greatly appreciated >>> >>> pete >>> >>> #start############################################################ >>> fileName = '/usr/home/poconnell/Desktop/objCube.obj' >>> theFileOpened = open(fileName,'r') >>> theTextAsList = theFileOpened.readlines() >> >> If you have a file with 1,000,000 lines you have now a list of 1,000,000 >> strings of which perhaps 1,000 match your criteria. You are squandering >> memory. Rule of thumb: never use readlines(), iterate over the file >> directly. >> >>> theTextAsListStripped = [] >>> for aLine in theTextAsList: >>> >>> theTextAsListStripped.append(aLine.strip("\n")) >>> >>> theTextAsListNoVn = [x for x in theTextAsListStripped if "vn" not in x] >>> theTextAsListNoVnOrVt = [x for x in theTextAsListNoVn if "vt" not in x] >>> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVn if x != >>> ""] >> >> I think that should be >> >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListNoVnOrVt if x != >> ""] >> >> You can combine the three if clauses or add them all to one list-comp: >> >> with open(filename) as lines: >> wanted = [line.strip("\n") for line in lines >> if "vn" not in line and "vt" not in line and line != "\n"] >> >> >> You can even have multiple if clauses in one list-comp (but that is rarely >> used): >> >> with open(filename) as lines: >> wanted = [line.strip("\n") for line >> if "vn" not in line >> if "vt" not in x >> if line != "\n"] >> >> While your problem is simple enough to combine all filters into one list- >> comp some problems are not. You can then prevent the intermediate lists from >> materializing by using generator expressions. The result minimizes memory >> consumption, too, and should be (almost) as fast. For example: >> >> with open(filename) as lines: >> # use gen-exps to remove empty and whitespace-only lines >> stripped = (line.strip() for line in lines) >> nonempty = (line for line in stripped if line) >> >> wanted = [line for line in nonempty >> if "vt" not in line and "vn" not in line] >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > > -- > - -- - From cecilia.chavana-bryant at ouce.ox.ac.uk Wed Aug 22 12:10:46 2012 From: cecilia.chavana-bryant at ouce.ox.ac.uk (Cecilia Chavana-Bryant) Date: Wed, 22 Aug 2012 10:10:46 +0000 Subject: [Tutor] Hello Python Tutor - help please! Message-ID: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> Dear all, I am just returning to my doctoral studies after a 7-month medical leave and desperately trying to catch up for lost time. I am COMPLETELY new to programming, well, I did try learning C for 3 weeks 3 yrs ago (with very little success) but had to stop and then spent 2 years in the Amazon climbing trees (lots more enjoyable than learning to programme!) and collecting loads of field data that I now need to post-process and analyse. By the way, the 3 weeks I spent trying to learn C really ended up being spent trying to get to grips with using a terminal for the first time in my life. Since getting back to work, I was advised to try learning Python instead of C as it is a much easier first language to learn. I have been trying, but again, to not great success. I started following "A Primer on Scientific programming with Python" but I kept getting lost and stuck, specially on the exercises. I have also been advised that I should not try to learn programming by following guides but by trying to write the programmes I need to analyse my data. Although I can understand the logic behind this last bit of advise (it gives context and direction to the learning process) I have also gotten stuck trying this approach as "I do not know how to programme!". Thus, I was hoping that some of you can remember how you got started and point me towards any really good interactive learning guides/materials and/or have a good learning strategy for a complete beginner. I have searched the web and was overwhelmed by choice of tutorials and guides. I have skimmed through a couple of tutorials but then fail to see how all that relates to my own work and I get stuck with what seems like basic important concepts so I don't progress. I then think I should try to make some progress with my own data analysing and go back to trying to learn to write a programme for my specific needs and get stuck again because this requires more advanced skills then the basic programming concepts I have been reading about on the learning guides. So, I am now feeling VERY frustrated and have no idea what on Earth I am doing! Can anyone please offer guidance in my learning process? I don't know how and what I should be spending my time learning first and/or if I should focus my learning towards the skill areas I will require to write my specific programmes, although I have no idea what these are. I would like advise on finding some really good interactive(let you know if your solution to an exercise is correct or not) and or video tutorials that give you feedback on the solutions you write to exercises. Many thanks in advance for all your help, it will be much appreciated! Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Wed Aug 22 13:16:38 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 22 Aug 2012 07:16:38 -0400 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__peter__ at web.de> wrote: > > wanted = [line.strip("\n") for line in lines > if "vn" not in line and "vt" not in line and line != "\n"] Here's an equivalent expression with the negation factored out: not ("vn" in line or "vt" in line or line == "\n") http://en.wikipedia.org/wiki/De_Morgan%27s_laws If you have a lot of tests all using the same operator (e.g. "in"), you can use "any" (OR) or "all" (AND) with a generator expression: vals = ["vn", "vt", "vu", "vv", "vw", "vx", "vy", "vz"] wanted = [line for line in lines if not any(v in line for v in vals)] From __peter__ at web.de Wed Aug 22 13:25:07 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 22 Aug 2012 13:25:07 +0200 Subject: [Tutor] list comprehension, testing for multiple conditions References: Message-ID: Pete O'Connell wrote: [Please don't to-post. Clip all text of previous posts except the portion relevant to your question] > Hi. The next step for me to parse the file as I want to is to change > lines that look like this: > f 21/21/21 22/22/22 24/24/23 23/23/24 > into lines that look like this: > f 21 22 23 24 > > Below is my terribly slow loop for doing this. I'd say you are not yet at the point where you should care for speed too much. Build the complete tool-chain with tests to verify correctness and then measure execution time to find the bottlenecks. > Any suggestions about > how to make this code more efficient would be greatly appreciated > > ################################################################################ > fileName = '/usr/home/poconnell/Desktop/objCube.obj' > > with open(fileName) as lines: > theGoodLines = [line.strip("\n") for line in lines if "vn" not in > line and "vt" not in line and line != "\n"] > > for i in range(len(theGoodLines)): > if theGoodLines[i][0] == "f": > aGoodLineAsList = theGoodLines[i].split(" ") > theGoodLines[i] = aGoodLineAsList[0] + " " + > aGoodLineAsList[1].split("/")[-1] + " " + > aGoodLineAsList[2].split("/")[-1] + " " + > aGoodLineAsList[3].split("/")[-1] + " " + > aGoodLineAsList[4].split("/")[-1] > > for anItem in theGoodLines: > print anItem If your sample data is representative you don't need most of the filtering: fileName = ... with open(fileName) as lines: for line in lines: if line.startswith("f "): print " ".join(part.rpartition("/")[-1] for part in line.split()) From mariocatch at gmail.com Wed Aug 22 14:26:57 2012 From: mariocatch at gmail.com (Mario Cacciatore) Date: Wed, 22 Aug 2012 05:26:57 -0700 Subject: [Tutor] Hello Python Tutor - help please! Message-ID: <-4981166029954119215@unknownmsgid> Hello, My highest recommendation for you is to start with a simple hello world program. Study that program, each line. Think about how and most importantly, why it works. Then, extend on it. Make it write to a file instead of a terminal. Then make it read from a file and print to the terminal. Then make it print one letter to each file, then read them back in and reconstruct the sentence. Just take it slow, and one step at a time. So many times people simply go for complex solutions and get lost in the complexity. Sorry for top posting. My phone client doesn't support inline replies. -Mario ------------------------------ From: Cecilia Chavana-Bryant Sent: 8/22/2012 6:35 AM To: tutor at python.org Subject: [Tutor] Hello Python Tutor - help please! Dear all, I am just returning to my doctoral studies after a 7-month medical leave and desperately trying to catch up for lost time. I am COMPLETELY new to programming, well, I did try learning C for 3 weeks 3 yrs ago (with very little success) but had to stop and then spent 2 years in the Amazon climbing trees (lots more enjoyable than learning to programme!) and collecting loads of field data that I now need to post-process and analyse. By the way, the 3 weeks I spent trying to learn C really ended up being spent trying to get to grips with using a terminal for the first time in my life. Since getting back to work, I was advised to try learning Python instead of C as it is a much easier first language to learn. I have been trying, but again, to not great success. I started following "A Primer on Scientific programming with Python" but I kept getting lost and stuck, specially on the exercises. I have also been advised that I should not try to learn programming by following guides but by trying to write the programmes I need to analyse my data. Although I can understand the logic behind this last bit of advise (it gives context and direction to the learning process) I have also gotten stuck trying this approach as "I do not know how to programme!". Thus, I was hoping that some of you can remember how you got started and point me towards any really good interactive learning guides/materials and/or have a good learning strategy for a complete beginner. I have searched the web and was overwhelmed by choice of tutorials and guides. I have skimmed through a couple of tutorials but then fail to see how all that relates to my own work and I get stuck with what seems like basic important concepts so I don't progress. I then think I should try to make some progress with my own data analysing and go back to trying to learn to write a programme for my specific needs and get stuck again because this requires more advanced skills then the basic programming concepts I have been reading about on the learning guides. So, I am now feeling VERY frustrated and have no idea what on Earth I am doing! Can anyone please offer guidance in my learning process? I don't know how and what I should be spending my time learning first and/or if I should focus my learning towards the skill areas I will require to write my specific programmes, although I have no idea what these are. I would like advise on finding some really good interactive(let you know if your solution to an exercise is correct or not) and or video tutorials that give you feedback on the solutions you write to exercises. Many thanks in advance for all your help, it will be much appreciated! Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 -------------- next part -------------- An HTML attachment was scrubbed... URL: From zaatlob at hotmail.com Wed Aug 22 14:34:01 2012 From: zaatlob at hotmail.com (leon zaat) Date: Wed, 22 Aug 2012 12:34:01 +0000 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> Message-ID: If you don't have any prior programmers skills, i would advice first to learn the basics. You will need a good foundation, before it is possible to create complex functions. Starting with complex functions is only frustrating if you don't understand the basics. From: cecilia.chavana-bryant at ouce.ox.ac.uk To: tutor at python.org Date: Wed, 22 Aug 2012 10:10:46 +0000 Subject: [Tutor] Hello Python Tutor - help please! Dear all, I am just returning to my doctoral studies after a 7-month medical leave and desperately trying to catch up for lost time. I am COMPLETELY new to programming, well, I did try learning C for 3 weeks 3 yrs ago (with very little success) but had to stop and then spent 2 years in the Amazon climbing trees (lots more enjoyable than learning to programme!) and collecting loads of field data that I now need to post-process and analyse. By the way, the 3 weeks I spent trying to learn C really ended up being spent trying to get to grips with using a terminal for the first time in my life. Since getting back to work, I was advised to try learning Python instead of C as it is a much easier first language to learn. I have been trying, but again, to not great success. I started following "A Primer on Scientific programming with Python" but I kept getting lost and stuck, specially on the exercises. I have also been advised that I should not try to learn programming by following guides but by trying to write the programmes I need to analyse my data. Although I can understand the logic behind this last bit of advise (it gives context and direction to the learning process) I have also gotten stuck trying this approach as "I do not know how to programme!". Thus, I was hoping that some of you can remember how you got started and point me towards any really good interactive learning guides/materials and/or have a good learning strategy for a complete beginner. I have searched the web and was overwhelmed by choice of tutorials and guides. I have skimmed through a couple of tutorials but then fail to see how all that relates to my own work and I get stuck with what seems like basic important concepts so I don't progress. I then think I should try to make some progress with my own data analysing and go back to trying to learn to write a programme for my specific needs and get stuck again because this requires more advanced skills then the basic programming concepts I have been reading about on the learning guides. So, I am now feeling VERY frustrated and have no idea what on Earth I am doing! Can anyone please offer guidance in my learning process? I don't know how and what I should be spending my time learning first and/or if I should focus my learning towards the skill areas I will require to write my specific programmes, although I have no idea what these are. I would like advise on finding some really good interactive(let you know if your solution to an exercise is correct or not) and or video tutorials that give you feedback on the solutions you write to exercises. Many thanks in advance for all your help, it will be much appreciated! Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 22 16:51:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Aug 2012 00:51:41 +1000 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> Message-ID: <5034F1FD.7090005@pearwood.info> Hello Cecilia, My replies are below, interleaved with your comments, which are prefixed with > marks. On 22/08/12 20:10, Cecilia Chavana-Bryant wrote: > By the way, the 3 weeks I spent trying to learn C really ended up > being spent trying to get to grips with using a terminal for the >first time in my life. Unfortunately, there will be a certain amount of that, or at least something quite similar to a terminal. Fortunately, using Python in the terminal is usually MUCH easier than C, and in my experience using Python's interactive interpreter is one of the best ways to learn the language. What sort of computer are you using? Windows, Linux, Macintosh, or something different? I think that most of the people here use Linux or Windows, but we can probably help you one way or the other. > Since getting back to work, I was advised to try learning Python >instead of C as it is a much easier first language to learn. Yes, definitely, but it is still programming. Don't overestimate the difficulty, if it was hard programmers couldn't learn to do it *wink*, but on the other hand it's not trivial either. > I have been trying, but again, to not great success. I started >following "A Primer on Scientific programming with Python" but I > kept getting lost and stuck, specially on the exercises. If you are willing to make a good, honest effort on the exercises first, we're happy to help you with them. We do like to see your attempt first, so that we can suggest fixes rather than solve the problem for you. > I have also been advised that I should not try to learn > programming by following guides but by trying to write the > programmes I need to analyse my data. Although I can understand > the logic behind this last bit of advise (it gives context and > direction to the learning process) I have also gotten stuck >trying this approach as "I do not know how to programme!". I'm entirely with you there. Having direction in your learning is a good thing. But until you understand the basic skills you need, it will be nothing but frustration and pain! I recommend that, if nothing else, you work through some basic tutorials so that you at least have some idea of basic language constructs like: - strings - lists - functions - ints - floats - basic arithmetic - importing modules etc. You could start with the official Python tutorial: http://docs.python.org/tutorial/index.html although I find that it is sometimes a bit wordy. (I should talk...) If you get stuck, don't hesitate to come back and ask questions, that's why we're here. > Thus, I was hoping that some of you can remember how you got >started I learned from the book "Learning Python" by Mark Lutz and David Ascher, and then by writing oodles and oodles of really bad code which I have long since thrown away :) [...] > So, I am now feeling VERY frustrated and have no idea what on >Earth I am doing! Can anyone please offer guidance in my >learning process? I feel your pain! That's how I feel every time I try to understand monads in Haskell (don't ask!). How about if you start off with a simple question you would like to solve using Python? Something relevant to your work. You may need to explain some of the concepts to us, since we're not ecologists. (At least, I'm not, I can't speak for others.) We can then try to guide you to a solution and introduce concepts as we go. -- Steven From steve at pearwood.info Wed Aug 22 16:53:53 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Aug 2012 00:53:53 +1000 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: <5034F281.6090707@pearwood.info> On 22/08/12 20:28, Pete O'Connell wrote: > Hi. The next step for me to parse the file as I want to is to change > lines that look like this: > f 21/21/21 22/22/22 24/24/23 23/23/24 > into lines that look like this: > f 21 22 23 24 In English, what is the rule you are applying here? My guess is: "Given three numbers separated by slashes, ignore the first two numbers and keep the third." E.g. "17/25/97" => 97. Am I close? > Below is my terribly slow loop for doing this. Any suggestions about > how to make this code more efficient would be greatly appreciated What makes you say it is "terribly slow"? Perhaps it is as fast as it could be under the circumstances. (Maybe it takes a long time because you have a lot of data, not because it is slow.) The first lesson of programming is not to be too concerned about speed until your program is correct. Like most such guidelines, this is not entirely true -- you don't want to write code which is unnecessarily slow. But the question you should be asking is, "is it fast enough?" rather than "is it fast?". Also, the sad truth is that Python tends to be slower than some other languages. (It's also faster than some other languages too.) But the general process is: 1) write something that works correctly; 2) if it is too slow, try to speed it up in Python; 3) if that's still too slow, try using something like cython or PyPy 4) if all else fails, now that you have a working prototype, re-write it again in C, Java, Lisp or Haskell. Once they see how much more work is involved in writing fast C code, most people decide that "fast enough" is fast enough :) > with open(fileName) as lines: > theGoodLines = [line.strip("\n") for line in lines if "vn" not in > line and "vt" not in line and line != "\n"] I prefer to write code in chains of filters. with open(fileName) as lines: # get rid of leading and trailing whitespace, including newlines lines = (line.strip() for line in lines) # ignore blanks lines = (line in lines if line) # ignore lines containing "vn" or "vt" theGoodLines = [line in lines if not ("vn" in line or "vt" in line)] Note that only the last step is a list comprehension using [ ], the others are generator expressions using ( ) instead. Will the above be faster than your version? I have no idea. But I think it is more readable and understandable. Some people might disagree. > for i in range(len(theGoodLines)): > if theGoodLines[i][0] == "f": > aGoodLineAsList = theGoodLines[i].split(" ") > theGoodLines[i] = aGoodLineAsList[0] + " " + > aGoodLineAsList[1].split("/")[-1] + " " + > aGoodLineAsList[2].split("/")[-1] + " " + > aGoodLineAsList[3].split("/")[-1] + " " + > aGoodLineAsList[4].split("/")[-1] Start with a helper function: def extract_last_item(term): """Extract the item from a term like a/b/c""" return term.split("/")[-1] for i, line in enumerate(theGoodLines): if line[0] == "f": terms = line.split() theGoodLines[i] = " ".join([extract_last_item(t) for t in terms]) See how you go with that. -- Steven From richkappler at gmail.com Wed Aug 22 17:04:05 2012 From: richkappler at gmail.com (richard kappler) Date: Wed, 22 Aug 2012 11:04:05 -0400 Subject: [Tutor] Hello Python Tutor - help please! Message-ID: This worked exceedingly well for me: http://learnpythonthehardway.org/book/ regards, Richard -- "Treat all disasters as if they were trivialities but never treat a triviality as if it were a disaster." -- *Quentin Crisp * -------------- next part -------------- An HTML attachment was scrubbed... URL: From cecilia.chavana-bryant at ouce.ox.ac.uk Wed Aug 22 17:33:03 2012 From: cecilia.chavana-bryant at ouce.ox.ac.uk (Cecilia Chavana-Bryant) Date: Wed, 22 Aug 2012 15:33:03 +0000 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk>, Message-ID: <573F2498A47C0F4BA6DF288738D8C199032A35@MBX06.ad.oak.ox.ac.uk> Hola Bill, Many thanks for your reply to my post, you seem to understand the predicament I am in very well. Unfortunately, I am currently working from home and do not have someone close by to help and this is why I came to this space. This is also why I asked for advise about interactive tutorials or video tutorials. I have found that I keep getting lost with the more traditional tutorials where you just read and then do exercises. Following the guide I mentioned on my initial post I got through the first 2 chapters but I found them quite hard going. I don't know if this makes me not a complete beginner but I certainly do not feel like I learned much from reading them. Maybe it is the trying to learn the "computer ecosystem" of terminal commands at the same time that is making this learning process so tough. With respect to my field data, during my 2 yrs of fieldwork I collected a large amount of data which is currently stored in excel files. My research involves remote sensing (data from Earth-observation satellites) and I work with data from the MODIS NASA satellite which monitors the health of forest canopies using reflectance data. My research is based in the Amazon. I have collected field data to monitor the leaf dynamics of canopy leaves during the dry season. Dry season is the time of year when many tropical trees change their old leaves for new ones. New leaves are more photosynthetically active (absorb more carbon from and release more oxygen into the atmosphere) so the leaf exchange of such a large forest region as the Amazon can have huge effects on regional and global carbon and water cycles and thus on global climate (apologies if I'm giving you loads more information than you need or requested?!). My data involves a large amount of data on leaf demography (we demographically surveyed more than 120,000 leaves), and thousands of morphological and reflectance measurements. I will have to reorganise this data and create a few easily manipulable datasets so I can sort data according to leaf age, canopy position, date, etc. Then I will have to do statistical analyses on the data. I will also have to model some of the data. Many thanks for taking the time to respond to my post so comprehensively and for your good wishes. Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 ________________________________ From: William R. Wing (Bill Wing) [wrw at mac.com] Sent: 22 August 2012 15:17 To: Cecilia Chavana-Bryant Cc: William R. Wing (Bill Wing) Subject: Re: [Tutor] Hello Python Tutor - help please! On Aug 22, 2012, at 6:10 AM, Cecilia Chavana-Bryant > wrote: Dear all, I am just returning to my doctoral studies after a 7-month medical leave and desperately trying to catch up for lost time. I am COMPLETELY new to programming, well, I did try learning C for 3 weeks 3 yrs ago (with very little success) but had to stop and then spent 2 years in the Amazon climbing trees (lots more enjoyable than learning to programme!) and collecting loads of field data that I now need to post-process and analyse. By the way, the 3 weeks I spent trying to learn C really ended up being spent trying to get to grips with using a terminal for the first time in my life. Could you say a few words about what the field data is, and how you hope to analyze it. That is, are you headed in the direction of plotting species density on maps, or the time evolution of something, or doing statistics? Since getting back to work, I was advised to try learning Python instead of C as it is a much easier first language to learn. I have been trying, but again, to not great success. I started following "A Primer on Scientific programming with Python" but I kept getting lost and stuck, specially on the exercises. I have also been advised that I should not try to learn programming by following guides but by trying to write the programmes I need to analyse my data. Although I can understand the logic behind this last bit of advise (it gives context and direction to the learning process) I have also gotten stuck trying this approach as "I do not know how to programme!". Thus, I was hoping that some of you can remember how you got started and point me towards any really good interactive learning guides/materials and/or have a good learning strategy for a complete beginner. I have searched the web and was overwhelmed by choice of tutorials and guides. I have skimmed through a couple of tutorials but then fail to see how all that relates to my own work and I get stuck with what seems like basic important concepts so I don't progress. I then think I should try to make some progress with my own data analysing and go back to trying to learn to write a programme for my specific needs and get stuck again because this requires more advanced skills then the basic programming concepts I have been reading about on the learning guides. So, I am now feeling VERY frustrated and have no idea what on Earth I am doing! Can anyone please offer guidance in my learning process? I don't know how and what I should be spending my time learning first and/or if I should focus my learning towards the skill areas I will require to write my specific programmes, although I have no idea what these are. I would like advise on finding some really good interactive(let you know if your solution to an exercise is correct or not) and or video tutorials that give you feedback on the solutions you write to exercises. Many thanks in advance for all your help, it will be much appreciated! I think there are three problems here, and you are (for better or worse) attempting to tackle all three at once. First, and foremost, "leaning to program" really means learning a particular way of thinking about a problem, and how to decompose it into a sequence of steps. Most programmers learned that way of thinking about problems so early in their careers they've forgotten it[*]. In parallel with that, you are trying to learn and understand the whole computer "ecosystem" of terminal commands, reading and writing files (and structuring their contents), data types, utilities, and libraries. Finally, there is the actual work of writing the specific programs necessary to deal with your data. Under any circumstances, tackling these all at once (or even in series) would be a heroic undertaking, doing so under the pressure of writing your thesis is (and this is purely my personal opinion) going to require that you have someone there at your elbow to help you through the first few days (weeks) of learning, particularly to help you with the first two issues I mention above. I agree that Python is an excellent choice for a first language. It allows instant testing, provides immediate feedback with very good error messages, and is very flexible. [*] Years ago, I agreed to help a co-worker "learn to program". This was back in the days of FORTRAN-IV, McCracken, and computers on which 64 thousand memory locations was a lot of memory. I showed him how to use an editor to enter code, loaned him my copy of McCracken, explained on a blackboard how the various statements worked and how FORTRAN directed logic flow and could do arithmetic. Then, as an initial exercise, I asked him to write a program to find prime numbers. This is a very standard initial exercise in programming, and forces the student to think through how you would test a number to find out if it is prime. If it is, print it out and if it isn't, go on to the next one. HIS solution was to define a square array indexed with the integers on each side. The cells of that array were the products of the integers, and he intended to then scan that square looking for the numbers that DIDN'T exist in it. Those would be prime. So, note that there is nothing logically wrong with this solution. But in a practical world (particularly a world with very limited memory) it stops working at a fairly small value of prime numbers. The point I'm trying to make (in far too long winded a fashion) is that "learning to program" involves a lot more than just rote leaning of syntax. We all wish you the VERY best of luck. We will give you all the help we can, but I do hope you have someone close by. Good luck, Bill Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From cecilia.chavana-bryant at ouce.ox.ac.uk Wed Aug 22 17:48:24 2012 From: cecilia.chavana-bryant at ouce.ox.ac.uk (Cecilia Chavana-Bryant) Date: Wed, 22 Aug 2012 15:48:24 +0000 Subject: [Tutor] Thanks!! Message-ID: <573F2498A47C0F4BA6DF288738D8C199032A9F@MBX06.ad.oak.ox.ac.uk> Hola, Just a big THANK YOU!!! to everyone that has replied to my post. I have been so confused about how to get started and since I am working from home, it has been a very frustrating and lonely experience so far. Many, many thanks for your empathy, encouragement and availability!! Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Wed Aug 22 18:15:57 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 22 Aug 2012 12:15:57 -0400 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032A35@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> <573F2498A47C0F4BA6DF288738D8C199032A35@MBX06.ad.oak.ox.ac.uk> Message-ID: On Wed, Aug 22, 2012 at 11:33 AM, Cecilia Chavana-Bryant wrote: > Hola Bill, > > Many thanks for your reply to my post, you seem to understand the > predicament I am in very well. Unfortunately, I am currently working from > home and do not have someone close by to help and this is why I came to this > space. This is also why I asked for advise about interactive tutorials or > video tutorials. I have found that I keep getting lost with the more > traditional tutorials where you just read and then do exercises. Following > the guide I mentioned on my initial post I got through the first 2 chapters > but I found them quite hard going. I don't know if this makes me not a > complete beginner but I certainly do not feel like I learned much from > reading them. Maybe it is the trying to learn the "computer ecosystem" of > terminal commands at the same time that is making this learning process so > tough. > > With respect to my field data, during my 2 yrs of fieldwork I collected a > large amount of data which is currently stored in excel files. My research > involves remote sensing (data from Earth-observation satellites) and I work > with data from the MODIS NASA satellite which monitors the health of forest > canopies using reflectance data. My research is based in the Amazon. I have > collected field data to monitor the leaf dynamics of canopy leaves during > the dry season. Dry season is the time of year when many tropical trees > change their old leaves for new ones. New leaves are more photosynthetically > active (absorb more carbon from and release more oxygen into the atmosphere) > so the leaf exchange of such a large forest region as the Amazon can have > huge effects on regional and global carbon and water cycles and thus on > global climate (apologies if I'm giving you loads more information than you > need or requested?!). My data involves a large amount of data on leaf > demography (we demographically surveyed more than 120,000 leaves), and > thousands of morphological and reflectance measurements. I will have to > reorganise this data and create a few easily manipulable datasets so I can > sort data according to leaf age, canopy position, date, etc. Then I will > have to do statistical analyses on the data. I will also have to model some > of the data. > > Many thanks for taking the time to respond to my post so comprehensively and > for your good wishes. > > > Cecilia Chavana-Bryant > DPhil Candidate - Remote sensing and tropical phenology > Environmental Change Institute > School of Geography and the Environment > University of Oxford > South Parks Road, Oxford, OX1 3QY > Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php > Tel Direct: +44 (0)1865 275861 > Fax: +44 (0)1865 275885 > ________________________________ > From: William R. Wing (Bill Wing) [wrw at mac.com] > Sent: 22 August 2012 15:17 > To: Cecilia Chavana-Bryant > Cc: William R. Wing (Bill Wing) > Subject: Re: [Tutor] Hello Python Tutor - help please! > > On Aug 22, 2012, at 6:10 AM, Cecilia Chavana-Bryant > wrote: > > Dear all, > > I am just returning to my doctoral studies after a 7-month medical leave and > desperately trying to catch up for lost time. I am COMPLETELY new to > programming, well, I did try learning C for 3 weeks 3 yrs ago (with very > little success) but had to stop and then spent 2 years in the Amazon > climbing trees (lots more enjoyable than learning to programme!) and > collecting loads of field data that I now need to post-process and analyse. > By the way, the 3 weeks I spent trying to learn C really ended up being > spent trying to get to grips with using a terminal for the first time in my > life. > > > Could you say a few words about what the field data is, and how you hope to > analyze it. That is, are you headed in the direction of plotting species > density on maps, or the time evolution of something, or doing statistics? > > Since getting back to work, I was advised to try learning Python instead of > C as it is a much easier first language to learn. I have been trying, but > again, to not great success. I started following "A Primer on Scientific > programming with Python" but I kept getting lost and stuck, specially on the > exercises. I have also been advised that I should not try to learn > programming by following guides but by trying to write the programmes I need > to analyse my data. Although I can understand the logic behind this last bit > of advise (it gives context and direction to the learning process) I have > also gotten stuck trying this approach as "I do not know how to programme!". > Thus, I was hoping that some of you can remember how you got started and > point me towards any really good interactive learning guides/materials > and/or have a good learning strategy for a complete beginner. I have > searched the web and was overwhelmed by choice of tutorials and guides. I > have skimmed through a couple of tutorials but then fail to see how all that > relates to my own work and I get stuck with what seems like basic important > concepts so I don't progress. I then think I should try to make some > progress with my own data analysing and go back to trying to learn to write > a programme for my specific needs and get stuck again because this requires > more advanced skills then the basic programming concepts I have been reading > about on the learning guides. So, I am now feeling VERY frustrated and have > no idea what on Earth I am doing! Can anyone please offer guidance in my > learning process? I don't know how and what I should be spending my time > learning first and/or if I should focus my learning towards the skill areas > I will require to write my specific programmes, although I have no idea what > these are. I would like advise on finding some really good interactive(let > you know if your solution to an exercise is correct or not) and or video > tutorials that give you feedback on the solutions you write to exercises. > > Many thanks in advance for all your help, it will be much appreciated! > > > > I think there are three problems here, and you are (for better or worse) > attempting to tackle all three at once. First, and foremost, "leaning to > program" really means learning a particular way of thinking about a problem, > and how to decompose it into a sequence of steps. Most programmers learned > that way of thinking about problems so early in their careers they've > forgotten it[*]. In parallel with that, you are trying to learn and > understand the whole computer "ecosystem" of terminal commands, reading and > writing files (and structuring their contents), data types, utilities, and > libraries. Finally, there is the actual work of writing the specific > programs necessary to deal with your data. Under any circumstances, > tackling these all at once (or even in series) would be a heroic > undertaking, doing so under the pressure of writing your thesis is (and this > is purely my personal opinion) going to require that you have someone there > at your elbow to help you through the first few days (weeks) of learning, > particularly to help you with the first two issues I mention above. > > I agree that Python is an excellent choice for a first language. It allows > instant testing, provides immediate feedback with very good error messages, > and is very flexible. > > [*] Years ago, I agreed to help a co-worker "learn to program". This was > back in the days of FORTRAN-IV, McCracken, and computers on which 64 > thousand memory locations was a lot of memory. I showed him how to use an > editor to enter code, loaned him my copy of McCracken, explained on a > blackboard how the various statements worked and how FORTRAN directed logic > flow and could do arithmetic. Then, as an initial exercise, I asked him to > write a program to find prime numbers. This is a very standard initial > exercise in programming, and forces the student to think through how you > would test a number to find out if it is prime. If it is, print it out and > if it isn't, go on to the next one. HIS solution was to define a square > array indexed with the integers on each side. The cells of that array were > the products of the integers, and he intended to then scan that square > looking for the numbers that DIDN'T exist in it. Those would be prime. So, > note that there is nothing logically wrong with this solution. But in a > practical world (particularly a world with very limited memory) it stops > working at a fairly small value of prime numbers. The point I'm trying to > make (in far too long winded a fashion) is that "learning to program" > involves a lot more than just rote leaning of syntax. > > We all wish you the VERY best of luck. We will give you all the help we > can, but I do hope you have someone close by. > > Good luck, > Bill > > > > Cecilia Chavana-Bryant > DPhil Candidate - Remote sensing and tropical phenology > Environmental Change Institute > School of Geography and the Environment > University of Oxford > South Parks Road, Oxford, OX1 3QY > Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php > Tel Direct: +44 (0)1865 275861 > Fax: +44 (0)1865 275885 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Some people have had a good experience with this: http://learnpythonthehardway.org/book/ It starts out very basic, requiring you to type in very small learning moments, then shifts gears and advances -- Joel Goldstick From alan.gauld at btinternet.com Wed Aug 22 18:16:54 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Aug 2012 17:16:54 +0100 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: On 22/08/12 10:51, Pete O'Connell wrote: >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" not in x and "vt" not in x and x!= ""] > > It works but what I don't understand about this line is why the ands > are not ors Because 'or' would include x if any one of the conditions was true. For example if 'vt' existed but not 'vn' then the line would be included because vn was not in the line, even though vt was. We are all assuming that you want to exclude the line if any of the phrases is present. Thats why I actually prefer the multiple if version that Peter posted: theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped if "vn" not in x if "vt" not in x if x!= ""] It's slightly more verbose but it makes the rules more explicit, IMHO. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From rdmoores at gmail.com Wed Aug 22 18:17:19 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 22 Aug 2012 09:17:19 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: <5027C480.3080705@davea.name> Message-ID: I've incorporated many of the suggestions I've received here. Here's a function, factor_integer(), for quickly factoring any integer up to 1e17: . Larger integers will be factored eventually -- the wait can be long or short. Probably long if they require the services of lines 82-91. Examples of extremes, both between 1e25 and 1e26: 2,835,334,663,465,375,591,838,337 [3, 19, 37, 71, 947, 19994908447741489] 1.172 secs 9,349,337,574,247,205,482,125,105 [3, 5, 2027, 2311296661, 133039358281] 402.5 secs These were found with the use of a related script, "factor_random_integers.py", pasted at Dick Moores From breamoreboy at yahoo.co.uk Wed Aug 22 18:24:51 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Aug 2012 17:24:51 +0100 Subject: [Tutor] Thanks!! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032A9F@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032A9F@MBX06.ad.oak.ox.ac.uk> Message-ID: On 22/08/2012 16:48, Cecilia Chavana-Bryant wrote: > Hola, > > Just a big THANK YOU!!! to everyone that has replied to my post. I have been so confused about how to get started and since I am working from home, it has been a very frustrating and lonely experience so far. Many, many thanks for your empathy, encouragement and availability!! > > > Cecilia Chavana-Bryant > DPhil Candidate - Remote sensing and tropical phenology > Environmental Change Institute > School of Geography and the Environment > University of Oxford > South Parks Road, Oxford, OX1 3QY > Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php > Tel Direct: +44 (0)1865 275861 > Fax: +44 (0)1865 275885 > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Please save your thanks until you're graduated from the tutor mailing list to the main Python mailing list :) For more help take a look here http://wiki.python.org/moin/BeginnersGuide/NonProgrammers -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Wed Aug 22 18:26:40 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Aug 2012 17:26:40 +0100 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> Message-ID: On 22/08/12 11:10, Cecilia Chavana-Bryant wrote: > "I do not know how to programme!". Thus, I was hoping that some of you > can remember how you got started and point me towards any really good > interactive learning guides/materials and/or have a good learning > strategy for a complete beginner. At the risk of self promotion you could try the early stages of my tutorial (see .sig). It starts from level zero and explains the concepts of programming before getting started writing code. I personally found that to be fundamental to my learning when I started (and why I included it!). It then goes through the basic programming structures using very simple examples - a multiplication table and address book mainly. (It includes VBScript and Javascript examples too but feel free to ignore them if you find they confuse more than help! The intent is to show the common structures present in all programming languages :-) Whether you progress beyond the first two sections depends on what you want to do next. Those would be enough to start writing programs related to your work and switching to another tutor at that point might be effective. But the concepts topics at the start of my tutor are I think relatively unique, and if diving straight into writing code isn't working maybe a view of the bigger picture will help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wprins at gmail.com Wed Aug 22 18:37:14 2012 From: wprins at gmail.com (Walter Prins) Date: Wed, 22 Aug 2012 17:37:14 +0100 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> Message-ID: Hi Cecilia, You've had a lot of good replies already, but I'd like to add the following points if I may: 1) You probably should figure out as much as that's possible up front exactly you're trying to do in terms of data processing first (e.g. some idea of the stats, graphs, summaries, operations etc), and then figure out whether Python is in fact the quickest/best way to get there for you. Python is very capable, it has many data analysis libraries and so on and is used by many scientists (NumPy, SciPy, Pandas comes to mind offhand), but then there are also many other languages and system also appropriate in this sphere. (R comes to mind.) Your goal (from my point of view) is not to become a programmer, but to get your research done. Python may be the way to achieve that, but from where I'm sitting it may also not be. 2) It may be useful to take some suitable courses from some of the very good free online courses now available from various sources such as Coursera. Some examples that seem relevant: Computing for Data Analysis: https://www.coursera.org/course/compdata Mathematics Biostatistics Boot camp: https://www.coursera.org/course/biostats Data Analysis https://www.coursera.org/course/dataanalysis There are also courses covering basic programming, including Python, for example: https://www.coursera.org/course/programming1 HTH, Walter From dfjennings at gmail.com Wed Aug 22 18:51:45 2012 From: dfjennings at gmail.com (Don Jennings) Date: Wed, 22 Aug 2012 12:51:45 -0400 Subject: [Tutor] better tools Message-ID: [slightly OT] After watching Bret Victor's talk[1], I want **much** better tools for programming (and all of the other stuff I do on the computer). John Resig, creator of jQuery, claims[2] that Victor's presentation inspired the new platform for learning javascript at Khan Academy[3]. I plan to try it out. Take care, Don [1] http://vimeo.com/36579366 [2] http://ejohn.org/blog/introducing-khan-cs/ [3] http://www.khanacademy.org/cs From steve at pearwood.info Wed Aug 22 20:54:57 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Aug 2012 04:54:57 +1000 Subject: [Tutor] pickle problems In-Reply-To: References: <5027C480.3080705@davea.name> Message-ID: <50352B01.4070903@pearwood.info> On 23/08/12 02:17, Richard D. Moores wrote: > I've incorporated many of the suggestions I've received here. > > Here's a function, factor_integer(), for quickly factoring any integer > up to 1e17:. That relies on a pre-existing cache of prime numbers. If you don't use those cached prime numbers, do you know how long your code takes? > Larger integers will be factored eventually -- the wait can be long or > short. Probably long if they require the services of lines 82-91. > > Examples of extremes, both between 1e25 and 1e26: > 2,835,334,663,465,375,591,838,337 [3, 19, 37, 71, 947, > 19994908447741489] 1.172 secs > 9,349,337,574,247,205,482,125,105 [3, 5, 2027, 2311296661, > 133039358281] 402.5 secs I'm astounded by the times you quote there. The first example includes the prime 19994908447741489, which is *not* in the cache, since it is bigger than 1e17. And yet it only takes about a second to find all six primes. If true, that's astonishingly good for something written in pure Python. The second example includes the prime 133039358281, which is smaller than 1e17 and so should be in the cache and therefore found (almost) instantly. And yet you claim it takes nearly seven minutes to find it. If correct, that's astonishingly awful given that you have the prime numbers already cached. Can you explain those timings? How did you measure them? -- Steven From crawlzone at gmail.com Wed Aug 22 22:12:26 2012 From: crawlzone at gmail.com (Ray Jones) Date: Wed, 22 Aug 2012 13:12:26 -0700 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk> Message-ID: <50353D2A.2010801@gmail.com> I highly recommend the Google Python class that is found on YouTube. The first video is found at http://www.youtube.com/watch?v=tKTZoB2Vjuk The supporting class materials and assignments are found at http://code.google.com/edu/languages/google-python-class/ . This series of videos begins at a pretty basic level, but subsequent videos increase the difficulty level pretty rapidly. Don't despair - the concept of how to properly manipulate strings, lists, tuples, and other objects is rather daunting, but if you're working on your doctoral studies, you already know that complex concepts aren't simply soaked up like water to a sponge ;). Ray On 08/22/2012 03:10 AM, Cecilia Chavana-Bryant wrote: > Dear all, > > I am just returning to my doctoral studies after a 7-month medical > leave and desperately trying to catch up for lost time. I am > COMPLETELY new to programming, well, I did try learning C for 3 weeks > 3 yrs ago (with very little success) but had to stop and then spent 2 > years in the Amazon climbing trees (lots more enjoyable than learning > to programme!) and collecting loads of field data that I now need to > post-process and analyse. By the way, the 3 weeks I spent trying to > learn C really ended up being spent trying to get to grips with using > a terminal for the first time in my life. > > Since getting back to work, I was advised to try learning Python > instead of C as it is a much easier first language to learn. I have > been trying, but again, to not great success. I started following "A > Primer on Scientific programming with Python" but I kept getting lost > and stuck, specially on the exercises. I have also been advised that I > should not try to learn programming by following guides but by trying > to write the programmes I need to analyse my data. Although I can > understand the logic behind this last bit of advise (it gives context > and direction to the learning process) I have also gotten stuck trying > this approach as "I do not know how to programme!". Thus, I was hoping > that some of you can remember how you got started and point me towards > any really good interactive learning guides/materials and/or have a > good learning strategy for a complete beginner. I have searched the > web and was overwhelmed by choice of tutorials and guides. I have > skimmed through a couple of tutorials but then fail to see how all > that relates to my own work and I get stuck with what seems like basic > important concepts so I don't progress. I then think I should try to > make some progress with my own data analysing and go back to trying to > learn to write a programme for my specific needs and get stuck again > because this requires more advanced skills then the basic programming > concepts I have been reading about on the learning guides. So, I am > now feeling VERY frustrated and have no idea what on Earth I am doing! > Can anyone please offer guidance in my learning process? I don't know > how and what I should be spending my time learning first and/or if I > should focus my learning towards the skill areas I will require to > write my specific programmes, although I have no idea what these are. > I would like advise on finding some really good interactive(let you > know if your solution to an exercise is correct or not) and or video > tutorials that give you feedback on the solutions you write to > exercises. > > Many thanks in advance for all your help, it will be much appreciated! > > > > Cecilia Chavana-Bryant > DPhil Candidate - Remote sensing and tropical phenology > Environmental Change Institute > School of Geography and the Environment > University of Oxford > South Parks Road, Oxford, OX1 3QY > Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php > Tel Direct: +44 (0)1865 275861 > Fax: +44 (0)1865 275885 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From cecilia.chavana-bryant at ouce.ox.ac.uk Wed Aug 22 22:51:12 2012 From: cecilia.chavana-bryant at ouce.ox.ac.uk (Cecilia Chavana-Bryant) Date: Wed, 22 Aug 2012 20:51:12 +0000 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <5034F1FD.7090005@pearwood.info> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk>, <5034F1FD.7090005@pearwood.info> Message-ID: <573F2498A47C0F4BA6DF288738D8C199032A81@MBX06.ad.oak.ox.ac.uk> Hola Steven, Many thanks for your reply. I use a Mac computer with OS X 10.6.8 Snow Leopard. I have not been using Python's interactive interpreter. I've been using the terminal and the editor TextWrangler, is using Python shell much different/easier to learn Python with? I do have it installed on my computer but have opened it just a couple of times. The friend that advised me to learn Python and was supposed to help me learn it installed a few things (modules, perhaps?) on my computer, but then he left for Australia (in his defence, it was a very good job offer and he does still help but the 12 hr time difference makes it difficult) so I never really found out what exactly he installed and what they were for. So, I just started using python with the terminal, perhaps it would be better for me to switch to the Python shell? Something I have been working on is trying to extract some calibration data from 2 different excel files into a csv file (I've been advised this is a good file format to put data into) so I can then use this to post-process around 4 thousand ASCII files. I thought this would be an easy job to start with (what a fool!!) as really all that is involved is multiplying the calibration values against the target values in the ASCII files and then multiply this by 100 to obtain absolute reflectance values. I wrote the below bit of code from bits of coding I found on the web, to be perfectly honest I wrote something like it and then my friend in Australia rewrote it properly. #!/usr/bin/env python import glob import xlrd import sys def main(fname, sheet_name): wb = xlrd.open_workbook(fname) sh = wb.sheet_by_name(sheet_name) data1 = sh.col_values(0) data2 = sh.col_values(1) return data1, data2 fname = "Cal_File_P17.xlsx" sheet_name = "RefPanelData" (data1, data2) = main(fname) print data1, data2 I do understand this bit of code (although I wouldn't have been able to write it from scratch) but I do not know where the data is being saved to. I have now installed the xlrd module on my computer and will run the code and see what happens, don't worry, my friend in Australia has already told me off for having the temerity of attempting to write code without running it! There are 5 header rows at the beginning of the excel calibration files and one of these is empty and one is a date so I'm not sure what these rows are going to look like when they are extracted, I shall soon find out. Also, I will have to find a way to insert 25 rows before the calibration headers as the ASCII files have 30 lines detailing the instrument condition at the time of measurement that I want to keep. All the actual measurement values that i will have to manipulate start after the row wavelength (I've attached examples of the files i am working with). On a different topic, this is the first time I subscribe to a forum and I've just realised that the long-winded signature of my student email account has been included in my posts, for some reason I thought only the main text would go into the post. I'll try to switch to a different email account as the student signature is almost longer than my posts! Many thanks for your help, Cecilia Chavana-Bryant DPhil Candidate - Remote sensing and tropical phenology Environmental Change Institute School of Geography and the Environment University of Oxford South Parks Road, Oxford, OX1 3QY Web: http://www.eci.ox.ac.uk/teaching/doctoral/chavanabryantcecilia.php Tel Direct: +44 (0)1865 275861 Fax: +44 (0)1865 275885 ________________________________________ From: tutor-bounces+cecilia.chavana-bryant=ouce.ox.ac.uk at python.org [tutor-bounces+cecilia.chavana-bryant=ouce.ox.ac.uk at python.org] on behalf of Steven D'Aprano [steve at pearwood.info] Sent: 22 August 2012 15:51 To: tutor at python.org Subject: Re: [Tutor] Hello Python Tutor - help please! Hello Cecilia, My replies are below, interleaved with your comments, which are prefixed with > marks. On 22/08/12 20:10, Cecilia Chavana-Bryant wrote: > By the way, the 3 weeks I spent trying to learn C really ended up > being spent trying to get to grips with using a terminal for the >first time in my life. Unfortunately, there will be a certain amount of that, or at least something quite similar to a terminal. Fortunately, using Python in the terminal is usually MUCH easier than C, and in my experience using Python's interactive interpreter is one of the best ways to learn the language. What sort of computer are you using? Windows, Linux, Macintosh, or something different? I think that most of the people here use Linux or Windows, but we can probably help you one way or the other. > Since getting back to work, I was advised to try learning Python >instead of C as it is a much easier first language to learn. Yes, definitely, but it is still programming. Don't overestimate the difficulty, if it was hard programmers couldn't learn to do it *wink*, but on the other hand it's not trivial either. > I have been trying, but again, to not great success. I started >following "A Primer on Scientific programming with Python" but I > kept getting lost and stuck, specially on the exercises. If you are willing to make a good, honest effort on the exercises first, we're happy to help you with them. We do like to see your attempt first, so that we can suggest fixes rather than solve the problem for you. > I have also been advised that I should not try to learn > programming by following guides but by trying to write the > programmes I need to analyse my data. Although I can understand > the logic behind this last bit of advise (it gives context and > direction to the learning process) I have also gotten stuck >trying this approach as "I do not know how to programme!". I'm entirely with you there. Having direction in your learning is a good thing. But until you understand the basic skills you need, it will be nothing but frustration and pain! I recommend that, if nothing else, you work through some basic tutorials so that you at least have some idea of basic language constructs like: - strings - lists - functions - ints - floats - basic arithmetic - importing modules etc. You could start with the official Python tutorial: http://docs.python.org/tutorial/index.html although I find that it is sometimes a bit wordy. (I should talk...) If you get stuck, don't hesitate to come back and ask questions, that's why we're here. > Thus, I was hoping that some of you can remember how you got >started I learned from the book "Learning Python" by Mark Lutz and David Ascher, and then by writing oodles and oodles of really bad code which I have long since thrown away :) [...] > So, I am now feeling VERY frustrated and have no idea what on >Earth I am doing! Can anyone please offer guidance in my >learning process? I feel your pain! That's how I feel every time I try to understand monads in Haskell (don't ask!). How about if you start off with a simple question you would like to solve using Python? Something relevant to your work. You may need to explain some of the concepts to us, since we're not ecologists. (At least, I'm not, I can't speak for others.) We can then try to guide you to a solution and introduce concepts as we go. -- Steven _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From pedrooconnell at gmail.com Wed Aug 22 23:19:28 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Thu, 23 Aug 2012 09:19:28 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: Thanks eryksun, that is a very clear example. Cheers pete On Wed, Aug 22, 2012 at 11:16 PM, eryksun wrote: > On Wed, Aug 22, 2012 at 3:06 AM, Peter Otten <__peter__ at web.de> wrote: >> >> wanted = [line.strip("\n") for line in lines >> if "vn" not in line and "vt" not in line and line != "\n"] > > Here's an equivalent expression with the negation factored out: > > not ("vn" in line or "vt" in line or line == "\n") > > http://en.wikipedia.org/wiki/De_Morgan%27s_laws > > If you have a lot of tests all using the same operator (e.g. "in"), > you can use "any" (OR) or "all" (AND) with a generator expression: > > vals = ["vn", "vt", "vu", "vv", "vw", "vx", "vy", "vz"] > wanted = [line for line in lines if not any(v in line for v in vals)] > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - From pedrooconnell at gmail.com Wed Aug 22 23:23:36 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Thu, 23 Aug 2012 09:23:36 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: <5034F281.6090707@pearwood.info> References: <5034F281.6090707@pearwood.info> Message-ID: On Thu, Aug 23, 2012 at 2:53 AM, Steven D'Aprano wrote: > On 22/08/12 20:28, Pete O'Connell wrote: >> >> Hi. The next step for me to parse the file as I want to is to change >> lines that look like this: >> f 21/21/21 22/22/22 24/24/23 23/23/24 >> into lines that look like this: >> f 21 22 23 24 > > > In English, what is the rule you are applying here? My guess is: > > "Given three numbers separated by slashes, ignore the first two numbers > and keep the third." > > E.g. "17/25/97" => 97. > > Am I close? Hi Steve, yes that is correct > > > >> Below is my terribly slow loop for doing this. Any suggestions about >> how to make this code more efficient would be greatly appreciated > > > What makes you say it is "terribly slow"? Perhaps it is as fast as it > could be under the circumstances. (Maybe it takes a long time because > you have a lot of data, not because it is slow.) OK maybe I am wrong about it being slow (I thought for loops were slower than lis comprehensions). But I do know I need it to be as fast as possible if I need to run it on a thousand files each with hundreds of thousands of lines > > The first lesson of programming is not to be too concerned about speed > until your program is correct. > > Like most such guidelines, this is not entirely true -- you don't want > to write code which is unnecessarily slow. But the question you should > be asking is, "is it fast enough?" rather than "is it fast?". > > Also, the sad truth is that Python tends to be slower than some other > languages. (It's also faster than some other languages too.) But the > general process is: > > 1) write something that works correctly; > > 2) if it is too slow, try to speed it up in Python; > > 3) if that's still too slow, try using something like cython or PyPy > > 4) if all else fails, now that you have a working prototype, re-write > it again in C, Java, Lisp or Haskell. > > Once they see how much more work is involved in writing fast C code, > most people decide that "fast enough" is fast enough :) OK I will keep it as is and see if I can live with it. Thanks Pete > > > >> with open(fileName) as lines: >> theGoodLines = [line.strip("\n") for line in lines if "vn" not in >> line and "vt" not in line and line != "\n"] > > > I prefer to write code in chains of filters. > > with open(fileName) as lines: > # get rid of leading and trailing whitespace, including newlines > lines = (line.strip() for line in lines) > # ignore blanks > lines = (line in lines if line) > # ignore lines containing "vn" or "vt" > theGoodLines = [line in lines if not ("vn" in line or "vt" in line)] > > Note that only the last step is a list comprehension using [ ], the others > are generator expressions using ( ) instead. > > Will the above be faster than your version? I have no idea. But I think it > is more readable and understandable. Some people might disagree. > > > >> for i in range(len(theGoodLines)): >> if theGoodLines[i][0] == "f": >> aGoodLineAsList = theGoodLines[i].split(" ") >> theGoodLines[i] = aGoodLineAsList[0] + " " + >> aGoodLineAsList[1].split("/")[-1] + " " + >> aGoodLineAsList[2].split("/")[-1] + " " + >> aGoodLineAsList[3].split("/")[-1] + " " + >> aGoodLineAsList[4].split("/")[-1] > > > > Start with a helper function: > > def extract_last_item(term): > """Extract the item from a term like a/b/c""" > return term.split("/")[-1] > > > for i, line in enumerate(theGoodLines): > if line[0] == "f": > terms = line.split() > theGoodLines[i] = " ".join([extract_last_item(t) for t in terms]) > > > > See how you go with that. > > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - From pedrooconnell at gmail.com Wed Aug 22 23:27:28 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Thu, 23 Aug 2012 09:27:28 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped > if "vn" not in x > if "vt" not in x > if x!= ""] > > It's slightly more verbose but it makes the rules more explicit, IMHO. I agree, it seems easier to read when written on multiple lines. I'll do it that way, Thanks Pete From cecilia.chavana at gmail.com Wed Aug 22 23:51:06 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Wed, 22 Aug 2012 22:51:06 +0100 Subject: [Tutor] Hello Python Tutor - help please! Message-ID: Steven, (now from my new account without all the long-winded signature) can files be attached to posts in this forum? Cecilia -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Thu Aug 23 00:09:52 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Aug 2012 23:09:52 +0100 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: <5034F281.6090707@pearwood.info> Message-ID: On 22/08/2012 22:23, Pete O'Connell wrote: > On Thu, Aug 23, 2012 at 2:53 AM, Steven D'Aprano wrote: Further to Steven's sound advice take a look at this http://wiki.python.org/moin/PythonSpeed/PerformanceTips IMHO the part on profiling is particulary useful. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Thu Aug 23 00:27:25 2012 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 22 Aug 2012 23:27:25 +0100 (BST) Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: <1345674445.43037.YahooMailNeo@web87703.mail.ir2.yahoo.com> Note uits not just that its on multiple lines, its the fact it has three distinct if statements.? All of them must be satisfied to be included in the comprehension. You could do it on 3 lines with one if statement: theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if "vn" not in x and ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???? "vt" not in x and ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????? x!= ""] But I still think the three if statements are clearer. However, if you need to use 'or' instead of 'and' then you need to go back to a compound statement but multi-line layout still aids legibility. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ >________________________________ > From: Pete O'Connell >To: Alan Gauld ; tutor at python.org >Sent: Wednesday, 22 August 2012, 22:27 >Subject: Re: [Tutor] list comprehension, testing for multiple conditions > >On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped >>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if "vn" not in x >>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if "vt" not in x >>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if x!= ""] >> >> It's slightly more verbose but it makes the rules more explicit, IMHO. > >I agree, it seems easier to read when written on multiple lines. I'll >do it that way, >Thanks >Pete > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 23 00:35:08 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Aug 2012 23:35:08 +0100 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: References: Message-ID: On 22/08/12 22:51, Cecilia Chavana-Bryant wrote: > Steven, (now from my new account without all the long-winded signature) > can files be attached to posts in this forum? Yes they can, but we prefer if you just include them in the body if they are fairly short (<100 lines?) or put them on a pastebin with a link. Some mail tools/servers/smartphones don't like mail attachments. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Aug 23 00:47:21 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Aug 2012 23:47:21 +0100 Subject: [Tutor] Hello Python Tutor - help please! In-Reply-To: <573F2498A47C0F4BA6DF288738D8C199032A81@MBX06.ad.oak.ox.ac.uk> References: <573F2498A47C0F4BA6DF288738D8C199032945@MBX06.ad.oak.ox.ac.uk>, <5034F1FD.7090005@pearwood.info> <573F2498A47C0F4BA6DF288738D8C199032A81@MBX06.ad.oak.ox.ac.uk> Message-ID: On 22/08/12 21:51, Cecilia Chavana-Bryant wrote: > def main(fname, sheet_name): > wb = xlrd.open_workbook(fname) > sh = wb.sheet_by_name(sheet_name) > data1 = sh.col_values(0) > data2 = sh.col_values(1) > > return data1, data2 > > fname = "Cal_File_P17.xlsx" > sheet_name = "RefPanelData" > (data1, data2) = main(fname) > > print data1, data2 > > ... I do not know where the data is being saved to. That's because it isn't being saved anywhere, it gets thrown away after printing it. If you want to save it you need to augment/replace the print statement with a file write statement(s) You could also write it out using the OS redirection facility. On Unix (ie Your MacOS Terminal) thats done by running the program like: $ python myprog.py > myOutputFile.txt Where you substitute your own desired program name and output filename of course! But that will just create a text file that looks like the output displayed on the Terminal, which is not, I think, what you are after. You probably want to do some extra work to save as a csv file. However, before you do that you could look at using the spreadsheet's SaveAs feature to export as a CSV directly, but it depends on how many spreadsheets you need to convert! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Aug 23 00:58:21 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Aug 2012 23:58:21 +0100 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: <5034F281.6090707@pearwood.info> Message-ID: On 22/08/12 22:23, Pete O'Connell wrote: >> What makes you say it is "terribly slow"? Perhaps it is as fast as it >> could be under the circumstances. (Maybe it takes a long time because >> you have a lot of data, not because it is slow.) > > OK maybe I am wrong about it being slow (I thought for loops were > slower than lis comprehensions). For loops are slightly slower than list comprehensions but depending on what is happening inside the loop/comprehension the difference may not be significant. > But I do know I need it to be as fast > as possible if I need to run it on a thousand files each with hundreds > of thousands of lines If you need it as "fast as possible" you need to run it on a supercomputer with a program in assembler after many hours of fine tuning. But that will cost a lot of money and you probably don't really need it. For example how often do you need to run this process? Is it a one off - set it running over lunch and it will likely be done by the time you get back. Is it an hourly event? Set it running in the background while you carry on processing the results of the previous run. Steven's point is that usually absolute speed is not as important as people think. Modern computers are absurdly fast. When I started using Linux it took about 2 hours to build the kernel. Now I can build it in about 2 minutes! Processing a thousand files is largely limited by the speed of your hard drive rather than the speed of the processing on the CPU. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From malaclypse2 at gmail.com Thu Aug 23 00:58:41 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 22 Aug 2012 18:58:41 -0400 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: <5034F281.6090707@pearwood.info> Message-ID: On Wed, Aug 22, 2012 at 5:23 PM, Pete O'Connell wrote: > OK maybe I am wrong about it being slow (I thought for loops were > slower than lis comprehensions). But I do know I need it to be as fast > as possible if I need to run it on a thousand files each with hundreds > of thousands of lines You're quite likely wrong about that. The overall runtime of your code is very likely to end up dominated by I/O with the hard drive, not the microsecond differences in how you process each line. The way to know for sure is to write code that is easy to read and does the correct thing. Then run the code an see if it's too slow for your purposes. If it is, profile the code and see where it's spending all of its time. Only then can you actually optimize the right places. Once you've identified where your code needs to be faster, you're likely to get more mileage out of a clever algorithm change than a micro-optimization to your filtering. Or maybe you'll discover that it really is worthwhile to optimize your filtering, because it's inside a tight loop, and that's where all of your time is spent. There are a lot of tricks you can use to slightly speed up your python code, but none of them are worth doing until you know for sure that you're optimizing in the right place. And if it turns out that the code's runtime is fine as first written, you don't have to waste your time doing lots of work for little gain. -- Jerry From pedrooconnell at gmail.com Thu Aug 23 01:23:59 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Thu, 23 Aug 2012 11:23:59 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: <5034F281.6090707@pearwood.info> Message-ID: Ok thanks for the advice everyone. Cheers Pete On Thu, Aug 23, 2012 at 10:58 AM, Jerry Hill wrote: > On Wed, Aug 22, 2012 at 5:23 PM, Pete O'Connell wrote: >> OK maybe I am wrong about it being slow (I thought for loops were >> slower than lis comprehensions). But I do know I need it to be as fast >> as possible if I need to run it on a thousand files each with hundreds >> of thousands of lines > > You're quite likely wrong about that. The overall runtime of your > code is very likely to end up dominated by I/O with the hard drive, > not the microsecond differences in how you process each line. The way > to know for sure is to write code that is easy to read and does the > correct thing. Then run the code an see if it's too slow for your > purposes. If it is, profile the code and see where it's spending all > of its time. Only then can you actually optimize the right places. > > Once you've identified where your code needs to be faster, you're > likely to get more mileage out of a clever algorithm change than a > micro-optimization to your filtering. Or maybe you'll discover that it > really is worthwhile to optimize your filtering, because it's inside a > tight loop, and that's where all of your time is spent. There are a > lot of tricks you can use to slightly speed up your python code, but > none of them are worth doing until you know for sure that you're > optimizing in the right place. And if it turns out that the code's > runtime is fine as first written, you don't have to waste your time > doing lots of work for little gain. > > -- > Jerry > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- - From rdmoores at gmail.com Thu Aug 23 01:32:57 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 22 Aug 2012 16:32:57 -0700 Subject: [Tutor] pickle problems In-Reply-To: <50352B01.4070903@pearwood.info> References: <5027C480.3080705@davea.name> <50352B01.4070903@pearwood.info> Message-ID: On Wed, Aug 22, 2012 at 11:54 AM, Steven D'Aprano wrote: > On 23/08/12 02:17, Richard D. Moores wrote: >> >> I've incorporated many of the suggestions I've received here, plus some important tips from Case Van Horsen, the gmpy2 maintainer, and I believe one of its developers. >> >> Here's a function, factor_integer(), for quickly factoring any integer >> up to 1e17:. > > > That relies on a pre-existing cache of prime numbers. If you don't use > those cached prime numbers, do you know how long your code takes? Much longer. >> Larger integers will be factored eventually -- the wait can be long or >> short. Probably long if they require the services of lines 82-91. >> >> Examples of extremes, both between 1e25 and 1e26: >> 2,835,334,663,465,375,591,838,337 [3, 19, 37, 71, 947, >> 19994908447741489] 1.172 secs >> 9,349,337,574,247,205,482,125,105 [3, 5, 2027, 2311296661, >> 133039358281] 402.5 secs > > > I'm astounded by the times you quote there. Excellent! > The first example includes the prime 19994908447741489, which is *not* > in the cache, since it is bigger than 1e17. And yet it only takes about > a second to find all six primes. If true, that's astonishingly good for > something written in pure Python. My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. C:\Windows\System32>python -m timeit -v -r 5 -s "from gmpy2 import is_prime" "is_prime(19994908447741489)" 10 loops -> 0.0008 secs 100 loops -> 0.0073 secs 1000 loops -> 0.0409 secs 10000 loops -> 0.394 secs raw times: 0.392 0.393 0.394 0.392 0.393 10000 loops, best of 5: 39.2 usec per loop and to show its speed for enormous prime ints: >>> is_prime(987987987199949084477414897654765465435634564357034523045023453475389457937283) True C:\Windows\System32>python -m timeit -v -r 5 -s "from gmpy2 import is_prime" "is_prime(987987987199949084477414897654765465435634564357034523045023453475389457937283)" 10 loops -> 0.0077 secs 100 loops -> 0.0765 secs 1000 loops -> 0.758 secs raw times: 0.757 0.758 0.757 0.757 0.758 1000 loops, best of 5: 757 usec per loop Also, of the prime factors of 2,835,334,663,465,375,591,838,337, all but the last one, 19994908447741489, are small and quickly found. Once factors 3, 19, 37, 71, 947 are found, 19994908447741489 is simply the quotient of 2835334663465375591838337 // (3*19*37*71*947), and is_prime confirms its primacy. > The second example includes the prime 133039358281, which is smaller > than 1e17 and so should be in the cache and therefore found (almost) > instantly. No, the cache has only primes up to 100,000,000, the last one being 99,999,989. > And yet you claim it takes nearly seven minutes to find it. > If correct, that's astonishingly awful given that you have the prime > numbers already cached. The delay is caused by the 2nd largest factor being a big one, 2311296661 which is greater than 100 million, whereas in the first example, the 2nd largest factor is a puny 947 -- but most important, I think, is that it smaller than 100 million. > Can you explain those timings? How did you measure them? Instead of just line 99 at the end of the pasted script, I had from time import clock as t t0 = t() print(factor_integer(2835334663465375591838337)) t1 = t() print("time =", round((t1-t0), 3)) """ OUTPUT: 2,835,334,663,465,375,591,838,337 [3, 19, 37, 71, 947, 19994908447741489] 1.121 """ Essentially the same algorithm is employed in the other script I pasted at , factor_random_integers(). I've run factor_random_integers a couple of more times, with num_integers, min_length, max_length = 10, 25, 25. See , "More output from factor_random_integers". Dick From crawlzone at gmail.com Thu Aug 23 03:57:23 2012 From: crawlzone at gmail.com (Ray Jones) Date: Wed, 22 Aug 2012 18:57:23 -0700 Subject: [Tutor] subprocess.Popen help...thanks In-Reply-To: <50349153.1040901@gmail.com> References: <5034384C.9080808@gmail.com> <50349153.1040901@gmail.com> Message-ID: <50358E03.7060304@gmail.com> Thanks to all who responded. I'm deeply into some of the links provided, and my understanding has greatly increased. Ray On 08/22/2012 12:59 AM, Andreas Perstinger wrote: > On 22.08.2012 03:39, Ray Jones wrote: >> Does anyone know of a link to a really good tutorial that would help me >> with subprocess.Popen? a tutorial that uses really small words and more >> examples than explanation? After 15 years of scripting, I'm ashamed to >> say that I'm still not all that familiar with input, output, pipes, etc. >> much beyond a simple 'ls | ws -l' or &2>/dev/null scenarios. The >> docs for Popen have left me completely boggled, and I'm not seeing much >> available on Google search. Any suggestions? > > What about this tutorial: > http://jimmyg.org/blog/2009/working-with-python-subprocess.html > > or Doug Hellmann's PyMOTW page about subprocess: > http://www.doughellmann.com/PyMOTW/subprocess/index.html > > Bye, Andreas > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From d at davea.name Thu Aug 23 05:39:46 2012 From: d at davea.name (Dave Angel) Date: Wed, 22 Aug 2012 23:39:46 -0400 Subject: [Tutor] pickle problems In-Reply-To: References: <5027C480.3080705@davea.name> <50352B01.4070903@pearwood.info> Message-ID: <5035A602.7010307@davea.name> On 08/22/2012 07:32 PM, Richard D. Moores wrote: > > > My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. You do know that this gmpy2 function is only statistically correct ? it can false positive. I don't know what the probs are, but it uses Miller-Rabin, with a default factor of 25. -- DaveA From rahool.shelke at gmail.com Thu Aug 23 08:12:19 2012 From: rahool.shelke at gmail.com (rahool shelke) Date: Thu, 23 Aug 2012 11:42:19 +0530 Subject: [Tutor] Help : File formatting Message-ID: Hi, I am beginner to python and doing coding on UNIX platforms(AIX,Linux,HP,Solaris) I want to take the list of files present in packages. On AIX we get the files list present in packges using command "lslpp -f ". Now i want to format output of above command such that only list of files with absolute paths to be taken out and put into separate list/dictionary. I am considering here the remote connectivity of hosts. Will you please guide me to get the solution for this ? -Rahul Shelke -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 23 09:40:16 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Aug 2012 08:40:16 +0100 Subject: [Tutor] Help : File formatting In-Reply-To: References: Message-ID: On 23/08/12 07:12, rahool shelke wrote: > I want to take the list of files present in packages. On AIX we get the > files list present in packges using command "lslpp -f ". Can you provide a short example of the output format from lslpp? > Now i want to format output of above command such that only list of > files with absolute paths to be taken out and put into separate > list/dictionary. Can you define precise rules for determining an absolute path? > I am considering here the remote connectivity of hosts. I'm not sure what you mean by that, can you elaborate? What are you considering? How to connect to them? How to specify them in the filenames? > Will you please guide me to get the solution for this ? The easiest way is for you to try what you think should work and post it along with what happened and your questions. That tells us your coding level and expectations. Please include the python version and the full text of any error messages not just a summary. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Thu Aug 23 10:43:14 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Aug 2012 18:43:14 +1000 Subject: [Tutor] pickle problems In-Reply-To: <5035A602.7010307@davea.name> References: <5027C480.3080705@davea.name> <50352B01.4070903@pearwood.info> <5035A602.7010307@davea.name> Message-ID: <20120823084313.GA4671@ando> On Wed, Aug 22, 2012 at 11:39:46PM -0400, Dave Angel wrote: > On 08/22/2012 07:32 PM, Richard D. Moores wrote: > > > > > > My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. > > You do know that this gmpy2 function is only statistically correct ? it > can false positive. I don't know what the probs are, but it uses > Miller-Rabin, with a default factor of 25. What Dave means is: - If gmpy2.is_prime says a number is not prime, then that is certainly true; - but if it says that a number is prime, then that is only probably true. With 25 Miller-Rabin tests, the probability of a false positive is (if I remember correctly) 1 time in 4**25 or about 1 time in a million billion. (That's American billion, 10**9, not old-style English billion.) If you tested a thousand prime numbers a second for thirty thousand years, you would expect perhaps one false positive. The odds are much higher that a hardware fault or stray cosmic ray hitting the CPU or memory chip will cause the computer to calculate the wrong answer. For anyone interested, here is my pyprimes module: http://pypi.python.org/pypi/pyprimes/ which is written in pure Python (no reliance on gmpy2), so you can actually see how the algorithms work, although it is correspondingly much slower. Source code can be found here: http://code.google.com/p/pyprimes/source/browse/src/pyprimes.py It is *extensively* documented. The API is alpha-quality and subject to change. -- Steven From pasokan at talentsprint.com Thu Aug 23 12:09:51 2012 From: pasokan at talentsprint.com (Asokan Pichai) Date: Thu, 23 Aug 2012 15:39:51 +0530 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: On Thu, Aug 23, 2012 at 2:57 AM, Pete O'Connell wrote: > On Thu, Aug 23, 2012 at 4:16 AM, Alan Gauld wrote: > >> theTextAsListNoVnOrVtOrEmptyLine = [x for x in theTextAsListStripped >> if "vn" not in x >> if "vt" not in x >> if x!= ""] >> >> It's slightly more verbose but it makes the rules more explicit, IMHO. > > I agree, it seems easier to read when written on multiple lines. I'll > do it that way, > Thanks > Pete You can look at the option of writing a function to do the selection part. If the function is named well and also corresponds to some domain operation or concept it can be very useful. def select(p): if p == "": return False return "vt" In p or "vn" in p .......... selectedLines = [ x for x in TextAsListStripped if select(x) ] ...... Asokan Pichai If a language is designed for non-programmers, soon only non-programs get written in it. --- Anonymouse From rdmoores at gmail.com Thu Aug 23 15:16:05 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 23 Aug 2012 06:16:05 -0700 Subject: [Tutor] pickle problems In-Reply-To: <5035A602.7010307@davea.name> References: <5027C480.3080705@davea.name> <50352B01.4070903@pearwood.info> <5035A602.7010307@davea.name> Message-ID: On Wed, Aug 22, 2012 at 8:39 PM, Dave Angel wrote: > On 08/22/2012 07:32 PM, Richard D. Moores wrote: >> >> >> My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. > > You do know that this gmpy2 function is only statistically correct ? Yes. See Steven's reply for the probabilities. Dick > it > can false positive. I don't know what the probs are, but it uses > Miller-Rabin, with a default factor of 25. From cecilia.chavana at gmail.com Thu Aug 23 15:18:55 2012 From: cecilia.chavana at gmail.com (Cecilia Chavana-Bryant) Date: Thu, 23 Aug 2012 14:18:55 +0100 Subject: [Tutor] What are all those letters after terminal commands? Message-ID: Hola, I'm going through the 'Command line crash course' by Zed Shaw, thanks to the people that recommended this book, its quite a good course, I can see what the author was going for with the title but if it wasn't for your recommendations, it would have put me off. At the beginning of Chapter 8 - Moving around (pushd, popd) on Source: 13 exercise 8 I found this command: mkdir -p i/like/icecream. I am guessing that the -p stands for directory path? I have seen other such letters sometimes with or without the ' - ' before them (I think) in commands so my question is, what are these letters for? what are they called? and could someone please point me to where I can find a list of these with descriptions of what they do. I have tried googling with no positive results as I don't know what they are called or I get just the information for the command they are used with. Many thanks in advance for the help, Cecilia -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Thu Aug 23 15:42:31 2012 From: wprins at gmail.com (Walter Prins) Date: Thu, 23 Aug 2012 14:42:31 +0100 Subject: [Tutor] What are all those letters after terminal commands? In-Reply-To: References: Message-ID: Hi Cecilia, On 23 August 2012 14:18, Cecilia Chavana-Bryant wrote: > recommendations, it would have put me off. At the beginning of Chapter 8 - > Moving around (pushd, popd) on Source: 13 exercise 8 I found this command: > mkdir -p i/like/icecream. I am guessing that the -p stands for directory > path? I have seen other such letters sometimes with or without the ' - ' > before them (I think) in commands so my question is, what are these letters > for? what are they called? and could someone please point me to where I can > find a list of these with descriptions of what they do. These are variously called command "switches" or "options", thus named because they switch certain behaviours on and off or specify optional information to the command. On *nix like systems, you can usually find full and comprehensive documentation on any command using the "man" (for manual) or "info" (in lieu of information) commands. For example, try: man mkdir In "mkdir"'s case, the default behaviour/convention is for mkdir's parameter to specify the single folder name to be created in the current working folder. The "-p" switch modifies this behaviour so that the parameter is instead interpreted to be a full path that may include several folders, and the behaviour is modified such that mkdir then will, as needed, created all the folders along the entire path specified. HTH, Walter From david at graniteweb.com Thu Aug 23 15:47:14 2012 From: david at graniteweb.com (David Rock) Date: Thu, 23 Aug 2012 08:47:14 -0500 Subject: [Tutor] What are all those letters after terminal commands? In-Reply-To: References: Message-ID: <20120823134714.GD28653@wdfs.gateway.2wire.net> * Cecilia Chavana-Bryant [2012-08-23 14:18]: > mkdir -p i/like/icecream. I am guessing that the -p stands for directory > path? I have seen other such letters sometimes with or without the ' - ' > before them (I think) in commands so my question is, what are these letters > for? what are they called? and could someone please point me to where I can They are called commandline options. Most programs allow you to use options to change the behavior of the program "on the fly." In this particular case, mkdir creates a directory, while mkdir -p means "make the directory and any parent directories that do not already exist. For example: If i/like/icecream does not exist, we try to create it with mkdir: mkdir i/like/icecream The result will be an error if i or i/like does not exist yet: mkdir: i/like: No such file or directory So we use -p, to also create the parent directories: mkdir -p i/like/icecream To learn about options that are available for a given command, try using the "man" command like so: man mkdir It will give you a list of options, what they do, and how to use them. In general, there are two things you can use on a commandline, options and arguments. Options are the switches that change the program's behavior and arguments are the inputs to the program (filenames, etc). -- David Rock david at graniteweb.com From __peter__ at web.de Thu Aug 23 15:51:00 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Aug 2012 15:51 +0200 Subject: [Tutor] What are all those letters after terminal commands? References: Message-ID: Cecilia Chavana-Bryant wrote: > Hola, > > I'm going through the 'Command line crash course' by Zed Shaw, thanks to > the people that recommended this book, its quite a good course, I can see > what the author was going for with the title but if it wasn't for your > recommendations, it would have put me off. At the beginning of Chapter 8 - > Moving around (pushd, popd) on Source: 13 exercise 8 I found this command: > mkdir -p i/like/icecream. I am guessing that the -p stands for directory > path? Arguments like -p or --parents are options that change the behaviour of a command or allow you to pass optional arguments to it. $ mkdir i/like/icecream will fail unless the directories i and i/like already exist. The leaf directory i/like/icecream must not yet exist. If the -p option is provided $ mkdir -p i/like/icecream will silently create all intermediate (or "parent", hence the abbreviation) directories and not complain if a directory i/like/icream already exists. You can see $ mkdir -p i/like/icecream as a shortcut for $ mkdir i $ mkdir i/like $ mkdir i/like/icecream > I have seen other such letters sometimes with or without the ' - ' > before them (I think) in commands so my question is, what are these > letters for? what are they called? and could someone please point me to > where I can find a list of these with descriptions of what they do. I have > tried > googling with no positive results as I don't know what they are called or > I get just the information for the command they are used with. For every shell command there is a "manpage" that describes what the command does and what options it will accept. You can display the manpage for mkdir with $ man mkdir > Many thanks in advance for the help, Cecilia Well, here is the place for basic help, but it should normally be about Python... From victoriahomsy at yahoo.com Thu Aug 23 16:17:36 2012 From: victoriahomsy at yahoo.com (Victoria Homsy) Date: Thu, 23 Aug 2012 15:17:36 +0100 (BST) Subject: [Tutor] Error message... Message-ID: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> Dear all,? Sorry to bother you with a beginner's problem again...? I have tried to write a program that can check if a string is a palindrome. My code is as follows: def isPalindrome(s): if len(s) <= 1: return True else: return s(0) == s(-1) and isPalindrome (s[1:-1]) isPalindrome('aba') However, when I try to run it in terminal I get the following error message:? Traceback (most recent call last): ? File "recursion.py", line 5, in ? ? isPalindrome('aba') ? File "recursion.py", line 3, in isPalindrome ? ? else: return s(0) == s(-1) and isPalindrome (s[1:-1]) TypeError: 'str' object is not callable I don't see why this wouldn't work... Many thanks in advance.? Kind regards, Victoria? -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Aug 23 16:28:04 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Aug 2012 16:28:04 +0200 Subject: [Tutor] Error message... References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> Message-ID: Victoria Homsy wrote: > Sorry to bother you with a beginner's problem again... This is the place for beginners. > I have tried to write a program that can check if a string is a > palindrome. My code is as follows: > > > def isPalindrome(s): > if len(s) <= 1: return True > else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > isPalindrome('aba') > > > However, when I try to run it in terminal I get the following error > message: > > Traceback (most recent call last): > File "recursion.py", line 5, in > isPalindrome('aba') > File "recursion.py", line 3, in isPalindrome > else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > TypeError: 'str' object is not callable > > > I don't see why this wouldn't work... If you want to get the nth charactor you have to put the index in brackets, not parens: >>> s = "foo" >>> s(0) # wrong, python tries to treat s as a function Traceback (most recent call last): File "", line 1, in TypeError: 'str' object is not callable >>> s[0] # correct 'f' From breamoreboy at yahoo.co.uk Thu Aug 23 16:29:25 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Aug 2012 15:29:25 +0100 Subject: [Tutor] Error message... In-Reply-To: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> Message-ID: On 23/08/2012 15:17, Victoria Homsy wrote: > Dear all, > > Sorry to bother you with a beginner's problem again... You're welcome as that's what we're here for. > > I have tried to write a program that can check if a string is a palindrome. My code is as follows: > > > def isPalindrome(s): > if len(s) <= 1: return True > else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > isPalindrome('aba') > > > However, when I try to run it in terminal I get the following error message: > > Traceback (most recent call last): > File "recursion.py", line 5, in > isPalindrome('aba') > File "recursion.py", line 3, in isPalindrome > else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > TypeError: 'str' object is not callable > > > I don't see why this wouldn't work... Always easier for another pair of eyes. The TypeError tells you exactly what the problem is. Just look very carefully at the return after the else and compare your use of the function parameter s. Then kick yourself and have another go :) > > Many thanks in advance. > > Kind regards, > > Victoria > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. From robert.day at merton.oxon.org Thu Aug 23 16:30:37 2012 From: robert.day at merton.oxon.org (Rob Day) Date: Thu, 23 Aug 2012 15:30:37 +0100 Subject: [Tutor] Error message... In-Reply-To: References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> Message-ID: On 23 August 2012 15:17, Victoria Homsy wrote: > > def isPalindrome(s): > if len(s) <= 1: return True > else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > > I don't see why this wouldn't work... > > Many thanks in advance. > > Kind regards, > > Victoria > Parentheses are used for function arguments in Python, whereas square brackets are used for slices - so the first character of s is not s(0) but s[0]. When you say s(0) and s(-1), Python thinks you're calling s as a function with 0 or -1 as the argument - hence, "str object is not callable". -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Aug 23 16:37:19 2012 From: d at davea.name (Dave Angel) Date: Thu, 23 Aug 2012 10:37:19 -0400 Subject: [Tutor] pickle problems In-Reply-To: <20120823084313.GA4671@ando> References: <5027C480.3080705@davea.name> <50352B01.4070903@pearwood.info> <5035A602.7010307@davea.name> <20120823084313.GA4671@ando> Message-ID: <5036401F.9060803@davea.name> On 08/23/2012 04:43 AM, Steven D'Aprano wrote: > On Wed, Aug 22, 2012 at 11:39:46PM -0400, Dave Angel wrote: >> On 08/22/2012 07:32 PM, Richard D. Moores wrote: >>> >>> >>> My code uses gmpy2.is_prime() (lines 79 and 89). is_prime() is VERY fast. >> You do know that this gmpy2 function is only statistically correct ? it >> can false positive. I don't know what the probs are, but it uses >> Miller-Rabin, with a default factor of 25. > What Dave means is: > > - If gmpy2.is_prime says a number is not prime, then that is > certainly true; > > - but if it says that a number is prime, then that is only > probably true. > > With 25 Miller-Rabin tests, the probability of a false positive is (if I > remember correctly) 1 time in 4**25 or about 1 time in a million > billion. (That's American billion, 10**9, not old-style English > billion.) If you tested a thousand prime numbers a second for thirty > thousand years, you would expect perhaps one false positive. > > The odds are much higher that a hardware fault or stray cosmic ray > hitting the CPU or memory chip will cause the computer to calculate the > wrong answer. > I knew of the Miller Rabin test, but I did not know the probabilities involved. I'm a little surprised I didn't see any docs on the gmpy2 site that gave such probabilities. it defaults to 25, without explaining how that number relates to anything in the wikipedia article. https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Does anyone know if the gmp2 code is testing the sequence described in wikipedia, where a is 2, 3, 5, 7, 11, 13, and 17 ? If that relates directly, it would seem that 7 tests would give 100% confidence for n up to 14+ digits. -- DaveA From victoriahomsy at yahoo.com Thu Aug 23 16:42:57 2012 From: victoriahomsy at yahoo.com (Victoria Homsy) Date: Thu, 23 Aug 2012 15:42:57 +0100 (BST) Subject: [Tutor] Error message... In-Reply-To: References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> Message-ID: <1345732977.96359.YahooMailNeo@web29501.mail.ird.yahoo.com> Excellent - thank you so much everyone. All is clear now!!? ________________________________ From: Mark Lawrence To: tutor at python.org Sent: Thursday, 23 August 2012, 15:29 Subject: Re: [Tutor] Error message... On 23/08/2012 15:17, Victoria Homsy wrote: > Dear all, > > Sorry to bother you with a beginner's problem again... You're welcome as that's what we're here for. > > I have tried to write a program that can check if a string is a palindrome. My code is as follows: > > > def isPalindrome(s): > if len(s) <= 1: return True > else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > isPalindrome('aba') > > > However, when I try to run it in terminal I get the following error message: > > Traceback (most recent call last): >? ? File "recursion.py", line 5, in >? ? ? isPalindrome('aba') >? ? File "recursion.py", line 3, in isPalindrome >? ? ? else: return s(0) == s(-1) and isPalindrome (s[1:-1]) > TypeError: 'str' object is not callable > > > I don't see why this wouldn't work... Always easier for another pair of eyes.? The TypeError tells you exactly what the problem is.? Just look very carefully at the return after the else and compare your use of the function parameter s.? Then kick yourself and have another go :) > > Many thanks in advance. > > Kind regards, > > Victoria > > > > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From wrw at mac.com Thu Aug 23 15:51:26 2012 From: wrw at mac.com (William Ray Wing) Date: Thu, 23 Aug 2012 09:51:26 -0400 Subject: [Tutor] What are all those letters after terminal commands? In-Reply-To: References: Message-ID: <8DDC4585-520C-49A1-8326-86A41405491E@mac.com> On Aug 23, 2012, at 9:18 AM, Cecilia Chavana-Bryant wrote: > Hola, > > I'm going through the 'Command line crash course' by Zed Shaw, thanks to the people that recommended this book, its quite a good course, I can see what the author was going for with the title but if it wasn't for your recommendations, it would have put me off. At the beginning of Chapter 8 - Moving around (pushd, popd) on Source: 13 exercise 8 I found this command: mkdir -p i/like/icecream. I am guessing that the -p stands for directory path? I have seen other such letters sometimes with or without the ' - ' before them (I think) in commands so my question is, what are these letters for? what are they called? and could someone please point me to where I can find a list of these with descriptions of what they do. I have tried googling with no positive results as I don't know what they are called or I get just the information for the command they are used with. > > Many thanks in advance for the help, Cecilia > _______________________________________________ > Those letters are options (sometimes called switches) and they modify the action of the command. Thus "$ ls" gives you a very terse list of the files in a directory. $ ls -a gives you a list of all the files, including invisible ones. $ ls -l gives you a long list that includes size, date, and protections. And $ ls -al does both. If you open a terminal session and enter $ man ls, you will get the "man page" for the ls command that will document these and several others. Don't worry you're doing great.. Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: From ron.painter.coding at gmail.com Thu Aug 23 17:10:57 2012 From: ron.painter.coding at gmail.com (Ron Painter) Date: Thu, 23 Aug 2012 11:10:57 -0400 Subject: [Tutor] Hello Python Tutor - help please! Message-ID: Hi, Cecilia: I came across your posts when catching up with my tutor-request digest emails. I did not see the Udacity site mentioned--if it was, my apologies for the repetition. Udacity.com, a free online education service, offers a number of high-quality courses. They include interactive quizzes using code sandboxes. The courses are 7 weeks long with online office hours and forums. If you do not want to wait for the next start date, you can take the courses online without getting instructor responses to students' posts. Free Interactive Computer Courses (interaction via online quizzes using code sandboxes): http://www.udacity.com/ Intro to Computer Science Python -- Beginner -- Project create functional search engine http://www.udacity.com/view#Course/cs101/CourseRev/apr2012/Unit/671001/Nugget/675002 Programming Languages Python, JavaScript -- Intermediate -- Project: Build a Functional Web Browser http://www.udacity.com/view#Course/cs262/CourseRev/apr2012/Unit/3001/Nugget/5001 Web Application Engineering Python -- Intermediate -- Project: Build a Functional Blog http://www.udacity.com/view#Course/cs253/CourseRev/apr2012/Unit/4001/Nugget/5002 Other courses available. Best regards, Ron Painter -------------- next part -------------- An HTML attachment was scrubbed... URL: From Steve.Flynn at capita.co.uk Thu Aug 23 16:42:16 2012 From: Steve.Flynn at capita.co.uk (Flynn, Stephen (L & P - IT)) Date: Thu, 23 Aug 2012 15:42:16 +0100 Subject: [Tutor] Python 3.2: processing text files in binary mode, because I want to remove carriage returns and line feeds... Message-ID: Python 3.2, as in the subject, although I also have 2.7 on this machine too. I have some data which contains text separated with field delimiters (|~) and a record terminator (||) 123456009999990|~52299999|~9999990|~0|~4|~1|~2006-09-08|~13:29:39|~some text.|~xxxxxxx, xxxxx|~|| 123456009999991|~52299999|~1999999|~0|~4|~1|~2009-06-05|~15:25:25|~some more text|~xxxxx, xxxxxxa|~|| 123456009999992|~51199999|~9999998|~8253265|~5|~11|~2011-07-19|~16:55:03 |~Some Split text over serveral lines |~Aldxxxxe, Mxxxx|~|| 123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06:53 |~Yet more text: which has been split up with carriage returns, line feeds or possibly both, depending upon your operating system. |~Imxxx, xxxxxxed|~|| I'm trying to reformat this data so that each record terminated with a "||" is on a single line, as in 123456009999990|~52299999|~9999990|~0|~4|~1|~2006-09-08|~13:29:39|~some text.|~xxxxxxx, xxxxx|~|| 123456009999991|~52299999|~1999999|~0|~4|~1|~2009-06-05|~15:25:25|~some more text|~xxxxx, xxxxxxa|~|| 123456009999992|~51199999|~9999998|~8253265|~5|~11|~2011-07-19|~16:55:03 |~Some Split text over serveral lines|~Aldxxxxe, Mxxxx|~|| 123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06:53 |~Yet more text: which has been split up with carriage returns, line feeds or possibly both, depending upon your operating system.|~Imxxx, xxxxxxed|~|| I've written the following code as a first attempt: ifile=r"C:\Documents and Settings\flynns\Desktop\sample-DCLTBCNTH.txt" ofile=r"C:\Documents and Settings\flynns\Desktop\output-DCLTBCNTH.txt" f=open(ifile, mode="rb") out=open(ofile, mode="w") line=f.readline() while (line) : if '||' in str(line): print(str(line), file=out) else: print(str(line), end='', file=out) line=f.readline() if __name__ == '__main__': pass The code attempts to read each line of the input file, and if it contains a "||", print the line to an output file. If it doesn't contain a "||" it emits the record without any carriage returns or line feeds and grabs another line from the input file. Whilst the "logic" seems to be working the output file I get out looks like this: b'123456009999990|~52299999|~9999990|~0|~4|~1|~2006-09-08|~13:29:39|~som e text.|~xxxxxxx, xxxxx|~||\r\n' b'123456009999991|~52299999|~1999999|~0|~4|~1|~2009-06-05|~15:25:25|~som e more text|~xxxxx, xxxxxxa|~||\r\n' b'123456009999992|~51199999|~9999998|~8253265|~5|~11|~2011-07-19|~16:55: 03|~Some Split test over\r\n'b' serveral\r\n'b' lines\r\n'b'|~Aldxxxxe, Mxxxx|~||\r\n' b'123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06: 53|~Yet more text: \r\n'b' which has been split up with\r\n'b' carriage returns, line feeds or possibly both, depending upon your operating system.\r\n'b'|~Imxxx, xxxxxxed|~||\r\n' This makes sense to me as I'm writing the file out in text mode and the \r and \n in the input stream are being interpreted as simple text. However, if I try to write the file out in binary mode, I get a traceback: Traceback (most recent call last): File "C:\Documents and Settings\flynns\workspace\joinlines\joinlines\joinlines.py", line 10, in print(str(line), file=out) TypeError: 'str' does not support the buffer interface Is there a method of writing out a binary mode file via print() and making use of the end keyword? If there's not, I presume I'll need to remove the \r\n from "line" in my else: section and push the amended data out via an out.write(line). How does one amend bytes in a "line" object Steve Flynn This email and any attachment to it are confidential. Unless you are the intended recipient, you may not use, copy or disclose either the message or any information contained in the message. If you are not the intended recipient, you should delete this email and notify the sender immediately. Any views or opinions expressed in this email are those of the sender only, unless otherwise stated. All copyright in any Capita material in this email is reserved. All emails, incoming and outgoing, may be recorded by Capita and monitored for legitimate business purposes. Capita exclude all liability for any loss or damage arising or resulting from the receipt, use or transmission of this email to the fullest extent permitted by law. From victoriahomsy at yahoo.com Thu Aug 23 17:33:04 2012 From: victoriahomsy at yahoo.com (Victoria Homsy) Date: Thu, 23 Aug 2012 16:33:04 +0100 (BST) Subject: [Tutor] Error message... In-Reply-To: <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> Message-ID: <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Dear All - sorry to bother you. I just tried to run this program: def isPalindrome(s): if len(s) <= 1: return True? else: return s[0] == s[-1] and isPalindrome (s[1:-1]) isPalindrome('aba') However when I run it in terminal it doesn't give me any answer - True or False. (I want the program to tell me whether the input string is True or False). In order to get an answer, I assume I would need to tell the program to print something. However I'm not sure where in the program I would do this. I tried this: def isPalindrome(s): if len(s) <= 1: return True and print "True" else: return s[0] == s[-1] and isPalindrome (s[1:-1]) isPalindrome('aba') However, this does not work - I get another error message.? Could somebody advise what I'm doing wrong here? Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mmistroni at gmail.com Thu Aug 23 17:39:03 2012 From: mmistroni at gmail.com (Marco Mistroni) Date: Thu, 23 Aug 2012 16:39:03 +0100 Subject: [Tutor] NTLM authentication Message-ID: Hi all i was wondering if anyone coud provide examples on how to open an URL that requires NTLM authentication i have tried to use python-ntml but it does not seems to work as i keep on getting this error lib\python2.6\ntlm\ntlm.py", line 219, in parse_NTLM_CHALLENGE_MESSAGE error: unpack requires a string argument of length 4 could anyone assist pls? w/kindest regards marco -------------- next part -------------- An HTML attachment was scrubbed... URL: From mmistroni at gmail.com Thu Aug 23 17:42:51 2012 From: mmistroni at gmail.com (Marco Mistroni) Date: Thu, 23 Aug 2012 16:42:51 +0100 Subject: [Tutor] NTLM authentication, python 2.6 and windows Message-ID: Hi all i was wondering if anyone coud provide examples on how to open an URL that requires NTLM authentication i have tried to use python-ntml but it does not seems to work as i keep on getting this errorlib\python2.6\ntlm\ntlm.py", line 219, in parse_NTLM_CHALLENGE_MESSAGEerror: unpack requires a string argument of length 4 I am running the client code on python 2.6, on a windows 7 machine, and i am connecting to an apache tomcat server running on a windows xp machine could anyone assist pls? > > w/kindest regards > marco > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Aug 23 18:13:46 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Aug 2012 02:13:46 +1000 Subject: [Tutor] NTLM authentication, python 2.6 and windows In-Reply-To: References: Message-ID: <503656BA.9060904@pearwood.info> On 24/08/12 01:42, Marco Mistroni wrote: > Hi all > i was wondering if anyone coud provide examples on how to open an URL > that requires NTLM authentication > i have tried to use python-ntml but it does not seems to work as i keep on > getting this errorlib\python2.6\ntlm\ntlm.py", line 219, in > parse_NTLM_CHALLENGE_MESSAGEerror: unpack requires a string argument of > length 4 > > I am running the client code on python 2.6, on a windows 7 machine, and i > am connecting to an apache tomcat server running on a windows xp machine Since Python is cross-platform, the operating system is usually the least important piece of information you need to supply. What is actually important is the code you are running, and the exact error message you get. Show us the code you use, what you expect it to do, and the full traceback you get. Copy and paste the full traceback: everything from the line "Traceback (most recent call last)" all the way to the end of the error message. You might also like to read this web site for hints on how to ask good questions that will get good answers -- and sometimes even answer them yourself: http://sscce.org/ -- Steven From __peter__ at web.de Thu Aug 23 18:16:40 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 23 Aug 2012 18:16:40 +0200 Subject: [Tutor] Python 3.2: processing text files in binary mode, because I want to remove carriage returns and line feeds... References: Message-ID: Flynn, Stephen (L & P - IT) wrote: > Python 3.2, as in the subject, although I also have 2.7 on this machine > too. > > > > I have some data which contains text separated with field delimiters > (|~) and a record terminator (||) > > 123456009999990|~52299999|~9999990|~0|~4|~1|~2006-09-08|~13:29:39|~some > text.|~xxxxxxx, xxxxx|~|| > 123456009999991|~52299999|~1999999|~0|~4|~1|~2009-06-05|~15:25:25|~some > more text|~xxxxx, xxxxxxa|~|| > 123456009999992|~51199999|~9999998|~8253265|~5|~11|~2011-07-19|~16:55:03 > |~Some Split text over > serveral > lines > |~Aldxxxxe, Mxxxx|~|| > 123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06:53 > |~Yet more text: > which has been split up with > carriage returns, line feeds or possibly both, depending upon your > operating system. > |~Imxxx, xxxxxxed|~|| > > > I'm trying to reformat this data so that each record terminated with a > "||" is on a single line, as in > > 123456009999990|~52299999|~9999990|~0|~4|~1|~2006-09-08|~13:29:39|~some > text.|~xxxxxxx, xxxxx|~|| > 123456009999991|~52299999|~1999999|~0|~4|~1|~2009-06-05|~15:25:25|~some > more text|~xxxxx, xxxxxxa|~|| > 123456009999992|~51199999|~9999998|~8253265|~5|~11|~2011-07-19|~16:55:03 > |~Some Split text over serveral lines|~Aldxxxxe, Mxxxx|~|| > 123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06:53 > |~Yet more text: which has been split up with carriage returns, line > feeds or possibly both, depending upon your operating system.|~Imxxx, > xxxxxxed|~|| > > > > I've written the following code as a first attempt: > > ifile=r"C:\Documents and Settings\flynns\Desktop\sample-DCLTBCNTH.txt" > ofile=r"C:\Documents and Settings\flynns\Desktop\output-DCLTBCNTH.txt" > > f=open(ifile, mode="rb") > out=open(ofile, mode="w") > line=f.readline() > > while (line) : > if '||' in str(line): > print(str(line), file=out) > else: > print(str(line), end='', file=out) > line=f.readline() > > if __name__ == '__main__': > pass > > > The code attempts to read each line of the input file, and if it > contains a "||", print the line to an output file. If it doesn't contain > a "||" it emits the record without any carriage returns or line feeds > and grabs another line from the input file. > > Whilst the "logic" seems to be working the output file I get out looks > like this: > > b'123456009999990|~52299999|~9999990|~0|~4|~1|~2006-09-08|~13:29:39|~som > e text.|~xxxxxxx, xxxxx|~||\r\n' > b'123456009999991|~52299999|~1999999|~0|~4|~1|~2009-06-05|~15:25:25|~som > e more text|~xxxxx, xxxxxxa|~||\r\n' > b'123456009999992|~51199999|~9999998|~8253265|~5|~11|~2011-07-19|~16:55: > 03|~Some Split test over\r\n'b' serveral\r\n'b' lines\r\n'b'|~Aldxxxxe, > Mxxxx|~||\r\n' > b'123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06: > 53|~Yet more text: \r\n'b' which has been split up with\r\n'b' carriage > returns, line feeds or possibly both, depending upon your operating > system.\r\n'b'|~Imxxx, xxxxxxed|~||\r\n' > > This makes sense to me as I'm writing the file out in text mode and the > \r and \n in the input stream are being interpreted as simple text. > > However, if I try to write the file out in binary mode, I get a > traceback: > > Traceback (most recent call last): > File "C:\Documents and > Settings\flynns\workspace\joinlines\joinlines\joinlines.py", line 10, in > > print(str(line), file=out) > TypeError: 'str' does not support the buffer interface > > > Is there a method of writing out a binary mode file via print() and > making use of the end keyword? > > > If there's not, I presume I'll need to remove the \r\n from "line" in my > else: section and push the amended data out via an out.write(line). How > does one amend bytes in a "line" object In binary mode the lines you are reading are bytes not str objects. If you want to convert from bytes to str use the decode method. Compare: >>> line = b"whatever" >>> print(str(line)) b'whatever' >>> print(line.decode()) whatever However, I don't see why you have to open your file in binary mode. Something like with open(infile) as instream: with open(outfile, "w") as outstream: for line in instream: if not line.endswith("||\n"): line = line.rstrip("\n") outstream.write(line) should do the right thing. From steve at pearwood.info Thu Aug 23 18:26:15 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Aug 2012 02:26:15 +1000 Subject: [Tutor] Error message... In-Reply-To: <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Message-ID: <503659A7.5070000@pearwood.info> On 24/08/12 01:33, Victoria Homsy wrote: > Dear All - sorry to bother you. I just tried to run this program: > > > def isPalindrome(s): > if len(s)<= 1: return True > else: return s[0] == s[-1] and isPalindrome (s[1:-1]) > isPalindrome('aba') > > > However when I run it in terminal it doesn't give me any answer - >True or False. (I want the program to tell me whether the input >string is True or False). In order to get an answer, I assume I >would need to tell the program to print something. However I'm >not sure where in the program I would do this. I tried this: > > def isPalindrome(s): > if len(s)<= 1: return True and print "True" > else: return s[0] == s[-1] and isPalindrome (s[1:-1]) > isPalindrome('aba') > > However, this does not work - I get another error message. Would you like to tell us what error message you get, or should we try to guess? I love guessing games! Ah, who am I fooling? I hate guessing games. It's always best if you copy and paste the full traceback you get, starting with the line "Traceback (most recent call last)" all the way to the end of the error message. In this case, I can guess that you are getting a SyntaxError. Am I close? If I'm right, you can read the SyntaxError and it will give you a hint as to where to look for the error: py> if len(s) <= 1: return True and print True File "", line 1 if len(s) <= 1: return True and print True ^ SyntaxError: invalid syntax See the caret ^ printed just below the offending line of source code and just above the message that it is a syntax error? In the terminal window, that will point to the first part of the line which Python doesn't understand. In this case, you're giving Python instructions in English, and it's not that smart. A human being might understand what you mean by "return True and print True", but that's invalid Python code. You need to separate that into two separate operations: 1) The isPalindrome function you write is responsible for returning the True or False flag, nothing more. 2) The piece of code that calls the function is responsible for printing the flag. So in this case, your isPalindrome function must return a flag: def isPalindrome(s): if len(s) <= 1: return True else: return s[0] == s[-1] and isPalindrome (s[1:-1]) And the caller is responsible for printing the result: result = isPalindrome('aba') print result Those two lines can be simplified to one line: print isPalindrome('aba') -- Steven From breamoreboy at yahoo.co.uk Thu Aug 23 18:36:14 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Aug 2012 17:36:14 +0100 Subject: [Tutor] Error message... In-Reply-To: <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Message-ID: On 23/08/2012 16:33, Victoria Homsy wrote: > > > > > Dear All - sorry to bother you. I just tried to run this program: > > > def isPalindrome(s): > if len(s) <= 1: return True > else: return s[0] == s[-1] and isPalindrome (s[1:-1]) > isPalindrome('aba') > > > However when I run it in terminal it doesn't give me any answer - True or False. (I want the program to tell me whether the input string is True or False). In order to get an answer, I assume I would need to tell the program to print something. However I'm not sure where in the program I would do this. I tried this: > > def isPalindrome(s): > if len(s) <= 1: return True and print "True" > else: return s[0] == s[-1] and isPalindrome (s[1:-1]) > isPalindrome('aba') > > However, this does not work - I get another error message. > > Could somebody advise what I'm doing wrong here? Thank you. You're not spending enough time thinking, seriously. In your original attempt you've got isPalindrome which returns True or False. You call the function but don't do anything with the return value, so it's simply discarded. Then you mess around with a perfectly good function instead of fixing the real problem. You've two options. The simplest is :- print 'isPalindrome returned', isPalindrome('aba') The alternative which is used when you want to keep using a return value is :- status = isPalindrome('aba') print 'isPalindrome returned', status > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Whoops might have helped if I'd hit "Send". -- Cheers. Mark Lawrence. From steve at pearwood.info Thu Aug 23 18:46:23 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Aug 2012 02:46:23 +1000 Subject: [Tutor] Python 3.2: processing text files in binary mode, because I want to remove carriage returns and line feeds... In-Reply-To: References: Message-ID: <50365E5F.3020406@pearwood.info> On 24/08/12 00:42, Flynn, Stephen (L & P - IT) wrote: > Python 3.2, as in the subject, although I also have 2.7 on this machine > too. > > > > I have some data which contains text separated with field delimiters > (|~) and a record terminator (||) [trim well over 50 lines of explanation] > Is there a method of writing out a binary mode file via print() and > making use of the end keyword? Please try to simplify your examples when posting them! You give an enormous amount of detail which simply isn't relevant to the question you end up asking. I acknowledge that you did try to make some attempt to trim the extraneous detail in your post, e.g.: |~Some Split text over serveral lines|~Aldxxxxe, Mxxxx|~|| 123456009999993|~59999999|~2999999|~8253265|~5|~11|~2011-07-11|~15:06:53 but still, your lead up to the question is intimidatingly large. You may find it useful to read this website: http://sscce.org/ for some ideas on how to ask short, to the point questions that will get good responses. In this case, to answer your question, no, I don't believe that you can write binary data to a file using print in Python 3, or at least, if you can, it isn't obvious and you probably shouldn't do so. print generates strings, and tries to write the string to the file. But a file in binary mode cannot take strings. It needs binary data. So if you try, you just get an error: py> data = b"abc1234" # Bytes objects b"..." is binary data. py> f = open("/tmp/rubbish.data", "wb") py> print(data, file=f) Traceback (most recent call last): File "", line 1, in TypeError: 'str' does not support the buffer interface A slightly cryptic error message, but clear enough if I try to write directly to the open file with a string: py> f.write("hello") Traceback (most recent call last): File "", line 1, in TypeError: 'str' does not support the buffer interface Since print automatically converts its argument into a string, this cannot work. If you have binary data, the best way to write it out to a file is by writing it directly to the file: py> data = b"abc1234" py> f = open("/tmp/rubbish.data", "wb") py> f.write(data) 7 > If there's not, I presume I'll need to remove the \r\n from "line" in my > else: section and push the amended data out via an out.write(line). How > does one amend bytes in a "line" object line = line.rstrip() will remove any whitespace, including \r and \n, from the right-hand side of the line. -- Steven From alan.gauld at btinternet.com Thu Aug 23 18:55:32 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Aug 2012 17:55:32 +0100 Subject: [Tutor] NTLM authentication, python 2.6 and windows In-Reply-To: References: Message-ID: On 23/08/12 16:42, Marco Mistroni wrote: > i have tried to use python-ntml but it does not seems to work as i keep > on getting thiserror > lib\python2.6\ntlm\ntlm.py", line 219, in parse_NTLM_CHALLENGE_MESSAGEerror: > unpack requires a string argument of length 4 It looks as if its working just fine. It looks like the user is not providing the right information, specifically "a string argument of length 4" But without any idea of what the code looks like I can only guess. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Thu Aug 23 18:59:26 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Aug 2012 02:59:26 +1000 Subject: [Tutor] What are all those letters after terminal commands? In-Reply-To: References: Message-ID: <5036616E.6060703@pearwood.info> On 23/08/12 23:18, Cecilia Chavana-Bryant wrote: [...] > I found this command: > mkdir -p i/like/icecream. I am guessing that the -p stands for directory > path? Ha, that's the trouble with command line interfaces -- they tend to end up being cryptic and painfully terse. In this case, -p actually stands for "parents", in the sense that mkdir is trying to create the folder "icecream" inside the parent folder "like", inside the grandparent folder "i". If any of the parent folders are missing, the -p option says to create the missing folders. I don't want to put you off learning about the command line, because knowledge is good. I've never learned something and then thought "I wish I was more ignorant". But honestly, you don't need to be a command line expert to make use of Python's interactive interpreter. To get started, all you need is one command: python and then press the ENTER key. That brings up Python's interactive interpreter, which uses Python syntax rather than the shell's rather cryptic commands and options. (Of course, the python command also takes a bunch of optional, and useful. command switches, but you can learn them as you go.) By all means continue with the command line book if you are getting something useful out of it, but don't think you *have* to learn the shell in order to use Python. The two are independent. -- Steven From afowler2 at broncos.uncfsu.edu Thu Aug 23 19:02:04 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Thu, 23 Aug 2012 17:02:04 +0000 Subject: [Tutor] Question Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> I am trying to complete an assignment and I am stuck at the if-else statements area. Could someone please help me? the instructions and what I have so far are below... Instructions: Your "main" function should do the following: (1) create an empty list; (2) ask the user if he/she wants to perform a list operation. if "yes": (a) prompt the user for the operation: "test", "peek", "add", or "remove"; (b) perform the operation: (i) if "test" then print whether the list is empty or not; (ii) if "peek" then print the number at the beginning of the list (with a label) but don't remove it from the list; (iii) if "add", then prompt the user for a number and add it to the end of the list; (iv) if "remove", then delete the number at the beginning of the list and print it (with a label); (c) print the entire list from beginning to end; repeat step (2) until the user enters "no". What I have so far.. def main(): l = list() x = eval(input('Enter a number: ')) while x >= 0: l.append(x) x = eval(input('Enter a number: ')) ask = input (" Do you want to perform a list operation?") if "yes": input (" Do you want to test, peek, add, or remove?") if "test": if not l: print("The list is not empty") else: print("The list is empty") elif "peek": print(l[0]) -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 23 19:59:29 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Aug 2012 18:59:29 +0100 Subject: [Tutor] Question In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 23/08/12 18:02, Ashley Fowler wrote: > def main(): > l = list() > x = eval(input('Enter a number: ')) Don;t use eval() its bad practicecand fort advanced use only. Instead explicitly convert to int() or float() > while x >= 0: > l.append(x) > x = eval(input('Enter a number: ')) Same here. > ask = input (" Do you want to perform a list operation?") > if "yes": You need to test if ask is equal to 'yes' Testing 'yes' directly will always be true because 'yes' always exists. > input (" Do you want to test, peek, add, or remove?") you need to store the reurn value from input as you did above > if "test": and use that stored value in the test. > if not l: > print("The list is not empty") > else: > print("The list is empty") HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From norman at khine.net Thu Aug 23 20:33:19 2012 From: norman at khine.net (Norman Khine) Date: Thu, 23 Aug 2012 19:33:19 +0100 Subject: [Tutor] better way to write this code Message-ID: Hello, I have this code (http://pastie.org/4575790) which pulls data from a list and then modifies some of the values such as the 'yield' entry, which has entries like: 21 15 ≤ 1000 ≤ 20 2.2 - 30 so that they are cleaned up. # -*- coding: UTF-8 -*- # Norman Khine import operator, json from BeautifulSoup import BeautifulSoup combos={0: 'id', 2: 'country', 3: 'type', 5: 'lat', 6: 'lon', 12: 'name' } TABLE_CONTENT = [['958','','USA','Atmospheric','1945/07/16 11:29:45','33.6753','-106.4747','','-.03','21','','','TRINITY',' ',' ','SourceData',' '],['959','','USA','Atmospheric','1945/08/05 23:16:02','34.395','132.4538','','-.58','15','','','LITTLEBOY',' ',' ','SourceData',' '],['1906','','GBR','Atmospheric','1958/08/22 17:24:00','1.67','-157.25','',' ',' ≤ 1000','','','Pennant 2',' ',' ','SourceData',' '],['28','','USA','Underground','1961/09/16 19:45:00','37.048','-116.034','0','.098',' ≤ 20','','','SHREW',' ',' ','SourceData','US Yucca Flat'],['5393637','','PRK','Underground','2009/05/25 00:54:45','41.2925','129.0657','','0','2.2 - 30','4.7','45','2009 North Korean Nuclear Test','Bulletin','TimeRes','SourceData','NK2009']] event_list = [] for event in TABLE_CONTENT: event_dict = {} for index, item in enumerate(event): if index == 8: if item == ' ': event_dict['depth'] = '0' else: event_dict['depth'] = item if index == 9: try: items = item.split() if len(items) >= 2: event_dict['yield'] = items[-1] else: if item == ' ': event_dict['yield'] = '10' else: event_dict['yield'] = item except: pass if index == 4: soup = BeautifulSoup(item) for a in soup.findAll('a'): event_dict['date'] = ''.join(a.findAll(text=True)) if index == 3: if 'Atmospheric' in item: event_dict['fill'] = 'red' if 'Underground' in item: event_dict['fill'] = 'green' elif index in combos: event_dict[combos[index]]=item event_list.append(event_dict) print event_dict event_list = sorted(event_list, key = operator.itemgetter('id')) f = open('detonations.json', 'w') f.write(json.dumps(event_list)) f.close() print 'detonations.json, written!' this then produces the .json file such as: [{"name": "Pennant 2", "country": "GBR", "lon": "-157.25", "yield": "1000", "lat": "1.67", "depth": "0", "date": "1958/08/22 17:24:00", "id": "1906", "fill": "red"}, {"name": "SHREW", "country": "USA", "lon": "-116.034", "yield": "20", "lat": "37.048", "depth": ".098", "date": "1961/09/16 19:45:00", "id": "28", "fill": "green"}, {"name": "2009 North Korean Nuclear Test", "country": "PRK", "lon": "129.0657", "yield": "30", "lat": "41.2925", "depth": "0", "date": "2009/05/25 00:54:45", "id": "5393637", "fill": "green"}, {"name": "TRINITY", "country": "USA", "lon": "-106.4747", "yield": "21", "lat": "33.6753", "depth": "-.03", "date": "1945/07/16 11:29:45", "id": "958", "fill": "red"}, {"name": "LITTLEBOY", "country": "USA", "lon": "132.4538", "yield": "15", "lat": "34.395", "depth": "-.58", "date": "1945/08/05 23:16:02", "id": "959", "fill": "red"} can the code be improved further? also, the content has 2,153 items, what will be the correct way to have this in a separate file and import this within this file to work on it? any advice much appreciated. norman -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From nielsen.jared at gmail.com Thu Aug 23 21:05:38 2012 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Thu, 23 Aug 2012 12:05:38 -0700 Subject: [Tutor] how to split/partition a string on keywords? Message-ID: Hi all, I'm new to programming and Python. I want to write a script that takes a string input and breaks the string at keywords then outputs the pieces on separate lines. I'm not sure how to break the string, though. I looked through the docs and found split() and partition(), which come close. But split() doesn't retain the separator and partition() retains the white space and returns a 3-tuple which I'll have to figure out how to rejoin nor does it partition on subsequent instances of the separator. Here's the script in its basic form: #!/usr/bin/python text = raw_input("Enter text: ") print "You entered ", text objects = text.partition(' and') print objects for object in objects: # Second Example print object For example, if I run this with the input: "Ham and cheese omelette with hasbrowns and coffee." I get: Ham and cheese omelette with hashbrowns and coffee. Any help is greatly appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From afowler2 at broncos.uncfsu.edu Thu Aug 23 20:36:13 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Thu, 23 Aug 2012 18:36:13 +0000 Subject: [Tutor] Question In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com>, Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD97@BL2PRD0710MB363.namprd07.prod.outlook.com> ________________________________________ From: tutor-bounces+afowler2=broncos.uncfsu.edu at python.org [tutor-bounces+afowler2=broncos.uncfsu.edu at python.org] on behalf of Alan Gauld [alan.gauld at btinternet.com] Sent: Thursday, August 23, 2012 5:59 PM To: tutor at python.org Subject: Re: [Tutor] Question On 23/08/12 18:02, Ashley Fowler wrote: > def main(): > l = list() > x = eval(input('Enter a number: ')) Don;t use eval() its bad practicecand fort advanced use only. Instead explicitly convert to int() or float() > while x >= 0: > l.append(x) > x = eval(input('Enter a number: ')) Same here. > ask = input (" Do you want to perform a list operation?") > if "yes": You need to test if ask is equal to 'yes' Testing 'yes' directly will always be true because 'yes' always exists. > input (" Do you want to test, peek, add, or remove?") you need to store the reurn value from input as you did above > if "test": and use that stored value in the test. > if not l: > print("The list is not empty") > else: > print("The list is empty") HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ______________________________________________________________________________________________________________________ REPLY: Thank you I finally fixed it. Can anyone break down how to do the next step which is: "You also need to write a function "printList" of one parameter that takes a list as its input and neatly prints the entire contents of the list in a column." _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From kwpolska at gmail.com Thu Aug 23 21:13:24 2012 From: kwpolska at gmail.com (Kwpolska) Date: Thu, 23 Aug 2012 21:13:24 +0200 Subject: [Tutor] Question In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: (resent due to sending off-list by mistake) Let's begin with telling you what you did wrong here. A fixed and completed code is below. (> = okay, # = modified/deleted; python code in `backticks`) On Thu, Aug 23, 2012 at 7:02 PM, Ashley Fowler wrote: > def main(): # l = list() l = [] is more standard. # x = eval(input('Enter a number: ')) int(), I can happily enter `import shutil; shutil.rmtree('/', True)` (replace '/' with 'C:\\' on Windows) and your system is dead. Also, why do you ask for a number now? You weren't supposed to. # while x >= 0: # l.append(x) # x = eval(input('Enter a number: ')) `while True:` goes there. You need to re-indent the following lines. # ask = input (" Do you want to perform a list operation?") drop the space in front of ( and after ". same below. You may want to add one after the question mark, though. > if "yes": `if ask == 'yes':`! # input (" Do you want to test, peek, add, or remove?") # if "test": same goes here. `ask=input; if ask=='test':`. > if not l: > print("The list is not empty") > else: > print("The list is empty") > # elif "peek": `elif ask == 'peek'` > print(l[0]) Then, you need to add more stuff. I did that for you: http://paste.pound-python.org/show/25126/ As a bonus, I made it so "yes", "YES", or " YeS " will yield the same effect. Hope you are using py3k -- otherwise, replace input with raw_input, as py2k's input() = py3k's eval(input())! (the risks were described above.) -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From kwpolska at gmail.com Thu Aug 23 21:18:02 2012 From: kwpolska at gmail.com (Kwpolska) Date: Thu, 23 Aug 2012 21:18:02 +0200 Subject: [Tutor] Question In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD97@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> <6962C976AE76AC4298CBF6FD6D0C63561A8EDD97@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On Thu, Aug 23, 2012 at 8:36 PM, Ashley Fowler wrote: > > ________________________________________ > From: tutor-bounces+afowler2=broncos.uncfsu.edu at python.org [tutor-bounces+afowler2=broncos.uncfsu.edu at python.org] on behalf of Alan Gauld [alan.gauld at btinternet.com] > Sent: Thursday, August 23, 2012 5:59 PM > To: tutor at python.org > Subject: Re: [Tutor] Question > > On 23/08/12 18:02, Ashley Fowler wrote: > >> def main(): >> l = list() >> x = eval(input('Enter a number: ')) > > Don;t use eval() its bad practicecand fort advanced use only. > Instead explicitly convert to int() or float() > >> while x >= 0: >> l.append(x) >> x = eval(input('Enter a number: ')) > > Same here. > > >> ask = input (" Do you want to perform a list operation?") >> if "yes": > > You need to test if ask is equal to 'yes' > Testing 'yes' directly will always be true because 'yes' always exists. > >> input (" Do you want to test, peek, add, or remove?") > > you need to store the reurn value from input as you did above > >> if "test": > > and use that stored value in the test. > >> if not l: >> print("The list is not empty") >> else: >> print("The list is empty") > > HTH > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > ______________________________________________________________________________________________________________________ > > REPLY: > > Thank you I finally fixed it. Can anyone break down how to do the next step which is: > > "You also need to write a function "printList" of one parameter that > takes a list as its input and neatly prints the entire contents of the > list in a column." > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor def printList(l): '\n'.join(l) There. Please take a look at my previous message. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From eryksun at gmail.com Thu Aug 23 21:41:59 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 23 Aug 2012 15:41:59 -0400 Subject: [Tutor] Question In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561A8EDD3B@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On Thu, Aug 23, 2012 at 1:02 PM, Ashley Fowler wrote: > > Instructions: Your "main" function should do the following: > (1) create an empty list; > (2) ask the user if he/she wants to perform a list operation. > if "yes": > (a) prompt the user for the operation: > "test", "peek", "add", or "remove"; > (b) perform the operation: > (i) if "test" then print whether the list is empty or not; > (ii) if "peek" then print the number at the beginning of the > list (with a label) but don't remove it from the list; > (iii) if "add", then prompt the user for a number and add > it to the end of the list; > (iv) if "remove", then delete the number at the beginning > of the list and print it (with a label); > (c) print the entire list from beginning to end; > repeat step (2) until the user enters "no". > > > def main(): > l = list() You can also use "l = []". By the way, "l" is a bad variable name. It looks like a number "1" in many fonts, and it isn't descriptive. You could be very original and call it numbers = [] > x = eval(input('Enter a number: ')) > while x >= 0: > l.append(x) > x = eval(input('Enter a number: ')) Don't use eval. Use float or int. Why are you looping until it's negative? According to your problem specification, a number should be appended when the user requests to "add" a number, and it says nothing about the valid range. More importantly this is your main loop and the statements that follows aren't in it. Pay close attention to indent level in Python code. > ask = input (" Do you want to perform a list operation?") > if "yes": The "if" sees a literal "yes" and treats that as a True statement -- always. Instead you need to do something like the following: run = input("Do you want to perform a list operation? (yes/no) ") if run == "yes": Let's show it in the while loop where it belongs: def main(): numbers = [] run = "yes" while run != "no": run = input("Do you want to perform a list operation? (yes/no) ") if run == "yes": > input (" Do you want to test, peek, add, or remove?") > if "test": OK, now you haven't even assigned the input to a variable. You're being cheeky, eh? Let's give this one an original name, too. Call it "op" for operation. op = input("Do you want to test, peek, add, or remove? ") if op == "test": > if not l: > print("The list is not empty") > else: > print("The list is empty") This is apparently the opposite of what you thought. The expression "not l" is True when the list *is empty*. Let's just use "if numbers" (using the new, more descriptive name for the list): if numbers: print("The list is not empty") else: print("The list is empty") > elif "peek": > print(l[0]) What will happen if the list is empty and you try to get index 0? For the "remove" operation, look into the list method "pop". From david at graniteweb.com Thu Aug 23 22:03:39 2012 From: david at graniteweb.com (David Rock) Date: Thu, 23 Aug 2012 15:03:39 -0500 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: <20120823200339.GE28653@wdfs.gateway.2wire.net> * Jared Nielsen [2012-08-23 12:05]: > Hi all, > I'm new to programming and Python. > I want to write a script that takes a string input and breaks the string at > keywords then outputs the pieces on separate lines. > But split() doesn't retain the separator and partition() retains the white > space and returns a 3-tuple which I'll have to figure out how to rejoin nor > does it partition on subsequent instances of the separator. While it's true that split() doesn't retain the separator, you still know what the separator is, right? Why not do something like: text = raw_input("Enter text: ") sep = 'and' parts = text.split(sep) for i in parts[:-1]: print i print sep print [-1] You might also want to consider stripping whitespace in the individual list items, too. -- David Rock david at graniteweb.com From david at graniteweb.com Thu Aug 23 22:05:48 2012 From: david at graniteweb.com (David Rock) Date: Thu, 23 Aug 2012 15:05:48 -0500 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: <20120823200339.GE28653@wdfs.gateway.2wire.net> References: <20120823200339.GE28653@wdfs.gateway.2wire.net> Message-ID: <20120823200548.GF28653@wdfs.gateway.2wire.net> * David Rock [2012-08-23 15:03]: > * Jared Nielsen [2012-08-23 12:05]: > > Hi all, > > I'm new to programming and Python. > > I want to write a script that takes a string input and breaks the string at > > keywords then outputs the pieces on separate lines. > > > But split() doesn't retain the separator and partition() retains the white > > space and returns a 3-tuple which I'll have to figure out how to rejoin nor > > does it partition on subsequent instances of the separator. > > While it's true that split() doesn't retain the separator, you still > know what the separator is, right? Why not do something like: > > text = raw_input("Enter text: ") > sep = 'and' > parts = text.split(sep) > for i in parts[:-1]: > print i > print sep > print [-1] Oops, print [-1] should have been print parts[-1] Hopefully you get the idea, though. -- David Rock david at graniteweb.com From eryksun at gmail.com Thu Aug 23 23:02:56 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 23 Aug 2012 17:02:56 -0400 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: On Thu, Aug 23, 2012 at 3:05 PM, Jared Nielsen wrote: > Hi all, > I'm new to programming and Python. > I want to write a script that takes a string input and breaks the string at > keywords then outputs the pieces on separate lines. This is just for printing? You can use replace(): >>> text = "Ham and cheese omelette with hasbrowns and coffee." >>> print text.replace(" and ", "\nand\n") Ham and cheese omelette with hasbrowns and coffee. From david at graniteweb.com Thu Aug 23 23:13:48 2012 From: david at graniteweb.com (David Rock) Date: Thu, 23 Aug 2012 16:13:48 -0500 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: <20120823211348.GG28653@wdfs.gateway.2wire.net> * eryksun [2012-08-23 17:02]: > On Thu, Aug 23, 2012 at 3:05 PM, Jared Nielsen wrote: > > Hi all, > > I'm new to programming and Python. > > I want to write a script that takes a string input and breaks the string at > > keywords then outputs the pieces on separate lines. > > This is just for printing? You can use replace(): > > >>> text = "Ham and cheese omelette with hasbrowns and coffee." > >>> print text.replace(" and ", "\nand\n") > Ham > and > cheese omelette with hasbrowns > and > coffee. I like that :-) If you aren't just printing, and want to use partition you will need to do some recursion (I assume that's the expected use case for partition). def repart(text): parts = text.partition('and') if parts[0] == text: return (parts[0],) else: return parts[:-1] + repart(parts[-1]) if __name__ == '__main__': text = "Ham and cheese omelette with hasbrowns and coffee." parts = repart(text) for part in parts: print part.strip() # Clean up whitespace when printing. -- David Rock david at graniteweb.com From eryksun at gmail.com Thu Aug 23 23:17:07 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 23 Aug 2012 17:17:07 -0400 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: <20120823200339.GE28653@wdfs.gateway.2wire.net> References: <20120823200339.GE28653@wdfs.gateway.2wire.net> Message-ID: On Thu, Aug 23, 2012 at 4:03 PM, David Rock wrote: > text = raw_input("Enter text: ") > sep = 'and' > parts = text.split(sep) > for i in parts[:-1]: > print i > print sep > print [-1] >>> "band".split("and") ['b', ''] It needs to be sep = " and ". That's assuming we're ignoring tabs. Line feed shouldn't be an issue since the source is raw_input. Anything more advanced should probably use regular expressions. From alan.gauld at btinternet.com Thu Aug 23 23:25:40 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Aug 2012 22:25:40 +0100 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: On 23/08/12 20:05, Jared Nielsen wrote: > But split() doesn't retain the separator and partition() retains the > white space and returns a 3-tuple which I'll have to figure out how to > rejoin nor does it partition on subsequent instances of the separator. David has shown one option for using split(), there are several others too. To use partition just call it repeatedly until the last string is empty. As ever the >>> prompt is your friend: >>> st = 'here we go and there you are and we all go roundabout' >>> h,s,t = st.partition(' and') >>> results = [h,s] >>> while t: ... h,s,t = t.partition(s) ... results += [h,s] ... >>> results ['here we go', ' and', ' there you are', ' and', ' we all go roundabout', ''] >>> It leaves an empty string at the end that can easily be trimmed off by slicing: results = results[:-1] Finally you can also find a solution using regular expressions, but they shouldn't be needed for something like this. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From akleider at sonic.net Fri Aug 24 00:08:58 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Thu, 23 Aug 2012 15:08:58 -0700 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> This question seemed a good excercise so I banged out a little script (which worked) but latter I saw posts showing code that by using string method 'partition' provided a more elegant solution. I was previously unaware of this method. My "bible" has been David M. Beazley's Python Essential Reference (3rdEd) in which this method is not mentioned (that I can see.) Should I switch "bibles?" (I often find myself wanting to hack in "off line environments" so something as old fashion as a book would be nice:-) Here's my script for what it's worth: #!/usr/bin/env python import sys usage = """test0 separator Requires one parameter, the text to be used to separate the input which will be requested by the program.""" if len(sys.argv) != 2: print usage separator = sys.argv[1] def separate(string, separator): ret = [] i = string.find(separator) l = len(separator) while i > 0: ret.append(string[:i]) ret.append(separator) string = string[i+l:] i = string.find(separator) ret.append(string) return ret def repart(string, separator): """Does the same as separator but using string method 'partition'""" parts = string.partition(separator) if parts[0] == string: return (parts[0], ) else: return parts[:-1] + repart(parts[-1], separator) input_str = raw_input("Enter text to split on '%s': "%(separator, )) separated_array = separate(input_str, separator) for s in separated_array: print s parted_array = repart(input_str, separator) for s in parted_array: print s > Hi all, > I'm new to programming and Python. > I want to write a script that takes a string input and breaks the string > at > keywords then outputs the pieces on separate lines. > I'm not sure how to break the string, though. > I looked through the docs and found split() and partition(), which come > close. > But split() doesn't retain the separator and partition() retains the white > space and returns a 3-tuple which I'll have to figure out how to rejoin > nor > does it partition on subsequent instances of the separator. > > Here's the script in its basic form: > > #!/usr/bin/python > > text = raw_input("Enter text: ") > print "You entered ", text > > objects = text.partition(' and') > print objects > > for object in objects: # Second Example > > print object > > For example, if I run this with the input: > "Ham and cheese omelette with hasbrowns and coffee." > I get: > Ham > and > cheese omelette with hashbrowns and coffee. > > Any help is greatly appreciated. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From wrw at mac.com Thu Aug 23 23:14:28 2012 From: wrw at mac.com (William R. Wing (Bill Wing)) Date: Thu, 23 Aug 2012 17:14:28 -0400 Subject: [Tutor] What are all those letters after terminal commands? In-Reply-To: <5036616E.6060703@pearwood.info> References: <5036616E.6060703@pearwood.info> Message-ID: <80532CB8-C288-4506-A54D-56E2D77F1A81@mac.com> On Aug 23, 2012, at 12:59 PM, Steven D'Aprano wrote: > On 23/08/12 23:18, Cecilia Chavana-Bryant wrote: > [...] >> I found this command: >> mkdir -p i/like/icecream. I am guessing that the -p stands for directory >> path? > > Ha, that's the trouble with command line interfaces -- they tend to end up > being cryptic and painfully terse. In this case, -p actually stands for > "parents", in the sense that mkdir is trying to create the folder "icecream" > inside the parent folder "like", inside the grandparent folder "i". If any > of the parent folders are missing, the -p option says to create the missing > folders. > > I don't want to put you off learning about the command line, because > knowledge is good. I've never learned something and then thought "I wish > I was more ignorant". But honestly, you don't need to be a command line > expert to make use of Python's interactive interpreter. To get started, > all you need is one command: > > python > > > and then press the ENTER key. That brings up Python's interactive > interpreter, which uses Python syntax rather than the shell's rather > cryptic commands and options. > While all that is true, I'm pretty sure she is going to need enough knowledge of the simplest -NIX commands to edit .py files, rename them occasionally, organize them into appropriate directories ('/working', '/development', and '/archive' as possible examples), and occasionally change a permission or two (-x for example). Ultimately, she will need to be able to examine her .profile (remember, she is running OS-X) file and possibly edit it. Finally, there are at least a few things that can be done most expeditiously, even in python, by spawning a python subtask and running a UNIX command there. I agree that she may never need to do shell programming (OS-X uses bash), but knowing the basics WILL be needed. -Bill > (Of course, the python command also takes a bunch of optional, and > useful. command switches, but you can learn them as you go.) > > By all means continue with the command line book if you are getting > something useful out of it, but don't think you *have* to learn the > shell in order to use Python. The two are independent. > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From eryksun at gmail.com Fri Aug 24 00:16:54 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 23 Aug 2012 18:16:54 -0400 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: On Thu, Aug 23, 2012 at 5:25 PM, Alan Gauld wrote: > > To use partition just call it repeatedly until the last string is empty. As > ever the >>> prompt is your friend: > >>>> st = 'here we go and there you are and we all go roundabout' >>>> h,s,t = st.partition(' and') >>>> results = [h,s] >>>> while t: > ... h,s,t = t.partition(s) > ... results += [h,s] > ... The keyword needs a space at both ends: >>> st = 'an androphobic andromedan android' >>> h,s,t = st.partition(' and') >>> results = [h,s] >>> while t: ... h,s,t = t.partition(s) ... results += [h,s] ... >>> results ['an', ' and', 'rophobic', ' and', 'romedan', ' and', 'roid', ''] > Finally you can also find a solution using regular expressions, but they > shouldn't be needed for something like this. It depends on how flexible it needs to be about common whitespace characters (space, tab, new line, carriage return) and punctuation. But this should be fine for raw_input. Tabs and punctuation could be replaced with spaces beforehand if necessary. Otherwise it won't partition a sentence on " and " such as, "I want an omelet--and some hash browns." From breamoreboy at yahoo.co.uk Fri Aug 24 00:27:47 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 23 Aug 2012 23:27:47 +0100 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: On 23/08/2012 23:08, akleider at sonic.net wrote: > This question seemed a good excercise so I banged out a little script > (which worked) but latter I saw posts showing code that by using string > method 'partition' provided a more elegant solution. > I was previously unaware of this method. My "bible" has been David M. > Beazley's Python Essential Reference (3rdEd) in which this method is not > mentioned (that I can see.) > Should I switch "bibles?" > (I often find myself wanting to hack in "off line environments" so > something as old fashion as a book would be nice:-) > Get another bible sure, but keep the Beazley book as he knows what he's on about. -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Fri Aug 24 00:55:56 2012 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 23 Aug 2012 23:55:56 +0100 (BST) Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: Message-ID: <1345762556.72259.YahooMailNeo@web87704.mail.ir2.yahoo.com> >>>> h,s,t = st.partition(' and') > >The keyword needs a space at both ends: > >? ? >>> st = 'an androphobic andromedan android' >? ? >>> results >? ? ['an', ' and', 'rophobic', ' and', 'romedan', ' and', 'roid', ''] > >Good catch, although to be honest I intended it to have a space... >But I didn't check the output closely enough! :-( > > >Alan g. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Fri Aug 24 01:03:02 2012 From: d at davea.name (Dave Angel) Date: Thu, 23 Aug 2012 19:03:02 -0400 Subject: [Tutor] Error message... In-Reply-To: <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Message-ID: <5036B6A6.2080203@davea.name> On 08/23/2012 11:33 AM, Victoria Homsy wrote: > > Dear All - sorry to bother you. I just tried to run this program: > > > def isPalindrome(s): > if len(s) <= 1: return True > else: return s[0] == s[-1] and isPalindrome (s[1:-1]) > isPalindrome('aba') > > > However when I run it in terminal it doesn't give me any answer - True or False. (I want the program to tell me whether the input string is True or False). In order to get an answer, I assume I would need to tell the program to print something. However I'm not sure where in the program I would do this. I tried this: > > def isPalindrome(s): > if len(s) <= 1: return True and print "True" > else: return s[0] == s[-1] and isPalindrome (s[1:-1]) > isPalindrome('aba') > > However, this does not work - I get another error message. > > Could somebody advise what I'm doing wrong here? Thank you. > > Could we trouble you for two obvious details? What version of Python are you running? What exactly is your error message? There are at least two possibilities, since two different versions of Python will give two different error messages. Or you could finesse the error by reverting the function to the version that worked, and printing in the calling code. The function shouldn't be printing in any case. While i've got your attention, could I talk you out of posting html messages to a text forum? All the indentation of those code fragments is lost, for me and probably most people. And don't top-post. Put your comments AFTER the part you quote from earlier messages. -- DaveA From alan.gauld at btinternet.com Fri Aug 24 01:55:32 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Aug 2012 00:55:32 +0100 Subject: [Tutor] What are all those letters after terminal commands? In-Reply-To: <80532CB8-C288-4506-A54D-56E2D77F1A81@mac.com> References: <5036616E.6060703@pearwood.info> <80532CB8-C288-4506-A54D-56E2D77F1A81@mac.com> Message-ID: On 23/08/12 22:14, William R. Wing (Bill Wing) wrote: > While all that is true, I'm pretty sure she is going to need enough knowledge > of the simplest -NIX commands to > edit .py files, > rename them occasionally, > organize them into appropriate directories > and occasionally change a permission or two But all of that can be done via the MacOS Finder and for most casual Python programmers on a MAC that's all they need. > I agree that she may never need to do shell programming > (OS-X uses bash), but knowing the basics WILL be needed. The basics can all be done via the GUI. Knowing the shell alternatives will likely make her more productive but won't be essential. Whether the extra productivity merits the time invested in learning will be a matter of circumstance. As somebody once said "GUIs make easy things trivial and hard things impossible" But most scientific Python programming doesn't require any "hard things", at least, not in terms of OS manipulation. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Fri Aug 24 02:00:44 2012 From: d at davea.name (Dave Angel) Date: Thu, 23 Aug 2012 20:00:44 -0400 Subject: [Tutor] better way to write this code In-Reply-To: References: Message-ID: <5036C42C.6030509@davea.name> On 08/23/2012 02:33 PM, Norman Khine wrote: > Hello, > I have this code (http://pastie.org/4575790) which pulls data from a list > and then modifies some of the values such as the 'yield' entry, which has > entries like: > > 21 > 15 > ≤ 1000 > ≤ 20 > 2.2 - 30 > > so that they are cleaned up. > > # -*- coding: UTF-8 -*- > # Norman Khine > import operator, json > from BeautifulSoup import BeautifulSoup > > combos={0: 'id', > 2: 'country', > 3: 'type', > 5: 'lat', > 6: 'lon', > 12: 'name' } > > TABLE_CONTENT = [['958',' > event_list = [] > for event in TABLE_CONTENT: > event_dict = {} > for index, item in enumerate(event): > if index == 8: > if item == ' ': > event_dict['depth'] = '0' > else: > event_dict['depth'] = item > if index == 9: > try: > > can the code be improved further? No idea. You mistakenly posted in html, so most of us cannot read the indentation. This is a text forum. (Yah, I know I could read the pastie link, but I try to stay within the list) > > also, the content has 2,153 items, what will be the correct way to have > this in a separate file and import this within this file to work on it? I assume you mean TABLE_CONTENT ? Just move the whole assignment to a new module, perhaps content.py, then import it as from content import TABLE_CONTENT -- DaveA From alan.gauld at btinternet.com Fri Aug 24 02:02:12 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Aug 2012 01:02:12 +0100 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: On 23/08/12 23:08, akleider at sonic.net wrote: > (I often find myself wanting to hack in "off line environments" so > something as old fashion as a book would be nice:-) Depends how off line you are. If you still have the python interpreter then just using dir() and help() should be all you need. I couldn't recall how partition worked so I just typed help(''.partition) at the >>> prompt. Most documentation you need is available that way, and if in doubt try it out at the prompt! But if you mean offline as in only paper and pencil then a book is the best bet and Beazley is good. I use him and Python in a Nutshell for my paper-only moments. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From akleider at sonic.net Fri Aug 24 03:08:44 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Thu, 23 Aug 2012 18:08:44 -0700 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: > On 23/08/12 23:08, akleider at sonic.net wrote: > >> (I often find myself wanting to hack in "off line environments" so >> something as old fashion as a book would be nice:-) > > Depends how off line you are. > If you still have the python interpreter then just using dir() and > help() should be all you need. > > I couldn't recall how partition worked so I just typed > help(''.partition) at the >>> prompt. > > Most documentation you need is available that way, and if in doubt try > it out at the prompt! > > But if you mean offline as in only paper and pencil then a book is the > best bet and Beazley is good. I use him and Python in a Nutshell for my > paper-only moments. Thank you for the tips. I will definitely KEEP Beazley- we've developed a very warm and cuddley relationship! (the Book, I mean:-) I raised the issue out of concern that the 'partition' method wasn't there and consequent worries that there may be other cool things I'm missing. Ahh, Ha! as I type this I suddenly realize what you are getting at: >>> s = "my string" >>> s.dir() Big "Thank You" alex > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From pedrooconnell at gmail.com Fri Aug 24 03:11:45 2012 From: pedrooconnell at gmail.com (Pete O'Connell) Date: Fri, 24 Aug 2012 13:11:45 +1200 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: Hi, I have tried to simplify things and am running into a bit of trouble. What i am really trying to do is: Keep all the lines starting with "v " and then delete those lines whose modulus 5 don't equal zero I have written it like this which seems to take a really long time (a couple of minutes when iteration over a folder with 200 files to parse) ##################################### with open(theFilePath) as lines: #keep only the lines beginning with "v " (this works) theGoodLines = [line.strip("\n") for line in lines if "v " == line[0:2]] theLinesAsListSubset = [] for i in range(len(theGoodLines)): nuke.tprint(i) if i%5 != 0: continue elif i%5 == 0: theLinesAsListSubset.append(theGoodLines[i]) ######################################## I think it would be better to include the modulud test within the original list comprehension but I am not sure how to access the index of "line": #something like this is a sketch of what I mean (I know it's wrong) theGoodLines = [line.strip("\n") for line in lines if "v " == line[0:2] and line.getIndex() % 5 == 0] Do I need enumerate for this maybe? Thanks Pete -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Fri Aug 24 03:39:29 2012 From: d at davea.name (Dave Angel) Date: Thu, 23 Aug 2012 21:39:29 -0400 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: Message-ID: <5036DB51.80705@davea.name> On 08/23/2012 09:11 PM, Pete O'Connell wrote: > Hi, I have tried to simplify things and am running into a bit of trouble. > What i am really trying to do is: Keep all the lines starting with "v " and > then delete those lines whose modulus 5 don't equal zero > > I have written it like this which seems to take a really long time (a > couple of minutes when iteration over a folder with 200 files to parse) > ##################################### > with open(theFilePath) as lines: > #keep only the lines beginning with "v " (this works) > theGoodLines = [line.strip("\n") for line in lines if "v " == > line[0:2]] Better to use startswith(), since short lines will cause the if expression above to blow up. > theLinesAsListSubset = [] > for i in range(len(theGoodLines)): When you see a line like this, it's usually clearer to do: for i, line in enumerate(theGoodLines): > nuke.tprint(i) > if i%5 != 0: > continue > elif i%5 == 0: > theLinesAsListSubset.append(theGoodLines[i]) It's confusing to me whether you meant to keep only one of every 5 lines of the filtered input, or to keep only those lines of the filtered input that came from the appropriate indices of the original data. You need a more precise spec before you can safely combine the two loops. (You may have it precise in your head; I'm just saying it isn't clear to me) > ######################################## > > I think it would be better to include the modulud test within the original > list comprehension but I am not sure how to access the index of "line": > #something like this is a sketch of what I mean (I know it's wrong) > theGoodLines = [line.strip("\n") for line in lines if "v " == > line[0:2] and line.getIndex() % 5 == 0] > > > Do I need enumerate for this maybe? Good call. Question is whether to do the enumerate on the original list, or on the list you get after. That decision would be based on the question above. To be honest, when I'm doing a non-trivial list comprehension, i tend to write it as a for loop first, get it correct, then reconsider if it can (& if it should be) rewritten as a comprehension. -- DaveA From eryksun at gmail.com Fri Aug 24 03:41:21 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 23 Aug 2012 21:41:21 -0400 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: On Thu, Aug 23, 2012 at 9:08 PM, wrote: > >>>> s.dir() > Surely you mean dir(s). Maybe you're thinking of the __dir__ special method you can add to a class to override the default behavior. You can also dir(str), or call help(str) to page through all of the doc strings. From akleider at sonic.net Fri Aug 24 03:50:17 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Thu, 23 Aug 2012 18:50:17 -0700 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: <5a2fcf38ebe367824fd7902a8956d5c3.squirrel@webmail.sonic.net> > On Thu, Aug 23, 2012 at 9:08 PM, wrote: >> >>>>> s.dir() >> > > Surely you mean dir(s). Maybe you're thinking of the __dir__ special > method you can add to a class to override the default behavior. > Yes, dir(s) is what I gave the interpreter. I should have used cut and paste (but tend not to for short bits.) > You can also dir(str), or call help(str) to page through all of the doc > strings. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From eryksun at gmail.com Fri Aug 24 04:34:31 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 23 Aug 2012 22:34:31 -0400 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: <5036DB51.80705@davea.name> References: <5036DB51.80705@davea.name> Message-ID: On Thu, Aug 23, 2012 at 9:39 PM, Dave Angel wrote: > >> theGoodLines = [line.strip("\n") for line in lines if "v " == >> line[0:2]] > > Better to use startswith(), since short lines will cause the if > expression above to blow up. A slice won't blow up. At worst you get an empty string. But the slice does create a temporary string, and subsequently the expression uses a high-level compare. startswith is more efficient and flexible in CPython. It does a low-level memory compare (memcmp). You can pass it a single string or a tuple of strings to match against, and you can set a start/stop position. From crawlzone at gmail.com Fri Aug 24 05:55:49 2012 From: crawlzone at gmail.com (Ray Jones) Date: Thu, 23 Aug 2012 20:55:49 -0700 Subject: [Tutor] Python working with Bash....arrrggggh! Message-ID: <5036FB45.5050907@gmail.com> As I code Python, I find myself falling back on Bash to handle basic OS tasks. How do you gurus deal with Python --> Bash conflicts? For example, if I wish to test if a file exists, I might do test = Popen('[ -f file-i-want-to-test-for ]') But the moment I invoke Bash for a test, I must deal with the fact that Bash returns a zero for true and a non-zero for false. But in Python, zero is false and non-zero is true. So if the file exists, the variable 'test' will be zero since that is what was returned by Bash. But if I want to test the variable for the existence of the file, I have to test the opposite: 'if not test:' because Python sees the zero as False. Does it become second nature to work with these conflicts? Or do you find it more expedient bypass the OS shell and work almost exclusively with Python? Ray From d at davea.name Fri Aug 24 06:20:36 2012 From: d at davea.name (Dave Angel) Date: Fri, 24 Aug 2012 00:20:36 -0400 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: <5036DB51.80705@davea.name> Message-ID: <50370114.6020004@davea.name> On 08/23/2012 10:34 PM, eryksun wrote: > On Thu, Aug 23, 2012 at 9:39 PM, Dave Angel wrote: >> >>> theGoodLines = [line.strip("\n") for line in lines if "v " == >>> line[0:2]] >> >> Better to use startswith(), since short lines will cause the if >> expression above to blow up. > > A slice won't blow up. You're right of course. For some reason I was looking at it as though it were simple subscripting, which can blow up (once the newline has been discarded, there might not be a [0] item. > At worst you get an empty string. But the slice > does create a temporary string, and subsequently the expression uses a > high-level compare. > > startswith is more efficient and flexible in CPython. It does a > low-level memory compare (memcmp). You can pass it a single string or > a tuple of strings to match against, and you can set a start/stop > position. > > -- DaveA From d at davea.name Fri Aug 24 06:26:39 2012 From: d at davea.name (Dave Angel) Date: Fri, 24 Aug 2012 00:26:39 -0400 Subject: [Tutor] list comprehension, testing for multiple conditions In-Reply-To: References: <5036DB51.80705@davea.name> Message-ID: <5037027F.2020004@davea.name> (you replied off-list, so I'm cc'ing the list here, to keep it public) On 08/23/2012 10:42 PM, Pete O'Connell wrote: > On Fri, Aug 24, 2012 at 1:39 PM, Dave Angel wrote: > >> On 08/23/2012 09:11 PM, Pete O'Connell wrote: >>> Hi, I have tried to simplify things and am running into a bit of trouble. >>> What i am really trying to do is: Keep all the lines starting with "v " >> and >>> then delete those lines whose modulus 5 don't equal zero >>> >>> I have written it like this which seems to take a really long time (a >>> couple of minutes when iteration over a folder with 200 files to parse) >>> ##################################### >>> with open(theFilePath) as lines: >>> #keep only the lines beginning with "v " (this works) >>> theGoodLines = [line.strip("\n") for line in lines if "v " == >>> line[0:2]] >> >> Better to use startswith(), since short lines will cause the if >> expression above to blow up. >> > Thanks that looks much safer. Sorry about that. Eryksun corrected me on that. Your present code won't blow up, but I'd still prefer startswith(). > >>> theLinesAsListSubset = [] >>> for i in range(len(theGoodLines)): >> >> When you see a line like this, it's usually clearer to do: >> for i, line in enumerate(theGoodLines): >>> nuke.tprint(i) >>> if i%5 != 0: >>> continue >>> elif i%5 == 0: >>> theLinesAsListSubset.append(theGoodLines[i]) >> It's confusing to me whether you meant to keep only one of every 5 lines >> of the filtered input, or to keep only those lines of the filtered input >> that came from the appropriate indices of the original data. You need a >> more precise spec before you can safely combine the two loops. (You may >> have it precise in your head; I'm just saying it isn't clear to me) >> > Sorry that wasn't clear. I want to keep every fifth line. Fifth of which list? The one you start with, or the one you get after throwing out the lines that don't begin with v ? You have presently coded the latter, and if that's what you want, I can't see any reasonable way to make it a single list comprehension. > >> >>> ######################################## >>> >>> I think it would be better to include the modulud test within the >> original >>> list comprehension but I am not sure how to access the index of "line": >>> #something like this is a sketch of what I mean (I know it's wrong) >>> theGoodLines = [line.strip("\n") for line in lines if "v " == >>> line[0:2] and line.getIndex() % 5 == 0] >>> >>> >>> Do I need enumerate for this maybe? >> Good call. Question is whether to do the enumerate on the original >> list, or on the list you get after. That decision would be based on the >> question above. >> >> I have noticed that part of the slowness comes from the feedback I am > getting on the command line with print statements. When I streamline those > it is much faster. > It is useable even in its current state, still out of curiosity and for my > python understanding, it would be nice to know if it is possible to write > it all within one list comprehension. > Thanks > Pete > >> -- DaveA From akleider at sonic.net Fri Aug 24 06:53:06 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Thu, 23 Aug 2012 21:53:06 -0700 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: <5036FB45.5050907@gmail.com> References: <5036FB45.5050907@gmail.com> Message-ID: > As I code Python, I find myself falling back on Bash to handle basic OS > tasks. How do you gurus deal with Python --> Bash conflicts? > > For example, if I wish to test if a file exists, I might do > > test = Popen('[ -f file-i-want-to-test-for ]') > > But the moment I invoke Bash for a test, I must deal with the fact that > Bash returns a zero for true and a non-zero for false. But in Python, > zero is false and non-zero is true. So if the file exists, the variable > 'test' will be zero since that is what was returned by Bash. But if I > want to test the variable for the existence of the file, I have to test > the opposite: 'if not test:' because Python sees the zero as False. > > Does it become second nature to work with these conflicts? Or do you > find it more expedient bypass the OS shell and work almost exclusively > with Python? try >>> import os.path >>> os.path.exists(path) or >>> os.path.isfile(file_name) http://docs.python.org/library/os.path.html > > > Ray > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From eryksun at gmail.com Fri Aug 24 07:37:09 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 24 Aug 2012 01:37:09 -0400 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: <5036FB45.5050907@gmail.com> References: <5036FB45.5050907@gmail.com> Message-ID: On Thu, Aug 23, 2012 at 11:55 PM, Ray Jones wrote: > For example, if I wish to test if a file exists, I might do > > test = Popen('[ -f file-i-want-to-test-for ]') > > But the moment I invoke Bash for a test, I must deal with the fact that > Bash returns a zero for true and a non-zero for false. But in Python, Please see os.path: http://docs.python.org/library/os.path That said, you can use check_call and check_output if you want a more Pythonic interface. These raise an exception for a non-zero return code. For example: # check.py from subprocess import check_call, CalledProcessError try: # in Debian [ is at /usr/bin/[ rc = check_call(["/usr/bin/[", "-f", "check.py", "]"]) print "success:", rc # or pass the command string to the shell rc = check_call("[ -f check.py ]", shell=True) print "success:", rc # force a failure rc = check_call("[ -f notfound ]", shell=True) print "success:", rc # this won't execute except CalledProcessError as e: print "failed: %d" % e.returncode From crawlzone at gmail.com Fri Aug 24 08:27:56 2012 From: crawlzone at gmail.com (Ray Jones) Date: Thu, 23 Aug 2012 23:27:56 -0700 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: References: <5036FB45.5050907@gmail.com> Message-ID: <50371EEC.6070809@gmail.com> On 08/23/2012 09:53 PM, akleider at sonic.net wrote: >> As I code Python, I find myself falling back on Bash to handle basic OS >> tasks. How do you gurus deal with Python --> Bash conflicts? >> >> For example, if I wish to test if a file exists, I might do >> >> test = Popen('[ -f file-i-want-to-test-for ]') >> >> But the moment I invoke Bash for a test, I must deal with the fact that >> Bash returns a zero for true and a non-zero for false. But in Python, >> zero is false and non-zero is true. So if the file exists, the variable >> 'test' will be zero since that is what was returned by Bash. But if I >> want to test the variable for the existence of the file, I have to test >> the opposite: 'if not test:' because Python sees the zero as False. >> >> Does it become second nature to work with these conflicts? Or do you >> find it more expedient bypass the OS shell and work almost exclusively >> with Python? > try > >>>> import os.path >>>> os.path.exists(path) > or >>>> os.path.isfile(file_name) That seems much cleaner to me than testing to see if 'os.listdir' contains a specific file. Thanks. I am forever confused, however, on which methods can be found where. I just spent quarter of an hour searching in sys,* os.*, and shutil.*. for a 'kill' command that I knew I'd seen before....I found it hidden in subprocess.Popen. Arrrgggh. These various imports are going to drive me to drink! Thanks for the response. Ray From crawlzone at gmail.com Fri Aug 24 08:34:37 2012 From: crawlzone at gmail.com (Ray Jones) Date: Thu, 23 Aug 2012 23:34:37 -0700 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: References: <5036FB45.5050907@gmail.com> Message-ID: <5037207D.4070809@gmail.com> On 08/23/2012 10:37 PM, eryksun wrote: > On Thu, Aug 23, 2012 at 11:55 PM, Ray Jones wrote: > >> For example, if I wish to test if a file exists, I might do >> >> test = Popen('[ -f file-i-want-to-test-for ]') >> >> But the moment I invoke Bash for a test, I must deal with the fact that >> Bash returns a zero for true and a non-zero for false. But in Python, > Please see os.path: > > http://docs.python.org/library/os.path > > That said, you can use check_call and check_output if you want a more > Pythonic interface. These raise an exception for a non-zero return > code. For example: > > # check.py > > from subprocess import check_call, CalledProcessError > > try: > > # in Debian [ is at /usr/bin/[ > rc = check_call(["/usr/bin/[", "-f", "check.py", "]"]) > print "success:", rc > > # or pass the command string to the shell > rc = check_call("[ -f check.py ]", shell=True) > print "success:", rc > > # force a failure > rc = check_call("[ -f notfound ]", shell=True) > print "success:", rc # this won't execute > > except CalledProcessError as e: > print "failed: %d" % e.returncode Ah, yes. Thanks for the reminder that the check_call automatically interprets the True/False returns from the OS. Ray From __peter__ at web.de Fri Aug 24 08:55:05 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Aug 2012 08:55:05 +0200 Subject: [Tutor] list comprehension, testing for multiple conditions References: Message-ID: Pete O'Connell wrote: > Hi, I have tried to simplify things and am running into a bit of trouble. > What i am really trying to do is: Keep all the lines starting with "v " > and then delete those lines whose modulus 5 don't equal zero > > I have written it like this which seems to take a really long time (a > couple of minutes when iteration over a folder with 200 files to parse) > ##################################### > with open(theFilePath) as lines: > #keep only the lines beginning with "v " (this works) > theGoodLines = [line.strip("\n") for line in lines if "v " == > line[0:2]] > theLinesAsListSubset = [] > for i in range(len(theGoodLines)): > nuke.tprint(i) > if i%5 != 0: > continue > elif i%5 == 0: > theLinesAsListSubset.append(theGoodLines[i]) > ######################################## > > I think it would be better to include the modulud test within the original > list comprehension but I am not sure how to access the index of "line": > #something like this is a sketch of what I mean (I know it's wrong) > theGoodLines = [line.strip("\n") for line in lines if "v " == > line[0:2] and line.getIndex() % 5 == 0] > > > Do I need enumerate for this maybe? With enumerate() you need two steps, but you can use a generator expression (which doesn't materialize the list as you might remember): # low memory consumption good = (line for line in lines if line.startswith("v ")) every_fifth = [line.strip("\n") for index, line in enumerate(prefixed) if not index % 5] Alternatively you can build the good list and apply slicing: # simple/fast good = [line.strip("\n") for line in lines if line.startswith("v ")] every_fifth = good[::5] Finally there's itertools.islice() which helps you combine the advantages of both: # low memory consumption, fast good = (line.strip("\n") for line in lines if line.startswith("v ")) every_fifths= list(itertools.islice(good, 0, None, 5)) From steve at pearwood.info Fri Aug 24 09:02:58 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Aug 2012 17:02:58 +1000 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: <50371EEC.6070809@gmail.com> References: <5036FB45.5050907@gmail.com> <50371EEC.6070809@gmail.com> Message-ID: <50372722.9060807@pearwood.info> On 24/08/12 16:27, Ray Jones wrote: > I am forever confused, however, on which methods can be found where. I > just spent quarter of an hour searching in sys,* os.*, and shutil.*. for > a 'kill' command that I knew I'd seen before....I found it hidden in > subprocess.Popen. Arrrgggh. These various imports are going to drive me > to drink! Possibly you've already started the drive? *wink* There is an os.kill. http://docs.python.org/library/os.html#os.kill -- Steven From crawlzone at gmail.com Fri Aug 24 09:05:50 2012 From: crawlzone at gmail.com (Ray Jones) Date: Fri, 24 Aug 2012 00:05:50 -0700 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: <50372722.9060807@pearwood.info> References: <5036FB45.5050907@gmail.com> <50371EEC.6070809@gmail.com> <50372722.9060807@pearwood.info> Message-ID: <503727CE.1000805@gmail.com> On 08/24/2012 12:02 AM, Steven D'Aprano wrote: > On 24/08/12 16:27, Ray Jones wrote: > >> I am forever confused, however, on which methods can be found where. I >> just spent quarter of an hour searching in sys,* os.*, and shutil.*. for >> a 'kill' command that I knew I'd seen before....I found it hidden in >> subprocess.Popen. Arrrgggh. These various imports are going to drive me >> to drink! > > Possibly you've already started the drive? *wink* > > There is an os.kill. > > > http://docs.python.org/library/os.html#os.kill Yes, I'd found that, but that requires a file descriptor rather than a file object - and I haven't advanced to file descriptors yet.... ;) From rdmoores at gmail.com Fri Aug 24 09:31:18 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 24 Aug 2012 00:31:18 -0700 Subject: [Tutor] pickle problems In-Reply-To: References: <5027C480.3080705@davea.name> <50352B01.4070903@pearwood.info> <5035A602.7010307@davea.name> <20120823084313.GA4671@ando> <5036401F.9060803@davea.name> Message-ID: Case Van Horsen wrote the following to me about gmpy2.is_prime. I post it with his permission. Dick Moores The summary: gmpy2.is_prime() just provides direct access to the underlying library (GMP or MPIR) function. Depending on the library and version, the behavior is subtly different. With the current version, both libraries behave similarly - the Miller-Rabin test is repeated 25 times. The sequence of bases checked are "random" values. The bases depend on the value of the number being tested. The bases really aren't random since same set of bases is used for each test of a given number. (Testing with 5 repetitions followed by another test with 10 repetitions actually repeats the tests with the first 5 bases and then tries 5 more bases. It is not the same as testing with 15 repetiions.) The Miller-Rabin test actually detects composite numbers. It will fail to detect a composite number with at most 1/4 of all possible bases. So after 5 tests, composite numbers are detected with a guaranteed error of 1 in 1024. On average, the error rate is less than that, but for certain composites the worst-case error estimates can be reached. The worst-case is reached when the number being checked is p*(2p-1) where both p and 2p-1 are prime. A few people have tried worst-case numbers with GMP and have found many values that require 10 or more iterations. (One value that requires 15 iterations has been found.) So the default of 25 iterations is reasonable but it could be set higher if you are truly paranoid however this will make the test slower for actual primes. An alternative primality test is known as the BPSW test. It is available in gmpy2 as is_bpsw_prp(). IIRC, the implementation requires about the same running time as 10 iterations of Miller-Rabin. But the biggest advantage is that there are no known false results. The test has not been proven to work 100% of the time. It is conjectured that there are false results should occur but no one has ever found one. The MPIR library has a faster version of the BPSW test but I don't make it available at the moment. I will probably add it to gmpy2 soon. From breamoreboy at yahoo.co.uk Fri Aug 24 10:28:06 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 24 Aug 2012 09:28:06 +0100 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: <50372722.9060807@pearwood.info> References: <5036FB45.5050907@gmail.com> <50371EEC.6070809@gmail.com> <50372722.9060807@pearwood.info> Message-ID: On 24/08/2012 08:02, Steven D'Aprano wrote: > On 24/08/12 16:27, Ray Jones wrote: > >> I am forever confused, however, on which methods can be found where. I >> just spent quarter of an hour searching in sys,* os.*, and shutil.*. for >> a 'kill' command that I knew I'd seen before....I found it hidden in >> subprocess.Popen. Arrrgggh. These various imports are going to drive me >> to drink! > > Possibly you've already started the drive? *wink* > > There is an os.kill. > > > http://docs.python.org/library/os.html#os.kill > > Would have taken the car over the cliff if it had been Java :) -- Cheers. Mark Lawrence. From steve at pearwood.info Fri Aug 24 11:24:31 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Aug 2012 19:24:31 +1000 Subject: [Tutor] Python working with Bash....arrrggggh! In-Reply-To: <503727CE.1000805@gmail.com> References: <5036FB45.5050907@gmail.com> <50371EEC.6070809@gmail.com> <50372722.9060807@pearwood.info> <503727CE.1000805@gmail.com> Message-ID: <5037484F.5030000@pearwood.info> On 24/08/12 17:05, Ray Jones wrote: > > On 08/24/2012 12:02 AM, Steven D'Aprano wrote: >> On 24/08/12 16:27, Ray Jones wrote: >> >>> I am forever confused, however, on which methods can be found where. I >>> just spent quarter of an hour searching in sys,* os.*, and shutil.*. for >>> a 'kill' command that I knew I'd seen before....I found it hidden in >>> subprocess.Popen. Arrrgggh. These various imports are going to drive me >>> to drink! >> >> Possibly you've already started the drive? *wink* >> >> There is an os.kill. >> >> >> http://docs.python.org/library/os.html#os.kill > > Yes, I'd found that, but that requires a file descriptor rather than a > file object - and I haven't advanced to file descriptors yet.... ;) os.kill doesn't take a file descriptor. It's a wrapper around the Unix/Linux kill command, and so takes a process id and a signal, exactly as you would use if you were using kill in the shell. As the Fine Manual says: os.kill(pid, sig) Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module. -- Steven From norman at khine.net Fri Aug 24 11:43:12 2012 From: norman at khine.net (Norman Khine) Date: Fri, 24 Aug 2012 10:43:12 +0100 Subject: [Tutor] better way to write this code In-Reply-To: <5036C42C.6030509@davea.name> References: <5036C42C.6030509@davea.name> Message-ID: ok, thanks On Fri, Aug 24, 2012 at 1:00 AM, Dave Angel wrote: > On 08/23/2012 02:33 PM, Norman Khine wrote: > > Hello, > > I have this code (http://pastie.org/4575790) which pulls data from a > list > > and then modifies some of the values such as the 'yield' entry, which has > > entries like: > > > > 21 > > 15 > > ≤ 1000 > > ≤ 20 > > 2.2 - 30 > > > > so that they are cleaned up. > > > > # -*- coding: UTF-8 -*- > > # Norman Khine > > import operator, json > > from BeautifulSoup import BeautifulSoup > > > > combos={0: 'id', > > 2: 'country', > > 3: 'type', > > 5: 'lat', > > 6: 'lon', > > 12: 'name' } > > > > TABLE_CONTENT = [['958',' > > > event_list = [] > > for event in TABLE_CONTENT: > > event_dict = {} > > for index, item in enumerate(event): > > if index == 8: > > if item == ' ': > > event_dict['depth'] = '0' > > else: > > event_dict['depth'] = item > > if index == 9: > > try: > > > > can the code be improved further? > > No idea. You mistakenly posted in html, so most of us cannot read the > indentation. This is a text forum. (Yah, I know I could read the > pastie link, but I try to stay within the list) > > > > > > also, the content has 2,153 items, what will be the correct way to have > > this in a separate file and import this within this file to work on it? > > I assume you mean TABLE_CONTENT ? > > Just move the whole assignment to a new module, perhaps content.py, > then import it as > > from content import TABLE_CONTENT > > > > > > -- > > DaveA > > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From bouncingcats at gmail.com Fri Aug 24 13:13:15 2012 From: bouncingcats at gmail.com (David) Date: Fri, 24 Aug 2012 21:13:15 +1000 Subject: [Tutor] Error message... In-Reply-To: <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Message-ID: On 24/08/2012, Victoria Homsy wrote: > > However, this does not work - I get another error message. > Could somebody advise what I'm doing wrong here? Thank you. 1) You are not carefully reading the entire error message. 2) You are not allowing us to do it either. Some other things too, probably, but we need to start with those two :) From __peter__ at web.de Fri Aug 24 14:11:20 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Aug 2012 14:11:20 +0200 Subject: [Tutor] better way to write this code References: Message-ID: Norman Khine wrote: > I have this code (http://pastie.org/4575790) which pulls data from a list > and then modifies some of the values such as the 'yield' entry, which has > entries like: > > 21 > 15 > ≤ 1000 > ≤ 20 > 2.2 - 30 > > so that they are cleaned up. > can the code be improved further? It will make the code a bit longer, but I'd still put the code for every column/key format into a separate function. Here's a sketch: class Skip(Exception): pass def as_is(item): return item def fix_date(item): soup = BeautifulSoup(item) for a in soup.findAll('a'): return ''.join(a.findAll(text=True)) raise Skip def fix_fill(item): if 'Underground' in item: return "green" elif 'Atmospheric' in item: return 'red' raise Skip fixers = [ (0, "id", as_is), (2, "country", as_is), (3, "fill", fix_fill), (4, "date", fix_date), # ... (12, "name", as_is), ] if __name__ == "__main__": INFILE = "table_content.json" OUTFILE = "detonations.json" with open(INFILE) as f: table_content = json.load(f) event_list = [] for event in table_content: event_dict = {} for index, name, fixer in fixers: item = event[index] try: event_dict[name] = fixer(item) except Skip: pass event_list.append(event_dict) event_list.sort(key=operator.itemgetter('id')) with open(OUTFILE, 'w') as f: json.dump(event_list, f) > also, the content has 2,153 items, what will be the correct way to have > this in a separate file and import this within this file to work on it? I think it would be better to store it in a data format. JSON seems the obvious choice. From eryksun at gmail.com Fri Aug 24 14:18:19 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 24 Aug 2012 08:18:19 -0400 Subject: [Tutor] better way to write this code In-Reply-To: References: Message-ID: On Thu, Aug 23, 2012 at 2:33 PM, Norman Khine wrote: > > import operator, json > from BeautifulSoup import BeautifulSoup If you have the source of TABLE_CONTENT, why don't you soup that instead? Otherwise, nevermind. > combos={0: 'id', > 2: 'country', > 3: 'type', > 5: 'lat', > 6: 'lon', > 12: 'name' } Here's the style I'd use for the above: combos = { 0: 'id', 2: 'country', 3: 'type', 5: 'lat', 6: 'lon', 12: 'name', } Put each entry on its own line, indented by two spaces, and leave a trailing comma on the last entry. The latter is especially important in sequences of strings to prevent them from being "silently"<=>"concatenated" if you were to add an entry and forget the comma. Below I've formatted the rest of your code to use 4 spaces instead of tabs. Please follow PEP 8 unless local style rules take precedence. If your code gets copied into a file that uses spaces, you end up with the headache of mixing tabs and spaces. So try to use spaces since that's what most people use. Your editor should be able to insert spaces when you press . > event_list = [] > for event in TABLE_CONTENT: > event_dict = {} > for index, item in enumerate(event): > if index == 8: > if item == ' ': > event_dict['depth'] = '0' > else: > event_dict['depth'] = item You can simplify the above with a ternary expression. IMO, this simplifies maintenance because you're not repeating the assignment to "event_dict['depth']": if index == 8: event_dict['depth'] = item if item != ' ' else '0' > if index == 9: > try: > items = item.split() > if len(items) >= 2: > event_dict['yield'] = items[-1] > else: > if item == ' ': > event_dict['yield'] = '10' > else: > event_dict['yield'] = item > except: > pass I fail to see why you need a try/except here. Avoid using a bare "except". Handle specific exceptions. > if index == 3: > if 'Atmospheric' in item: > event_dict['fill'] = 'red' > if 'Underground' in item: > event_dict['fill'] = 'green' Should there be a default value for event_dict['fill']? > event_list.append(event_dict) > print event_dict > event_list = sorted(event_list, key = operator.itemgetter('id')) You may as well sort in place: event_list.sort(operator.itemgetter('id')) > f = open('detonations.json', 'w') > f.write(json.dumps(event_list)) > f.close() > print 'detonations.json, written!' Use "with" to make sure the file gets closed even if there's an exception. Also, you may as well "dump" directly to the file instead of creating a string with "dumps": with open('detonations.json', 'w') as f: json.dump(event_list, f) From eryksun at gmail.com Fri Aug 24 14:36:07 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 24 Aug 2012 08:36:07 -0400 Subject: [Tutor] better way to write this code In-Reply-To: References: Message-ID: On Fri, Aug 24, 2012 at 8:11 AM, Peter Otten <__peter__ at web.de> wrote: > > for index, name, fixer in fixers: > item = event[index] @Norman I forgot to mention this. You should index the event instead of iterating over it. I suppose each event has a name in index 12, so you shouldn't get an IndexError. Then you can loop over combos: for index, name in combos.items(): event_dict[name] = event[index] Or you can generalize the process as Peter has shown. From norman at khine.net Fri Aug 24 17:02:50 2012 From: norman at khine.net (Norman Khine) Date: Fri, 24 Aug 2012 16:02:50 +0100 Subject: [Tutor] better way to write this code In-Reply-To: References: Message-ID: thank you for the detailed replies, i will try to update the code and post it when done On Fri, Aug 24, 2012 at 1:36 PM, eryksun wrote: > On Fri, Aug 24, 2012 at 8:11 AM, Peter Otten <__peter__ at web.de> wrote: > > > > for index, name, fixer in fixers: > > item = event[index] > > @Norman > I forgot to mention this. You should index the event instead of > iterating over it. I suppose each event has a name in index 12, so you > shouldn't get an IndexError. > > Then you can loop over combos: > > for index, name in combos.items(): > event_dict[name] = event[index] > > Or you can generalize the process as Peter has shown. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From nielsen.jared at gmail.com Fri Aug 24 19:30:13 2012 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Fri, 24 Aug 2012 10:30:13 -0700 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: Thanks everyone. As I'm learning programming what I find most interesting is that there's always more than one way to solve a problem. I implemented eryksun's suggestion and used the replace() method. But, playing around with it, what I discovered is that it won't store the change. For example, when the input text is, "Ham and cheese or chicken and waffles": #!/usr/bin/python text = raw_input("Enter text: ") print text.replace("and", "\nand").replace("or", "\nor") I get: Ham and cheese or chicken and waffles. But if I run the following: #!/usr/bin/python text = raw_input("Enter text: ") text.replace("and", "\nand") text.replace("or", "\nor") print text I get the text as it was entered. Is there a way to replace text in a string without splitting or partitioning? The bigger picture for this little project is a "poetry machine", in which a user enters some prose and the program chops it up into modern poetry. So, this is a long shot naive noob question, but is there any way to count syllables in words in a string? Or at least approximate this procedure? On Thu, Aug 23, 2012 at 3:08 PM, wrote: > This question seemed a good excercise so I banged out a little script > (which worked) but latter I saw posts showing code that by using string > method 'partition' provided a more elegant solution. > I was previously unaware of this method. My "bible" has been David M. > Beazley's Python Essential Reference (3rdEd) in which this method is not > mentioned (that I can see.) > Should I switch "bibles?" > (I often find myself wanting to hack in "off line environments" so > something as old fashion as a book would be nice:-) > > Here's my script for what it's worth: > > #!/usr/bin/env python > > import sys > > usage = """test0 separator > Requires one parameter, the text to be used to separate the input which > will be requested by the program.""" > > if len(sys.argv) != 2: > print usage > separator = sys.argv[1] > > def separate(string, separator): > ret = [] > i = string.find(separator) > l = len(separator) > while i > 0: > ret.append(string[:i]) > ret.append(separator) > string = string[i+l:] > i = string.find(separator) > ret.append(string) > return ret > > def repart(string, separator): > """Does the same as separator but using string method 'partition'""" > parts = string.partition(separator) > if parts[0] == string: > return (parts[0], ) > else: > return parts[:-1] + repart(parts[-1], separator) > > input_str = raw_input("Enter text to split on '%s': "%(separator, )) > > separated_array = separate(input_str, separator) > for s in separated_array: > print s > parted_array = repart(input_str, separator) > for s in parted_array: > print s > > > > > > Hi all, > > I'm new to programming and Python. > > I want to write a script that takes a string input and breaks the string > > at > > keywords then outputs the pieces on separate lines. > > I'm not sure how to break the string, though. > > I looked through the docs and found split() and partition(), which come > > close. > > But split() doesn't retain the separator and partition() retains the > white > > space and returns a 3-tuple which I'll have to figure out how to rejoin > > nor > > does it partition on subsequent instances of the separator. > > > > Here's the script in its basic form: > > > > #!/usr/bin/python > > > > text = raw_input("Enter text: ") > > print "You entered ", text > > > > objects = text.partition(' and') > > print objects > > > > for object in objects: # Second Example > > > > print object > > > > For example, if I run this with the input: > > "Ham and cheese omelette with hasbrowns and coffee." > > I get: > > Ham > > and > > cheese omelette with hashbrowns and coffee. > > > > Any help is greatly appreciated. > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Aug 24 20:13:13 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 24 Aug 2012 20:13:13 +0200 Subject: [Tutor] how to split/partition a string on keywords? References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: Jared Nielsen wrote: > I implemented eryksun's suggestion and used the replace() method. > But, playing around with it, what I discovered is that it won't store the > change. > For example, when the input text is, "Ham and cheese or chicken and > waffles": > > #!/usr/bin/python > > text = raw_input("Enter text: ") > > print text.replace("and", "\nand").replace("or", "\nor") > > I get: > Ham > and cheese > or chicken > and waffles. > > But if I run the following: > > #!/usr/bin/python > > text = raw_input("Enter text: ") > > text.replace("and", "\nand") > text.replace("or", "\nor") > > print text > > I get the text as it was entered. > Is there a way to replace text in a string without splitting or > partitioning? The replace() method does not modify the original string, it returns a new string with the appropriate replacements. With a line like > text.replace("and", "\nand") you throw away that new string. Change it to text = text.replace("and", "\nand") to keep it. However, I doubt you will be satisfied with the result of your script for long: >>> print raw_input().replace("and", "and\n") vandalizing androids wandering the wastelands vand alizing and roids wand ering the wasteland s From matt.gregory at oregonstate.edu Fri Aug 24 20:22:13 2012 From: matt.gregory at oregonstate.edu (Matt Gregory) Date: Fri, 24 Aug 2012 11:22:13 -0700 Subject: [Tutor] creating a subclass from superclass without __init__ Message-ID: Is it possible to create a subclass of a superclass that doesn't have an __init__ and is only created through another class. Here is an example of what isn't working: class Spam(object): def __new__(cls, *args): return super(Spam, cls).__new__(cls, args) def __init__(self): raise AttributeError('Cannot create Spam') def test(self): print 'This is a Spam class' class SpamMaker(object): def make_spam(self): return Spam.__new__(Spam) class SubSpam(Spam): def __new__(cls, *args): return SpamMaker().make_spam() def test(self): print 'This is a SubSpam class' b = SpamMaker().make_spam() b.test() c = SubSpam() c.test() prints This is a Spam class This is a Spam class I know that I'm not creating the SubSpam instance correctly, but I have no idea how to do it. It's probably something very obvious that I'm missing. My real use case is using the Python bindings to GDAL and trying to create a subclass of gdal.Band which can't be instantiated directly. thanks, matt From steve at pearwood.info Fri Aug 24 21:01:44 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 25 Aug 2012 05:01:44 +1000 Subject: [Tutor] creating a subclass from superclass without __init__ In-Reply-To: References: Message-ID: <5037CF98.3080602@pearwood.info> On 25/08/12 04:22, Matt Gregory wrote: > Is it possible to create a subclass of a superclass that doesn't have an >__init__ and is only created through another class. > Here is an example of what isn't working: > > class Spam(object): > def __new__(cls, *args): > return super(Spam, cls).__new__(cls, args) > def __init__(self): > raise AttributeError('Cannot create Spam') That will prevent anything from successfully initialising a Spam instance. It doesn't prevent anything from creating a Spam instance, only initialising it. Anyone can simply call Spam.__new__ directly, and bypass the booby-trap in __init__. Why do you want to do this? > class SubSpam(Spam): > def __new__(cls, *args): > return SpamMaker().make_spam() While it is legal for a class __new__ method to return an instance of a different class, that should be considered very unusual. Why do you want SubSpam(*args) to return a Spam instance instead of a SubSpam instance? > def test(self): > print 'This is a SubSpam class' [...] > My real use case is using the Python bindings to GDAL and trying >to create a subclass of gdal.Band which can't be instantiated >directly. That's not a use-case. A use-case is a real-world problem that you are trying to solve. You have skipped the problem and jumped straight to what you think is the solution: "create a subclass which can't be instantiated directly". I can't imagine any problem where the solution to the problem depends on whether or not you instantiated a subclass using: Spam(*args) or SpamMaker.make(*args) Why do you care if Spam is called directly? -- Steven From eryksun at gmail.com Fri Aug 24 21:33:00 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 24 Aug 2012 15:33:00 -0400 Subject: [Tutor] how to split/partition a string on keywords? In-Reply-To: References: <743ee5b469c6d45ac6e9603e16300dab.squirrel@webmail.sonic.net> Message-ID: On Fri, Aug 24, 2012 at 1:30 PM, Jared Nielsen wrote: > > But if I run the following: > > > #!/usr/bin/python > > text = raw_input("Enter text: ") > > text.replace("and", "\nand") > text.replace("or", "\nor") > > print text > > I get the text as it was entered. > Is there a way to replace text in a string without splitting or > partitioning? A Python string is immutable. So replace() just gives you a new string. You can assign it to a different variable. > The bigger picture for this little project is a "poetry machine", in which a > user enters some prose and the program chops it up into modern poetry. Based on your original problem description, I think you'll get the best result using regular expression substitution: import re words = "|".join(["and", "or"]) pattern = r"(^|\W)({kwds})(?=\W|$)".format(kwds=words) prose = [] line = raw_input("Enter text: ") while line: prose.append(line) line = raw_input() poetry = " ".join(re.sub(pattern, r"\1\n\2", line) for line in prose) print poetry # try # or... ham and cheese or chicken and waffles...and... # whatever she wants--and he'll have the special. Output: or... ham and cheese or chicken and waffles... and... whatever she wants-- and he'll have the special. The first thing to note with regular expressions is always use (r)aw string literals, such as r"text". The expressions make heavy use of backslash escapes, so you don't want Python's compiler to process regular string escapes. It's much simpler to use raw mode than to quote all the backslashes as '\\'. In "pattern" you see three sub-patterns (groups) in parentheses. In group one, the ^ matches at the beginning of the line, the \W matches any non-alphanumeric character, and the | means the group will match on either ^ or \W. In group two, I'm using Python's string formatting to map the string "and|or" into {kwds}. As before the | means this group matches on either the literal "and" or the literal "or". Group 3 is a bit more complicated. It starts with the ?= operator. This is a lookahead operation. When this groups matches it won't consume any of the string. This allows overlapping matches on the non-alphanumeric character \W. In other words, if you have "some text and and repeated", the whitespace joining the first " and " and the second " and " can count toward both matches. The $ means this group also matches at the end of the line. Finally all of the prose lines are processed with re.sub. This looks for "pattern" in "line" and replaces it with r"\1\n\2". In the replacement string \1 is group 1, \2 is group 2, and \n is a new line character. Please see the re docs for further explanation: http://docs.python.org/library/re Here's a pretty good tutorial in general: http://www.regular-expressions.info > So, this is a long shot naive noob question, but is there any way to count > syllables in words in a string? Or at least approximate this procedure? At this point you're getting into natural language processing. You can try the Natural Language Processing Toolkit (NLTK), but I don't know if it breaks words into syllables: http://nltk.org Good luck. From matt.gregory at oregonstate.edu Fri Aug 24 22:03:41 2012 From: matt.gregory at oregonstate.edu (Matt Gregory) Date: Fri, 24 Aug 2012 13:03:41 -0700 Subject: [Tutor] creating a subclass from superclass without __init__ In-Reply-To: <5037CF98.3080602@pearwood.info> References: <5037CF98.3080602@pearwood.info> Message-ID: On 8/24/2012 12:01 PM, Steven D'Aprano wrote: > That's not a use-case. A use-case is a real-world problem that you > are trying to solve. You have skipped the problem and jumped straight > to what you think is the solution: "create a subclass which can't be > instantiated directly". > > I can't imagine any problem where the solution to the problem depends > on whether or not you instantiated a subclass using: > > Spam(*args) > > or > > SpamMaker.make(*args) Clearly my explanation was pretty bad. I'll start with my real-world problem instead, although it may be outside the scope of this list (thus my lame attempt to abstract it). There are two classes of interest in GDAL - a Dataset which typically describes a geospatial raster file and Band which described a single band from the Dataset. gdal.Band just raises an AttributeError within __init__, but a gdal.Band instance can be created using gdal.Dataset.GetRasterBand(bandNum). I had wanted to create additional methods on gdal.Band that would allow some GIS type operations (e.g. overloading __add__), thus I thought I would need a subclass of gdal.Band. So I was unsure of how to get an instance of my subclass when the only way I knew how to create an instance of the superclass (gdal.Band) was through the call to GetRasterBand(). Does that make any sense in relation to my contrived example? In my example, I didn't really want to create a Spam instance from the SubSpam __new__, I just didn't know how to return a SubSpam instance that was forced to go through SpamMaker.make_spam(). matt From crawlzone at gmail.com Sat Aug 25 00:36:23 2012 From: crawlzone at gmail.com (Ray Jones) Date: Fri, 24 Aug 2012 15:36:23 -0700 Subject: [Tutor] 2.7.3 'CalledProcessError' error....why? Message-ID: <503801E7.8060202@gmail.com> My code: try: subprocess.check_call(['ping', '-w1', ip]) except CalledProcessError: print 'System', ip, 'is not responding. Exiting....' sys.exit(4) else: return None The result: Traceback (most recent call last): File "./testing.py", line 222, in main() File "./testing.py", line 170, in main ip, port, endTime = parse_command_line(args) File "./testing.py", line 111, in parse_command_line ip = do_ip(args) File "./testing.py", line 85, in do_ip ping_ip(ip) File "./testing.py", line 44, in ping_ip except CalledProcessError: NameError: global name 'CalledProcessError' is not defined I didn't think Python return errors needed to be defined. I'll work around it with call_output, but what the heck is going on here? What do I not understand? From emile at fenx.com Sat Aug 25 01:14:47 2012 From: emile at fenx.com (Emile van Sebille) Date: Fri, 24 Aug 2012 16:14:47 -0700 Subject: [Tutor] 2.7.3 'CalledProcessError' error....why? In-Reply-To: <503801E7.8060202@gmail.com> References: <503801E7.8060202@gmail.com> Message-ID: On 8/24/2012 3:36 PM Ray Jones said... > My code: > > try: > subprocess.check_call(['ping', '-w1', ip]) > except CalledProcessError: > print 'System', ip, 'is not responding. Exiting....' > sys.exit(4) > else: return None > > The result: > > Traceback (most recent call last): > File "./testing.py", line 222, in > main() > File "./testing.py", line 170, in main > ip, port, endTime = parse_command_line(args) > File "./testing.py", line 111, in parse_command_line > ip = do_ip(args) > File "./testing.py", line 85, in do_ip > ping_ip(ip) > File "./testing.py", line 44, in ping_ip > except CalledProcessError: Try it as: except subprocess.CalledProcessError, e print e.output HTH, Emile > NameError: global name 'CalledProcessError' is not defined > > I didn't think Python return errors needed to be defined. I'll work > around it with call_output, but what the heck is going on here? What do > I not understand? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From crawlzone at gmail.com Sat Aug 25 01:38:26 2012 From: crawlzone at gmail.com (Ray Jones) Date: Fri, 24 Aug 2012 16:38:26 -0700 Subject: [Tutor] 2.7.3 'CalledProcessError' error....why? In-Reply-To: References: <503801E7.8060202@gmail.com> Message-ID: <50381072.10404@gmail.com> On 08/24/2012 04:14 PM, Emile van Sebille wrote: > On 8/24/2012 3:36 PM Ray Jones said... >> My code: >> >> try: >> subprocess.check_call(['ping', '-w1', ip]) >> except CalledProcessError: >> print 'System', ip, 'is not responding. Exiting....' >> sys.exit(4) >> else: return None >> >> The result: >> >> Traceback (most recent call last): >> File "./testing.py", line 222, in >> main() >> File "./testing.py", line 170, in main >> ip, port, endTime = parse_command_line(args) >> File "./testing.py", line 111, in parse_command_line >> ip = do_ip(args) >> File "./testing.py", line 85, in do_ip >> ping_ip(ip) >> File "./testing.py", line 44, in ping_ip >> except CalledProcessError: > > Try it as: > > except subprocess.CalledProcessError, e > print e.output It worked like a charm. Thanks. Ray From steve at pearwood.info Sat Aug 25 03:17:16 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 25 Aug 2012 11:17:16 +1000 Subject: [Tutor] creating a subclass from superclass without __init__ In-Reply-To: References: <5037CF98.3080602@pearwood.info> Message-ID: <5038279C.7030309@pearwood.info> On 25/08/12 06:03, Matt Gregory wrote: > There are two classes of interest in GDAL - a Dataset which typically >describes a geospatial raster file and Band which described a single band > from the Dataset. gdal.Band just raises an AttributeError within __init__, >but a gdal.Band instance can be created using >gdal.Dataset.GetRasterBand(bandNum). What a nasty, horrible, pointless restriction. Is there any reason why you need to follow it? > I had wanted to create additional methods on gdal.Band that would allow >some GIS type operations (e.g. overloading __add__), thus I thought I would > need a subclass of gdal.Band. So I was unsure of how to get an instance of >my subclass when the only way I knew how to create an instance of the >superclass (gdal.Band) was through the call to GetRasterBand(). class Band(object): def __new__(cls, *args): return super(Band, cls).__new__(cls, *args) def __init__(cls, *args): # Silly booby-trap. raise AttributeError('not only is this a pointless restriction ' 'on how you create instances, but this is the wrong ' 'exception type too!') def GetRasterBand(*args): return Band.__new__(Band, *args) class MyBand(Band): def __init__(self, *args): # Override the silly booby trap. pass def __add__(self, other): return 'something...' __rand__ = __add__ Works for me. > Does that make any sense in relation to my contrived example? In my > example, I didn't really want to create a Spam instance from the > SubSpam __new__, I just didn't know how to return a SubSpam instance > that was forced to go through SpamMaker.make_spam(). I don't think you can, but I don't think you need to. -- Steven From eryksun at gmail.com Sat Aug 25 06:55:49 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 25 Aug 2012 00:55:49 -0400 Subject: [Tutor] creating a subclass from superclass without __init__ In-Reply-To: References: <5037CF98.3080602@pearwood.info> Message-ID: On Fri, Aug 24, 2012 at 4:03 PM, Matt Gregory wrote: > > There are two classes of interest in GDAL - a Dataset which typically > describes a geospatial raster file and Band which described a single band > from the Dataset. gdal.Band just raises an AttributeError within __init__, > but a gdal.Band instance can be created using > gdal.Dataset.GetRasterBand(bandNum). SWIG! Why'd it have it be SWIG? C++... very dangerous. You go first. ;) GDAL Usage http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/val_repl.py Proxy Classes Driver http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L355 Dataset http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L647 Band http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/osgeo/gdal.py#L851 SWIG Interfaces http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Driver.i#L31 http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Dataset.i#L308 http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/Band.i#L182 http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/include/python/gdal_python.i#L260 _gdal module http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/extensions/gdal_wrap.cpp Open, OpenShared, GetDriver, GetDriverByName, etc return proxies to C++ shadow classes. It doesn't look like the SWIG interface was written for extending the classes in Python (something called "directors" according to the docs). The proxies lack a callable __init__ because the C++ shadow classes have private constructors. Instead, you have calls such as Dataset.GetRasterBand, which calls _gdal.Dataset_GetRasterBand (i.e. the C++ function _wrap_Dataset_GetRasterBand), which calls GDALDatasetShadow_GetRasterBand, which calls GDALGetRasterBand. The result gets cast to a GDALRasterBandShadow*. Finally, Python gets the latter wrapped as a Band proxy. > I had wanted to create additional methods on gdal.Band that would allow some > GIS type operations (e.g. overloading __add__), thus I thought I would need > a subclass of gdal.Band. So I was unsure of how to get an instance of my > subclass when the only way I knew how to create an instance of the > superclass (gdal.Band) was through the call to GetRasterBand(). I don't see an easy way to do this without modifying the SWIG interface or monkey patching the Python classes. But my knowledge of SWIG is superficial. You should ask this on a more appropriate forum, or Stack Overflow. From chigga101 at gmail.com Sat Aug 25 13:45:29 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sat, 25 Aug 2012 12:45:29 +0100 Subject: [Tutor] Error message... In-Reply-To: References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Message-ID: >Hi Victoria. im a total beginner aswell but i noticed something. shouldnt this line: else: return s(0) == s(-1) and isPalindrome (s[1:-1]) be else: return s[0] == s[-1] and isPalindrome (s[1:-1]) it looks like you have the string s as a function which you are trying to call. what you wanted was an index position right? which should be s[] instead of s(). thanks for the help David. Sorry about the sent mail. Gmail is pretty confusing for me:( _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From victoriahomsy at yahoo.com Sat Aug 25 15:08:23 2012 From: victoriahomsy at yahoo.com (Victoria Homsy) Date: Sat, 25 Aug 2012 14:08:23 +0100 (BST) Subject: [Tutor] Error message... In-Reply-To: References: <1345731456.21993.YahooMailNeo@web29502.mail.ird.yahoo.com> <1345733864.56057.YahooMailNeo@web29504.mail.ird.yahoo.com> <1345735984.41184.YahooMailNeo@web29506.mail.ird.yahoo.com> Message-ID: <1345900103.1503.YahooMailNeo@web29505.mail.ird.yahoo.com> Thank you everyone for your help with my question - I understand what I was doing wrong now. I know I'm posting wrongly so I'm going to go and figure out how to do it properly for the future. Have a great day. From akleider at sonic.net Sat Aug 25 17:53:54 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Sat, 25 Aug 2012 08:53:54 -0700 Subject: [Tutor] reason(s) for trailing comma in dict declarations Message-ID: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> Part of a previous post: """ Here's the style I'd use: combos = { 0: 'id', 2: 'country', 3: 'type', 5: 'lat', 6: 'lon', 12: 'name', } Put each entry on its own line, indented by two spaces, and leave a trailing comma on the last entry. The latter is especially important in sequences of strings to prevent them from being "silently"<=>"concatenated" if you were to add an entry and forget the comma. """ When I first saw this I thought it would lead to a syntax error so tried it out.. Then played with it to try to recreate the '"silently"<=>"concatenated"' problem but couldn't. I like this syntax because it avoids the syntax error if the comma is omitted when adding an entry but I don't understand the (potential) concatenation problem. Could you explain please? alex From __peter__ at web.de Sat Aug 25 18:07:13 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 25 Aug 2012 18:07:13 +0200 Subject: [Tutor] reason(s) for trailing comma in dict declarations References: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> Message-ID: akleider at sonic.net wrote: > Part of a previous post: > """ > Here's the style I'd use: > > combos = { > 0: 'id', > 2: 'country', > 3: 'type', > 5: 'lat', > 6: 'lon', > 12: 'name', > } > > Put each entry on its own line, indented by two spaces, and leave a > trailing comma on the last entry. The latter is especially important > in sequences of strings to prevent them from being > "silently"<=>"concatenated" if you were to add an entry and forget the > comma. > """ > > When I first saw this I thought it would lead to a syntax error so tried > it out.. > Then played with it to try to recreate the '"silently"<=>"concatenated"' > problem but couldn't. I like this syntax because it avoids the syntax > error if the comma is omitted when adding an entry but I don't understand > the (potential) concatenation problem. > > Could you explain please? Consider the following list: >>> ["the" ... "quick", ... "brown" ... "fox"] ['thequick', 'brownfox'] "the" and "quick" look like two entries in the list, but as the comma is missing they are merged together. Same for "brown" and "fox". I think you cannot run into this problem with dictionaries as accidentally merging a value and a key would result in a syntax error at the second colon: >>> {"the": "quick" ... "brown": "fox"} File "", line 2 "brown": "fox"} ^ SyntaxError: invalid syntax From eryksun at gmail.com Sat Aug 25 18:09:58 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 25 Aug 2012 12:09:58 -0400 Subject: [Tutor] reason(s) for trailing comma in dict declarations In-Reply-To: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> References: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> Message-ID: On Sat, Aug 25, 2012 at 11:53 AM, wrote: > > Put each entry on its own line, indented by two spaces, and leave a > trailing comma on the last entry. The latter is especially important > in sequences of strings to prevent them from being > "silently"<=>"concatenated" if you were to add an entry and forget the > comma. > """ > > When I first saw this I thought it would lead to a syntax error so tried > it out.. > Then played with it to try to recreate the '"silently"<=>"concatenated"' > problem but couldn't. I like this syntax because it avoids the syntax > error if the comma is omitted when adding an entry but I don't understand > the (potential) concatenation problem. > > Could you explain please? Sure, I said the problem is with "sequences", not mappings. The syntax for a dictionary will catch the mistake. But try it with a list. x = [ "string1", "string2", "string3" # no comma ] Later you decide to add "string4" but forget to add the comma: x = [ "string1", "string2", "string3" # no comma "string4" ] Result: ['string1', 'string2', 'string3string4'] From akleider at sonic.net Sat Aug 25 19:00:44 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Sat, 25 Aug 2012 10:00:44 -0700 Subject: [Tutor] reason(s) for trailing comma in dict declarations In-Reply-To: References: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> Message-ID: Thanks for the clarification. Now it is clear. ak > On Sat, Aug 25, 2012 at 11:53 AM, wrote: >> >> Put each entry on its own line, indented by two spaces, and leave a >> trailing comma on the last entry. The latter is especially important >> in sequences of strings to prevent them from being >> "silently"<=>"concatenated" if you were to add an entry and forget the >> comma. >> """ >> >> When I first saw this I thought it would lead to a syntax error so tried >> it out.. >> Then played with it to try to recreate the '"silently"<=>"concatenated"' >> problem but couldn't. I like this syntax because it avoids the syntax >> error if the comma is omitted when adding an entry but I don't >> understand >> the (potential) concatenation problem. >> >> Could you explain please? > > Sure, I said the problem is with "sequences", not mappings. The syntax > for a dictionary will catch the mistake. But try it with a list. > > x = [ > "string1", > "string2", > "string3" # no comma > ] > > Later you decide to add "string4" but forget to add the comma: > > x = [ > "string1", > "string2", > "string3" # no comma > "string4" > ] > > Result: > > ['string1', 'string2', 'string3string4'] > > From alan.gauld at btinternet.com Sat Aug 25 19:39:55 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Aug 2012 18:39:55 +0100 Subject: [Tutor] reason(s) for trailing comma in dict declarations In-Reply-To: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> References: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> Message-ID: On 25/08/12 16:53, akleider at sonic.net wrote: > in sequences of strings to prevent them from being > "silently"<=>"concatenated" if you were to add an entry and forget the > comma. > error if the comma is omitted when adding an entry but I don't understand > the (potential) concatenation problem. Consider: >>> 'fred','joe' ('fred', 'joe') >>> 'fred''joe' 'fredjoe' >>> two adjacent strings without a comma get combined into a single string. Its a feature... mainly a remnant from the C foundations I suspect. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Sat Aug 25 22:31:36 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 25 Aug 2012 16:31:36 -0400 Subject: [Tutor] reason(s) for trailing comma in dict declarations In-Reply-To: References: <57648d4a4b30dfa1f888e4cbf15aeaf0.squirrel@webmail.sonic.net> Message-ID: On Sat, Aug 25, 2012 at 1:39 PM, Alan Gauld wrote: > > two adjacent strings without a comma get combined into a single string. > Its a feature... mainly a remnant from the C foundations I suspect. As a feature it can come in handy with long strings in expressions. Just for reference about the "C foundations", here's a similar example in C. #include int main() { int i, x_len; char *x[] = { "string1", "string2", "string3" /* no comma */ "string4" }; x_len = sizeof(x) / sizeof(x[0]); for (i = 0; i < x_len; i++) { printf("%s\n", x[i]); } return 0; } Output: string1 string2 string3string4 From crawlzone at gmail.com Sun Aug 26 02:46:08 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sat, 25 Aug 2012 17:46:08 -0700 Subject: [Tutor] 2.7.3 Popen argument issues Message-ID: <503971D0.5040609@gmail.com> Is there a method by which I can get an exact representation of command line arguments passed by Popen as seen by the called program? The argument error I receive shows me an argument that looks exactly like the argument that I use with Bash (it should - I copied and pasted it) - but the Bash version works while the Python version doesn't. The purpose is to capture http streaming video to a file and also split it out to a local port so that I can pop in and monitor where I am in the stream. Here is my Bash call to vlc (it works): vlc http://"$HOST":$PORT -I dummy --sout '#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}' & Here is my Python call to vlc (error response to follow): vlcExec = sp.Popen(['vlc', 'http://' + ip + ':' + port, '-I dummy', '--sout \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=outFile + '.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\'']) (That is not an escaped double quote at the end - that is " \' " and " ' "). Here is the resulting error from vlc: vlc: unknown option or missing mandatory argument `--sout '#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}'' From Joel.Levine at dartmouth.edu Sun Aug 26 03:56:53 2012 From: Joel.Levine at dartmouth.edu (Joel Levine) Date: Sun, 26 Aug 2012 01:56:53 +0000 Subject: [Tutor] askopenfilename - columns missing in navigationwindow Message-ID: Mystery of the day: I'm using Python 2.6. My program uses from tkFileDialog import askopenfilename and fn=askopenfilename() I use this program repeatedly. Up to a few hours ago, the navigation window opened with a full array of columns, including name and date. Once I mistakenly typed control-F (Find) instead of letters (for the file). Now the navigation window opens with only one column, the name column. The problem is there in my program, or in the IDLE interpreter, or in a terminal Python interpreter -- just using the two lines, the import statement and the askopenfilename. This is Python 2.6 on a Mac. A complete restart of the machine did not solve the problem. Any suggestions? Joel Levine -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Sun Aug 26 05:02:29 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 25 Aug 2012 23:02:29 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: <503971D0.5040609@gmail.com> References: <503971D0.5040609@gmail.com> Message-ID: On Sat, Aug 25, 2012 at 8:46 PM, Ray Jones wrote: > > Here is my Python call to vlc (error response to follow): > > vlcExec = sp.Popen(['vlc', 'http://' + ip + ':' + port, '-I dummy', > '--sout > \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=outFile > + '.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\'']) > Arguments are split on spaces. For example, it's ['-I', 'dummy'], not ['-I dummy']. You can put the command in a string and split() it. Hopefully the following will work once you set the values you're using for ip/port. import subprocess ip = "192.168.0.2" port = "1234" out_file = "testing.avi" out_ip = "127.0.0.1" out_port = "11300" dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port) sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http) cmd = "vlc http://%s:%s -I dummy --sout %s" % (ip, port, sout) p = subprocess.Popen(cmd.split()) To answer your initial question, you could call a Python script (or a Bash script if you prefer) instead of vlc. In Python, just print out the list sys.argv. cmd = "python argtest.py http://%s:%s -I dummy --sout %s" % (ip, port, sout) p = subprocess.Popen(cmd.split()) # argtest.py import sys print "\nArgs:" for arg in sys.argv: print arg From eryksun at gmail.com Sun Aug 26 05:11:52 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 25 Aug 2012 23:11:52 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: References: <503971D0.5040609@gmail.com> Message-ID: On Sat, Aug 25, 2012 at 11:02 PM, eryksun wrote: > > import subprocess > > ip = "192.168.0.2" > port = "1234" > > out_file = "testing.avi" > out_ip = "127.0.0.1" > out_port = "11300" > dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file > dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port) > sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http) > If that doesn't work, vlc might not like the extra quotes around sout. Try it without them: sout = "#duplicate{dst=%s,dst=%s}" % (dst_file, dst_http) From eryksun at gmail.com Sun Aug 26 06:25:18 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 00:25:18 -0400 Subject: [Tutor] askopenfilename - columns missing in navigationwindow In-Reply-To: References: Message-ID: On Sat, Aug 25, 2012 at 9:56 PM, Joel Levine wrote: > > from tkFileDialog import askopenfilename > fn=askopenfilename() > > I use this program repeatedly. Up to a few hours ago, the navigation window > opened with a full array of columns, including name and date. > > Once I mistakenly typed control-F (Find) instead of letters (for the > file). Now the navigation window opens with only one column, the name > column. Try running "tclsh" in the terminal. Then enter: package require Tk set fn [tk_getOpenFile] puts $fn exit Is it the same dialog as in Python? Knowing whether the problem is with Tcl/Tk or with Tkinter might get you closer to a solution. From eryksun at gmail.com Sun Aug 26 07:19:08 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 01:19:08 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: References: <503971D0.5040609@gmail.com> Message-ID: On Sat, Aug 25, 2012 at 11:02 PM, eryksun wrote: > > out_file = "testing.avi" > out_ip = "127.0.0.1" > out_port = "11300" > dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file > dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port) > sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http) > > cmd = "vlc http://%s:%s -I dummy --sout %s" % (ip, port, sout) > > p = subprocess.Popen(cmd.split()) Wow... That was silly of me. Don't use split(). It would split up arguments that have internal spaces. Just build the list. sout = "#duplicate{dst=%s,dst=%s}" % (dst_file, dst_http) cmd = ["vlc", "http://%s:%s" % (ip, port), "-I", "dummy", "--sout", sout] p = subprocess.Popen(cmd) From wolfrage8765 at gmail.com Sun Aug 26 10:35:18 2012 From: wolfrage8765 at gmail.com (Jordan) Date: Sun, 26 Aug 2012 10:35:18 +0200 Subject: [Tutor] better tools In-Reply-To: References: Message-ID: <5039DFC6.801@gmail.com> On 08/22/2012 06:51 PM, Don Jennings wrote: > [slightly OT] > > After watching Bret Victor's talk[1], I want **much** better tools for programming (and all of the other stuff I do on the computer). > > John Resig, creator of jQuery, claims[2] that Victor's presentation inspired the new platform for learning javascript at Khan Academy[3]. I plan to try it out. > > I agree so I was wondering what is your plan? I am thinking about making some contributions to NINJA IDE that will build towards this sort of an environment. Would you care to help since I have limited time and I am sure you have limited time too? But together we would be twice as fast. From crawlzone at gmail.com Sun Aug 26 13:55:47 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sun, 26 Aug 2012 04:55:47 -0700 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: References: <503971D0.5040609@gmail.com> Message-ID: <503A0EC3.3030907@gmail.com> On 08/25/2012 10:19 PM, eryksun wrote: > On Sat, Aug 25, 2012 at 11:02 PM, eryksun wrote: >> out_file = "testing.avi" >> out_ip = "127.0.0.1" >> out_port = "11300" >> dst_file = '"transcode{vb=400}:std{access=file,mux=avi,dst=%s}"' % out_file >> dst_http = '"std{access=http,mux=mpjpeg,dst=%s:%s}"' % (out_ip, out_port) >> sout = "'#duplicate{dst=%s,dst=%s}'" % (dst_file, dst_http) >> >> cmd = "vlc http://%s:%s -I dummy --sout %s" % (ip, port, sout) >> >> p = subprocess.Popen(cmd.split()) > Wow... That was silly of me. Don't use split(). It would split up > arguments that have internal spaces. Just build the list. > > sout = "#duplicate{dst=%s,dst=%s}" % (dst_file, dst_http) > > cmd = ["vlc", "http://%s:%s" % (ip, port), "-I", "dummy", "--sout", sout] > p = subprocess.Popen(cmd) [0x8d42554] stream_out_standard stream out error: no mux specified or found by extension [0x8d42134] main stream output error: stream chain failed for `standard{mux="",access=""#duplicate{dst="transcode{vb=400}",dst="std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=localhost:11300}"}""}' Notice the addition of `standard{mux="",access='' ' before the `#duplicate' . I think --sout adds a default combination if it doesn't find a proper argument. Unfortunately, I know a bit less about vlc command line arguments than I do about Python's generation of those arguments.... ;) I had originally patched together the vlc command line from examples on the web. I think I'll go back and refamiliarize myself with vlc's command line and see if I can interweave my new understanding of how Python creates arguments with some further vlc knowledge. Thanks a bunch for the help. If you get an epiphany, I'm always open to suggestions.... ;) Ray From dfjennings at gmail.com Sun Aug 26 14:57:05 2012 From: dfjennings at gmail.com (Don Jennings) Date: Sun, 26 Aug 2012 08:57:05 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: References: Message-ID: <51E75D82-5259-4AA2-BF06-A1380ED8AEC3@gmail.com> On Aug 26, 2012, at 12:25 AM, tutor-request at python.org wrote: > Message: 2 > Date: Sat, 25 Aug 2012 17:46:08 -0700 > From: Ray Jones > To: tutor at python.org > Subject: [Tutor] 2.7.3 Popen argument issues > Message-ID: <503971D0.5040609 at gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > Is there a method by which I can get an exact representation of command > line arguments passed by Popen as seen by the called program? The > argument error I receive shows me an argument that looks exactly like > the argument that I use with Bash (it should - I copied and pasted it) - > but the Bash version works while the Python version doesn't. > > The purpose is to capture http streaming video to a file and also split > it out to a local port so that I can pop in and monitor where I am in > the stream. > > Here is my Bash call to vlc (it works): > > vlc http://"$HOST":$PORT -I dummy --sout > '#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}' Notice that the above call ends in a single quote? > & > > Here is my Python call to vlc (error response to follow): > > vlcExec = sp.Popen(['vlc', 'http://' + ip + ':' + port, '-I dummy', > '--sout > \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=outFile > + '.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\'']) > > (That is not an escaped double quote at the end - that is " \' " and " ' "). > > Here is the resulting error from vlc: > > vlc: unknown option or missing mandatory argument `--sout > '#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}'' Notice that this one ends in a double quote? Those are **definitely not** the same :>) Take care, Don -------------- next part -------------- An HTML attachment was scrubbed... URL: From crawlzone at gmail.com Sun Aug 26 15:01:54 2012 From: crawlzone at gmail.com (Ray Jones) Date: Sun, 26 Aug 2012 06:01:54 -0700 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: <51E75D82-5259-4AA2-BF06-A1380ED8AEC3@gmail.com> References: <51E75D82-5259-4AA2-BF06-A1380ED8AEC3@gmail.com> Message-ID: <503A1E42.7050205@gmail.com> On 08/26/2012 05:57 AM, Don Jennings wrote: > > On Aug 26, 2012, at 12:25 AM, tutor-request at python.org > wrote: > >> Message: 2 >> Date: Sat, 25 Aug 2012 17:46:08 -0700 >> From: Ray Jones > >> To: tutor at python.org >> Subject: [Tutor] 2.7.3 Popen argument issues >> Message-ID: <503971D0.5040609 at gmail.com >> > >> Content-Type: text/plain; charset=ISO-8859-1 >> >> Is there a method by which I can get an exact representation of command >> line arguments passed by Popen as seen by the called program? The >> argument error I receive shows me an argument that looks exactly like >> the argument that I use with Bash (it should - I copied and pasted it) - >> but the Bash version works while the Python version doesn't. >> >> The purpose is to capture http streaming video to a file and also split >> it out to a local port so that I can pop in and monitor where I am in >> the stream. >> >> Here is my Bash call to vlc (it works): >> >> vlc http://"$HOST":$PORT -I dummy --sout >> '#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}' > > Notice that the above call ends in a single quote? > >> & >> >> Here is my Python call to vlc (error response to follow): >> >> vlcExec = sp.Popen(['vlc', 'http://' + ip + ':' + port, '-I dummy', >> '--sout >> \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=outFile >> + '.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\'']) >> >> (That is not an escaped double quote at the end - that is " \' " and >> " ' "). >> >> Here is the resulting error from vlc: >> >> vlc: unknown option or missing mandatory argument `--sout >> '#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}'' > > Notice that this one ends in a double quote? Those are **definitely > not** the same :>) I noticed that too. It took me a while to see why the difference.... Notice that vlc added a backtick before quoting the '--sout' string. That additional quote is the close of the backtick that it used to quote the string. ;) -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Aug 26 16:05:25 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 27 Aug 2012 00:05:25 +1000 Subject: [Tutor] askopenfilename - columns missing in navigationwindow In-Reply-To: References: Message-ID: <503A2D25.3030806@pearwood.info> On 26/08/12 11:56, Joel Levine wrote: > Mystery of the day: I'm using Python 2.6. My program uses > > from tkFileDialog import askopenfilename > > and > > fn=askopenfilename() > > I use this program repeatedly. Up to a few hours ago, the navigation >window opened with a full array of columns, including name and date. > > Once I mistakenly typed control-F (Find) instead of letters (for the >file). Now the navigation window opens with only one column, the name > column. Control-F or Command-F? (I assume Mac's still use the Command key, ? -- it's been many years since I've had a Mac.) > The problem is there in my program, or in the IDLE interpreter, or in >a terminal Python interpreter -- just using the two lines, the import >statement and the askopenfilename. I expect that this is Mac-specific behaviour. On Linux, at least, I don't get multiple columns at all, and Control-F does nothing. I expect that the file dialog box you see is the native Mac open file dialog, and Ctrl-F (or Cmd-F) doesn't do Find inside the dialog, but changes the dialog settings, which are then remembered for the next time. Have you tried hitting Ctrl-F again to see if it toggles the change? You may need to ask some Mac experts how to control the open file dialog settings. -- Steven From eryksun at gmail.com Sun Aug 26 16:12:28 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 10:12:28 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: <503A0EC3.3030907@gmail.com> References: <503971D0.5040609@gmail.com> <503A0EC3.3030907@gmail.com> Message-ID: On Sun, Aug 26, 2012 at 7:55 AM, Ray Jones wrote: > > [0x8d42554] stream_out_standard stream out error: no mux specified or > found by extension > [0x8d42134] main stream output error: stream chain failed for > `standard{mux="",access=""#duplicate{dst="transcode{vb=400}",dst="std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=localhost:11300}"}""}' > > > Notice the addition of `standard{mux="",access='' ' before the > `#duplicate' . I think --sout adds a default combination if it doesn't > find a proper argument. Unfortunately, I know a bit less about vlc > command line arguments than I do about Python's generation of those > arguments.... ;) But the Bash call worked? cmd = 'vlc http://"HOST":PORT -I dummy --sout \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\'' print '\n'.join(shlex.split(cmd)) # shlex.split parses like the shell Output: vlc http://HOST:PORT -I dummy --sout #duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"} From chigga101 at gmail.com Sun Aug 26 20:09:32 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sun, 26 Aug 2012 19:09:32 +0100 Subject: [Tutor] calculation issue. Message-ID: Hi guys ive been reading a beginners book on python and the author created a simple game with the liewires package. I understand the explaination but there is just 1 part that i don't quite get. I'm not great at maths but this isn't complex at all. i don't know if i should post the whole code, cos' its only 1 line i don't get, so ill post only relevant code and my comments inbetwen these lines "-----" <- so skim to those lines which are relevant. 1st here is the line of code i don't get. # sets buffer to approx 30% of pizza height, regardless of pizza speed self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1 so the game is about a chef who drops pizzas from the top of a building. Both chef and pizza are objects from a chef and pizza class. ----- the pizza falls at a speed of 1. ------- class Pizza(games.Sprite): ---speed = 1-------- image = games.load_image("pizza.bmp") def __init__(self, x, y = 90): """ Initialize a Pizza object. """ super(Pizza, self).__init__(image = Pizza.image, x = x, y = y, ------dy = Pizza.speed------ #dy is the verticle movement of the falling pizzas ----------the pizza object is initilized in the chef class---------- ------self.tme_till_drop is the property i need help with--------- class Chef(games.Sprite): def __init__(self, y = 55, speed = 2, odds_change = 200): """ Initialize the Chef object. """ super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = y, dx = speed) self.odds_change = odds_change ----self.time_til_drop = 0-------- self.check_drop() #this is the function that determines timing of pizza drops def check_drop(self): """ Decrease countdown or drop pizza and reset countdown. """ if self.time_til_drop > 0: self.time_til_drop -= 1 else: new_pizza = Pizza(x = self.x) #positions the pizza's x(horizontal positon) to match that of the chef's games.screen.add(new_pizza) # set buffer to approx 30% of pizza height, regardless of pizza speed self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1 i understand the code until the comment set buffer. No where in the code does it say the pizza's height. Im guessing the height is the actual pizza image's height in pixels. "pizza.bmp" heres the code: self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1 so if, let's say the pizza height is 60(pixels). multiplying it by 1.3 gives me 78. dividing it by Pizza.speed seems pointless as Pizza.speed is 1, and i will get 78 again. then adding one makes it 79. i don't see how 79 is 30% of 60. 60 being the pizza's height. I know im looking at it all wrong. Can anyone please help explain to me that line of code and the authors calculation:( From eryksun at gmail.com Sun Aug 26 22:36:50 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 16:36:50 -0400 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On Sun, Aug 26, 2012 at 2:09 PM, Matthew Ngaha wrote: > > heres the code: self.time_til_drop = int(new_pizza.height * 1.3 / > Pizza.speed) + 1 height / speed is the number of steps it takes for a pizza to fall its complete height. It sets time_til_drop to approximately 130% of that number (with a +1 fudge factor in case the int() value is 0). It seems this function is called at every step to decrement time_til_drop until it's 0. Once it's 0, a new pizza is created to be dropped. The designer wants a 30% buffer (in steps) between the time one pizza has finished dropping and the creation of a new pizza. From eryksun at gmail.com Sun Aug 26 23:11:39 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 17:11:39 -0400 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On Sun, Aug 26, 2012 at 4:36 PM, eryksun wrote: > > it's 0. Once it's 0, a new pizza is created to be dropped. The > designer wants a 30% buffer (in steps) between the time one pizza has > finished dropping and the creation of a new pizza. To clarify, say the chef is at y=100, and a pizza is 100 pixels high with a speed of 2 pixels/step. check_drop() creates a new pizza and sets time_til_drop to 1 + int(100 * 1.3 / 2) = 66. So check_drop will be called 66 times, i.e. (65, 64, ..., 0), before another pizza gets created. In that time the previous pizza has fallen 2 * 66 = 132 pixels, from y=100 down to y=232 (if 0,0 is the upper left-hand corner). The bottom of the next pizza is at y = 100 + 100 = 200. That leaves a buffer between them of 232 - 200 = 32 pixels, which is 32% in this case. From alan.gauld at btinternet.com Sun Aug 26 23:56:07 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 26 Aug 2012 22:56:07 +0100 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On 26/08/12 19:09, Matthew Ngaha wrote: > heres the code: > self.time_til_drop = int(new_pizza.height * 1.3 / > Pizza.speed) + 1 > > so if, let's say the pizza height is 60(pixels). multiplying it by 1.3 > gives me 78. dividing it by Pizza.speed seems pointless as Pizza.speed > is 1, and i will get 78 again. then adding one makes it 79. Caveat: This is a guess since as you say we can't see the full code and don't know for sure what height refers to. What I think the author is saying is that before releasing a new pizza the previous one must be fully clear(ie its full height) plus some clearance(a buffer) and in this case its about 30% Thus the first pizza must travel 130% of its height before we are safe to release the next pizza without the images overlapping. For a 60px pizza that means a gap of 79 pixels approx. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Mon Aug 27 00:11:22 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 18:11:22 -0400 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On Sun, Aug 26, 2012 at 5:56 PM, Alan Gauld wrote: > > For a 60px pizza that means a gap of 79 pixels approx. Just to be clear, it's a gap of 79 pixels between the top y coordinates, not between the bottom of one pizza and the top of the other. The latter gap is 79 - 60 = 19 pixels, or a 31.67% buffer. From eryksun at gmail.com Mon Aug 27 00:11:24 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 18:11:24 -0400 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On Sun, Aug 26, 2012 at 5:56 PM, Alan Gauld wrote: > > For a 60px pizza that means a gap of 79 pixels approx. Just to be clear, it's a gap of 79 pixels between the top y coordinates, not between the bottom of one pizza and the top of the other. The latter gap is 79 - 60 = 19 pixels, or a 31.67% buffer. From chigga101 at gmail.com Mon Aug 27 00:19:21 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sun, 26 Aug 2012 23:19:21 +0100 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: thanks guys. Eryksun your explaination was brilliant and clear. i understood what you explained but got a bit confused with this line: The bottom of the next pizza is at y = 100 + 100 = 200 if this Pizza (pizza.b) was on hold at position y = 100 and the previous pizza(pizza.a) had fallen an additional 132 pixels to 232 before pizza.b was released, shouldn't pizza.b still only be at position y = 100? how did it gain the additional 100 pixels if it was on hold while pizza.a reached 232. i understand how you've worked it out, just need more clarification on how the Pizza.b fell an addition 100 pixels making its total distance 200. Sorry Alan the code was rather big, i didnt want it to be pain for you guys trying to help. here is the full code. I do understand the concept and what you've explained. i just want to fully understand the method for when i dont have a book to hold my hand:) back to the code: # Pizza Panic # Player must catch falling pizzas before they hit the ground from livewires import games, color import random games.init(screen_width = 640, screen_height = 480, fps = 50) class Pan(games.Sprite): """ A pan controlled by player to catch falling pizzas. """ image = games.load_image("pan.bmp") def __init__(self): """ Initialize Pan object and create Text object for score. """ super(Pan, self).__init__(image = Pan.image, x = games.mouse.x, bottom = games.screen.height) self.score = games.Text(value = 0, size = 25, color = color.black, top = 5, right = games.screen.width - 10) games.screen.add(self.score) def update(self): """ Move to mouse x position. """ self.x = games.mouse.x if self.left < 0: self.left = 0 if self.right > games.screen.width: self.right = games.screen.width self.check_catch() def check_catch(self): """ Check if catch pizzas. """ for pizza in self.overlapping_sprites: self.score.value += 10 self.score.right = games.screen.width - 10 pizza.handle_caught() class Pizza(games.Sprite): """ A pizza which falls to the ground. """ image = games.load_image("pizza.bmp") speed = 1 def __init__(self, x, y = 90): """ Initialize a Pizza object. """ super(Pizza, self).__init__(image = Pizza.image, x = x, y = y, dy = Pizza.speed) def update(self): """ Check if bottom edge has reached screen bottom. """ if self.bottom > games.screen.height: self.end_game() self.destroy() def handle_caught(self): """ Destroy self if caught. """ self.destroy() def end_game(self): """ End the game. """ end_message = games.Message(value = "Game Over", size = 90, color = color.red, x = games.screen.width/2, y = games.screen.height/2, lifetime = 5 * games.screen.fps, after_death = games.screen.quit) games.screen.add(end_message) class Chef(games.Sprite): """ A chef which moves left and right, dropping pizzas. """ image = games.load_image("chef.bmp") def __init__(self, y = 55, speed = 2, odds_change = 200): """ Initialize the Chef object. """ super(Chef, self).__init__(image = Chef.image, x = games.screen.width / 2, y = y, dx = speed) self.odds_change = odds_change self.time_til_drop = 0 def update(self): """ Determine if direction needs to be reversed. """ if self.left < 0 or self.right > games.screen.width: self.dx = -self.dx elif random.randrange(self.odds_change) == 0: self.dx = -self.dx self.check_drop() def check_drop(self): """ Decrease countdown or drop pizza and reset countdown. """ if self.time_til_drop > 0: self.time_til_drop -= 1 else: new_pizza = Pizza(x = self.x) games.screen.add(new_pizza) # set buffer to approx 30% of pizza height, regardless of pizza speed self.time_til_drop = int(new_pizza.height * 1.3 / Pizza.speed) + 1 def main(): """ Play the game. """ wall_image = games.load_image("wall.jpg", transparent = False) games.screen.background = wall_image the_chef = Chef() games.screen.add(the_chef) the_pan = Pan() games.screen.add(the_pan) games.mouse.is_visible = False games.screen.event_grab = True games.screen.mainloop() # start it up! main() From alan.gauld at btinternet.com Mon Aug 27 00:23:51 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 26 Aug 2012 23:23:51 +0100 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On 26/08/12 23:11, eryksun wrote: > On Sun, Aug 26, 2012 at 5:56 PM, Alan Gauld wrote: >> >> For a 60px pizza that means a gap of 79 pixels approx. > > Just to be clear, it's a gap of 79 pixels between the top y Yes, sorry that wasn't clear. The gap *between* images is only 19px. The 130% is how far one pizza drops before the next is released. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Mon Aug 27 00:47:14 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 26 Aug 2012 18:47:14 -0400 Subject: [Tutor] calculation issue. In-Reply-To: References: Message-ID: On Sun, Aug 26, 2012 at 6:19 PM, Matthew Ngaha wrote: > > if this Pizza (pizza.b) was on hold at position y = 100 and the > previous pizza(pizza.a) had fallen an additional 132 pixels to 232 > before pizza.b was released, shouldn't pizza.b still only be at > position y = 100? how did it gain the additional 100 pixels if it was > on hold while pizza.a reached 232. i understand how you've worked it > out, just need more clarification on how the Pizza.b fell an addition > 100 pixels making its total distance 200. The top of pizza.b is at y=100, but the bottom of pizza.b is at y=199 (sorry, I had a one-off error there). The gap between the pizzas is 232 - 199 - 1 = 32 pixels, from y=200 to y=231. > Sorry Alan the code was rather big, i didnt want it to be pain for > you guys trying to help. here is the full code. I do understand the You can link to the full code at a site such as pastbin.com, but be sure to include the relevant parts in your question. From crawlzone at gmail.com Mon Aug 27 09:52:00 2012 From: crawlzone at gmail.com (Ray Jones) Date: Mon, 27 Aug 2012 00:52:00 -0700 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: References: <503971D0.5040609@gmail.com> <503A0EC3.3030907@gmail.com> Message-ID: <503B2720.50901@gmail.com> On 08/26/2012 07:12 AM, eryksun wrote: > On Sun, Aug 26, 2012 at 7:55 AM, Ray Jones wrote: >> [0x8d42554] stream_out_standard stream out error: no mux specified or >> found by extension >> [0x8d42134] main stream output error: stream chain failed for >> `standard{mux="",access=""#duplicate{dst="transcode{vb=400}",dst="std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=localhost:11300}"}""}' >> >> >> Notice the addition of `standard{mux="",access='' ' before the >> `#duplicate' . I think --sout adds a default combination if it doesn't >> find a proper argument. Unfortunately, I know a bit less about vlc >> command line arguments than I do about Python's generation of those >> arguments.... ;) > But the Bash call worked? > > cmd = 'vlc http://"HOST":PORT -I dummy --sout > \'#duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"}\'' > > print '\n'.join(shlex.split(cmd)) # shlex.split parses like the shell > > Output: > > vlc > http://HOST:PORT > -I > dummy > --sout > #duplicate{dst="transcode{vb=400}:std{access=file,mux=avi,dst=testing.avi}",dst="std{access=http,mux=mpjpeg,dst=127.0.0.1:11300}"} Yes, the Bash call worked (in fact I tried it just prior to sending the original message just to be sure). I guess I'm a bit confused about 'splitting' the arguments. You said that Python splits arguments on spaces. What exactly happens to the arguments if we split them up into individual strings separated by commas? From d at davea.name Mon Aug 27 14:38:25 2012 From: d at davea.name (Dave Angel) Date: Mon, 27 Aug 2012 08:38:25 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: <503B2720.50901@gmail.com> References: <503971D0.5040609@gmail.com> <503A0EC3.3030907@gmail.com> <503B2720.50901@gmail.com> Message-ID: <503B6A41.2040504@davea.name> On 08/27/2012 03:52 AM, Ray Jones wrote: > On 08/26/2012 07:12 AM, eryksun wrote: >> > Yes, the Bash call worked (in fact I tried it just prior to sending the > original message just to be sure). > > I guess I'm a bit confused about 'splitting' the arguments. You said > that Python splits arguments on spaces. What exactly happens to the > arguments if we split them up into individual strings separated by commas? > > Python doesn't split the arguments, you've already split them. And you're not using shell=, so you don't invoke bash. However, the shell example (not using python) DOES split the arguments by spaces, and you have to replicate what the shell would do. Beyond that I cannot help. I'm not at all familiar with vlc. -- DaveA From eryksun at gmail.com Mon Aug 27 14:45:14 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 27 Aug 2012 08:45:14 -0400 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: <503B2720.50901@gmail.com> References: <503971D0.5040609@gmail.com> <503A0EC3.3030907@gmail.com> <503B2720.50901@gmail.com> Message-ID: On Mon, Aug 27, 2012 at 3:52 AM, Ray Jones wrote: > > Yes, the Bash call worked (in fact I tried it just prior to sending the > original message just to be sure). > > I guess I'm a bit confused about 'splitting' the arguments. You said > that Python splits arguments on spaces. What exactly happens to the > arguments if we split them up into individual strings separated by commas? I meant that only in comparison to how the shell tokenizes a command. For example, in the shell if you want spaces to be ignored, you have to escape them or use quotes, and if you want to include raw quotes/backslashes you have to escape them too. To tokenize a command string as the shell would, you can use shlex.split(): http://docs.python.org/library/shlex.html#shlex.split Otherwise the space character doesn't have any special significance. What matters is that all of the argument strings are in the list in the right order. It doesn't matter how they get there. So instead of using shlex.split you may as well build the list directly. Popen uses the args list/tuple to call the executable (after forking and setting up the pipes). This passes through the os module on to the built-in posix module. There it eventually calls the POSIX system function execv(path, argv). (FYI, in Windows there's no fork/exec; Win32 CreateProcess is used instead.) POSIX is a system standard that's part of the core UNIX standard (unix.org). It's a collaboration between IEEE and The Open Group. Here's the documentation for the exec family of calls: http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html Here's their description of the arguments of execv(path, argv): * The argument "path" points to a pathname that identifies the new process image file. * The argument "argv" is an array of character pointers to null- terminated strings. The application shall ensure that the last member of this array is a null pointer. These strings shall constitute the argument list available to the new process image. The value in argv[0] should point to a filename that is associated with the process being started by one of the exec functions. Most programs expect their arguments to have been tokenized as the shell would as a matter of convention. So, for example, if vlc gets "-I" in argv[1] it expects that argv[2] will be a value such as "dummy". A value of "-I dummy" in argv[1] in principle shouldn't work. In practice vlc seems pretty flexible and accepts it both ways. From crawlzone at gmail.com Mon Aug 27 23:38:34 2012 From: crawlzone at gmail.com (Ray Jones) Date: Mon, 27 Aug 2012 14:38:34 -0700 Subject: [Tutor] 2.7.3 Popen argument issues In-Reply-To: References: <503971D0.5040609@gmail.com> <503A0EC3.3030907@gmail.com> <503B2720.50901@gmail.com> Message-ID: <503BE8DA.5040205@gmail.com> On 08/27/2012 05:45 AM, eryksun wrote: > Most programs expect their arguments to have been tokenized as the > shell would as a matter of convention. So, for example, if vlc gets > "-I" in argv[1] it expects that argv[2] will be a value such as > "dummy". A value of "-I dummy" in argv[1] in principle shouldn't work. > In practice vlc seems pretty flexible and accepts it both ways. Thanks for the explanation. By the way, my call to vlc now works. Thanks for breaking up the parts of the argument into variables - it helped me understand greatly both what was happening on the command line and how to make code more readable. What worked was to remove the extra quotes from around the argument parts and placing them around the %s formatting inside 'sout'. I'm not certain why that made a difference.... (BTW, I had gotten the dst_file portion to work, but the dst_http kept failing. I finally used the debug switch vlc and 'less' to compare the outputs of the working Bash call and the non-working Python call.....well, it did finally jump out at me that the working call contained a 'mpjpeg' mux while the non-working call was misnamed a 'mjpeg' mux. ) Thanks for your help - I have a greater understanding of what's going on. When I'm 80, I might be able to claim guruship like you! ;) Ray From rdmoores at gmail.com Tue Aug 28 02:26:26 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 27 Aug 2012 17:26:26 -0700 Subject: [Tutor] Why begin a function name with an underscore Message-ID: I've been going through Steven D'Aprano's pyprimes () and have many questions. One is why begin a function name with an underscore. He has several functions that do this. Two are: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected an integer but got %r' % obj) def _validate_num(obj): """Raise an exception if obj is not a finite real number.""" m = obj + 0 # May raise TypeError. if not isfinite(m): raise ValueError('expected a finite real number but got %r' % obj) I'm stealing these for their usefulness. But why the underscores? I think I've also seen elsewhere variable names that also begin with an underscore, e.g., _a . Dick Moores From newkedison at gmail.com Tue Aug 28 02:34:26 2012 From: newkedison at gmail.com (=?UTF-8?B?572X5YGl5b+g?=) Date: Tue, 28 Aug 2012 08:34:26 +0800 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: > > It's better to use the single-leading-underscore convention, _internal. > This isn't name mangled at all; it just indicates to others to "be careful > with this, it's an internal implementation detail; don't touch it if you > don't *fully* understand it". It's only a convention though. you can read some other info for naming in python here: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#naming On 28 August 2012 08:26, Richard D. Moores wrote: > I've been going through Steven D'Aprano's pyprimes > () and have many > questions. One is why begin a function name with an underscore. He has > several functions that do this. Two are: > > def _validate_int(obj): > """Raise an exception if obj is not an integer.""" > m = int(obj + 0) # May raise TypeError. > if obj != m: > raise ValueError('expected an integer but got %r' % obj) > > > def _validate_num(obj): > """Raise an exception if obj is not a finite real number.""" > m = obj + 0 # May raise TypeError. > if not isfinite(m): > raise ValueError('expected a finite real number but got %r' % obj) > > I'm stealing these for their usefulness. But why the underscores? > > I think I've also seen elsewhere variable names that also begin with > an underscore, e.g., _a . > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- ?????????????? ??? ???021?57850218?225 ???13764515733 ??/GTalk?newkedison at gmail.com IMPORTANT NOTE: This e-mail and any attachment are confidential and may contain trade secrets and may also be legally privileged or otherwise protected from disclosure. If you have received it in error, you are herewith informed about its status. Please notify us immediately by reply e-mail and then delete this e-mail and any attachment from your system. You are prohibited from making use of or copying this e-mail or any attachment or disclosing the contents to any other person. ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Tue Aug 28 06:13:09 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 27 Aug 2012 21:13:09 -0700 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > something like: > > def _validate_int(obj): > """Raise an exception if obj is not an integer.""" > m = int(obj + 0) # May raise TypeError. > if obj != m: > raise ValueError('expected an integer but got %r' % obj) > > > is a really awkward way to test if something's an integer, and checking > types in general is usually a sign of larger flaws in laying out useful > code. What the best way to test if something's an integer? Dick Moores From jerry.scofield at gmail.com Tue Aug 28 09:13:02 2012 From: jerry.scofield at gmail.com (Jerry Zhang) Date: Tue, 28 Aug 2012 15:13:02 +0800 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: 2012/8/28 Richard D. Moores > On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett > wrote: > > > something like: > > > > def _validate_int(obj): > > """Raise an exception if obj is not an integer.""" > > m = int(obj + 0) # May raise TypeError. > > if obj != m: > > raise ValueError('expected an integer but got %r' % obj) > > > > > > is a really awkward way to test if something's an integer, and checking > > types in general is usually a sign of larger flaws in laying out useful > > code. > > What the best way to test if something's an integer? > >>>a = 4 >>>isinstance(a, int) True > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Tue Aug 28 10:06:37 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Tue, 28 Aug 2012 01:06:37 -0700 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang wrote: > > > 2012/8/28 Richard D. Moores > >> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett >> wrote: >> >> > something like: >> > >> > def _validate_int(obj): >> > """Raise an exception if obj is not an integer.""" >> > m = int(obj + 0) # May raise TypeError. >> > if obj != m: >> > raise ValueError('expected an integer but got %r' % obj) >> > >> > >> > is a really awkward way to test if something's an integer, and checking >> > types in general is usually a sign of larger flaws in laying out useful >> > code. >> >> What the best way to test if something's an integer? > > >>>>a = 4 >>>>isinstance(a, int) > True >>> isinstance(3., int) False What if I wanted 3., 1234., etc. to be considered ints, as they are by _validate_int() ? Dick From timomlists at gmail.com Tue Aug 28 10:21:55 2012 From: timomlists at gmail.com (Timo) Date: Tue, 28 Aug 2012 10:21:55 +0200 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: <503C7FA3.3020601@gmail.com> Op 28-08-12 10:06, Richard D. Moores schreef: > On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang wrote: >> >> 2012/8/28 Richard D. Moores >> >>> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett >>> wrote: >>> >>>> something like: >>>> >>>> def _validate_int(obj): >>>> """Raise an exception if obj is not an integer.""" >>>> m = int(obj + 0) # May raise TypeError. >>>> if obj != m: >>>> raise ValueError('expected an integer but got %r' % obj) >>>> >>>> >>>> is a really awkward way to test if something's an integer, and checking >>>> types in general is usually a sign of larger flaws in laying out useful >>>> code. >>> What the best way to test if something's an integer? >> >>>>> a = 4 >>>>> isinstance(a, int) >> True >>>> isinstance(3., int) > False > > What if I wanted 3., 1234., etc. to be considered ints, as they are by > _validate_int() ? >>> isinstance(3., (int, float)) True Because 3. is a float, not int. Timo > > Dick > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Tue Aug 28 11:02:11 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Aug 2012 11:02:11 +0200 Subject: [Tutor] Why begin a function name with an underscore References: <503C7FA3.3020601@gmail.com> Message-ID: Timo wrote: > Op 28-08-12 10:06, Richard D. Moores schreef: >> On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang >> wrote: >>> >>> 2012/8/28 Richard D. Moores >>> >>>> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett >>>> wrote: >>>> >>>>> something like: >>>>> >>>>> def _validate_int(obj): >>>>> """Raise an exception if obj is not an integer.""" >>>>> m = int(obj + 0) # May raise TypeError. >>>>> if obj != m: >>>>> raise ValueError('expected an integer but got %r' % obj) >>>>> >>>>> >>>>> is a really awkward way to test if something's an integer, and >>>>> checking types in general is usually a sign of larger flaws in laying >>>>> out useful code. >>>> What the best way to test if something's an integer? >>> >>>>>> a = 4 >>>>>> isinstance(a, int) >>> True >>>>> isinstance(3., int) >> False >> >> What if I wanted 3., 1234., etc. to be considered ints, as they are by >> _validate_int() ? > > >>> isinstance(3., (int, float)) > True > > Because 3. is a float, not int. Note that the original check takes the value into account, too: >>> import pyprimes >>> pyprimes._validate_int(1.0) >>> pyprimes._validate_int(1.5) Traceback (most recent call last): File "", line 1, in File "pyprimes.py", line 286, in _validate_int raise ValueError('expected an integer but got %r' % obj) ValueError: expected an integer but got 1.5 >>> import fractions >>> pyprimes._validate_int(fractions.Fraction(10,2)) Personally, I'm a big fan of ducktyping, so I would probably remove the check completely and live with the consequences: >>> pyprimes._validate_int = lambda x: None >>> pyprimes.isprime_naive(8.5) True garbage-in, garbage-out -- so what. From breamoreboy at yahoo.co.uk Tue Aug 28 11:13:27 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 Aug 2012 10:13:27 +0100 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: On 28/08/2012 05:13, Richard D. Moores wrote: > On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > >> something like: >> >> def _validate_int(obj): >> """Raise an exception if obj is not an integer.""" >> m = int(obj + 0) # May raise TypeError. >> if obj != m: >> raise ValueError('expected an integer but got %r' % obj) >> >> >> is a really awkward way to test if something's an integer, and checking >> types in general is usually a sign of larger flaws in laying out useful >> code. > > What the best way to test if something's an integer? > > Dick Moores > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I rarely if ever test for any object type. I certainly wouldn't bother with the function above, I'd just like the program bomb and fix the fault at source. YMMV. -- Cheers. Mark Lawrence. From rdmoores at gmail.com Tue Aug 28 11:13:47 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Tue, 28 Aug 2012 02:13:47 -0700 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: <503C7FA3.3020601@gmail.com> References: <503C7FA3.3020601@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: > Op 28-08-12 10:06, Richard D. Moores schreef: >> What if I wanted 3., 1234., etc. to be considered ints, as they are by >> _validate_int() ? > > >>>> isinstance(3., (int, float)) > True > > Because 3. is a float, not int. And >>> isinstance(3.7, (int, float)) True No, I'm asking for something equivalent to _validate_int(). Dick > > Timo > >> >> Dick >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From hugo.yoshi at gmail.com Tue Aug 28 11:43:12 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 28 Aug 2012 11:43:12 +0200 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 11:13 AM, Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: > > Op 28-08-12 10:06, Richard D. Moores schreef: > > >> What if I wanted 3., 1234., etc. to be considered ints, as they are by > >> _validate_int() ? > > > > > >>>> isinstance(3., (int, float)) > > True > > > > Because 3. is a float, not int. > > And > >>> isinstance(3.7, (int, float)) > True > > No, I'm asking for something equivalent to _validate_int(). > > Dick > > kind of a hack, but how about: def validate_int(x): return int(x) == x >>> validate_int(3.4) False >>> validate_int(3.) True I know enough about floating point numbers to not trust this in obscure corner cases, but it might suffice for you. HTH, Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Aug 28 12:00:32 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Aug 2012 12:00:32 +0200 Subject: [Tutor] Why begin a function name with an underscore References: <503C7FA3.3020601@gmail.com> Message-ID: Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: >> Op 28-08-12 10:06, Richard D. Moores schreef: > >>> What if I wanted 3., 1234., etc. to be considered ints, as they are by >>> _validate_int() ? >> >> >>>>> isinstance(3., (int, float)) >> True >> >> Because 3. is a float, not int. > > And >>>> isinstance(3.7, (int, float)) > True > > No, I'm asking for something equivalent to _validate_int(). That's not how it works. You have to give a spec first and then you can compare how well it is met by a particular implementation. If you start with an implementation and then declare its behaviour to be the spec the implementation will always "win". That's why ISO 29500 works so well -- for the company that wrote it. Anyway here's an alternative implementation: >>> def vi(x): ... if not isinstance(x, numbers.Number): ... raise TypeError ... if not int(x) == x: ... raise ValueError ... >>> vi("") Traceback (most recent call last): File "", line 1, in File "", line 3, in vi TypeError >>> vi(1.5) Traceback (most recent call last): File "", line 1, in File "", line 5, in vi ValueError >>> vi(1.0) >>> The differences to _validate_int() are subtle: >>> class S(str): ... def __eq__(self, other): return True ... def __ne__(self, other): return False ... def __add__(self, other): return self ... >>> vi(S("42")) Traceback (most recent call last): File "", line 1, in File "", line 3, in vi TypeError >>> _validate_int(S("42")) >>> From shahdharmit at gmail.com Tue Aug 28 13:23:27 2012 From: shahdharmit at gmail.com (Dharmit Shah) Date: Tue, 28 Aug 2012 16:53:27 +0530 Subject: [Tutor] Recursion always returns None Message-ID: Hello, I am trying to do the following : 1) Ask user for the length of the word that he'd like to guess (for hangman game). 2) Pick a random word from /usr/share/dict/words (which I understand is not the best choice for hangman). 3) Call a function that would pick a random word to proceed further. Below is the code for the part I described above : [code] #!/bin/env python import random def pick_random(l, ln): # picks a random word from the list l of length ln global mystery word = random.choice(l) if word.__len__() != ln: pick_random(l, ln) # recursion else: print "Should return %s" % word # prints the chosen random word correctly return word # always return None, why? :( if __name__ == "__main__": ln = raw_input("How long word can you guess (number of alphabets) : ") ln = int(ln) l = [] with open("/usr/share/dict/words", "r") as f: for i in f.readlines(): i = i.split("\n")[0] if i.isalpha(): l.append(i) word = pick_random(l, ln) print word [/code] Sample output : $ python hangman.py How long word can you guess (number of alphabets) : 6 Should return inarch None $ The problem is that the last line "print word" always prints None. I know I am doing something wrong in the recursion part of the function "pick_random". Can someone please point what I am missing. Thank you! Cheers, Dharmit -- Dharmit Shah www.about.me/dharmit From wayne at waynewerner.com Tue Aug 28 13:24:38 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Tue, 28 Aug 2012 06:24:38 -0500 (CDT) Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: On Mon, 27 Aug 2012, Richard D. Moores wrote: > On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > >> something like: >> >> def _validate_int(obj): >> """Raise an exception if obj is not an integer.""" >> m = int(obj + 0) # May raise TypeError. >> if obj != m: >> raise ValueError('expected an integer but got %r' % obj) >> >> >> is a really awkward way to test if something's an integer, and checking >> types in general is usually a sign of larger flaws in laying out useful >> code. > > What the best way to test if something's an integer? try: whatever_you_want(supposed_integer) except ValueError: print("Oops, that wasn't an integer! Please try again") That's usually the best way... HTH, Wayne From hugo.yoshi at gmail.com Tue Aug 28 13:54:52 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 28 Aug 2012 13:54:52 +0200 Subject: [Tutor] Recursion always returns None In-Reply-To: References: Message-ID: On Tue, Aug 28, 2012 at 1:23 PM, Dharmit Shah wrote: > Hello, > > I am trying to do the following : > > 1) Ask user for the length of the word that he'd like to guess (for > hangman game). > 2) Pick a random word from /usr/share/dict/words (which I understand > is not the best choice for hangman). > 3) Call a function that would pick a random word to proceed further. > > Below is the code for the part I described above : > > [code] > > #!/bin/env python > import random > > def pick_random(l, ln): # picks a random word from the list > l of length ln > global mystery > word = random.choice(l) > if word.__len__() != ln: > pick_random(l, ln) # recursion > else: > print "Should return %s" % word # prints the chosen random > word correctly > return word # always > return None, why? :( > > Okay, a good technique here is to just go over the program step by step, keeping track of the call stack. An interactive debugger is great for this, but it's good to be able to do it in your mind too. Let's say we call pick_random with ln=6, and the nice wordlist you have. Our call stack looks like so: main (not in a function) --> pick_random(l, ln) So, we pick a random word, then compare its length (by the way, you should use len(word), not word.__len__(); it's much nicer to read). Now we have two possible options, either the word is the right length or it isn't. The first case is pretty trivial (return the word, done). So let's consider the second case. This will happen a lot, the chances of picking a 6 character word right away are pretty low. Okay so what happens now? We call pick_word again, pretty simple. Call stack: main --> pick_random(l, ln) --> pick_random(l, ln) pick_random's on the call stack twice now, which is what recursion means. Now we could go on and say we don't find the right length word again, and again, and again, but it won't give us any more information here. We'd just keep stacking up the same function. So let's say the second call to pick_random found a right word, and returned it. Call stack: main --> pick_random(l, ln) We're back in the first pick_random function, right where we left off. And now we can see our error, right here: if word.__len__() != ln: pick_random(l, ln) # recursion What do we do with the return value of the recursive call? Well, nothing. It just disappears. And then we continue on, past the if statement, fall out of the function, and pick_random will return None. What we should have done, is this: if word.__len__() != ln: return pick_random(l, ln) # recursion In general, when you call a function you should always be mindful of whether it has a return value, and where its return value is going. And when you're doing recursion, it is very important to understand how the call stack works, and how you can stack multiple instances of the same function on top of each other. HTH, Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Tue Aug 28 13:52:55 2012 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 28 Aug 2012 04:52:55 -0700 Subject: [Tutor] Recursion always returns None In-Reply-To: References: Message-ID: <503CB117.8030205@alchemy.com> On 28-Aug-12 04:23, Dharmit Shah wrote: > Hello, > > I am trying to do the following : > > 1) Ask user for the length of the word that he'd like to guess (for > hangman game). > 2) Pick a random word from /usr/share/dict/words (which I understand > is not the best choice for hangman). > 3) Call a function that would pick a random word to proceed further. > > Below is the code for the part I described above : I'm struggling to understand why you're using recursion here. It serves no evident purpose for what you're doing, and given a large dictionary of words, could seriously tank the performance of the application, quite possibly run it out of resources and crash it altogether. But since you're using recursion, note that when you make your recursive call, you're ignoring the return value from that call, so no returned value ever gets propagated out to the outer layers of recursion. If the outermost layer took the recursion (length of the chosen word is wrong), it calls itself and then never takes the branch including the return statement at all. ("return word" is never executed in that case). This causes the function to just 'fall off' the end which implicitly returns None. HTH > > [code] > > #!/bin/env python > import random > > def pick_random(l, ln): # picks a random word from the list > l of length ln > global mystery > word = random.choice(l) > if word.__len__() != ln: > pick_random(l, ln) # recursion > else: > print "Should return %s" % word # prints the chosen random > word correctly > return word # always > return None, why? :( > > if __name__ == "__main__": > ln = raw_input("How long word can you guess (number of alphabets) : ") > ln = int(ln) > l = [] > with open("/usr/share/dict/words", "r") as f: > for i in f.readlines(): > i = i.split("\n")[0] > if i.isalpha(): > l.append(i) > > word = pick_random(l, ln) > print word > > [/code] > > Sample output : > > $ python hangman.py > How long word can you guess (number of alphabets) : 6 > Should return inarch > None > $ > > The problem is that the last line "print word" always prints None. I > know I am doing something wrong in the recursion part of the function > "pick_random". Can someone please point what I am missing. Thank you! > > Cheers, > Dharmit > -- Steve Willoughby / steve at alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C From eryksun at gmail.com Tue Aug 28 14:51:20 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 28 Aug 2012 08:51:20 -0400 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__peter__ at web.de> wrote: > > Anyway here's an alternative implementation: > >>>> def vi(x): > ... if not isinstance(x, numbers.Number): > ... raise TypeError > ... if not int(x) == x: > ... raise ValueError You could test against numbers.Integral. But it's not fool-proof. Someone can register an incompatible class with the abstract base class (ABC). >>> import numbers >>> isinstance("42", numbers.Integral) False >>> numbers.Integral.register(str) >>> isinstance("42", numbers.Integral) True http://docs.python.org/library/abc From __peter__ at web.de Tue Aug 28 15:08:51 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Aug 2012 15:08:51 +0200 Subject: [Tutor] Why begin a function name with an underscore References: <503C7FA3.3020601@gmail.com> Message-ID: eryksun wrote: > On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__peter__ at web.de> wrote: >> >> Anyway here's an alternative implementation: >> >>>>> def vi(x): >> ... if not isinstance(x, numbers.Number): >> ... raise TypeError >> ... if not int(x) == x: >> ... raise ValueError > > You could test against numbers.Integral. That would reject "floats with an integral value" and therefore doesn't comply with the -- non-existing -- spec. > But it's not fool-proof. Nothing is. The code attempts to make expectations more explicit than Steven did in his original implementation. > Someone can register an incompatible class with the abstract base > class (ABC). > > >>> import numbers > >>> isinstance("42", numbers.Integral) > False > >>> numbers.Integral.register(str) > >>> isinstance("42", numbers.Integral) > True That's quite an elaborate scheme to shoot yourself in the foot ;) From eryksun at gmail.com Tue Aug 28 15:24:38 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 28 Aug 2012 09:24:38 -0400 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 9:08 AM, Peter Otten <__peter__ at web.de> wrote: > > That would reject "floats with an integral value" and therefore doesn't > comply with the -- non-existing -- spec. Gotcha. >> >>> import numbers >> >>> isinstance("42", numbers.Integral) >> False >> >>> numbers.Integral.register(str) >> >>> isinstance("42", numbers.Integral) >> True > > That's quite an elaborate scheme to shoot yourself in the foot ;) It was just a quick example. In practice what could happen is someone would register an integer-like class (instead of subclassing numbers.Integral or numbers.Number) that is incomplete and ends up raising an exception. From d at davea.name Tue Aug 28 15:48:30 2012 From: d at davea.name (Dave Angel) Date: Tue, 28 Aug 2012 09:48:30 -0400 Subject: [Tutor] Recursion always returns None In-Reply-To: References: Message-ID: <503CCC2E.1020409@davea.name> On 08/28/2012 07:23 AM, Dharmit Shah wrote: > Hello, > > I am trying to do the following : > > 1) Ask user for the length of the word that he'd like to guess (for > hangman game). > 2) Pick a random word from /usr/share/dict/words (which I understand > is not the best choice for hangman). > 3) Call a function that would pick a random word to proceed further. > > Below is the code for the part I described above : > > [code] > > #!/bin/env python > import random > > def pick_random(l, ln): # picks a random word from the list > l of length ln > global mystery > word = random.choice(l) > if word.__len__() != ln: > pick_random(l, ln) # recursion > else: > print "Should return %s" % word # prints the chosen random > word correctly > return word # always > return None, why? :( There's no return statement here, to cover the case where the if-clause succeeded. > if __name__ == "__main__": > ln = raw_input("How long word can you guess (number of alphabets) : ") > ln = int(ln) > l = [] > with open("/usr/share/dict/words", "r") as f: > for i in f.readlines(): > i = i.split("\n")[0] > if i.isalpha(): > l.append(i) > > word = pick_random(l, ln) > print word > > [/code] > > Sample output : > > $ python hangman.py > How long word can you guess (number of alphabets) : 6 > Should return inarch > None > $ > > The problem is that the last line "print word" always prints None. I > know I am doing something wrong in the recursion part of the function > "pick_random". Can someone please point what I am missing. Thank you! > > Cheers, > Dharmit > There are two things wrong, one of which has already been pointed out. But the most fundamental thing that's wrong is that once you have called the recursion, you don't return a value at all, simply falling off the end of the function. Python returns a value of None when you omit the return statement. So you should add a statement 'return word', which will eliminate the None. But of course it'll be the wrong word. To fix that, you need to assign the results of the call to pick_random() to the same local variable, word. As others have pointed out, this is a poor choice for recursion. Recursion can be more readable for some problems, when the problem statement is naturally recursive. But even then, it can frequently lead to stack overruns, and performance problems. But in this case a simple loop would make much more sense. So unless the instructor is requiring you to use recursion, please redo it as a loop. While we're at it, please use the len() function, rather than __len__() method. And instead doing a split() method for eliminating the linefeeds, what you really want to do is rstrip(). -- DaveA From shahdharmit at gmail.com Tue Aug 28 17:51:55 2012 From: shahdharmit at gmail.com (Dharmit Shah) Date: Tue, 28 Aug 2012 21:21:55 +0530 Subject: [Tutor] Recursion always returns None In-Reply-To: <503CCC2E.1020409@davea.name> References: <503CCC2E.1020409@davea.name> Message-ID: Hello, @Hugo Arts : Thank you! That was awesome to read. Thanks for the len() suggestion. @ Steve : Thank you. As suggested by Dave Angel, I am going to try the loop. And even before implementing it, I can feel that it's going to be more efficient than recursion. @Dave Angel : Thank you for the loop idea. It didn't strike me at all. @All : Thanks a bunch for helping me out. :) Cheers, Dharmit On Tue, Aug 28, 2012 at 7:18 PM, Dave Angel wrote: > On 08/28/2012 07:23 AM, Dharmit Shah wrote: >> Hello, >> >> I am trying to do the following : >> >> 1) Ask user for the length of the word that he'd like to guess (for >> hangman game). >> 2) Pick a random word from /usr/share/dict/words (which I understand >> is not the best choice for hangman). >> 3) Call a function that would pick a random word to proceed further. >> >> Below is the code for the part I described above : >> >> [code] >> >> #!/bin/env python >> import random >> >> def pick_random(l, ln): # picks a random word from the list >> l of length ln >> global mystery >> word = random.choice(l) >> if word.__len__() != ln: >> pick_random(l, ln) # recursion >> else: >> print "Should return %s" % word # prints the chosen random >> word correctly >> return word # always >> return None, why? :( > > There's no return statement here, to cover the case where the if-clause > succeeded. > >> if __name__ == "__main__": >> ln = raw_input("How long word can you guess (number of alphabets) : ") >> ln = int(ln) >> l = [] >> with open("/usr/share/dict/words", "r") as f: >> for i in f.readlines(): >> i = i.split("\n")[0] >> if i.isalpha(): >> l.append(i) >> >> word = pick_random(l, ln) >> print word >> >> [/code] >> >> Sample output : >> >> $ python hangman.py >> How long word can you guess (number of alphabets) : 6 >> Should return inarch >> None >> $ >> >> The problem is that the last line "print word" always prints None. I >> know I am doing something wrong in the recursion part of the function >> "pick_random". Can someone please point what I am missing. Thank you! >> >> Cheers, >> Dharmit >> > > There are two things wrong, one of which has already been pointed out. > But the most fundamental thing that's wrong is that once you have called > the recursion, you don't return a value at all, simply falling off the > end of the function. Python returns a value of None when you omit the > return statement. > > So you should add a statement 'return word', which will eliminate the > None. But of course it'll be the wrong word. To fix that, you need to > assign the results of the call to pick_random() to the same local > variable, word. > > As others have pointed out, this is a poor choice for recursion. > Recursion can be more readable for some problems, when the problem > statement is naturally recursive. But even then, it can frequently lead > to stack overruns, and performance problems. But in this case a simple > loop would make much more sense. So unless the instructor is requiring > you to use recursion, please redo it as a loop. > > While we're at it, please use the len() function, rather than __len__() > method. And instead doing a split() method for eliminating the > linefeeds, what you really want to do is rstrip(). > > > > -- > > DaveA > -- Dharmit Shah www.about.me/dharmit From breamoreboy at yahoo.co.uk Tue Aug 28 18:03:33 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 Aug 2012 17:03:33 +0100 Subject: [Tutor] Recursion always returns None In-Reply-To: References: <503CCC2E.1020409@davea.name> Message-ID: On 28/08/2012 16:51, Dharmit Shah wrote: > > @ Steve : Thank you. As suggested by Dave Angel, I am going to try the > loop. And even before implementing it, I can feel that it's going to > be more efficient than recursion. > May I ask why you appear to be concerned with efficiency for a hangman game? -- Cheers. Mark Lawrence. From alan.gauld at btinternet.com Tue Aug 28 18:13:21 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Aug 2012 17:13:21 +0100 Subject: [Tutor] Recursion always returns None In-Reply-To: References: <503CCC2E.1020409@davea.name> Message-ID: On 28/08/12 16:51, Dharmit Shah wrote: > @Dave Angel : Thank you for the loop idea. It didn't strike me at all. > For some reason some beginners seem to find recursion a natural pattern. Many others, including quite experienced programmers find it a mind bending concept. But as a general rule, where you want to repeat an naction a number of times thing loops. If you know in advance how many times to repeat (eg all the items in a list) think 'for' loops, if you don't know how often (eg until some input condition is met) think 'while' loops. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at alchemy.com Tue Aug 28 18:31:45 2012 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 28 Aug 2012 09:31:45 -0700 Subject: [Tutor] Recursion always returns None In-Reply-To: References: <503CCC2E.1020409@davea.name> Message-ID: <503CF271.7070700@alchemy.com> On 28-Aug-12 09:03, Mark Lawrence wrote: > On 28/08/2012 16:51, Dharmit Shah wrote: >> >> @ Steve : Thank you. As suggested by Dave Angel, I am going to try the >> loop. And even before implementing it, I can feel that it's going to >> be more efficient than recursion. >> > > May I ask why you appear to be concerned with efficiency for a hangman > game? Not the game per se, but if you're searching a file of thousands of words one at a time, randomly picking words and recursing if they're not what you intended, the hit--worst case--could be catastrophic. -- Steve Willoughby / steve at alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C From steve at alchemy.com Tue Aug 28 18:34:00 2012 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 28 Aug 2012 09:34:00 -0700 Subject: [Tutor] Recursion always returns None In-Reply-To: References: <503CCC2E.1020409@davea.name> Message-ID: <503CF2F8.6070007@alchemy.com> On 28-Aug-12 09:13, Alan Gauld wrote: > On 28/08/12 16:51, Dharmit Shah wrote: > >> @Dave Angel : Thank you for the loop idea. It didn't strike me at all. >> > For some reason some beginners seem to find recursion a natural pattern. There is a certain "hey, you can do that? That's cool!" factor when you first discover recursion. When it naturally applies to a problem, it's still a powerful thing, but yes, it's often misapplied by beginners. -- Steve Willoughby / steve at alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C From crawlzone at gmail.com Tue Aug 28 19:41:38 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 10:41:38 -0700 Subject: [Tutor] Installing modules with easy_install Message-ID: <503D02D2.40204@gmail.com> I'm working on another Python replacement for a Bash script, and I ran into a need for enhanced time zone functions. Following directions I found on a web site, I did the following: # easy_install --upgrade pytz Searching for pytz Reading http://pypi.python.org/simple/pytz/ Reading http://pytz.sourceforge.net Reading http://sourceforge.net/project/showfiles.php?group_id=79122 Reading http://www.stuartbishop.net/Software/pytz Reading http://sourceforge.net/projects/pytz/ Best match: pytz 2012d Downloading http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d-py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e Processing pytz-2012d-py2.7.egg creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages Adding pytz 2012d to easy-install.pth file Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg Processing dependencies for pytz Finished processing dependencies for pytz Everything I'm reading suggests that now I should have the pytz module available to me. But from iPython: In [1]: import pytz --------------------------------------------------------------------------- ImportError Traceback (most recent call last) /home/ray/ in () ImportError: No module named pytz In [2]: import pytz-2012d ------------------------------------------------------------ File "", line 1 import pytz-2012d ^ SyntaxError: invalid syntax So what do I need to do to get Python to recognize this module? Ray From joel.goldstick at gmail.com Tue Aug 28 19:48:46 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 28 Aug 2012 13:48:46 -0400 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D02D2.40204@gmail.com> References: <503D02D2.40204@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 1:41 PM, Ray Jones wrote: > I'm working on another Python replacement for a Bash script, and I ran > into a need for enhanced time zone functions. Following directions I > found on a web site, I did the following: > > # easy_install --upgrade pytz > Searching for pytz > Reading http://pypi.python.org/simple/pytz/ > Reading http://pytz.sourceforge.net > Reading http://sourceforge.net/project/showfiles.php?group_id=79122 > Reading http://www.stuartbishop.net/Software/pytz > Reading http://sourceforge.net/projects/pytz/ > Best match: pytz 2012d > Downloading > http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d-py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e > Processing pytz-2012d-py2.7.egg > creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages > Adding pytz 2012d to easy-install.pth file > > Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Processing dependencies for pytz > Finished processing dependencies for pytz > > > Everything I'm reading suggests that now I should have the pytz module > available to me. But from iPython: > > > In [1]: import pytz > --------------------------------------------------------------------------- > ImportError Traceback (most recent call last) > > /home/ray/ in () > > ImportError: No module named pytz > > > In [2]: import pytz-2012d > ------------------------------------------------------------ > File "", line 1 > import pytz-2012d > ^ > SyntaxError: invalid syntax > > > > So what do I need to do to get Python to recognize this module? > > > Ray > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor so you are calling it the wrong thing when you import see here: http://pytz.sourceforge.net/#example-usage Example & Usage Localized times and date arithmetic >>> from datetime import datetime, timedelta >>> from pytz import timezone >>> import pytz >>> utc = pytz.utc >>> utc.zone 'UTC' >>> eastern = timezone('US/Eastern') >>> eastern.zone 'US/Eastern' >>> amsterdam = timezone('Europe/Amsterdam') >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z' -- Joel Goldstick From __peter__ at web.de Tue Aug 28 20:06:26 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Aug 2012 20:06:26 +0200 Subject: [Tutor] Installing modules with easy_install References: <503D02D2.40204@gmail.com> Message-ID: Ray Jones wrote: > I'm working on another Python replacement for a Bash script, and I ran > into a need for enhanced time zone functions. Following directions I > found on a web site, I did the following: > > # easy_install --upgrade pytz > Searching for pytz > Reading http://pypi.python.org/simple/pytz/ > Reading http://pytz.sourceforge.net > Reading http://sourceforge.net/project/showfiles.php?group_id=79122 > Reading http://www.stuartbishop.net/Software/pytz > Reading http://sourceforge.net/projects/pytz/ > Best match: pytz 2012d > Downloading > http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e > Processing pytz-2012d-py2.7.egg > creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages > Adding pytz 2012d to easy-install.pth file > > Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Processing dependencies for pytz > Finished processing dependencies for pytz > > > Everything I'm reading suggests that now I should have the pytz module > available to me. But from iPython: > > > In [1]: import pytz > --------------------------------------------------------------------------- > ImportError Traceback (most recent call > last) > > /home/ray/ in () > > ImportError: No module named pytz Do you have multiple python installations on your machine? Do you run easy_install in one and ipython in another? From crawlzone at gmail.com Tue Aug 28 20:39:52 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 11:39:52 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: References: <503D02D2.40204@gmail.com> Message-ID: <503D1078.3030605@gmail.com> On 08/28/2012 11:06 AM, Peter Otten wrote: > Ray Jones wrote: > >> I'm working on another Python replacement for a Bash script, and I ran >> into a need for enhanced time zone functions. Following directions I >> found on a web site, I did the following: >> >> # easy_install --upgrade pytz >> Searching for pytz >> Reading http://pypi.python.org/simple/pytz/ >> Reading http://pytz.sourceforge.net >> Reading http://sourceforge.net/project/showfiles.php?group_id=79122 >> Reading http://www.stuartbishop.net/Software/pytz >> Reading http://sourceforge.net/projects/pytz/ >> Best match: pytz 2012d >> Downloading >> http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- > py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e >> Processing pytz-2012d-py2.7.egg >> creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >> Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages >> Adding pytz 2012d to easy-install.pth file >> >> Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >> Processing dependencies for pytz >> Finished processing dependencies for pytz >> >> >> Everything I'm reading suggests that now I should have the pytz module >> available to me. But from iPython: >> >> >> In [1]: import pytz >> > --------------------------------------------------------------------------- >> ImportError Traceback (most recent call >> last) >> >> /home/ray/ in () >> >> ImportError: No module named pytz > Do you have multiple python installations on your machine? Do you run > easy_install in one and ipython in another? Perhaps. But the module is not accessible from the 'python' shell, from 'idle', or from iPython. As I peruse Synaptic I find I have active installations of Ubuntu's basic python, python2.7, and python2.7-minimal. But are these separate installations? Virtually every system package thinks it's dependent on each one of these python packages. Suggestions? Ray From steve at pearwood.info Tue Aug 28 21:35:10 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Aug 2012 05:35:10 +1000 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D02D2.40204@gmail.com> References: <503D02D2.40204@gmail.com> Message-ID: <503D1D6E.9020808@pearwood.info> On 29/08/12 03:41, Ray Jones wrote: > I'm working on another Python replacement for a Bash script, and I ran > into a need for enhanced time zone functions. Following directions I > found on a web site, I did the following: > > # easy_install --upgrade pytz [...] > Everything I'm reading suggests that now I should have the pytz module > available to me. But from iPython: > ImportError: No module named pytz Any time you get mysterious errors in iPython, or IDLE, or any other add-on to Python, it is important to determine whether the problem is with Python itself, or the add-on. In this case, start up the vanilla Python interactive environment by entering "python" at the $ prompt, then "import pytz" at the >>> prompt. If you get an error: - copy and paste the full traceback - show us the contents of sys.path You can also determine how many Python installations you have. At the bash $ prompt, type: python TAB TAB (that is, press the TAB key twice is succession, do not type the letters "t" "a" "b") and your shell will list the possible executable Python's on your system. Copy and paste that output. -- Steven From __peter__ at web.de Tue Aug 28 21:44:06 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 28 Aug 2012 21:44:06 +0200 Subject: [Tutor] Installing modules with easy_install References: <503D02D2.40204@gmail.com> <503D1078.3030605@gmail.com> Message-ID: Ray Jones wrote: > On 08/28/2012 11:06 AM, Peter Otten wrote: >> Ray Jones wrote: >> >>> I'm working on another Python replacement for a Bash script, and I ran >>> into a need for enhanced time zone functions. Following directions I >>> found on a web site, I did the following: >>> >>> # easy_install --upgrade pytz >>> Searching for pytz >>> Reading http://pypi.python.org/simple/pytz/ >>> Reading http://pytz.sourceforge.net >>> Reading http://sourceforge.net/project/showfiles.php?group_id=79122 >>> Reading http://www.stuartbishop.net/Software/pytz >>> Reading http://sourceforge.net/projects/pytz/ >>> Best match: pytz 2012d >>> Downloading >>> http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- >> py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e >>> Processing pytz-2012d-py2.7.egg >>> creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >>> Extracting pytz-2012d-py2.7.egg to >>> /usr/local/lib/python2.7/dist-packages Adding pytz 2012d to >>> easy-install.pth file >>> >>> Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >>> Processing dependencies for pytz >>> Finished processing dependencies for pytz >>> >>> >>> Everything I'm reading suggests that now I should have the pytz module >>> available to me. But from iPython: >>> >>> >>> In [1]: import pytz >>> >> --------------------------------------------------------------------------- >>> ImportError Traceback (most recent call >>> last) >>> >>> /home/ray/ in () >>> >>> ImportError: No module named pytz >> Do you have multiple python installations on your machine? Do you run >> easy_install in one and ipython in another? > Perhaps. But the module is not accessible from the 'python' shell, from > 'idle', or from iPython. > > As I peruse Synaptic I find I have active installations of Ubuntu's > basic python, python2.7, and python2.7-minimal. But are these separate > installations? No, I suspected that you had a system python and a self-compiled one, i. e. that you have both /usr/local/bin/python and /usr/bin/python If that were the case then you should be able to import pytz into one of these. From crawlzone at gmail.com Tue Aug 28 21:47:49 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 12:47:49 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D1D6E.9020808@pearwood.info> References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> Message-ID: <503D2065.8070406@gmail.com> On 08/28/2012 12:35 PM, Steven D'Aprano wrote: > On 29/08/12 03:41, Ray Jones wrote: >> I'm working on another Python replacement for a Bash script, and I ran >> into a need for enhanced time zone functions. Following directions I >> found on a web site, I did the following: >> >> # easy_install --upgrade pytz > [...] >> Everything I'm reading suggests that now I should have the pytz module >> available to me. But from iPython: > >> ImportError: No module named pytz > > > Any time you get mysterious errors in iPython, or IDLE, or any other > add-on to Python, it is important to determine whether the problem is > with Python itself, or the add-on. > > In this case, start up the vanilla Python interactive environment by > entering "python" at the $ prompt, then "import pytz" at the >>> prompt. > > If you get an error: > > - copy and paste the full traceback > > - show us the contents of sys.path > > > You can also determine how many Python installations you have. At the > bash $ prompt, type: > > python TAB TAB > > (that is, press the TAB key twice is succession, do not type the > letters "t" "a" "b") > > and your shell will list the possible executable Python's on your > system. Copy and paste that output. > I tried importing the module with each of the three shells that I have on my system (I think I need to get rid of idle - I never use it anymore): python, ipython, and idle. None of them recognize the module. Discovering which python binaries are available shows that I have /usr/bin/python and /usr/bin/python2.7. /usr/bin/python is a link to /usr/bin/python2.7. My sys.path shows the following: ['', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/usr/lib/python2.7/dist-packages/IPython/Extensions', u'/home/ray/.ipython'] Aha! The sys path is in /usr/lib but the module was installed in /usr/local/lib. What's the easiest fix for this? Creating a link to the module in the OS, or adding (or changing) the sys.path? Ray From crawlzone at gmail.com Tue Aug 28 21:49:24 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 12:49:24 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: References: <503D02D2.40204@gmail.com> <503D1078.3030605@gmail.com> Message-ID: <503D20C4.8020906@gmail.com> On 08/28/2012 12:44 PM, Peter Otten wrote: > Ray Jones wrote: > >> On 08/28/2012 11:06 AM, Peter Otten wrote: >>> Ray Jones wrote: >>> >>>> I'm working on another Python replacement for a Bash script, and I ran >>>> into a need for enhanced time zone functions. Following directions I >>>> found on a web site, I did the following: >>>> >>>> # easy_install --upgrade pytz >>>> Searching for pytz >>>> Reading http://pypi.python.org/simple/pytz/ >>>> Reading http://pytz.sourceforge.net >>>> Reading http://sourceforge.net/project/showfiles.php?group_id=79122 >>>> Reading http://www.stuartbishop.net/Software/pytz >>>> Reading http://sourceforge.net/projects/pytz/ >>>> Best match: pytz 2012d >>>> Downloading >>>> http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- >>> py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e >>>> Processing pytz-2012d-py2.7.egg >>>> creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >>>> Extracting pytz-2012d-py2.7.egg to >>>> /usr/local/lib/python2.7/dist-packages Adding pytz 2012d to >>>> easy-install.pth file >>>> >>>> Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >>>> Processing dependencies for pytz >>>> Finished processing dependencies for pytz >>>> >>>> >>>> Everything I'm reading suggests that now I should have the pytz module >>>> available to me. But from iPython: >>>> >>>> >>>> In [1]: import pytz >>>> > --------------------------------------------------------------------------- >>>> ImportError Traceback (most recent call >>>> last) >>>> >>>> /home/ray/ in () >>>> >>>> ImportError: No module named pytz >>> Do you have multiple python installations on your machine? Do you run >>> easy_install in one and ipython in another? >> Perhaps. But the module is not accessible from the 'python' shell, from >> 'idle', or from iPython. >> >> As I peruse Synaptic I find I have active installations of Ubuntu's >> basic python, python2.7, and python2.7-minimal. But are these separate >> installations? > No, I suspected that you had a system python and a self-compiled one, i. e. > that you have both > > /usr/local/bin/python > > and > > /usr/bin/python > > If that were the case then you should be able to import pytz into one of > these. Bingo! Ray From eryksun at gmail.com Tue Aug 28 21:52:26 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 28 Aug 2012 15:52:26 -0400 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D1078.3030605@gmail.com> References: <503D02D2.40204@gmail.com> <503D1078.3030605@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 2:39 PM, Ray Jones wrote: > >> Do you have multiple python installations on your machine? Do you run >> easy_install in one and ipython in another? > > Perhaps. But the module is not accessible from the 'python' shell, from > 'idle', or from iPython. > > As I peruse Synaptic I find I have active installations of Ubuntu's > basic python, python2.7, and python2.7-minimal. But are these separate > installations? Virtually every system package thinks it's dependent on > each one of these python packages. > > Suggestions? Those are not separate installations. The python package depends on python2.7 and python-minimal. The latter depends on python2.7-minimal. You should be able to install pytz from the Ubuntu repository. Search for the package python-tz. If you install from the repository, be sure to manually delete the old installation in the local dist-packages directory. http://packages.ubuntu.com/search?searchon=names&keywords=python-tz That said, in Debian Wheezy, pytz installs and runs fine from /usr/local/lib/python2.7/dist-packages. You could hack a temporary fix with a .pth file or using the PYTHONPATH environment variable, but it's better to figure out the problem. To help debug your problem, first check which Python installation you're running. Run "ls -Hl `which python`". Is it /usr/bin/python? Next check whether the latter path is actually in sys.path. In Debian, on which Ubuntu is based, it gets added in /usr/lib/python2.7/site.py: elif os.sep == '/': sitepackages.append(os.path.join(prefix, "local/lib", "python" + sys.version[:3], "dist-packages")) sitepackages.append(os.path.join(prefix, "lib", "python" + sys.version[:3], "dist-packages")) Next check /usr/local/lib/python2.7/dist-packages/easy_install.pth. Does it have a line with "./pytz-2012d-py2.7.egg" to include the latter directory on the path (ignore the wonky code to modify the path's insertion point) ? Check inside that directory for a pytz directory that has an __init__.py. From crawlzone at gmail.com Tue Aug 28 22:00:15 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 13:00:15 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D1D6E.9020808@pearwood.info> References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> Message-ID: <503D234F.9010705@gmail.com> On 08/28/2012 12:35 PM, Steven D'Aprano wrote: > On 29/08/12 03:41, Ray Jones wrote: >> I'm working on another Python replacement for a Bash script, and I ran >> into a need for enhanced time zone functions. Following directions I >> found on a web site, I did the following: >> >> # easy_install --upgrade pytz > [...] >> Everything I'm reading suggests that now I should have the pytz module >> available to me. But from iPython: > >> ImportError: No module named pytz > > > Any time you get mysterious errors in iPython, or IDLE, or any other > add-on to Python, it is important to determine whether the problem is > with Python itself, or the add-on. > > In this case, start up the vanilla Python interactive environment by > entering "python" at the $ prompt, then "import pytz" at the >>> prompt. > > If you get an error: > > - copy and paste the full traceback > > - show us the contents of sys.path > > > You can also determine how many Python installations you have. At the > bash $ prompt, type: > > python TAB TAB > > (that is, press the TAB key twice is succession, do not type the > letters "t" "a" "b") > > and your shell will list the possible executable Python's on your > system. Copy and paste that output. Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included in the sys.path. Now what? Ray From crawlzone at gmail.com Tue Aug 28 22:12:16 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 13:12:16 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: References: <503D02D2.40204@gmail.com> <503D1078.3030605@gmail.com> Message-ID: <503D2620.7060201@gmail.com> On 08/28/2012 12:52 PM, eryksun wrote: > On Tue, Aug 28, 2012 at 2:39 PM, Ray Jones wrote: >>> Do you have multiple python installations on your machine? Do you run >>> easy_install in one and ipython in another? >> Perhaps. But the module is not accessible from the 'python' shell, from >> 'idle', or from iPython. >> >> As I peruse Synaptic I find I have active installations of Ubuntu's >> basic python, python2.7, and python2.7-minimal. But are these separate >> installations? Virtually every system package thinks it's dependent on >> each one of these python packages. >> >> Suggestions? > Those are not separate installations. The python package depends on > python2.7 and python-minimal. The latter depends on python2.7-minimal. > You should be able to install pytz from the Ubuntu repository. Search > for the package python-tz. If you install from the repository, be sure > to manually delete the old installation in the local dist-packages > directory. I took out the pytz egg package and installed from Synaptic (I had looked there originally, but I searched pytz rather than python-tz). I now have an importably pytz. So that particular issue is cleared up, but what about modules that aren't available in Synaptic? It seems I still have an issue with easy_install, no? Ray From eryksun at gmail.com Tue Aug 28 22:11:53 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 28 Aug 2012 16:11:53 -0400 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D234F.9010705@gmail.com> References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> <503D234F.9010705@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: > > Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included > in the sys.path. Now what? Good, but does sys.path contain /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? From crawlzone at gmail.com Tue Aug 28 22:13:12 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 13:13:12 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> <503D234F.9010705@gmail.com> Message-ID: <503D2658.4040207@gmail.com> On 08/28/2012 01:11 PM, eryksun wrote: > On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: >> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included >> in the sys.path. Now what? > Good, but does sys.path contain > /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? No. Ray From crawlzone at gmail.com Tue Aug 28 22:17:23 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 13:17:23 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> <503D234F.9010705@gmail.com> Message-ID: <503D2753.90409@gmail.com> On 08/28/2012 01:11 PM, eryksun wrote: > On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: >> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included >> in the sys.path. Now what? > Good, but does sys.path contain > /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? More info: /usr/lib/python2.7/dist-packages does not contain a easy-install.pth file. Ray From emile at fenx.com Tue Aug 28 22:35:38 2012 From: emile at fenx.com (Emile van Sebille) Date: Tue, 28 Aug 2012 13:35:38 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D2753.90409@gmail.com> References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> <503D234F.9010705@gmail.com> <503D2753.90409@gmail.com> Message-ID: On 8/28/2012 1:17 PM Ray Jones said... > On 08/28/2012 01:11 PM, eryksun wrote: >> On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: >>> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included >>> in the sys.path. Now what? >> Good, but does sys.path contain >> /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? > More info: > > /usr/lib/python2.7/dist-packages does not contain a easy-install.pth file. > You installed this with easy_install, so the version of python therein referenced is the same one that now should have access to it. Try : which easy_install then cat the result. The first line points to the executable python that installed pytz. On my local system this looks like: head -1 `which easy_install` #! /usr/bin/python copy and paste in the result, then import pytz from there and paste in the results if it doesn't work. Emile From crawlzone at gmail.com Tue Aug 28 22:48:16 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 13:48:16 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> <503D234F.9010705@gmail.com> <503D2753.90409@gmail.com> Message-ID: <503D2E90.909@gmail.com> On 08/28/2012 01:35 PM, Emile van Sebille wrote: > On 8/28/2012 1:17 PM Ray Jones said... >> On 08/28/2012 01:11 PM, eryksun wrote: >>> On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: >>>> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is >>>> included >>>> in the sys.path. Now what? >>> Good, but does sys.path contain >>> /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? >> More info: >> >> /usr/lib/python2.7/dist-packages does not contain a easy-install.pth >> file. >> > > You installed this with easy_install, so the version of python therein > referenced is the same one that now should have access to it. > > Try : which easy_install > > then cat the result. The first line points to the executable python > that installed pytz. On my local system this looks like: > > > head -1 `which easy_install` > #! /usr/bin/python > > copy and paste in the result, then import pytz from there and paste in > the results if it doesn't work. #! /usr/bin/python I think you come late to the party, but jump in - there's lots of room! We have solved the pytz problem by quarantining the pytz*.egg and installing python-tz from the Debian system. But while pytz now works, it does not address the problem of easy-install-ed modules not being recognized by python (btw, in perusing the /usr/local/bin/.../dist-packages I also found a shodan module (whatever that is) that python does not recognize - and I don't find shodan in the Debian packages). Ray From alan.gauld at btinternet.com Tue Aug 28 23:14:21 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Aug 2012 22:14:21 +0100 Subject: [Tutor] Recursion always returns None In-Reply-To: <503CF2F8.6070007@alchemy.com> References: <503CCC2E.1020409@davea.name> <503CF2F8.6070007@alchemy.com> Message-ID: On 28/08/12 17:34, Steve Willoughby wrote: >>> >> For some reason some beginners seem to find recursion a natural pattern. > > There is a certain "hey, you can do that? That's cool!" factor when you > first discover recursion. My point was that it seems to be a natural idea for many beginners, they discover it without being told. They just assume it will work. It comes up time and time again on this list from people who have never heard of it but are using it. Whereas others who need to be explicitly taught about it find it totally bizarre and mind bending. I've come to the conclusion that its a right-brain, left-brain type of thing. For some it just seems logical, for others perverse! Presumably John McCarthy was one of those who found it natural! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From joel.goldstick at gmail.com Tue Aug 28 23:35:02 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 28 Aug 2012 17:35:02 -0400 Subject: [Tutor] Recursion always returns None In-Reply-To: References: <503CCC2E.1020409@davea.name> <503CF2F8.6070007@alchemy.com> Message-ID: On Tue, Aug 28, 2012 at 5:14 PM, Alan Gauld wrote: > On 28/08/12 17:34, Steve Willoughby wrote: >>>> >>>> >>> For some reason some beginners seem to find recursion a natural pattern. >> >> >> There is a certain "hey, you can do that? That's cool!" factor when you >> first discover recursion. > > > My point was that it seems to be a natural idea for many beginners, they > discover it without being told. They just assume it will work. > It comes up time and time again on this list from people who have > never heard of it but are using it. > > Whereas others who need to be explicitly taught about it find it totally > bizarre and mind bending. I've come to the conclusion that its a > right-brain, left-brain type of thing. For some it just seems logical, for > others perverse! > > Presumably John McCarthy was one of those who found it natural! :-) > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Interesting idea. I thought it was pretty cool when I studied it, and re-study recursion, but I seldom think of writing code with recursion. It scares me in an unnatural way. I think that best problems for recursion are ones with really deep data structures, and i fear they will run out of stack space. No evidence, just my take on using recursion as opposed to liking to read how small some recursive solutions are. -- Joel Goldstick From emile at fenx.com Tue Aug 28 23:42:03 2012 From: emile at fenx.com (Emile van Sebille) Date: Tue, 28 Aug 2012 14:42:03 -0700 Subject: [Tutor] Installing modules with easy_install In-Reply-To: <503D2E90.909@gmail.com> References: <503D02D2.40204@gmail.com> <503D1D6E.9020808@pearwood.info> <503D234F.9010705@gmail.com> <503D2753.90409@gmail.com> <503D2E90.909@gmail.com> Message-ID: On 8/28/2012 1:48 PM Ray Jones said... > On 08/28/2012 01:35 PM, Emile van Sebille wrote: >> You installed this with easy_install, so the version of python therein >> referenced is the same one that now should have access to it. >> >> Try : which easy_install >> >> then cat the result. The first line points to the executable python >> that installed pytz. On my local system this looks like: >> >> >> head -1 `which easy_install` >> #! /usr/bin/python >> >> copy and paste in the result, then import pytz from there and paste in >> the results if it doesn't work. > #! /usr/bin/python > > I think you come late to the party, but jump in - there's lots of room! > > We have solved the pytz problem by quarantining the pytz*.egg and > installing python-tz from the Debian system. But while pytz now works, > it does not address the problem of easy-install-ed modules not being > recognized by python (btw, in perusing the > /usr/local/bin/.../dist-packages I also found a shodan module (whatever > that is) that python does not recognize - and I don't find shodan in the > Debian packages). > > My point was that easy_install isn't broken. Emile root at paj39:/home/emile/web# /usr/bin/easy_install pytz Searching for pytz Best match: pytz 2009l Adding pytz 2009l to easy-install.pth file Using /usr/lib/python2.6/dist-packages Processing dependencies for pytz Finished processing dependencies for pytz root at paj39:/home/emile/web# /usr/bin/python Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pytz >>> From bfishbein79 at gmail.com Wed Aug 29 00:30:18 2012 From: bfishbein79 at gmail.com (Benjamin Fishbein) Date: Tue, 28 Aug 2012 17:30:18 -0500 Subject: [Tutor] running more than one python program at the same time Message-ID: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Hello, I wrote a program that I want to have running 24/7. But the problem is that I also want to write and run other programs. I'm using Idle and it won't let me run more than one script at a time. Do you know if there's a way to do this? Or do I need to buy a second computer? Thanks, Ben From marc.tompkins at gmail.com Wed Aug 29 00:49:01 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 28 Aug 2012 15:49:01 -0700 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: On Tue, Aug 28, 2012 at 3:30 PM, Benjamin Fishbein wrote: > Hello, > I wrote a program that I want to have running 24/7. But the problem is > that I also want to write and run other programs. I'm using Idle and it > won't let me run more than one script at a time. Do you know if there's a > way to do this? Or do I need to buy a second computer? > Thanks, > Ben > > IDLE is just an IDE (Integrated Development Environment), meant to improve the convenience and efficiency of writing Python. It is NOT intended to be the primary way you run your Python scripts once you've written them, and it specifically cannot handle multiple scripts executing simultaneously. So the general answer to your question is: only use IDLE for writing and testing your script, not for running it once it's production-ready. Under Windows (which, for my sins, is my primary environment), you can: - open multiple command prompts (Start/Run/CMD) and type "python MyScript.py" in each of them - double-click on the icon for each .py or file, which will launch a copy of Python and execute the script - create a Task or a Service which will run "python MyScript.py" either at boot-up or at a scheduled time - or any of a few other methods. You have similar options in Mac and Linux environments, but I'll leave it to others to enumerate them. -------------- next part -------------- An HTML attachment was scrubbed... URL: From crawlzone at gmail.com Wed Aug 29 00:48:59 2012 From: crawlzone at gmail.com (Ray Jones) Date: Tue, 28 Aug 2012 15:48:59 -0700 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: <503D4ADB.4020709@gmail.com> On 08/28/2012 03:30 PM, Benjamin Fishbein wrote: > Hello, > I wrote a program that I want to have running 24/7. But the problem is that I also want to write and run other programs. I'm using Idle and it won't let me run more than one script at a time. Do you know if there's a way to do this? Or do I need to buy a second computer? > Thanks, > Ben Can you make each script executable and run them without idle? Ray From brian.van.den.broek at gmail.com Wed Aug 29 01:04:14 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Tue, 28 Aug 2012 19:04:14 -0400 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: On 28 Aug 2012 18:33, "Benjamin Fishbein" wrote: > > Hello, > I wrote a program that I want to have running 24/7. But the problem is that I also want to write and run other programs. I'm using Idle and it won't let me run more than one script at a time. Do you know if there's a way to do this? Or do I need to buy a second computer? > Thanks, > Ben Hi Ben, Idle may be useful for developing with (provided you aren't making an app with tkinter) but isn't always the best choice for running one. Do you know how to run python from a command prompt? (If not, post back to the list being sure to tell us your OS and I or someone else will help you.) If you run you 24/7 program that way, idle will be free for you to work. There are other ways to get your program to run in the background, but again these are OS-dependant. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From bfishbein79 at gmail.com Wed Aug 29 01:17:48 2012 From: bfishbein79 at gmail.com (Ben Fishbein) Date: Tue, 28 Aug 2012 18:17:48 -0500 Subject: [Tutor] running more than one python program at the same time In-Reply-To: References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: I'm on a Mac. Using Lion. I just tried opening the terminal and typing "python." And I'm able to open several terminal windows this way. I think this should be able to run many programs simultaneously. Thanks for your help. -Ben On Tue, Aug 28, 2012 at 6:04 PM, Brian van den Broek < brian.van.den.broek at gmail.com> wrote: > > On 28 Aug 2012 18:33, "Benjamin Fishbein" wrote: > > > > Hello, > > I wrote a program that I want to have running 24/7. But the problem is > that I also want to write and run other programs. I'm using Idle and it > won't let me run more than one script at a time. Do you know if there's a > way to do this? Or do I need to buy a second computer? > > Thanks, > > Ben > > Hi Ben, > > Idle may be useful for developing with (provided you aren't making an app > with tkinter) but isn't always the best choice for running one. > > Do you know how to run python from a command prompt? (If not, post back to > the list being sure to tell us your OS and I or someone else will help > you.) If you run you 24/7 program that way, idle will be free for you to > work. There are other ways to get your program to run in the background, > but again these are OS-dependant. > > Best, > > Brian vdB > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bfishbein79 at gmail.com Wed Aug 29 01:19:17 2012 From: bfishbein79 at gmail.com (Ben Fishbein) Date: Tue, 28 Aug 2012 18:19:17 -0500 Subject: [Tutor] running more than one python program at the same time In-Reply-To: References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: Stupid question: how do I run a program from the terminal? I've always just gone to the drop down menu and clicked run to do it in idle. On Tue, Aug 28, 2012 at 6:17 PM, Ben Fishbein wrote: > I'm on a Mac. Using Lion. I just tried opening the terminal and typing > "python." And I'm able to open several terminal windows this way. I think > this should be able to run many programs simultaneously. Thanks for your > help. > -Ben > > > On Tue, Aug 28, 2012 at 6:04 PM, Brian van den Broek < > brian.van.den.broek at gmail.com> wrote: > >> >> On 28 Aug 2012 18:33, "Benjamin Fishbein" wrote: >> > >> > Hello, >> > I wrote a program that I want to have running 24/7. But the problem is >> that I also want to write and run other programs. I'm using Idle and it >> won't let me run more than one script at a time. Do you know if there's a >> way to do this? Or do I need to buy a second computer? >> > Thanks, >> > Ben >> >> Hi Ben, >> >> Idle may be useful for developing with (provided you aren't making an app >> with tkinter) but isn't always the best choice for running one. >> >> Do you know how to run python from a command prompt? (If not, post back >> to the list being sure to tell us your OS and I or someone else will help >> you.) If you run you 24/7 program that way, idle will be free for you to >> work. There are other ways to get your program to run in the background, >> but again these are OS-dependant. >> >> Best, >> >> Brian vdB >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Aug 29 02:09:21 2012 From: d at davea.name (Dave Angel) Date: Tue, 28 Aug 2012 20:09:21 -0400 Subject: [Tutor] running more than one python program at the same time In-Reply-To: References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: <503D5DB1.3070300@davea.name> On 08/28/2012 07:19 PM, Ben Fishbein wrote: > Stupid question: how do I run a program from the terminal? I've always just > gone to the drop down menu and clicked run to do it in idle. > > Haven't you noticed that the correct method of posting on this forum is to put your remarks AFTER the part you're responding to? And deleting the parts you're not responding to? This message was top-posted. Anyway, to run a program from the terminal, you type its name, followed by any arguments it may have. So if you want to run python, type python from the prompt. i think you knew that. Perhaps you're asking how to pass the name of the script to python. Fortunately, it simply expects the path name to the script. So if your script is at ./'mydir/myscript.py, you'd type: Ben at mymachine:~$ python mydir/myscript.py If the script takes arguments, you'd put them after the script name. -- DaveA From steve at pearwood.info Wed Aug 29 04:35:16 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Aug 2012 12:35:16 +1000 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> Message-ID: <503D7FE4.7020702@pearwood.info> On 29/08/12 08:30, Benjamin Fishbein wrote: > Hello, > I wrote a program that I want to have running 24/7. But the problem is > that I also want to write and run other programs. I'm using Idle and >it won't let me run more than one script at a time. Then don't use IDLE. IDLE is for running code interactively, not for long-running programs > Do you know if there's a way to do this? Or do I need to buy a second > computer? Your computer is already running dozens, possible hundreds of programs simultaneously. On my home box, I can see 198 programs currently active. They don't call them "multitasking operating systems" for nothing :) (Actually, "multitasking" is one of those things that people no longer talk about, because it's just assumed that *every* computer does it. This was not always the case -- twenty years ago, the difference between single and multitasking computers was a big deal. Now, probably the only computer you have that doesn't multitask is your microwave oven. Even your iPhone multitasks -- it just won't let apps multitask, but the phone itself does.) You need to run your python script the same way you run any other script for your system: In Windows, you will need something like a batch file or equivalent, which directly runs your script using the Python interpreter. Put this batch file wherever you put other batch files that you want to be run automatically when the system starts up. In Linux, you can set your script as an init.d script to have it automatically run by the operating system. If you're using a GUI desktop like KDE, Trinity, Gnome or similar, it will have something equivalent to a "startup folder" where you put files you want to run when the desktop starts. If you're running a Mac, there will be something similar. If you can't be bothered, or don't want, your system to automatically start up your script, you can run it manually from your system's shell. In Windows, that is the DOS prompt -- either cmd.com or command.exe, I never remember which one is which. In Linux, you start up an xterm or other terminal window. Using KDE, I can do either of these: Start menu > System > Terminal or right-click on the desktop and select "Konsole". Because I use a terminal so often, I have a shortcut permanently in my task bar so I can open a terminal with one click. Other desktops will have something similar. However you do it, you will get a terminal/xterm/console/DOS prompt window, showing a dollar sign prompt: $ That's the shell, waiting for you to give it commands. You can now run your python script. At the prompt, type: python /path/to/my/script.py then press ENTER. If your script needs arguments passed from the command line, put them after the path: python /path/to/my/script.py -z --foo --bar spam ham eggs 23 42 but I'm guessing that if you don't know how to run a script from the command line, it probably doesn't need command line arguments :) -- Steven From akleider at sonic.net Wed Aug 29 05:28:37 2012 From: akleider at sonic.net (akleider at sonic.net) Date: Tue, 28 Aug 2012 20:28:37 -0700 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <503D4ADB.4020709@gmail.com> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> <503D4ADB.4020709@gmail.com> Message-ID: <83cfef3e8d486a01906b83838ca35d13.squirrel@webmail.sonic.net> > On 08/28/2012 03:30 PM, Benjamin Fishbein wrote: >> Hello, >> I wrote a program that I want to have running 24/7. But the problem is >> that I also want to write and run other programs. I'm using Idle and it >> won't let me run more than one script at a time. Do you know if there's >> a way to do this? Or do I need to buy a second computer? >> Thanks, >> Ben > Can you make each script executable and run them without idle? > > > Ray > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > The following works with Linux and might with OSX as well. add "#!/usr/bin/env python" as the first line of your script. Then from the terminal change its permissions: $ chmod 755 /paht/to/my/script/script.py After that you can start your program with: $ /path/to/my/script/script.py If you add "&" to the end of the line it'll go into the background and you'll get your terminal back. I am less confident that the following will work on your Mac but there is probably something equivalent. If you wanted it to run when ever the computer is on, see if there is a file called "/etc/rc.local" If there is, edit it (you'll need root privileges to do so) and add as a last line: "/path/to/my/script/script.py" The words between the slashes will of course have to be modified to suit your situation. From alan.gauld at btinternet.com Wed Aug 29 10:07:32 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Aug 2012 09:07:32 +0100 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <503D5DB1.3070300@davea.name> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> <503D5DB1.3070300@davea.name> Message-ID: On 29/08/12 01:09, Dave Angel wrote: > On 08/28/2012 07:19 PM, Ben Fishbein wrote: >> Stupid question: how do I run a program from the terminal? > > Ben at mymachine:~$ python mydir/myscript.py > > If the script takes arguments, you'd put them after the script name. If it doesn't take arguments you can do this by dragging the script from Finder into the Terminal, it saves some typing... Also note you don't need a Terminal per program. If you end the program line with an ampersand (&) the program will run in the background. You can then start another program in the same Terminal. Of course, if they require user interaction then things get a bit complicated so you're better with multiple Terminals, but if its just background data processing you can run as many programs as you like in a single Terminal. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wrw at mac.com Wed Aug 29 14:27:28 2012 From: wrw at mac.com (William R. Wing (Bill Wing)) Date: Wed, 29 Aug 2012 08:27:28 -0400 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <83cfef3e8d486a01906b83838ca35d13.squirrel@webmail.sonic.net> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> <503D4ADB.4020709@gmail.com> <83cfef3e8d486a01906b83838ca35d13.squirrel@webmail.sonic.net> Message-ID: <2556CF03-41AE-485A-9A2B-DB72A25D1BE6@mac.com> On Aug 28, 2012, at 11:28 PM, akleider at sonic.net wrote: >> On 08/28/2012 03:30 PM, Benjamin Fishbein wrote: >>> Hello, >>> I wrote a program that I want to have running 24/7. But the problem is >>> that I also want to write and run other programs. I'm using Idle and it >>> won't let me run more than one script at a time. Do you know if there's >>> a way to do this? Or do I need to buy a second computer? >>> Thanks, >>> Ben >> Can you make each script executable and run them without idle? >> >> >> Ray >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > The following works with Linux and might with OSX as well. I sent the paragraphs below to Ben yesterday as an off-list message because I figured it wasn't of general enough interest. But, in the hope of sharing knowledge, I'm resending, this time to the list. ---- Ben, On Mac OS-X you have several options. Apple's python (and the python.org copy) both install a minimalist app called PythonLauncher. Assuming it is present (look in your Applications folder, possibly in a Python folder there), simply double-clicking on a .py file in the Finder will launch that script. In general, Mac editors that are python-aware will save python scripts as executable (look for rwx in the owner column of an "ls -l" terminal listing). If the script isn't executable, type chmod +x .py in the terminal window ( is the Return key). Assuming the script is executable, in a terminal window, you can type "python .py and the python interpreter will be told to run the .py file. Next, if the first line in the python file is #!/usr/bin/env python (or if it explicitly references the version of python you are using) then in the terminal, you can type ./.py and the bash shell will invoke python for you. If it is a script that needs to be running all the time, you can turn it into a stand-alone application using py2app or PythonInstaller and place the resulting applications in your list of login items. Google will get you to either of both of those. Finally, if it needs to be run in response to some particular event (like mounting a disk) or at a particular time of day, you can create a launchd plist file and use launchctl to create an entry in the launchd daemon list. (This is a very much more complex subject than this one-liner might imply, and needs to be taken up on a different list.) -Bill From steve at pearwood.info Wed Aug 29 15:57:12 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Aug 2012 23:57:12 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: <503E1FB8.7050901@pearwood.info> On 28/08/12 10:26, Richard D. Moores wrote: > I've been going through Steven D'Aprano's pyprimes > () and have many > questions. One is why begin a function name with an underscore. He has > several functions that do this. Two are: [...] > I'm stealing these for their usefulness. But why the underscores? Single-underscore names are generally the convention for "private" functions, methods, classes, or other names. Here, "private" means: - the function depends on platform-specific features that may not always be available; - the function is experimental, and will become part of the public library some day, but not yet; - the author doesn't want to treat the function as a supported part of the library, but only as an internal detail; - the function is subject to change without notice; - it is an implementation detail, and may become obsolete if the implementation changes; - or all of the above. Think of a single leading underscore as the Python equivalent of those stickers you see on electrical equipment that say "No user-serviceable parts". If you mess with it, you void your warranty. Python will not stop you if you use call a library's private functions from your code, but you can't exactly rely on it. The library author makes no promise (implied or explicit) that single underscore _functions will work the way you expect, or still be there in the next version. There are a few exceptions. For example, the collections.namedtuple type uses single underscore methods as part of its *public* interface, to ensure that the namedtuple methods don't clash with the tuple's named fields. But that's quite unusual. Also, when you do "from module import *", Python will skip any single underscore names. -- Steven From steve at pearwood.info Wed Aug 29 16:13:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Aug 2012 00:13:41 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: <503E2395.2070005@pearwood.info> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > something like: > > def _validate_int(obj): > """Raise an exception if obj is not an integer.""" > m = int(obj + 0) # May raise TypeError. > if obj != m: > raise ValueError('expected an integer but got %r' % obj) > > > is a really awkward way to test if something's an integer, Not really. It's trivially easy: just call _validate_int(x) You don't have to inspect a return result and decide what to do. If it returns, x is an integer. If it doesn't, you get a nice exception: TypeError for non-numeric objects ValueError for non-integer numbers, or float/Decimal NANs and infinities > and checking > types in general is usually a sign of larger flaws in laying out useful > code. That's probably true, but especially for library code, that's not always the case. I'm merely following the usual best-practice of Python code (to the best of my ability), as seen in the standard library: py> import math py> math.factorial(2.5) Traceback (most recent call last): File "", line 1, in ValueError: factorial() only accepts integral values py> math.factorial("2") Traceback (most recent call last): File "", line 1, in TypeError: an integer is required What would you expect factorial to do in these cases, if not raise an exception? Speaking of argument validation, this amuses me: http://thedailywtf.com/Articles/Argument_About_Argument_Validation.aspx -- Steven From marc.tompkins at gmail.com Wed Aug 29 16:24:40 2012 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Wed, 29 Aug 2012 07:24:40 -0700 Subject: [Tutor] running more than one python program at the same time In-Reply-To: <503D7FE4.7020702@pearwood.info> References: <97A9CEDD-F0E7-4E09-909C-73E09AE6247C@gmail.com> <503D7FE4.7020702@pearwood.info> Message-ID: On Tue, Aug 28, 2012 at 7:35 PM, Steven D'Aprano wrote: In Windows, that is the DOS prompt -- either cmd.com or command.exe, I > never remember which one is which. I'm pretty sure that was intentional, but just in case... In MS-DOS/PC-DOS, and in 16-bit versions of Windows (up to Windows 98/Me, in other words), the command interpreter is COMMAND.COM In 32-bit versions of Windows, you can still use the 16-bit interpreter if you want - although it's deprecated, and has been removed entirely in 64-bit Windows - but the native 32-bit command interpreter is CMD.EXE (I used all-caps for emphasis without requiring HTML formatting, but in fact Windows is generally case-insensitive.) -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 29 17:52:20 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Aug 2012 01:52:20 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> Message-ID: <503E3AB4.4050803@pearwood.info> On 28/08/12 19:02, Peter Otten wrote: > Personally, I'm a big fan of ducktyping, so I would probably remove the > check completely and live with the consequences: > >>>> >>> pyprimes._validate_int = lambda x: None >>>> >>> pyprimes.isprime_naive(8.5) > True > > garbage-in, garbage-out -- so what. Duck-typing means that if an object implements the interface for an int, or if it behaves like an int (not quite the same thing!), you can safely treat it as an int. 8.5 is not an int, and isn't a prime number, so why is isprime claiming it is prime? That's a bug -- or rather, it *would* be a bug except you deliberately broke it. There's a strong argument to be made that isprime should accept any number and return False for those which aren't integers, rather than raise an exception. That's reasonable behaviour. But silently returning garbage? That's the worst thing you could do, *far* worse than an unexpected exception or an overly-strict type-check. Errors should occur as close as possible to where they are introduced, and functions shouldn't return garbage -- they should either be correct, or raise an exception. Returning some junk value is bad. Imagine if you accidentally called len(None), and instead of raising, it returned 17. That's how bad it is. I'm reminded of this quote: "I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn?t crash is a horrible nightmare." -- CD Smith -- Steven From steve at pearwood.info Wed Aug 29 18:21:47 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Aug 2012 02:21:47 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> Message-ID: <503E419B.5070200@pearwood.info> On 28/08/12 20:00, Peter Otten wrote: [...] > The differences to _validate_int() are subtle: > >>>> class S(str): > ... def __eq__(self, other): return True > ... def __ne__(self, other): return False > ... def __add__(self, other): return self > ... >>>> vi(S("42")) > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in vi > TypeError >>>> _validate_int(S("42")) >>>> It seems to me that, in some ways at least, S("42") is a string that quacks like an int (if you add it to zero, you get itself), and therefore under duck-typing rules you might be justified in calling it an int. Of course, if you actually try to use it as an int, it fails to walk like an int or swim like an int, so this is a case of "garbage in, garbage out". There is always tension between safety and freedom. A strict type-check will increase safety by preventing many GIGO errors, but it also reduces freedom to use your own WholeNumber type. But freedom to use your own numeric types is also freedom to abuse types like S above. -- Steven From steve at pearwood.info Wed Aug 29 18:36:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Aug 2012 02:36:54 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: Message-ID: <503E4526.2050408@pearwood.info> On 28/08/12 21:24, Wayne Werner wrote: > On Mon, 27 Aug 2012, Richard D. Moores wrote: >> What the best way to test if something's an integer? > > try: > whatever_you_want(supposed_integer) > except ValueError: > print("Oops, that wasn't an integer! Please try again") > > That's usually the best way... Actually, that's close to the worst way, since you take a nice, useful exception which prints a traceback showing exactly what went wrong, and replace it with a pointless, silly message which can't be caught by the caller. Trying again may be impossible, or inappropriate, and certainly isn't up to the function to make that decision, that's up to the caller to decide what is the appropriate response to invalid data. -- Steven From steve at pearwood.info Thu Aug 30 04:31:46 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Aug 2012 12:31:46 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503E2395.2070005@pearwood.info> Message-ID: <503ED092.4000507@pearwood.info> On 30/08/12 05:39, Japhy Bartlett wrote: > If you tried to check in code like this: > > def _validate_int(obj): >>> """Raise an exception if obj is not an integer.""" >>> m = int(obj + 0) # May raise TypeError. >>> if obj != m: >>> raise ValueError('expected an integer but got %r' % obj) >>> >>> > we would pat you on your head and be sure to review all of your work very > closely. That's a very good idea. You might learn something, and become better Python programmers who write robust, correct code. Because the advice you give here in this email makes at least two rookie mistakes. Pay attention now sonny, Grandpa is going to teach you the right way to suck eggs. > If nothing else, it's a _very_ verbose way of checking that > something is an int. If something's meant to be an int, cast it to an int. It doesn't check if something is an int. It checks if it is an integer. Do you understand the difference? Richard does, and he's just learning. Rookie mistake #1: casting to int works on strings, and I don't want to allow strings as valid arguments. This isn't Perl or PHP where strings that look like numbers are implicitly treated as numbers. If you call int on input arguments as a way of telling whether or not you have been passed an int, your code is buggy. > If it's garbage it will throw an error - you don't need to invent your > own messaging. I don't exactly understand what you mean by "invent your own messaging". I'm not inventing any messaging -- I use the stock-standard Python exception mechanism. > If >you're worried about rounding, something like `assert int(x) == x` will do > nicely. Rookie mistake #2: it certainly will not. assert is not guaranteed to run. If you use assert to validate input arguments, your code is buggy because assertions can be disabled at runtime. Your code will then run without input validation. If you intend to validate your inputs, and use assert to do so, your code is buggy. >> That's probably true, but especially for library code, that's not always >> the case. I'm merely following the usual best-practice of Python code (to >> the best of my ability), as seen in the standard library: >> >> > The person asking for help is not writing library code. They're trying to > learn python, and being pedantic about "best-practice" is not particularly > helpful, imho. That is the most ridiculously stupid thing I've seen for a long, long time. What do you think we should be teaching people? Do you think that beginners shouldn't learn how to write good quality, robust, bug-free code? Regardless of whether Richard wants to write library code or not, the code you are trying to sneer at *is* library code. Richard is trying to understand why it does what it does. In four sentences: 1) I check input data so my functions detect bad data up front instead of silently failing with the wrong result. 2) I intend to accept arbitrary numeric types, not just ints, so long as they are integer valued. (Although in hindsight, I'm not sure that's the right decision.) 3) I put that check in a function so that if I need to correct a bug, I only have to correct it in one place, not ten or twenty. 4) And I make it a private function because it is not part of my library's core API, and I want the freedom to remove or replace the function if I find a better or different way to do it. -- Steven From __peter__ at web.de Thu Aug 30 10:44:50 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Aug 2012 10:44:50 +0200 Subject: [Tutor] Why begin a function name with an underscore References: <503C7FA3.3020601@gmail.com> <503E419B.5070200@pearwood.info> Message-ID: Steven D'Aprano wrote: > On 28/08/12 20:00, Peter Otten wrote: > [...] >> The differences to _validate_int() are subtle: >> >>>>> class S(str): >> ... def __eq__(self, other): return True >> ... def __ne__(self, other): return False >> ... def __add__(self, other): return self >> ... >>>>> vi(S("42")) >> Traceback (most recent call last): >> File "", line 1, in >> File "", line 3, in vi >> TypeError >>>>> _validate_int(S("42")) >>>>> > > > It seems to me that, in some ways at least, S("42") is a string > that quacks like an int (if you add it to zero, you get itself), > and therefore under duck-typing rules you might be justified in > calling it an int. > > Of course, if you actually try to use it as an int, it fails to > walk like an int or swim like an int, so this is a case of > "garbage in, garbage out". The class was written to demonstrate that your and my implementation of an integer check may give different results, if only in corner cases with little practical relevance. > There is always tension between safety and freedom. D'accord. I tend to err on the side of freedom. >>> sum(["a", "b", "c"], "") Traceback (most recent call last): File "", line 1, in TypeError: sum() can't sum strings [use ''.join(seq) instead] gives me the creeps even though it'd never occur to me to actually use sum() to join a sequence of strings. > A strict > type-check will increase safety by preventing many GIGO errors, > but it also reduces freedom to use your own WholeNumber type. > But freedom to use your own numeric types is also freedom to > abuse types like S above. From eryksun at gmail.com Thu Aug 30 11:23:09 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 30 Aug 2012 05:23:09 -0400 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> <503E419B.5070200@pearwood.info> Message-ID: On Thu, Aug 30, 2012 at 4:44 AM, Peter Otten <__peter__ at web.de> wrote: > >>>> sum(["a", "b", "c"], "") > Traceback (most recent call last): > File "", line 1, in > TypeError: sum() can't sum strings [use ''.join(seq) instead] > > gives me the creeps even though it'd never occur to me to actually use sum() > to join a sequence of strings. class Start(object): def __add__(self, other): return other >>> sum(['Bypassing', ' nanny', ' typecheck...'], Start()) 'Bypassing nanny typecheck...' FTFY (It goes without saying: never do this.) From jayeola at gmail.com Thu Aug 30 15:30:52 2012 From: jayeola at gmail.com (John Maclean) Date: Thu, 30 Aug 2012 14:30:52 +0100 Subject: [Tutor] understanding pydoc try Message-ID: <503F6B0C.1010809@gmail.com> What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. try_stmt ::= try1_stmt | try2_stmt I can write simple statements as shown below, but I want to actually understand what I am doing. try: import io print("importing io") except ImportError: print("nothing to import") foo = None try: import somefunctionthatdoesnotexist print("importing ...") except ImportError: print("nothing to import") foo = None From mi.janssen at gmail.com Thu Aug 30 16:03:33 2012 From: mi.janssen at gmail.com (=?ISO-8859-1?Q?Michael_Jan=DFen?=) Date: Thu, 30 Aug 2012 16:03:33 +0200 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F6B0C.1010809@gmail.com> References: <503F6B0C.1010809@gmail.com> Message-ID: On 30 August 2012 15:30, John Maclean wrote: > What does the first line from `pydoc try` actually mean? This does not > look like the syntax that one is supposed to use. > > try_stmt ::= try1_stmt | try2_stmt > looks like part of the python language reference. It goes a little further and explains what try1_stmt and try2_stmt actually suppose to mean: http://docs.python.org/reference/compound_stmts.html#the-try-statement try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression [("as" | ",") target]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite Let me try to rephrase it: "a try statement is either of try-except-else-finally or of try-finally form". This notation is used to formally describe language syntax: http://docs.python.org/reference/introduction.html#notation best, Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Aug 30 16:05:27 2012 From: d at davea.name (Dave Angel) Date: Thu, 30 Aug 2012 10:05:27 -0400 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F6B0C.1010809@gmail.com> References: <503F6B0C.1010809@gmail.com> Message-ID: <503F7327.2090506@davea.name> On 08/30/2012 09:30 AM, John Maclean wrote: > What does the first line from `pydoc try` actually mean? This does not > look like the syntax that one is supposed to use. > > try_stmt ::= try1_stmt | try2_stmt > You're looking at the first of three BNF statements. BNF (Backus Naur Form, or something like that) is a way of describing a grammar. i'll quote the whole thing here, and try to explain it. The following is from Python 3.2's pydoc: try_stmt ::= try1_stmt | try2_stmt try1_stmt ::= "try" ":" suite ("except" [expression ["as" target]] ":" suite)+ ["else" ":" suite] ["finally" ":" suite] try2_stmt ::= "try" ":" suite "finally" ":" suite The first statement says that a try_stmt is one or the other of two formats. This simply says there are two syntaxes you can use, depending on what try features you want. The second lists the (most common, i expect) syntax. It has a literal try token, followed by a literal colon token, followed by a suite of statements (that's defined elsewhere, but would include simple statements, if statements, and so on. It wouldn't include def or class, presumably). Then there are one or more except clauses. Note the trailing + which means this element may be repeated, but must be present at least once. Then there is an optional else clause. Then an optional finally clause. These must be present in the specific order stated above. And you can't (for example) have an else without an except, because except is one or more times. The second syntax does not include the except nor else clauses. Is that clearer? -- DaveA From steve at pearwood.info Thu Aug 30 16:09:19 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Aug 2012 00:09:19 +1000 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F6B0C.1010809@gmail.com> References: <503F6B0C.1010809@gmail.com> Message-ID: <503F740F.2040108@pearwood.info> On 30/08/12 23:30, John Maclean wrote: > What does the first line from `pydoc try` actually mean? This does not look like the syntax that one is supposed to use. > > try_stmt ::= try1_stmt | try2_stmt That's a description of the Python grammar in some variation of Backus-Naur Form. In English, it means: A "try statement" is either a "try1 statement" or a "try2 statement". Presumably then there will be a definition of try1_stmt and try2_stmt, also in BNF form, probably in terms of other statements, until eventually the syntax of try statements is fully defined. http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form#Example http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form > I can write simple statements as shown below, but I want to actually understand what I am doing. > > > > try: > import io > print("importing io") > except ImportError: > print("nothing to import") > foo = None This first block attempts to import the "io" module, and then print "importing io". If the import process fails, an exception is raised. But since the io module does exist and can be imported, nothing happens and execution happily goes on to the part *after* the except clause: > try: > import somefunctionthatdoesnotexist > print("importing ...") > except ImportError: > print("nothing to import") > foo = None This time, since "somefunction blah blah" probably doesn't exist, the import process does fail, and an exception is raised, interrupting normal execution of the code. Instead of the next line running, execution of your code halts and Python signals an ImportError. However, then the "except ImportError" line takes over and catches the exception. "nothing to import" is printed and foo is set to None. Does that help? -- Steven From jayeola at gmail.com Thu Aug 30 16:43:31 2012 From: jayeola at gmail.com (John Maclean) Date: Thu, 30 Aug 2012 15:43:31 +0100 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F7327.2090506@davea.name> References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> Message-ID: <503F7C13.1070507@gmail.com> On 08/30/2012 03:05 PM, Dave Angel wrote: > On 08/30/2012 09:30 AM, John Maclean wrote: >> What does the first line from `pydoc try` actually mean? This does not >> look like the syntax that one is supposed to use. >> >> try_stmt ::= try1_stmt | try2_stmt >> > You're looking at the first of three BNF statements. BNF (Backus Naur > Form, or something like that) is a way of describing a grammar. i'll > quote the whole thing here, and try to explain it. > > The following is from Python 3.2's pydoc: > > try_stmt ::= try1_stmt | try2_stmt > try1_stmt ::= "try" ":" suite > ("except" [expression ["as" target]] ":" suite)+ > ["else" ":" suite] > ["finally" ":" suite] > try2_stmt ::= "try" ":" suite > "finally" ":" suite > > The first statement says that a try_stmt is one or the other of two > formats. This simply says there are two syntaxes you can use, depending > on what try features you want. > > The second lists the (most common, i expect) syntax. It has a literal > try token, followed by a literal colon token, followed by a suite of > statements (that's defined elsewhere, but would include simple > statements, if statements, and so on. It wouldn't include def or class, > presumably). > > Then there are one or more except clauses. Note the trailing + which > means this element may be repeated, but must be present at least once. > > Then there is an optional else clause. > > Then an optional finally clause. > > These must be present in the specific order stated above. And you can't > (for example) have an else without an except, because except is one or > more times. > > The second syntax does not include the except nor else clauses. > > Is that clearer? > Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another set TLA that I don't need to know ;-) From d at davea.name Thu Aug 30 17:22:42 2012 From: d at davea.name (Dave Angel) Date: Thu, 30 Aug 2012 11:22:42 -0400 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F7C13.1070507@gmail.com> References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> <503F7C13.1070507@gmail.com> Message-ID: <503F8542.6060508@davea.name> On 08/30/2012 10:43 AM, John Maclean wrote: > On 08/30/2012 03:05 PM, Dave Angel wrote: >> >> > > Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another > set TLA that I don't need to know ;-) > I learned BNF in about 1972. I've used about 35 languages since (not counting hobby ones). It can clarify a new language better than many paragraphs of description. But I've found that it's seldom completely rigorous. -- DaveA From steve at alchemy.com Thu Aug 30 17:26:51 2012 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 30 Aug 2012 08:26:51 -0700 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F8542.6060508@davea.name> References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> <503F7C13.1070507@gmail.com> <503F8542.6060508@davea.name> Message-ID: <503F863B.9050507@alchemy.com> On 30-Aug-12 08:22, Dave Angel wrote: > On 08/30/2012 10:43 AM, John Maclean wrote: >> On 08/30/2012 03:05 PM, Dave Angel wrote: >>> >>> >> >> Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another >> set TLA that I don't need to know ;-) >> > > I learned BNF in about 1972. I've used about 35 languages since (not > counting hobby ones). It can clarify a new language better than many > paragraphs of description. But I've found that it's seldom completely > rigorous. True, usually because people aren't as careful writing it as they are real code that needs to be executed by something. Maybe it would help to start by describing your grammar to YACC, getting it to work, and then expressing that back out as BNF (or just leaving it in YACC code). -- Steve Willoughby / steve at alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C From d at davea.name Thu Aug 30 17:36:17 2012 From: d at davea.name (Dave Angel) Date: Thu, 30 Aug 2012 11:36:17 -0400 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F863B.9050507@alchemy.com> References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> <503F7C13.1070507@gmail.com> <503F8542.6060508@davea.name> <503F863B.9050507@alchemy.com> Message-ID: <503F8871.1070608@davea.name> On 08/30/2012 11:26 AM, Steve Willoughby wrote: > On 30-Aug-12 08:22, Dave Angel wrote: >> On 08/30/2012 10:43 AM, John Maclean wrote: >>> On 08/30/2012 03:05 PM, Dave Angel wrote: >>>> >>>> >>> >>> Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another >>> set TLA that I don't need to know ;-) >>> >> >> I learned BNF in about 1972. I've used about 35 languages since (not >> counting hobby ones). It can clarify a new language better than many >> paragraphs of description. But I've found that it's seldom completely >> rigorous. > > True, usually because people aren't as careful writing it as they are > real code that needs to be executed by something. Maybe it would help > to start by describing your grammar to YACC, getting it to work, and > then expressing that back out as BNF (or just leaving it in YACC code). > > There's another reason, that I usually assumed to be the case. It usually happens at a place where the grammar is particularly tricky, and where the only valid thing to do in BNF is to list lots of cases (as the one in this thread lists two). So I assumed the BNF was more-or-less deliberately dumbed down to make it more legible. I like your explanation better, though. -- DaveA From alan.gauld at btinternet.com Thu Aug 30 18:15:29 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 30 Aug 2012 17:15:29 +0100 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F7C13.1070507@gmail.com> References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> <503F7C13.1070507@gmail.com> Message-ID: On 30/08/12 15:43, John Maclean wrote: > Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another > set TLA that I don't need to know ;-) Actually, BNF is one of those useful skills for any programmer because almost every language is 'formally' described using it - at least since the days of Algol, for which it was invented. A simplified version of it is also used to define most command line tools and their arguments so its definitely worth learning, at least the basics. It can save a lot of typing when you want to precisely specify the allowed grammar in a problem. There are tools which can translate BNF like text into something close to code, which is useful if you ever have to define your own programming language. Admittedly not something most programmers ever need to do, but it does happen occasionally that its the easiest way to solve a problem. (The so-called mini-language design pattern) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From jayeola at gmail.com Thu Aug 30 18:21:44 2012 From: jayeola at gmail.com (John Maclean) Date: Thu, 30 Aug 2012 17:21:44 +0100 Subject: [Tutor] understanding pydoc try In-Reply-To: References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> <503F7C13.1070507@gmail.com> Message-ID: <503F9318.3020601@gmail.com> On 08/30/2012 05:15 PM, Alan Gauld wrote: > On 30/08/12 15:43, John Maclean wrote: > >> Thanks. This is a heck of a lot more clearer to me! BNF, huh? Another >> set TLA that I don't need to know ;-) > > Actually, BNF is one of those useful skills for any programmer because > almost every language is 'formally' described using it - at least > since the days of Algol, for which it was invented. > > A simplified version of it is also used to define most command line > tools and their arguments so its definitely worth learning, at least > the basics. It can save a lot of typing when you want to precisely > specify the allowed grammar in a problem. > > There are tools which can translate BNF like text into something close > to code, which is useful if you ever have to define your own > programming language. Admittedly not something most programmers ever > need to do, but it does happen occasionally that its the easiest way > to solve a problem. (The so-called mini-language design pattern) > > My main issue is that I am a sysadmin and not a programmer. I am aware of pydoc but not of BNF. So I was a bit taken aback when I saw the BNF syntax. It was obvious to me that syntax of the try statements were not python syntax but had no clue how to parse it. BTW - where in pydoc is it mentioned, (or anywhere else for that matter), to refer to BNF? From alan.gauld at btinternet.com Thu Aug 30 18:44:34 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 30 Aug 2012 17:44:34 +0100 Subject: [Tutor] understanding pydoc try In-Reply-To: <503F9318.3020601@gmail.com> References: <503F6B0C.1010809@gmail.com> <503F7327.2090506@davea.name> <503F7C13.1070507@gmail.com> <503F9318.3020601@gmail.com> Message-ID: On 30/08/12 17:21, John Maclean wrote: > My main issue is that I am a sysadmin and not a programmer. I am aware > of pydoc but not of BNF. So I was a bit taken aback when I saw the BNF > syntax. It was obvious to me that syntax of the try statements were not > python syntax but had no clue how to parse it. BTW - where in pydoc is > it mentioned, (or anywhere else for that matter), to refer to BNF? If you are writing (or reading!) code you are a programmer! :-) Michael already gave a link to the notation page on the web site which does explicitly mention BNF but, to be honest it would not be surprising if it didn't. It would be like specifically saying that a web page was written in HTML, nowadays its often just assumed that anyone creating web pages knows about HTML... Similarly languages are usually specified in some approximation of BNF. The link was: http://docs.python.org/reference/introduction.html#notation -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Thu Aug 30 20:07:18 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Aug 2012 20:07:18 +0200 Subject: [Tutor] Why begin a function name with an underscore References: <503C7FA3.3020601@gmail.com> <503E3AB4.4050803@pearwood.info> Message-ID: Steven D'Aprano wrote: > On 28/08/12 19:02, Peter Otten wrote: >> Personally, I'm a big fan of ducktyping, so I would probably remove the >> check completely and live with the consequences: >> >>>>> >>> pyprimes._validate_int = lambda x: None >>>>> >>> pyprimes.isprime_naive(8.5) >> True >> >> garbage-in, garbage-out -- so what. > > > Duck-typing means that if an object implements the interface for an int, > or if it behaves like an int (not quite the same thing!), you can safely > treat it as an int. > > 8.5 is not an int, and isn't a prime number, so why is isprime claiming > it is prime? That's a bug -- or rather, it *would* be a bug except you > deliberately broke it. No, I demonstrated the limitations of an alternate implementation, one that I prefer in spite of these limitations. > There's a strong argument to be made that isprime should accept any > number and return False for those which aren't integers, rather than > raise an exception. That's reasonable behaviour. > > But silently returning garbage? That's the worst thing you could do, *far* > worse than an unexpected exception or an overly-strict type-check. Allowing floats for a primality test is a can of worms anyway. You will inevitably run out of significant digits: >>> from pyprimes import isprime_naive as isprime >>> 2**61-1 2305843009213693951 >>> isprime(2305843009213693951.0) False >>> isprime(2305843009213693951) True >>> int(2305843009213693951.0) 2305843009213693952 > Errors should occur as close as possible to where they are introduced, > and functions shouldn't return garbage -- they should either be correct, > or raise an exception. Returning some junk value is bad. Imagine if you > accidentally called len(None), and instead of raising, it returned 17. > That's how bad it is. > > > I'm reminded of this quote: > > > "I find it amusing when novice programmers believe their main job is > preventing programs from crashing. ... More experienced programmers > realize that correct code is great, code that crashes could use > improvement, but incorrect code that doesn?t crash is a horrible > nightmare." -- CD Smith I think it depends on what you expect from a function. If you want it to execute a particular algorithm it is OK not to impose limitations on the input. As a building block for an actual application I'd go with a whitelist and require a numbers.Integral or even int (assuming Python 3) instance. From steve at pearwood.info Thu Aug 30 21:49:53 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Aug 2012 05:49:53 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> <503E3AB4.4050803@pearwood.info> Message-ID: <503FC3E1.4080101@pearwood.info> On 31/08/12 04:07, Peter Otten wrote: > Allowing floats for a primality test is a can of worms anyway. You will > inevitably run out of significant digits: [snip] Yes, I had more or less come to the same conclusion earlier. The problem is that although sufficiently large floats are all integer-valued, they're not necessarily the integer value that you think: py> 1e100 == 10**100 False [...] >> "I find it amusing when novice programmers believe their main job is >> preventing programs from crashing. ... More experienced programmers >> realize that correct code is great, code that crashes could use >> improvement, but incorrect code that doesn?t crash is a horrible >> nightmare." -- CD Smith > > I think it depends on what you expect from a function. If you want it to > execute a particular algorithm it is OK not to impose limitations on the > input. This makes no sense to me. Algorithms depend on valid input, both in real life and in computing. If you accept invalid input, you're not executing the algorithm that you want, you're executing a *different* algorithm. The algorithm for "make a soft boiled egg" assumes you have an egg, not a brick, and further that it is a chicken egg, not an ostrich egg or a flea's egg. The algorithm for "sort a list" requires a list, not a dict. If by some fluke your code runs all the way to the end when you call it with a dict argument, you haven't *sorted a list*. You've just wasted CPU cycles for no good reason. The algorithm for "is this a prime number?" depends on the argument being a positive, integer-valued number. Accepting any garbage input and just *hoping* that the function will eventually raise an *appropriate* exception doesn't seem very wise to me. You can boil that ostrich egg for three minutes too, but it won't taste very nice when you go to eat it. > As a building block for an actual application I'd go with a whitelist > and require a numbers.Integral or even int (assuming Python 3) instance. I am supporting Python 2.5 onwards, so I can't use numbers.Integral. -- Steven From eryksun at gmail.com Thu Aug 30 22:57:31 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 30 Aug 2012 16:57:31 -0400 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> <503E3AB4.4050803@pearwood.info> Message-ID: On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten <__peter__ at web.de> wrote: > > Allowing floats for a primality test is a can of worms anyway. You will > inevitably run out of significant digits: Allowing floats can also lead to type errors for operations that require an integral type, but at least they're easier to catch with proper testing. For example, line 323 will raise a TypeError if n is a float: http://code.google.com/p/pyprimes/source/browse/src/pyprimes.py#323 1.0 == 1, but range(1.0) is not allowed and neither is [0,1][1.0]. From abhishek.vit at gmail.com Fri Aug 31 00:19:50 2012 From: abhishek.vit at gmail.com (Abhishek Pratap) Date: Thu, 30 Aug 2012 15:19:50 -0700 Subject: [Tutor] using multiprocessing efficiently to process large data file Message-ID: Hi Guys I have a with few million lines. I want to process each block of 8 lines and from my estimate my job is not IO bound. In other words it takes a lot more time to do the computation than it would take for simply reading the file. I am wondering how can I go about reading data from this at a faster pace and then farm out the jobs to worker function using multiprocessing module. I can think of two ways. 1. split the split and read it in parallel(dint work well for me ) primarily because I dont know how to read a file in parallel efficiently. 2. keep reading the file sequentially into a buffer of some size and farm out a chunks of the data through multiprocessing. Any example would be of great help. Thanks! -Abhi From ramit.prasad at jpmorgan.com Fri Aug 31 00:35:07 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 30 Aug 2012 22:35:07 +0000 Subject: [Tutor] using multiprocessing efficiently to process large data file In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741663B98F@SCACMX008.exchad.jpmchase.net> > I have a with few million lines. I want to process each block of 8 > lines and from my estimate my job is not IO bound. In other words it > takes a lot more time to do the computation than it would take for > simply reading the file. > > I am wondering how can I go about reading data from this at a faster > pace and then farm out the jobs to worker function using > multiprocessing module. > > I can think of two ways. > > 1. split the split and read it in parallel(dint work well for me ) > primarily because I dont know how to read a file in parallel > efficiently. > 2. keep reading the file sequentially into a buffer of some size and > farm out a chunks of the data through multiprocessing. > > Any example would be of great help. > The general logic should work, but did not test with a real file. with open( file, 'r' ) as f: data = f.readlines() iterdata = iter(data ) grouped_data =[] for d in iterdata: l = [d, next(iterdata)] # make this list 8 elements instead grouped_data.append( l ) # batch_process on grouped data Theoretically you might be able to call next() directly on the file without doing readlines(). Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From alan.gauld at btinternet.com Fri Aug 31 01:49:19 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Aug 2012 00:49:19 +0100 Subject: [Tutor] using multiprocessing efficiently to process large data file In-Reply-To: References: Message-ID: On 30/08/12 23:19, Abhishek Pratap wrote: > I am wondering how can I go about reading data from this at a faster > pace and then farm out the jobs to worker function using > multiprocessing module. > > I can think of two ways. > > 1. split the split and read it in parallel(dint work well for me ) > primarily because I dont know how to read a file in parallel > efficiently. Can you show us what you tried? It's always easier to give an answer to a concrete example than to a hypethetical scenario. > 2. keep reading the file sequentially into a buffer of some size and > farm out a chunks of the data through multiprocessing. This is the model I've used. In pseudo code for line, data in enumerate(file): while line % chunksize: chunk.append(data) launch_subprocess(chunk) I'd tend to go for big chunks - if you have a million lines in your file I'd pick a chunksize of around 10,000-100,000 lines. If you go too small the overhead of starting the subprocess will swamp any gains you get. Also remember the constraints of how many actual CPUs/Cores you have. Too many tasks spread over too few CPUs will just cause more swapping. Any less than 4 cores is probably not worth the effort. Just maximise the efficiency of your algorithm - which is probably worth doing first anyway. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From afowler2 at broncos.uncfsu.edu Fri Aug 31 02:15:41 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Fri, 31 Aug 2012 00:15:41 +0000 Subject: [Tutor] Printing a list as a column Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561F37C8BD@BL2PRD0710MB363.namprd07.prod.outlook.com> Does anyone know how to print a list in a form of a column instead of a row? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfjennings at gmail.com Fri Aug 31 02:41:02 2012 From: dfjennings at gmail.com (Don Jennings) Date: Thu, 30 Aug 2012 20:41:02 -0400 Subject: [Tutor] Tutor Digest, Vol 102, Issue 98 In-Reply-To: References: Message-ID: <49870031-CAF9-4D91-9E15-D8ECF11FEFC9@gmail.com> On Aug 30, 2012, at 8:15 PM, tutor-request at python.org wrote: > Message: 6 > Date: Fri, 31 Aug 2012 00:15:41 +0000 > From: Ashley Fowler > To: "tutor at python.org" > Subject: [Tutor] Printing a list as a column > Message-ID: > <6962C976AE76AC4298CBF6FD6D0C63561F37C8BD at BL2PRD0710MB363.namprd07.prod.outlook.com> > > Content-Type: text/plain; charset="iso-8859-1" > > Does anyone know how to print a list in a form of a column instead of a row? Please give an example of what you mean here. For example, you might want to see: ['a', 'b', 'c', 'd', 'e'] My guess is that what you want is for the list ['a', 'b', 'c', 'd', 'e'] to print out as: a b c d e Yes? Are you familiar with the join method of strings? Try it out. If you have trouble, let us know :>) Take care, Don From breamoreboy at yahoo.co.uk Fri Aug 31 02:44:47 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 01:44:47 +0100 Subject: [Tutor] Printing a list as a column In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37C8BD@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37C8BD@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/2012 01:15, Ashley Fowler wrote: > Does anyone know how to print a list in a form of a column instead of a row? > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > This simple? >>> mylist=range(5) >>> for x in mylist: ... print x ... 0 1 2 3 4 -- Cheers. Mark Lawrence. From afowler2 at broncos.uncfsu.edu Fri Aug 31 03:12:37 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Fri, 31 Aug 2012 01:12:37 +0000 Subject: [Tutor] Printing list in a Column Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561F37C908@BL2PRD0710MB363.namprd07.prod.outlook.com> Can anyone help me edit this code below to return the list in the form of a column instead of a row? def printList(): list1 = input("Insert a list") list = [list1] print (list) -------------- next part -------------- An HTML attachment was scrubbed... URL: From afowler2 at broncos.uncfsu.edu Fri Aug 31 01:32:30 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Thu, 30 Aug 2012 23:32:30 +0000 Subject: [Tutor] Scheme Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)?(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the "pi" constant. Be sure to use "cube" from problem (1) to find the cube of r (r^3). Tests: (sphere 2) ==> 33.51 ... (sphere 5.3) ==> 623.61 ... -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Aug 31 03:24:58 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Aug 2012 11:24:58 +1000 Subject: [Tutor] Scheme In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: <5040126A.2040800@pearwood.info> On 31/08/12 09:32, Ashley Fowler wrote: > This is a problem using the Scheme programming...Can anybody help me with this problem? Probably, but not likely to be anyone here. Have you considered asking on a Lisp or Scheme mailing list or discussion forum? -- Steven From breamoreboy at yahoo.co.uk Fri Aug 31 03:28:49 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 02:28:49 +0100 Subject: [Tutor] Printing list in a Column In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37C908@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37C908@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/2012 02:12, Ashley Fowler wrote: > Can anyone help me edit this code below to return the list in the form of a column instead of a row? > > > > > > def printList(): > list1 = input("Insert a list") > list = [list1] > print (list) > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Use one of the two answers that you got roughly half an hour before you posted this? -- Cheers. Mark Lawrence. From steve at pearwood.info Fri Aug 31 03:27:53 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Aug 2012 11:27:53 +1000 Subject: [Tutor] Why begin a function name with an underscore In-Reply-To: References: <503C7FA3.3020601@gmail.com> <503E3AB4.4050803@pearwood.info> Message-ID: <50401319.8090905@pearwood.info> On 31/08/12 06:57, eryksun wrote: > On Thu, Aug 30, 2012 at 2:07 PM, Peter Otten<__peter__ at web.de> wrote: >> >> Allowing floats for a primality test is a can of worms anyway. You will >> inevitably run out of significant digits: > > Allowing floats can also lead to type errors for operations that > require an integral type, but at least they're easier to catch with > proper testing. For example, line 323 will raise a TypeError if n is a > float: Good catch, thank you. And that's why the module is still in alpha. -- Steven From swordangel at gmail.com Fri Aug 31 03:32:41 2012 From: swordangel at gmail.com (Kal Sze) Date: Fri, 31 Aug 2012 09:32:41 +0800 Subject: [Tutor] Scheme In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: And this looks like a homework problem, too. It is against etiquette to just ask for the solution to homework on ANY forum, message board, or mailing list. Since it's been given to you as homework, you're supposed to give it enough thoughts, and (hopefully) come up with your solution. Even when you go to the Lisp or Scheme mailing list, you should at least show what you have tried, paste your own code, and tell them where you are stuck. On 31 August 2012 07:32, Ashley Fowler wrote: > This is a problem using the Scheme programming...Can anybody help me > with this problem? > > 2. Write a procedure (sphere r) that takes the radius of a sphere > as the value of its input parameter and returns the volume of that > sphere given by the formula: (4/3)?(r^3). Use (require scheme/math) > or (require racket/math) to load the math library containing the > "pi" constant. > Be sure to use "cube" from problem (1) to find the cube of r (r^3). > > Tests: > (sphere 2) ==> 33.51 ... > (sphere 5.3) ==> 623.61 ... > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri Aug 31 03:32:43 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 02:32:43 +0100 Subject: [Tutor] Scheme In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/2012 00:32, Ashley Fowler wrote: > This is a problem using the Scheme programming...Can anybody help me with this problem? > > > 2. Write a procedure (sphere r) that takes the radius of a sphere > as the value of its input parameter and returns the volume of that > sphere given by the formula: (4/3)?(r^3). Use (require scheme/math) > or (require racket/math) to load the math library containing the > "pi" constant. > Be sure to use "cube" from problem (1) to find the cube of r (r^3). > > Tests: > (sphere 2) ==> 33.51 ... > (sphere 5.3) ==> 623.61 ... > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Have I got this right? You're asking a question about the Scheme programming language on a Python mailing list, yes? -- Cheers. Mark Lawrence. From afowler2 at broncos.uncfsu.edu Fri Aug 31 03:35:23 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Fri, 31 Aug 2012 01:35:23 +0000 Subject: [Tutor] Scheme In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com>, Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561F37C936@BL2PRD0710MB363.namprd07.prod.outlook.com> Understood. Disregard this problem. ________________________________ From: tutor-bounces+afowler2=broncos.uncfsu.edu at python.org [tutor-bounces+afowler2=broncos.uncfsu.edu at python.org] on behalf of Kal Sze [swordangel at gmail.com] Sent: Friday, August 31, 2012 1:32 AM To: tutor at python.org Subject: Re: [Tutor] Scheme And this looks like a homework problem, too. It is against etiquette to just ask for the solution to homework on ANY forum, message board, or mailing list. Since it's been given to you as homework, you're supposed to give it enough thoughts, and (hopefully) come up with your solution. Even when you go to the Lisp or Scheme mailing list, you should at least show what you have tried, paste your own code, and tell them where you are stuck. On 31 August 2012 07:32, Ashley Fowler > wrote: This is a problem using the Scheme programming...Can anybody help me with this problem? 2. Write a procedure (sphere r) that takes the radius of a sphere as the value of its input parameter and returns the volume of that sphere given by the formula: (4/3)?(r^3). Use (require scheme/math) or (require racket/math) to load the math library containing the "pi" constant. Be sure to use "cube" from problem (1) to find the cube of r (r^3). Tests: (sphere 2) ==> 33.51 ... (sphere 5.3) ==> 623.61 ... _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From afowler2 at broncos.uncfsu.edu Fri Aug 31 03:49:06 2012 From: afowler2 at broncos.uncfsu.edu (Ashley Fowler) Date: Fri, 31 Aug 2012 01:49:06 +0000 Subject: [Tutor] Scheme In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com>, Message-ID: <6962C976AE76AC4298CBF6FD6D0C63561F37C948@BL2PRD0710MB363.namprd07.prod.outlook.com> yes ________________________________________ From: tutor-bounces+afowler2=broncos.uncfsu.edu at python.org [tutor-bounces+afowler2=broncos.uncfsu.edu at python.org] on behalf of Mark Lawrence [breamoreboy at yahoo.co.uk] Sent: Friday, August 31, 2012 1:32 AM To: tutor at python.org Subject: Re: [Tutor] Scheme On 31/08/2012 00:32, Ashley Fowler wrote: > This is a problem using the Scheme programming...Can anybody help me with this problem? > > > 2. Write a procedure (sphere r) that takes the radius of a sphere > as the value of its input parameter and returns the volume of that > sphere given by the formula: (4/3)?(r^3). Use (require scheme/math) > or (require racket/math) to load the math library containing the > "pi" constant. > Be sure to use "cube" from problem (1) to find the cube of r (r^3). > > Tests: > (sphere 2) ==> 33.51 ... > (sphere 5.3) ==> 623.61 ... > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Have I got this right? You're asking a question about the Scheme programming language on a Python mailing list, yes? -- Cheers. Mark Lawrence. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From etanes.rm at gmail.com Fri Aug 31 05:39:53 2012 From: etanes.rm at gmail.com (Scurvy Scott) Date: Thu, 30 Aug 2012 20:39:53 -0700 Subject: [Tutor] Lambda?? Whaaaaat? Message-ID: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> I'm fairly new to python having recently completed LPTHW. While randomly reading stack overflow I've run into "lambda" but haven't seen an explanation of what that is, how it works, etc. Would anyone care to point me in the right direction? Thanks in advance Scott From dwightdhutto at gmail.com Fri Aug 31 06:20:56 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Fri, 31 Aug 2012 00:20:56 -0400 Subject: [Tutor] Lambda?? Whaaaaat? In-Reply-To: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> References: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> Message-ID: Hey Scott, Always refer to google and the python docs first. from http://docs.python.org/tutorial/controlflow.html#lambda-forms 4.7.5. Lambda Forms By popular demand, a few features commonly found in functional programming languages like Lisp have been added to Python. With the lambdakeyword, small anonymous functions can be created. Here?s a function that returns the sum of its two arguments: lambda a, b: a+b. Lambda forms can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda forms can reference variables from the containing scope: >>> >>> def make_incrementor(n):... return lambda x: x + n...>>> f = make_incrementor(42)>>> f(0)42>>> f(1)43 and also, this looks interesting as well: http://www.diveintopython.net/power_of_introspection/lambda_functions.html I haven't used lambdas but a few times, so a little google research can go a long way. Sincerely, David Hutto http://hitwebdevelopment.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Fri Aug 31 06:23:46 2012 From: d at davea.name (Dave Angel) Date: Fri, 31 Aug 2012 00:23:46 -0400 Subject: [Tutor] Lambda?? Whaaaaat? In-Reply-To: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> References: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> Message-ID: <50403C52.1030905@davea.name> On 08/30/2012 11:39 PM, Scurvy Scott wrote: > I'm fairly new to python having recently completed LPTHW. While randomly reading stack overflow I've run into "lambda" but haven't seen an explanation of what that is, how it works, etc. > Would anyone care to point me in the right direction? lambda is an alternative syntax for defining a function. However, the function has no name, and can be defined in the middle of another expression. The main constraint is that a lambda function can consist only of a single expression. No statements, no if's, no loops. It's main usefulness is for callbacks, for example for a sort operation, or gui event handlers. if you had a function that returned the square of its argument, you might define it as follows: def square(x): return x*x You could accomplish the same thing by the lambda function: square = lambda x : x*x Here we create a function with lambda, then bind it to the name square. No benefit, but it shows the syntax. More interesting is if we want to sort a list of two-tuples, using the 3rd element of each as our sort key. mylist.sort(key=lambda x : x[2]) There are other ways to accomplish that (and I think there's a standard library function for it), but maybe it shows you what it could be used for. The function is generated, the sort happens, and the function is discarded. (all code untested) -- DaveA From wrw at mac.com Fri Aug 31 05:27:24 2012 From: wrw at mac.com (William R. Wing (Bill Wing)) Date: Thu, 30 Aug 2012 23:27:24 -0400 Subject: [Tutor] Tutor Digest, Vol 102, Issue 98 In-Reply-To: <49870031-CAF9-4D91-9E15-D8ECF11FEFC9@gmail.com> References: <49870031-CAF9-4D91-9E15-D8ECF11FEFC9@gmail.com> Message-ID: > On Aug 30, 2012, at 8:15 PM, tutor-request at python.org wrote: > >> Message: 6 >> Date: Fri, 31 Aug 2012 00:15:41 +0000 >> From: Ashley Fowler >> To: "tutor at python.org" >> Subject: [Tutor] Printing a list as a column >> Message-ID: >> <6962C976AE76AC4298CBF6FD6D0C63561F37C8BD at BL2PRD0710MB363.namprd07.prod.outlook.com> >> >> Content-Type: text/plain; charset="iso-8859-1" >> >> Does anyone know how to print a list in a form of a column instead of a row? > How about - >>> for item in iter(list): >>>?.print item -Bill From steve at pearwood.info Fri Aug 31 06:43:08 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Aug 2012 14:43:08 +1000 Subject: [Tutor] Lambda?? Whaaaaat? In-Reply-To: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> References: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> Message-ID: <504040DC.7050607@pearwood.info> On 31/08/12 13:39, Scurvy Scott wrote: > I'm fairly new to python having recently completed LPTHW. While >randomly reading stack overflow I've run into "lambda" but >haven't seen an explanation of what that is, how it works, etc. > Would anyone care to point me in the right direction? Lambda is just a short-cut for making a function. Instead of writing a function like this: def func(a, b=2): return a*b - 1 you can do this instead: lambda a, b=2: a*b - 1 Note that you don't use the "return" keyword in lambdas. There are two limitations on functions you create with lambda: * they don't have names -- they are "anonymous functions" (this is both a strength and a weakness) * you are limited to a single expression in the function body, so big complicated functions can't be (easily, or at all) be created with lambda Other than that, they are ordinary functions no different from those you create using def. Why would you use lambda instead of def? Frankly, normally you wouldn't, but there are a few situations where you might. One of the most common is using "callback functions" for GUI frameworks or similar. Here's another example, easier to show. Suppose you want to sort a list of movie titles, but skipping leading "The". movies = ["Star Wars", "The Lord of the Rings", "Zoolander", "The Last Supper", "True Lies", "Watchmen", "Con Air", "The Frighteners", "The Long Kiss Goodnight", "The Avengers"] from pprint import pprint movies.sort(key=lambda title: title.replace("The ", "")) pprint(movies) Without lambda, you would have to do this: def key_function(title): # I can't think of a better name return title.replace("The ", "") movies.sort(key=key_function) which is a bit of a waste if you only use key_function once and never again, or if you have many different key functions. (Note that, strictly speaking, my key function above is not quite right. Here is a better one: lambda title: title[4:] if title.startswith("The ") else title but that's a bit harder to understand. So the strength of lambda is that it is an expression, not a statement, and can be embedded directly where you want it. Here's a list of functions: list_of_functions = [ lambda s: s.upper() + "!!", lambda s: "??" + s.lower(), lambda s: "?" + s.title() + "!", ] for func in list_of_functions: print(func("Hello world")) Where does the name come from? Lambda is the Greek letter L, and for reasons I don't know, it is the traditional name used for functions in some of the more abstract areas of computer science. From computer science the name became well known in programming languages like Lisp, and from there to Python. -- Steven From dwightdhutto at gmail.com Fri Aug 31 07:50:42 2012 From: dwightdhutto at gmail.com (Dwight Hutto) Date: Fri, 31 Aug 2012 01:50:42 -0400 Subject: [Tutor] Tutor Digest, Vol 102, Issue 98 In-Reply-To: References: <49870031-CAF9-4D91-9E15-D8ECF11FEFC9@gmail.com> Message-ID: I think the OP is about to get the point, that there are usually several workarounds that people have found on their way to designing a function, or using built-ins(not that we don't use built-ins to create those workarounds). But you have to always remember to reference the docs, google. the help() function, then the list, unless it's something urgent, like a client needing a function added quickly, and you haven't worked with python in that particular area yet. Best Regards, David Hutto, http://hitwebdevelopment.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Aug 31 09:48:26 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Aug 2012 08:48:26 +0100 Subject: [Tutor] Lambda?? Whaaaaat? In-Reply-To: <504040DC.7050607@pearwood.info> References: <80F1502B-76DC-4E0C-815E-D2058E6500C9@gmail.com> <504040DC.7050607@pearwood.info> Message-ID: On 31/08/12 05:43, Steven D'Aprano wrote: > Where does the name come from? Lambda is the Greek letter L, > and for reasons I don't know, it is the traditional name used > for functions in some of the more abstract areas of computer > science. More specifically it is the name of a branch of mathematics called Lambda Calculus which is the theoretical underpinning of a lot of computer science. It predates the study of computing (in the modern mechanised sense) in the same way as Boolean logic predates modern computing, but both are essential theoretical building blocks. Python's lambda feature is therefore useful in formal teaching scenarios, which was an important aspect of Python as originally perceived. Python grew out of several other teaching languages and a lot of the early features were explicitly added to assist in teaching computing theory to students. Google Lambda Calculus for more but be prepared to have your mind boggled. It's not an intuitive branch of math! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Aug 31 09:55:04 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Aug 2012 08:55:04 +0100 Subject: [Tutor] Scheme In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/12 00:32, Ashley Fowler wrote: > This is a problem using the Scheme programming...Can anybody help me > with this problem? > > 2. Write a procedure (sphere r) that takes the radius of a sphere > as the value of its input parameter and returns the volume of that > sphere given by the formula: (4/3)?(r^3). Use (require scheme/math) > or (require racket/math) to load the math library containing the > "pi" constant. from math import pi def sphere(r): return (4.0/3) * math.pi *(r**3) > Be sure to use "cube" from problem (1) to find the cube of r (r^3). Sorry I don't have access to problem 1 but luckily in Python I don't need it! Now just translate that into Scheme :-) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Aug 31 09:58:50 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Aug 2012 08:58:50 +0100 Subject: [Tutor] Printing list in a Column In-Reply-To: <6962C976AE76AC4298CBF6FD6D0C63561F37C908@BL2PRD0710MB363.namprd07.prod.outlook.com> References: <6962C976AE76AC4298CBF6FD6D0C63561F37C908@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/12 02:12, Ashley Fowler wrote: > Can anyone help me edit this code below to return the list in the form > of a column instead of a row? > > def printList(): > list1 = input("Insert a list") > list = [list1] > print (list) First you need to convert the string that your user types into a list of individual entries. At the moment your list consists of a single string so the column will only have a single line. You probably want to look at the split() method of strings. Alternatively you could write a loop which reads each entry individually from the user. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From breamoreboy at yahoo.co.uk Fri Aug 31 10:31:44 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 09:31:44 +0100 Subject: [Tutor] Tutor Digest, Vol 102, Issue 98 In-Reply-To: References: <49870031-CAF9-4D91-9E15-D8ECF11FEFC9@gmail.com> Message-ID: On 31/08/2012 04:27, William R. Wing (Bill Wing) wrote: > > How about - > >>>> for item in iter(list): >>>> ?.print item Overengineering? :) A list is an iterator. > > -Bill > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Cheers. Mark Lawrence. From bala.biophysics at gmail.com Fri Aug 31 10:37:04 2012 From: bala.biophysics at gmail.com (Bala subramanian) Date: Fri, 31 Aug 2012 10:37:04 +0200 Subject: [Tutor] checking input parameters Message-ID: Friends, I use the following way to check for the input parameters. I would like to know if there is a any better way to show or describe the script usage. So when the user just runs it without any input params., the program shd not execute but just shows the documentation. from sys import argv if not argv[1:]: print 'Program usage: python script file-list1 file-list2' print 'file list should contain the names of the atom list' -------- -------- Thanks, Bala -- C. Balasubramanian From breamoreboy at yahoo.co.uk Fri Aug 31 10:36:20 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 09:36:20 +0100 Subject: [Tutor] Scheme In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/2012 08:55, Alan Gauld wrote: > > Now just translate that into Scheme :-) > > HTH > Anyone know of an application to automate Python to Scheme translation? :) -- Cheers. Mark Lawrence. From swordangel at gmail.com Fri Aug 31 10:44:43 2012 From: swordangel at gmail.com (Kal Sze) Date: Fri, 31 Aug 2012 16:44:43 +0800 Subject: [Tutor] Scheme In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: That's left as an exercise to the reader. On Friday, August 31, 2012, Mark Lawrence wrote: > On 31/08/2012 08:55, Alan Gauld wrote: > >> >> Now just translate that into Scheme :-) >> >> HTH >> >> > Anyone know of an application to automate Python to Scheme translation? :) > > -- > Cheers. > > Mark Lawrence. > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Aug 31 10:58:36 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 31 Aug 2012 10:58:36 +0200 Subject: [Tutor] checking input parameters References: Message-ID: Bala subramanian wrote: > I use the following way to check for the input parameters. I would > like to know if there is a any better way to show or describe the > script usage. So when the user just runs it without any input params., > the program shd not execute but just shows the documentation. > > from sys import argv > if not argv[1:]: > print 'Program usage: python script file-list1 file-list2' > print 'file list should contain the names of the atom list' > -------- > -------- I may look like a lot of overhead at first, but in the long run it will pay off if you use argparse: $ cat argparse_demo.py def main(): import argparse parser = argparse.ArgumentParser( description="what the script is supposed to do") parser.add_argument( "file_list1", metavar="file-list1", help="your help text for file-list1") parser.add_argument( "file_list2", metavar="file-list2", help="your help text for file-list2") args = parser.parse_args() print "Do something with {!r} and {!r}".format( args.file_list1, args.file_list2) if __name__ == "__main__": main() $ python argparse_demo.py usage: argparse_demo.py [-h] file-list1 file-list2 argparse_demo.py: error: too few arguments $ python argparse_demo.py -h usage: argparse_demo.py [-h] file-list1 file-list2 what the script is supposed to do positional arguments: file-list1 your help text for file-list1 file-list2 your help text for file-list2 optional arguments: -h, --help show this help message and exit $ python argparse_demo.py alpha.txt beta.txt Do something with 'alpha.txt' and 'beta.txt' For more see the documentation at http://docs.python.org/howto/argparse.html#id1 From breamoreboy at yahoo.co.uk Fri Aug 31 11:13:15 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 10:13:15 +0100 Subject: [Tutor] Scheme In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: On 31/08/2012 09:44, Kal Sze wrote: > That's left as an exercise to the reader. > > On Friday, August 31, 2012, Mark Lawrence wrote: > >> On 31/08/2012 08:55, Alan Gauld wrote: >> >>> >>> Now just translate that into Scheme :-) >>> >>> HTH >>> >>> >> Anyone know of an application to automate Python to Scheme translation? :) >> >> -- >> Cheers. >> >> Mark Lawrence. >> >> ______________________________**_________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/**mailman/listinfo/tutor >> > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Please don't top post. -- Cheers. Mark Lawrence. From steve at pearwood.info Fri Aug 31 13:51:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Aug 2012 21:51:41 +1000 Subject: [Tutor] Scheme In-Reply-To: References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> Message-ID: <5040A54D.7060400@pearwood.info> On 31/08/12 19:13, Mark Lawrence wrote: [...] > Please don't top post. And Mark, please trim your replies. Bottom posting without trimming is just as annoying as top posting without trimming. (Among other things, your post ended up containing THREE copies of the mailing list footer.) -- Steven From breamoreboy at yahoo.co.uk Fri Aug 31 14:35:53 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Aug 2012 13:35:53 +0100 Subject: [Tutor] Scheme In-Reply-To: <5040A54D.7060400@pearwood.info> References: <6962C976AE76AC4298CBF6FD6D0C63561F37B89E@BL2PRD0710MB363.namprd07.prod.outlook.com> <5040A54D.7060400@pearwood.info> Message-ID: On 31/08/2012 12:51, Steven D'Aprano wrote: > On 31/08/12 19:13, Mark Lawrence wrote: > [...] >> Please don't top post. > > And Mark, please trim your replies. Bottom posting without trimming is > just as annoying as top posting without trimming. > > (Among other things, your post ended up containing THREE copies of the > mailing list footer.) > My apologies but I'd swear blind that I'd done just that. Who's been playing with the time machine? :) -- Cheers. Mark Lawrence. From ramit.prasad at jpmorgan.com Fri Aug 31 15:41:38 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 31 Aug 2012 13:41:38 +0000 Subject: [Tutor] using multiprocessing efficiently to process large data file In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741663B98F@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741663C948@SCACMX008.exchad.jpmchase.net> Please always respond to the list. And avoid top posting. > -----Original Message----- > From: Abhishek Pratap [mailto:abhishek.vit at gmail.com] > Sent: Thursday, August 30, 2012 5:47 PM > To: Prasad, Ramit > Subject: Re: [Tutor] using multiprocessing efficiently to process large data > file > > Hi Ramit > > Thanks for your quick reply. Unfortunately given the size of the file > I cant afford to load it all into memory at one go. > I could read, lets say first 1 million lines process them in parallel > and so on. I am looking for some example which does something similar. > > -Abhi > The same logic should work just process your batch after checking size and iterate over the file directly instead of reading in memory. with open( file, 'r' ) as f: iterdata = iter(f) grouped_data =[] for d in iterdata: l = [d, next(iterdata)] # make this list 8 elements instead grouped_data.append( l ) if len(grouped_data) > 1000000/8: # one million lines # process batch grouped_data = [] This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From wrw at mac.com Fri Aug 31 16:23:08 2012 From: wrw at mac.com (William R. Wing (Bill Wing)) Date: Fri, 31 Aug 2012 10:23:08 -0400 Subject: [Tutor] Tutor Digest, Vol 102, Issue 98 In-Reply-To: References: <49870031-CAF9-4D91-9E15-D8ECF11FEFC9@gmail.com> Message-ID: <49197693-CDCC-4BCD-AA5F-CC2AA2D1EC6E@mac.com> On Aug 31, 2012, at 4:31 AM, Mark Lawrence wrote: > On 31/08/2012 04:27, William R. Wing (Bill Wing) wrote: >> >> How about - >> >>>>> for item in iter(list): >>>>> ?.print item > > Overengineering? :) A list is an iterator. > Right you are - should have been: for item in list: ....print item -Bill From rdmoores at gmail.com Fri Aug 31 18:49:45 2012 From: rdmoores at gmail.com (Richard D. Moores) Date: Fri, 31 Aug 2012 09:49:45 -0700 Subject: [Tutor] Problem caused by installing 2.7.3 Message-ID: MS Windows 7 Home Premium 64-bit SP1 I've been using 3.x for a long time, but the other day I thought it would also be good to have the latest version of 2.x available. So I downloaded it and installed it. I have some useful (to me) scripts that I use frequently, that I call with Windows shortcut keys. They used to open in a nice cmd.exe window I'd configured to my liking. Now I find that they open in the default white on black tiny window, C:\Python27\python.exe, which tries to run the 3.x scripts in 2.7. How did that change come about, and how can I correct it? FWIW, the path in System Variables is C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Python32\;C:\Python31\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program Files (x86)\QuickTime\QTSystem\ Thanks, Dick Moores From crawlzone at gmail.com Fri Aug 31 19:05:44 2012 From: crawlzone at gmail.com (Ray Jones) Date: Fri, 31 Aug 2012 10:05:44 -0700 Subject: [Tutor] Using a calling program to change Python script arguments Message-ID: <5040EEE8.3020103@gmail.com> As an aid to learning Python, I am currently in the process of converting my Bash scripts into Python scripts. Through the years, as I have accumulated a variety of sites, I have been maintaining a half dozen or so Bash scripts that basically do the same thing: log me into a streaming video site and record the stream. So as I make this conversion, it's becoming obvious to me that as my Python skill increases, I will be making changes to these scripts. But that means maintaining all these scripts in parallel with each other. I'm considering creating calling scripts (either Bash or another Python script), all of which call a single Python capture script. Each calling script would have its own info concerning IP, port, user name, password, http or rtsp, time zone info, length of capture time, etc., and then those scripts would pass their information to the single Python script, greatly easing the maintenance issue. But that's a ton of command line arguments to pass to the Python capture script and have it parse. Is there another method for one Python script to call/import/execute a Python script and integrate the name space so that the variables in each of the calling scripts would be directly usable by the Python module/child process/whatever without passing arguments either via line arguments (yeccchhh!) or function calls (considerably better)? Arguably, once the calling script passed the info to the main script, it really wouldn't need to be there at all. So how high is this pie-in-the-sky dream of mine? Ray From eryksun at gmail.com Fri Aug 31 19:57:38 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 31 Aug 2012 13:57:38 -0400 Subject: [Tutor] Problem caused by installing 2.7.3 In-Reply-To: References: Message-ID: On Fri, Aug 31, 2012 at 12:49 PM, Richard D. Moores wrote: > > MS Windows 7 Home Premium 64-bit SP1 > > I have some useful (to me) scripts that I use frequently, that I call > with Windows shortcut keys. They used to open in a nice cmd.exe window > I'd configured to my liking. Now I find that they open in the default > white on black tiny window, C:\Python27\python.exe, which tries to run > the 3.x scripts in 2.7. How did that change come about, and how can I > correct it? .py files are associated (assoc .py) with Python.File (ftype Python.File), which defines the command to run .py console scripts. The 2.x installer overwrote the registry keys. If you plan to write scripts for both 2.x and 3.x, the best solution, IMO, is to install pylauncher to add shebang support to your scripts: https://bitbucket.org/vinay.sajip/pylauncher The simpler of the two installations for you would be launchwin.amd64.msi. It puts py.exe and pyw.exe in the Windows folder, so you don't have to muck with PATH. Once installed your scripts will run with py.exe, which takes care of parsing the shebang and starting the required interpreter. You can define additional shebangs in %LOCALAPPDATA%\py.ini (e.g. to add support for pypy). I think this also works in %APPDATA%\py.ini if you'd rather use your roaming profile. See the docs for more information: https://bitbucket.org/vinay.sajip/pylauncher/raw/tip/Doc/launcher.rst From alan.gauld at btinternet.com Fri Aug 31 23:19:27 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Aug 2012 22:19:27 +0100 Subject: [Tutor] Using a calling program to change Python script arguments In-Reply-To: <5040EEE8.3020103@gmail.com> References: <5040EEE8.3020103@gmail.com> Message-ID: On 31/08/12 18:05, Ray Jones wrote: > script and have it parse. Is there another method for one Python script > to call/import/execute a Python script and integrate the name space so > that the variables in each of the calling scripts would be directly > usable by the Python module/child process/whatever without passing > arguments either via line arguments (yeccchhh!) or function calls Just create a module with the variables in it. Then import and access that module data in all your scripts. eg. import mysitedata print mysitedata.myValue -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From crawlzone at gmail.com Fri Aug 31 23:51:40 2012 From: crawlzone at gmail.com (Ray Jones) Date: Fri, 31 Aug 2012 14:51:40 -0700 Subject: [Tutor] Using a calling program to change Python script arguments In-Reply-To: References: <5040EEE8.3020103@gmail.com> Message-ID: <504131EC.7010907@gmail.com> On 08/31/2012 02:19 PM, Alan Gauld wrote: > On 31/08/12 18:05, Ray Jones wrote: > >> script and have it parse. Is there another method for one Python script >> to call/import/execute a Python script and integrate the name space so >> that the variables in each of the calling scripts would be directly >> usable by the Python module/child process/whatever without passing >> arguments either via line arguments (yeccchhh!) or function calls > > > Just create a module with the variables in it. > > Then import and access that module data in all your scripts. > > eg. > > import mysitedata > print mysitedata.myValue Backasswards was I again. I was thinking of loading the main from my calling program. I will try that. The only thing I would have to pass on the command line then is which module I want to import for the site I want to access. Okay. Now I must figure out how to create the module and have my calling script look in the right place.... ;) Thanks. Ray From damjan.kuzmic at gmail.com Wed Aug 29 22:20:27 2012 From: damjan.kuzmic at gmail.com (damjan kuzmic) Date: Wed, 29 Aug 2012 22:20:27 +0200 Subject: [Tutor] math description Message-ID: Hello, i would like to know how to write a formula that in excell looks like this: A / EXP(-LN(2) * t) -- greetings car kuzma -------------- next part -------------- An HTML attachment was scrubbed... URL: From matthewlove94 at googlemail.com Fri Aug 17 22:11:34 2012 From: matthewlove94 at googlemail.com (Matthew Love) Date: Fri, 17 Aug 2012 20:11:34 -0000 Subject: [Tutor] Doing the same thing twice. Works first time but not the second. Message-ID: This error is confusing me. I am wondering if anyone can help me out. Using W7 64 Ultimate Using Python 3.1.1 This is the relevant part of the program. class Player(object): def __init__(self): print("Player created.") def inventory(self): self.inventory = ["torch"] return self.inventory player = Player() print("\nYour current inventory is", end= " ") print(player.inventory()) print("\nYour current inventory is", end= " ") print(player.inventory()) The first time the program prints the inventory correctly but when it does the exact same thing again it throws up an error. I can't think of a reason for why this happens. This is the error: Traceback (most recent call last): File "C:\Users\Matthew\Desktop\test.py", line 16, in print(player.inventory()) TypeError: 'list' object is not callable Can anyone help me with this? I'd appreciate it. From niceguysan at gmail.com Sat Aug 18 16:12:40 2012 From: niceguysan at gmail.com (=?UTF-8?B?xaHDo8Ox?=) Date: Sat, 18 Aug 2012 14:12:40 -0000 Subject: [Tutor] How to get MAC address using Python at windows 7 Message-ID: I need to find some way how i can get mac address of windows 7 in python.. -- ** [image: San's personal blog] * * -------------- next part -------------- An HTML attachment was scrubbed... URL: From strikerrosen at yahoo.com Sun Aug 19 22:44:42 2012 From: strikerrosen at yahoo.com (Andrew Rosen) Date: Sun, 19 Aug 2012 20:44:42 -0000 Subject: [Tutor] Python Message-ID: <1345408977.91331.YahooMailNeo@web65506.mail.ac4.yahoo.com> I have a Windows Computer and I'm trying to make a shortcut on my desktop that will run a program, so I don't have to open up a New Window form Python Shell and use that to run the program. I can't figure out how to do it, can you help me? -------------- next part -------------- An HTML attachment was scrubbed... URL: From vicki at stanfield.net Sun Aug 19 03:17:24 2012 From: vicki at stanfield.net (vickistan) Date: Sun, 19 Aug 2012 01:17:24 -0000 Subject: [Tutor] how to print array without adding newline Message-ID: <1345339036049-4985646.post@n6.nabble.com> Hello: I am trying to output an array to another program that takes an array as input, but the print statement adds a newline. If it were adding to each individual element, I could solve it easily, but it is adding one at the end of the array. Is there another way to print an array besides print arrayname If it were a string, I have a whole host of options, but I need it to be output as an array. Each element is a url. I call it from a browser, and it works except for the added newline. Here are the relevant lines: ================= /* code that connects to cloudfiles omitted */ containers = conn.get_all_containers() i=0 print "Content-type: text/html\n\n"; wholelist=containers[0].list_objects() random.shuffle(wholelist) newlist=[] try: del wholelist[int(sys.argv[1]):] while i < int(sys.argv[1]): newlist.append("http://example.com/"+wholelist[i].rstrip()) i = i+1 except IndexError, e: del newlist[5] print newlist ============== The output I am seeing is as follows: ['http://example.com/wet-longhaireddachshund.jpg', 'http://example.com/dachshund2.jpg', 'http://example.com/dachshundingrass.jpg'] Any tips on better coding practices are welcome, but please don't beat me up Thanks, vickistan -- View this message in context: http://python.6.n6.nabble.com/how-to-print-array-without-adding-newline-tp4985646.html Sent from the Python - tutor mailing list archive at Nabble.com. From virusfallen at yahoo.com Mon Aug 20 06:50:44 2012 From: virusfallen at yahoo.com (john) Date: Mon, 20 Aug 2012 04:50:44 -0000 Subject: [Tutor] python 2.7.1 Message-ID: <4C6597F7-E210-4EF8-803A-26F299B25DF4@yahoo.com> print "hello world" #this is just something to say >>> /Users/jonathan/Documents/hello.py File "", line 1 /Users/jonathan/Documents/hello.py ^ SyntaxError: invalid syntax what am i doing wrong? -------------- next part -------------- An HTML attachment was scrubbed... URL: 20/d971c3ae/attachment.html>