Re: [Tutor] Passing a Variable
> I've read your code. Frankly I don't understand your problem. I also don't see any occurrence of "health". There isn't a reference to health here. My goal is to have this code act as a checker for health, fatigue, time_remaining, or any other sort of statistic you'd like to throw into it. My problem is that when I try: instance = Statistic(stat=health, sound=spam, low=1, mid=15, high=30) health can change elsewhere in the program, but the instance of statistic class won't automatically see it. > Also your description of the program and the program itself is kinda overwhelming, and so much of that information is not relevant to your question. That makes it hard to understand the question. My apologies if this came across as verbose. I'm a newbie at all things python, so I'm still learning everything from code to conventions. > You can simplify the above logic: Thank you for that. I will happily accept style suggestions whenever I can get them. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling another script
hi, thanks everyone. i tried sys.exit() inside one.py and it works to return to DOS prompt. thanks tcl From: tc...@hotmail.com To: bgai...@gmail.com; tutor@python.org Date: Mon, 4 Apr 2011 05:10:50 + Subject: Re: [Tutor] Calling another script hi, i opened a cmd DOS prompt to execute one.py. it works to execute codes from two.py and three.py. yes, you are fight it will not re-execute the module. is there a way to do it? i want after the python script finishes execution will return the control to the DOS prompt instead of leaving as >>>. i tried putting sys.exit(). thanks cltee Date: Mon, 4 Apr 2011 00:29:19 -0400 From: bgai...@gmail.com To: tutor@python.org Subject: Re: [Tutor] Calling another script On 4/3/2011 11:58 PM, tee chwee liong wrote: hi, i want to read from a file which will indicate which operation to execute. so i'm using configparser module. i want one.py to read a configuration file and executes two.py and three.py. however, it only executes two.py and not three.py codes. pls help advise. I see no reason for the problem you report. I tested a brief version: import two import three and it worked as expected. The only reason I can think of for it not working is that you had already imported three.py. Importing again will NOT re-execute the module! thanks tcl + one.py: import ConfigParser config = ConfigParser.ConfigParser() config.read("configuration.ini") operation=config.get("config", "operation") if int(operation)== 0: import two import three else: print "Default" two.py: print "executing script number 2" three.py: print "executing script number 3" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling another script
hi, i opened a cmd DOS prompt to execute one.py. it works to execute codes from two.py and three.py. yes, you are fight it will not re-execute the module. is there a way to do it? i want after the python script finishes execution will return the control to the DOS prompt instead of leaving as >>>. i tried putting sys.exit(). thanks cltee Date: Mon, 4 Apr 2011 00:29:19 -0400 From: bgai...@gmail.com To: tutor@python.org Subject: Re: [Tutor] Calling another script On 4/3/2011 11:58 PM, tee chwee liong wrote: hi, i want to read from a file which will indicate which operation to execute. so i'm using configparser module. i want one.py to read a configuration file and executes two.py and three.py. however, it only executes two.py and not three.py codes. pls help advise. I see no reason for the problem you report. I tested a brief version: import two import three and it worked as expected. The only reason I can think of for it not working is that you had already imported three.py. Importing again will NOT re-execute the module! thanks tcl + one.py: import ConfigParser config = ConfigParser.ConfigParser() config.read("configuration.ini") operation=config.get("config", "operation") if int(operation)== 0: import two import three else: print "Default" two.py: print "executing script number 2" three.py: print "executing script number 3" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a numpy matrix to a numpy array
This may work: from pprint import pprint plan = """xo xo ox oo ox ox """ width = plan.index("\n") height = plan.count("\n") a = [[w, h] for h in xrange(height) for w in xrange(width)] for (xy, c) in zip(a, plan.replace("\n", "")): xy.append(c) pprint(a) |46>%run test.py [[0, 0, 'x'], [1, 0, 'o'], [2, 0, 'o'], [3, 0, 'o'], [4, 0, 'o'], [5, 0, 'o'], [0, 1, 'x'], [1, 1, 'o'], [2, 1, 'o'], [3, 1, 'o'], [4, 1, 'o'], [5, 1, 'o'], [0, 2, 'o'], [1, 2, 'x'], [2, 2, 'x'], [3, 2, 'x'], [4, 2, 'x'], [5, 2, 'x'], [0, 3, 'o'], [1, 3, 'o'], [2, 3, 'o'], [3, 3, 'o'], [4, 3, 'o'], [5, 3, 'o'], [0, 4, 'o'], [1, 4, 'o'], [2, 4, 'o'], [3, 4, 'o'], [4, 4, 'o'], [5, 4, 'x'], [0, 5, 'o'], [1, 5, 'o'], [2, 5, 'o'], [3, 5, 'o'], [4, 5, 'o'], [5, 5, 'x']] * David Crisp [2011-04-04 13:09:35 +1000]: > On Fri, Apr 1, 2011 at 2:04 AM, Peter Otten <__pete...@web.de> wrote: > > David Crisp wrote: > > > >> Hello, > >> > >> I have a very simple question / problem I need answered. The problem > >> is imnot entirely sure of the correct terminology and langauge to use > >> to describe it. (One of the reasons im using this miling list) > >> > >> I have a 2d matrix representing the X the Y and the Z value of a > >> point. I wish to convert that matrix to an array. What is a good > >> way of doing so? > >> > >> Eg: > >> Matrix > >> 012345 > >> 0xo > >> 1xo > >> 2ox > >> 3oo > >> 4ox > >> 5ox > >> > >> > >> I want to convert that to a 2d array which looks like: > >> 0,0,x > >> 0,1,o > >> 0,2,o > >> 0,3,o > >> 0,4,o > >> 0,5,o > >> ... > >> 5,4,o > >> 5,5,o > >> > >> I am pretty sure it is simple. I'm just having a brain fade. > > > > Using basic numpy: > > > import numpy as np > a = np.array(list("xoo" > > ... "oxx" > > ... "oxo")).reshape(3,3) > a > > array([['x', 'o', 'o'], > > ['o', 'x', 'x'], > > ['o', 'x', 'o']], > > dtype='|S1') > np.array([np.arange(9)//3, np.arange(9)%3, a.flatten()]).transpose() > > array([['0', '0', 'x'], > > ['0', '1', 'o'], > > ['0', '2', 'o'], > > ['1', '0', 'o'], > > ['1', '1', 'x'], > > ['1', '2', 'x'], > > ['2', '0', 'o'], > > ['2', '1', 'x'], > > ['2', '2', 'o']], > > dtype='|S8') > np.array([np.arange(9)//3, np.arange(9)%3, > > (a=="x").flatten()]).transpose() > > array([[0, 0, 1], > > [0, 1, 0], > > [0, 2, 0], > > [1, 0, 0], > > [1, 1, 1], > > [1, 2, 1], > > [2, 0, 0], > > [2, 1, 1], > > [2, 2, 0]]) > np.array([np.arange(9)//3, np.arange(9)%3, a.flatten()], > > dtype=object).transpose() > > array([[0, 0, x], > > [0, 1, o], > > [0, 2, o], > > [1, 0, o], > > [1, 1, x], > > [1, 2, x], > > [2, 0, o], > > [2, 1, x], > > [2, 2, o]], dtype=object) > > > > If that's not good enough you may also ask on the numpy mailing list. > > Thanks Peter, > > That appears to do what I want, in a way.How does this work if you > have a matrix which is of variable size? For instance, some of my > data will create a 10 by 10 matrix but some will create a 40 by 40 > matrix, Or for that matter any size.I notice your example > specifically states there will be 9 outputs ( tupples? ) what if I > want to say "just create as many tuples as you need to use to > transpose the data" > > Regards, > David > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calling another script
On 4/3/2011 11:58 PM, tee chwee liong wrote: hi, i want to read from a file which will indicate which operation to execute. so i'm using configparser module. i want one.py to read a configuration file and executes two.py and three.py. however, it only executes two.py and not three.py codes. pls help advise. I see no reason for the problem you report. I tested a brief version: import two import three and it worked as expected. The only reason I can think of for it not working is that you had already imported three.py. Importing again will NOT re-execute the module! thanks tcl + *_one.py:_* import ConfigParser config = ConfigParser.ConfigParser() config.read("configuration.ini") operation=config.get("config", "operation") if int(operation)== 0: import two import three else: print "Default" *_two.py:_* print "executing script number 2" *_three.py:_* print "executing script number 3" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Calling another script
hi, i want to read from a file which will indicate which operation to execute. so i'm using configparser module. i want one.py to read a configuration file and executes two.py and three.py. however, it only executes two.py and not three.py codes. pls help advise. thanks tcl + one.py: import ConfigParser config = ConfigParser.ConfigParser() config.read("configuration.ini") operation=config.get("config", "operation") if int(operation)== 0: import two import three else: print "Default" two.py: print "executing script number 2" three.py: print "executing script number 3" configuration.ini Description: Binary data import ConfigParser config = ConfigParser.ConfigParser() config.read("configuration.ini") operation=config.get("config", "operation") if int(operation)== 0: import two import three else: print "Default" print "executing script number 2" print "executing script number 3" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Passing a Variable
On 4/3/2011 9:55 PM, Ryan Strunk wrote: Hi list, Hi I've read your code. Frankly I don't understand your problem. I also don't see any occurrence of "health". Could you point to a specific line of code, explain what you want and what you are getting. Also your description of the program and the program itself is kinda overwhelming, and so much of that information is not relevant to your question. That makes it hard to understand the question. How about posting the smallest possible piece of code that exemplifies the problem? Python does not "pass by value". It passes a reference to the argument. In essence: def foo(a): pass b = somePythonObject # b is now a reference to somePythonObject foo(b) In calling the function Python "binds" local name a to somePythonObject a is now another reference to somePythonObject I am in the midst of trying to code a game based entirely on audio cues, and I've run into a bit of a snag when trying to monitor certain variables. I'll lay out the framework of what I'm going for in the hope that it makes sense when written down. In a standard video game I could have a health bar go from normal to yellow to red as it diminishes. In audio, though, I don't have that luxury. As a result, I have conceptualized a system whereby a player hears a sound every so often if a particular stat drops into the caution range. If the player drops into the danger range, the sound loops continuously. I also wanted to make sure that if the player dropped from caution to danger, there wasn't a big, awkward pause in the sound loop and that the player would know immediately that his stat had dropped (see first and second if checks in the check method). The problem: My existing methods directly update stats. For example: the player class has a self.health stat which is directly affected by other methods. This has caused no problem up until now. When I pass self.health to the code I will paste below, however, the Statistic class does not receive health, but rather health's value. I understand that python passes variables by value and not by reference, and this has not been a problem up until now. Now that I am trying to design a class which explicitly checks a specific variable, though, I can't fathom a way to do it unless I pass a direct reference, and I'm not sure that can be done. I need to figure out a way for the below code to check the value of the health variable and act on it. This way, if player's self.health changes, the static class will take note of that and respond accordingly. It occurred to me to make Statistic a child of int, but I'm told that's more trouble than I probably want to deal with. Any suggestions/advice anyone has would be greatly appreciated. Best, Ryan import sound_lib from game_utils import delay #this encapsulates threading.Timer's assignment and start method class Statistic(object): def __init__(self, stat=None, sound=None, low=None, mid=None, high=None): self.stat = stat self.sound = sound self.low = low self.mid = mid self.high = high self.status = 'safe' self.auto_check_timer = None def auto_check(self): if self.stat> self.high: self.status = 'safe' return if self.mid<= self.stat<= self.high: self.status = 'caution' self.sound.play(True) self.auto_check_timer = delay(self.sound.bytes_to_seconds(len(self.sound))*2, self.auto_check) return if self.low<= self.stat< self.mid: self.status = 'danger' self.sound.play(True) self.auto_check_timer = delay(self.sound.bytes_to_seconds(len(self.sound)), self.auto_check) You can simplify the above logic: if self.stat > self.high: self.status = 'safe' elif self.stat >= self.mid: self.status = 'caution' self.sound.play(True) self.auto_check_timer = delay(self.sound.bytes_to_seconds(len(self.sound))*2, self.auto_check) elif self.stat >= self.low: self.status = 'danger' self.sound.play(True) self.auto_check_timer = delay(self.sound.bytes_to_seconds(len(self.sound)), self.auto_check) def check(self): if self.status = 'caution' and self.low<= self.stat< self.mid: #This will set the program to start a constant alarm when the stat level has dropped below caution self.auto_check_timer.cancel() if self.sound.is_playing: #to assist in setting up the caution to danger transition #a standard playing sound will have a timer running alongside it, so skip the next guard and return if self.auto_check_timer.is_alive() == False: #guard to make sure program doesn't catch every playing sound, should prevent repeated checks from recalling auto_check sound_duration = sel
Re: [Tutor] Passing a Variable
On Sun, Apr 03, 2011 at 08:55:25PM -0500, Ryan Strunk wrote: > I understand that python passes variables by value and not by reference You understand wrongly. Python is neither pass-by-value nor pass-by-reference. I've written thousands of words on this topic before, so excuse me if I'm a little terse. Rather than write it all out again, I'll just point you at this post: http://mail.python.org/pipermail/tutor/2010-December/080505.html You might also like to read this: http://effbot.org/zone/call-by-object.htm > and > this has not been a problem up until now. Now that I am trying to design a > class which explicitly checks a specific variable, though, I can't fathom a > way to do it unless I pass a direct reference, and I'm not sure that can be > done. One standard way to do this is to have your statistic class have a player attribute, and then have it check the player.health attribute. class Statistic(object): # Check statistics of a player. def __init__(self, player): self.player = player def check_health(self): if self.player.health < 0: print "Bam, you're dead!" An alternative is to have the player object check its own health, calling some appropriate notification object. This could be a global variable, or an attribute of the player (that way each player could have their own notification user-interface). notifier = Notify(sound='on', health_bar='off') # whatever... class Player(object): def __init__(self): self.health = 100 def check_health(self): if self.health < 0: notifier.announce_dead(self) elif self.health < 10: notifer.announce_critical(self) else: notifier.announce_normal(self) Or any of many variations on these. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Converting a numpy matrix to a numpy array
On Fri, Apr 1, 2011 at 2:04 AM, Peter Otten <__pete...@web.de> wrote: > David Crisp wrote: > >> Hello, >> >> I have a very simple question / problem I need answered. The problem >> is imnot entirely sure of the correct terminology and langauge to use >> to describe it. (One of the reasons im using this miling list) >> >> I have a 2d matrix representing the X the Y and the Z value of a >> point. I wish to convert that matrix to an array. What is a good >> way of doing so? >> >> Eg: >> Matrix >> 012345 >> 0xo >> 1xo >> 2ox >> 3oo >> 4ox >> 5ox >> >> >> I want to convert that to a 2d array which looks like: >> 0,0,x >> 0,1,o >> 0,2,o >> 0,3,o >> 0,4,o >> 0,5,o >> ... >> 5,4,o >> 5,5,o >> >> I am pretty sure it is simple. I'm just having a brain fade. > > Using basic numpy: > import numpy as np a = np.array(list("xoo" > ... "oxx" > ... "oxo")).reshape(3,3) a > array([['x', 'o', 'o'], > ['o', 'x', 'x'], > ['o', 'x', 'o']], > dtype='|S1') np.array([np.arange(9)//3, np.arange(9)%3, a.flatten()]).transpose() > array([['0', '0', 'x'], > ['0', '1', 'o'], > ['0', '2', 'o'], > ['1', '0', 'o'], > ['1', '1', 'x'], > ['1', '2', 'x'], > ['2', '0', 'o'], > ['2', '1', 'x'], > ['2', '2', 'o']], > dtype='|S8') np.array([np.arange(9)//3, np.arange(9)%3, > (a=="x").flatten()]).transpose() > array([[0, 0, 1], > [0, 1, 0], > [0, 2, 0], > [1, 0, 0], > [1, 1, 1], > [1, 2, 1], > [2, 0, 0], > [2, 1, 1], > [2, 2, 0]]) np.array([np.arange(9)//3, np.arange(9)%3, a.flatten()], > dtype=object).transpose() > array([[0, 0, x], > [0, 1, o], > [0, 2, o], > [1, 0, o], > [1, 1, x], > [1, 2, x], > [2, 0, o], > [2, 1, x], > [2, 2, o]], dtype=object) > > If that's not good enough you may also ask on the numpy mailing list. Thanks Peter, That appears to do what I want, in a way.How does this work if you have a matrix which is of variable size? For instance, some of my data will create a 10 by 10 matrix but some will create a 40 by 40 matrix, Or for that matter any size.I notice your example specifically states there will be 9 outputs ( tupples? ) what if I want to say "just create as many tuples as you need to use to transpose the data" Regards, David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Passing a Variable
Hi list, I am in the midst of trying to code a game based entirely on audio cues, and I've run into a bit of a snag when trying to monitor certain variables. I'll lay out the framework of what I'm going for in the hope that it makes sense when written down. In a standard video game I could have a health bar go from normal to yellow to red as it diminishes. In audio, though, I don't have that luxury. As a result, I have conceptualized a system whereby a player hears a sound every so often if a particular stat drops into the caution range. If the player drops into the danger range, the sound loops continuously. I also wanted to make sure that if the player dropped from caution to danger, there wasn't a big, awkward pause in the sound loop and that the player would know immediately that his stat had dropped (see first and second if checks in the check method). The problem: My existing methods directly update stats. For example: the player class has a self.health stat which is directly affected by other methods. This has caused no problem up until now. When I pass self.health to the code I will paste below, however, the Statistic class does not receive health, but rather health's value. I understand that python passes variables by value and not by reference, and this has not been a problem up until now. Now that I am trying to design a class which explicitly checks a specific variable, though, I can't fathom a way to do it unless I pass a direct reference, and I'm not sure that can be done. I need to figure out a way for the below code to check the value of the health variable and act on it. This way, if player's self.health changes, the static class will take note of that and respond accordingly. It occurred to me to make Statistic a child of int, but I'm told that's more trouble than I probably want to deal with. Any suggestions/advice anyone has would be greatly appreciated. Best, Ryan import sound_lib from game_utils import delay #this encapsulates threading.Timer's assignment and start method class Statistic(object): def __init__(self, stat=None, sound=None, low=None, mid=None, high=None): self.stat = stat self.sound = sound self.low = low self.mid = mid self.high = high self.status = 'safe' self.auto_check_timer = None def auto_check(self): if self.stat > self.high: self.status = 'safe' return if self.mid <= self.stat <= self.high: self.status = 'caution' self.sound.play(True) self.auto_check_timer = delay(self.sound.bytes_to_seconds(len(self.sound))*2, self.auto_check) return if self.low <= self.stat < self.mid: self.status = 'danger' self.sound.play(True) self.auto_check_timer = delay(self.sound.bytes_to_seconds(len(self.sound)), self.auto_check) def check(self): if self.status = 'caution' and self.low <= self.stat < self.mid: #This will set the program to start a constant alarm when the stat level has dropped below caution self.auto_check_timer.cancel() if self.sound.is_playing: #to assist in setting up the caution to danger transition #a standard playing sound will have a timer running alongside it, so skip the next guard and return if self.auto_check_timer.is_alive() == False: #guard to make sure program doesn't catch every playing sound, should prevent repeated checks from recalling auto_check sound_duration = self.sound.bytes_to_seconds(len(self.sound)) - self.sound.bytes_to_seconds(self.sound.position) self.auto_check_timer = delay(sound_duration, self.auto_check) return if self.auto_check_timer == False: #if the timer has never been called, call auto_check self.auto_check() return if self.auto_check_timer.is_alive == True: #there's already a timer running. return return #If it gets this far, it's because the timer already ran, the player is 'safe', and another check is being performed self.auto_check() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Prologix GPIB to USB converter
markri...@gsoftcon.com wrote: Hello everyone. Is there any code examples out there to on how to use the prologix GPIB to USB converter? Probably. What does this have to do with learning Python? This is not a general "ask any computer-related question" list. It's not even a general "ask anything related to Python" list. This is specifically for learning the programming language Python, aimed at beginners. In any case, Google is your friend. Have you tried googling for "prologix GPIB to USB converter +python"? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Prologix GPIB to USB converter
wrote in message news:1301863224.23037191@192.168.4.58... Hello everyone. Is there any code examples out there to on how to use the prologix GPIB to USB converter? Googling prologix gpib to usb converter python code Got me Resources for GPIB Controllers || Prologix, LLC Prologix GPIB Configurator - Configuration utility for Prologix GPIB-USB controller by John Miles. ... The following sample programs demonstrate how to programmatically send and ... C/C++ sample · Binary transfer sample in Python. ... As the first hit. I didn't visit but it sounds promising. There were another 3 or 4 tyhat looked like they might have code. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 86, Issue 12
Hello, 2011/4/3 Mateusz Koryciński > > (2) You have a *mess* of unreadable variable names. You have: >> >> w.steps w.x w.y w.world data.c data.n data.e data.s data.w data.cc >> >> (yes, you have data.c AND data.cc!!!) plus loop variables z i j and r, >> and z is never used! How is anyone supposed to understand what this >> does? Even if you solve this problem *today*, in a week you will have >> forgotten what the code does and you won't understand it. >> >> > It's an objects from another classes. Variable z is used because there is a > need to provide number of steps to execute by user. Variable c mean central > cell in array whe are considering right now, other one is neighbours but cc, > cc means new state (couse basing on central cell and its neighbors we want > to change value to cc). > I think the point that was being made was that all this (and the other explanations you gave) should be in or circumscribed by your source code, and that you should try to use longer/more meaningful variable names. It wasn't specifically a request for you to explain all of these things here on the email list, although that might be helpful going forward. Nevertheless you still need to make these things part of and as obvious as possible from reading your source code. It should always be the definitive reference. IMHO source which has to be substantially explained outside of itself should be improved/clarified/documented/commented inside the source until external explanations are largely unneccesary and the code is as far as practicable self-documenting. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Prologix GPIB to USB converter
Hello everyone. Is there any code examples out there to on how to use the prologix GPIB to USB converter? Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sorting based on elements which are nested in a list
On 04/04/11 00:14, ranjan das wrote: > > I have a list which I want to sort based on ('a','b','c') first and then > based on (1,2,3) > > How do i do these using itemgetter() since the list is nested > > A=[('k3', ['b', 3]), ('k2', ['a', 1]), ('k1', ['a', 3]), ('k4', ['c', 2])] > > The solution I am looking for is > > A_sorted=[ ('k2', ['a', 1]), ('k1', ['a', 3]), ('k3', ['b', 3]) ('k4', > ['c', 2])] itemgetter() is only a convenience function, you can make your own function, like this: A_sorted = sorted(A, key=lambda i: (i[1][0], i[1][1])) or in your particular case, since list is compared in lexicographical order, this would actually work: A_sorted = sorted(A, key=itemgetter(1)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex question
Hugo Arts wrote: > 2011/4/3 "Andrés Chandía" : >> >> >> I continue working with RegExp, but I have reached a point for wich I >> can't find documentation, maybe there is no possible way to do it, any >> way I throw the question: >> >> This is my code: >> >> contents = re.sub(r'Á', >> "A", contents) >> contents = re.sub(r'á', "a", >> contents) >> contents = re.sub(r'É', "E", contents) >> contents = re.sub(r'é', "e", contents) >> contents = re.sub(r'Í', "I", contents) >> contents = re.sub(r'í', "i", contents) >> contents = re.sub(r'Ó', "O", contents) >> contents = re.sub(r'ó', "o", contents) >> contents = re.sub(r'Ú', "U", contents) >> contents = re.sub(r'ú', "u", contents) >> >> It is >> clear that I need to convert any accented vowel into the same not >> accented vowel, The >> qestion is : is there a way to say that whenever you find an accented >> character this one >> has to change into a non accented character, but not every character, it >> must be only this vowels and accented this way, because at the language I >> am working with, there are letters >> like ü, and ñ that should remain the same. >> > > Okay, first thing, forget about regexes for this problem.They're too > complicated and not suited to it. > > Encoding issues make this a somewhat complicated problem. In Unicode, > There's two ways to encode most accented characters. For example, the > character "Ć" can be encoded both by U+0106, "LATIN CAPITAL LETTER C > WITH ACUTE", and a combination of U+0043 and U+0301, being simply 'C' > and the 'COMBINING ACUTE ACCENT', respectively. You must remove both > forms to be sure every accented character is gone from your string. > > using unicode.translate, you can craft a translation table to > translate the accented characters to their non-accented counterparts. > The combining characters can simply be removed by mapping them to > None. If you go that road you might be interested in Fredrik Lundh's article at http://effbot.org/zone/unicode-convert.htm The class presented there is a bit tricky, but for your purpose it might be sufficient to subclass it: >>> KEEP_CHARS = set(ord(c) for c in u"üñ") >>> class Map(unaccented_map): ... def __missing__(self, key): ... if key in KEEP_CHARS: ... self[key] = key ... return key ... return unaccented_map.__missing__(self, key) ... >>> print u"äöü".translate(Map()) aoü ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] sorting based on elements which are nested in a list
I have a list which I want to sort based on ('a','b','c') first and then based on (1,2,3) How do i do these using itemgetter() since the list is nested A=[('k3', ['b', 3]), ('k2', ['a', 1]), ('k1', ['a', 3]), ('k4', ['c', 2])] The solution I am looking for is A_sorted=[ ('k2', ['a', 1]), ('k1', ['a', 3]), ('k3', ['b', 3]) ('k4', ['c', 2])] Please suggest Regards, ranjan -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex question
2011/4/3 "Andrés Chandía" : > > > I continue working with RegExp, but I have reached a point for wich I can't > find > documentation, maybe there is no possible way to do it, any way I throw the > question: > > This is my code: > > contents = re.sub(r'Á', > "A", contents) > contents = re.sub(r'á', "a", > contents) > contents = re.sub(r'É', "E", contents) > contents = re.sub(r'é', "e", contents) > contents = re.sub(r'Í', "I", contents) > contents = re.sub(r'í', "i", contents) > contents = re.sub(r'Ó', "O", contents) > contents = re.sub(r'ó', "o", contents) > contents = re.sub(r'Ú', "U", contents) > contents = re.sub(r'ú', "u", contents) > > It is > clear that I need to convert any accented vowel into the same not accented > vowel, > The > qestion is : is there a way to say that whenever you find an accented > character this > one > has to change into a non accented character, but not every character, it must > be only > this vowels and accented this way, because at the language I am working with, > there are > letters > like ü, and ñ that should remain the same. > Okay, first thing, forget about regexes for this problem.They're too complicated and not suited to it. Encoding issues make this a somewhat complicated problem. In Unicode, There's two ways to encode most accented characters. For example, the character "Ć" can be encoded both by U+0106, "LATIN CAPITAL LETTER C WITH ACUTE", and a combination of U+0043 and U+0301, being simply 'C' and the 'COMBINING ACUTE ACCENT', respectively. You must remove both forms to be sure every accented character is gone from your string. using unicode.translate, you can craft a translation table to translate the accented characters to their non-accented counterparts. The combining characters can simply be removed by mapping them to None. HTH, Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex question
I continue working with RegExp, but I have reached a point for wich I can't find documentation, maybe there is no possible way to do it, any way I throw the question: This is my code: contents = re.sub(r'Á', "A", contents) contents = re.sub(r'á', "a", contents) contents = re.sub(r'É', "E", contents) contents = re.sub(r'é', "e", contents) contents = re.sub(r'Í', "I", contents) contents = re.sub(r'í', "i", contents) contents = re.sub(r'Ó', "O", contents) contents = re.sub(r'ó', "o", contents) contents = re.sub(r'Ú', "U", contents) contents = re.sub(r'ú', "u", contents) It is clear that I need to convert any accented vowel into the same not accented vowel, The qestion is : is there a way to say that whenever you find an accented character this one has to change into a non accented character, but not every character, it must be only this vowels and accented this way, because at the language I am working with, there are letters like ü, and ñ that should remain the same. thanks you all. ___ andrés chandía P No imprima innecesariamente. ¡Cuide el medio ambiente! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 86, Issue 12
Hi, Thanks for all yout responses! I will answer below each one (I hope it wouldn't be a problem). > > > Message: 1 > Date: Sat, 2 Apr 2011 23:45:49 +0100 > From: "Alan Gauld" > To: tutor@python.org > Subject: Re: [Tutor] 'for' iteration stop problem > Message-ID: > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; >reply-type=original > > > "Mateusz Korycinski" wrote > > > My problem is simple for sure, but unfortunately I'm a bit beginner > > and I've > > stucked in it. I hope it is not a problem since as I understand this > > mailing > > list is for beginners. > > No problem, we try to answer questions such as this here. > > > I have some problem with 'for' loop in algorithm. > > Code and description for this problem could be find here: > > > http://stackoverflow.com/questions/5520145/how-to-stop-iteration-when-if-statement-is-true > > There are several ways to get out of a loop, the most common are: > > a) To exit a single level of loop use break > > > for n in range(500): >if n == 200: break >else; pass > > b) To break out of a set of nested loops such as you have then > use break combined with a sentinal and check the sentinal at > each level: > > exitLoop = False > for x in range(20): >if exitLoop : break >for y in range(50): >if exitLoop: break >for z in range(500): >if z == 200: > exitLoop = True > break > > c) Use an exception: > > class ExitLoopException(Exception): pass > > try: > for x in range(20): >for y in range(50): >for z in range(500): >if z == 200: > raise ExitLoopException > except ExitLoopException: pass > > > There are other ways but those should cover most eventualities. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > Thanks for advices, I will try to use it that way, but first I must test it on small examples to get better know how it is working. > > -- > > Message: 2 > Date: Sat, 2 Apr 2011 18:38:12 -0500 > From: Wayne Werner > To: Alan Gauld > Cc: tutor@python.org > Subject: Re: [Tutor] 'for' iteration stop problem > Message-ID: > > Content-Type: text/plain; charset="iso-8859-1" > > On Sat, Apr 2, 2011 at 5:45 PM, Alan Gauld >wrote: > > > > > There are other ways but those should cover most eventualities. > > > I find that my preferred way when the loops are nested is to move the loops > into a function and then return: > > def do_something(collection, collection2): >for x in collection: >for y in collection 2: > if someCase(x,y): > return > > It seems (at least to me) a little nicer than sentinel values or throwing > an > exception. > > Just my $.02 > -Wayne > I've tried it that way, but I haven't succed. Perhaps I'm doing sth wrong, I will check one more time. Hi, I will answer your questions below ech one. > Message: 3 > Date: Sun, 03 Apr 2011 10:59:08 +1000 > From: Steven D'Aprano > To: tutor@python.org > Subject: Re: [Tutor] 'for' iteration stop problem > Message-ID: <4d97c65c.6070...@pearwood.info> > Content-Type: text/plain; charset=UTF-8; format=flowed > > Mateusz Koryci?ski wrote: > > I have some problem with 'for' loop in algorithm. > > Code and description for this problem could be find here: > > > http://stackoverflow.com/questions/5520145/how-to-stop-iteration-when-if-statement-is-true > > Other people have answered your question about exiting nested loops, but > that's not your real problem. Your real problem is that the piece of > code shown is unreadable and unmaintainable! > > (1) Python has functions: please use them to break your code into small, > easily understandable pieces. > > (2) You have a *mess* of unreadable variable names. You have: > > w.steps w.x w.y w.world data.c data.n data.e data.s data.w data.cc > > (yes, you have data.c AND data.cc!!!) plus loop variables z i j and r, > and z is never used! How is anyone supposed to understand what this > does? Even if you solve this problem *today*, in a week you will have > forgotten what the code does and you won't understand it. > > It's an objects from another classes. Variable z is used because there is a need to provide number of steps to execute by user. Variable c mean central cell in array whe are considering right now, other one is neighbours but cc, cc means new state (couse basing on central cell and its neighbors we want to change value to cc). > (3) w.steps is never used except to needlessly(?) repeat the same > calculations over and over again. > Described above. > (4) w.x and w.y at least seem to be fairly obvious, although the names > are terrible. > x - row numbers provided by user and in other class, which creates array (from NumPy). y - the same but cols > > (5) What are w.world and all the data.* variables? Are they "north", > "south", "east", "west"? > w.World is a NumPy 2D array, c is central cell