View Issue Details

IDProjectCategoryView StatusLast Update
0000459ascendpygtk guipublic2011-04-12 09:44
Reporterjohn 
Assigned To 
PrioritynormalSeverityminorReproducibilityhave not tried
Status newResolutionopen 
Product Version 
Target Version1.0Fixed in Version 
Summary0000459: in Python, can't address range of array elements
Descriptionit would be useful to be able to assign values to a range of elements, eg

M.a[1:5] = 5

or something like that. (Kelvin's suggestion)
TagsNo tags attached.

Relationships

Activities

ujjavalverma10

2011-04-09 04:01

reporter   ~0000718

Last edited: 2011-04-09 04:15

View 3 revisions

Overriding the __setslice__ method of a list object solves this one. I have uploaded a code to demonstrate this. Please verify that I have done it correctly.

-- code --
class myList(list):
    def __setslice__(self, i, j, sequence):
        for x in range(i,j):
            self.__setitem__(x,sequence)

a = myList('123456789');
a[1:4] = 5;

-- code ends --

ujjavalverma10

2011-04-09 04:13

reporter  

setslice.py (172 bytes)

john

2011-04-09 13:28

administrator   ~0000720

Please provide the modified code in the form of a patch for the ASCEND code (use "svn diff").

Be careful to think about what happens when the array indices are invalid (exception handling).

ujjavalverma10

2011-04-11 04:42

reporter   ~0000724

I have made some changes to handle boundary cases and the new class now extends the original list object syntax instead of modifying it. I am uploading the file.

But I feel it is better to use it in form of a separate file instead of modifying any existing python file.
Please suggest a file in which I can ADD this new class and submit the changes as a patch.

ujjavalverma10

2011-04-11 04:43

reporter  

listExtend.py (713 bytes)

john

2011-04-11 14:43

administrator   ~0000725

The method for patches relating to new files is

svn up
# make your changes, including create a new file somedir/myfile.c
svn add somedir/myfile.c
svn diff > my.patch

# if you had write access you could also then run
# svn ci
# to commit (check in) your changes.

ujjavalverma10

2011-04-12 06:02

reporter  

ujjaval-issue459.patch (1,547 bytes)
Index: ascend/system/system.c
===================================================================
--- ascend/system/system.c	(revision 3365)
+++ ascend/system/system.c	(working copy)
@@ -88,12 +88,7 @@
   slv_set_instance(sys,inst);
 
 #if DOTIME
-  comptime = tm_cpu_time() - comptime;
-  if(comptime >= 1e-3){
-    CONSOLE_DEBUG("System built (time %5.3f s)",comptime);
-  }else{
-    CONSOLE_DEBUG("System built (time <1ms)");
-  }
+  FPRINTF(stderr,"Time to build system = %g s (outputs zero for build time < 1ms )\n", ((tm_cpu_time() - comptime)>=0.00001)?(tm_cpu_time()-comptime):0);
 #endif
   return(sys);
 }
Index: pygtk/listExtend.py
===================================================================
--- pygtk/listExtend.py	(revision 0)
+++ pygtk/listExtend.py	(revision 0)
@@ -0,0 +1,31 @@
+##############################
+# Modified list class that enables assigning values to a range of elements
+# Submited by : Ujjaval
+# Addresses Issue 459
+##############################
+
+class myList(list):
+	def __setslice__(self, i, j, sequence):
+		if(isinstance(sequence,list)):
+			list.__setslice__(self, i, j, sequence)
+		else:
+			try:
+				for x in range(i,j):
+					self.__setitem__(x,sequence)
+			except:
+				print "Unexpected error:", sys.exc_info()[0]
+
+#################################
+#		SAMPLE USAGE :
+#
+#	1.Extended Syntax :
+#a = myList('123456789');
+#print a;
+#a[1:4] = 5;
+#print a;
+#	2.Original Syntax :
+#b = myList('123456789');
+#print b;
+#b[1:4] = [34,65];
+#print b;
+#################################
ujjaval-issue459.patch (1,547 bytes)

ujjavalverma10

2011-04-12 06:03

reporter   ~0000731

I have uploaded the new file as a patch.

john

2011-04-12 09:44

administrator   ~0000732

The attached code is not a patch at all... it is just an isolated code fragment. To fix this bug you need to incorporate the __setslice__ overloading into ASCEND's Python code.

Issue History

Date Modified Username Field Change
2010-05-21 10:57 john New Issue
2011-02-22 17:34 john Target Version => 1.0
2011-04-09 04:01 ujjavalverma10 Note Added: 0000718
2011-04-09 04:13 ujjavalverma10 File Added: setslice.py
2011-04-09 04:14 ujjavalverma10 Note Edited: 0000718 View Revisions
2011-04-09 04:15 ujjavalverma10 Note Edited: 0000718 View Revisions
2011-04-09 13:28 john Note Added: 0000720
2011-04-11 04:42 ujjavalverma10 Note Added: 0000724
2011-04-11 04:43 ujjavalverma10 File Added: listExtend.py
2011-04-11 14:43 john Note Added: 0000725
2011-04-12 06:02 ujjavalverma10 File Added: ujjaval-issue459.patch
2011-04-12 06:03 ujjavalverma10 Note Added: 0000731
2011-04-12 09:44 john Note Added: 0000732