[Tutor] Removing values from a dictionary if they are present in a list
I have the following information A={'g2': [4,5,3], 'g1': [1, 3]} B=[2,3,5] Now I want to remeove the elements in B if they are present (as values) in dictionary A. My expected solution is A= {'g2': [4], 'g1': [1] } I wrote the following piece of code which gives me thhe right code, but I am sure there must be a much shorter and more elegant way of doing it. Please suggest reject_list=[] for element in B: for key in A.keys(): for i in range(len(A[key])): if element==A[key][i]: reject_list.append((key,A[key][i])) print reject_list for i in range(len(reject_list)): print (reject_list[i][0],reject_list[i][1]) Index=A[reject_list[i][0]].index(reject_list[i][1]) print (reject_list[i][0],reject_list[i][1],Index) del A[reject_list[i][0]][Index] print A result obtained: {'g2': [4], 'g1': [1]} ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing values from a dictionary if they are present in alist
"ranjan das" wrote A={'g2': [4,5,3], 'g1': [1, 3]} B=[2,3,5] Now I want to remeove the elements in B if they are present (as values) in dictionary A. My first thought was to start with the dictionary, something like for key in A: A[key] = [val for val in A[key] if val not in B] Dunno what the efficiency will be like because there are a lot of hidden loops going on - but at least they are in C not Python. But I don't think it would be much worse than your option... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing values from a dictionary if they are present in a list
ranjan das wrote: > I have the following information > > A={'g2': [4,5,3], 'g1': [1, 3]} > > B=[2,3,5] > > Now I want to remeove the elements in B if they are present (as values) in > dictionary A. > > My expected solution is > > A= {'g2': [4], 'g1': [1] } > > I wrote the following piece of code which gives me thhe right code, but I > am sure there must be a much shorter and more elegant way of doing it. > Please suggest The following is a bit shorter, but not really elegant: >>> a = {'g2': [4, 5, 3], 'g1': [1, 3]} >>> b = [2, 3, 5] >>> for v in a.itervalues(): ... v[:] = [item for item in v if item not in b] ... >>> a {'g2': [4], 'g1': [1]} If you are free to choose your data types use sets instead of lists: >>> a = {'g2': set([4, 5, 3]), 'g1': set([1, 3])} >>> b = set([2, 3, 5]) >>> for v in a.itervalues(): ... v -= b ... >>> a {'g2': set([4]), 'g1': set([1])} ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing sub modules
"bob gailer" wrote On 3/31/2011 1:07 PM, Prasad, Ramit wrote: This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, . no responsibility is accepted by JPMorgan Chase& ... I hope your lawyers are happy with the above text. I personally find it annoying. Its a sad fact of modern life that many companies insist on adding these legal notices to outgoing mail. My own company does the same to anything I send from my work account outside the business. Its not quite as long as JPs above but it has much the same tone. :-( Corporate paranoia is a sad but inevitable side effect of the litigation crazy society we now live in coupled to the universal reach of the internet... Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing values from a dictionary if they are present in a list
On Fri, Apr 1, 2011 at 9:52 AM, ranjan das wrote: > I have the following information > > A={'g2': [4,5,3], 'g1': [1, 3]} > > B=[2,3,5] > > Now I want to remeove the elements in B if they are present (as values) in > dictionary A. > > My expected solution is > > A= {'g2': [4], 'g1': [1] } > > I wrote the following piece of code which gives me thhe right code, but I am > sure there must be a much shorter and more elegant way of doing it. Please > suggest > > reject_list=[] > > for element in B: > > for key in A.keys(): > > for i in range(len(A[key])): > > if element==A[key][i]: > > reject_list.append((key,A[key][i])) > > > > print reject_list > > > for i in range(len(reject_list)): > > print (reject_list[i][0],reject_list[i][1]) > > Index=A[reject_list[i][0]].index(reject_list[i][1]) > > print (reject_list[i][0],reject_list[i][1],Index) > > del A[reject_list[i][0]][Index] First, your loops are distinctly unpythonic. In many other languages one does indeed go over a list or similar object by having a numeric loop variable i, and then using list[i], but in Python we loop over the list itself instead. Applying that to your code, and removing the in-between prints, we get: reject_list=[] for element in B: for key in A.keys(): for Aelement in A[key]: if element == Aelement: reject_list.append(key, Aelement) for deleteelement in reject_list: index = A[deletelement[0]].index(deleteelement[1]) del A[deleteelement[0]][index] Next, realize that we are comparing each element in B with each element in A, and then add information only on the element of A to the list. Also, there is no need to add something twice if it occurs twice in B. Thus, we can simplify this by just checking for each element of A whether it is in B: reject_list = [] for key in A.keys(): for element in A[key]: if element in B: reject_list.append(key, element) for deleteelement in reject_list: index = A[deletelement[0]].index(deleteelement[1]) del A[deleteelement[0]][index] However, when working this way, we will have all elements from one key in A together. We could also work with a separate list for each key, and do those in turns: for key in A.keys(): reject_list = [] for element in A[key]: if element in B: reject_list.append(element) for element in reject_list: index = A[key].index(element) del A[key][index] Still, we can go further. Why first create a list of things to do and then do it? We can do it immediately; however, then we have to change the loop variable, because things go wrong if you remove elements from a list while looping over that same list. for key in A.keys(): copy = A[key][:] for element in copy: if element in B: index = A[key].index(element) del A[key][index] A further small shortening is done by realizing that the default way of looping over a dictionary is to loop over its keys: for key in A: copy = A[key][:] for element in copy: if element in B: index = A[key].index(element) del A[key][index] A following step is to see that we only use the keys to get to the values. Why not use the values directly? for value in A.values(): copy = value[:] for element in copy: if element in B: index = value.index(element) del value[index] Can this still be shortened? Definitely. However, for the most obvious shortening I have to make an assumption, namely that the values in A are _not_ used elsewhere. Upto now we have removed elements from those values; we now will replace the value instead. This means that in Z = [0, 1] A = {"Hello": Z, "Goodbye: [0,2]} B = [0] # The previous code Z will have changed to [1] whereas in Z = [0, 1] A = {"Hello": Z, "Goodbye: [0,2]} B = [0] # The upcoming code Z will have remained [0,1] With this proviso, we have: for key in A: newvalue = [] for element in A[key]: if element not in B: newvalue.append(element) A[key] = newvalue This on itself is not shorter than the previous forms, but it can be used in a list expression like this: for key in A: A[key] = [element for element in A[key] if element not in B] -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing values from a dictionary if they are present in a list
ranjan das wrote: I have the following information A={'g2': [4,5,3], 'g1': [1, 3]} B=[2,3,5] Now I want to remeove the elements in B if they are present (as values) in dictionary A. My expected solution is A= {'g2': [4], 'g1': [1] } Do you care about the order of the elements in A's values? Will there be any duplicate values? If the answer is No to both of those, the simplest solution is probably this: b = set(B) for key, values in A.items(): A[key] = list( set(values).difference(b) ) For Python 3, you will need to change the call A.items() to list(A.items()), but otherwise they should be the same. If you do care about order and duplicates, then this should do it: # untested for key, values in A.items(): A[key] = [x for x in values if x not in B] Not the most efficient code in the universe, but for small lists (say, a few hundred items) it should be perfectly fine. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Removing values from a dictionary if they are present in a list
Steven D'Aprano wrote: > b = set(B) > for key, values in A.items(): > A[key] = list( set(values).difference(b) ) > > > For Python 3, you will need to change the call A.items() to > list(A.items()), but otherwise they should be the same. The documentation doesn't say so explicitly, see http://docs.python.org/dev/py3k/library/stdtypes.html """ Iterating views while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries. """ but I think you can change the values without converting the dictview to a list first: >>> a = {1:2, 3:4, 5:6} >>> for k, v in a.items(): ... a[k] = "+" * v ... >>> a {1: '++', 3: '', 5: '++'} ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing sub modules
>However the sub-modules co.py and order.py are *not* imported by the >main module. They are only loaded on demand, when you say "import >stats.co" or "from stats.order import median" or similar. >So it depends on the module. That is exactly what I wondering and an excellent example to illustrate it. Thank you! Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates. This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you. Please refer to http://www.jpmorgan.com/pages/disclosures for disclosures relating to European legal entities. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] PyVISA GPIB
I would like to control electronic instruments with PyVISA. I have downloaded PyVISA and unpacked the files into the Python27/lib/site-packages dir and in the IDLE GUI I run "import visa' for a quick check and I get this error: import visa Traceback (most recent call last): File "", line 1, in import visa ImportError: No module named visa I'm scratching my head. Help Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] argparse csv + choices
On 03/30/2011 06:05 PM, Robert Kern wrote: On 3/30/11 10:32 AM, Neal Becker wrote: I'm trying to combine 'choices' with a comma-seperated list of options, so I could do e.g., --cheat=a,b parser.add_argument ('--cheat', choices=('a','b','c'), type=lambda x: x.split(','), default=[]) test.py --cheat a error: argument --cheat: invalid choice: ['a'] (choose from 'a', 'b', 'c') The validation of choice is failing, because parse returns a list, not an item. Suggestions? Do the validation in the type function. import argparse class ChoiceList(object): def __init__(self, choices): self.choices = choices def __repr__(self): return '%s(%r)' % (type(self).__name__, self.choices) def __call__(self, csv): args = csv.split(',') remainder = sorted(set(args) - set(self.choices)) if remainder: raise ValueError("invalid choices: %r (choose from %r)" % (remainder, self.choices)) return args parser = argparse.ArgumentParser() parser.add_argument('--cheat', type=ChoiceList(['a','b','c']), default=[]) print parser.parse_args(['--cheat=a,b']) parser.parse_args(['--cheat=a,b,d']) Hello, Great code, Simply for nicer output could be: def __call__(self, csv): try: args = csv.split(',') remainder = sorted(set(args) - set(self.choices)) if remainder: raise ValueError("invalid choices: %r (choose from %r)" % (remainder, self.choices)) return args except ValueError, e: raise argparse.ArgumentTypeError(e) Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA GPIB
Hi Mark, On Fri, Apr 1, 2011 at 11:42 AM, wrote: > I would like to control electronic instruments with PyVISA. I have > downloaded PyVISA and unpacked the files into the Python27/lib/site-packages > dir and in the IDLE > GUI I run "import visa' for a quick check and I get this error: > > import visa > > Traceback (most recent call last): > File "", line 1, in > import visa > ImportError: No module named visa > > I'm scratching my head. Help > > Mark R Rivet, Genesis Software Consulting > ASCT(Computer Technologies), BSIT/SE(Software Engineering) > Electrical Engineering Technician > Member IEEE, Computer Society > > > Do or do not; there is no try. Could this be the problem? PyVISA doesn’t implement VISA itself. Instead, PyVISA provides bindings to the VISA library (a DLL or “shared object” file). This library is usually shipped with your GPIB interface or software like LabVIEW. Alternatively, you can download it from your favourite equipment vendor (National Instruments, Agilent, etc). quote from this document: http://pyvisa.sourceforge.net/pyvisa.pdf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Meta language and code generation
Hello All, I would to ask you if somebody has experience or can give direction in a new project I have. I have a meta language description (in xml) from which I should generate code on different languages. In my case, lisp and tcl. Any idea in term of design, examples, links will be appreciated! Kind Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA GPIB
Sorry, On Fri, Apr 1, 2011 at 1:00 PM, Donald Bedsole wrote: > Hi Mark, > > On Fri, Apr 1, 2011 at 11:42 AM, wrote: >> I would like to control electronic instruments with PyVISA. I have >> downloaded PyVISA and unpacked the files into the Python27/lib/site-packages >> dir and in the IDLE >> GUI I run "import visa' for a quick check and I get this error: >> >> import visa >> >> Traceback (most recent call last): >> File "", line 1, in >> import visa >> ImportError: No module named visa >> >> I'm scratching my head. Help >> >> Mark R Rivet, Genesis Software Consulting >> ASCT(Computer Technologies), BSIT/SE(Software Engineering) >> Electrical Engineering Technician >> Member IEEE, Computer Society >> >> >> Do or do not; there is no try. > > Could this be the problem? > > PyVISA doesn’t implement VISA itself. Instead, PyVISA provides > bindings to the VISA library (a DLL or > “shared object” file). This library is usually shipped with your GPIB > interface or software like LabVIEW. Alternatively, you can download it > from your favourite equipment vendor (National Instruments, Agilent, > etc). > > quote from this document: > > http://pyvisa.sourceforge.net/pyvisa.pdf > I read the document a little better and visa is supposed to be part of the function. But maybe something else in the document might help you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Run application from MS-DOS with sys.argv
Hello! I'm going to answer to the question I posted in this list. After a search trouhg web and analize the code, I just had to add a try/except statement. Now I get what I need. Here is the information: directorio = sys.argv[1] extension = sys.argv[2] csv_salida = sys.argv[3] #Here I add the line: try: if len(sys.argv) == 4: else: print "Tus argumentos no son correctos" and here: except IndexError: print "Tus argumentos no son correctos" 2011/3/29 Susana Iraiis Delgado Rodriguez > Hello List: > > I developed a script to walk through a specific directory in my PC and > look for files with the same extension (.shp). I want the user to enter from > MS-DOS and write the the directory, file extension and csv filename. > My script reads the arguments from Windows console, but when I opened the > txt file and csv file I noticed that isn't walking through all the root I > wrote in MS-DOS. This is my module: > > import os, csv, time, socket, sys > from osgeo import ogr,gdal,osr > #This should be the order for the arguments('csv_args.py [root] > [file_extension] [csv filename]') > #The user is typing python csv_args.py C:\ .shp csv.csv > directorio = sys.argv[1] > extension = sys.argv[2] > csv_salida = sys.argv[3] > if len(sys.argv) == 4: > print 'Iniciando...' > gdal.AllRegister() > file_list = [] > folders = None > for root, folders, files in os.walk(directorio): > file_list.extend(os.path.join(root,fi) for fi in files if > fi.endswith(extension)) > f = open(csv_salida, 'wb') > log = open ('errores.txt','w') > writer = csv.writer(f) > ruta = 'Ruta' > archivo = 'archivo' > x_min = 'x_min' > x_max = 'x_max' > y_min = 'y_min' > y_max = 'y_max' > geometria = 'geometria' > num_elem = 'num_elem' > prj = '.prj' > proyeccion = 'proyeccion' > fecha = 'fecha_modificacion' > maq = 'maquina_host' > usu = 'usuario' > campos = > [ruta,archivo,x_min,x_max,y_min,y_max,geometria,num_elem,prj,proyeccion,fecha,maq,usu] > writer.writerow(campos) > for row, filepath in enumerate(file_list, start=1): > (ruta, filename) = os.path.split(filepath) > shapeData = ogr.Open(filepath) > shp = 'Error al abrir el archivo' +filepath > if shapeData is None: > print shp > log.write(shp+"\n") > else: > layer = shapeData.GetLayer() > feature = layer.GetNextFeature() > x_y = layer.GetExtent() > x1 = x_y[0] > x2 = x_y[1] > y1 = x_y[2] > y2 = x_y[3] > defn = layer.GetLayerDefn() > geo = defn.GetGeomType() > cuenta = layer.GetFeatureCount() > proy = layer.GetSpatialRef() > prjtext = ''+str(proy)+'' > n = os.path.splitext(filepath) > p = n[0]+'.prj' > shx = n[0]+'.shx' > dbf = n[0]+'.dbf' > filepath = ''+filepath+'' > filename = ''+filename+'' > t = time.strftime("%m/%d/%Y %I:%M:%S > %p",time.localtime(os.path.getmtime(filepath))) > modificacion = ''+t+'' > usuario = os.environ.get("USERNAME") > user = ''+usuario+'' > host = socket.gethostname() > maquina = ''+host+'' > if os.path.exists(shx): > print 'El archivo ' +shx +' existe' > else: > og.write('No existe el archivo ' +shx+"\n") > if os.path.exists(dbf): > print 'El archivo ' +dbf +' existe' > else: > log.write('No existe el archivo ' +dbf+"\n") > if os.path.exists(p): > aRow= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 1, > prjtext, modificacion, maquina, user] > writer.writerow(aRow) > else: > aRow1= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, > 0, prjtext, modificacion, maquina, user] > writer.writerow(aRow1) > log.close() > f.close() > print "El archivo esta listo" > else: > print "Tus argumentos no son correctos" > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA GPIB
On 01/04/2011 18:00, Donald Bedsole wrote: Hi Mark, On Fri, Apr 1, 2011 at 11:42 AM, wrote: I would like to control electronic instruments with PyVISA. I have downloaded PyVISA and unpacked the files into the Python27/lib/site-packages dir and in the IDLE GUI I run "import visa' for a quick check and I get this error: import visa Traceback (most recent call last): File "", line 1, in import visa ImportError: No module named visa I'm scratching my head. Help Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. Could this be the problem? No. The problem above is at a higher level. This is what you get at a lower level trying to import the code. At least I think so :) c:\Users\Mark\Cashflow\Python>python -V Python 2.7.1 c:\Users\Mark\Cashflow\Python>python -c "import visa" Traceback (most recent call last): File "", line 1, in File "c:\python27\lib\site-packages\visa.py", line 1, in from pyvisa.visa import * File "c:\python27\lib\site-packages\pyvisa\visa.py", line 231, in resource_manager = ResourceManager() File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 105, in __new__ it.init(*args, **kwds) File "c:\python27\lib\site-packages\pyvisa\visa.py", line 227, in init self.session = self.vi = vpp43.open_default_resource_manager() File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 758, in open_default_resource_manager visa_library().viOpenDefaultRM(byref(session)) File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 175, in __call__ self.load_library() File "c:\python27\lib\site-packages\pyvisa\vpp43.py", line 141, in load_library self.__lib = windll.visa32 File "c:\python27\lib\ctypes\__init__.py", line 423, in __getattr__ dll = self._dlltype(name) File "c:\python27\lib\ctypes\__init__.py", line 353, in __init__ self._handle = _dlopen(self._name, mode) WindowsError: [Error 126] The specified module could not be found PyVISA doesn’t implement VISA itself. Instead, PyVISA provides bindings to the VISA library (a DLL or “shared object” file). This library is usually shipped with your GPIB interface or software like LabVIEW. Alternatively, you can download it from your favourite equipment vendor (National Instruments, Agilent, etc). quote from this document: http://pyvisa.sourceforge.net/pyvisa.pdf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Cheers. Mark L. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] counting a list of elements
Hello, I would like to get advice on the best practice to count elements in a list (built from scractch). The number of elements is in the range 1e3 and 1e6. 1) I could create a generator and set a counter (i +=1) in the loop. 2) or simply len(mylist). I don't need the content of the list, indeed, in term of memory I don't want to wast it. But I suppose len() is optimized too (C impementation). If you have some thought to share don't hesitate. Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Meta language and code generation
On Fri, Apr 1, 2011 at 1:09 PM, Karim wrote: > > Hello All, > > I would to ask you if somebody has experience or can give direction in a > new project I have. > I have a meta language description (in xml) from which I should generate > code on different > languages. In my case, lisp and tcl. > > Any idea in term of design, examples, links will be appreciated! > > Karim, You might want check out Anltr. We were using it to translate from one query language to another. http://www.antlr.org/ http://www.antlr.org/api/Python/index.html -Tino ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA GPIB
"Donald Bedsole" said: > Sorry, > > On Fri, Apr 1, 2011 at 1:00 PM, Donald Bedsole wrote: >> Hi Mark, >> >> On Fri, Apr 1, 2011 at 11:42 AM, wrote: >>> I would like to control electronic instruments with PyVISA. I have >>> downloaded PyVISA and unpacked the files into the >>> Python27/lib/site-packages dir >>> and in the IDLE >>> GUI I run "import visa' for a quick check and I get this error: >>> >>> import visa >>> >>> Traceback (most recent call last): >>> File "", line 1, in >>> import visa >>> ImportError: No module named visa >>> >>> I'm scratching my head. Help >>> >>> Mark R Rivet, Genesis Software Consulting >>> ASCT(Computer Technologies), BSIT/SE(Software Engineering) >>> Electrical Engineering Technician >>> Member IEEE, Computer Society >>> >>> >>> Do or do not; there is no try. >> >> Could this be the problem? >> >> PyVISA doesn’t implement VISA itself. Instead, PyVISA provides >> bindings to the VISA library (a DLL or >> “shared object” file). This library is usually shipped with >> your GPIB >> interface or software like LabVIEW. Alternatively, you can download it >> from your favourite equipment vendor (National Instruments, Agilent, >> etc). >> >> quote from this document: >> >> http://pyvisa.sourceforge.net/pyvisa.pdf >> > > I read the document a little better and visa is supposed to be part of > the function. But maybe something else in the document might help > you. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Yes, I better download that doc and read it carefully; thanks Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA GPIB
"Donald Bedsole" said: > Hi Mark, > > On Fri, Apr 1, 2011 at 11:42 AM, wrote: >> I would like to control electronic instruments with PyVISA. I have >> downloaded PyVISA and unpacked the files into the Python27/lib/site-packages >> dir >> and in the IDLE >> GUI I run "import visa' for a quick check and I get this error: >> >> import visa >> >> Traceback (most recent call last): >> File "", line 1, in >> import visa >> ImportError: No module named visa >> >> I'm scratching my head. Help >> >> Mark R Rivet, Genesis Software Consulting >> ASCT(Computer Technologies), BSIT/SE(Software Engineering) >> Electrical Engineering Technician >> Member IEEE, Computer Society >> >> >> Do or do not; there is no try. > > Could this be the problem? > > PyVISA doesn’t implement VISA itself. Instead, PyVISA provides > bindings to the VISA library (a DLL or > “shared object” file). This library is usually shipped with your > GPIB > interface or software like LabVIEW. Alternatively, you can download it > from your favourite equipment vendor (National Instruments, Agilent, > etc). > > quote from this document: > > http://pyvisa.sourceforge.net/pyvisa.pdf > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Hmmm, that shouldn't be a problem, I have LabView installed on my pc. Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA GPIB
"Donald Bedsole" said: > Sorry, > > On Fri, Apr 1, 2011 at 1:00 PM, Donald Bedsole wrote: >> Hi Mark, >> >> On Fri, Apr 1, 2011 at 11:42 AM, wrote: >>> I would like to control electronic instruments with PyVISA. I have >>> downloaded PyVISA and unpacked the files into the >>> Python27/lib/site-packages dir >>> and in the IDLE >>> GUI I run "import visa' for a quick check and I get this error: >>> >>> import visa >>> >>> Traceback (most recent call last): >>> File "", line 1, in >>> import visa >>> ImportError: No module named visa >>> >>> I'm scratching my head. Help >>> >>> Mark R Rivet, Genesis Software Consulting >>> ASCT(Computer Technologies), BSIT/SE(Software Engineering) >>> Electrical Engineering Technician >>> Member IEEE, Computer Society >>> >>> >>> Do or do not; there is no try. >> >> Could this be the problem? >> >> PyVISA doesn’t implement VISA itself. Instead, PyVISA provides >> bindings to the VISA library (a DLL or >> “shared object” file). This library is usually shipped with >> your GPIB >> interface or software like LabVIEW. Alternatively, you can download it >> from your favourite equipment vendor (National Instruments, Agilent, >> etc). >> >> quote from this document: >> >> http://pyvisa.sourceforge.net/pyvisa.pdf >> > > I read the document a little better and visa is supposed to be part of > the function. But maybe something else in the document might help > you. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Well, I have Labview installed on my system including the visa libraries. Mark R Rivet, Genesis Software Consulting ASCT(Computer Technologies), BSIT/SE(Software Engineering) Electrical Engineering Technician Member IEEE, Computer Society Do or do not; there is no try. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
On 4/1/2011 10:16 AM Karim said... I would like to get advice on the best practice to count elements Well, I suspect you're more interested in knowing the total count of how many as opposed to counting how many. To get the total count of how many use len(mylist). Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
Am 01.04.2011 19:16, schrieb Karim: Hello, I would like to get advice on the best practice to count elements in a list (built from scractch). It's not clear to me what your starting point is. If you don't have a list and don't need a list, but have a large number of objects you create in your code sequentially or a large number of other events and you want to know how many of those objects exist / events have occured, then simply use a counter while creating. If you have a list (for whatever reason), then use len(). HTH, Jan The number of elements is in the range 1e3 and 1e6. 1) I could create a generator and set a counter (i +=1) in the loop. 2) or simply len(mylist). I don't need the content of the list, indeed, in term of memory I don't want to wast it. But I suppose len() is optimized too (C impementation). If you have some thought to share don't hesitate. Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
The nice thing about Python is you don't have to build things from scratch, and I imagine most people would discourage on account of it being a waste of time, other then education value. But the "best practice" in my humble opinion would be to use the built in len function. If what you are after is to see how often 1e3 comes up in your list, then you would use list.count('1e3'). In general, its best to use the python built ins. On Fri, Apr 1, 2011 at 1:16 PM, Karim wrote: > > > Hello, > > I would like to get advice on the best practice to count elements in a list > (built from scractch). > The number of elements is in the range 1e3 and 1e6. > > 1) I could create a generator and set a counter (i +=1) in the loop. > > 2) or simply len(mylist). > > I don't need the content of the list, indeed, in term of memory I don't > want to wast it. But I suppose len() is optimized too (C impementation). > > If you have some thought to share don't hesitate. > > Karim > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Meta language and code generation
Am 01.04.2011 19:09, schrieb Karim: Hello All, I would to ask you if somebody has experience or can give direction in a new project I have. I have a meta language description (in xml) from which I should generate code on different languages. In my case, lisp and tcl. You need to provide more information of your description to get some specific hints. The other day a had a xml file containing a business object model with hierarchy and relations. Then I wrote a code generator to build a module with Python classes for each business item. The code generator created properties for lazily resolving relations modelled via ids in the database and stuff like that. This was very straightforeward using a simple print statements like the following: print "class %s(object):\n" % class_name print "def __init__(self, %s)" % constr_arg ... Cheers, Jan Any idea in term of design, examples, links will be appreciated! Kind Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
Am 01.04.2011 19:16, schrieb Karim: Hello, I would like to get advice on the best practice to count elements in a list (built from scractch). The number of elements is in the range 1e3 and 1e6. 1) I could create a generator and set a counter (i +=1) in the loop. 2) or simply len(mylist). I don't need the content of the list, indeed, in term of memory I don't want to wast it. But I suppose len() is optimized too (C impementation). If you have some thought to share don't hesitate. Just a general suggestion: Provide code examples. I know most of the times you don't have code examples yet as you're thinking of how to solve your problems. But if you post one of the possible solutions the experienced guys here will very likely direct you in the proper direction. But without code it's hard to understand what you're after. Cheers, Jan Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Meta language and code generation
On 04/01/2011 08:29 PM, Knacktus wrote: Am 01.04.2011 19:09, schrieb Karim: Hello All, I would to ask you if somebody has experience or can give direction in a new project I have. I have a meta language description (in xml) from which I should generate code on different languages. In my case, lisp and tcl. You need to provide more information of your description to get some specific hints. The other day a had a xml file containing a business object model with hierarchy and relations. Then I wrote a code generator to build a module with Python classes for each business item. The code generator created properties for lazily resolving relations modelled via ids in the database and stuff like that. This was very straightforeward using a simple print statements like the following: print "class %s(object):\n" % class_name print "def __init__(self, %s)" % constr_arg ... Cheers, Jan In fact in xml I have something like that: A metafunction in fact kind of x y I have to generate the call_back_do_stuff_function(x,y) in lisp and tcl according to a catalog of specs of what this function must do generically. I can do prints for each metafunctio I read but my concern is is there std libs to help to have good design and methodology. Is it interesting to use command module of something like that, is interesting to use a parser like pylex or pyparsing? I have 50 metafunctions in catalog for now. In fact, my point is to have a great extensive design methodology. Strategy pattern would suit? Other patterns? std modules? Lots of question here! Regards Karim Any idea in term of design, examples, links will be appreciated! Kind Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA
Well I tried the setup file and here is what I get: C:\Users\Rivetmr\PyVisa_1.3\PyVISA-1.3>Python setup.py install Traceback (most recent call last): File "setup.py", line 60, in home_dir = os.environ['HOME'] File "C:\Python27\Lib\os.py", line 423, in __getitem__ return self.data[key.upper()] KeyError: 'HOME' From: Yashwin Kanchan [mailto:yashwinkanc...@gmail.com] Sent: Friday, April 01, 2011 11:53 AM To: Manatee Cc: python-l...@python.org Subject: Re: PyVISA Hi Have you installed the module after unzipping it? python setup.py install Got it from the README file in the downloaded tar. Regards Yashwin Kanchan On 1 April 2011 16:29, Manatee wrote: I have unpacked the PyVISA files into the Python/lib/site-packages dir and from the IDLE GUI I get and error import visa Traceback (most recent call last): File "", line 1, in import visa ImportError: No module named visa There must be more to just putting the files in the correct directory. Need help configuring PyVISA to work. My ultimate goal is to control electronic instruments with Python through visa. -- http://mail.python.org/mailman/listinfo/python-list ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Run application from MS-DOS with sys.argv
On 01/04/2011 18:15, Susana Iraiis Delgado Rodriguez wrote: Hello! I'm going to answer to the question I posted in this list. After a search trouhg web and analize the code, I just had to add a try/except statement. Now I get what I need. Here is the information: directorio = sys.argv[1] extension = sys.argv[2] csv_salida = sys.argv[3] #Here I add the line: try: if len(sys.argv) == 4: else: print "Tus argumentos no son correctos" and here: except IndexError: print "Tus argumentos no son correctos" You appear to be mixing two ways of doing the same thing. Either check the number of entries in sys.argv or catch the index error, don't do both. Read this, it probably explains things better than I can http://mail.python.org/pipermail/python-list/2003-May/203039.html 2011/3/29 Susana Iraiis Delgado Rodriguez mailto:susana.delgad...@utzmg.edu.mx>> Hello List: I developed a script to walk through a specific directory in my PC and look for files with the same extension (.shp). I want the user to enter from MS-DOS and write the the directory, file extension and csv filename. My script reads the arguments from Windows console, but when I opened the txt file and csv file I noticed that isn't walking through all the root I wrote in MS-DOS. This is my module: import os, csv, time, socket, sys from osgeo import ogr,gdal,osr #This should be the order for the arguments('csv_args.py [root] [file_extension] [csv filename]') #The user is typing python csv_args.py C:\ .shp csv.csv directorio = sys.argv[1] extension = sys.argv[2] csv_salida = sys.argv[3] if len(sys.argv) == 4: print 'Iniciando...' gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk(directorio): file_list.extend(os.path.join(root,fi) for fi in files if fi.endswith(extension)) f = open(csv_salida, 'wb') log = open ('errores.txt','w') writer = csv.writer(f) ruta = 'Ruta' archivo = 'archivo' x_min = 'x_min' x_max = 'x_max' y_min = 'y_min' y_max = 'y_max' geometria = 'geometria' num_elem = 'num_elem' prj = '.prj' proyeccion = 'proyeccion' fecha = 'fecha_modificacion' maq = 'maquina_host' usu = 'usuario' campos = [ruta,archivo,x_min,x_max,y_min,y_max,geometria,num_elem,prj,proyeccion,fecha,maq,usu] writer.writerow(campos) for row, filepath in enumerate(file_list, start=1): (ruta, filename) = os.path.split(filepath) shapeData = ogr.Open(filepath) shp = 'Error al abrir el archivo' +filepath if shapeData is None: print shp log.write(shp+"\n") else: layer = shapeData.GetLayer() feature = layer.GetNextFeature() x_y = layer.GetExtent() x1 = x_y[0] x2 = x_y[1] y1 = x_y[2] y2 = x_y[3] defn = layer.GetLayerDefn() geo = defn.GetGeomType() cuenta = layer.GetFeatureCount() proy = layer.GetSpatialRef() prjtext = ''+str(proy)+'' n = os.path.splitext(filepath) p = n[0]+'.prj' shx = n[0]+'.shx' dbf = n[0]+'.dbf' filepath = ''+filepath+'' filename = ''+filename+'' t = time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(filepath))) modificacion = ''+t+'' usuario = os.environ.get("USERNAME") user = ''+usuario+'' host = socket.gethostname() maquina = ''+host+'' if os.path.exists(shx): print 'El archivo ' +shx +' existe' else: og.write('No existe el archivo ' +shx+"\n") if os.path.exists(dbf): print 'El archivo ' +dbf +' existe' else: log.write('No existe el archivo ' +dbf+"\n") if os.path.exists(p): aRow= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 1, prjtext, modificacion, maquina, user] writer.writerow(aRow) else: aRow1= [ filepath, filename, x1, x2, y1, y2, geo, cuenta, 0, prjtext, modificacion, maquina, user] writer.writerow(aRow1) log.close() f.close() print "El archivo esta listo" else: print "Tus argumentos no son correctos" ___ Tutor maillist - Tutor@python.org To
Re: [Tutor] PyVISA
"Mark R Rivet" wrote Well I tried the setup file and here is what I get: C:\Users\Rivetmr\PyVisa_1.3\PyVISA-1.3>Python setup.py install Traceback (most recent call last): File "setup.py", line 60, in home_dir = os.environ['HOME'] Windows doesn't by default define a HOME environment variable. Are you sure your module is not Unix specific? If not then the easiest solution is probably to just define a value for HOME... and see if it works! HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyVISA
On 01/04/2011 20:10, Alan Gauld wrote: "Mark R Rivet" wrote Well I tried the setup file and here is what I get: C:\Users\Rivetmr\PyVisa_1.3\PyVISA-1.3>Python setup.py install Traceback (most recent call last): File "setup.py", line 60, in home_dir = os.environ['HOME'] Windows doesn't by default define a HOME environment variable. Are you sure your module is not Unix specific? If not then the easiest solution is probably to just define a value for HOME... and see if it works! HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor I think someone has already said this, but just to be sure there's a Windows executable here http://sourceforge.net/projects/pyvisa/files/PyVISA/1.3/ HTH. Mark L. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
On 04/01/2011 08:41 PM, Knacktus wrote: Am 01.04.2011 19:16, schrieb Karim: Hello, I would like to get advice on the best practice to count elements in a list (built from scractch). The number of elements is in the range 1e3 and 1e6. 1) I could create a generator and set a counter (i +=1) in the loop. 2) or simply len(mylist). I don't need the content of the list, indeed, in term of memory I don't want to wast it. But I suppose len() is optimized too (C impementation). If you have some thought to share don't hesitate. Just a general suggestion: Provide code examples. I know most of the times you don't have code examples yet as you're thinking of how to solve your problems. But if you post one of the possible solutions the experienced guys here will very likely direct you in the proper direction. But without code it's hard to understand what you're after. Cheers, Jan Thank you all for you answers to clarified I built a collection of dictionnaries which represent database query on a bug tracking system: backlog_tables , csv_backlog_table = _backlog_database(table=table, periods=intervals_list) backlog_tables is a dictionnary of bug info dictionnaries. The keys of backlog_tables is a time intervall (YEAR-MONTH) as shown below: backlog_tables= {'2011-01-01': [{'Assigned Date': datetime.date(2010, 10, 25), 'Category': 'Customer_claim', 'Date': datetime.date(2010, 10, 22), 'Duplicate Date': None, 'Fixed Reference': None, 'Headline': 'Impovement for all test', 'Identifier': '23269', 'Last Modified': datetime.date(2010, 10, 25), 'Priority': 'Low', 'Project': 'MY_PROJECT', 'Reference': 'MY_PROJECT@1.7beta2@20101006.0', 'Resolved Date': None, 'Severity': 'improvement', 'State': 'A', 'Submitter': 'Somebody'}, . } _backlog_database() compute the tuple backlog_tables , csv_backlog_table: In fact csv_backlog_table is the same as backlog_tables but instead of having the query dictionnaries it holds only the number of query which I use to create a CSV file and a graph over time range. _backlog_database() is as follow: def _backlog_database(table=None, periods=None): """Internal function. Re-arrange database table according to a time period. Only monthly management is computed in this version. @param table the database of the list of defects. Each defect is a dictionnary with fixed keys. @param periods the intervals list of months and the first element is the starting date and the the last element is the ending date in string format. @return (periods_tables, csv_table), a tuple of periodic dictionnary table and the same keys dictionnary with defect numbers associated values. """ if periods is None: raise ValueError('Time interval could not be empty!') periods_tables = {} csv_table = {} interval_table = [] for interval in periods: split_date = interval.split('-') for row in table: if not len(split_date) == 3: limit_date = _first_next_month_day(year=int(split_date[0]), month=int(split_date[1]), day=1) if row['Date'] < limit_date: if not row['Resolved Date']: if row['State'] == 'K': if row['Last Modified'] >= limit_date: interval_table.append(row) elif row['State'] == 'D': if row['Duplicate Date'] >= limit_date: interval_table.append(row) # New, Assigned, Opened, Postponed, Forwarded, cases. else: interval_table.append(row) else: if row['Resolved Date'] >= limit_date: interval_table.append(row) periods_tables[interval] = interval_table csv_table[interval] = str(len(interval_table)) interval_table = [] return periods_tables, csv_table This is not the whole function I reduce it on normal case but it shows what I am doing. In fact I choose to have both dictionnaries to debug my function and analyse what's going on. When everything will be fine I will need only the csv table (with number per period) to create the graphs. That's why I was asking for length computing. Honnestly, the actual queries number is 500 (bug id) but It could be more in other project. I was ambitious when I sais 1000 to 10 dictionnaries elements but for the whole list of products we have internally It could be 5. Regards Karim Karim ___ Tutor maillist - Tuto
Re: [Tutor] PyVISA
2011/4/1 Alan Gauld > > "Mark R Rivet" wrote > > > Well I tried the setup file and here is what I get: >> C:\Users\Rivetmr\PyVisa_1.3\PyVISA-1.3>Python setup.py install >> Traceback (most recent call last): >> File "setup.py", line 60, in >> home_dir = os.environ['HOME'] >> > > Windows doesn't by default define a HOME environment variable. > Are you sure your module is not Unix specific? > IDLE 1.2 >>> import os >>> os.environ '.. 'HOME': 'C:\\Documents and Settings\\Administrator', ...} this is not home? I am geting exactly the same error IDLE 1.2 No Subprocess >>> removing 'build' (and everything under it) Traceback (most recent call last): File "C:\Python25\Lib\PyVISA-1.3\setup.py", line 60, in home_dir = os.environ['HOME'] File "C:\Python25\lib\os.py", line 429, in __getitem__ return self.data[key.upper()] KeyError: 'HOME' -- Tsartsaris Sotirios ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Meta language and code generation
Am 01.04.2011 20:56, schrieb Karim: On 04/01/2011 08:29 PM, Knacktus wrote: Am 01.04.2011 19:09, schrieb Karim: Hello All, I would to ask you if somebody has experience or can give direction in a new project I have. I have a meta language description (in xml) from which I should generate code on different languages. In my case, lisp and tcl. You need to provide more information of your description to get some specific hints. The other day a had a xml file containing a business object model with hierarchy and relations. Then I wrote a code generator to build a module with Python classes for each business item. The code generator created properties for lazily resolving relations modelled via ids in the database and stuff like that. This was very straightforeward using a simple print statements like the following: print "class %s(object):\n" % class_name print " def __init__(self, %s)" % constr_arg ... Cheers, Jan In fact in xml I have something like that: A metafunction in fact kind of x y I have to generate the call_back_do_stuff_function(x,y) in lisp and tcl according to a catalog of specs of what this function must do generically. I can do prints for each metafunctio I read but my concern is is there std libs to help to have good design and methodology. Is it interesting to use command module of something like that, is interesting to use a parser like pylex or pyparsing? I have 50 metafunctions in catalog for now. In fact, my point is to have a great extensive design methodology. Strategy pattern would suit? Other patterns? std modules? No ideas about patterns or standarad lib modules from my side, but a short description of how I would do it: Create an abstraction for the catalogue. That's as far as I can see the hardest part. Here you have to decide if and how to split the things your functions have to do into reusable chunks. Then create code generators for these chunks (using print statements). You can name these functions and store the references in dicts like catalog_comp_name_to_tcl_gen. If you get new functions that need new building blocks you can write new generator functions and extend your dictionaries. The generation of your tcl and lisp "functions-frames" should be straigt forward. You need to link the parameters to the building block generator functions you've created before. When you're done with that, you can improve the design step by step. Too me, this approach works better than thinking to much about design in advance, as often you don't see what you really need unless you've started to code. HTH, Jan Lots of question here! Regards Karim Any idea in term of design, examples, links will be appreciated! Kind Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
Am 01.04.2011 21:31, schrieb Karim: On 04/01/2011 08:41 PM, Knacktus wrote: Am 01.04.2011 19:16, schrieb Karim: Hello, I would like to get advice on the best practice to count elements in a list (built from scractch). The number of elements is in the range 1e3 and 1e6. 1) I could create a generator and set a counter (i +=1) in the loop. 2) or simply len(mylist). I don't need the content of the list, indeed, in term of memory I don't want to wast it. But I suppose len() is optimized too (C impementation). If you have some thought to share don't hesitate. Just a general suggestion: Provide code examples. I know most of the times you don't have code examples yet as you're thinking of how to solve your problems. But if you post one of the possible solutions the experienced guys here will very likely direct you in the proper direction. But without code it's hard to understand what you're after. Cheers, Jan Thank you all for you answers to clarified I built a collection of dictionnaries which represent database query on a bug tracking system: backlog_tables , csv_backlog_table = _backlog_database(table=table, periods=intervals_list) backlog_tables is a dictionnary of bug info dictionnaries. The keys of backlog_tables is a time intervall (YEAR-MONTH) as shown below: backlog_tables= {'2011-01-01': [{'Assigned Date': datetime.date(2010, 10, 25), 'Category': 'Customer_claim', 'Date': datetime.date(2010, 10, 22), 'Duplicate Date': None, 'Fixed Reference': None, 'Headline': 'Impovement for all test', 'Identifier': '23269', 'Last Modified': datetime.date(2010, 10, 25), 'Priority': 'Low', 'Project': 'MY_PROJECT', 'Reference': 'MY_PROJECT@1.7beta2@20101006.0', 'Resolved Date': None, 'Severity': 'improvement', 'State': 'A', 'Submitter': 'Somebody'}, . } _backlog_database() compute the tuple backlog_tables , csv_backlog_table: In fact csv_backlog_table is the same as backlog_tables but instead of having the query dictionnaries it holds only the number of query which I use to create a CSV file and a graph over time range. _backlog_database() is as follow: def _backlog_database(table=None, periods=None): """Internal function. Re-arrange database table according to a time period. Only monthly management is computed in this version. @param table the database of the list of defects. Each defect is a dictionnary with fixed keys. @param periods the intervals list of months and the first element is the starting date and the the last element is the ending date in string format. @return (periods_tables, csv_table), a tuple of periodic dictionnary table and the same keys dictionnary with defect numbers associated values. """ if periods is None: raise ValueError('Time interval could not be empty!') periods_tables = {} csv_table = {} interval_table = [] for interval in periods: split_date = interval.split('-') for row in table: if not len(split_date) == 3: limit_date = _first_next_month_day(year=int(split_date[0]), month=int(split_date[1]), day=1) if row['Date'] < limit_date: if not row['Resolved Date']: if row['State'] == 'K': if row['Last Modified'] >= limit_date: interval_table.append(row) elif row['State'] == 'D': if row['Duplicate Date'] >= limit_date: interval_table.append(row) # New, Assigned, Opened, Postponed, Forwarded, cases. else: interval_table.append(row) else: if row['Resolved Date'] >= limit_date: interval_table.append(row) periods_tables[interval] = interval_table csv_table[interval] = str(len(interval_table)) interval_table = [] return periods_tables, csv_table This is not the whole function I reduce it on normal case but it shows what I am doing. In fact I choose to have both dictionnaries to debug my function and analyse what's going on. When everything will be fine I will need only the csv table (with number per period) to create the graphs. That's why I was asking for length computing. Honnestly, the actual queries number is 500 (bug id) but It could be more in other project. I was ambitious when I sais 1000 to 10 dictionnaries elements but for the whole list of products we have internally It could be 5. I see some similarity with my coding style (doing things "by the way"), which might not be so good ;-). With this background information I would keep the responsibilities seperated. Your _backlog_database() function is supposed to do one thing: Return a dictionary which holds the interval and a list of result dicts. You could call this dict interval_to_result_tables (to indicate that the values are lists). That's all your function should do. Then you want to print a report. This piece of functionality needs to know how long the lists for each dictionary entry are. Then this print_report function should be responsible to get the information it needs by creating it itself or calling another function, which has the purpose to create the information. Latter would be a bit too much, as the length would be simply be: number_of_tables = len(interval_to_result
[Tutor] Finding prime numbers script runs much faster when run via bash shell than idle
Hello there, Totally new to python with some *nix scripting knowledge. I wrote a simple piece of code as an exercise to an online -free- class that finds prime numbers. When I run it via IDLE it's taking way more time than if I run it via a (bash) shell (on Os X 10.6) while doing $>python -d mypp.py I'm really curious from a performance perspective as to what could cause such a noticeable difference. Thank you very much! Here is the code --- #Finding the 1000th Prime number # # ### STATE VARIABLES INITIALIZATION ### tested_number = 3 testing_against = 3 prime_counter = 1 ### Starting the loop ## ### Number of prime numbers we want to find ## while(prime_counter < 1000): ### Testing if there is remainder of the division by the testing_against var while ((tested_number%testing_against != 0) and (testing_against < tested_number)): testing_against=testing_against + 1 if (tested_number != testing_against): x = 1 else: prime_counter = prime_counter + 1 print prime_counter, 'found so far' ## Incrementing the tested number by 2 so we only test odd numbers tested_number = tested_number + 2 ## Reinitialization of the testing_against var to reenter the second loop in the required var state testing_against = 3 ## Printing the prime number print (tested_number - 2), 'is the 1000th prime number' -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Meta language and code generation
On 04/02/2011 06:38 AM, Knacktus wrote: Am 01.04.2011 20:56, schrieb Karim: On 04/01/2011 08:29 PM, Knacktus wrote: Am 01.04.2011 19:09, schrieb Karim: Hello All, I would to ask you if somebody has experience or can give direction in a new project I have. I have a meta language description (in xml) from which I should generate code on different languages. In my case, lisp and tcl. You need to provide more information of your description to get some specific hints. The other day a had a xml file containing a business object model with hierarchy and relations. Then I wrote a code generator to build a module with Python classes for each business item. The code generator created properties for lazily resolving relations modelled via ids in the database and stuff like that. This was very straightforeward using a simple print statements like the following: print "class %s(object):\n" % class_name print " def __init__(self, %s)" % constr_arg ... Cheers, Jan In fact in xml I have something like that: A metafunction in fact kind of x y I have to generate the call_back_do_stuff_function(x,y) in lisp and tcl according to a catalog of specs of what this function must do generically. I can do prints for each metafunctio I read but my concern is is there std libs to help to have good design and methodology. Is it interesting to use command module of something like that, is interesting to use a parser like pylex or pyparsing? I have 50 metafunctions in catalog for now. In fact, my point is to have a great extensive design methodology. Strategy pattern would suit? Other patterns? std modules? No ideas about patterns or standarad lib modules from my side, but a short description of how I would do it: Create an abstraction for the catalogue. That's as far as I can see the hardest part. Here you have to decide if and how to split the things your functions have to do into reusable chunks. Then create code generators for these chunks (using print statements). You can name these functions and store the references in dicts like catalog_comp_name_to_tcl_gen. If you get new functions that need new building blocks you can write new generator functions and extend your dictionaries. The generation of your tcl and lisp "functions-frames" should be straigt forward. You need to link the parameters to the building block generator functions you've created before. When you're done with that, you can improve the design step by step. Too me, this approach works better than thinking to much about design in advance, as often you don't see what you really need unless you've started to code. HTH, Jan Thank you very much Jan! I have a direction. I see the the light now ;o). Karim Lots of question here! Regards Karim Any idea in term of design, examples, links will be appreciated! Kind Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting a list of elements
On 04/02/2011 07:00 AM, Knacktus wrote: Am 01.04.2011 21:31, schrieb Karim: On 04/01/2011 08:41 PM, Knacktus wrote: Am 01.04.2011 19:16, schrieb Karim: Hello, I would like to get advice on the best practice to count elements in a list (built from scractch). The number of elements is in the range 1e3 and 1e6. 1) I could create a generator and set a counter (i +=1) in the loop. 2) or simply len(mylist). I don't need the content of the list, indeed, in term of memory I don't want to wast it. But I suppose len() is optimized too (C impementation). If you have some thought to share don't hesitate. Just a general suggestion: Provide code examples. I know most of the times you don't have code examples yet as you're thinking of how to solve your problems. But if you post one of the possible solutions the experienced guys here will very likely direct you in the proper direction. But without code it's hard to understand what you're after. Cheers, Jan Thank you all for you answers to clarified I built a collection of dictionnaries which represent database query on a bug tracking system: backlog_tables , csv_backlog_table = _backlog_database(table=table, periods=intervals_list) backlog_tables is a dictionnary of bug info dictionnaries. The keys of backlog_tables is a time intervall (YEAR-MONTH) as shown below: backlog_tables= {'2011-01-01': [{'Assigned Date': datetime.date(2010, 10, 25), 'Category': 'Customer_claim', 'Date': datetime.date(2010, 10, 22), 'Duplicate Date': None, 'Fixed Reference': None, 'Headline': 'Impovement for all test', 'Identifier': '23269', 'Last Modified': datetime.date(2010, 10, 25), 'Priority': 'Low', 'Project': 'MY_PROJECT', 'Reference': 'MY_PROJECT@1.7beta2@20101006.0', 'Resolved Date': None, 'Severity': 'improvement', 'State': 'A', 'Submitter': 'Somebody'}, . } _backlog_database() compute the tuple backlog_tables , csv_backlog_table: In fact csv_backlog_table is the same as backlog_tables but instead of having the query dictionnaries it holds only the number of query which I use to create a CSV file and a graph over time range. _backlog_database() is as follow: def _backlog_database(table=None, periods=None): """Internal function. Re-arrange database table according to a time period. Only monthly management is computed in this version. @param table the database of the list of defects. Each defect is a dictionnary with fixed keys. @param periods the intervals list of months and the first element is the starting date and the the last element is the ending date in string format. @return (periods_tables, csv_table), a tuple of periodic dictionnary table and the same keys dictionnary with defect numbers associated values. """ if periods is None: raise ValueError('Time interval could not be empty!') periods_tables = {} csv_table = {} interval_table = [] for interval in periods: split_date = interval.split('-') for row in table: if not len(split_date) == 3: limit_date = _first_next_month_day(year=int(split_date[0]), month=int(split_date[1]), day=1) if row['Date'] < limit_date: if not row['Resolved Date']: if row['State'] == 'K': if row['Last Modified'] >= limit_date: interval_table.append(row) elif row['State'] == 'D': if row['Duplicate Date'] >= limit_date: interval_table.append(row) # New, Assigned, Opened, Postponed, Forwarded, cases. else: interval_table.append(row) else: if row['Resolved Date'] >= limit_date: interval_table.append(row) periods_tables[interval] = interval_table csv_table[interval] = str(len(interval_table)) interval_table = [] return periods_tables, csv_table This is not the whole function I reduce it on normal case but it shows what I am doing. In fact I choose to have both dictionnaries to debug my function and analyse what's going on. When everything will be fine I will need only the csv table (with number per period) to create the graphs. That's why I was asking for length computing. Honnestly, the actual queries number is 500 (bug id) but It could be more in other project. I was ambitious when I sais 1000 to 10 dictionnaries elements but for the whole list of products we have internally It could be 5. I see some similarity with my coding style (doing things "by the way"), which might not be so good ;-). With this background information I would keep the responsibilities seperated. Your _backlog_database() function is supposed to do one thing: Return a dictionary which holds the interval and a list of result dicts. You could call this dict interval_to_result_tables (to indicate that the values are lists). That's all your function should do. Then you want to print a report. This piece of functionality needs to know how long the lists for each dictionary entry are. Then this print_report function should be responsible to get the information it needs by creating it itself or calling another function, which has the purpose to create the information. Latter would be a bit too much, as the length would be simply b
Re: [Tutor] Finding prime numbers script runs much faster when run via bash shell than idle
On 04/02/2011 07:10 AM, Jaime Gago wrote: Hello there, Totally new to python with some *nix scripting knowledge. I wrote a simple piece of code as an exercise to an online -free- class that finds prime numbers. When I run it via IDLE it's taking way more time than if I run it via a (bash) shell (on Os X 10.6) while doing $>python -d mypp.py I'm really curious from a performance perspective as to what could cause such a noticeable difference. Thank you very much! Here is the code --- #Finding the 1000th Prime number # # ### STATE VARIABLES INITIALIZATION ### tested_number = 3 testing_against = 3 prime_counter = 1 ### Starting the loop ## ### Number of prime numbers we want to find ## while(prime_counter< 1000): ### Testing if there is remainder of the division by the testing_against var while ((tested_number%testing_against != 0) and (testing_against< tested_number)): testing_against=testing_against + 1 if (tested_number != testing_against): x = 1 else: prime_counter = prime_counter + 1 print prime_counter, 'found so far' ## Incrementing the tested number by 2 so we only test odd numbers tested_number = tested_number + 2 ## Reinitialization of the testing_against var to reenter the second loop in the required var state testing_against = 3 ## Printing the prime number print (tested_number - 2), 'is the 1000th prime number' -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Just a advice for readibility: testing_against += 1 prime_counter = prime_counter + 1 instead of: testing_against=testing_against + 1 prime_counter += 1 And IDLE add a layer due to the interactive feature to display result. It is python interpreter plus Tk layer. Just a thought nothing truly precise. Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding prime numbers script runs much faster when run via bash shell than idle
On 04/02/2011 07:10 AM, Jaime Gago wrote: Hello there, Totally new to python with some *nix scripting knowledge. I wrote a simple piece of code as an exercise to an online -free- class that finds prime numbers. When I run it via IDLE it's taking way more time than if I run it via a (bash) shell (on Os X 10.6) while doing $>python -d mypp.py I'm really curious from a performance perspective as to what could cause such a noticeable difference. Thank you very much! Here is the code --- #Finding the 1000th Prime number # # ### STATE VARIABLES INITIALIZATION ### tested_number = 3 testing_against = 3 prime_counter = 1 ### Starting the loop ## ### Number of prime numbers we want to find ## while(prime_counter< 1000): ### Testing if there is remainder of the division by the testing_against var while ((tested_number%testing_against != 0) and (testing_against< tested_number)): testing_against=testing_against + 1 if (tested_number != testing_against): x = 1 else: prime_counter = prime_counter + 1 print prime_counter, 'found so far' ## Incrementing the tested number by 2 so we only test odd numbers tested_number = tested_number + 2 ## Reinitialization of the testing_against var to reenter the second loop in the required var state testing_against = 3 ## Printing the prime number print (tested_number - 2), 'is the 1000th prime number' -- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Just a advice for readibility: testing_against += 1 prime_counter = prime_counter + 1 instead of: testing_against=testing_against + 1 prime_counter += 1 And IDLE adds a layer due to the interactive feature to display result. It is python interpreter plus Tk layer. Just a thought nothing truly precise. Regards Karim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor