text
stringlengths
0
828
efficient for reading in a file, making changes and writing it again.
""""""
if not hasattr(element_function, '__call__'):
return
file_path = getattr(file_or_path, 'name', file_or_path)
context = iter(iterparse(file_path, events=('start', 'end')))
root = None # Capture root for Memory management
# Start event loads child; by the End event it's ready for processing
for event, child in context:
if root is None:
root = child
if event == 'end': # Ensures the element has been fully read
element_function(child, **kwargs)
root.clear()"
4049,"def strip_namespaces(file_or_xml):
""""""
Removes all namespaces from the XML file or string passed in.
If file_or_xml is not a file or string, it is returned as is.
""""""
xml_content = _xml_content_to_string(file_or_xml)
if not isinstance(xml_content, string_types):
return xml_content
# This pattern can have overlapping matches, necessitating the loop
while _NAMESPACES_FROM_DEC_REGEX.search(xml_content) is not None:
xml_content = _NAMESPACES_FROM_DEC_REGEX.sub(r'\1', xml_content)
# Remove namespaces at the tag level
xml_content = _NAMESPACES_FROM_TAG_REGEX.sub(r'\1', xml_content)
# Remove namespaces at the attribute level
xml_content = _NAMESPACES_FROM_ATTR_REGEX.sub(r'\1\3', xml_content)
return xml_content"
4050,"def strip_xml_declaration(file_or_xml):
""""""
Removes XML declaration line from file or string passed in.
If file_or_xml is not a file or string, it is returned as is.
""""""
xml_content = _xml_content_to_string(file_or_xml)
if not isinstance(xml_content, string_types):
return xml_content
# For Python 2 compliance: replacement string must not specify unicode u''
return _XML_DECLARATION_REGEX.sub(r'', xml_content, 1)"
4051,"def write_element(elem_to_parse, file_or_path, encoding=DEFAULT_ENCODING):
""""""
Writes the contents of the parsed element to file_or_path
:see: get_element(parent_to_parse, element_path)
""""""
xml_header = '<?xml version=""1.0"" encoding=""{0}""?>'.format(encoding)
get_element_tree(elem_to_parse).write(file_or_path, encoding, xml_header)"
4052,"def floating_point_to_datetime(day, fp_time):
""""""Convert a floating point time to a datetime.""""""
result = datetime(year=day.year, month=day.month, day=day.day)
result += timedelta(minutes=math.ceil(60 * fp_time))
return result"
4053,"def adhan(day, location, parameters, timezone_offset=0):
""""""Calculate adhan times given the parameters.
This function will compute the adhan times for a certain location on
certain day. The method for calculating the prayers as well as the time for
Asr can also be specified. The timezone offset naively adds the specified
number of hours to each time that is returned.
:param day: The datetime.date to calculate for
:param location: 2-tuple of floating point coordiantes for latitude and
longitude of location in degrees
:param parameters: A dictionary-like object of parameters for computing
adhan times. Commonly used calculation methods are
available in the adhan.methods module
:param timezone_offset: The number of hours to add to each prayer time
to account for timezones. Can be floating point
""""""
latitude, longitude = location
#
# To reduce a little repetitiveness, using a partial function that has the
# day and latitude already set
#
time_at_sun_angle = partial(
compute_time_at_sun_angle,
day=day,
latitude=latitude
)
zuhr_time = compute_zuhr_utc(day, longitude)
shuruq_time = zuhr_time - time_at_sun_angle(angle=SUNRISE_ANGLE)
maghrib_time = zuhr_time + time_at_sun_angle(angle=SUNSET_ANGLE)