<commit_before>#!/usr/bin/env python
# -*- coding: latin-1 -*-
"""
Author: kmunve

Returns a list of available meteorological parameters from www.eklima.no
CSV files with English and Norwegian terms are created.
"""
from lxml import etree
from wsklima_requests import wsKlimaRequest


def hourly_params():
    # Make an eklima request
    wr = wsKlimaRequest('getElementsFromTimeserieType', {'timeserietypeID': 2}).get()
    
    # Parse XML string
    root = etree.fromstring(wr.content)
    
    # Temporary list of element codes
    elem_list = []

    # Open file for Norwegian version
    fid = open('eklima_param_list_no.csv', 'w')
    fid.write('#Gruppe,#Undergruppe,#Kode,#Enhet,#Nummer,#Beskrivelse\n')

    # Iterate over all "item" elements
    for element in root.iter("item"):
        fid.write("{0},{1},{2},{3},{4},{5}\n".format(element.find('elemGroup').text.encode('utf-8'), element.find('name').text.encode('utf-8'), element.find('elemCode').text.encode('utf-8'), element.find('unit').text.encode('utf-8'), element.find('elemNo').text.encode('utf-8'), element.find('description').text.encode('utf-8')))
            
        elem_list.append(element.find('elemCode').text)

    fid.close()
 
    wr =  wsKlimaRequest('getElementsProperties', {'language': 'en', 'elem_codes': elem_list}).get()

    # Parse XML string
    root = etree.fromstring(wr.content)

    # Print tree structure of file
    #print etree.tostring(root, pretty_print=True)

    # Open file for English version
    fid = open('eklima_param_list_en.csv', 'w')
    fid.write('#Group,#Subgroup,#Code,#Unit,#Number,#Description\n')

    # Iterate over all "item" elements
    for element in root.iter("item"):
        fid.write("{0},{1},{2},{3},{4},{5}\n".format(element.find('elemGroup').text.encode('utf-8'), element.find('name').text.encode('utf-8'), element.find('elemCode').text.encode('utf-8'), element.find('unit').text.encode('utf-8'), element.find('elemNo').text.encode('utf-8'), element.find('description').text.encode('utf-8')))

    fid.close()
    print '...files written'
    

if __name__ =="__main__":
    hourly_params()<commit_msg>Replaced for-loop by list comprehension in eklima_param_list.py.<commit_after>#!/usr/bin/env python
# -*- coding: latin-1 -*-
"""
Author: kmunve

Returns a list of available meteorological parameters from www.eklima.no
CSV files with English and Norwegian terms are created.
"""
from lxml import etree
from wsklima_requests import wsKlimaRequest


def hourly_params():
    # Make an eklima request using default Norwegian language
    wr = wsKlimaRequest('getElementsFromTimeserieType', {'timeserietypeID': 2}).get()
    
    # Parse XML string
    root = etree.fromstring(wr.content)

    # Open file for Norwegian version
    fid = open('eklima_param_list_no.csv', 'w')
    fid.write('#Gruppe,#Undergruppe,#Kode,#Enhet,#Nummer,#Beskrivelse\n')

    # Iterate over all "item" elements in the XML file
    [fid.write("{0},{1},{2},{3},{4},{5}\n".format(element.find('elemGroup').text.encode('utf-8'), element.find('name').text.encode('utf-8'), element.find('elemCode').text.encode('utf-8'), element.find('unit').text.encode('utf-8'), element.find('elemNo').text.encode('utf-8'), element.find('description').text.encode('utf-8'))) for element in root.iter("item")]
    # Temporary list of element codes
    elem_list = [element.find('elemCode').text for element in root.iter("item")]

    fid.close()

    # Make an eklima request for the property of the same elements using English language
    # Requires function getElementsProperties, which gives same output.
    wr = wsKlimaRequest('getElementsProperties', {'language': 'en', 'elem_codes': elem_list}).get()

    # Parse XML string
    root = etree.fromstring(wr.content)

    # Open file with English version
    fid = open('eklima_param_list_en.csv', 'w')
    fid.write('#Group,#Subgroup,#Code,#Unit,#Number,#Description\n')

    # Iterate over all "item" elements using list comprehension
    [fid.write("{0},{1},{2},{3},{4},{5}\n".format(element.find('elemGroup').text.encode('utf-8'), element.find('name').text.encode('utf-8'), element.find('elemCode').text.encode('utf-8'), element.find('unit').text.encode('utf-8'), element.find('elemNo').text.encode('utf-8'), element.find('description').text.encode('utf-8'))) for element in root.iter("item")]

    fid.close()
    print '...files written'


if __name__ =="__main__":
    hourly_params()
{'fraction_non_alphanumeric': 0.17828932520875648, 'fraction_numerical': 0.011735499887158655, 'mean_word_length': 4.533083645443196, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5132561', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1418, 'n_words': 293}