From lac at openend.se Tue Sep 1 07:31:02 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 01 Sep 2015 07:31:02 +0200 Subject: [Tutor] Query regarding loop problem In-Reply-To: References: <201508300938.t7U9c9cw020809@fido.openend.se> <201508301755.t7UHtD4M002241@fido.openend.se> <201508302252.t7UMqpZD003518@fido.openend.se> Message-ID: <201509010531.t815V2r3028598@fido.openend.se> I have been told that in gmail it is simple, but far from obvious to get your mail to open so you add to the bottom. There are three tiny dots which conceal the previous text. Click on that, and then add below. I am also told that if you open your mail message with 'control A' (hold the control key down and press A) this will also work. So we can see if that works ... (Thanks to Rustom Mody, who as a gmail user ought to know such things. :) ) Laura From 261100nc at gmail.com Tue Sep 1 15:07:51 2015 From: 261100nc at gmail.com (Nathan Clark) Date: Tue, 1 Sep 2015 14:07:51 +0100 Subject: [Tutor] problem with code In-Reply-To: References: Message-ID: I have written another basic program out of python, please could you instruct me on how to fix it question= input("What is your question) print("Let me think about that") time.sleep(10) #10 seconds wait print (question) print ("lol") On Thu, Aug 20, 2015 at 4:08 PM, Job wrote: > #i would do it this way in python2 > #maybe you can use as reference > > time = int(raw_input('x')) > > If time => 2: > print 'y' > else: > print 'z' > > Good luck, > > Job > > > On Aug 20, 2015, at 2:50 AM, Nathan Clark <261100nc at gmail.com> wrote: > > > > I have written a basic program out of python and it is not functioning, > > please could you proof read my code and tell me how to fix it.It is in > > python 3.3 > > > > time=int(input("How long on average do you spend on the computer per > day?") > > (print("that seems reasonable")) if time<=2 > > else print ("get a life") > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Tue Sep 1 15:22:09 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 1 Sep 2015 23:22:09 +1000 Subject: [Tutor] problem with code In-Reply-To: References: Message-ID: <20150901132209.GF19373@ando.pearwood.info> On Tue, Sep 01, 2015 at 02:07:51PM +0100, Nathan Clark wrote: > I have written another basic program out of python, please could you > instruct me on how to fix it I assume you are using Python 3, is that correct? > question= input("What is your question) > print("Let me think about that") > time.sleep(10) #10 seconds wait > print (question) > print ("lol") Indentation is significant in Python, so to fix this, you need to align your code to the left-hand margin. You also need to import the time module. Lastly, you need to fix the SyntaxError on line 1. You have a string with an opening quote, but no closing quote: "What is your question Add a closing quote (and a question mark). import time question= input("What is your question?") print("Let me think about that") time.sleep(10) #10 seconds wait print (question) print ("lol") -- Steve From alan.gauld at btinternet.com Tue Sep 1 16:07:18 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 01 Sep 2015 15:07:18 +0100 Subject: [Tutor] problem with code In-Reply-To: References: Message-ID: On 01/09/15 14:07, Nathan Clark wrote: > I have written another basic program out of python, please could you > instruct me on how to fix it > > question= input("What is your question) > print("Let me think about that") > time.sleep(10) #10 seconds wait > print (question) > print ("lol") Please, always include the full error message you get. Firstly it helps us identify the problem without having to copy your code or study it in detail. Secondly it allows us to show you how to use these messages to fix your own problems without having to post a mail each time. Steven has taken the time to analyze your code in this case but please, in future, include the error reports. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From marcus.luetolf at bluewin.ch Thu Sep 3 15:32:52 2015 From: marcus.luetolf at bluewin.ch (=?UTF-8?B?TWFyY3VzIEzDvHRvbGY=?=) Date: Thu, 03 Sep 2015 15:32:52 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions Message-ID: <55E84C04.2010200@bluewin.ch> dear pythonistats as a newcomber I want to create a set of lists containing n items, for example n = 3: (['a','b','c'], ['a','d','e'].......). The sequence of items in each list should be different. If the letters 'a'........'z' are used and n = 3 there is a maximum of 301 lists. The following code works only for lists containing 1 item: import random list = ['a', 'b', 'c', 'd',....... 'z'] random.shuffle(list) for x in list: print x how can I solve my task wit n items ? Thank you for help, Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From __peter__ at web.de Thu Sep 3 17:39:43 2015 From: __peter__ at web.de (Peter Otten) Date: Thu, 03 Sep 2015 17:39:43 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions References: <55E84C04.2010200@bluewin.ch> Message-ID: Marcus L?tolf wrote: > as a newcomber I want to create a set of lists containing n items, for > example n = 3: (['a','b','c'], ['a','d','e'].......). > The sequence of items in each list should be different. If the letters > 'a'........'z' are used and n = 3 there is a maximum of 301 lists. > The following code works only for lists containing 1 item: > > import random > list = ['a', 'b', 'c', 'd',....... 'z'] > random.shuffle(list) > for x in list: > print x > > how can I solve my task wit n items ? At first I thought you might want itertools.combinations() >>> import string, itertools >>> for t in itertools.combinations(string.ascii_lowercase, 3): ... print t # list(t) if you actually need a list ... ('a', 'b', 'c') ('a', 'b', 'd') ('a', 'b', 'e') ('a', 'b', 'f') ('a', 'b', 'g') [snip] but that gives >>> sum(1 for t in itertools.combinations(string.ascii_lowercase, 3)) 2600 2600 different tuples Can you give more details on how to pick the lists? From martin at linux-ip.net Thu Sep 3 19:43:55 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 3 Sep 2015 10:43:55 -0700 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <55E84C04.2010200@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> Message-ID: Greetings Marcus, Peter Otten has also responded, recommending itertools. I think both he and I are not sure how you wish to generate your result list (or lists). But, I have a comment or two. > dear pythonistats > as a newcomber I want to create a set of lists containing n items, for > example n = 3: (['a','b','c'], ['a','d','e'].......). > The sequence of items in each list should be different. If the letters > 'a'........'z' are used and n = 3 there is a maximum of 301 lists. > The following code works only for lists containing 1 item: > > import random > list = ['a', 'b', 'c', 'd',....... 'z'] I would recommend against using the name "list". The name list is a Python builtin which creates a list(). Try it at the interactive prompt: >>> list() [] So, I'd recommend changing that name to "l" or "result" or something like that. > random.shuffle(list) > for x in list: > print x > > how can I solve my task wit n items ? > Thank you for help, Marcus. You are using random. Do you want n randomly selected items from the input list? The random module provides random.sample() to select n items from a sequence. If so, try this out at the intercative prompt. Perhaps this is what you are looking for? >>> import random >>> import string >>> l = list(string.lowercase) >>> random.sample(l, 7) ['z', 'h', 'e', 'n', 'c', 'f', 'r'] Best of luck, -Martin -- Martin A. Brown http://linux-ip.net/ From breamoreboy at yahoo.co.uk Thu Sep 3 21:09:18 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 3 Sep 2015 20:09:18 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <55E84C04.2010200@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> Message-ID: On 03/09/2015 14:32, Marcus L?tolf wrote: > dear pythonistats > as a newcomber I want to create a set of lists containing n items, for > example n = 3: (['a','b','c'], ['a','d','e'].......). > The sequence of items in each list should be different. If the letters > 'a'........'z' are used and n = 3 there is a maximum of 301 lists. The formula for combinations is n! / (r! * (n - r)!) = 26! / (3! * 23!) = 26 * 25 * 24 / (3 * 2 * 1) = 2600, the number given by Peter Otten in his earlier answer using itertools.combinations. The number for permutations is n! / (n - k)! = 26! / 23! = 26 * 25 * 24 = 15,600. So what criteria are you using to give 301? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From dyoo at hashcollision.org Thu Sep 3 21:44:29 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 3 Sep 2015 12:44:29 -0700 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> Message-ID: > At first I thought you might want itertools.combinations() > >>>> import string, itertools >>>> for t in itertools.combinations(string.ascii_lowercase, 3): > ... print t # list(t) if you actually need a list > ... > ('a', 'b', 'c') > ('a', 'b', 'd') > ('a', 'b', 'e') > ('a', 'b', 'f') > ('a', 'b', 'g') > [snip] > > but that gives > >>>> sum(1 for t in itertools.combinations(string.ascii_lowercase, 3)) > 2600 At the very least, that matches the expected result mathematically, since the number of combinations of 3 elements out of 26 possibilities is "26 choose 3". http://mathworld.wolfram.com/Combination.html and "26 choose 3" computes to 2600. ####################### >>> 26 * 25 * 24 / (3 * 2 * 1) 2600 ####################### From nymcity at yahoo.com Fri Sep 4 03:27:59 2015 From: nymcity at yahoo.com (Nym City) Date: Fri, 4 Sep 2015 01:27:59 +0000 (UTC) Subject: [Tutor] Syntax error and EOL Error Message-ID: <1930837628.1270400.1441330079990.JavaMail.yahoo@mail.yahoo.com> Hello, I am working with a csv file that has following sample data: Rank??? URL??? Linking Root Domains 1??? facebook.com/??? 9616487 2??? twitter.com/??? 6454936 3??? google.com/??? 5868081 4??? youtube.com/??? 5442206 5??? wordpress.org/??? 4051288 In my program, I am reading in this csv file, taking only data from the second column and striping off the leading "/" import csv DomainList = [] domains = open('domainlist.csv', 'r') DomainList = csv.reader(domains) DomainList = [column[1] for column in DomainList] DomainList = (str(DomainList).rstrip('/') print('\n'.join(DomainList)) I keep getting EOL error on the second the last line and syntax error on the last print line. Even when I change the print line to say the following: print(DomainList) Please advise. Thank you in advance. ?Thank you. From __peter__ at web.de Fri Sep 4 09:23:12 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 04 Sep 2015 09:23:12 +0200 Subject: [Tutor] Syntax error and EOL Error References: <1930837628.1270400.1441330079990.JavaMail.yahoo@mail.yahoo.com> Message-ID: Nym City via Tutor wrote: > import csv > DomainList = [] > > domains = open('domainlist.csv', 'r') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList] > DomainList = (str(DomainList).rstrip('/') > print('\n'.join(DomainList)) > > > I keep getting EOL error on the second the last line and syntax error on > the last print line. Even when I change the print line to say the > following: print(DomainList) Please advise. Thank you in advance. > Thank you. Look at the lines preceding the one triggering the SyntaxError. Usually there is an opening (, [ or { which doesn't have a matching closing counterpart. In your case count the parens in the line > DomainList = (str(DomainList).rstrip('/') By the way, even without error this line will not have the desired effect. Given >>> DomainList = ["facebook.com/", "twitter.com/"] converting to string gives >>> str(DomainList) "['facebook.com/', 'twitter.com/']" and as that doesn't end with a "/" applying the rstrip() method has no effect. Instead you can modify the line > DomainList = [column[1] for column in DomainList] where you can invoke the rstrip() method on every string in the list. From marcus.luetolf at bluewin.ch Fri Sep 4 16:28:10 2015 From: marcus.luetolf at bluewin.ch (=?UTF-8?Q?marcus_l=C3=BCtolf?=) Date: Fri, 4 Sep 2015 16:28:10 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> Message-ID: <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Hello Peter, hello Martin, many thanks for your very quick response !!! As for Peter's advice: > At first I thought you might want itertools.combinations() > >>>> import string, itertools >>>> for t in itertools.combinations(string.ascii_lowercase, 3): > ... print t # list(t) if you actually need a list > ... > ('a', 'b', 'c') > ('a', 'b', 'd') > ('a', 'b', 'e') > ('a', 'b', 'f') > ('a', 'b', 'g') > [snip] > > but that gives > >>>> sum(1 for t in itertools.combinations(string.ascii_lowercase, 3)) > 2600 the 5 lists above do not match my task insofar as every of the 5 lists contains 'a' and 'b' which should occur only once, hence my count of a maximum of 301 lists, which might nor be correct 100%. My be one could put it in Python as follows: > ('a', 'b', 'c') = True > ('a', 'b', 'd')= False > ('a', 'b', 'e')= False > ('a', 'b', 'f')= False > ('a', 'b', 'g')= False I should probably tell you the real task are a series (maximum ~ 301) lists in which real names of people are assigned to the items/letters for 2 people(golfers) can be in the same list(flight) only once for an extended period of time. The next step would be to assign compatible and noncompatible attributes to the items/letters which will reduce the maximum of possible lists(flights) As for Martin's advice: You are using random. Do you want n randomly selected items from the input list? The random module provides random.sample() to select n items from a sequence. If so, try this out at the intercative prompt. Perhaps this is what you are looking for? >>> import random >>> import string >>> l = list(string.lowercase) >>> random.sample(l, 7) ['z', 'h', 'e', 'n', 'c', 'f', 'r'] The two modules random and string should provide 7 ore more sets of 3 items not just one (letter ['z',..................'r']) like ['z', 'a', 'b'], ['h','a','c'], ['e','a','d'], ['n','a','f'],.........['r','a','g']. I hope I could make myself clear(er) and used the appropriate format to communicate. Thanks again for your help, Marcus ........................................................................................................................................................................... -----Urspr?ngliche Nachricht----- Von: marcus.luetolf at bluewin.ch [mailto:marcus.luetolf at bluewin.ch] Gesendet: Freitag, 4. September 2015 15:27 An: marcus.luetolf at bluewin.ch Betreff: Fwd: Creating lists with definite (n) items without repetitions ----Urspr?ngliche Nachricht---- Von : marcus.luetolf at bluewin.ch Datum : 03/09/2015 - 15:32 (UTC) An : tutor at python.org Betreff : Creating lists with definite (n) items without repetitions dear pythonistas as a newcomber I want to create a set of lists containing n items, for example n = 3: (['a','b','c'], ['a','d','e'].......). The sequence of items in each list should be different. If the letters 'a'........'z' are used and n = 3 there is a maximum of 301 lists. The following code works only for lists containing 1 item: import random list = ['a', 'b', 'c', 'd',....... 'z'] random.shuffle(list) for x in list: print x how can I solve my task wit n items ? Thank you for help, Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From __peter__ at web.de Sat Sep 5 11:09:42 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 05 Sep 2015 11:09:42 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: marcus l?tolf wrote: > Hello Peter, hello Martin, > many thanks for your very quick response !!! > > As for Peter's advice: > >> At first I thought you might want itertools.combinations() >> >>>>> import string, itertools >>>>> for t in itertools.combinations(string.ascii_lowercase, 3): >> ... print t # list(t) if you actually need a list >> ... >> ('a', 'b', 'c') >> ('a', 'b', 'd') >> ('a', 'b', 'e') >> ('a', 'b', 'f') >> ('a', 'b', 'g') >> [snip] >> >> but that gives >> >>>>> sum(1 for t in itertools.combinations(string.ascii_lowercase, 3)) >> 2600 > > the 5 lists above do not match my task insofar as every of the 5 lists > contains 'a' and 'b' which should occur only once, hence my count of a > maximum of 301 lists, which might nor be correct 100%. My be one could put > it in Python as follows: >> ('a', 'b', 'c') = True >> ('a', 'b', 'd')= False >> ('a', 'b', 'e')= False >> ('a', 'b', 'f')= False >> ('a', 'b', 'g')= False > I should probably tell you the real task are a series (maximum ~ 301) > lists in which real names of people are assigned to the items/letters for > 2 people(golfers) can be in the same list(flight) only once for an > extended period of time. OK, you want all triples where no pair inside a triple is repeated. There are 325 pairs, and one triple uses up three pairs. That puts an upper limit of 325/3 = 108 on the number of triples. You then have to find all sets of pairs where the union contains My attempts to solve this analytically have failed so far, therefore I wrote a script that checks random triples for the above condition: $ cat all_teams_101.py import random import sys import textwrap from itertools import combinations, count from string import ascii_lowercase if len(sys.argv) > 1: random.seed(int(sys.argv[1])) def shuffled(items): items = list(items) random.shuffle(items) return items triples = list(combinations(ascii_lowercase, 3)) best_teams = [] best_shuffled = None try: for i in count(): seen_pairs = set() teams = [] shuffled_teams = shuffled(triples) for team in shuffled_teams: team_pairs = [ frozenset(t) for t in [team[:2], team[1:], team[::2]] ] if all((pair not in seen_pairs) for pair in team_pairs): seen_pairs.update(team_pairs) teams.append(team) if len(teams) > len(best_teams): print(i, len(teams)) best_teams = teams best_shuffled = shuffled_teams except KeyboardInterrupt: print() print(textwrap.fill(" ".join(map("".join, best_teams)))) $ python3 all_teams_101.py 42 0 97 55 98 220 100 561 101 ^C bcq aoy nrz bhs gpx cjt cmu cnw fnq tvx cix afx bpv dhm hno dpy hil cfy jlv cdg mnt bln htw aiu fhp jsw qru gkq egl gtu lqx ars kwz emr inp jky kmp uwy gow hjx aep aqt kns bft biw ops efs ghr mov fgi lms dnu bjm lyz cek fkv gmy dev qsy dlt dqw huz bdr nxy eiy dsz mxz flr acz klu adk csv ehq bez eju foz avw iko jpq ewx gjz bkx prw abg qvz krt fmw dfj jor irv ptz ajn dox sux imq clo eot ist bou gnv hvy $ It's probably a good idea to ask your question in a mathematics forum -- this problem looks common enough to have a name and some brain power spent on it. (Google found https://en.wikipedia.org/wiki/Kirkman's_schoolgirl_problem which looks similar; maybe someone here can make sense of it) > The next step would be to assign compatible and noncompatible attributes > to the items/letters which will reduce the maximum of possible > lists(flights) From __peter__ at web.de Sat Sep 5 11:27:02 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 05 Sep 2015 11:27:02 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: Peter Otten wrote: [proofreading goof] > OK, you want all triples where no pair inside a triple is repeated. > There are 325 pairs, and one triple uses up three pairs. > That puts an upper limit of 325/3 = 108 on the number of triples. > You then have to find all sets of three > pairs where the union contains ... three letters. From zebra05 at gmail.com Sat Sep 5 15:06:25 2015 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Sat, 5 Sep 2015 15:06:25 +0200 Subject: [Tutor] 2 vs 3 Message-ID: Hi all, A colleague and I are embarking on a project for a client. We have agreed to implement a REST API in Falcon (www.falconframework.org) and colleague wants to implement it in Python 3. Are there any advantages/ disadvantages of using Python 3? I have always stuck to Python 2. -- Kind regards, Sithu Lloyd Dube From steve at pearwood.info Sat Sep 5 15:25:41 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 5 Sep 2015 23:25:41 +1000 Subject: [Tutor] 2 vs 3 In-Reply-To: References: Message-ID: <20150905132540.GT19373@ando.pearwood.info> On Sat, Sep 05, 2015 at 03:06:25PM +0200, Sithembewena Lloyd Dube wrote: > Hi all, > > A colleague and I are embarking on a project for a client. We have agreed > to implement a REST API in Falcon (www.falconframework.org) and colleague > wants to implement it in Python 3. > > Are there any advantages/ disadvantages of using Python 3? I have always > stuck to Python 2. Advantages of Python 2: ======================= - More tutorials and websites about Python 2. - Slightly faster for some operations. - Some operations with mixed Unicode text + bytes succeed without raising an exception; the result isn't what you want, but if you are the sort of programmer who thinks that it is better to get the wrong answer with no error instead of an error, you might think this is an advantage. - Some libraries are available for Python 2 but not 3 yet. Advantages of Python 3: ======================= - All on-going development of the language and new features will go into Python 3. - Cleaner, more consistent language: only one type of class, not two; no longer has the proliferation of similar dictionary methods (e.g. keys, iterkeys, viewkeys); std lib is organised a bit better. - Useful new features that Python 2 does not have, such as Enums, asyncio, pathlib, and others. - __del__ finalizer methods are easier and safer now. - Cleaner separation between Unicode text and bytes. If you accidently mix them together, you get an exception, instead of Python trying to guess what result you might want. - More efficient and cleaner handling of .pyc files. If you decide to use Python 3, 3.3 should be the oldest version you consider. 3.4 has many improvements and you should consider using that. -- Steve From breamoreboy at yahoo.co.uk Sat Sep 5 17:37:57 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 5 Sep 2015 16:37:57 +0100 Subject: [Tutor] 2 vs 3 In-Reply-To: <20150905132540.GT19373@ando.pearwood.info> References: <20150905132540.GT19373@ando.pearwood.info> Message-ID: On 05/09/2015 14:25, Steven D'Aprano wrote: > On Sat, Sep 05, 2015 at 03:06:25PM +0200, Sithembewena Lloyd Dube wrote: >> Hi all, >> >> A colleague and I are embarking on a project for a client. We have agreed >> to implement a REST API in Falcon (www.falconframework.org) and colleague >> wants to implement it in Python 3. >> >> Are there any advantages/ disadvantages of using Python 3? I have always >> stuck to Python 2. > > If you decide to use Python 3, 3.3 should be the oldest version you > consider. 3.4 has many improvements and you should consider using that. > People should be aware that Python 3.5 is scheduled for full release on September 13, 2015, see https://www.python.org/dev/peps/pep-0478/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From zebra05 at gmail.com Sat Sep 5 21:03:04 2015 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Sat, 5 Sep 2015 21:03:04 +0200 Subject: [Tutor] 2 vs 3 In-Reply-To: References: <20150905132540.GT19373@ando.pearwood.info> Message-ID: @Steven, @Mark, Thanks for the feedback. On Sat, Sep 5, 2015 at 5:37 PM, Mark Lawrence wrote: > On 05/09/2015 14:25, Steven D'Aprano wrote: > >> On Sat, Sep 05, 2015 at 03:06:25PM +0200, Sithembewena Lloyd Dube wrote: >> >>> Hi all, >>> >>> A colleague and I are embarking on a project for a client. We have agreed >>> to implement a REST API in Falcon (www.falconframework.org) and >>> colleague >>> wants to implement it in Python 3. >>> >>> Are there any advantages/ disadvantages of using Python 3? I have always >>> stuck to Python 2. >>> >> >> If you decide to use Python 3, 3.3 should be the oldest version you >> consider. 3.4 has many improvements and you should consider using that. >> >> > People should be aware that Python 3.5 is scheduled for full release on > September 13, 2015, see https://www.python.org/dev/peps/pep-0478/ > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Kind regards, Sithu Lloyd Dube From breamoreboy at yahoo.co.uk Sun Sep 6 00:28:49 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 5 Sep 2015 23:28:49 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: On 05/09/2015 10:09, Peter Otten wrote: > marcus l?tolf wrote: > >> Hello Peter, hello Martin, >> many thanks for your very quick response !!! >> >> As for Peter's advice: >> >> the 5 lists above do not match my task insofar as every of the 5 lists >> contains 'a' and 'b' which should occur only once, hence my count of a >> maximum of 301 lists, which might nor be correct 100%. My be one could put >> it in Python as follows: >>> ('a', 'b', 'c') = True >>> ('a', 'b', 'd')= False >>> ('a', 'b', 'e')= False >>> ('a', 'b', 'f')= False >>> ('a', 'b', 'g')= False So for completeness it follows that:- ('a', 'c', 'd') == False ('b', 'c', 'd') == False yes? >> I should probably tell you the real task are a series (maximum ~ 301) >> lists in which real names of people are assigned to the items/letters for >> 2 people(golfers) can be in the same list(flight) only once for an >> extended period of time. > > It's probably a good idea to ask your question in a mathematics forum -- > this problem looks common enough to have a name and some brain power spent > on it. > To me this is clearly an example of a Steiner Triple system associated with Balanced Incomplete Block Design. Which means I found this http://mathforum.org/library/drmath/view/52263.html which got me to https://en.wikipedia.org/wiki/Steiner_system and also http://oeis.org/A137348/a137348.txt. Just one minor little question, am I actually correct? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Sun Sep 6 00:33:02 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 05 Sep 2015 23:33:02 +0100 Subject: [Tutor] 2 vs 3 In-Reply-To: References: Message-ID: On 05/09/15 14:06, Sithembewena Lloyd Dube wrote: > A colleague and I are embarking on a project for a client. We have agreed > to implement a REST API in Falcon (www.falconframework.org) and colleague > wants to implement it in Python 3. Python 3 is the future and where we will all inevitably wind up. There are very few people still using Python 1.x nowadays, progress happens. And its much easier to start a new project on a new version that to try to migrate it after your got it built. So I'd see this as an opportunity to move with the times and go with v3. Once you get used to it - and it is a big jump, don't underestimate the learning time - v3 is superior. It's more consistent and the new features are potentially useful. Take the time to really explore what v3 offers, not just the new syntax items. Caveat: If you need a specialist library that hasn't been ported that may force you to use v2. But very few of the s significant libraries are stuck on 2 now. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Mon Sep 7 04:40:14 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 7 Sep 2015 12:40:14 +1000 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: <20150907024014.GZ19373@ando.pearwood.info> Hello Marcus, On Fri, Sep 04, 2015 at 04:28:10PM +0200, marcus l?tolf wrote: [...] > I should probably tell you the real task are a series (maximum ~ 301) > lists in which real names of people are assigned to the items/letters > for 2 people(golfers) can be in the same list(flight) only once for an > extended period of time. The next step would be to assign compatible > and noncompatible attributes to the items/letters which will reduce > the maximum of possible lists(flights) Sorry, that description doesn't help me understand your problem. Perhaps you could show how to generate the pairs you want given (say) a small list of names. Let us call them A, B, C, D and E (five people), taken three at a time? (1) Can you write out all the possible lists for n=3 (the maximum)? Assuming order does not matter, I get ten lists: A B C A B D A B E A C D A C E A D E B C D B C E B D E C D E (2) Can you show what you mean by "compatible and noncompatible attributes", and use them to "reduce the maximum of possible lists"? How do you decide which of the ten above are allowed and which are not? -- Steve From marc.tompkins at gmail.com Mon Sep 7 08:22:49 2015 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sun, 6 Sep 2015 23:22:49 -0700 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: On Fri, Sep 4, 2015 at 7:28 AM, marcus l?tolf wrote: > I should probably tell you the real task are a series (maximum ~ 301) > lists in which real names of people are assigned to the items/letters for > 2 people(golfers) can be in the same list(flight) only once for an > extended period of time. > The next step would be to assign compatible and noncompatible attributes > to the items/letters which will reduce the maximum of possible > lists(flights) > > I came up with this (obviously it could be made shorter, but I thought it would be clearer this way): import string, itertools def main(): validCombos = [] usedPairs = [] allCombos = itertools.combinations(string.ascii_lowercase, 3) for combo in allCombos: pair1 = (combo[0],combo[1]) pair2 = (combo[0],combo[2]) pair3 = (combo[1],combo[2]) if pair1 in usedPairs or pair2 in usedPairs or pair3 in usedPairs: next else: usedPairs.append(pair1) usedPairs.append(pair2) usedPairs.append(pair3) validCombos.append(combo) print(validCombos) print(len(validCombos)) if __name__ == '__main__': main() The resulting triplets seem to meet your criteria - but there are only 90 of them. How confident are you about the number 301? From nymcity at yahoo.com Mon Sep 7 02:40:33 2015 From: nymcity at yahoo.com (Nym City) Date: Mon, 7 Sep 2015 00:40:33 +0000 (UTC) Subject: [Tutor] Syntax error and EOL Error In-Reply-To: References: Message-ID: <756074126.2391117.1441586433214.JavaMail.yahoo@mail.yahoo.com> Hello, Thank you for your response. I have made updates and here is the new code: import csv DomainList = [] domains = open('domainlist.csv', 'rb') DomainList = csv.reader(domains) DomainList = [column[1] for column in DomainList(str.rstrip('/'))] print(DomainList) For "DomainList = [column[1] for column in DomainList(str.rstrip('/'))]" line, I am getting following error:TypeError: '_csv.reader' object is not callable Doing some research, i thought it might be because I am importing the csv as 'r' only so i tried changing it to 'rb' and 'w' but received the same error. Also, not sure if the next error is because of the above issue but I get syntax error when I do the last print and I do not see any syntax issue with it. As always, thanks in advance. ?Thank you. On Friday, September 4, 2015 3:23 AM, Peter Otten <__peter__ at web.de> wrote: Nym City via Tutor wrote: >? import csv > DomainList = [] > > domains = open('domainlist.csv', 'r') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList] > DomainList = (str(DomainList).rstrip('/') > print('\n'.join(DomainList)) > > > I keep getting EOL error on the second the last line and syntax error on > the last print line. Even when I change the print line to say the > following: print(DomainList) Please advise. Thank you in advance. > Thank you. Look at the lines preceding the one triggering the SyntaxError. Usually there is an opening (, [ or { which doesn't have a matching closing counterpart. In your case count the parens in the line > DomainList = (str(DomainList).rstrip('/') By the way, even without error this line will not have the desired effect. Given >>> DomainList = ["facebook.com/", "twitter.com/"] converting to string gives >>> str(DomainList) "['facebook.com/', 'twitter.com/']" and as that doesn't end with a "/" applying the rstrip() method has no effect. Instead you can modify the line > DomainList = [column[1] for column in DomainList] where you can invoke the rstrip() method on every string in the list. _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From sunnycemetery at gmail.com Mon Sep 7 03:15:52 2015 From: sunnycemetery at gmail.com (Grady Martin) Date: Sun, 6 Sep 2015 21:15:52 -0400 Subject: [Tutor] 2 vs 3 In-Reply-To: References: Message-ID: <20150907011552.GB1202@evasion> On 2015?09?05? 23?33?, Alan Gauld wrote: >Once you get used to it - and it is a big jump, don't >underestimate the learning time - v3 is superior. This is the first I've heard someone describe the learning curve from 2 to 3 in anything but negligible terms. What makes the jump big? From lac at openend.se Mon Sep 7 11:14:10 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 07 Sep 2015 11:14:10 +0200 Subject: [Tutor] 2 vs 3 In-Reply-To: <20150907011552.GB1202@evasion> References: <20150907011552.GB1202@evasion> Message-ID: <201509070914.t879EADr017503@fido.openend.se> In a message of Sun, 06 Sep 2015 21:15:52 -0400, Grady Martin writes: >On 2015?09?05? 23?33?, Alan Gauld wrote: >>Once you get used to it - and it is a big jump, don't >>underestimate the learning time - v3 is superior. > >This is the first I've heard someone describe the learning curve from 2 to 3 in anything but negligible terms. What makes the jump big? It's big for people in English speaking languges who have never used unicode before. Those of us who have been using unicode with Python 2 for a very long time, in contrast, say 'my word, this is a whole lot simpler'. :) Laura From alan.gauld at btinternet.com Mon Sep 7 11:16:16 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 07 Sep 2015 10:16:16 +0100 Subject: [Tutor] 2 vs 3 In-Reply-To: <20150907011552.GB1202@evasion> References: <20150907011552.GB1202@evasion> Message-ID: On 07/09/15 02:15, Grady Martin wrote: > On 2015?09?05? 23?33?, Alan Gauld wrote: >> Once you get used to it - and it is a big jump, don't >> underestimate the learning time - v3 is superior. > > This is the first I've heard someone describe the learning curve from 2 > to 3 in anything but negligible terms. What makes the jump big? Yes, that's what I thought too. Then I tried it. It's nothing huge, but thee are a lot of small changes that continually catch you out. Modules have been moved into packages Module names have changed. Functions have been moved to new modules. Function return types have changes Exception handling has changed Error types have changed Class heirarchies have changed (especially the io hierarchy) Data types have changed metaclasses have changed Strings have changed to Unicode and bytes And of course if you are really moving to the v3 way of doing things you should move to using the new string formatting style. I found it took me a couple of months before I stopped having to jump into the docs and use help() even where I thought I knew what I was doing. Don't get me wrong, its still a worthwhile thing to do. But don't be fooled by the hype, it is not a trivial change. Your coding speed will slow down for a while till you get used to it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Mon Sep 7 11:24:44 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 07 Sep 2015 10:24:44 +0100 Subject: [Tutor] Syntax error and EOL Error In-Reply-To: <756074126.2391117.1441586433214.JavaMail.yahoo@mail.yahoo.com> References: <756074126.2391117.1441586433214.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 07/09/15 01:40, Nym City via Tutor wrote: > Hello, > Thank you for your response. I have made updates and here is the new code: > import csv > DomainList = [] > > domains = open('domainlist.csv', 'rb') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList(str.rstrip('/'))] Since DomainList is a csv.reader object what made you think you could call it with a string argument? You were not doing that previously? > For "DomainList = [column[1] for column in DomainList(str.rstrip('/'))]" > line, I am getting following error:TypeError: '_csv.reader' object is not callable Which is true, and should be obvious how to fix. Stop calling it. That means don't have parentheses after it. > Doing some research, i thought it might be because I am importing > the csv as 'r' only You are not importing the csv as 'r' you are opening your file as 'r'. This has nothing to do with the error. That is because you are calling a non callable object. > Also, not sure if the next error is because of the above issue but > I get syntax error when I do the last print Can you tell us how you managed to get that second error? The interpreter should have stopped after the first error so you should never get a second error. You must have modified the code in some way. You would need to show us the actual code you ran to reach the print statement, otherwise we can't begin to guess what might be causing the syntax error. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Mon Sep 7 11:32:32 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 07 Sep 2015 11:32:32 +0200 Subject: [Tutor] 2 vs 3 In-Reply-To: References: <20150907011552.GB1202@evasion> Message-ID: <201509070932.t879WWFT022087@fido.openend.se> In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes: >And of course if you are really moving to the v3 way of >doing things you should move to using the new string >formatting style. I don't think there is any great push to prefer the new style over the old style 'just because' when you are doing simple things. Laura From alan.gauld at btinternet.com Mon Sep 7 12:01:00 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 07 Sep 2015 11:01:00 +0100 Subject: [Tutor] 2 vs 3 In-Reply-To: <201509070932.t879WWFT022087@fido.openend.se> References: <20150907011552.GB1202@evasion> <201509070932.t879WWFT022087@fido.openend.se> Message-ID: On 07/09/15 10:32, Laura Creighton wrote: > In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes: > >> And of course if you are really moving to the v3 way of >> doing things you should move to using the new string >> formatting style. > > I don't think there is any great push to prefer the new style over > the old style 'just because' when you are doing simple things. No 'big push' because there's too much historic code out there. But I thought I'd read somewhere when it first came out that the old style was deprecated? But I may be hallucinating! :-) OTOH the new style brings enough benefits, especially in the areas of dynamic formatting, that I'd strongly suggest moving to it if you are making the jump to v3 anyway! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Mon Sep 7 13:34:52 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 7 Sep 2015 21:34:52 +1000 Subject: [Tutor] 2 vs 3 In-Reply-To: <201509070932.t879WWFT022087@fido.openend.se> References: <20150907011552.GB1202@evasion> <201509070932.t879WWFT022087@fido.openend.se> Message-ID: <20150907113452.GC19373@ando.pearwood.info> On Mon, Sep 07, 2015 at 11:32:32AM +0200, Laura Creighton wrote: > In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes: > > >And of course if you are really moving to the v3 way of > >doing things you should move to using the new string > >formatting style. > > I don't think there is any great push to prefer the new style over > the old style 'just because' when you are doing simple things. If you're referring to "%s" % x versus "{}".format(x), no, there's no official push for format. They tried that, but the push-back from people who like the %s formatting was too large. %s formatting is NOT deprecated, and will not be any time in the forseeable future; %s formatting is still necessary for anyone who wants to write code that works with version 2.4 on up; format() is more powerful, but also slower. Of course individual projects and work-places may include their own in-house rules, which may prefer %s or format. But as far as Python the language goes, you can use whichever you prefer. -- Steve From steve at pearwood.info Mon Sep 7 13:58:27 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 7 Sep 2015 21:58:27 +1000 Subject: [Tutor] 2 vs 3 In-Reply-To: References: <20150907011552.GB1202@evasion> <201509070932.t879WWFT022087@fido.openend.se> Message-ID: <20150907115827.GD19373@ando.pearwood.info> On Mon, Sep 07, 2015 at 11:01:00AM +0100, Alan Gauld wrote: > On 07/09/15 10:32, Laura Creighton wrote: > >In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes: > > > >>And of course if you are really moving to the v3 way of > >>doing things you should move to using the new string > >>formatting style. > > > >I don't think there is any great push to prefer the new style over > >the old style 'just because' when you are doing simple things. > > No 'big push' because there's too much historic code out there. > But I thought I'd read somewhere when it first came out that > the old style was deprecated? But I may be hallucinating! :-) When the format() method first came out, there was talk about deprecating % formatting, but it was only talk and never became official. > OTOH the new style brings enough benefits, especially in the > areas of dynamic formatting, that I'd strongly suggest moving > to it if you are making the jump to v3 anyway! It's worth learning both formatting systems, even if you're just using Python 2. The format method started in 2.6, and 2.7 added the "autonumbering" feature that makes it much easier to use: "{0} {1} {2}".format(a, b, c) # needed in 2.6 "{} {} {}".format(a, b, c) # 2.7 and up Having said that, I still don't know how to use format other than the basics, as most of the code I write has to be compatible with 2.4 onwards. -- Steve From oscar.j.benjamin at gmail.com Mon Sep 7 14:34:55 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 7 Sep 2015 13:34:55 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: On 5 September 2015 at 23:28, Mark Lawrence wrote: > On 05/09/2015 10:09, Peter Otten wrote: >> >>> the 5 lists above do not match my task insofar as every of the 5 lists >>> contains 'a' and 'b' which should occur only once, hence my count of a >>> maximum of 301 lists, which might nor be correct 100%. My be one could >>> put >>> it in Python as follows: >>>> >>>> ('a', 'b', 'c') = True >>>> ('a', 'b', 'd')= False >>>> ('a', 'b', 'e')= False >>>> ('a', 'b', 'f')= False >>>> ('a', 'b', 'g')= False > > So for completeness it follows that:- > > ('a', 'c', 'd') == False > ('b', 'c', 'd') == False > > yes? I think so. >>> I should probably tell you the real task are a series (maximum ~ 301) >>> lists in which real names of people are assigned to the items/letters >>> for >>> 2 people(golfers) can be in the same list(flight) only once for an >>> extended period of time. >> > >> It's probably a good idea to ask your question in a mathematics forum -- >> this problem looks common enough to have a name and some brain power spent >> on it. >> > > To me this is clearly an example of a Steiner Triple system > associated with Balanced Incomplete Block Design. Which means I found this > http://mathforum.org/library/drmath/view/52263.html which got me to > https://en.wikipedia.org/wiki/Steiner_system and also > http://oeis.org/A137348/a137348.txt. Just one minor little question, am I > actually correct? It sounds like the social golfer problem (or Kirkman's schoolgirl problem, both of which Steiner systems): http://mathworld.wolfram.com/SocialGolferProblem.html The problem is that there are 26 people and they are divided into groups of 3 each day. We would like to know if it is possible to arrange it so that each player plays each other player exactly once over some period of days. It is not exactly possible to do this with 26 people in groups of 3. Think about it from the perspective of 1 person. They must play against all 25 other people in pairs with neither of the other people repeated: the set of pairs they play against must partition the set of other players. Clearly it can only work if the number of other players is even. I'm not sure but I think that maybe for an exact solution you need to have n=1(mod6) or n=3(mod6) which gives: n = 1, 3, 7, 9, 13, 15, 19, 21, 25, 27, ... The formula for the number of triples if the exact solution exists is n*(n-1)/6 which comes out as 26*25/6 = 108.33333 (the formula doesn't give an integer because the exact solution doesn't exist). The question is what do you want to do with the leftovers if it's not possible for every player to play exactly once? Also what do you want with these triples? Are you just trying to count them? Are you interested to know which triples come out? It's not unique and the number of possible sets of triples is massive. -- Oscar From zebra05 at gmail.com Mon Sep 7 16:28:15 2015 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 7 Sep 2015 16:28:15 +0200 Subject: [Tutor] 2 vs 3 In-Reply-To: <20150907115827.GD19373@ando.pearwood.info> References: <20150907011552.GB1202@evasion> <201509070932.t879WWFT022087@fido.openend.se> <20150907115827.GD19373@ando.pearwood.info> Message-ID: This has turned into a very interesting discussion. Thank you to everyone who's participating and sharing nuggets of information on 2 vs 3. On Mon, Sep 7, 2015 at 1:58 PM, Steven D'Aprano wrote: > On Mon, Sep 07, 2015 at 11:01:00AM +0100, Alan Gauld wrote: > > On 07/09/15 10:32, Laura Creighton wrote: > > >In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes: > > > > > >>And of course if you are really moving to the v3 way of > > >>doing things you should move to using the new string > > >>formatting style. > > > > > >I don't think there is any great push to prefer the new style over > > >the old style 'just because' when you are doing simple things. > > > > No 'big push' because there's too much historic code out there. > > But I thought I'd read somewhere when it first came out that > > the old style was deprecated? But I may be hallucinating! :-) > > When the format() method first came out, there was talk about > deprecating % formatting, but it was only talk and never became > official. > > > OTOH the new style brings enough benefits, especially in the > > areas of dynamic formatting, that I'd strongly suggest moving > > to it if you are making the jump to v3 anyway! > > It's worth learning both formatting systems, even if you're just using > Python 2. The format method started in 2.6, and 2.7 added the > "autonumbering" feature that makes it much easier to use: > > "{0} {1} {2}".format(a, b, c) # needed in 2.6 > "{} {} {}".format(a, b, c) # 2.7 and up > > > Having said that, I still don't know how to use format other than the > basics, as most of the code I write has to be compatible with 2.4 > onwards. > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Kind regards, Sithu Lloyd Dube From breamoreboy at yahoo.co.uk Mon Sep 7 18:58:53 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 7 Sep 2015 17:58:53 +0100 Subject: [Tutor] 2 vs 3 In-Reply-To: <20150907113452.GC19373@ando.pearwood.info> References: <20150907011552.GB1202@evasion> <201509070932.t879WWFT022087@fido.openend.se> <20150907113452.GC19373@ando.pearwood.info> Message-ID: On 07/09/2015 12:34, Steven D'Aprano wrote: > On Mon, Sep 07, 2015 at 11:32:32AM +0200, Laura Creighton wrote: >> In a message of Mon, 07 Sep 2015 10:16:16 +0100, Alan Gauld writes: >> >>> And of course if you are really moving to the v3 way of >>> doing things you should move to using the new string >>> formatting style. >> >> I don't think there is any great push to prefer the new style over >> the old style 'just because' when you are doing simple things. > > If you're referring to "%s" % x versus "{}".format(x), no, there's no > official push for format. They tried that, but the push-back from people > who like the %s formatting was too large. > > %s formatting is NOT deprecated, and will not be any time in the > forseeable future; http://www.gossamer-threads.com/lists/python/dev/969817 http://bugs.python.org/issue14123 -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From richkappler at gmail.com Tue Sep 8 18:00:35 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 8 Sep 2015 12:00:35 -0400 Subject: [Tutor] find second occurance of string in line Message-ID: I need to find the index of the second occurance of a string in an xml file for parsing. I understand re well enough to do what I want to do on the first instance, but despite many Google searches have yet to find something to get the index of the second instance, because split won't really work on my xml file (if I understand split properly) as there are no spaces. Specifically I'm looking for the second in an objectdata line. Not all lines are objectdata lines, though all objectdata lines do have more than one . What I have so far: import re with open("example.xml", 'r') as f: for line in f: if "objectdata" in line: if "" in line: x = "" first = x.index(line) second = x[first+1:].index(line) print first, second else: print "no timestamp" else: print "no objectdata" my traceback: Traceback (most recent call last): File "2iter.py", line 10, in first = x.index(line) ValueError: substring not found -- All internal models of the world are approximate. ~ Sebastian Thrun From alan.gauld at btinternet.com Tue Sep 8 18:23:31 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 08 Sep 2015 17:23:31 +0100 Subject: [Tutor] find second occurance of string in line In-Reply-To: References: Message-ID: On 08/09/15 17:00, richard kappler wrote: > I need to find the index of the second occurance of a string in an xml file > for parsing. Do you want to find just the second occurence in the *file* or the second occurence within a given tag in the file (and there could be multiple such tags)? > I understand re well enough to do what I want to do Using re to parse XML is usually the wrong way to go about it. Fortunately you are not using re in the code below. However, a real XML parser such as etree(from the std lib) or lxml might work better. > first instance, but despite many Google searches have yet to find something > to get the index of the second instance, because split won't really work on > my xml file (if I understand split properly) as there are no spaces. split can split on any character you want, whitespace just happens to be the default. > Specifically I'm looking for the second in an objectdata line. Is objectdata within a specific tag? Usually when parsing XML its the tags you look for first since "lines" can be broken over multiple lines and multiple tags can exist on one literal line. > Not all lines are objectdata lines, though all objectdata lines do have > more than one . This implies there are many objectdata lines within your file? See the first comment above... do you want the second index for the first objectdata line or do you want it for every objectdata line? > import re You don't use this. > with open("example.xml", 'r') as f: > for line in f: > if "objectdata" in line: > if "" in line: > x = "" You should assign this once above the loops, it saves a lot of duplicated work. > first = x.index(line) This is looking for the index of line within x. I suspect you really want first = line.index(x) > second = x[first+1:].index(line) You can specify a start position for index directly: second = line.index(x,first+1) > print first, second > else: > print "no timestamp" > else: > print "no objectdata" > > my traceback: > > Traceback (most recent call last): > File "2iter.py", line 10, in > first = x.index(line) > ValueError: substring not found That's what you get when the search fails. You should use try/except when using index() Alternatively try using str.find() which returns -1 when no index is found. But you need to check before using it because -1 is, of course, a valid string index! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From richkappler at gmail.com Tue Sep 8 18:56:43 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 8 Sep 2015 12:56:43 -0400 Subject: [Tutor] Fwd: find second occurance of string in line In-Reply-To: References: Message-ID: > Do you want to find just the second occurence in the *file* or the second occurence within a given tag in the file (and there could be multiple such tags)? There are multiple objectdata lines in the file and I wish to find the second occurence of timestamp in each of those lines. > Is objectdata within a specific tag? Usually when parsing XML its the tags you look for first since "lines" can be broken over multiple lines and multiple tags can exist on one literal line. objectdata is within a tag as is timestamp. Here's an example: 0381UDI1322015-06-18T14:28:06.570531630381UDI12015-06-18T14:27:5037913062015-06-18T14:27:50.81151.450-10.66FFFFF001SE0381icdim01322142C0050.00017.20015.2IN1000000000 0044]C00379622001900000175168000643903637408AcSuccess,AC,LFT,Cubic,ValidDim0049518349622001900000175168000643903637408FxgEpic,RefBarcode,ScannersTop,Ref,ValidRead,SortableBarcode,EpicBarcode700660001019911281372936-501307200084.06]C00379622001900000175168000643903637408 > import re I know I don't use this in the snippet of code I sent, it is used elsewhere in the script and I missed that when trimming. > You should assign this once above the loops, it saves a lot of duplicated work. Yes, I understand, again, it is a snippet of a much larger piece of code. > second = line.index(x,first+1) I think that is what I was looking for, off to test now. Thanks Alan! regards, Richard From __peter__ at web.de Tue Sep 8 19:40:03 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 08 Sep 2015 19:40:03 +0200 Subject: [Tutor] Fwd: find second occurance of string in line References: Message-ID: richard kappler wrote: >> Do you want to find just the second occurence in the *file* or the second > occurence within a given tag in the file (and there could be multiple such > tags)? > > There are multiple objectdata lines in the file and I wish to find the > second occurence of timestamp in each of those lines. > >> Is objectdata within a specific tag? Usually when parsing XML its the > tags you look for first since "lines" can be broken over multiple lines > and multiple tags can exist on one literal line. > > objectdata is within a tag as is timestamp. Here's an example: > > xsi:noNamespaceSchemaLocation="Logging.xsd" > version="1.0">0381UDI132 > 2015-06-18T14:28:06.570 > 531630381UDI12015-06-18T14:27:50379 > 1306 oi="607360" on="379" ox="02503" oc="0" is="49787" ie="50312" lftf="N" > lfts="7" errornb="0" > iostate="DC00">2015-06-18T14:27:50.811 unit="inch">51.45 unit="ms">0... part. Here's a way to get these (all of them) with lxml: import lxml.etree tree = lxml.etree.parse("example.xml") print tree.xpath("//objectdata/general/timestamp/text()") From sjeik_appie at hotmail.com Tue Sep 8 21:08:22 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 8 Sep 2015 19:08:22 +0000 Subject: [Tutor] Fwd: find second occurance of string in line In-Reply-To: References: , , , , Message-ID: > To: tutor at python.org > From: __peter__ at web.de > Date: Tue, 8 Sep 2015 19:40:03 +0200 > Subject: Re: [Tutor] Fwd: find second occurance of string in line > > richard kappler wrote: > > >> Do you want to find just the second occurence in the *file* or the second > > occurence within a given tag in the file (and there could be multiple such > > tags)? > > > > There are multiple objectdata lines in the file and I wish to find the > > second occurence of timestamp in each of those lines. > > > >> Is objectdata within a specific tag? Usually when parsing XML its the > > tags you look for first since "lines" can be broken over multiple lines > > and multiple tags can exist on one literal line. > > > > objectdata is within a tag as is timestamp. Here's an example: > > > > > xsi:noNamespaceSchemaLocation="Logging.xsd" > > version="1.0">0381UDI132 > > 2015-06-18T14:28:06.570 > > 531630381UDI12015-06-18T14:27:50379 > > 1306 oi="607360" on="379" ox="02503" oc="0" > is="49787" ie="50312" lftf="N" > > lfts="7" errornb="0" > > iostate="DC00">2015-06-18T14:27:50.811 > unit="inch">51.45 > unit="ms">0 > [snip] > > I'm inferring from the above that you do not really want the "second" > timestamp in the line -- there is no line left intace anyway;) -- but rather > the one in the ... part. > > Here's a way to get these (all of them) with lxml: > > import lxml.etree > > tree = lxml.etree.parse("example.xml") > print tree.xpath("//objectdata/general/timestamp/text()") Nice. I do need to try lxml some time. Is the "text()" part xpath as well? When I try it with elementtree, I get * a SyntaxError. root.find("//objectdata/general/timestamp/text()") File "", line unknown SyntaxError: cannot use absolute path on element root.find("./general/timestamp/text()") Traceback (most recent call last): File "", line 1, in root.find("./general/timestamp/text()") File "/usr/lib/python2.7/xml/etree/ElementPath.py", line 285, in find return iterfind(elem, path, namespaces).next() File "/usr/lib/python2.7/xml/etree/ElementPath.py", line 263, in iterfind selector.append(ops[token[0]](next, token)) * a KeyError root.find("./general/timestamp/text()") Traceback (most recent call last): File "", line 1, in root.find("./general/timestamp/text()") File "/usr/lib/python2.7/xml/etree/ElementPath.py", line 285, in find return iterfind(elem, path, namespaces).next() File "/usr/lib/python2.7/xml/etree/ElementPath.py", line 263, in iterfind selector.append(ops[token[0]](next, token)) KeyError: '()' Anyway, I would have done it like below. I added the pretty-printing because I think it's a really useful function. import xml.etree.cElementTree as et import xml.dom.minidom data = """\ * copy-paste xml here """ data = xml.dom.minidom.parseString(data).toprettyxml() print data root = et.fromstring(data) #root = et.parse(xml_filename).getroot() timestamp = root.findtext(".//general/timestamp") Best wishes, Albert-Jan From __peter__ at web.de Tue Sep 8 21:37:07 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 08 Sep 2015 21:37:07 +0200 Subject: [Tutor] Fwd: find second occurance of string in line References: Message-ID: Albert-Jan Roskam wrote: >> import lxml.etree >> >> tree = lxml.etree.parse("example.xml") >> print tree.xpath("//objectdata/general/timestamp/text()") > > Nice. I do need to try lxml some time. Is the "text()" part xpath as well? Yes. I think ElementTree supports a subset of XPath. From __peter__ at web.de Wed Sep 9 09:55:24 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Sep 2015 09:55:24 +0200 Subject: [Tutor] find second occurance of string in line References: Message-ID: richard kappler wrote: > On Tue, Sep 8, 2015 at 1:40 PM, Peter Otten <__peter__ at web.de> wrote: >> I'm inferring from the above that you do not really want the "second" >> timestamp in the line -- there is no line left intace anyway;) -- but >> rather >> the one in the ... part. >> >> Here's a way to get these (all of them) with lxml: >> >> import lxml.etree >> >> tree = lxml.etree.parse("example.xml") >> print tree.xpath("//objectdata/general/timestamp/text()") > No no, I'm not sure how I can be much more clear, that is one (1) line of > xml that I provided, not many, and I really do want what I said in the > very beginning, the second instance of for each of those > lines. It looks likes I was not clear enough: XML doesn't have the concept of lines. When you process XML "by line" you have buggy code. > Got it figured out with guidance from Alan's response though: > > #!/usr/bin/env python > > with open("example.xml", 'r') as f: > for line in f: > if "objectdata" in line: > if "" in line: > x = "" > a = "" > first = line.index(x) > second = line.index(x, first+1) > b = line.index(a) > c = line.index(a, b+1) > y = second + 11 > timestamp = line[y:c] > print timestamp Just for fun take the five minutes to install lxml and compare the output of the two scripts. If it's the same now there's no harm switching to lxml, and you are making future failures less likely. From fal at libero.it Wed Sep 9 13:05:52 2015 From: fal at libero.it (Francesco Loffredo) Date: Wed, 9 Sep 2015 13:05:52 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> Message-ID: <55F01290.4040407@libero.it> Oscar Benjamin wrote: The problem is that there are 26 people and they are divided into groups of 3 each day. We would like to know if it is possible to arrange it so that each player plays each other player exactly once over some period of days. It is not exactly possible to do this with 26 people in groups of 3. Think about it from the perspective of 1 person. They must play against all 25 other people in pairs with neither of the other people repeated: the set of pairs they play against must partition the set of other players. Clearly it can only work if the number of other players is even. I'm not sure but I think that maybe for an exact solution you need to have n=1(mod6) or n=3(mod6) which gives: n = 1, 3, 7, 9, 13, 15, 19, 21, 25, 27, ... The formula for the number of triples if the exact solution exists is n*(n-1)/6 which comes out as 26*25/6 = 108.33333 (the formula doesn't give an integer because the exact solution doesn't exist). ------------------------------------ A quick solution is to add one "dummy" letter to the pool of the OP's golfers. I used "!" as the dummy one. This way, you end up with 101 triples, 11 of which contain the dummy player. But this is better than the 25-item pool, that resulted in an incomplete set of triples (for example, A would never play with Z) So, in your real-world problem, you will have 11 groups of 2 people instead of 3. Is this a problem? import pprint, itertools pool = "abcdefghijklmnopqrstuvwxyz!" def maketriples(tuplelist): final = [] used = set() for a, b, c in tuplelist: if ( ((a,b) in used) or ((b,c) in used) or ((a,c) in used) or ((b,a) in used) or ((c,b) in used) or ((c,a) in used) ): continue else: final.append((a, b, c)) used.add((a,b)) used.add((a,c)) used.add((b,c)) used.add((b,a)) used.add((c,a)) used.add((c,b)) return final combos = list(itertools.combinations(pool, 3)) print("combos contains %s triples." % len(combos)) triples = maketriples(combos) print("maketriples(combos) contains %s triples." % len(triples)) pprint.pprint(triples) From richkappler at gmail.com Wed Sep 9 15:05:23 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 09:05:23 -0400 Subject: [Tutor] More Pythonic? Message-ID: Would either or both of these work, if both, which is the better or more Pythonic way to do it, and why? ####################### import whatIsNeeded writefile = open("writefile", 'a') with open(readfile, 'r') as f: for line in f: if keyword in line: do stuff f1.write(line) else: f1.write(line) writefile.close() ###################### import whatIsNeeded with open(readfile, 'r') as f: for line in f: try: if keyword in line: do stuff except: do nothing with open(writefile, 'a') as f1: f1.write(line) ###################### or something else altogether? I'm thinking the first way is better as it only opens the files once whereas it seems to me the second script would open and close the writefile once per iteration, and the do nothing in the except seems just wrong to me. Is my thinking on target here? regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From sjeik_appie at hotmail.com Wed Sep 9 15:31:22 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 9 Sep 2015 13:31:22 +0000 Subject: [Tutor] Fwd: find second occurance of string in line In-Reply-To: References: , , , , , Message-ID: > To: tutor at python.org > From: __peter__ at web.de > Date: Tue, 8 Sep 2015 21:37:07 +0200 > Subject: Re: [Tutor] Fwd: find second occurance of string in line > > Albert-Jan Roskam wrote: > > >> import lxml.etree > >> > >> tree = lxml.etree.parse("example.xml") > >> print tree.xpath("//objectdata/general/timestamp/text()") > > > > Nice. I do need to try lxml some time. Is the "text()" part xpath as well? > > Yes. I think ElementTree supports a subset of XPath. aha, I see. I studied lxml.de a bit last night and it seems to be better in many ways. Writing appears to be mmmuch faster than cElementtree while many other situations are comparable to cElementtree. I love the objectify part of the package. Would you say that (given iterparse) lxml is also the module to process giant (ie. larger than RAM) xml files)? The webpage recommends a cascade of try-except ImportError statementsL first lxml, then cElementtree, then elementtree. But given that there are slight API differences, is that really a good idea? How would you test whether the code runs under both lxml and under the alternatives? Would you uninstall lxml to force that one of the alternatives is used? Best wishes, Albert-Jan From richkappler at gmail.com Wed Sep 9 15:32:34 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 09:32:34 -0400 Subject: [Tutor] iterating through a directory Message-ID: Yes, many questions today. I'm working on a data feed script that feeds 'events' into our test environment. In production, we monitor a camera that captures an image as product passes by, gathers information such as barcodes and package ID from the image, and then sends out the data as a line of xml to one place for further processing and sends the image to another place for storage. Here is where our software takes over, receiving the xml data and images from vendor equipment. Our software then processes the xml data and allows retrieval of specific images associated with each line of xml data. Our test environment must simulate the feed from the vendor equipment, so I'm writing a script that feeds the xml from an actual log to one place for processing, and pulls images from a pool of 20, which we recycle, to associate with each event and sends them to another dir for saving and searching. As my script iterates through each line of xml data (discussed yesterday) to simulate the feed from the vendor camera equipment, it parses ID information about the event then sends the line of data on. As it does so, it needs to pull the next image in line from the image pool directory, rename it, send it to a different directory for saving. I'm pretty solid on all of this except iterating through the image pool. My idea is to just keep looping through the image pool, as each line of xmldata is parsed, the next image in line gets pulled out, renamed with the identifying information from the xml data, and both are sent on to different places. I only have one idea for doing this iterating through the image pool, and frankly I think the idea is pretty weak. The only thing I can come up with is to change the name of each image in the pool from something like 0219PS01CT1_20111129_000004_00044979.jpg to 1.jpg, 2.jpg, 3.jpg etc., then doing something like: i = 0 here begins my for line in file loop: if i == 20: i = 1 else: i += 1 do stuff with the xml file including get ID info rename i.jpg to IDinfo.jpg send it all on That seems pretty barbaric, any thoughts on where to look for better ideas? I'm presuming there are modules out there that I am unaware of or capabilities I am unaware of in modules I do know a little about that I am missing. -- All internal models of the world are approximate. ~ Sebastian Thrun From __peter__ at web.de Wed Sep 9 15:37:37 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Sep 2015 15:37:37 +0200 Subject: [Tutor] More Pythonic? References: Message-ID: richard kappler wrote: > Would either or both of these work, if both, which is the better or more > Pythonic way to do it, and why? > > ####################### > > import whatIsNeeded > > writefile = open("writefile", 'a') > > with open(readfile, 'r') as f: > for line in f: > if keyword in line: > do stuff > f1.write(line) > else: > f1.write(line) Why do you invoke f1.write() twice? > writefile.close() > > ###################### > > import whatIsNeeded > > with open(readfile, 'r') as f: > for line in f: > try: > if keyword in line: > do stuff > except: What exceptions are you expecting here? Be explicit. You probably don't want to swallow a KeyboardInterrupt. And if something unexpected goes wrong a noisy complaint gives you the chance to either fix an underlying bug or explicitly handle the exception in future runs of the script. > do nothing That's spelt pass > with open(writefile, 'a') as f1: > f1.write(line) Opening the file once per line written seems over-the-top to me. > ###################### > > or something else altogether? I tend to put the processing into into a generator. That makes it easy to replace the source or the consumer: def process_lines(instream): for line in instream: if keyword in line: do stuff yield line with open(sourcefile) as instream: with open(destfile, "a") as outstream: outstream.writelines(process_lines(instream)) Now if you want to read from stdin and print to stdout: sys.stdout.writelines(process_lines(sys.stdin)) > I'm thinking the first way is better as it only opens the files once > whereas it seems to me the second script would open and close the > writefile once per iteration, and the do nothing in the except seems just > wrong to me. It's not clear why you need the try...except: pass. Please provide some more background information. From richkappler at gmail.com Wed Sep 9 15:40:16 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 09:40:16 -0400 Subject: [Tutor] Fwd: Fwd: find second occurance of string in line In-Reply-To: References: Message-ID: > It looks likes I was not clear enough: XML doesn't have the concept of lines. When you process XML "by line" you have buggy code. No Peter, I'm pretty sure it was I who was less than clear. The xml data is generated by events, one line in a log for each event, so while xml doesn't have the concept of lines, each event creates a new line of xml data in the log from which I will be reading. Sorry about the confusion. I should have phrased it better, perhaps calling them events instead of lines? > Just for fun take the five minutes to install lxml and compare the output of the two scripts. If it's the same now there's no harm switching to lxml, and you are making future failures less likely. I'll take a look at it, thanks. regards, Richard From steve at pearwood.info Wed Sep 9 15:41:08 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 9 Sep 2015 23:41:08 +1000 Subject: [Tutor] More Pythonic? In-Reply-To: References: Message-ID: <20150909134108.GJ19373@ando.pearwood.info> On Wed, Sep 09, 2015 at 09:05:23AM -0400, richard kappler wrote: > Would either or both of these work, if both, which is the better or more > Pythonic way to do it, and why? The first works, but isn't really the best way to do it: > ####################### > > import whatIsNeeded > > writefile = open("writefile", 'a') > > with open(readfile, 'r') as f: > for line in f: > if keyword in line: > do stuff > f1.write(line) > else: > f1.write(line) > > writefile.close() > > ###################### Better would be this: with open("writefile", 'a') as outfile: with open("readfile", 'r') as infile: for line in infile: if keyword in line: do stuff outfile.write(line) (I think your intention is to always write the lines into the output file, but there are enough typos in your version that I can't be completely sure.) This, on the other hand, is certainly not what you want: > import whatIsNeeded > > with open(readfile, 'r') as f: > for line in f: > try: > if keyword in line: > do stuff > except: > do nothing Why are you ignoring *all errors*? That will make it impossible (or at least very hard) to cancel the script with Ctrl-C, and it will cover up programming errors. Apart from a very few expert uses, you should never use a bare except. If you really want to "ignore all errors", use `except Exception`, but even that is not good practice. You should list and catch only the precise errors that you know you wish to ignore and can safely handle. Everything else indicates a bug that needs fixing. By the way, "do nothing" in Python is spelled "pass". > with open(writefile, 'a') as f1: > f1.write(line) And this will repeatedly open the file, append one line, then close it again. Almost certainly not what you want -- it's wasteful and potentially expensive. > ###################### > > or something else altogether? > > I'm thinking the first way is better as it only opens the files once > whereas it seems to me the second script would open and close the writefile > once per iteration, and the do nothing in the except seems just wrong to > me. Is my thinking on target here? Spot on target. -- Steve From richkappler at gmail.com Wed Sep 9 15:47:04 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 09:47:04 -0400 Subject: [Tutor] More Pythonic? In-Reply-To: References: Message-ID: > It's not clear why you need the try...except: pass. Please provide some more background information. I don't need the try, this was more of a "are there different ways to do this, which is better and why?" experiment. I am learning, so tend to write script that is more brute force than elegant and pythonic, wish to write better code. I do okay, but there are many nuances to Python that I just haven't run across. For example: > with open(sourcefile) as instream: > with open(destfile, "a") as outstream: > outstream.writelines(process_lines(instream)) I had no idea I could nest with statements like that. It seems obvious now, but I didn't know. For the record, I have made a couple other posts this morning that explain the script constraints far better than I did here. For the sake of brevity I shant repeat the info here other than to say it's not reading from stdin, but from a log file to simulate stdin in a test environment. regards, Richard On Wed, Sep 9, 2015 at 9:37 AM, Peter Otten <__peter__ at web.de> wrote: > richard kappler wrote: > > > Would either or both of these work, if both, which is the better or more > > Pythonic way to do it, and why? > > > > ####################### > > > > import whatIsNeeded > > > > writefile = open("writefile", 'a') > > > > with open(readfile, 'r') as f: > > for line in f: > > if keyword in line: > > do stuff > > f1.write(line) > > else: > > f1.write(line) > > Why do you invoke f1.write() twice? > > > writefile.close() > > > > ###################### > > > > import whatIsNeeded > > > > with open(readfile, 'r') as f: > > for line in f: > > try: > > if keyword in line: > > do stuff > > except: > > What exceptions are you expecting here? Be explicit. You probably don't > want > to swallow a KeyboardInterrupt. And if something unexpected goes wrong a > noisy complaint gives you the chance to either fix an underlying bug or > explicitly handle the exception in future runs of the script. > > > do nothing > > That's spelt > pass > > > with open(writefile, 'a') as f1: > > f1.write(line) > > Opening the file once per line written seems over-the-top to me. > > > ###################### > > > > or something else altogether? > > I tend to put the processing into into a generator. That makes it easy to > replace the source or the consumer: > > def process_lines(instream): > for line in instream: > if keyword in line: > do stuff > yield line > > with open(sourcefile) as instream: > with open(destfile, "a") as outstream: > outstream.writelines(process_lines(instream)) > > Now if you want to read from stdin and print to stdout: > > sys.stdout.writelines(process_lines(sys.stdin)) > > > I'm thinking the first way is better as it only opens the files once > > whereas it seems to me the second script would open and close the > > writefile once per iteration, and the do nothing in the except seems just > > wrong to me. > > It's not clear why you need the try...except: pass. > Please provide some more background information. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- All internal models of the world are approximate. ~ Sebastian Thrun From sjeik_appie at hotmail.com Wed Sep 9 15:46:07 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 9 Sep 2015 13:46:07 +0000 Subject: [Tutor] iterating through a directory In-Reply-To: References: Message-ID: > Date: Wed, 9 Sep 2015 09:32:34 -0400 > From: richkappler at gmail.com > To: tutor at python.org > Subject: [Tutor] iterating through a directory > > Yes, many questions today. I'm working on a data feed script that feeds > 'events' into our test environment. In production, we monitor a camera that > captures an image as product passes by, gathers information such as > barcodes and package ID from the image, and then sends out the data as a > line of xml to one place for further processing and sends the image to > another place for storage. Here is where our software takes over, receiving > the xml data and images from vendor equipment. Our software then processes > the xml data and allows retrieval of specific images associated with each > line of xml data. Our test environment must simulate the feed from the > vendor equipment, so I'm writing a script that feeds the xml from an actual > log to one place for processing, and pulls images from a pool of 20, which > we recycle, to associate with each event and sends them to another dir for > saving and searching. > > As my script iterates through each line of xml data (discussed yesterday) > to simulate the feed from the vendor camera equipment, it parses ID > information about the event then sends the line of data on. As it does so, > it needs to pull the next image in line from the image pool directory, > rename it, send it to a different directory for saving. I'm pretty solid on > all of this except iterating through the image pool. My idea is to just > keep looping through the image pool, as each line of xmldata is parsed, the > next image in line gets pulled out, renamed with the identifying > information from the xml data, and both are sent on to different places. > > I only have one idea for doing this iterating through the image pool, and > frankly I think the idea is pretty weak. The only thing I can come up with > is to change the name of each image in the pool from something > like 0219PS01CT1_20111129_000004_00044979.jpg to 1.jpg, 2.jpg, 3.jpg etc., > then doing something like: > > i = 0 > > here begins my for line in file loop: > if i == 20: > i = 1 > else: > i += 1 > do stuff with the xml file including get ID info > rename i.jpg to IDinfo.jpg > send it all on > > That seems pretty barbaric, any thoughts on where to look for better ideas? > I'm presuming there are modules out there that I am unaware of or > capabilities I am unaware of in modules I do know a little about that I am > missing. I do not really understand what you intend to do, but the following modules might come in handy -os (os.rename, os.listdir) -glob (glob.iglob or glob.glob) -shutil (shutil.copy) From __peter__ at web.de Wed Sep 9 15:53:33 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Sep 2015 15:53:33 +0200 Subject: [Tutor] Fwd: find second occurance of string in line References: Message-ID: Albert-Jan Roskam wrote: >> To: tutor at python.org >> From: __peter__ at web.de >> Date: Tue, 8 Sep 2015 21:37:07 +0200 >> Subject: Re: [Tutor] Fwd: find second occurance of string in line >> >> Albert-Jan Roskam wrote: >> >> >> import lxml.etree >> >> >> >> tree = lxml.etree.parse("example.xml") >> >> print tree.xpath("//objectdata/general/timestamp/text()") >> > >> > Nice. I do need to try lxml some time. Is the "text()" part xpath as >> > well? >> >> Yes. I think ElementTree supports a subset of XPath. > > aha, I see. I studied lxml.de a bit last night and it seems to be better > in many ways. Writing appears to be mmmuch faster than cElementtree while > many other situations are comparable to cElementtree. I love the objectify > part of the package. Would you say that (given iterparse) lxml is also the > module to process giant (ie. larger than RAM) xml files)? Yes; but that would not be backed by experience ;) > The webpage recommends a cascade of try-except ImportError statementsL > first lxml, then cElementtree, then elementtree. But given that there are > slight API differences, is that really a good idea? I tend to shy away from such complications, just as I write Python-3-only code now. > How would you test > whether the code runs under both lxml and under the alternatives? Would > you uninstall lxml to force that one of the alternatives is used? Again, I have not much experience with code that must cope with different environments. I have simulated a missing module (not lxml) with the following trick: $ mkdir missing_lxml $ echo 'raise ImportError' > missing_lxml/lxml.py $ python3 -c 'import lxml' $ PYTHONPATH=missing_lxml python3 -c 'import lxml' Traceback (most recent call last): File "", line 1, in File "/home/peter/missing_lxml/lxml.py", line 1, in raise ImportError ImportError Those who regularly need different configurations probably use virtualenv, or virtual machines when the differences are not limited to Python. From richkappler at gmail.com Wed Sep 9 16:24:57 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 10:24:57 -0400 Subject: [Tutor] A further question about opening and closing files Message-ID: Under a different subject line (More Pythonic?) Steven D'Aprano commented: > And this will repeatedly open the file, append one line, then close it > again. Almost certainly not what you want -- it's wasteful and > potentially expensive. And I get that. It does bring up another question though. When using with open(somefile, 'r') as f: with open(filename, 'a') as f1: for line in f: the file being appended is opened and stays open while the loop iterates, then the file closes when exiting the loop, yes? Does this not have the potential to be expensive as well if you are writing a lot of data to the file? I did a little experiment: >>> f1 = open("output/test.log", 'a') >>> f1.write("this is a test") >>> f1.write("this is a test") >>> f1.write('why isn\'t this writing????') >>> f1.close() monitoring test.log as I went. Nothing was written to the file until I closed it, or at least that's the way it appeared to the text editor in which I had test.log open (gedit). In gedit, when a file changes it tells you and gives you the option to reload the file. This didn't happen until I closed the file. So I'm presuming all the writes sat in a buffer in memory until the file was closed, at which time they were written to the file. Is that actually how it happens, and if so does that not also have the potential to cause problems if memory is a concern? regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From richkappler at gmail.com Wed Sep 9 16:29:25 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 10:29:25 -0400 Subject: [Tutor] iterating through a directory In-Reply-To: References: Message-ID: Albert-Jan, thanks for the response. shutil.copyfile does seem to be one of the tools I need to make the copying, renaming the copy and saving it elsewhere in one line instead of three or more. Still not sure how to efficiently get the script to keep moving to the next file in the directory though, in other words, for each iteration in the loop, I want it to fetch, rename and send/save the next image in line. Hope that brings better understanding. Thanks for the tip! regards, Richard On Wed, Sep 9, 2015 at 9:46 AM, Albert-Jan Roskam wrote: > > Date: Wed, 9 Sep 2015 09:32:34 -0400 > > From: richkappler at gmail.com > > To: tutor at python.org > > Subject: [Tutor] iterating through a directory > > > > > Yes, many questions today. I'm working on a data feed script that feeds > > 'events' into our test environment. In production, we monitor a camera > that > > captures an image as product passes by, gathers information such as > > barcodes and package ID from the image, and then sends out the data as a > > line of xml to one place for further processing and sends the image to > > another place for storage. Here is where our software takes over, > receiving > > the xml data and images from vendor equipment. Our software then > processes > > the xml data and allows retrieval of specific images associated with each > > line of xml data. Our test environment must simulate the feed from the > > vendor equipment, so I'm writing a script that feeds the xml from an > actual > > log to one place for processing, and pulls images from a pool of 20, > which > > we recycle, to associate with each event and sends them to another dir > for > > saving and searching. > > > > As my script iterates through each line of xml data (discussed yesterday) > > to simulate the feed from the vendor camera equipment, it parses ID > > information about the event then sends the line of data on. As it does > so, > > it needs to pull the next image in line from the image pool directory, > > rename it, send it to a different directory for saving. I'm pretty solid > on > > all of this except iterating through the image pool. My idea is to just > > keep looping through the image pool, as each line of xmldata is parsed, > the > > next image in line gets pulled out, renamed with the identifying > > information from the xml data, and both are sent on to different places. > > > > I only have one idea for doing this iterating through the image pool, and > > frankly I think the idea is pretty weak. The only thing I can come up > with > > is to change the name of each image in the pool from something > > like 0219PS01CT1_20111129_000004_00044979.jpg to 1.jpg, 2.jpg, 3.jpg > etc., > > then doing something like: > > > > i = 0 > > > > here begins my for line in file loop: > > if i == 20: > > i = 1 > > else: > > i += 1 > > do stuff with the xml file including get ID info > > rename i.jpg to IDinfo.jpg > > send it all on > > > > That seems pretty barbaric, any thoughts on where to look for better > ideas? > > I'm presuming there are modules out there that I am unaware of or > > capabilities I am unaware of in modules I do know a little about that I > am > > missing. > > I do not really understand what you intend to do, but the following > modules might come in handy > -os (os.rename, os.listdir) > -glob (glob.iglob or glob.glob) > -shutil (shutil.copy) > > > > -- All internal models of the world are approximate. ~ Sebastian Thrun From lac at openend.se Wed Sep 9 17:16:09 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 09 Sep 2015 17:16:09 +0200 Subject: [Tutor] Fwd: find second occurance of string in line In-Reply-To: References: Message-ID: <201509091516.t89FG9gs012657@fido.openend.se> Peter Otten >Those who regularly need different configurations probably use virtualenv, >or virtual machines when the differences are not limited to Python. Use tox for this. https://testrun.org/tox/latest/ However for development purposes it often helps to have a --force the_one_that_I_want option (for command lines) or a global variable, or a config file for modules. How badly you want this depends on your own personal development style, and how happy you are popping in and out of virtualenvs. Many people prefer to write their whole new thing for one library (say elementtree) and then test it/port it against the other 2, one at a time, making a complete set of patches for one adaptation at a time. Other people prefer to write their code so that, feature by feature they first get it to work with one library, and then with another, and then with the third, and then they write the next new bit of code, so that they never have to do a real port. Life is messy enough that you often do a bit of this and a bit of the other thing, even if you would prefer to not need to, especially if hungry customers are demanding exactly what they need (and we don't care about the other ways it will eventually work for other people). Laura From lac at openend.se Wed Sep 9 17:28:21 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 09 Sep 2015 17:28:21 +0200 Subject: [Tutor] A further question about opening and closing files In-Reply-To: References: Message-ID: <201509091528.t89FSLML015602@fido.openend.se> >I did a little experiment: > >>>> f1 = open("output/test.log", 'a') >>>> f1.write("this is a test") >>>> f1.write("this is a test") >>>> f1.write('why isn\'t this writing????') >>>> f1.close() If you want the thing written out, use f1.flush() whenever you want to make sure this happens. If you want completely unbuffered writing, then you can open your file this way, with f1 = open("output/test.log", 'a', 0) I think if you are on windows you can only get unbuffered writing if you open your file in binary mode. Laura From richkappler at gmail.com Wed Sep 9 17:40:07 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 9 Sep 2015 11:40:07 -0400 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <201509091528.t89FSLML015602@fido.openend.se> References: <201509091528.t89FSLML015602@fido.openend.se> Message-ID: Thanks, tried them both, both work great on Linux. Now I understand better. regards, Richard On Wed, Sep 9, 2015 at 11:28 AM, Laura Creighton wrote: > >I did a little experiment: > > > >>>> f1 = open("output/test.log", 'a') > >>>> f1.write("this is a test") > >>>> f1.write("this is a test") > >>>> f1.write('why isn\'t this writing????') > >>>> f1.close() > > If you want the thing written out, use f1.flush() whenever you want to > make sure this happens. > > If you want completely unbuffered writing, then you can open your file > this way, with f1 = open("output/test.log", 'a', 0) I think if you are > on windows you can only get unbuffered writing if you open your file > in binary mode. > > Laura > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- All internal models of the world are approximate. ~ Sebastian Thrun From alan.gauld at btinternet.com Wed Sep 9 18:36:22 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Sep 2015 17:36:22 +0100 Subject: [Tutor] iterating through a directory In-Reply-To: References: Message-ID: On 09/09/15 15:29, richard kappler wrote: > Still not sure how to efficiently get the script to keep moving to the next > file in the directory though, in other words, for each iteration in the > loop, I want it to fetch, rename and send/save the next image in line. Hope > that brings better understanding. Sounds like you want a circular list. The traditional way to generate a circular index into a list is to use the modulo (%) operator But the itertools module gives you a better option with the cycle function: import itertools as it for img in it.cycle(os.listdir(my_img_path)): process(img) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Sep 9 18:42:05 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Sep 2015 17:42:05 +0100 Subject: [Tutor] A further question about opening and closing files In-Reply-To: References: Message-ID: On 09/09/15 15:24, richard kappler wrote: >>>> f1 = open("output/test.log", 'a') >>>> f1.write("this is a test") >>>> f1.write("this is a test") >>>> f1.write('why isn\'t this writing????') >>>> f1.close() > > monitoring test.log as I went. Nothing was written to the file until I > closed it, or at least that's the way it appeared to the text editor For a short example like this its true, for a bigger example the buffer will be flushed periodically, as it fills up. This is not a Python thing it's an OS feature, the same is true for any program. Its much more efficient use of the IO bus. (Its also why you should always explicitly close a file opened for writing - unless using with which does it for you) You can force the writes (I see Laura has shown how) but mostly you should just let the OS do it's thing. Otherwise you risk cluttering up the IO bus and preventing other programs from writing their files. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From oscar.j.benjamin at gmail.com Wed Sep 9 18:59:47 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 9 Sep 2015 17:59:47 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <55F01290.4040407@libero.it> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> Message-ID: On 9 September 2015 at 12:05, Francesco Loffredo via Tutor wrote: > Oscar Benjamin wrote: > > The problem is that there are 26 people and they are divided into > groups of 3 each day. We would like to know if it is possible to > arrange it so that each player plays each other player exactly once > over some period of days. > > It is not exactly possible to do this with 26 people in groups of 3. > Think about it from the perspective of 1 person. They must play > against all 25 other people in pairs with neither of the other people > repeated: the set of pairs they play against must partition the set of > other players. Clearly it can only work if the number of other players > is even. > > I'm not sure but I think that maybe for an exact solution you need to > have n=1(mod6) or n=3(mod6) which gives: > n = 1, 3, 7, 9, 13, 15, 19, 21, 25, 27, ... > > The formula for the number of triples if the exact solution exists is > n*(n-1)/6 which comes out as 26*25/6 = 108.33333 (the formula doesn't > give an integer because the exact solution doesn't exist). > ------------------------------------ > > A quick solution is to add one "dummy" letter to the pool of the OP's > golfers. > I used "!" as the dummy one. This way, you end up with 101 triples, 11 of > which contain the dummy player. > But this is better than the 25-item pool, that resulted in an incomplete set > of triples (for example, A would never play with Z) > So, in your real-world problem, you will have 11 groups of 2 people instead > of 3. Is this a problem? > > > import pprint, itertools > pool = "abcdefghijklmnopqrstuvwxyz!" > > def maketriples(tuplelist): > final = [] > used = set() > for a, b, c in tuplelist: > if ( ((a,b) in used) or ((b,c) in used) or ((a,c) in used) or ((b,a) > in used) or ((c,b) in used) or ((c,a) in used) ): > continue > else: > final.append((a, b, c)) > used.add((a,b)) > used.add((a,c)) > used.add((b,c)) > used.add((b,a)) > used.add((c,a)) > used.add((c,b)) > return final > > combos = list(itertools.combinations(pool, 3)) > print("combos contains %s triples." % len(combos)) > > triples = maketriples(combos) > > print("maketriples(combos) contains %s triples." % len(triples)) > pprint.pprint(triples) I don't think the code above works. For n=27 it should count 117 (according to the formula I showed) but instead it comes up with 101. I tried it with a smaller n by setting pool to range(1, 9+1) meaning that n=9. The output is: combos contains 84 triples. maketriples(combos) contains 8 triples. [(1, 2, 3), (1, 4, 5), (1, 6, 7), (1, 8, 9), (2, 4, 6), (2, 5, 7), (3, 4, 7), (3, 5, 6)] However I can construct a set of 12 triples containing each number exactly 4 times which is the exact Steiner triple system: 1 6 8 1 2 3 1 5 9 1 4 7 2 6 7 2 4 9 2 5 8 3 5 7 3 6 9 3 8 4 4 5 6 7 8 9 This is the number of triples predicted by the formula: 9*(9-1)/6 = 12 -- Oscar From fal at libero.it Wed Sep 9 19:45:43 2015 From: fal at libero.it (Francesco Loffredo) Date: Wed, 9 Sep 2015 19:45:43 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> Message-ID: <55F07047.4000803@libero.it> On 09/09/2015 18:59, Oscar Benjamin wrote: > On 9 September 2015 at 12:05, Francesco Loffredo via Tutor > wrote: >> A quick solution is to add one "dummy" letter to the pool of the OP's >> golfers. >> I used "!" as the dummy one. This way, you end up with 101 triples, 11 of >> which contain the dummy player. >> But this is better than the 25-item pool, that resulted in an incomplete set >> of triples (for example, A would never play with Z) >> So, in your real-world problem, you will have 11 groups of 2 people instead >> of 3. Is this a problem? >> >> >> import pprint, itertools >> pool = "abcdefghijklmnopqrstuvwxyz!" >> >> def maketriples(tuplelist): >> final = [] >> used = set() >> for a, b, c in tuplelist: >> if ( ((a,b) in used) or ((b,c) in used) or ((a,c) in used) or ((b,a) >> in used) or ((c,b) in used) or ((c,a) in used) ): >> continue >> else: >> final.append((a, b, c)) >> used.add((a,b)) >> used.add((a,c)) >> used.add((b,c)) >> used.add((b,a)) >> used.add((c,a)) >> used.add((c,b)) >> return final >> >> combos = list(itertools.combinations(pool, 3)) >> print("combos contains %s triples." % len(combos)) >> >> triples = maketriples(combos) >> >> print("maketriples(combos) contains %s triples." % len(triples)) >> pprint.pprint(triples) > I don't think the code above works. For n=27 it should count 117 > (according to the formula I showed) but instead it comes up with 101. > > I tried it with a smaller n by setting pool to range(1, 9+1) meaning > that n=9. The output is: > > combos contains 84 triples. > maketriples(combos) contains 8 triples. > [(1, 2, 3), > (1, 4, 5), > (1, 6, 7), > (1, 8, 9), > (2, 4, 6), > (2, 5, 7), > (3, 4, 7), > (3, 5, 6)] > > However I can construct a set of 12 triples containing each number > exactly 4 times which is the exact Steiner triple system: > > 1 6 8 > 1 2 3 > 1 5 9 > 1 4 7 > 2 6 7 > 2 4 9 > 2 5 8 > 3 5 7 > 3 6 9 > 3 8 4 > 4 5 6 > 7 8 9 > > This is the number of triples predicted by the formula: 9*(9-1)/6 = 12 > > -- > Oscar > That's very interesting! This takes me to my question to Tutors: what's wrong with the above code? Francesco From lac at openend.se Wed Sep 9 20:20:44 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 09 Sep 2015 20:20:44 +0200 Subject: [Tutor] A further question about opening and closing files In-Reply-To: References: Message-ID: <201509091820.t89IKi0F025714@fido.openend.se> In a message of Wed, 09 Sep 2015 17:42:05 +0100, Alan Gauld writes: >You can force the writes (I see Laura has shown how) but >mostly you should just let the OS do it's thing. Otherwise >you risk cluttering up the IO bus and preventing other >programs from writing their files. Is this something we have to worry about these days? I haven't worried about it for a long time, and write real time multiplayer games which demand unbuffered writes .... Of course, things would be different if I were sending gigabytes of video down the pipe, but for the sort of small writes I am doing, I don't think there is any performance problem at all. Anybody got some benchmarks so we can find out? Laura From timomlists at gmail.com Wed Sep 9 20:36:17 2015 From: timomlists at gmail.com (Timo) Date: Wed, 9 Sep 2015 20:36:17 +0200 Subject: [Tutor] More Pythonic? In-Reply-To: <20150909134108.GJ19373@ando.pearwood.info> References: <20150909134108.GJ19373@ando.pearwood.info> Message-ID: <55F07C21.7060509@gmail.com> Op 09-09-15 om 15:41 schreef Steven D'Aprano: > On Wed, Sep 09, 2015 at 09:05:23AM -0400, richard kappler wrote: >> Would either or both of these work, if both, which is the better or more >> Pythonic way to do it, and why? > The first works, but isn't really the best way to do it: > >> ####################### >> >> import whatIsNeeded >> >> writefile = open("writefile", 'a') >> >> with open(readfile, 'r') as f: >> for line in f: >> if keyword in line: >> do stuff >> f1.write(line) >> else: >> f1.write(line) >> >> writefile.close() >> >> ###################### > Better would be this: > > with open("writefile", 'a') as outfile: > with open("readfile", 'r') as infile: > for line in infile: > if keyword in line: > do stuff > outfile.write(line) > It's also possible to use multiple with statements on the same line. Can someone with more expert Python knowledge than me comment on whether it's different from using them separate as mentioned by Steven? This is what I had in mind: with open("writefile", 'a') as outfile, open("readfile", 'r') as infile: pass # Rest of the code here Timo From steve at pearwood.info Wed Sep 9 20:46:53 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 10 Sep 2015 04:46:53 +1000 Subject: [Tutor] A further question about opening and closing files In-Reply-To: References: Message-ID: <20150909184653.GK19373@ando.pearwood.info> On Wed, Sep 09, 2015 at 10:24:57AM -0400, richard kappler wrote: > Under a different subject line (More Pythonic?) Steven D'Aprano commented: > > > And this will repeatedly open the file, append one line, then close it > > again. Almost certainly not what you want -- it's wasteful and > > potentially expensive. > > And I get that. It does bring up another question though. When using > > with open(somefile, 'r') as f: > with open(filename, 'a') as f1: > for line in f: > > the file being appended is opened and stays open while the loop iterates, > then the file closes when exiting the loop, yes? The file closes when exiting the *with block*, not necessarily the loop. Consider: with open(blah blah blah) as f: for line in f: pass time.sleep(120) # file isn't closed until we get here Even if the file is empty, and there are no lines, it will be held open for two minutes. > Does this not have the > potential to be expensive as well if you are writing a lot of data to the > file? Er, expensive in what way? Yes, I suppose it is more expensive to write 1 gigabyte of data to a file than to write 1 byte. What's your point? If you want to write 1 GB, then you have to write 1 GB, and it will take as long as it takes. Look at it this way: suppose you have to hammer 1000 nails into a fence. You can grab your hammer out of your tool box, hammer one nail, put the hammer back in the tool box and close the lid, open the lid, take the hammer out again, hammer one nail, put the hammer back in the tool box, close the lid, open the lid again, take out the hammer... Or you take the hammer out, hammer 1000 nails, then put the hammer away. Sure, while you are hammering those 1000 nails, you're not mowing the lawn, painting the porch, walking the dog or any of the dozen other jobs you have to do, but you have to hammer those nails eventually. > I did a little experiment: > > >>> f1 = open("output/test.log", 'a') > >>> f1.write("this is a test") > >>> f1.write("this is a test") > >>> f1.write('why isn\'t this writing????') > >>> f1.close() > > monitoring test.log as I went. Nothing was written to the file until I > closed it, or at least that's the way it appeared to the text editor in > which I had test.log open (gedit). In gedit, when a file changes it tells > you and gives you the option to reload the file. This didn't happen until I > closed the file. So I'm presuming all the writes sat in a buffer in memory > until the file was closed, at which time they were written to the file. Correct. All modern operating systems do that. Writing to disk is slow, *hundreds of thousands of times slower* than writing to memory, so the operating system will queue up a reasonable amount of data before actually forcing it to the disk drive. > Is that actually how it happens, and if so does that not also have the > potential to cause problems if memory is a concern? No. The operating system is not stupid enough to queue up gigabytes of data. Typically the buffer is a something like 128 KB of data (I think), or maybe a MB or so. Writing a couple of short lines of text won't fill it, which is why you don't see any change until you actually close the file. Try writing a million lines, and you'll see something different. The OS will flush the buffer when it is full, or when you close the file, whichever happens first. If you know that you're going to take a long time to fill the buffer, say you're performing a really slow calculation, and your data is trickling in really slowly, then you might do a file.flush() every few seconds or so. Or if you're writing an ACID database. But for normal use, don't try to out-smart the OS, because you will fail. This is really specialised know-how. Have you noticed how slow gedit is to save files? That's because the gedit programmers thought they were smarter than the OS, so every time they write a file, they call flush() and sync(). Possibly multiple times. All that happens is that they slow the writing down greatly. Other text editors let the OS manage this process, and saving is effectively instantaneous. With gedit, there's a visible pause when it saves. (At least in all the versions of gedit I've used.) And the data is not any more safe than the other text editors, because when the OS has written to the hard drive, there is no guarantee that the data has hit the platter yet. Hard drives themselves contain buffers, and they won't actually write data to the platter until they are good and ready. -- Steve From steve at pearwood.info Wed Sep 9 20:58:22 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 10 Sep 2015 04:58:22 +1000 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <201509091820.t89IKi0F025714@fido.openend.se> References: <201509091820.t89IKi0F025714@fido.openend.se> Message-ID: <20150909185822.GL19373@ando.pearwood.info> On Wed, Sep 09, 2015 at 08:20:44PM +0200, Laura Creighton wrote: > In a message of Wed, 09 Sep 2015 17:42:05 +0100, Alan Gauld writes: > >You can force the writes (I see Laura has shown how) but > >mostly you should just let the OS do it's thing. Otherwise > >you risk cluttering up the IO bus and preventing other > >programs from writing their files. > > Is this something we have to worry about these days? I haven't > worried about it for a long time, and write real time multiplayer > games which demand unbuffered writes .... Of course, things > would be different if I were sending gigabytes of video down the > pipe, but for the sort of small writes I am doing, I don't think > there is any performance problem at all. > > Anybody got some benchmarks so we can find out? Good question! There's definitely a performance hit, but it's not as big as I expected: py> with Stopwatch(): ... with open("/tmp/junk", "w") as f: ... for i in range(100000): ... f.write("a") ... time taken: 0.129952 seconds py> with Stopwatch(): ... with open("/tmp/junk", "w") as f: ... for i in range(100000): ... f.write("a") ... f.flush() ... time taken: 0.579273 seconds What really gets expensive is doing a sync. py> with Stopwatch(): ... with open("/tmp/junk", "w") as f: ... fid = f.fileno() ... for i in range(100000): ... f.write("a") ... f.flush() ... os.fsync(fid) ... time taken: 123.283973 seconds Yes, that's right. From half a second to two minutes. -- Steve From __peter__ at web.de Wed Sep 9 20:59:42 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Sep 2015 20:59:42 +0200 Subject: [Tutor] More Pythonic? References: <20150909134108.GJ19373@ando.pearwood.info> <55F07C21.7060509@gmail.com> Message-ID: Timo wrote: > Op 09-09-15 om 15:41 schreef Steven D'Aprano: >> On Wed, Sep 09, 2015 at 09:05:23AM -0400, richard kappler wrote: >>> Would either or both of these work, if both, which is the better or more >>> Pythonic way to do it, and why? >> The first works, but isn't really the best way to do it: >> >>> ####################### >>> >>> import whatIsNeeded >>> >>> writefile = open("writefile", 'a') >>> >>> with open(readfile, 'r') as f: >>> for line in f: >>> if keyword in line: >>> do stuff >>> f1.write(line) >>> else: >>> f1.write(line) >>> >>> writefile.close() >>> >>> ###################### >> Better would be this: >> >> with open("writefile", 'a') as outfile: >> with open("readfile", 'r') as infile: >> for line in infile: >> if keyword in line: >> do stuff >> outfile.write(line) >> > It's also possible to use multiple with statements on the same line. Can > someone with more expert Python knowledge than me comment on whether > it's different from using them separate as mentioned by Steven? > > This is what I had in mind: > > with open("writefile", 'a') as outfile, open("readfile", 'r') as infile: > pass # Rest of the code here This requires Python 2.7 or higher. Other than that the choice is merely a matter of taste. Both versions even produce the same bytecode: $ cat nested_with.py def f(): with open("writefile", 'a') as outfile, open("readfile", 'r') as infile: pass # Rest of the code here def g(): with open("writefile", 'a') as outfile: with open("readfile", 'r') as infile: pass # Rest of the code here print(f.__code__.co_code == g.__code__.co_code) $ python nested_with.py True Personally I find one item per with statement more readable and don't care about the extra indentation level. From breamoreboy at yahoo.co.uk Wed Sep 9 21:33:16 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 9 Sep 2015 20:33:16 +0100 Subject: [Tutor] iterating through a directory In-Reply-To: References: Message-ID: On 09/09/2015 14:32, richard kappler wrote: > Yes, many questions today. I'm working on a data feed script that feeds > 'events' into our test environment. In production, we monitor a camera that > captures an image as product passes by, gathers information such as > barcodes and package ID from the image, and then sends out the data as a > line of xml to one place for further processing and sends the image to > another place for storage. Here is where our software takes over, receiving > the xml data and images from vendor equipment. Our software then processes > the xml data and allows retrieval of specific images associated with each > line of xml data. Our test environment must simulate the feed from the > vendor equipment, so I'm writing a script that feeds the xml from an actual > log to one place for processing, and pulls images from a pool of 20, which > we recycle, to associate with each event and sends them to another dir for > saving and searching. > > As my script iterates through each line of xml data (discussed yesterday) > to simulate the feed from the vendor camera equipment, it parses ID > information about the event then sends the line of data on. As it does so, > it needs to pull the next image in line from the image pool directory, > rename it, send it to a different directory for saving. I'm pretty solid on > all of this except iterating through the image pool. My idea is to just > keep looping through the image pool, as each line of xmldata is parsed, the > next image in line gets pulled out, renamed with the identifying > information from the xml data, and both are sent on to different places. > > I only have one idea for doing this iterating through the image pool, and > frankly I think the idea is pretty weak. The only thing I can come up with > is to change the name of each image in the pool from something > like 0219PS01CT1_20111129_000004_00044979.jpg to 1.jpg, 2.jpg, 3.jpg etc., > then doing something like: > > i = 0 > > here begins my for line in file loop: > if i == 20: > i = 1 > else: > i += 1 > do stuff with the xml file including get ID info > rename i.jpg to IDinfo.jpg > send it all on > > That seems pretty barbaric, any thoughts on where to look for better ideas? > I'm presuming there are modules out there that I am unaware of or > capabilities I am unaware of in modules I do know a little about that I am > missing. > I'm not sure what you're trying to do so maybe https://docs.python.org/3/library/collections.html#collections.deque or https://docs.python.org/3/library/itertools.html#itertools.cycle -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Wed Sep 9 21:25:06 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Sep 2015 20:25:06 +0100 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <201509091820.t89IKi0F025714@fido.openend.se> References: <201509091820.t89IKi0F025714@fido.openend.se> Message-ID: <55F08792.4090203@btinternet.com> On 09/09/15 19:20, Laura Creighton wrote: > In a message of Wed, 09 Sep 2015 17:42:05 +0100, Alan Gauld writes: >> You can force the writes (I see Laura has shown how) but >> mostly you should just let the OS do it's thing. Otherwise >> you risk cluttering up the IO bus and preventing other >> programs from writing their files. > Is this something we have to worry about these days? I haven't > worried about it for a long time, and write real time multiplayer > games which demand unbuffered writes .... Of course, things > would be different if I were sending gigabytes of video down the > pipe, but for the sort of small writes I am doing, I don't think > there is any performance problem at all. > > Anybody got some benchmarks so we can find out? > > Laura If you are working on a small platform - think mobile device - and it has a single channel bus to the storage area then one of the worst things you can do is write lots of small chunks of data to it. The overhead (in hardware) of opening and locking the bus is almost as much as the data transit time and so can choke the bus for a significant amount of time (I'm talking milliseconds here but in real-time that's significant). But even on a major OS platform bus contention does occasionally rear its head. I've seen multi-processor web servers "lock up" due to too many threads dumping data at once. Managing the data bus is (part of) what the OS is there to do, it's best to let it do its job, second guessing it is rarely the right thing. Remember, the impact is never on your own program it's on all the other processes running on the same platform. There are usually tools to monitor the IO bus performance though, so it's fairly easy to diagnose/check. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Wed Sep 9 21:42:20 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 09 Sep 2015 21:42:20 +0200 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <55F08792.4090203@btinternet.com> References: <201509091820.t89IKi0F025714@fido.openend.se> <55F08792.4090203@btinternet.com> Message-ID: <201509091942.t89JgKQ3013823@fido.openend.se> In a message of Wed, 09 Sep 2015 20:25:06 +0100, Alan Gauld writes: >On 09/09/15 19:20, Laura Creighton wrote: >If you are working on a small platform - think mobile device - and it has >a single channel bus to the storage area then one of the worst things >you can do is write lots of small chunks of data to it. The overhead >(in hardware) of opening and locking the bus is almost as much as >the data transit time and so can choke the bus for a significant amount >of time (I'm talking milliseconds here but in real-time that's significant). But if I shoot you with my laser cannon, I want you to get the message that you are dead _now_ and not when some buffer fills up ... Laura From alan.gauld at btinternet.com Thu Sep 10 00:19:43 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Sep 2015 23:19:43 +0100 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <201509091942.t89JgKQ3013823@fido.openend.se> References: <201509091820.t89IKi0F025714@fido.openend.se> <55F08792.4090203@btinternet.com> <201509091942.t89JgKQ3013823@fido.openend.se> Message-ID: <55F0B07F.9010309@btinternet.com> On 09/09/15 20:42, Laura Creighton wrote: > In a message of Wed, 09 Sep 2015 20:25:06 +0100, Alan Gauld writes: >> On 09/09/15 19:20, Laura Creighton wrote: >> If you are working on a small platform - think mobile device - and it has >> a single channel bus to the storage area then one of the worst things >> you can do is write lots of small chunks of data to it. The overhead >> (in hardware) of opening and locking the bus is almost as much as >> the data transit time and so can choke the bus for a significant amount >> of time (I'm talking milliseconds here but in real-time that's significant). > But if I shoot you with my laser cannon, I want you to get the > message that you are dead _now_ and not when some buffer fills up ... There are two things about that: 1) human reaction time is measured in 100s of milliseconds so the delay is not likely to be meaningful. If you do the flushes every 10ms instead of every write (assuming you are writing frequently) nobody is likely to notice. 2) Gamers tend not to be doing other things while playing, so you can pretty much monopolize the bus if you want to, So if you know that you're the only game in town(sic) then go ahead and flush everything to disk. It won't do much harm. But... ..., if your game engine is running on a server shared by other users and some of them are running critical apps (think a businesses billing or accounting suite that must complete its run within a 1 hour window say) then you become very unpopular quickly. In practice that means the sys admin will see who is flattening the bus and nice that process down till it stops hurting the others. That means your game now runs at 10% the CPU power it had a while ago... As programmers we very rarely have the control over our environment that we like to think we do. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From fal at libero.it Thu Sep 10 09:45:10 2015 From: fal at libero.it (Francesco Loffredo) Date: Thu, 10 Sep 2015 09:45:10 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <55F07047.4000803@libero.it> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> <55F07047.4000803@libero.it> Message-ID: <55F13506.4030608@libero.it> On 09/09/2015 19:45, Francesco Loffredo via Tutor wrote: > On 09/09/2015 18:59, Oscar Benjamin wrote: >> I don't think the code above works. For n=27 it should count 117 >> (according to the formula I showed) but instead it comes up with 101. >> >> I tried it with a smaller n by setting pool to range(1, 9+1) meaning >> that n=9. The output is: >> >> combos contains 84 triples. >> maketriples(combos) contains 8 triples. >> [(1, 2, 3), >> (1, 4, 5), >> (1, 6, 7), >> (1, 8, 9), >> (2, 4, 6), >> (2, 5, 7), >> (3, 4, 7), >> (3, 5, 6)] >> >> However I can construct a set of 12 triples containing each number >> exactly 4 times which is the exact Steiner triple system: >> >> 1 6 8 >> 1 2 3 >> 1 5 9 >> 1 4 7 >> 2 6 7 >> 2 4 9 >> 2 5 8 >> 3 5 7 >> 3 6 9 >> 3 8 4 >> 4 5 6 >> 7 8 9 >> >> This is the number of triples predicted by the formula: 9*(9-1)/6 = 12 >> >> -- >> Oscar >> > That's very interesting! This takes me to my question to Tutors: > what's wrong with the above code? I wrote a small routine (below) to check when and if my code and the formula do match. It easily shows that they only match for len(pool) == (2 ** N) - 1, with N greater or equal to 2. My problem remains: WHY don't they match for every length? How did you build your 12-triples set? What's wrong with my algorithm? And, most of all (and on topic, too): how can you build a Python program that builds your triples list? Francesco -------------------------------------------------------------------------------------------------------------------------------------- import pprint, itertools poolbase = "abcdefghijklmnopqrstuvwxyz!ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789" def maketriples(tuplelist): final = [] used = set() for a, b, c in tuplelist: if ( ((a,b) in used) or ((b,c) in used) or ((a,c) in used) or ((b,a) in used) or ((c,b) in used) or ((c,a) in used) ): continue else: final.append((a, b, c)) used.add((a,b)) used.add((a,c)) used.add((b,c)) used.add((b,a)) used.add((c,a)) used.add((c,b)) return final def formula(sequence): dim = len(sequence) return (dim * (dim - 1) / 6) matching = {} for i in range(3, len(poolbase) + 1): pool = poolbase[:i] print("pool = %s: -> '%s'" % (i, pool)) combos = list(itertools.combinations(pool, 3)) print("combos contains %s triples." % len(combos)) triples = maketriples(combos) theory = formula(pool) print("maketriples(combos) yields %s triples." % len(triples)) print("formula(pool) gives %s." % theory) if len(triples) == theory: pprint.pprint(triples) matching[len(pool)] = len(triples) input("\n--- Press Enter ---") print("-------------------------------------") print("Dict of matching solutions and number of obtained triples:") pprint.pprint(matching) From richkappler at gmail.com Thu Sep 10 17:38:54 2015 From: richkappler at gmail.com (richard kappler) Date: Thu, 10 Sep 2015 11:38:54 -0400 Subject: [Tutor] indent error on if/elif/else Message-ID: Here's my code, no tabs were used, all whitespace verified made with spacebar: print("Please enter a number for feed speed...") print("1 - Batch") print("2 - 2 per second") print("3 - Real Time") print("4 - Exit") if x == ord('1'): delay = 0 elif x == ord('2'): delay = 0.5 elif x == ord('3'): # set delay real time # read timestamp from line, create an offset # send lin iaw server time with offset else: print("bad choice, exiting") os.exit() ################# Here's the Traceback: File "dataFeedTest.py", line 44 else: ^ IndentationError: expected an indented block Not a clue why it's doing this. Any help? regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From mail at timgolden.me.uk Thu Sep 10 17:41:42 2015 From: mail at timgolden.me.uk (Tim Golden) Date: Thu, 10 Sep 2015 16:41:42 +0100 Subject: [Tutor] indent error on if/elif/else In-Reply-To: References: Message-ID: <55F1A4B6.7080706@timgolden.me.uk> On 10/09/2015 16:38, richard kappler wrote: > Here's my code, no tabs were used, all whitespace verified made with > spacebar: > > print("Please enter a number for feed speed...") > print("1 - Batch") > print("2 - 2 per second") > print("3 - Real Time") > print("4 - Exit") > > if x == ord('1'): > delay = 0 > elif x == ord('2'): > delay = 0.5 > elif x == ord('3'): > # set delay real time > # read timestamp from line, create an offset > # send lin iaw server time with offset > else: > print("bad choice, exiting") > os.exit() > > ################# > > Here's the Traceback: > > File "dataFeedTest.py", line 44 > else: > ^ > IndentationError: expected an indented block > > > Not a clue why it's doing this. Any help? Because the comments don't qualify as a statement. You need to have at least a "pass" in there. TJG From lac at openend.se Thu Sep 10 17:44:27 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 10 Sep 2015 17:44:27 +0200 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <55F0B07F.9010309@btinternet.com> References: <201509091820.t89IKi0F025714@fido.openend.se> <55F08792.4090203@btinternet.com> <201509091942.t89JgKQ3013823@fido.openend.se> <55F0B07F.9010309@btinternet.com> Message-ID: <201509101544.t8AFiRHD018295@fido.openend.se> In a message of Wed, 09 Sep 2015 23:19:43 +0100, Alan Gauld writes: >..., if your game engine is running on a server shared by other users and >some of them are running critical apps (think a businesses billing or >accounting suite that must complete its run within a 1 hour window say) >then >you become very unpopular quickly. In practice that means the sys admin >will >see who is flattening the bus and nice that process down till it stops >hurting >the others. That means your game now runs at 10% the CPU power it had >a while ago... We were talking about mobile devices ... These days every business billing and accounting suite I know of -- and we _make_ a bookkeeping suite, which we need to interface with many other things .... runs in its own VM and doesn't share with anybody. multiplayer games tend to run in their own VM as well, and too many users using the game can and does degrade performance. But I don't think that having too many users flush their stream objects is a significant part of this problem, compared to the problems of getting your bits out of your graphics card, for instance. Laura From alan.gauld at btinternet.com Thu Sep 10 18:17:26 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Sep 2015 17:17:26 +0100 Subject: [Tutor] A further question about opening and closing files In-Reply-To: <201509101544.t8AFiRHD018295@fido.openend.se> References: <201509091820.t89IKi0F025714@fido.openend.se> <55F08792.4090203@btinternet.com> <201509091942.t89JgKQ3013823@fido.openend.se> <55F0B07F.9010309@btinternet.com> <201509101544.t8AFiRHD018295@fido.openend.se> Message-ID: On 10/09/15 16:44, Laura Creighton wrote: >> ..., if your game engine is running on a server shared by other users and >> some of them are running critical apps (think a businesses billing or > We were talking about mobile devices ... Ok, in that case I'd guess the client playing the game is only interested in the game so won't care about impact on other processes. > These days every business billing and accounting suite I know of -- > and we _make_ a bookkeeping suite, which we need to interface > with many other things .... > > runs in its own VM and doesn't share with anybody. But it does. The physical file system and its IO bus is still shared by every VM and therefore by every app on every VM. And when you do a flush you force the VM to write to the physical IO bus. Even if the VM uses a virtual file system (which is no good if you are sharing with other VMs in a common database environment) it will almost certainly be manifested as a physical file eventually. If you are lucky the VM will defer flushes to the physical FS but that is normally a configurable parameter. One of the biggest problems facing modern data centres is how to manage access to physical devices from virtual environments. It is a very delicate balancing act and very easy to get wrong. And where in the past a mistake upset a handful of apps it now impacts hundreds. We are still in early days with VM technology and the monitoring tools are improving and the hardware architectures are developing with parallel buses etc to avoid these kinds of problems. But most of the data centres I work with are still running at least 50% capacity on pre VM-optimised servers. > multiplayer games tend to run in their own VM as well, and too many > users using the game can and does degrade performance. But I don't > think that having too many users flush their stream objects is a > significant part of this problem, Regular flushes hardly ever affect the program doing the flushing. It's whatever is sharing the IO bus. Remember the issue is the physical hardware bus not the software environment. If you are writing to your own file system over a segregated bus (and there are several technologies for doing that (SAN, NAS, multiplexed buses etc etc)) then its not usually an issue. It's only if you are on a machine where you share a single IO channel with other apps that problems can occur - for the other apps. However, I suspect we might now be getting a wee bit OT for most readers on this list! So I will put my architect's hat away again. :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From ben+python at benfinney.id.au Thu Sep 10 18:18:09 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 11 Sep 2015 02:18:09 +1000 Subject: [Tutor] indent error on if/elif/else References: Message-ID: <85zj0u6yf2.fsf@benfinney.id.au> richard kappler writes: > Here's the Traceback: > > File "dataFeedTest.py", line 44 > else: > ^ > IndentationError: expected an indented block A comment is not a statement. So, your second ?elif? block is empty, then immediately followed by an ?else?; Python expected an indented block *of statements*. Yes, that's not exactly clear from the error message; don't feel bad :-) > Not a clue why it's doing this. Any help? If you want to show ?I will write something useful here later?, try writing a named function, and immediately write an empty function that fits that interface:: def set_delay_real_time(): pass def read_timestamp(line): pass def create_offset(timestamp): pass def send_lin_iaw_time(timestamp, offset): pass if x == ord('1'): delay = 0 elif x == ord('2'): delay = 0.5 elif x == ord('3'): set_delay_real_time() timestamp = read_timestamp(line) offset = create_offset(timestamp) send_lin_iaw_server_time(timestamp, offset) else: print("bad choice, exiting") os.exit() That way, you express your intent in actual function names, which is vital to good code. You are exercising the discipline to think about what function to write at exactly the point where that's most important, i.e. at the point where the function is used. -- \ ?To have the choice between proprietary software packages, is | `\ being able to choose your master. Freedom means not having a | _o__) master.? ?Richard M. Stallman, 2007-05-16 | Ben Finney From sarika1989.08 at gmail.com Thu Sep 10 19:00:24 2015 From: sarika1989.08 at gmail.com (Sarika Shrivastava) Date: Thu, 10 Sep 2015 22:30:24 +0530 Subject: [Tutor] Question how to ready data from excle file and convert the german to english language Message-ID: Hello pythonistats, I wanted to ready data from excel file which in german Language and onovert to English language ?? -- Thanks Sarika Shrivastava | Software Developer Trainee www.zeomega.com From fiberfolly at gmail.com Fri Sep 11 00:46:18 2015 From: fiberfolly at gmail.com (D Wyatt) Date: Thu, 10 Sep 2015 15:46:18 -0700 Subject: [Tutor] About using list in a function In-Reply-To: References: Message-ID: Scrambled on gmail here too. From alan.gauld at btinternet.com Fri Sep 11 02:32:43 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 11 Sep 2015 01:32:43 +0100 Subject: [Tutor] Question how to ready data from excle file and convert the german to english language In-Reply-To: References: Message-ID: On 10/09/15 18:00, Sarika Shrivastava wrote: > I wanted to ready data from excel file which in german Language and > onovert to English language ?? Those are two completely separate questions. To read Excel there are several options. The simplest is if you can convert the file to csv and use the csv module from the standard library. Failing that Google python excel reader, you should come up with something like xlrd or openpyxl To translate from German to English you probably should look into the API from Google for their translator. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From richkappler at gmail.com Fri Sep 11 18:00:12 2015 From: richkappler at gmail.com (richard kappler) Date: Fri, 11 Sep 2015 12:00:12 -0400 Subject: [Tutor] ftp socket.error Message-ID: I can connect via ftp, but when I try to send a file, I get a no route to host error, I don't understand. code: >>> import ftplib >>> from ftplib import FTP >>> fBOT = FTP() >>> oldfile = '/home/test/DataFeed/input/images/BOT/1.jpg' >>> newfile = 'new.jpg' >>> oldfile = open('/home/test/DataFeed/input/images/BOT/1.jpg', 'rb') >>> fBOT.connect('192.168.2.23', 2021) '220 Service ready for new user.' >>> fBOT.login('BOT', 'sick') '230 User logged in, proceed.' >>> fBOT.storbinary('STOR newfile', oldfile) Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.6/ftplib.py", line 452, in storbinary conn = self.transfercmd(cmd) File "/usr/lib64/python2.6/ftplib.py", line 360, in transfercmd return self.ntransfercmd(cmd, rest)[0] File "/usr/lib64/python2.6/ftplib.py", line 326, in ntransfercmd conn = socket.create_connection((host, port), self.timeout) File "/usr/lib64/python2.6/socket.py", line 567, in create_connection raise error, msg socket.error: [Errno 113] No route to host Any ideas? regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From reuben.dlink at gmail.com Fri Sep 11 18:16:54 2015 From: reuben.dlink at gmail.com (Reuben) Date: Fri, 11 Sep 2015 21:46:54 +0530 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: <65E56298-1A72-4C4B-AD0C-3DE25EF4B697@gmail.com> Check if the ftp server ip is pinging Sent from my iPhone > On 11-Sep-2015, at 9:30 pm, richard kappler wrote: > > I can connect via ftp, but when I try to send a file, I get a no route to > host error, I don't understand. > > code: > >>>> import ftplib >>>> from ftplib import FTP >>>> fBOT = FTP() >>>> oldfile = '/home/test/DataFeed/input/images/BOT/1.jpg' >>>> newfile = 'new.jpg' >>>> oldfile = open('/home/test/DataFeed/input/images/BOT/1.jpg', 'rb') >>>> fBOT.connect('192.168.2.23', 2021) > '220 Service ready for new user.' >>>> fBOT.login('BOT', 'sick') > '230 User logged in, proceed.' >>>> fBOT.storbinary('STOR newfile', oldfile) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib64/python2.6/ftplib.py", line 452, in storbinary > conn = self.transfercmd(cmd) > File "/usr/lib64/python2.6/ftplib.py", line 360, in transfercmd > return self.ntransfercmd(cmd, rest)[0] > File "/usr/lib64/python2.6/ftplib.py", line 326, in ntransfercmd > conn = socket.create_connection((host, port), self.timeout) > File "/usr/lib64/python2.6/socket.py", line 567, in create_connection > raise error, msg > socket.error: [Errno 113] No route to host > > Any ideas? > > regards, Richard > > -- > > All internal models of the world are approximate. ~ Sebastian Thrun > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From richkappler at gmail.com Fri Sep 11 18:17:54 2015 From: richkappler at gmail.com (richard kappler) Date: Fri, 11 Sep 2015 12:17:54 -0400 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: Figured it out. On the receiving machine I had to # modprobe ip_conntrack_ftp On Fri, Sep 11, 2015 at 12:00 PM, richard kappler wrote: > I can connect via ftp, but when I try to send a file, I get a no route to > host error, I don't understand. > > code: > > >>> import ftplib > >>> from ftplib import FTP > >>> fBOT = FTP() > >>> oldfile = '/home/test/DataFeed/input/images/BOT/1.jpg' > >>> newfile = 'new.jpg' > >>> oldfile = open('/home/test/DataFeed/input/images/BOT/1.jpg', 'rb') > >>> fBOT.connect('192.168.2.23', 2021) > '220 Service ready for new user.' > >>> fBOT.login('BOT', 'sick') > '230 User logged in, proceed.' > >>> fBOT.storbinary('STOR newfile', oldfile) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib64/python2.6/ftplib.py", line 452, in storbinary > conn = self.transfercmd(cmd) > File "/usr/lib64/python2.6/ftplib.py", line 360, in transfercmd > return self.ntransfercmd(cmd, rest)[0] > File "/usr/lib64/python2.6/ftplib.py", line 326, in ntransfercmd > conn = socket.create_connection((host, port), self.timeout) > File "/usr/lib64/python2.6/socket.py", line 567, in create_connection > raise error, msg > socket.error: [Errno 113] No route to host > > Any ideas? > > regards, Richard > > -- > > All internal models of the world are approximate. ~ Sebastian Thrun > -- All internal models of the world are approximate. ~ Sebastian Thrun From richkappler at gmail.com Fri Sep 11 18:47:33 2015 From: richkappler at gmail.com (richard kappler) Date: Fri, 11 Sep 2015 12:47:33 -0400 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: No, apparently I didn't figure it out. I thought I had as after the modprobe I was getting a an EOFError, but now I'm getting the no route to host error again. I can ping it, and as you can see from the original post, I am able to establish a connection and log in, it's just when I try to send a file it goes bollocks up. Still need ideas. regards, Richard On Fri, Sep 11, 2015 at 12:17 PM, richard kappler wrote: > Figured it out. On the receiving machine I had to > > # modprobe ip_conntrack_ftp > > > On Fri, Sep 11, 2015 at 12:00 PM, richard kappler > wrote: > >> I can connect via ftp, but when I try to send a file, I get a no route to >> host error, I don't understand. >> >> code: >> >> >>> import ftplib >> >>> from ftplib import FTP >> >>> fBOT = FTP() >> >>> oldfile = '/home/test/DataFeed/input/images/BOT/1.jpg' >> >>> newfile = 'new.jpg' >> >>> oldfile = open('/home/test/DataFeed/input/images/BOT/1.jpg', 'rb') >> >>> fBOT.connect('192.168.2.23', 2021) >> '220 Service ready for new user.' >> >>> fBOT.login('BOT', 'sick') >> '230 User logged in, proceed.' >> >>> fBOT.storbinary('STOR newfile', oldfile) >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib64/python2.6/ftplib.py", line 452, in storbinary >> conn = self.transfercmd(cmd) >> File "/usr/lib64/python2.6/ftplib.py", line 360, in transfercmd >> return self.ntransfercmd(cmd, rest)[0] >> File "/usr/lib64/python2.6/ftplib.py", line 326, in ntransfercmd >> conn = socket.create_connection((host, port), self.timeout) >> File "/usr/lib64/python2.6/socket.py", line 567, in create_connection >> raise error, msg >> socket.error: [Errno 113] No route to host >> >> Any ideas? >> >> regards, Richard >> >> -- >> >> All internal models of the world are approximate. ~ Sebastian Thrun >> > > > > -- > > All internal models of the world are approximate. ~ Sebastian Thrun > -- All internal models of the world are approximate. ~ Sebastian Thrun From martin at linux-ip.net Sat Sep 12 00:22:48 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Fri, 11 Sep 2015 15:22:48 -0700 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: Hi there Richard, Strictly speaking, it's no Python question, but... good ol' FTP. >>> socket.error: [Errno 113] No route to host Your program is receiving an EHOSTUNREACH. >>> import errno >>> errno.errorcode[113] 'EHOSTUNREACH' This occurs at precisely the moment that your program is trying to initiate a data transfer. Every firewall administrator in the world loves FTP for precisely this reason. (And, when I say "love", you can replace this with a verb or of your choice.) Without packet captures, I will merely guess (based on experience). 1. The receiving machine is running the Python program, builds a connection on port 21 (this is called the FTP command channel), you log in and all is well. 2. The moment you try to transfer any data, the FTP client (your receiving machine) and the FTP server negotiate either FTP passive mode or FTP active (retronym) mode. I'm guessing that your FTP client is choosing passive mode. (Your FTP client might produce logging of this negotiation.) 3. Using the connection information, the client attempts to build an FTP data channel. So, your machine running the Python program initiates a connection to the FTP server. 4. The FTP server is (probably) configured to allow connections inbound to TCP/21 (FTP Command Channel), but doesn't know to allow the connections to the ephemeral port(s) negotiated during step 2 (above). So, the firewall on the FTP Server sends an ICMP Type 3, Code 1 [0]. >> Figured it out. On the receiving machine I had to >> >> # modprobe ip_conntrack_ftp Right instinct! Try this same command on the FTP server side. Unless your Python FTP client is negotiating active mode, the server will be the "problem" in this case. > No, apparently I didn't figure it out. I thought I had as after > the modprobe I was getting a an EOFError, but now I'm getting the > no route to host error again. I can ping it, and as you can see > from the original post, I am able to establish a connection and > log in, it's just when I try to send a file it goes bollocks up. > Still need ideas. Hopefully, my idea #1 helps. (If not, you'll need to do some packet captures and probably crank up the logging on the FTP server, too.) I do have another idea, though. Have you ever wondered about the slow demise of FTP? All of this command-channel, data-channel, PORT or PASV nonsense goes away when you use a protocol that runs over a single TCP port. Worked fine in the early days of the Internet before firewalls and NAT. Anyway, short idea #2: If it's anonymous access, use HTTP. If authenticated access, use ssh/scp/sftp. Good luck, -Martin [0] http://www.networksorcery.com/enp/protocol/icmp/msg3.htm -- Martin A. Brown http://linux-ip.net/ From breamoreboy at yahoo.co.uk Sat Sep 12 05:16:20 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 12 Sep 2015 04:16:20 +0100 Subject: [Tutor] About using list in a function In-Reply-To: References: Message-ID: On 10/09/2015 23:46, D Wyatt wrote: > Scrambled on gmail here too. Please provide some context when you reply, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From nanney.56 at gmail.com Sat Sep 12 05:45:54 2015 From: nanney.56 at gmail.com (Robert Nanney) Date: Fri, 11 Sep 2015 22:45:54 -0500 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: I may be mistaken, but it looks like you are trying to open the socket on port 2021. Standard ftp uses 21. Is the server listening on 2021? On Sep 11, 2015 5:29 PM, "Martin A. Brown" wrote: > > Hi there Richard, > > Strictly speaking, it's no Python question, but... good ol' FTP. > > socket.error: [Errno 113] No route to host >>>> >>> > Your program is receiving an EHOSTUNREACH. > > >>> import errno > >>> errno.errorcode[113] > 'EHOSTUNREACH' > > This occurs at precisely the moment that your program is trying to > initiate a data transfer. Every firewall administrator in the world loves > FTP for precisely this reason. (And, when I say "love", you can replace > this with a verb or of your choice.) > > Without packet captures, I will merely guess (based on experience). > > 1. The receiving machine is running the Python program, builds a > connection on port 21 (this is called the FTP command > channel), you log in and all is well. > 2. The moment you try to transfer any data, the FTP client (your > receiving machine) and the FTP server negotiate either FTP > passive mode or FTP active (retronym) mode. I'm guessing > that your FTP client is choosing passive mode. (Your FTP > client might produce logging of this negotiation.) > 3. Using the connection information, the client attempts to build > an FTP data channel. So, your machine running the Python > program initiates a connection to the FTP server. > 4. The FTP server is (probably) configured to allow connections > inbound to TCP/21 (FTP Command Channel), but doesn't know to > allow the connections to the ephemeral port(s) negotiated > during step 2 (above). So, the firewall on the FTP Server > sends an ICMP Type 3, Code 1 [0]. > > Figured it out. On the receiving machine I had to >>> >>> # modprobe ip_conntrack_ftp >>> >> > Right instinct! Try this same command on the FTP server side. Unless your > Python FTP client is negotiating active mode, the server will be the > "problem" in this case. > > No, apparently I didn't figure it out. I thought I had as after the >> modprobe I was getting a an EOFError, but now I'm getting the no route to >> host error again. I can ping it, and as you can see from the original post, >> I am able to establish a connection and log in, it's just when I try to >> send a file it goes bollocks up. Still need ideas. >> > > Hopefully, my idea #1 helps. (If not, you'll need to do some packet > captures and probably crank up the logging on the FTP server, too.) > > I do have another idea, though. Have you ever wondered about the slow > demise of FTP? All of this command-channel, data-channel, PORT or PASV > nonsense goes away when you use a protocol that runs over a single TCP > port. Worked fine in the early days of the Internet before firewalls and > NAT. > > Anyway, short idea #2: > > If it's anonymous access, use HTTP. > If authenticated access, use ssh/scp/sftp. > > Good luck, > > -Martin > > [0] http://www.networksorcery.com/enp/protocol/icmp/msg3.htm > > -- > Martin A. Brown > http://linux-ip.net/ > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From martin at linux-ip.net Sat Sep 12 18:24:09 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sat, 12 Sep 2015 09:24:09 -0700 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: Hello and good morning > I may be mistaken, but it looks like you are trying to open the > socket on port 2021. Standard ftp uses 21. Is the server listening > on 2021? Ooof! And, in fact, that is a great point! I overlooked that in the original snippet! Everything I wrote still stands, except that you need to tell the ip_conntrack_ftp (or nf_conntrack_ftp) kernel module to watch for a command channel on TCP/2021. modprobe ip_conntrack_ftp ports=21,2021 That means that the ip_conntrack_ftp module will watch flows on both ports. I'm glad you observed that important detail, Robert! -Martin >> Strictly speaking, it's no Python question, but... good ol' FTP. >> >> socket.error: [Errno 113] No route to host >>>>> >>>> >> Your program is receiving an EHOSTUNREACH. >> >> >>> import errno >> >>> errno.errorcode[113] >> 'EHOSTUNREACH' >> >> This occurs at precisely the moment that your program is trying to >> initiate a data transfer. Every firewall administrator in the world loves >> FTP for precisely this reason. (And, when I say "love", you can replace >> this with a verb or of your choice.) >> >> Without packet captures, I will merely guess (based on experience). >> >> 1. The receiving machine is running the Python program, builds a >> connection on port 21 (this is called the FTP command >> channel), you log in and all is well. >> 2. The moment you try to transfer any data, the FTP client (your >> receiving machine) and the FTP server negotiate either FTP >> passive mode or FTP active (retronym) mode. I'm guessing >> that your FTP client is choosing passive mode. (Your FTP >> client might produce logging of this negotiation.) >> 3. Using the connection information, the client attempts to build >> an FTP data channel. So, your machine running the Python >> program initiates a connection to the FTP server. >> 4. The FTP server is (probably) configured to allow connections >> inbound to TCP/21 (FTP Command Channel), but doesn't know to >> allow the connections to the ephemeral port(s) negotiated >> during step 2 (above). So, the firewall on the FTP Server >> sends an ICMP Type 3, Code 1 [0]. >> >> Figured it out. On the receiving machine I had to >>>> >>>> # modprobe ip_conntrack_ftp >>>> >>> >> Right instinct! Try this same command on the FTP server side. Unless your >> Python FTP client is negotiating active mode, the server will be the >> "problem" in this case. >> >> No, apparently I didn't figure it out. I thought I had as after the >>> modprobe I was getting a an EOFError, but now I'm getting the no route to >>> host error again. I can ping it, and as you can see from the original post, >>> I am able to establish a connection and log in, it's just when I try to >>> send a file it goes bollocks up. Still need ideas. >>> >> >> Hopefully, my idea #1 helps. (If not, you'll need to do some packet >> captures and probably crank up the logging on the FTP server, too.) >> >> I do have another idea, though. Have you ever wondered about the slow >> demise of FTP? All of this command-channel, data-channel, PORT or PASV >> nonsense goes away when you use a protocol that runs over a single TCP >> port. Worked fine in the early days of the Internet before firewalls and >> NAT. >> >> Anyway, short idea #2: >> >> If it's anonymous access, use HTTP. >> If authenticated access, use ssh/scp/sftp. >> >> Good luck, >> >> -Martin >> >> [0] http://www.networksorcery.com/enp/protocol/icmp/msg3.htm >> >> -- >> Martin A. Brown >> http://linux-ip.net/ >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > -- Martin A. Brown http://linux-ip.net/ From manjunatha.mahalingappa at gmail.com Sun Sep 13 09:29:02 2015 From: manjunatha.mahalingappa at gmail.com (Manju M) Date: Sun, 13 Sep 2015 00:29:02 -0700 Subject: [Tutor] =?utf-8?q?=E2=80=8BHow_to_use_the_returned_telnet_object_?= =?utf-8?q?after_creating_the_telnet_session=2E?= Message-ID: Hello all, First I would like thank you for creating such good platform for discussing python..!!! Assume that I will pass IP and port information from a function to open the telnet session. have opened the telnet session and after opening the telnet session I returned telnet object to calling function. Now in the calling function If I use that object to read or write to terminal I'm getting ERROR ?AttributeError: 'NoneType' object has no attribute 'read_very_eager'?. #Open telnet connection to devices def open_telnet_conn(dname,ip,port): try: TELNET_PORT = port TELNET_TIMEOUT = 5 READ_TIMEOUT = 5 cmd = "show config" #Logging into device connection=telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT) time.sleep(1) connection.write(cmd + "\n") #Here I'm able to write to connection object.. connection.write("\n") time.sleep(2) router_output = connection.read_very_eager() print router_output return(connection) except IOError: print "Input parameter error! Please check username, password and file name." #Function to read device IP and port . and this info for opening the telnet session. def IP_port(file): T = [] F = open(file,'r') F.seek(0) line=F.read() tuples = re.findall(r'(.+?)\s+(.+?)\s+(\d+)',line) #(dname,IP,port)= tuples for (dname,ip,port) in tuples: T1=open_telnet_conn(dname,ip,port) #HERE I will get the telnet object point to the same location as the connection object. But I'm unable to write or read anything here. I think need to convert the object T1 to TELNET CLASS object type.. print T1 T1.write("show config") <<<<<<<< References: Message-ID: On 13/09/15 08:29, Manju M wrote: > Assume that I will pass IP and port information from a function to open the > telnet session. have opened the telnet session and after opening the telnet > session I returned telnet object to calling function. That makes sense so far. Unfortunately its hard to read your code below as it has lost all line formatting. Usually this is because you have used HTML mail. It helps a lot if you post code as plain text. > Now in the calling function If I use that object to read or write to > terminal I'm getting ERROR ?AttributeError: 'NoneType' object has no > attribute 'read_very_eager'?. A None type object often means you have a second return path from your function that does not explicitly return anything. For example in your function, if there is an exception you will return None. Looking at your code: > def open_telnet_conn(dname,ip,port): You should probably set the port to some default value... > try: > TELNET_PORT = port > TELNET_TIMEOUT = 5 > READ_TIMEOUT = 5 > cmd = "show config" > #Logging into device > > connection=telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT) > > time.sleep(1) > connection.write(cmd + "\n") > > #Here I'm able to write to connection object.. > connection.write("\n") > time.sleep(2) > > router_output = connection.read_very_eager() > print router_output What do you get printed here? > > return(connection) > except IOError: > > print "Input parameter error! Please check username, password and file > name." It looks like you always return connection unless you get an exception which generates an error message. I assume you do not see the error printed? What happens if the connection does not succeed? Do you only get IOError for all of the operations you try? Could there be other errors? Are there? > #Function to read device IP and port . and this info for opening the telnet > session. > def IP_port(file): > T = [] > F = open(file,'r') > F.seek(0) You don't need to seek(0) on a newly opened file. It starts at 0 automatically. > line=F.read() > tuples = re.findall(r'(.+?)\s+(.+?)\s+(\d+)',line) I didn't check the regex but I assume you have printed out tuples to ensure its correct? > for (dname,ip,port) in tuples: > T1=open_telnet_conn(dname,ip,port) > #HERE I will get the telnet object point to the same location as the > connection object. But I'm unable to write or read anything here. I think > need to convert the object T1 to TELNET CLASS object type.. Python doesn't need type conversions of that sort. You are returning a telnet object from the function so the T1 variable will reference a telnet object. > print T1 what prints here? > T1.write("show config") <<<<<<<< Show us the full error text. Python errors contain lots of useful information but without seeing exactly what it says we are partly guessing. > router_output = T1.read_very_eager() > print router_output > T.append(T1) > return(T) > > > # import the LC/RSP name ,port and IP address > file='/users/manmahal/MANJU/IP_port.txt' > list_of_telnet=IP_port(file) > > > > *NOTE*: the content of the file is as below: > > RSP1 172.27.40.60 2002 Please tell us more about the output. And please repost the code in plain text. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From nymcity at yahoo.com Sun Sep 13 16:20:00 2015 From: nymcity at yahoo.com (Nym City) Date: Sun, 13 Sep 2015 14:20:00 +0000 (UTC) Subject: [Tutor] Syntax error and EOL Error In-Reply-To: References: Message-ID: <1161896359.1681290.1442154000420.JavaMail.yahoo@mail.yahoo.com> Hello, Sorry for the late response. It took me sometime to find the solution. Below is my updated code which seems to be working fine now. Just wanted to share with the group here. import csv DomainList = [] domains = open('domainlist.csv', 'r') DomainList = csv.reader(domains) DomainList = [column[1] for column in DomainList] strip_list = [item.rstrip('/') for item in DomainList] print('\n'.join(strip_list)) ?Thank you. On Monday, September 7, 2015 5:25 AM, Alan Gauld wrote: On 07/09/15 01:40, Nym City via Tutor wrote: > Hello, > Thank you for your response. I have made updates and here is the new code: > import csv > DomainList = [] > > domains = open('domainlist.csv', 'rb') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList(str.rstrip('/'))] Since DomainList is a csv.reader object what made you think you could call it with a string argument? You were not doing that previously? > For "DomainList = [column[1] for column in DomainList(str.rstrip('/'))]" > line, I am getting following error:TypeError: '_csv.reader' object is not callable Which is true, and should be obvious how to fix. Stop calling it. That means don't have parentheses after it. > Doing some research, i thought it might be because I am importing > the csv as 'r' only You are not importing the csv as 'r' you are opening your file as 'r'. This has nothing to do with the error. That is because you are calling a non callable object. > Also, not sure if the next error is because of the above issue but > I get syntax error when I do the last print Can you tell us how you managed to get that second error? The interpreter should have stopped after the first error so you should never get a second error. You must have modified the code in some way. You would need to show us the actual code you ran to reach the print statement, otherwise we can't begin to guess what might be causing the syntax error. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From dyoo at hashcollision.org Sun Sep 13 21:01:03 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 13 Sep 2015 12:01:03 -0700 Subject: [Tutor] Syntax error and EOL Error In-Reply-To: <1161896359.1681290.1442154000420.JavaMail.yahoo@mail.yahoo.com> References: <1161896359.1681290.1442154000420.JavaMail.yahoo@mail.yahoo.com> Message-ID: On Sun, Sep 13, 2015 at 7:20 AM, Nym City via Tutor wrote: > Hello, > Sorry for the late response. It took me sometime to find the solution. Below is my updated code which seems to be working fine now. Just wanted to share with the group here. > > import csv > DomainList = [] > > domains = open('domainlist.csv', 'r') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList] > strip_list = [item.rstrip('/') for item in DomainList] > print('\n'.join(strip_list)) Style suggestions. 1. The initial assignment of: DomainList = [] can be omitted: the code does another assignment that completely ignores the initial value. 2. The reassignment of DomainList to the result of csv.reader() is confusing, because the new value isn't of the same type as the old value. I'd recommend that your program set aside a separate variable name for the csv reader. Call it "DomainListCSV" or something that can be distinguished from the list of domains that your program is collecting. ############################################### import csv domains = open('domainlist.csv', 'r') DomainListCSV = csv.reader(domains) DomainList = [column[1] for column in DomainListCSV] strip_list = [item.rstrip('/') for item in DomainList] print('\n'.join(strip_list)) ################################################ That way, if you see the word "DomainList" in your program, its conceptual meaning is more stable: its meaning doesn't have to change from one statement to the next. Assigning to a variable just once makes the program easier to understand. (That being said, we might have technical reasons for doing variable re-assignment. But such cases aren't as prevalent as one might expect!) 3. Finally, it might be good to stick with a single style for naming variables. You're using both underscored names and CapCased names. Consistency suggests that we pick a style and stick with it throughout the program. Doing an eclectic mix of naming conventions hurts readability a bit. If we stick with underscored names: ############################################### import csv domain_file = open('domainlist.csv', 'r') domains_csv = csv.reader(domain_file) domain_list = [column[1] for column in domains_csv] strip_list = [item.rstrip('/') for item in domain_list] print('\n'.join(strip_list)) ################################################ that would be one way to correct this issue. Or use CapWords. The main point is: try to use a single style that's shared across the program as a whole. Hope this helps! From dyoo at hashcollision.org Sun Sep 13 21:16:49 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 13 Sep 2015 12:16:49 -0700 Subject: [Tutor] =?utf-8?q?=E2=80=8BHow_to_use_the_returned_telnet_object_?= =?utf-8?q?after_creating_the_telnet_session=2E?= In-Reply-To: References: Message-ID: On Sun, Sep 13, 2015 at 12:29 AM, Manju M wrote: > > Assume that I will pass IP and port information from a function to open the > telnet session. have opened the telnet session and after opening the telnet > session I returned telnet object to calling function. Hi Manju, I apologize for potentially hijacking your question, but you may want to consider SSH over the telnet protocol. There are Python libraries that know how to speak SSH: https://wiki.python.org/moin/SecureShell and the use of Telnet is often discouraged because it does not protect its data. From oscar.j.benjamin at gmail.com Mon Sep 14 13:50:17 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 14 Sep 2015 12:50:17 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <55F13506.4030608@libero.it> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> <55F07047.4000803@libero.it> <55F13506.4030608@libero.it> Message-ID: On 10 September 2015 at 08:45, Francesco Loffredo via Tutor wrote: > > I wrote a small routine (below) to check when and if my code and the formula > do match. It easily shows that > they only match for len(pool) == (2 ** N) - 1, with N greater or equal to 2. That's interesting. I'm not sure exactly why that would happen. > My problem remains: WHY don't they match for every length? Your algorithm is not generally guaranteed to find a full solution. > How did you build your 12-triples set? I took it from this diagram: https://upload.wikimedia.org/wikipedia/commons/e/eb/Hesse_configuration.svg > What's wrong with my algorithm? And, most of all (and on topic, too): how can you build a Python program that builds your triples list? Perhaps if we compare this problem to a different problem then we can see how your algorithm may not be optimal. Imagine solving a sudoku puzzle. Your algorithm loops through all triples and accepts any triple if it doesn't immediately conflict with any of the triples already accepted. If you were solving a sudoku puzzle then an analogous algorithm would take each empty square and fill it with any number that doesn't contradict any of the currently filled squares. If you try this on a real puzzle then you will reach a dead end and you won't fill the grid. The problem is that some of the squares you filled in could have had a number of possible values and you've just chosen them arbitrarily (and incorrectly). The solution for a sudoku solving algorithm would be to back-track. One of the previously filled squares was filled incorrectly so go back and change what you had before. As a recursive algorithm you would take an unfilled square, loop over the possible values that it can take and for each of those values attempt to fill the remainder of the sudoko grid. The analogous backtracking algorithm for this problem would mean deciding first to include a particular triple if it is compatible with the already accepted triples and then continuing with the remaining triples to see if it leads to a full set (you know how big a full set should be). If it does not lead to a full set then you were wrong to include one of your current triples so now decide not to include it and again loop through the remaining triples looking for a full set. I imagine that the complexity of this algorithm will grow rapidly as N increases. For N=9 you have thousands of triples, so the powerset of this has 2**N members meaning that this binary backtracking algorithm has a very large space to explore. The space is heavily pruned by the pairing constraint though so it may not work out as badly as I expect. Also there will be many possible solutions and you only really need to find one. Up to isomorphism there is 1 solution for N=9 but that means there will be 9! isomorphic solutions (the number of ways of relabelling the numbers 1 through 9). This means that you may not have to go far in the search before coming across a solution. -- Oscar From richkappler at gmail.com Mon Sep 14 16:29:37 2015 From: richkappler at gmail.com (richard kappler) Date: Mon, 14 Sep 2015 10:29:37 -0400 Subject: [Tutor] Type Error Message-ID: Still working on my data feed script, if you'll recall from previous emails, it reads incoming data and creates a name for image files based on the incoming data in a test environment. below is a snippet of that code that copies the next image in the pool renaming it as it copies, sends to another machine via ftp, the closes the file and *** should then delete the file *** but does not. img = str(i) + '.jpg' ID = device + '_' + timestamp + '_' + counter + '.jpg' src = '/home/test/DataFeed/input/BOT/' + img dst = '/home/test/DataFeed/output/BOT' + ID shututil.copyfile(src, dst) file = open(dst, 'rb') sendBOT01.storbinary('STOR ' + ID, file) file.close() os.remove(file) everything works except the os.remove(file) which gives the following error: Traceback (most recent call last): File "DataFeedBatch.py", line 104, in os.remove(file) TypeError: coercing to Unicode: need string or buffer, file found I don't understand the error -- All internal models of the world are approximate. ~ Sebastian Thrun From zachary.ware+pytut at gmail.com Mon Sep 14 16:40:33 2015 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Mon, 14 Sep 2015 09:40:33 -0500 Subject: [Tutor] Type Error In-Reply-To: References: Message-ID: On Mon, Sep 14, 2015 at 9:29 AM, richard kappler wrote: > everything works except the os.remove(file) which gives the following error: > > Traceback (most recent call last): > File "DataFeedBatch.py", line 104, in > os.remove(file) > TypeError: coercing to Unicode: need string or buffer, file found > > I don't understand the error os.remove() expects a filename, and you're passing it a 'file' object. -- Zach From joel.goldstick at gmail.com Mon Sep 14 16:46:26 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 14 Sep 2015 10:46:26 -0400 Subject: [Tutor] Type Error In-Reply-To: References: Message-ID: On Mon, Sep 14, 2015 at 10:29 AM, richard kappler wrote: > Still working on my data feed script, if you'll recall from previous > emails, it reads incoming data and creates a name for image files based on > the incoming data in a test environment. below is a snippet of that code > that copies the next image in the pool renaming it as it copies, sends to > another machine via ftp, the closes the file and *** should then delete the > file *** but does not. > > img = str(i) + '.jpg' > ID = device + '_' + timestamp + '_' + counter + '.jpg' > src = '/home/test/DataFeed/input/BOT/' + img > dst = '/home/test/DataFeed/output/BOT' + ID > shututil.copyfile(src, dst) > file = open(dst, 'rb') > sendBOT01.storbinary('STOR ' + ID, file) > file.close() > os.remove(file) > > > everything works except the os.remove(file) which gives the following error: > > Traceback (most recent call last): > File "DataFeedBatch.py", line 104, in > os.remove(file) > TypeError: coercing to Unicode: need string or buffer, file found > > I don't understand the error > -- > > All internal models of the world are approximate. ~ Sebastian Thrun > I'm not sure if you want to remove src or dst, but whichever one you want to remove, use the name, not the file object, eg: os.remove(src) _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com From krohit at zeomega.com Mon Sep 14 16:43:13 2015 From: krohit at zeomega.com (Rohit kumar) Date: Mon, 14 Sep 2015 20:13:13 +0530 Subject: [Tutor] Type Error In-Reply-To: References: Message-ID: Correct, The filename should be passed as string value. Regards, -- Rohit Kumar | Technical Lead ZeOmega | Population Health Management Solutions Improving Population Health One Person at a Time Office: +91 80 4243 2000 (4067) | Mobile: +91 9342637703 -----Original Message----- From: Tutor [mailto:tutor-bounces+rohitraj007=gmail.com at python.org] On Behalf Of Zachary Ware Sent: 14 September 2015 20:11 To: tutor Subject: Re: [Tutor] Type Error On Mon, Sep 14, 2015 at 9:29 AM, richard kappler wrote: > everything works except the os.remove(file) which gives the following error: > > Traceback (most recent call last): > File "DataFeedBatch.py", line 104, in > os.remove(file) > TypeError: coercing to Unicode: need string or buffer, file found > > I don't understand the error os.remove() expects a filename, and you're passing it a 'file' object. -- Zach _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -- This e-mail message (including any attachments) may contain information that is confidential, protected by the attorney-client or other applicable privileges, or otherwise comprising non-public information. This message is intended to be conveyed only to the designated recipient(s). If you have any reason to believe you are not an intended recipient of this message, please notify the sender by replying to this message and then deleting it from your system. Any use, dissemination, distribution, or reproduction of this message by unintended recipients is not authorized and may be unlawful. From pete at leptonyx.com Mon Sep 14 16:53:16 2015 From: pete at leptonyx.com (Pete) Date: Mon, 14 Sep 2015 15:53:16 +0100 Subject: [Tutor] Python 3.5 32-bit installer not creating standard registry key In-Reply-To: References: Message-ID: <711734f4af01d43266ab2d29a90461be@leptonyx.com> Sorry, I've just realised that my previous message was wrong - please delete it. I now see that the key in question is actually a Python for Windows Extensions key. The problem appears to lie in the Python for Windows Extensions 32-bit installer not recognising any of the new Python 3.5 32-bit keys. I'll have to find some way of contacting the Python for Windows Extensions developers to ask them which key the installer is looking for, and then try to determine why I don't have it. Pete Subject: Python 3.5 32-bit installer not creating standard registry key Date: 2015-09-14 15:46 From: Pete To: tutor at python.org Hi there, I am looking for the best place to post a problem I'm having with Python 3.5. The 64-bit installer works fine, creating among other 64-bit registry keys a generic one called HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\pywin32-py3.5. Installing Python for Windows Extensions afterwards works fine as its installer looks for this particular key on installation. However, the 32-bit Python 3.5 installer does not create a similar generic key in the 32-bit registry, meaning that I can't install Python for Windows Extensions 32-bit afterwards. This problem did not occur with Python 3.4 which correctly installed both keys (with 3.4 in the key name instead of 3.5). Pete From alan.gauld at btinternet.com Mon Sep 14 17:51:41 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 14 Sep 2015 16:51:41 +0100 Subject: [Tutor] Python 3.5 32-bit installer not creating standard registry key In-Reply-To: <711734f4af01d43266ab2d29a90461be@leptonyx.com> References: <711734f4af01d43266ab2d29a90461be@leptonyx.com> Message-ID: On 14/09/15 15:53, Pete wrote: > > Sorry, I've just realised that my previous message was wrong We don't seem to have that one. Are you sure you sent it to the tutor list? > Python 3.5 32-bit keys. I'll have to find some way of contacting the > Python for Windows Extensions developers to ask them which key the > installer is looking for, and then try to determine why I don't have it. Try the Win32 mailing list/forum. https://mail.python.org/mailman/listinfo/python-win32 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From richkappler at gmail.com Mon Sep 14 17:55:18 2015 From: richkappler at gmail.com (richard kappler) Date: Mon, 14 Sep 2015 11:55:18 -0400 Subject: [Tutor] ftp socket.error In-Reply-To: References: Message-ID: Thanks for all the assistance, turned out it was a problem with the iptables not having an accept for eth1. Onward and upward! regards, Richard On Sat, Sep 12, 2015 at 12:24 PM, Martin A. Brown wrote: > > Hello and good morning > > I may be mistaken, but it looks like you are trying to open the socket on >> port 2021. Standard ftp uses 21. Is the server listening on 2021? >> > > Ooof! And, in fact, that is a great point! I overlooked that in the > original snippet! > > Everything I wrote still stands, except that you need to tell the > ip_conntrack_ftp (or nf_conntrack_ftp) kernel module to watch for a command > channel on TCP/2021. > > modprobe ip_conntrack_ftp ports=21,2021 > > That means that the ip_conntrack_ftp module will watch flows on both ports. > > I'm glad you observed that important detail, Robert! > > -Martin > > > Strictly speaking, it's no Python question, but... good ol' FTP. >>> >>> socket.error: [Errno 113] No route to host >>> >>>> >>>>>> >>>>> Your program is receiving an EHOSTUNREACH. >>> >>> >>> import errno >>> >>> errno.errorcode[113] >>> 'EHOSTUNREACH' >>> >>> This occurs at precisely the moment that your program is trying to >>> initiate a data transfer. Every firewall administrator in the world >>> loves >>> FTP for precisely this reason. (And, when I say "love", you can replace >>> this with a verb or of your choice.) >>> >>> Without packet captures, I will merely guess (based on experience). >>> >>> 1. The receiving machine is running the Python program, builds a >>> connection on port 21 (this is called the FTP command >>> channel), you log in and all is well. >>> 2. The moment you try to transfer any data, the FTP client (your >>> receiving machine) and the FTP server negotiate either FTP >>> passive mode or FTP active (retronym) mode. I'm guessing >>> that your FTP client is choosing passive mode. (Your FTP >>> client might produce logging of this negotiation.) >>> 3. Using the connection information, the client attempts to build >>> an FTP data channel. So, your machine running the Python >>> program initiates a connection to the FTP server. >>> 4. The FTP server is (probably) configured to allow connections >>> inbound to TCP/21 (FTP Command Channel), but doesn't know to >>> allow the connections to the ephemeral port(s) negotiated >>> during step 2 (above). So, the firewall on the FTP Server >>> sends an ICMP Type 3, Code 1 [0]. >>> >>> Figured it out. On the receiving machine I had to >>> >>>> >>>>> # modprobe ip_conntrack_ftp >>>>> >>>>> >>>> Right instinct! Try this same command on the FTP server side. Unless >>> your >>> Python FTP client is negotiating active mode, the server will be the >>> "problem" in this case. >>> >>> No, apparently I didn't figure it out. I thought I had as after the >>> >>>> modprobe I was getting a an EOFError, but now I'm getting the no route >>>> to >>>> host error again. I can ping it, and as you can see from the original >>>> post, >>>> I am able to establish a connection and log in, it's just when I try to >>>> send a file it goes bollocks up. Still need ideas. >>>> >>>> >>> Hopefully, my idea #1 helps. (If not, you'll need to do some packet >>> captures and probably crank up the logging on the FTP server, too.) >>> >>> I do have another idea, though. Have you ever wondered about the slow >>> demise of FTP? All of this command-channel, data-channel, PORT or PASV >>> nonsense goes away when you use a protocol that runs over a single TCP >>> port. Worked fine in the early days of the Internet before firewalls and >>> NAT. >>> >>> Anyway, short idea #2: >>> >>> If it's anonymous access, use HTTP. >>> If authenticated access, use ssh/scp/sftp. >>> >>> Good luck, >>> >>> -Martin >>> >>> [0] http://www.networksorcery.com/enp/protocol/icmp/msg3.htm >>> >>> -- >>> Martin A. Brown >>> http://linux-ip.net/ >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >>> >>> >> > -- > Martin A. Brown > http://linux-ip.net/ > -- All internal models of the world are approximate. ~ Sebastian Thrun From lac at openend.se Mon Sep 14 19:10:44 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 14 Sep 2015 19:10:44 +0200 Subject: [Tutor] Python 3.5 32-bit installer not creating standard registry key In-Reply-To: <711734f4af01d43266ab2d29a90461be@leptonyx.com> References: <711734f4af01d43266ab2d29a90461be@leptonyx.com> Message-ID: <201509141710.t8EHAiJi005732@fido.openend.se> In a message of Mon, 14 Sep 2015 15:53:16 +0100, Pete writes: > > >Sorry, I've just realised that my previous message was wrong - please >delete it. I now see that the key in question is actually a Python for >Windows Extensions key. The problem appears to lie in the Python for >Windows Extensions 32-bit installer not recognising any of the new >Python 3.5 32-bit keys. I'll have to find some way of contacting the >Python for Windows Extensions developers to ask them which key the >installer is looking for, and then try to determine why I don't have it. > >Pete post a bug here: http://bugs.python.org/ Laura From pete at leptonyx.com Mon Sep 14 19:19:47 2015 From: pete at leptonyx.com (Pete) Date: Mon, 14 Sep 2015 18:19:47 +0100 Subject: [Tutor] Python 3.5 32-bit installer not creating standard registry key In-Reply-To: <201509141710.t8EHAiJi005732@fido.openend.se> References: <711734f4af01d43266ab2d29a90461be@leptonyx.com> <201509141710.t8EHAiJi005732@fido.openend.se> Message-ID: <8f98e5e3fb04dbbc8e9bd3359dea86d5@leptonyx.com> Thanks Laura. In the end I posted the bug to Python for Windows Extensions (http://sourceforge.net/p/pywin32/bugs/702/) because I suspect that the fault may well lie with the PWE installer. If I don't get any resolution there then I'll post to the Python bugs link you kindly list. However, if you recommend that I post in both places now then I'll do that. Pete On 2015-09-14 18:10, Laura Creighton wrote: > In a message of Mon, 14 Sep 2015 15:53:16 +0100, Pete writes: >> >> >> Sorry, I've just realised that my previous message was wrong - please >> delete it. I now see that the key in question is actually a Python for >> Windows Extensions key. The problem appears to lie in the Python for >> Windows Extensions 32-bit installer not recognising any of the new >> Python 3.5 32-bit keys. I'll have to find some way of contacting the >> Python for Windows Extensions developers to ask them which key the >> installer is looking for, and then try to determine why I don't have >> it. >> >> Pete > > post a bug here: > http://bugs.python.org/ > > Laura From soertly at gmail.com Mon Sep 14 19:20:55 2015 From: soertly at gmail.com (Sarah) Date: Mon, 14 Sep 2015 13:20:55 -0400 Subject: [Tutor] syntax error Message-ID: Hi What's wrong with the following code? def main() lunch = int(input('How many hours did you eat?')) cost = float(input('Enter the hourly cost: ')) gross_cost = lunch * cost print('cost:$', format(cost, '.2f'), sep='') main() I get the error File "", line 6 Thanks, Sarah From vijayram.gopu at gmail.com Mon Sep 14 19:02:35 2015 From: vijayram.gopu at gmail.com (vijayram) Date: Mon, 14 Sep 2015 10:02:35 -0700 Subject: [Tutor] Need help with python Message-ID: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> Hi, I need help with python nose tests and test-suite? can I request help with a tutor.. Thank you, vijay From joel.goldstick at gmail.com Mon Sep 14 20:25:08 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 14 Sep 2015 14:25:08 -0400 Subject: [Tutor] syntax error In-Reply-To: References: Message-ID: On Mon, Sep 14, 2015 at 1:20 PM, Sarah wrote: > Hi > What's wrong with the following code? > > def main() > lunch = int(input('How many hours did you eat?')) > cost = float(input('Enter the hourly cost: ')) > gross_cost = lunch * cost > print('cost:$', format(cost, '.2f'), sep='') > main() > > > I get the error File "", line 6 > > Thanks, Sarah > You forgot the : after def main(...): > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com From steve at pearwood.info Mon Sep 14 20:29:06 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 15 Sep 2015 04:29:06 +1000 Subject: [Tutor] syntax error In-Reply-To: References: Message-ID: <20150914182906.GD31152@ando.pearwood.info> On Mon, Sep 14, 2015 at 01:20:55PM -0400, Sarah wrote: > Hi > What's wrong with the following code? > > def main() > lunch = int(input('How many hours did you eat?')) > cost = float(input('Enter the hourly cost: ')) > gross_cost = lunch * cost > print('cost:$', format(cost, '.2f'), sep='') > main() > > > I get the error File "", line 6 The above should be fine when saved and run from a .py file, but at the interactive interpreter, you need to leave a blank line after functions. Try leaving a blank after the print(...) line and before main(). -- Steve From steve at pearwood.info Mon Sep 14 20:30:56 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 15 Sep 2015 04:30:56 +1000 Subject: [Tutor] Need help with python In-Reply-To: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> References: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> Message-ID: <20150914183055.GE31152@ando.pearwood.info> On Mon, Sep 14, 2015 at 10:02:35AM -0700, vijayram wrote: > Hi, > > I need help with python nose tests and test-suite? can I request help with a tutor.. Sure. Ask your questions on the mailing list, and somebody will answer. This is a group for *public* tutoring, so that everyone can learn from it, not private tutoring. -- Steve From alan.gauld at btinternet.com Mon Sep 14 20:45:36 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 14 Sep 2015 19:45:36 +0100 Subject: [Tutor] syntax error In-Reply-To: References: Message-ID: On 14/09/15 19:25, Joel Goldstick wrote: > On Mon, Sep 14, 2015 at 1:20 PM, Sarah wrote: > >> Hi >> What's wrong with the following code? >> >> def main() >> lunch = int(input('How many hours did you eat?')) >> cost = float(input('Enter the hourly cost: ')) >> gross_cost = lunch * cost >> print('cost:$', format(cost, '.2f'), sep='') >> main() >> >> >> I get the error File "", line 6 >> >> Thanks, Sarah >> > > You forgot the : after def main(...): Also your print line is, I suspect, wrong. I assume you want to print gross_cost not cost? You could also use the format method of the string which tends to be more flexible: print('cost: ${:.2f}'.format(gross_cost)) hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Mon Sep 14 20:48:01 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 14 Sep 2015 19:48:01 +0100 Subject: [Tutor] Need help with python In-Reply-To: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> References: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> Message-ID: On 14/09/15 18:02, vijayram wrote: > Hi, > > I need help with python nose tests and test-suite? can I request help with a tutor.. You can try asking basic questions here, but as nose is not part of the standard library you might get more help on the python testing list: The gmane.org reference is here: gmane.comp.python.testing.general -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Tue Sep 15 00:31:38 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 15 Sep 2015 00:31:38 +0200 Subject: [Tutor] Python 3.5 32-bit installer not creating standard registry key In-Reply-To: <8f98e5e3fb04dbbc8e9bd3359dea86d5@leptonyx.com> References: <711734f4af01d43266ab2d29a90461be@leptonyx.com> <201509141710.t8EHAiJi005732@fido.openend.se> <8f98e5e3fb04dbbc8e9bd3359dea86d5@leptonyx.com> Message-ID: <201509142231.t8EMVcuI020345@fido.openend.se> In a message of Mon, 14 Sep 2015 18:19:47 +0100, Pete writes: > > >Thanks Laura. > >In the end I posted the bug to Python for Windows Extensions >(http://sourceforge.net/p/pywin32/bugs/702/) because I suspect that the >fault may well lie with the PWE installer. > >If I don't get any resolution there then I'll post to the Python bugs >link you kindly list. However, if you recommend that I post in both >places now then I'll do that. > >Pete I don't know what is best. I know that people with other installers are having problems getting 3.5.0 working on their machines, but whether their problems and your problems are related, I have no clue. Laura From sroden01 at saintmarys.edu Mon Sep 14 23:50:15 2015 From: sroden01 at saintmarys.edu (Shannon Rodenbeck) Date: Mon, 14 Sep 2015 17:50:15 -0400 Subject: [Tutor] Invalid Syntax Message Message-ID: I have downloaded Python 3.4.3 on my Mac 10.10.4. When i try running my programs from IDLE to go on Shell i get the message that says "Invalid Syntax". I've tried my programs on a different windows computer and it runs and works perfectly. How do I fix this problem so the programs are able to run on my Mac. Thank you, Shannon Rodenbeck sroden01 at saintmarys.edu From emile at fenx.com Tue Sep 15 01:07:38 2015 From: emile at fenx.com (Emile van Sebille) Date: Mon, 14 Sep 2015 16:07:38 -0700 Subject: [Tutor] Invalid Syntax Message In-Reply-To: References: Message-ID: On 9/14/2015 2:50 PM, Shannon Rodenbeck wrote: > I have downloaded Python 3.4.3 on my Mac 10.10.4. When i try running my > programs from IDLE to go on Shell i get the message that says "Invalid > Syntax". I've tried my programs on a different windows computer and it runs > and works perfectly. How do I fix this problem so the programs are able to > run on my Mac. > Start by providing a complete copy-n-paste of the screen when the error occurs. That normally provides enough detail to diagnose the issue. Emile From lac at openend.se Tue Sep 15 01:25:52 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 15 Sep 2015 01:25:52 +0200 Subject: [Tutor] Need help with python In-Reply-To: References: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> Message-ID: <201509142325.t8ENPqKk002098@fido.openend.se> In a message of Mon, 14 Sep 2015 19:48:01 +0100, Alan Gauld writes: >On 14/09/15 18:02, vijayram wrote: >> Hi, >> >> I need help with python nose tests and test-suite? can I request help with a tutor.. > >You can try asking basic questions here, but as nose is not >part of the standard library you might get more help on the >python testing list: > >The gmane.org reference is here: > >gmane.comp.python.testing.general I am not getting anything sensible out of this. But the testing list is over here: http://lists.idyll.org/pipermail/testing-in-python/ Though it has been unbelievably quiet this month ... Laura From alan.gauld at btinternet.com Tue Sep 15 01:34:14 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Sep 2015 00:34:14 +0100 Subject: [Tutor] Need help with python In-Reply-To: <201509142325.t8ENPqKk002098@fido.openend.se> References: <0DA7714B-0B1B-404C-952B-F1018EA52FDE@gmail.com> <201509142325.t8ENPqKk002098@fido.openend.se> Message-ID: <55F75976.5010600@btinternet.com> On 15/09/15 00:25, Laura Creighton wrote: > The gmane.org reference is here: > > gmane.comp.python.testing.general > I am not getting anything sensible out of this. Works for me, but admittedly I only see one post in September. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Sep 15 01:38:42 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Sep 2015 00:38:42 +0100 Subject: [Tutor] Invalid Syntax Message In-Reply-To: References: Message-ID: On 14/09/15 22:50, Shannon Rodenbeck wrote: > I have downloaded Python 3.4.3 on my Mac 10.10.4. When i try running my > programs from IDLE to go on Shell i get the message that says "Invalid > Syntax". I've tried my programs on a different windows computer and it runs > and works perfectly. How do I fix this problem so the programs are able to > run on my Mac. Are you absolutely sure the two machines are running the same python versions? Try this on both: import sys print(sys.version) In particular make sure that the IDLE version matches the interpreter version you expect. If its all OK then show us the actual code and actual error message (all of it). One final thing to try is running your code on the Mac under the Terminal app: $ python3 yourscript.py See if that works differently. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From nymcity at yahoo.com Tue Sep 15 02:45:08 2015 From: nymcity at yahoo.com (Nym City) Date: Tue, 15 Sep 2015 00:45:08 +0000 (UTC) Subject: [Tutor] Syntax error and EOL Error In-Reply-To: References: Message-ID: <47156993.2565685.1442277908748.JavaMail.yahoo@mail.yahoo.com> -Thank you very much for your feedback. It was definitely helpful. I will try to stay consistent with my coding style in the future projects. ? On Sunday, September 13, 2015 3:01 PM, Danny Yoo wrote: On Sun, Sep 13, 2015 at 7:20 AM, Nym City via Tutor wrote: > Hello, > Sorry for the late response. It took me sometime to find the solution. Below is my updated code which seems to be working fine now. Just wanted to share with the group here. > > import csv > DomainList = [] > > domains = open('domainlist.csv', 'r') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList] > strip_list = [item.rstrip('/') for item in DomainList] > print('\n'.join(strip_list)) Style suggestions. 1.? The initial assignment of: ? ? DomainList = [] can be omitted: the code does another assignment that completely ignores the initial value. 2.? The reassignment of DomainList to the result of csv.reader() is confusing, because the new value isn't of the same type as the old value.? I'd recommend that your program set aside a separate variable name for the csv reader.? Call it "DomainListCSV" or something that can be distinguished from the list of domains that your program is collecting. ############################################### import csv domains = open('domainlist.csv', 'r') DomainListCSV = csv.reader(domains) DomainList = [column[1] for column in DomainListCSV] strip_list = [item.rstrip('/') for item in DomainList] print('\n'.join(strip_list)) ################################################ That way, if you see the word "DomainList" in your program, its conceptual meaning is more stable: its meaning doesn't have to change from one statement to the next.? Assigning to a variable just once makes the program easier to understand. (That being said, we might have technical reasons for doing variable re-assignment.? But such cases aren't as prevalent as one might expect!) 3.? Finally, it might be good to stick with a single style for naming variables.? You're using both underscored names and CapCased names. Consistency suggests that we pick a style and stick with it throughout the program.? Doing an eclectic mix of naming conventions hurts readability a bit. If we stick with underscored names: ############################################### import csv domain_file = open('domainlist.csv', 'r') domains_csv = csv.reader(domain_file) domain_list = [column[1] for column in domains_csv] strip_list = [item.rstrip('/') for item in domain_list] print('\n'.join(strip_list)) ################################################ that would be one way to correct this issue.? Or use CapWords.? The main point is: try to use a single style that's shared across the program as a whole. Hope this helps! From nymcity at yahoo.com Tue Sep 15 03:41:49 2015 From: nymcity at yahoo.com (Nym City) Date: Tue, 15 Sep 2015 01:41:49 +0000 (UTC) Subject: [Tutor] syntax error In-Reply-To: References: Message-ID: <349037835.2541036.1442281309300.JavaMail.yahoo@mail.yahoo.com> Hello, I am also just trying to understand this code and could not understand the print statement.I would have just used: print('cost:$',gross_cost) Do you mind telling little bit about what: print('cost: ${:.2f}'.format(gross_cost)) does do and why one would use this over my plain version? ?Thank you. On Monday, September 14, 2015 2:46 PM, Alan Gauld wrote: On 14/09/15 19:25, Joel Goldstick wrote: > On Mon, Sep 14, 2015 at 1:20 PM, Sarah wrote: > >> Hi >> What's wrong with the following code? >> >> def main() >>? ? ? ? lunch = int(input('How many hours did you eat?')) >>? ? ? ? cost = float(input('Enter the hourly cost: ')) >>? ? ? ? gross_cost = lunch * cost >>? ? ? ? print('cost:$', format(cost, '.2f'), sep='') >> main() >> >> >> I get the error File "", line 6 >> >> Thanks, Sarah >> > > You forgot the : after def main(...): Also your print line is, I suspect, wrong. I assume you want to print gross_cost not cost? You could also use the format method of the string which tends to be more flexible: print('cost: ${:.2f}'.format(gross_cost)) hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Tue Sep 15 11:00:06 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Sep 2015 10:00:06 +0100 Subject: [Tutor] syntax error In-Reply-To: <349037835.2541036.1442281309300.JavaMail.yahoo@mail.yahoo.com> References: <349037835.2541036.1442281309300.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 15/09/15 02:41, Nym City via Tutor wrote: > Hello, > I am also just trying to understand this code and could not understand the print statement.I would have just used: > print('cost:$',gross_cost) > Do you mind telling little bit about what: > print('cost: ${:.2f}'.format(gross_cost)) > does do and why one would use this over my plain version? When in doubt use the >>> prompt: >>> gross_cost = 4.6 >>> print('cost: $',gross_cost) cost: $4.6 >>> print('cost: ${:.2f}'.format(gross_cost)) cost: $4.60 So the .2f forces the output to be formatted as a floating point number with 2 digits after the decimal point. And the format() inserts the values into the {} markers in the string to which its attached. Another longer example: >>> quantity = 4 >>> unit_cost = 4 >>> tax = 0.1 >>> print('''I bought {} items at ${:.2f} with {}% tax, ... making a total cost of: ${:.2f} ... '''.format(quantity, ... unit_cost, ... int(tax*100), ... quantity * unit_cost * (1+tax))) I bought 4 items at $4.00 with 10% tax making a total cost of: $17.60 Notice this time that :.2f forced the integer unit_cost(4) to be shown as a float with 2 decimal places(4.00). There are lots of other codes you can use to modify the formatting. Check the language reference in the docs under 'formatting': https://docs.python.org/3/library/string.html#formatstrings -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From fal at libero.it Tue Sep 15 23:14:29 2015 From: fal at libero.it (Francesco A. Loffredo) Date: Tue, 15 Sep 2015 23:14:29 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> <55F07047.4000803@libero.it> <55F13506.4030608@libero.it> Message-ID: <55F88A35.1050602@libero.it> On 14/09/2015 13:50, Oscar Benjamin wrote: > On 10 September 2015 at 08:45, Francesco Loffredo via Tutor > wrote: >> I wrote a small routine (below) to check when and if my code and the formula >> do match. It easily shows that >> they only match for len(pool) == (2 ** N) - 1, with N greater or equal to 2. > That's interesting. I'm not sure exactly why that would happen. > >> My problem remains: WHY don't they match for every length? > Your algorithm is not generally guaranteed to find a full solution. > >> How did you build your 12-triples set? > I took it from this diagram: > https://upload.wikimedia.org/wikipedia/commons/e/eb/Hesse_configuration.svg > >> What's wrong with my algorithm? And, most of all (and on topic, too): how can you build a Python program that builds your triples list? > Perhaps if we compare this problem to a different problem then we can > see how your algorithm may not be optimal. Imagine solving a sudoku > puzzle. > > Your algorithm loops through all triples and accepts any triple if it > doesn't immediately conflict with any of the triples already accepted. > If you were solving a sudoku puzzle then an analogous algorithm would > take each empty square and fill it with any number that doesn't > contradict any of the currently filled squares. If you try this on a > real puzzle then you will reach a dead end and you won't fill the > grid. The problem is that some of the squares you filled in could have > had a number of possible values and you've just chosen them > arbitrarily (and incorrectly). > > > ... > Also there will be many possible solutions and you only really need to > find one. Up to isomorphism there is 1 solution for N=9 but that means > there will be 9! isomorphic solutions (the number of ways of > relabelling the numbers 1 through 9). This means that you may not have > to go far in the search before coming across a solution. > > -- > Oscar > Thank you for your explanation, Oscar! I'll study a better algorithm and I'll post it here. While I hope Marcus Luetolf ( the OP) will find it useful, I will certainly get some learning from this exercise. Francesco From gokoproject at gmail.com Wed Sep 16 02:46:31 2015 From: gokoproject at gmail.com (John Wong) Date: Tue, 15 Sep 2015 20:46:31 -0400 Subject: [Tutor] Is context manager the answer to synchronous function calls? Message-ID: Hi Here is the code: def create_vm(.....): # returns vm object def modify_vm(.....): return new vm object def get_vm_status(.....): return status # Constraints: # I cannot modify vm until vm is finished, # I also cannot modify VM while the VM is being updated. # Obviously it looks ugly and wrong to include the while # in my functio. Those are user APIs. # Straight-forward option create_vm(...) status = get_vm_status(...) while status != "ready": time.sleep(10) status = get_vm_status(...) modify_vm(....) while status != "ready": time.sleep(10) status = get_vm_status(...) print("done!") I was thinking about context manager. But I feel like that's not the right choice. If you can guess cloning a vm in my program will require several more function calls. What's your recommendation to accomplish this? Thank you. John From alan.gauld at btinternet.com Wed Sep 16 12:56:09 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 16 Sep 2015 11:56:09 +0100 Subject: [Tutor] Is context manager the answer to synchronous function calls? In-Reply-To: References: Message-ID: On 16/09/15 01:46, John Wong wrote: > def create_vm(.....): > # returns vm object > > def modify_vm(.....): > return new vm object > > def get_vm_status(.....): > return status > # Constraints: > # I cannot modify vm until vm is finished, > # I also cannot modify VM while the VM is being updated. > # Obviously it looks ugly and wrong to include the while > # in my functio. Those are user APIs. You don't actually specify but I'm guessing VM means Virtual Machine? Is it a specific type of VM? eg VMWare/VirtualBox or somesuch, or is it more of a sandbox environment like virtualenv? That might help us understand the restrictions better. > # Straight-forward option > create_vm(...) > status = get_vm_status(...) > while status != "ready": > time.sleep(10) > status = get_vm_status(...) > modify_vm(....) > while status != "ready": > time.sleep(10) > status = get_vm_status(...) > print("done!") > > > I was thinking about context manager. But I feel like that's not the > right choice. A context manager might wait for the initial ready state and handle the shutdown for you but I don't think it would be so easy to deal with the intermediate pauses. The simplest approach is to make the functions synchronous and not return until the status changes, thus blocking the client code. But that's not usually the most user friendly approach, especially if using the code in an interactive environment. I would be tempted to use an asynchronous approach with a when_ready() function that takes my function as an input parameter. You could then run the when_ready in a thread or, I suspect, utilize the asyncio or asyncore modules, although I haven't tried using them for this kind of thing myself. The bottom line is you need to wait for the status to change. How you do that wait is up to you but you can make it more readable for the user. The easier it is for the user the harder it will be for you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Wed Sep 16 13:54:24 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 16 Sep 2015 12:54:24 +0100 Subject: [Tutor] Is context manager the answer to synchronous function calls? In-Reply-To: References: Message-ID: On 16/09/2015 11:56, Alan Gauld wrote: > On 16/09/15 01:46, John Wong wrote: >> def create_vm(.....): >> # returns vm object >> >> def modify_vm(.....): >> return new vm object >> >> def get_vm_status(.....): >> return status >> # Constraints: >> # I cannot modify vm until vm is finished, >> # I also cannot modify VM while the VM is being updated. >> # Obviously it looks ugly and wrong to include the while >> # in my functio. Those are user APIs. > > You don't actually specify but I'm guessing VM > means Virtual Machine? Is it a specific type > of VM? eg VMWare/VirtualBox or somesuch, or is > it more of a sandbox environment like virtualenv? > That might help us understand the restrictions better. > >> # Straight-forward option >> create_vm(...) >> status = get_vm_status(...) >> while status != "ready": >> time.sleep(10) >> status = get_vm_status(...) >> modify_vm(....) >> while status != "ready": >> time.sleep(10) >> status = get_vm_status(...) >> print("done!") >> >> >> I was thinking about context manager. But I feel like that's not the >> right choice. > > A context manager might wait for the initial ready state and > handle the shutdown for you but I don't think it would be > so easy to deal with the intermediate pauses. > > The simplest approach is to make the functions synchronous > and not return until the status changes, thus blocking > the client code. But that's not usually the most user friendly > approach, especially if using the code in an interactive > environment. > > I would be tempted to use an asynchronous approach with a > when_ready() function that takes my function as an input > parameter. You could then run the when_ready in a thread > or, I suspect, utilize the asyncio or asyncore modules, > although I haven't tried using them for this kind of > thing myself. > > The bottom line is you need to wait for the status to > change. How you do that wait is up to you but you can > make it more readable for the user. The easier it is for > the user the harder it will be for you. > Assuming your (Alan's) guess is correct, and I certainly agree it's plausible, I suspect this might be better asked on the main Python mailing list, I don't see this as tutor material. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From richkappler at gmail.com Wed Sep 16 19:03:04 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 16 Sep 2015 13:03:04 -0400 Subject: [Tutor] changing a variable with raw_input Message-ID: Missing something obvious here, but down with the flu so I'm foggy and just can't see it. I need to set a variable 'delay' to be used in time.sleep(delay) later on in a script, based on user input. Something odd is going on in setting the variable though. Here's the snippet where I'm trying to set the variable: #!/usr/bin/env python delay = 7 print("1 - Batch") print("2 - 2 per second") print("3 - Real Time") choice = raw_input("Enter feed speed choice (then press enter): ") choice = int(choice) if choice == 1: print "running as batch" delay == 0 elif choice == 2: print "feeding 2 events per second" delay == 0.5 elif choice == 3: print "feeding real time" delay = 'real time' else: print "choice not available" os._exit(0) print 'delay = ' + str(delay) If I enter 1 or 2, I get delay = 7, if I enter 3, I get delay = real time. HUH??? regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From richkappler at gmail.com Wed Sep 16 19:14:59 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 16 Sep 2015 13:14:59 -0400 Subject: [Tutor] changing a variable with raw_input In-Reply-To: References: Message-ID: Nevermind, figured it out. it needed to be delay = 0 and delay = 0.5, not == regards, Richard the virus ridden On Wed, Sep 16, 2015 at 1:03 PM, richard kappler wrote: > Missing something obvious here, but down with the flu so I'm foggy and > just can't see it. I need to set a variable 'delay' to be used in > time.sleep(delay) later on in a script, based on user input. Something odd > is going on in setting the variable though. Here's the snippet where I'm > trying to set the variable: > > #!/usr/bin/env python > > delay = 7 > > print("1 - Batch") > print("2 - 2 per second") > print("3 - Real Time") > choice = raw_input("Enter feed speed choice (then press enter): ") > choice = int(choice) > > if choice == 1: > print "running as batch" > delay == 0 > > elif choice == 2: > print "feeding 2 events per second" > delay == 0.5 > > elif choice == 3: > print "feeding real time" > delay = 'real time' > > else: > print "choice not available" > os._exit(0) > > print 'delay = ' + str(delay) > > > If I enter 1 or 2, I get delay = 7, if I enter 3, I get delay = real time. > > HUH??? > > regards, Richard > > -- > > All internal models of the world are approximate. ~ Sebastian Thrun > -- All internal models of the world are approximate. ~ Sebastian Thrun From breamoreboy at yahoo.co.uk Wed Sep 16 22:43:29 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 16 Sep 2015 21:43:29 +0100 Subject: [Tutor] changing a variable with raw_input In-Reply-To: References: Message-ID: On 16/09/2015 18:14, richard kappler wrote: > Nevermind, figured it out. it needed to be delay = 0 and delay = 0.5, not == > > regards, Richard the virus ridden > > On Wed, Sep 16, 2015 at 1:03 PM, richard kappler > wrote: > >> Missing something obvious here, but down with the flu so I'm foggy and >> just can't see it. I need to set a variable 'delay' to be used in >> time.sleep(delay) later on in a script, based on user input. Something odd >> is going on in setting the variable though. Here's the snippet where I'm >> trying to set the variable: >> >> #!/usr/bin/env python >> >> delay = 7 >> >> print("1 - Batch") >> print("2 - 2 per second") >> print("3 - Real Time") >> choice = raw_input("Enter feed speed choice (then press enter): ") >> choice = int(choice) >> >> if choice == 1: >> print "running as batch" >> delay == 0 >> >> elif choice == 2: >> print "feeding 2 events per second" >> delay == 0.5 >> >> elif choice == 3: >> print "feeding real time" >> delay = 'real time' >> >> else: >> print "choice not available" >> os._exit(0) >> >> print 'delay = ' + str(delay) >> >> >> If I enter 1 or 2, I get delay = 7, if I enter 3, I get delay = real time. >> >> HUH??? >> >> regards, Richard >> >> -- >> >> All internal models of the world are approximate. ~ Sebastian Thrun >> > I suggest that you learn a little about debugging problems like this, such as putting print statements into your code or actually stepping through your code in a debugger. Would you also be kind enough not to top post here, it can make things very difficult to read in long threads, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From ljetibo at gmail.com Fri Sep 18 09:10:00 2015 From: ljetibo at gmail.com (=?UTF-8?B?RGlubyBCZWt0ZcWhZXZpxIc=?=) Date: Fri, 18 Sep 2015 09:10:00 +0200 Subject: [Tutor] Calling instance method in IDLE magically calls __len__? Message-ID: Hello, For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and at https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the case spaces get messed up). class Errors: def __init__(self): pass def toFile(self): pass def __len__(self): print "len is called" return 0 Which is just fine if I call it over terminal, however calling it in IDLE: >>> e = Errors() >>> len(e) len is called 0 as expected, but when try to call the method toFile, "len is called" gets printed as soon as I put parenthesis "(" behind the toFile. >>> len is called e.toFile( Now I recognize that I shouldn't use __len__ to print stuff, I should use __string__ or at least __repr__, but I found it weird that __len__ would get called in that situation. So out of a stupid mistake an interesting question! Why does the "len is called" get printed to IDLE when you try to call toFile? How does the interpreter handle this? Any kind of clarification would be greatly appreciated. Hopefully I managed to get the right mailing list, Dino From steve at pearwood.info Fri Sep 18 10:03:10 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 18 Sep 2015 18:03:10 +1000 Subject: [Tutor] Calling instance method in IDLE magically calls __len__? In-Reply-To: References: Message-ID: <20150918080310.GO31152@ando.pearwood.info> Hi Dino, On Fri, Sep 18, 2015 at 09:10:00AM +0200, Dino Bekte?evi? wrote: > Hello, > > For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and at > https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the > case spaces get messed up). > > class Errors: > def __init__(self): > pass > def toFile(self): > pass > def __len__(self): > print "len is called" > return 0 > > Which is just fine if I call it over terminal, however calling it in IDLE: > > >>> e = Errors() > >>> len(e) > len is called > 0 > > as expected, but when try to call the method toFile, "len is called" gets > printed as soon as I put parenthesis "(" behind the toFile. > > >>> len is called > e.toFile( I'm afraid I cannot replicate that behaviour. Also the output seems strange -- the "len is called" is printed on the same line as the prompt, and e.toFile( afterwards. If you quit idle and restart it, do you get the same behaviour? What's the exact version of Python? import sys print sys.version -- Steve From nymcity at yahoo.com Fri Sep 18 01:17:50 2015 From: nymcity at yahoo.com (Nym City) Date: Thu, 17 Sep 2015 23:17:50 +0000 (UTC) Subject: [Tutor] syntax error In-Reply-To: References: Message-ID: <374966920.1243535.1442531870154.JavaMail.yahoo@mail.yahoo.com> -Perfect. Thank you. On Tuesday, September 15, 2015 5:00 AM, Alan Gauld wrote: On 15/09/15 02:41, Nym City via Tutor wrote: > Hello, > I am also just trying to understand this code and could not understand the print statement.I would have just used: >? print('cost:$',gross_cost) > Do you mind telling little bit about what: > print('cost: ${:.2f}'.format(gross_cost)) > does do and why one would use this over my plain version? When in doubt use the >>> prompt: >>> gross_cost = 4.6 >>> print('cost: $',gross_cost) cost: $4.6 >>> print('cost: ${:.2f}'.format(gross_cost)) cost: $4.60 So the .2f forces the output to be formatted as a floating point number with 2 digits after the decimal point. And the format() inserts the values into the {} markers in the string to which its attached. Another longer example: >>> quantity = 4 >>> unit_cost = 4 >>> tax = 0.1 >>> print('''I bought {} items at ${:.2f} with {}% tax, ... making a total cost of: ${:.2f} ... '''.format(quantity, ...? ? ? ? ? unit_cost, ...? ? ? ? ? int(tax*100), ...? ? ? ? ? quantity * unit_cost * (1+tax))) I bought 4 items at $4.00 with 10% tax making a total cost of: $17.60 Notice this time that :.2f forced the integer unit_cost(4) to be shown as a float with 2 decimal places(4.00). There are lots of other codes you can use to modify the formatting. Check the language reference in the docs under 'formatting': https://docs.python.org/3/library/string.html#formatstrings -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Fri Sep 18 12:19:03 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 18 Sep 2015 12:19:03 +0200 Subject: [Tutor] Calling instance method in IDLE magically calls __len__? References: Message-ID: Dino Bekte?evi? wrote: > Hello, > > For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and > at https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the > case spaces get messed up). > > class Errors: > def __init__(self): > pass > def toFile(self): > pass > def __len__(self): > print "len is called" > return 0 > > Which is just fine if I call it over terminal, however calling it in IDLE: > >>>> e = Errors() >>>> len(e) > len is called > 0 > > as expected, but when try to call the method toFile, "len is called" gets > printed as soon as I put parenthesis "(" behind the toFile. > >>>> len is called > e.toFile( > > Now I recognize that I shouldn't use __len__ to print stuff, I should use > __string__ or at least __repr__, but I found it weird that __len__ would > get called in that situation. So out of a stupid mistake an interesting > question! > > Why does the "len is called" get printed to IDLE when you try to call > toFile? How does the interpreter handle this? Any kind of clarification > would be greatly appreciated. This is a bug in Idle. In idlelib.CallTips.get_arg_text() there is a check if ob.im_self: ... to decide if the first argument (i. e. self) to the function should be ignored. This implicitly calls __len__() if im_self (in your case the Errors instance associated with the bound toFile method) has a __len__ and no __nonzero__ method: >>> class Errors: ... def __init__(self): ... pass ... def toFile(self): ... pass ... def __len__(self): ... print "len is called" ... return 0 ... >>> e = Errors() >>> if e: pass ... len is called >>> if e.toFile.im_self: ... pass ... len is called Normally this is intended as it allows you to write the idiomatic if some_list: print "the list is not empty" instead of the long-winded if len(some_list) > 0: ... Therefore I recommend that you write test methods like __len__() without any side effects. However, the problem with Idle is that it decides that the method is unbound and therefore includes self into the list of arguments to be supplied by the user: >>> import idlelib.CallTips >>> idlelib.CallTips.get_arg_text(e.toFile) len is called '(self)' For comparison the result when len(e) != 0: >>> class Foo: # class with user-supplied len ... def __init__(self, len): self._len = len ... def __len__(self): ... print "len =", self._len ... return self._len ... def bar(self, one, two): pass ... >>> idlelib.CallTips.get_arg_text(Foo(0).bar) len = 0 '(self, one, two)' >>> idlelib.CallTips.get_arg_text(Foo(1).bar) len = 1 '(one, two)' >>> idlelib.CallTips.get_arg_text(Foo(42).bar) len = 42 '(one, two)' This can be fixed by changing the check to the stricter if ob.im_self is not None: ... As this has already been done (see http://bugs.python.org/issue21654) you don't need to report the bug. From ljetibo at gmail.com Fri Sep 18 13:21:53 2015 From: ljetibo at gmail.com (=?UTF-8?B?RGlubyBCZWt0ZcWhZXZpxIc=?=) Date: Fri, 18 Sep 2015 13:21:53 +0200 Subject: [Tutor] Tutor Digest, Vol 139, Issue 29 Calling instance method in IDLE magically calls __len__? Message-ID: Hello all, > Hi Dino, > > > I'm afraid I cannot replicate that behaviour. Also the output seems > strange -- the "len is called" is printed on the same line as the > prompt, and e.toFile( afterwards. > > If you quit idle and restart it, do you get the same behaviour? What's > the exact version of Python? > > import sys > print sys.version > > > > -- > Steve The exact version is 2.7.6 (2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]) The behaviour is replicable even after OS restarts. As you've noticed the print statement gets executed and pushes the typing command into a new-line, this behaviour is not replicable when the file is imported as a module or run the terminal. I have tried figuring out what was happening by using the inspect module to see who calls the function __len__ with some radically big context but I can't seem to interpret the output myself (bit too deep in pythons belly for my level). Code is shown bellow: import inspect class Errors: def __init__(self): pass def toFile(self): pass def __len__(self): curframe = inspect.currentframe() calframe = inspect.getouterframes(curframe, context=15) for i in range(15): print " caller: ",calframe[i][3] print "len is called" return 0 What I get as output now: >>> len(e) caller: __len__ caller: caller: runcode caller: main caller: len is called 0 >>> caller: __len__ caller: get_arg_text caller: fetch_tip caller: get_the_calltip caller: localcall caller: pollresponse caller: _getresponse caller: getresponse caller: handle caller: __init__ caller: __init__ caller: finish_request caller: process_request caller: _handle_request_noblock caller: handle_request caller: manage_socket caller: run caller: __bootstrap_inner caller: __bootstrap len is called e.toFile( Maybe that can help? I know it doesn't really, it would be better to print the class that owns the methods called, however I don't know how to do that, so here's at least the file names: >>> len(e) caller: /home/dino/Desktop/bitbucket/refactor/errors/errors.py caller: caller: /usr/lib/python2.7/idlelib/run.py caller: /usr/lib/python2.7/idlelib/run.py caller: len is called 0 >>> caller: /home/dino/Desktop/bitbucket/refactor/errors/errors.py caller: /usr/lib/python2.7/idlelib/CallTips.py caller: /usr/lib/python2.7/idlelib/CallTips.py caller: /usr/lib/python2.7/idlelib/run.py caller: /usr/lib/python2.7/idlelib/rpc.py caller: /usr/lib/python2.7/idlelib/rpc.py caller: /usr/lib/python2.7/idlelib/rpc.py caller: /usr/lib/python2.7/idlelib/rpc.py caller: /usr/lib/python2.7/idlelib/run.py caller: /usr/lib/python2.7/SocketServer.py caller: /usr/lib/python2.7/idlelib/rpc.py caller: /usr/lib/python2.7/SocketServer.py caller: /usr/lib/python2.7/SocketServer.py caller: /usr/lib/python2.7/SocketServer.py caller: /usr/lib/python2.7/SocketServer.py caller: /usr/lib/python2.7/idlelib/run.py caller: /usr/lib/python2.7/threading.py caller: /usr/lib/python2.7/threading.py caller: /usr/lib/python2.7/threading.py len is called e.toFile( Thanks for the help, Dino From marcus.luetolf at bluewin.ch Fri Sep 18 17:17:20 2015 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Fri, 18 Sep 2015 17:17:20 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> Message-ID: <0f2801d0f225$200da020$6028e060$@bluewin.ch> dear pythonistas, in the code below: how can I solve my task wit n items ? Thank you for help, Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From marcus.luetolf at bluewin.ch Fri Sep 18 17:41:52 2015 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Fri, 18 Sep 2015 17:41:52 +0200 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once Message-ID: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> dear pythonistas in the code below >>> import string, itertools >>> s = ['ab','ac','bc','ad','ae','de'] >>> count = 0 >>> for startlist in itertools.combinations(s, 3): >>> count = count + 1 >>> stl = list(startlist) >>> print count, stl >>> for pair in s: >>> x = stl.count(pair) >>> print x, pair the variable stl produces 20 lists. The variable x counts how many times the items of s occur in each of the 20 lists. How can I concatenate the 20 lists in oder to get one count for each of the items in s , for example 10 for 'ab'? My further task will be expand s to all 26 letters oft he alphabet (giving 325 items) and the to eliminate all list in which the items in s occur more than once. Thank you for help, Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From alan.gauld at btinternet.com Fri Sep 18 18:57:55 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 18 Sep 2015 17:57:55 +0100 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> Message-ID: On 18/09/15 16:41, marcus l?tolf wrote: >>>> s = ['ab','ac','bc','ad','ae','de'] >>>> for startlist in itertools.combinations(s, 3): > How can I concatenate the 20 lists in oder to get one count for each of the items in s , for example 10 for 'ab'? If I understand you correctly, something like this: >>> counts = {'ab':0,'ac':0,'bc':0,'ad':0,'ae':0,'de':0} >>> for combo in it.combinations(counts.keys(),3): ... for pair in combo: ... counts[pair] += 1 ... >>> counts {'ac': 10, 'ab': 10, 'ae': 10, 'ad': 10, 'bc': 10, 'de': 10} Is that what you want? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri Sep 18 18:59:24 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 18 Sep 2015 17:59:24 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <0f2801d0f225$200da020$6028e060$@bluewin.ch> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <0f2801d0f225$200da020$6028e060$@bluewin.ch> Message-ID: On 18/09/15 16:17, marcus l?tolf wrote: > dear pythonistas, > > in the code below: > > > how can I solve my task wit n items ? > Thank you for help, Marcus. I see no code... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From ahmedn82 at hotmail.com Sat Sep 19 12:29:31 2015 From: ahmedn82 at hotmail.com (Ahmed AL-Masri) Date: Sat, 19 Sep 2015 18:29:31 +0800 Subject: [Tutor] Opencv Message-ID: Dear fellows, I have a project for hand detection as the person wave his hand than give an action. I am trying to use the current library in opencv using the haar cascade. I use the following code with the available xml data base from internet But it seems that the data base is not good enough or the code is not correct because sometimes it detect other objects as well. do you have any idea how we can use the opencv or another library to detect the hand for different scale or how to build good xml database Thanks a lot for your cooperation and support.Ahmed From ldl08 at gmx.net Sat Sep 19 15:22:28 2015 From: ldl08 at gmx.net (David) Date: Sat, 19 Sep 2015 15:22:28 +0200 Subject: [Tutor] [newbie] import error after restart (virtualenv) Message-ID: <55FD6194.8000903@gmx.net> Dear Tutors, I am reading through Harry Percival's "Test-Driven Development with Python". As I finished chapter 3 yesterday, I was fully on track, perfectly aligned with the book. Today I restarted my computer, activated the virtualenv in question -- and get an error message that was not there beforehand: (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists/lists$ python tests.py Traceback (most recent call last): File "tests.py", line 5, in from lists.views import home_page ImportError: No module named 'lists' I neither understand why he doesn't find 'lists' anymore nor do I know how to solve the problem. Nothing seems to have changed in the meantime... Can you please guide me towards a solution? Thank you! David The project structure looks as follows: (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists$ tree . ??? db.sqlite3 ??? functional_tests.py ??? lists ? ??? admin.py ? ??? __init__.py ? ??? migrations ? ? ??? __init__.py ? ??? models.py ? ??? tests.py ? ??? views.py ??? manage.py ??? superlists ??? __init__.py ??? __pycache__ ? ??? __init__.cpython-34.pyc ? ??? settings.cpython-34.pyc ? ??? urls.cpython-34.pyc ? ??? wsgi.cpython-34.pyc ??? settings.py ??? urls.py ??? wsgi.py -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe at googlegroups.com. To post to this group, send email to django-users at googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55FCA478.5000609%40gmx.net. For more options, visit https://groups.google.com/d/optout. views.py from django.shortcuts import render from django.http import HttpResponse # Create your views here. def home_page(request): return HttpResponse('To-Do lists') tests.py from django.core.urlresolvers import resolve from django.test import TestCase from django.http import HttpRequest from lists.views import home_page class HomePageTest(TestCase): def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertEqual(found.func, home_page) def test_home_page_returns_correct_html(self): request = HttpRequest() response = home_page(request) self.assertTrue(response.content.startswith(b'')) self.assertIn(b'To-Do lists', response.content) self.assertTrue(response.content.endswith(b'')) From __peter__ at web.de Sat Sep 19 16:07:53 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 19 Sep 2015 16:07:53 +0200 Subject: [Tutor] [newbie] import error after restart (virtualenv) References: <55FD6194.8000903@gmx.net> Message-ID: David wrote: > Dear Tutors, > > I am reading through Harry Percival's "Test-Driven Development with > Python". > > As I finished chapter 3 yesterday, I was fully on track, perfectly > aligned with the book. > > Today I restarted my computer, activated the virtualenv in question -- > and get an error message that was not there beforehand: > > (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists/lists$ > python tests.py > Traceback (most recent call last): > File "tests.py", line 5, in > from lists.views import home_page > ImportError: No module named 'lists' > > > I neither understand why he doesn't find 'lists' anymore nor do I know > how to solve the problem. Nothing seems to have changed in the meantime... > > Can you please guide me towards a solution? > > Thank you! > > David > > > > The project structure looks as follows: > > (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists$ > tree . > ??? db.sqlite3 > ??? functional_tests.py > ??? lists > ? ??? admin.py > ? ??? __init__.py > ? ??? migrations > ? ? ??? __init__.py > ? ??? models.py > ? ??? tests.py > ? ??? views.py > ??? manage.py > ??? superlists > ??? __init__.py > ??? __pycache__ > ? ??? __init__.cpython-34.pyc > ? ??? settings.cpython-34.pyc > ? ??? urls.cpython-34.pyc > ? ??? wsgi.cpython-34.pyc > ??? settings.py > ??? urls.py > ??? wsgi.py Given this layout you have to ensure that the parent folder of lists is in sys.path. This can be achieved by setting the PYTHONPATH variable for just this invocation $ PYTHONPATH=.. python tests.py or in a more permanent way and preferably with absolute paths. However, are you sure you ran tests.py explicitly? I've only had a cursory look at django and no project handy to check, but if I remember correctly $ ./manage.py test should take care of the details. From chandankumar.093047 at gmail.com Sat Sep 19 18:16:12 2015 From: chandankumar.093047 at gmail.com (chandan kumar) Date: Sat, 19 Sep 2015 21:46:12 +0530 Subject: [Tutor] How to parse a mailing list thread? Message-ID: Hello, I am looking for a python module which i can use to parse mailing thread and extract some information from it. Any pointer regarding that would be helpful. Thanks, Chandan Kumar From ldl08 at gmx.net Sat Sep 19 18:21:42 2015 From: ldl08 at gmx.net (David) Date: Sat, 19 Sep 2015 18:21:42 +0200 Subject: [Tutor] [newbie] import error after restart (virtualenv) In-Reply-To: References: <55FD6194.8000903@gmx.net> Message-ID: <55FD8B96.6050307@gmx.net> Hello Peter, this was indeed the problem -- I didn't go through manage.py! Weird I didn't have that on the radar anymore. Putting lists/ onto the Python path did not solve the problem. Thanks for your help! David On 19/09/15 16:07, Peter Otten wrote: > David wrote: > >> Dear Tutors, >> >> I am reading through Harry Percival's "Test-Driven Development with >> Python". >> >> As I finished chapter 3 yesterday, I was fully on track, perfectly >> aligned with the book. >> >> Today I restarted my computer, activated the virtualenv in question -- >> and get an error message that was not there beforehand: >> >> > (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists/lists$ >> python tests.py >> Traceback (most recent call last): >> File "tests.py", line 5, in >> from lists.views import home_page >> ImportError: No module named 'lists' >> >> >> I neither understand why he doesn't find 'lists' anymore nor do I know >> how to solve the problem. Nothing seems to have changed in the meantime... >> >> Can you please guide me towards a solution? >> >> Thank you! >> >> David >> >> >> >> The project structure looks as follows: >> >> (Percival_TDD)david at lubuntu:~/PycharmProjects/Percival_TDD/superlists$ >> tree . >> ??? db.sqlite3 >> ??? functional_tests.py >> ??? lists >> ? ??? admin.py >> ? ??? __init__.py >> ? ??? migrations >> ? ? ??? __init__.py >> ? ??? models.py >> ? ??? tests.py >> ? ??? views.py >> ??? manage.py >> ??? superlists >> ??? __init__.py >> ??? __pycache__ >> ? ??? __init__.cpython-34.pyc >> ? ??? settings.cpython-34.pyc >> ? ??? urls.cpython-34.pyc >> ? ??? wsgi.cpython-34.pyc >> ??? settings.py >> ??? urls.py >> ??? wsgi.py > > Given this layout you have to ensure that the parent folder of lists is in > sys.path. This can be achieved by setting the PYTHONPATH variable for just > this invocation > > $ PYTHONPATH=.. python tests.py > > or in a more permanent way and preferably with absolute paths. > > However, are you sure you ran tests.py explicitly? I've only had a cursory > look at django and no project handy to check, but if I remember correctly > > $ ./manage.py test > > should take care of the details. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From danny.yoo at gmail.com Sat Sep 19 18:27:12 2015 From: danny.yoo at gmail.com (Danny Yoo) Date: Sat, 19 Sep 2015 11:27:12 -0500 Subject: [Tutor] [newbie] import error after restart (virtualenv) In-Reply-To: <55FD8B96.6050307@gmx.net> References: <55FD6194.8000903@gmx.net> <55FD8B96.6050307@gmx.net> Message-ID: On Sep 19, 2015 6:22 PM, "David" wrote: > > Hello Peter, > > this was indeed the problem -- I didn't go through manage.py! Weird I > didn't have that on the radar anymore. > > Putting lists/ onto the Python path did not solve the problem. > You probably want to put the *parent* of lists/ onto the Python path. Good luck! From cs at zip.com.au Sun Sep 20 07:41:31 2015 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 20 Sep 2015 15:41:31 +1000 Subject: [Tutor] How to parse a mailing list thread? In-Reply-To: References: Message-ID: <20150920054131.GA10830@cskk.homeip.net> On 19Sep2015 21:46, chandan kumar wrote: >I am looking for a python module which i can use to parse mailing thread >and extract some information from it. > >Any pointer regarding that would be helpful. You should describe where the email messages are stored. I'll presume you have obtained the messages. Construct a Message object from each message text. See the email.message module: https://docs.python.org/3/library/email.message.html#module-email.message Every message has a Message-ID: header which uniquely identifies it. Replies to that message have that id in the In_Reply-To: header. (If you're parsing usenet newsgroup messages, you want the References: header - personally I consult both.) The complete specification of an email message is here: http://tools.ietf.org/html/rfc2822 and the email.message module (and the other email.* modules) makes most of it easily available. If you need to parse email addresses import the "getaddresses" function from the "email.utils" module. Constuct a graph connecting messages with the replies. You're done! Cheers, Cameron Simpson From churche at mcmaster.ca Sun Sep 20 21:19:47 2015 From: churche at mcmaster.ca (EILEEN CHURCH CARSON) Date: Sun, 20 Sep 2015 15:19:47 -0400 Subject: [Tutor] Creating a match file Message-ID: Hi there, I want to write a program that reads in data from two csv files with 6 columns each. I want it to determines if the data in the first two columns is the same, and if so read all six columns in that line from each of the two files into a 12 column output file. Please let me know if you know of specific resources or python commands that will help me do this. Thank you! Eileen From marcus.luetolf at bluewin.ch Sun Sep 20 17:47:01 2015 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Sun, 20 Sep 2015 17:47:01 +0200 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> Message-ID: <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> Hi Alan G, thanks for your quick answer. The copy of my code below, sent on 18/09/15 16:41 is for some reasons incomplete. It should read >>> import string, itertools >>> s = ['ab','ac','bc','ad','ae','de'] >>> count = 0 >>> for startlist in itertools.combinations(s, 3): >>> count = count + 1 >>> stl = list(startlist) >>> print count, stl >>> for pair in s: >>> x = stl.count(pair) >>> print x, pair My task is to have lists containing 3 tuples, each tuple ocurring only once. In my code above I started off with 6 tuples ( = s) of which 20 lists (count) are produced containing each of the 6 tuples multiple times. Therefore, I try to write additional code for eliminating all lists which contain tuples occuring more than once. If I use all 325 tuples I would first get many thousand lists. Your code proposal uses dictionaries. If I used dictionaries with all the 325 possible tuples I would have to do a lot of typing and not getting lists with 3 tuples in it. So, I am stuck somehow and hope to have made my tasks clear. Thanks for further help, Marcus. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -----Urspr?ngliche Nachricht----- Von: Tutor [mailto:tutor-bounces+marcus.luetolf=bluewin.ch at python.org] Im Auftrag von Alan Gauld Gesendet: Freitag, 18. September 2015 18:58 An: tutor at python.org Betreff: Re: [Tutor] Creating lists with 3 (later4) items occuring only once On 18/09/15 16:41, marcus l?tolf wrote: >>>> s = ['ab','ac','bc','ad','ae','de'] for startlist in >>>> itertools.combinations(s, 3): > How can I concatenate the 20 lists in oder to get one count for each of the items in s , for example 10 for 'ab'? If I understand you correctly, something like this: >>> counts = {'ab':0,'ac':0,'bc':0,'ad':0,'ae':0,'de':0} >>> for combo in it.combinations(counts.keys(),3): ... for pair in combo: ... counts[pair] += 1 ... >>> counts {'ac': 10, 'ab': 10, 'ae': 10, 'ad': 10, 'bc': 10, 'de': 10} Is that what you want? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From sudhierb at gmail.com Sat Sep 19 16:22:07 2015 From: sudhierb at gmail.com (Sudhier Batara) Date: Sat, 19 Sep 2015 19:52:07 +0530 Subject: [Tutor] Doubly linked list getting "AttributeError: 'NoneType' object has no attribute 'element'" Message-ID: Hi, I have the below code which is a doubly linked list , which returns the Position of the node rather than the node itself.The code is quite self explanatory.In the below code i call following methods , p = L.add_last(8) print p.element() the above methods works fine adds the element into the List instance and also printing the element shows the element in the list. But when i call the below methods, q = L.add_after(p,5) q.element() throws following error _______________________________________ in make position in make position 8 inside validate 8 in make position Traceback (most recent call last): File "positional_dlist.py", line 128, in print q.element() File "positional_dlist.py", line 45, in element return self._node.element AttributeError: 'NoneType' object has no attribute 'element' ____________________________________________________ Complete code below #!/usr/bin/python class _DoublelinkedBase: class _Node: def __init__(self,element,prev,Next): self.element = element self._prev = prev self._Next = Next def __init__(self): self._header = self._Node(None,None,None) self._trailer = self._Node(None,None,None) self._header._Next = self._trailer self._trailer._prev = self._header self._size = 0 def __len__(self): return self._size def is_empty(self): return self._size == 0 def _insert_between(self,element,predecessor,successor): newest = self._Node(element,predecessor,successor) predecessor._Next = newest successor._prev = newest self._size += 1 def _delete_node(self,node): predecessor = node._prev successor = node._Next predecessor._Next = successor successor._prev = predecessor element = node.element node.element = node._Next = node._prev = None self._size -= 1 class PositionalList(_DoublelinkedBase): class Position: def __init__(self,container,node): self._container = container self._node = node def element(self): return self._node.element def __eq__(self,other): return type(other) is type(self) and other._node is self._node def __ne__(self,other): return not (other == self) def _validate(self, p): if not isinstance(p,self.Position): raise TypeError("p is not of type Position") if p._container is not self: raise ValueError("p is not the instance of this list") if p._node._Next is None: print p._node raise ValueError("No longer a valid node") print "inside validate" print p.element() return p._node def _make_position(self,node): if node is self._header or node is self._trailer: print "in make position for none" return None else: print "in make position" return self.Position(self,node) def first(self): return self._make_position(self._header._Next) def last(self): return self._make_position(self._trailer._prev) def before(self,p): node = self._validate(p) return self._make_position(node._prev) def after(self,p): node = self._validate(p) return self._make_position(node._Next) def __iter__(self): cursor = self.first() while cursor is not None: yield cursor.element() cursor = self.after(cursor) def _insert_between(self,e,predecessor,successor): #node = super(_DoublelinkedBase,self)._insert_between(e,predecessor,successor) node = _DoublelinkedBase()._insert_between(e,predecessor,successor) return self._make_position(node) def add_first(self,e): return self._insert_between(e,self._header,self._header._Next) def add_last(self,e): return self._insert_between(e,self._trailer._prev,self._trailer) def add_before(self,p,e): original = self._validate(p) return self._insert_between(e,original._prev,original) def add_after(self,p,e): original = self._validate(p) return self._insert_between(e,original,original._Next) def delete_node(self,p): original = self._validate(p) return self._delete_node(original) def replace(self,p,e): original = self._validate(p) old_value = original.element original.element = e return old_value L = PositionalList() p = L.add_last(8) p = L.first() print p.element() q = L.add_after(p,5) print q.element() From __peter__ at web.de Mon Sep 21 19:51:02 2015 From: __peter__ at web.de (Peter Otten) Date: Mon, 21 Sep 2015 19:51:02 +0200 Subject: [Tutor] Creating a match file References: Message-ID: EILEEN CHURCH CARSON wrote: > I want to write a program that reads in data from two csv files with 6 > columns each. I want it to determines if the data in the first two columns > is the same, and if so read all six columns in that line from each of the > two files into a 12 column output file. > > Please let me know if you know of specific resources or python commands > that will help me do this. Use the csv module to read and write your data. Read one csv file into a dict that maps the two key columns to the complete row. Then when you read the other csv file you can look up the corresponding record in the dict. Here's a sketch of the part tha performs join*: for row in ...: try: other = lookup[row[:2]] except KeyError: # no corresponding data found, ignore row pass else: # write combined row # result_writer is a csv.writer() instance result_writer.writerow(row + other) Come back with your code when you need more hints. (*) If you know some SQL another option is to read the two csv files into an sqlite3 db -- sqlite3 is also part of the standard library. From steve at pearwood.info Mon Sep 21 20:13:01 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 22 Sep 2015 04:13:01 +1000 Subject: [Tutor] Doubly linked list getting "AttributeError: 'NoneType' object has no attribute 'element'" In-Reply-To: References: Message-ID: <20150921181301.GI31152@ando.pearwood.info> Hi Sudhier, and welcome, Unfortunately I'm about to step away from the computer, so I can't really answer your question in detail, but since it's been 2 or 3 days since you asked your question, and I haven't seen any replies at all, I thought I'd give a quick reply so you know we aren't ignoring you. (My inbox is suffering from overload at the moment, so it is possible that others have answered your question and I haven't noticed. If so, my apologies for wasting everyone's time.) Regards, Steve On Sat, Sep 19, 2015 at 07:52:07PM +0530, Sudhier Batara wrote: > Hi, > > I have the below code which is a doubly linked list , which returns > the Position of the node rather than the node itself.The code is quite > self explanatory.In the below code i call following methods , > > p = L.add_last(8) > print p.element() > > the above methods works fine adds the element into the List instance > and also printing the element shows the element in the list. > > But when i call the below methods, > > q = L.add_after(p,5) > q.element() > > throws following error > > > _______________________________________ > in make position > in make position > 8 > inside validate > 8 > in make position > Traceback (most recent call last): > File "positional_dlist.py", line 128, in > print q.element() > File "positional_dlist.py", line 45, in element > return self._node.element > AttributeError: 'NoneType' object has no attribute 'element' > ____________________________________________________ > > Complete code below > > > > #!/usr/bin/python > > class _DoublelinkedBase: > class _Node: > def __init__(self,element,prev,Next): > self.element = element > self._prev = prev > self._Next = Next > > def __init__(self): > self._header = self._Node(None,None,None) > self._trailer = self._Node(None,None,None) > self._header._Next = self._trailer > self._trailer._prev = self._header > self._size = 0 > > def __len__(self): > return self._size > > def is_empty(self): > return self._size == 0 > > def _insert_between(self,element,predecessor,successor): > newest = self._Node(element,predecessor,successor) > predecessor._Next = newest > successor._prev = newest > self._size += 1 > > def _delete_node(self,node): > predecessor = node._prev > successor = node._Next > predecessor._Next = successor > successor._prev = predecessor > element = node.element > node.element = node._Next = node._prev = None > self._size -= 1 > > class PositionalList(_DoublelinkedBase): > class Position: > def __init__(self,container,node): > self._container = container > self._node = node > > def element(self): > return self._node.element > > def __eq__(self,other): > return type(other) is type(self) and other._node is self._node > > def __ne__(self,other): > return not (other == self) > > def _validate(self, p): > if not isinstance(p,self.Position): > raise TypeError("p is not of type Position") > if p._container is not self: > raise ValueError("p is not the instance of this list") > if p._node._Next is None: > print p._node > raise ValueError("No longer a valid node") > print "inside validate" > print p.element() > return p._node > > def _make_position(self,node): > if node is self._header or node is self._trailer: > print "in make position for none" > return None > else: > print "in make position" > return self.Position(self,node) > > def first(self): > return self._make_position(self._header._Next) > > def last(self): > return self._make_position(self._trailer._prev) > > def before(self,p): > node = self._validate(p) > return self._make_position(node._prev) > > def after(self,p): > node = self._validate(p) > return self._make_position(node._Next) > > def __iter__(self): > cursor = self.first() > while cursor is not None: > yield cursor.element() > cursor = self.after(cursor) > > def _insert_between(self,e,predecessor,successor): > #node = > super(_DoublelinkedBase,self)._insert_between(e,predecessor,successor) > node = _DoublelinkedBase()._insert_between(e,predecessor,successor) > return self._make_position(node) > > def add_first(self,e): > return self._insert_between(e,self._header,self._header._Next) > > def add_last(self,e): > return self._insert_between(e,self._trailer._prev,self._trailer) > > def add_before(self,p,e): > original = self._validate(p) > return self._insert_between(e,original._prev,original) > > def add_after(self,p,e): > original = self._validate(p) > return self._insert_between(e,original,original._Next) > > def delete_node(self,p): > original = self._validate(p) > return self._delete_node(original) > > def replace(self,p,e): > original = self._validate(p) > old_value = original.element > original.element = e > return old_value > > > L = PositionalList() > p = L.add_last(8) > p = L.first() > print p.element() > q = L.add_after(p,5) > print q.element() > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From martin at linux-ip.net Mon Sep 21 20:16:29 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 21 Sep 2015 11:16:29 -0700 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> Message-ID: Hello again Marcus, > My task is to have lists containing 3 tuples, each tuple ocurring > only once. In my code above I started off with 6 tuples ( = s) of > which 20 lists (count) are produced containing each of the 6 > tuples multiple times. Below, I'm confirming that there are 325 input "tuples". I would, however, call them pairs (since the term tuple has a different meaning in Python). If I were to turn one of your pairs 'ab' into a tuple, I would call it ('a', 'b'). Anyway, here's confirmation on your numbers (I don't think anybody has been doubting these numbers that you keep repeating; I'm just confirming them): >>> len([''.join(x) for x in itertools.combinations(string.ascii_lowercase, 2)]) 325 Confirming that there are 20 combinations starting from 6 pairs (N.B. you have 6 pairs in your code, although they are slightly different than my 6 pairs): >>> s = [''.join(x) for x in itertools.combinations('abcd',2)] >>> len(s) 6 >>> len(list(itertools.combinations(s, 3))) 20 > Your code proposal uses dictionaries. If I used dictionaries with > all the 325 possible tuples I would have to do a lot of typing and > not getting lists with 3 tuples in it. Alan's code proposal was using dictionaries merely to count and validate. Essentially, all his code did was prove to you (and us) that itertools.combinations() was doing what it advertises. > Therefore, I try to write additional code for eliminating all > lists which contain tuples occuring more than once. If I use all > 325 tuples I would first get many thousand lists. > > So, I am stuck somehow and hope to have made my tasks clear. The problem remains that none of us understands exactly what you mean when you say duplicates. If you use itertools.combinations(), no tuple produced will contain a pair more than once. So, you clearly have some other duplicate detection criteria in mind. In your first postings, you used single letters and built tuple pairs. Now, you are using concatenated string pairs. This is syntactic sugar (for most of us). The real issue is that we do not understand your needs for duplicate detection. Several people (at least Oscar, Mark, Peter, Alan, Marc and I) have attempted to infer which duplicates you are trying to remove and even provide guidance on generic, known solutions to the problem that you are posing (the Steiner system, for example). Here are some of the earlier replies: https://mail.python.org/pipermail/tutor/2015-September/106692.html https://mail.python.org/pipermail/tutor/2015-September/106705.html In the following response you sent to the list, https://mail.python.org/pipermail/tutor/2015-September/106685.html you mentioned golfers and the passing of time, but you made no further explanation of the rules for tossing duplicates. Could you try again to explain what you mean by duplicate? It's possible that once you better explain the rules of the system you are trying to model, somebody can provide further assistance. [('ab', 'ac', 'ad'), ('ab', 'ac', 'bc'), ('ab', 'ac', 'bd'), ('ab', 'ac', 'cd'), ('ab', 'ad', 'bc'), ('ab', 'ad', 'bd'), ('ab', 'ad', 'cd'), ('ab', 'bc', 'bd'), ('ab', 'bc', 'cd'), ('ab', 'bd', 'cd'), ('ac', 'ad', 'bc'), ('ac', 'ad', 'bd'), ('ac', 'ad', 'cd'), ('ac', 'bc', 'bd'), ('ac', 'bc', 'cd'), ('ac', 'bd', 'cd'), ('ad', 'bc', 'bd'), ('ad', 'bc', 'cd'), ('ad', 'bd', 'cd'), ('bc', 'bd', 'cd')] I've pasted above a small list of tuples (using your most recent pair generation). Could you annotate each tuple and explain why it should be kept or thrown away? That may help me (and any others who are interested) understand what you are asking. -Martin >>>>> s = ['ab','ac','bc','ad','ae','de'] for startlist in >>>>> itertools.combinations(s, 3): > >> How can I concatenate the 20 lists in oder to get one count for each of the items in s , for example 10 for 'ab'? > > > If I understand you correctly, something like this: > > >>> counts = {'ab':0,'ac':0,'bc':0,'ad':0,'ae':0,'de':0} > >>> for combo in it.combinations(counts.keys(),3): > ... for pair in combo: > ... counts[pair] += 1 > ... > >>> counts > {'ac': 10, 'ab': 10, 'ae': 10, 'ad': 10, 'bc': 10, 'de': 10} > > Is that what you want? > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > > --- > Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. > https://www.avast.com/antivirus > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Martin A. Brown http://linux-ip.net/ From fal at libero.it Mon Sep 21 23:54:25 2015 From: fal at libero.it (Francesco A. Loffredo) Date: Mon, 21 Sep 2015 23:54:25 +0200 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> <55F07047.4000803@libero.it> <55F13506.4030608@libero.it> Message-ID: <56007C91.6050003@libero.it> On 15/09/2015 23.14, Francesco A. Loffredo wrote: On 14/09/2015 13:50, Oscar Benjamin wrote: ... Your algorithm loops through all triples and accepts any triple if it doesn't immediately conflict with any of the triples already accepted. If you were solving a sudoku puzzle then an analogous algorithm would take each empty square and fill it with any number that doesn't contradict any of the currently filled squares. If you try this on a real puzzle then you will reach a dead end and you won't fill the grid. The problem is that some of the squares you filled in could have had a number of possible values and you've just chosen them arbitrarily (and incorrectly). ... Also there will be many possible solutions and you only really need to find one. Up to isomorphism there is 1 solution for N=9 but that means there will be 9! isomorphic solutions (the number of ways of relabelling the numbers 1 through 9). This means that you may not have to go far in the search before coming across a solution. -- Oscar Thank you for your explanation, Oscar! I'll study a better algorithm and I'll post it here. While I hope Marcus Luetolf ( the OP) will find it useful, I will certainly get some learning from this exercise. Still thinking about it. I have read about Steiner systems and the Social Golfer problem, and I'm afraid this will remain an Unsolved Problem despite my efforts. Up to now, I thought that backtracking can't be a solution, unless I can rethink my algorithm as a tree traversal. Problem is, when you arrive at the end of the combos list and you discover you hit a dead end, how can you choose which triple you should change? One possible solution is a non-deterministic one: instead of reading the list in order, I could shuffle it. And if my solution doesn't match the formula, I could simply repeat the random reading of the list. As you said, <>! Of course, dear Tutors, if some of you can think a way to educate my guessing, you're more than welcome! Here is my current algorithm: import pprint, itertools, random, math poolbase = "abcdefghijklmnopqrstuvwxyz!ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789" def maketriples(tuplelist): final = [] used = set() for a, b, c in tuplelist: if ( ((a,b) in used) or ((b,c) in used) or ((a,c) in used) or ((b,a) in used) or ((c,b) in used) or ((c,a) in used) ): continue else: final.append((a, b, c)) used.add((a,b)) used.add((a,c)) used.add((b,c)) used.add((b,a)) used.add((c,a)) used.add((c,b)) return final def formula(sequence): dim = len(sequence) return (dim * (dim - 1) / 6) for i in range(3, len(poolbase) + 1): pool = poolbase[:i] print("pool = %s: -> '%s'" % (i, pool)) combos = list(itertools.combinations(pool, 3)) print("combos contains %s triples." % len(combos)) now_quit = 100 * len(combos) matching = False tries_count = 0 theory = formula(pool) while not matching: triples = maketriples(random.sample(combos, len(combos))) tries_count += 1 matching = (len(triples) == theory) if matching: print("Found a match after %s tries!" % tries_count) else: if not (tries_count % (10 ** int(math.log(tries_count, 10)))): print("Tried %s times..." % tries_count) if tries_count > now_quit: print("Reached %s tries, now quitting!" % tries_count) break print("maketriples(combos) yields %s triples." % len(triples)) print("formula(pool) gives %s." % theory) if matching: pprint.pprint(triples) input("\n--- Press Enter ---") else: print("\n\n") print("-------------------------------------") --------------------------- Francesco From emile at fenx.com Tue Sep 22 00:50:02 2015 From: emile at fenx.com (Emile van Sebille) Date: Mon, 21 Sep 2015 15:50:02 -0700 Subject: [Tutor] Creating a match file In-Reply-To: References: Message-ID: On 9/20/2015 12:19 PM, EILEEN CHURCH CARSON wrote: > I want to write a program that reads in data from two csv files with 6 > columns each. I want it to determines if the data in the first two columns > is the same, and if so read all six columns in that line from each of the > two files into a 12 column output file. I think I'd read in the first file and create a dictionary using the first two fields as the key, and the remaining six fields as the data. then, read in the second file, loop through again creating a key of the first two fields and if that exists in the first dictionary, get that data and write all 12 fields to a new dictionary. When done, write the results to the output file. Emile From martin at linux-ip.net Tue Sep 22 03:10:04 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 21 Sep 2015 18:10:04 -0700 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> Message-ID: Marcus, I have more questions for you, as well as a possible solution (though it's a bit more verbose than I would have liked to offer). Question: Problem A: Are you looking to identify the complete set of possible options for constructing the triples of pairs? Problem B: Are you looking only to construct one set that satisfies the problem? [see my lousy solution] You may observe that the question I ask is quite similar to the question asked by Francesco [0]. If you are asking about the complete set of possible options (Problem A), then I submit to you that you are asking a mathematical question, not a Python question. If that's the case, perhaps you should look further into the Steiner system and/or ask again on the list. If you are asking about finding an individual solution satisfying the constraints, I submit to you that either my approach or Francesco's approach could work for you. If that's the case, then using random.sample may offer you some help. See my sample, below--it should work on Python 2.x or Python 3.x. Comments: * There are rules in your system about when a player can play again. The rules were not fully clear to me, so I allowed players to be selecteda second time, if there were no more players who had not already been chosen. * This program produces only one possible scenario for 7 rounds of 3 distinct, simultaneously played 2-player games. No player can play twice in the same round. (Simple arithmetic determines the minimum number of players.) If your question is Problem A, then I wonder if you know anybody who knows combinatorics? I do not. -Martin [0] https://mail.python.org/pipermail/tutor/2015-September/106820.html #! /usr/bin/python from __future__ import print_function import sys import string import random import logging logging.basicConfig(stream=sys.stderr, level=logging.INFO) logger = logging.getLogger() class NotEnoughPlayers(Exception): pass def choose_game_participants(players, pcount=2): '''returns a tuple of players for a single game ''' if len(players) < pcount: raise NotEnoughPlayers("not enough players, need %d, have only %d: %r" % (pcount, len(players), players)) game = tuple(sorted(random.sample(players, pcount))) return game def choose_match_games(players, pcount=2, gcount=3): '''returns a list of games where no player is duplicated ''' mcount = pcount * gcount if len(players) < mcount: raise NotEnoughPlayers("not enough players, need %d, have only %d: %r" % (mcount, len(players), players)) eligible_players = random.sample(players, mcount) match = list() while eligible_players: m = choose_game_participants(eligible_players, pcount) for x in m: eligible_players.remove(x) match.append(m) match.sort() # optional return match def generate_rounds(players, pcount, gcount, rcount): games = set() matches = list() mcount = pcount * gcount eligible_players = list(players) if mcount > len(eligible_players): raise NotEnoughPlayers("not enough players (%d) to guarantee %d %d-player games per match" % (len(eligible_players), gcount, pcount)) r = 1 while r <= rcount: try: proposed_match = choose_match_games(eligible_players, pcount, gcount) except NotEnoughPlayers: logger.info("adding %d additional players in round %d to meet minimum pool requirements", mcount, r) how_many = mcount - len(eligible_players) eligible_players.extend(random.sample(players, how_many)) continue already_played = games.intersection(set(proposed_match)) if already_played: logger.info("discarding %d %r because %r have already played", r, proposed_match, list(already_played)) continue else: games.update(proposed_match) matches.append(proposed_match) logger.info('Proposed match %r', proposed_match) for game in proposed_match: for player in game: eligible_players.remove(player) r = r + 1 return matches def log_match_info(matches, detail=False): for mnum, match in enumerate(matches, 1): logger.info("match summary %d: %r", mnum, match) for gnum, game in enumerate(match, 1): if not detail: continue logger.info("match detail %d, game %d: players %r", mnum, gnum, game) def log_match_summary(matches): log_match_info(matches, detail=False) def log_match_detail(matches): log_match_info(matches, detail=True) if __name__ == '__main__': players = list(string.ascii_uppercase) random.shuffle(players) # players = set('ABCDEFGHIJ') pcount = 2 # players per game gcount = 3 # games per match rcount = 7 # rounds (of matches) matches = generate_rounds(players, pcount, gcount, rcount) log_match_detail(matches) # -- end of file -- Martin A. Brown http://linux-ip.net/ From richkappler at gmail.com Tue Sep 22 14:37:56 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 22 Sep 2015 08:37:56 -0400 Subject: [Tutor] stx, etx (\x02, \x03) Message-ID: I have a file with several lines. I need to prepend each line with \x02 and append each line with \x03 for reading into Splunk. I can get the \x02 at the beginning of each line, no problem, but can't get the \x03 to go on the end of the line. Instead it goes to the beginning of the next line. I have tried: #!/usr/bin/env python with open('input/test.xml', 'r') as f1: with open('mod1.xml', 'a') as f2: for line in f1: s = ('\x02' + line + '\x03') f2.write(s) as well as the same script but using .join, to no avail. What am I missing? regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From breamoreboy at yahoo.co.uk Tue Sep 22 14:58:32 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 22 Sep 2015 13:58:32 +0100 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: On 22/09/2015 13:37, richard kappler wrote: > I have a file with several lines. I need to prepend each line with \x02 and > append each line with \x03 for reading into Splunk. I can get the \x02 at > the beginning of each line, no problem, but can't get the \x03 to go on the > end of the line. Instead it goes to the beginning of the next line. > > I have tried: > > #!/usr/bin/env python > > with open('input/test.xml', 'r') as f1: > with open('mod1.xml', 'a') as f2: > for line in f1: > s = ('\x02' + line + '\x03') > f2.write(s) > > as well as the same script but using .join, to no avail. What am I missing? > > regards, Richard > Your line has EOL on the end. Getting rid of the unneeded brackets try:- s = '\x02' + line[:-1] + '\x03\n' -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From richkappler at gmail.com Tue Sep 22 15:13:31 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 22 Sep 2015 09:13:31 -0400 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: Thanks for the reply Mark, tried that, same result. On Tue, Sep 22, 2015 at 8:58 AM, Mark Lawrence wrote: > On 22/09/2015 13:37, richard kappler wrote: > >> I have a file with several lines. I need to prepend each line with \x02 >> and >> append each line with \x03 for reading into Splunk. I can get the \x02 at >> the beginning of each line, no problem, but can't get the \x03 to go on >> the >> end of the line. Instead it goes to the beginning of the next line. >> >> I have tried: >> >> #!/usr/bin/env python >> >> with open('input/test.xml', 'r') as f1: >> with open('mod1.xml', 'a') as f2: >> for line in f1: >> s = ('\x02' + line + '\x03') >> f2.write(s) >> >> as well as the same script but using .join, to no avail. What am I >> missing? >> >> regards, Richard >> >> > Your line has EOL on the end. Getting rid of the unneeded brackets try:- > > s = '\x02' + line[:-1] + '\x03\n' > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- All internal models of the world are approximate. ~ Sebastian Thrun From __peter__ at web.de Tue Sep 22 15:24:17 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 22 Sep 2015 15:24:17 +0200 Subject: [Tutor] stx, etx (\x02, \x03) References: Message-ID: richard kappler wrote: [Richard, please don't top-post. Thank you] > On Tue, Sep 22, 2015 at 8:58 AM, Mark Lawrence > wrote: >> On 22/09/2015 13:37, richard kappler wrote: >>> I have tried: >>> #!/usr/bin/env python >>> >>> with open('input/test.xml', 'r') as f1: >>> with open('mod1.xml', 'a') as f2: >>> for line in f1: >>> s = ('\x02' + line + '\x03') >>> f2.write(s) >>> >>> as well as the same script but using .join, to no avail. What am I >>> missing? >> Your line has EOL on the end. Getting rid of the unneeded brackets try:- >> >> s = '\x02' + line[:-1] + '\x03\n' > Thanks for the reply Mark, tried that, same result. That is *very* unlikely. Verify that you deleted the existing mod1.xml. As you open the file in append mode existing faulty lines persist. From oscar.j.benjamin at gmail.com Tue Sep 22 15:43:21 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 22 Sep 2015 14:43:21 +0100 Subject: [Tutor] Creating lists with definite (n) items without repetitions In-Reply-To: <56007C91.6050003@libero.it> References: <55E84C04.2010200@bluewin.ch> <25120664.33672.1441373226951.JavaMail.webmail@bluewin.ch> <7b8201d0e71d$efe0a850$cfa1f8f0$@bluewin.ch> <55F01290.4040407@libero.it> <55F07047.4000803@libero.it> <55F13506.4030608@libero.it> <56007C91.6050003@libero.it> Message-ID: On 21 September 2015 at 22:54, Francesco A. Loffredo wrote: >> > Still thinking about it. I have read about Steiner systems and the Social > Golfer problem, and I'm afraid this will remain an Unsolved Problem despite > my efforts. Up to now, I thought that backtracking can't be a solution, > unless I can rethink my algorithm as a tree traversal. > Problem is, when you arrive at the end of the combos list and you discover > you hit a dead end, how can you choose which triple you should change? I would remove the last one added and then loop over all triples starting from the one after that. I have written a simple implementation below. The problem is that it's really slow for anything more than N=9. There are many ways it can be improved though if you want it to scale to higher levels: #!/usr/bin/env python from itertools import permutations, combinations import pprint, itertools def get_triples(labels): Nlabels = len(labels) if Nlabels % 6 not in (1, 3): raise ValueError("No exact solution") target = (Nlabels * (Nlabels - 1)) // 6 all_triples = list(combinations(labels, 3)) Ntriples = len(all_triples) triples = [] pairs = set() start_index = 0 while True: for index in range(start_index, Ntriples): triple = all_triples[index] # If the triple fits try it out and see what happens... if not any(p in pairs for p in combinations(triple, 2)): triples.append((triple, index)) pairs.update(permutations(triple, 2)) # Success! if len(triples) == target: return [triple for triple, _ in triples] else: # We tried every combination of the current triples # and all subsequently available triples. Time to # pop off the triple at the top and look for another # one to replace it. triple, start_index = triples.pop() pairs.difference_update(permutations(triple, 2)) # Start from the triple after the popped one. start_index += 1 if __name__ == "__main__": import sys Nlabels = int(sys.argv[1]) labels = range(1, Nlabels + 1) triples = get_triples(labels) pprint.pprint(triples) To improve on this I would probably work on it as a constraint propagation problem rather than looping over all the combinations. The problem with backtracking on the full set of combinations is that it grows combinatorially which means it gets complicated very quickly as N increases. For N=9 this ran in 50ms. At the time of writing I'm still waiting for it to finish N=13 under pypy (it's only been a couple of minutes). I don't know if I expect it to finish in a few seconds or take longer than the age of the universe. -- Oscar From richkappler at gmail.com Tue Sep 22 16:07:51 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 22 Sep 2015 10:07:51 -0400 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: > > > > > That is *very* unlikely. > Perhaps, but I assure you it is what is happening. > > > Verify that you deleted the existing mod1.xml. As you open the file in > > append mode existing faulty lines persist. > I did, hence the above assurance. Here's the modified code: #!/usr/bin/env python stx = '\x02' etx = '\x03' with open('input/PS06Test_100Packages.xml', 'r') as f1: with open('mod1.xml', 'a') as f2: for line in f1: s = stx + line[:-1] + etx f2.write(s) mod1.xml has the \x02 hex at the beginning of line 1, then line 2 and all further lines begin with the \x03 hex then the \x02 hex and then the line. regards, Richard From breamoreboy at yahoo.co.uk Tue Sep 22 16:23:06 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 22 Sep 2015 15:23:06 +0100 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: On 22/09/2015 15:07, richard kappler wrote: >> >>> That is *very* unlikely. >> > > Perhaps, but I assure you it is what is happening. > >> >>> Verify that you deleted the existing mod1.xml. As you open the file in >>> append mode existing faulty lines persist. >> > > I did, hence the above assurance. > > Here's the modified code: > > #!/usr/bin/env python > > stx = '\x02' > etx = '\x03' > > with open('input/PS06Test_100Packages.xml', 'r') as f1: > with open('mod1.xml', 'a') as f2: > for line in f1: > s = stx + line[:-1] + etx > f2.write(s) > > mod1.xml has the \x02 hex at the beginning of line 1, then line 2 and all > further lines begin with the \x03 hex then the \x02 hex and then the line. > > regards, Richard Reread my original post and you'll see that your "s" isn't set the same way mine was. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From richkappler at gmail.com Tue Sep 22 16:44:24 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 22 Sep 2015 10:44:24 -0400 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: > >> > Reread my original post and you'll see that your "s" isn't set the same > way mine was No. My implementation of your code: #!/usr/bin/env python with open('input/PS06Test_100Packages.xml', 'r') as f1: with open('mod1.xml', 'a') as f2: for line in f1: s = '\x02' + line[:-1] + '\x03\n' f2.write(s) gives me: line 1 starts with the \x02 hex then the line line 2 is the \xo3 hex alone line 3 starts with the \x02 hex then the line line 4 is the \x03 hex alone lather rinse repeat. So I mispoke, please accept my apology, it wasn't exactly the same result as my original code, it put the \x03 on it's own line. Oddly enough, I'm wondering if it's the xml that's making things screwy. I ran a little experiment, creating a test text file: line one line two line three line four and ran the above code (changing the file names of course) and it came out perfect. Each line begins with the \x02 hex and ends with the \x03 hex. regards, Richard From oscar.j.benjamin at gmail.com Tue Sep 22 18:32:29 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 22 Sep 2015 17:32:29 +0100 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: On 22 September 2015 at 15:44, richard kappler wrote: >> >>> >> Reread my original post and you'll see that your "s" isn't set the same >> way mine was > > > No. My implementation of your code: > > #!/usr/bin/env python > > with open('input/PS06Test_100Packages.xml', 'r') as f1: > with open('mod1.xml', 'a') as f2: > for line in f1: > s = '\x02' + line[:-1] + '\x03\n' > f2.write(s) > > gives me: > line 1 starts with the \x02 hex then the line > line 2 is the \xo3 hex alone > line 3 starts with the \x02 hex then the line > line 4 is the \x03 hex alone The 4th part is both the \x03 character and the \n newline character as a single string. Your version doesn't have a newline character at the end of s: #!/usr/bin/env python stx = '\x02' etx = '\x03' with open('input/PS06Test_100Packages.xml', 'r') as f1: with open('mod1.xml', 'a') as f2: for line in f1: s = stx + line[:-1] + etx f2.write(s) -- Oscar From doark at mail.com Tue Sep 22 22:07:50 2015 From: doark at mail.com (David Niklas) Date: Tue, 22 Sep 2015 16:07:50 -0400 Subject: [Tutor] tkinter create a frame and whats the parent Message-ID: <20150922160750.72ae7e2b@ulgy_thing> Hello, I was following the doc, and in the beginning he gives an example where he creates a frame and accesses the main window by the name of Master. http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf I wanted to know, does the name have to be Master? And why does he pass a second arg to tk.Frame.__init___ ? Thanks, David From __peter__ at web.de Wed Sep 23 15:03:23 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 23 Sep 2015 15:03:23 +0200 Subject: [Tutor] tkinter create a frame and whats the parent References: <20150922160750.72ae7e2b@ulgy_thing> Message-ID: David Niklas wrote: > Hello, I was following the doc, and in the beginning he gives an > example where he creates a frame and accesses the main window by the > name of Master. > > http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf I suppose you are asking about """ #!/usr/bin/env python import Tkinter as tk class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.grid() self.createWidgets() def createWidgets(self): self.quitButton = tk.Button(self, text='Quit', command=self.quit) self.quitButton.grid() app = Application() app.master.title('Sample application') app.mainloop() """ A link that is more accessible than the PDF is http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/minimal-app.html > I wanted to know, does the name have to be Master? You can answer that question yourself. Rename to something unlikely to produce a nameclash, e. g. david_niklas, and then run the script again. Does it fail? If so, where? Come back here if you have trouble explaining your findings. > And why does he pass > a second arg to tk.Frame.__init___ ? Python has "bound" and "unbound" methods. Given a simple example class Foo and an instance foo >>> class Foo: ... def bar(self, x): ... return x * x ... >>> foo = Foo() you get an unbound method with >>> Foo.bar and a bound method with >>> foo.bar > A bound method stores the first argument (usually named self) so that you can omit it when you invoke the method. You normally only use bound methods as they are easier to invoke. Compare: >>> foo.bar(2) 4 >>> Foo.bar(foo, 2) 4 Bound methods also make code more generic. def add_bar_unbound(a, b, x): return Foo.bar(a, x) + Foo.bar(b, x) will always invoke Foo.bar, no matter what kind of object you provide as a and b whereas def add_bar_bound(a, b, x): return a.bar(x) + b.bar(x) will work with with any object that provides a suitable bar method. But sometimes you have to explicit. How would you write a subclass of Foo with a bar method that should do the same calculation as that of Foo.bar and then add something? def bar(self, x): return self.bar(x) + 42 doesn't work because it will invoke the same bar() method over and over again until it fails. One answer is to use an unbound method: >>> class Baz(Foo): ... def bar(self, x): ... return Foo.bar(self, x) + 42 ... >>> baz = Baz() >>> baz.bar(2) 46 Baz.bar invokes Foo.bar which calculates and returns the product of x, then Baz.bar adds 42 and returns the result. You might like to experiment a little and invoke add_bar_bound and add_bar_unbound with Foo or Baz instances or any combination. Try to predict the results. (Just in case you want to investigate further: the alternative to unbound methods in the context of inheritance is super()) From sudhierb at gmail.com Tue Sep 22 03:45:28 2015 From: sudhierb at gmail.com (Sudhier Batara) Date: Tue, 22 Sep 2015 01:45:28 +0000 Subject: [Tutor] Doubly linked list getting "AttributeError: 'NoneType' object has no attribute 'element'" In-Reply-To: <20150921181301.GI31152@ando.pearwood.info> References: <20150921181301.GI31152@ando.pearwood.info> Message-ID: Thanks for the reply Steve, I solved it myself.I was not returning 'newest' Node in parent class. --sudhier On Mon, 21 Sep 2015 11:43 pm Steven D'Aprano wrote: > Hi Sudhier, and welcome, > > Unfortunately I'm about to step away from the computer, so I can't > really answer your question in detail, but since it's been 2 or 3 days > since you asked your question, and I haven't seen any replies at all, I > thought I'd give a quick reply so you know we aren't ignoring you. > > (My inbox is suffering from overload at the moment, so it is possible > that others have answered your question and I haven't noticed. If so, my > apologies for wasting everyone's time.) > > Regards, > > > Steve > > On Sat, Sep 19, 2015 at 07:52:07PM +0530, Sudhier Batara wrote: > > Hi, > > > > I have the below code which is a doubly linked list , which returns > > the Position of the node rather than the node itself.The code is quite > > self explanatory.In the below code i call following methods , > > > > p = L.add_last(8) > > print p.element() > > > > the above methods works fine adds the element into the List instance > > and also printing the element shows the element in the list. > > > > But when i call the below methods, > > > > q = L.add_after(p,5) > > q.element() > > > > throws following error > > > > > > _______________________________________ > > in make position > > in make position > > 8 > > inside validate > > 8 > > in make position > > Traceback (most recent call last): > > File "positional_dlist.py", line 128, in > > print q.element() > > File "positional_dlist.py", line 45, in element > > return self._node.element > > AttributeError: 'NoneType' object has no attribute 'element' > > ____________________________________________________ > > > > Complete code below > > > > > > > > #!/usr/bin/python > > > > class _DoublelinkedBase: > > class _Node: > > def __init__(self,element,prev,Next): > > self.element = element > > self._prev = prev > > self._Next = Next > > > > def __init__(self): > > self._header = self._Node(None,None,None) > > self._trailer = self._Node(None,None,None) > > self._header._Next = self._trailer > > self._trailer._prev = self._header > > self._size = 0 > > > > def __len__(self): > > return self._size > > > > def is_empty(self): > > return self._size == 0 > > > > def _insert_between(self,element,predecessor,successor): > > newest = self._Node(element,predecessor,successor) > > predecessor._Next = newest > > successor._prev = newest > > self._size += 1 > > > > def _delete_node(self,node): > > predecessor = node._prev > > successor = node._Next > > predecessor._Next = successor > > successor._prev = predecessor > > element = node.element > > node.element = node._Next = node._prev = None > > self._size -= 1 > > > > class PositionalList(_DoublelinkedBase): > > class Position: > > def __init__(self,container,node): > > self._container = container > > self._node = node > > > > def element(self): > > return self._node.element > > > > def __eq__(self,other): > > return type(other) is type(self) and other._node is > self._node > > > > def __ne__(self,other): > > return not (other == self) > > > > def _validate(self, p): > > if not isinstance(p,self.Position): > > raise TypeError("p is not of type Position") > > if p._container is not self: > > raise ValueError("p is not the instance of this list") > > if p._node._Next is None: > > print p._node > > raise ValueError("No longer a valid node") > > print "inside validate" > > print p.element() > > return p._node > > > > def _make_position(self,node): > > if node is self._header or node is self._trailer: > > print "in make position for none" > > return None > > else: > > print "in make position" > > return self.Position(self,node) > > > > def first(self): > > return self._make_position(self._header._Next) > > > > def last(self): > > return self._make_position(self._trailer._prev) > > > > def before(self,p): > > node = self._validate(p) > > return self._make_position(node._prev) > > > > def after(self,p): > > node = self._validate(p) > > return self._make_position(node._Next) > > > > def __iter__(self): > > cursor = self.first() > > while cursor is not None: > > yield cursor.element() > > cursor = self.after(cursor) > > > > def _insert_between(self,e,predecessor,successor): > > #node = > > super(_DoublelinkedBase,self)._insert_between(e,predecessor,successor) > > node = > _DoublelinkedBase()._insert_between(e,predecessor,successor) > > return self._make_position(node) > > > > def add_first(self,e): > > return self._insert_between(e,self._header,self._header._Next) > > > > def add_last(self,e): > > return self._insert_between(e,self._trailer._prev,self._trailer) > > > > def add_before(self,p,e): > > original = self._validate(p) > > return self._insert_between(e,original._prev,original) > > > > def add_after(self,p,e): > > original = self._validate(p) > > return self._insert_between(e,original,original._Next) > > > > def delete_node(self,p): > > original = self._validate(p) > > return self._delete_node(original) > > > > def replace(self,p,e): > > original = self._validate(p) > > old_value = original.element > > original.element = e > > return old_value > > > > > > L = PositionalList() > > p = L.add_last(8) > > p = L.first() > > print p.element() > > q = L.add_after(p,5) > > print q.element() > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > From fiberfolly at gmail.com Sat Sep 26 01:30:28 2015 From: fiberfolly at gmail.com (D Wyatt) Date: Fri, 25 Sep 2015 16:30:28 -0700 Subject: [Tutor] Opencv In-Reply-To: References: Message-ID: On Sat, Sep 19, 2015 at 3:29 AM, Ahmed AL-Masri wrote: > Dear fellows, > I have a project for > ?... > ?Dear Ahmed, Sorry, I do not have an answer for your question, but I did want to point out that we are not all 'fellows' here :)? -- Deb Wyatt in WA From martin at linux-ip.net Sat Sep 26 19:37:53 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sat, 26 Sep 2015 10:37:53 -0700 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: <216c01d0f85d$58eb3180$0ac19480$@bluewin.ch> References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> <216c01d0f85d$58eb3180$0ac19480$@bluewin.ch> Message-ID: Good morning(PDT)/evening(CET) Marcus, > again thanks for your endeavour, ist tought me to really think deeply how to > specify my task fort he Python language. > Before I start to work with your "heavy" piece of code for a beginner below > I like to make the following statements: > > 1. my task is to create lists or tupleS (whichever works best) containing 3 > letters, later to be assigned names, in unique sequences. OK. So, your second round of postings was closer to the actual problem you are trying to solve. And, now, it's clear to me that you are operating with triples. You are doing something with pairs, but I don't understand what. See below. > 2. My first approach with pairs like 'a b', 'a c',..... does not work with > itertools.combinations(s, 3): Although, it produces lists/tuples with 3 > pairs > there are 4 letters in each list/tuple whereas I need only 3. Understood. > 3. Therfore, I'am working with single letters creating lists/tuples with 3 > letters: ('a', 'b', 'c'), ('a','b','d')........ > Using all 26 letters of the alphabet would correspond to Problem A: > Impossible to handle. Not impossible, at all. And, in fact, here is a wonderful point. If you can describe the problem using a small sample and capture all of the rules of your system (or model), then you can write a program to solve that problem. Professional programmers often use small data samples like this to test the validity of their code, before running the code on a larger input data set. Also, it sounds like you want to solve problem A, which is to enumerate (or simply find the size of) the result set in this system you are constructing. > Using only 15 letters would already create 455 lists/tuples. > The 9 letters produce 84 lists/tuples. Confirmed: >>> len(list(itertools.combinations(string.ascii_lowercase[:9], 3))) 84 >>> len(list(itertools.combinations(string.ascii_lowercase[:15], 3))) 455 > So I am using 9 letters which is practical for one letter or name can be > combined with 2 others 5 times or on 5 days, each letter/name can occur > only once a day. > > But if I am isolating lists/tuple with unique sequences by hand It is precisely here that I (and probably the others on this mailing list) do not understand what you are trying to accomplish. What are the rules that you are using for 'isolating lists with unique sequences'? It sounds like something with subsequences. I'm going to make a stab at writing down the rules I think you may be using. I will probably be wrong. Please correct me. As noted above, I'm going to use a very small sample, using an "alphabet" of 5 letters. I'm taking a stab at the rules in your system and writing down. Could you correct and/or explain whether I have understood what you are doing? [ ('a', 'b', 'c'), # keep ('a', 'b', 'd'), # discard; subsequence ('a', 'b') already seen ('a', 'b', 'e'), # discard; subsequence ('a', 'b') already seen ('a', 'c', 'd'), # keep ('a', 'c', 'e'), # discard; subsequence ('a', 'c') already seen ('a', 'd', 'e'), # keep ('b', 'c', 'd'), # discard; subsequences ('b', 'c') and ('c', 'd') already seen ('b', 'c', 'e'), # discard; subsequence ('b', 'c') already seen ('b', 'd', 'e'), # discard; subsequence ('b', 'd') already seen ('c', 'd', 'e') # keep ] Now, some additional, more general, comments.... > 4. I have come to the conclusion that my task is too mathematical for > itertools. It's possible that itertools may not have the tools exactly that you are looking for, but several of us suggested it because there seems to be some part of your problem that can be satisfied by the module. Maybe you need some other algorithm or module. I'll make an analogy. When you are building a house, you use many different sorts of tools and materials. Though programming involves no tangible result, there are many tools and techniques you can apply. Which sort of tool we might recommend depends on what sort of house you want to build. Do you want to build a house of stone, brick or wood? With curved railings? Clear or stained glass windows? We can help you by suggesting things like Python's itertools or other modules. We can suggest alternate approaches or algorithms. We can also help with the technical implementation (i.e. what does the code look like), but we can only help if the problem is clearly understood. Some of us can even help with the mathematical part of the problem (I, less so than some of the others on this list). Understanding the problem, though, is the beginning of the process of writing software. I will reproduce your wonderfully relevant comment: > [taught] me to really think deeply how to specify my task [for > the] Python language. You will always benefit from thinking deeply about what you hope to accomplish before starting to code. (I have known quite a few programmers with decades of experience who will not touch the computer until they have written a description of the problem on paper.) Also: There is no shame in writing code and throwing it away if it helps you get closer to the solution. It's common (and a good idea) to throw away code. > I hope I can be successfull with your code below although it will > me take time to comprehend it. Don't worry unduly about my code. Note that it solves Problem B, providing one possible example. This appears not to be what you want. It will not enumerate all possible examples. Good luck and enjoy the remainder of your weekend, Marcus, -Martin > -----Urspr?ngliche Nachricht----- > Von: Martin A. Brown [mailto:martin at linux-ip.net] > Gesendet: Dienstag, 22. September 2015 03:10 > An: marcus l?tolf > Cc: tutor at python.org > Betreff: Re: [Tutor] Creating lists with 3 (later4) items occuring only once > > > Marcus, > > I have more questions for you, as well as a possible solution (though it's a > bit more verbose than I would have liked to offer). > > Question: > > Problem A: Are you looking to identify the complete set of > possible options for constructing the triples of pairs? > > Problem B: Are you looking only to construct one set that > satisfies the problem? [see my lousy solution] > > You may observe that the question I ask is quite similar to the question > asked by Francesco [0]. > > If you are asking about the complete set of possible options (Problem A), > then I submit to you that you are asking a mathematical question, not a > Python question. If that's the case, perhaps you should look further into > the Steiner system and/or ask again on the list. > > If you are asking about finding an individual solution satisfying the > constraints, I submit to you that either my approach or Francesco's approach > could work for you. If that's the case, then using random.sample may offer > you some help. See my sample, below--it should work on Python 2.x or Python > 3.x. > > Comments: > > * There are rules in your system about when a player can play > again. The rules were not fully clear to me, so I allowed > players to be selecteda second time, if there were no more > players who had not already been chosen. > > * This program produces only one possible scenario for 7 > rounds of 3 distinct, simultaneously played 2-player games. > No player can play twice in the same round. (Simple arithmetic > determines the minimum number of players.) > > If your question is Problem A, then I wonder if you know anybody who knows > combinatorics? I do not. > > -Martin > > [0] https://mail.python.org/pipermail/tutor/2015-September/106820.html > > > #! /usr/bin/python > > from __future__ import print_function > > import sys > import string > import random > import logging > > logging.basicConfig(stream=sys.stderr, level=logging.INFO) logger = > logging.getLogger() > > > class NotEnoughPlayers(Exception): > pass > > > def choose_game_participants(players, pcount=2): > '''returns a tuple of players for a single game > ''' > if len(players) < pcount: > raise NotEnoughPlayers("not enough players, need %d, have only %d: > %r" % > (pcount, len(players), players)) > game = tuple(sorted(random.sample(players, pcount))) > return game > > > def choose_match_games(players, pcount=2, gcount=3): > '''returns a list of games where no player is duplicated > ''' > mcount = pcount * gcount > if len(players) < mcount: > raise NotEnoughPlayers("not enough players, need %d, have only %d: > %r" % > (mcount, len(players), players)) > eligible_players = random.sample(players, mcount) > match = list() > while eligible_players: > m = choose_game_participants(eligible_players, pcount) > for x in m: > eligible_players.remove(x) > match.append(m) > match.sort() # optional > return match > > > def generate_rounds(players, pcount, gcount, rcount): > games = set() > matches = list() > mcount = pcount * gcount > eligible_players = list(players) > if mcount > len(eligible_players): > raise NotEnoughPlayers("not enough players (%d) to guarantee %d > %d-player games per match" % > (len(eligible_players), gcount, pcount)) > r = 1 > while r <= rcount: > try: > proposed_match = choose_match_games(eligible_players, pcount, > gcount) > except NotEnoughPlayers: > logger.info("adding %d additional players in round %d to meet > minimum pool requirements", > mcount, r) > how_many = mcount - len(eligible_players) > eligible_players.extend(random.sample(players, how_many)) > continue > already_played = games.intersection(set(proposed_match)) > if already_played: > logger.info("discarding %d %r because %r have already played", > r, proposed_match, list(already_played)) > continue > else: > games.update(proposed_match) > matches.append(proposed_match) > logger.info('Proposed match %r', proposed_match) > for game in proposed_match: > for player in game: > eligible_players.remove(player) > r = r + 1 > return matches > > > def log_match_info(matches, detail=False): > for mnum, match in enumerate(matches, 1): > logger.info("match summary %d: %r", mnum, match) > for gnum, game in enumerate(match, 1): > if not detail: > continue > logger.info("match detail %d, game %d: players %r", > mnum, gnum, game) > > > def log_match_summary(matches): > log_match_info(matches, detail=False) > > > def log_match_detail(matches): > log_match_info(matches, detail=True) > > > if __name__ == '__main__': > players = list(string.ascii_uppercase) > random.shuffle(players) > # players = set('ABCDEFGHIJ') > pcount = 2 # players per game > gcount = 3 # games per match > rcount = 7 # rounds (of matches) > matches = generate_rounds(players, pcount, gcount, rcount) > log_match_detail(matches) > > # -- end of file > > > -- > Martin A. Brown > http://linux-ip.net/ > > > --- > Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. > https://www.avast.com/antivirus > > > -- Martin A. Brown http://linux-ip.net/ From badouglas at gmail.com Sun Sep 27 18:38:05 2015 From: badouglas at gmail.com (bruce) Date: Sun, 27 Sep 2015 12:38:05 -0400 Subject: [Tutor] generate a list/dict with a dynamic name.. Message-ID: Hi. I can do a basic a=[] to generate a simple list.. i can do a a="aa"+bb" how can i do a a=[] where a would have the value of "aabb" in other words, generate a list/dict with a dynamically generated name IRC replies have been "don't do it".. or it's bad.. but no one has said you can do it this way.. just curious.. thanks From kwpolska at gmail.com Sun Sep 27 18:45:20 2015 From: kwpolska at gmail.com (Chris Warrick) Date: Sun, 27 Sep 2015 18:45:20 +0200 Subject: [Tutor] generate a list/dict with a dynamic name.. In-Reply-To: References: Message-ID: On 27 September 2015 at 18:38, bruce wrote: > Hi. > > I can do a basic > a=[] > to generate a simple list.. > > i can do a a="aa"+bb" > > how can i do a > a=[] > > where a would have the value of "aabb" > > in other words, generate a list/dict with a dynamically generated name > > IRC replies have been "don't do it".. or it's bad.. but no one has > said you can do it this way.. > > just curious.. > > thanks > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor And we really mean it. There are two ways. One involves e**l, which means you are executing arbitrary code, and if a contained some malicious code, it could break your system. Using e**l is considered bad for this precise reason: you don?t know if the input might lead to formatting your hard drive. And you should not trust it to be good. The other way involves modifying the g*****s() dict. It does not always work though. But for a REAL way to do it, just create a dict and use it ? you can have arbitrary variable names just fine: things = {} a = "aabb" things[a] = [] PS. why are you creating a out of two strings? Why not just a = "aabb"? -- Chris Warrick PGP: 5EAAEA16 From breamoreboy at yahoo.co.uk Sun Sep 27 19:32:45 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 27 Sep 2015 18:32:45 +0100 Subject: [Tutor] generate a list/dict with a dynamic name.. In-Reply-To: References: Message-ID: On 27/09/2015 17:38, bruce wrote: > Hi. > > I can do a basic > a=[] > to generate a simple list.. > > i can do a a="aa"+bb" Really? >>> a="aa"+bb" File "", line 1 a="aa"+bb" ^ SyntaxError: EOL while scanning string literal No. Moral of the story, never type something in directly, always cut and paste using the interactive interpreter. > > how can i do a > a=[] > > where a would have the value of "aabb" > > in other words, generate a list/dict with a dynamically generated name I've no idea what you mean in this context. `a` is the name you are assigning some object to. `a = []` is a list, but having `a` be "aabb" means `a` is a string. So do you want:- >>> a=2*'a'+2*'b' >>> a 'aabb' Or >>> a=[2*'a'+2*'b'] >>> a ['aabb'] Or what? > > IRC replies have been "don't do it".. or it's bad.. but no one has > said you can do it this way.. What replies? Please be extremely cautious on sites that are nowhere near as precise as Python ones, for example stackoverflow or reddit. There is some complete dross out there that gets upvoted, whereas on a Python site it would be torn to pieces. To be fair, some answers are good, please just be careful with what you read. > > just curious.. > > thanks -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From steve at pearwood.info Sun Sep 27 19:40:31 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 28 Sep 2015 03:40:31 +1000 Subject: [Tutor] generate a list/dict with a dynamic name.. In-Reply-To: References: Message-ID: <20150927174030.GJ23642@ando.pearwood.info> On Sun, Sep 27, 2015 at 12:38:05PM -0400, bruce wrote: > Hi. > > I can do a basic > a=[] > to generate a simple list.. > > i can do a a="aa"+bb" > > how can i do a > a=[] > > where a would have the value of "aabb" > > in other words, generate a list/dict with a dynamically generated name You don't. That is a mistake. How would you use it? Suppose you could wave a magic wand and create such a variable: make_variable("aabb", 23) # this doesn't actually work print aabb # prints 23 But what is the point of doing that? Since you already know the variable name, just do it the good old fashioned way: aabb = 23 print 23 "Ah," you say, "but what if I don't know what the name is until the program actually runs?" name = random.choice(['fred', 'barney', 'wilma', 'betty']) make_variable(name, 23) print ... # err, what the hell do I put here??? You can see the problem. `print name` doesn't work, since that will print the name itself (say, 'fred'). Working with dynamically generated names is just a nightmare. You **really** don't want to do there. But... if you insist, and really want to shoot yourself in the foot... Python does give you the magic wand to do it. (But please don't.) In fact, two magic wands. Here's the first, using eval and exec: py> name = "fred" py> exec("%s = 23" % name) py> eval(name) 23 But don't do that: (1) It's confusing to anyone who has to understand your code. (2) It's really hard to debug when something goes wrong. (3) It's slow. About 10 times slower than regular Python code. (4) It may be a security risk. If the code you exec() or eval() ever comes from an untrusted users (say, in a web application), you have just opened a serious back door into your computer. Here's another way: py> name = "fred" py> globals()[name] = 42 py> print globals()[name] 42 That's a bit better, but still, don't do it. However, it does give us a clue how to do this safely. Instead of using a global variable, we keep track of our "dynamic variables" in their own custom namespace. (A namespace is just a fancy word for an isolated collection of named things, i.e. variables.) py> ns = {} py> name = 'wilma' py> ns[name] = 999 py> ns[name] += 1 py> print ns['wilma'] 1000 By doing it this way, you can still dynamically generate your names, but keep them isolated from the rest of the program. And you can easily print out all your "variables" to see what's going on: py> print ns {'wilma': 1000} It's just an ordinary dict. -- Steve From dyoo at hashcollision.org Sun Sep 27 20:31:40 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 27 Sep 2015 11:31:40 -0700 Subject: [Tutor] generate a list/dict with a dynamic name.. In-Reply-To: References: Message-ID: On Sun, Sep 27, 2015 at 9:38 AM, bruce wrote: > Hi. > > I can do a basic > a=[] > to generate a simple list.. > > i can do a a="aa"+bb" > > how can i do a > a=[] > > where a would have the value of "aabb" > > in other words, generate a list/dict with a dynamically generated name There is a conceptual split we make about things that are are "static" (constant throughout time), and other things should be "dynamic" (possibly changing throughout time). In modern programming style, most people prefer that the program text itself be static, but the *data* in the program may be dynamic. (This preference toward static program text has not always been true, nor is it universally true. Self-modifying programs do exist. A modern example, for example, are Just-In-Time systems, where the program text is reconstructed on-the-fly in response to the current execution of a program.) In general, most programming systems you'll see will strongly prefer that the program text be static, because static systems are easier to understand: their meaning doesn't change over time nor does it depend on the circumstances of the outside world. This being said, the preference for static program text doesn't mean we can't handle dynamically generated names. We can place the dynamism in the *data structures* that our programs handle, rather than in the program text itself. A dictionary is a data structure that can associate names to values, and that's a common tool we use to do what you're asking. For example: ###### my_dict = {} my_dict['aabb'] = [] ###### Here, the name 'aabb' is static: it's part of the program text. But the names we add to a dictionary don't always have to be static. A dictionary, in particular, allows those names to be dynamically determined. If we are using Python 3, the following snippet will show this in action: ####### my_dict = {} name = input('Please enter a name: ') my_dict[name] = [] ####### and if we are using Python 2, do this instead: ####### my_dict = {} name = raw_input('Please enter a name: ') my_dict[name] = [] ####### From guirguismichel at yahoo.co.uk Fri Sep 25 12:58:23 2015 From: guirguismichel at yahoo.co.uk (Michel Guirguis) Date: Fri, 25 Sep 2015 13:58:23 +0300 Subject: [Tutor] mathematical and statistical functions in Python Message-ID: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> Good afternoon, Thanks for your e-mail. I did not receive e-mail from Mr Otten.Basically, the problem that I am facing is that python 3.4 does not recognise the mathematical and statistical functions. For example, I am trying to find the square root, sqrt(25) but the program does not recognise the definition of sqrt(). The same problem i am facing with the mean, the variance and the standard deviation. This is the main problem. Once I solve it, I can input the parameters of the mathematical financial model. Thanks for your help, Michel From marcus.luetolf at bluewin.ch Sat Sep 26 15:14:54 2015 From: marcus.luetolf at bluewin.ch (=?iso-8859-1?Q?marcus_l=FCtolf?=) Date: Sat, 26 Sep 2015 15:14:54 +0200 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> Message-ID: <216c01d0f85d$58eb3180$0ac19480$@bluewin.ch> Hello Martin, again thanks for your endeavour, ist tought me to really think deeply how to specify my task fort he Python language. Before I start to work with your "heavy" piece of code for a beginner below I like to make the following statements: 1. my task is to create lists or tupleS (whichever works best) containing 3 letters, later to be assigned names, in unique sequences. 2. My first approach with pairs like 'a b', 'a c',..... does not work with itertools.combinations(s, 3): Although, it produces lists/tuples with 3 pairs there are 4 letters in each list/tuple whereas I need only 3. 3. Therfore, I'am working with single letters creating lists/tuples with 3 letters: ('a', 'b', 'c'), ('a','b','d')........ Using all 26 letters of the alphabet would correspond to Problem A: Impossible to handle. Using only 15 letters would already create 455 lists/tuples. So I am using 9 letters which is practical for one letter or name can be combined with 2 others 5 times or on 5 days, each letter/name can occur only once a day. The 9 letters produce 84 lists/tuples. But if I am isolating lists/tuple with unique sequences by hand I am left with 15 lists/tuples but with a uneven distrubution of the 9 letters: a 8 b 5 c 6 d 5 e 6 f 6 g 4 h 4 i 4 This variance gets the smaller the more letters are used. 4. I have come to the conclusion that my task is too mathematical for itertools. I hope I can be successfull with your code below although it will me take time to comprehend it. Sorry for this long text, regards, Marcus. -----Urspr?ngliche Nachricht----- Von: Martin A. Brown [mailto:martin at linux-ip.net] Gesendet: Dienstag, 22. September 2015 03:10 An: marcus l?tolf Cc: tutor at python.org Betreff: Re: [Tutor] Creating lists with 3 (later4) items occuring only once Marcus, I have more questions for you, as well as a possible solution (though it's a bit more verbose than I would have liked to offer). Question: Problem A: Are you looking to identify the complete set of possible options for constructing the triples of pairs? Problem B: Are you looking only to construct one set that satisfies the problem? [see my lousy solution] You may observe that the question I ask is quite similar to the question asked by Francesco [0]. If you are asking about the complete set of possible options (Problem A), then I submit to you that you are asking a mathematical question, not a Python question. If that's the case, perhaps you should look further into the Steiner system and/or ask again on the list. If you are asking about finding an individual solution satisfying the constraints, I submit to you that either my approach or Francesco's approach could work for you. If that's the case, then using random.sample may offer you some help. See my sample, below--it should work on Python 2.x or Python 3.x. Comments: * There are rules in your system about when a player can play again. The rules were not fully clear to me, so I allowed players to be selecteda second time, if there were no more players who had not already been chosen. * This program produces only one possible scenario for 7 rounds of 3 distinct, simultaneously played 2-player games. No player can play twice in the same round. (Simple arithmetic determines the minimum number of players.) If your question is Problem A, then I wonder if you know anybody who knows combinatorics? I do not. -Martin [0] https://mail.python.org/pipermail/tutor/2015-September/106820.html #! /usr/bin/python from __future__ import print_function import sys import string import random import logging logging.basicConfig(stream=sys.stderr, level=logging.INFO) logger = logging.getLogger() class NotEnoughPlayers(Exception): pass def choose_game_participants(players, pcount=2): '''returns a tuple of players for a single game ''' if len(players) < pcount: raise NotEnoughPlayers("not enough players, need %d, have only %d: %r" % (pcount, len(players), players)) game = tuple(sorted(random.sample(players, pcount))) return game def choose_match_games(players, pcount=2, gcount=3): '''returns a list of games where no player is duplicated ''' mcount = pcount * gcount if len(players) < mcount: raise NotEnoughPlayers("not enough players, need %d, have only %d: %r" % (mcount, len(players), players)) eligible_players = random.sample(players, mcount) match = list() while eligible_players: m = choose_game_participants(eligible_players, pcount) for x in m: eligible_players.remove(x) match.append(m) match.sort() # optional return match def generate_rounds(players, pcount, gcount, rcount): games = set() matches = list() mcount = pcount * gcount eligible_players = list(players) if mcount > len(eligible_players): raise NotEnoughPlayers("not enough players (%d) to guarantee %d %d-player games per match" % (len(eligible_players), gcount, pcount)) r = 1 while r <= rcount: try: proposed_match = choose_match_games(eligible_players, pcount, gcount) except NotEnoughPlayers: logger.info("adding %d additional players in round %d to meet minimum pool requirements", mcount, r) how_many = mcount - len(eligible_players) eligible_players.extend(random.sample(players, how_many)) continue already_played = games.intersection(set(proposed_match)) if already_played: logger.info("discarding %d %r because %r have already played", r, proposed_match, list(already_played)) continue else: games.update(proposed_match) matches.append(proposed_match) logger.info('Proposed match %r', proposed_match) for game in proposed_match: for player in game: eligible_players.remove(player) r = r + 1 return matches def log_match_info(matches, detail=False): for mnum, match in enumerate(matches, 1): logger.info("match summary %d: %r", mnum, match) for gnum, game in enumerate(match, 1): if not detail: continue logger.info("match detail %d, game %d: players %r", mnum, gnum, game) def log_match_summary(matches): log_match_info(matches, detail=False) def log_match_detail(matches): log_match_info(matches, detail=True) if __name__ == '__main__': players = list(string.ascii_uppercase) random.shuffle(players) # players = set('ABCDEFGHIJ') pcount = 2 # players per game gcount = 3 # games per match rcount = 7 # rounds (of matches) matches = generate_rounds(players, pcount, gcount, rcount) log_match_detail(matches) # -- end of file -- Martin A. Brown http://linux-ip.net/ --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From marcus.luetolf at bluewin.ch Sun Sep 27 13:39:02 2015 From: marcus.luetolf at bluewin.ch (=?iso-8859-1?Q?marcus_l=FCtolf?=) Date: Sun, 27 Sep 2015 13:39:02 +0200 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> <216c01d0f85d$58eb3180$0ac19480$@bluewin.ch> Message-ID: <40a201d0f919$1eb150a0$5c13f1e0$@bluewin.ch> Hello Martin. I never exspected to get such motivating comments and advice !!! Thank you again. Referring to my statments below 1. I explain my task in plain text: Flights (in golfers language) or Triples (in computer language) composed of 3 golfers (in golfers language) or 3 letters (in computer language) play once a day for n days. Each golfer or letter should only once be combined with another, meaning a combination of 2 golfers/letters should never be >1 for n days. 2. I considered ['a +b', 'a + c', 'b+c'] == ['a','b','c'] 3. I'am glad that it can be done with all letters. However, with Triples I need a number dividable by 3 so the maximum would be 24 golfers or letters. I hope to get a program allowing to change the varables like number of days played(n) and number of golfers/letters, up to a max of 24 according to different situations. That is why I limited my samples to 9 and 15. 5 and 7 would not work since ther are prime numbers. The posting of your sample with 5 letters below is correct. Having discarded the subsequences with "already seen's" 4 Triples/sequences are left but a variance of the contained letters: 'a' occurs 3 times 'b' occurs 1 time 'c' occurs 3 times 'd' occurs 3 times 'e' occurs 2 times which of course does not fit my task. That made me think itertools might not be the right tool. However, I noticed variance gets smaller with bigger samples and might turn 0 with 24 letters. But I don't know how to eliminate the "already seen's" by code. You are absolutely right that one has to write down first exactly his task to be accomplished. But my knowledge goes only as far as "Python for Infomatics" (by MOOC/Coursera) and "Practical Programming" . I know there are a myriad of other modules and tools etc. and there I need the help of "Pythonistas". To where should I proceed now ? Have a sunny Sunday, Marcus. ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- ---- -----Urspr?ngliche Nachricht----- Von: Martin A. Brown [mailto:martin at linux-ip.net] Gesendet: Samstag, 26. September 2015 19:38 An: marcus l?tolf Cc: tutor at python.org Betreff: Re: AW: [Tutor] Creating lists with 3 (later4) items occuring only once Good morning(PDT)/evening(CET) Marcus, > again thanks for your endeavour, ist tought me to really think deeply > how to specify my task fort he Python language. > Before I start to work with your "heavy" piece of code for a beginner > below I like to make the following statements: > > 1. my task is to create lists or tupleS (whichever works best) > containing 3 letters, later to be assigned names, in unique sequences. OK. So, your second round of postings was closer to the actual problem you are trying to solve. And, now, it's clear to me that you are operating with triples. You are doing something with pairs, but I don't understand what. See below. > 2. My first approach with pairs like 'a b', 'a c',..... does not work with > itertools.combinations(s, 3): Although, it produces lists/tuples with 3 > pairs > there are 4 letters in each list/tuple whereas I need only 3. Understood. > 3. Therfore, I'am working with single letters creating lists/tuples > with 3 > letters: ('a', 'b', 'c'), ('a','b','d')........ > Using all 26 letters of the alphabet would correspond to Problem A: > Impossible to handle. Not impossible, at all. And, in fact, here is a wonderful point. If you can describe the problem using a small sample and capture all of the rules of your system (or model), then you can write a program to solve that problem. Professional programmers often use small data samples like this to test the validity of their code, before running the code on a larger input data set. Also, it sounds like you want to solve problem A, which is to enumerate (or simply find the size of) the result set in this system you are constructing. > Using only 15 letters would already create 455 lists/tuples. > The 9 letters produce 84 lists/tuples. Confirmed: >>> len(list(itertools.combinations(string.ascii_lowercase[:9], 3))) 84 >>> len(list(itertools.combinations(string.ascii_lowercase[:15], 3))) 455 > So I am using 9 letters which is practical for one letter or name can > be combined with 2 others 5 times or on 5 days, each letter/name can > occur only once a day. > > But if I am isolating lists/tuple with unique sequences by hand It is precisely here that I (and probably the others on this mailing list) do not understand what you are trying to accomplish. What are the rules that you are using for 'isolating lists with unique sequences'? It sounds like something with subsequences. I'm going to make a stab at writing down the rules I think you may be using. I will probably be wrong. Please correct me. As noted above, I'm going to use a very small sample, using an "alphabet" of 5 letters. I'm taking a stab at the rules in your system and writing down. Could you correct and/or explain whether I have understood what you are doing? [ ('a', 'b', 'c'), # keep ('a', 'b', 'd'), # discard; subsequence ('a', 'b') already seen ('a', 'b', 'e'), # discard; subsequence ('a', 'b') already seen ('a', 'c', 'd'), # keep ('a', 'c', 'e'), # discard; subsequence ('a', 'c') already seen ('a', 'd', 'e'), # keep ('b', 'c', 'd'), # discard; subsequences ('b', 'c') and ('c', 'd') already seen ('b', 'c', 'e'), # discard; subsequence ('b', 'c') already seen ('b', 'd', 'e'), # discard; subsequence ('b', 'd') already seen ('c', 'd', 'e') # keep ] Now, some additional, more general, comments.... > 4. I have come to the conclusion that my task is too mathematical for > itertools. It's possible that itertools may not have the tools exactly that you are looking for, but several of us suggested it because there seems to be some part of your problem that can be satisfied by the module. Maybe you need some other algorithm or module. I'll make an analogy. When you are building a house, you use many different sorts of tools and materials. Though programming involves no tangible result, there are many tools and techniques you can apply. Which sort of tool we might recommend depends on what sort of house you want to build. Do you want to build a house of stone, brick or wood? With curved railings? Clear or stained glass windows? We can help you by suggesting things like Python's itertools or other modules. We can suggest alternate approaches or algorithms. We can also help with the technical implementation (i.e. what does the code look like), but we can only help if the problem is clearly understood. Some of us can even help with the mathematical part of the problem (I, less so than some of the others on this list). Understanding the problem, though, is the beginning of the process of writing software. I will reproduce your wonderfully relevant comment: > [taught] me to really think deeply how to specify my task [for > the] Python language. You will always benefit from thinking deeply about what you hope to accomplish before starting to code. (I have known quite a few programmers with decades of experience who will not touch the computer until they have written a description of the problem on paper.) Also: There is no shame in writing code and throwing it away if it helps you get closer to the solution. It's common (and a good idea) to throw away code. > I hope I can be successfull with your code below although it will me > take time to comprehend it. Don't worry unduly about my code. Note that it solves Problem B, providing one possible example. This appears not to be what you want. It will not enumerate all possible examples. Good luck and enjoy the remainder of your weekend, Marcus, -Martin > -----Urspr?ngliche Nachricht----- > Von: Martin A. Brown [mailto:martin at linux-ip.net] > Gesendet: Dienstag, 22. September 2015 03:10 > An: marcus l?tolf > Cc: tutor at python.org > Betreff: Re: [Tutor] Creating lists with 3 (later4) items occuring > only once > > > Marcus, > > I have more questions for you, as well as a possible solution (though > it's a bit more verbose than I would have liked to offer). > > Question: > > Problem A: Are you looking to identify the complete set of > possible options for constructing the triples of pairs? > > Problem B: Are you looking only to construct one set that > satisfies the problem? [see my lousy solution] > > You may observe that the question I ask is quite similar to the > question asked by Francesco [0]. > > If you are asking about the complete set of possible options (Problem > A), then I submit to you that you are asking a mathematical question, > not a Python question. If that's the case, perhaps you should look > further into the Steiner system and/or ask again on the list. > > If you are asking about finding an individual solution satisfying the > constraints, I submit to you that either my approach or Francesco's > approach could work for you. If that's the case, then using > random.sample may offer you some help. See my sample, below--it > should work on Python 2.x or Python 3.x. > > Comments: > > * There are rules in your system about when a player can play > again. The rules were not fully clear to me, so I allowed > players to be selecteda second time, if there were no more > players who had not already been chosen. > > * This program produces only one possible scenario for 7 > rounds of 3 distinct, simultaneously played 2-player games. > No player can play twice in the same round. (Simple arithmetic > determines the minimum number of players.) > > If your question is Problem A, then I wonder if you know anybody who > knows combinatorics? I do not. > > -Martin > > [0] > https://mail.python.org/pipermail/tutor/2015-September/106820.html > > > #! /usr/bin/python > > from __future__ import print_function > > import sys > import string > import random > import logging > > logging.basicConfig(stream=sys.stderr, level=logging.INFO) logger = > logging.getLogger() > > > class NotEnoughPlayers(Exception): > pass > > > def choose_game_participants(players, pcount=2): > '''returns a tuple of players for a single game > ''' > if len(players) < pcount: > raise NotEnoughPlayers("not enough players, need %d, have only %d: > %r" % > (pcount, len(players), players)) > game = tuple(sorted(random.sample(players, pcount))) > return game > > > def choose_match_games(players, pcount=2, gcount=3): > '''returns a list of games where no player is duplicated > ''' > mcount = pcount * gcount > if len(players) < mcount: > raise NotEnoughPlayers("not enough players, need %d, have only %d: > %r" % > (mcount, len(players), players)) > eligible_players = random.sample(players, mcount) > match = list() > while eligible_players: > m = choose_game_participants(eligible_players, pcount) > for x in m: > eligible_players.remove(x) > match.append(m) > match.sort() # optional > return match > > > def generate_rounds(players, pcount, gcount, rcount): > games = set() > matches = list() > mcount = pcount * gcount > eligible_players = list(players) > if mcount > len(eligible_players): > raise NotEnoughPlayers("not enough players (%d) to guarantee > %d %d-player games per match" % > (len(eligible_players), gcount, pcount)) > r = 1 > while r <= rcount: > try: > proposed_match = choose_match_games(eligible_players, > pcount, > gcount) > except NotEnoughPlayers: > logger.info("adding %d additional players in round %d to > meet minimum pool requirements", > mcount, r) > how_many = mcount - len(eligible_players) > eligible_players.extend(random.sample(players, how_many)) > continue > already_played = games.intersection(set(proposed_match)) > if already_played: > logger.info("discarding %d %r because %r have already played", > r, proposed_match, list(already_played)) > continue > else: > games.update(proposed_match) > matches.append(proposed_match) > logger.info('Proposed match %r', proposed_match) > for game in proposed_match: > for player in game: > eligible_players.remove(player) > r = r + 1 > return matches > > > def log_match_info(matches, detail=False): > for mnum, match in enumerate(matches, 1): > logger.info("match summary %d: %r", mnum, match) > for gnum, game in enumerate(match, 1): > if not detail: > continue > logger.info("match detail %d, game %d: players %r", > mnum, gnum, game) > > > def log_match_summary(matches): > log_match_info(matches, detail=False) > > > def log_match_detail(matches): > log_match_info(matches, detail=True) > > > if __name__ == '__main__': > players = list(string.ascii_uppercase) > random.shuffle(players) > # players = set('ABCDEFGHIJ') > pcount = 2 # players per game > gcount = 3 # games per match > rcount = 7 # rounds (of matches) > matches = generate_rounds(players, pcount, gcount, rcount) > log_match_detail(matches) > > # -- end of file > > > -- > Martin A. Brown > http://linux-ip.net/ > > > --- > Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. > https://www.avast.com/antivirus > > > -- Martin A. Brown http://linux-ip.net/ --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From vijayram.gopu at gmail.com Sat Sep 26 19:25:25 2015 From: vijayram.gopu at gmail.com (vijayram) Date: Sat, 26 Sep 2015 10:25:25 -0700 Subject: [Tutor] xunit unittest.TestSuite Message-ID: <4B6E2DFB-FB1F-4CAB-971E-60931AF585BE@gmail.com> Hi All, I am facing this same issue described here: https://github.com/nose-devs/nose/issues/542 any alternative or solution to this issue that anyone is aware of... please kindly suggest? need a tutor or help in someway Thank you, VJ From martin at linux-ip.net Sun Sep 27 20:48:28 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 27 Sep 2015 11:48:28 -0700 Subject: [Tutor] mathematical and statistical functions in Python In-Reply-To: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> References: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> Message-ID: Greetings Michel, > Thanks for your e-mail. I did not receive e-mail from Mr > Otten. Nobody in their right mind wants to receive an email from Mr. Otten. ;) > Basically, the problem that I am facing is that python 3.4 does > not recognise the mathematical and statistical functions. https://docs.python.org/3/library/math.html#module-math > For example, I am trying to find the square root, sqrt(25) but the > program does not recognise the definition of sqrt(). The same > problem i am facing with the mean, the variance and the standard > deviation. In your code, include: import math eleven = math.sqrt(121) > This is the main problem. Once I solve it, I can input the > parameters of the mathematical financial model. You also said statistics. If you are looking for the basic tools, then you can find them here: https://docs.python.org/3/library/statistics.html#module-statistics Thggere are other, richer tools in third-party libraries if you are looking for more advanced tools. But, perhaps you only need to start with the above. Good luck, -Martin -- Martin A. Brown http://linux-ip.net/ From dyoo at hashcollision.org Sun Sep 27 20:48:12 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 27 Sep 2015 11:48:12 -0700 Subject: [Tutor] mathematical and statistical functions in Python In-Reply-To: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> References: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> Message-ID: On Fri, Sep 25, 2015 at 3:58 AM, Michel Guirguis wrote: > Good afternoon, > > Thanks for your e-mail. I did not receive e-mail from Mr Otten.Basically, the problem that I am facing is that python 3.4 does not recognise the mathematical and statistical functions. Many mathematical functions are in the auxiliary "math" library: https://docs.python.org/3.3/library/math.html At the head of your program, add ###### import math ###### to tell Python to include support for the mathematical library. Then, later on in your program, you can use the square-root function by referring to "math.sqrt". ###### print(math.sqrt(25)) ###### See the documentation link above for the other common mathematical functions that you can access through "math". > For example, I am trying to find the square root, sqrt(25) but the program does not recognise the definition of sqrt(). The same problem i am facing with the mean, the variance and the standard deviation. Other functions, such as variance and standard deviation, are more specialized, and exist in other libraries. In this case, I think you want to look at the "statistics" library: https://docs.python.org/3/library/statistics.html Like the 'math' library, you will need to import the library at the head of your program before you can use it. Also note that the reference will often omit the use of the library name in its examples just to be brief. So in the example presented in: https://docs.python.org/3/library/statistics.html#statistics.stdev you may need to change this: stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]) to this: statistics.stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75]) See: https://docs.python.org/3.5/tutorial/stdlib.html#mathematics for a few more examples. If you have more questions, please feel free to ask the mailing list. Good luck. From alan.gauld at btinternet.com Sun Sep 27 20:50:48 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 27 Sep 2015 19:50:48 +0100 Subject: [Tutor] mathematical and statistical functions in Python In-Reply-To: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> References: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> Message-ID: On 25/09/15 11:58, Michel Guirguis wrote: > the problem that I am facing is that python 3.4 does not recognise the > mathematical and statistical functions. That's because they are in the math and statistics modules which you have to import: Python 3.4.0 (default, Jun 19 2015, 14:20:21) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.sqrt(25) 5.0 >>> import statistics >>> dir(statistics) ['Decimal', 'Fraction', 'StatisticsError', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_type', '_counts', '_decimal_to_ratio', '_exact_ratio', '_ss', '_sum', 'collections', 'math', 'mean', 'median', 'median_grouped', 'median_high', 'median_low', 'mode', 'pstdev', 'pvariance', 'stdev', 'variance'] >>> statistics.stdev([1,2,3,4,5,6,7,6,5,4,3,2,1]) 1.9644272343292228 >>> Python comes with literally hundreds of modules containing functions and classes which are not built in to the language. You need to import the module and then preced4e the function you require with the module name. See the module index page in the docs: https://docs.python.org/3/py-modindex.html HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Sun Sep 27 21:05:42 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 27 Sep 2015 12:05:42 -0700 Subject: [Tutor] xunit unittest.TestSuite In-Reply-To: <4B6E2DFB-FB1F-4CAB-971E-60931AF585BE@gmail.com> References: <4B6E2DFB-FB1F-4CAB-971E-60931AF585BE@gmail.com> Message-ID: On Sat, Sep 26, 2015 at 10:25 AM, vijayram wrote: > Hi All, > > I am facing this same issue described here: https://github.com/nose-devs/nose/issues/542 > > any alternative or solution to this issue that anyone is aware of... please kindly suggest? need a tutor or help in someway I don't think we can effectively answer your question, unfortunately. The question is a bit unclear, given that in both links, you're referring to bug reports that have be resolved or closed. Specifically: we don't know unambiguously what you're asking. But more fundamentally, "nose" appears to be a third-party library for unit-testing. We on Tutor specialize in introductory programming, and we typically try to stick to commonly-used standard library modules; we don't have expertise on all of Python programming. We may not be the best people to advise you on "nose" due to lack of familiarity with that library. You probably want to talk with the nose-users discussion group; try asking them here: https://groups.google.com/forum/#!forum/nose-users Good luck! From alan.gauld at btinternet.com Sun Sep 27 21:18:15 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 27 Sep 2015 20:18:15 +0100 Subject: [Tutor] Opencv In-Reply-To: References: Message-ID: On 19/09/15 11:29, Ahmed AL-Masri wrote: > I have a project for hand detection as the person wave his hand than give an action. > I am trying to use the current library in opencv using the haar cascade. This list is for people learning Python programming with the standard library. opencv is a large third party set of modules for a very specific use case. You will find a Q&A forum on the opencv web site: http://answers.opencv.org/questions/ This is much more likely to be able to help than this forum. (Although I'm always surprised by how many of these exotic questions find an answer here!) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From martin at linux-ip.net Sun Sep 27 21:22:43 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 27 Sep 2015 12:22:43 -0700 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: <40a201d0f919$1eb150a0$5c13f1e0$@bluewin.ch> References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> <216c01d0f85d$58eb3180$0ac19480$@bluewin.ch> <40a201d0f919$1eb150a0$5c13f1e0$@bluewin.ch> Message-ID: Hello Marcus, > I never exspected to get such motivating comments and advice !!! > Thank you again. Much appreciated! Each of us contributes to this mailing list in some way or another. And, not a one of us sprang fully-formed from Zeus's head with our Python expertise. > 1. I explain my task in plain text: > Flights (in golfers language) or Triples (in computer language) composed of > 3 golfers (in golfers language) or 3 letters (in > computer language) play once a day for n days. > Each golfer or letter should only once be combined with another, meaning a > combination of 2 golfers/letters should never > be >1 for n days. OK, so this certainly appears to be the social golfer problem or some similar variant. I had never heard of it before your posting and it is not a problem space I'm familiar with. Earlier, people pointed out the similarity of the Steiner triple system and Kirkman's schoolgirl problem. I think this is where you should study now. I suspect that you would benefit more from talking to somebody who knows combinatorics and/or this particular problem. In particular, I think the closest already-proposed partial solution to your problem was from Marc Tompkins. You might have a second look at that and see what you make of it: https://mail.python.org/pipermail/tutor/2015-September/106695.html I also find myself asking the same question Marc asked at the end of his posting: How confident are you about the number 301? > 2. I considered ['a +b', 'a + c', 'b+c'] == ['a','b','c'] This is a combination. So, for example, for this very small part of your problem: >>> list(itertools.combinations(('a','b','c'), 2)) [('a', 'b'), ('a', 'c'), ('b', 'c')] Now, you need to figure out how to keep the stuff you want and pitch the duplicates. > 3. I'am glad that it can be done with all letters. However, with > Triples I need a number dividable by 3 so the maximum would be 24 > golfers or letters. > > That is why I limited my samples to 9 and 15. 5 and 7 would not > work since ther are prime numbers. > > I hope to get a program allowing to change the varables like > number of days played(n) and number of golfers/letters, up to a > max of 24 according to different situations. Here's how I would accomplish the second part (this is an implementation suggestion only): import sys import string if __name__ == '__main__': try: alphalength = int(sys.argv[1]) except IndexError: alphalength = 5 alphabet = string.ascii_letters[:alphalength] result = compute_solution(alphabet) # -- print result or summary stats on result You would still have to write the computation to solve your problem and apply all the constraints you wish to apply. Now, the part that I have not done anything with is the matter of days played. > The posting of your sample with 5 letters below is correct. I goofed in my generation of the list. After writing a bit of code to try it out, I see that the last item in the list below, ('c', 'd', 'e'), should have been discarded because pair ('c', 'd') was already seen. [ ('a', 'b', 'c'), # keep ('a', 'b', 'd'), # discard; subsequence ('a', 'b') already seen ('a', 'b', 'e'), # discard; subsequence ('a', 'b') already seen ('a', 'c', 'd'), # keep ('a', 'c', 'e'), # discard; subsequence ('a', 'c') already seen ('a', 'd', 'e'), # keep ('b', 'c', 'd'), # discard; subsequences ('b', 'c') and ('c', 'd') already seen ('b', 'c', 'e'), # discard; subsequence ('b', 'c') already seen ('b', 'd', 'e'), # discard; subsequence ('b', 'd') already seen ('c', 'd', 'e') # discard: subsequenc ('c', 'd') already seen ] > That made me think itertools might not be the right tool. It can be part of the solution. See above (and below). Of course, it's not the whole solution--that's your challenge, isn't it? Here are the parts of the problem with which itertools can help you. #1: It can generate the list of possible triples for you: itertools.combinations('abcde', 3) #2: From each triple, it can generate the pairs: itertools.combinations(('a', 'b', 'c'), 2) > Having discarded > the subsequences with "already seen's" > 4 Triples/sequences are left but a variance of the contained letters: > 'a' occurs 3 times > 'b' occurs 1 time > 'c' occurs 3 times > 'd' occurs 3 times > 'e' occurs 2 times > which of course does not fit my task. > > However, I noticed variance gets smaller with bigger samples and might turn > 0 with 24 letters. The variance decrease seems nifty. > But I don't know how to eliminate the "already seen's" by code. Ah-ha! Well, see Marc Tompkins' code for an example of tracking "already seen" pairs. Here's a generic technique that will work for identifying "already seen", but tailored for your problem. # assume a single triple, for example triple = ('a', 'b', 'c') gen_pairs = lambda x: itertools.combinations(x, 2) already_seen = set() for pair in gen_pairs(triple): already_seen.add(pair) # after the loop completes, you should have: # already_seen = {('a', 'b'), ('a', 'c'), ('b', 'c')} You can then, later, test to see if the pair(s) are in the already_seen set. > You are absolutely right that one has to write down first exactly his task > to be accomplished. But my knowledge goes only > as far as "Python for Infomatics" (by MOOC/Coursera) and "Practical > Programming" . I know there are a myriad of other > modules and tools etc. and there I need the help of "Pythonistas". > > To where should I proceed now ? I would suggest the following: * write down the problem as you understand it * write code to solve the problem * find a shortcoming in the code (or your description of the problem) * try to fix the problem description or the code - if success, pat yourself on the back and drink a beer (or whatever) - if failure, send the smallest possible relevant problem description and code to the Python tutor list and we will try to help While this has been a bit of a fun problem and learning experience for me, I am going to stop posting on this thread. I lack sufficient experience in combinatorics to guide you in thinking properly (and clearly) about this problem and that is where you are stuck at the moment. Sorry I can't help you much further. Good luck, Marcus, -Martin -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Sun Sep 27 21:27:24 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 27 Sep 2015 20:27:24 +0100 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: On 22/09/15 15:44, richard kappler wrote: > gives me: > line 1 starts with the \x02 hex then the line > line 2 is the \xo3 hex alone > line 3 starts with the \x02 hex then the line > line 4 is the \x03 hex alone > lather rinse repeat. Silly possibility but are your lines fixed length? And if so might they be the width of your terminal/display so the lines are wrapping? Can you broaden the display to see if they unwrap? Just a wild idea... > So I mispoke, please accept my apology, it wasn't exactly the same result > as my original code, it put the \x03 on it's own line. > > Oddly enough, I'm wondering if it's the xml that's making things screwy. XML always makes things screwy since it doesn't have the concept of lines. If you are working with XML you should be parsing it using a proper XML parser (eg lxml) and reassembling it similarly. Thinking of XML and lines is nearly always a bad place to start. XML is a continuous text stream that sometimes is presented as a set of lines to humans. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sun Sep 27 21:30:01 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 27 Sep 2015 20:30:01 +0100 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: On 22/09/15 17:32, Oscar Benjamin wrote: >> with open('input/PS06Test_100Packages.xml', 'r') as f1: >> with open('mod1.xml', 'a') as f2: >> for line in f1: >> s = '\x02' + line[:-1] + '\x03\n' A further possibility is the [-1] is not stripping the full EOL on your system. Try using line.rstrip() instead, it should remove all non text characters. Another random thought. I'm playing catch up since I just returned from a week's vacation... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Sun Sep 27 23:46:27 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 27 Sep 2015 23:46:27 +0200 Subject: [Tutor] mathematical and statistical functions in Python In-Reply-To: References: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> Message-ID: <201509272146.t8RLkRBS013120@fido.openend.se> In a message of Sun, 27 Sep 2015 11:48:28 -0700, "Martin A. Brown" writes: > > >Greetings Michel, > >> Thanks for your e-mail. I did not receive e-mail from Mr >> Otten. > >Nobody in their right mind wants to receive an email from Mr. Otten. ;) > >> Basically, the problem that I am facing is that python 3.4 does >> not recognise the mathematical and statistical functions. > > https://docs.python.org/3/library/math.html#module-math > >> For example, I am trying to find the square root, sqrt(25) but the >> program does not recognise the definition of sqrt(). The same >> problem i am facing with the mean, the variance and the standard >> deviation. > >In your code, include: > > import math > eleven = math.sqrt(121) > >> This is the main problem. Once I solve it, I can input the >> parameters of the mathematical financial model. > >You also said statistics. If you are looking for the basic tools, >then you can find them here: > > https://docs.python.org/3/library/statistics.html#module-statistics > >Thggere are other, richer tools in third-party libraries if you are >looking for more advanced tools. But, perhaps you only need to >start with the above. > >Good luck, > >-Martin The tutor list is sending mail out late. Michel had 2 problems. The first is that he hadn't subscribed to python list, which meant that he wasn't getting the replies that weren't explicitly emailed to him. The second problem was that he didn't know he needed to import math and statistics. I think things are working ok now, and he knows about the tutor list, too. :) great, welcome. Laura From alan.gauld at btinternet.com Mon Sep 28 01:19:39 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 28 Sep 2015 00:19:39 +0100 Subject: [Tutor] mathematical and statistical functions in Python In-Reply-To: <201509272146.t8RLkRBS013120@fido.openend.se> References: <6166600D-74FE-4D9F-97C4-DE17F3E65F15@yahoo.co.uk> <201509272146.t8RLkRBS013120@fido.openend.se> Message-ID: On 27/09/15 22:46, Laura Creighton wrote: > The tutor list is sending mail out late. Partly due to me being on vacation and only checking the moderation Q twice last week. > Michel had 2 problems. The first is that he hadn't subscribed to > python list, which meant that he wasn't getting the replies that > weren't explicitly emailed to him. Plus any messages sent went to the Q. Apologies, normal service should now be restored. :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Sep 28 01:55:32 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 27 Sep 2015 16:55:32 -0700 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: On Tue, Sep 22, 2015 at 5:37 AM, richard kappler wrote: > I have a file with several lines. I need to prepend each line with \x02 and > append each line with \x03 for reading into Splunk. I can get the \x02 at > the beginning of each line, no problem, but can't get the \x03 to go on the > end of the line. Instead it goes to the beginning of the next line. Hi Richard, Just to check: what operating system are you running your program in? Also, what version of Python? This detail may matter. Your question is about line endings, and line ending conventions are platform-specific. Windows, Mac, and Linux use different character sequences for line endings, which can be infuriatingly non-uniform. If you are using Python 3, you can use "universal newline" support by default, so that you just have to consider '\n' as the line terminator in your text files. If you're in Python 2, you can open the file in universal newline mode. ############################## with open('input/test.xml', 'rU') as f1: ... ############################## See: https://docs.python.org/2/library/functions.html#open for details. > I have tried: > > #!/usr/bin/env python > > with open('input/test.xml', 'r') as f1: > with open('mod1.xml', 'a') as f2: > for line in f1: > s = ('\x02' + line + '\x03') > f2.write(s) > > as well as the same script but using .join, to no avail. Question: can you explain why the program is opening 'mod1.xml' in 'a' append mode? Why not in 'w' write mode? This may be important because multiple runs of the program will append to the end of the file, so if you inspect the output file, you may be confusing the output of prior runs of your program. Also, it's likely that the output file will be malformed, since there should just be one XML document per file. In summary: opening the output file in append mode looks a bit dubious here. To your other question: > What am I missing? Likely, the lines being returned from f1 still have a line terminator at the end. You'll want to interpose the '\x03' right before the line terminator. Mark Laurence's suggestion to use: s = '\x02' + line[:-1] + '\x03\n' looks ok to me. From dyoo at hashcollision.org Mon Sep 28 02:04:50 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 27 Sep 2015 17:04:50 -0700 Subject: [Tutor] Opencv In-Reply-To: References: Message-ID: > This list is for people learning Python programming with the standard > library. opencv is a large third party set of modules for a very > specific use case. > > You will find a Q&A forum on the opencv web site: > > http://answers.opencv.org/questions/ > > This is much more likely to be able to help than this forum. > (Although I'm always surprised by how many of these exotic > questions find an answer here!) I agree with Alan; OpenCV questions are advanced enough that you need to talk with the community that is most familiar with them. You might also try Stack Overflow with questions tagged with the "opencv" category: http://stackoverflow.com/questions/tagged/opencv This is not to say that there is no one here in Tutor that can help, but you are more likely to get a good answer from OpenCV specialists in this situation. From oscar.j.benjamin at gmail.com Mon Sep 28 14:36:42 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 28 Sep 2015 13:36:42 +0100 Subject: [Tutor] Creating lists with 3 (later4) items occuring only once In-Reply-To: <40a201d0f919$1eb150a0$5c13f1e0$@bluewin.ch> References: <0f9401d0f228$8d4e31b0$a7ea9510$@bluewin.ch> <4e2601d0f3bb$9a133b90$ce39b2b0$@bluewin.ch> <216c01d0f85d$58eb3180$0ac19480$@bluewin.ch> <40a201d0f919$1eb150a0$5c13f1e0$@bluewin.ch> Message-ID: On 27 September 2015 at 12:39, marcus l?tolf wrote: > Hello Martin. > I never exspected to get such motivating comments and advice !!! Thank you > again. > Referring to my statments below > > 1. I explain my task in plain text: > Flights (in golfers language) or Triples (in computer language) composed of > 3 golfers (in golfers language) or 3 letters (in > computer language) play once a day for n days. > Each golfer or letter should only once be combined with another, meaning a > combination of 2 golfers/letters should never > be >1 for n days. > > 2. I considered ['a +b', 'a + c', 'b+c'] == ['a','b','c'] > > 3. I'am glad that it can be done with all letters. However, with Triples I > need a number dividable by 3 so the maximum would be > 24 golfers or letters. I hope to get a program allowing to change the > varables like number of days played(n) and number of > golfers/letters, up to a max of 24 according to different situations. > > That is why I limited my samples to 9 and 15. 5 and 7 would not work since > ther are prime numbers. Ah okay. This is what I thought your problem was but with an additional constraint. I will spell out the two constraints formally: You want a set of triples where each triple consists of three distinct players and: 1) Each player plays each other player exactly once. 2) It is possible to arrange the triples so that every player plays once on each day. Constraint 1 means that you need N=1mod6 or N=3mod6. Constraint 2 means that you need N to be a multiple of three so we cannot have N=1mod6. This means that we must have N=3mod6 or in other words: N = 3, 9, 15, 21, 27, ... If I understand correctly you are only interested to know how to find a solution to constraints 1) and 2) for these values of N. You do not care what happens for values of N where a perfect solution is impossible. > The posting of your sample with 5 letters below is correct. Having discarded > the subsequences with "already seen's" > 4 Triples/sequences are left but a variance of the contained letters: > 'a' occurs 3 times > 'b' occurs 1 time > 'c' occurs 3 times > 'd' occurs 3 times > 'e' occurs 2 times > which of course does not fit my task. That made me think itertools might > not be the right tool. > However, I noticed variance gets smaller with bigger samples and might turn > 0 with 24 letters. So you want every player to play the same number of times? That's reasonable. Is it also important that every player plays every other player exactly once (i.e. constraint 1) )? Unfortunately you will not be able to meet constraint 1) for N=24 since it is an even number. > But I don't know how to eliminate the "already seen's" by code. > > You are absolutely right that one has to write down first exactly his task > to be accomplished. But my knowledge goes only > as far as "Python for Infomatics" (by MOOC/Coursera) and "Practical > Programming" . I know there are a myriad of other > modules and tools etc. and there I need the help of "Pythonistas". > To where should I proceed now ? I would suggest that you begin working on pen and paper. How can you construct a solution for N=3, N=9 etc? An algorithm occurs to me as follows. Consider N=9 so we have golfers ABCDEFGHI. We know that player A needs to play some games so let's begin there (A... ... Player A will at some point need to play both B and C and has not played them yet so (ABC) ... Player A has not played everyone and will need to play players D and E so (ABC) (ADE) ... Continuing we have (ABC) (ADE) (AFG) (AHI) ... Now player A has played everyone but player B hasn't so (ABC) (ADE) (AFG) (AHI) (B...) ... Player B has already played both A and C which leaves the possibilities DEFGHI. At some point player B must play D so let's assume that's what happens: (ABC) (ADE) (AFG) (AHI) (BD...) ... Who can play with B and D? Well B has played A and C and D has played A and E. So that leaves FGHI. We now loop through those possibilities trying each out. This is where the algorithm recurses: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI ... Now B has still not played everyone and at some point B must play G so let's try that out: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BG...) ... Okay so who can play with B and G. Well B has played ACDF and G has played AF so we need something that is not ABCDFG which leaves EHI as possibilities. Let's loop through these trying them out. We'll begin with E: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGE) - E could be HI ... Now B still hasn't played everyone and there's only two possibilities H and I. which gives (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGE) - E could be HI (BHI) <- conflict!! ... Oh dear H and I have already played each other. Now we need to back track. Maybe we were wrong to choose E? We'll try H instead: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGH) - H could be I (E was already rejected on backtrack) ... Now B has one more game to play against E and I: (ABC) (ADE) (AFG) (AHI) (BDF) - F could be GHI (BGH) - H could be I (E was already rejected on backtrack) (BEI) ... Okay so now A and B have played everyone. What about C? And so on... Follow this through on pen and paper. I don't think it will take too long. Once you understand the algorithm you can think about how to tell the computer to do it. The target is that every player has played every other. You will have reached this target when the number of triples is N*(N-1)/6. Why is that? Well how many pairs are there? I can make all pairs by combining each of N people with each of the N-1 remaining people. However this double counts each pair since I count AB and BA. So the number of pairs is N*(N-1)/2. Each pair is contained in exactly one triple and each triple contains 3 distinct pairs. It follows that the number of pairs is 3 times the number of triples. So the number of triples is N*(N-1)/6. If we were working with quadruples instead of triples then each quadruple contains 6 distinct pairs so the number of quadruples is N*(N-1)/12. In general if we work k-tuples then the number of pairs in each k-tuple is k*(k-1)/2. It follows that the number of k-tuples is N*(N-1)/(k*(k-1)) so when we have that many we've reached a solution. Clearly a necessary (but not sufficient) condition for the existence of a perfect solution is that N*(N-1) is divisible by k*(k-1). We also know that N-1 needs to be divisible by k-1 since player each player plays every other player in groups of size k-1. Finally we have that N itself must be divisible by k so that every golfer can play on every day. Bringing these together we have that N is divisible by k and N-1 is divisible by k-1. For triples this gives: N = 3, 9, 15, 21, 27, 33, ... which is correct since we already established that N=3mod6. For quadruples k=4 and we need that N is divisible by 4 and n-1 is divisible by 3 which gives: N = 4, 16, 28, 40, 52, ... In other words N=4mod12. However is is possible that not all of these admit perfect solutions. In general you may need to simply try and find a perfect solution exhaustively but the above two results suggest the general form N=k mod k*(k-1) This guarantees that N is divisible by k and N-1 by k-1. Which is certainly a necessary condition but not necessarily a sufficient one. -- Oscar From richkappler at gmail.com Mon Sep 28 15:04:39 2015 From: richkappler at gmail.com (richard kappler) Date: Mon, 28 Sep 2015 09:04:39 -0400 Subject: [Tutor] stx, etx (\x02, \x03) In-Reply-To: References: Message-ID: > > Hi Richard, > > Just to check: what operating system are you running your program in? > Also, what version of Python? > Hi Danny, using Linux and Python 2.7 > > > > ############################## > with open('input/test.xml', 'rU') as f1: ... > ############################## > > > Question: can you explain why the program is opening 'mod1.xml' in 'a' > append mode? Why not in 'w' write mode? > > > This may be important because multiple runs of the program will append > to the end of the file, so if you inspect the output file, you may be > confusing the output of prior runs of your program. Also, it's likely > that the output file will be malformed, since there should just be one > XML document per file. In summary: opening the output file in append > mode looks a bit dubious here. > > > > To your other question: > > > What am I missing? > > Likely, the lines being returned from f1 still have a line terminator > at the end. You'll want to interpose the '\x03' right before the line > terminator. Mark Laurence's suggestion to use: > > s = '\x02' + line[:-1] + '\x03\n' > > looks ok to me. > Actually, you solved it, but it required both with open('input/test.xml', 'rU') as f1:... and s = 'x02' + line[:-1] + 'x03\n' Either of the above alone do not resolve the issue, but both together do. As far as the write vs append, I understand your concerns but that is for testing during scripting only. Ultimately the script outputs to a socket on another machine, not to a file, so that gets commented out. The append is because I am using a 100 line test file with a for line in f1: statement in it, and I want the results for each line appended to the output file. I delete the results file before each run. Thanks for all the help! regards, Richard -- All internal models of the world are approximate. ~ Sebastian Thrun From kfh777 at earthlink.net Mon Sep 28 22:27:19 2015 From: kfh777 at earthlink.net (Ken Hammer) Date: Mon, 28 Sep 2015 16:27:19 -0400 Subject: [Tutor] Python type confusion? Message-ID: A simple "type" problem? The following code works as a py file with the XX'd lines replacing the two later "raw_input" lines. Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? Same result if I use/omit the parens around the poly tuple. #### evaluate a polynomial as formula for a defined function ##poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2 ##x = -13 poly = raw_input("Type, 0.0-n ,") ## these do not work in place of above x = raw_input("Type your val of x, ") ## 'str' object is not callable? total = 0.0 for i in range(len(poly)): totalTerm = poly[i]* (x ** i) total += totalTerm print "totalTerm ", i , totalTerm print "Equation Value", total #### Good answer follows: totalTerm 0 0.0 totalTerm 1 0.0 totalTerm 2 845.0 totalTerm 3 -20432.1 totalTerm 4 199927.0 Equation Value 180339.9 >>> thanks, Ken Hammer From illusiontechniques at gmail.com Tue Sep 29 01:13:15 2015 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Sep 2015 16:13:15 -0700 Subject: [Tutor] Python type confusion? In-Reply-To: References: Message-ID: On Mon, Sep 28, 2015 at 1:27 PM, Ken Hammer wrote: > A simple "type" problem? > > The following code works as a py file with the XX'd lines replacing the two later "raw_input" lines. > Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? Same result if I use/omit > the parens around the poly tuple. > > #### evaluate a polynomial as formula for a defined function > ##poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2 > ##x = -13 > poly = raw_input("Type, 0.0-n ,") ## these do not work in place of above > x = raw_input("Type your val of x, ") ## 'str' object is not callable? y = int(x) #see below > > total = 0.0 > for i in range(len(poly)): > totalTerm = poly[i]* (x ** i) Here you would get "unsupported operand type" since you are getting the ith power of a string. > total += totalTerm > print "totalTerm ", i , totalTerm > print "Equation Value", total > > #### Good answer follows: > totalTerm 0 0.0 > totalTerm 1 0.0 > totalTerm 2 845.0 > totalTerm 3 -20432.1 > totalTerm 4 199927.0 > Equation Value 180339.9 >>>> > thanks, Ken Hammer > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From illusiontechniques at gmail.com Tue Sep 29 01:13:58 2015 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Sep 2015 16:13:58 -0700 Subject: [Tutor] Python type confusion? In-Reply-To: References: Message-ID: On Mon, Sep 28, 2015 at 4:13 PM, C Smith wrote: > On Mon, Sep 28, 2015 at 1:27 PM, Ken Hammer wrote: >> A simple "type" problem? >> >> The following code works as a py file with the XX'd lines replacing the two later "raw_input" lines. >> Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? Same result if I use/omit >> the parens around the poly tuple. >> >> #### evaluate a polynomial as formula for a defined function >> ##poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2 >> ##x = -13 >> poly = raw_input("Type, 0.0-n ,") ## these do not work in place of above >> x = raw_input("Type your val of x, ") ## 'str' object is not callable? > y = int(x) #see below > >> >> total = 0.0 >> for i in range(len(poly)): >> totalTerm = poly[i]* (x ** i) > Here you would get "unsupported operand type" since you are getting > the ith power of a string. EDIT: change "x" to "y" > > >> total += totalTerm >> print "totalTerm ", i , totalTerm >> print "Equation Value", total >> >> #### Good answer follows: >> totalTerm 0 0.0 >> totalTerm 1 0.0 >> totalTerm 2 845.0 >> totalTerm 3 -20432.1 >> totalTerm 4 199927.0 >> Equation Value 180339.9 >>>>> >> thanks, Ken Hammer >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor From dyoo at hashcollision.org Tue Sep 29 01:34:45 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 28 Sep 2015 16:34:45 -0700 Subject: [Tutor] Python type confusion? In-Reply-To: References: Message-ID: On Mon, Sep 28, 2015 at 1:27 PM, Ken Hammer wrote: > A simple "type" problem? > > The following code works as a py file with the XX'd lines replacing the two later "raw_input" lines. > Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? Same result if I use/omit > the parens around the poly tuple. As C Smith notes, raw_input() returns a string. As the name suggests, it treats its input as raw text, and does not try to interpret it as data. Interpreting text as data isn't too bad if we follow certain conventions. One of the most popular conventions is to treat the text as JavaScript object notation (JSON), because basically everything knows how to parse JSON these days. We can use the 'json' module to parse JSON-encoded text. For example, here's some sample use of the json.loads() string-parsing function from the interactive interpreter: ####################################### >>> import json >>> json.loads('[0.0, 0.0, 5.0, 9.3, 7.0]') [0.0, 0.0, 5.0, 9.3, 7.0] >>> json.loads('42') 42 ####################################### The main limitation here is that this knows how to handle lists, but it doesn't know how to handle tuples, since there's no such thing as tuples in JSON. Hopefully that isn't too painful for your case, but let us know if it is. To use this parser for your own program, add the import to the top of your program: ######### import json ######### and then wrap a use of json.loads() around each of the raw_input() calls. Like this: ########################################## import json poly = json.loads(raw_input("Type, 0.0-n ,")) x = json.loads(raw_input("Type your val of x, ")) # ... rest of program here ########################################## Hope this helps! From dyoo at hashcollision.org Tue Sep 29 01:41:47 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 28 Sep 2015 16:41:47 -0700 Subject: [Tutor] Python type confusion? In-Reply-To: References: Message-ID: > As C Smith notes, raw_input() returns a string. As the name suggests, > it treats its input as raw text, and does not try to interpret it as > data. Whoops! I slightly misspoke here: I mean to say that it does not try to interpret it as *structured* data. That is, we want things that look like numbers to be treated as numbers. Likewise, we'd like a string that looks like a list of numbers to be treated as a list of numbers. That's the role of a parser, and why we need to do something, such as using the json.loads() function. Others on the list might suggest instead using input() instead of raw_input(), which will try to interpret what you enter in as if it were a Python expression. Effectively, this will also parse the text into structured values. However, although this is a straightforward way to make your program work, I don't think it's a good approach because input() has a hidden use of the infamous "eval()" function embedded in it. Certain inputs to input() can cause your program to do weird things due to its use of eval(), so I think it's best to avoid it. Dunno if you're familiar with all the problems with eval(); if you are interested, ask, and I'm sure there will be a lot of response from the list. Best of wishes to you! From nymcity at yahoo.com Tue Sep 29 01:45:48 2015 From: nymcity at yahoo.com (Nym City) Date: Mon, 28 Sep 2015 23:45:48 +0000 (UTC) Subject: [Tutor] Custom Function that Takes argument Message-ID: <2095555101.2224584.1443483948446.JavaMail.yahoo@mail.yahoo.com> Hello, I am learning how to create custom functions and watched the tutorial online that uses API for locu to do cool stuff. I wanted to go little bit beyond the tutorial and add my own features. The problem that I am running into is that I cannot figure out how to prompt a user to input their zip code and use that information against the function. here is my code: import urllib2 import json locu_api = 'not included here for obvious reasons' zip = raw_input('What is your zip: ') def zip_search(query): ??? api_key = locu_api ??? url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key ??? zip = query.replace(' ', '%20') ??? final_url = url + '&zip=' + zip + "&category=restaurant" ??? jason_obj = urllib2.urlopen(final_url) ??? data = json.load(jason_obj) ??? for item in data['objects']: ??????? print item['name'], item['phone'], item['street_address'], item['categories'] print zip_search(zip_search()) ?Thank you. From alan.gauld at btinternet.com Tue Sep 29 03:03:09 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 29 Sep 2015 02:03:09 +0100 Subject: [Tutor] Custom Function that Takes argument In-Reply-To: <2095555101.2224584.1443483948446.JavaMail.yahoo@mail.yahoo.com> References: <2095555101.2224584.1443483948446.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 29/09/15 00:45, Nym City via Tutor wrote: > I am learning how to create custom functions and watched the > tutorial online that uses API for locu Since most folks here probably don't know locu you should maybe give us a URL. Then we can know what it is you are talking about. > I cannot figure out how to prompt a user to input their zip > code and use that information against the function. The normal way is to pass it in as a parameter of the function: def myfunc(aZipCode): print aZipCode zip = raw_input('What is your zip: ') myfunc(zip) > import urllib2 > import json > > locu_api = 'not included here for obvious reasons' > zip = raw_input('What is your zip: ') > > def zip_search(query): > api_key = locu_api > url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key > zip = query.replace(' ', '%20') > final_url = url + '&zip=' + zip + "&category=restaurant" > jason_obj = urllib2.urlopen(final_url) > data = json.load(jason_obj) > for item in data['objects']: > print item['name'], item['phone'], item['street_address'], item['categories'] > > print zip_search(zip_search()) Here you are passing the results of your own function in as an argument to your function. That should give an error since the inner call has no argument and the function is looking for one. You need to pass in your query string: print zip_search(zip) Always post any errors you get it helps us help you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Tue Sep 29 03:18:31 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 29 Sep 2015 02:18:31 +0100 Subject: [Tutor] Python type confusion? In-Reply-To: References: Message-ID: On 28/09/2015 21:27, Ken Hammer wrote: As you've all ready had answers I've just one thing to say below. > total = 0.0 > for i in range(len(poly)): > totalTerm = poly[i]* (x ** i) > total += totalTerm > print "totalTerm ", i , totalTerm > print "Equation Value", total > Perhaps the most used piece of code that although not actually wrong, is certainly frowned upon as you can write:- for i, item in enumerate(poly): totalTerm = item * (x ** i) ... -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From questions.anon at gmail.com Tue Sep 29 05:16:36 2015 From: questions.anon at gmail.com (questions anon) Date: Tue, 29 Sep 2015 13:16:36 +1000 Subject: [Tutor] skip/slice more than every second? Message-ID: a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] how can I show the first then skip three and show the next and so on? For example: show 1 then skip 2,3,4 then show 5 then skip 6,7,8 then show 9 then skip 10,11,12, etc. Any feedback will be greatly appreciated. From questions.anon at gmail.com Tue Sep 29 05:54:44 2015 From: questions.anon at gmail.com (questions anon) Date: Tue, 29 Sep 2015 13:54:44 +1000 Subject: [Tutor] skip/slice more than every second? In-Reply-To: References: Message-ID: thankyou but I just realised I wrote the question wrong - how do I do the inverse of above so hide 1 show 2,3,4 hide 5, show 6,7,8 etc. thanks in advance On Tue, Sep 29, 2015 at 1:33 PM, Sebastian Cheung < sebastian_cheung at yahoo.com> wrote: > print range(1,15,4) > > ans: [1, 5, 9,13] > > > > Sent from my iPhone > > > On 29 Sep 2015, at 04:16, questions anon > wrote: > > > > a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] > > > > how can I show the first then skip three and show the next and so on? > > For example: > > show 1 > > then skip 2,3,4 > > then show 5 > > then skip 6,7,8 > > then show 9 > > then skip 10,11,12, > > > > etc. > > > > Any feedback will be greatly appreciated. > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Tue Sep 29 05:55:41 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 29 Sep 2015 13:55:41 +1000 Subject: [Tutor] skip/slice more than every second? In-Reply-To: References: Message-ID: <20150929035541.GP23642@ando.pearwood.info> On Tue, Sep 29, 2015 at 01:16:36PM +1000, questions anon wrote: > a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] > > how can I show the first then skip three and show the next and so on? > For example: > show 1 > then skip 2,3,4 > then show 5 > then skip 6,7,8 > then show 9 > then skip 10,11,12, Here is one method, using slicing: py> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] py> a[::4] [1, 5, 9, 13] The slice a[::4] is equivalent to a[0:len(a):4], that is, it starts at position 0, keeps going until the end, and extracts every fourth value. Here is another method: py> it = iter(a) py> while True: ... try: ... print(next(it)) ... who_cares = next(it), next(it), next(it) ... except StopIteration: ... break ... 1 5 9 13 This grabs each item in turn, prints the first, then throws away three, then prints the next, and so on. This is a good technique for when you want something that doesn't fit into the fairly simple patterns available using slicing, e.g.: - print 1 item; - throw away 2; - print 3 items; - throw away 4; - print 5 items; - throw away 6; - print 7 items; - throw away 8; etc. But for cases where slicing does the job, it is better to use that. -- Steve From steve at pearwood.info Tue Sep 29 06:16:39 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 29 Sep 2015 14:16:39 +1000 Subject: [Tutor] skip/slice more than every second? In-Reply-To: References: Message-ID: <20150929041639.GR23642@ando.pearwood.info> On Tue, Sep 29, 2015 at 01:54:44PM +1000, questions anon wrote: > thankyou but I just realised I wrote the question wrong - > > how do I do the inverse of above > so > hide 1 show 2,3,4 hide 5, show 6,7,8 etc. > > thanks in advance A little more tricky. The version I showed using iter() and a while loop will work, but perhaps this is simpler: py> for i in range(0, len(a), 4): ... print(a[i+1:i+4]) ... [2, 3, 4] [6, 7, 8] [10, 11, 12] [14] -- Steve From __peter__ at web.de Tue Sep 29 09:33:16 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 29 Sep 2015 09:33:16 +0200 Subject: [Tutor] skip/slice more than every second? References: Message-ID: questions anon wrote: > thankyou but I just realised I wrote the question wrong - > > how do I do the inverse of above > so > hide 1 show 2,3,4 hide 5, show 6,7,8 etc. > > thanks in advance You can use del on a slice: >>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] >>> del a[::4] >>> a [2, 3, 4, 6, 7, 8, 10, 11, 12, 14] As this modifies the list you may want to make a copy first. From alan.gauld at btinternet.com Tue Sep 29 11:36:13 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 29 Sep 2015 10:36:13 +0100 Subject: [Tutor] skip/slice more than every second? In-Reply-To: References: Message-ID: On 29/09/15 08:33, Peter Otten wrote: >> hide 1 show 2,3,4 hide 5, show 6,7,8 etc. >> >> thanks in advance > > You can use del on a slice: And just for kicks you can use a list comprehension: print [n for i,n in enumerate(a) if i%4] hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From chirag.shahani at gmail.com Tue Sep 29 04:28:47 2015 From: chirag.shahani at gmail.com (Chirag Shahani) Date: Mon, 28 Sep 2015 19:28:47 -0700 Subject: [Tutor] Regarding installing a python package Message-ID: Hi, Could any one please help me understand the following: Suppose I install a python package using python setup.py install provided by the developer of a package. I need to track that changes in my file system.. meaning what new directories and files got created? Also, what is the concept of the egg file that gets created? What would be the location of this egg file and how would it be named ****.egg? Also, the ***.egg-info directory? Thanks in advance. -- Chirag From sebastian_cheung at yahoo.com Tue Sep 29 05:33:24 2015 From: sebastian_cheung at yahoo.com (Sebastian Cheung) Date: Tue, 29 Sep 2015 04:33:24 +0100 Subject: [Tutor] skip/slice more than every second? In-Reply-To: References: Message-ID: print range(1,15,4) ans: [1, 5, 9,13] Sent from my iPhone > On 29 Sep 2015, at 04:16, questions anon wrote: > > a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] > > how can I show the first then skip three and show the next and so on? > For example: > show 1 > then skip 2,3,4 > then show 5 > then skip 6,7,8 > then show 9 > then skip 10,11,12, > > etc. > > Any feedback will be greatly appreciated. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Tue Sep 29 12:30:04 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 29 Sep 2015 11:30:04 +0100 Subject: [Tutor] skip/slice more than every second? In-Reply-To: References: Message-ID: On 29/09/15 04:33, Sebastian Cheung via Tutor wrote: >> On 29 Sep 2015, at 04:16, questions anon wrote: >> >> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] >> >> how can I show the first then skip three and show the next and so on? > print range(1,15,4) > > ans: [1, 5, 9,13] > While this gives the output that the OP asked for, its what's known as a coincidental solution. It is just a coincidence that the data generated by range() matches the sample data provided. If the OP had provided a random set of data or used a different type (eg characters) then this would not help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Sep 29 12:34:17 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 29 Sep 2015 11:34:17 +0100 Subject: [Tutor] Merging of two separate flask apps In-Reply-To: References: Message-ID: <560A6929.5080800@btinternet.com> On 29/09/15 11:06, Sebastian Cheung wrote: > The idea is learn how to merge different Flask apps as one. Please don't hijack an existing thread for new topics, it messes up the archive for searching. However, in this case the question is very Flask specific and would be better directed to a Flask community forum such as one of those described here: http://flask.pocoo.org/community/ This list is really for core Python language/library questions. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From sebastian_cheung at yahoo.com Tue Sep 29 12:06:05 2015 From: sebastian_cheung at yahoo.com (Sebastian Cheung) Date: Tue, 29 Sep 2015 11:06:05 +0100 Subject: [Tutor] Merging of two separate flask apps In-Reply-To: References: Message-ID: Hi, I am trying to merge two flask apps, both from Miguel Grinberg's Flask-SocketIO-Chat into another one of his Microblog so that after user login to the main microblog then the other Chat app could be launched if user selects of the options, which in this case is the Chat. The idea is learn how to merge different Flask apps as one. Sent from my iPhone > On 29 Sep 2015, at 10:36, Alan Gauld wrote: > > On 29/09/15 08:33, Peter Otten wrote: > >>> hide 1 show 2,3,4 hide 5, show 6,7,8 etc. >>> >>> thanks in advance >> >> You can use del on a slice: > > And just for kicks you can use a list comprehension: > > print [n for i,n in enumerate(a) if i%4] > > hth > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From lac at openend.se Tue Sep 29 13:20:16 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 29 Sep 2015 13:20:16 +0200 Subject: [Tutor] Regarding installing a python package In-Reply-To: References: Message-ID: <201509291120.t8TBKGVT009177@fido.openend.se> In a message of Mon, 28 Sep 2015 19:28:47 -0700, Chirag Shahani writes: >Hi, > >Could any one please help me understand the following: > >Suppose I install a python package using python setup.py install provided >by the developer of a package. I need to track that changes in my file >system.. meaning what new directories and files got created? Also, what is >the concept of the egg file that gets created? What would be the location >of this egg file and how would it be named ****.egg? Also, the ***.egg-info >directory? > >Thanks in advance. > >-- >Chirag This is a useful package to install. https://pypi.python.org/pypi/watchdog It will happily log all changes to your filesystem, or only in the places you are interested in watching. However, the desire to do such a thing often means that your real problem is that you are using python setup.py to install packages. This can be ok if that is what you want to do, but it may be that in your case what you want to do instead is to make a virtual environment and install your package(s) there. This means you can keep your system, global-wide environment pure, uncluttered with packages, and have a separate virtualenv for each combination of python packages you want to work with together. And you don't have to worry about which things got installed globally at all -- because none of them do. Laura From oscar.j.benjamin at gmail.com Tue Sep 29 16:28:26 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 29 Sep 2015 14:28:26 +0000 Subject: [Tutor] Regarding installing a python package In-Reply-To: References: Message-ID: On Tue, 29 Sep 2015 at 10:47 Chirag Shahani wrote: > Hi, > > Could any one please help me understand the following: > > Suppose I install a python package using python setup.py install provided > by the developer of a package. I need to track that changes in my file > system.. meaning what new directories and files got created? Also, what is > the concept of the egg file that gets created? What would be the location > of this egg file and how would it be named ****.egg? Also, the ***.egg-info > directory? > It really depends. The setup.py script is really just an arbitrary program that can do anything and can create files in any location it chooses. Mostly setup.py files have a standard behaviour which is to install files in the site-packages folder for your Python installation (unless you provide command line options indicating an alternative location) and to install executables so that they will be available on PATH. So usually if I have a project called project_name then it will create a folder called project_name in the site-packages folder and then fill that folder with Python files and maybe some other data files. Really though it could do anything so if you need to know exactly what it's doing then you'll need to read the setup.py and understand distutils and setuptools. For example in my system the site-packages folder is called /usr/lib/python2.7/dist-packages For some reason Ubuntu calls it dist-packages but in most Python installations it is called site-packages. If I install ipython by running its setup.py then I will have an egg-info file called: /usr/lib/python2.7/dist-packages/ipython-0.12.1.egg-info and also a folder called /usr/lib/python2.7/dist-packages/IPython which contains all of the Python files for ipython. It also installs an executable file called ipython which is found at /usr/bin/ipython The exact details of what files are installed and where they go can vary depending on what operating system and Python version you are using. -- Oscar From crusier at gmail.com Tue Sep 29 17:47:02 2015 From: crusier at gmail.com (Crusier) Date: Tue, 29 Sep 2015 08:47:02 -0700 Subject: [Tutor] Beautiful Soup Message-ID: Hi I have recently finished reading "Starting out with Python" and I really want to do some web scraping. Please kindly advise where I can get more information about BeautifulSoup. It seems that Documentation is too hard for me. Furthermore, I have tried to scrap this site but it seems that there is an error (). Please advise what I should do in order to overcome this. from bs4 import BeautifulSoup import urllib.request HKFile = urllib.request.urlopen("https://bochk.etnet.com.hk/content/bochkweb/tc/quote_transaction_daily_history.php?code=2388") HKHtml = HKFile.read() HKFile.close() print(HKFile) Thank you Hank From joel.goldstick at gmail.com Tue Sep 29 18:05:07 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 29 Sep 2015 12:05:07 -0400 Subject: [Tutor] Beautiful Soup In-Reply-To: References: Message-ID: On Tue, Sep 29, 2015 at 11:47 AM, Crusier wrote: > Hi > > I have recently finished reading "Starting out with Python" and I > really want to do some web scraping. Please kindly advise where I can > get more information about BeautifulSoup. It seems that Documentation > is too hard for me. > > Furthermore, I have tried to scrap this site but it seems that there > is an error (). Please > advise what I should do in order to overcome this. > > > from bs4 import BeautifulSoup > import urllib.request > > HKFile = urllib.request.urlopen(" > https://bochk.etnet.com.hk/content/bochkweb/tc/quote_transaction_daily_history.php?code=2388 > ") > HKHtml = HKFile.read() > HKFile.close() > > print(HKFile) > > Thank you > Hank > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > many people find this package to be easier to use than the built in python support for reading urls: http://docs.python-requests.org/en/latest/ -- Joel Goldstick http://joelgoldstick.com From __peter__ at web.de Tue Sep 29 18:51:20 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 29 Sep 2015 18:51:20 +0200 Subject: [Tutor] Beautiful Soup References: Message-ID: Crusier wrote: > I have recently finished reading "Starting out with Python" and I > really want to do some web scraping. Please kindly advise where I can > get more information about BeautifulSoup. It seems that Documentation > is too hard for me. If you tell us what you don't understand and what you want to achieve we may be able to help you. > from bs4 import BeautifulSoup > import urllib.request > > HKFile = > urllib.request.urlopen("https://bochk.etnet.com.hk/content/bochkweb/tc/quote_transaction_daily_history.php?code=2388") > HKHtml = HKFile.read() > HKFile.close() > > print(HKFile) > Furthermore, I have tried to scrap this site but it seems that there > is an error (). That's not an error, that's what urlopen() returns. If an error occurs Python libraries are usually explicit an throw an exception. If the exception is not handled by your script by default Python prints a traceback and exits. For example: >>> import urllib.request >>> urllib.request.urlopen("http://httpbin.org/status/404") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python3.4/urllib/request.py", line 469, in open response = meth(req, response) File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python3.4/urllib/request.py", line 507, in error return self._call_chain(*args) File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain result = func(*args) File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: NOT FOUND That's what a well-behaved error looks like ;) > Please advise what I should do in order to overcome this. If you want to print the contents of the page just replace the line > print(HKFile) in your code with print(HKHtml.decode("utf-8")) From lac at openend.se Tue Sep 29 20:48:49 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 29 Sep 2015 20:48:49 +0200 Subject: [Tutor] Beautiful Soup In-Reply-To: References: Message-ID: <201509291848.t8TImnLe010475@fido.openend.se> >> Hi >> >> I have recently finished reading "Starting out with Python" and I >> really want to do some web scraping. Please kindly advise where I can >> get more information about BeautifulSoup. It seems that Documentation >> is too hard for me. >> >> Furthermore, I have tried to scrap this site but it seems that there >> is an error (). Please >> advise what I should do in order to overcome this. >> >> >> from bs4 import BeautifulSoup >> import urllib.request >> >> HKFile = urllib.request.urlopen(" >> https://bochk.etnet.com.hk/content/bochkweb/tc/quote_transaction_daily_history.php?code=2388 >> ") >> HKHtml = HKFile.read() >> HKFile.close() >> >> print(HKFile) is not an error. If you want to print your file change print(HKFile) to print(HKHtml.decode("some-encoding")) where some-encoding is what the website is encoded in, these days utf-8 is most likely. If you want a tutorial on webscraping, not Beautiful Soup try: http://doc.scrapy.org/en/latest/intro/tutorial.html which is about using scrapy, a set of useful webscraping tools. the scrapy wiki is also useful https://github.com/scrapy/scrapy/wiki and there are many video tutorials available if you like that sort of thing. Just google for python scrapy tutorial. Laura From nymcity at yahoo.com Tue Sep 29 18:40:53 2015 From: nymcity at yahoo.com (Nym City) Date: Tue, 29 Sep 2015 16:40:53 +0000 (UTC) Subject: [Tutor] Beautiful Soup In-Reply-To: References: Message-ID: <1627879074.2764933.1443544853824.JavaMail.yahoo@mail.yahoo.com> Hello,?I have personally found this tutorial to be helpful. Check it out: https://www.youtube.com/watch?v=3xQTJi2tqgk?Thank you. On Tuesday, September 29, 2015 12:05 PM, Joel Goldstick wrote: On Tue, Sep 29, 2015 at 11:47 AM, Crusier wrote: > Hi > > I have recently finished reading "Starting out with Python" and I > really want to do some web scraping. Please kindly advise where I can > get more information about BeautifulSoup. It seems that Documentation > is too hard for me. > > Furthermore, I have tried to scrap this site but it seems that there > is an error (). Please > advise what I should do in order to overcome this. > > > from bs4 import BeautifulSoup > import urllib.request > > HKFile = urllib.request.urlopen(" > https://bochk.etnet.com.hk/content/bochkweb/tc/quote_transaction_daily_history.php?code=2388 > ") > HKHtml = HKFile.read() > HKFile.close() > > print(HKFile) > > Thank you > Hank > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > many people find this package to be easier to use than the built in python support for reading urls: http://docs.python-requests.org/en/latest/ -- Joel Goldstick http://joelgoldstick.com _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From chirag.shahani at gmail.com Wed Sep 30 02:28:48 2015 From: chirag.shahani at gmail.com (Chirag Shahani) Date: Tue, 29 Sep 2015 17:28:48 -0700 Subject: [Tutor] Regarding installing a python package In-Reply-To: References: Message-ID: Thanks a lot for the response. I understand better now. On Tue, Sep 29, 2015 at 7:28 AM, Oscar Benjamin wrote: > On Tue, 29 Sep 2015 at 10:47 Chirag Shahani > wrote: > >> Hi, >> >> Could any one please help me understand the following: >> >> Suppose I install a python package using python setup.py install provided >> by the developer of a package. I need to track that changes in my file >> system.. meaning what new directories and files got created? Also, what is >> the concept of the egg file that gets created? What would be the location >> of this egg file and how would it be named ****.egg? Also, the >> ***.egg-info >> directory? >> > > It really depends. The setup.py script is really just an arbitrary program > that can do anything and can create files in any location it chooses. > Mostly setup.py files have a standard behaviour which is to install files > in the site-packages folder for your Python installation (unless you > provide command line options indicating an alternative location) and to > install executables so that they will be available on PATH. > > So usually if I have a project called project_name then it will create a > folder called project_name in the site-packages folder and then fill that > folder with Python files and maybe some other data files. Really though it > could do anything so if you need to know exactly what it's doing then > you'll need to read the setup.py and understand distutils and setuptools. > > For example in my system the site-packages folder is called > > /usr/lib/python2.7/dist-packages > > For some reason Ubuntu calls it dist-packages but in most Python > installations it is called site-packages. If I install ipython by running > its setup.py then I will have an egg-info file called: > > /usr/lib/python2.7/dist-packages/ipython-0.12.1.egg-info > > and also a folder called > > /usr/lib/python2.7/dist-packages/IPython > > which contains all of the Python files for ipython. It also installs an > executable file called ipython which is found at > > /usr/bin/ipython > > The exact details of what files are installed and where they go can vary > depending on what operating system and Python version you are using. > > -- > Oscar > From guirguismichel at yahoo.co.uk Wed Sep 30 08:12:20 2015 From: guirguismichel at yahoo.co.uk (Michel Guirguis) Date: Wed, 30 Sep 2015 09:12:20 +0300 Subject: [Tutor] How to calculate the cumulative normal distribution Message-ID: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> Good afternoon, How to calculate the cumulative normal distribution function CND in order to use this figure to calculate the call option based on the Black and Scholes model. >>> from math import* >>> def CND(X): m = abs(X) [a1,a2,a3,a4,a5]=(0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429) k = 1/(1+0.2316419*m) CND1 = 1.0-1.0/ sqrt(2*3.1415)* exp(-m*m*0.5) CND2 =(a1*k + a2*k*k+a3*k*k*k+a4*k*k*k*k+a5*k*k*k*k*k) CND = CND1*CND2 >>> d1=(math.log(S/K)+(r-q+(sig*sig)*0.5)*T)/(sig*math.sqrt(T)) >>> d1 0.10606601717798211 >>> d2 =d1-sig*math.sqrt(T) >>> d2 -0.03535533905932742 >>> call = 50*exp(-0.02*0.5)*CND(d1)-50*exp(-0.03*0.5)*CND(d2) >>> call Thanks, Michel From lac at openend.se Wed Sep 30 11:13:04 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 30 Sep 2015 11:13:04 +0200 Subject: [Tutor] How to calculate the cumulative normal distribution In-Reply-To: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> References: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> Message-ID: <201509300913.t8U9D4Ix008998@fido.openend.se> In a message of Wed, 30 Sep 2015 09:12:20 +0300, Michel Guirguis writes: >Good afternoon, > >How to calculate the cumulative normal distribution function CND in order to use this figure to calculate the call option based on the Black and Scholes model. The easy way is to install scipy. Then you do this: >>> from scipy.stats import norm >>> norm.cdf(1.96) array(0.97500210485177952) The easy way to install scipy is to get your python via anaconda https://www.continuum.io/downloads Laura From sebastian_cheung at yahoo.com Wed Sep 30 10:55:19 2015 From: sebastian_cheung at yahoo.com (Sebastian Cheung) Date: Wed, 30 Sep 2015 09:55:19 +0100 Subject: [Tutor] Python Optical Character Recognition Message-ID: How to read a jpg or png file into python and extract text thanks. I would imagine getting small symbols like ; or , to be difficult? Any good framework like this? Seb Sent from my iPhone From steve at pearwood.info Wed Sep 30 14:11:36 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 30 Sep 2015 22:11:36 +1000 Subject: [Tutor] How to calculate the cumulative normal distribution In-Reply-To: <201509300913.t8U9D4Ix008998@fido.openend.se> References: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> <201509300913.t8U9D4Ix008998@fido.openend.se> Message-ID: <20150930121136.GW23642@ando.pearwood.info> On Wed, Sep 30, 2015 at 11:13:04AM +0200, Laura Creighton wrote: > In a message of Wed, 30 Sep 2015 09:12:20 +0300, Michel Guirguis writes: > >Good afternoon, > > > >How to calculate the cumulative normal distribution function CND in > >order to use this figure to calculate the call option based on the > >Black and Scholes model. > > The easy way is to install scipy. That's no fun :-( -- Steve From steve at pearwood.info Wed Sep 30 14:15:22 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 30 Sep 2015 22:15:22 +1000 Subject: [Tutor] How to calculate the cumulative normal distribution In-Reply-To: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> References: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> Message-ID: <20150930121522.GX23642@ando.pearwood.info> On Wed, Sep 30, 2015 at 09:12:20AM +0300, Michel Guirguis wrote: > Good afternoon, > > How to calculate the cumulative normal distribution function CND in > order to use this figure to calculate the call option based on the > Black and Scholes model. > > >>> from math import* > >>> def CND(X): > m = abs(X) > [a1,a2,a3,a4,a5]=(0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429) > k = 1/(1+0.2316419*m) > CND1 = 1.0-1.0/ sqrt(2*3.1415)* exp(-m*m*0.5) > CND2 =(a1*k + a2*k*k+a3*k*k*k+a4*k*k*k*k+a5*k*k*k*k*k) > CND = CND1*CND2 I haven't checked that algorithm, but you need to return a result, not assign to the function name: return CND1*CND2 -- Steve From steve at pearwood.info Wed Sep 30 14:36:33 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 30 Sep 2015 22:36:33 +1000 Subject: [Tutor] How to calculate the cumulative normal distribution In-Reply-To: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> References: <571CF484-76E2-4289-840D-5892DE4096AD@yahoo.co.uk> Message-ID: <20150930123633.GY23642@ando.pearwood.info> On Wed, Sep 30, 2015 at 09:12:20AM +0300, Michel Guirguis wrote: > Good afternoon, > > How to calculate the cumulative normal distribution function CND in > order to use this figure to calculate the call option based on the > Black and Scholes model. > > >>> from math import* > >>> def CND(X): > m = abs(X) > [a1,a2,a3,a4,a5]=(0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429) > k = 1/(1+0.2316419*m) > CND1 = 1.0-1.0/ sqrt(2*3.1415)* exp(-m*m*0.5) > CND2 =(a1*k + a2*k*k+a3*k*k*k+a4*k*k*k*k+a5*k*k*k*k*k) > CND = CND1*CND2 That cannot possibly be the *cumulative* normal distribution, as that should approach 1 as x gets really large: py> CND(2000000.0) 6.893832052455898e-07 Are you sure this is the right description or algorithm? -- Steve From stefan_ml at behnel.de Wed Sep 30 16:02:20 2015 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 30 Sep 2015 16:02:20 +0200 Subject: [Tutor] Python Optical Character Recognition In-Reply-To: References: Message-ID: Sebastian Cheung via Tutor schrieb am 30.09.2015 um 10:55: > How to read a jpg or png file into python and extract text thanks. I > would imagine getting small symbols like ; or , to be difficult? That depends entirely on the OCR engine. And you won't normally get in touch with that very deeply. > Any good framework like this? Searching for "python ocr" gives me a couple of hits, including this one: https://pypi.python.org/pypi/pytesseract PyPI has some more matches that might be relevant: https://pypi.python.org/pypi?%3Aaction=search&term=ocr&submit=search Stefan