Re: Using Dictionary

2015-04-14 Thread Pippo
On Tuesday, 14 April 2015 09:54:46 UTC-4, Michiel Overtoom wrote: > On Apr 14, 2015, at 15:34, Pippo wrote: > > > How can I use dictionary to save the following information? > > What a curious question. The purpose of a dictionary is not to save > information, but to store data as a key -> valu

Re: Using Dictionary

2015-04-14 Thread Steven D'Aprano
On Tue, 14 Apr 2015 11:34 pm, Pippo wrote: > How can I use dictionary to save the following information? > > #C[Health] > #P[Information] > #ST[genetic information] > #C[oral | (recorded in (any form | medium))] > #C[Is created or received by] > #A[health care provider | health plan | public heal

Re: Using Dictionary

2015-04-14 Thread Michiel Overtoom
On Apr 14, 2015, at 15:34, Pippo wrote: > How can I use dictionary to save the following information? What a curious question. The purpose of a dictionary is not to save information, but to store data as a key -> value mapping: telbook = {} telbook["jan"] = "0627832873" telbook["ma

Using Dictionary

2015-04-14 Thread Pippo
How can I use dictionary to save the following information? #C[Health] #P[Information] #ST[genetic information] #C[oral | (recorded in (any form | medium))] #C[Is created or received by] #A[health care provider | health plan | public health authority | employer | life insurer | school | universit

Re: Using dictionary key as a regular expression class

2010-01-23 Thread Steve Holden
Chris Jones wrote: > On Fri, Jan 22, 2010 at 05:07:13PM EST, Arnaud Delobelle wrote: > > [..] > >> import codecs >> from collections import defaultdict >> >> tcounters = defaultdict(int) >> f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8") >> >> for c in f.read(): >> tcoun

Re: Using dictionary key as a regular expression class

2010-01-23 Thread Chris Jones
On Sat, Jan 23, 2010 at 02:45:41AM EST, Terry Reedy wrote: > On 1/22/2010 9:58 PM, Chris Jones wrote: >> Do you mean I should just read the file one character at a time? > > Whoops, my misdirection (you can .read(1), but this is s l o w. > I meant to suggest processing it a char at a time. R

Re: Using dictionary key as a regular expression class

2010-01-22 Thread Terry Reedy
On 1/22/2010 9:58 PM, Chris Jones wrote: On Fri, Jan 22, 2010 at 08:46:35PM EST, Terry Reedy wrote: Do you mean I should just read the file one character at a time? Whoops, my misdirection (you can .read(1), but this is s l o w. I meant to suggest processing it a char at a time. 1. If

Re: Using dictionary key as a regular expression class

2010-01-22 Thread Chris Jones
On Fri, Jan 22, 2010 at 08:46:35PM EST, Terry Reedy wrote: > On 1/22/2010 4:47 PM, Chris Jones wrote: >> I was writing a script that counts occurrences of characters in source code >> files: >> >> #!/usr/bin/python >> import codecs >> tcounters = {} >> f = codecs.open('/home/gavron/git/screen/src/

Re: Using dictionary key as a regular expression class

2010-01-22 Thread Terry Reedy
On 1/22/2010 4:47 PM, Chris Jones wrote: I was writing a script that counts occurrences of characters in source code files: #!/usr/bin/python import codecs tcounters = {} f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8") for uline in f: lline = [] for char in uline[:-1

Re: Using dictionary key as a regular expression class

2010-01-22 Thread Chris Jones
On Fri, Jan 22, 2010 at 05:07:13PM EST, Arnaud Delobelle wrote: [..] > import codecs > from collections import defaultdict > > tcounters = defaultdict(int) > f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8") > > for c in f.read(): > tcounters[c] += 1 > > for c, n in tco

Re: Using dictionary key as a regular expression class

2010-01-22 Thread Arnaud Delobelle
Chris Jones writes: > I was writing a script that counts occurrences of characters in source > code files: > > #!/usr/bin/python > import codecs > tcounters = {} > f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8") > for uline in f: > lline = [] > for char in uline[:-1]: >

Using dictionary key as a regular expression class

2010-01-22 Thread Chris Jones
I was writing a script that counts occurrences of characters in source code files: #!/usr/bin/python import codecs tcounters = {} f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8") for uline in f: lline = [] for char in uline[:-1]: lline += [char] counters = {} for

Re: Using dictionary to hold regex patterns?

2008-11-26 Thread Bruno Desthuilliers
André a écrit : (snip) you don't need to use pattern.items()... Here is something I use (straight cut-and-paste): def parse_single_line(self, line): '''Parses a given line to see if it match a known pattern''' for name in self.patterns: result = self.patterns[nam

Re: Using dictionary to hold regex patterns?

2008-11-25 Thread Thomas Mlynarczyk
John Machin schrieb: No, "complicated" is more related to unused features. In the case of using an aeroplane to transport 3 passengers 10 km along the autobahn, you aren't using the radar, wheel-retractability, wings, pressurised cabin, etc. In your original notion of using a dict in your lexer,

Re: Using dictionary to hold regex patterns?

2008-11-24 Thread Steve Holden
John Machin wrote: > On Nov 25, 4:38 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> [...] >>> Judging which of two structures is "simpler" should not be independent >>> of those requirements. I don't see a role for intuition in this >>> process. >> Maybe I should have said "upon first sight" / "judging

Re: Using dictionary to hold regex patterns?

2008-11-24 Thread John Machin
On Nov 25, 4:38 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > John Machin schrieb: > > > Rephrasing for clarity: Don't use a data structure that is more > > complicated than that indicated by your requirements. > > Could you please define "complicated" in this context? In terms of > characters

Re: Using dictionary to hold regex patterns?

2008-11-24 Thread Thomas Mlynarczyk
John Machin schrieb: Rephrasing for clarity: Don't use a data structure that is more complicated than that indicated by your requirements. Could you please define "complicated" in this context? In terms of characters to type and reading, the dict is surely simpler. But I suppose that under t

Re: Using dictionary to hold regex patterns?

2008-11-24 Thread Thomas Mlynarczyk
Dennis Lee Bieber schrieb: Is "[ ( name, regex ), ... ]" really "simpler" than "{ name: regex, ... }"? Intuitively, I would consider the dictionary to be the simpler structure. Why, when you aren't /using/ the name to retrieve the expression... So as soon as I start retrieving a re

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Marc 'BlackJack' Rintsch
On Mon, 24 Nov 2008 00:46:42 +0100, Gilles Ganault wrote: > On Sun, 23 Nov 2008 23:18:06 +, MRAB <[EMAIL PROTECTED]> > wrote: >>A list is an ordered collection of items. Each item can be anything: a >>string, an integer, a dictionary, a tuple, a list... > > Yup, learned something new today. N

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Gilles Ganault
On Sun, 23 Nov 2008 23:18:06 +, MRAB <[EMAIL PROTECTED]> wrote: >A list is an ordered collection of items. Each item can be anything: a >string, an integer, a dictionary, a tuple, a list... Yup, learned something new today. Naively, I though a list was index=value, where value=a single piece

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread MRAB
Gilles Ganault wrote: On Sun, 23 Nov 2008 17:55:48 +, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: But there is no reason why you should use a dictionary; just use a list of key-value pairs: Thanks for the tip. I didn't know it was possible to use arrays to hold more than one value. Actuall

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Gilles Ganault
On Sun, 23 Nov 2008 17:55:48 +, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >But there is no reason why you should use a dictionary; just use a list >of key-value pairs: Thanks for the tip. I didn't know it was possible to use arrays to hold more than one value. Actually, it's a better solutio

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread John Machin
On Nov 24, 7:49 am, Thomas Mlynarczyk <[EMAIL PROTECTED]> wrote: > John Machin schrieb: > > > General tip: Don't us a data structure that is more complicated than > > what you need. > > Is "[ ( name, regex ), ... ]" really "simpler" than "{ name: regex, ...}"? > Intuitively, I would consider the d

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread John Machin
On Nov 24, 7:48 am, John Machin <[EMAIL PROTECTED]> wrote: > On Nov 24, 5:36 am, Terry Reedy <[EMAIL PROTECTED]> wrote: > > > print name + '#' + regex > > Perhaps you meant: >    print key + "#" + regex.pattern I definitely meant: print name + '#' + regex.pattern -- http://mail.python.org/mailm

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread André
On Nov 23, 1:40 pm, Gilles Ganault <[EMAIL PROTECTED]> wrote: > Hello > > After downloading a web page, I need to search for several patterns, > and if found, extract information and put them into a database. > > To avoid a bunch of "if m", I figured maybe I could use a dictionary > to hold the pat

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Thomas Mlynarczyk
John Machin schrieb: General tip: Don't us a data structure that is more complicated than what you need. Is "[ ( name, regex ), ... ]" really "simpler" than "{ name: regex, ... }"? Intuitively, I would consider the dictionary to be the simpler structure. Greetings, Thomas -- Ce n'est pas

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread John Machin
On Nov 24, 5:36 am, Terry Reedy <[EMAIL PROTECTED]> wrote: > Gilles Ganault wrote: > > Hello > > > After downloading a web page, I need to search for several patterns, > > and if found, extract information and put them into a database. > > > To avoid a bunch of "if m", I figured maybe I could use a

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread John Machin
On Nov 24, 6:55 am, Gilles Ganault <[EMAIL PROTECTED]> wrote: > On Sun, 23 Nov 2008 17:55:48 +, Arnaud Delobelle > > <[EMAIL PROTECTED]> wrote: > >But there is no reason why you should use a dictionary; just use a list > >of key-value pairs: > > >patterns = [ > >    ("pattern1", re.compile(">.+

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Benjamin Kaplan
On Sun, Nov 23, 2008 at 2:55 PM, Gilles Ganault <[EMAIL PROTECTED]> wrote: > On Sun, 23 Nov 2008 17:55:48 +, Arnaud Delobelle > <[EMAIL PROTECTED]> wrote: > >But there is no reason why you should use a dictionary; just use a list > >of key-value pairs: > > > >patterns = [ > >("pattern1", r

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Vlastimil Brom
2008/11/23 Gilles Ganault <[EMAIL PROTECTED]> > Hello > > After downloading a web page, I need to search for several patterns, > and if found, extract information and put them into a database. > > To avoid a bunch of "if m", I figured maybe I could use a dictionary > to hold the patterns, and loop

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Gilles Ganault
On Sun, 23 Nov 2008 17:55:48 +, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: >But there is no reason why you should use a dictionary; just use a list >of key-value pairs: > >patterns = [ >("pattern1", re.compile(">.+?.+?>(.+?)"), Thanks for the tip, but... I thought that lists could only us

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Terry Reedy
Gilles Ganault wrote: Hello After downloading a web page, I need to search for several patterns, and if found, extract information and put them into a database. To avoid a bunch of "if m", I figured maybe I could use a dictionary to hold the patterns, and loop through it: Good idea. import r

Re: Using dictionary to hold regex patterns?

2008-11-23 Thread Arnaud Delobelle
Gilles Ganault <[EMAIL PROTECTED]> writes: > Hello > > After downloading a web page, I need to search for several patterns, > and if found, extract information and put them into a database. > > To avoid a bunch of "if m", I figured maybe I could use a dictionary > to hold the patterns, and loop th

Using dictionary to hold regex patterns?

2008-11-23 Thread Gilles Ganault
Hello After downloading a web page, I need to search for several patterns, and if found, extract information and put them into a database. To avoid a bunch of "if m", I figured maybe I could use a dictionary to hold the patterns, and loop through it: == pattern = {} pattern["pattern1"] = ">.