<commit_before>import json
import os
import platform
import sublime
import sublime_plugin
import subprocess

# monkeypatch `Region` to be iterable
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

if platform.system() == 'Darwin':
	os_name = 'osx'
elif  platform.system() == 'Windows':
	os_name = 'windows'
else:
	os_name = 'linux'

BIN_PATH = os.path.join(
	sublime.packages_path(),
	os.path.dirname(os.path.realpath(__file__)),
	'babel-transform.js'
)

class BabelCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		selected_text = self.get_text()
		code = self.babelify(selected_text)

		if code:
			w = sublime.Window.new_file(self.view.window())
			w.settings().set('default_extension', 'js')
			w.set_syntax_file(self.view.settings().get('syntax'))
			w.set_scratch(True)
			w.insert(edit, 0, code)

	def babelify(self, data):
		try:
			return node_bridge(data, BIN_PATH, [json.dumps({
				# from sublime
				'filename': self.view.file_name(),
				'newline_at_eof': self.view.settings().get('ensure_newline_at_eof_on_save'),
				# from babel-sublime settings
				'debug': self.get_setting('debug'),
				'use_local_babel': self.get_setting('use_local_babel'),
				'node_modules': self.get_setting_by_os('node_modules'),
				'options': self.get_setting('options')
			})])
		except Exception as e:
			return str(e)

	def get_text(self):
		if not self.has_selection():
			region = sublime.Region(0, self.view.size())
			return self.view.substr(region)

		selected_text = ''
		for region in self.view.sel():
			selected_text = selected_text + self.view.substr(region) + '\n'
		return selected_text

	def has_selection(self):
		for sel in self.view.sel():
			start, end = sel
			if start != end:
				return True
		return False

	def get_setting(self, key):
		settings = self.view.settings().get('Babel')
		if settings is None:
			settings = sublime.load_settings('Babel.sublime-settings')
		return settings.get(key)

	def get_setting_by_os(self, key):
		setting = self.get_setting(key)
		if setting:
			return setting.get(os_name)

def node_bridge(data, bin, args=[]):
	env = None
	startupinfo = None
	if os_name == 'osx':
		# GUI apps in OS X doesn't contain .bashrc/.zshrc set paths
		env = os.environ.copy()
		env['PATH'] += ':/usr/local/bin'
	elif os_name == 'windows':
		startupinfo = subprocess.STARTUPINFO()
		startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
	try:
		p = subprocess.Popen(
			['node', bin] + args,
			stdout=subprocess.PIPE,
			stdin=subprocess.PIPE,
			stderr=subprocess.PIPE,
			env=env,
			startupinfo=startupinfo
		)
	except OSError:
		raise Exception('Error: Couldn\'t find "node" in "%s"' % env['PATH'])
	stdout, stderr = p.communicate(input=data.encode('utf-8'))
	stdout = stdout.decode('utf-8')
	stderr = stderr.decode('utf-8')
	if stderr:
		raise Exception('Error: %s' % stderr)
	else:
		return stdout
<commit_msg>Convert babel.py indentation to spaces<commit_after>import json
import os
import platform
import sublime
import sublime_plugin
import subprocess

# monkeypatch `Region` to be iterable
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()

if platform.system() == 'Darwin':
    os_name = 'osx'
elif  platform.system() == 'Windows':
    os_name = 'windows'
else:
    os_name = 'linux'

BIN_PATH = os.path.join(
    sublime.packages_path(),
    os.path.dirname(os.path.realpath(__file__)),
    'babel-transform.js'
)

class BabelCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        selected_text = self.get_text()
        code = self.babelify(selected_text)

        if code:
            w = sublime.Window.new_file(self.view.window())
            w.settings().set('default_extension', 'js')
            w.set_syntax_file(self.view.settings().get('syntax'))
            w.set_scratch(True)
            w.insert(edit, 0, code)

    def babelify(self, data):
        try:
            return node_bridge(data, BIN_PATH, [json.dumps({
                # from sublime
                'filename': self.view.file_name(),
                'newline_at_eof': self.view.settings().get('ensure_newline_at_eof_on_save'),
                # from babel-sublime settings
                'debug': self.get_setting('debug'),
                'use_local_babel': self.get_setting('use_local_babel'),
                'node_modules': self.get_setting_by_os('node_modules'),
                'options': self.get_setting('options')
            })])
        except Exception as e:
            return str(e)

    def get_text(self):
        if not self.has_selection():
            region = sublime.Region(0, self.view.size())
            return self.view.substr(region)

        selected_text = ''
        for region in self.view.sel():
            selected_text = selected_text + self.view.substr(region) + '\n'
        return selected_text

    def has_selection(self):
        for sel in self.view.sel():
            start, end = sel
            if start != end:
                return True
        return False

    def get_setting(self, key):
        settings = self.view.settings().get('Babel')
        if settings is None:
            settings = sublime.load_settings('Babel.sublime-settings')
        return settings.get(key)

    def get_setting_by_os(self, key):
        setting = self.get_setting(key)
        if setting:
            return setting.get(os_name)

def node_bridge(data, bin, args=[]):
    env = None
    startupinfo = None
    if os_name == 'osx':
        # GUI apps in OS X doesn't contain .bashrc/.zshrc set paths
        env = os.environ.copy()
        env['PATH'] += ':/usr/local/bin'
    elif os_name == 'windows':
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    try:
        p = subprocess.Popen(
            ['node', bin] + args,
            stdout=subprocess.PIPE,
            stdin=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=env,
            startupinfo=startupinfo
        )
    except OSError:
        raise Exception('Error: Couldn\'t find "node" in "%s"' % env['PATH'])
    stdout, stderr = p.communicate(input=data.encode('utf-8'))
    stdout = stdout.decode('utf-8')
    stderr = stderr.decode('utf-8')
    if stderr:
        raise Exception('Error: %s' % stderr)
    else:
        return stdout
<|endoftext|>


<commit_before>"""
Codes for estimating various parameters related to Hydrometeors.

@title: hydrometeors
@author: Valentin Louf <valentin.louf@monash.edu>
@institutions: Monash University and the Australian Bureau of Meteorology
@creation: 04/04/2017
@date: 20/11/2017

.. autosummary::
    :toctree: generated/

    dsd_retrieval
    hydrometeor_classification
    liquid_ice_mass
    merhala_class_convstrat
    rainfall_rate
"""
# Other Libraries
import pyart
import numpy as np

from csu_radartools import csu_liquid_ice_mass, csu_fhc, csu_blended_rain, csu_dsd

def dsd_retrieval(radar, gatefilter, kdp_name, zdr_name, refl_name='DBZ_CORR'):
    """
    Compute the DSD retrieval using the csu library.

    Parameters:
    ===========
        radar:
            Py-ART radar structure.
        refl_name: str
            Reflectivity field name.
        zdr_name: str
            ZDR field name.
        kdp_name: str
            KDP field name.

    Returns:
    ========
        nw_dict: dict
            Normalized Intercept Parameter.
        d0_dict: dict
            Median Volume Diameter.
    """
    dbz = radar.fields[refl_name]['data'].copy().filled(np.NaN)
    zdr = radar.fields[zdr_name]['data'].copy()
    try:
        kdp = radar.fields[kdp_name]['data'].copy().filled(np.NaN)
    except AttributeError:
        kdp = radar.fields[kdp_name]['data'].copy()

    d0, Nw, mu = csu_dsd.calc_dsd(dz=dbz, zdr=zdr, kdp=kdp, band='C')

    Nw = np.log10(Nw)
    Nw[gatefilter.gate_excluded] = np.NaN
    Nw = np.ma.masked_invalid(Nw).astype(np.float32)
    np.ma.set_fill_value(Nw, np.NaN)

    d0[gatefilter.gate_excluded] = np.NaN
    d0 = np.ma.masked_invalid(d0).astype(np.float32)
    np.ma.set_fill_value(d0, np.NaN)

    nw_dict = {'data': Nw,
               'units': 'AU', 'long_name': 'Normalized Intercept Parameter',
               'standard_name': 'Normalized Intercept Parameter',
               '_FillValue': np.NaN,
               '_Least_significant_digit': 2,
               'description': "Log10 of the NW. Retrieval based on Bringi et al. (2009)."}

    d0_dict = {'data': d0,
               'units': 'mm', 'long_name': 'Median Volume Diameter',
               'standard_name': 'Median Volume Diameter',
               '_FillValue': np.NaN,
               '_Least_significant_digit': 2,
               'description': "D0 retrieval based on Bringi et al. (2009)."}

    return nw_dict, d0_dict

def hydrometeor_classification(radar, gatefilter, kdp_name, zdr_name, refl_name='DBZ_CORR',
                               rhohv_name='RHOHV_CORR',
                               temperature_name='temperature',
                               height_name='height'):
    """
    Compute hydrometeo classification.

    Parameters:
    ===========
        radar:
            Py-ART radar structure.
        refl_name: str
            Reflectivity field name.
        zdr_name: str
            ZDR field name.
        kdp_name: str
            KDP field name.
        rhohv_name: str
            RHOHV field name.
        temperature_name: str
            Sounding temperature field name.
        height: str
            Gate height field name.

    Returns:
    ========
        hydro_meta: dict
            Hydrometeor classification.
    """
    refl = radar.fields[refl_name]['data'].copy().filled(np.NaN)
    zdr = radar.fields[zdr_name]['data'].copy().filled(np.NaN)
    try:
        kdp = radar.fields[kdp_name]['data'].copy().filled(np.NaN)
    except AttributeError:
        kdp = radar.fields[kdp_name]['data'].copy()
    rhohv = radar.fields[rhohv_name]['data']
    try:
        radar_T = radar.fields[temperature_name]['data']
        use_temperature = True
    except Exception:
        use_temperature = False

    if use_temperature:
        scores = csu_fhc.csu_fhc_summer(dz=refl, zdr=zdr, rho=rhohv, kdp=kdp, use_temp=True, band='C', T=radar_T)
    else:
        scores = csu_fhc.csu_fhc_summer(dz=refl, zdr=zdr, rho=rhohv, kdp=kdp, use_temp=False, band='C')

    hydro = np.argmax(scores, axis=0) + 1
    hydro[gatefilter.gate_excluded] = 0
    hydro_data = np.ma.masked_equal(hydro.astype(np.int16), 0)

    the_comments = "1: Drizzle; 2: Rain; 3: Ice Crystals; 4: Aggregates; " +\
                   "5: Wet Snow; 6: Vertical Ice; 7: LD Graupel; 8: HD Graupel; 9: Hail; 10: Big Drops"

    hydro_meta = {'data': hydro_data, 'units': ' ', 'long_name': 'Hydrometeor classification', '_FillValue': np.int16(0),
                  'standard_name': 'Hydrometeor_ID', 'comments': the_comments}

    return hydro_meta

def merhala_class_convstrat(radar, dbz_name="DBZ_CORR", rain_name="radar_estimated_rain_rate",
                            d0_name="D0", nw_name="NW"):
    """
    Merhala Thurai's has a criteria for classifying rain either Stratiform
    Convective or Mixed, based on the D-Zero value and the log10(Nw) value.
    Merhala's rain classification is 1 for Stratiform, 2 for Convective and 3
    for Mixed, 0 if no rain.

    """
    # Extracting data.
    d0 = radar.fields[d0_name]['data']
    nw = radar.fields[nw_name]['data']
    rainrate = radar.fields[rain_name]['data']
    dbz = radar.fields[dbz_name]['data']

    classification = np.zeros_like(dbz, dtype=int)

    # Invalid data
    pos0 = (d0 >= -5) & (d0 <= 100)
    pos1 = (nw >= -10) & (nw <= 100)

    # Classification index.
    indexa = nw - 6.4 + 1.7 * d0

    # Classifying
    classification[(indexa > 0.1) & (dbz > 20)] = 2
    classification[(indexa > 0.1) & (dbz <= 20)] = 1
    classification[indexa < -0.1] = 1
    classification[(indexa >= -0.1) & (indexa <= 0.1)] = 3

    # Masking invalid data.
    # classification = np.ma.masked_where(~pos0 | ~pos1 | dbz.mask, classification)

    # Generate metada.
    class_meta = {'data': classification,
                  'standard_name': 'echo_classification',
                  'long_name': 'Merhala Thurai echo classification',
                  'valid_min': 0,
                  'valid_max': 3,
                  'comment_1': 'Convective-stratiform echo classification based on Merhala Thurai',
                  'comment_2': '0 = Undefined, 1 = Stratiform, 2 = Convective, 3 = Mixed'}

    return class_meta

def rainfall_rate(radar, gatefilter, kdp_name, zdr_name, refl_name='DBZ_CORR',
                  hydro_name='radar_echo_classification', temperature_name='temperature'):
    """
    Rainfall rate algorithm from csu_radartools.

    Parameters:
    ===========
        radar:
            Py-ART radar structure.
        refl_name: str
            Reflectivity field name.
        zdr_name: str
            ZDR field name.
        kdp_name: str
            KDP field name.
        hydro_name: str
            Hydrometeor classification field name.

    Returns:
    ========
        rainrate: dict
            Rainfall rate.
    """
    dbz = radar.fields[refl_name]['data'].filled(np.NaN)
    zdr = radar.fields[zdr_name]['data'].filled(np.NaN)
    fhc = radar.fields[hydro_name]['data']
    try:
        kdp = radar.fields[kdp_name]['data'].filled(np.NaN)
    except AttributeError:
        kdp = radar.fields[kdp_name]['data']

    rain, _ = csu_blended_rain.calc_blended_rain_tropical(dz=dbz, zdr=zdr, kdp=kdp, fhc=fhc, band='C')

    rain[(gatefilter.gate_excluded) | np.isnan(rain) | (rain < 0)] = 0

    try:
        temp = radar.fields[temperature_name]['data']
        rain[temp < 0] = 0
    except Exception:
        pass

    rainrate = {"long_name": 'Blended Rainfall Rate',
                "units": "mm h-1",
                "standard_name": "rainfall_rate",
                '_Least_significant_digit': 2,
                '_FillValue': np.NaN,
                "description": "Rainfall rate algorithm based on Thompson et al. 2016.",
                "data": rain.astype(np.float32)}

    return rainrate
<commit_msg>Update metadata for hydrometeors retrievals<commit_after>"""
Codes for estimating various parameters related to Hydrometeors.

@title: hydrometeors
@author: Valentin Louf <valentin.louf@monash.edu>
@institutions: Monash University and the Australian Bureau of Meteorology
@creation: 04/04/2017
@date: 20/11/2017

.. autosummary::
    :toctree: generated/

    dsd_retrieval
    hydrometeor_classification
    liquid_ice_mass
    merhala_class_convstrat
    rainfall_rate
"""
# Other Libraries
import pyart
import numpy as np

from csu_radartools import csu_liquid_ice_mass, csu_fhc, csu_blended_rain, csu_dsd

def dsd_retrieval(radar, gatefilter, kdp_name, zdr_name, refl_name='DBZ_CORR'):
    """
    Compute the DSD retrieval using the csu library.

    Parameters:
    ===========
        radar:
            Py-ART radar structure.
        refl_name: str
            Reflectivity field name.
        zdr_name: str
            ZDR field name.
        kdp_name: str
            KDP field name.

    Returns:
    ========
        nw_dict: dict
            Normalized Intercept Parameter.
        d0_dict: dict
            Median Volume Diameter.
    """
    dbz = radar.fields[refl_name]['data'].copy().filled(np.NaN)
    zdr = radar.fields[zdr_name]['data'].copy()
    try:
        kdp = radar.fields[kdp_name]['data'].copy().filled(np.NaN)
    except AttributeError:
        kdp = radar.fields[kdp_name]['data'].copy()

    d0, Nw, mu = csu_dsd.calc_dsd(dz=dbz, zdr=zdr, kdp=kdp, band='C')

    Nw = np.log10(Nw)
    Nw[gatefilter.gate_excluded] = np.NaN
    Nw = np.ma.masked_invalid(Nw).astype(np.float32)
    np.ma.set_fill_value(Nw, np.NaN)

    d0[gatefilter.gate_excluded] = np.NaN
    d0 = np.ma.masked_invalid(d0).astype(np.float32)
    np.ma.set_fill_value(d0, np.NaN)

    nw_dict = {'data': Nw,
               'long_name': 'normalized_intercept_parameter',
               '_FillValue': np.NaN,
               '_Least_significant_digit': 2,
               'reference': "doi:10.1175/2009JTECHA1258.1"}

    d0_dict = {'data': d0,
               'units': 'mm',
               'long_name': 'median_volume_diameter',
               '_FillValue': np.NaN,
               '_Least_significant_digit': 2,
               'reference': "doi:10.1175/2009JTECHA1258.1"}

    return nw_dict, d0_dict

def hydrometeor_classification(radar, gatefilter, kdp_name, zdr_name, refl_name='DBZ_CORR',
                               rhohv_name='RHOHV_CORR',
                               temperature_name='temperature',
                               height_name='height'):
    """
    Compute hydrometeo classification.

    Parameters:
    ===========
        radar:
            Py-ART radar structure.
        refl_name: str
            Reflectivity field name.
        zdr_name: str
            ZDR field name.
        kdp_name: str
            KDP field name.
        rhohv_name: str
            RHOHV field name.
        temperature_name: str
            Sounding temperature field name.
        height: str
            Gate height field name.

    Returns:
    ========
        hydro_meta: dict
            Hydrometeor classification.
    """
    refl = radar.fields[refl_name]['data'].copy().filled(np.NaN)
    zdr = radar.fields[zdr_name]['data'].copy().filled(np.NaN)
    try:
        kdp = radar.fields[kdp_name]['data'].copy().filled(np.NaN)
    except AttributeError:
        kdp = radar.fields[kdp_name]['data'].copy()
    rhohv = radar.fields[rhohv_name]['data']
    try:
        radar_T = radar.fields[temperature_name]['data']
        use_temperature = True
    except Exception:
        use_temperature = False

    if use_temperature:
        scores = csu_fhc.csu_fhc_summer(dz=refl, zdr=zdr, rho=rhohv, kdp=kdp, use_temp=True, band='C', T=radar_T)
    else:
        scores = csu_fhc.csu_fhc_summer(dz=refl, zdr=zdr, rho=rhohv, kdp=kdp, use_temp=False, band='C')

    hydro = np.argmax(scores, axis=0) + 1
    hydro[gatefilter.gate_excluded] = 0
    hydro_data = np.ma.masked_equal(hydro.astype(np.int16), 0)

    the_comments = "1: Drizzle; 2: Rain; 3: Ice Crystals; 4: Aggregates; " +\
                   "5: Wet Snow; 6: Vertical Ice; 7: LD Graupel; 8: HD Graupel; 9: Hail; 10: Big Drops"

    hydro_meta = {'data': hydro_data, 'units': ' ', 'long_name': 'Hydrometeor classification', '_FillValue': np.int16(0),
                  'standard_name': 'Hydrometeor_ID', 'comments': the_comments}

    return hydro_meta

def merhala_class_convstrat(radar, dbz_name="DBZ_CORR", rain_name="radar_estimated_rain_rate",
                            d0_name="D0", nw_name="NW"):
    """
    Merhala Thurai's has a criteria for classifying rain either Stratiform
    Convective or Mixed, based on the D-Zero value and the log10(Nw) value.
    Merhala's rain classification is 1 for Stratiform, 2 for Convective and 3
    for Mixed, 0 if no rain.
    """
    # Extracting data.
    d0 = radar.fields[d0_name]['data']
    nw = radar.fields[nw_name]['data']
    rainrate = radar.fields[rain_name]['data']
    dbz = radar.fields[dbz_name]['data']

    classification = np.zeros_like(dbz, dtype=int)

    # Invalid data
    pos0 = (d0 >= -5) & (d0 <= 100)
    pos1 = (nw >= -10) & (nw <= 100)

    # Classification index.
    indexa = nw - 6.4 + 1.7 * d0

    # Classifying
    classification[(indexa > 0.1) & (dbz > 20)] = 2
    classification[(indexa > 0.1) & (dbz <= 20)] = 1
    classification[indexa < -0.1] = 1
    classification[(indexa >= -0.1) & (indexa <= 0.1)] = 3

    # Masking invalid data.
    classification = np.ma.masked_where(~pos0 | ~pos1 | dbz.mask, classification)

    # Generate metada.
    class_meta = {'data': classification,
                  'long_name': 'thurai_echo_classification',
                  'valid_min': 0,
                  'valid_max': 3,
                  'comment_1': 'Convective-stratiform echo classification based on Merhala Thurai',
                  'comment_2': '0 = Undefined, 1 = Stratiform, 2 = Convective, 3 = Mixed'}

    return class_meta

def rainfall_rate(radar, gatefilter, kdp_name, zdr_name, refl_name='DBZ_CORR',
                  hydro_name='radar_echo_classification', temperature_name='temperature'):
    """
    Rainfall rate algorithm from csu_radartools.

    Parameters:
    ===========
        radar:
            Py-ART radar structure.
        refl_name: str
            Reflectivity field name.
        zdr_name: str
            ZDR field name.
        kdp_name: str
            KDP field name.
        hydro_name: str
            Hydrometeor classification field name.

    Returns:
    ========
        rainrate: dict
            Rainfall rate.
    """
    dbz = radar.fields[refl_name]['data'].filled(np.NaN)
    zdr = radar.fields[zdr_name]['data'].filled(np.NaN)
    fhc = radar.fields[hydro_name]['data']
    try:
        kdp = radar.fields[kdp_name]['data'].filled(np.NaN)
    except AttributeError:
        kdp = radar.fields[kdp_name]['data']

    rain, _ = csu_blended_rain.calc_blended_rain_tropical(dz=dbz, zdr=zdr, kdp=kdp, fhc=fhc, band='C')

    rain[(gatefilter.gate_excluded) | np.isnan(rain) | (rain < 0)] = 0

    try:
        temp = radar.fields[temperature_name]['data']
        rain[temp < 0] = 0
    except Exception:
        pass

    rainrate = {"long_name": 'Blended Rainfall Rate',
                "units": "mm h-1",
                "standard_name": "rainfall_rate",
                '_Least_significant_digit': 2,
                '_FillValue': np.NaN,
                "description": "Rainfall rate algorithm based on Thompson et al. 2016.",
                "data": rain.astype(np.float32)}

    return rainrate
<|endoftext|>


<commit_before>import os
import sys
import time
import uuid

from redis import Redis
from redis import StrictRedis
from rq import Queue
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

class GZHandler(PatternMatchingEventHandler):
    """
    Handles when an event on the directory being watched happens that matches
    the values in patterns
    """

    patterns = ["*"]
    # want to ignore certain pcap files from splitter as they contain junk
    ignore_patters = ["*-miscellaneous*"]
    # don't want to process files in on_modified for files that have already
    # been created and processed
    created_files = set()
    try:
        # let jobs run for up to one day
        q = Queue(connection=Redis(host='redis'), default_timeout=86400)
        r = StrictRedis(host='redis', port=6379, db=0)
    except Exception as e:  # pragma: no cover
        print("Unable to connect to redis:", str(e))

    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        uid = str(uuid.uuid4())
        hostname = os.environ.get("VENT_HOST")
        if not hostname:
            hostname = ""

        try:
            # TODO should directories be treated as bulk paths to send to a
            #      plugin?
            if not event.is_directory:
                # wait for long copies to finish
                historicalSize = -1
                while (historicalSize != os.path.getsize(event.src_path)):
                    historicalSize = os.path.getsize(event.src_path)
                    time.sleep(0.1)

                # check if the file was already queued and ignore
                exists = False
                print(uid+" started " + event.src_path)
                jobs = self.r.keys(pattern="rq:job*")
                for job in jobs:
                    print(uid + " ***")
                    description = self.r.hget(job, 'description')
                    print(uid + " " + description)
                    if description.startswith("watch.file_queue('"):
                        print(uid + " " +
                              description.split("watch.file_queue('" +
                                                hostname + "_")[1][:-2])
                        print(uid + " " + event.src_path)
                        if description.split("watch.file_queue('" +
                                             hostname +
                                             "_")[1][:-2] == event.src_path:
                            print(uid + " true")
                            exists = True
                    elif description.startswith("watch.gpu_queue('"):
                        print(uid + " " +
                              description.split('"file": "')[1].split('"')[0])
                        print(uid + " " + event.src_path)
                        if description.split('"file": "')[1].split('"')[0] == event.src_path:
                            print(uid + " true")
                            exists = True
                    print(uid + " ***")

                if not exists:
                    # !! TODO this should be a configuration option in the
                    #         vent.template
                    print(uid + " let's queue it " + event.src_path)
                    # let jobs be queued for up to 30 days
                    self.q.enqueue('watch.file_queue',
                                   hostname + "_" + event.src_path,
                                   ttl=2592000)
                print(uid + " end " + event.src_path)
        except Exception as e:  # pragma: no cover
            print("file drop error: " + str(e))

    def on_created(self, event):
        self.created_files.add(event.src_path)
        self.process(event)

    def on_modified(self, event):
        # don't perform any action if file was already created or file is
        # deleted
        if (event.src_path not in self.created_files and
                os.path.exists(event.src_path)):
            # add to created files because the file was moved into directory,
            # which is what should be creating it, but some OS's treat it as a
            # modification with docker mounts
            self.created_files.add(event.src_path)
            self.process(event)

if __name__ == '__main__':  # pragma: no cover
    args = None
    if len(sys.argv) > 1:
        args = sys.argv[1:]

    observer = Observer()
    observer.schedule(GZHandler(), path=args[0] if args else '/files',
                      recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:  # pragma: no cover
        observer.stop()

    observer.join()
<commit_msg>copy path to local variable<commit_after>import os
import sys
import time
import uuid

from redis import Redis
from redis import StrictRedis
from rq import Queue
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

class GZHandler(PatternMatchingEventHandler):
    """
    Handles when an event on the directory being watched happens that matches
    the values in patterns
    """

    patterns = ["*"]
    # want to ignore certain pcap files from splitter as they contain junk
    ignore_patters = ["*-miscellaneous*"]
    # don't want to process files in on_modified for files that have already
    # been created and processed
    created_files = set()
    try:
        # let jobs run for up to one day
        q = Queue(connection=Redis(host='redis'), default_timeout=86400)
        r = StrictRedis(host='redis', port=6379, db=0)
    except Exception as e:  # pragma: no cover
        print("Unable to connect to redis:", str(e))

    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        uid = str(uuid.uuid4())
        hostname = os.environ.get("VENT_HOST")
        if not hostname:
            hostname = ""

        try:
            # TODO should directories be treated as bulk paths to send to a
            #      plugin?
            if not event.is_directory:
                spath = event.src_path
                # wait for long copies to finish
                historicalSize = -1
                while (historicalSize != os.path.getsize(spath)):
                    historicalSize = os.path.getsize(spath)
                    time.sleep(0.1)

                # check if the file was already queued and ignore
                exists = False
                print(uid+" started " + spath)
                jobs = self.r.keys(pattern="rq:job*")
                for job in jobs:
                    print(uid + " ***")
                    description = self.r.hget(job, 'description')
                    print(uid + " " + description)
                    if description.startswith("watch.file_queue('"):
                        print(uid + " " +
                              description.split("watch.file_queue('" +
                                                hostname + "_")[1][:-2])
                        print(uid + " " + spath)
                        if description.split("watch.file_queue('" +
                                             hostname +
                                             "_")[1][:-2] == spath:
                            print(uid + " true")
                            exists = True
                    elif description.startswith("watch.gpu_queue('"):
                        print(uid + " " +
                              description.split('"file": "')[1].split('"')[0])
                        print(uid + " " + spath)
                        if description.split('"file": "')[1].split('"')[0] == spath:
                            print(uid + " true")
                            exists = True
                    print(uid + " ***")

                if not exists:
                    # !! TODO this should be a configuration option in the
                    #         vent.template
                    print(uid + " let's queue it " + spath)
                    # let jobs be queued for up to 30 days
                    self.q.enqueue('watch.file_queue',
                                   hostname + "_" + spath,
                                   ttl=2592000)
                print(uid + " end " + spath)
        except Exception as e:  # pragma: no cover
            print("file drop error: " + str(e))

    def on_created(self, event):
        self.created_files.add(event.src_path)
        self.process(event)

    def on_modified(self, event):
        # don't perform any action if file was already created or file is
        # deleted
        if (event.src_path not in self.created_files and
                os.path.exists(event.src_path)):
            # add to created files because the file was moved into directory,
            # which is what should be creating it, but some OS's treat it as a
            # modification with docker mounts
            self.created_files.add(event.src_path)
            self.process(event)

if __name__ == '__main__':  # pragma: no cover
    args = None
    if len(sys.argv) > 1:
        args = sys.argv[1:]

    observer = Observer()
    observer.schedule(GZHandler(), path=args[0] if args else '/files',
                      recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:  # pragma: no cover
        observer.stop()

    observer.join()
<|endoftext|>


<commit_before>####################################################################
# This is a python program that allows you to verify your ballot   #
# using the RSA algorithm. It requires the Pyhton interpreter from #
#   http://python.org                                              #
# it also requires the RSA module which you can install with       #
#   pip install rsa                                                #
# Save this program with a name like "verify.py" and run it        #
# The output of this program "valid" or "invalid"                  #
####################################################################

# import required libraries
import base64, rsa # install module with "pip install rsa".

# this is the ballot to verify
ballot = """
{{=XML(ballot.ballot_content)}}
""".strip()

# this is the ballot RSA signature
signature = base64.b16decode("{{=ballot.signature.split('-')[1]}}")

# this is the election public key
pk_pem = """
{{=election.public_key.strip()}}
"""

# this is the code that verifies the signature
public_key = rsa.PublicKey.load_pkcs1(pk_pem)
if rsa.verify(ballot, signature, public_key)==None:
    print 'valid'
else:
    print 'invalid'
<commit_msg>fixed typo spelling, thanks David<commit_after>####################################################################
# This is a python program that allows you to verify your ballot   #
# using the RSA algorithm. It requires the Python interpreter from #
#   http://python.org                                              #
# it also requires the RSA module which you can install with       #
#   pip install rsa                                                #
# Save this program with a name like "verify.py" and run it        #
# The output of this program "valid" or "invalid"                  #
####################################################################

# import required libraries
import base64, rsa # install module with "pip install rsa".

# this is the ballot to verify
ballot = """
{{=XML(ballot.ballot_content)}}
""".strip()

# this is the ballot RSA signature
signature = base64.b16decode("{{=ballot.signature.split('-')[1]}}")

# this is the election public key
pk_pem = """
{{=election.public_key.strip()}}
"""

# this is the code that verifies the signature
public_key = rsa.PublicKey.load_pkcs1(pk_pem)
if rsa.verify(ballot, signature, public_key)==None:
    print 'valid'
else:
    print 'invalid'
<|endoftext|>


<commit_before>from waitlist.utility.swagger import api, header_to_datetime
from waitlist.utility.swagger.eve import get_esi_client, ESIResponse,\
    get_expire_time
from waitlist.utility.swagger.eve.fleet.responses import EveFleet, EveFleetWings,\
    WingCreated, EveFleetMembers, SquadCreated
from waitlist.utility.swagger.eve.fleet.models import EveFleetWing,\
    EveFleetSquad, FleetSettings

class EveFleetEndpoint(object):
    def __init__(self, fleetID):
        # type: (int) -> None
        self.__fleetID = fleetID

    def get_member(self):
        # type: (int) -> dict(str, Any)
        client = get_esi_client()
        response = client.request(api.op['get_fleets_fleet_id_members'](fleet_id=self.__fleetID))
        if response.status_code == 200:
            return EveFleetMembers(get_expire_time(response), response.status_code, None, response.data)
        return EveFleetMembers(get_expire_time(response), response.status_code, response.data['error'])

    def get_fleet_settings(self):
        # type: () -> EveFleet
        '''
        Get fleet information
        get_fleets_fleet_id_ok {
            is_free_move (boolean):

            Is free-move enabled ,
            is_registered (boolean):

            Does the fleet have an active fleet advertisement ,
            is_voice_enabled (boolean):

            Is EVE Voice enabled ,
            motd (string):

            Fleet MOTD in CCP flavoured HTML
        }
        '''
        client = get_esi_client()
        response = client.request(api.op['get_fleets_fleet_id'](fleet_id=self.__fleetID))
        if (response.status_code == 200):
            return EveFleet(get_expire_time(response), response.status_code, None,
                             response.data['is_free_move'], response.data['is_registered'],
                             response.data['is_voice_enabled'], response.data['motd'])
        return EveFleet(get_expire_time(response), response.status_code, response.data['error'],
                        None, None,
                        None, None
                        )

    def set_fleet_settings(self, is_free_move, motd):
        # type: (boolean, str) -> ESIResponse
        settings = FleetSettings(is_free_move, motd)
        client = get_esi_client()

        response = client.request(api.op['put_fleets_fleet_id_new_settings'](fleet_id=self.__fleetID, new_settings=settings.get_esi_data()))
        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)
        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])

    def get_wings(self):
        # type: () -> EveFleetWings
        client = get_esi_client()
        response = client.request(api.op['get_fleets_fleet_id_wings'](fleet_id=self.__fleetID))
        if response.status_code == 200:
            wings = []
            for wing in response.data:
                wingID = wing['id']
                wingName = wing['name']
                squads = []
                for squad in wing['squads']:
                    squad = EveFleetSquad(squad['id'], squad['name'])
                    squads.append(squad)
                fleetWing = EveFleetWing(wingID, wingName, squads)
                wings.append(fleetWing)

            return EveFleetWings(get_expire_time(response), response.status_code, None, wings)
        return EveFleetWings(get_expire_time(response), response.status_code, response.data['error'], None)

    def create_wing(self):
        # type: () -> WingCreated
        client = get_esi_client()
        response = client.request(api.op['post_fleets_fleet_id_wings'](fleet_id=self.__fleetID))
        if response.status_code == 201:
            return WingCreated(get_expire_time(response), response.status_code, None,
                               response.data['wing_id']
                               )
        return WingCreated(get_expire_time(response), response.status_code, response.data['error'])

    def set_wing_name(self, wingID, name):
        # type: (int, str) -> ESIResponse
        client = get_esi_client()
        data = {'name': name}
        response = client.request(api.op['put_fleets_fleet_id_wings_wing_id'](fleet_id=self.__fleetID, wing_id=wingID, naming=data))

        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)

        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])

    def create_squad(self, wingID):
        # type: (int) -> SquadCreated
        client = get_esi_client()
        response = client.request(api.op['post_fleets_fleet_id_wings_wing_id_squads'](fleet_id=self.__fleetID, wing_id=wingID))
        if response.status_code == 201:
            return SquadCreated(get_expire_time(response), response.status_code, None,
                                wingID, response.data['squad_id']
                                )
        return SquadCreated(get_expire_time(response), response.status_code, None, None, None)

    def set_squad_name(self, squadID, name):
        # type: (int, str) -> ESIResponse
        client = get_esi_client()
        response = client.request(api.op['put_fleets_fleet_id_squads_squad_id'](fleet_id=self.__fleetID, squad_id=squadID))
        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)
        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])

    def invite(self, characterID, role, squadID, wingID):
        # type: (int, str, int, int) -> dict(str, Any)
        '''
        'fleet_commander', 'wing_commander', 'squad_commander', 'squad_member'
        '''
        client = get_esi_client()
        invite = {};
        invite['character_id'] = characterID
        invite['role'] = role
        if squadID is not None:
            invite['squad_id'] = squadID
        if wingID is not None:
            invite['wing_id'] = wingID
        response = client.request(api.op['post_fleets_fleet_id_members_invitation'](fleet_id=self.__fleetID, invitation=invite))
        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)
        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])
<commit_msg>fixed some missing parameters<commit_after>from waitlist.utility.swagger import api, header_to_datetime
from waitlist.utility.swagger.eve import get_esi_client, ESIResponse,\
    get_expire_time
from waitlist.utility.swagger.eve.fleet.responses import EveFleet, EveFleetWings,\
    WingCreated, EveFleetMembers, SquadCreated
from waitlist.utility.swagger.eve.fleet.models import EveFleetWing,\
    EveFleetSquad, FleetSettings

class EveFleetEndpoint(object):
    def __init__(self, fleetID):
        # type: (int) -> None
        self.__fleetID = fleetID

    def get_member(self):
        # type: (int) -> dict(str, Any)
        client = get_esi_client()
        response = client.request(api.op['get_fleets_fleet_id_members'](fleet_id=self.__fleetID))
        if response.status_code == 200:
            return EveFleetMembers(get_expire_time(response), response.status_code, None, response.data)
        return EveFleetMembers(get_expire_time(response), response.status_code, response.data['error'], None)

    def get_fleet_settings(self):
        # type: () -> EveFleet
        '''
        Get fleet information
        get_fleets_fleet_id_ok {
            is_free_move (boolean):

            Is free-move enabled ,
            is_registered (boolean):

            Does the fleet have an active fleet advertisement ,
            is_voice_enabled (boolean):

            Is EVE Voice enabled ,
            motd (string):

            Fleet MOTD in CCP flavoured HTML
        }
        '''
        client = get_esi_client()
        response = client.request(api.op['get_fleets_fleet_id'](fleet_id=self.__fleetID))
        if (response.status_code == 200):
            return EveFleet(get_expire_time(response), response.status_code, None,
                             response.data['is_free_move'], response.data['is_registered'],
                             response.data['is_voice_enabled'], response.data['motd'])
        return EveFleet(get_expire_time(response), response.status_code, response.data['error'],
                        None, None,
                        None, None
                        )

    def set_fleet_settings(self, is_free_move, motd):
        # type: (boolean, str) -> ESIResponse
        settings = FleetSettings(is_free_move, motd)
        client = get_esi_client()

        response = client.request(api.op['put_fleets_fleet_id_new_settings'](fleet_id=self.__fleetID, new_settings=settings.get_esi_data()))
        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)
        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])

    def get_wings(self):
        # type: () -> EveFleetWings
        client = get_esi_client()
        response = client.request(api.op['get_fleets_fleet_id_wings'](fleet_id=self.__fleetID))
        if response.status_code == 200:
            wings = []
            for wing in response.data:
                wingID = wing['id']
                wingName = wing['name']
                squads = []
                for squad in wing['squads']:
                    squad = EveFleetSquad(squad['id'], squad['name'])
                    squads.append(squad)
                fleetWing = EveFleetWing(wingID, wingName, squads)
                wings.append(fleetWing)

            return EveFleetWings(get_expire_time(response), response.status_code, None, wings)
        return EveFleetWings(get_expire_time(response), response.status_code, response.data['error'], None)

    def create_wing(self):
        # type: () -> WingCreated
        client = get_esi_client()
        response = client.request(api.op['post_fleets_fleet_id_wings'](fleet_id=self.__fleetID))
        if response.status_code == 201:
            return WingCreated(get_expire_time(response), response.status_code, None,
                               response.data['wing_id']
                               )
        return WingCreated(get_expire_time(response), response.status_code, response.data['error'])

    def set_wing_name(self, wingID, name):
        # type: (int, str) -> ESIResponse
        client = get_esi_client()
        data = {'name': name}
        response = client.request(api.op['put_fleets_fleet_id_wings_wing_id'](fleet_id=self.__fleetID, wing_id=wingID, naming=data))

        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)

        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])

    def create_squad(self, wingID):
        # type: (int) -> SquadCreated
        client = get_esi_client()
        response = client.request(api.op['post_fleets_fleet_id_wings_wing_id_squads'](fleet_id=self.__fleetID, wing_id=wingID))
        if response.status_code == 201:
            return SquadCreated(get_expire_time(response), response.status_code, None,
                                wingID, response.data['squad_id']
                                )
        return SquadCreated(get_expire_time(response), response.status_code, None, None, None)

    def set_squad_name(self, squadID, name):
        # type: (int, str) -> ESIResponse
        client = get_esi_client()
        response = client.request(api.op['put_fleets_fleet_id_squads_squad_id'](fleet_id=self.__fleetID, squad_id=squadID, naming={'name': name}))
        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)
        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])

    def invite(self, characterID, role, squadID, wingID):
        # type: (int, str, int, int) -> dict(str, Any)
        '''
        'fleet_commander', 'wing_commander', 'squad_commander', 'squad_member'
        '''
        client = get_esi_client()
        invite = {};
        invite['character_id'] = characterID
        invite['role'] = role
        if squadID is not None:
            invite['squad_id'] = squadID
        if wingID is not None:
            invite['wing_id'] = wingID
        response = client.request(api.op['post_fleets_fleet_id_members_invitation'](fleet_id=self.__fleetID, invitation=invite))
        if response.status_code == 204:
            return ESIResponse(get_expire_time(response), response.status_code, None)
        return ESIResponse(get_expire_time(response), response.status_code, response.data['error'])
<|endoftext|>


<commit_before>import bio_utility, utility, clusterization

# Create folder for PDB structures and filling it with PDB files.
folder_name = utility.folder_creation(utility.folder_path_generation())
# Filling in the protein sequences.
protein_list = bio_utility.fill_proteins_sequences(bio_utility.construct_protein_list(
    input("Enter the filename: ")), folder_name, "PDBs")
# Obtaining and writing down the results of alignment in the "size" cluster
bio_utility.saving_alignment_results(bio_utility.alignment_at_cluster_group(
    clusterization.clusterisation_via_ligand_size(protein_list)), folder_name, 'ligand_size')
# Obtaining and writing down the results of alignment in the "size" cluster
bio_utility.saving_alignment_results(bio_utility.alignment_at_cluster_group(
    clusterization.clusterisation_via_ligand_type(protein_list)), folder_name, 'ligand_type')

print("All has been done, master!")<commit_msg>Added saving protein sequences to .csv.<commit_after>import bio_utility, utility, clusterization
from os.path import join

def save_sequences(proteins, file_name):
    with open(file_name, 'w') as file:
        file.write('id,sequence\n')
        for protein in proteins:
            file.write('{0},{1}\n'.format(protein.id, protein.sequence))

# Create folder for PDB structures and filling it with PDB files.
folder_name = utility.folder_creation(utility.folder_path_generation())
# Filling in the protein sequences.
protein_list = bio_utility.fill_proteins_sequences(bio_utility.construct_protein_list(
    input("Enter the filename: ")), folder_name, "PDBs")
# Obtaining and writing down the results of alignment in the "size" cluster
bio_utility.saving_alignment_results(bio_utility.alignment_at_cluster_group(
    clusterization.clusterisation_via_ligand_size(protein_list)), folder_name, 'ligand_size')
# Obtaining and writing down the results of alignment in the "size" cluster
bio_utility.saving_alignment_results(bio_utility.alignment_at_cluster_group(
    clusterization.clusterisation_via_ligand_type(protein_list)), folder_name, 'ligand_type')

save_sequences(protein_list, join(folder_name, 'sequences.csv'))

print("All has been done, master!")<|endoftext|>


<commit_before># django imports
from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.utils.translation import ugettext_lazy as _

# lfs imports
import lfs.core.settings as lfs_settings
from lfs.checkout.settings import CHECKOUT_TYPES
from lfs.checkout.settings import CHECKOUT_TYPE_SELECT
from lfs.core.fields.thumbs import ImageWithThumbsField
from lfs.catalog.models import StaticBlock

class Country(models.Model):
    """Holds country relevant data for the shop.
    """
    code = models.CharField(_(u"Country code"), max_length=2)
    name = models.CharField(_(u"Name"), max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Countries'
        ordering = ("name", )

class ActionGroup(models.Model):
    """Actions of a action group can be displayed on several parts of the web
    page.

    **Attributes**:

    name
        The name of the group.
    """
    name = models.CharField(blank=True, max_length=100, unique=True)

    class Meta:
        ordering = ("name", )

    def __unicode__(self):
        return self.name

    def get_actions(self):
        """Returns the actions of this group.
        """
        return self.actions.filter(active=True)

class Action(models.Model):
    """A action is a link which belongs to a action groups.

    **Attributes**:

    group
        The belonging group.

    title
        The title of the menu tab.

    link
        The link to the object.

    active
        If true the tab is displayed.

    position
        the position of the tab within the menu.

    parent
        Parent tab to create a tree.
    """
    active = models.BooleanField(_(u"Active"), default=False)
    title = models.CharField(_(u"Title"), max_length=40)
    link = models.CharField(_(u"Link"), blank=True, max_length=100)
    group = models.ForeignKey(ActionGroup, verbose_name=_(u"Group"), related_name="actions")
    position = models.IntegerField(_(u"Position"), default=999)
    parent = models.ForeignKey("self", verbose_name=_(u"Parent"), blank=True, null=True)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ("position", )

class Shop(models.Model):
    """Holds all shop related information.

    At the moment there must be exactly one shop with id == 1. (This may be
    changed in future to provide multi shops.)

    Instance variables:

    - name
       The name of the shop. This is used for the the title of the HTML pages

    - shop_owner
       The shop owner. This is displayed within several places for instance the
       checkout page

    - from_email
       This e-mail address is used for the from header of all outgoing e-mails

    - notification_emails
       This e-mail addresses are used for incoming notification e-mails, e.g.
       received an order. One e-mail address per line.

    - description
       A description of the shop

    - image
      An image which can be used as default image if a category doesn't have one

    - product_cols, product_rows, category_cols
       Upmost format information, which defines how products and categories are
       displayed within several views. These may be inherited by categories and
       sub categories.

    - google_analytics_id
       Used to generate google analytics tracker code and e-commerce code. the
       id has the format UA-xxxxxxx-xx and is provided by Google.

    - ga_site_tracking
       If selected and the google_analytics_id is given google analytics site
       tracking code is inserted into the HTML source code.

    - ga_ecommerce_tracking
       If selected and the google_analytics_id is given google analytics
       e-commerce tracking code is inserted into the HTML source code.

    - countries
       Selected countries will be offered to the shop customer tho choose for
       shipping and invoice address.

    - default_country
       This country will be used to calculate shipping price if the shop
       customer doesn't have select a country yet.

    - price_calculator
        Class that implements lfs.price.PriceCalculator for calculating product
        price. This is the default price calculator for all products.

    - checkout_type
       Decides whether the customer has to login, has not to login or has the
       choice to to login or not to be able to check out.

    - confirm_toc
       If this is activated the shop customer has to confirm terms and
       conditions to checkout.
    """
    name = models.CharField(_(u"Name"), max_length=30)
    shop_owner = models.CharField(_(u"Shop owner"), max_length=100, blank=True)
    from_email = models.EmailField(_(u"From e-mail address"))
    notification_emails = models.TextField(_(u"Notification email addresses"))

    description = models.TextField(_(u"Description"), blank=True)
    image = ImageWithThumbsField(_(u"Image"), upload_to="images", blank=True, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400)))
    static_block = models.ForeignKey(StaticBlock, verbose_name=_(u"Static block"), blank=True, null=True, related_name="shops")

    product_cols = models.IntegerField(_(u"Product cols"), default=1)
    product_rows = models.IntegerField(_(u"Product rows"), default=10)
    category_cols = models.IntegerField(_(u"Category cols"), default=1)
    google_analytics_id = models.CharField(_(u"Google Analytics ID"), blank=True, max_length=20)
    ga_site_tracking = models.BooleanField(_(u"Google Analytics Site Tracking"), default=False)
    ga_ecommerce_tracking = models.BooleanField(_(u"Google Analytics E-Commerce Tracking"), default=False)

    invoice_countries = models.ManyToManyField(Country, verbose_name=_(u"Invoice Countries"), related_name="invoice")
    shipping_countries = models.ManyToManyField(Country, verbose_name=_(u"Shipping Countries"), related_name="shipping")
    default_country = models.ForeignKey(Country, verbose_name=_(u"Default country"))

    # You can find locale information here: http://en.wikipedia.org/wiki/Locale
    default_locale = models.CharField(_(u"Default Shop Locale"), max_length=20, default="en_US.UTF-8")
    use_international_currency_code = models.BooleanField(_(u"Use international currency codes"), default=False)
    price_calculator = models.CharField(choices=lfs_settings.LFS_PRICE_CALCULATOR_DICTIONARY.items(), max_length=255, default=lfs_settings.LFS_DEFAULT_PRICE_CALCULATOR)

    checkout_type = models.PositiveSmallIntegerField(_(u"Checkout type"), choices=CHECKOUT_TYPES, default=CHECKOUT_TYPE_SELECT)
    confirm_toc = models.BooleanField(default=False)

    class Meta:
        permissions = (("manage_shop", "Manage shop"),)

    def __unicode__(self):
        return self.name

    def get_format_info(self):
        """
        """
        return {
            "product_cols": self.product_cols,
            "product_rows": self.product_rows,
            "category_cols": self.category_cols,
        }

    def get_default_country(self):
        """Returns the default country of the shop.
        """
        cache_key = "%s-default-country-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id)
        default_country = cache.get(cache_key)
        if default_country:
            return default_country

        default_country = self.default_country
        cache.set(cache_key, default_country)

        return default_country

    def get_notification_emails(self):
        """Returns the notification e-mail addresses as list
        """
        import re
        adresses = re.split("[\s,]+", self.notification_emails)
        return adresses

    def get_parent_for_portlets(self):
        """Implements contract for django-portlets. Returns always None as there
        is no parent for a shop.
        """
        return None

class Application(models.Model):
    version = models.CharField(_("Version"), blank=True, max_length=10)

from monkeys import *
<commit_msg>Changed label of 'default country'<commit_after># django imports
from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.utils.translation import ugettext_lazy as _

# lfs imports
import lfs.core.settings as lfs_settings
from lfs.checkout.settings import CHECKOUT_TYPES
from lfs.checkout.settings import CHECKOUT_TYPE_SELECT
from lfs.core.fields.thumbs import ImageWithThumbsField
from lfs.catalog.models import StaticBlock

class Country(models.Model):
    """Holds country relevant data for the shop.
    """
    code = models.CharField(_(u"Country code"), max_length=2)
    name = models.CharField(_(u"Name"), max_length=100)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'Countries'
        ordering = ("name", )

class ActionGroup(models.Model):
    """Actions of a action group can be displayed on several parts of the web
    page.

    **Attributes**:

    name
        The name of the group.
    """
    name = models.CharField(blank=True, max_length=100, unique=True)

    class Meta:
        ordering = ("name", )

    def __unicode__(self):
        return self.name

    def get_actions(self):
        """Returns the actions of this group.
        """
        return self.actions.filter(active=True)

class Action(models.Model):
    """A action is a link which belongs to a action groups.

    **Attributes**:

    group
        The belonging group.

    title
        The title of the menu tab.

    link
        The link to the object.

    active
        If true the tab is displayed.

    position
        the position of the tab within the menu.

    parent
        Parent tab to create a tree.
    """
    active = models.BooleanField(_(u"Active"), default=False)
    title = models.CharField(_(u"Title"), max_length=40)
    link = models.CharField(_(u"Link"), blank=True, max_length=100)
    group = models.ForeignKey(ActionGroup, verbose_name=_(u"Group"), related_name="actions")
    position = models.IntegerField(_(u"Position"), default=999)
    parent = models.ForeignKey("self", verbose_name=_(u"Parent"), blank=True, null=True)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ("position", )

class Shop(models.Model):
    """Holds all shop related information.

    At the moment there must be exactly one shop with id == 1. (This may be
    changed in future to provide multi shops.)

    Instance variables:

    - name
       The name of the shop. This is used for the the title of the HTML pages

    - shop_owner
       The shop owner. This is displayed within several places for instance the
       checkout page

    - from_email
       This e-mail address is used for the from header of all outgoing e-mails

    - notification_emails
       This e-mail addresses are used for incoming notification e-mails, e.g.
       received an order. One e-mail address per line.

    - description
       A description of the shop

    - image
      An image which can be used as default image if a category doesn't have one

    - product_cols, product_rows, category_cols
       Upmost format information, which defines how products and categories are
       displayed within several views. These may be inherited by categories and
       sub categories.

    - google_analytics_id
       Used to generate google analytics tracker code and e-commerce code. the
       id has the format UA-xxxxxxx-xx and is provided by Google.

    - ga_site_tracking
       If selected and the google_analytics_id is given google analytics site
       tracking code is inserted into the HTML source code.

    - ga_ecommerce_tracking
       If selected and the google_analytics_id is given google analytics
       e-commerce tracking code is inserted into the HTML source code.

    - countries
       Selected countries will be offered to the shop customer tho choose for
       shipping and invoice address.

    - default_country
       This country will be used to calculate shipping price if the shop
       customer doesn't have select a country yet.

    - price_calculator
        Class that implements lfs.price.PriceCalculator for calculating product
        price. This is the default price calculator for all products.

    - checkout_type
       Decides whether the customer has to login, has not to login or has the
       choice to to login or not to be able to check out.

    - confirm_toc
       If this is activated the shop customer has to confirm terms and
       conditions to checkout.
    """
    name = models.CharField(_(u"Name"), max_length=30)
    shop_owner = models.CharField(_(u"Shop owner"), max_length=100, blank=True)
    from_email = models.EmailField(_(u"From e-mail address"))
    notification_emails = models.TextField(_(u"Notification email addresses"))

    description = models.TextField(_(u"Description"), blank=True)
    image = ImageWithThumbsField(_(u"Image"), upload_to="images", blank=True, null=True, sizes=((60, 60), (100, 100), (200, 200), (400, 400)))
    static_block = models.ForeignKey(StaticBlock, verbose_name=_(u"Static block"), blank=True, null=True, related_name="shops")

    product_cols = models.IntegerField(_(u"Product cols"), default=1)
    product_rows = models.IntegerField(_(u"Product rows"), default=10)
    category_cols = models.IntegerField(_(u"Category cols"), default=1)
    google_analytics_id = models.CharField(_(u"Google Analytics ID"), blank=True, max_length=20)
    ga_site_tracking = models.BooleanField(_(u"Google Analytics Site Tracking"), default=False)
    ga_ecommerce_tracking = models.BooleanField(_(u"Google Analytics E-Commerce Tracking"), default=False)

    invoice_countries = models.ManyToManyField(Country, verbose_name=_(u"Invoice countries"), related_name="invoice")
    shipping_countries = models.ManyToManyField(Country, verbose_name=_(u"Shipping countries"), related_name="shipping")
    default_country = models.ForeignKey(Country, verbose_name=_(u"Default shipping country"))

    # You can find locale information here: http://en.wikipedia.org/wiki/Locale
    default_locale = models.CharField(_(u"Default Shop Locale"), max_length=20, default="en_US.UTF-8")
    use_international_currency_code = models.BooleanField(_(u"Use international currency codes"), default=False)
    price_calculator = models.CharField(choices=lfs_settings.LFS_PRICE_CALCULATOR_DICTIONARY.items(), max_length=255, default=lfs_settings.LFS_DEFAULT_PRICE_CALCULATOR)

    checkout_type = models.PositiveSmallIntegerField(_(u"Checkout type"), choices=CHECKOUT_TYPES, default=CHECKOUT_TYPE_SELECT)
    confirm_toc = models.BooleanField(default=False)

    class Meta:
        permissions = (("manage_shop", "Manage shop"),)

    def __unicode__(self):
        return self.name

    def get_format_info(self):
        """
        """
        return {
            "product_cols": self.product_cols,
            "product_rows": self.product_rows,
            "category_cols": self.category_cols,
        }

    def get_default_country(self):
        """Returns the default country of the shop.
        """
        cache_key = "%s-default-country-%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, self.id)
        default_country = cache.get(cache_key)
        if default_country:
            return default_country

        default_country = self.default_country
        cache.set(cache_key, default_country)

        return default_country

    def get_notification_emails(self):
        """Returns the notification e-mail addresses as list
        """
        import re
        adresses = re.split("[\s,]+", self.notification_emails)
        return adresses

    def get_parent_for_portlets(self):
        """Implements contract for django-portlets. Returns always None as there
        is no parent for a shop.
        """
        return None

class Application(models.Model):
    version = models.CharField(_("Version"), blank=True, max_length=10)

from monkeys import *
<|endoftext|>


<commit_before>import rsf
import numpy as np
import matplotlib.pyplot as plt
import sys

def error():
    print "Error! Valid test types are step and shs"
    sys.exit()

if len(sys.argv)<2:
    error()

test = sys.argv[1]

if test=='step':

    model = rsf.RateState()

    # Set model initial conditions
    model.mu0 = 0.6
    model.a = 0.005
    model.b = 0.01
    model.dc = 10.
    model.k = 1e-3
    model.v = 1.
    model.vlp = 10.

    # We want to solve for 30 seconds at 100Hz
    model.model_time = np.arange(0,40.01,0.01)

    # We want to slide at 1 um/s for 10 s, then at 10 um/s for 31
    lp_velocity = np.ones_like(model.model_time)
    lp_velocity[10*100:] = 10. # duplicate chris' step

    # This makes an interesting case for sure. Different linear acceleartion profiles
    #lp_velocity[10*100:11*100] = np.linspace(1,10,100) # ramp over 1 second
    #lp_velocity[10*100:15*100] = np.linspace(1,10,500) # ramp over 5 seconds

    # Set the model load point velocity, must be same shape as model.model_time
    model.loadpoint_velocity = lp_velocity

    results = model.solve()

    velocity = results.slider_velocity
    friction = results.friction
    state = results.state1

    print np.shape(model.model_time)
    print np.shape(velocity)
    print np.shape(friction)
    print np.shape(state)

    t = np.arange(0,30,0.01)

    # Read in Chris Marone's Solutions
    cjm_disp, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_model.dis',skiprows=2,unpack=True)
    cjm_time, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_model.tim',skiprows=2,unpack=True)
    cjm_state = np.loadtxt('cjm_model_comparison/cjm_model.sta',skiprows=2,unpack=True,usecols=[1])
    cjm_vel,cjm_mu2 = np.loadtxt('cjm_model_comparison/cjm_model.vel',skiprows=2,unpack=True)
    cjm_vel = np.exp(cjm_vel)*1.

    fig = plt.figure(1)
    plt.title('Slider Velocity')
    plt.plot(velocity,color='k',label='JRL')
    plt.plot(cjm_vel,color='r',label='CJM')
    #plt.plot(velocities,color='b')

    fig = plt.figure(2)
    plt.title('Friction')
    plt.plot(model.model_time-10.,friction,color='k',label='JRL')
    plt.plot(cjm_time,cjm_mu,color='r',label='CJM')

    fig = plt.figure(3)
    plt.title('State Variable')
    plt.plot(model.model_time-10.,state,color='k',label='JRL')
    plt.plot(cjm_time,cjm_state,color='r',label='CJM')

    fig = plt.figure(4)
    plt.title('Phase')
    #ln(v/v0),mu
    plt.plot(np.log(model.results.slider_velocity/model.vref),model.results.friction,color='k',label='JRL')
    plt.plot(np.log(cjm_vel/1.),cjm_mu2,color='r',label='CJM')

    fig = plt.figure(5)
    plt.title('Loading Velocity Profile')
    plt.plot(model.model_time-10., model.loadpoint_velocity)

    plt.show()

if test == 'shs':
    ## SHS EXAMPLE

    model = rsf.RateState()

    # Set model initial conditions
    model.mu0 = 0.6
    model.a = 0.005
    model.b = 0.01
    model.dc = 10.
    model.k = 1e-3
    model.v = 1.
    model.vlp = 10.

    # We want to solve for 30 seconds at 100Hz
    model.model_time = np.arange(0,310.01,0.01)

    # We want to slide at 1 um/s for 10 s, then at 10 um/s for 31
    lp_velocity = np.ones_like(model.model_time)
    lp_velocity[10*100:110*100] = 0.0#1e-10 # duplicate chris' step

    # Set the model load point velocity, must be same shape as model.model_time
    model.loadpoint_velocity = lp_velocity

    results = model.solve()

    velocity = results.slider_velocity
    friction = results.friction
    state = results.state1

    print np.shape(model.model_time)
    print np.shape(velocity)
    print np.shape(friction)
    print np.shape(state)

    # Read in Chris Marone's Solutions
    cjm_disp, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_shs.dis',skiprows=2,unpack=True)
    cjm_time, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_shs.tim',skiprows=2,unpack=True)
    cjm_state = np.loadtxt('cjm_model_comparison/cjm_shs.sta',skiprows=2,unpack=True,usecols=[1])
    cjm_vel,cjm_mu2 = np.loadtxt('cjm_model_comparison/cjm_shs.vel',skiprows=2,unpack=True)
    cjm_vel = np.exp(cjm_vel)*1.

    fig = plt.figure(1)
    plt.title('Slider Velocity')
    plt.plot(velocity,color='k',label='JRL')
    plt.plot(cjm_vel,color='r',label='CJM')
    #plt.plot(velocities,color='b')

    fig = plt.figure(2)
    plt.title('Friction')
    plt.plot(model.model_time-10.,friction,color='k',label='JRL')
    plt.plot(cjm_time,cjm_mu,color='r',label='CJM')

    fig = plt.figure(3)
    plt.title('State Variable')
    plt.plot(model.model_time-10.,state,color='k',label='JRL')
    plt.plot(cjm_time,cjm_state,color='r',label='CJM')

    fig = plt.figure(4)
    plt.title('Phase')
    #ln(v/v0),mu
    plt.plot(np.log(model.results.slider_velocity/model.vref),model.results.friction,color='k',label='JRL')
    plt.plot(np.log(cjm_vel/1.),cjm_mu2,color='r',label='CJM')

    fig = plt.figure(5)
    plt.title('Loading Velocity Profile')
    plt.plot(model.model_time-10., model.loadpoint_velocity)

    plt.show()

else:
    error()
<commit_msg>Add negative velocity example to test<commit_after>import rsf
import numpy as np
import matplotlib.pyplot as plt
import sys

def error():
    print "Error! Valid test types are step and shs"
    sys.exit()

if len(sys.argv)<2:
    error()

test = sys.argv[1]

if test=='step':

    model = rsf.RateState()

    # Set model initial conditions
    model.mu0 = 0.6
    model.a = 0.005
    model.b = 0.01
    model.dc = 10.
    model.k = 1e-3
    model.v = 1.
    model.vlp = 10.

    # We want to solve for 30 seconds at 100Hz
    model.model_time = np.arange(0,40.01,0.01)

    # We want to slide at 1 um/s for 10 s, then at 10 um/s for 31
    lp_velocity = np.ones_like(model.model_time)
    lp_velocity[10*100:] = 10. # duplicate chris' step

    # This makes an interesting case for sure. Different linear acceleartion profiles
    #lp_velocity[10*100:11*100] = np.linspace(1,10,100) # ramp over 1 second
    #lp_velocity[10*100:15*100] = np.linspace(1,10,500) # ramp over 5 seconds

    # Set the model load point velocity, must be same shape as model.model_time
    model.loadpoint_velocity = lp_velocity

    results = model.solve()

    velocity = results.slider_velocity
    friction = results.friction
    state = results.state1

    print np.shape(model.model_time)
    print np.shape(velocity)
    print np.shape(friction)
    print np.shape(state)

    t = np.arange(0,30,0.01)

    # Read in Chris Marone's Solutions
    cjm_disp, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_model.dis',skiprows=2,unpack=True)
    cjm_time, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_model.tim',skiprows=2,unpack=True)
    cjm_state = np.loadtxt('cjm_model_comparison/cjm_model.sta',skiprows=2,unpack=True,usecols=[1])
    cjm_vel,cjm_mu2 = np.loadtxt('cjm_model_comparison/cjm_model.vel',skiprows=2,unpack=True)
    cjm_vel = np.exp(cjm_vel)*1.

    fig = plt.figure(1)
    plt.title('Slider Velocity')
    plt.plot(velocity,color='k',label='JRL')
    plt.plot(cjm_vel,color='r',label='CJM')
    #plt.plot(velocities,color='b')

    fig = plt.figure(2)
    plt.title('Friction')
    plt.plot(model.model_time-10.,friction,color='k',label='JRL')
    plt.plot(cjm_time,cjm_mu,color='r',label='CJM')

    fig = plt.figure(3)
    plt.title('State Variable')
    plt.plot(model.model_time-10.,state,color='k',label='JRL')
    plt.plot(cjm_time,cjm_state,color='r',label='CJM')

    fig = plt.figure(4)
    plt.title('Phase')
    #ln(v/v0),mu
    plt.plot(np.log(model.results.slider_velocity/model.vref),model.results.friction,color='k',label='JRL')
    plt.plot(np.log(cjm_vel/1.),cjm_mu2,color='r',label='CJM')

    fig = plt.figure(5)
    plt.title('Loading Velocity Profile')
    plt.plot(model.model_time-10., model.loadpoint_velocity)

    plt.show()

if test == 'shs':
    ## SHS EXAMPLE

    model = rsf.RateState()

    # Set model initial conditions
    model.mu0 = 0.6
    model.a = 0.005
    model.b = 0.01
    model.dc = 10.
    model.k = 1e-3
    model.v = 1.
    model.vlp = 10.

    # We want to solve for 30 seconds at 100Hz
    model.model_time = np.arange(0,310.01,0.01)

    # We want to slide at 1 um/s for 10 s, then at 10 um/s for 31
    lp_velocity = np.ones_like(model.model_time)
    lp_velocity[10*100:110*100] = 0.0#1e-10 # duplicate chris' step

    # Set the model load point velocity, must be same shape as model.model_time
    model.loadpoint_velocity = lp_velocity

    results = model.solve()

    velocity = results.slider_velocity
    friction = results.friction
    state = results.state1

    print np.shape(model.model_time)
    print np.shape(velocity)
    print np.shape(friction)
    print np.shape(state)

    # Read in Chris Marone's Solutions
    cjm_disp, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_shs.dis',skiprows=2,unpack=True)
    cjm_time, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_shs.tim',skiprows=2,unpack=True)
    cjm_state = np.loadtxt('cjm_model_comparison/cjm_shs.sta',skiprows=2,unpack=True,usecols=[1])
    cjm_vel,cjm_mu2 = np.loadtxt('cjm_model_comparison/cjm_shs.vel',skiprows=2,unpack=True)
    cjm_vel = np.exp(cjm_vel)*1.

    fig = plt.figure(1)
    plt.title('Slider Velocity')
    plt.plot(velocity,color='k',label='JRL')
    plt.plot(cjm_vel,color='r',label='CJM')
    #plt.plot(velocities,color='b')

    fig = plt.figure(2)
    plt.title('Friction')
    plt.plot(model.model_time-10.,friction,color='k',label='JRL')
    plt.plot(cjm_time,cjm_mu,color='r',label='CJM')

    fig = plt.figure(3)
    plt.title('State Variable')
    plt.plot(model.model_time-10.,state,color='k',label='JRL')
    plt.plot(cjm_time,cjm_state,color='r',label='CJM')

    fig = plt.figure(4)
    plt.title('Phase')
    #ln(v/v0),mu
    plt.plot(np.log(model.results.slider_velocity/model.vref),model.results.friction,color='k',label='JRL')
    plt.plot(np.log(cjm_vel/1.),cjm_mu2,color='r',label='CJM')

    fig = plt.figure(5)
    plt.title('Loading Velocity Profile')
    plt.plot(model.model_time-10., model.loadpoint_velocity)

    plt.show()

if test == 'exp':
    ## SHS EXAMPLE

    model = rsf.RateState()

    # Set model initial conditions
    model.mu0 = 0.6
    model.a = 0.005
    model.b = 0.01
    model.dc = 10.
    model.k = 1e-3
    model.v = 1.
    model.vlp = 10.

    # We want to solve for 30 seconds at 100Hz
    model.model_time = np.arange(0,310.01,0.01)

    # We want to slide at 1 um/s for 10 s, then at 10 um/s for 31
    lp_velocity = np.ones_like(model.model_time)
    lp_velocity[8*100:10*100] = -1.
    lp_velocity[10*100:110*100] = 0.0#

    # Set the model load point velocity, must be same shape as model.model_time
    model.loadpoint_velocity = lp_velocity

    results = model.solve()

    velocity = results.slider_velocity
    friction = results.friction
    state = results.state1

    print np.shape(model.model_time)
    print np.shape(velocity)
    print np.shape(friction)
    print np.shape(state)

    # Read in Chris Marone's Solutions
    cjm_disp, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_shs.dis',skiprows=2,unpack=True)
    cjm_time, cjm_mu = np.loadtxt('cjm_model_comparison/cjm_shs.tim',skiprows=2,unpack=True)
    cjm_state = np.loadtxt('cjm_model_comparison/cjm_shs.sta',skiprows=2,unpack=True,usecols=[1])
    cjm_vel,cjm_mu2 = np.loadtxt('cjm_model_comparison/cjm_shs.vel',skiprows=2,unpack=True)
    cjm_vel = np.exp(cjm_vel)*1.

    fig = plt.figure(1)
    plt.title('Slider Velocity')
    plt.plot(velocity,color='k',label='JRL')
    plt.plot(cjm_vel,color='r',label='CJM')
    #plt.plot(velocities,color='b')

    fig = plt.figure(2)
    plt.title('Friction')
    plt.plot(model.model_time-10.,friction,color='k',label='JRL')
    plt.plot(cjm_time,cjm_mu,color='r',label='CJM')

    fig = plt.figure(3)
    plt.title('State Variable')
    plt.plot(model.model_time-10.,state,color='k',label='JRL')
    plt.plot(cjm_time,cjm_state,color='r',label='CJM')

    fig = plt.figure(4)
    plt.title('Phase')
    #ln(v/v0),mu
    plt.plot(np.log(model.results.slider_velocity/model.vref),model.results.friction,color='k',label='JRL')
    plt.plot(np.log(cjm_vel/1.),cjm_mu2,color='r',label='CJM')

    fig = plt.figure(5)
    plt.title('Loading Velocity Profile')
    plt.plot(model.model_time-10., model.loadpoint_velocity)

    plt.show()

else:
    error()
<|endoftext|>