def handle_metadata(self, metadata): '''Store metadata in buffer to be written out the first time handle_data is called.''' if len(metadata) == 0: raise ArgumentError(u'Metadata must contain at least one element') metadata[0] = u'#' + make_utf8(metadata[0]) self.buffer.append(metadata)
def handle_special_command(self, command, command_args): if command == 'crash': from dysense.core.utility import make_utf8 raise Exception(make_utf8("Intentional crash for testing.")) if command == 'toggle_quality': self.data_quality_ok = not self.data_quality_ok if command == 'time_test': self.send_time_test_events = not self.send_time_test_events
def _run_process(self, relative_path, startup_args, language): startup_args[0] = make_utf8(startup_args[0]) # make sure sensor ID is a string. startup_args[1] = make_utf8(startup_args[1]) # make sure instrument ID is a string. startup_args[2] = json_dumps_unicode(startup_args[2]) # serialize sensor settings startup_args[3] = make_utf8(startup_args[3]) # make sure connect endpoint is encoded. absolute_path = os.path.join(os.getcwd(), relative_path) absolute_path = make_utf8(absolute_path) try: if language == 'python': # Need to include sys.executable when executing a python script. self.process = subprocess.Popen([sys.executable, absolute_path] + startup_args, shell=False) else: # Run general process startup. self.process = subprocess.Popen([absolute_path] + startup_args, shell=False) except WindowsError: print absolute_path raise self.process.wait()
def write(self, data): '''Write data to file or buffer it depending on class settings. Data is a list.''' if (data is None) or (len(data) == 0): # Create blank one element tuple so it's obvious in log that no data was received. raise Exception(u"Data can't be empty.") for i, val in enumerate(data): if type(data[i]) == float: # Convert all floats using built in representation function. This avoids loss of precision # due to the way floats are printed in python. val = repr(val) # Make sure all data is encoded as utf8. data[i] = make_utf8(val) # Check if all we need to do is buffer data. if self.buffer_size > 1: if len(self.buffer) < (self.buffer_size - 1): self.buffer.append(data) return # Make sure file is open so we can write to it. if self.file is None: self.file = open(self.file_path, 'wb') self.writer = csv.writer(self.file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) if len(self.buffer) > 0: # Write all the data we've been saving. self.writer.writerows(self.buffer) self.buffer = [] # Write current sample data. self.writer.writerow(data) # Make sure data gets written in case of power failure. self.file.flush()
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals from dysense.core.utility import make_unicode, make_utf8 u = '字字' u = make_utf8(u) #print u l = u'{}'.format(u) print l l = b'{}'.format(u) print l u = 'idzie wąż wąską dróżką'.encode('utf8') print type(u) u = str(u) print u uu = u.decode('utf8') print(uu) s = uu.encode('cp1250') print(s)