from django.shortcuts import render_to_response, HttpResponseRedirect
from models import BlogPost, Category
from django.template import RequestContext
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from settings import TEMPLATE_DIRS
from django.core.context_processors import csrf
from django.views.decorators.cache import cache_control
from django.contrib.syndication.views import Feed
from django.core.cache import cache
from django.contrib.comments import Comment

def clearcache():
    try:
        cache._cache.clear() #in-memory caching
        cache._cache.flush_all()
    except AttributeError:
    # try filesystem caching next
        old = cache._cull_frequency
        old_max = cache._max_entries
        cache._max_entries = 0
        cache._cull_frequency = 1
        cache._cull_frequency = old
        cache._max_entries = old_max

@cache_control(no_cache=True, must_revalidate=True)
def get_post_by_slug(request, blogpost_slug):
    """
    Single post with comments and a comment form.
    """
    clearcache()
    post = BlogPost.objects.get(slug=blogpost_slug)

    d = dict(post=post, user=request.user, page_title=post.short_body)
    d.update(csrf(request))

    return render_to_response(TEMPLATE_DIRS[0] + "post.html", d, context_instance=RequestContext(request))

@cache_control(no_cache=True, must_revalidate=True)
def main(request):
    """Main listing."""
    clearcache()
    posts = BlogPost.objects.all().order_by("-timestamp")
    paginator = Paginator(posts, 5)

    category_list = Category.objects.all().order_by('title')[:5] #getting 5 recent categories

    try:
        page = int(request.GET.get("page", '1'))
    except ValueError:
        page = 1
    try:
        posts = paginator.page(page)
    except (InvalidPage, EmptyPage):
        posts = paginator.page(paginator.num_pages)

    d = dict(posts=posts, user=request.user, post_list=posts.object_list, page_title='My blogposts',
             category_list=category_list[:5])
    d.update(csrf(request))
    return render_to_response(TEMPLATE_DIRS[0] + "list.html", d, context_instance=RequestContext(request))

@cache_control(no_cache=True, must_revalidate=True)
def get_by_category(request, category_slug):
    # Get specified category
    clearcache()
    posts = BlogPost.objects.all().order_by('-timestamp')
    category_posts = []
    for post in posts:
        if post.categories.filter(slug=category_slug):
            category_posts.append(post)

    # Add pagination
    paginator = Paginator(category_posts, 5)

    # Get the category
    category = Category.objects.filter(slug=category_slug)[0]

    # Get the specified page
    try:
        page = int(request.GET.get("page", '1'))
    except ValueError:
        page = 1
    try:
        category_posts = paginator.page(page)
    except (InvalidPage, EmptyPage):
        category_posts = paginator.page(paginator.num_pages)

    d = dict(posts=category_posts, user=request.user, post_list=category_posts.object_list, category=category,
             page_title=category.title)
    d.update(csrf(request))
    return render_to_response(TEMPLATE_DIRS[0] + "category.html", d, context_instance=RequestContext(request))

class PostsFeed(Feed):
    title = "Tim Shenkao's blog posts"
    link = "/feeds/posts/"
    description = "Posts from Tim Shenkao's blog"

    def items(self):
        return BlogPost.objects.order_by('-timestamp')[:5]

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.body

@cache_control(no_cache=True, must_revalidate=True)
def comment_posted(request):
    if request.GET.get('c', False):
        comment_id = request.GET['c']
        comment = Comment.objects.get(pk=comment_id)
        post = BlogPost.objects.get(id=comment.object_pk)
        if post:
            return HttpResponseRedirect(post.get_absolute_url())
    return HttpResponseRedirect("/")


import re
from Preprocessing.Preprocessor import Preprocessor
from DataElements.DosageElement import DosageElement

class DosageRegExtractor(object):

    def __init__(self,rawTextFileName, intermediateXMLFileName):
        preprocess = Preprocessor(rawTextFileName, intermediateXMLFileName)
        self.Text = preprocess.rawText()

    def findEntity(self):

         dosage_amount ={'dos_amt': [], 'a_offset':[]}
         dosage_unit={'dos_unit': [], 'u_offset':[]}
         dosage_freq={'dos_freq': [], 'f_offset':[]}

         freq = '(week|day|month)'
         Freq = '(once|twice|everyday|weekly|every other day|one|two|three|four|five)'
         dose = '(mg|miligram|miligrams|mgs|kg|ug|ml|ul|mililitre|mls|milititres|tabs|tablets|tablet)'
         freq_code= '(1X|BID|BIW|HS|PRN|Q12H|Q2H|Q3H|Q3W|Q4H|Q5H|Q6H|Q8H|QD|QH|QID|QM|QOD|QOW|TID|TIW|UNK|QHS)'

         regex = re.compile(r'((\d{1,4}(\.[0-9]{1})?).?'+dose+ '.?'
                       '(.?([0-9].every.[0-9].'+freq+ ')|(.?[0-9].times.a.' +freq+ ')|(.?([0-9].?-.?[0-9].?times.?(per|a).?'+freq+'))|(.?'+Freq+'.?(a|per).?'+freq+')|(.?'+freq_code+'))?)'
                       '',re.IGNORECASE)

         it = re.finditer(regex, self.Text)

         for match in it:
                record = match.groups()

                dosage_amount['dos_amt'].append( record[1])

                dosage_amount['a_offset'].append(match.span(2))
                dosage_unit['dos_unit'].append(record[3])
                dosage_unit['u_offset'].append(match.span(4))
                dosage_freq['dos_freq'].append(record[4])
                dosage_freq['f_offset'].append(match.span(5))
         #print(dosage_amount, dosage_freq, dosage_unit)
         elementList =[]
         for a in range(0,len(dosage_amount['dos_amt'])):
                #print (dosage_freq['dos_freq'][a], [list(dosage_freq['f_offset'][a])]  )

                elementList .append([DosageElement(dosage_amount['dos_amt'][a], [list(dosage_amount['a_offset'][a])], "DosageRegExtractor" , "DOSE_AMT"), DosageElement(dosage_freq['dos_freq'][a],[list(dosage_freq['f_offset'][a])], "DosageRegExtractor" , "DOSE_FREQ"), DosageElement(dosage_unit['dos_unit'][a], [list(dosage_unit['u_offset'][a])], "DosageRegExtractor" , "DOSE_UNIT")])

         #print elementList
         return elementList


from django import forms
from django.utils.safestring import mark_safe
from django.template.loader import render_to_string
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from parsley.decorators import parsleyfy
from django.forms import ModelForm

from wastelinqbroker.globals import *
from wastelinqbroker import settings
from facility.models import *
from generator._models.Profiles import *
from tools._models.MasterContacts import *
from lookup.models import (DisposalMethod, )
from processcode.models import *

class CustomerProfileForm(ModelForm):
    Active_DF_CHOICES_RADIO = (
        ("PrimaryDF","Active"),
        ("SecondaryDF","Active"),
    )
    """  DateExpires1 = forms.DateField(input_formats=(settings.DATE_INPUT_FORMATS, ),required=False, widget=forms.DateInput(format=settings.DATE_INPUT_FORMATS,attrs={'class': 'w1 inputcalendar','required':'false'}))
    DisposalFacility1 = forms.ModelChoiceField(queryset=DisposalFacility.objects.all().order_by('Name'))
    HandlingCode1 = forms.ChoiceField(choices=HandlingCode_CHOICES, widget=forms.Select(attrs={'class': 'w4'}))

    DateExpires2 = forms.DateField(input_formats=(settings.DATE_INPUT_FORMATS, ),required=False, widget=forms.DateInput(format=settings.DATE_INPUT_FORMATS,attrs={'class': 'w1 inputcalendar','required':'false'}))
    DisposalFacility2 = forms.ModelChoiceField(queryset=DisposalFacility.objects.all().order_by('Name'))
    HandlingCode2 = forms.ChoiceField(choices=HandlingCode_CHOICES, widget=forms.Select(attrs={'class': 'w4'})) """

    #Active_DF = forms.ChoiceField( widget=forms.RadioSelect())
    Status= forms.ChoiceField(choices=CUSTOMER_PROFILE_STATUS_CHOICES, widget=forms.Select(attrs={'class': 'w4'}))

    class Meta:
        model = CustomerProfile
        fields = '__all__'
        exclude = ('Customer','Active_DF')
        widgets ={
            'Name': forms.TextInput(),
            #'DisposalFacility' : forms.ForeignKey(DisposalFacility, on_delete=forms.SET_NULL) ,
            #CostCenter :  forms.ForeignKey(CostCenters, on_delete=forms.PROTECT)
            'Status': forms.TextInput() ,
            'DateExpires' : forms.DateInput(format=settings.DATE_INPUT_FORMATS,attrs={'class': 'w1 inputcalendar'}),
            'ApprovalNumber' : forms.TextInput(),
            'HandlingCode' : forms.TextInput(),#forms.ForeignKey(HandlingCodes, on_delete=forms.PROTECT)
        }

class CustProfileDisposalFacilityForm(ModelForm):
    HandlingCode_CHOICES = (
        ("N/A","N/A"),
        ("H010- METALS RECOVERY","H010- METALS RECOVERY"),
        ("H020- SOLVENTS RECOVERY","H020- SOLVENTS RECOVERY"),
        ("H039- OTHER RECOVERY OR RECLAMATION FOR REUSE","H039- OTHER RECOVERY OR RECLAMATION FOR REUSE"),
        ("H050- ENERGY RECOVERY","H050- ENERGY RECOVERY"),
        ("H061- FUEL BLENDING","H061- FUEL BLENDING"),
        ("H040- INCINERATION","H040- INCINERATION"),
        ("H070- CHEMICAL TREATMENT","H070- CHEMICAL TREATMENT"),
        ("H081- BIOLOGICAL TREATMENT","H081- BIOLOGICAL TREATMENT"),
        ("H100- PHYSICAL TREATMENT ONLY","H100- PHYSICAL TREATMENT ONLY"),
        ("H110- STABILIZATION","H110- STABILIZATION"),
        ("H120- COMB. OF CHEMICAL","H120- COMB. OF CHEMICAL"),
        ("H121- NEUTRALIZATION ONLY","H121- NEUTRALIZATION ONLY"),
        ("H122- EVAPORATION","H122- EVAPORATION"),
        ("H129- OTHER TREATMENT","H129- OTHER TREATMENT"),
        ("H131- LAND TREATMENT OR APPLICATION","H131- LAND TREATMENT OR APPLICATION"),
        ("H132- LANDFILL OR SURFACE IMPOUNDMENT","H132- LANDFILL OR SURFACE IMPOUNDMENT"),
        ("H134- DEEPWELL OR UNDERGROUND INJECTION","H134- DEEPWELL OR UNDERGROUND INJECTION"),
        ("H135- DISCHARGE TO SEWER","H135- DISCHARGE TO SEWER"),
        ("H141- STORAGE","H141- STORAGE"),
    )
    DateExpires = forms.DateField(input_formats=(settings.DATE_INPUT_FORMATS, ),required=False, widget=forms.DateInput(format=settings.DATE_INPUT_FORMATS,attrs={'class': 'w1 inputcalendar','required':'false'}))
    #DisposalFacility = forms.ModelChoiceField(queryset=DisposalFacility.objects.all().order_by('Name'))
    HandlingCode = forms.ChoiceField(choices=HandlingCode_CHOICES, widget=forms.Select(attrs={'class': 'w4'}))
    class Meta:
        model = CustProfileDisposalFacility
        fields = '__all__'
class GeneratorAndCustomerInformationForm(ModelForm):

    EmergencyNumber = forms.CharField(max_length=255)

    FacilityState = forms.ChoiceField(choices=STATE_CHOICES, widget=forms.Select(attrs={'class': 'w33','placeholder':'State'}))

    SiteMailingState = forms.ChoiceField(choices=STATE_CHOICES, widget=forms.Select(attrs={'class': 'w33','placeholder':'State'}))

    BillingState = forms.ChoiceField(choices=STATE_CHOICES, widget=forms.Select(attrs={'class': 'w33','placeholder':'State'}))

    class Meta:
        model=GeneratorAndCustomerInformation
        fields = '__all__'
        exclude = ('EPAId', 'StateId', 'NAISC', )
        widgets = {
            'FacilityAddress1': forms.TextInput(),
            'FacilityAddress2': forms.TextInput(),
            'FacilityCity': forms.TextInput(),
            'FacilityState': forms.Select(),
            'FacilityZip': forms.TextInput(),
            'FacilityPhone': forms.TextInput(),

            'SiteMailingAddress1': forms.TextInput(),
            'SiteMailingAddress2': forms.TextInput(),
            'SiteMailingCity': forms.TextInput(),
            'SiteMailingState': forms.Select(),
            'SiteMailingZip': forms.TextInput(),
            'SiteMailingPhone': forms.TextInput(),

            'BillingAddress1': forms.TextInput(),
            'BillingAddress2': forms.TextInput(),
            'BillingCity': forms.TextInput(),
            'BillingState': forms.Select(),
            'BillingZip': forms.TextInput(),
            'BillingPhone': forms.TextInput(),
        }

class GeneratingProcessAndMaterialCompositionForm(ModelForm):
    class Meta:
        model = GeneratingProcessAndMaterialComposition
        fields = '__all__'
        widgets = {
            'WasteStreamDescription': forms.Textarea(attrs={'class': 'txtarea ',"rows":"3"}),
            'ProcessGeneratingTheWaste': forms.Textarea(attrs={'class': 'txtarea ',"rows":"3"}),
        }

class PhysicalAndChemicalPropertiesForm(ModelForm):
    CHEMICAL_PHYSICALSTATE_CHOICES = (
        ("Solid","Solid"),
        ("Dust/Powder","Dust/Powder"),
        ("Debris","Debris"),
        ("Sludge","Sludge"),
        ("Liquid","Liquid"),
        ("Gas/Aerosol","Gas/Aerosol"),
        ("Varies","Varies"),
    )
    pH_RADIO_CHOICES = (
        ("≤ 2","≤ 2"),
        ("> 2 to ≤ 5","> 2 to ≤ 5"),
        ("> 5 to ≤ 10","> 5 to ≤ 10"),
        ("> 10 to ≤ 12.5","> 10 to ≤ 12.5"),
        (">12.5",">12.5"),
        ("N/A","N/A"),
    )
    FLASH_POINT_RADIO_CHOICES = (
        ("< 90°F","< 90°F"),
        ("≥ 90°F to < 140°F","≥ 90°F to < 140°F"),
        ("≥ 140°F to < 150°F","≥ 140°F to < 150°F"),
        ("≥ 150°F to < 199°F","≥ 150°F to < 199°F"),
        ("≥ 200°F","≥ 200°F"),
    )
    YESNO_CHOICES_RADIO = (
        ("Yes","Yes"),
        ("No","No"),
    )
    PCPColor= forms.CharField(max_length=255)
    PCPOdor= forms.CharField(max_length=255)
    PCPBTUValue= forms.CharField(max_length=255)
    PCPViscosity= forms.CharField(max_length=255)
    PCPDensity= forms.CharField(max_length=255)
    PCPhysicalStateSolidLiquidSemiSolid = forms.ChoiceField(choices=CHEMICAL_PHYSICALSTATE_CHOICES, widget=forms.RadioSelect())
    PCpH = forms.ChoiceField(choices=pH_RADIO_CHOICES, widget=forms.RadioSelect())
    PCFlashPoint = forms.ChoiceField(choices=FLASH_POINT_RADIO_CHOICES, widget=forms.RadioSelect())
    #RegulatoryDOTHazardousMaterial= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    RegulatoryRestrictedUnderLDR= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    RegulatoryLDRSubcaterogyWastewater= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    RegulatoryUHCs= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    Regulatory500PPMVOC= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    RegulatoryLDRSubcaterogyNonWastewater= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    RegulatoryAlternativeStandardsofSoil= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    #Facility = forms.ForeignKey(Facility, on_delete=forms.PROTECT)
    #CopyNumber = forms.IntegerField()
    #CustomerProfile = forms.ForeignKey(CustomerProfile, on_delete=forms.CASCADE)

    class Meta:
        model =   PhysicalAndChemicalProperties
        fields = '__all__'
        widgets ={
            'PCPhysicalStateSolid2' : forms.CheckboxInput(),
            'PCPPhysicalStateLiquid2': forms.CheckboxInput(),
            'PCPPhysicalStateSludge2': forms.CheckboxInput(),
            'PCPPhysicalStateGas2': forms.CheckboxInput(),
            'PCPPhysicalStateSolidPercent': forms.TextInput(),
            'PCPPhysicalStateLiquidPercent':  forms.TextInput(),
            'PCPPhysicalStateSludgePercent':  forms.TextInput(),
            'PCPPhysicalStateGasPercent':  forms.TextInput(),
            'PCPOtherPropertiesExplosive': forms.CheckboxInput(),
            'PCPOtherPropertiesRadioactive': forms.CheckboxInput(),
            'PCPOtherPropertiesThermallyUnstable': forms.CheckboxInput(),
            'PCPOtherPropertiesShockSensitive': forms.CheckboxInput(),
            'PCPOtherPropertiesPyrophoric': forms.CheckboxInput(),
            'PCPOtherPropertiesOxidizer': forms.CheckboxInput(),
            'PCPOtherPropertiesWaterReactive': forms.CheckboxInput(),
            'PCPOtherPropertiesAirReactive': forms.CheckboxInput(),
            'PCPOtherPropertiesReactiveCyanides': forms.CheckboxInput(),
            'PCPOtherPropertiesReactiveSulfides': forms.CheckboxInput(),
            'PCPOtherPropertiesPolymerizable': forms.CheckboxInput(),
            'PCPOtherPropertiesPolymerizableInhibited': forms.CheckboxInput(),
            'PCPOtherPropertiesAbestosFriable': forms.CheckboxInput(),
            'PCPOtherPropertiesAbestosNonFriable': forms.CheckboxInput(),
            'PCPOtherPropertiesMetalFines': forms.CheckboxInput(),
            'PCPOtherPropertiesOrganaicPeroxides': forms.CheckboxInput(),
            'PCPOtherPropertiesDioxins': forms.CheckboxInput(),
            'PCPOtherPropertiesFurans': forms.CheckboxInput(),
            'PCPOtherPropertiesNORM': forms.CheckboxInput(),
            'PCPOtherPropertiesBiohazard': forms.CheckboxInput(),
            'PCPOtherPropertiesNONE': forms.CheckboxInput(),
        }

class ShippingAndPackagingInformationForm(ModelForm):
    VOLUMETYPE_CHOICES_RADIO = (
        ("Tons","Tons"),
        ("Lbs","Lbs"),
        ("Yds","Yds"),
        ("Gals","Gals"),
        ("Kgs","Kgs"),
    )
    FREQUENCY_CHOICES_RADIO = (
        ("One Time","One Time"),
        ("Monthly","Monthly"),
        ("Annually","Annually"),
    )
    YESNO_CHOICES_RADIO = (
        ("Yes","Yes"),
        ("No","No"),
    )
    ShippingAndPackagingVolume= forms.CharField(max_length=255, widget=forms.TextInput(attrs={"style":"float:left;margin-left:15px;"}))
    ShippingAndPackagingVolumeType= forms.ChoiceField(choices=VOLUMETYPE_CHOICES_RADIO, widget=forms.Select(attrs={"style":"width:auto;float:left;margin-left:5px;padding: 0.25rem 0;"}))
    #ShippingAndPackagingVolume= forms.CharField(max_length=255, widget=forms.TextInput(attrs={"class":"left"}))
    #ShippingAndPackagingVolumeType= forms.ChoiceField(choices=VOLUMETYPE_CHOICES_RADIO, widget=forms.Select(attrs={"class":"width-auto left"}))
    ShippingAndPackagingFrequency= forms.ChoiceField(choices=FREQUENCY_CHOICES_RADIO, widget=forms.RadioSelect())
    ShippingAndPackagingUSDOT= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    #WasteManagementUnits = forms.CharField(max_length=255)
    #ShippingAndPackagingUSDOTComment= forms.CharField(max_length=255)
    #CustomerProfile = forms.ForeignKey(CustomerProfile, on_delete=forms.CASCADE)

    class Meta:
        model = ShippingAndPackagingInformation
        fields = '__all__'
        widgets = {
            'ShippingAndPackagingUSDOTComment': forms.Textarea(attrs={'class': 'w1',"rows":"5"}),
        }

class ProcessingDetailContentForm(ModelForm):
    UNITS_CHOICES= (
        ("G","G"),
    )
    Final_TSDF = forms.ModelChoiceField(queryset=DisposalFacility.objects, empty_label='N/A', widget=forms.Select(attrs={'disabled':'disabled'}))
    Container_Type = forms.ModelChoiceField(queryset=ContainerType.objects, empty_label='N/A', widget=forms.Select(attrs={'onchange':'changeContainerType();'}))
    Container_Size = forms.ModelChoiceField(queryset=ContainerSize.objects, empty_label='N/A', widget=forms.Select(attrs={'disabled':'disabled'}))
    ProcessCode = forms.ModelChoiceField(queryset=ProcessCode.objects, empty_label='N/A', widget=forms.Select(attrs={'disabled':'disabled'}))
    Cost_Basis = forms.ModelChoiceField(queryset=CostBasis.objects, empty_label='N/A', widget=forms.Select())
    Dollar_Lk = forms.ChoiceField(choices=YESNO_CHOICES, widget=forms.Select())
    Active = forms.ChoiceField(choices=YESNO_CHOICES, widget=forms.Select())
    Shipping_Units = forms.ChoiceField(choices=UNITS_CHOICES, widget=forms.Select())
    Billing_Units = forms.ChoiceField(choices=UNITS_CHOICES, widget=forms.Select())
    class Meta:
        model = ProcessingDetail_Content
        fields = '__all__'
        """ widgets = {
            'Final_TSDF': forms.Select(attrs={'class': 'w1'}),
            'Vendor_Process_Code': forms.TextInput(attrs={'class': 'w1'}),
            'Container_Type': forms.Select(attrs={'class': 'w1'}),
            'Container_Size': forms.Select(attrs={'class': 'w1'}),
            'Cost_Basis': forms.Select(attrs={'class': 'w1'}),
            'Cost': forms.TextInput(attrs={'class': 'w1'}),
            'Min_Cost': forms.TextInput(attrs={'class': 'w1'}),
        } """

class RegulatoryInformationForm(ModelForm):
    YESNO_CHOICES_RADIO = (
        ("Yes","Yes"),
        ("No","No"),
    )
    WasteDetermination_CHOICES_RADIO = (
        ("Generator Knowledge","Generator Knowledge"),
        ("Waste Analysis","Waste Analysis"),
        ("SDS","SDS"),
    )

    Exemption_CHOICES_RADIO = (
        ("§261.4(a)(1) - Domestic Sewage and Mixtures of Domestic Sewage","§261.4(a)(1) - Domestic Sewage and Mixtures of Domestic Sewage"),
        ("§261.4(a)(2) - Point Source Discharge","§261.4(a)(2) - Point Source Discharge"),
        ("§261.4(a)(3) - Irrigation Return Flow","§261.4(a)(3) - Irrigation Return Flow"),
        ("§261.4(a)(4) - Radioactive Waste","§261.4(a)(4) - Radioactive Waste"),
        ("§261.4(a)(5) - In-Situ Mining","§261.4(a)(5) - In-Situ Mining"),
        ("§261.4(a)(6) - Pulping Liquors","§261.4(a)(6) - Pulping Liquors"),
        ("§261.4(a)(7) - Spent Sulfuric Acid","§261.4(a)(7) - Spent Sulfuric Acid"),
        ("§261.4(a)(8) - Reclamation in Enclosed Tanks","§261.4(a)(8) - Reclamation in Enclosed Tanks"),
        ("§261.4(a)(9) - Spent Wood Preservatives","§261.4(a)(9) - Spent Wood Preservatives"),
        ("§261.4(a)(10) - Coke By-Product Wastes","§261.4(a)(10) - Coke By-Product Wastes"),
        ("§261.4(a)(11) - Splash Condenser Dross Residue","§261.4(a)(11) - Splash Condenser Dross Residue"),
        ("§261.4(a)(12) - Hazardous Secondary Materials From the Petroleum Refining Industry","§261.4(a)(12) - Hazardous Secondary Materials From the Petroleum Refining Industry"),
        ("§261.4(a)(13) - Excluded Scrap Metal","§261.4(a)(13) - Excluded Scrap Metal"),
        ("§261.4(a)(14) - Shredded Circuit Boards","§261.4(a)(14) - Shredded Circuit Boards"),
        ("§261.4(a)(15) - Pulping Condensates Derived from Kraft Mill Steam Strippers","§261.4(a)(15) - Pulping Condensates Derived from Kraft Mill Steam Strippers"),
        ("§261.4(a)(17) - Spent materials generated within the primary mineral processing industry from which minerals, acids, cyanide, water, or other values are recovered by mineral processing or by beneficiation","§261.4(a)(17) - Spent materials generated within the primary mineral processing industry from which minerals, acids, cyanide, water, or other values are recovered by mineral processing or by beneficiation"),
        ("§261.4(a)(18) - Petrochemical recovered oil from an associated organic chemical manufacturing facility","§261.4(a)(18) - Petrochemical recovered oil from an associated organic chemical manufacturing facility"),
        ("§261.4(a)(19) - Spent caustic solutions from petroleum refining liquid treating processes used as a feedstock to produce cresylic or naphthenic acid","§261.4(a)(19) - Spent caustic solutions from petroleum refining liquid treating processes used as a feedstock to produce cresylic or naphthenic acid"),
        ("§261.4(a)(20) - Hazardous secondary materials used to make zinc fertilizers","§261.4(a)(20) - Hazardous secondary materials used to make zinc fertilizers"),
        ("§261.4(a)(21) - Zinc fertilizers made from hazardous wastes, or excluded hazardous secondary materials","§261.4(a)(21) - Zinc fertilizers made from hazardous wastes, or excluded hazardous secondary materials"),
        ("§261.4(a)(22) - Used cathode ray tubes (CRTs)","§261.4(a)(22) - Used cathode ray tubes (CRTs)"),
        ("§261.4(a)(23) - Hazardous secondary material generated and legitimately reclaimed within the United States or its territories and under the control of the generator","§261.4(a)(23) - Hazardous secondary material generated and legitimately reclaimed within the United States or its territories and under the control of the generator"),
        ("§261.4(a)(24) - Hazardous secondary material that is generated and then transferred to a verified reclamation facility for the purpose of reclamation is not a solid waste","§261.4(a)(24) - Hazardous secondary material that is generated and then transferred to a verified reclamation facility for the purpose of reclamation is not a solid waste"),
        ("§261.4(a)(26) - Solvent-contaminated wipes that are sent for cleaning and reuse are not solid wastes from the point of generation","§261.4(a)(26) - Solvent-contaminated wipes that are sent for cleaning and reuse are not solid wastes from the point of generation"),
        ("§261.4(a)(27) - Hazardous secondary material that is generated and then transferred to another person for the purpose of remanufacturing is not a solid waste","§261.4(a)(27) - Hazardous secondary material that is generated and then transferred to another person for the purpose of remanufacturing is not a solid waste"),
    )
    ORIGIN_CHOICES = (
        ('1 - Was generated on-site from a product or service activity.', '1 - Was generated on-site from a product or service activity.'),
        ('2 - Resulted from a spill clean-up, equipment decommissioning, or emergency removal.', '2 - Resulted from a spill clean-up, equipment decommissioning, or emergency removal.'),
        ('3 - Derived from the on-site management of a non-hazardous waste.', '3 - Derived from the on-site management of a non-hazardous waste.'),
        ('4 - Was received from off-site and was not recycled or treated on-site.', '4 - Was received from off-site and was not recycled or treated on-site.'),
        ('5 - Was residual from the on-site treatment, disposal, or recycling of preciously existing hazardous waste.', '5 - Was residual from the on-site treatment, disposal, or recycling of preciously existing hazardous waste.'),
        ('6 - Was from a state, federal, or locally funded cleanup.', '6 - Was from a state, federal, or locally funded cleanup.'),
        ('7 - Was from a corrective action or closure.', '7 - Was from a corrective action or closure.'),
    )

    EPAForm_CHOICES = (
        ("0","N/A"),
        ("Mixed Media/Debris/Devices",(
                                    ("W001 - LAB PACKS FROM ANY SOURCE NOT CONTAINING ACUTE HAZARDOUS WASTE","W001 - LAB PACKS FROM ANY SOURCE NOT CONTAINING ACUTE HAZARDOUS WASTE"),
                                    ("W002 - CONTAMINATED DEBRIS: PAPER, CLOTHING, RAGS, WOOD, EMPTY FIBER OR PLASTIC CONTAINERS, GLASS, PIPING, OTHER SOLIDS","W002 - CONTAMINATED DEBRIS: PAPER, CLOTHING, RAGS, WOOD, EMPTY FIBER OR PLASTIC CONTAINERS, GLASS, PIPING, OTHER SOLIDS"),
                                    ("W004 - LAB PACKS FROM ANY SOURCE CONTAINING ACUTE HAZARDOUS WASTE","W004 - LAB PACKS FROM ANY SOURCE CONTAINING ACUTE HAZARDOUS WASTE"),
                                    ("W005 - WASTE PHARMACEUTICALS MANAGED AS HAZARDOUS WASTE","W005 - WASTE PHARMACEUTICALS MANAGED AS HAZARDOUS WASTE"),
                                    ("W301 - CONTAMINATED SOIL","W301 - CONTAMINATED SOIL"),
                                    ("W309 - BATTERIES, BATTERY PARTS, CORES, CASINGS","W309 - BATTERIES, BATTERY PARTS, CORES, CASINGS"),
                                    ("W310 - FILTERS, SOLID ADSORBENTS, ION EXCHANGE RESINS AND SPENT CARBON","W310 - FILTERS, SOLID ADSORBENTS, ION EXCHANGE RESINS AND SPENT CARBON"),
                                    ("W320 - ELECTRICAL DEVICES (LAMPS, THERMOSTATS, CRTS, ETC.)","W320 - ELECTRICAL DEVICES (LAMPS, THERMOSTATS, CRTS, ETC.)"),
                                    ("W512 - SEDIMENT OR LAGOON DRAGOUT, DRILLING OR OTHER MUDS","W512 - SEDIMENT OR LAGOON DRAGOUT, DRILLING OR OTHER MUDS"),
                                    ("W801 - COMPRESSED GAS","W801 - COMPRESSED GAS"),
                                )
        ),
        ("Inorganic Liquids",(
                            ("W101 - VERY DILUTE AQUEOUS WASTE CONTAINING MORE THAN 99% WATER","W101 - VERY DILUTE AQUEOUS WASTE CONTAINING MORE THAN 99% WATER"),
                            ("W103 - SPENT CONCENTRATED ACID","W103 - SPENT CONCENTRATED ACID"),
                            ("W105 - ACIDIC AQUEOUS WASTES LESS THAN 5% ACID","W105 - ACIDIC AQUEOUS WASTES LESS THAN 5% ACID"),
                            ("W107 - AQUEOUS WASTE CONTAINING CYANIDES","W107 - AQUEOUS WASTE CONTAINING CYANIDES"),
                            ("W110 - CAUSTIC AQUEOUS WASTE WITHOUT CYANIDES","W110 - CAUSTIC AQUEOUS WASTE WITHOUT CYANIDES"),
                            ("W113 - OTHER AQUEOUS WASTE OR WASTEWATERS","W113 - OTHER AQUEOUS WASTE OR WASTEWATERS"),
                            ("W117 - WASTE LIQUID MERCURY","W117 - WASTE LIQUID MERCURY"),
                            ("W119 - OTHER INORGANIC LIQUID","W119 - OTHER INORGANIC LIQUID"),
                        )
        ),
        ("Organic Liquids",(
                            ("W200 - STILL BOTTOMS IN LIQUID FORM","W200 - STILL BOTTOMS IN LIQUID FORM"),
                            ("W202 - CONCENTRATED HALOGENATED (E.G. CHLORINATED) SOLVENT","W202 - CONCENTRATED HALOGENATED (E.G. CHLORINATED) SOLVENT"),
                            ("W203 - CONCENTRATED NON-HALOGENATED (E.G. NON-CHLORINATED) SOLVENT","W203 - CONCENTRATED NON-HALOGENATED (E.G. NON-CHLORINATED) SOLVENT"),
                            ("W204 - CONCENTRATED HALOGENATED / NON-HALOGENATED SOLVENT MIXTURE","W204 - CONCENTRATED HALOGENATED / NON-HALOGENATED SOLVENT MIXTURE"),
                            ("W205 - OIL-WATER EMULSION OR MIXTURE","W205 - OIL-WATER EMULSION OR MIXTURE"),
                            ("W206 - WASTE OIL MANAGED AS HAZARDOUS WASTE","W206 - WASTE OIL MANAGED AS HAZARDOUS WASTE"),
                            ("W209 - PAINT, INK, LACQUER, OR VARNISH","W209 - PAINT, INK, LACQUER, OR VARNISH"),
                            ("W210 - REACTIVE OR POLYMERIZABLE ORGANIC LIQUIDS AND ADHESIVES","W210 - REACTIVE OR POLYMERIZABLE ORGANIC LIQUIDS AND ADHESIVES"),
                            ("W211 - PAINT THINNER OR PETROLEUM DISTILLATES","W211 - PAINT THINNER OR PETROLEUM DISTILLATES"),
                            ("W219 - OTHER ORGANIC LIQUID","W219 - OTHER ORGANIC LIQUID"),
                        )
        ),
        ("Inorganic Solids",(
                            ("W303 - ASH","W303 - ASH"),
                            ("W304 - SLAGS, DROSSES, AND OTHER SOLID THERMAL RESIDUES","W304 - SLAGS, DROSSES, AND OTHER SOLID THERMAL RESIDUES"),
                            ("W307 - METAL SCALE, FILINGS AND SCRAP (INCLUDING METAL DRUMS)","W307 - METAL SCALE, FILINGS AND SCRAP (INCLUDING METAL DRUMS)"),
                            ("W312 - CYANIDE OR METAL CYANIDE BEARING SOLIDS, SALTS OR CHEMICALS","W312 - CYANIDE OR METAL CYANIDE BEARING SOLIDS, SALTS OR CHEMICALS"),
                            ("W316 - METAL SALTS OR CHEMICALS NOT CONTAINING CYANIDES","W316 - METAL SALTS OR CHEMICALS NOT CONTAINING CYANIDES"),
                            ("W319 - OTHER INORGANIC SOLIDS","W319 - OTHER INORGANIC SOLIDS"),
                        )
        ),
        ("Organic Solids",(
                            ("W401 - PESTICIDE SOLIDS","W401 - PESTICIDE SOLIDS"),
                            ("W403 - SOLID RESINS, PLASTICS OR POLYMERIZED ORGANICS","W403 - SOLID RESINS, PLASTICS OR POLYMERIZED ORGANICS"),
                            ("W405 - EXPLOSIVES OR REACTIVE ORGANIC SOLIDS","W405 - EXPLOSIVES OR REACTIVE ORGANIC SOLIDS"),
                            ("W406 - DRIED PAINT (PAINT CHIPS, FILTERS, AIR FILTERS, OTHER)","W406 - DRIED PAINT (PAINT CHIPS, FILTERS, AIR FILTERS, OTHER)"),
                            ("W409 - OTHER ORGANIC SOLIDS","W409 - OTHER ORGANIC SOLIDS"),
                        )
        ),
        ("Inorganic Sludges",(
                            ("W501 - LIME AND/OR METAL HYDROXIDE SLUDGES AND SOLIDS WITH NO CYANIDES","W501 - LIME AND/OR METAL HYDROXIDE SLUDGES AND SOLIDS WITH NO CYANIDES"),
                            ("W503 - GYPSUM SLUDGES FROM WASTEWATER TREATMENT OR AIR POLLUTION CONTROL","W503 - GYPSUM SLUDGES FROM WASTEWATER TREATMENT OR AIR POLLUTION CONTROL"),
                            ("W504 - OTHER SLUDGES FROM WASTEWATER TREATMENT OR AIR POLLUTION CONTROL","W504 - OTHER SLUDGES FROM WASTEWATER TREATMENT OR AIR POLLUTION CONTROL"),
                            ("W505 - METAL BEARING SLUDGES (INCLUDING PLATING SLUDGE) NOT CONTAINING CYANIDES","W505 - METAL BEARING SLUDGES (INCLUDING PLATING SLUDGE) NOT CONTAINING CYANIDES"),
                            ("W506 - CYANIDE-BEARING SLUDGES","W506 - CYANIDE-BEARING SLUDGES"),
                            ("W519 - OTHER INORGANIC SLUDGES","W519 - OTHER INORGANIC SLUDGES"),
                            ("W603 - OILY SLUDGE","W603 - OILY SLUDGE"),
                            ("W604 - PAINT OR INK SLUDGES, STILL BOTTOMS IN SLUDGE FORM","W604 - PAINT OR INK SLUDGES, STILL BOTTOMS IN SLUDGE FORM"),
                            ("W606 - RESINS, TARS, POLYMER OR TARRY SLUDGE","W606 - RESINS, TARS, POLYMER OR TARRY SLUDGE"),
                            ("W609 - OTHER ORGANIC SLUDGE","W609 - OTHER ORGANIC SLUDGE"),
            )
        ),
    )

    WCHazardous= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    WCHazardousComment= forms.ChoiceField(choices=Exemption_CHOICES_RADIO, widget=forms.Select())
    WCHazardousF= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    #WCHazardousFYes= forms.ManyToManyField(EPA_FK, related_name='WasteStream_WasteClassHazardousF')
    WCHazardousK= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    #WCHazardousKYes= forms.ManyToManyField(EPA_FK, related_name='WasteStream_WasteClassHazardousK')
    WCHazardousP= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    #WCHazardousPYes= forms.ManyToManyField(EPA_PU, related_name='WasteStream_WasteClassHazardousP')
    WCHazardousU= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    #WCHazardousUYes= forms.ManyToManyField(EPA_PU, related_name='WasteStream_WasteClassHazardousU')
    WCHazardousIgnitable= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    WCHazardousCorrosive = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    WCHazardousReactive= forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    WCHazardousToxic = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())

    StateWasteCode = forms.CharField(max_length=255)
    UniversalWaste = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    UsedOil = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    TSCARegulatedPCBWaste = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    NonHazardousWaste = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    HazardousWaste = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    RCRAExempt = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    CERCLARegulatedWaste = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    BenzeneNESHAPWaste  = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())
    HalogenatedOrganicCompound = forms.ChoiceField(choices=YESNO_CHOICES_RADIO, widget=forms.RadioSelect())

    OriginCode = forms.ChoiceField(choices=ORIGIN_CHOICES, widget=forms.Select())
    EPAFormCode = forms.ChoiceField(choices=EPAForm_CHOICES, widget=forms.Select())
    WasteDeterminationBasedOn = forms.ChoiceField(choices=WasteDetermination_CHOICES_RADIO, widget=forms.RadioSelect())
    #WCHazardousToxicYes= forms.ManyToManyField(EPA_D, related_name='WasteStream_WasteClassHazardousD')
    #CustomerProfile = forms.ForeignKey(CustomerProfile, on_delete=forms.CASCADE)

    class Meta:
        model = RegulatoryInformation
        fields = '__all__'
        exclude = ('WCHazardousFYes','WCHazardousKYes','WCHazardousPYes','WCHazardousUYes','WCHazardousToxicYes','WCSolidWaste','WCSolidWasteComment',)

class CertificationForm(ModelForm):
    #CertificationGeneratorRepresentativeName= forms.ModelChoiceField(queryset=Personnel.objects.filter(AcctMgr=True).order_by('Name'))
    #CertificationGeneratorTitle= forms.CharField(max_length=4000)
    CertificationGeneratorSignature= forms.CharField(max_length=4000)
    CertificationDate= forms.DateField(input_formats=(settings.DATE_INPUT_FORMATS, ), widget=forms.DateInput(format=settings.DATE_INPUT_FORMATS,attrs={'class': 'inputcalendar2'}))
    #UserId = forms.ForeignKey(User, on_delete=forms.PROTECT)
    #CustomerProfile = forms.ForeignKey(CustomerProfile, on_delete=forms.CASCADE)

    class Meta:
        model = Certification
        fields = '__all__'

        widgets = {
            'CertificationGeneratorRepresentativeName': forms.Select(attrs={'onchange': 'changecontact(this.value)'}),
        }
    def __init__(self, *args, **kwargs):
        Customer_id = kwargs.pop('Customer_id', None)
        super(CertificationForm, self).__init__(*args, **kwargs)
        if Customer_id:
            self.fields['CertificationGeneratorRepresentativeName'].queryset = MasterContact.objects.filter(gen_mastercontact__Customer_id=Customer_id)

class WasteProfileChemicalForm(ModelForm):
    CHEMICAL_UNITTYPE_CHOICES = (
        ("","-----"),
        ("Vol %","Vol %"),
        ("Wt %","Wt %"),
        ("PPM","PPM"),
    )
    ChemicalPhysicalComposion = forms.CharField(max_length=255)
    CAS= forms.CharField(max_length=255)
    Typical= forms.CharField(max_length=255)
    Range= forms.CharField(max_length=255)
    UnitType = forms.ChoiceField(choices=CHEMICAL_UNITTYPE_CHOICES, widget=forms.Select())
    #GeneratingProcessAndMaterialComposition = forms.ForeignKey(GeneratingProcessAndMaterialComposition, on_delete=forms.CASCADE)
    class Meta:
        model = WasteProfileChemical
        fields = '__all__'

""" class WasteProfileShippingPackagingForm(ModelForm):
    ContainerType_CHOICES = (
        ("BA","BA - Burlap, cloth, paper or plastic bags"),
        ("CF","CF - Fiber or plastic boxes, cartons, cases"),
        ("CM","CM - Metal boxes, barrel, kegs, cartons, cases (including roll offs)"),
        ("CW","CW - Wooden boxes, carton, cases"),
        ("CY","CY - Cylinder"),
        ("DF","DF - Fiberboard or plastic drums"),
        ("DM","DM - Metal Drums, barrels, kegs"),
        ("DT","DT - Dump Truck"),
        ("DW","DW - Wooden drums, barrels, keg"),
        ("TP","TP - Tanks portable"),
        ("TT","TT - Tank Truck"),
    )
    ContainerType = forms.CharField(max_length=255)
    ContainerSize= forms.CharField(max_length=255)
    VolumeType = forms.CharField(max_length=255)
    CostBasis= forms.CharField(max_length=255)
    Cost= forms.CharField(max_length=255)
    MinCost=forms.CharField(max_length=255)
    MinCostUnit=forms.CharField(max_length=255)
    #ShippingAndPackagingInformation = forms.ForeignKey(ShippingAndPackagingInformation, on_delete=forms.CASCADE)
    class Meta:
        model = WasteProfileShippingPackaging
        fields = '__all__' """


TESTING = 777
LEARNING = 666

#NBPATTERNS = 8 # 2-back

# Here you choose which learning method to use:

# For faster, but less biologically plausible method (simple node-perturbation
# method, similar to Fiete & Seung 2006), uncomment this:
# It is faster because you only compute the
# Hebbian increment on the few timesteps where a perturbation actually
# occurs.

# RNN parameters
PROBACONN = 1.0;  # Dense connectivity
G = 1.5;   # Early chaotic regime. Chaos ma non troppo.
tau=30.

# Update rule prameters
# For slower, but more biologically plausible method (as described in the paper, see http:#biorxiv.org/content/early/2016/06/07/057729 ):
METHOD = 'DELTAX';  # 'NODEPERT';
ETA = .1;  # Learning rate
MODULTYPE = 'DECOUPLED';  # Modulations (exploratory perturbations) are applied independently to each neuron.
ALPHAMODUL = 16.0;  # Note that TAU = 30ms, so the real ALPHAMODUL is 16.0 * dt / TAU ~= .5
MAXDW = 3e-4;
PROBAMODUL = .003;
RNGSEED = 0;
DEBUG = 0;
ALPHATRACE = .75;
ALPHATRACEEXC = 0.05;
SQUARING = 1;
RANDW = 0;

# Suffix for output files
SUFFIX = "_G" + str(G) + "_MAXDW" + str(MAXDW) + "_ETA" + str(ETA) + "_ALPHAMODUL" + str(ALPHAMODUL) + "_PROBAMODUL" + str(PROBAMODUL) + "_SQUARING" +str(SQUARING) + "_MODULTYPE-" + MODULTYPE + \
    "_ALPHATRACE" + str(ALPHATRACE) + "_METHOD-" + METHOD +  "_ATRACEEXC" + str(ALPHATRACEEXC) + "_TAU" + str(tau) + \
    "_RNGSEED" + \
    str(RNGSEED);


from prometheus_client import Histogram, Counter, Summary, Info
import env

# Default labels
metric_label_keys = ['endpoint', 'app', 'hostname']

# Metrics
AMOUNT_TIMES_CALLED = Counter('amount_times_called', 'Amount of times a function was called', metric_label_keys)
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request', metric_label_keys)
PONG_REQUESTS = Counter('pong_requests_total', 'Total times you hit your PONG', metric_label_keys)
MISS_REQUESTS = Counter('miss_requests_total', 'Total times you missed your PONG', metric_label_keys)

def get_labels_dict(*metric_values):
    labels = {
        "app": env.APP_NAME,
        "hostname": env.HOSTNAME,
    }
    for i, label_value in enumerate(metric_values):
        labels.__setitem__(metric_label_keys[i], label_value)
    return labels


"""
* Python2D Text Drawing Engine
* Currently developed by Frantisek Skladany, under MIT license, compatible with Python 3.x
* Github: biosx32/Python2D-text-drawing-engine
"""

from Control.Console import interact
from Objects.Canvas import Canvas
from random import randint as rnd

if __name__ == "__main__":
    print('::::::::: S T A R T ::::::::::\n')

    f = Canvas(6, 6)
    for i in range(f.width*f.height):
        f.add_point(rnd(0, f.width-1), rnd(0, f.height-1))
    f.show()

    interact()

# vytvori vykreslovanie z kolekcie

# setter a getter pre setovanie pozici a pri  vytvarani objektu

#check coordinates method, if xy in map

dict().items()._abc_cacheis
isinstance


import sys
import validator
import json
import utxo_set
import config
import pending_pool
import pickle
from flask import (
    Flask,
    request
)

app = Flask(__name__)

@app.route('/transaction/new', methods=['POST'])
def submit_tx():
    if request.method == 'POST':
        next_transaction = request.get_data()
        next_transaction = next_transaction.decode('utf-8')
        pending_pool.database(next_transaction)
        return(str(next_transaction))

@app.route('/transaction/pending', methods=['GET'])
def pending_transaction():
    if request.method == 'GET':
        data = pending_pool.get_data('pool.pickle')
        if data != False:
            return str(data)
        else:
            return("Something get wrong!\nYou have no transaction in pool")

@app.route('/chain', methods=['GET'])
def get_chain():
    if request.method == 'GET':
        data = pending_pool.get_data('blockchain.pickle')
        if data != False:
            if (type(data) == str):
                data = json.loads(data)
            return str(data)
        else:
            return("Something get wrong!\nYou have no chain")

@app.route('/nodes', methods=['GET'])
def list_of_nodes():
    if request.method == 'GET':
        data = pending_pool.read_nodes_from_file('node')
        ret = ''.join(data)
        if data != False:
            return (ret)
        else:
            return("Something get wrong!\nYou have no nodes")

@app.route('/chain/length', methods=['GET'])
def chain_length():
    if request.method == 'GET':
        data = pending_pool.get_data('blockchain.pickle')
    if data != False:
        if type(data) is dict:
            lens = str(data['chain'][0]['height'])
        else:
            lens = str(data.chain[0].height)
        return lens
    else:
        return("0")

@app.route('/block/last', methods=['GET'])
def last_block():
    if request.method == 'GET':
        data = pending_pool.get_data('blockchain.pickle')
        if data != False:
            if type(data) is dict:
                block = data['chain'][0]
            else:
                block = data.chain[0]
            return str(block)
        else:
            return("Something get wrong!\nYou have no chain")

@app.route('/block', methods=['GET'])
def block_height():
    if request.method == 'GET':
        height = str(request.args.get('height'))
        data = pending_pool.get_data('blockchain.pickle')
        if data != False:
            i = len(data.chain) - 1
            while(i >= 0):
                if (int(data.chain[i].height) == int(height)):
                    return str(data.chain[i])
                i -= 1
            return "No such block!"
        else:
            return "There no chain data!"

@app.route('/balance', methods=['GET'])
def get_balance():
    if request.method == 'GET':
        addr = str(request.args.get('addr'))
        setik = utxo_set.get_utxo_set("Pitcoin", addr)
        balance = utxo_set.check_balance(setik)
        return('
Your balance is ' + str(balance) + ' satoshi
')

@app.route('/utxo', methods=['GET'])
def utxo():
    if request.method == 'GET':
        data = pending_pool.get_data('utxo.pickle')
        if data != False:
            return str(data)
        else:
            return("Something get wrong!\nYou have no utxo in pool")

@app.route('/block/new', methods=['POST'])
def receive_new_block():
    if request.method == 'POST':
        validator.mining(0)
        block = request.form
        if validator.block(block) == True:
            validator.consensus()
        validator.mining(1)
        return "OK"
    return ("OK")

@app.route('/getDifficulty', methods=['GET'])
def get_difficulty():
    if request.method == 'GET':
        data = pending_pool.get_data('blockchain.pickle')
        if data != False:
            diff = data.diff
            return str(diff)
        else:
            return("Something get wrong!\nYou have no chain")

def main():
    ip = pending_pool.read_nodes_from_file()
    if ip == False:
        PORT = input("Choose port\n")
        try:
            val = int(PORT)
            try:
                fd = open("port.txt", 'w')
                fd.write(PORT)
                fd.close()
                pending_pool.add_node_to_file("0.0.0.0:" + PORT)
                app.run(host='0.0.0.0', port=PORT, debug=False)
                print("!!!Server port was saved in a file port.txt!!!")
            except IOError:
                print("IOError")
        except (ValueError, PermissionError, OverflowError) as e:
            print("Invalid port, stupid")
    else:
        app.run(host=ip[0][:7], port=ip[0][8:13], debug=False)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

from activity_recognition_engine import ActivityRecognitionEngine
from gabriel_server.local_engine import runner
import logging

INPUT_QUEUE_MAXSIZE = 60
PORT = 9099
TOKENS = 2

logging.basicConfig(level=logging.INFO)

def main():
    def engine_setup():
        return ActivityRecognitionEngine()

    runner.run(engine_setup, ActivityRecognitionEngine.ENGINE_NAME, INPUT_QUEUE_MAXSIZE, PORT, TOKENS)

if __name__ == '__main__':
    main()


# coding=utf-8
import re
import requests
from requests.exceptions import ConnectionError

"""
爬虫api：
    搜索结果页：get_index_result(search)
    小说章节页：get_chapter(url)
    章节内容：get_article(url)
"""
class DdSpider(object):

    def __init__(self):
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'}

    def parse_url(self, url):
        try:
            resp = requests.get(url, headers=self.headers)
            if resp.status_code == 200:
                # 处理一下网站打印出来中文乱码的问题
                resp.encoding = 'utf-8'
                return resp.text
            return None
        except ConnectionError:
            print('Error.')
        return None

    # 搜索结果页数据
    def get_index_result(self, search):
        url = 'http://zhannei.baidu.com/cse/search?q={search}&p={page}&s=1682272515249779940&entry=1'.format(
            search=search, page=0)
        resp = self.parse_url(url)
        resp = re.sub(r'', '', resp)
        resp = re.sub(r'', '', resp)
        p = re.compile(r'<a cpos="img".*?<img src="(.*?)".*?'
                       + r'(.*?).*?
(.*?)'
                       + r'
.*?<span.*?(.*?).*?title">(.*?).*?title">(.*?)', re.S)
        items = re.findall(p, resp)
        for i in items:
            data = {
                'image': i[0],
                'url': i[1],
                'title': i[2].strip(),
                'profile': i[3].strip().replace('\u3000', '').replace('\n', ''),
                'author': i[4].strip(),
                'style': i[5].strip(),
                'time': i[6].strip()
            }
            yield data

    # 小说章节页数据
    def get_chapter(self, url):
        resp = self.parse_url(url)
        p = re.compile(r'
 (.*?)
')
        chapters = re.findall(p, resp)
        for i in chapters:
            data = {
                'url': str(url) + i[0],
                'chapter': i[1]
            }
            yield data

    # 章节内容页数据
    def get_article(self, url):
        resp = self.parse_url(url)
        p = re.compile(r'
(.*?)
', re.S)
        article = re.findall(p, resp)
        # 文章中的'
'标签先不去除，后面在模版中使用
        return article[0].strip()

# dd = DdSpider()
# print(dd.get_article('http://www.23us.cc/html/158/158120/8275025.html'))


import ROOT

try:
    import cPickle as pickle
except ImportError:
    import pickle

def main():
    with open("trigger_efficiency_eff.pkl", 'r') as f:
        x, y, yerr = pickle.load(f)
    graph = ROOT.TGraphAsymmErrors(len(x))
    for i in range(len(x)):
        graph.SetPoint(i, x[i], y[i])
        graph.SetPointEYhigh(i, yerr[0,i])
        graph.SetPointEYlow(i, yerr[1,i])
        erf = ROOT.TF1("erf", "0.5*[0]*(1 + TMath::Erf((x-[1])/(TMath::Sqrt(2)*[2])))", 0, 500)
    erf.SetParameter(0, 1)
    erf.SetParameter(1, 150)
    erf.SetParameter(2, 10)
    graph.Fit(erf)
    with open("fit_results.pkl", 'w') as f:
        p0 = erf.GetParameter(0)
        if p0 > 1.:
            p0 = 1.
        pickle.dump((1, erf.GetParameter(1), erf.GetParameter(2)), f)

if __name__ == "__main__":
    main()


from __future__ import print_function
import os
import argparse
import torch
import json
import csv
import shutil
import torch.backends.cudnn as cudnn
import numpy as np
from data import cfg_mnet, cfg_re50, cfg_mnet_highway
from layers.functions.prior_box import PriorBox
from utils.nms.py_cpu_nms import py_cpu_nms
import cv2
# from models.retinaface_highway import RetinaFace
from utils.box_utils import decode, decode_landm
from utils.timer import Timer
from utils.model_parse import  mask_decorater
from utils.filter_pruner import filter_pruner
from utils.graph_from_trace import VisualGraph

parser = argparse.ArgumentParser(description='Retinaface')
parser.add_argument('-m', '--trained_model', default='./weights/mobilenet0.25_Final.pth')
parser.add_argument('--network', default='mobile0.25', help='Backbone network mobile0.25, resnet50, or mobile0.25_highway')
parser.add_argument('--cpu', action="store_true", default=False, help='Use cpu inference')
# parser.add_argument('--sparsity_file', required=True, help='config file for the pruner')
args = parser.parse_args()

if args.network == 'mobile0.25_highway':
    args.trained_model = './weights/scratch_epoch_250.pth.tar'

cfg = None
if args.network == "mobile0.25":
    cfg = cfg_mnet
elif args.network == "resnet50":
    cfg = cfg_re50
elif args.network == "mobile0.25_highway":
        cfg = cfg_mnet_highway

def check_keys(model, pretrained_state_dict):
    ckpt_keys = set(pretrained_state_dict.keys())
    model_keys = set(model.state_dict().keys())
    used_pretrained_keys = model_keys & ckpt_keys
    unused_pretrained_keys = ckpt_keys - model_keys
    missing_keys = model_keys - ckpt_keys
    print('Missing keys:{}'.format(len(missing_keys)))
    print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
    print('Used keys:{}'.format(len(used_pretrained_keys)))
    assert len(used_pretrained_keys) > 0, 'load NONE from pretrained checkpoint'
    return True

def remove_prefix(state_dict, prefix):
    ''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
    print('remove prefix \'{}\''.format(prefix))
    f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
    return {f(key): value for key, value in state_dict.items()}

def load_model(model, pretrained_path, load_to_cpu):
    print('Loading pretrained model from {}'.format(pretrained_path))
    if load_to_cpu:
        pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
    else:
        device = torch.cuda.current_device()
        pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
    if "state_dict" in pretrained_dict.keys():
        pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
    else:
        pretrained_dict = remove_prefix(pretrained_dict, 'module.')
    check_keys(model, pretrained_dict)
    model.load_state_dict(pretrained_dict, strict=False)
    return model

def forward_hook(module, inputs, output):
    if hasattr(module, 'flops'):
        return
    input=inputs[0]
    g = module.groups
    n, c, _, _ = input.size()
    _, _, h,w= output.size()
    k1, k2 = module.kernel_size
    flops = (1*c*k1*k2 ) * h * w * module.out_channels / g
    module.flops = flops

def get_flops(net, data):
    Flops = {}
    total_flops = 0
    for name, layer in net.named_modules():
        if isinstance(layer, torch.nn.Conv2d):
            layer.register_forward_hook(forward_hook)
    net(data)
    for name, layer in net.named_modules():
        if isinstance(layer, torch.nn.Conv2d):
            total_flops += layer.flops
            Flops[name] = layer.flops
    print('TOTAL FLOPS', total_flops)
    return Flops

def load_sparsity(filepath):
    jf = open(filepath, 'r')
    tmp_cfg = json.load(jf)
    jf.close()
    for c in json.loads(tmp_cfg["config_list"]):
        print(c['op_names'][0])
        print(c['sparsity'])
        prune_cfg[c['op_names'][0]] = 1.0 - c['sparsity']

def flops_traverse(node, ratio):
    #print(node)
    if node in gf.c2py and gf.c2py[node].isOp and node.kind()=='aten::_convolution':
        # print(node)
        name = gf.c2py[node].name
        if name not in in_prune_ratio:
            in_prune_ratio[name] = ratio
        else:
            in_prune_ratio[name] = max(ratio, in_prune_ratio[name])
        ratio = 1.0
        if name in prune_cfg:
            ratio = prune_cfg[name]
    if node in gf.forward_edge:
        for _next in gf.forward_edge[node]:
            flops_traverse(_next, ratio)

if __name__ == '__main__':
    performances = {}
    flops = {}

    sparsities = [0.1, 0.3, 0.5, 0.7, 0.9]
    pruners = ['NetAdaptPruner005', 'SimulatedAnnealingPruner']
    for pruner in pruners:
        performances[pruner] = []
        flops[pruner] = []
        for sparsity in sparsities:
            with open(os.path.join('experiment_data/archive640/', pruner, str(sparsity).replace('.', ''), 'performance.json'), 'r') as jsonfile:
                performance = json.load(jsonfile)
                performances[pruner].append(performance['finetuned'])

            sparsity_file = os.path.join('experiment_data/archive640/', pruner, str(sparsity).replace('.', ''), 'search_result.json')

            # FLOPS calc
            # load pre-trained model
            if args.network == "mobile0.25":
                from models.retinaface import RetinaFace
            elif args.network == "mobile0.25_highway":
                from models.retinaface_highway import RetinaFace
            net = RetinaFace(cfg=cfg)
            net = load_model(net, args.trained_model, args.cpu)

            print('Finished loading model!')
            #print(net)
            cudnn.benchmark = False
            device = torch.device("cpu" if args.cpu else "cuda")
            net = net.to(device)
            # data = torch.rand(1, 3, 560, 1024)
            data = torch.rand(1,3, 640, 480)
            data = data.to(device)
            gf = VisualGraph(net,data)
            FLOPS = get_flops(net, data)
            pruned_flops = 0.0
            visited = set()
            in_prune_ratio = {}
            prune_cfg = {}

            load_sparsity(sparsity_file)
            for input in gf.graph.inputs():
                if input.type().kind() == 'ClassType':
                    continue
                flops_traverse(input, 1.0)
            for name in FLOPS:
                #print(prune_cfg[name])
                #print(in_prune_ratio.keys())
                remained = 1.0
                if name in prune_cfg:
                    remained = prune_cfg[name]
                # print(name)
                if name not in in_prune_ratio:
                    in_prune_ratio[name] = 1
                pruned_flops += FLOPS[name] * remained * in_prune_ratio[name]

            flops[pruner].append(int(pruned_flops))

    for pruner in pruners:
        with open(os.path.join('experiment_data/flops_{}.csv'.format(pruner)), 'w+') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(['accuracy', 'flops', 'sparsity'])
            for idx, performance in enumerate(performances[pruner]):
                writer.writerow([performance, flops[pruner][idx], sparsities[idx]])


# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request

from AokaiSpider.items import CostPriceItemLoader, CostPriceItem
import time
from AokaiSpider.utils import zhesu

class CostAbsSpider(scrapy.Spider):
    name = 'cost-abs'
    breed = "ABS"
    allowed_domains = ['www.ex-cp.com']
    start_urls = ["http://www.ex-cp.com/plastic/list-16-1.html"]
    total_pages = 0
    strat_page = 1

    def parse(self, response):
        """
        """
        post_nodes = response.css(".catlist_li")
        cur_date = time.strftime('%Y-%m-%d', time.localtime(time.time()));
        for post_node in post_nodes:
            post_url = post_node.css("a::attr(href)").extract_first("")
            post_date = "2018-" + zhesu.date_time_convert(post_node.css("a::attr(title)").extract_first(""))
            if post_date == cur_date:
                yield Request(url=post_url, callback = self.parse_detail)

        # if self.total_pages == 0:
        #     common_url = response.css("#destoon_previous ::attr(value)").extract_first("")
        #     self.total_pages = int(self.get_page(common_url.strip()))
        #
        # c_page = 1 if int(self.get_page(response.url)) == None else int(self.get_page(response.url))
        # if c_page < self.total_pages:
        #     next_url = self.breed_url+ str(c_page + 1)+".html"
        #     print("ABS出厂价URL:" + next_url)
        #     yield Request(url=next_url, callback=self.parse)

    def parse_detail(self, response):
        print("current_detail_page:" + response.url)
        trs = response.css("tbody tr")
        date_time_str = "2018-" + zhesu.date_time_convert(response.css("#title::text").extract_first(""))
        date_time = int(time.mktime(time.strptime(date_time_str,'%Y-%m-%d')))
        for tr in trs[1:]:
            tds = tr.css("td")
            cost_item = CostPriceItem()
            cost_item["breed"] = self.breed
            cost_item["spec"] = tds[0].css("td::text").extract_first("").strip()
            cost_item["brand"] = tds[1].css("td::text").extract_first("").strip()
            cost_item["area"] = zhesu.area_convert(tds[2].css("td::text").extract_first("").strip())
            cost_item["price"] = tds[3].css("td::text").extract_first("").strip()
            cost_item["updown"] = tds[4].css("td::text").extract_first("").strip()
            cost_item["product_unit"] = "元/吨"
            cost_item["release_date"] = date_time*1000
            cost_item["release_date_str"] = date_time_str
            if cost_item["spec"] == None or cost_item["brand"] == None or cost_item["spec"] == "" or cost_item["brand"] == "" \
                or cost_item["area"] == None or cost_item["area"] == "" :
                continue
            yield cost_item

    def get_page(self, url):
        url = url[url.rindex("/") + 1:]
        attrs = url.replace(".html", "").split("-")
        return  attrs[len(attrs) - 1]


# coding: utf-8

# # Web crawling
#
# The following content heavily draws on [Web Scraping with Python](https://proquest.safaribooksonline.com/book/programming/python/9781491985564) (2018) by Ryan Mitchell.
#
# - First, we learn how create a list of URLs from a webpage.
# - Second, we learn how to crawl through the list of these URLs.

# ## Web crawling
#
# The idea behind web scrawling is recursion. Instead of scraping one web page, we do the same process over and over different web pages (or websites) using Uinform Resource locations (or URLs). Since web crawling substantially increases work load of the target's server, do this very carefully again within *technical*, *legal*, and *ethical* boundaries.

# ### Create a list of URLs
#
# Since we're studying computational methods, let's look at the list of past winners of Turing Award -- the computer science equivalent to Nobel prizes and collect links from the URL.
#

# To begin with, let's find out whether we can gain access to the web page.

# In[1]:

from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.error import URLError

try:
    page = urlopen('https://en.wikipedia.org/wiki/Turing_Award')
except HTTPError as e:
    print(e)
except URLError as e:
    print("The server is broken")
else:
    print("The site is working")

# Then, let's scrap the table and the find the element (the URLs of the prize winners) we need.

# In[2]:

from bs4 import BeautifulSoup

soup = BeautifulSoup(page, "html.parser")

wiki_table = soup.find('table',{'class':'wikitable'})

# Find some patterns we can exploit.

# In[3]:

import re

wiki_table.find_all('tr')[1].find_all('td')[0].find('a').get('href') # first winner
wiki_table.find_all('tr')[2].find_all('td')[0].find('a').get('href') # second winner

# Calculate the total number of prize winners. Remember each row in the table contains information about a prize winner.

# In[4]:

winners=len(wiki_table.find_all('tr'))

# In[5]:

links = []

for i in range(0,winners-1):
    if i <= winners:
        links.append(wiki_table.find_all('tr')[i+1].find_all('td')[0].find('a').get('href'))
        print(links[i]) # to check whether we get the right list
    else:
        print("Something is wrong") # for debugging

# **Challenge**
#
# But we have one problem. The links are not the full URLs we look for. We want "https://en.wikipedia.org/wiki/Alan_Perlis" not "wiki/Alan_Perlis". How can you fix this?

# In[6]:

''.join(["https://en.wikipedia.org",links[0]]) # using join method

# In[7]:

new_links = []

for i in range(0,winners-1):
    if i <= winners-1:
        new_links.append(''.join(["https://en.wikipedia.org",links[i]]))
        print(new_links[i]) # to check whether we get the right list
    else:
        print("Something is wrong") # for debugging

# ### Crawl through the list

# Let's extract the birth information from each of the prize winner's Wikipage.

# In[8]:

try:
    test_page = urlopen(new_links[0])
except HTTPError as e:
    print(e)
except URLError as e:
    print("The server is broken")
else:
    print("The site is working")

test_soup = BeautifulSoup(test_page, "html.parser")
test_infobox = test_soup.find("table",{"class":"vcard"})

test_infobox.find(text = re.compile("Born")).find_next().text

# Now we loop the code over the list.

# In[9]:

birth_list = []

for url in new_links:
    req = urlopen(url)
    page = BeautifulSoup(req, 'html.parser')
    birth = page.find("table",{"class":"vcard"}).find(text = re.compile("Born")).find_next().text
    birth_list.append(birth)

# In[10]:

birth_list[0:winners-1]

##birth list cleaned
##regular expressions

birth_list_cleaned=[]

for element in birth_list:
    birth_list_cleaned.append(re.findall('[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]',element))


# -*- coding: utf-8 -*-
import scrapy
import re
import time
from copy import deepcopy

class GupiaoSpider(scrapy.Spider):
    name = 'gupiao'
    allowed_domains = ['jin10.com', "view.jin10.com"]
    time_format = '%Y-%m-%d %X'
    now_time = time.strftime(time_format, time.localtime())
    start_urls = ['https://view.jin10.com/flash?max_time={}'.format(now_time)]

    def parse(self, response):
        data = response.body.decode()
        data = re.findall(r'.*?\((.*?)\);', data, re.S)[0]
        mess = re.findall(r'\{"data":\[(.*?)\],.*\}', data, re.S)[0]
        time_show = re.findall(r'"time_show":"(.*?)",?', mess, re.S)[-1:][0]
        for a in range(60): ()
        item = {}
        mess_a = re.findall(r'\{(.*?)\}', mess)[a]
        if mess_a is not None:
            item['id'] = re.findall(r'"id":"(\w+)",?', mess_a, re.S)[0]
            item['title_content'] = re.findall(r'"title_content":"(.*?)",?', mess_a, re.S)
            item['time_show'] = re.findall(r'"time_show":"(.*?)",?', mess_a, re.S)[0]
            item['url'] = re.findall(r'"url":"(.*?)",?', mess_a, re.S)[0]
        next_url = 'https://view.jin10.com/flash?max_time={}'.format(time_show)
        print(item)
        yield scrapy.Request(
            next_url,
            callback=self.parse,
            meta={"item": deepcopy(item)}
        )


from xai.brain.wordbase.verbs._drag import _DRAG

#calss header
class _DRAGGED(_DRAG, ):
	def __init__(self,):
		_DRAG.__init__(self)
		self.name = "DRAGGED"
		self.specie = 'verbs'
		self.basic = "drag"
		self.jsondata = {}


from tensorflow.keras import models
from tensorflow.keras.layers import Dense, Input, GlobalMaxPool1D, BatchNormalization, Conv1D, Add, ReLU

class CNNR:
    def __init__(self, name, output_dim, loss, last_activation):
        self.name = name
        self.output_dim = output_dim
        self.loss = loss
        self.last_activation = last_activation
        self.model = self.get_model()
        self.compile_model()

    def get_model(self):
        X_input = Input(shape=(187, 1))
        X = Conv1D(32, kernel_size=5, activation='relu')(X_input)

        X_after = Conv1D(32, kernel_size=5, activation='relu', padding='same')(X)
        X_shortcut = X
        X = self.residual_block_32(X)
        X = self.residual_block_32(X)
        X = self.residual_block_32(X)
        X = self.residual_block_32(X)

        X = Conv1D(32, kernel_size=5, activation='relu', padding='same')(X)
        X = Add()([X, X_after])
        X = BatchNormalization()(X)
        X = Conv1D(256, kernel_size=3, activation='relu')(X)
        transfer_layer = GlobalMaxPool1D()(X)
        X = Dense(64, activation='relu', name="dense_1")(transfer_layer)
        X = Dense(64, activation='relu', name="dense_2")(X)
        X_out = Dense(self.output_dim, activation=self.last_activation, name='output_layer')(X)

        model = models.Model(inputs=X_input, outputs=X_out)
        return model

    def compile_model(self):
        self.model.compile(optimizer=optimizers.Adam(0.001), loss=self.loss, metrics=['accuracy'])

    def residual_block_16(self, X):
        X_shortcut = X
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(16, kernel_size=3, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(16, kernel_size=3, padding = 'same')(X)
        #X = GlobalMaxPool1D()(X)
        X = Add()([X, X_shortcut])
        return X

    def residual_block_32(self, X):
        X_shortcut = X
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(32, kernel_size=3, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(32, kernel_size=3, padding = 'same')(X)
        #X = GlobalMaxPool1D()(X)
        X = Add()([X, X_shortcut])
        return X

    def residual_block_32_up(self, X):
        X_shortcut = Conv1D(32, kernel_size=1, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(32, kernel_size=3, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(32, kernel_size=3, padding = 'same')(X)
        #X = GlobalMaxPool1D()(X)
        X = Add()([X, X_shortcut])
        return X

    def residual_block_64(self, X):
        X_shortcut = X
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(64, kernel_size=3, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(64, kernel_size=3, padding = 'same')(X)
        X = Add()([X, X_shortcut])
        return X

    def residual_block_64_up(self, X):
        X_shortcut = Conv1D(64, kernel_size=1, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(64, kernel_size=3, padding = 'same')(X)
        X = BatchNormalization()(X)
        X = ReLU()(X)
        X = Conv1D(64, kernel_size=3, padding = 'same')(X)
        #X = GlobalMaxPool1D()(X)
        X = Add()([X, X_shortcut])
        return X


# -*- coding: utf-8 -*-

import sys
reload(sys)
sys.setdefaultencoding('UTF8')

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import netcdf_file as netcdf
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import math

#lonb1 = -77.5
lonb1 = -76.5
lonb2 = -65.5
latb1 = -57
#latb1 = -65
latb2 = -15

#fig = plt.figure(figsize=[12, 7.8])
fig = plt.figure()
ax = plt.subplot(111)

#box = ax.get_position()
#ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

cl_map = Basemap(projection='cyl', llcrnrlat = latb1, urcrnrlat = latb2, llcrnrlon = lonb1, urcrnrlon = lonb2, resolution = 'i')

cl_map.drawparallels(np.arange(-60, 51, 2.5), labels=[1,0,0,0], fontsize = 8, xoffset = .2, color = 'gray', dashes=[1,4], linewidth = .3)
cl_map.drawmeridians(np.arange(11, 360., 3), labels=[0,0,0,1], fontsize = 8, yoffset = .2, color = 'gray', dashes=[1,4], linewidth = .3)
#cl_map.drawcountries(linewidth = .5, color = '.3', zorder = 3)
#cl_map.drawcoastlines(linewidth = .5, color = '.3', zorder = 3)
#cl_map.drawstates(linewidth = .5, color = '.3', zorder = 3)

#cl_map.readshapefile('CHL_adm/CHL_adm0', 'chile_country', linewidth=0.05)
cl_map.readshapefile('CHL_adm/CHL_adm1', 'chile_regions', drawbounds=False)

patches = []
for info, shape in zip(cl_map.chile_regions_info, cl_map.chile_regions):
    patches.append( Polygon(np.array(shape), True) )
ax.add_collection(PatchCollection(patches, facecolor= '#FFCC99', edgecolor='k', linewidths=.3, zorder=2))

nc = netcdf('ETOPO1_Ice_c_gdal.grd', 'r')
dim = nc.variables['dimension'][:]
delta = nc.variables['spacing'][0]
z = nc.variables['z'][:]

lon0 = np.arange(-180,180,delta)
lat0 = np.arange(90-delta/2,-90,-delta)
zgrid = np.zeros([dim[1], dim[0]])
for y in np.arange(dim[1]):
    zgrid[y,:] = z[(dim[0]*y):(dim[0]*(y+1))]

zgrid = zgrid[6000:9000,5000:7000]
lat0 = lat0[6000:9000]
lon0 = lon0[5000:7000]

#fig = plt.figure(figsize=[12, 7.8])

lons, lats = np.meshgrid(lon0, lat0)
x, y = cl_map(lons, lats)
#cs = cl_map.contourf(x, y, zgrid, [0, 1500], colors=('w', '.8', '.6'), extend = 'both')

#cl_map.shadedrelief()

#plon = -76
#plon = -80
plon = lonb1 + 2

easter_lat = -27.15
rec = plt.Rectangle((plon - .4, easter_lat - .4), .8, .8, ec = 'k', lw = .5, fc = 'w', zorder = 3)
ax.add_patch(rec)
plt.arrow(plon - .4, easter_lat, -.6, 0, head_width = 0.3, head_length = 0.2, lw = .5, fc='k', ec='k')
#plt.text(plon, -27.95, 'I. de Pascua', size = 8, ha = 'center', va = 'center')

juan_lat = -33.66667
rec = plt.Rectangle((plon - .4, juan_lat - .4), .8, .8, ec = 'k', lw = .5, fc = 'w', zorder = 3)
ax.add_patch(rec)
plt.arrow(plon - .4, juan_lat, -.6, 0, head_width = 0.3, head_length = 0.2, lw = .5, fc='k', ec='k')
#plt.text(-78.98333, -34.44, 'J. Fdez', size = 8, ha = 'center', va = 'center')

frei_lat = -54.5
rec = plt.Rectangle((plon - .4, frei_lat - .4), .8, .8, ec = 'k', lw = .5, fc = 'w', zorder = 3)
ax.add_patch(rec)
plt.arrow(plon, frei_lat - .4, 0, -1, head_width = 0.3, head_length = 0.2, lw = .5, fc='k', ec='k')
#plt.text(plon, -51, 'Antartica', size = 8, ha = 'center', va = 'center')

#axin = inset_axes(ax, width=1.5, height=1.5, loc=8)
"""
axin = inset_axes(ax, width=1.5, height=1.5, bbox_to_anchor=(1, 1), bbox_transform=ax.transAxes)
inmap = Basemap(ax=axin,width=1200000*2,height=900000*2,projection='lcc',resolution=None,lat_1=-70,lat_0=-70,lon_0=-65.)
inmap.shadedrelief()
inmap.drawparallels(np.arange(-90, 90, 5), labels=[0,0,1,0], fontsize = 7, xoffset = .2, color = 'gray', dashes=[1,1], linewidth = .3)
inmap.drawmeridians(np.arange(0, 360., 30), labels=[0,0,0,1], fontsize = 7, yoffset = .2, color = 'gray', dashes=[1,1], linewidth = .3)
"""

#ax = plt.axes([.04, .02, .1, .2])

#ax = plt.axes([.45, .01, .1, .2])
#m = Basemap(width=1200000*2,height=900000*2,projection='lcc',resolution=None,lat_1=-70,lat_0=-70,lon_0=-65.)
#m.shadedrelief()
#m.fillcontinents(color = '.6', lake_color = 'w')
#m.drawparallels(np.arange(-90, 90, 30), labels=[1,0,0,0], fontsize = 7, xoffset = .2, color = 'gray', dashes=[1,1], linewidth = .3)
#m.drawmeridians(np.arange(0, 360., 30), labels=[0,0,0,1], fontsize = 7, yoffset = .2, color = 'gray', dashes=[1,1], linewidth = .3)

stations = np.array([
    [-18.33333, -70.33333, '#3399CC', 'Arica', 'left', 2.8, 0],
    [-18.53333, -70.31667, '#FF6600', None, 'right', 0, 0],
    [-20.53972, -70.17861, 'green', 'Iquique', 'left', 3.7, 0],
    [-22.91083, -68.2, '#FF6600', 'San Pedro de Atacama', 'left', 9.3, 0],
    [-23.45028, -70.44111, 'green', 'Antofagasta', 'right', -5.5, 0],
    #[-27.16056, -109.4272, 'green'],
    [easter_lat, plon, 'green', 'Isla de Pascua', 'right', -6.1, 0],
    [-27.26389, -70.77417, '#FF6600', 'Caldera', 'left', 3.8, 0],
    [-29.9175, -71.20111, 'green', 'La Serena', 'left', 4.6, 0],
    [-33.03833, -71.62917, 'green', 'Valparaiso', 'right', -4.7, 3],
    [-33.35, -70.29361, '#FF6600', 'Farellones', 'left', 4.6, 1.5],
    [-33.445, -70.68278, 'green', 'Santiago', 'left', 4.2, 0],
    #[-33.66667, -78.98333, '#3399CC', 'Juan Fernandez', 'right', -15, 0],
    [juan_lat, plon, '#3399CC', 'Juan Fernandez', 'right', -6.6, 1.5],
    [-34.17389, -70.77694, '#FF6600', 'Rancagua', 'right', -4.5, 0.25],
    [-34.96667, -71.23333, '#3399CC', 'Curico', 'left', 3.3, 0],
    [-35.41972, -71.66944, '#FF6600', 'Talca', 'right', -2.8, 0],
    [-36.56667, -72.03333, '#3399CC', 'Chillan', 'left', 3.4, 0],
    [-36.77833, -73.0625, 'green', 'Concepcion', 'right', -5.1, 0],
    [-36.90361, -71.41, '#FF6600', None, 'left', 1, 0],
    [-38.70306, -72.54917, '#FF6600', None, 'left', 1, 0],
    [-38.75, -72.63333, '#3399CC', 'Temuco', 'right', -3.8, 0],
    [-39.61667, -73.08333, '#3399CC', 'Valdivia', 'left', 3.8, 0],
    [-39.81389, -73.24806, '#FF6600', None, 'right', -1, 0],
    [-40.6, -73.05, '#3399CC', 'Osorno', 'left', 3.7, -1],
    [-41.435, -73.09778, 'green', 'Puerto Montt', 'right', -5.7, 0],
    [-45.59389, -72.10861, 'green', 'Coyhaique', 'right', -4.8, 0],
    [-45.91667, -71.68333, '#3399CC', 'Balmaceda', 'left', 5, 0],
    [-53.00333, -70.845, 'green', 'Punta Arenas', 'left', 5.8, 0],
    #[-62.19306, -58.9825, 'green'],
    [frei_lat, plon, 'green', 'C.M.A. Edo. Frei', 'right', -6.6, 0],
    ])

points = list()
for station in stations:

    #colors = ('#243F93', '#EC663B')
    colors = ('#3399CC', '#FF6600')

    """
    if station[2] not in colors:
        xy = []
        start = 0.75
        for ratio in (0.5, 0.5):
            x = [0] + np.cos(np.linspace(2*math.pi*start,2*math.pi*(start+ratio), 30)).tolist()
            y = [0] + np.sin(np.linspace(2*math.pi*start,2*math.pi*(start+ratio), 30)).tolist()
            xy1 = zip(x,y)
            xy.append(xy1)
            start += ratio
        for i, xyi in enumerate(xy):
            point = plt.scatter(station[1], station[0], marker=(xyi,0), s = 15, lw = .2, zorder = 4, color=colors[i])
    else:
        point = plt.scatter(station[1], station[0], s = 45, lw = .2, zorder = 4, color=station[2])
    """
    point_b = plt.scatter(station[1], station[0], s = 20, lw = .2, zorder = 4, color='black')

    if station[3]:
        if station[4] == 'left':
            text_x = lonb2 + 3
        elif station[4] == 'right':
            text_x = lonb1 - 3

        if station[2] not in colors:
            xy = []
            start = 0.75
            for ratio in (0.5, 0.5):
                x = [0] + np.cos(np.linspace(2*math.pi*start,2*math.pi*(start+ratio), 30)).tolist()
                y = [0] + np.sin(np.linspace(2*math.pi*start,2*math.pi*(start+ratio), 30)).tolist()
                xy1 = zip(x,y)
                xy.append(xy1)
                start += ratio
            for i, xyi in enumerate(xy):
                point = plt.scatter(text_x + station[5], station[0] + .2 + station[6], marker=(xyi,0), s = 180, lw = .2, zorder = 4, color=colors[i], clip_on=False)
        else:
            point = plt.scatter(text_x + station[5], station[0] + .2 + station[6], s = 180, lw = .2, zorder = 4, color=station[2], clip_on=False)

        points.append(point)

        ax.annotate(
            station[3],
            (station[1], station[0]),
            xytext=(text_x, station[0] + station[6]),
            #textcoords='offset points',
            ha=station[4],
            fontsize=10,
            bbox={'boxstyle':'square,pad=0.5','fc':'#999999', 'linewidth':0},
            arrowprops={'arrowstyle': '-', 'linewidth': .5}
            )

"""
ax.legend(tuple(points),
           ('Estacion climatologica', 'Estacion UV', 'Estacion climatologica UV'),
           scatterpoints=1,
           loc='upper left',
           ncol=1,
           fontsize=12,
           bbox_to_anchor=(1, 1)
           )
"""

ax.legend(tuple(points),
            ('Estacion climatologica', 'Estacion climatologica UV', 'Estacion UV'),
            scatterpoints=1,
            loc='upper center',
            ncol=1,
            fontsize=12,
            bbox_to_anchor=(0.5, 1.07),
            fancybox=True,
            shadow=True
            )

mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()


import tensorflow as tf
import gym
import joblib
import os
import os.path as osp
from spinup.utils.logx import restore_tf_graph

def load_policy(fpath, itr='last', deterministic=False):

    # handle which epoch to load from
    if itr=='last':
        saves = [int(x[11:]) for x in os.listdir(fpath) if 'simple_save' in x and len(x)>11]
        itr = '%d'%max(saves) if len(saves) > 0 else ''
    else:
        itr = '%d'%itr

    # load the things!
    sess = tf.Session()
    model = restore_tf_graph(sess, osp.join(fpath, 'simple_save'+itr))

    # get the correct op for executing actions
    if deterministic and 'mu' in model.keys():
        # 'deterministic' is only a valid option for SAC policies
        print('Using deterministic action op.')
        action_op = model['mu']
    else:
        print('Using default action op.')
        action_op = model['pi']

    # make function for producing an action given a single state
    get_action = lambda x : sess.run(action_op, feed_dict={model['x']: x[None,:]})[0]

    # try to load environment from save
    # (sometimes this will fail because the environment could not be pickled)
    try:
        state = joblib.load(osp.join(fpath, 'vars'+itr+'.pkl'))
        env = state['env']
    except:
        env = None

    return env, get_action

if __name__ == '__main__':

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--env', type=str, default='BipedalWalkerHardcore-v2')
    parser.add_argument('--render', type=bool, default=True)
    args = parser.parse_args()

    file_model = '/home/liu/project/DRL/saved_models/biped_sac1_stump6_actnoise0.3_alphaauto_2/biped_sac1_stump6_actnoise0.3_alphaauto_2_s0'
    print(file_model)

    _env, get_action = load_policy(file_model, deterministic=True)

    test_env = gym.make(args.env)
    ave_ep_ret = 0
    for j in range(10000):
        o, r, d, ep_ret, ep_len = test_env.reset(), 0, False, 0, 0
        while not d: # (d or (ep_len == 2000)):
            o, r, d, _ = test_env.step(get_action(o))
            ep_ret += r
            ep_len += 1
            if args.render:
                test_env.render()
        ave_ep_ret = (j*ave_ep_ret + ep_ret)/(j+1)
        print('ep_len', ep_len, 'ep_ret:', ep_ret, 'ave_ep_ret:',ave_ep_ret,'({}/10000)'.format(j+1) )


# EC503 Final Project: Exploration of Dimensionality Reduction
# Implementation of Simple Autoencoder in Keras (fully connected)
# REFERENCE: https://blog.keras.io/building-autoencoders-in-keras.html

from keras.layers import Input, Dense
from keras.models import Model
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt # plot digits
from keras.callbacks import TensorBoard # for logging
from sklearn import svm
from sklearn import cross_validation, grid_search
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.externals import joblib
from sklearn.svm import SVC
from sklearn import preprocessing

np.set_printoptions(threshold=np.inf)

# size of our encoded representation
encoding_dim = 32  # 32 floats -> compression of factor 24.5, assuming the input is 784 floats

# input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
# Rectified linear activation is our activation function, to prevent vanishing gradient
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input, with standard sigmoid activation
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)

### ENCODER ###
# this model maps an input to its encoded representation. This is what we want to explore.
encoder = Model(input_img, encoded)

### DECODER ###
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))

# Configure model using adadelta optimizer for gradients and cross entropy loss function
autoencoder.compile(optimizer='rmsprop', loss='mean_squared_error')

### DATA PREPROCESS ###
(x_train, train_labels), (x_test, test_labels) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape) # Encodes from 784 dimensions to 32 dimensions

# We fit the data to itself, since we want to reconstruct input data
autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')]) # to check loss

# encode and decode some digits from test set
encoded_test = encoder.predict(x_test) # 0-9
encoded_train = encoder.predict(x_train)

# normalize each feature to increase probability of convergence :)
encoded_train = preprocessing.normalize(encoded_train)
encoded_test = preprocessing.normalize(encoded_test)

# print(encoded_train)
print(encoded_train.shape)
print(encoded_test.shape)

decoded_imgs = decoder.predict(encoded_test)

# CLASSIFY
#param = [
#    {
#        "kernel": ["linear"],
#        "C"     : [1, 10, 100, 1000]
#    },
#    {
#        "kernel": ["rbf"],
#        "C"     : [1, 10, 100, 1000],
#        "gamma" : [1e-2, 1e-3, 1e-4, 1e-5]
#    }
#]

param = [{"kernel": ["rbf"], "C": [1000], "gamma": [0.01]}]

# Turn off probability estimation, set decision function to One Versus One
svm = SVC(probability=False, decision_function_shape='ovo', cache_size=71680)

# 10-fold cross validation, use 4 thread as each fold and each parameter set can be train in parallel
clf = grid_search.GridSearchCV(svm, param, cv=5, n_jobs=4, verbose=3)
clf.fit(encoded_train, train_labels)

print("\nBest parameters set:")
print(clf.best_params_)

# Testing on classifier..
y_predict = clf.predict(encoded_test)

print(type(y_predict))
# labels_sort = ','.join(map(str, train_labels))

labels_sort = sorted(list(set(test_labels)))

# labels_sort = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

#labels_sort = np.array([0,1,2,3,4,5,6,7,8,9])
print("\nConfusion matrix:")
print("Labels: {0}\n".format(",".join(str(labels_sort))))
print(confusion_matrix(test_labels, y_predict, labels=labels_sort))

print("\nClassification report:")
print(classification_report(test_labels, y_predict))

# Classify
# clf = svm.SVC(decision_function_shape='ovo')
# clf.fit(encoded_data, train_labels)
# decisions = clf.predict(encoded_imgs)
# CCR = (decisions == test_labels) / len(test_labels)
# print(CCR)

# Plots
n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

# n = 10
# plt.figure(figsize=(20, 8))
# for i in range(n):
#     ax = plt.subplot(1, n, i + 1)
#     plt.imshow(encoded_imgs[i].reshape(4, 4 * 2).T)
#     plt.gray()
#     ax.get_xaxis().set_visible(False)
#     ax.get_yaxis().set_visible(False)
# plt.show()


# -*- coding: utf-8 -*-

'''Minimal Python package.

Contains one function which adds its two parameters.
'''

from codecs import open
from os import path

root = path.abspath(path.dirname(__file__))

with open(path.join(root, 'VERSION.txt'), encoding='utf-8') as f:
    version = f.read().strip()

__version__ = version

__all__ = ['add']

def add(x, y):
	'''Adds x and y.'''
	return x + y


#!/usr/bin/env python

import gtk

class Image(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.set_title("Image")
        self.set_default_size(800, 600)
        self.connect("destroy", gtk.main_quit)

        vbox = gtk.VBox(False, 5)
        self.add(vbox)

        scrolledwindow = gtk.ScrolledWindow()
        vbox.pack_start(scrolledwindow, True, True, 0)

        self.image = gtk.Image()
        scrolledwindow.add_with_viewport(self.image)

        self.button = gtk.FileChooserButton("Select Image", None)
        self.button.connect("file-set", self.on_load_clicked)
        vbox.pack_start(self.button, False, False, 0)

    def on_load_clicked(self, widget):
        self.image.set_from_file(self.button.get_filename())

window = Image()
window.show_all()

gtk.main()


# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0

from typing import Any, Dict, List, Union  # noqa: F401

from databuilder.models.neo4j_csv_serde import Neo4jCsvSerializable, NODE_KEY, \
    NODE_LABEL, RELATION_START_KEY, RELATION_START_LABEL, RELATION_END_KEY, \
    RELATION_END_LABEL, RELATION_TYPE, RELATION_REVERSE_TYPE

class Watermark(Neo4jCsvSerializable):
    # type: (...) -> None
    """
    Table watermark result model.
    Each instance represents one row of table watermark result.
    """
    LABEL = 'Watermark'
    KEY_FORMAT = '{database}://{cluster}.{schema}' \
                 '/{table}/{part_type}/'
    WATERMARK_TABLE_RELATION_TYPE = 'BELONG_TO_TABLE'
    TABLE_WATERMARK_RELATION_TYPE = 'WATERMARK'

    def __init__(self,
                 create_time,  # type: str
                 database,  # type: str
                 schema,  # type: str
                 table_name,  # type: str
                 part_name,  # type: str
                 part_type='high_watermark',  # type: str
                 cluster='gold',  # type: str
                 ):
        # type: (...) -> None
        self.create_time = create_time
        self.database = database.lower()
        self.schema = schema.lower()
        self.table = table_name.lower()
        self.parts = []  # type: list

        if '=' not in part_name:
            raise Exception('Only partition table has high watermark')

        # currently we don't consider nested partitions
        idx = part_name.find('=')
        name, value = part_name.lower()[:idx], part_name.lower()[idx + 1:]
        self.parts = [(name, value)]
        self.part_type = part_type.lower()
        self.cluster = cluster.lower()
        self._node_iter = iter(self.create_nodes())
        self._relation_iter = iter(self.create_relation())

    def create_next_node(self):
        # type: (...) -> Union[Dict[str, Any], None]
        # return the string representation of the data
        try:
            return next(self._node_iter)
        except StopIteration:
            return None

    def create_next_relation(self):
        # type: (...) -> Union[Dict[str, Any], None]
        try:
            return next(self._relation_iter)
        except StopIteration:
            return None

    def get_watermark_model_key(self):
        # type: (...) -> str
        return Watermark.KEY_FORMAT.format(database=self.database,
                                           cluster=self.cluster,
                                           schema=self.schema,
                                           table=self.table,
                                           part_type=self.part_type)

    def get_metadata_model_key(self):
        # type: (...) -> str
        return '{database}://{cluster}.{schema}/{table}'.format(database=self.database,
                                                                cluster=self.cluster,
                                                                schema=self.schema,
                                                                table=self.table)

    def create_nodes(self):
        # type: () -> List[Dict[str, Any]]
        """
        Create a list of Neo4j node records
        :return:
        """
        results = []
        for part in self.parts:
            results.append({
                NODE_KEY: self.get_watermark_model_key(),
                NODE_LABEL: Watermark.LABEL,
                'partition_key': part[0],
                'partition_value': part[1],
                'create_time': self.create_time
            })
        return results

    def create_relation(self):
        # type: () -> List[Dict[str, Any]]
        """
        Create a list of relation map between watermark record with original table
        :return:
        """
        results = [{
            RELATION_START_KEY: self.get_watermark_model_key(),
            RELATION_START_LABEL: Watermark.LABEL,
            RELATION_END_KEY: self.get_metadata_model_key(),
            RELATION_END_LABEL: 'Table',
            RELATION_TYPE: Watermark.WATERMARK_TABLE_RELATION_TYPE,
            RELATION_REVERSE_TYPE: Watermark.TABLE_WATERMARK_RELATION_TYPE
        }]
        return results


import cx_Freeze

executables=[cx_Freeze.Executable("mygame.py")]

cx_Freeze.setup(
    name="Snake",
    options={"build_exe":{"packages":["pygame"],"include_files":["apple.png","snakehead.png"] }},
    description="Snake Game",
    executables=executables
)


import os

RUBY_VERSION = "2.6.3"

PACKAGES_TO_INSTALL = [
    # System Utilities
    "dconf-editor",
    "gconf-editor",
    "gnome-tweak-tool",
    "p7zip",
    "unzip",
    "p7zip-plugins",
    "xclip",
    "snapd",

    # Codecs
    "amrnb",
    "amrwb",
    "faad2",
    "flac",
    "ffmpeg",
    "gpac-libs",
    "lame",
    "libfc14audiodecoder",
    "mencoder",
    "mplayer",
    "x264",
    "x265",
    "gstreamer-plugins-espeak",
    "gstreamer-plugins-fc",
    "gstreamer-rtsp",
    "gstreamer-plugins-good",
    "gstreamer-plugins-bad",
    "gstreamer-plugins-bad-free-extras",
    "gstreamer-plugins-bad-nonfree",
    "gstreamer-plugins-ugly",
    "gstreamer-ffmpeg",
    "gstreamer1-plugins-base",
    "gstreamer1-libav",
    "gstreamer1-plugins-bad-free-extras",
    "gstreamer1-plugins-bad-freeworld",
    "gstreamer1-plugins-base-tools",
    "gstreamer1-plugins-good-extras",
    "gstreamer1-plugins-ugly",
    "gstreamer1-plugins-bad-free",
    "gstreamer1-plugins-good",

    # Multimedia Applications
    "vlc",
    "audacity",
    "soundconverter",
    "easytag",
    "tagtool",
    "simplescreenrecorder",
    "youtube-dl",

    # Science Applications
    "stellarium",
    "speedcrunch",

    # Internet Applications
    "chromium",
    "telegram-desktop",
    "transmission-gtk",

    # Graphics and Office Applications
    "libreoffice-langpack-pt-BR",
    "gimp",
    "inkscape",
    "dia",
    "pinta",
    "calibre",

    # Games
    "gnome-mines",

    # Development Utilities
    "ImageMagick",
    "graphviz",
    "zlib",
    "zlib-devel",
    "gcc-c++",
    "patch",
    "readline",
    "readline-devel",
    "libyaml-devel",
    "libffi-devel",
    "openssl-devel",
    "make",
    "bzip2",
    "autoconf",
    "automake",
    "libtool",
    "bison",
    "curl",

    # Git
    "git-core gitg",
    # Vim
    "vim",
    # Node
    "nodejs npm",
    # SQLite
    "sqlite sqlite-devel sqlite-doc sqlitebrowser",
    # Redis
    "redis",
    # Elixir
    "elixir",
    # Open JDK 11
    "java-11-openjdk.x86_64"
    # PostgreSQL
    "postgresql postgresql-devel",
    # Docker
    "docker docker-compose",
    # Zsh
    "zsh",

]

def command(command):
    os.system(command)

def add_fusion_repository():
    command("sudo dnf install --nogpgcheck "
            "http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm"
            "http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm")

def install_spotify():
    command("sudo dnf config-manager --add-repo=http://negativo17.org/repos/fedora-spotify.repo")
    PACKAGES_TO_INSTALL << "spotify-client"

def install_steam():
    command(
        "sudo dnf config-manager --add-repo=http://negativo17.org/repos/fedora-steam.repo")
    PACKAGES_TO_INSTALL << "steam"

def install_sublime():
    command(
        "sudo rpm -v --import https://download.sublimetext.com/sublimehq-rpm-pub.gpg")
    command("sudo dnf config-manager --add-repo https://download.sublimetext.com/rpm/stable/x86_64/sublime-text.repo")
    PACKAGES_TO_INSTALL << "sublime-text"

def install_vs_code():
    command("sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc")
    command("sudo sh -c 'echo -e \"[code]\nname=Visual Studio Code\nbaseurl="
            "https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey="
            "https://packages.microsoft.com/keys/microsoft.asc\" > /etc/yum.repos.d/vscode.repo'")
    PACKAGES_TO_INSTALL << "code"

def install_yarn():
    command("curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo")
    PACKAGES_TO_INSTALL << "yarn"

def setup_zsh():
    command("curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh; zsh")
    command("chsh -s $(which zsh) $USER")

def update_repositories():
    command("sudo dnf makecache && sudo dnf check-update")

def update_system():
    command("sudo dnf update -y")

def install_packages():
    command("sudo dnf install " + " ".join(PACKAGES_TO_INSTALL) + " -y")

def setup_dotfiles():
    pass

def setup_snap():
    command("sudo ln -s /var/lib/snapd/snap /snap")

def setup_docker():
    command("sudo systemctl start docker")
    command("sudo systemctl enable docker")
    command("sudo groupadd docker && sudo gpasswd -a $USER docker && sudo systemctl restart docker")
    command("sudo curl -L \"https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)\""
            " -o /usr/local/bin/docker-compose")
    command("sudo chmod +x /usr/local/bin/docker-compose")

def install_ruby():
    command("git clone https://github.com/rbenv/rbenv.git ~/.rbenv")
    command("echo 'export PATH=\"$HOME/.rbenv/bin:$PATH\"' >> ~/.zshrc")
    command("echo 'eval \"$(rbenv init -)\"' >> ~/.zshrc")
    command("exec $SHELL")
    command(
        "git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build")
    command(
        "echo 'export PATH=\"$HOME/.rbenv/plugins/ruby-build/bin:$PATH\"' >> ~/.zshrc")
    command("exec $SHELL")
    command("rbenv install " + RUBY_VERSION)
    command("rbenv global " + RUBY_VERSION)
    command("gem install bundler pry awesome_print rubocop scss_lint rails")

def install_postman():
    command("sudo snap install postman")

def install_heroku_cli():
    command("sudo snap install --classic heroku")

def install_node_packages():
    command("sudo npm install --global webpack gulp hexo")

def install_python_packages():
    command("/usr/bin/python3 -m pip install -U autopep8 --user")

def config_language():
    command("sudo dnf install system-config-language")

if __name__ == '__main__':
    add_fusion_repository()
    install_spotify()
    install_steam()
    install_sublime()
    install_vs_code()

    update_repositories()
    update_system()

    install_packages()

    setup_dotfiles()

    setup_zsh()
    setup_snap()
    setup_docker()

    install_ruby()
    install_postman()
    install_heroku_cli()
    install_node_packages()
    install_python_packages()

    config_language()