Python LMF library
 All Classes Namespaces Files Functions Variables
attr.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """! @package utils
4 """
5 
6 from utils.error_handling import Warning
7 from utils.io import ENCODING
8 
9 def check_attr_type(val, typ, msg):
10  """! @brief Check that attribute value is of specified type.
11  @param val The attribute value to check.
12  @param typ The allowed Python type(s): simple, or Python set or list.
13  @param msg The message to display if value is not of correct type.
14  """
15  # Python set or list of allowed types
16  if type(typ) is set or type(typ) is list:
17  if type(val) not in typ:
18  print Warning(msg)
19  # Simple allowed type
20  elif type(val) is not typ:
21  print Warning(msg)
22 
23 def check_attr_range(value, range, msg, mapping=None):
24  """! @brief Check that attribute value is in specified range.
25  @param value The attribute value to check.
26  @param range A Python set giving the range of allowed values.
27  @param msg The message to display if value is out-of-range.
28  @param mapping A Python dictionary giving mapping between values (i.e. from MDF to LMF)
29  @return The value to set, or None if out-of-range.
30  """
31  # Check value
32  if value not in range:
33  # Check converted value
34  if mapping is not None:
35  try:
36  converted_value = mapping[value]
37  except KeyError:
38  print Warning(msg)
39  else:
40  # Converted value to set
41  return converted_value
42  else:
43  print Warning(msg)
44  else:
45  # Value to set
46  return value
47 
49  """! @brief Verify that date format is composed as follows: YYYY-MM-DD (ISO 8601).
50  If not, display a Warning message.
51  @param date Date to check.
52  """
53  import re
54  if not re.match("^\d{4}-[01]\d-[0-3]\d$", date):
55  print Warning("Date must be formatted as follows: YYYY-MM-DD (given date is %s)" % date.encode(ENCODING))
56 
58  """! @brief Verify that time format is composed as follows: THH:MM:SS,MSMS (ISO 8601: 'T' for Time).
59  If not, display a Warning message.
60  @param time Time to check.
61  """
62  import re
63  if not re.match("^T[0-2]\d:[0-5]\d:[0-5]\d(\,\d+|)$", time):
64  print Warning("Time must be formatted as follows: THH:MM:SS,MSMS (given time is %s)" % time.encode(ENCODING))
65 
66 def check_duration_format(duration):
67  """! @brief Verify that duration format is composed as follows: PTxxHxxMxxS (ISO 8601: 'P' for Period).
68  If not, display a Warning message.
69  @param duration Duration to check.
70  """
71  import re
72  if not re.match("^PT[0-2]\dH[0-5]\dM[0-5]\dS$", duration):
73  print Warning("Duration must be formatted as follows: PTxxHxxMxxS (given duration is %s)" % duration.encode(ENCODING))
def check_attr_type
Check that attribute value is of specified type.
Definition: attr.py:9
def check_date_format
Verify that date format is composed as follows: YYYY-MM-DD (ISO 8601).
Definition: attr.py:48
def check_attr_range
Check that attribute value is in specified range.
Definition: attr.py:23
def check_time_format
Verify that time format is composed as follows: THH:MM:SS,MSMS (ISO 8601: 'T' for Time)...
Definition: attr.py:57
def check_duration_format
Verify that duration format is composed as follows: PTxxHxxMxxS (ISO 8601: 'P' for Period)...
Definition: attr.py:66