From c4cd4e26ada8e488ee685cca6f71caaab3d91730 Mon Sep 17 00:00:00 2001
From: Quentin Codelupi <quentin.codelupi@cern.ch>
Date: Thu, 25 Jul 2019 10:51:13 +0200
Subject: [PATCH] [clean] some duplicates code

---
 .idea/.gitignore                             |  6 +++
 .idea/inspectionProfiles/Project_Default.xml | 10 +++++
 property-pull.py                             | 40 ++++----------------
 3 files changed, 24 insertions(+), 32 deletions(-)
 create mode 100644 .idea/.gitignore
 create mode 100644 .idea/inspectionProfiles/Project_Default.xml

diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..0ed6303
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,6 @@
+# Default ignored files
+/workspace.xml
+
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..c2d4450
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,10 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <Languages>
+        <language minSize="146" name="Python" />
+      </Languages>
+    </inspection_tool>
+  </profile>
+</component>
\ No newline at end of file
diff --git a/property-pull.py b/property-pull.py
index a488b95..dc3714b 100644
--- a/property-pull.py
+++ b/property-pull.py
@@ -47,6 +47,10 @@ sums_cols = udf(lambda arr: 0 if arr == [] else __builtins__.sum(arr), IntegerTy
 '''
 
 
+def write_file(_device, _start_time, _end_time, _property, data):
+    write_file(_device, _start_time, _end_time, _property, data)
+
+
 def uncompress(_data_array, _index_array, _size, _nrows):
     print('uncompress start')
     result = np.zeros(_size, dtype=np.int)
@@ -243,14 +247,7 @@ def pull_integral(_start_time, _end_time, _device):
     db_cursor = mydb.cursor()
     db_cursor.execute('UPDATE data SET integral = %s where id = %s', (json.dumps(data), data_id,))
 
-    if _files:
-        # Writing output #
-        with open(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-                  + _end_time.replace(' ', '_') + '_' + _property + '.dat', 'w') as outfile:
-            json.dump(data, outfile)
-
-        print(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-              + _end_time.replace(' ', '_') + '_' + _property + '.dat' + " Pulled")
+    write_file(_device, _start_time, _end_time, _property, data)
 
 
 # Not Working with too mny dat (dump memory)
@@ -323,14 +320,7 @@ def pull_raw_dist(_start_time, _end_time, _device):
     db_cursor = mydb.cursor()
     db_cursor.execute('UPDATE data SET raw_dist = %s where id = %s', (json.dumps(data), data_id,))
 
-    if _files:
-        # Writing output #
-        with open(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-                  + _end_time.replace(' ', '_') + '_' + _property + '.dat', 'w') as outfile:
-            json.dump(data, outfile)
-
-        print(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-              + _end_time.replace(' ', '_') + '_' + _property + '.dat' + " Pulled")
+    write_file(_device, _start_time, _end_time, _property, data)
 
 
 def pull_integral_dist(_start_time, _end_time, _device):
@@ -406,14 +396,7 @@ def pull_integral_dist(_start_time, _end_time, _device):
     db_cursor = mydb.cursor()
     db_cursor.execute('UPDATE data SET integral_dist = %s where id = %s', (json.dumps(data), data_id,))
 
-    if _files:
-        # Writing output #
-        with open(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-                  + _end_time.replace(' ', '_') + '_' + _property + '.dat', 'w') as outfile:
-            json.dump(data, outfile)
-
-        print(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-              + _end_time.replace(' ', '_') + '_' + _property + '.dat' + " Pulled")
+    write_file(_device, _start_time, _end_time, _property, data)
 
 
 def pull_turnloss(_start_time, _end_time, _device):
@@ -504,14 +487,7 @@ def pull_turnloss(_start_time, _end_time, _device):
     db_cursor = mydb.cursor()
     db_cursor.execute('UPDATE data SET turnloss = %s where id = %s', (json.dumps(data), data_id,))
 
-    if _files:
-        # Writing output #
-        with open(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-                  + _end_time.replace(' ', '_') + '_' + _property + '.dat', 'w') as outfile:
-            json.dump(data, outfile)
-
-        print(_dest_dir + '/' + _device + '_' + _start_time.replace(' ', '_') + 'to'
-              + _end_time.replace(' ', '_') + '_' + _property + '.dat' + " Pulled")
+    write_file(_device, _start_time, _end_time, _property, data)
 
 
 if __name__ == '__main__':
-- 
GitLab