__deepcopy__ without recursive copies?
#!/usr/bin/env python import sys import copy ''' How to define __deepcopy__ with out causing recursive calls to copies of self? Bernie Day 01/25/05 I was using deepcopy on DeviceManager and this worked well. When I defined __deepcopy__, so I could have a handle to the children of the Master device Manager I got into trouble. I see that there is a method using a dic to limit this, but I can't seem to get it to work. This is not the code I was using but should represent it well. Thanks! ''' class DeviceManager: def __init__(self,devFile): DevFile = open(devFile) devList = [Device(line) for line in DevFile] #etc, etc... def __deepcopy__(self): miniMe = copy.deepcopy(self) miniMe.devList = tuple(devList) return miniMe class Device: def __init__(self,line): self.copyies = [] #do something with line here def __deepcopy__(self): miniMe = copy.deepcopy(self) self.copyies.append(miniMe) return miniMe DevMan1 = DeviceManager(devfile) devMan2 = copy.deepcopy(DevMan1) -- http://mail.python.org/mailman/listinfo/python-list
Wow!
That is clever, gives a lot of insight into how the __dict__ == the object. This is somewhat like the solution I am using from the Cookbook, an Empty object copy. This is cleaner and very much more concise. Thank you! -- http://mail.python.org/mailman/listinfo/python-list
The Python: Rag October issue available
The Python: Rag October issue available The October issue of The Python: Rag is available at: http://www.pythonrag.org A monthly, free, community run, Python magazine - issues are in pdf format, intended for anyone interested in Python, without being particularly serious. If you have anything you would like to say about Python, please contribute. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python: Rag October issue available
On Fri, 02 Oct 2009 11:32:41 -0700, steven.oldner wrote: Hi, no -its just put on the website. Unless there's a method you can suggest? Cheers Bernie > On Oct 2, 11:14 am, Bernie wrote: >> The Python: Rag October issue available >> >> The October issue of The Python: Rag is available at: >> >> http://www.pythonrag.org >> >> A monthly, free, community run, Python magazine - issues are in pdf >> format, intended for anyone interested in Python, without being >> particularly serious. If you have anything you would like to say about >> Python, please contribute. > > Thanks! Any way to subscribe to it? -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python: Rag October issue available
On Sat, 03 Oct 2009 20:09:18 -0700, TerryP wrote: > On Oct 3, 4:29 pm, Bernie wrote: >> Hi, no -its just put on the website. Unless there's a method you can >> suggest? > > Not to butt in, but off the top of my head, you could probably set up a > mailing list and post the link to the file every cycle - simple but > effective. Yes, good suggestion - I've had a look at google groups and they seem to provide comprehensive facilities. I'll set one up. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python: Rag October issue available
On Sun, 04 Oct 2009 07:37:35 -0500, Bernie wrote: > On Sat, 03 Oct 2009 20:09:18 -0700, TerryP wrote: > >> On Oct 3, 4:29 pm, Bernie wrote: >>> Hi, no -its just put on the website. Unless there's a method you can >>> suggest? >> >> Not to butt in, but off the top of my head, you could probably set up a >> mailing list and post the link to the file every cycle - simple but >> effective. > > Yes, good suggestion - I've had a look at google groups and they seem to > provide comprehensive facilities. I'll set one up. And here it is: http://groups.google.co.uk/group/pythonrag -- http://mail.python.org/mailman/listinfo/python-list
The Python: Rag November issue available
The November issue of The Python: Rag is available at: http://www.pythonrag.org A monthly, free, community run, Python magazine - issues are in pdf format, intended for anyone interested in Python. -- http://mail.python.org/mailman/listinfo/python-list
The Python: Rag September issue available
The September issue of The Python: Rag is available at: http://www.pythonrag.org A monthly, free, community run, Python magazine - issues are in pdf format, intended for anyone interested in Python, without being particularly serious. If you have anything you would like to say about Python, please contribute. -- http://mail.python.org/mailman/listinfo/python-list
Re: print syntax
On Thu, 03 Sep 2009 12:22:14 -0400, doug wrote: > I am new to python, working by way through 'Core Python Programming'. I > can find no description of using print with the built-in type for > formatting. I think I have got some [most?] of it from Chun, google, and > python.org. My comment is - it should not be that hard to find. I would > suggest a link from the print syntax section. > > What is seems to be is: > >print "format-spec" % (variable-list) > > I assume the '%' is required token. > > > > _ > Douglas Denault > http://www.safeport.com > d...@safeport.com > Voice: 301-217-9220 >Fax: 301-217-9277 You say "using print with the built-in type for formatting." - It's two separate things, print just prints a string. You can use the formatting quite separately on the string first. my_string = "%s %s %s" % (1, 2, 3)# creates string "1 2 3" print my_string so you probably would find difficulty is searching for 'print formatting', you should be looking for string formatting. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie problem with urllib.request.urlopen
On Tuesday, September 26, 2017 at 12:32:18 PM UTC-3, Bernie Connors wrote: > Hello, > > My first post here on C.L.P. I have only written a few python scripts > in 2.7 and now I'm trying my first python 3 script. Can you tell me why this > snippet won't run? > --- > from urllib.request import urlopen > > with > urlopen('http://geonb.snb.ca/arcgis/rest/services/GeoNB_SNB_Parcels/MapServer/0/query?outSR=4617&f=JSON&where=PID='75385120'') > as conn: > print(conn) > --- > Thanks, > Bernie. Thomas, The PID parameter at the end of my url must be enclosed in single quotes, '75385120', or the API won't execute the query. I have the code in a python notebook on Azure - https://notebooks.azure.com/n/n31C2DSCOr8/notebooks/URLopen%20Test.ipynb Here are the error messages I am getting: --- HTTPError Traceback (most recent call last) in () 1 from urllib.request import urlopen > 2 with urlopen("http://geonb.snb.ca/arcgis/rest/services/GeoNB_SNB_Parcels/MapServer/0/query?outSR=4617&f=JSON&where=PID='75385120'") as conn: 3 print(conn) ~/anaconda3_410/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 160 else: 161 opener = _opener --> 162 return opener.open(url, data, timeout) 163 164 def install_opener(opener): ~/anaconda3_410/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout) 469 for processor in self.process_response.get(protocol, []): 470 meth = getattr(processor, meth_name) --> 471 response = meth(req, response) 472 473 return response ~/anaconda3_410/lib/python3.5/urllib/request.py in http_response(self, request, response) 579 if not (200 <= code < 300): 580 response = self.parent.error( --> 581 'http', request, response, code, msg, hdrs) 582 583 return response ~/anaconda3_410/lib/python3.5/urllib/request.py in error(self, proto, *args) 507 if http_err: 508 args = (dict, 'default', 'http_error_default') + orig_args --> 509 return self._call_chain(*args) 510 511 # XXX probably also want an abstract factory that knows when it makes ~/anaconda3_410/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args) 441 for handler in handlers: 442 func = getattr(handler, meth_name) --> 443 result = func(*args) 444 if result is not None: 445 return result ~/anaconda3_410/lib/python3.5/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs) 587 class HTTPDefaultErrorHandler(BaseHandler): 588 def http_error_default(self, req, fp, code, msg, hdrs): --> 589 raise HTTPError(req.full_url, code, msg, hdrs, fp) 590 591 class HTTPRedirectHandler(BaseHandler): HTTPError: HTTP Error 403: Forbidden -- Thanks, Bernie. -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie problem with urllib.request.urlopen
On Tuesday, September 26, 2017 at 12:32:18 PM UTC-3, Bernie Connors wrote: > Hello, > > My first post here on C.L.P. I have only written a few python scripts > in 2.7 and now I'm trying my first python 3 script. Can you tell me why this > snippet won't run? > --- > from urllib.request import urlopen > > with > urlopen('http://geonb.snb.ca/arcgis/rest/services/GeoNB_SNB_Parcels/MapServer/0/query?outSR=4617&f=JSON&where=PID='75385120'') > as conn: > print(conn) > --- > Thanks, > Bernie. Peter Otten, Yes that seems to work better. Thanks for the tips. But I see now that I am getting some http errors when I try to run this code from the Microsoft Azure Notebooks. Here is the URL to my Note Book: https://notebooks.azure.com/n/n31C2DSCOr8/notebooks/URLopen%20Test.ipynb And here is the error: URLError: Does anybody know if something can be done about this with urllib? Or is this completely related to the firewall rules at http://geonb.snb.ca?? Thanks, Bernie. -- https://mail.python.org/mailman/listinfo/python-list
Extracting and summing student scores from a JSON file using Python 2.7.10
This should be a simple problem but I have wasted hours on it. Any help would be appreciated. [I have taken my code back to almost the very beginning.] The student scores need to be summed. import json import urllib url = "http://www.wickson.net/geography_assignment.json"; response = urllib.urlopen(url) data = json.loads(response.read()) lst1 = list(data.items()) print lst1 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extracting and summing student scores from a JSON file using Python 2.7.10
On Monday, 9 November 2015 19:30:23 UTC-5, MRAB wrote: > On 2015-11-09 23:52, Bernie Lazlo wrote: > > This should be a simple problem but I have wasted hours on it. Any help > > would be appreciated. [I have taken my code back to almost the very > > beginning.] > > > > The student scores need to be summed. > > > > import json > > import urllib > > url = "http://www.wickson.net/geography_assignment.json"; > > response = urllib.urlopen(url) > > data = json.loads(response.read()) > > lst1 = list(data.items()) > > print lst1 > > > Do it a step at a time. > > It's a list, so start with indexing. MRAB: I think of the file as two lists. The second list appears to be a list of tuples containing "names" and "scores". How would you index or extract those. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extracting and summing student scores from a JSON file using Python 2.7.10
On Monday, 9 November 2015 20:31:52 UTC-5, MRAB wrote: > On 2015-11-10 01:12, Bernie Lazlo wrote: > > On Monday, 9 November 2015 19:30:23 UTC-5, MRAB wrote: > >> On 2015-11-09 23:52, Bernie Lazlo wrote: > >> > This should be a simple problem but I have wasted hours on it. Any help > >> > would be appreciated. [I have taken my code back to almost the very > >> > beginning.] > >> > > >> > The student scores need to be summed. > >> > > >> > import json > >> > import urllib > >> > url = "http://www.wickson.net/geography_assignment.json"; > >> > response = urllib.urlopen(url) > >> > data = json.loads(response.read()) > >> > lst1 = list(data.items()) > >> > print lst1 > >> > > >> Do it a step at a time. > >> > >> It's a list, so start with indexing. > > > > MRAB: > > > > I think of the file as two lists. The second list appears to be a list of > > tuples containing "names" and "scores". How would you index or extract > > those. > > > Right, so lst1[1] gets you closer to what you want. > > Further indexing will get you even closer. === lst2 = lst1[1] removes first line of instructions printing lst2[1:2] produces essentially the list of students and scores ?? ([{u'student ': u'Hannah', u'score': 77}, {u'student ': u'Emily', u'score': 57}, {u'student ': u'Olivia', u'score': 80}, {u'student ': u'Nora', u'score': 70}, -- https://mail.python.org/mailman/listinfo/python-list
Re: Extracting and summing student scores from a JSON file using Python 2.7.10
On Monday, 9 November 2015 18:53:06 UTC-5, Bernie Lazlo wrote: > This should be a simple problem but I have wasted hours on it. Any help would > be appreciated. [I have taken my code back to almost the very beginning.] > > The student scores need to be summed. > > import json > import urllib > url = "http://www.wickson.net/geography_assignment.json"; > response = urllib.urlopen(url) > data = json.loads(response.read()) > lst1 = list(data.items()) > print lst1 == Many thanks, MRAB! :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Extracting and summing student scores from a JSON file using Python 2.7.10
On Monday, 9 November 2015 22:54:05 UTC-5, wayne@gmail.com wrote: > On Monday, 9 November 2015 22:27:40 UTC-5, Denis McMahon wrote: > > On Mon, 09 Nov 2015 15:52:45 -0800, Bernie Lazlo wrote: > > > > > This should be a simple problem but I have wasted hours on it. Any help > > > would be appreciated. [I have taken my code back to almost the very > > > beginning.] > > > > > > The student scores need to be summed. > > > > > > import json import urllib url = > > > "http://www.wickson.net/geography_assignment.json"; > > > response = urllib.urlopen(url) > > > data = json.loads(response.read()) > > > lst1 = list(data.items()) > > > print lst1 > > > > I find that pprint.pprint is useful for looking at data structures. > > > > Having looked at the data, and then using appropriate substitutions for > > and in the following: > > > > sumscore = 0 > > students = 0 > > > > for dic in : > > sumscore = sumscore + dic[] > > students += 1 > > > > print 'Sum of', students, 'scores is', sumscore > > print 'Average of', students, 'scores is', sumscore / students > > > > It was trivial to generate: > > > > Sum of 50 scores is 3028 > > Average of 50 scores is 60 > > > > -- > > Denis McMahon > = Thanks for the reply, Denis. I hope this comes as easily to me some day. :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Extracting and summing student scores from a JSON file using Python 2.7.10
On Monday, 9 November 2015 18:53:06 UTC-5, Bernie Lazlo wrote: > This should be a simple problem but I have wasted hours on it. Any help would > be appreciated. [I have taken my code back to almost the very beginning.] > > The student scores need to be summed. > > import json > import urllib > url = "http://www.wickson.net/geography_assignment.json"; > response = urllib.urlopen(url) > data = json.loads(response.read()) > lst1 = list(data.items()) > print lst1 Pete, thanks for the input. Sometimes it just takes a suggestion to break past the "brick wall". -- https://mail.python.org/mailman/listinfo/python-list
Re: shelve.open call gives error
"Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > En Sat, 09 Feb 2008 03:35:14 -0200, waltbrad <[EMAIL PROTECTED]> > escribi?: > >> On Feb 8, 5:29 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: >>> En Fri, 08 Feb 2008 06:36:53 -0200, waltbrad <[EMAIL PROTECTED]> >>> escribió: >>> >>> > Working through the Mark Lutz book Programming Python 3rd Edition. >>> > A couple of modules in the "Preview" chapter give me errors. Both on a >>> > shelve.open call: >>> >>> shelve uses the anydbm module; anydbm tries to select the best database >>> module available, but apparently fails in your system. >> >> But as I gain experience I'd like to return to this issue and try to >> fix it. Can you give me advice on how to go about that? >> >> I'm working on a win98 system. I have python on a linux system, >> (Kubuntu) and winxp but it's more convenient right now for me to use >> the 98 laptop. > > I've tried the example on WinXP and it runs fine. Looks like the bsddb > package isn't working on Windows98; I don't know if that platform is still > supported or not. Try submitting a bug report http://bugs.python.org > > -- > Gabriel Genellina > Yeah, I think you're right. I also have no problems with it on XP. 5.1 is supposed to be the last version that get win9x support. Thanks. -- http://mail.python.org/mailman/listinfo/python-list