Re: [Tutor] run with default value if input not given

2012-10-29 Thread Peter Otten
Saad Javed wrote: > Hi, > > #!/usr/bin/env python > > import sys > > x = 'Saad is a boy' > > def main(x): > a = [] > b = x.split(' ') > for item in b: > a.append(item) > print a > if __name__ == '__main__': > x = sys.argv[1] > main(x) > > > How can I make this program run with the default va

Re: [Tutor] greater precision?

2012-10-29 Thread Peter Otten
John Collins wrote: > Hi, > OS: XP > Py: 2.7.2 > > I am running some old scripts, not written by me, that produce > substantial calculated values data as decimals, 12 significant > figures. AFAIK, the keys calls are; [On modern hardware] Python uses IEEE 754 double-precision

Re: [Tutor] Python Pipes

2012-10-29 Thread Peter Otten
Ganesh Manal wrote: > Please give me sample python program that works with python31 $ touch sample.py $ cat sample.py $ python3 sample.py So the minimal python3 program is an empty file. ___ Tutor maillist - Tutor@python.org To unsubscribe or change

Re: [Tutor] How (not!) lengthy should functions be?

2015-04-16 Thread Peter Otten
boB Stepp wrote: > As I go through my current coding project(s) I find myself breaking > functions down into other functions, especially when I see see > (unnecessarily) duplicated code fragments. I understand this to be > regarded as good practice. However, I am wondering if I am carrying > thing

Re: [Tutor] lists, name semantics

2015-04-18 Thread Peter Otten
Bill Allen wrote: > Everyone that responded, > > Thanks very much for the excellent explanations! The distinction between > a reference to an object and a seperate copy of the object is quite clear > now. You can test your newfound knowledge by predicting the output of the following script:

Re: [Tutor] introspection

2015-04-20 Thread Peter Otten
Alex Kleider wrote: > On 2015-04-20 09:15, Joel Goldstick wrote: >> On Mon, Apr 20, 2015 at 11:24 AM, Alex Kleider >> wrote: >>> Does python provide the introspective ability to retrieve the name to >>> which >>> an object is bound? >>> >>> For example: >>> $ python3 >>> Python 3.4.0 (default, A

Re: [Tutor] calling a method directly

2015-04-21 Thread Peter Otten
Jim Mooney wrote: > Is there any difference between these two since they give the same result, > and when is the second preferred? > x = 'ABE' x.lower() > 'abe' str.lower(x) > 'abe' You may call str.lower() explicitly for subclasses of str. If the subclass overrides the lower() me

[Tutor] Pun panic, was Re: calling a method directly

2015-04-21 Thread Peter Otten
Peter Otten wrote: >>>> class Cow: > ... def lower(self): return "moo" If you can't make sense of that: at the time of writing I thought that "lowering" was a synonym for "mooing". After a look into the dictionary i

Re: [Tutor] Pun panic

2015-04-21 Thread Peter Otten
Ben Finney wrote: > "Martin A. Brown" writes: > >> Good morning underscore underscore Peter underscore underscore, > > The Pythonic way to pronounce that would be “dunder Pete dunder” > https://wiki.python.org/moin/DunderAlias>. > Hm, should I campaign for a peter() builtin?

Re: [Tutor] name shortening in a csv module output

2015-04-23 Thread Peter Otten
Jim Mooney wrote: > I'm trying the csv module. It all went well until I tried shortening a > long first name I put in just to exercise things. It didn't shorten. > Original file lines: > Stewartrewqrhjeiwqhreqwhreowpqhrueqwphruepqhruepqwhruepwhqupr|Dorsey| nec.malesu...@quisqueporttitoreros.co

Re: [Tutor] name shortening in a csv module output

2015-04-23 Thread Peter Otten
Jim Mooney wrote: > .. > >> Ï»¿ >> >> is the UTF-8 BOM (byte order mark) interpreted as Latin 1. >> >> If the input is UTF-8 you can get rid of the BOM with >> >> with open("data.txt", encoding="utf-8-sig") as csvfile: >

[Tutor] Codec lookup, was Re: name shortening in a csv module output

2015-04-25 Thread Peter Otten
Steven D'Aprano wrote: > On Fri, Apr 24, 2015 at 04:34:19PM -0700, Jim Mooney wrote: > >> I was looking things up and although there are aliases for utf_8 (utf8 >> and utf-8) I see no aliases for utf_8_sig, so I'm surprised the utf-8-sig >> I tried using, worked at all. Actually, I was trying to

Re: [Tutor] How to output dictionary data to CSV file

2015-04-28 Thread Peter Otten
Paradox wrote: > I have some data structured like this: > > {'B002':'NRP 2014','B003':'HBB 2015'} > > Basically account numbers and project names, each account number has a > project name. I represent it in a dictionary because that seemed the > best way to keep the account numbers and project

Re: [Tutor] Good Taste Question: Using SQLite3 in Python

2015-04-29 Thread Peter Otten
Jugurtha Hadjar wrote: > I have a class with methods that access a database (SQLite3). I have > included an excerpt showin reading and writing and would like to know if > I'm doing it right. (i.e: Is it bad code and what to improve). > > Here are some improvements and my rationale (check my thi

Re: [Tutor] comparison on Types

2015-04-29 Thread Peter Otten
Ian D wrote: > I was looking at the example code below. I am using python 2.7. > > I am wondering why when I substitute the while n! = "guess" to while n!= > guess (<-- no quotes) I get a problem? What is that problem? The only thing I see is that the > else: > print "you guessed it

Re: [Tutor] subprocess.Popen(..., cwd) and UNC paths

2015-04-29 Thread Peter Otten
Albert-Jan Roskam wrote: > Hello, > > Windows has the 'feature' that the CD command does not work with UNC > paths. So in the code below, I cannot use the 'cwd' parameter of > subprocess.Popen. Therefore I use pushd/popd to accomplish the same > effect. Is there a better way to do this? (other th

[Tutor] Good name(s) for to-be-ignored variables, was Re: Good Taste Question: Using SQLite3 in Python

2015-04-29 Thread Peter Otten
Roel Schroeven wrote: > Alan Gauld schreef op 2015-04-29 18:43: >> On 29/04/15 17:03, Jugurtha Hadjar wrote: >>> http://docs.python-guide.org/en/latest/writing/style/#create-an-ignored-variable >> >> I've seen this before and strongly disagree with it. > > I disagree with your disagreement. I'll

Re: [Tutor] Is there a way to store and later use comparison operators (<, <=, =, >=, >) ?

2015-04-29 Thread Peter Otten
boB Stepp wrote: > On Wed, Apr 29, 2015 at 3:46 PM, Marc Tompkins > wrote: >> On Wed, Apr 29, 2015 at 1:10 PM, boB Stepp >> wrote: >>> >>> Python 2.4.4, Solaris 10. >>> >>> I have some functions that I believe I could collapse into a single >>> function if I only knew how: >>> >>> def choose_com

Re: [Tutor] query

2015-05-01 Thread Peter Otten
Vishakh Rameshan wrote: > i have downloaded and installed python 3.4.3 > and when i just type print "with message to display" it shows missing > paranthesis error > but what i have learnt is that inorder to display message onle print > command and message in "msg" In Python 2 you would write pri

Re: [Tutor] Having Unusual results

2015-05-02 Thread Peter Otten
Jag Sherrington wrote: > Hi Can anyone tell me where I am going wrong the end result doesn't add > up? Leaving out the Python side for a moment let's look again at one part of your problem: You need 20 buns. There are 8 buns in a package. How many packages do you need? You could say 2.5 becau

Re: [Tutor] Python program malfunction

2015-05-04 Thread Peter Otten
Jag Sherrington wrote: > Hi, > There appears to be a problem with this program. > It is taken from the "Starting out with Python" book third edition. > > The problem is when the "validate the wholesale cost" is introduced. > Without the validator the code works fine, but with it the code won't le

Re: [Tutor] Python Help

2015-05-04 Thread Peter Otten
Grace Anne St Clair-Bates wrote: > I am trying to write a program that uses while and if loops, but my print > statements are not going through and showing up when I run the module. I > have tried numerous things and changed in the code and cannot for the life > of me figure out why it won't print

Re: [Tutor] trouble with stringio function in python 3.2

2015-05-04 Thread Peter Otten
anupama srinivas murthy wrote: > Hello, > > My python code needs to run on versions 2.7 to 3.4. To use stringio > function as appropriate, the code i use is; > > if sys.version < '3': > dictionary = io.StringIO(u"""\n""".join(english_words)) > else: > dictionary =

Re: [Tutor] trouble with stringio function in python 3.2

2015-05-04 Thread Peter Otten
Peter Otten wrote: > anupama srinivas murthy wrote: > >> Hello, >> >> My python code needs to run on versions 2.7 to 3.4. To use stringio >> function as appropriate, the code i use is; >> >> if sys.version < '3': >>

Re: [Tutor] split a string inside a list

2015-05-09 Thread Peter Otten
Kayla Hiltermann wrote: > i am trying to make a pythagorean triples checker (a^2 + b^2 = c^2). the > user enters three sides to a triangle and my code determines if it is a > pythagorean triple (aka right triangle) or not. i have the entire code > pretty much done, except i want to account for var

Re: [Tutor] reading lines from a list of files

2015-05-11 Thread Peter Otten
Alex Kleider wrote: > Is there a better (more 'Pythonic') way to do the following? > > for f_name in f_names: > with open(f_name, 'r') as f: > for line in f: There's the fileinput module but persona

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Peter Otten
Alex Kleider wrote: > On 2015-05-11 23:48, Peter Otten wrote: >> Alex Kleider wrote: >> >>> Is there a better (more 'Pythonic') way to do the following? >>> >>> for f_name in f_names: >>> with open(f_name, 'r&#x

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Peter Otten
Oscar Benjamin wrote: > A generator cannot guarantee that execution continues after a yield so > any context manager used around a yield is dependent on __del__. I > think a good rule of thumb is "don't yield from a with block". Uh-oh, I am afraid I did this quite a few times. Most instances seem

Re: [Tutor] reading lines from a list of files

2015-05-12 Thread Peter Otten
Albert-Jan Roskam wrote: > It was not that long ago that I found out about the fileinput module, so I > sometimes forget to use it. It is not specify the encoding of the files, > is it? It'd be nice if one could specify a tuple of encodings, e.g. > ('utf-8-sig', 'latin1', 'cp1252'). > > input(fil

Re: [Tutor] Getting import to use a variable name

2015-05-20 Thread Peter Otten
Jim Mooney Py3.4.3winXP wrote: > On 19 May 2015 at 17:25, Jim Mooney Py3.4.3winXP > wrote: > >> >> If I can get dir to accept x I can parse the output to get rid of the >> __xxx stuff and print it out. >> > > By that I mean dir will give me a list of strings I can then use __doc__ > on to get a

Re: [Tutor] Is there a way to use "with" across suite boundaries?

2015-05-22 Thread Peter Otten
Jim Mooney Py3.4.3winXP wrote: > '''I was using with open...:, but I'm printing a header in one function, > calling a looping > function to print detail lines, then returning to the calling function to > print > the footer. But that didn't work since the with statement only seems to > work with th

Re: [Tutor] Is there a way to use "with" across suite boundaries?

2015-05-23 Thread Peter Otten
Jim Mooney Py3.4.3winXP wrote: > function. Is there > a way around this, other than passing the file as I have here? Also, is it > a good idea to pass a file handle like that or is there a better way? Not necessarily better, but instead of passing around the file you can rewrite your code to use

Re: [Tutor] Extracting data between strings

2015-05-27 Thread Peter Otten
richard kappler wrote: > I'm writing a script that reads from an in-service log file in xml format > that can grow to a couple gigs in 24 hours, then gets zipped out and > restarts at zero. My script must check to see if new entries have been > made, find specific lines based on 2 different start

Re: [Tutor] Yielding from a with block

2015-05-28 Thread Peter Otten
Oscar Benjamin wrote: > On 23 May 2015 at 10:52, Peter Otten <__pete...@web.de> wrote: > I'm just wondering what other people think about this. Should code > like make_lines below be discouraged? >> def make_lines(): >> with open('co2-sample.c

Re: [Tutor] unittest with random population data

2015-05-30 Thread Peter Otten
Sydney Shall wrote: > MAC OSX 10.10.3 > Enthought Python 2.7 > > I am an almost beginner. > > Following advice from you generous people, I have chosen a project that > interests me, to develop some knowledge of python. > My projest is a simulation of a biological population. > I have a base clas

Re: [Tutor] Req Assistance with 1st Project.

2015-05-31 Thread Peter Otten
Nym City via Tutor wrote: > Hello all, > I am working on my first Python project and I need some help. Here is what > my project is about. I want to be able to read in from a text file that > contains a list of IP addresses (maybe even hostnames) and write out each > IP in the list in the followin

Re: [Tutor] Retain UTF-8 Character in Python List

2015-06-01 Thread Peter Otten
Boy Sandy Gladies Arriezona wrote: > Hi, it's my first time in here. Welcome! > I hope you don't mind if I straight to the > question. > I do some work in python 2 and my job is to collect some query and then > send it to java program via json. We're doing batch update in Apache > Phoenix, that

Re: [Tutor] Should error checking be duplicated for both functions if one function calls another one?

2015-06-01 Thread Peter Otten
boB Stepp wrote: > On Mon, Jun 1, 2015 at 9:33 AM, David Palao > wrote: >> Hello, >> Not sure if I got it, but, in my opinion functions should do only one >> thing.So if function 2 finds an error, it should raise it. There >> should be another function (function 1 in your case?) taking care of >

Re: [Tutor] creating a dictionary for capital quiz program

2015-06-02 Thread Peter Otten
Stephanie Quiles wrote: > Good evening, > > As you may have noticed i am really struggling with functions and > dictionaries. I need to figure out why this program is allowing me to > continue entering incorrect data instead of telling me my answer is > incorrect. also at the end it’s not tallyin

Re: [Tutor] creating a dictionary for capital quiz program

2015-06-02 Thread Peter Otten
ZBUDNIEWEK. JAKUB wrote: > I'm a newbie, but was able to tune it to correctly reply to user inputs. > 2. Why (on Windows) do I have to give inputs in quotes not to cause an > error (for ll input the error is ' NameError: name 'll' is not defined')? If you are running the script under Python 2 yo

Re: [Tutor] creating a dictionary for capital quiz program

2015-06-02 Thread Peter Otten
Alan Gauld wrote: > On 02/06/15 15:15, Peter Otten wrote: > >> Not an optimization, but if the user enters neither Y nor N you might ask >> again instead of assuming Y. > > He does. He only breaks if the user enters N > >>> choice =

Re: [Tutor] string delimiters

2015-06-03 Thread Peter Otten
richard kappler wrote: > Perhaps the better way for me to have asked this question would have been: > > How can I find the location within a string of every instance of a > character such as ']'? >>> import re >>> s = "alpha]beta]gamma]delta" >>> [m.start() for m in re.finditer(r"]", s)] [5, 10,

Re: [Tutor] dictionary of lists

2015-06-04 Thread Peter Otten
Chris Stinemetz wrote: > Although I am certain it is not very efficient I was able to > accomplish what I wanted with the following code I wrote: > > import os > import pprint > import csv > from collections import defaultdict > > print_map = {'MOU':0, 'Call_Att':1, 'Device':2} > header = ['IME

Re: [Tutor] Sorting a list of list

2015-06-05 Thread Peter Otten
Stephen Nelson-Smith wrote: > As part of my league secretary program (to which thread I shall reply > again > shortly), I need to sort a list of lists. I've worked out that I can use > sorted() and operator.itemgetter to sort by a value at a known position in > each list. Is it possible to do th

Re: [Tutor] python 3.4 documentation

2015-06-14 Thread Peter Otten
Alex Kleider wrote: > I'm using python 3.4 on an ubuntu 14.4 LTS OS and frequently find myself > 'off line'. > I'd like to download the standard library documentation to have at hand > on my hard drive. > I tried > prompt> wget -r https://docs.python.org/3/library/index.html > but links appear to

Re: [Tutor] How to return a list in an exception object?

2015-06-17 Thread Peter Otten
David Aldrich wrote: > Hi > > I have a function that compares a set of files with a reference set of > files. All files are compared and then, if any differences were found, an > exception is raised: > > class Error(Exception): pass > > def check_results(file_list): > > > > if isDif

Re: [Tutor] Attacking this problem (2 parts):JSON object to CSV file

2015-06-19 Thread Peter Otten
Alan Gauld wrote: > On 19/06/15 02:44, Saran Ahluwalia wrote: > >> However with the example JSON object (above) i receive the following >> error when applying this function: >> >> ValueError: too many values to unpack >> > > Please always include the full error trace not just the final line. >

Re: [Tutor] removing nodes using ElementTree

2015-06-29 Thread Peter Otten
street.swee...@mailworks.org wrote: > Hello all, > > I'm trying to merge and filter some xml. This is working well, but I'm > getting one node that's not in my list to include. Python version is > 3.4.0. > > The goal is to merge multiple xml files and then write a new one based > on whether or

Re: [Tutor] Are the methods in a class copied or just linked to?

2015-07-02 Thread Peter Otten
Danny Yoo wrote: > On Thu, Jul 2, 2015 at 12:30 PM, Jim Mooney Py3.4.3winXP > wrote: >> When an instance uses a class method, does it actually use the method >> that is in the class object's memory space, or is the method copied to >> the instance? > Unsure. How would it be observable? [snip i

Re: [Tutor] Variable reference

2015-07-07 Thread Peter Otten
Alan Gauld wrote: > On 07/07/15 01:18, Danny Yoo wrote: >> I'd also add that the 'del' statement has near-zero utility. >> >> 'del' is a language blemish. It should not be used by beginners, >> because it asks them to try to manually manage the lifetime of their >> variable names. That's an unre

Re: [Tutor] Variable reference

2015-07-07 Thread Peter Otten
Steven D'Aprano wrote: > On Tue, Jul 07, 2015 at 04:08:30PM +0200, Peter Otten wrote: > >> For dicts and lists a method would work as well. Even now you can write >> >> items.pop(index) # instead of del items[index] >> lookup.pop(key) # del lookup[key] >&

Re: [Tutor] Variable reference

2015-07-08 Thread Peter Otten
Steven D'Aprano wrote: > When writing a script or application, name management is not a big deal. > But in a library, temporary variables are pollution. They make it harder > for the users of your library to tell what's part of the API and what > isn't, and they make "from module import *" less

Re: [Tutor] for loop for long numbers

2015-08-03 Thread Peter Otten
Dima Kulik wrote: > I want to make a for loop with a huge numbers. > for example: > > for i in range (0,90): some_code() > but range and xrange cant operate with such big numbers. > Can some on help me? If you are just beginning with Python and do not require any libraries that are

Re: [Tutor] for loop for long numbers

2015-08-04 Thread Peter Otten
Peter Otten wrote: > But note that if some_code() takes 1 microsecond the loop will run for > about Sorry, that should be millisecond (1/1000 s). >>>> 90/60/60/24/1000 > 104.17 > > 104 days. _

Re: [Tutor] scratching my head

2015-08-04 Thread Peter Otten
Laura Creighton wrote: > In a message of Mon, 03 Aug 2015 18:22:32 +1000, Cameron Simpson writes: > >>That depends. This is the tutor list; we're helping Clayton debug his code >>as an aid to learning. While it's good to know about the facilities in the >>standard library, pointing him directly a

Re: [Tutor] scratching my head - still

2015-08-05 Thread Peter Otten
Cameron Simpson wrote: > On 05Aug2015 12:46, Steven D'Aprano wrote: >>On Tue, Aug 04, 2015 at 05:52:15PM -0700, Clayton Kirkwood wrote: >>> As seen below (closely), some filenames are not being removed while >>> others are, such as in the first stanza, some pdfs are removed, some >>> aren't. In t

Re: [Tutor] Plotting asymmetric error bars for a single point in matplotlib

2015-08-05 Thread Peter Otten
Colin Ross wrote: > Hi all, > > Goal: To plot asymmetric x error bars for a single point using errorbar. I > am interested in displaying the inter quartile range (IQR) for a data set. > > Code: > > import numpy as np > import matplotlib.pyplot as plt > > y = 1.0 > data = np.random.rand(100) >

Re: [Tutor] Problem on select esecution of object in a class

2015-08-05 Thread Peter Otten
jarod_v6--- via Tutor wrote: > I have a class with many objects and I want to select using opt parse > some function id = options.step > ena = Rnaseq(options.configura, options.rst, options.outdir) > now = datetime.datetime.now() > ena.show() > diz = {} > f

Re: [Tutor] a few question about my evolving program

2015-08-12 Thread Peter Otten
Clayton Kirkwood wrote: > Look here: After some clean-up: > if current_filename in target_directory_file_list: >current_stat_info = os.stat( >current_directory_path + '/' + current_filename, >follow_symlinks=False) >current_file_size = current_stat_in

Re: [Tutor] Searching through files for values

2015-08-14 Thread Peter Otten
Jason Brown wrote: > (accidentally replied directly to Cameron) > > Thanks, Cameron. It looks like that value_file.close() tab was > accidentally tabbed when I pasted the code here. Thanks for the > suggestion > for using 'with' though! That's will be handy. > > To test, I tried manually spec

Re: [Tutor] How to skip a single file when using shutil.make_archive()

2015-08-14 Thread Peter Otten
Anthony Papillion wrote: > I'm creating an archive of a directory using shutil.make_archive and need > to skip a single file if it is present in that directory. Is there a way > to do this or should I be looking to ZipFile to meet this need? I should not post this, especially on a tutor list, but

Re: [Tutor] variable existence q

2015-08-15 Thread Peter Otten
Clayton Kirkwood wrote: > 10 top_directory = "/users/Clayton/Pictures" > > def override_defaults(): > 56 return( top_directory, filetypes, target_directory ) > > 80 top_directory, filetypes, target_directory = override_defaults() > > > File "C:/Users/Clayton/python/find picture

Re: [Tutor] variable existence q

2015-08-16 Thread Peter Otten
Clayton Kirkwood wrote: >> > Above is the actual code. Clayton, try to understand how scoping works in Python before you go back to your actual code. Can you predict what the following snippet will produce? x = "first global" def f(): return x def g(): return x x = "local" x = "

Re: [Tutor] Test discovery not locating module to test

2015-08-20 Thread Peter Otten
boB Stepp wrote: > W7 64-bit. Py 3.4.3 > > unittest result: > > E:\Projects\mcm>python -m unittest > E > == > ERROR: test.db.test_mcm_db_mgr (unittest.loader.ModuleImportFailure) > --

Re: [Tutor] Request for help with os.walk() combining os.path.ismount() in Linux

2015-08-20 Thread Peter Otten
Laura Creighton wrote: > In a message of Thu, 20 Aug 2015 09:25:29 +0100, Alan Gauld writes: >> >>It always amazes me how often the same issues come up in >>rapid succession. We must have had 4 or 5 of these types >>of errors in the last couple of months, and other times >>we go for 6 months or mo

Re: [Tutor] Test discovery not locating module to test

2015-08-20 Thread Peter Otten
boB Stepp wrote: > On Thu, Aug 20, 2015 at 2:37 AM, Peter Otten <__pete...@web.de> wrote: > >> Assuming E:/Projects/mcm/src is in the PYTHONPATH mcm_db_mgr.py (what an >> alphabet soup!)... > > Written out in full, this would be > > montessori_classroo

Re: [Tutor] Do not understand why test is running.

2015-08-20 Thread Peter Otten
boB Stepp wrote: > On Thu, Aug 20, 2015 at 10:13 PM, Steven D'Aprano > wrote: >> On Thu, Aug 20, 2015 at 09:01:50PM -0500, boB Stepp wrote: >> >> >>> import unittest >>> >>> # import modules to be tested: >>> import mcm.db.manager >>> >>> class ManagerTestCase(unittest.TestCase): >>> def setU

Re: [Tutor] Do not understand why test is running.

2015-08-22 Thread Peter Otten
boB Stepp wrote: > In the cold light of morning, I see that in this invocation, the path > is wrong. But even if I correct it, I get the same results: > > e:\Projects\mcm>py -m unittest ./test/db/test_manager.py [...] > ValueError: Empty module name Make sure that there are files ./test/__init

Re: [Tutor] Do not understand why test is running.

2015-08-23 Thread Peter Otten
boB Stepp wrote: > On Sat, Aug 22, 2015 at 3:18 AM, Peter Otten <__pete...@web.de> wrote: >> boB Stepp wrote: >> >>> In the cold light of morning, I see that in this invocation, the path >>> is wrong. But even if I correct it, I get the same results: >&g

Re: [Tutor] Help error 504

2015-08-25 Thread Peter Otten
Gonzalo V wrote: > i am new in python and its very intuitive! but i am in problems with that > code. As Danny already explained an exception can only be caught if the function is inside a try ... except ...: try: req = urllib.request.urlopen(...) except urllib.error.HTTPError as e: ...

Re: [Tutor] Help error 504

2015-08-25 Thread Peter Otten
Timo wrote: > Op 25-08-15 om 02:08 schreef Gonzalo V: >> how can simulate or emulate an error 504? > I think using a site like http://httpbin.org/ is a bit easier than mock > or your own server. > Just change your request url to: > http://httpbin.org/status/504 Yes, that's nice! Plus, it's

Re: [Tutor] Help error 504

2015-08-26 Thread Peter Otten
Danny Yoo wrote: > Unit tests that depend on external dependencies can be "flaky": they > might fail for reasons that you don't anticipate. I'd recommend not > depending on an external web site like this: it puts load on someone > else, which they might not appreciate. If you have a well-defined

Re: [Tutor] value range checker

2015-08-27 Thread Peter Otten
Albert-Jan Roskam wrote: > I have a written a function checks the validity of values. The ranges of > valid values are stored in a database table. > > Such a table contains three columns: category, min and max. One record of > such a table specifies the range for > > a certain category, but a ca

Re: [Tutor] How should my code handle db connections? Should my db manager module use OOP?

2015-08-27 Thread Peter Otten
boB Stepp wrote: > My ongoing project will be centered around an SQLite db. Since almost > all data needed by the program will be stored in this db, my thought > is that I should create a connection to this db shortly after program > startup and keep this connection open until program closure. I

Re: [Tutor] Creating lists with definite (n) items without repetitions

2015-09-03 Thread Peter Otten
Marcus Lütolf wrote: > as a newcomber I want to create a set of lists containing n items, for > example n = 3: (['a','b','c'], ['a','d','e']...). > The sequence of items in each list should be different. If the letters > 'a''z' are used and n = 3 there is a maximum of 301 lists. > The

Re: [Tutor] Syntax error and EOL Error

2015-09-04 Thread Peter Otten
Nym City via Tutor wrote: > import csv > DomainList = [] > > domains = open('domainlist.csv', 'r') > DomainList = csv.reader(domains) > DomainList = [column[1] for column in DomainList] > DomainList = (str(DomainList).rstrip('/') > print('\n'.join(DomainList)) > > > I keep getting EOL error on

Re: [Tutor] Creating lists with definite (n) items without repetitions

2015-09-05 Thread Peter Otten
marcus lütolf wrote: > Hello Peter, hello Martin, > many thanks for your very quick response !!! > > As for Peter's advice: > >> At first I thought you might want itertools.combinations() >> > import string, itertools > for t in itertools.combinations(string.ascii_lowercase, 3): >> ...

Re: [Tutor] Creating lists with definite (n) items without repetitions

2015-09-05 Thread Peter Otten
Peter Otten wrote: [proofreading goof] > OK, you want all triples where no pair inside a triple is repeated. > There are 325 pairs, and one triple uses up three pairs. > That puts an upper limit of 325/3 = 108 on the number of triples. > You then have to find all sets of three &

Re: [Tutor] Fwd: find second occurance of string in line

2015-09-08 Thread Peter Otten
richard kappler wrote: >> Do you want to find just the second occurence in the *file* or the second > occurence within a given tag in the file (and there could be multiple such > tags)? > > There are multiple objectdata lines in the file and I wish to find the > second occurence of timestamp in e

Re: [Tutor] Fwd: find second occurance of string in line

2015-09-08 Thread Peter Otten
Albert-Jan Roskam wrote: >> import lxml.etree >> >> tree = lxml.etree.parse("example.xml") >> print tree.xpath("//objectdata/general/timestamp/text()") > > Nice. I do need to try lxml some time. Is the "text()" part xpath as well? Yes. I think ElementTree supports a subset of XPath. __

Re: [Tutor] find second occurance of string in line

2015-09-09 Thread Peter Otten
richard kappler wrote: > On Tue, Sep 8, 2015 at 1:40 PM, Peter Otten <__pete...@web.de> wrote: >> I'm inferring from the above that you do not really want the "second" >> timestamp in the line -- there is no line left intace anyway;) -- but >> rather

Re: [Tutor] More Pythonic?

2015-09-09 Thread Peter Otten
richard kappler wrote: > Would either or both of these work, if both, which is the better or more > Pythonic way to do it, and why? > > ### > > import whatIsNeeded > > writefile = open("writefile", 'a') > > with open(readfile, 'r') as f: > for line in f: > if ke

Re: [Tutor] Fwd: find second occurance of string in line

2015-09-09 Thread Peter Otten
Albert-Jan Roskam wrote: >> To: tutor@python.org >> From: __pete...@web.de >> Date: Tue, 8 Sep 2015 21:37:07 +0200 >> Subject: Re: [Tutor] Fwd: find second occurance of string in line >> >> Albert-Jan Roskam wrote: >> >> >> import lxml.etree >> >> >> >> tree = lxml.etree.parse("example.xml") >>

Re: [Tutor] More Pythonic?

2015-09-09 Thread Peter Otten
Timo wrote: > Op 09-09-15 om 15:41 schreef Steven D'Aprano: >> On Wed, Sep 09, 2015 at 09:05:23AM -0400, richard kappler wrote: >>> Would either or both of these work, if both, which is the better or more >>> Pythonic way to do it, and why? >> The first works, but isn't really the best way to do i

Re: [Tutor] Calling instance method in IDLE magically calls __len__?

2015-09-18 Thread Peter Otten
Dino Bektešević wrote: > Hello, > > For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and > at https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the > case spaces get messed up). > > class Errors: > def __init__(self): > pass > def toFile(self

Re: [Tutor] [newbie] import error after restart (virtualenv)

2015-09-19 Thread Peter Otten
David wrote: > Dear Tutors, > > I am reading through Harry Percival's "Test-Driven Development with > Python". > > As I finished chapter 3 yesterday, I was fully on track, perfectly > aligned with the book. > > Today I restarted my computer, activated the virtualenv in question -- > and get an

Re: [Tutor] Creating a match file

2015-09-21 Thread Peter Otten
EILEEN CHURCH CARSON wrote: > I want to write a program that reads in data from two csv files with 6 > columns each. I want it to determines if the data in the first two columns > is the same, and if so read all six columns in that line from each of the > two files into a 12 column output file. >

Re: [Tutor] stx, etx (\x02, \x03)

2015-09-22 Thread Peter Otten
richard kappler wrote: [Richard, please don't top-post. Thank you] > On Tue, Sep 22, 2015 at 8:58 AM, Mark Lawrence > wrote: >> On 22/09/2015 13:37, richard kappler wrote: >>> I have tried: >>> #!/usr/bin/env python >>> >>> with open('input/test.xml', 'r') as f1: >>> with open('mod1.xml'

Re: [Tutor] tkinter create a frame and whats the parent

2015-09-23 Thread Peter Otten
David Niklas wrote: > Hello, I was following the doc, and in the beginning he gives an > example where he creates a frame and accesses the main window by the > name of Master. > > http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf I suppose you are asking about """ #!/usr/bin/env python

Re: [Tutor] skip/slice more than every second?

2015-09-29 Thread Peter Otten
questions anon wrote: > thankyou but I just realised I wrote the question wrong - > > how do I do the inverse of above > so > hide 1 show 2,3,4 hide 5, show 6,7,8 etc. > > thanks in advance You can use del on a slice: >>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] >>> del a[::4] >>> a [2, 3, 4, 6,

Re: [Tutor] Beautiful Soup

2015-09-29 Thread Peter Otten
Crusier wrote: > I have recently finished reading "Starting out with Python" and I > really want to do some web scraping. Please kindly advise where I can > get more information about BeautifulSoup. It seems that Documentation > is too hard for me. If you tell us what you don't understand and wh

Re: [Tutor] Cumulative distribution function

2015-10-01 Thread Peter Otten
Michel Guirguis wrote: [Please don't start a new thread for an old problem] > I have a problem with the cumulative distribution function in calculating > derivatives. I am getting a call option figure of 2.5961 while it should > be 2.9081. Could you please help. > >>> S=50 > >>> K=50 > >>> r=0.0

[Tutor] How to write a teller machine simulation, was Re: Python 3.5 Question

2015-10-09 Thread Peter Otten
Sasuke Uchiha wrote: > Hi, I would like to know how to create a code similar to the result below. > I want to input a number like 1000, and for it to give me the maximum > output with the least amount of bills (such as 100s) > >- > >The bill amounts you have are 100s, 50s, 20s, 10s, 5s,

Re: [Tutor] How to read all integers from a binary file?

2015-10-09 Thread Peter Otten
David Aldrich wrote: > Hi > > I have a binary file of 32-bit unsigned integers. I want to read all those > integers into a list. > > I have: > > ulData = [] > with open(ulBinFileName, "rb") as inf: > ulData.append(struct.unpack('i', inf.read(4))) > > This obviously reads only o

Re: [Tutor] how to unittest cli input

2015-10-11 Thread Peter Otten
Alex Kleider wrote: > It'll take more studying on my part before I'll be able to implement > Ben's suggestion. I find Ben's example instructive, but when you're just starting you might prefer a simpler approach: import unittest from unittest import mock import collect class TestCollectData(u

Re: [Tutor] Guidance on using custom exceptions please

2015-10-13 Thread Peter Otten
David Aldrich wrote: > Consider a 'send' method that sends a message to another system via a > socket. This method will wait for a response before returning. There are > two possible error conditions: > > > 1) Timeout - i.e. no response received > > 2) Illegal response received > >

Re: [Tutor] how to unittest cli input

2015-10-14 Thread Peter Otten
Alex Kleider wrote: > On 2015-10-13 14:44, Alex Kleider wrote: >> On 2015-10-13 12:11, Danny Yoo wrote: >> >> >>> ## >>> def make_ask(f, l, p): >>> d = {'Enter your first name: ' : f, >>>'Enter your last name: ' : l, >>>'Your mobile phone #: ' : p}

Re: [Tutor] how to unittest cli input

2015-10-15 Thread Peter Otten
Alex Kleider wrote: > On 2015-10-14 12:27, Peter Otten wrote: >> Alex Kleider wrote: >> >>> On 2015-10-13 14:44, Alex Kleider wrote: >>>> On 2015-10-13 12:11, Danny Yoo wrote: >>>> >>>> >>>>> #

Re: [Tutor] File operation query

2015-10-15 Thread Peter Otten
Reuben wrote: > I need some clarification for below code. In line 2 of below code snippet, > I have provided read and write permission. Assuming I have provided some > string input as requested in line 1 - when I try to open "check.txt" file > after running the script, it is always empty - it does

Re: [Tutor] Changes to dbm module/file format

2015-10-16 Thread Peter Otten
Alan Gauld wrote: > Has anyone noticed a change to the DBM file format or is it an OS > specific thing? Last time I used dbm it was on a Windoze box with > Python 3.3 and it generated sets of 3 files for each 'database' > created. (I think this is what it did under v2 as well?) > > I just used it

<    1   2   3   4   5   6   7   8   9   10   >