From catherine.devlin at gmail.com Mon Dec 3 02:01:46 2012 From: catherine.devlin at gmail.com (Catherine Devlin) Date: Sun, 2 Dec 2012 20:01:46 -0500 Subject: [CentralOH] Columbus Python Workshop, Jan 18-19 Message-ID: Hi, COhPy! Everything is clear for the Columbus Python Workshop in January! LeadingEdje has stepped up to feed our participants, and Pillar is providing the locale. https://tinyurl.com/cmh-py-workshop There's only one thing that we need from COhPy: publicity! Find our participants-to-be and bring them in! I'm way out here in Dayton and can't publicize in Columbus very well. For that, I really need you, and it's crucial to the workshop's success. I've attached a flyer for the workshop. Please print it and distribute it madly. - invite your friends, spouses, coworkers, neighbors, daughters, nieces, daughters' friends, daughters' nieces, daughters' nieces' friends, etc. - post the flyer in libraries! - and other places with bulletin boards (Panera, bookstores, college campuses...) - airdrop from 40,000 feet over downtown etc I would like to spread the word through various organizations - both geeky and not. For geeky organizations, these come to mind: TechLife Ohio techcolumbus.org GirlDevelopIt OSU Open Source Club ... but what else? But part of the point of the workshop is to go beyond geekdom's traditional reach, so if I could get suggestions and/or contact information for other groups, that would be awesome! Girl Scout troops come to mind, homeschooler organizations, YWCA... I'd really like more ideas here. In case you've forgotten all about the Workshop, here's your reminder: ------------------------------------------------------------------ The Columbus Python Workshop for women and their friends is a free hands-on introduction to computer programming that's fun, accessible, and practical even to those who've never programmed at all before. We empower women of all ages and backgrounds to learn programming in a beginner-friendly environment. Thanks to our sponsor, LeadingEdje, food will be provided for participants! The workshop will be held in Pillar's brand-new office in Columbus' Short North. The workshop is the latest in a series based on the famous Boston Python Workshop; they've already introduced hundreds of beginners to programming in Boston, Indianapolis, Portland, Chicago, and Kansas City. Now it's Ohio's turn, so spread the word! Get more details and sign up now: https://tinyurl.com/cmh-py-workshop Thanks to our sponsor, LeadingEdje: leadingedje.com and our venue provider, Pillar: pillartechnology.com and thanks to YOU, -- - Catherine http://catherinedevlin.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: flyer.pdf Type: application/pdf Size: 162714 bytes Desc: not available URL: From patrick.shuff at gmail.com Mon Dec 3 08:31:16 2012 From: patrick.shuff at gmail.com (Patrick Shuff) Date: Sun, 2 Dec 2012 23:31:16 -0800 Subject: [CentralOH] Pyp - Piping Python through Pipes Message-ID: Hey Pythonistas! A coworker mentioned this python project last week and I figured I would pass it along since there was recent discussion of piping on the mailing list. http://code.google.com/p/pyp/ Here is a quick description from the project: Pyp is a linux command line text manipulation tool similar to awk or sed, > but which uses standard python string and list methods as well as custom > functions evolved to generate fast results in an intense production > environment. Pyed Pyper was developed at Sony Pictures Imageworks to > facilitate the construction of complex image manipulation "one-liner" > commands during visual effects work on *Alice in Wonderland, Green > Lantern,* and the *The Amazing Spiderman.* And a quick example from the project: ls | pyp "p.replace('maybe','yes') | pp.sort() | pp[1:3] |p , p , > p.strip('abc') | whitespace | p[3], 'no' | p.upper() " Looks pretty interesting! I plan to put some time into it this week :) Enjoy! Patrick * * * * -------------- next part -------------- An HTML attachment was scrubbed... URL: From jep200404 at columbus.rr.com Tue Dec 4 23:58:16 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Tue, 4 Dec 2012 17:58:16 -0500 Subject: [CentralOH] Introspection of unique_together Message-ID: <20121204175816.1e78c93c.jep200404@columbus.rr.com> Using Django, how can a method find out the value of unique_together? From kurtis.mullins at gmail.com Wed Dec 5 00:55:26 2012 From: kurtis.mullins at gmail.com (Kurtis Mullins) Date: Tue, 4 Dec 2012 18:55:26 -0500 Subject: [CentralOH] Introspection of unique_together In-Reply-To: <20121204175816.1e78c93c.jep200404@columbus.rr.com> References: <20121204175816.1e78c93c.jep200404@columbus.rr.com> Message-ID: Possibly Model.meta.unique_together, but I haven't tried it. On Tue, Dec 4, 2012 at 5:58 PM, wrote: > Using Django, how can a method find out the value of unique_together? > > _______________________________________________ > CentralOH mailing list > CentralOH at python.org > http://mail.python.org/mailman/listinfo/centraloh > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jep200404 at columbus.rr.com Wed Dec 5 02:01:20 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Tue, 4 Dec 2012 20:01:20 -0500 Subject: [CentralOH] Introspection of unique_together In-Reply-To: References: <20121204175816.1e78c93c.jep200404@columbus.rr.com> Message-ID: <20121204200120.7e76bd14.jep200404@columbus.rr.com> On Tue, 4 Dec 2012 18:55:26 -0500, Kurtis Mullins wrote: > Possibly Model.meta.unique_together, but I haven't tried it. print repr(Model.meta.unique_together) NameError: global name 'Model' is not defined print repr(models.Model.meta.unique_together) AttributeError: type object 'Model' has no attribute 'meta' print repr(models.Model.Meta.unique_together) AttributeError: type object 'Model' has no attribute 'Meta' How would Model or models.Model know about any one particular class anyway? I had already tried cls.Meta.unique_together (in a class method) without success before my original post. Here's some more. print repr(cls.Meta.unique_together) AttributeError: type object 'Book' has no attribute 'Meta' print repr(cls.meta.unique_together) AttributeError: type object 'Book' has no attribute 'meta' print repr(cls.unique_together) AttributeError: type object 'Book' has no attribute 'unique_together' From kurtis.mullins at gmail.com Wed Dec 5 02:02:53 2012 From: kurtis.mullins at gmail.com (Kurtis Mullins) Date: Tue, 4 Dec 2012 20:02:53 -0500 Subject: [CentralOH] Introspection of unique_together In-Reply-To: <20121204200120.7e76bd14.jep200404@columbus.rr.com> References: <20121204175816.1e78c93c.jep200404@columbus.rr.com> <20121204200120.7e76bd14.jep200404@columbus.rr.com> Message-ID: My apologies, I meant Model as a placeholder for your Model class name; not the Model class itself. For example: Person.meta.unique_together On Dec 4, 2012 8:01 PM, wrote: > On Tue, 4 Dec 2012 18:55:26 -0500, Kurtis Mullins < > kurtis.mullins at gmail.com> wrote: > > > Possibly Model.meta.unique_together, but I haven't tried it. > > print repr(Model.meta.unique_together) > NameError: global name 'Model' is not defined > > print repr(models.Model.meta.unique_together) > AttributeError: type object 'Model' has no attribute 'meta' > > print repr(models.Model.Meta.unique_together) > AttributeError: type object 'Model' has no attribute 'Meta' > > How would Model or models.Model know about any one particular > class anyway? I had already tried cls.Meta.unique_together > (in a class method) without success before my original post. > Here's some more. > > print repr(cls.Meta.unique_together) > AttributeError: type object 'Book' has no attribute 'Meta' > > print repr(cls.meta.unique_together) > AttributeError: type object 'Book' has no attribute 'meta' > > print repr(cls.unique_together) > AttributeError: type object 'Book' has no attribute 'unique_together' > > _______________________________________________ > CentralOH mailing list > CentralOH at python.org > http://mail.python.org/mailman/listinfo/centraloh > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jep200404 at columbus.rr.com Wed Dec 5 02:07:51 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Tue, 4 Dec 2012 20:07:51 -0500 Subject: [CentralOH] Introspection of unique_together In-Reply-To: <20121204200120.7e76bd14.jep200404@columbus.rr.com> References: <20121204175816.1e78c93c.jep200404@columbus.rr.com> <20121204200120.7e76bd14.jep200404@columbus.rr.com> Message-ID: <20121204200751.4b5ec8c7.jep200404@columbus.rr.com> On Tue, 4 Dec 2012 20:01:20 -0500, jep200404 at columbus.rr.com wrote: > I had already tried cls.Meta.unique_together > (in a class method) without success before my original post. That was close. cls._meta.unique_together works. From jep200404 at columbus.rr.com Wed Dec 5 02:49:34 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Tue, 4 Dec 2012 20:49:34 -0500 Subject: [CentralOH] Programming Conventions Message-ID: <20121204204934.0cbd81a9.jep200404@columbus.rr.com> Brandon mentioned variable naming last night. Dan Saks[1] gave a presentation at Columbus Code Camp[2] about developing coding conventions for things such as but not limited to names and whitespace, based on empirical readability studies, not on opinions. It was a good presentation. Unfortunately, I don't see anything on this web site about that topic. [1] http://dansaks.com/ [2] http://columbuscodecamp.com/ From joskra42.list at gmail.com Wed Dec 5 04:08:24 2012 From: joskra42.list at gmail.com (Joshua Kramer) Date: Tue, 4 Dec 2012 22:08:24 -0500 Subject: [CentralOH] How do .eggs work in search paths? Message-ID: Hello, I have a basic question. I am trying to put Mezzanine under Apache using wsgi. In order to not clutter up the system site-packages directory, I have a directory like so: /srv/website/www/site-packages Under this site-packages are a number of packages provided by Mezzanine: bleach-1.1.5-py2.6.egg filebrowser_safe-0.2.12-py2.6.egg Mezzanine-1.2.4-py2.6.egg pytz-2012h-py2.6.egg Django-1.4.2-py2.6.egg grappelli_safe-0.2.10-py2.6.egg Pillow-1.7.8-py2.6-linux-x86_64.egg setuptools-0.6c11-py2.6.egg easy-install.pth html5lib-0.95-py2.6.egg pip-1.1-py2.6.egg setuptools.pth The eggs are really directories themselves, and under each .egg lies the module. So for example, under Mezzanine-1.2.4-py2.6.egg, there's a 'mezzanine' directory. In the wsgi launcher, I have a call like this: sys.path.append('/srv/website/www/site-packages') This appears to not be working, as Apache can't find any of the modules under that site-packages directory that are within the eggs. Is there a cleaner or more elegant way to get to where I want to be? Thanks! -JK -------------- next part -------------- An HTML attachment was scrubbed... URL: From nludban at columbus.rr.com Wed Dec 5 13:10:15 2012 From: nludban at columbus.rr.com (Neil Ludban) Date: Wed, 5 Dec 2012 07:10:15 -0500 Subject: [CentralOH] How do .eggs work in search paths? In-Reply-To: References: Message-ID: <20121205071015.91718e1b.nludban@columbus.rr.com> See if this works: import site site.addsitedir('/srv/website/www/site-packages') http://docs.python.org/2/library/site.html On Tue, 4 Dec 2012 22:08:24 -0500 Joshua Kramer wrote: > Hello, > > I have a basic question. I am trying to put Mezzanine under Apache using > wsgi. In order to not clutter up the system site-packages directory, I > have a directory like so: > > /srv/website/www/site-packages > > Under this site-packages are a number of packages provided by Mezzanine: > > bleach-1.1.5-py2.6.egg > filebrowser_safe-0.2.12-py2.6.egg > Mezzanine-1.2.4-py2.6.egg > pytz-2012h-py2.6.egg > Django-1.4.2-py2.6.egg > grappelli_safe-0.2.10-py2.6.egg > Pillow-1.7.8-py2.6-linux-x86_64.egg > setuptools-0.6c11-py2.6.egg > easy-install.pth > html5lib-0.95-py2.6.egg > pip-1.1-py2.6.egg > setuptools.pth > > The eggs are really directories themselves, and under each .egg lies the > module. So for example, under Mezzanine-1.2.4-py2.6.egg, there's a > 'mezzanine' directory. > > In the wsgi launcher, I have a call like this: > > sys.path.append('/srv/website/www/site-packages') > > This appears to not be working, as Apache can't find any of the modules > under that site-packages directory that are within the eggs. > > Is there a cleaner or more elegant way to get to where I want to be? > > Thanks! > -JK From kurtis.mullins at gmail.com Wed Dec 5 13:38:48 2012 From: kurtis.mullins at gmail.com (Kurtis Mullins) Date: Wed, 5 Dec 2012 07:38:48 -0500 Subject: [CentralOH] How do .eggs work in search paths? In-Reply-To: <20121205071015.91718e1b.nludban@columbus.rr.com> References: <20121205071015.91718e1b.nludban@columbus.rr.com> Message-ID: I don't use Apache; I use nginx, but I wonder if it's possible to simply use a Virtual Environment with Apache? That's what I do with uWSGI. It makes it a lot easier because you can simply create a requirements.txt file and easily re-deploy or upgrade your environment as needed. On Wed, Dec 5, 2012 at 7:10 AM, Neil Ludban wrote: > > See if this works: > > import site > site.addsitedir('/srv/website/www/site-packages') > > > http://docs.python.org/2/library/site.html > > > On Tue, 4 Dec 2012 22:08:24 -0500 > Joshua Kramer wrote: > > Hello, > > > > I have a basic question. I am trying to put Mezzanine under Apache using > > wsgi. In order to not clutter up the system site-packages directory, I > > have a directory like so: > > > > /srv/website/www/site-packages > > > > Under this site-packages are a number of packages provided by Mezzanine: > > > > bleach-1.1.5-py2.6.egg > > filebrowser_safe-0.2.12-py2.6.egg > > Mezzanine-1.2.4-py2.6.egg > > pytz-2012h-py2.6.egg > > Django-1.4.2-py2.6.egg > > grappelli_safe-0.2.10-py2.6.egg > > Pillow-1.7.8-py2.6-linux-x86_64.egg > > setuptools-0.6c11-py2.6.egg > > easy-install.pth > > html5lib-0.95-py2.6.egg > > pip-1.1-py2.6.egg > > setuptools.pth > > > > The eggs are really directories themselves, and under each .egg lies the > > module. So for example, under Mezzanine-1.2.4-py2.6.egg, there's a > > 'mezzanine' directory. > > > > In the wsgi launcher, I have a call like this: > > > > sys.path.append('/srv/website/www/site-packages') > > > > This appears to not be working, as Apache can't find any of the modules > > under that site-packages directory that are within the eggs. > > > > Is there a cleaner or more elegant way to get to where I want to be? > > > > Thanks! > > -JK > _______________________________________________ > CentralOH mailing list > CentralOH at python.org > http://mail.python.org/mailman/listinfo/centraloh > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wam at cisco.com Wed Dec 5 18:39:03 2012 From: wam at cisco.com (William McVey) Date: Wed, 05 Dec 2012 12:39:03 -0500 Subject: [CentralOH] How do .eggs work in search paths? In-Reply-To: References: Message-ID: <1354729143.6700.24.camel@zelbinion.cisco.com> On Tue, 2012-12-04 at 22:08 -0500, Joshua Kramer wrote: > The eggs are really directories themselves, and under each .egg lies > the module. So for example, under Mezzanine-1.2.4-py2.6.egg, there's > a 'mezzanine' directory. > > In the wsgi launcher, I have a call like this: > sys.path.append('/srv/website/www/site-packages') Eggs, both the zipped up versions as well as unzipped directory versions are containers for packages, more akin to directories than python packages or modules. Setuptools adds .egg files/directories to your sys.path directly (typically using something like an easy-install.pth file). > > This appears to not be working, as Apache can't find any of the > modules under that site-packages directory that are within the eggs. Right, because your sys.path doesn't list a directory full of packages, it lists a directory full of eggs (e.g. directories). > > Is there a cleaner or more elegant way to get to where I want to be? Yup. You *really* want to be using virtualenv. Apache's mod_wsgi supports virtualenvs (http://code.google.com/p/modwsgi/wiki/VirtualEnvironments). Lacking that, you might want to try using .pth files in your system path to augment your search list (see the docs for 'site' module for details on .pth files) to include the paths to your .eggs. Still, the virtualenv is a much cleaner (and altogether more supportable) solution. -- William From jep200404 at columbus.rr.com Fri Dec 7 01:43:23 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Thu, 6 Dec 2012 19:43:23 -0500 Subject: [CentralOH] Counting References in Django Message-ID: <20121206194323.5f56fafb.jep200404@columbus.rr.com> In Django, what's a better way of counting references to a record of an arbitrary model than my following working code? c = 0 for r in record._meta.get_all_related_objects(): c += r.model.objects.filter( **{r.field.name + '__exact': record.id}).count() I can rearrange that into a single statement as follows, but it's harder to read, so that's not better. c = sum(map( (lambda r: r.model.objects.filter( **{r.field.name + '__exact': record.id}).count()), record._meta.get_all_related_objects())) From jep200404 at columbus.rr.com Fri Dec 7 02:12:10 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Thu, 6 Dec 2012 20:12:10 -0500 Subject: [CentralOH] Counting References in Django: Ditch the .id In-Reply-To: <20121206194323.5f56fafb.jep200404@columbus.rr.com> References: <20121206194323.5f56fafb.jep200404@columbus.rr.com> Message-ID: <20121206201210.181150a8.jep200404@columbus.rr.com> On Thu, 6 Dec 2012 19:43:23 -0500, jep200404 at columbus.rr.com wrote: > In Django, what's a better way of counting references to a record > of an arbitrary model than my following working code? > > c = 0 > for r in record._meta.get_all_related_objects(): > c += r.model.objects.filter( > **{r.field.name + '__exact': record.id}).count() Ditch the .id: c = 0 for r in record._meta.get_all_related_objects(): c += r.model.objects.filter( **{r.field.name + '__exact': record}).count() From jep200404 at columbus.rr.com Sat Dec 8 07:26:17 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Sat, 8 Dec 2012 01:26:17 -0500 Subject: [CentralOH] Reinventing the Wheel: Counting References in Django In-Reply-To: <20121206194323.5f56fafb.jep200404@columbus.rr.com> References: <20121206194323.5f56fafb.jep200404@columbus.rr.com> Message-ID: <20121208012617.3aacb8ba.jep200404@columbus.rr.com> What are the standard ways of counting references in Django? I get the feeling that I'm reinventing the wheel below; that there's likely some standard way of counting references. On Thu, 6 Dec 2012 19:43:23 -0500, jep200404 at columbus.rr.com wrote: > In Django, what's a better way of counting references to a record > of an arbitrary model than my following working code? > > c = 0 > for r in record._meta.get_all_related_objects(): > c += r.model.objects.filter( > **{r.field.name + '__exact': record.id}).count() That pattern and variations of it appeared several times, so I put the code in functions below. The code above would be replaced with just c = count_references(previous). def count_particular_references(record_or_model, query_keyword_suffix, target): c = 0 for f in record_or_model._meta.get_all_related_objects(): c += f.model.objects.filter( **{f.field.name + query_keyword_suffix: target}).count() return c def count_references(record): return count_particular_references(record, '__exact', record) Imagine one has a File model with information about an actual file. Also, one has Book, Magazine, and Flyer models, each of which have a ForeignKey pointing to a record in the File model. For a given row of File, named file_info, one can find out how many other records in the various tables refer to file_info, with the following: count_references(file_info) If one wants to know how many records refer to File records that have a particular sha1sum value, one could do: count_particular_references(File, '__sha1sum__exact', sha1sum) From jep200404 at columbus.rr.com Sat Dec 8 15:49:26 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Sat, 8 Dec 2012 09:49:26 -0500 Subject: [CentralOH] Reinventing the Wheel: Counting References in Django In-Reply-To: <20121208012617.3aacb8ba.jep200404@columbus.rr.com> References: <20121206194323.5f56fafb.jep200404@columbus.rr.com> <20121208012617.3aacb8ba.jep200404@columbus.rr.com> Message-ID: <20121208094926.2fec04b5.jep200404@columbus.rr.com> On Sat, 8 Dec 2012 01:26:17 -0500, jep200404 at columbus.rr.com wrote: > The code above would be > replaced with just c = count_references(previous). s/previous/record/ From jep200404 at columbus.rr.com Tue Dec 11 21:08:08 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Tue, 11 Dec 2012 15:08:08 -0500 Subject: [CentralOH] Guido Moves From Google to Dropbox Message-ID: <20121211150808.71c9653b.jep200404@columbus.rr.com> Guido has left Google and is going to Dropbox. https://tech.dropbox.com/2012/12/welcome-guido/ From eric at intellovations.com Tue Dec 11 21:43:36 2012 From: eric at intellovations.com (Eric Floehr) Date: Tue, 11 Dec 2012 15:43:36 -0500 Subject: [CentralOH] Django/Postgresql unique_together with None/NULL Values In-Reply-To: <20121120102641.68ac200a.jep200404@columbus.rr.com> References: <20121120102641.68ac200a.jep200404@columbus.rr.com> Message-ID: Jim, On Tue, Nov 20, 2012 at 10:26 AM, wrote: > What's a cleaner way of accomplishing the unique_together stuff, > hopefully without the (redundant) previous_id_num attribute? > > I'm using Postgresql with Django. Postgresql's implementation of > unique constraints when a value is NULL (None in Python) does not > work as desired. I figured out an ugly workaround. Hopefully, > you know a better way. > Null in a database sense means "missing data", and the SQL standard specifically states that two NULL values are not equal. That's why the unique-together stuff doesn't work as expected. The thought being that since the data point is "unknown" you can't ascribe equality to any two unknown data points. For more than you ever wanted to know about NULL, there is a fairly comprehensive Wikipedia article [1]. Now you might say, when I do group by or ordering in SQL, the NULL's are grouped and ordered together. How can that be in NULL != NULL? The SQL standard gets around that by defining group by and order by as putting "not distinct" items together. And SQL defines "any two values that are equal to one another, or any two Nulls" as being "not distinct". So while NULL does not equal NULL, they are indistinct from one another as far as SQL goes. An (ugly) thought is to not use None/NULL values for previous to > indicate end of linked list. > Instead one would set up a sentinel dummy Book record for the end of > a linked list. That would make unique_constraint happy, but push > ugliness into the code that groks the linked list. > Not ugly at all, that is a standard way to do that in a relational database. Another common way is to use an order or positional field as part of the unique-together rather than the index field. It would start at 1 (or 0) for the first item in the list, and increment as you add items. That works best if you are just adding to the end of the list, not reordering, and it involves adding another field to the table and is another field to maintain. [1] http://en.wikipedia.org/wiki/Null_(SQL) -Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From eric at intellovations.com Tue Dec 11 22:18:35 2012 From: eric at intellovations.com (Eric Floehr) Date: Tue, 11 Dec 2012 16:18:35 -0500 Subject: [CentralOH] Counting References in Django In-Reply-To: <20121206194323.5f56fafb.jep200404@columbus.rr.com> References: <20121206194323.5f56fafb.jep200404@columbus.rr.com> Message-ID: Jim, On Thu, Dec 6, 2012 at 7:43 PM, wrote: > In Django, what's a better way of counting references to a record > of an arbitrary model than my following working code? > > c = 0 > for r in record._meta.get_all_related_objects(): > c += r.model.objects.filter( > **{r.field.name + '__exact': record.id}).count() > > I can rearrange that into a single statement as follows, > but it's harder to read, so that's not better. > > c = sum(map( > (lambda r: r.model.objects.filter( > **{r.field.name + '__exact': record.id}).count()), > record._meta.get_all_related_objects())) > The code as it is, is good. It's a clever way to use introspection in Django to count references to a model instance. _meta is a nice interface (even if it is internal) to easily find out about the model and do what you did in a general way that is applicable to any model. You don't have to write specific code for each model, and worry about having to change it every time you add a new model that points to the model you'd like to keep track of how many are referencing it. But you asked about a better way, and here are my thoughts on that. In programming, reference counting is very important. One huge use case is in garbage collected languages like Java, Python, or Go. The runtime needs an easy, efficient way to know when an object is no longer being referred to and thus it's memory can be deallocated. We use counts because that is the best way to accomplish that. Rarely do we care *what* is pointing to us, just that there is something. However, a relational database gives us all kinds of facilities for knowing what is pointing to a row (i.e. an object), it's what it's about... they don't call them *relational* for nothing :-). So when you are pondering counting references in a database context, it's always a good idea to step back and ask yourself, "What does a count give me that the existing facilities in my relational database don't already". A count makes sense in answering questions like "How many books point to this author?", or "How many observations point to this location?". But your code is very general and cannot answer specific questions like that. Your code could answer the question "Is there anything pointing to this record?", but that's about it. So it might be good to take a step back and ask why you are asking that question, and if there are better ways of asking. You might be asking that question to see if it was ok to delete a record. If you are asking if anything is pointing to it, you must only want to delete a record if nothing is pointing at it. Django's Model.delete() method will not only delete the record the instance of the Model is representing, but it will also look at all the foreign key relationships and delete any records that are currently pointing to that record. By default, Django does a "cascaded delete". So if you delete an author, any books that point to that author will also be deleted. So you use your code above to check to see if anything is pointing to that author, and if the answer is 0, go ahead and delete. But you don't have to do that. There is a parameter to ForeignKey that allows you to specify how you want to handle deletes. The default is CASCADE (and it's cascade even though it will construct a PostgreSQL database without cascade, so if you do the same delete in raw SQL, it'll not work). But you can choose PROTECT, which will then do the same thing PostgreSQL will do by default, which is not allow you to delete an object that is still being referenced. You can also choose SET_NULL or SET_DEFAULT, which on a delete will set the referencing object's reference to either NULL or the default value specified. You can also pass in a call-back to call custom code to do whatever. Because a relational database handles the relations for us by default, we rarely have to use reference counting in a general sense, however, because Django's ORM supports a number of different database platforms, with varying degrees of referential integrity, it is handing in software what PostgreSQL handles by default. And by default, what Django creates in the PostgreSQL database it makes doesn't match what it does in code. I.e. "DELETE FROM authors WHERE id=1;" will not give the same results as "Authors.objects.get(pk=1).delete()" if that author is being pointed to by one or more books. And that's really confusing. -Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From eric at intellovations.com Wed Dec 12 16:46:03 2012 From: eric at intellovations.com (Eric Floehr) Date: Wed, 12 Dec 2012 10:46:03 -0500 Subject: [CentralOH] IPython Installation In-Reply-To: <20121115195312.1727c355.jep200404@columbus.rr.com> References: <20121115195312.1727c355.jep200404@columbus.rr.com> Message-ID: IPython just received a $1.15 million dollar grant from the Alfred P. Sloan Foundation [1], so if you think it does amazing things today (it does), the future looks very bright indeed! Anyone who saw Brandon's talk at the last COhPy meeting can attest to the power of the IPython console and IPython Notebook. [1] http://mail.scipy.org/pipermail/ipython-dev/2012-December/010799.html -Eric On Thu, Nov 15, 2012 at 7:53 PM, wrote: > Several local folks have already been using IPython, or have > started using IPython and pandas recently because of the newly > published "Python for Data Analysis"[1]. At least one person > had difficulty installing pandas and/or ipython with pip > in a virtualenv under Centos 6.3, so here's a recipe for such. > > Some of the following system packages might not be necessary. > Also, more system packages might be required that are not listed below. > Please post corrections. > sudo yum install python-devel > sudo yum install gcc-c++ > sudo yum install freetype > sudo yum install freetype-devel > sudo yum install libpng-devel > > virtualenv env > source env/bin/activate > pip install numpy > pip install matplotlib > pip install pandas > pip install ipython > > [1] Python for Data Analysis > Wes McKinney > O'Reilly > 978-1-449-31979-3 > Jason Conaway might be particularly interested in Chapter 11. > > _______________________________________________ > CentralOH mailing list > CentralOH at python.org > http://mail.python.org/mailman/listinfo/centraloh > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jep200404 at columbus.rr.com Thu Dec 20 20:24:16 2012 From: jep200404 at columbus.rr.com (jep200404 at columbus.rr.com) Date: Thu, 20 Dec 2012 14:24:16 -0500 Subject: [CentralOH] Counting References in Django In-Reply-To: References: <20121206194323.5f56fafb.jep200404@columbus.rr.com> Message-ID: <20121220142416.2e4c198b.jep200404@columbus.rr.com> On Tue, 11 Dec 2012 16:18:35 -0500, Eric Floehr wrote: > You might be asking that question [how many records refer to > some arbitrary record] to see if it was ok to delete a record. Often I am. > There is a parameter to ForeignKey that > allows you to specify how you want to handle deletes. The default is > CASCADE (and it's cascade even though it will construct a PostgreSQL > database without cascade, so if you do the same delete in raw SQL, it'll > not work). But you can choose PROTECT, which will then do the same thing > PostgreSQL will do by default, which is not allow you to delete an object > that is still being referenced. You can also choose SET_NULL or > SET_DEFAULT, which on a delete will set the referencing object's reference > to either NULL or the default value specified. You can also pass in a > call-back to call custom code to do whatever. Thanks! on_delete=PROTECT[1] avoids the need for counting references before deleting some possibly referred to record, and allows me to clean up the code with a Pythonic try/except. > You might be asking that question [how many records refer to > some arbitrary record] to see if it was ok to delete a record. Sometimes I am not. Sometimes I need to delete or move a regular file, not a record, if there are no references to the record that has information (like the path) about the file. If I was deleting the record also, I could wrangle the file in the else: clause of a try/except, but I retain the record about the file, even when there are no references to that record, so for that situation, I still need to count references. [1] https://docs.djangoproject.com/en/1.4/ref/models/fields/#django.db.models.ForeignKey.on_delete From mark at microenh.com Thu Dec 27 17:07:38 2012 From: mark at microenh.com (Mark Erbaugh) Date: Thu, 27 Dec 2012 11:07:38 -0500 Subject: [CentralOH] DoJo West Needs a new home or new night Message-ID: Western Pythonistas, I just contacted the Westland Library to reserve our meeting room into 2013 and discovered that the room will not be available on the 1st and 3rd Mondays. Since we don't meet on the last Monday (COhPy meeting), that doesn't leave us too many evenings. Mark