Re: Write this accumuator in a functional style
On Tuesday, July 11, 2017 at 4:11:50 PM UTC+5:30, Alain Ketterlin wrote: > Steven D'Aprano writes: > > > I have a colleague who is allergic to mutating data structures. Yeah, I > > know, he needs to just HTFU but I thought I'd humour him. > > > > Suppose I have an iterator that yields named tuples: > > > > Parrot(colour='blue', species='Norwegian', status='tired and shagged out') > > > > and I want to collect them by colour: > > > > accumulator = {'blue': [], 'green': [], 'red': []} > > for parrot in parrots: > > accumulator[parrot.colour].append(parrot) > > > > > > That's pretty compact and understandable, but it require mutating a bunch > > of pre-allocated lists inside an accumulator. Can we re-write this in a > > functional style? > > Here is a sketch in OCaml-style (incomplete of course): > > type color = Blue | Green | Red;; > type parrot = { c: color; ... };; > > let rec collect list_of_parrots = > match list_of_parrots with > | nil -> (nil,nil,nil) > | h :: q -> > let b,g,r = collect q in > match h with > | {c=Blue} -> (h::b,g,r) > | {c=Green} -> (b,h::g,r) > | {c=Red} -> (b,g,h::r) > ;; Separating the recursion from the pattern-match-to-discriminate [Also its in haskell since I dont have an *ML handy] Code - data Color = Red|Blue|Green deriving (Show) type Species = String type Status = String type Parrot = (Color, Species, Status) -- discriminating cons discons :: Parrot -> ([Parrot], [Parrot], [Parrot]) -> ([Parrot], [Parrot], [Parrot]) discons p@(Red,_,_) (r,g,b) = (p:r, g, b) discons p@(Green,_,_) (r,g,b) = (r, p:g, b) discons p@(Blue,_,_) (r,g,b) = (r, g, p:b) -- Loop disc :: [Parrot] -> ([Parrot], [Parrot], [Parrot]) disc = foldr discons ([],[],[]) - Run: - λ> let parrotlist = [(Blue, "norwe", "happy"), (Green, "austral", "tired"), (Red, "amer", "god-knows")] λ> disc parrotlist ([(Red,"amer","god-knows")],[(Green,"austral","tired")],[(Blue,"norwe","happy")]) λ> - Getting it into python would need a foldr (python's reduce is a foldl) There is an identity foldl op id l = foldr (flip op) id (reverse l) However for this we need the list to be a real (finite) list; not an iterator/infinite etc OTOH I suspect the spec as returning a bunch of lists is more likely to be a bunch of bags (Counter in python); in which case foldr can be replaced by foldl(reduce) -- https://mail.python.org/mailman/listinfo/python-list
Re: Write this accumuator in a functional style
On Wed, 12 Jul 2017 17:47:50 +1200, Gregory Ewing wrote: > Steve D'Aprano wrote: >> - Greg's dict comprehension version requires N+1 passes through the >> data, >> one to convert to a list, and 1 per each possible key. > > Just to be clear, my solution was a response to the requirement that it > be written in a purely functional style. It's not now I would actually > recommend doing it! I never imagined anything else! -- Steve -- https://mail.python.org/mailman/listinfo/python-list
Re: Write this accumuator in a functional style
Steven D'Aprano writes: > for parrot in parrots: > accumulator[parrot.colour].append(parrot) > > That's pretty compact and understandable, but it require mutating a bunch > of pre-allocated lists inside an accumulator. Can we re-write this in a > functional style? Not so easily in Python since the built-in list and dict types are designed for mutation update. In Haskell, the list type is a linked list and the dictionary type is a balanced tree. So, you can make a new list consisting of a new item consed to the front of the old list, and you can make a new ("updated") dictionary by building O(log n) new nodes. You might like Chris Okasaki's wonderful book "Purely Functional Data Structures" that explains all this and more. -- https://mail.python.org/mailman/listinfo/python-list
Re: Write this accumuator in a functional style
On Wed, Jul 12, 2017 at 7:52 PM, Paul Rubin wrote: > Not so easily in Python since the built-in list and dict types are > designed for mutation update. In Haskell, the list type is a linked > list and the dictionary type is a balanced tree. So, you can make a new > list consisting of a new item consed to the front of the old list, and > you can make a new ("updated") dictionary by building O(log n) new > nodes. Is that actual O(log n), or amortized? If you build a tree by forever inserting larger values (which can happen easily - imagine using a dict to record hourly statistics, using time.time()//3600 as the key), at some point it'll need to be rebalanced, which could at worst case be O(n). But I could believe that it's amortized logarithmic, although I can't myself prove that it is. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Test 0 and false since false is 0
On 12/07/17 03:29, Stefan Ram wrote: Grant Edwards writes: False is required to be a singleton. »singleton« usually means »the sole object of its class«. »Ensure a class only has one instance, and provide a global point of access to it.« - Gamma et al. We are using the term differently. We are vast, we contain multitudes, etc, etc. type( False ) type( True ) It seems, »False« is not a singleton under the implementation of Python I used. The point that was being made is that there are no other bools than True and False, and they are distinct from the objects 1 and 0. -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
class Person: def __init__(self, name, job=None, pay=0): self.name = name self.job = job self.pay = pay def lastName(self): return self.name.split()[-1] def giveRaise(self, percent): self.pay = int(self.pay * (1 + percent)) def __repr__(self): return '[Person: %s, %s]' % (self.name, self.pay) class Manager(Person): def giveraise(self, percent, bonus=.10): Person.giveRaise(self, percent + bonus) if __name__ == '__main__': #self-test code bob = Person('Bob Smith') sue = Person('Sue Jones', job='dev', pay=10) print(bob) print(sue) print(bob.lastName(), sue.lastName()) sue.giveRaise(.10) print(sue.pay) tom = Manager('Tom Jones', 'mgr', 5) tom.giveRaise(.10) print(tom.lastName()) print(tom) -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
Please COPY AND PASTE the FULL error, starting with the line "Traceback". The code you show below looks fine, and you don't need an import, so I don't know what error you are getting. On Wed, 12 Jul 2017 10:31 pm, lunkamba...@gmail.com wrote: > class Person: > def __init__(self, name, job=None, pay=0): > self.name = name > self.job = job > self.pay = pay > def lastName(self): > return self.name.split()[-1] > def giveRaise(self, percent): > self.pay = int(self.pay * (1 + percent)) > def __repr__(self): > return '[Person: %s, %s]' % (self.name, self.pay) > class Manager(Person): > def giveraise(self, percent, bonus=.10): > Person.giveRaise(self, percent + bonus) > > if __name__ == '__main__': > #self-test code > bob = Person('Bob Smith') > sue = Person('Sue Jones', job='dev', pay=10) > print(bob) > print(sue) > print(bob.lastName(), sue.lastName()) > sue.giveRaise(.10) > print(sue.pay) > tom = Manager('Tom Jones', 'mgr', 5) > tom.giveRaise(.10) > print(tom.lastName()) > print(tom) -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
On Wednesday, 12 July 2017 18:20:32 UTC+5:30, Steve D'Aprano wrote: > Please COPY AND PASTE the FULL error, starting with the line "Traceback". > > The code you show below looks fine, and you don't need an import, so I don't > know what error you are getting. > > > On Wed, 12 Jul 2017 10:31 pm, lunkamba...@gmail.com wrote: > > > class Person: > > def __init__(self, name, job=None, pay=0): > > self.name = name > > self.job = job > > self.pay = pay > > def lastName(self): > > return self.name.split()[-1] > > def giveRaise(self, percent): > > self.pay = int(self.pay * (1 + percent)) > > def __repr__(self): > > return '[Person: %s, %s]' % (self.name, self.pay) > > class Manager(Person): > > def giveraise(self, percent, bonus=.10): > > Person.giveRaise(self, percent + bonus) > > > > if __name__ == '__main__': > > #self-test code > > bob = Person('Bob Smith') > > sue = Person('Sue Jones', job='dev', pay=10) > > print(bob) > > print(sue) > > print(bob.lastName(), sue.lastName()) > > sue.giveRaise(.10) > > print(sue.pay) > > tom = Manager('Tom Jones', 'mgr', 5) > > tom.giveRaise(.10) > > print(tom.lastName()) > > print(tom) > > -- > Steve > “Cheer up,” they said, “things could be worse.” So I cheered up, and sure > enough, things got worse. i tried from idle interpreter from person import Manager >>> from person import Manager Traceback (most recent call last): File "", line 1, in from person import Manager ImportError: cannot import name 'Manager' and this also >>> import person >>> tom = Manager('parias lunkamaba', 'mgr', 50) then i get this Traceback (most recent call last): File "", line 1, in tom=Manager('Tome Jones', 'mgr', 5) NameError: name 'Manager' is not defined -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
On Wednesday, 12 July 2017 18:20:32 UTC+5:30, Steve D'Aprano wrote: > Please COPY AND PASTE the FULL error, starting with the line "Traceback". > > The code you show below looks fine, and you don't need an import, so I don't > know what error you are getting. > > > On Wed, 12 Jul 2017 10:31 pm, lunkamba...@gmail.com wrote: > > > class Person: > > def __init__(self, name, job=None, pay=0): > > self.name = name > > self.job = job > > self.pay = pay > > def lastName(self): > > return self.name.split()[-1] > > def giveRaise(self, percent): > > self.pay = int(self.pay * (1 + percent)) > > def __repr__(self): > > return '[Person: %s, %s]' % (self.name, self.pay) > > class Manager(Person): > > def giveraise(self, percent, bonus=.10): > > Person.giveRaise(self, percent + bonus) > > > > if __name__ == '__main__': > > #self-test code > > bob = Person('Bob Smith') > > sue = Person('Sue Jones', job='dev', pay=10) > > print(bob) > > print(sue) > > print(bob.lastName(), sue.lastName()) > > sue.giveRaise(.10) > > print(sue.pay) > > tom = Manager('Tom Jones', 'mgr', 5) > > tom.giveRaise(.10) > > print(tom.lastName()) > > print(tom) > > -- > Steve > “Cheer up,” they said, “things could be worse.” So I cheered up, and sure > enough, things got worse. only in python 3.5.3 -- https://mail.python.org/mailman/listinfo/python-list
botlib - framework to program bots
BOTLIB - Framework to program bots is released in the Public Domain - https://lnkd.in/ginB49K #publicdomain #python3 #xmpp #irc #bot Framework to program bots. -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
WoFy The 95s wrote: > i tried from idle interpreter > > from person import Manager > > > from person import Manager > Traceback (most recent call last): > File "", line 1, in > from person import Manager > ImportError: cannot import name 'Manager' Enter import person person.__file__ in idle's shell. There may be another file called person.py which is imported instead of the one you intended. > and this also import person > tom = Manager('parias lunkamaba', 'mgr', 50) > > then i get this > > > Traceback (most recent call last): > File "", line 1, in > tom=Manager('Tome Jones', 'mgr', 5) > NameError: name 'Manager' is not defined This is standard behaviour. If after import person you want to access a name in the person module you have to use the qualified name, e. g. tom = person.Manager('parias lunkamaba', 'mgr', 50) -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
On Wednesday, 12 July 2017 18:57:11 UTC+5:30, Peter Otten wrote: > WoFy The 95s wrote: > > > i tried from idle interpreter > > > > from person import Manager > > > > > > > from person import Manager > > Traceback (most recent call last): > > File "", line 1, in > > from person import Manager > > ImportError: cannot import name 'Manager' > > > Enter > > import person > person.__file__ > > in idle's shell. There may be another file called person.py which is > imported instead of the one you intended. > > > > and this also > import person > > > tom = Manager('parias lunkamaba', 'mgr', 50) > > > > then i get this > > > > > > Traceback (most recent call last): > > File "", line 1, in > > tom=Manager('Tome Jones', 'mgr', 5) > > NameError: name 'Manager' is not defined > > This is standard behaviour. If after > > import person > > you want to access a name in the person module you have to use the qualified > name, e. g. > > tom = person.Manager('parias lunkamaba', 'mgr', 50) >>>import person >>>tom = person.Manager('Parias lunkamba', 'mgr', 50) >>>Traceback (most recent call last): File "", line 1, in tom = person.Manager('parias lunkamba', 'mgr', 50) AttributeError: module 'person' has no attribute 'Manager' why the module in python 3.5 doesn't recognize the Manager class? -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
WoFy The 95s wrote: > On Wednesday, 12 July 2017 18:57:11 UTC+5:30, Peter Otten wrote: >> WoFy The 95s wrote: >> >> > i tried from idle interpreter >> > >> > from person import Manager >> > >> > >> > >> from person import Manager >> > Traceback (most recent call last): >> > File "", line 1, in >> > from person import Manager >> > ImportError: cannot import name 'Manager' >> >> >> Enter >> >> import person >> person.__file__ >> >> in idle's shell. There may be another file called person.py which is >> imported instead of the one you intended. >> >> >> > and this also >> import person >> > >> tom = Manager('parias lunkamaba', 'mgr', 50) >> > >> > then i get this >> > >> > >> > Traceback (most recent call last): >> > File "", line 1, in >> > tom=Manager('Tome Jones', 'mgr', 5) >> > NameError: name 'Manager' is not defined >> >> This is standard behaviour. If after >> >> import person >> >> you want to access a name in the person module you have to use the >> qualified name, e. g. >> >> tom = person.Manager('parias lunkamaba', 'mgr', 50) > > > import person tom = person.Manager('Parias lunkamba', 'mgr', 50) Traceback (most recent call last): > File "", line 1, in > tom = person.Manager('parias lunkamba', 'mgr', 50) > AttributeError: module 'person' has no attribute 'Manager' > > why the module in python 3.5 doesn't recognize the Manager class? Again, you probably have two files called person.py, and the one you import does not contain a Manager class. (Different directories in sys.path can explain why different person.py files will be imported in differing python versions) As I said, you can find out the file's location with >>> person.__file__ or have a look at the module content with >>> import inspect >>> import person >>> print(inspect.getsource(person)) -- https://mail.python.org/mailman/listinfo/python-list
Re: python 3.5 raiaing an error when import the class Manager in this module sayning name Manager is not define
On Wednesday, 12 July 2017 18:01:35 UTC+5:30, WoFy The 95s wrote: > class Person: > def __init__(self, name, job=None, pay=0): > self.name = name > self.job = job > self.pay = pay > def lastName(self): > return self.name.split()[-1] > def giveRaise(self, percent): > self.pay = int(self.pay * (1 + percent)) > def __repr__(self): > return '[Person: %s, %s]' % (self.name, self.pay) > class Manager(Person): > def giveraise(self, percent, bonus=.10): > Person.giveRaise(self, percent + bonus) > > if __name__ == '__main__': > #self-test code > bob = Person('Bob Smith') > sue = Person('Sue Jones', job='dev', pay=10) > print(bob) > print(sue) > print(bob.lastName(), sue.lastName()) > sue.giveRaise(.10) > print(sue.pay) > tom = Manager('Tom Jones', 'mgr', 5) > tom.giveRaise(.10) > print(tom.lastName()) > print(tom) i removed some space and it worked thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Re: Test 0 and false since false is 0
On 7/12/2017 7:35 AM, Rhodri James wrote: On 12/07/17 03:29, Stefan Ram wrote: Grant Edwards writes: False is required to be a singleton. »singleton« usually means »the sole object of its class«. »Ensure a class only has one instance, and provide a global point of access to it.« - Gamma et al. type( False ) type( True ) It seems, »False« is not a singleton under the implementation of Python I used. The point that was being made is that there are no other bools than True and False, and they are distinct from the objects 1 and 0. By analogy with 'singleton', True and False constitute a 'doubleton' in the sense of being the sole 2 objects of class Bool. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Better Regex and exception handling for this small code
On Wednesday, 12 July 2017 02:32:29 UTC+10, Ganesh Pal wrote: > Dear Python friends > > I am trying to open a file and check if there is a pattern has changed > after the task got completed? > > file data: > > > #tail -f /file.txt > .. > Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = b6b20a65, > journal_crc = d2097b00 > Note: Task completed successfully. > Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = d976d35e, > journal_crc = a176af10 > > > I have the below piece of code but would like to make this better more > pythonic , I found regex pattern and exception handling poor here , any > quick suggestion in your spare time is welcome. > > > #open the existing file if the flag is set and check if there is a match > > log_file='/file.txt' > flag_is_on=1 > > data = None > with open(log_file, 'r') as f: > data = f.readlines() > > > if flag_is_on: > logdata = '\n'.join(data) > reg = "initiator_crc =(?P[\s\S]*?), journal_crc" > crc = re.findall(re.compile(reg), logdata) > if not crc: > raise Exception("Pattern not found in logfile") > > checksumbefore = crc[0].strip() > checksumafter = crc[1].strip() > logging.info("checksumbefore :%s and checksumafter:%s" > % (checksumbefore, checksumafter)) > > if checksumbefore == checksumafter: >raise Exception("checksum not macthing") > > I am on Linux and Python 2.7 > > Regards, > Ganesh There's not much need to compile regexes unless you've got *a lot* of them in your code. The first ones are automatically compiled and cached: https://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile Cheers, Nick -- https://mail.python.org/mailman/listinfo/python-list
Re: Better Regex and exception handling for this small code
On 2017-07-12 23:49, Nick Mellor wrote: On Wednesday, 12 July 2017 02:32:29 UTC+10, Ganesh Pal wrote: Dear Python friends I am trying to open a file and check if there is a pattern has changed after the task got completed? file data: #tail -f /file.txt .. Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = b6b20a65, journal_crc = d2097b00 Note: Task completed successfully. Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = d976d35e, journal_crc = a176af10 I have the below piece of code but would like to make this better more pythonic , I found regex pattern and exception handling poor here , any quick suggestion in your spare time is welcome. #open the existing file if the flag is set and check if there is a match log_file='/file.txt' flag_is_on=1 data = None with open(log_file, 'r') as f: data = f.readlines() if flag_is_on: logdata = '\n'.join(data) reg = "initiator_crc =(?P[\s\S]*?), journal_crc" crc = re.findall(re.compile(reg), logdata) if not crc: raise Exception("Pattern not found in logfile") checksumbefore = crc[0].strip() checksumafter = crc[1].strip() logging.info("checksumbefore :%s and checksumafter:%s" % (checksumbefore, checksumafter)) if checksumbefore == checksumafter: raise Exception("checksum not macthing") I am on Linux and Python 2.7 Regards, Ganesh There's not much need to compile regexes unless you've got *a lot* of them in your code. The first ones are automatically compiled and cached: https://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile I think this is the first time that I've seen someone pass a compiled pattern into re.findall. The usual way is to pass the pattern as a string: crc = re.findall(reg, logdata) If you have a lot of them, or it's in a loop that'll iterate many times, it'll be quicker if you compile it first (outside the loop): pattern = re.compile(reg) and then use the compiled pattern's .findall method: crc = pattern.findall(logdata) -- https://mail.python.org/mailman/listinfo/python-list
pythonpath in ipython console
hello, I am a bit confused, i use spyder, when i execute in ipython console program start fails with message 'Attribute error' when I start same program via python console everything works fine, even start from terminal workes fine. It seems that i python does not load Pythonpath, although wdir= set to execution path and all the modules are stored in that directory. How can I avoid this error? cheers -- https://mail.python.org/mailman/listinfo/python-list
No pip3.5 bin after install python3.5.1 from source code
Hi all: I installed python3.5.1 from source code, but found strange events. on RHEL6, before "make install", if I run "yum -y install openssl openssl-devel" even though the two lib is installed, then the pip bin would installed, else not installed; on SLES11 SP3, even though I run "rpm -i libopenssl-devel-0.9.8j-2.1.x86_64.rpm" before "make install", pip bin still can't be installed, but after install python3, I downloaded source code of pip 7.1.2, and also installed pip from source, and it is installed successfully. Why? Any have met the same problem? Thanks Dahui -- https://mail.python.org/mailman/listinfo/python-list