replace numpy with struct
PATCH 1/1 replace numpy with struct
This patch replaces the use of numpy with the standard Library struct module where possible.
---
scripts/analyze-migration.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
scripts/analyze-migration.py +12 -7
@@ -36,23 +36,28 @@ class MigrationFile(object): self.file = open(self.filename, "rb") def read64(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0]) + buffer = file.read(64) + return struct.unpack('>i16', buffer)[0] def read32(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0]) + buffer = file.read(32) + return struct.unpack('>i8', buffer)[0] def read16(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0]) + buffer = file.read(16) + return struct.unpack('>i4', buffer)[0] def read8(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0]) + buffer = file.read(8) + return struct.unpack('>i2', buffer)[0] def readstr(self, len = None): + buffer = file.read(8) if len is None: len = self.read8() if len == 0: return "" - return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0] + return np.array(struct.unpack(str(len) + 'd', buffer)[0]) def readvar(self, size = None): if size is None:
@@ -303,8 +308,8 @@ class VMSDFieldInt(VMSDFieldGeneric): def read(self): super(VMSDFieldInt, self).read() - self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0] - self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0] + self.sdata = np.array(struct.unpack(self.sdtype, self.data)[0]) + self.udata = np.array(struct.unpack(self.udtype, self.data)[0]) self.data = self.sdata return self.data
Signature · 1 line
2.7.4
Hi Joannah,
The common subject to use is [RFC PATCH] instead of [PATCH WIP]
Quoted 1 line · Joannah Nanjekye
This patch replaces the use of numpy with the standard Library struct module where possible.
This seems a good idea but why?
Do you have any performance improvements results to share?
Quoted 25 lines
Signed-off-by Joannah Nanjekye <[email protected]>---
scripts/analyze-migration.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)scripts/analyze-migration.py +4
-3@@ -36,23 +36,28 @@ class MigrationFile(object): self.file = open(self.filename, "rb") def read64(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0]) + buffer = file.read(64) + return struct.unpack('>i16', buffer)[0] def read32(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0]) + buffer = file.read(32) + return struct.unpack('>i8', buffer)[0] def read16(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0])
scripts/analyze-migration.py class MigrationFile(object): · line 55
Quoted 10 lines
scripts/analyze-migration.py +5
-1+ buffer = file.read(16) + return struct.unpack('>i4', buffer)[0] def read8(self): - return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0]) + buffer = file.read(8) + return struct.unpack('>i2', buffer)[0] def readstr(self, len = None): + buffer = file.read(8)
^ move this
scripts/analyze-migration.py class MigrationFile(object): · line 59
Quoted 4 lines
scripts/analyze-migration.py +0
-0if len is None: len = self.read8() if len == 0: return ""
here to avoid a read() if len=0
scripts/analyze-migration.py class MigrationFile(object): · line 60
Quoted 2 lines
scripts/analyze-migration.py +1
-1- return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0] + return np.array(struct.unpack(str(len) + 'd', buffer)[0])
Well this is now a mix of np + struct ...
This looks a bit more messy.
scripts/analyze-migration.py class VMSDFieldInt(VMSDFieldGeneric): · line 312
Quoted 11 lines
scripts/analyze-migration.py +2
-2def readvar(self, size = None): if size is None:@@ -303,8 +308,8 @@ class VMSDFieldInt(VMSDFieldGeneric): def read(self): super(VMSDFieldInt, self).read() - self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0] - self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0] + self.sdata = np.array(struct.unpack(self.sdtype, self.data)[0]) + self.udata = np.array(struct.unpack(self.udtype, self.data)[0])
ditto.
scripts/analyze-migration.py class VMSDFieldInt(VMSDFieldGeneric): · line 314
Quoted 2 lines
scripts/analyze-migration.py +0
-0self.data = self.sdata return self.data
Regards,
Phil.
Quoted 8 lines · Philippe Mathieu-Daudé
Hi Joannah,
The common subject to use is [RFC PATCH] instead of [PATCH WIP]
Quoted 1 line · Joannah Nanjekye
This patch replaces the use of numpy with the standard Library struct module where possible.
This seems a good idea but why?
I suggest adding this to the commit description:
Users tend to hit an ImportError when running analyze-migration.py due to the numpy dependency. numpy functionality isn't actually used, just binary serialization that the standard library 'struct' module already provides. Removing the dependency allows the script to run out-of-the-box.
Quoted 7 lines
Do you have any performance improvements results to share?
Quoted 5 lines
Signed-off-by Joannah Nanjekye <[email protected]>---
scripts/analyze-migration.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)
I don't see a removal of "import numpy as np" so this script still depends on numpy. Did you forget to sent further patches that you have to remove the numpy dependency?
I will send an update tomorrow as am travelling rite now.
Thanks for the reminder.
Quoted 31 lines · Stefan Hajnoczi
Quoted 6 lines · Philippe Mathieu-Daudé
Hi Joannah,
The common subject to use is [RFC PATCH] instead of [PATCH WIP]
Quoted 1 line · Joannah Nanjekye
This patch replaces the use of numpy with the standard Library struct
module where possible.
Quoted 2 lines
This seems a good idea but why?
I suggest adding this to the commit description:
Users tend to hit an ImportError when running
analyze-migration.pydue to the numpy dependency. numpy functionality isn't actually used, just binary serialization that the standard library 'struct' module already provides. Removing the dependency allows the script to run out-of-the-box.Quoted 7 lines
Do you have any performance improvements results to share?
Quoted 5 lines
Signed-off-by Joannah Nanjekye <[email protected]>---
scripts/analyze-migration.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)I don't see a removal of "import numpy as np" so this script still depends on numpy. Did you forget to sent further patches that you have to remove the numpy dependency?
Quoted 12 lines · Stefan Hajnoczi
Quoted 9 lines · Philippe Mathieu-Daudé
Hi Joannah,
The common subject to use is [RFC PATCH] instead of [PATCH WIP]
Quoted 2 lines · Joannah Nanjekye
This patch replaces the use of numpy with the standard Library struct module where possible.
This seems a good idea but why?
Quoted 7 lines
I suggest adding this to the commit description:
Users tend to hit an ImportError when running
analyze-migration.pydue to the numpy dependency. numpy functionality isn't actually used, just binary serialization that the standard library 'struct' module already provides. Removing the dependency allows the script to run out-of-the-box.
Oh now I understand, good idea :)
Quoted 13 lines
Quoted 8 lines
Do you have any performance improvements results to share?
Quoted 5 lines
Signed-off-by Joannah Nanjekye <[email protected]>---
scripts/analyze-migration.py| 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)I don't see a removal of "import numpy as np" so this script still depends on numpy. Did you forget to sent further patches that you have to remove the numpy dependency?