def strava2trainable(strava): trainable = {} trainable["strava_id"] = strava.id trainable["title"] = strava.name trainable["commute"] = strava.commute # The desciption seems not to be included in the response? # trainable["description"] = strava.description trainable["distance"] = float(strava.distance) trainable["duration"] = serialize(strava.moving_time) trainable["elevation_gain"] = float(strava.total_elevation_gain) trainable["sport"] = get_trainable_activity_type(strava) trainable["date"] = serialize(strava.start_date) trainable["heartrate"] = strava.average_heartrate return trainable
def build_changes(self, old_values, new_values): """Returns a dictionary with the old and new values for each field which has changed""" diff = {} for field in new_values: oldv = serialize(old_values.get(field)) newv = serialize(new_values.get(field)) if newv == oldv: continue if field == "data": diff[field] = {"old": oldv, "new": newv} else: diff[field] = {"old": serialize(oldv), "new": serialize(newv)} return diff
def perform(self, items): """Returns the serialized item as string :items: @todo :returns: @todo """ data = [] for item in items: # Ensure that every item has a UUID. Set missing UUID here # if the item has no uuid set yet. if not item.uuid: item.reset_uuid() if self._fields is None: # Default export. Export all fields excluding relations values = item.get_values(serialized=self._serialized, include_relations=self._relations) else: values = {} for field in self._fields: value = item.get_value(field) if self._serialized: value = serialize(value) values[field] = value data.append(self.flatten(values)) return self.serialize(data)
def get_values(self, include_relations=False, serialized=False): """Will return a dictionary with the values of the item. On default the function will return the pythonic values and excludes all related items. This behaviour can be changed by setting the `include_relations` and `serialized` option. In case the relations are included and the values are serialized then the serialized value of a related item is its id. :include_relations: Flag if relations should be included in the returned dictionary. :serialized: Flag if the values should be serialized. :returns: Dictionary with key value pairs. """ values = {} for field in get_columns_from_instance(self, include_relations): # Ignore private form fields if field.startswith("_"): continue if serialized: value = serialize(getattr(self, field)) else: value = getattr(self, field) values[field] = value return values
def serialize(self, data): output = StringIO.StringIO() book = xlsxwriter.Workbook(output) if len(data) > 0: keys = sorted(data[0].keys()) sheet = book.add_worksheet(self._clazz.__tablename__) row = 0 col = 0 # write header for key in keys: sheet.write(row, col, key) col += 1 # write data col = 0 row = 1 for item in data: for key in keys: value = serialize(item[key]) sheet.write(row, col, value) col += 1 row += 1 col = 0 book.close() output.seek(0) return output.read()
def test_serialize_datetime(): from ringo.lib.helpers import serialize from datetime import datetime dt = datetime(1977, 3, 12, 0, 0, 0) result = serialize(dt) assert result == "1977-03-12 00:00:00" assert type(result) == str
def write_row(series, data, meanvalue=0): row = [] row.append(str(serialize(getattr(data, self.diagram_data_time)))) for serie in series: if serie == self._build_series_label(data): value = getattr(data, self.diagram_data_value) - meanvalue row.append(str(value)) if self.errors_enabled(): row.append( str(getattr(data, self.diagram_data_error_value))) else: row.append("") if self.errors_enabled(): row.append("") return ",".join(row)
def perform(self, items): """Will export the given items. Depending if the Exporter has been initialised with the `serialized` parameter the export will return a list of dictionaries (each with the values) or in the exporter specific format (e.g. JSON). :items: Items which will be exported. :returns: Exported items. (Format Depends on the export configuration). """ data = [] # Check if the given item(s) is a list. If not we put it into a # temporary list. if not isinstance(items, list): _items = [items] else: _items = items for item in _items: # Ensure that every item has a UUID. Set missing UUID here # if the item has no uuid set yet. if not item.uuid: item.reset_uuid() # Check if a configuration is provided. # FIXME: Read support for deprecated "relations" argument? # Is missing here. (ti) <2017-05-23 14:02> if not self._config or len(self._config.config) == 0: # No configuration is provided. Export all fields # exluding relations. values = item.get_values(serialized=self._serialized) else: # Configuration is provided. Export fields and relations # based on the given configuration. values = {} if self._config.includes_wildcard(): fields = [p.key for p in get_props_from_instance(item)] fields.extend(self._config.get_relation_fields()) else: fields = self._config.config for field in fields: if isinstance(field, dict): for relation in field: clazz = getattr(self._clazz, relation).mapper.class_ exporter = Exporter(clazz, serialized=self._serialized, config=ExportConfiguration( field[relation])) value = item.get_value(relation) value = exporter.perform(value) values[relation] = value else: value = serialize(item.get_value(field)) values[field] = value data.append(self.flatten(values)) # If the input to the method was a single item we will return a # single exported item. if not isinstance(items, list): if len(data) > 0: data = data[0] else: data = None return self.serialize(data)
def test_serialize_list(): from ringo.lib.helpers import serialize result = serialize(["foo", "bar", "baz"]) assert result == "['foo', 'bar', 'baz']" assert type(result) == unicode
def test_serialize_int(): from ringo.lib.helpers import serialize result = serialize(23) assert result == "23" assert type(result) == unicode