From ben+python at benfinney.id.au Wed Apr 1 00:08:37 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 01 Apr 2015 15:08:37 +1100 Subject: New to Programming - XML Processing References: Message-ID: <85384k32yi.fsf@benfinney.id.au> catperson writes: > I am new to programming, though not new to computers. I'm looking to > teach myself Python 3 and am working my way through a tutorial. Congratulations! Python 3 is a fine language to be your first. Which tutorial are you following? It may be relevant. > At the point I'm at in the tutorial I am tasked with parsing out an > XML file created with a Garmin Forerunner and am just having a > terrible time getting my head around the concepts. Have you worked with manipulating XML documents in the past? If I describe XML as a machine-readable hierarchical data serialisation format, does that sound like noise or are you comfortable that you understand that description? > What I'm looking for is some suggested reading that might give me some > of the theory of operation behind ElementTree and then how to parse > out specific elements. You'll need to understand the principles behind XML, then. Warning: it is not simple! XML is machine-readable, but rarely pleasant for *humans* to read or understand. > I'm hoping with enough reading I can experiment and work my way > through the problem and end up with a hopefully clear understanding of > the ElementTree module and Dictionairies. XML is a standard ? a large number of standards, really ? from the W3 Consortium. They have a good overview of the ?core? of XML . There are countless XML formats though; XML is a markup language to use as a basis for constructing data serialisation formats. You need to know what the specific format (called a ?schema?) is before you can expect to make sense of it, and so in order to understand what's in the document you need a description of the specific schema from whoever wrote the document. General XML tutorials do exist; here is one comprehensive one . You'll likely want to learn more about the specifics of whatever XML format is being used, though, before going much beyond the basic principles. -- \ ?Politics is not the art of the possible. It consists in | `\ choosing between the disastrous and the unpalatable.? ?John | _o__) Kenneth Galbraith, 1962-03-02 | Ben Finney From rustompmody at gmail.com Wed Apr 1 00:17:38 2015 From: rustompmody at gmail.com (Rustom Mody) Date: Tue, 31 Mar 2015 21:17:38 -0700 (PDT) Subject: New to Programming - XML Processing In-Reply-To: References: Message-ID: <0b0fd795-8f3e-443e-acc2-aed9b5d200ba@googlegroups.com> On Wednesday, April 1, 2015 at 8:57:15 AM UTC+5:30, catperson wrote: > I am new to programming, though not new to computers. I'm looking to > teach myself Python 3 and am working my way through a tutorial. At > the point I'm at in the tutorial I am tasked with parsing out an XML > file created with a Garmin Forerunner and am just having a terrible > time getting my head around the concepts. What I'm looking for is > some suggested reading that might give me some of the theory of > operation behind ElementTree and then how to parse out specific > elements. Most of what I have been able to find in examples that I > can understand use very simplistic XML files and this Garmin file is > many levels of sub-elements and some of those elements have attributes > assigned, like . > > I'm hoping with enough reading I can experiment and work my way > through the problem and end up with a hopefully clear understanding of > the ElementTree module and Dictionairies. > > Thanks for any suggestions in advance. Suggestions: 1. Learn to use the interpreter interactively; ie (at the least)? ie a. Start up python (without a program) b. Play around with trivial expressions c. Explore introspective features - help(), type() dir() 2. Do you know about triple-quoted strings? a. Start small (or trivial) sub-parts of your XML as triple-quoted examples in the interpreter and start throwing them at elementtree b. If they dont work trivialize further; if they work add complexity ----------- ? At the least because environments like Idle are more conducive to such playing From amfarrell at mit.edu Wed Apr 1 00:26:30 2015 From: amfarrell at mit.edu (Andrew Farrell) Date: Tue, 31 Mar 2015 23:26:30 -0500 Subject: New to Programming - XML Processing In-Reply-To: <85384k32yi.fsf@benfinney.id.au> References: <85384k32yi.fsf@benfinney.id.au> Message-ID: > I am new to programming, though not new to computers. One quick tip: when starting a new project, it is sometimes is useful to see if others have done the same thing and use their approach to guide yours. In your case, googling "Garmin Forerunner xml python" results in this blog post which is recent enough that it should be helpful. http://www.nezirteke.com/2014/02/using-python-to-plot-garmin-forerunner.html Note that coming up with good phrases for google to help you with a programming task is a skill that you get better at with time. Welcome to Python! ^-^ On Tue, Mar 31, 2015 at 11:08 PM, Ben Finney wrote: > catperson writes: > > > I am new to programming, though not new to computers. I'm looking to > > teach myself Python 3 and am working my way through a tutorial. > > Congratulations! Python 3 is a fine language to be your first. > > Which tutorial are you following? It may be relevant. > > > At the point I'm at in the tutorial I am tasked with parsing out an > > XML file created with a Garmin Forerunner and am just having a > > terrible time getting my head around the concepts. > > Have you worked with manipulating XML documents in the past? > > If I describe XML as a machine-readable hierarchical data serialisation > format, does that sound like noise or are you comfortable that you > understand that description? > > > What I'm looking for is some suggested reading that might give me some > > of the theory of operation behind ElementTree and then how to parse > > out specific elements. > > You'll need to understand the principles behind XML, then. Warning: it > is not simple! XML is machine-readable, but rarely pleasant for *humans* > to read or understand. > > > I'm hoping with enough reading I can experiment and work my way > > through the problem and end up with a hopefully clear understanding of > > the ElementTree module and Dictionairies. > > XML is a standard ? a large number of standards, really ? from the W3 > Consortium. They have a good overview of the ?core? of XML > . > > There are countless XML formats though; XML is a markup language to use > as a basis for constructing data serialisation formats. You need to know > what the specific format (called a ?schema?) is before you can expect to > make sense of it, and so in order to understand what's in the document > you need a description of the specific schema from whoever wrote the > document. > > General XML tutorials do exist; here is one comprehensive one > . You'll likely want to > learn more about the specifics of whatever XML format is being used, > though, before going much beyond the basic principles. > > -- > \ ?Politics is not the art of the possible. It consists in | > `\ choosing between the disastrous and the unpalatable.? ?John | > _o__) Kenneth Galbraith, 1962-03-02 | > Ben Finney > > -- > https://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From amfarrell at mit.edu Wed Apr 1 00:27:57 2015 From: amfarrell at mit.edu (Andrew Farrell) Date: Tue, 31 Mar 2015 23:27:57 -0500 Subject: New to Programming - XML Processing In-Reply-To: <0b0fd795-8f3e-443e-acc2-aed9b5d200ba@googlegroups.com> References: <0b0fd795-8f3e-443e-acc2-aed9b5d200ba@googlegroups.com> Message-ID: You should follow Rustom's advice before just diving into the blog post I linked to. Otherwise you risk blindly following things and losing your bearings when you run into bugs. On Tue, Mar 31, 2015 at 11:17 PM, Rustom Mody wrote: > On Wednesday, April 1, 2015 at 8:57:15 AM UTC+5:30, catperson wrote: > > I am new to programming, though not new to computers. I'm looking to > > teach myself Python 3 and am working my way through a tutorial. At > > the point I'm at in the tutorial I am tasked with parsing out an XML > > file created with a Garmin Forerunner and am just having a terrible > > time getting my head around the concepts. What I'm looking for is > > some suggested reading that might give me some of the theory of > > operation behind ElementTree and then how to parse out specific > > elements. Most of what I have been able to find in examples that I > > can understand use very simplistic XML files and this Garmin file is > > many levels of sub-elements and some of those elements have attributes > > assigned, like . > > > > I'm hoping with enough reading I can experiment and work my way > > through the problem and end up with a hopefully clear understanding of > > the ElementTree module and Dictionairies. > > > > Thanks for any suggestions in advance. > > Suggestions: > 1. Learn to use the interpreter interactively; ie (at the least)? ie > > a. Start up python (without a program) > b. Play around with trivial expressions > c. Explore introspective features - help(), type() dir() > > 2. Do you know about triple-quoted strings? > a. Start small (or trivial) sub-parts of your XML as triple-quoted > examples in the > interpreter and start throwing them at elementtree > b. If they dont work trivialize further; if they work add complexity > ----------- > ? At the least because environments like Idle are more conducive to such > playing > -- > https://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rustompmody at gmail.com Wed Apr 1 00:49:21 2015 From: rustompmody at gmail.com (Rustom Mody) Date: Tue, 31 Mar 2015 21:49:21 -0700 (PDT) Subject: New to Programming - XML Processing In-Reply-To: References: Message-ID: <08549bc7-959f-43c3-b03e-aee5d8f744f8@googlegroups.com> On Wednesday, April 1, 2015 at 8:57:15 AM UTC+5:30, catperson wrote: > I'm hoping with enough reading I can experiment and work my way > through the problem and end up with a hopefully clear understanding of > the ElementTree module and Dictionairies. Also: If you are not familiar with dictionaries, better grok them (and lists and tuples and some else) from a good tut like https://docs.python.org/3.4/tutorial/ BEFORE taking on ElementTree From breamoreboy at yahoo.co.uk Wed Apr 1 04:00:09 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 01 Apr 2015 09:00:09 +0100 Subject: New to Programming - XML Processing In-Reply-To: References: <0b0fd795-8f3e-443e-acc2-aed9b5d200ba@googlegroups.com> Message-ID: On 01/04/2015 05:27, Andrew Farrell wrote: > You should follow Rustom's advice before just diving into the blog post > I linked to. Otherwise you risk blindly following things and losing your > bearings when you run into bugs. > Sound advice, but would you please be kind enough to intersperse your answers or bottom post as top posting is heavily frowned upon here. TIA. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From mohan463 at gmail.com Wed Apr 1 06:15:11 2015 From: mohan463 at gmail.com (mohan463 at gmail.com) Date: Wed, 1 Apr 2015 03:15:11 -0700 (PDT) Subject: Memory Issue using marking property in spotfire Message-ID: <5c4309b6-0546-4ab2-822b-fbef3b43a26f@googlegroups.com> Hi All, we have developed Iron Python script in spotfire to execute some calculation based on the marked rows. The Script holding the memory and not realsing . if we marked 1000+ rows its taking MBs of memory and keep on increasing. Please help us, is there any way in iron python to release memory. Thanks Mohan From breamoreboy at yahoo.co.uk Wed Apr 1 07:12:42 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 01 Apr 2015 12:12:42 +0100 Subject: Memory Issue using marking property in spotfire In-Reply-To: <5c4309b6-0546-4ab2-822b-fbef3b43a26f@googlegroups.com> References: <5c4309b6-0546-4ab2-822b-fbef3b43a26f@googlegroups.com> Message-ID: On 01/04/2015 11:15, mohan463 at gmail.com wrote: > Hi All, > > we have developed Iron Python script in spotfire to execute some calculation based on the marked rows. The Script holding the memory and not realsing . if we marked 1000+ rows its taking MBs of memory and keep on increasing. > > Please help us, is there any way in iron python to release memory. > > Thanks > Mohan > I'm sorry that I can't help directly, but if you don't get your answer here there is an ironpython list at either gmane.comp.python.ironpython.user or https://mail.python.org/mailman/listinfo/ironpython-users -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From lynto28 at gmail.com Wed Apr 1 07:51:29 2015 From: lynto28 at gmail.com (Didymus) Date: Wed, 1 Apr 2015 04:51:29 -0700 (PDT) Subject: Logging Custom Levels? In-Reply-To: References: Message-ID: <2ba601a0-ba32-4c29-9125-2409aeca19f9@googlegroups.com> On Tuesday, March 31, 2015 at 1:37:29 PM UTC-4, Jean-Michel Pichavant wrote: > > A solution is pretty simple, do not use an intermediate log function pdebug. > > import logging > PDEBUG_NUM=20 > logging.addLevelName(PDEBUG_NUM, "PDEBUG") > > logger = logging.getLogger('foo') > logging.basicConfig(level=logging.DEBUG, format='%(message)s %(lineno)d') > > logger.log(PDEBUG_NUM, 'This will work :') > > > If you *really* want to go for the hackish way, forget about the inspect module, the following pdebug function should do the trick: > > def pdebug(self, message, *args, **kws): > if self.isEnabledFor(PDEBUG_NUM): > self._log(PDEBUG_NUM, message, args, **kws) > > Cheers, > > JM > Very good, thank you! Tom From gandalf at shopzeus.com Wed Apr 1 08:01:31 2015 From: gandalf at shopzeus.com (=?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=) Date: Wed, 01 Apr 2015 14:01:31 +0200 Subject: pycurl.error: (55, 'select/poll returned error') In-Reply-To: <871tk756hk.fsf@handshake.de> References: <5514FFEE.4030204@shopzeus.com> <87fv8pwrk0.fsf@handshake.de> <5517A5DC.6060001@shopzeus.com> <871tk756hk.fsf@handshake.de> Message-ID: <551BDE1B.3030602@shopzeus.com> >>>> When calling curl.perform() on a curl instance I get this: >>>> >>>> pycurl.error: (55, 'select/poll returned error') >>>> .... >> The same server is happily accepting files over 4GB from other >> clients. Those other clients are exactly the same client programs, but >> they are running on Windows. Only the one on this FreeBSD box is >> throwing the error. > Maybe, you can find out what was the real error reported by > "select/poll" (the "55" above might be the "errno" and in > you OS header files, you may find what it means). > > *nix* derivatives have a peculiarity (missing under Windows). > There waiting system calls may prematurely return with > the error "interrupted system call". > Likely, this should give waiting applications a chance to reassess > the state after a signal interrupt and decide for themselves > whether the waiting should be continued. In most contexts, > "interrupted system call" should not be considered an error and the > interrupted call should be restartet. Maybe, "pycurl" misses to do that. > I have figured out that the limit is exactly 2048KB. I'll probably post a bug report to pycurl developers. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. From turiddociccuddu at yahoo.com Wed Apr 1 08:49:57 2015 From: turiddociccuddu at yahoo.com (ME MO CHE OTTO) Date: Wed, 1 Apr 2015 05:49:57 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <7d471aa5-b6ed-4800-9855-37319ba126ee@googlegroups.com> References: <7d471aa5-b6ed-4800-9855-37319ba126ee@googlegroups.com> Message-ID: -- redacted -- From ahlusar.ahluwalia at gmail.com Wed Apr 1 09:43:45 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Wed, 1 Apr 2015 06:43:45 -0700 (PDT) Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> Message-ID: On Tuesday, March 31, 2015 at 9:19:37 AM UTC-4, Dave Angel wrote: > On 03/31/2015 07:00 AM, Saran A wrote: > > > @DaveA: This is a homework assignment. .... Is it possible that you > could provide me with some snippets or guidance on where to place your > suggestions (for your TO DOs 2,3,4,5)? > > > > > > On Monday, March 30, 2015 at 2:36:02 PM UTC-4, Dave Angel wrote: > > >> > >> It's missing a number of your requirements. But it's a start. > >> > >> If it were my file, I'd have a TODO comment at the bottom stating known > >> changes that are needed. In it, I'd mention: > >> > >> 1) your present code is assuming all filenames come directly from the > >> commandline. No searching of a directory. > >> > >> 2) your present code does not move any files to success or failure > >> directories > >> > > In function validate_files() > Just after the line > print('success with %s on %d reco... > you could move the file, using shutil. Likewise after the failure print. > > >> 3) your present code doesn't calculate or write to a text file any > >> statistics. > > You successfully print to sys.stderr. So you could print to some other > file in the exact same way. > > >> > >> 4) your present code runs once through the names, and terminates. It > >> doesn't "monitor" anything. > > Make a new function, perhaps called main(), with a loop that calls > validate_files(), with a sleep after each pass. Of course, unless you > fix TODO#1, that'll keep looking for the same files. No harm in that if > that's the spec, since you moved the earlier versions of the files. > > But if you want to "monitor" the directory, let the directory name be > the argument to main, and let main do a dirlist each time through the > loop, and pass the corresponding list to validate_files. > > >> > >> 5) your present code doesn't check for zero-length files > >> > > In validate_and_process_data(), instead of checking filesize against > ftell, check it against zero. > > >> I'd also wonder why you bother checking whether the > >> os.path.getsize(file) function returns the same value as the os.SEEK_END > >> and ftell() code does. Is it that you don't trust the library? Or that > >> you have to run on Windows, where the line-ending logic can change the > >> apparent file size? > >> > >> I notice you're not specifying a file mode on the open. So in Python 3, > >> your sizes are going to be specified in unicode characters after > >> decoding. Is that what the spec says? It's probably safer to > >> explicitly specify the mode (and the file encoding if you're in text). > >> > >> I see you call strip() before comparing the length. Could there ever be > >> leading or trailing whitespace that's significant? Is that the actual > >> specification of line size? > >> > >> -- > >> DaveA > > > > > > > I ask this because I have been searching fruitlessly through for some time and there are so many permutations that I am bamboozled by which is considered best practice. > > > > Moreover, as to the other comments, those are too specific. The scope of the assignment is very limited, but I am learning what I need to look out or ask questions regarding specs - in the future. > > > > > -- > DaveA @DaveA My most recent commit (https://github.com/ahlusar1989/WGProjects/blob/master/P1version2.0withassumptions_mods.py) has more annotations and comments for each file. I have attempted to address the functional requirements that you brought up: 1) Before, my present code was assuming all filenames come directly from the commandline. No searching of a directory. I think that I have addressed this. 2) My present code does not move any files to success or failure directories (requirements for this assignment1). I am still wondering if and how I should use shututil() like you advised me to. I keep receiving a syntax error when declaring this below the print statement. 3) You correctly reminded me that my present code doesn't calculate or write to a text file any statistics or errors for the cause of the error. (Should I use the copy or copy2 method in order provide metadata? If so, should I wrap it into a try and except logic?) 4) Before, my present code runs once through the names, and terminates. It doesn't "monitor" anything. I think I have addressed this with the main function - correct? 5) Before, my present code doesn't check for zero-length files - I have added a comment there in case that is needed) I realize appreciate your invaluable feedback. I have grown a lot with this assignment! Sincerely, Saran From burak.arslan at arskom.com.tr Wed Apr 1 09:44:02 2015 From: burak.arslan at arskom.com.tr (Burak Arslan) Date: Wed, 01 Apr 2015 16:44:02 +0300 Subject: New to Programming - XML Processing In-Reply-To: References: Message-ID: <551BF622.6060502@arskom.com.tr> On 04/01/15 06:27, catperson wrote: > I am new to programming, though not new to computers. I'm looking to > teach myself Python 3 and am working my way through a tutorial. At > the point I'm at in the tutorial I am tasked with parsing out an XML > file created with a Garmin Forerunner and am just having a terrible > time getting my head around the concepts. What I'm looking for is > some suggested reading that might give me some of the theory of > operation behind ElementTree and then how to parse out specific > elements. Most of what I have been able to find in examples that I > can understand use very simplistic XML files and this Garmin file is > many levels of sub-elements and some of those elements have attributes > assigned, like . As everybody loves objects, there are many libraries that parse xml documents to regular python objects (with proper hierarchy). These are supposed to save you from dealing with ElementTree api directly. Have a look here: http://stackoverflow.com/questions/19545067/python-joining-and-writing-xml-etrees-trees-stored-in-a-list Hth, Burak From ahlusar.ahluwalia at gmail.com Wed Apr 1 10:17:30 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Wed, 1 Apr 2015 07:17:30 -0700 (PDT) Subject: Addendum to Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: Message-ID: On Sunday, March 29, 2015 at 8:33:43 AM UTC-4, Peter Otten wrote: > Saran Ahluwalia wrote: > > > On Sunday, March 29, 2015 at 7:33:04 AM UTC-4, Saran Ahluwalia wrote: > >> Below are the function's requirements. I am torn between using the OS > >> module or some other quick and dirty module. In addition, my ideal > >> assumption that this could be cross-platform. "Records" refers to > >> contents in a file. What are some suggestions from the Pythonistas? > >> > >> * Monitors a folder for files that are dropped throughout the day > >> > >> * When a file is dropped in the folder the program should scan the file > >> > >> o IF all the records in the file have the same length > >> > >> o THEN the file should be moved to a "success" folder and a text file > >> written indicating the total number of records processed > >> > >> o IF the file is empty OR the records are not all of the same length > >> > >> o THEN the file should be moved to a "failure" folder and a text file > >> written indicating the cause for failure (for example: Empty file or line > >> 100 was not the same length as the rest). > > > > Below are some functions that I have been playing around with. I am not > > sure how to create a functional program from each of these constituent > > parts. I could use decorators or simply pass a function within another > > function. > > Throwing arbitrary code at a task in the hope that something sticks is not a > good approach. You already have given a clear description of the problem, so > start with that and try to "pythonize" it. Example: > > def main(): > while True: > files_to_check = get_files_in_monitored_folder() > for file in files_to_check: > if is_good(file): > move_to_success_folder(file) > else: > move_to_failure_folder(file) > wait_a_minute() > > if __name__ == "__main__": > main() > > Then write bogus implementations for the building blocks: > > def get_files_in_monitored_folder(): > return ["/foo/bar/ham", "/foo/bar/spam"] > > def is_good(file): > return file.endswith("/ham") > > def move_to_failure_folder(file): > print("failure", file) > > def move_to_success_folder(file): > print("success", file) > > def wait_a_minute(): > raise SystemExit("bye") # we don't want to enter the loop while > developing > > Now successively replace the dummy function with functions that do the right > thing. Test them individually (preferrably using unit tests) so that when > you have completed them all and your program does not work like it should > you can be sure that there is a flaw in the main function. > > > [code] > > import time > > import fnmatch > > import os > > import shutil > > > > > > #If you want to write to a file, and if it doesn't exist, do this: > > Hm, are these your personal notes or is it an actual script? > > > if not os.path.exists(filepath): > > f = open(filepath, 'w') > > #If you want to read a file, and if it exists, do the following: > > > > try: > > f = open(filepath) > > except IOError: > > print 'I will be moving this to the ' > > > > > > #Changing a directory to "/home/newdir" > > os.chdir("/home/newdir") > > Never using os.chdir() is a good habit to get into. > > > def move(src, dest): > > shutil.move(src, dest) > > > > def fileinfo(file): > > filename = os.path.basename(file) > > rootdir = os.path.dirname(file) > > lastmod = time.ctime(os.path.getmtime(file)) > > creation = time.ctime(os.path.getctime(file)) > > filesize = os.path.getsize(file) > > > > print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation, > > filesize) > > > > searchdir = r'D:\Your\Directory\Root' > > matches = [] > > > > def search > > Everytime you post code that doesn't even compile you lose some goodwill. > In a few lines of code meant to demonstrate a problem a typo may be > acceptable, but for something that you probably composed in an editor you > should take the time to run it and fix at least the syntax errors. > > > for root, dirnames, filenames in os.walk(searchdir): > > ## for filename in fnmatch.filter(filenames, '*.c'): > > for filename in filenames: > > ## matches.append(os.path.join(root, filename)) > > ##print matches > > fileinfo(os.path.join(root, filename)) > > > > > > def get_files(src_dir): > > # traverse root directory, and list directories as dirs and files as files > > for root, dirs, files in os.walk(src_dir): > > path = root.split('/') > > for file in files: > > process(os.path.join(root, file)) > > os.remove(os.path.join(root, file)) > > > > def del_dirs(src_dir): > > for dirpath, _, _ in os.walk(src_dir, topdown=False): # Listing the > > files > > if dirpath == src_dir: > > break > > try: > > os.rmdir(dirpath) > > except OSError as ex: > > print(ex) > > > > > > def main(): > > get_files(src_dir) > > del_dirs(src_dir) > > > > > > if __name__ == "__main__": > > main() > > > > > > [/code] @PeterOtten Thank you for the feedback - since your last correspondence I have come up with this (my most recent commit: https://github.com/ahlusar1989/WGProjects/blob/master/P1version2.0withassumptions_mods.py From teqisid at gmail.com Wed Apr 1 11:46:36 2015 From: teqisid at gmail.com (teqisid at gmail.com) Date: Wed, 1 Apr 2015 08:46:36 -0700 (PDT) Subject: ImportError: No module named 'requests.packages.urllib3' Message-ID: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> When I am trying to import requests library in python shell, I get this error: ImportError: No module named 'requests.packages.urllib3' I have tried searching online and followed a few probable solutions but yet no luck. Does anyone know how I can solve this error? I am using python 2.7 From ian.g.kelly at gmail.com Wed Apr 1 12:04:09 2015 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 1 Apr 2015 10:04:09 -0600 Subject: ImportError: No module named 'requests.packages.urllib3' In-Reply-To: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> References: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> Message-ID: On Wed, Apr 1, 2015 at 9:46 AM, wrote: > When I am trying to import requests library in python shell, I get this error: ImportError: No module named 'requests.packages.urllib3' > I have tried searching online and followed a few probable solutions but yet no luck. > Does anyone know how I can solve this error? > I am using python 2.7 It sounds like there's something missing from your requests installation. How did you attempt to install it, and were there any errors? From rosuav at gmail.com Wed Apr 1 12:18:25 2015 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 Apr 2015 03:18:25 +1100 Subject: ImportError: No module named 'requests.packages.urllib3' In-Reply-To: References: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> Message-ID: On Thu, Apr 2, 2015 at 3:04 AM, Ian Kelly wrote: > On Wed, Apr 1, 2015 at 9:46 AM, wrote: >> When I am trying to import requests library in python shell, I get this error: ImportError: No module named 'requests.packages.urllib3' >> I have tried searching online and followed a few probable solutions but yet no luck. >> Does anyone know how I can solve this error? >> I am using python 2.7 > > It sounds like there's something missing from your requests > installation. How did you attempt to install it, and were there any > errors? It's also possible that you've shadowed a key module. Copy and paste the entire traceback, including all the file names, and there might be a clue somewhere. Or put your code in a file called "1.py", in a directory on its own, and run that; that name can't accidentally shadow a normal module name. (I know this, because I built a WSGI web site that has most of its code in a file of that name. Importing that took a bit of hoop-jumping; you can't simply "import 1", because that doesn't make any sense.) ChrisA From teqisid at gmail.com Wed Apr 1 12:42:15 2015 From: teqisid at gmail.com (teqisid at gmail.com) Date: Wed, 1 Apr 2015 09:42:15 -0700 (PDT) Subject: ImportError: No module named 'requests.packages.urllib3' In-Reply-To: References: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> Message-ID: On Wednesday, April 1, 2015 at 12:05:12 PM UTC-4, Ian wrote: > On Wed, Apr 1, 2015 at 9:46 AM, wrote: > > When I am trying to import requests library in python shell, I get this error: ImportError: No module named 'requests.packages.urllib3' > > I have tried searching online and followed a few probable solutions but yet no luck. > > Does anyone know how I can solve this error? > > I am using python 2.7 > > It sounds like there's something missing from your requests > installation. How did you attempt to install it, and were there any > errors? I installed it using pip. I had to use sudo pip install requests to get the module in the site-packages directory. I did not see any error. From teqisid at gmail.com Wed Apr 1 12:43:55 2015 From: teqisid at gmail.com (teqisid at gmail.com) Date: Wed, 1 Apr 2015 09:43:55 -0700 (PDT) Subject: ImportError: No module named 'requests.packages.urllib3' In-Reply-To: References: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> Message-ID: <9cacb818-2b97-4fe1-9e64-8c71b9d6a9a9@googlegroups.com> On Wednesday, April 1, 2015 at 12:42:39 PM UTC-4, teq... at gmail.com wrote: > On Wednesday, April 1, 2015 at 12:05:12 PM UTC-4, Ian wrote: > > On Wed, Apr 1, 2015 at 9:46 AM, wrote: > > > When I am trying to import requests library in python shell, I get this error: ImportError: No module named 'requests.packages.urllib3' > > > I have tried searching online and followed a few probable solutions but yet no luck. > > > Does anyone know how I can solve this error? > > > I am using python 2.7 > > > > It sounds like there's something missing from your requests > > installation. How did you attempt to install it, and were there any > > errors? > > I installed it using pip. I had to use sudo pip install requests to get the module in the site-packages directory. I did not see any error. Below is the full stack trace for the error: Traceback (most recent call last): File "/usr/local/bin/behave", line 8, in load_entry_point('behave==1.2.4', 'console_scripts', 'behave')() File "build/bdist.macosx-10.9-intel/egg/behave/__main__.py", line 111, in main File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 659, in run File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 665, in run_with_paths File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 649, in load_step_definitions File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 306, in exec_file File "/Users/abubakar.siddiq/projects/oms_automation_test/features/steps/callback_ftp.py", line 2, in import requests File "/Library/Python/2.7/site-packages/requests/__init__.py", line 58, in from . import utils File "/Library/Python/2.7/site-packages/requests/utils.py", line 26, in from .compat import parse_http_list as _parse_list_header File "/Library/Python/2.7/site-packages/requests/compat.py", line 42, in from .packages.urllib3.packages.ordered_dict import OrderedDict File "/Library/Python/2.7/site-packages/requests/packages/__init__.py", line 95, in load_module raise ImportError("No module named '%s'" % (name,)) ImportError: No module named 'requests.packages.urllib3' From teqisid at gmail.com Wed Apr 1 13:52:47 2015 From: teqisid at gmail.com (teqisid at gmail.com) Date: Wed, 1 Apr 2015 10:52:47 -0700 (PDT) Subject: ImportError: No module named 'requests.packages.urllib3' In-Reply-To: <9cacb818-2b97-4fe1-9e64-8c71b9d6a9a9@googlegroups.com> References: <0d6728d9-07c1-41a3-a872-20bc9cc27aed@googlegroups.com> <9cacb818-2b97-4fe1-9e64-8c71b9d6a9a9@googlegroups.com> Message-ID: On Wednesday, April 1, 2015 at 12:44:12 PM UTC-4, teq... at gmail.com wrote: > On Wednesday, April 1, 2015 at 12:42:39 PM UTC-4, teq... at gmail.com wrote: > > On Wednesday, April 1, 2015 at 12:05:12 PM UTC-4, Ian wrote: > > > On Wed, Apr 1, 2015 at 9:46 AM, wrote: > > > > When I am trying to import requests library in python shell, I get this error: ImportError: No module named 'requests.packages.urllib3' > > > > I have tried searching online and followed a few probable solutions but yet no luck. > > > > Does anyone know how I can solve this error? > > > > I am using python 2.7 > > > > > > It sounds like there's something missing from your requests > > > installation. How did you attempt to install it, and were there any > > > errors? > > > > I installed it using pip. I had to use sudo pip install requests to get the module in the site-packages directory. I did not see any error. > > Below is the full stack trace for the error: > > Traceback (most recent call last): > File "/usr/local/bin/behave", line 8, in > load_entry_point('behave==1.2.4', 'console_scripts', 'behave')() > File "build/bdist.macosx-10.9-intel/egg/behave/__main__.py", line 111, in main > File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 659, in run > File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 665, in run_with_paths > File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 649, in load_step_definitions > File "build/bdist.macosx-10.9-intel/egg/behave/runner.py", line 306, in exec_file > File "/Users/abubakar.siddiq/projects/oms_automation_test/features/steps/callback_ftp.py", line 2, in > import requests > File "/Library/Python/2.7/site-packages/requests/__init__.py", line 58, in > from . import utils > File "/Library/Python/2.7/site-packages/requests/utils.py", line 26, in > from .compat import parse_http_list as _parse_list_header > File "/Library/Python/2.7/site-packages/requests/compat.py", line 42, in > from .packages.urllib3.packages.ordered_dict import OrderedDict > File "/Library/Python/2.7/site-packages/requests/packages/__init__.py", line 95, in load_module > raise ImportError("No module named '%s'" % (name,)) > ImportError: No module named 'requests.packages.urllib3' Ok, so the import requests works now. But my main goal is to make it work with my behave scenarios. It was working before until I did something which I am not sure what it is! So, when I try to run any of the behave scenarios, I get the listed error. From inkprs at gmail.com Wed Apr 1 17:26:43 2015 From: inkprs at gmail.com (Rio) Date: Wed, 1 Apr 2015 14:26:43 -0700 (PDT) Subject: instance attribute "a" defined outside __init__ Message-ID: Hi, When running below code, I get error saying: instance attribute "a" defined outside __init__ class Foo: var = 9 def add(self, a, b): self.a = a self.b = b print a+b def __init__(self): print 10 bar = Foo() # create object bar.add(4, 7) # call method by object.method print bar.var # access class variable why the output prints: 10 11 9 why not serially? 9 11 10 From gordon at panix.com Wed Apr 1 17:37:25 2015 From: gordon at panix.com (John Gordon) Date: Wed, 1 Apr 2015 21:37:25 +0000 (UTC) Subject: instance attribute "a" defined outside __init__ References: Message-ID: In Rio writes: > Hi, When running below code, I get error saying: > instance attribute "a" defined outside __init__ That's a warning, not an error. And it's a warning from pylint, not from Python itself. It's trying to suggest better style, that's all. It's unusual to define instance variables in functions other __init__. > class Foo: > var = 9 > def add(self, a, b): > self.a = a > self.b = b > print a+b > def __init__(self): > print 10 > bar = Foo() # create object > bar.add(4, 7) # call method by object.method > print bar.var # access class variable > why the output prints: > 10 > 11 > 9 10 is printed by Foo.__init__(), when an instance of Foo is created. 11 is printed by calling bar.add(4, 7). 9 is printed by your statement 'print bar.var'. Your program has those statements in that order, so they are printed in that order. Why did you expect a different order? -- John Gordon Imagine what it must be like for a real medical doctor to gordon at panix.com watch 'House', or a real serial killer to watch 'Dexter'. From me at anonymous.invalid Wed Apr 1 18:33:48 2015 From: me at anonymous.invalid (catperson) Date: Wed, 01 Apr 2015 15:33:48 -0700 Subject: New to Programming - XML Processing References: <0b0fd795-8f3e-443e-acc2-aed9b5d200ba@googlegroups.com> Message-ID: On Tue, 31 Mar 2015 21:17:38 -0700 (PDT), Rustom Mody wrote: >On Wednesday, April 1, 2015 at 8:57:15 AM UTC+5:30, catperson wrote: >> I am new to programming, though not new to computers. I'm looking to >> teach myself Python 3 and am working my way through a tutorial. At >> the point I'm at in the tutorial I am tasked with parsing out an XML >> file created with a Garmin Forerunner and am just having a terrible >> time getting my head around the concepts. What I'm looking for is >> some suggested reading that might give me some of the theory of >> operation behind ElementTree and then how to parse out specific >> elements. Most of what I have been able to find in examples that I >> can understand use very simplistic XML files and this Garmin file is >> many levels of sub-elements and some of those elements have attributes >> assigned, like . >> >> I'm hoping with enough reading I can experiment and work my way >> through the problem and end up with a hopefully clear understanding of >> the ElementTree module and Dictionairies. >> >> Thanks for any suggestions in advance. > >Suggestions: >1. Learn to use the interpreter interactively; ie (at the least)? ie > >a. Start up python (without a program) >b. Play around with trivial expressions >c. Explore introspective features - help(), type() dir() > >2. Do you know about triple-quoted strings? >a. Start small (or trivial) sub-parts of your XML as triple-quoted examples in the >interpreter and start throwing them at elementtree >b. If they dont work trivialize further; if they work add complexity >----------- >? At the least because environments like Idle are more conducive to such playing I thank everyone for their feedback. I like the above advice and also Rustom's other comments about learning dictionaries. I'm thinking in the back of my mind my issue might be more around the dictionary than ElementTree at this point. I'll fill in my situation a little based on the queries back to me. I'm using a book, Python Programming Fundamentals by Kent D. Lee. http://knuth.luther.edu/~leekent/IntroToComputing/ It looks like he has updated his website since I last looked at it. I know there are many tutorials out there, however, I picked this one and resolved to complete it start to finish. That means a lot of side research and reading, which I'm happy to do. I've got somewhat of a handle on the basic XML parsing process (my opinion). The book uses minidom, but my reading suggests that ElementTree is a better option, so I thought I'd switch to that and attempt to duplicate the exercise. I understand this bit; import xml.etree.ElementTree as etree tree = etree.parse('workout.tcx') root = tree.getroot() and when I experiment and type root[1][1][0] in a console I can get the ID field from the xml file with a start time for the workout. That tells me it parsed properly. I'm having trouble getting my head around iterating over the elements, pulling out an activity element with an attribute of either running or biking, then getting multiple Trackpoint elements generated by the ForeRunner every 2 minutes, with an ultimate goal of creating a graph using turtle graphics (later in the exercise). I initially thought my trouble was with parsing the xml, but now I'm thinking my problem is I didn't pay enough attention to iterating over a dictionary. I will focus my efforts on Rustom's suggestions so please don't give me a solution. Some of my background is automotive technician, and a statement by one of my instructors has stuck in my mind over the years. He said, a friends car won't start, do you take booster cables or a can of gas with you? If you understand the theory of operation of an internal combustion engine, you can solve almost any problem. This is a small sample of the xml file. It's 170239 lines currently. 2011-06-06T12:21:04Z 3283.1700000 4821.6821289 2.8058963 398 125 167 Active Manual 34.5225040 -77.3563351 -0.3433838 0.0000000 116 Absent Jim. From leodavinci111 at gmail.com Wed Apr 1 19:26:26 2015 From: leodavinci111 at gmail.com (leonardo davinci) Date: Wed, 1 Apr 2015 16:26:26 -0700 Subject: Gpg python installer Message-ID: I am using Kleopatra(gpg for win) to verify the 3.4.3 python installer, Windows x86 MSI > www.python.org /ftp/python/3.4.3/ python-3.4.3.msi >. This file does not have a email in the digital signature and I am having trouble verifying the validity of the download. Any help would be appreciated. >thanks >Leo -------------- next part -------------- An HTML attachment was scrubbed... URL: From sturla.molden at gmail.com Wed Apr 1 19:51:16 2015 From: sturla.molden at gmail.com (Sturla Molden) Date: Wed, 1 Apr 2015 23:51:16 +0000 (UTC) Subject: Lockfile hanling References: Message-ID: <1636137113449625036.504680sturla.molden-gmail.com@news.gmane.org> Ian Kelly wrote: > As long as there's not *also* some other external process that needs > to access the file occasionally. :-) Then there is multiprocessing.Lock :) From davea at davea.name Wed Apr 1 19:51:47 2015 From: davea at davea.name (Dave Angel) Date: Wed, 01 Apr 2015 19:51:47 -0400 Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> Message-ID: <551C8493.8000500@davea.name> On 04/01/2015 09:43 AM, Saran A wrote: > On Tuesday, March 31, 2015 at 9:19:37 AM UTC-4, Dave Angel wrote: >> On 03/31/2015 07:00 AM, Saran A wrote: >> >> > @DaveA: This is a homework assignment. .... Is it possible that you >> could provide me with some snippets or guidance on where to place your >> suggestions (for your TO DOs 2,3,4,5)? >> > >> >> >>> On Monday, March 30, 2015 at 2:36:02 PM UTC-4, Dave Angel wrote: >> >>>> >>>> It's missing a number of your requirements. But it's a start. >>>> >>>> If it were my file, I'd have a TODO comment at the bottom stating known >>>> changes that are needed. In it, I'd mention: >>>> >>>> 1) your present code is assuming all filenames come directly from the >>>> commandline. No searching of a directory. >>>> >>>> 2) your present code does not move any files to success or failure >>>> directories >>>> >> >> In function validate_files() >> Just after the line >> print('success with %s on %d reco... >> you could move the file, using shutil. Likewise after the failure print. >> >>>> 3) your present code doesn't calculate or write to a text file any >>>> statistics. >> >> You successfully print to sys.stderr. So you could print to some other >> file in the exact same way. >> >>>> >>>> 4) your present code runs once through the names, and terminates. It >>>> doesn't "monitor" anything. >> >> Make a new function, perhaps called main(), with a loop that calls >> validate_files(), with a sleep after each pass. Of course, unless you >> fix TODO#1, that'll keep looking for the same files. No harm in that if >> that's the spec, since you moved the earlier versions of the files. >> >> But if you want to "monitor" the directory, let the directory name be >> the argument to main, and let main do a dirlist each time through the >> loop, and pass the corresponding list to validate_files. >> >>>> >>>> 5) your present code doesn't check for zero-length files >>>> >> >> In validate_and_process_data(), instead of checking filesize against >> ftell, check it against zero. >> >>>> I'd also wonder why you bother checking whether the >>>> os.path.getsize(file) function returns the same value as the os.SEEK_END >>>> and ftell() code does. Is it that you don't trust the library? Or that >>>> you have to run on Windows, where the line-ending logic can change the >>>> apparent file size? >>>> >>>> I notice you're not specifying a file mode on the open. So in Python 3, >>>> your sizes are going to be specified in unicode characters after >>>> decoding. Is that what the spec says? It's probably safer to >>>> explicitly specify the mode (and the file encoding if you're in text). >>>> >>>> I see you call strip() before comparing the length. Could there ever be >>>> leading or trailing whitespace that's significant? Is that the actual >>>> specification of line size? >>>> >>>> -- >>>> DaveA >>> >>> >> >>> I ask this because I have been searching fruitlessly through for some time and there are so many permutations that I am bamboozled by which is considered best practice. >>> >>> Moreover, as to the other comments, those are too specific. The scope of the assignment is very limited, but I am learning what I need to look out or ask questions regarding specs - in the future. >>> >> >> >> -- >> DaveA > > @DaveA > > My most recent commit (https://github.com/ahlusar1989/WGProjects/blob/master/P1version2.0withassumptions_mods.py) has more annotations and comments for each file. Perhaps you don't realize how github works. The whole point is it preserves the history of your code, and you use the same filename for each revision. Or possibly it's I that doesn't understand it. I use git, but haven't actually used github for my own code. > > I have attempted to address the functional requirements that you brought up: > > 1) Before, my present code was assuming all filenames come directly from the commandline. No searching of a directory. I think that I have addressed this. > Have you even tried to run the code? It quits immediately with an exception since your call to main() doesn't pass any arguments, and main requires one. > def main(dirslist): > while True: > for file in dirslist: > return validate_files(file) > time.sleep(5) In addition, you aren't actually doing anything to find what the files in the directory are. I tried to refer to dirlist, as a hint. A stronger hint: look up os.listdir() And that list of files has to change each time through the while loop, that's the whole meaning of scanning. You don't just grab the names once, you look to see what's there. The next thing is that you're using a variable called 'file', while that's a built-in type in Python. So you really want to use a different name. Next, you have a loop through the magical dirslist to get individual filenames, but then you call the validate_files() function with a single file, but that function is expecting a list of filenames. One or the other has to change. Next, you return from main after validating the first file, so no others will get processed. > 2) My present code does not move any files to success or failure directories (requirements for this assignment1). I am still wondering if and how I should use shututil() like you advised me to. I keep receiving a syntax error when declaring this below the print statement. You had a function to do that in your very first post to this thread. Have you forgotten it already? As for syntax errors, you can't expect any help on that when you don't show any code nor the syntax error traceback. Remember that when a syntax error is shown, it's frequently the previous line or two that actually was wrong. > > 3) You correctly reminded me that my present code doesn't calculate or write to a text file any statistics or errors for the cause of the error. > #I wrote an error class in case the I needed such a specification for a notifcation - this is not in the requirements > class ErrorHandler: > > def __init__(self): > pass > > def write(self, string): > > # write error to file > fname = " Error report (" + time.strftime("%Y-%m-%d %I-%M%p") + ").txt" > handler = open(fname, "w") > handler.write(string) > handler.close() This looks like Java code. With no data attributes, and only one method(function), what's the reason you don't just use a function? original_stderr = sys.stderr # stored in case want to revert sys.stderr = ErrorHandler() This is just bogus. There are rare times when a programmer might want to replace stderr, but that's just unnecessarily confusing in a program like this one. When you want to write printable data to another file, just use that file's handle as the argument to the file= argument of print(). You could use an instance of ErrorHandler as a file handle. Or do it one of a dozen other straightforward ways. While we're at it, why do you keep creating new files for your error report? And overwriting all the old messages with whatever new ones come in the same minute? > (Should I use the copy or copy2 method in order provide metadata? If so, should I wrap it into a try and except logic?) No idea what you mean here. What metadata? And what do copy and copy2 have to do with anything here? > > 4) Before, my present code runs once through the names, and terminates. It doesn't "monitor" anything. I think I have addressed this with the main function - correct? > Not even close. > 5) Before, my present code doesn't check for zero-length files - I have added a comment there in case that is needed) > > I realize appreciate your invaluable feedback. I have grown a lot with this assignment! > > Sincerely, > > Saran > -- DaveA From sohcahtoa82 at gmail.com Wed Apr 1 20:46:17 2015 From: sohcahtoa82 at gmail.com (sohcahtoa82 at gmail.com) Date: Wed, 1 Apr 2015 17:46:17 -0700 (PDT) Subject: New to Programming - XML Processing In-Reply-To: References: <0b0fd795-8f3e-443e-acc2-aed9b5d200ba@googlegroups.com> Message-ID: <5ca90f79-4e98-46d9-a971-2db8e8585ef0@googlegroups.com> On Wednesday, April 1, 2015 at 3:34:15 PM UTC-7, catperson wrote: > On Tue, 31 Mar 2015 21:17:38 -0700 (PDT), Rustom Mody > wrote: > > >On Wednesday, April 1, 2015 at 8:57:15 AM UTC+5:30, catperson wrote: > >> I am new to programming, though not new to computers. I'm looking to > >> teach myself Python 3 and am working my way through a tutorial. At > >> the point I'm at in the tutorial I am tasked with parsing out an XML > >> file created with a Garmin Forerunner and am just having a terrible > >> time getting my head around the concepts. What I'm looking for is > >> some suggested reading that might give me some of the theory of > >> operation behind ElementTree and then how to parse out specific > >> elements. Most of what I have been able to find in examples that I > >> can understand use very simplistic XML files and this Garmin file is > >> many levels of sub-elements and some of those elements have attributes > >> assigned, like . > >> > >> I'm hoping with enough reading I can experiment and work my way > >> through the problem and end up with a hopefully clear understanding of > >> the ElementTree module and Dictionairies. > >> > >> Thanks for any suggestions in advance. > > > >Suggestions: > >1. Learn to use the interpreter interactively; ie (at the least)? ie > > > >a. Start up python (without a program) > >b. Play around with trivial expressions > >c. Explore introspective features - help(), type() dir() > > > >2. Do you know about triple-quoted strings? > >a. Start small (or trivial) sub-parts of your XML as triple-quoted examples in the > >interpreter and start throwing them at elementtree > >b. If they dont work trivialize further; if they work add complexity > >----------- > >? At the least because environments like Idle are more conducive to such playing > > I thank everyone for their feedback. I like the above advice and also > Rustom's other comments about learning dictionaries. I'm thinking in > the back of my mind my issue might be more around the dictionary than > ElementTree at this point. > > I'll fill in my situation a little based on the queries back to me. > > I'm using a book, Python Programming Fundamentals by Kent D. Lee. > > http://knuth.luther.edu/~leekent/IntroToComputing/ > > It looks like he has updated his website since I last looked at it. I > know there are many tutorials out there, however, I picked this one > and resolved to complete it start to finish. That means a lot of side > research and reading, which I'm happy to do. > > I've got somewhat of a handle on the basic XML parsing process (my > opinion). The book uses minidom, but my reading suggests that > ElementTree is a better option, so I thought I'd switch to that and > attempt to duplicate the exercise. > > I understand this bit; > > import xml.etree.ElementTree as etree > tree = etree.parse('workout.tcx') > root = tree.getroot() > > 34.5225040 > -77.3563351 > Jim. This is on the coast of North Carolina, for anyone curious, just south of Snead's Ferry. From mr.smittye at gmail.com Wed Apr 1 22:22:28 2015 From: mr.smittye at gmail.com (E Smitty) Date: Wed, 1 Apr 2015 19:22:28 -0700 (PDT) Subject: Gpg python installer In-Reply-To: References: Message-ID: On Wednesday, April 1, 2015 at 4:26:40 PM UTC-7, leonardo davinci wrote: > I am using Kleopatra(gpg for win) to verify the 3.4.3 python installer, Windows x86 MSI > > >. This file does not have a email in the digital signaure and I am having trouble verifying the validity of the download. > > Any help would be appreciated. > > >thanks > > >Leo Could you tell us what steps you have taken? From nad at acm.org Thu Apr 2 03:13:22 2015 From: nad at acm.org (Ned Deily) Date: Thu, 02 Apr 2015 00:13:22 -0700 Subject: Gpg python installer References: Message-ID: In article , leonardo davinci wrote: > I am using Kleopatra(gpg for win) to verify the 3.4.3 python installer, > Windows x86 MSI > > > > www.python.org > /ftp/python/3.4.3/ > python-3.4.3.msi > >. This file does > not have a email in the digital signature and I am having trouble verifying > the validity of the download. Unfortunately, verifying the PGP signature of release files isn't the most user-friendly process, especially on Windows. The release files from python.org are typically PGP-signed in armored detached signature files, in other words, for each release file (like python-3.4.3.msi) there is a separate signature file with an appended .asc extension (python-3.4.3.msi.asc). If you go to the python.org downloads page (https://www.python.org/downloads/) and click on the release in question, it should take you to the page for the release (https://www.python.org/downloads/release/python-343/). Near the bottom of the page, there is a list of downloadable files and to the right of each one there is a "GPG" column with a "SIG" link for each file. Clicking on the SIG link should download the corresponding signature file (python-3.4.3.msi.asc). I'm not familiar with Kleopatra's interface but normally you'd want to download both the installer file and its asc file to the same directory/folder and then tell the GPG program to verify the asc file. The PGP/GPG program will also need to have access to the public keys of the creators / signers of the downloadable files. You will find them listed near the bottom of the Downloads page (https://www.python.org/downloads/). Independently thereof, the python.org Windows installer files are also signed with a public-key code signing certificate that should be automatically verified by the Windows installer program. (Likewise, for the Mac OS X installer files.) Hope this helps! -- Ned Deily, nad at acm.org From ahlusar.ahluwalia at gmail.com Thu Apr 2 08:02:55 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 05:02:55 -0700 (PDT) Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Message-ID: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> Good Morning: I understand this error message when I run this code. However, I am curious to know what the most pythonic way is to convert the list to a string? I use Python 2.7. "Traceback (most recent call last): before = dict([(f, None) for f in os.listdir(dirlist)]) TypeError: coercing to Unicode: need string or buffer, list found" The sample code that I am trying to run is: path = "/Users/Desktop/Projects/" dirlist = os.listdir(path) before = dict([(f, None) for f in os.listdir(dirlist)]) def main(dirlist): while True: time.sleep(10) #time between update check after = dict([(f, None) for f in os.listdir(dirlist)]) added = [f for f in after if not f in before] if added: print('Successfully added new file - ready to validate') if __name__ == "__main__": main() From rosuav at gmail.com Thu Apr 2 08:24:54 2015 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 2 Apr 2015 23:24:54 +1100 Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> Message-ID: On Thu, Apr 2, 2015 at 11:02 PM, Saran A wrote: > I understand this error message when I run this code. However, I am curious to know what the most pythonic way is to convert the list to a string? I use Python 2.7. > I don't think you actually want to convert a list into a string, here. Tell me if I'm understanding your code's intention correctly: > The sample code that I am trying to run is: > > path = "/Users/Desktop/Projects/" > dirlist = os.listdir(path) > before = dict([(f, None) for f in os.listdir(dirlist)]) Start up and get a full list of pre-existing files. > def main(dirlist): > while True: > time.sleep(10) #time between update check Then, every ten seconds... > after = dict([(f, None) for f in os.listdir(dirlist)]) > added = [f for f in after if not f in before] ... get a list of files, and if there are new ones... > if added: > print('Successfully added new file - ready to validate') > if __name__ == "__main__": > main() ... print out a message. If that's what you're trying to do, I would suggest using a directory notification system instead. Here's one that I use on Linux: https://github.com/Rosuav/shed/blob/master/dirwatch.py Here's another one, this time built for Windows: https://github.com/Rosuav/shed/blob/master/senddir.py But even if you absolutely have to poll, like that, you'll need to make a few code changes. The exception you're getting is symptomatic of just one problem with the code as published. My suspicion is that you just want to use listdir(path) rather than listdir(dirlist) - but if you want subdirectories, then you'll need to do things a bit differently (probably using os.walk instead). Also: You say you're using Python 2.7. If you have no particular reason to use 2.7, you'll do better to jump to Python 3. Your code will probably run identically, when it's this simple. ChrisA From __peter__ at web.de Thu Apr 2 08:26:21 2015 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Apr 2015 14:26:21 +0200 Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> Message-ID: Saran A wrote: > Good Morning: > > I understand this error message when I run this code. However, I am > curious to know what the most pythonic way is to convert the list to a > string? I use Python 2.7. > > "Traceback (most recent call last): > before = dict([(f, None) for f in os.listdir(dirlist)]) > TypeError: coercing to Unicode: need string or buffer, list found" > > > The sample code that I am trying to run is: > > path = "/Users/Desktop/Projects/" > dirlist = os.listdir(path) At this point dirlist is a list of names of the files and directories in "/Users/Desktop/Projects/" Assuming that the Projects folder contains the subfolders or files /Users/Desktop/Projects/foo, /Users/Desktop/Projects/bar and /Users/Desktop/Projects/baz dirlist looks like this: ["foo", "bar", "baz"] It makes no sense to pass this list to os.listdir() as you do below: > before = dict([(f, None) for f in os.listdir(dirlist)]) Forget about the other details in the error message; the actual problem is the "list found" part. Now what would be a possible fix? Sorry, I have no idea what your intention is. Again, you don't need to convert your list to string, you need to decide what directory you want to pass to listdir(). If you have multiple such directories you need to invoke listdir() multiple times with a single directory, typically in a loop. Bonus info: > while True: > time.sleep(10) #time between update check This loop will never terminate. From fomcl at yahoo.com Thu Apr 2 08:42:18 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 2 Apr 2015 05:42:18 -0700 Subject: New to Programming - XML Processing Message-ID: <1427978538.23367.BPMail_high_carrier@web163804.mail.gq1.yahoo.com> ----------------------------- On Wed, Apr 1, 2015 10:00 AM CEST Mark Lawrence wrote: >On 01/04/2015 05:27, Andrew Farrell wrote: >> You should follow Rustom's advice before just diving into the blog post >> I linked to. Otherwise you risk blindly following things and losing your >> bearings when you run into bugs. >> > >Sound advice, but would you please be kind enough to intersperse your answers or bottom post as top posting is heavily frowned upon here. > >TIA. Would it be possible to use a script that checks every incoming mail to the Python mail list? Main ingredients beatfilsoup (to textify HTML mails) and difflib (to check whether replies contain parts of the original message, and that no top posting happens) Just an idea. Albert-Jan >-- My fellow Pythonistas, ask not what our language can do for you, ask >what you can do for our language. > >Mark Lawrence > >-- https://mail.python.org/mailman/listinfo/python-list From ahlusar.ahluwalia at gmail.com Thu Apr 2 08:46:57 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 05:46:57 -0700 (PDT) Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> Message-ID: <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> On Thursday, April 2, 2015 at 8:26:01 AM UTC-4, Chris Angelico wrote: > On Thu, Apr 2, 2015 at 11:02 PM, Saran A wrote: > > I understand this error message when I run this code. However, I am curious to know what the most pythonic way is to convert the list to a string? I use Python 2.7. > > > > I don't think you actually want to convert a list into a string, here. > Tell me if I'm understanding your code's intention correctly: > > > The sample code that I am trying to run is: > > > > path = "/Users/Desktop/Projects/" > > dirlist = os.listdir(path) > > before = dict([(f, None) for f in os.listdir(dirlist)]) > > Start up and get a full list of pre-existing files. > > > def main(dirlist): > > while True: > > time.sleep(10) #time between update check > > Then, every ten seconds... > > > after = dict([(f, None) for f in os.listdir(dirlist)]) > > added = [f for f in after if not f in before] > > ... get a list of files, and if there are new ones... > > > if added: > > print('Successfully added new file - ready to validate') > > if __name__ == "__main__": > > main() > > ... print out a message. > > If that's what you're trying to do, I would suggest using a directory > notification system instead. Here's one that I use on Linux: > > https://github.com/Rosuav/shed/blob/master/dirwatch.py > > Here's another one, this time built for Windows: > > https://github.com/Rosuav/shed/blob/master/senddir.py > > But even if you absolutely have to poll, like that, you'll need to > make a few code changes. The exception you're getting is symptomatic > of just one problem with the code as published. My suspicion is that > you just want to use listdir(path) rather than listdir(dirlist) - but > if you want subdirectories, then you'll need to do things a bit > differently (probably using os.walk instead). > > Also: You say you're using Python 2.7. If you have no particular > reason to use 2.7, you'll do better to jump to Python 3. Your code > will probably run identically, when it's this simple. > > ChrisA @ChrisA - this is a smaller function that will take the most updated file. My intention is the following: * Monitor a folder for files that are dropped throughout the day * When a file is dropped in the folder the program should scan the file o IF all the contents in the file have the same length (let's assume line length) o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed o IF the file is empty OR the contents are not all of the same length o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). Here is the code I have written: import os import time import glob import sys def initialize_logger(output_dir): logger = logging.getLogger() logger.setLevel(logging.DEBUG) # create console handler and set level to info handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create error file handler and set level to error handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create debug file handler and set level to debug handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") handler.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) #Helper Functions for the Success and Failure Folder Outcomes, respectively #checks the length of the file def file_len(filename with open(filename) as f: for i, l in enumerate(f): pass return i + 1 #copies file to new destination def copyFile(src, dest): try: shutil.copy(src, dest) # eg. src and dest are the same file except shutil.Error as e: print('Error: %s' % e) # eg. source or destination doesn't exist except IOError as e: print('Error: %s' % e.strerror) #Failure Folder def move_to_failure_folder_and_return_error_file(): os.mkdir('Failure') copyFile(filename, 'Failure') initialize_logger('rootdir/Failure') logging.error("Either this file is empty or the lines") # Success Folder Requirement def move_to_success_folder_and_read(file): os.mkdir('Success') copyFile(filename, 'Success') print("Success", file) return file_len() #This simply checks the file information by name def fileinfo(file): filename = os.path.basename(file) rootdir = os.path.dirname(file) lastmod = time.ctime(os.path.getmtime(file)) creation = time.ctime(os.path.getctime(file)) filesize = os.path.getsize(file) return filename, rootdir, lastmod, creation, filesize if __name__ == '__main__': import sys validate_files(sys.argv[1:]) From ahlusar.ahluwalia at gmail.com Thu Apr 2 08:51:50 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 05:51:50 -0700 (PDT) Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> Message-ID: <1f248f13-92f6-40d3-8d9e-a94677d4d003@googlegroups.com> On Thursday, April 2, 2015 at 8:26:51 AM UTC-4, Peter Otten wrote: > Saran A wrote: > > > Good Morning: > > > > I understand this error message when I run this code. However, I am > > curious to know what the most pythonic way is to convert the list to a > > string? I use Python 2.7. > > > > "Traceback (most recent call last): > > before = dict([(f, None) for f in os.listdir(dirlist)]) > > TypeError: coercing to Unicode: need string or buffer, list found" > > > > > > The sample code that I am trying to run is: > > > > path = "/Users/Desktop/Projects/" > > dirlist = os.listdir(path) > > At this point dirlist is a list of names of the files and directories in > > "/Users/Desktop/Projects/" > > Assuming that the Projects folder contains the subfolders or files > /Users/Desktop/Projects/foo, /Users/Desktop/Projects/bar and > /Users/Desktop/Projects/baz dirlist looks like this: > > ["foo", "bar", "baz"] > > It makes no sense to pass this list to os.listdir() as you do below: > > > before = dict([(f, None) for f in os.listdir(dirlist)]) > > Forget about the other details in the error message; the actual problem is > the "list found" part. > > Now what would be a possible fix? Sorry, I have no idea what your intention > is. Again, you don't need to convert your list to string, you need to decide > what directory you want to pass to listdir(). If you have multiple such > directories you need to invoke listdir() multiple times with a single > directory, typically in a loop. > > Bonus info: > > > while True: > > time.sleep(10) #time between update check > > This loop will never terminate. @Peter I understand that the intention of this program is to not terminate. Here is what I have written so far: I thought I would run this by you, since you offer such valuable feedback, in the past. Just a quick rundown on what I want my program to do: * Monitor a folder for files that are dropped throughout the day * When a file is dropped in the folder the program should scan the file o IF all the contents in the file have the same length (let's assume line length) o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed o IF the file is empty OR the contents are not all of the same length o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). Here is the code I have written: import os import time import glob import sys def initialize_logger(output_dir): logger = logging.getLogger() logger.setLevel(logging.DEBUG) # create console handler and set level to info handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create error file handler and set level to error handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create debug file handler and set level to debug handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") handler.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) def main(dirslist): while True: for file in os.listdir(dirslist) : return validate_files(file) time.sleep(5) if __name__ == "__main__": main() #Helper Functions for the Success and Failure Folder Outcomes, respectively #checks the length of the file def file_len(filename with open(filename) as f: for i, l in enumerate(f): pass return i + 1 #copies file to new destination def copyFile(src, dest): try: shutil.copy(src, dest) # eg. src and dest are the same file except shutil.Error as e: print('Error: %s' % e) # eg. source or destination doesn't exist except IOError as e: print('Error: %s' % e.strerror) #Failure Folder def move_to_failure_folder_and_return_error_file(): os.mkdir('Failure') copyFile(filename, 'Failure') initialize_logger('rootdir/Failure') logging.error("Either this file is empty or the lines") # Success Folder Requirement def move_to_success_folder_and_read(file): os.mkdir('Success') copyFile(filename, 'Success') print("Success", file) return file_len() #This simply checks the file information by name def fileinfo(file): filename = os.path.basename(file) rootdir = os.path.dirname(file) lastmod = time.ctime(os.path.getmtime(file)) creation = time.ctime(os.path.getctime(file)) filesize = os.path.getsize(file) return filename, rootdir, lastmod, creation, filesize if __name__ == '__main__': import sys validate_files(sys.argv[1:]) I am trying to specifically address the fact that the program does not: The present code does not move any files to success or failure directories (I have added functions at the end that could serve to address this requirement) The present code doesn't calculate or write to a text file. The present code runs once through the names, and terminates. It doesn't "monitor" anything - I think that I have added the correct while loop to address this The present code doesn't check for zero-length files -Saran- From rosuav at gmail.com Thu Apr 2 09:06:31 2015 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Apr 2015 00:06:31 +1100 Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> Message-ID: On Thu, Apr 2, 2015 at 11:46 PM, Saran A wrote: > @ChrisA - this is a smaller function that will take the most updated file. My intention is the following: > > * Monitor a folder for files that are dropped throughout the day > > * When a file is dropped in the folder the program should scan the file > > o IF all the contents in the file have the same length (let's assume line length) > > o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed > > o IF the file is empty OR the contents are not all of the same length > > o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). > Sounds like a perfect job for inotify, then. Your function will be called whenever there's a new file. > Here is the code I have written: > > def initialize_logger(output_dir): > logger = logging.getLogger() > ... > def file_len(filename > with open(filename) as f: > for i, l in enumerate(f): > pass > return i + 1 These functions are all getting defined inside your initialize_logger() function. I suspect you want them to be flush left instead. > def copyFile(src, dest): > try: > shutil.copy(src, dest) > # eg. src and dest are the same file > except shutil.Error as e: > print('Error: %s' % e) > # eg. source or destination doesn't exist > except IOError as e: > print('Error: %s' % e.strerror) Recommendation: Skip the try/except, and just let exceptions bubble up. Don't just print out messages and keep going. > def move_to_failure_folder_and_return_error_file(): > os.mkdir('Failure') > copyFile(filename, 'Failure') > initialize_logger('rootdir/Failure') > logging.error("Either this file is empty or the lines") This doesn't move the file, it copies it. Is that your intention? Moving a file is pretty easy. Just use os.rename(). > if __name__ == '__main__': > import sys > validate_files(sys.argv[1:]) I've no idea what validate_files() does, as you haven't included that. I think you could code this fairly efficiently as a simple callback off pyinotify, or if you're not on Linux, with one of the equivalent services. What you're doing here (watching for files, looking inside them, and moving them when done) is pretty common around the world. ChrisA From ahlusar.ahluwalia at gmail.com Thu Apr 2 09:06:53 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 06:06:53 -0700 (PDT) Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> Message-ID: On Wednesday, April 1, 2015 at 7:52:27 PM UTC-4, Dave Angel wrote: > On 04/01/2015 09:43 AM, Saran A wrote: > > On Tuesday, March 31, 2015 at 9:19:37 AM UTC-4, Dave Angel wrote: > >> On 03/31/2015 07:00 AM, Saran A wrote: > >> > >> > @DaveA: This is a homework assignment. .... Is it possible that you > >> could provide me with some snippets or guidance on where to place your > >> suggestions (for your TO DOs 2,3,4,5)? > >> > > >> > >> > >>> On Monday, March 30, 2015 at 2:36:02 PM UTC-4, Dave Angel wrote: > >> > >>>> > >>>> It's missing a number of your requirements. But it's a start. > >>>> > >>>> If it were my file, I'd have a TODO comment at the bottom stating known > >>>> changes that are needed. In it, I'd mention: > >>>> > >>>> 1) your present code is assuming all filenames come directly from the > >>>> commandline. No searching of a directory. > >>>> > >>>> 2) your present code does not move any files to success or failure > >>>> directories > >>>> > >> > >> In function validate_files() > >> Just after the line > >> print('success with %s on %d reco... > >> you could move the file, using shutil. Likewise after the failure print. > >> > >>>> 3) your present code doesn't calculate or write to a text file any > >>>> statistics. > >> > >> You successfully print to sys.stderr. So you could print to some other > >> file in the exact same way. > >> > >>>> > >>>> 4) your present code runs once through the names, and terminates. It > >>>> doesn't "monitor" anything. > >> > >> Make a new function, perhaps called main(), with a loop that calls > >> validate_files(), with a sleep after each pass. Of course, unless you > >> fix TODO#1, that'll keep looking for the same files. No harm in that if > >> that's the spec, since you moved the earlier versions of the files. > >> > >> But if you want to "monitor" the directory, let the directory name be > >> the argument to main, and let main do a dirlist each time through the > >> loop, and pass the corresponding list to validate_files. > >> > >>>> > >>>> 5) your present code doesn't check for zero-length files > >>>> > >> > >> In validate_and_process_data(), instead of checking filesize against > >> ftell, check it against zero. > >> > >>>> I'd also wonder why you bother checking whether the > >>>> os.path.getsize(file) function returns the same value as the os.SEEK_END > >>>> and ftell() code does. Is it that you don't trust the library? Or that > >>>> you have to run on Windows, where the line-ending logic can change the > >>>> apparent file size? > >>>> > >>>> I notice you're not specifying a file mode on the open. So in Python 3, > >>>> your sizes are going to be specified in unicode characters after > >>>> decoding. Is that what the spec says? It's probably safer to > >>>> explicitly specify the mode (and the file encoding if you're in text). > >>>> > >>>> I see you call strip() before comparing the length. Could there ever be > >>>> leading or trailing whitespace that's significant? Is that the actual > >>>> specification of line size? > >>>> > >>>> -- > >>>> DaveA > >>> > >>> > >> > >>> I ask this because I have been searching fruitlessly through for some time and there are so many permutations that I am bamboozled by which is considered best practice. > >>> > >>> Moreover, as to the other comments, those are too specific. The scope of the assignment is very limited, but I am learning what I need to look out or ask questions regarding specs - in the future. > >>> > >> > >> > >> -- > >> DaveA > > > > @DaveA > > > > My most recent commit (https://github.com/ahlusar1989/WGProjects/blob/master/P1version2.0withassumptions_mods.py) has more annotations and comments for each file. > > Perhaps you don't realize how github works. The whole point is it > preserves the history of your code, and you use the same filename for > each revision. > > Or possibly it's I that doesn't understand it. I use git, but haven't > actually used github for my own code. > > > > > I have attempted to address the functional requirements that you brought up: > > > > 1) Before, my present code was assuming all filenames come directly from the commandline. No searching of a directory. I think that I have addressed this. > > > > Have you even tried to run the code? It quits immediately with an > exception since your call to main() doesn't pass any arguments, and main > requires one. > > > def main(dirslist): > > while True: > > for file in dirslist: > > return validate_files(file) > > time.sleep(5) > > In addition, you aren't actually doing anything to find what the files > in the directory are. I tried to refer to dirlist, as a hint. A > stronger hint: look up os.listdir() > > And that list of files has to change each time through the while loop, > that's the whole meaning of scanning. You don't just grab the names > once, you look to see what's there. > > The next thing is that you're using a variable called 'file', while > that's a built-in type in Python. So you really want to use a different > name. > > Next, you have a loop through the magical dirslist to get individual > filenames, but then you call the validate_files() function with a single > file, but that function is expecting a list of filenames. One or the > other has to change. > > Next, you return from main after validating the first file, so no others > will get processed. > > > 2) My present code does not move any files to success or failure directories (requirements for this assignment1). I am still wondering if and how I should use shututil() like you advised me to. I keep receiving a syntax error when declaring this below the print statement. > > You had a function to do that in your very first post to this thread. > Have you forgotten it already? As for syntax errors, you can't expect > any help on that when you don't show any code nor the syntax error > traceback. Remember that when a syntax error is shown, it's frequently > the previous line or two that actually was wrong. > > > > > 3) You correctly reminded me that my present code doesn't calculate or write to a text file any statistics or errors for the cause of the error. > > > #I wrote an error class in case the I needed such a specification for > a notifcation - this is not in the requirements > > class ErrorHandler: > > > > def __init__(self): > > pass > > > > def write(self, string): > > > > # write error to file > > fname = " Error report (" + time.strftime("%Y-%m-%d %I-%M%p") > + ").txt" > > handler = open(fname, "w") > > handler.write(string) > > handler.close() > > > > > This looks like Java code. With no data attributes, and only one > method(function), what's the reason you don't just use a function? > > > original_stderr = sys.stderr # stored in case want to revert > sys.stderr = ErrorHandler() > > This is just bogus. There are rare times when a programmer might want > to replace stderr, but that's just unnecessarily confusing in a program > like this one. When you want to write printable data to another file, > just use that file's handle as the argument to the file= argument of > print(). You could use an instance of ErrorHandler as a file handle. > Or do it one of a dozen other straightforward ways. > > While we're at it, why do you keep creating new files for your error > report? And overwriting all the old messages with whatever new ones > come in the same minute? > > > > (Should I use the copy or copy2 method in order provide metadata? If so, should I wrap it into a try and except logic?) > > No idea what you mean here. What metadata? And what do copy and copy2 > have to do with anything here? > > > > > 4) Before, my present code runs once through the names, and terminates. It doesn't "monitor" anything. I think I have addressed this with the main function - correct? > > > Not even close. > > > 5) Before, my present code doesn't check for zero-length files - I have added a comment there in case that is needed) > > > > I realize appreciate your invaluable feedback. I have grown a lot with this assignment! > > > > Sincerely, > > > > Saran > > > > > -- > DaveA @DaveA Thanks for your help on this homework assignment. I started from scratch last night. I have added some comments that will perhaps help clarify my intentions and my thought process. Thanks again. from __future__ import print_function import os import time import glob import sys def initialize_logger(output_dir): logger = logging.getLogger() logger.setLevel(logging.DEBUG) # create console handler and set level to info handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create error file handler and set level to error handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create debug file handler and set level to debug handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") handler.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) #Helper Functions for the Success and Failure Folder Outcomes, respectively def file_len(filename): with open(filename) as f: for i, l in enumerate(f): pass return i + 1 def copy_and_move_File(src, dest): try: shutil.rename(src, dest) # eg. src and dest are the same file except shutil.Error as e: print('Error: %s' % e) # eg. source or destination doesn't exist except IOError as e: print('Error: %s' % e.strerror) # Call main(), with a loop that calls # validate_files(), with a sleep after each pass. Before, my present #code was assuming all filenames come directly from the commandline. There was no actual searching #of a directory. # I am assuming that this is appropriate since I moved the earlier versions of the files. # I let the directory name be the argument to main, and let main do a dirlist each time through the loop, # and pass the corresponding list to validate_files. path = "/some/sample/path/" dirlist = os.listdir(path) before = dict([(f, None) for f in dirlist) #####Syntax Error? before = dict([(f, None) for f in dirlist) ^ SyntaxError: invalid syntax def main(dirlist): while True: time.sleep(10) #time between update check after = dict([(f, None) for f in dirlist) added = [f for f in after if not f in before] if added: print('Sucessfully added new file - ready to validate') ####add return statement here to pass to validate_files if __name__ == "__main__": main() #check for record time and record length - logic to be written to either pass to Failure or Success folder respectively def validate_files(): creation = time.ctime(os.path.getctime(added)) lastmod = time.ctime(os.path.getmtime(added)) #Potential Additions/Substitutions - what are the implications/consequences for this def move_to_failure_folder_and_return_error_file(): os.mkdir('Failure') copy_and_move_File(filename, 'Failure') initialize_logger('rootdir/Failure') logging.error("Either this file is empty or there are no lines") def move_to_success_folder_and_read(f): os.mkdir('Success') copy_and_move_File(filename, 'Success') print("Success", f) return file_len() #This simply checks the file information by name------> is this needed anymore? def fileinfo(file): filename = os.path.basename(f) rootdir = os.path.dirname(f) filesize = os.path.getsize(f) return filename, rootdir, filesize if __name__ == '__main__': import sys validate_files(sys.argv[1:]) # -- end of file From johnnybureddu at gmx.com Thu Apr 2 09:15:37 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:15:37 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <0cd7ca99-6a9b-4923-9797-9351a1b418cb@googlegroups.com> References: <0cd7ca99-6a9b-4923-9797-9351a1b418cb@googlegroups.com> Message-ID: -- redacted -- From johnnybureddu at gmx.com Thu Apr 2 09:17:13 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:17:13 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <4f5ceab4-d175-4b96-829b-8c8fab7f77f0@googlegroups.com> References: <4f5ceab4-d175-4b96-829b-8c8fab7f77f0@googlegroups.com> Message-ID: <1c0942dc-0889-40a7-b79e-3895d36fcec4@googlegroups.com> -- redacted -- From johnnybureddu at gmx.com Thu Apr 2 09:19:11 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:19:11 -0700 (PDT) Subject: -- redacted -- In-Reply-To: References: Message-ID: <0abd11c1-6dfa-4c57-86ea-e2d7fc014d92@googlegroups.com> -- redacted -- From johnnybureddu at gmx.com Thu Apr 2 09:19:53 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:19:53 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <360baff8-8111-49df-bde7-97df7e53ed17@googlegroups.com> References: <360baff8-8111-49df-bde7-97df7e53ed17@googlegroups.com> Message-ID: <2d877f3b-fb66-4afe-90bd-cc6ee71af22d@googlegroups.com> -- redacted -- From johnnybureddu at gmx.com Thu Apr 2 09:21:40 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:21:40 -0700 (PDT) Subject: -- redacted -- In-Reply-To: References: Message-ID: <72a48841-af75-48d3-a1c1-bfc3a199d10a@googlegroups.com> -- redacted -- From johnnybureddu at gmx.com Thu Apr 2 09:22:19 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:22:19 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <7d471aa5-b6ed-4800-9855-37319ba126ee@googlegroups.com> References: <7d471aa5-b6ed-4800-9855-37319ba126ee@googlegroups.com> Message-ID: <6cc06ab2-c6e9-4b92-a607-97b0d557df8c@googlegroups.com> -- redacted -- From johnnybureddu at gmx.com Thu Apr 2 09:22:36 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Thu, 2 Apr 2015 06:22:36 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <76d6b00b-d8fa-437a-9b0a-0c5e1170e67f@googlegroups.com> References: <76d6b00b-d8fa-437a-9b0a-0c5e1170e67f@googlegroups.com> Message-ID: -- redacted -- From ahlusar.ahluwalia at gmail.com Thu Apr 2 09:28:10 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 06:28:10 -0700 (PDT) Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> Message-ID: <33906269-3688-48fc-8315-e88eac644ace@googlegroups.com> On Thursday, April 2, 2015 at 9:06:49 AM UTC-4, Chris Angelico wrote: > On Thu, Apr 2, 2015 at 11:46 PM, Saran A wrote: > > @ChrisA - this is a smaller function that will take the most updated file. My intention is the following: > > > > * Monitor a folder for files that are dropped throughout the day > > > > * When a file is dropped in the folder the program should scan the file > > > > o IF all the contents in the file have the same length (let's assume line length) > > > > o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed > > > > o IF the file is empty OR the contents are not all of the same length > > > > o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). > > > > Sounds like a perfect job for inotify, then. Your function will be > called whenever there's a new file. > > > Here is the code I have written: > > > > def initialize_logger(output_dir): > > logger = logging.getLogger() > > ... > > def file_len(filename > > with open(filename) as f: > > for i, l in enumerate(f): > > pass > > return i + 1 > > These functions are all getting defined inside your > initialize_logger() function. I suspect you want them to be flush left > instead. > > > def copyFile(src, dest): > > try: > > shutil.copy(src, dest) > > # eg. src and dest are the same file > > except shutil.Error as e: > > print('Error: %s' % e) > > # eg. source or destination doesn't exist > > except IOError as e: > > print('Error: %s' % e.strerror) > > Recommendation: Skip the try/except, and just let exceptions bubble > up. Don't just print out messages and keep going. > > > def move_to_failure_folder_and_return_error_file(): > > os.mkdir('Failure') > > copyFile(filename, 'Failure') > > initialize_logger('rootdir/Failure') > > logging.error("Either this file is empty or the lines") > > This doesn't move the file, it copies it. Is that your intention? > > Moving a file is pretty easy. Just use os.rename(). > > > if __name__ == '__main__': > > import sys > > validate_files(sys.argv[1:]) > > I've no idea what validate_files() does, as you haven't included that. > > I think you could code this fairly efficiently as a simple callback > off pyinotify, or if you're not on Linux, with one of the equivalent > services. What you're doing here (watching for files, looking inside > them, and moving them when done) is pretty common around the world. > > ChrisA @ChrisA validate_files will: #double check for record time and record length - logic to be written to either pass to Failure or Success folder respectively. I welcome your thoughts on this. def validate_files(): creation = time.ctime(os.path.getctime(added)) lastmod = time.ctime(os.path.getmtime(added)) Does this address the issue. I particularly like writing my own exceptions as they provide me with more information on what could be the root cause. I know that in other circumstances, try and except are not the best practice. I appreciate the reminder though. Does this modification to copyFile do the job of moving the file? I haven't written a test yet. Thanks for catching the indentation for the helper functions. def copyFile(src, dest): > > try: > > shutil.rename(src, dest) > > # eg. src and dest are the same file > > except shutil.Error as e: > > print('Error: %s' % e) > > # eg. source or destination doesn't exist > > except IOError as e: > > print('Error: %s' % e.strerror) From rosuav at gmail.com Thu Apr 2 09:57:41 2015 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Apr 2015 00:57:41 +1100 Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: <33906269-3688-48fc-8315-e88eac644ace@googlegroups.com> References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> <33906269-3688-48fc-8315-e88eac644ace@googlegroups.com> Message-ID: On Fri, Apr 3, 2015 at 12:28 AM, Saran A wrote: > Does this modification to copyFile do the job of moving the file? I haven't written a test yet. > > Thanks for catching the indentation for the helper functions. > > def copyFile(src, dest): >> > try: >> > shutil.rename(src, dest) >> > # eg. src and dest are the same file >> > except shutil.Error as e: >> > print('Error: %s' % e) >> > # eg. source or destination doesn't exist >> > except IOError as e: >> > print('Error: %s' % e.strerror) You shouldn't need shutil here; just os.rename(src, dest) should do the trick. But be careful! Now you have a function which moves a file, and it's called "copyFile". If its purpose changes, so should its name. Have fun! ChrisA From richismyname at gmail.com Thu Apr 2 12:09:55 2015 From: richismyname at gmail.com (richismyname at gmail.com) Date: Thu, 2 Apr 2015 09:09:55 -0700 (PDT) Subject: Python to C++ in a pickle In-Reply-To: References: Message-ID: <12eeb7de-29a1-4c06-b938-234bda1b7c76@googlegroups.com> On Tuesday, July 4, 2000 at 12:00:00 AM UTC-7, Larry Whitley wrote: > I'd like to pickle an object in python and unpickle it in C++. The object > in question is a python dictionary so it will have to go into a C++ map. > Has someone done this and is willing to share? Or, is the cPickle source > available on the web somewhere? > > Larry Check out http://www.picklingtools.com. It gives you C++ code for pickling and unpickling as well as data structures for manipulating those structures. From ykravetz89 at gmail.com Thu Apr 2 15:41:27 2015 From: ykravetz89 at gmail.com (ykravetz89 at gmail.com) Date: Thu, 2 Apr 2015 12:41:27 -0700 (PDT) Subject: pysftp connection not sending files, hanging on 'put' In-Reply-To: References: Message-ID: <0df21ec2-149b-46ff-87f1-34864648abde@googlegroups.com> On Saturday, January 24, 2015 at 11:32:43 AM UTC-5, Jules Stevenson wrote: > Hi List, > > > I'm trying to send some files via pysftp, authentication seems fine, but actually putting the file results in it hanging, doing nothing. The files are small (<200kb) so I don't believe it's an upload issue (internet access is fine). Eventually the connection times out. > > > Code: > > > > if os.listdir(ftp_path):# only do if files are there > with pysftp.Connection(FTP_SERVER, > > port=2222 > > username=FTP_USER,? > > private_key=FTP_SSH_PRIVATE_KEY_PATH > > ) as sftp: > with sftp.cd(FTP_REMOTE_FOLDER): > > for f in os.listdir(ftp_path): > > if os.path.isfile(os.path.join(ftp_path,f)) : > > # upload image to server > > self.log.info("HB FTP, Start upload: "+f) > > print(ftp_path+"\\"+f) > > sftp.put(os.path.join(ftp_path,f)) > > self.log.info("HB FTP, Finished Upload: "+f) > > > > Logging output here: > > > > FSW_COMMS paramiko.transport 01/24/2015 04:23:58 PM: INFO: Authentication (publickey) successful! > FSW_COMMS paramiko.transport 01/24/2015 04:23:58 PM: DEBUG: [chan 1] Max packet in: 32768 bytes > FSW_COMMS paramiko.transport 01/24/2015 04:23:58 PM: DEBUG: [chan 1] Max packet out: 0 bytes > FSW_COMMS paramiko.transport 01/24/2015 04:23:58 PM: DEBUG: Secsh channel 1 opened. > FSW_COMMS paramiko.transport 01/24/2015 04:23:58 PM: DEBUG: [chan 1] Sesch channel 1 request ok > FSW_COMMS paramiko.transport.sftp 01/24/2015 04:23:58 PM: INFO: [chan 1] Opened sftp connection (server version 3) > FSW_COMMS paramiko.transport.sftp 01/24/2015 04:23:58 PM: DEBUG: [chan 1] normalize('.') > FSW_COMMS paramiko.transport.sftp 01/24/2015 04:23:58 PM: DEBUG: [chan 1] stat('files') > FSW_COMMS paramiko.transport.sftp 01/24/2015 04:23:58 PM: DEBUG: [chan 1] normalize('files') > FSW_COMMS root 01/24/2015 04:23:58 PM: INFO: HB FTP, Start upload: 2015-01-17-19-37-07.jpg > D:\FSW\ftp\2015-01-17-19-37-07.jpg > FSW_COMMS paramiko.transport.sftp 01/24/2015 04:23:58 PM: DEBUG: [chan 1] open('/files/2015-01-17-19-37-07.jpg', 'wb') > FSW_COMMS paramiko.transport.sftp 01/24/2015 04:23:58 PM: DEBUG: [chan 1] open('/files/2015-01-17-19-37-07.jpg', 'wb') -> 34613039393262343666383036653839 > > > Any help much appreciated, > > > Jules I am having the exact issue, have you found a solution? From davea at davea.name Thu Apr 2 17:10:46 2015 From: davea at davea.name (Dave Angel) Date: Thu, 02 Apr 2015 17:10:46 -0400 Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> Message-ID: <551DB056.5010405@davea.name> On 04/02/2015 09:06 AM, Saran A wrote: > > Thanks for your help on this homework assignment. I started from scratch last night. I have added some comments that will perhaps help clarify my intentions and my thought process. Thanks again. > > from __future__ import print_function I'll just randomly comment on some things I see here. You've started several threads, on two different forums, so it's impractical to figure out what's really up. > > #Helper Functions for the Success and Failure Folder Outcomes, respectively > > def file_len(filename): This is an indentation error, as you forgot to start at the left margin > with open(filename) as f: > for i, l in enumerate(f): > pass > return i + 1 > > > def copy_and_move_File(src, dest): ditto > try: > shutil.rename(src, dest) Is there a reason you don't use the move function? rename won't work if the two directories aren't on the same file system. > # eg. src and dest are the same file > except shutil.Error as e: > print('Error: %s' % e) > # eg. source or destination doesn't exist > except IOError as e: > print('Error: %s' % e.strerror) > > > # Call main(), with a loop that calls # validate_files(), with a sleep after each pass. Before, my present #code was assuming all filenames come directly from the commandline. There was no actual searching #of a directory. > > # I am assuming that this is appropriate since I moved the earlier versions of the files. > # I let the directory name be the argument to main, and let main do a dirlist each time through the loop, > # and pass the corresponding list to validate_files. > > > path = "/some/sample/path/" > dirlist = os.listdir(path) > before = dict([(f, None) for f in dirlist) > > #####Syntax Error? before = dict([(f, None) for f in dirlist) > ^ > SyntaxError: invalid syntax Look at the line in question. There's an unmatched set of brackets. Not that it matters, since you don't need these 2 lines for anything. See my comments on some other forum. > > def main(dirlist): bad name for a directory path variable. > while True: > time.sleep(10) #time between update check Somewhere inside this loop, you want to obtain a list of files in the specified directory. And you want to do something with that list. You don't have to worry about what the files were last time, because presumably those are gone. Unless in an unwritten part of the spec, you're supposed to abort if any filename is repeated over time. > after = dict([(f, None) for f in dirlist) > added = [f for f in after if not f in before] > if added: > print('Sucessfully added new file - ready to validate') > ####add return statement here to pass to validate_files > if __name__ == "__main__": > main() You'll need an argument to call main() > > > #check for record time and record length - logic to be written to either pass to Failure or Success folder respectively > > def validate_files(): Where are all the parameters to this function? > creation = time.ctime(os.path.getctime(added)) > lastmod = time.ctime(os.path.getmtime(added)) > > > > #Potential Additions/Substitutions - what are the implications/consequences for this > > def move_to_failure_folder_and_return_error_file(): > os.mkdir('Failure') > copy_and_move_File(filename, 'Failure') > initialize_logger('rootdir/Failure') > logging.error("Either this file is empty or there are no lines") > > > def move_to_success_folder_and_read(f): > os.mkdir('Success') > copy_and_move_File(filename, 'Success') > print("Success", f) > return file_len() > > #This simply checks the file information by name------> is this needed anymore? > > def fileinfo(file): > filename = os.path.basename(f) > rootdir = os.path.dirname(f) > filesize = os.path.getsize(f) > return filename, rootdir, filesize > > if __name__ == '__main__': > import sys > validate_files(sys.argv[1:]) > > # -- end of file > -- DaveA From PointedEars at web.de Thu Apr 2 17:31:04 2015 From: PointedEars at web.de (Thomas 'PointedEars' Lahn) Date: Thu, 02 Apr 2015 23:31:04 +0200 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> Message-ID: <1905422.ubGH1B8UfE@PointedEars.de> Ian Kelly wrote: > [?] Thomas 'PointedEars' Lahn [?] wrote: >> Ian Kelly wrote: >>> Within a grammar, the question of "is an X a Y" is nonsensical in >>> isolation. It can only be answered in relation to a parse tree. >>> Consider the simple grammar: >>> >>> S -> A | B >>> A -> x >>> B -> x >>> >>> Is x an A? It depends. >> >> No, by the definition 2 below, that we all accepted implicitly up to this >> point, x is *definitely* an A. > > What gives you the impression that I ever accepted it? ,- | | What the grammar that you quoted from shows is that STRING+ is an | expression. There is *no way* for you to make that statement if you did not accept definition (2). >> (2) Let the statement ?x is an A? be true if x can be produced in a >> production chain starting with or including the non-terminal A >> left-hand side ? >> >> x ? A ? ?A (? ? A ? ? ? x). > > Sorry, but this definition just seems entirely arbitrary to me. It is just the formalization of the definition that we all have agreed to, including you. > Mathematically, it looks nonsensical; A is a symbol, not a set. ?A? is the goal symbol of a production, so it can be interpreted as the superset of all set of terminals that can be produced from it, through the goal symbols that can be produced from it. And all of us implicitly did that when we said ?STRING(+) (literals) is/are (not) (an) expression(s)?. > This question of whether "x is an A" is informal and not a topic of formal > language theory so far as I'm aware. Can you cite some source for it? No, because I was formalizing the ad-hoc definition by Chris Angelico in . >> Now, according to these definitions, in the offered grammar x is *both* >> an A and a B. Because what matters is _not_ the practical result of >> production chains (the actual parse tree), but the certainty of the >> theoretical possibility of it. > > This strikes me as being a lot like arguing, "some kites are toys, and > some kites are birds; therefore, all kites are both toys and birds." False analogy again. We are discussing *in theory* a *formal* grammar. Its goal symbols have *no meaning* except what can be produced from them. > As noted above, the inaccuracy that Gregory pointed out has no bearing > on my argument. But it does. > You're really going to make me spell it out, aren't you? Fine, here you > go. > > single_input -> [?] -> expr -> [?] -> atom -> STRING STRING > > Note: the derivation contains exactly one expr node, which indirectly > produces both STRINGs. Neither STRING in this derivation is > individually produced from the expr. So you have proven that which nobody ever doubted nor requested, but I pointed out already. What you have still not proven is what you claimed: the parse tree. I am sorry that you cannot see that your argument is strewn with gaping defects in logic, but I think I will stop trying to convince you of that now. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail. From sohcahtoa82 at gmail.com Thu Apr 2 18:26:01 2015 From: sohcahtoa82 at gmail.com (sohcahtoa82 at gmail.com) Date: Thu, 2 Apr 2015 15:26:01 -0700 (PDT) Subject: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: <1905422.ubGH1B8UfE@PointedEars.de> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> Message-ID: On Thursday, April 2, 2015 at 2:33:17 PM UTC-7, Thomas 'PointedEars' Lahn wrote: > Ian Kelly wrote: > > > [?] Thomas 'PointedEars' Lahn [?] wrote: > >> Ian Kelly wrote: > >>> Within a grammar, the question of "is an X a Y" is nonsensical in > >>> isolation. It can only be answered in relation to a parse tree. > >>> Consider the simple grammar: > >>> > >>> S -> A | B > >>> A -> x > >>> B -> x > >>> > >>> Is x an A? It depends. > >> > >> No, by the definition 2 below, that we all accepted implicitly up to this > >> point, x is *definitely* an A. > > > > What gives you the impression that I ever accepted it? > > ,- > | > | What the grammar that you quoted from shows is that STRING+ is an > | expression. > > There is *no way* for you to make that statement if you did not accept > definition (2). > > >> (2) Let the statement ?x is an A? be true if x can be produced in a > >> production chain starting with or including the non-terminal A > >> left-hand side ? > >> > >> x ? A ? ?A (? ? A ? ? ? x). > > > > Sorry, but this definition just seems entirely arbitrary to me. > > It is just the formalization of the definition that we all have agreed to, > including you. > > > Mathematically, it looks nonsensical; A is a symbol, not a set. > > ?A? is the goal symbol of a production, so it can be interpreted as the > superset of all set of terminals that can be produced from it, through the > goal symbols that can be produced from it. And all of us implicitly did > that when we said ?STRING(+) (literals) is/are (not) (an) expression(s)?. > > > This question of whether "x is an A" is informal and not a topic of formal > > language theory so far as I'm aware. Can you cite some source for it? > > No, because I was formalizing the ad-hoc definition by Chris Angelico in > . > > >> Now, according to these definitions, in the offered grammar x is *both* > >> an A and a B. Because what matters is _not_ the practical result of > >> production chains (the actual parse tree), but the certainty of the > >> theoretical possibility of it. > > > > This strikes me as being a lot like arguing, "some kites are toys, and > > some kites are birds; therefore, all kites are both toys and birds." > > False analogy again. We are discussing *in theory* a *formal* grammar. Its > goal symbols have *no meaning* except what can be produced from them. > > > As noted above, the inaccuracy that Gregory pointed out has no bearing > > on my argument. > > But it does. > > > You're really going to make me spell it out, aren't you? Fine, here you > > go. > > > > single_input -> [?] -> expr -> [?] -> atom -> STRING STRING > > > > Note: the derivation contains exactly one expr node, which indirectly > > produces both STRINGs. Neither STRING in this derivation is > > individually produced from the expr. > > So you have proven that which nobody ever doubted nor requested, but I > pointed out already. What you have still not proven is what you claimed: > the parse tree. > > I am sorry that you cannot see that your argument is strewn with gaping > defects in logic, but I think I will stop trying to convince you of that > now. > > -- > PointedEars > > Twitter: @PointedEars2 > Please do not cc me. / Bitte keine Kopien per E-Mail. *sigh* https://xkcd.com/386/ From ahlusar.ahluwalia at gmail.com Thu Apr 2 19:43:01 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 16:43:01 -0700 (PDT) Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> Message-ID: <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> On Thursday, April 2, 2015 at 5:11:20 PM UTC-4, Dave Angel wrote: > On 04/02/2015 09:06 AM, Saran A wrote: > > > > > Thanks for your help on this homework assignment. I started from scratch last night. I have added some comments that will perhaps help clarify my intentions and my thought process. Thanks again. > > > > from __future__ import print_function > > I'll just randomly comment on some things I see here. You've started > several threads, on two different forums, so it's impractical to figure > out what's really up. > > > > > > > #Helper Functions for the Success and Failure Folder Outcomes, respectively > > > > def file_len(filename): > > This is an indentation error, as you forgot to start at the left margin > > > with open(filename) as f: > > for i, l in enumerate(f): > > pass > > return i + 1 > > > > > > def copy_and_move_File(src, dest): > > ditto > > > try: > > shutil.rename(src, dest) > > Is there a reason you don't use the move function? rename won't work if > the two directories aren't on the same file system. > > > # eg. src and dest are the same file > > except shutil.Error as e: > > print('Error: %s' % e) > > # eg. source or destination doesn't exist > > except IOError as e: > > print('Error: %s' % e.strerror) > > > > > > # Call main(), with a loop that calls # validate_files(), with a sleep after each pass. Before, my present #code was assuming all filenames come directly from the commandline. There was no actual searching #of a directory. > > > > # I am assuming that this is appropriate since I moved the earlier versions of the files. > > # I let the directory name be the argument to main, and let main do a dirlist each time through the loop, > > # and pass the corresponding list to validate_files. > > > > > > path = "/some/sample/path/" > > dirlist = os.listdir(path) > > before = dict([(f, None) for f in dirlist) > > > > #####Syntax Error? before = dict([(f, None) for f in dirlist) > > ^ > > SyntaxError: invalid syntax > > Look at the line in question. There's an unmatched set of brackets. Not > that it matters, since you don't need these 2 lines for anything. See > my comments on some other forum. > > > > > def main(dirlist): > > bad name for a directory path variable. > > > while True: > > time.sleep(10) #time between update check > > Somewhere inside this loop, you want to obtain a list of files in the > specified directory. And you want to do something with that list. You > don't have to worry about what the files were last time, because > presumably those are gone. Unless in an unwritten part of the spec, > you're supposed to abort if any filename is repeated over time. > > > > after = dict([(f, None) for f in dirlist) > > added = [f for f in after if not f in before] > > if added: > > print('Sucessfully added new file - ready to validate') > > ####add return statement here to pass to validate_files > > if __name__ == "__main__": > > main() > > You'll need an argument to call main() > > > > > > > #check for record time and record length - logic to be written to either pass to Failure or Success folder respectively > > > > def validate_files(): > > Where are all the parameters to this function? > > > creation = time.ctime(os.path.getctime(added)) > > lastmod = time.ctime(os.path.getmtime(added)) > > > > > > > > #Potential Additions/Substitutions - what are the implications/consequences for this > > > > def move_to_failure_folder_and_return_error_file(): > > os.mkdir('Failure') > > copy_and_move_File(filename, 'Failure') > > initialize_logger('rootdir/Failure') > > logging.error("Either this file is empty or there are no lines") > > > > > > def move_to_success_folder_and_read(f): > > os.mkdir('Success') > > copy_and_move_File(filename, 'Success') > > print("Success", f) > > return file_len() > > > > #This simply checks the file information by name------> is this needed anymore? > > > > def fileinfo(file): > > filename = os.path.basename(f) > > rootdir = os.path.dirname(f) > > filesize = os.path.getsize(f) > > return filename, rootdir, filesize > > > > if __name__ == '__main__': > > import sys > > validate_files(sys.argv[1:]) > > > > # -- end of file > > > > > -- > DaveA @DaveA I debugged and rewrote everything. Here is the full version. Feel free to tear this apart. The homework assignment is not due until tomorrow, so I am currently also experimenting with pyinotify as well. I do have questions regarding how to make this function compatible with the ProcessEvent Class. I will create another post for this. What would you advise in regards to renaming the inaptly named dirlist? # # # Without data to examine here, I can only guess based on this requirement's language that # # fixed records are in the input. If so, here's a slight revision to the helper functions that I wrote earlier which # # takes the function fileinfo as a starting point and demonstrates calling a function from within a function. # I tested this little sample on a small set of files created with MD5 checksums. I wrote the Python in such a way as it # would work with Python 2.x or 3.x (note the __future__ at the top). # # # There are so many wonderful ways of failure, so, from a development standpoint, I would probably spend a bit # # more time trying to determine which failure(s) I would want to report to the user, and how (perhaps creating my own Exceptions) # # # The only other comments I would make are about safe-file handling. # # # #1: Question: After a user has created a file that has failed (in # # # processing),can the user create a file with the same name? # # # If so, then you will probably want to look at some sort # # # of file-naming strategy to avoid overwriting evidence of # # # earlier failures. # # # File naming is a tricky thing. I referenced the tempfile module [1] and the Maildir naming scheme to see two different # # types of solutions to the problem of choosing a unique filename. ## I am assuming that all of my files are going to be specified in unicode ## Utilized Spyder's Scientific Computing IDE to debug, check for indentation errors and test function suite from __future__ import print_function import os.path import time import logging def initialize_logger(output_dir): logger = logging.getLogger() logger.setLevel(logging.DEBUG) # create console handler and set level to info handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create error file handler and set level to error handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create debug file handler and set level to debug handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") handler.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) #Returns filename, rootdir and filesize def fileinfo(f): filename = os.path.basename(f) rootdir = os.path.dirname(f) filesize = os.path.getsize(f) return filename, rootdir, filesize #returns length of file def file_len(f): with open(f) as f: for i, l in enumerate(f): pass return i + 1 #attempts to copy file and move file to it's directory def copy_and_move_file(src, dest): try: os.rename(src, dest) # eg. src and dest are the same file except IOError as e: print('Error: %s' % e.strerror) path = "." dirlist = os.listdir(path) def main(dirlist): before = dict([(f, 0) for f in dirlist]) while True: time.sleep(1) #time between update check after = dict([(f, None) for f in dirlist]) added = [f for f in after if not f in before] if added: f = ''.join(added) print('Sucessfully added %s file - ready to validate') %() return validate_files(f) else: return move_to_failure_folder_and_return_error_file(f) def validate_files(f): creation = time.ctime(os.path.getctime(f)) lastmod = time.ctime(os.path.getmtime(f)) if creation == lastmod and file_len(f) > 0: return move_to_success_folder_and_read(f) if file_len < 0 and creation != lastmod: return move_to_success_folder_and_read(f) else: return move_to_failure_folder_and_return_error_file(f) #Potential Additions/Substitutions def move_to_failure_folder_and_return_error_file(): filename, rootdir, lastmod, creation, filesize = fileinfo(file) os.mkdir('Failure') copy_and_move_file( 'Failure') initialize_logger('rootdir/Failure') logging.error("Either this file is empty or there are no lines") def move_to_success_folder_and_read(): filename, rootdir, lastmod, creation, filesize = fileinfo(file) os.mkdir('Success') copy_and_move_file(rootdir, 'Success') #file name print("Success", file) return file_len(file) if __name__ == '__main__': main(dirlist) From rosuav at gmail.com Thu Apr 2 20:12:12 2015 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Apr 2015 11:12:12 +1100 Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> Message-ID: On Fri, Apr 3, 2015 at 11:03 AM, Dennis Lee Bieber wrote: >>o IF all the contents in the file have the same length (let's assume line length) >> >>o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed >> >>o IF the file is empty OR the contents are not all of the same length >> >>o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). >> > You still haven't defined how you determine the "correct length" of the > record. What if the first line is 79 characters, and all the others are 80 > characters? Do you report ALL lines EXCEPT the first as being the wrong > length, when really it is the first line that is wrong? Relatively immaterial here; in the first place, line length is just a placeholder (my guess is it's more likely to be something like "CSV files with the same number of cells on each row", or something), and in the second place, the lines aren't the failures - if there's a mismatch, the entire file is deemed wrong. It doesn't matter whether it's the first line or the other lines, the file is dead. ChrisA From ahlusar.ahluwalia at gmail.com Thu Apr 2 20:14:30 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 17:14:30 -0700 (PDT) Subject: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found In-Reply-To: References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> <57693d65-e683-4972-ac8d-97b2feace3bb@googlegroups.com> Message-ID: <81391d9c-74a0-429a-82e6-d874057b8f9c@googlegroups.com> On Thursday, April 2, 2015 at 8:03:53 PM UTC-4, Dennis Lee Bieber wrote: > On Thu, 2 Apr 2015 05:46:57 -0700 (PDT), Saran A > declaimed the following: > > > > >@ChrisA - this is a smaller function that will take the most updated file. My intention is the following: > > > >* Monitor a folder for files that are dropped throughout the day > > > I would suggest that your first prototype is to be a program that > contains a function whose only purpose is to report on the files it finds > -- forget about all the processing/moving of the files until you can > successfully loop around the work of fetching the directory and handling > the file names found (by maybe printing the names of the ones determined to > be new since last fetch). > > >* When a file is dropped in the folder the program should scan the file > > > >o IF all the contents in the file have the same length (let's assume line length) > > > >o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records/lines/words processed > > > >o IF the file is empty OR the contents are not all of the same length > > > >o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). > > > You still haven't defined how you determine the "correct length" of the > record. What if the first line is 79 characters, and all the others are 80 > characters? Do you report ALL lines EXCEPT the first as being the wrong > length, when really it is the first line that is wrong? > > Also, if the files are Unicode (UTF-8, in particular) -- the byte > length of a line could differ but the character length could be the same. > > >Here is the code I have written: > > > >import os > >import time > >import glob > >import sys > > > >def initialize_logger(output_dir): > > logger = logging.getLogger() > > logger.setLevel(logging.DEBUG) > > > > # create console handler and set level to info > > handler = logging.StreamHandler() > > handler.setLevel(logging.INFO) > > formatter = logging.Formatter("%(levelname)s - %(message)s") > > handler.setFormatter(formatter) > > logger.addHandler(handler) > > > > # create error file handler and set level to error > > handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") > > handler.setLevel(logging.ERROR) > > formatter = logging.Formatter("%(levelname)s - %(message)s") > > handler.setFormatter(formatter) > > logger.addHandler(handler) > > > > # create debug file handler and set level to debug > > handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") > > handler.setLevel(logging.DEBUG) > > formatter = logging.Formatter("%(levelname)s - %(message)s") > > handler.setFormatter(formatter) > > logger.addHandler(handler) > > > >#Helper Functions for the Success and Failure Folder Outcomes, respectively > > > >#checks the length of the file > > def file_len(filename > > with open(filename) as f: > > for i, l in enumerate(f): > > pass > > return i + 1 > > > >#copies file to new destination > > > > def copyFile(src, dest): > > try: > > shutil.copy(src, dest) > > # eg. src and dest are the same file > > except shutil.Error as e: > > print('Error: %s' % e) > > # eg. source or destination doesn't exist > > except IOError as e: > > print('Error: %s' % e.strerror) > > > >#Failure Folder > > > >def move_to_failure_folder_and_return_error_file(): > > os.mkdir('Failure') > > copyFile(filename, 'Failure') > > initialize_logger('rootdir/Failure') > > logging.error("Either this file is empty or the lines") > > > ># Success Folder Requirement > > > >def move_to_success_folder_and_read(file): > > os.mkdir('Success') > > copyFile(filename, 'Success') > > print("Success", file) > > return file_len() > > > > > >#This simply checks the file information by name > > > >def fileinfo(file): > > filename = os.path.basename(file) > > rootdir = os.path.dirname(file) > > lastmod = time.ctime(os.path.getmtime(file)) > > creation = time.ctime(os.path.getctime(file)) > > filesize = os.path.getsize(file) > > return filename, rootdir, lastmod, creation, filesize > > > >if __name__ == '__main__': > > import sys > > validate_files(sys.argv[1:]) > > Yeesh... Did you even try running that? > > validate_files is not defined > file_len is at the wrong indentation > is syntactically garbage > is a big time-waste (you read the file just to > enumerate the number of lines? Why didn't you count the lines while > checking the line lengths) > copyFile is at the wrong indentation > (after a bunch of word_word, why camelCase here) > > Correct all the edit errors and copy/paste the actual file that at > least attempts to run. > > You might also want to look at os.stat, rather than using three os.path > calls. > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ @Dennis: Below is my full program (so far). Please feel free to tear it apart and provide me with constructive criticism. I have been programming for 8 months now and this is a huge learning experience for me. Feedback and modifications is very welcome. What would be a better name for dirlist? # # # Without data to examine here, I can only guess based on this requirement's language that # # fixed records are in the input. ##I made the assumption that the directories are in the same filesystem # # Takes the function fileinfo as a starting point and demonstrates calling a function from within a function. # I tested this little sample on a small set of files created with MD5 checksums. I wrote the Python in such a way as it # would work with Python 2.x or 3.x (note the __future__ at the top). # # # There are so many wonderful ways of failure, so, from a development standpoint, I would probably spend a bit # # more time trying to determine which failure(s) I would want to report to the user, and how (perhaps creating my own Exceptions) # # # The only other comments I would make are about safe-file handling. # # # #1: Question: After a user has created a file that has failed (in # # # processing),can the user create a file with the same name? # # # If so, then you will probably want to look at some sort # # # of file-naming strategy to avoid overwriting evidence of # # # earlier failures. # # # File naming is a tricky thing. I referenced the tempfile module [1] and the Maildir naming scheme to see two different # # types of solutions to the problem of choosing a unique filename. ## I am assuming that all of my files are going to be specified in unicode ## Utilized Spyder's Scientific Computing IDE to debug, check for indentation errors and test function suite from __future__ import print_function import os.path import time import difflib import logging def initialize_logger(output_dir): logger = logging.getLogger() logger.setLevel(logging.DEBUG) # create console handler and set level to info handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create error file handler and set level to error handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create debug file handler and set level to debug handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") handler.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) #This function's purpose is to obtain the filename, rootdir and filesize def fileinfo(f): filename = os.path.basename(f) rootdir = os.path.dirname(f) filesize = os.path.getsize(f) return filename, rootdir, filesize #This helper function returns the length of the file def file_len(f): with open(f) as f: for i, l in enumerate(f): pass return i + 1 #This helper function attempts to copy file and move file to the respective directory #I am assuming that the directories are in the same filesystem # If directories ARE in different file systems, I would use the following helper function: # def move(src, dest): # shutil.move(src, dest) def copy_and_move_file(src, dest): try: os.rename(src, dest) # eg. src and dest are the same file except IOError as e: print('Error: %s' % e.strerror) path = "." dirlist = os.listdir(path) # Caveats of the "main" function is that it does not scale well #(although it is appropriate if one assumes that there will be few changes) # It does not account for updated files existing in the directory - only new files "dropped" in # (If this was included in the requirements, os.stat would be appropriate here) def main(dirlist): before = dict([(f, 0) for f in dirlist]) while True: time.sleep(1) #time between update check after = dict([(f, None) for f in dirlist]) added = [f for f in after if not f in before] if added: f = ''.join(added) print('Sucessfully added %s file - ready to validate') %(f) return validate_files(f) else: return move_to_failure_folder_and_return_error_file(f) def validate_files(f): creation = time.ctime(os.path.getctime(f)) lastmod = time.ctime(os.path.getmtime(f)) if creation == lastmod and file_len(f) > 0: return move_to_success_folder_and_read(f) if file_len < 0 and creation != lastmod: return move_to_success_folder_and_read(f) else: return move_to_failure_folder_and_return_error_file(f) # Failure/Success Folder Functions def move_to_failure_folder_and_return_error_file(): filename, rootdir, lastmod, creation, filesize = fileinfo(file) os.mkdir('Failure') copy_and_move_file( 'Failure') initialize_logger('rootdir/Failure') logging.error("Either this file is empty or there are no lines") def move_to_success_folder_and_read(): filename, rootdir, lastmod, creation, filesize = fileinfo(file) os.mkdir('Success') copy_and_move_file(rootdir, 'Success') #file name print("Success", file) return file_len(file) if __name__ == '__main__': main(dirlist) From ian.g.kelly at gmail.com Thu Apr 2 20:21:53 2015 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 2 Apr 2015 18:21:53 -0600 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: <1905422.ubGH1B8UfE@PointedEars.de> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> Message-ID: On Thu, Apr 2, 2015 at 3:31 PM, Thomas 'PointedEars' Lahn wrote: > Ian Kelly wrote: > >> [?] Thomas 'PointedEars' Lahn [?] wrote: >>> Ian Kelly wrote: >>>> Within a grammar, the question of "is an X a Y" is nonsensical in >>>> isolation. It can only be answered in relation to a parse tree. >>>> Consider the simple grammar: >>>> >>>> S -> A | B >>>> A -> x >>>> B -> x >>>> >>>> Is x an A? It depends. >>> >>> No, by the definition 2 below, that we all accepted implicitly up to this >>> point, x is *definitely* an A. >> >> What gives you the impression that I ever accepted it? > > ,- > | > | What the grammar that you quoted from shows is that STRING+ is an > | expression. > > There is *no way* for you to make that statement if you did not accept > definition (2). Actually, there is a very simple way: I was being sloppily imprecise when I wrote that. First, I was speaking only in reference to the class of parse trees with more than one STRING, as that was the topic under discussion. I should have been clearer about that. Second, I never should have used the term "STRING+" there (or anywhere else in this discussion), as that merely clouded the point I was trying to offer. What I *meant* was that the complete sequence of produced STRINGs -- as opposed to any individual STRING -- is an expression, and I inappropriately used "STRING+" to denote that. (Maybe that is the point you were trying to make when you were talking about EBNF before.) >> This question of whether "x is an A" is informal and not a topic of formal >> language theory so far as I'm aware. Can you cite some source for it? > > No, because I was formalizing the ad-hoc definition by Chris Angelico in > . That URI is not useful to me as I don't use a newsreader. Is that the message dated Sun, 22 Mar 2015 14:36:48 +1100? I don't see any suggestion of this definition in that post. t>>> Now, according to these definitions, in the offered grammar x is *both* >>> an A and a B. Because what matters is _not_ the practical result of >>> production chains (the actual parse tree), but the certainty of the >>> theoretical possibility of it. >> >> This strikes me as being a lot like arguing, "some kites are toys, and >> some kites are birds; therefore, all kites are both toys and birds." > > False analogy again. We are discussing *in theory* a *formal* grammar. Its > goal symbols have *no meaning* except what can be produced from them. We're discussing a formal grammar as if it described a classification hierarchy, which gives meaning to statements like "A STRING is an expr". Otherwise, that statement is meaningless and can be neither true nor false. So I think that the analogy between one such hierarchy and another is apt. >> You're really going to make me spell it out, aren't you? Fine, here you >> go. >> >> single_input -> [?] -> expr -> [?] -> atom -> STRING STRING >> >> Note: the derivation contains exactly one expr node, which indirectly >> produces both STRINGs. Neither STRING in this derivation is >> individually produced from the expr. > > So you have proven that which nobody ever doubted nor requested, but I > pointed out already. As I tried to point out several posts back when I suggested that we were in agreement, and which you flatly denied. > What you have still not proven is what you claimed: > the parse tree. Because you misunderstood my claim. From ahlusar.ahluwalia at gmail.com Thu Apr 2 21:30:42 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Thu, 2 Apr 2015 18:30:42 -0700 (PDT) Subject: New to Programming: Adding custom functions with ipynotify classes Message-ID: <3c02ea7e-b562-40c1-99cf-6e9784f37e7b@googlegroups.com> Hello All: Here is the program that I am trying to write (with specs): * Monitors a folder for files that are dropped throughout the day * When a file is dropped in the folder the program should scan the file o IF all the records in the file have the same length (line length) o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records processed o IF the file is empty OR the records are not all of the same length o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). Many on forums suggest using ipynotify. I am wondering how to combine my current script and add it to the ipynotify. Below is my original script (the ipynotify script is provided after this) [code] # # # Without data to examine here, I can only guess based on this requirement's language that # # fixed records are in the input. ##I made the assumption that the directories are in the same filesystem # # Takes the function fileinfo as a starting point and demonstrates calling a function from within a function. # I tested this little sample on a small set of files created with MD5 checksums. I wrote the Python in such a way as it # would work with Python 2.x or 3.x (note the __future__ at the top). # # # There are so many wonderful ways of failure, so, from a development standpoint, I would probably spend a bit # # more time trying to determine which failure(s) I would want to report to the user, and how (perhaps creating my own Exceptions) # # # The only other comments I would make are about safe-file handling. # # # #1: Question: After a user has created a file that has failed (in # # # processing),can the user create a file with the same name? # # # If so, then you will probably want to look at some sort # # # of file-naming strategy to avoid overwriting evidence of # # # earlier failures. # # # File naming is a tricky thing. I referenced the tempfile module [1] and the Maildir naming scheme to see two different # # types of solutions to the problem of choosing a unique filename. ## I am assuming that all of my files are going to be specified in unicode ## Utilized Spyder's Scientific Computing IDE to debug, check for indentation errors and test function suite from __future__ import print_function import os.path import time import difflib import logging def initialize_logger(output_dir): logger = logging.getLogger() logger.setLevel(logging.DEBUG) # create console handler and set level to info handler = logging.StreamHandler() handler.setLevel(logging.INFO) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create error file handler and set level to error handler = logging.FileHandler(os.path.join(output_dir, "error.log"),"w", encoding=None, delay="true") handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) # create debug file handler and set level to debug handler = logging.FileHandler(os.path.join(output_dir, "all.log"),"w") handler.setLevel(logging.DEBUG) formatter = logging.Formatter("%(levelname)s - %(message)s") handler.setFormatter(formatter) logger.addHandler(handler) #This function's purpose is to obtain the filename, rootdir and filesize def fileinfo(f): filename = os.path.basename(f) rootdir = os.path.dirname(f) filesize = os.path.getsize(f) return filename, rootdir, filesize #This helper function returns the length of the file def file_len(f): with open(f) as f: for i, l in enumerate(f): pass return i + 1 #This helper function attempts to copy file and move file to the respective directory #I am assuming that the directories are in the same filesystem # If directories ARE in different file systems, I would use the following helper function: # def move(src, dest): # shutil.move(src, dest) def copy_and_move_file(src, dest): try: os.rename(src, dest) # eg. src and dest are the same file except IOError as e: print('Error: %s' % e.strerror) path = "." dirlist = os.listdir(path) # Caveats of the "main" function is that it does not scale well #(although it is appropriate if one assumes that there will be few changes) # It does not account for updated files existing in the directory - only new files "dropped" in # (If this was included in the requirements, os.stat would be appropriate here) def main(dirlist): before = dict([(f, 0) for f in dirlist]) while True: time.sleep(1) #time between update check after = dict([(f, None) for f in dirlist]) added = [f for f in after if not f in before] if added: f = ''.join(added) print('Sucessfully added %s file - ready to validate') %(f) return validate_files(f) else: return move_to_failure_folder_and_return_error_file(f) def validate_files(f): creation = time.ctime(os.path.getctime(f)) lastmod = time.ctime(os.path.getmtime(f)) if creation == lastmod and file_len(f) > 0: return move_to_success_folder_and_read(f) if file_len < 0 and creation != lastmod: return move_to_success_folder_and_read(f) else: return move_to_failure_folder_and_return_error_file(f) # Failure/Success Folder Functions def move_to_failure_folder_and_return_error_file(): filename, rootdir, lastmod, creation, filesize = fileinfo(file) os.mkdir('Failure') copy_and_move_file( 'Failure') initialize_logger('rootdir/Failure') logging.error("Either this file is empty or there are no lines") def move_to_success_folder_and_read(): filename, rootdir, lastmod, creation, filesize = fileinfo(file) os.mkdir('Success') copy_and_move_file(rootdir, 'Success') #file name print("Success", file) return file_len(file) if __name__ == '__main__': main(dirlist) [/code] Here is my ipynotify script that I have tried writing, following the tutorial: [code] # My version for w: monitors events and logs them into a log file. # import os.path from pyinotify import pyinotify timestamp = datetime.today() #time_record mask = pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO #watched events class EventHandler(pyinotify.ProcessEvent): def process_IN_CREATE(self, event): print "Created: %s " % os.path.join(event.path, event.name) event_log = open('/Users/sahluwalia/Desktop/', 'a') event_log.write(event.name + ' - ' + timestamp.strftime('%c') + '\n') event_log.close() def process_IN_MOVED_TO(self, event): print "Moved: %s " % os.path.join(event.path, event.name) event_log = open('/Users/sahluwalia/Desktop/', 'a') event_log.write(event.name + ' - ' + timestamp.strftime('%c') + '\n') event_log.close() handler = EventHandler() #instantiated EventHandler Class notifier = pyinotify.Notifier(wm, handler) class Watcher(pyinotify.ProcessEvent): #I haave modified the Watcher class to process and read a new file creation or added file watchdir = '/tmp/watch' def __init__(self): pyinotify.ProcessEvent.__init__(self) wm = pyinotify.WatchManager() self.notifier = pyinotify.ThreadedNotifier(wm, self) wdd = wm.add_watch(self.watchdir, pyinotify.EventsCodes.IN_CREATE) print "Watching", self.watchdir self.notifier.start() def process_IN_CREATE(self, event): print "Seen:", event pathname = os.path.join(event.path, event.name) pfile = self._parse(pathname) print(pfile) def process_IN_MOVED_TO(self, event): print "Moved: %s " % os.path.join(event.path, event.name) pathname = os.path.join(event.path, event.name) pfile = self._parse(pathname) print(pfile) def _parse(self, filename): f = open(filename) file = [line.strip() for line in f] f.close() return file class Log(pyinotify.ProcessEvent): def my_init(self, fileobj): """ Method automatically called from ProcessEvent.__init__(). Additional keyworded arguments passed to ProcessEvent.__init__() are then delegated to my_init(). This is the case for fileobj. """ self._fileobj = fileobj def process_default(self, event): self._fileobj.write(str(event) + '\n') self._fileobj.flush() class TrackModifications(pyinotify.ProcessEvent): def process_IN_MODIFY(self, event): print 'IN_MODIFY' class Empty(pyinotify.ProcessEvent): #Inherited class to display message def my_init(self, msg): self._msg = msg def process_default(self, event): #writes decribing the event print self._msg # pyinotify.log.setLevel(10) filelog = file('/Failure', 'w') while True: try: notifier.process_events() if notifier.check_events(): notifier.read_events() try: # It is important to pass named extra arguments like 'fileobj' handler = Empty(TrackModifications(Log(fileobj=filelog)), msg='This is an error message or notificaiton that will be logged ') notifier = pyinotify.Notifier(wm, default_proc_fun=handler) wm.add_watch('/tmp', pyinotify.ALL_EVENTS) notifier.loop() filelog.close() except KeyboardInterrupt: notifier.stop() break finally: filelog.close() if __name__ == '__main__': Watcher() [/code] From hayesstw at telkomsa.net Fri Apr 3 00:44:19 2015 From: hayesstw at telkomsa.net (Steve Hayes) Date: Fri, 03 Apr 2015 06:44:19 +0200 Subject: New to Programming - XML Processing References: Message-ID: On Thu, 2 Apr 2015 05:42:18 -0700, Albert-Jan Roskam wrote: > >----------------------------- >On Wed, Apr 1, 2015 10:00 AM CEST Mark Lawrence wrote: > >>On 01/04/2015 05:27, Andrew Farrell wrote: >>> You should follow Rustom's advice before just diving into the blog post >>> I linked to. Otherwise you risk blindly following things and losing your >>> bearings when you run into bugs. >>> >> >>Sound advice, but would you please be kind enough to intersperse your answers or bottom post as top posting is heavily frowned upon here. >> >>TIA. > >Would it be possible to use a script that checks every incoming mail to the Python mail list? Main ingredients beatfilsoup (to textify And that badly formatted posts like this are corrected for proper line length. -- Steve Hayes from Tshwane, South Africa Web: http://www.khanya.org.za/stevesig.htm Blog: http://khanya.wordpress.com E-mail - see web page, or parse: shayes at dunelm full stop org full stop uk From steve+comp.lang.python at pearwood.info Fri Apr 3 01:40:18 2015 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Fri, 03 Apr 2015 16:40:18 +1100 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> Message-ID: <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> On Friday 03 April 2015 08:31, Thomas 'PointedEars' Lahn wrote: > I am sorry that you cannot see that your argument is strewn with gaping > defects in logic, but I think I will stop trying to convince you of that > now. I'm sorry, I've been away for four days and have lost track of this thread. Have you reached an agreement about the number of angels which dance on the head of a pin yet, or will this argument continue for another week? This sterile, pointless arguing about the minutia of pedantic definitions is not even close to useful. Honestly Thomas, Ian, nobody cares any more. If I were a betting man, I'd bet that neither of you can describe, in one sentence, what is the actual disagreement between the two of you, let alone why it matters for Python programming. -- Steve From rosuav at gmail.com Fri Apr 3 01:57:57 2015 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 3 Apr 2015 16:57:57 +1100 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Apr 3, 2015 at 4:40 PM, Steven D'Aprano wrote: > This sterile, pointless arguing about the minutia of pedantic definitions is > not even close to useful. Honestly Thomas, Ian, nobody cares any more. If I > were a betting man, I'd bet that neither of you can describe, in one > sentence, what is the actual disagreement between the two of you, let alone > why it matters for Python programming. I know that it started in response to my statement that string literal concatenation wasn't an expression as such, but I have no idea what either side of the current debate is, nor how it affects my statement's validity. I _had_ hoped it would reach some sort of conclusion - I may be right, I may be wrong, but it'd be nice to know which. So far... no idea. ChrisA From steve+comp.lang.python at pearwood.info Fri Apr 3 02:02:35 2015 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Fri, 03 Apr 2015 17:02:35 +1100 Subject: generator/coroutine terminology References: <87y4mznmj8.fsf@elektro.pacujo.net> <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com> <551a9ebe$0$2987$e4fe514c@dreader35.news.xs4all.nl> Message-ID: <551e2cfd$0$11123$c3e8da3@news.astraweb.com> On Wednesday 01 April 2015 00:18, Albert van der Horst wrote: > In article <55062bda$0$12998$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: >>The biggest difference is syntactic. Here's an iterator which returns a >>never-ending sequence of squared numbers 1, 4, 9, 16, ... >> >>class Squares: >> def __init__(self): >> self.i = 0 >> def __next__(self): >> self.i += 1 >> return self.i**2 >> def __iter__(self): >> return self > > You should give an example of usage. As a newby I'm not up to > figuring out the specification from source for > something built of the mysterious __ internal > thingies. > (I did experiment with Squares interactively. But I didn't get > further than creating a Squares object.) Ah, sorry about that! Usage is: it = Squares() # create an iterator print(next(it)) # print the first value x = next(it) # extract the second while x < 100: print(x) x = next(it) Beware of doing this: for x in Squares(): print(x) since Squares is an *infinite* generator, it will continue for ever if you let it. Fortunately you can hit Ctrl-C to interrupt the for loop at any point. In Python 2, you will need to rename __next__ to just next without the double-leading-and-trailing underscores. >>Here's the same thing written as a generator: >> >>def squares(): >> i = 1 >> while True: >> yield i**2 >> i += 1 And for this one: it = squares() # create the iterator print(next(it)) # print the first value x = next(it) # extract the second while x < 100: print(x) x = next(it) Usage is pretty much exactly the same. -- Steve From ian.g.kelly at gmail.com Fri Apr 3 02:40:05 2015 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 3 Apr 2015 00:40:05 -0600 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Apr 2, 2015 at 11:40 PM, Steven D'Aprano wrote: > This sterile, pointless arguing about the minutia of pedantic definitions is > not even close to useful. Honestly Thomas, Ian, nobody cares any more. If I > were a betting man, I'd bet that neither of you can describe, in one > sentence, what is the actual disagreement between the two of you, let alone > why it matters for Python programming. Oh, either of us *could* describe it in one sentence; it's just that our descriptions of how we disagree would not agree. ;-) From no.email at nospam.invalid Fri Apr 3 02:46:42 2015 From: no.email at nospam.invalid (Paul Rubin) Date: Thu, 02 Apr 2015 23:46:42 -0700 Subject: generator/coroutine terminology References: <87y4mznmj8.fsf@elektro.pacujo.net> <55062bda$0$12998$c3e8da3$5496439d@news.astraweb.com> <551a9ebe$0$2987$e4fe514c@dreader35.news.xs4all.nl> Message-ID: <87h9sx90a5.fsf@jester.gateway.sonic.net> albert at spenarnc.xs4all.nl (Albert van der Horst) writes: > You should give an example of usage. As a newby I'm not up to > figuring out the specification from source for > something built of the mysterious __ internal > thingies. In reality because of generator expressions, the yield statement, and some useful built-in generators in the itertools module, you rarely have to use those special methods. I'd advise reading through the itertools module documentation from beginning to end, trying to understand what everything does. Even if some are not that useful, it will help convey the mode of thinking that went into these features. At a somewhat deeper level you might like the SICP book: http://mitpress.mit.edu/sicp/ It's somewhat old now and it's about Scheme rather than Python, but it builds up the relevant concepts quite nicely. Regarding the squares example, consider this even simpler generator: def count(n): while True: yield n n += 1 so count(1) yields 1, 2, 3, 4 ... This is a very useful generator but you don't need to write it since it's included in the itertools module as itertools.count. Its initial value defaults to 0. So the squares generator can be written: def squares(): return (i*i for i in itertools.count(0)) Now if you want all the squares less than 100 (i.e. 0, 1, 4, 9, ..., 81): wanted = itertools.takewhile(lambda x: x<100, squares()) You can print that out as a list: print(list(wanted)) The built-in sum function consumes an iterator, so you can add up the squares less than 100: print(sum(itertools.takewhile(lambda x: x<100, squares()))) this prints 205 which is 1+4+9+16+25+36+49+64+81. These features fit together quite elegantly and code like this flows off the fingertips naturally once you've used to it. From marko at pacujo.net Fri Apr 3 03:13:11 2015 From: marko at pacujo.net (Marko Rauhamaa) Date: Fri, 03 Apr 2015 10:13:11 +0300 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> Message-ID: <874mox3cs8.fsf@elektro.pacujo.net> Chris Angelico : > I know that it started in response to my statement that string literal > concatenation wasn't an expression as such, but I have no idea what > either side of the current debate is, nor how it affects my > statement's validity. This is what I have gathered: - A Python expression cannot directly follow another expression. A connector is required by the syntax. - That's untrue: * "abc" is an expression * "def" is an expression * "abc" "def" is an expression QED - Merging "abc" "def" is a matter of lexical analysis. - No, it's right there in the syntax definition. - Well, ok, however, the parse tree for "abc" "def" is not expr: expr: atom: string: "abc" expr: atom: string: "def" Rather, the correct parse tree is: expr: expr: atom: string: "abc" string: "def" Thus, a Python expression still is not directly following another expression. My own take: all sides are correct. Thomas is most correct and is being obnoxious about it. 1. A lexical analyzer *could* take care of concatenating string literals. It would have to be smart enough to handle intervening comments. On the other hand, the lexical analyzer is just a part of the parser; the line between them often is blurred. 2. The counterexample "abc" "def" *does* demonstrate that expressions can at times follow each other immediately. It is a nice point even if not all that consequential. Somewhat analogously: * ord is an expression * ("a") is an expression * ord("a") is an expression 3. Arguing about definitions is silly. Is 0 a natural number? Is 1 a prime number? Marko From __peter__ at web.de Fri Apr 3 06:45:17 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 03 Apr 2015 12:45:17 +0200 Subject: Strategy/ Advice for How to Best Attack this Problem? References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> Message-ID: Saran A wrote: > I debugged and rewrote everything. Here is the full version. Feel free to > tear this apart. The homework assignment is not due until tomorrow, so I > am currently also experimenting with pyinotify as well. Saran, try to make a realistic assessment of your capability. Your "debugged" code still has this gem > while True: > time.sleep(1) #time between update check and you are likely to miss your deadline anyway. While the inotify approach may be the "right way" to attack this problem from the point of view of an experienced developer like Chris as a newbie you should stick to the simplest thing that can possibly work. In a previous post I mentioned unit tests, but even an ad-hoc test in the interactive interpreter will show that your file_len() function doesn't work > #returns length of file > def file_len(f): > with open(f) as f: > for i, l in enumerate(f): > pass > return i + 1 > $ python Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> #returns length of file ... def file_len(f): ... with open(f) as f: ... for i, l in enumerate(f): ... pass ... return i + 1 ... >>> with open("tmp.txt", "w") as f: f.write("abcde") ... >>> file_len("tmp.txt") 1 It reports size 1 for every file. A simple unit test would look like this (assuming watchscript.py is the name of the script to be tested): import unittest from watchscript import file_len class FileLen(unittest.TestCase): def test_five(self): LEN = 5 FILENAME = "tmp.txt" with open(FILENAME, "w") as f: f.write("*" * LEN) self.assertEqual(file_len(FILENAME), 5) if __name__ == "__main__": unittest.main() Typically for every tested function you add a new TestCase subclass and for every test you perform for that function you add a test_...() method to that class. The unittest.main() will collect these methods, run them and generate a report. For the above example it will complain: $ python test_watchscript.py F ====================================================================== FAIL: test_five (__main__.FileLen) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_watchscript.py", line 12, in test_five self.assertEqual(file_len(FILENAME), 5) AssertionError: 1 != 5 ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (failures=1) The file_len() function is but an example of the many problems with your code. There is no shortcut, you have to decide for every function what exactly it should do and then verify that it actually does what it is meant to do. Try to negotiate an extra week or two with your supervisor and then start small: On day one make a script that moves all files from one directory to another. Make sure that the source and destination directory are only stored in one place in your script so that they can easily be replaced. Run the script after every change and devise tests that demonstrate that it works correctly. On day two make a script that checks all files in a directory. Put the checks into a function that takes a file path as its only parameter. Write a few correct and a few broken files and test that only the correct ones are recognized as correct, and only the broken ones are flagged as broken, and that all files in the directory are checked. On day three make a script that combines the above and only moves the correct files into another directory. Devise tests to verify that both directories contain the expected files before and after your script is executed. On day four make a script that also moves the bad files into another directory. Modify your tests from the previous day to check the contents of all three directories. On day five wrap your previous efforts in a loop that runs forever. I seems to work? You are not done. Write tests that demonstrate that it does work. Try to think of the corner cases: what if there are no new files on one iteration? What if there is a new file with the same name as a previous one? Don't handwave, describe the reaction of your script in plain English with all the gory details and then translate into Python. Heureka! Note that a "day" in the above outline can be 15 minutes or a week. You're done when you're done. Also note that the amount of code you have written bears no indication of how close you are to your goal. An experienced programmer will end up with less code than you to fulfill the same spec. That shouldn't bother you. On the other hand you should remove code that doesn't contribute to your script's goal immediately. If you keep failed attempts and side tracks your code will become harder and harder to maintain, and that's the last thing you need when it's already hard to get the necessary parts right. Good luck! From steve+comp.lang.python at pearwood.info Fri Apr 3 07:37:29 2015 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Fri, 03 Apr 2015 22:37:29 +1100 Subject: New to Programming: Adding custom functions with ipynotify classes References: <3c02ea7e-b562-40c1-99cf-6e9784f37e7b@googlegroups.com> Message-ID: <551e7b7a$0$13012$c3e8da3$5496439d@news.astraweb.com> On Fri, 3 Apr 2015 12:30 pm, Saran A wrote: > Hello All: > > Here is the program that I am trying to write (with specs): [...] Do you have an actual question? If you want a code review of your entire application, the polite thing to do is to ask for volunteers first, before dropping 200+ lines of code on us. If you have a specific question you would like to ask, then it is good to focus on that issue alone, not your entire application. I see you say this: > Many on forums suggest using ipynotify. I am wondering how to combine my > current script and add it to the ipynotify. Have you read the examples and followed the tutorial? https://github.com/seb-m/pyinotify/wiki/List-of-Examples https://github.com/seb-m/pyinotify/wiki/Tutorial By the way, I have found at least one bug in your code: > #This helper function returns the length of the file > def file_len(f): > with open(f) as f: > for i, l in enumerate(f): > pass > return i + 1 Not as given it doesn't. It will raise an exception if the file cannot be opened, and it will return 1 for any file you can read. After you fix the incorrect indentation, the function is horribly inefficient. Instead, use this: import os os.stat(filename).st_size -- Steven From d at davea.name Fri Apr 3 07:59:28 2015 From: d at davea.name (Dave Angel) Date: Fri, 03 Apr 2015 07:59:28 -0400 Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> Message-ID: <551E80A0.9070205@davea.name> On 04/02/2015 07:43 PM, Saran A wrote: > > I debugged and rewrote everything. Here is the full version. Feel free to tear this apart. The homework assignment is not due until tomorrow, so I am currently also experimenting with pyinotify as well. I do have questions regarding how to make this function compatible with the ProcessEvent Class. I will create another post for this. > > What would you advise in regards to renaming the inaptly named dirlist? Asked and answered. You called it path at one point. dir_name would also be good. The point is that if you have a good name, you're less likely to have unreasonable code processing that name. For example, > > # # # Without data to examine here, I can only guess based on this requirement's language that > # # fixed records are in the input. If so, here's a slight revision to the helper functions that I wrote earlier which > # # takes the function fileinfo as a starting point and demonstrates calling a function from within a function. > # I tested this little sample on a small set of files created with MD5 checksums. I wrote the Python in such a way as it > # would work with Python 2.x or 3.x (note the __future__ at the top). > > # # # There are so many wonderful ways of failure, so, from a development standpoint, I would probably spend a bit > # # more time trying to determine which failure(s) I would want to report to the user, and how (perhaps creating my own Exceptions) > > # # # The only other comments I would make are about safe-file handling. > > # # # #1: Question: After a user has created a file that has failed (in > # # # processing),can the user create a file with the same name? > # # # If so, then you will probably want to look at some sort > # # # of file-naming strategy to avoid overwriting evidence of > # # # earlier failures. > > # # # File naming is a tricky thing. I referenced the tempfile module [1] and the Maildir naming scheme to see two different > # # types of solutions to the problem of choosing a unique filename. >> ## I am assuming that all of my files are going to be specified in unicode > > ## Utilized Spyder's Scientific Computing IDE to debug, check for indentation errors and test function suite > > from __future__ import print_function > > import os.path > import time > import logging > > > def initialize_logger(output_dir): I didn't ever bother to read the body of this function, since a simple print(mydata, file=mylog_file) will suffice to add data to the chosen text file. Why is it constantly getting more complex, to solve a problem that was simple in the beginning? > > > #Returns filename, rootdir and filesize > > def fileinfo(f): > filename = os.path.basename(f) > rootdir = os.path.dirname(f) > filesize = os.path.getsize(f) > return filename, rootdir, filesize > > #returns length of file > def file_len(f): > with open(f) as f: > for i, l in enumerate(f): > pass > return i + 1 Always returns 1 or None. Check the indentation, and test to see what it does for empty file, for a file with one line, and for a file with more than one line. > #attempts to copy file and move file to it's directory > def copy_and_move_file(src, dest): Which is it, are you trying to copy it, or move it? Pick one and make a good function name that shows your choice. > try: > os.rename(src, dest) Why are you using rename, when you're trying to move the file? Take a closer look at shutil, and see if it has a function that does it safer than rename. The function you need uses rename, when it'll work, and does it other ways when rename will not. > # eg. src and dest are the same file > except IOError as e: > print('Error: %s' % e.strerror) A try/except that doesn't try to correct the problem is not generally useful. Figure out what could be triggering the exception, and how you're going to handle it. If it cannot be handled, terminate the program. For example, what if you don't have permissions to modify one of the specified directories? You can't get any useful work done, so you should notify the user and exit. An alternative is to produce 50 thousand reports of each file you've got, telling how it succeeded or failed, over and over. > > path = "." > dirlist = os.listdir(path) > > def main(dirlist): > before = dict([(f, 0) for f in dirlist]) Since dirlist is a path, it's a string. So you're looping through the characters of the name of the path. I still don't have a clue what the dict is supposed to mean here. > while True: > time.sleep(1) #time between update check That loop goes forever, so the following code will never run. > after = dict([(f, None) for f in dirlist]) Once again, you're looping through the letters of the directory name. Or if dirlist is really a list, and you're deciding that's what it should be, then of course after will be identical to before. > added = [f for f in after if not f in before] > if added: > f = ''.join(added) > print('Sucessfully added %s file - ready to validate') %() > return validate_files(f) > else: > return move_to_failure_folder_and_return_error_file(f) > > > > def validate_files(f): You completely changed the scope of this function. Why does it still have an s in its name? > creation = time.ctime(os.path.getctime(f)) > lastmod = time.ctime(os.path.getmtime(f)) What do timestamps have to do with anything? > if creation == lastmod and file_len(f) > 0: > return move_to_success_folder_and_read(f) How have you decided that the records are all the same length? > if file_len < 0 and creation != lastmod: file_len cannot be safely compared to 0, it's a function. If this were Python 3, it would give you a runtime error for trying. Even if you had successfully called the function here, how is it possible for it to return a negative value? > return move_to_success_folder_and_read(f) > else: > return move_to_failure_folder_and_return_error_file(f) > > > #Potential Additions/Substitutions > > def move_to_failure_folder_and_return_error_file(): > filename, rootdir, lastmod, creation, filesize = fileinfo(file) fileinfo() returns 3 things, and you're stuffing them in 5 variables. > os.mkdir('Failure') What do you do the second time through this function, when that directory is already existing? > copy_and_move_file( 'Failure') The function takes two arguments, neither of which is likely to be that string. > initialize_logger('rootdir/Failure') > logging.error("Either this file is empty or there are no lines") > > def move_to_success_folder_and_read(): > filename, rootdir, lastmod, creation, filesize = fileinfo(file) > os.mkdir('Success') > copy_and_move_file(rootdir, 'Success') #file name > print("Success", file) > return file_len(file) > > > > if __name__ == '__main__': > main(dirlist) So now you're passing a list of filenames to main()? How then is it going to know when new files arrive? You were originally going to tell it a directory name. That's why I suggested that you call the parameter 'path' or 'dirname'. -- DaveA From davea at davea.name Fri Apr 3 08:12:38 2015 From: davea at davea.name (Dave Angel) Date: Fri, 03 Apr 2015 08:12:38 -0400 Subject: New to Programming: Adding custom functions with ipynotify classes In-Reply-To: <551e7b7a$0$13012$c3e8da3$5496439d@news.astraweb.com> References: <3c02ea7e-b562-40c1-99cf-6e9784f37e7b@googlegroups.com> <551e7b7a$0$13012$c3e8da3$5496439d@news.astraweb.com> Message-ID: <551E83B6.2010604@davea.name> On 04/03/2015 07:37 AM, Steven D'Aprano wrote: > On Fri, 3 Apr 2015 12:30 pm, Saran A wrote: > >> #This helper function returns the length of the file >> def file_len(f): >> with open(f) as f: >> for i, l in enumerate(f): >> pass >> return i + 1 > > Not as given it doesn't. It will raise an exception if the file cannot be > opened, and it will return 1 for any file you can read. > > After you fix the incorrect indentation, the function is horribly > inefficient. Instead, use this: > > import os > os.stat(filename).st_size > > No, he actually wants the number of records in the file. So he still needs to read the whole file. Naturally I figured that out from the phrase in the spec: "indicating the total number of records processed" not from the comment in the code. -- DaveA From ahlusar.ahluwalia at gmail.com Fri Apr 3 08:50:28 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Fri, 3 Apr 2015 05:50:28 -0700 (PDT) Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> Message-ID: On Friday, April 3, 2015 at 8:05:14 AM UTC-4, Dave Angel wrote: > On 04/02/2015 07:43 PM, Saran A wrote: > > > > > I debugged and rewrote everything. Here is the full version. Feel free to tear this apart. The homework assignment is not due until tomorrow, so I am currently also experimenting with pyinotify as well. I do have questions regarding how to make this function compatible with the ProcessEvent Class. I will create another post for this. > > > > What would you advise in regards to renaming the inaptly named dirlist? > > Asked and answered. You called it path at one point. dir_name would > also be good. The point is that if you have a good name, you're less > likely to have unreasonable code processing that name. For example, > > > > > # # # Without data to examine here, I can only guess based on this requirement's language that > > # # fixed records are in the input. If so, here's a slight revision to the helper functions that I wrote earlier which > > # # takes the function fileinfo as a starting point and demonstrates calling a function from within a function. > > # I tested this little sample on a small set of files created with MD5 checksums. I wrote the Python in such a way as it > > # would work with Python 2.x or 3.x (note the __future__ at the top). > > > > # # # There are so many wonderful ways of failure, so, from a development standpoint, I would probably spend a bit > > # # more time trying to determine which failure(s) I would want to report to the user, and how (perhaps creating my own Exceptions) > > > > # # # The only other comments I would make are about safe-file handling. > > > > # # # #1: Question: After a user has created a file that has failed (in > > # # # processing),can the user create a file with the same name? > > # # # If so, then you will probably want to look at some sort > > # # # of file-naming strategy to avoid overwriting evidence of > > # # # earlier failures. > > > > # # # File naming is a tricky thing. I referenced the tempfile module [1] and the Maildir naming scheme to see two different > > # # types of solutions to the problem of choosing a unique filename. > >> ## I am assuming that all of my files are going to be specified in unicode > > > > ## Utilized Spyder's Scientific Computing IDE to debug, check for indentation errors and test function suite > > > > from __future__ import print_function > > > > import os.path > > import time > > import logging > > > > > > def initialize_logger(output_dir): > > > I didn't ever bother to read the body of this function, since a simple > > print(mydata, file=mylog_file) > > will suffice to add data to the chosen text file. Why is it constantly > getting more complex, to solve a problem that was simple in the beginning? > > > > > > > #Returns filename, rootdir and filesize > > > > def fileinfo(f): > > filename = os.path.basename(f) > > rootdir = os.path.dirname(f) > > filesize = os.path.getsize(f) > > return filename, rootdir, filesize > > > > #returns length of file > > def file_len(f): > > with open(f) as f: > > for i, l in enumerate(f): > > pass > > return i + 1 > > Always returns 1 or None. Check the indentation, and test to see what > it does for empty file, for a file with one line, and for a file with > more than one line. > > > > #attempts to copy file and move file to it's directory > > def copy_and_move_file(src, dest): > > Which is it, are you trying to copy it, or move it? Pick one and make a > good function name that shows your choice. > > > try: > > os.rename(src, dest) > > Why are you using rename, when you're trying to move the file? Take a > closer look at shutil, and see if it has a function that does it safer > than rename. The function you need uses rename, when it'll work, and > does it other ways when rename will not. > > > # eg. src and dest are the same file > > except IOError as e: > > print('Error: %s' % e.strerror) > > A try/except that doesn't try to correct the problem is not generally > useful. Figure out what could be triggering the exception, and how > you're going to handle it. If it cannot be handled, terminate the program. > > For example, what if you don't have permissions to modify one of the > specified directories? You can't get any useful work done, so you > should notify the user and exit. An alternative is to produce 50 > thousand reports of each file you've got, telling how it succeeded or > failed, over and over. > > > > > path = "." > > dirlist = os.listdir(path) > > > > def main(dirlist): > > before = dict([(f, 0) for f in dirlist]) > > Since dirlist is a path, it's a string. So you're looping through the > characters of the name of the path. I still don't have a clue what the > dict is supposed to mean here. > > > while True: > > time.sleep(1) #time between update check > > That loop goes forever, so the following code will never run. > > > after = dict([(f, None) for f in dirlist]) > > Once again, you're looping through the letters of the directory name. > Or if dirlist is really a list, and you're deciding that's what it > should be, then of course after will be identical to before. > > > added = [f for f in after if not f in before] > > if added: > > f = ''.join(added) > > print('Sucessfully added %s file - ready to validate') %() > > return validate_files(f) > > else: > > return move_to_failure_folder_and_return_error_file(f) > > > > > > > > def validate_files(f): > > You completely changed the scope of this function. Why does it still > have an s in its name? > > > creation = time.ctime(os.path.getctime(f)) > > lastmod = time.ctime(os.path.getmtime(f)) > > What do timestamps have to do with anything? > > > if creation == lastmod and file_len(f) > 0: > > return move_to_success_folder_and_read(f) > > How have you decided that the records are all the same length? > > > if file_len < 0 and creation != lastmod: > > file_len cannot be safely compared to 0, it's a function. If this were > Python 3, it would give you a runtime error for trying. Even if you had > successfully called the function here, how is it possible for it to > return a negative value? > > > return move_to_success_folder_and_read(f) > > else: > > return move_to_failure_folder_and_return_error_file(f) > > > > > > #Potential Additions/Substitutions > > > > def move_to_failure_folder_and_return_error_file(): > > filename, rootdir, lastmod, creation, filesize = fileinfo(file) > > fileinfo() returns 3 things, and you're stuffing them in 5 variables. > > > os.mkdir('Failure') > > What do you do the second time through this function, when that > directory is already existing? > > > copy_and_move_file( 'Failure') > > The function takes two arguments, neither of which is likely to be that > string. > > > initialize_logger('rootdir/Failure') > > logging.error("Either this file is empty or there are no lines") > > > > > > def move_to_success_folder_and_read(): > > filename, rootdir, lastmod, creation, filesize = fileinfo(file) > > os.mkdir('Success') > > copy_and_move_file(rootdir, 'Success') #file name > > print("Success", file) > > return file_len(file) > > > > > > > > if __name__ == '__main__': > > main(dirlist) > > So now you're passing a list of filenames to main()? How then is it > going to know when new files arrive? You were originally going to tell > it a directory name. That's why I suggested that you call the parameter > 'path' or 'dirname'. > > > > -- > DaveA @DaveA I addressed most of the issues. I do admit that, as a novice, I feel beholden to the computer - hence the over-engineering. As you correctly stated: " > What do you do the second time through this function, when that > directory is already existing? > > > copy_and_move_file( 'Failure') > > The function takes two arguments, neither of which is likely to be that > string. > > > initialize_logger('rootdir/Failure') > > logging.error("Either this file is empty or there are no lines")" How would I ensure that this s directory is made only once and every file that is passeed goes only to 'success' or 'failure'? From rustompmody at gmail.com Fri Apr 3 08:52:17 2015 From: rustompmody at gmail.com (Rustom Mody) Date: Fri, 3 Apr 2015 05:52:17 -0700 (PDT) Subject: r"\"" ??? (was A simple single line, triple-quoted comment) In-Reply-To: <874mox3cs8.fsf@elektro.pacujo.net> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> <874mox3cs8.fsf@elektro.pacujo.net> Message-ID: <55a914a0-5901-4be5-9a35-74435ec060d1@googlegroups.com> On Friday, April 3, 2015 at 12:43:32 PM UTC+5:30, Marko Rauhamaa wrote: > 3. Arguing about definitions is silly. Is 0 a natural number? Is 1 a > prime number? Speaking about silliness of definitions, I was knocked out in class by this today: >>> r"\"" '\\"' Seeing the docs https://docs.python.org/3.4/reference/lexical_analysis.html#string-and-bytes-literals it talks of this explicitly But I still find it strange -- I would have expected this kind of error behavior: >>> "A string" "another incomplete SyntaxError: EOL while scanning string literal >>> From ahlusar.ahluwalia at gmail.com Fri Apr 3 09:40:45 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Fri, 3 Apr 2015 06:40:45 -0700 (PDT) Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> Message-ID: <149a7683-15a0-4fa6-882f-4ff8982e9e0c@googlegroups.com> On Friday, April 3, 2015 at 6:46:21 AM UTC-4, Peter Otten wrote: > Saran A wrote: > > > I debugged and rewrote everything. Here is the full version. Feel free to > > tear this apart. The homework assignment is not due until tomorrow, so I > > am currently also experimenting with pyinotify as well. > > Saran, try to make a realistic assessment of your capability. Your > "debugged" code still has this gem > > > while True: > > time.sleep(1) #time between update check > > and you are likely to miss your deadline anyway. > > While the inotify approach may be the "right way" to attack this problem > from the point of view of an experienced developer like Chris as a newbie > you should stick to the simplest thing that can possibly work. > > In a previous post I mentioned unit tests, but even an ad-hoc test in the > interactive interpreter will show that your file_len() function doesn't work > > > #returns length of file > > def file_len(f): > > with open(f) as f: > > for i, l in enumerate(f): > > pass > > return i + 1 > > > > $ python > Python 2.7.6 (default, Mar 22 2014, 22:59:56) > [GCC 4.8.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> #returns length of file > ... def file_len(f): > ... with open(f) as f: > ... for i, l in enumerate(f): > ... pass > ... return i + 1 > ... > >>> with open("tmp.txt", "w") as f: f.write("abcde") > ... > >>> file_len("tmp.txt") > 1 > > It reports size 1 for every file. > > A simple unit test would look like this (assuming watchscript.py is the name > of the script to be tested): > > import unittest > > from watchscript import file_len > > class FileLen(unittest.TestCase): > def test_five(self): > LEN = 5 > FILENAME = "tmp.txt" > with open(FILENAME, "w") as f: > f.write("*" * LEN) > > self.assertEqual(file_len(FILENAME), 5) > > if __name__ == "__main__": > unittest.main() > > Typically for every tested function you add a new TestCase subclass and for > every test you perform for that function you add a test_...() method to that > class. The unittest.main() will collect these methods, run them and generate > a report. For the above example it will complain: > > $ python test_watchscript.py > F > ====================================================================== > FAIL: test_five (__main__.FileLen) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "test_watchscript.py", line 12, in test_five > self.assertEqual(file_len(FILENAME), 5) > AssertionError: 1 != 5 > > ---------------------------------------------------------------------- > Ran 1 test in 0.001s > > FAILED (failures=1) > > > The file_len() function is but an example of the many problems with your > code. There is no shortcut, you have to decide for every function what > exactly it should do and then verify that it actually does what it is meant > to do. Try to negotiate an extra week or two with your supervisor and then > start small: > > On day one make a script that moves all files from one directory to another. > Make sure that the source and destination directory are only stored in one > place in your script so that they can easily be replaced. Run the script > after every change and devise tests that demonstrate that it works > correctly. > > On day two make a script that checks all files in a directory. Put the > checks into a function that takes a file path as its only parameter. > Write a few correct and a few broken files and test that only the correct > ones are recognized as correct, and only the broken ones are flagged as > broken, and that all files in the directory are checked. > > On day three make a script that combines the above and only moves the > correct files into another directory. Devise tests to verify that both > directories contain the expected files before and after your script is > executed. > > On day four make a script that also moves the bad files into another > directory. Modify your tests from the previous day to check the contents of > all three directories. > > On day five wrap your previous efforts in a loop that runs forever. > I seems to work? You are not done. Write tests that demonstrate that it does > work. Try to think of the corner cases: what if there are no new files on > one iteration? What if there is a new file with the same name as a previous > one? Don't handwave, describe the reaction of your script in plain English > with all the gory details and then translate into Python. > > Heureka! > > Note that a "day" in the above outline can be 15 minutes or a week. You're > done when you're done. Also note that the amount of code you have written > bears no indication of how close you are to your goal. An experienced > programmer will end up with less code than you to fulfill the same spec. > That shouldn't bother you. On the other hand you should remove code that > doesn't contribute to your script's goal immediately. If you keep failed > attempts and side tracks your code will become harder and harder to > maintain, and that's the last thing you need when it's already hard to get > the necessary parts right. > > Good luck! @Peter: Thank you emphasizing unit testing. I will be sure to be more mindful regarding this moving forward. FYI I addressed those "gems" :) It's amazing what some sleep can do. From rosuav at gmail.com Fri Apr 3 10:40:10 2015 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 4 Apr 2015 01:40:10 +1100 Subject: r"\"" ??? (was A simple single line, triple-quoted comment) In-Reply-To: <55a914a0-5901-4be5-9a35-74435ec060d1@googlegroups.com> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> <874mox3cs8.fsf@elektro.pacujo.net> <55a914a0-5901-4be5-9a35-74435ec060d1@googlegroups.com> Message-ID: On Fri, Apr 3, 2015 at 11:52 PM, Rustom Mody wrote: > Speaking about silliness of definitions, I was knocked out in class by this today: > >>>> r"\"" > '\\"' > > Seeing the docs > https://docs.python.org/3.4/reference/lexical_analysis.html#string-and-bytes-literals > it talks of this explicitly > > But I still find it strange -- I would have expected this kind of error behavior: > >>>> "A string" "another incomplete > SyntaxError: EOL while scanning string literal >>>> I'm sorry, I'm not understanding your confusion here. A raw string literal allows escaped quote characters - is that the problem? ChrisA From rustompmody at gmail.com Fri Apr 3 11:14:17 2015 From: rustompmody at gmail.com (Rustom Mody) Date: Fri, 3 Apr 2015 08:14:17 -0700 (PDT) Subject: r"\"" ??? (was A simple single line, triple-quoted comment) In-Reply-To: References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> <874mox3cs8.fsf@elektro.pacujo.net> <55a914a0-5901-4be5-9a35-74435ec060d1@googlegroups.com> Message-ID: <11da5626-e656-4bf4-b0c3-b092508daa56@googlegroups.com> On Friday, April 3, 2015 at 8:10:54 PM UTC+5:30, Chris Angelico wrote: > On Fri, Apr 3, 2015 at 11:52 PM, Rustom Mody wrote: > > Speaking about silliness of definitions, I was knocked out in class by this today: > > > >>>> r"\"" > > '\\"' > > > > Seeing the docs > > https://docs.python.org/3.4/reference/lexical_analysis.html#string-and-bytes-literals > > it talks of this explicitly > > > > But I still find it strange -- I would have expected this kind of error behavior: > > > >>>> "A string" "another incomplete > > SyntaxError: EOL while scanning string literal > >>>> > > I'm sorry, I'm not understanding your confusion here. A raw string > literal allows escaped quote characters - is that the problem? > > ChrisA Yeah... I guess you could call it that. Raw? half-cooked? Or double-cooked? # This looks properly raw >>> r"\a" '\\a' >>> # This not so surprising >>> r"\\" '\\\\' # Thats 2 not 4 backslashes - still ok >>> len(r"\\") 2 # Until you try this >>> r"\" SyntaxError: EOL while scanning string literal #... which seems to be 'correctable' by >>> r"\"" '\\"' >>> In short I see some behavior What's the pattern or intention I dont get. Does a backslash escape?? Seems to depend on the moonphase? >>> print ("\n") >>> print (r"\n") \n ------- ? Worsened by eclipse tomorrow From johnnybureddu at gmx.com Fri Apr 3 11:25:31 2015 From: johnnybureddu at gmx.com (REVOLUCION!) Date: Fri, 3 Apr 2015 08:25:31 -0700 (PDT) Subject: -- redacted -- Message-ID: <8c349d11-cc30-4f85-a1ed-e8312d05f9bf@googlegroups.com> -- redacted -- From ian.g.kelly at gmail.com Fri Apr 3 11:25:57 2015 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 3 Apr 2015 09:25:57 -0600 Subject: A simple single line, triple-quoted comment is giving syntax error. Why? In-Reply-To: <874mox3cs8.fsf@elektro.pacujo.net> References: <3533816.ZYnZ2OzjCs@PointedEars.de> <3971951.908YQu3oQO@PointedEars.de> <1504323.jUKfeKbQsP@PointedEars.de> <2362875.FIK8ImHTJA@PointedEars.de> <1823599.Pr2V0MPR6q@PointedEars.de> <9781856.5cutuNVPEq@PointedEars.de> <3894022.plrvF2nHWu@PointedEars.de> <1905422.ubGH1B8UfE@PointedEars.de> <551e27c5$0$12904$c3e8da3$5496439d@news.astraweb.com> <874mox3cs8.fsf@elektro.pacujo.net> Message-ID: On Fri, Apr 3, 2015 at 1:13 AM, Marko Rauhamaa wrote: > 2. The counterexample "abc" "def" *does* demonstrate that expressions > can at times follow each other immediately. It is a nice point even > if not all that consequential. > > Somewhat analogously: > * ord is an expression > * ("a") is an expression > * ord("a") is an expression I'm tired of this also, so I'll make this response short. By analogy to English: * "far" is an English adverb. * "lands" is an English noun. * "far lands" is an English noun phrase. * Therefore, a noun phrase in English can consist of an adverb immediately followed by a noun that is modified by the adverb The fallacy here is that the "far" in "far lands" is used as an adjective, not an adverb. I think that the same fallacy applies to the Python expressions above. If you disagree, that's fine; I'll let it go. From joepureddu at yahoo.com Fri Apr 3 11:38:46 2015 From: joepureddu at yahoo.com (Dieffe Diciotto) Date: Fri, 3 Apr 2015 08:38:46 -0700 (PDT) Subject: -- redacted -- In-Reply-To: <7d471aa5-b6ed-4800-9855-37319ba126ee@googlegroups.com> References: <7d471aa5-b6ed-4800-9855-37319ba126ee@googlegroups.com> Message-ID: <2b5250f8-f9f1-4ed4-8bf3-cf4cdfdfc0e9@googlegroups.com> -- redacted -- From gabriele.silvagn at tiscali.it Fri Apr 3 12:37:33 2015 From: gabriele.silvagn at tiscali.it (silvagni) Date: Fri, 3 Apr 2015 18:37:33 +0200 Subject: Defamation Message-ID: <580B262E-F8F4-48D8-8215-00B795D8B129@tiscali.it> Please remove the page: https://mail.python.org/pipermail/python-list/2014-September/691616.html Thank You Gabriele Silvagni From denismfmcmahon at gmail.com Fri Apr 3 12:39:06 2015 From: denismfmcmahon at gmail.com (Denis McMahon) Date: Fri, 3 Apr 2015 16:39:06 +0000 (UTC) Subject: New to Programming: Adding custom functions with ipynotify classes References: <3c02ea7e-b562-40c1-99cf-6e9784f37e7b@googlegroups.com> Message-ID: On Thu, 02 Apr 2015 18:30:42 -0700, Saran A wrote: > Here is the program that I am trying to write (with specs): Saran, please stop prefacing every subject with "New to programming:" - it does not give an clue whatsoever as to what your post is about. -- Denis McMahon, denismfmcmahon at gmail.com From ahlusar.ahluwalia at gmail.com Fri Apr 3 13:56:10 2015 From: ahlusar.ahluwalia at gmail.com (Saran A) Date: Fri, 3 Apr 2015 10:56:10 -0700 (PDT) Subject: New to Programming: Adding custom functions with ipynotify classes In-Reply-To: References: <3c02ea7e-b562-40c1-99cf-6e9784f37e7b@googlegroups.com> Message-ID: On Friday, April 3, 2015 at 12:40:11 PM UTC-4, Denis McMahon wrote: > On Thu, 02 Apr 2015 18:30:42 -0700, Saran A wrote: > > > Here is the program that I am trying to write (with specs): > > Saran, please stop prefacing every subject with "New to programming:" - > it does not give an clue whatsoever as to what your post is about. > > -- > Denis McMahon, denismfmcmahon at gmail.com @Denis Thank you for your feedback From davea at davea.name Fri Apr 3 16:21:11 2015 From: davea at davea.name (Dave Angel) Date: Fri, 03 Apr 2015 16:21:11 -0400 Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> Message-ID: <551EF637.80801@davea.name> On 04/03/2015 08:50 AM, Saran A wrote: > On Friday, April 3, 2015 at 8:05:14 AM UTC-4, Dave Angel wrote: >> On 04/02/2015 07:43 PM, Saran A wrote: > > I addressed most of the issues. I do admit that, as a novice, I feel beholden to the computer - hence the over-engineering. > Should be quite the opposite. As a novice, you ought to be testing the heck out of your functions, worrying about whether they are properly named, properly commented, and properly tested. >>> os.mkdir('Success') > As you correctly stated: > > " >> What do you do the second time through this function, when that >> directory is already existing? >> >>> copy_and_move_file( 'Failure') >> >> The function takes two arguments, neither of which is likely to be that >> string. >> >>> initialize_logger('rootdir/Failure') >>> logging.error("Either this file is empty or there are no lines")" > > > How would I ensure that this s directory is made only once and every file that is passeed goes only to 'success' or 'failure'? > Well, you could use an if clause checking with os.exist(). If the directory already exists, don't call the mkdir function. That may not be perfect, but it should suffice for an assignment at your level. Alternatively, you could set a global variable equal to 'Failure' or whatever the full path to the directory is going to be, and do a mkdir at the beginning of main(). Likewise for success directory, and the output text file. In that case, of course, instead of creating the directory, you open the file (for append, of course, so the next run of the program doesn't trash the file), and keep the file handle handy. -- DaveA From tjreedy at udel.edu Fri Apr 3 17:58:18 2015 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 03 Apr 2015 17:58:18 -0400 Subject: Defamation In-Reply-To: <580B262E-F8F4-48D8-8215-00B795D8B129@tiscali.it> References: <580B262E-F8F4-48D8-8215-00B795D8B129@tiscali.it> Message-ID: <551F0CFA.9050908@udel.edu> On 4/3/2015 12:37 PM, silvagni wrote: > Please remove the page: > https://mail.python.org/pipermail/python-list/2014-September/691616.html > > Thank You > Gabriele Silvagni You can try sending mail to python-list-owner at python.org, but it will not do much good. Even if a post could be removed from python.org, which I do not believe will be done, the list is mirrored at news.gmane.org, google groups, comp.lang.python (multiple news servers worldwide), and elsewhere I believe. It is also indexed by search engines. Sorry. -- Terry Jan Reedy From ben+python at benfinney.id.au Fri Apr 3 18:18:25 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 04 Apr 2015 09:18:25 +1100 Subject: Defamation References: <580B262E-F8F4-48D8-8215-00B795D8B129@tiscali.it> Message-ID: <857fts26vi.fsf@benfinney.id.au> silvagni writes: > Please remove the page: > https://mail.python.org/pipermail/python-list/2014-September/691616.html > > Thank You > Gabriele Silvagni You have given no motivation for us to do so. A bald request to remove a message is unlikely to be actioned. Also, you are addressing the wrong people; we are participants, not administrators, of this forum. If you want to make a complaint to the administrators of this forum, please read , the end of the page shows how to contact the administrators. -- \ ?? whoever claims any right that he is unwilling to accord to | `\ his fellow-men is dishonest and infamous.? ?Robert G. | _o__) Ingersoll, _The Liberty of Man, Woman and Child_, 1877 | Ben Finney From breamoreboy at yahoo.co.uk Fri Apr 3 19:47:49 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 04 Apr 2015 00:47:49 +0100 Subject: Defamation In-Reply-To: <580B262E-F8F4-48D8-8215-00B795D8B129@tiscali.it> References: <580B262E-F8F4-48D8-8215-00B795D8B129@tiscali.it> Message-ID: On 03/04/2015 17:37, silvagni wrote: > Please remove the page: > https://mail.python.org/pipermail/python-list/2014-September/691616.html > > Thank You > Gabriele Silvagni > I mark all of these as offensive on google groups. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From cs at zip.com.au Fri Apr 3 21:31:34 2015 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 4 Apr 2015 12:31:34 +1100 Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: <551EF637.80801@davea.name> References: <551EF637.80801@davea.name> Message-ID: <20150404013134.GA11202@cskk.homeip.net> On 03Apr2015 16:21, Dave Angel wrote: >On 04/03/2015 08:50 AM, Saran A wrote: >>On Friday, April 3, 2015 at 8:05:14 AM UTC-4, Dave Angel wrote: >>>On 04/02/2015 07:43 PM, Saran A wrote: >>>> os.mkdir('Success') > >>As you correctly stated: >>>What do you do the second time through this function, when that >>>directory is already existing? >>>> copy_and_move_file( 'Failure') [...] >>How would I ensure that this s directory is made only once and every file that is passeed goes only to 'success' or 'failure'? > >Well, you could use an if clause checking with os.exist(). If the >directory already exists, don't call the mkdir function. That may not >be perfect, but it should suffice for an assignment at your level. As a general remark that's slightly racy: checking, then doing. On all UNIX platforms, and probably Windows and others, mkdir is atomic: it works or it does not. So the reliable way tends to look like this: try: os.mkdir("blah") except FileExistsError: ... the directory exists, act accordingly ... except: ... something else went wrong, complain, abort, whatever else: ... directory successfully made ... FileExistsError only came in in Python 3.3, for earlier Pythons catch OSError and check the exception's .errno against errno.EEXIST. Cheers, Cameron Simpson I am a Bear of Very Little Brain and long words Bother Me. - Winnie-the-Pooh From rustompmody at gmail.com Fri Apr 3 22:16:11 2015 From: rustompmody at gmail.com (Rustom Mody) Date: Fri, 3 Apr 2015 19:16:11 -0700 (PDT) Subject: Strategy/ Advice for How to Best Attack this Problem? In-Reply-To: References: <87a8yvs34u.fsf@jester.gateway.pace.com> <0623f75c-bf93-4a0f-9d87-86986185cdc3@googlegroups.com> <4d3a3336-5dc1-4fe8-8ef6-1616c6629d01@googlegroups.com> Message-ID: <421dcb67-5d2d-4666-a3af-b0b5c61f3a74@googlegroups.com> On Saturday, April 4, 2015 at 1:52:20 AM UTC+5:30, Dave Angel wrote: > On 04/03/2015 08:50 AM, Saran A wrote: > > On Friday, April 3, 2015 at 8:05:14 AM UTC-4, Dave Angel wrote: > >> On 04/02/2015 07:43 PM, Saran A wrote: > > > > I addressed most of the issues. I do admit that, as a novice, I feel beholden to the computer - hence the over-engineering. > > > > Should be quite the opposite. As a novice, you ought to be testing the > heck out of your functions, worrying about whether they are properly > named, properly commented, and properly tested. I think this is a culture problem. We dont (typically) take python so seriously that we are 'beholden' (!!!) to python/computer/python's creator/each other out here/etc. In other cultures (sometimes called 'bondage-and-discipline' languages http://c2.com/cgi/wiki?BondageAndDisciplineLanguage you are expected to write mountains of documentation before writing code. The best we can make out, you are writing too much code, trying out (also called hacking) too little. Remember python is named after Monty Python. If you hunt youtube for Monty python or other productions of John Cleese like Fawlty Towers you'll get a picture of the spirit Not directly related but recently posted on this list https://www.youtube.com/watch?v=kAG39jKi0lI In short, if you are not having fun you are not getting python. If you are having fun, mucking around is not taboo... Also called using the interpreter interactively From pa at noemail.invalid Fri Apr 3 23:40:48 2015 From: pa at noemail.invalid (Paul Arrington) Date: Sat, 4 Apr 2015 03:40:48 +0000 (UTC) Subject: Defamation References: Message-ID: On Fri, 03 Apr 2015 18:37:33 +0200, silvagni wrote: > Please remove the page: > https://mail.python.org/pipermail/python-list/2014-September/691616.html > > Thank You Gabriele Silvagni This is a primarily a "Usenet" group, (Google that term), and is hosted by a distributed network of servers, and the python.org mail-server is merely one mirror of that network. Posts like that one _are_ annoying but are impossible to remove from the wider network. You _could_ try tackling the point-of-injection, which happens to be Google-Groups (which is another Usenet mirror), so Google bear prime responsibility for neglecting to filter the message. Good luck with that, though, because Google are very difficult to even contact. From zljubisicmob at gmail.com Sat Apr 4 09:07:12 2015 From: zljubisicmob at gmail.com (zljubisicmob at gmail.com) Date: Sat, 4 Apr 2015 06:07:12 -0700 (PDT) Subject: How to set request module logging to certain level (python 3.4.2) Message-ID: <26c26c73-c1d1-48ae-8a5e-1f1e20b4df2b@googlegroups.com> Hi, if I execute: import logging, requests logging.basicConfig(filename="obrisi.log", level=10, format='%(asctime)s %(levelname)s %(message)s',datefmt='%d.%m.%Y %H:%M:%S') logging.getLogger('requests').setLevel(logging.ERROR) url = 'http://radio.hrt.hr/prvi-program/arhiva/povijest-cetvrtkom/126/' wpage = requests.get(url) the obrisi.log has the following contents: 04.04.2015 13:40:23 INFO Starting new HTTP connection (1): radio.hrt.hr 04.04.2015 13:40:23 DEBUG "GET /prvi-program/arhiva/povijest-cetvrtkom/126/ HTTP/1.1" 200 None >From requests module, I would like to log from error level above. How to achieve that (raspberry pi 2, osmc, python 3.4.2? Regards. From joe.farro at gmail.com Sat Apr 4 11:45:53 2015 From: joe.farro at gmail.com (Joe Farro) Date: Sat, 4 Apr 2015 08:45:53 -0700 (PDT) Subject: beautifulSoup 4.1 In-Reply-To: References: Message-ID: <2ef785df-9ea8-4a41-9592-841d346c8857@googlegroups.com> Could use zip: tds = iter(soup('td')) for abbr, defn in zip(tds, tds): print abbr.get_text(), defn.get_text() From me.pankaj786 at gmail.com Sat Apr 4 11:56:22 2015 From: me.pankaj786 at gmail.com (pankaj sharma) Date: Sat, 4 Apr 2015 08:56:22 -0700 (PDT) Subject: OOP for System Administrator Message-ID: Hi, I'm a Linux system administrator and my work requires me to write bash scripts (100-500 lines) for process monitoring, server health check and automate some manual processes. Now I've started to learn python as I want to write scripts in python rather than bash or any shell. My questions is Do I need to learn Object oriented Python in order to be good at python scripting. I know basics of OOP but personally didn't like it much. Thanks Pankaj From ian.g.kelly at gmail.com Sat Apr 4 11:57:58 2015 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 4 Apr 2015 09:57:58 -0600 Subject: How to set request module logging to certain level (python 3.4.2) In-Reply-To: References: <26c26c73-c1d1-48ae-8a5e-1f1e20b4df2b@googlegroups.com> Message-ID: On Sat, Apr 4, 2015 at 9:21 AM, Dennis Lee Bieber wrote: > On Sat, 4 Apr 2015 06:07:12 -0700 (PDT), zljubisicmob at gmail.com declaimed > the following: > >From requests module, I would like to log from error level above. >> > > Unless the requests module documents that is uses a logger named > "requests", you are likely stuck with having to adjust the level of the > root (unnamed) logger. You can include "%(name)s" in the format string to see the name of the logger that was used. > http://docs.python-requests.org/en/latest/api/?highlight=log > > implies the correct name to use for the logger is > "requests.packages.urllib3". If that's the case, then configuring the "requests" logger should work, since it's a hierarchical ancestor of "requests.packages.urllib3". From zljubisicmob at gmail.com Sat Apr 4 14:29:53 2015 From: zljubisicmob at gmail.com (zljubisicmob at gmail.com) Date: Sat, 4 Apr 2015 11:29:53 -0700 (PDT) Subject: How to set request module logging to certain level (python 3.4.2) In-Reply-To: References: <26c26c73-c1d1-48ae-8a5e-1f1e20b4df2b@googlegroups.com> Message-ID: <2b7c488b-e144-41b4-a679-ed6d9f07ce67@googlegroups.com> On Saturday, April 4, 2015 at 5:23:02 PM UTC+2, Dennis Lee Bieber wrote: > implies the correct name to use for the logger is > "requests.packages.urllib3". I have changed a script log_test.py. Now it looks like this: #!/usr/bin/python3 import logging, requests logging.basicConfig(filename="obrisi.log", level=10, format='%(asctime)s %(levelname)s %(message)s',datefmt='%d.%m.%Y %H:%M:%S') logging.getLogger('requests.packages.urllib3').setLevel(logging.ERROR) url = 'http://radio.hrt.hr/prvi-program/arhiva/povijest-cetvrtkom/126/' def abc(): wpage = requests.get(url) wpage = requests.get(url) abc() from linux shell I am running it as: rm obrisi.log; ./log_test.py; cat obrisi.log the output is: 04.04.2015 20:29:05 INFO Starting new HTTP connection (1): radio.hrt.hr 04.04.2015 20:29:05 DEBUG "GET /prvi-program/arhiva/povijest-cetvrtkom/126/ HTTP/1.1" 200 None 04.04.2015 20:29:05 INFO Starting new HTTP connection (1): radio.hrt.hr 04.04.2015 20:29:05 DEBUG "GET /prvi-program/arhiva/povijest-cetvrtkom/126/ HTTP/1.1" 200 None o As you can see, it still doesn't work. I really don't get it. I am confused. :( Regards. From torriem at gmail.com Sat Apr 4 15:04:27 2015 From: torriem at gmail.com (Michael Torrie) Date: Sat, 04 Apr 2015 13:04:27 -0600 Subject: OOP for System Administrator In-Reply-To: References: Message-ID: <552035BB.3070200@gmail.com> On 04/04/2015 09:56 AM, pankaj sharma wrote: > I'm a Linux system administrator and my work requires me to write > bash scripts (100-500 lines) for process monitoring, server health > check and automate some manual processes. Now I've started to learn > python as I want to write scripts in python rather than bash or any > shell. > > My questions is Do I need to learn Object oriented Python in order to > be good at python scripting. I know basics of OOP but personally > didn't like it much. Let me guess. Your exposure to OOP comes from Java or C#? Python's use of OOP is very natural and powerful. But you don't have to use OO design in your programs. So no you don't need to learn oo Python, just learn and use Python, period. As needed you can benefit from the strong OO capabilities of Python without having to use that paradigm for your own programs. >From a system programmer's point of view, other features of Python, like generators are going to give you far more utility. I recommend you take a look at this presentation for how Python can fit in your work: http://www.dabeaz.com/generators/ Note that while Python is a scripting language, it's not a shell language. To interface with other processes, you'll have to use popen() and connect to the standard pipes (in, out, err). It's not like bash where any reference to a program in the path is executed and piped automatically. A long time ago I wrote a little wrapper around popen() to make it easier and faster to interact with unix commands. From zljubisicmob at gmail.com Sat Apr 4 15:38:49 2015 From: zljubisicmob at gmail.com (zljubisicmob at gmail.com) Date: Sat, 4 Apr 2015 12:38:49 -0700 (PDT) Subject: How to set request module logging to certain level (python 3.4.2) In-Reply-To: <2b7c488b-e144-41b4-a679-ed6d9f07ce67@googlegroups.com> References: <26c26c73-c1d1-48ae-8a5e-1f1e20b4df2b@googlegroups.com> <2b7c488b-e144-41b4-a679-ed6d9f07ce67@googlegroups.com> Message-ID: <260d47af-7926-4067-922d-674ba50ec03a@googlegroups.com> Looks like I have found (with your help) a solution. Instead of: logging.getLogger('requests.packages.urllib3').setLevel(logging.ERROR) the line should look like: logging.getLogger('urllib3').setLevel(logging.ERROR) After changing the line, everything is OK. Thank you all. Regards. From emile at fenx.com Sat Apr 4 16:27:26 2015 From: emile at fenx.com (Emile van Sebille) Date: Sat, 04 Apr 2015 13:27:26 -0700 Subject: OOP for System Administrator In-Reply-To: References: Message-ID: On 4/4/2015 8:56 AM, pankaj sharma wrote: > Hi, > > I'm a Linux system administrator and my work requires me to write bash scripts (100-500 lines) for process monitoring, server health check and automate some manual processes. Now I've started to learn python as I want to write scripts in python rather than bash or any shell. > > My questions is Do I need to learn Object oriented Python in order to be good at python scripting. I know basics of OOP but personally didn't like it much. Michael already responded to the oo question. You might also find this helpful: http://docs.linuxtone.org/ebooks/Python/OReilly.Python.for.Unix.and.Linux.System.Administration.Sep.2008.pdf Emile From damien.p.george at gmail.com Sat Apr 4 19:43:58 2015 From: damien.p.george at gmail.com (Damien George) Date: Sun, 5 Apr 2015 00:43:58 +0100 Subject: MicroPython 1.4.1 released Message-ID: Hello everyone, We are pleased to announce the release of MicroPython version 1.4.1! MicroPython is an implementation of Python 3.4 which is optimised for systems with minimal resources, including microcontrollers. Since our last announcement, this release is both more "micro" and more "Python". Code size of the bare Thumb2 architecture version has dropped to under 71k (without reduction of features), the RAM usage has been further optimised, and support for 16-bit microcontrollers has been proven via the port to a PIC microcontroller with just 8k RAM. On the Python side of things, there is now a "stackless" mode with both strict and non-strict behaviour. Strict will always use the heap to allocate a call frame, where non-strict will fall back to the C stack if the heap is exhausted. More special methods have been implemented, along with proper descriptors, OrderedDict class, basic frozen module support and the ability to override builtins, among other things. The test suite has grown and coverage of the code is now beyond 91%. Many other features have been implemented for the ports to various microcontroller platforms, bugs have been fixed and the docs have been improved. A full change log is available at https://micropython.org/resources/micropython-ChangeLog.txt . For more information about the project please visit http://micropython.org/ https://github.com/micropython/micropython Best regards, Damien George. From PointedEars at web.de Sat Apr 4 19:51:00 2015 From: PointedEars at web.de (Thomas 'PointedEars' Lahn) Date: Sun, 05 Apr 2015 01:51 +0200 Subject: Python regex exercise References: Message-ID: <2633598.mOnDxdZfPB@PointedEars.de> Robert Clove wrote: > Do anyone have good links to python regex or other python problems for > beginners but with solution. > > Please mail me. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail. From vincent at vincentdavis.net Sat Apr 4 20:21:12 2015 From: vincent at vincentdavis.net (Vincent Davis) Date: Sat, 4 Apr 2015 18:21:12 -0600 Subject: Python regex exercise In-Reply-To: <2633598.mOnDxdZfPB@PointedEars.de> References: <2633598.mOnDxdZfPB@PointedEars.de> Message-ID: On Sat, Apr 4, 2015 at 5:51 PM, Thomas 'PointedEars' Lahn < PointedEars at web.de> wrote: > > Do anyone have good links to python regex or other python problems for > > beginners but with solution. > > > > Please mail me. > ?I recently found? this https://regex101.com/#python Vincent Davis 720-301-3003 -------------- next part -------------- An HTML attachment was scrubbed... URL: From flebber.crue at gmail.com Sat Apr 4 20:46:34 2015 From: flebber.crue at gmail.com (Sayth) Date: Sat, 4 Apr 2015 17:46:34 -0700 (PDT) Subject: emacs for python web development Message-ID: <9352484c-b2ad-4d67-9282-50d00bfa0003@googlegroups.com> Has anyone got a good configuration or advice for setting up a good python web development environment in emacs? Sayth From ben+python at benfinney.id.au Sun Apr 5 01:36:29 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 05 Apr 2015 15:36:29 +1000 Subject: emacs for python web development References: <9352484c-b2ad-4d67-9282-50d00bfa0003@googlegroups.com> Message-ID: <85zj6nyw4i.fsf@benfinney.id.au> Sayth writes: > Has anyone got a good configuration or advice for setting up a good > python web development environment in emacs? You can start at the Python Wiki page on Emacs as an editor . -- \ ?? a Microsoft Certified System Engineer is to information | `\ technology as a McDonalds Certified Food Specialist is to the | _o__) culinary arts.? ?Michael Bacarella | Ben Finney From flebber.crue at gmail.com Sun Apr 5 02:00:51 2015 From: flebber.crue at gmail.com (Sayth) Date: Sat, 4 Apr 2015 23:00:51 -0700 (PDT) Subject: emacs for python web development In-Reply-To: References: <9352484c-b2ad-4d67-9282-50d00bfa0003@googlegroups.com> Message-ID: <5b7674a0-2df6-4551-9fc5-5fb5f62eb39e@googlegroups.com> Thanks, got the python bit down is just the Web for flask and django. Getting the templates and snippets to work in a good flow is where I am looking for advice. Cheers Sayth From me.pankaj786 at gmail.com Sun Apr 5 04:36:31 2015 From: me.pankaj786 at gmail.com (pankaj sharma) Date: Sun, 5 Apr 2015 01:36:31 -0700 (PDT) Subject: OOP for System Administrator In-Reply-To: References: Message-ID: <9d76022a-af8c-427f-91fc-cb1315fae56a@googlegroups.com> Yes Michael, your guess is right. I once was a Java Programmer :) Thanks for the advice and link. Regards, From peter at peterrowley.com Sun Apr 5 13:28:51 2015 From: peter at peterrowley.com (Peter Rowley) Date: Sun, 05 Apr 2015 13:28:51 -0400 Subject: Trying to hire or grow a Python developer for a 2 year contract Message-ID: Hi, I'm at York University in Toronto, Canada. We have a large Python-and-Oracle web application written with Pyramids and YUI that we use to manage the staffing of courses at York and are looking for an intermediate or senior developer for a 2 year contract to work on extensions to the application. If you are interested or know of someone who would be, please e-mail me at prowley at yorku.ca. The salary is approximately $75,000 a year and benefits are excellent. We have been looking for someone for this position for a while and are starting to think about hiring someone who has good programming skills but not necessarily Python. To that end, if you're a Python developer or hire them, I'd be interested in your opinion of which common programming languages that people might know would enable learning Python? I'm guessing PHP for the dynamic language concepts and Java for the structure, but that's not based on much evidence. Thanks in advance Peter Rowley, Director of Applications and Integration University Information Technology, York University, Toronto prowley at yorku.ca -------------- next part -------------- An HTML attachment was scrubbed... URL: From izbyshev at ispras.ru Sun Apr 5 13:45:07 2015 From: izbyshev at ispras.ru (Alexey Izbyshev) Date: Sun, 05 Apr 2015 20:45:07 +0300 Subject: Permission denied when opening a file that was created concurrently by os.rename (Windows) Message-ID: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> Hello! I've hit a strange problem that I reduced to the following test case: * Run several python processes in parallel that spin in the following loop: while True: if os.path.isfile(fname): with open(fname, 'rb') as f: f.read() break * Then, run another process that creates a temporary file and then renames it to the name than other processes are expecting * Now, some of the reading processes occasionally fail with "Permission denied" OSError I was able to reproduce it on two Windows 7 64-bit machines. It seems when the file appears on the filesystem it is still unavailable to reading, but I have no idea how it can happen. Both source and destination files are in the same directory, and the destination doesn't exist before calling os.rename. Everything I could find indicates that os.rename should be atomic under this conditions even on Windows, so nobody should be able to observe the destination in unaccessible state. I know that I can workaround this problem by removing useless os.path.isfile() check and wrapping open() with try-except, but I'd like to know the root cause of the problem. Please share you thoughts. The test case is attached, the main file is test.bat. Python is expected to be in PATH. Stderr of readers is redirected to *.log. You may need to run several times to hit the issue. Alexey Izbyshev, research assistant, ISP RAS From rosuav at gmail.com Sun Apr 5 13:55:24 2015 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 6 Apr 2015 03:55:24 +1000 Subject: Permission denied when opening a file that was created concurrently by os.rename (Windows) In-Reply-To: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> References: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> Message-ID: On Mon, Apr 6, 2015 at 3:45 AM, Alexey Izbyshev wrote: > The test case is attached, the main file is test.bat. Python is expected to > be in PATH. Stderr of readers is redirected to *.log. You may need to run > several times to hit the issue. You have an interesting-looking problem, but the attachment didn't arrive. Is it short enough to include in-line as text in your email? ChrisA From izbyshev at ispras.ru Sun Apr 5 14:08:52 2015 From: izbyshev at ispras.ru (Alexey Izbyshev) Date: Sun, 05 Apr 2015 21:08:52 +0300 Subject: Permission denied when opening a file that was created concurrently by os.rename (Windows) In-Reply-To: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> References: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> Message-ID: On 2015-04-05 20:45, Alexey Izbyshev wrote: > Hello! > > I've hit a strange problem that I reduced to the following test case: > * Run several python processes in parallel that spin in the following > loop: > while True: > if os.path.isfile(fname): > with open(fname, 'rb') as f: > f.read() > break > * Then, run another process that creates a temporary file and then > renames it to the name than other processes are expecting > * Now, some of the reading processes occasionally fail with > "Permission denied" OSError > > I was able to reproduce it on two Windows 7 64-bit machines. It seems > when the file appears on the filesystem it is still unavailable to > reading, but I have no idea how it can happen. Both source and > destination files are in the same directory, and the destination > doesn't exist before calling os.rename. Everything I could find > indicates that os.rename should be atomic under this conditions even > on Windows, so nobody should be able to observe the destination in > unaccessible state. > > I know that I can workaround this problem by removing useless > os.path.isfile() check and wrapping open() with try-except, but I'd > like to know the root cause of the problem. Please share you thoughts. > > The test case is attached, the main file is test.bat. Python is > expected to be in PATH. Stderr of readers is redirected to *.log. You > may need to run several times to hit the issue. > > Alexey Izbyshev, > research assistant, > ISP RAS The attachment was rejected because of .bat files, so here is the code: test.bat: ====================== @echo off del test.txt del *.log for /L %%i in (1, 1, 10) do ( start /b run-with-log.bat %%i ) pythonw test-rename.py ====================== run-with-log.bat: ====================== @echo off pythonw test-read.py %1 >nul 2>%1.log exit ====================== -------------- next part -------------- A non-text attachment was scrubbed... Name: test.zip Type: application/zip Size: 501 bytes Desc: not available URL: From davea at davea.name Sun Apr 5 14:11:23 2015 From: davea at davea.name (Dave Angel) Date: Sun, 05 Apr 2015 14:11:23 -0400 Subject: Permission denied when opening a file that was created concurrently by os.rename (Windows) In-Reply-To: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> References: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> Message-ID: <55217ACB.8020301@davea.name> On 04/05/2015 01:45 PM, Alexey Izbyshev wrote: > Hello! > > I've hit a strange problem that I reduced to the following test case: > * Run several python processes in parallel that spin in the following loop: > while True: > if os.path.isfile(fname): > with open(fname, 'rb') as f: > f.read() > break > * Then, run another process that creates a temporary file and then > renames it to the name than other processes are expecting > * Now, some of the reading processes occasionally fail with "Permission > denied" OSError > > I was able to reproduce it on two Windows 7 64-bit machines. It seems > when the file appears on the filesystem it is still unavailable to > reading, but I have no idea how it can happen. Both source and > destination files are in the same directory, and the destination doesn't > exist before calling os.rename. Everything I could find indicates that > os.rename should be atomic under this conditions even on Windows, so > nobody should be able to observe the destination in unaccessible state. > > I know that I can workaround this problem by removing useless > os.path.isfile() check and wrapping open() with try-except, but I'd like > to know the root cause of the problem. Please share you thoughts. > > The test case is attached, the main file is test.bat. Python is expected > to be in PATH. Stderr of readers is redirected to *.log. You may need to > run several times to hit the issue. > > Alexey Izbyshev, > research assistant, > ISP RAS > The attachment is missing; please just include it inline, after reducing it to a reasonably minimal sample. My guess is that the process that does the os.rename is not closing the original file before renaming it. So even though the rename is atomic, the file is still locked by the first process. -- DaveA From ellisd23 at gmail.com Sun Apr 5 15:20:48 2015 From: ellisd23 at gmail.com (Daniel Ellis) Date: Sun, 5 Apr 2015 12:20:48 -0700 (PDT) Subject: Help with pipes, buffering and pseudoterminals Message-ID: I have a small little tool I'd like to make. It essentially takes piped input, modifies the text in some way, and immediately prints the output. The problem I'm having is that any output I pipe to the program seems to be buffered, removing the desired effect. >From what I understand, I need to somehow have the input be retrieved via a pseudoterminal. The problem that I'm having is that most examples on the internet seem to assume I would like to launch a program in a forked pty process, which doesn't really fit my use case. I've tried a number of things, but I seem to be unable to get even a basic understanding of how to use the pty module. Here's a piece of code I whipped up just to try to get a feel for what is going on when I use pty.fork, but it doesn't seem to do what I think it should: import pty import os import sys pid, fd = pty.fork() print pid, fd sys.stdout.flush() os.read(fd, 1024) This only seems to print from the parent process. I read that I need to do the os.read call for the fork to happen. I've also tried printing *after* the os.read call. I realize this does very little to solve my overall goal, but I figure understanding what is going on is probably a worthwhile first step. From garabik-news-2005-05 at kassiopeia.juls.savba.sk Sun Apr 5 15:49:55 2015 From: garabik-news-2005-05 at kassiopeia.juls.savba.sk (garabik-news-2005-05 at kassiopeia.juls.savba.sk) Date: Sun, 5 Apr 2015 19:49:55 +0000 (UTC) Subject: ANN: polynice 0.7 - a nice(1) like utility for throttling processes Message-ID: polynice is a nice(1)-like command line utility for unix systems to throttle long running processes beyond what can be achieved by nice(1), by repeatedly suspending and resuming the process. It is written for python3, though there is some python2.7 compatibility. Author: Radovan Garab?k URL: http://kassiopeia.juls.savba.sk/~garabik/software/polynice.html License: GPL (v2) Notable changes: * This is the first public release under the name `polynice' - the utility has been renamed from `verynice' due to name clash with an existing software. * polynice can now emulate timeout(1) - terminate process after given time has passed * MacOS X improvements * better python2 compatiblity -- ----------------------------------------------------------- | Radovan Garab?k http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! From tjreedy at udel.edu Sun Apr 5 16:10:06 2015 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 05 Apr 2015 16:10:06 -0400 Subject: Permission denied when opening a file that was created concurrently by os.rename (Windows) In-Reply-To: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> References: <51cbd8a4eb7040f60aa74d127803b441@ispras.ru> Message-ID: On 4/5/2015 1:45 PM, Alexey Izbyshev wrote: > Hello! > > I've hit a strange problem that I reduced to the following test case: > * Run several python processes in parallel that spin in the following loop: > while True: > if os.path.isfile(fname): > with open(fname, 'rb') as f: > f.read() > break Besides anything else, I would consider adding a minimal sleep in the loop. > * Then, run another process that creates a temporary file and then > renames it to the name than other processes are expecting > * Now, some of the reading processes occasionally fail with "Permission > denied" OSError -- Terry Jan Reedy From jenesuispasdave at gmail.com Sun Apr 5 16:38:11 2015 From: jenesuispasdave at gmail.com (Dave Hein) Date: Sun, 5 Apr 2015 13:38:11 -0700 (PDT) Subject: Is it possible to deliver different source distributions for different Python versions? Message-ID: <9826e06e-be2f-4e3f-87b2-89e933a0d003@googlegroups.com> I would like to distribute a python package with different code for Python 2.* than for Python 3.*. (Mostly this is because of different unicode string handling). There is nothing in to setuptools or PyPi that directly supports this scenario. But perhaps there could be some script run at install time that moves the correct source code to the right location? In other works, if I included both source code versions in the distribution (in a src2 and a src3 subdirectory) then a function invoked at install time could detect the python version and copy the appropriate source code to the right location. Is that at all possible? Is there some install time hook that lets me supply custom installation code? -- Dave Hein From nobody at nowhere.invalid Sun Apr 5 20:06:46 2015 From: nobody at nowhere.invalid (Nobody) Date: Mon, 06 Apr 2015 01:06:46 +0100 Subject: Help with pipes, buffering and pseudoterminals References: Message-ID: On Sun, 05 Apr 2015 12:20:48 -0700, Daniel Ellis wrote: > This only seems to print from the parent process. I read that I need to > do the os.read call for the fork to happen. I've also tried printing > *after* the os.read call. The child process has its std{in,out,err} attached to the newly-created pty, so that's where the output from the child's "print" goes. You'll see that output if the parent prints the string returned from the os.read() call. From cs at zip.com.au Sun Apr 5 20:11:09 2015 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 6 Apr 2015 10:11:09 +1000 Subject: Help with pipes, buffering and pseudoterminals In-Reply-To: References: Message-ID: <20150406001109.GA1009@cskk.homeip.net> On 05Apr2015 12:20, Daniel Ellis wrote: >I have a small little tool I'd like to make. It essentially takes piped input, modifies the text in some way, and immediately prints the output. The problem I'm having is that any output I pipe to the program seems to be buffered, removing the desired effect. That depends on the upstream program; it does the buffering. The pipe itself presents received data downstream immediately. However, as you've seen, almost every program buffers its standard output if the output is not a tty; this is automatic in the stdio C library and results in more fficient use of I/O. >From what I understand, I need to somehow have the input be retrieved via a pseudoterminal. This is a rather gross hack, though sometimes all you can do. While some programs have an option to force unbuffered output, most do not. Attaching their output to a pty is one way to encourage them to at least line buffer their output. However, you should bear in mind that the reason that programs line buffer to a terminal is that they presume they are in an interactive situation with a person watching. The program _may_ act differently in other ways as well, such as asking question it might not otherwise ask in "batch" mode (where it might cautiously not ask and presume "no"). Also, output sent through a pty is subject to the line discipline in the terminal; temrinals are funny things with much historical behaviour. At the least you pobably want your pty in "raw" mode to avoid all sorts of stuff that can be done to your data. >The problem that I'm having is that most examples on the internet seem to assume I would like to launch a program in a forked pty process, which doesn't really fit my use case. Indeed not, but not to worry. You don't need to fork. >I've tried a number of things, but I seem to be unable to get even a basic understanding of how to use the pty module. Have you every used a pty from C? Do you know how ptys work? (master side, slave side, etc). >Here's a piece of code I whipped up just to try to get a feel for what is going on when I use pty.fork, but it doesn't seem to do what I think it should: > > import pty > import os > import sys > > pid, fd = pty.fork() > print pid, fd > sys.stdout.flush() > os.read(fd, 1024) > >This only seems to print from the parent process. The documentation for pty.fork says: Return value is (pid, fd). Note that the child gets pid 0, and the fd is invalid. So the child cannot used "fd". It further says that the child has its stdin and stdout attached to the pty, and that the pty is the child's controlling terminal (this means it is affected by things like "typing" ^C at the pty, etc). >I read that I need to do the os.read call for the fork to happen. I've also tried printing *after* the os.read call. Don't try to adapt fork-based tutorials to your needs. Understand ptys directly first. >I realize this does very little to solve my overall goal, but I figure understanding what is going on is probably a worthwhile first step. What you probably want to use is pty.openpty() instead. No fork. You will get back file descriptors for the master and slave sides of the pty. Then you can use these with the subprocess module to connect your input program. Or, guessing from your opening sentence, you can write a wrapper script whose whole purpose is to run a program on a pty. Regarding terminology: a pseudoterminal (pty) is a device that looks like a traditional serial terminal. All terminal emulators like xterm use one, and so do other programs presenting a terminal session such as the sshd process handling an interactive remote login. When you call pty.openpty() you are handed two file descriptors: one for the master side of the pty and one for the slave side. The slave side is the side that looks like a terminal, and is what a typical use would connect a child process to. The master side is the other side of the pty. When a program writes to the "slave" side, the output is available for read on the master side, much like a pipe. When a program writes to the master side, the output is available for read on the slave side, _as_ _if_ _typed_ at the terminal. A pty is not necessarily going to solve your problem unless you can get your input via the pty. From the sounds of it you're in this situation: command-generating-output | your-program such that your input is attached to a pipe, and because "command-generating-output" is attached to a pipe it is block buffering its output, hence your problem. You can't undo that situation after the fact. To solve your problem via a pty you need to contrive to set up "command-generating-output" already attached to a pty. One way to do that is for "your-program" to open a pty and itself invoke "command-generating-output" with its output via the pty, which is why so many tutorials suppose a "fork" situation. One typical away to do that is to pass the "command-generating-output" command name and args to your program, eg: your-program command-generating-output [args...] Then your main program can gather that up: import sys command_generating_output = sys.argv[1:] Then you can call pty.openpty(), and then use the slave file descriptor with subprocess.Popen to invoke command_generating_output. Thus the generating command will be talking to you via a pty instead of a pipe. Cheers, Cameron Simpson It is interesting to think of the great blaze of heaven that we winnow down to animal shapes and kitchen tools. - Don DeLillo From steve+comp.lang.python at pearwood.info Sun Apr 5 21:00:48 2015 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Mon, 06 Apr 2015 11:00:48 +1000 Subject: Is it possible to deliver different source distributions for different Python versions? References: <9826e06e-be2f-4e3f-87b2-89e933a0d003@googlegroups.com> Message-ID: <5521dac2$0$12980$c3e8da3$5496439d@news.astraweb.com> On Mon, 6 Apr 2015 06:38 am, Dave Hein wrote: > I would like to distribute a python package with different code for > Python 2.* than for Python 3.*. (Mostly this is because of different > unicode string handling). > > There is nothing in to setuptools or PyPi that directly supports > this scenario. > > But perhaps there could be some script run at install time that moves > the correct source code to the right location? In other works, if I > included both source code versions in the distribution (in a src2 and > a src3 subdirectory) then a function invoked at install time could > detect the python version and copy the appropriate source code to the > right location. > > Is that at all possible? Is there some install time hook that lets me > supply custom installation code? I'm not aware of any standard solution to that, but I'm not a setuptools expert. setup.py files are Python code, so you can put any code you like in them. But, as far as I am concerned, having the installer pick and choose what source files to include is not a good solution. Instead, you should pick one of these two alternatives: (1) Supply a separate package for 2.x and 3.x, each with their own installer. The installer confirms that it is running under the correct version of Python, and just installs. (2) Or supply a single package with a single installer that works under both 2.x and 3.x. (This is my preference.) One way of handling the second case is to only support 3.3 or better: that way, you can still use u"..." for Unicode strings. Hardly anyone used 3.1 or 3.2, and 3.0 is no longer supported, so it is quite reasonable to insist people upgrade to 3.3. Another way to handle the second case is to use conditional imports: # mymodule2 mystring = u"????" # Also works in 3.3 or better. # mymodule3 mystring = "????" # main application from __future__ import print_function from future_builtins import * if sys.version < '3': import mymodule2 as mymodule else: import mymodule3 as mymodule print(mymodule.mystring) -- Steven From sepideh.gh at gmail.com Sun Apr 5 22:26:41 2015 From: sepideh.gh at gmail.com (Sepideh Ghanavati) Date: Sun, 5 Apr 2015 19:26:41 -0700 (PDT) Subject: XML Parsing Message-ID: Hi, I know basic of python and I have an xml file created from csv which has three attributes "category", "definition" and "definition description". I want to parse through xml file and identify actors, constraints, principal from the text. However, I am not sure what is the best way to go. Any suggestion? From ben+python at benfinney.id.au Sun Apr 5 22:39:26 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 06 Apr 2015 12:39:26 +1000 Subject: XML Parsing References: Message-ID: <85fv8eyo81.fsf@benfinney.id.au> Sepideh Ghanavati writes: > I know basic of python and I have an xml file created from csv What XML schema defines the document's format? Without knowing the schema, parsing will be unreliable. What created the document? Why is it relevant that the document was ?created from CSV?? > which has three attributes "category", "definition" and "definition > description". What do you mean by ?attributes?? In Python, an attribute has a specific meaning. In XML, an attribute has a rather different meaning. Neither of those meanings seems to apply to ?the XML document has three attributes?. XML documents don't have attributes; differnt XML elements in a document have different attributes. > I want to parse through xml file and identify actors, constraints, > principal from the text. How are those defined in the document's schema? > However, I am not sure what is the best way to go. Any suggestion? You should: * Learn some more about XML . * Learn exactly what formal document schema defines the document . If the document isn't accompanied by a specification of exactly what its schema is, you're going to have a difficult time. -- \ ?If I melt dry ice, can I swim without getting wet?? ?Steven | `\ Wright | _o__) | Ben Finney From breamoreboy at yahoo.co.uk Sun Apr 5 23:27:26 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 Apr 2015 04:27:26 +0100 Subject: Is it possible to deliver different source distributions for different Python versions? In-Reply-To: <9826e06e-be2f-4e3f-87b2-89e933a0d003@googlegroups.com> References: <9826e06e-be2f-4e3f-87b2-89e933a0d003@googlegroups.com> Message-ID: On 05/04/2015 21:38, Dave Hein wrote: > I would like to distribute a python package with different code for > Python 2.* than for Python 3.*. (Mostly this is because of different > unicode string handling). > > There is nothing in to setuptools or PyPi that directly supports > this scenario. > > But perhaps there could be some script run at install time that moves > the correct source code to the right location? In other works, if I > included both source code versions in the distribution (in a src2 and > a src3 subdirectory) then a function invoked at install time could > detect the python version and copy the appropriate source code to the > right location. > > Is that at all possible? Is there some install time hook that lets me > supply custom installation code? > > -- > Dave Hein > I can't help directly but have you looked here https://packaging.python.org/en/latest/ ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From stefan_ml at behnel.de Mon Apr 6 01:46:11 2015 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 06 Apr 2015 07:46:11 +0200 Subject: Is it possible to deliver different source distributions for different Python versions? In-Reply-To: <9826e06e-be2f-4e3f-87b2-89e933a0d003@googlegroups.com> References: <9826e06e-be2f-4e3f-87b2-89e933a0d003@googlegroups.com> Message-ID: Dave Hein schrieb am 05.04.2015 um 22:38: > I would like to distribute a python package with different code for > Python 2.* than for Python 3.*. (Mostly this is because of different > unicode string handling). > > There is nothing in to setuptools or PyPi that directly supports > this scenario. > > But perhaps there could be some script run at install time that moves > the correct source code to the right location? In other works, if I > included both source code versions in the distribution (in a src2 and > a src3 subdirectory) then a function invoked at install time could > detect the python version and copy the appropriate source code to the > right location. > > Is that at all possible? Is there some install time hook that lets me > supply custom installation code? Sure. You can simply change the directory in which distutils looks for your Python code: https://docs.python.org/2/distutils/setupscript.html#listing-whole-packages However, in general, you shouldn't be doing this. It's usually easier (definitely in the long-term) to keep your sources cross-Py2.x/3.x compatible, maybe with the help of tools like "six" or "python-future", than to try to keep separate source trees in sync. http://python-future.org/ Stefan From stefan_ml at behnel.de Mon Apr 6 01:54:41 2015 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 06 Apr 2015 07:54:41 +0200 Subject: XML Parsing In-Reply-To: References: Message-ID: Sepideh Ghanavati schrieb am 06.04.2015 um 04:26: > I know basic of python and I have an xml file created from csv which has > three attributes "category", "definition" and "definition description". > I want to parse through xml file and identify actors, constraints, > principal from the text. However, I am not sure what is the best way to > go. Any suggestion? If it's really generated from a CSV file, you could also parse that instead: https://docs.python.org/3/library/csv.html Admittedly, CSV files are simple, but they also have major problems, especially when it comes to detecting their character encoding and their specific format (tab/comma/semicolon/space/whatever separated, with or without escaping, quoted values, ...). Meaning, you can easily end up reading nonsense from the file instead of the content that was originally put into it. So, if you want to parse from XML instead, use ElementTree: https://docs.python.org/3/library/xml.etree.elementtree.html Stefan From michael at linuxsystems.rocks Mon Apr 6 04:40:17 2015 From: michael at linuxsystems.rocks (Michael S.) Date: Mon, 06 Apr 2015 11:40:17 +0300 Subject: implementing pyshark Message-ID: <55224671.1020104@linuxsystems.rocks> Hi everyone. while trying to implement pyshark, i am getting this error: import pyshark capture = pyshark.LiveCapture(interface='wlan0') capture.sniff(timeout=50) capture i have tried also to run it through interpreter yet, i got this error: import pyshark --------------------------------------------------------------------------- ImportError Traceback (most recent call last) in () ----> 1 import pyshark /usr/local/lib/python2.7/dist-packages/pyshark-0.3.4-py2.7.egg/pyshark/__init__.py in () ----> 1 from pyshark.capture.live_capture import LiveCapture 2 from pyshark.capture.file_capture import FileCapture 3 from pyshark.capture.remote_capture import RemoteCapture 4 from pyshark.capture.inmem_capture import InMemCapture /usr/local/lib/python2.7/dist-packages/pyshark-0.3.4-py2.7.egg/pyshark/capture/live_capture.py in () ----> 1 from pyshark.capture.capture import Capture 2 from pyshark.tshark.tshark import get_tshark_interfaces 3 4 5 class LiveCapture(Capture): /usr/local/lib/python2.7/dist-packages/pyshark-0.3.4-py2.7.egg/pyshark/capture/capture.py in () 1 from __future__ import unicode_literals 2 import os ----> 3 import logbook 4 import sys 5 /usr/local/lib/python2.7/dist-packages/logbook/__init__.py in () 12 13 ---> 14 from logbook.base import LogRecord, Logger, LoggerGroup, NestedSetup, \ 15 Processor, Flags, get_level_name, lookup_level, dispatch_record, \ 16 CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG, NOTSET, \ /usr/local/lib/python2.7/dist-packages/logbook/base.py in () 16 from datetime import datetime 17 from logbook import helpers ---> 18 from logbook.concurrency import thread_get_name, thread_get_ident, greenlet_get_ident 19 20 from logbook.helpers import to_safe_json, parse_iso8601, cached_property, \ /usr/local/lib/python2.7/dist-packages/logbook/concurrency.py in () 29 30 if has_gevent: ---> 31 from gevent._threading import (Lock as ThreadLock, 32 RLock as ThreadRLock, 33 get_ident as thread_get_ident, ImportError: No module named _threading i tried importing threading first and importing pyshark, but the answer was the same. any suggestions? From dylanlee.evans95 at gmail.com Mon Apr 6 07:23:28 2015 From: dylanlee.evans95 at gmail.com (Dylan Evans) Date: Mon, 6 Apr 2015 12:23:28 +0100 Subject: Is it possible to deliver different source distributions for different Python versions? In-Reply-To: References: Message-ID: > I would like to distribute a python package with different code for? > Python 2.* than for Python 3.*. (Mostly this is because of different? > unicode string handling).? > There is nothing in to setuptools or PyPi that directly supports? > this scenario.? The 2to3 utility will handle the unicode differences. You can set the keyword argument use_2to3 to True in setup.py dependent on the version number like so: ==================== extra = {} if sys.version_info >= (3, ): ? ? extra['use_2to3'] = True ==================== and then put **extra in setup.py. This will run 2to3 on the code when it installs it. I don?t know whether this is good style but it does work. I got it from the feedparser source code.? --? Dylan Evans On 6 April 2015 at 11:05:47, python-list-request at python.org (python-list-request at python.org) wrote: Send Python-list mailing list submissions to python-list at python.org To subscribe or unsubscribe via the World Wide Web, visit https://mail.python.org/mailman/listinfo/python-list or, via email, send a message with subject or body 'help' to python-list-request at python.org You can reach the person managing the list at python-list-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Python-list digest..." Today's Topics: 1. Permission denied when opening a file that was created concurrently by os.rename (Windows) (Alexey Izbyshev) 2. Re: Permission denied when opening a file that was created concurrently by os.rename (Windows) (Chris Angelico) 3. Re: Permission denied when opening a file that was created concurrently by os.rename (Windows) (Alexey Izbyshev) 4. Re: Permission denied when opening a file that was created concurrently by os.rename (Windows) (Dave Angel) 5. Help with pipes, buffering and pseudoterminals (Daniel Ellis) 6. Re: Permission denied when opening a file that was created concurrently by os.rename (Windows) (Terry Reedy) 7. Is it possible to deliver different source distributions for different Python versions? (Dave Hein) 8. ANN: polynice 0.7 - a nice(1) like utility for throttling processes (garabik-news-2005-05 at kassiopeia.juls.savba.sk) 9. Re: Help with pipes, buffering and pseudoterminals (Nobody) 10. Re: Help with pipes, buffering and pseudoterminals (Cameron Simpson) 11. Re: Is it possible to deliver different source distributions for different Python versions? (Steven D'Aprano) 12. XML Parsing (Sepideh Ghanavati) 13. Re: XML Parsing (Ben Finney) 14. Re: Is it possible to deliver different source distributions for different Python versions? (Mark Lawrence) 15. Re: Is it possible to deliver different source distributions for different Python versions? (Stefan Behnel) 16. Re: XML Parsing (Stefan Behnel) 17. Re: OOP for System Administrator (pankaj sharma) 18. implementing pyshark (Michael S.) Hello! I've hit a strange problem that I reduced to the following test case: * Run several python processes in parallel that spin in the following loop: while True: if os.path.isfile(fname): with open(fname, 'rb') as f: f.read() break * Then, run another process that creates a temporary file and then renames it to the name than other processes are expecting * Now, some of the reading processes occasionally fail with "Permission denied" OSError I was able to reproduce it on two Windows 7 64-bit machines. It seems when the file appears on the filesystem it is still unavailable to reading, but I have no idea how it can happen. Both source and destination files are in the same directory, and the destination doesn't exist before calling os.rename. Everything I could find indicates that os.rename should be atomic under this conditions even on Windows, so nobody should be able to observe the destination in unaccessible state. I know that I can workaround this problem by removing useless os.path.isfile() check and wrapping open() with try-except, but I'd like to know the root cause of the problem. Please share you thoughts. The test case is attached, the main file is test.bat. Python is expected to be in PATH. Stderr of readers is redirected to *.log. You may need to run several times to hit the issue. Alexey Izbyshev, research assistant, ISP RAS On Mon, Apr 6, 2015 at 3:45 AM, Alexey Izbyshev wrote: > The test case is attached, the main file is test.bat. Python is expected to > be in PATH. Stderr of readers is redirected to *.log. You may need to run > several times to hit the issue. You have an interesting-looking problem, but the attachment didn't arrive. Is it short enough to include in-line as text in your email? ChrisA On 2015-04-05 20:45, Alexey Izbyshev wrote: > Hello! > > I've hit a strange problem that I reduced to the following test case: > * Run several python processes in parallel that spin in the following > loop: > while True: > if os.path.isfile(fname): > with open(fname, 'rb') as f: > f.read() > break > * Then, run another process that creates a temporary file and then > renames it to the name than other processes are expecting > * Now, some of the reading processes occasionally fail with > "Permission denied" OSError > > I was able to reproduce it on two Windows 7 64-bit machines. It seems > when the file appears on the filesystem it is still unavailable to > reading, but I have no idea how it can happen. Both source and > destination files are in the same directory, and the destination > doesn't exist before calling os.rename. Everything I could find > indicates that os.rename should be atomic under this conditions even > on Windows, so nobody should be able to observe the destination in > unaccessible state. > > I know that I can workaround this problem by removing useless > os.path.isfile() check and wrapping open() with try-except, but I'd > like to know the root cause of the problem. Please share you thoughts. > > The test case is attached, the main file is test.bat. Python is > expected to be in PATH. Stderr of readers is redirected to *.log. You > may need to run several times to hit the issue. > > Alexey Izbyshev, > research assistant, > ISP RAS The attachment was rejected because of .bat files, so here is the code: test.bat: ====================== @echo off del test.txt del *.log for /L %%i in (1, 1, 10) do ( start /b run-with-log.bat %%i ) pythonw test-rename.py ====================== run-with-log.bat: ====================== @echo off pythonw test-read.py %1 >nul 2>%1.log exit ====================== On 04/05/2015 01:45 PM, Alexey Izbyshev wrote: > Hello! > > I've hit a strange problem that I reduced to the following test case: > * Run several python processes in parallel that spin in the following loop: > while True: > if os.path.isfile(fname): > with open(fname, 'rb') as f: > f.read() > break > * Then, run another process that creates a temporary file and then > renames it to the name than other processes are expecting > * Now, some of the reading processes occasionally fail with "Permission > denied" OSError > > I was able to reproduce it on two Windows 7 64-bit machines. It seems > when the file appears on the filesystem it is still unavailable to > reading, but I have no idea how it can happen. Both source and > destination files are in the same directory, and the destination doesn't > exist before calling os.rename. Everything I could find indicates that > os.rename should be atomic under this conditions even on Windows, so > nobody should be able to observe the destination in unaccessible state. > > I know that I can workaround this problem by removing useless > os.path.isfile() check and wrapping open() with try-except, but I'd like > to know the root cause of the problem. Please share you thoughts. > > The test case is attached, the main file is test.bat. Python is expected > to be in PATH. Stderr of readers is redirected to *.log. You may need to > run several times to hit the issue. > > Alexey Izbyshev, > research assistant, > ISP RAS > The attachment is missing; please just include it inline, after reducing it to a reasonably minimal sample. My guess is that the process that does the os.rename is not closing the original file before renaming it. So even though the rename is atomic, the file is still locked by the first process. -- DaveA I have a small little tool I'd like to make. It essentially takes piped input, modifies the text in some way, and immediately prints the output. The problem I'm having is that any output I pipe to the program seems to be buffered, removing the desired effect. >From what I understand, I need to somehow have the input be retrieved via a pseudoterminal. The problem that I'm having is that most examples on the internet seem to assume I would like to launch a program in a forked pty process, which doesn't really fit my use case. I've tried a number of things, but I seem to be unable to get even a basic understanding of how to use the pty module. Here's a piece of code I whipped up just to try to get a feel for what is going on when I use pty.fork, but it doesn't seem to do what I think it should: import pty import os import sys pid, fd = pty.fork() print pid, fd sys.stdout.flush() os.read(fd, 1024) This only seems to print from the parent process. I read that I need to do the os.read call for the fork to happen. I've also tried printing *after* the os.read call. I realize this does very little to solve my overall goal, but I figure understanding what is going on is probably a worthwhile first step. On 4/5/2015 1:45 PM, Alexey Izbyshev wrote: > Hello! > > I've hit a strange problem that I reduced to the following test case: > * Run several python processes in parallel that spin in the following loop: > while True: > if os.path.isfile(fname): > with open(fname, 'rb') as f: > f.read() > break Besides anything else, I would consider adding a minimal sleep in the loop. > * Then, run another process that creates a temporary file and then > renames it to the name than other processes are expecting > * Now, some of the reading processes occasionally fail with "Permission > denied" OSError -- Terry Jan Reedy I would like to distribute a python package with different code for Python 2.* than for Python 3.*. (Mostly this is because of different unicode string handling). There is nothing in to setuptools or PyPi that directly supports this scenario. But perhaps there could be some script run at install time that moves the correct source code to the right location? In other works, if I included both source code versions in the distribution (in a src2 and a src3 subdirectory) then a function invoked at install time could detect the python version and copy the appropriate source code to the right location. Is that at all possible? Is there some install time hook that lets me supply custom installation code? -- Dave Hein polynice is a nice(1)-like command line utility for unix systems to throttle long running processes beyond what can be achieved by nice(1), by repeatedly suspending and resuming the process. It is written for python3, though there is some python2.7 compatibility. Author: Radovan Garab?k URL: http://kassiopeia.juls.savba.sk/~garabik/software/polynice.html License: GPL (v2) Notable changes: * This is the first public release under the name `polynice' - the utility has been renamed from `verynice' due to name clash with an existing software. * polynice can now emulate timeout(1) - terminate process after given time has passed * MacOS X improvements * better python2 compatiblity -- ----------------------------------------------------------- | Radovan Garab?k http://kassiopeia.juls.savba.sk/~garabik/ | | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk | ----------------------------------------------------------- Antivirus alert: file .signature infected by signature virus. Hi! I'm a signature virus! Copy me into your signature file to help me spread! On Sun, 05 Apr 2015 12:20:48 -0700, Daniel Ellis wrote: > This only seems to print from the parent process. I read that I need to > do the os.read call for the fork to happen. I've also tried printing > *after* the os.read call. The child process has its std{in,out,err} attached to the newly-created pty, so that's where the output from the child's "print" goes. You'll see that output if the parent prints the string returned from the os.read() call. On 05Apr2015 12:20, Daniel Ellis wrote: >I have a small little tool I'd like to make. It essentially takes piped input, modifies the text in some way, and immediately prints the output. The problem I'm having is that any output I pipe to the program seems to be buffered, removing the desired effect. That depends on the upstream program; it does the buffering. The pipe itself presents received data downstream immediately. However, as you've seen, almost every program buffers its standard output if the output is not a tty; this is automatic in the stdio C library and results in more fficient use of I/O. >From what I understand, I need to somehow have the input be retrieved via a pseudoterminal. This is a rather gross hack, though sometimes all you can do. While some programs have an option to force unbuffered output, most do not. Attaching their output to a pty is one way to encourage them to at least line buffer their output. However, you should bear in mind that the reason that programs line buffer to a terminal is that they presume they are in an interactive situation with a person watching. The program _may_ act differently in other ways as well, such as asking question it might not otherwise ask in "batch" mode (where it might cautiously not ask and presume "no"). Also, output sent through a pty is subject to the line discipline in the terminal; temrinals are funny things with much historical behaviour. At the least you pobably want your pty in "raw" mode to avoid all sorts of stuff that can be done to your data. >The problem that I'm having is that most examples on the internet seem to assume I would like to launch a program in a forked pty process, which doesn't really fit my use case. Indeed not, but not to worry. You don't need to fork. >I've tried a number of things, but I seem to be unable to get even a basic understanding of how to use the pty module. Have you every used a pty from C? Do you know how ptys work? (master side, slave side, etc). >Here's a piece of code I whipped up just to try to get a feel for what is going on when I use pty.fork, but it doesn't seem to do what I think it should: > > import pty > import os > import sys > > pid, fd = pty.fork() > print pid, fd > sys.stdout.flush() > os.read(fd, 1024) > >This only seems to print from the parent process. The documentation for pty.fork says: Return value is (pid, fd). Note that the child gets pid 0, and the fd is invalid. So the child cannot used "fd". It further says that the child has its stdin and stdout attached to the pty, and that the pty is the child's controlling terminal (this means it is affected by things like "typing" ^C at the pty, etc). >I read that I need to do the os.read call for the fork to happen. I've also tried printing *after* the os.read call. Don't try to adapt fork-based tutorials to your needs. Understand ptys directly first. >I realize this does very little to solve my overall goal, but I figure understanding what is going on is probably a worthwhile first step. What you probably want to use is pty.openpty() instead. No fork. You will get back file descriptors for the master and slave sides of the pty. Then you can use these with the subprocess module to connect your input program. Or, guessing from your opening sentence, you can write a wrapper script whose whole purpose is to run a program on a pty. Regarding terminology: a pseudoterminal (pty) is a device that looks like a traditional serial terminal. All terminal emulators like xterm use one, and so do other programs presenting a terminal session such as the sshd process handling an interactive remote login. When you call pty.openpty() you are handed two file descriptors: one for the master side of the pty and one for the slave side. The slave side is the side that looks like a terminal, and is what a typical use would connect a child process to. The master side is the other side of the pty. When a program writes to the "slave" side, the output is available for read on the master side, much like a pipe. When a program writes to the master side, the output is available for read on the slave side, _as_ _if_ _typed_ at the terminal. A pty is not necessarily going to solve your problem unless you can get your input via the pty. From the sounds of it you're in this situation: command-generating-output | your-program such that your input is attached to a pipe, and because "command-generating-output" is attached to a pipe it is block buffering its output, hence your problem. You can't undo that situation after the fact. To solve your problem via a pty you need to contrive to set up "command-generating-output" already attached to a pty. One way to do that is for "your-program" to open a pty and itself invoke "command-generating-output" with its output via the pty, which is why so many tutorials suppose a "fork" situation. One typical away to do that is to pass the "command-generating-output" command name and args to your program, eg: your-program command-generating-output [args...] Then your main program can gather that up: import sys command_generating_output = sys.argv[1:] Then you can call pty.openpty(), and then use the slave file descriptor with subprocess.Popen to invoke command_generating_output. Thus the generating command will be talking to you via a pty instead of a pipe. Cheers, Cameron Simpson It is interesting to think of the great blaze of heaven that we winnow down to animal shapes and kitchen tools. - Don DeLillo On Mon, 6 Apr 2015 06:38 am, Dave Hein wrote: > I would like to distribute a python package with different code for > Python 2.* than for Python 3.*. (Mostly this is because of different > unicode string handling). > > There is nothing in to setuptools or PyPi that directly supports > this scenario. > > But perhaps there could be some script run at install time that moves > the correct source code to the right location? In other works, if I > included both source code versions in the distribution (in a src2 and > a src3 subdirectory) then a function invoked at install time could > detect the python version and copy the appropriate source code to the > right location. > > Is that at all possible? Is there some install time hook that lets me > supply custom installation code? I'm not aware of any standard solution to that, but I'm not a setuptools expert. setup.py files are Python code, so you can put any code you like in them. But, as far as I am concerned, having the installer pick and choose what source files to include is not a good solution. Instead, you should pick one of these two alternatives: (1) Supply a separate package for 2.x and 3.x, each with their own installer. The installer confirms that it is running under the correct version of Python, and just installs. (2) Or supply a single package with a single installer that works under both 2.x and 3.x. (This is my preference.) One way of handling the second case is to only support 3.3 or better: that way, you can still use u"..." for Unicode strings. Hardly anyone used 3.1 or 3.2, and 3.0 is no longer supported, so it is quite reasonable to insist people upgrade to 3.3. Another way to handle the second case is to use conditional imports: # mymodule2 mystring = u"????" # Also works in 3.3 or better. # mymodule3 mystring = "????" # main application from __future__ import print_function from future_builtins import * if sys.version < '3': import mymodule2 as mymodule else: import mymodule3 as mymodule print(mymodule.mystring) -- Steven Hi, I know basic of python and I have an xml file created from csv which has three attributes "category", "definition" and "definition description". I want to parse through xml file and identify actors, constraints, principal from the text. However, I am not sure what is the best way to go. Any suggestion? Sepideh Ghanavati writes: > I know basic of python and I have an xml file created from csv What XML schema defines the document's format? Without knowing the schema, parsing will be unreliable. What created the document? Why is it relevant that the document was ?created from CSV?? > which has three attributes "category", "definition" and "definition > description". What do you mean by ?attributes?? In Python, an attribute has a specific meaning. In XML, an attribute has a rather different meaning. Neither of those meanings seems to apply to ?the XML document has three attributes?. XML documents don't have attributes; differnt XML elements in a document have different attributes. > I want to parse through xml file and identify actors, constraints, > principal from the text. How are those defined in the document's schema? > However, I am not sure what is the best way to go. Any suggestion? You should: * Learn some more about XML . * Learn exactly what formal document schema defines the document . If the document isn't accompanied by a specification of exactly what its schema is, you're going to have a difficult time. -- \ ?If I melt dry ice, can I swim without getting wet?? ?Steven | `\ Wright | _o__) | Ben Finney On 05/04/2015 21:38, Dave Hein wrote: > I would like to distribute a python package with different code for > Python 2.* than for Python 3.*. (Mostly this is because of different > unicode string handling). > > There is nothing in to setuptools or PyPi that directly supports > this scenario. > > But perhaps there could be some script run at install time that moves > the correct source code to the right location? In other works, if I > included both source code versions in the distribution (in a src2 and > a src3 subdirectory) then a function invoked at install time could > detect the python version and copy the appropriate source code to the > right location. > > Is that at all possible? Is there some install time hook that lets me > supply custom installation code? > > -- > Dave Hein > I can't help directly but have you looked here https://packaging.python.org/en/latest/ ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence Dave Hein schrieb am 05.04.2015 um 22:38: > I would like to distribute a python package with different code for > Python 2.* than for Python 3.*. (Mostly this is because of different > unicode string handling). > > There is nothing in to setuptools or PyPi that directly supports > this scenario. > > But perhaps there could be some script run at install time that moves > the correct source code to the right location? In other works, if I > included both source code versions in the distribution (in a src2 and > a src3 subdirectory) then a function invoked at install time could > detect the python version and copy the appropriate source code to the > right location. > > Is that at all possible? Is there some install time hook that lets me > supply custom installation code? Sure. You can simply change the directory in which distutils looks for your Python code: https://docs.python.org/2/distutils/setupscript.html#listing-whole-packages However, in general, you shouldn't be doing this. It's usually easier (definitely in the long-term) to keep your sources cross-Py2.x/3.x compatible, maybe with the help of tools like "six" or "python-future", than to try to keep separate source trees in sync. http://python-future.org/ Stefan Sepideh Ghanavati schrieb am 06.04.2015 um 04:26: > I know basic of python and I have an xml file created from csv which has > three attributes "category", "definition" and "definition description". > I want to parse through xml file and identify actors, constraints, > principal from the text. However, I am not sure what is the best way to > go. Any suggestion? If it's really generated from a CSV file, you could also parse that instead: https://docs.python.org/3/library/csv.html Admittedly, CSV files are simple, but they also have major problems, especially when it comes to detecting their character encoding and their specific format (tab/comma/semicolon/space/whatever separated, with or without escaping, quoted values, ...). Meaning, you can easily end up reading nonsense from the file instead of the content that was originally put into it. So, if you want to parse from XML instead, use ElementTree: https://docs.python.org/3/library/xml.etree.elementtree.html Stefan Yes Michael, your guess is right. I once was a Java Programmer :) Thanks for the advice and link. Regards, Hi everyone. while trying to implement pyshark, i am getting this error: import pyshark capture = pyshark.LiveCapture(interface='wlan0') capture.sniff(timeout=50) capture i have tried also to run it through interpreter yet, i got this error: import pyshark --------------------------------------------------------------------------- ImportError Traceback (most recent call last) in () ----> 1 import pyshark /usr/local/lib/python2.7/dist-packages/pyshark-0.3.4-py2.7.egg/pyshark/__init__.py in () ----> 1 from pyshark.capture.live_capture import LiveCapture 2 from pyshark.capture.file_capture import FileCapture 3 from pyshark.capture.remote_capture import RemoteCapture 4 from pyshark.capture.inmem_capture import InMemCapture /usr/local/lib/python2.7/dist-packages/pyshark-0.3.4-py2.7.egg/pyshark/capture/live_capture.py in () ----> 1 from pyshark.capture.capture import Capture 2 from pyshark.tshark.tshark import get_tshark_interfaces 3 4 5 class LiveCapture(Capture): /usr/local/lib/python2.7/dist-packages/pyshark-0.3.4-py2.7.egg/pyshark/capture/capture.py in () 1 from __future__ import unicode_literals 2 import os ----> 3 import logbook 4 import sys 5 /usr/local/lib/python2.7/dist-packages/logbook/__init__.py in () 12 13 ---> 14 from logbook.base import LogRecord, Logger, LoggerGroup, NestedSetup, \ 15 Processor, Flags, get_level_name, lookup_level, dispatch_record, \ 16 CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG, NOTSET, \ /usr/local/lib/python2.7/dist-packages/logbook/base.py in () 16 from datetime import datetime 17 from logbook import helpers ---> 18 from logbook.concurrency import thread_get_name, thread_get_ident, greenlet_get_ident 19 20 from logbook.helpers import to_safe_json, parse_iso8601, cached_property, \ /usr/local/lib/python2.7/dist-packages/logbook/concurrency.py in () 29 30 if has_gevent: ---> 31 from gevent._threading import (Lock as ThreadLock, 32 RLock as ThreadRLock, 33 get_ident as thread_get_ident, ImportError: No module named _threading i tried importing threading first and importing pyshark, but the answer was the same. any suggestions? -- https://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon Apr 6 07:52:30 2015 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: Mon, 06 Apr 2015 21:52:30 +1000 Subject: Is it possible to deliver different source distributions for different Python versions? References: Message-ID: <55227381$0$13003$c3e8da3$5496439d@news.astraweb.com> On Mon, 6 Apr 2015 09:23 pm, Dylan Evans wrote: [content snipped] Hi Dylan, and welcome! If you're going to post in response to people's questions, would you please disable HTML email and trim your responses? This mailing list is mirrored on Usenet, as comp.lang.python, where HTML is against the rules of the newsgroup and is considered rude. If you're planning on sticking around as a regular contributor, and we hope you do, the best way to read this group is to use individual emails, not digests. When you reply to a digest, unless you trim the response, we get a copy of the ENTIRE digest, in this case eighteen posts we have already seen before. And then *doubled*, because Gmail sends a plain text version plus a HTML version. So we get 1855 lines of text shoved down our throat, including a massive block of rubbish that looks like this: