from dotenv import load_dotenv
import meraki
import os

load_dotenv('environment.env')

### MUST CREATE environment.env FILE WITH THE FOLLOWING VARIABLES ###
API_KEY = os.getenv('API_KEY')
network_id = os.getenv('NET_ID') # Optionally you can get this at runtime using the calls below

# Initialize Dashboard object
dashboard = meraki.DashboardAPI(API_KEY)

########################
# Uncomment if you need to get your org or network ID.
# Get organizations
# response = dashboard.organizations.getOrganizations()
# print(response)

# # Get networks
# response = dashboard.organizations.getOrganizationNetworks(
#     organization_id, total_pages='all'
# )
# print(response)
########################

#### Specify Tags and Targets Here ####

# Specify Target Strings to match
match_tag_list = ['ios', 'mac', 'apple'] # Here we match the apple/iOS Auto tags

# SPECIFY DESIRED NEW TAG TAG HERE
new_tag = 'SUPER_TAG'

# List the devices enrolled in an SM network with various specified fields and filters
# https://developer.cisco.com/meraki/api-v1/#!get-network-sm-devices
sm_devices_response = dashboard.sm.getNetworkSmDevices(
    network_id, total_pages='all', fields=['autoTags']
)

# print(sm_devices_response)

devices_without_tag =[]
device_names = []

# For all devices
# Check if the VPN tag is applied or not
# Then flag device if it matches the specified iderntifiers
for device in sm_devices_response:
    if new_tag not in device['tags']:
        for tag in device['autoTags']:
            for mac_identifier in match_tag_list:
                if mac_identifier.lower() in tag.lower():
                    devices_without_tag.append(device['id'])
                    device_names.append(device['name'])

# Remove duplicates
devices_without_tag = list(set(devices_without_tag))
# Print Results
print('Checked ' + str(len(sm_devices_response)) +f' devices. Here are the IDs of Apple devices without the {new_tag} tag')
print(devices_without_tag)
print('Device Names:')
print(device_names)

# If flagged devices are found
# Add the tag to the flagged devices
# https://developer.cisco.com/meraki/api-v1/#!modify-network-sm-devices-tags
if devices_without_tag:
    update_tags_response = dashboard.sm.modifyNetworkSmDevicesTags(
        network_id, updateAction='add', ids=devices_without_tag, tags=[new_tag],
    )

    # Print updates devices
    print(f'Added {new_tag} to the following devices:')
    for device in update_tags_response:
        print(device['id'])

print('Task Complete')

# Included is an API call to remove the flag if desired.
remove_tag = dashboard.sm.modifyNetworkSmDevicesTags(
    network_id, scope=['withAny', new_tag], updateAction='delete', tags=[new_tag],
)


from matplotlib import pyplot as plt
from matplotlib.dates import date2num
import numpy as np
import argparse
import csv
from datetime import datetime
import math
import os

# USAGE:
#   python plot.py -folder 2014-04-01_2017-11-13
# AVAILABLE OPTIONS:
#   -folder (required)
# EXAMPLE:
#   python plot.py -folder 2014-04-01_2017-11-13
#   python plot.py -folder 2014-04-01_2017-12-19

cwd = os.getcwd()
parser = argparse.ArgumentParser(description='Analyze sales team data')
parser.add_argument('-folder', '--folder', help='Folder name', required=True)
args = vars(parser.parse_args())

# CONSTANTS
showPlt = False

def get_csv_data(filename):
    with open(cwd + '/' + args['folder'] + '/' + filename) as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        raw_data = np.array([tuple(row) for (i, row) in enumerate(readCSV)])
        heading = raw_data[0]
        data = raw_data[1:]
        data[data=='']='0'
        data_dict = [dict(zip(heading, row)) for row in data]
    return (heading, data_dict)

def get_col(colName, rows):
    return np.array([cols[colName] for cols in rows])

def convert_to_datetime(dates):
    return np.array([datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ').date() for date in dates]).astype('str')

def setupPlt():
    plt.figure(figsize=(20, 9), dpi=80)
    plt.xticks(fontsize=7)
    plt.yticks(fontsize=7)
    plt.xticks(rotation='vertical')

def savePlt(filename):
    if showPlt:
        plt.show()
    plt.savefig(cwd + '/' + args['folder'] + '/charts/' + filename + '.svg')

# annual-retention-monthly-trend.csv
#
filename = 'annual-retention-monthly-trend.csv'
(heading, data) = get_csv_data(filename)
retention_rate = get_col('retention_rate', data)
date = get_col('date', data)

setupPlt()
plt.bar(date, retention_rate)
plt.title(filename)
plt.ylabel('retention_rate')
plt.xlabel('monthly_cohort')
savePlt(filename)

# initial-purchase-cycle-by-monthly-cohort.csv
#
filename = 'initial-purchase-cycle-by-monthly-cohort.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_cycle = get_col('initial_purchase_cycle_in_days', data).astype(float)
# purchase_value = get_col('initial_purchase_value', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.bar(x + 0.00, purchase_cycle, color='#EE3224', width=0.25, label='purchase_cycle')
# plt.bar(x + 0.25, purchase_value, color='#FFC222', width=0.25, label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('initial_purchase_cycle_in_days')
plt.xlabel('purchase_date')
savePlt(filename)

# repeat-purchase-cycle-by-monthly-cohort.csv
#
filename = 'repeat-purchase-cycle-by-monthly-cohort.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_cycle = get_col('avg_purchase_cycle', data).astype(float)
# purchase_value = get_col('avg_purchase_size', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.bar(x + 0.00, purchase_cycle, color='#EE3224', width=0.25, label='purchase_cycle')
# plt.bar(x + 0.25, purchase_value, color='#FFC222', width=0.25, label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('initial_purchase_cycle_in_days')
plt.xlabel('purchase_date')
savePlt(filename)

# new-deals-weekly-cohort-trend.csv
#
filename = 'new-deals-weekly-cohort-trend.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_value = get_col('decimal_value', data).astype(float)
num_new_clients = get_col('contact_count', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.plot(x, purchase_value, color='#EE3224', label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('purchase_value')
plt.xlabel('purchase_date')
savePlt(filename)

# number of new orders
setupPlt()
plt.xticks(x, date)
plt.plot(x, num_new_clients, color='#EE3224', label='num_new_clients')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('num_new_clients')
plt.xlabel('purchase_date')
savePlt(filename)

# repeat-deals-weekly-cohort-trend.csv
#
filename = 'repeat-deals-weekly-cohort-trend.csv'
(heading, data) = get_csv_data(filename)
date = get_col('date', data)
purchase_value = get_col('decimal_value', data).astype(float)
num_repeat_clients = get_col('contact_count', data).astype(float)
x = np.arange(len(date))

setupPlt()
plt.xticks(x, date)
plt.plot(x, purchase_value, color='#EE3224', label='purchase_value')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('purchase_value')
plt.xlabel('purchase_date')
savePlt(filename)

# number of repeat orders
setupPlt()
plt.xticks(x, date)
plt.plot(x, num_repeat_clients, color='#EE3224', label='num_repeat_clients')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('num_repeat_clients')
plt.xlabel('purchase_date')
savePlt(filename)

# initial-purchase-cycle-by-deal-size-distribution.csv
#
filename = 'initial-purchase-cycle-by-deal-size-distribution.csv'
(heading, data) = get_csv_data(filename)
bucket_labels = get_col('bucket', data)
avg_initial_purchase_cycle_in_days = get_col('avg_initial_purchase_cycle_in_days', data).astype(float)
x = np.arange(len(bucket_labels))

setupPlt()
plt.xticks(x, bucket_labels)
plt.bar(x, avg_initial_purchase_cycle_in_days, color='#EE3224', label='avg_initial_purchase_cycle_in_days')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('avg_initial_purchase_cycle_in_days')
plt.xlabel('bucket')
savePlt(filename)

# avg-deal-size-by-contact-distribution.csv
#
filename = 'avg-deal-size-by-contact-distribution.csv'
(heading, data) = get_csv_data(filename)
bucket_labels = get_col('avg deal size bucket', data)
frequency = get_col('frequency', data).astype(int)
x = np.arange(len(bucket_labels))

setupPlt()
plt.xticks(x, bucket_labels)
plt.bar(x, frequency, color='#EE3224', label='frequency')
plt.legend(loc='upper left')
plt.title(filename)
plt.ylabel('frequency')
plt.xlabel('avg deal size bucket')
savePlt(filename)


################################################################################

# Version 1.0    $Revision: 1 $                                                #

#                                                                              #

#    Copyright © 1997 - 2014 by IXIA                                           #

#    All Rights Reserved.                                                      #

#                                                                              #

#    Revision Log:                                                             #

#    12/12/2014 - Sayantan Pramanick - created sample                          #

#                                                                              #

################################################################################

################################################################################

#                                                                              #

#                                LEGAL  NOTICE:                                #

#                                ==============                                #

# The following code and documentation (hereintime.sleep("the script") is an        #

# example script for demonstration purposes only.                              #

# The script is not a standard commercial product offered by Ixia and have     #

# been developed and is being provided for use only as indicated herein. The   #

# script [and all modifications enhancements and updates thereto (whether      #

# made by Ixia and/or by the user and/or by a third party)] shall at all times #

# remain the property of Ixia.                                                 #

#                                                                              #

# Ixia does not warrant (i) that the functions contained in the script will    #

# meet the users requirements or (ii) that the script will be without          #

# omissions or error-free.                                                     #

# THE SCRIPT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND IXIA         #

# DISCLAIMS ALL WARRANTIES EXPRESS IMPLIED STATUTORY OR OTHERWISE              #

# INCLUDING BUT NOT LIMITED TO ANY WARRANTY OF MERCHANTABILITY AND FITNESS FOR #

# A PARTICULAR PURPOSE OR OF NON-INFRINGEMENT.                                 #

# THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SCRIPT  IS WITH THE #

# USER.                                                                        #

# IN NO EVENT SHALL IXIA BE LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING   #

# OUT OF THE USE OF OR THE INABILITY TO USE THE SCRIPT OR ANY PART THEREOF     #

# INCLUDING BUT NOT LIMITED TO ANY LOST PROFITS LOST BUSINESS LOST OR          #

# DAMAGED DATA OR SOFTWARE OR ANY INDIRECT INCIDENTAL PUNITIVE OR              #

# CONSEQUENTIAL DAMAGES EVEN IF IXIA HAS BEEN ADVISED OF THE POSSIBILITY OF    #

# SUCH DAMAGES IN ADVANCE.                                                     #

# Ixia will not be required to provide any software maintenance or support     #

# services of any kind (e.g. any error corrections) in connection with the     #

# script or any part thereof. The user acknowledges that although Ixia may     #

# from time to time and in its sole discretion provide maintenance or support  #

# services for the script any such services are subject to the warranty and    #

# damages limitations forth herein and will not obligate Ixia to provide   #

# any additional maintenance or support services.                              #

#                                                                              #

################################################################################

################################################################################

#                                                                              #

# Description:                                                                 #

#    This script intends to demonstrate how to use NGPF Audio Video Bridging   #

#     API.                                                                     #

#                                                                              #

#    1. It will create one MSRP Talker in one topology and 2 MSRP Listeners    #

#       in another topology. gPTP clocks will be added in talkers and          #

#       listeners.                                                             #

#    2. Start all protocols.                                                   #

#    3. Retrieve protocol statistics.                                          #

#    4. Retrieve protocol learned info.                                        #

#    7. Configure L2-L3 traffic.                                               #

#    9. Start the L2-L3 traffic.                                               #

#   12. Retrieve L2-L3 traffic stats.                                          #

#   13. Stop L2-L3 traffic.                                                    #

#   15. Stop all protocols.                                                    #

# Ixia Softwares:                                                              #

#    IxOS      6.80 EA (6.80.1100.7)                                         #

#    IxNetwork 7.40 EA (7.40.929.15)                                            #

#                                                                              #

################################################################################

import os

import sys

import time

def assignPorts (ixNet, realPort1, realPort2) :

     chassis1 = realPort1[0]

     chassis2 = realPort2[0]

     card1    = realPort1[1]

     card2    = realPort2[1]

     port1    = realPort1[2]

     port2    = realPort2[2]

     root = ixNet.getRoot()

     vport1 = ixNet.add(root, 'vport')

     ixNet.commit()

     vport1 = ixNet.remapIds(vport1)[0]

     vport2 = ixNet.add(root, 'vport')

     ixNet.commit()

     vport2 = ixNet.remapIds(vport2)[0]

     chassisObj1 = ixNet.add(root + '/availableHardware', 'chassis')

     ixNet.setAttribute(chassisObj1, '-hostname', chassis1)

     ixNet.commit()

     chassisObj1 = ixNet.remapIds(chassisObj1)[0]

     if (chassis1 != chassis2) :

         chassisObj2 = ixNet.add(root + '/availableHardware', 'chassis')

         ixNet.setAttribute(chassisObj2, '-hostname', chassis2)

         ixNet.commit()

         chassisObj2 = ixNet.remapIds(chassisObj2)[0]

     else :

         chassisObj2 = chassisObj1

     # end if

     cardPortRef1 = chassisObj1 + '/card:%s/port:%s' % (card1,port1)

     ixNet.setMultiAttribute(vport1, '-connectedTo', cardPortRef1,

         '-rxMode', 'captureAndMeasure', '-name', 'Ethernet - 001')

     ixNet.commit()

     cardPortRef2 = chassisObj2 + '/card:%s/port:%s' % (card2,port2)

     ixNet.setMultiAttribute(vport2, '-connectedTo', cardPortRef2,

         '-rxMode', 'captureAndMeasure', '-name', 'Ethernet - 002')

     ixNet.commit()

# end def assignPorts

################################################################################

# Either feed the ixNetwork library path in the sys.path as below, or put the

# IxNetwork.pm file somewhere else where we python can autoload it.

# "IxNetwork.pm" is available in <IxNetwork_installer_path>\API\Python

################################################################################

ixNetPath = r'C:\Program Files (x86)\Ixia\IxNetwork\7.40-EA\API\Python'

sys.path.append(ixNetPath)

import IxNetwork

#################################################################################

# Give chassis/client/ixNetwork server port/ chassis port HW port information

# below

#################################################################################

print("!!! Test Script Starts !!!")

ixTclServer = '10.205.25.88'

ixTclPort   = '7777'

ports       = [('10.205.28.65', '2', '1',), ('10.205.28.65', '2', '2',)]

# get IxNet class

ixNet = IxNetwork.IxNet()

print("connecting to IxNetwork client")

ixNet.connect(ixTclServer, '-port', ixTclPort, '-version', '7.40',

     '-setAttribute', 'strict')

# cleaning up the old configfile, and creating an empty config

print("cleaning up the old configfile, and creating an empty config")

ixNet.execute('newConfig')

# assigning ports

assignPorts(ixNet, ports[0], ports[1])

time.sleep(5)

root   = ixNet.getRoot()

vport1 = ixNet.getList(root, 'vport')[0]

vport2 = ixNet.getList(root, 'vport')[1]

print("Adding 2 topologies")

ixNet.add(root, 'topology', '-vports', vport1)

ixNet.add(root, 'topology', '-vports', vport2)

ixNet.commit()

topologies = ixNet.getList(ixNet.getRoot(), 'topology')

topo1 = topologies[0]

topo2 = topologies[1]

print("Adding 1 device groups in topology 1")

ixNet.add(topo1, 'deviceGroup')

print("Adding 2 device groups in topology 2")

ixNet.add(topo2, 'deviceGroup')

ixNet.add(topo2, 'deviceGroup')

ixNet.commit()

t1devices = ixNet.getList(topo1, 'deviceGroup')

t2devices = ixNet.getList(topo2, 'deviceGroup')

t1dev1 = t1devices[0]

t2dev1 = t2devices[0]

t2dev2 = t2devices[1]

print("Configuring the multipliers (number of sessions)")

ixNet.setAttribute(t1dev1, '-multiplier', 1)

ixNet.setAttribute(t2dev1, '-multiplier', 2)

ixNet.setAttribute(t2dev2, '-multiplier', 1)

ixNet.commit()

print("Adding ethernet/mac endpoints")

ixNet.add(t1dev1, 'ethernet')

ixNet.add(t2dev1, 'ethernet')

ixNet.add(t2dev2, 'ethernet')

ixNet.commit()

mac1 = ixNet.getList(t1dev1, 'ethernet')[0]

mac2 = ixNet.getList(t2dev1, 'ethernet')[0]

mac3 = ixNet.getList(t2dev2, 'ethernet')[0]

print("Configuring the mac addresses")

ixNet.setMultiAttribute(ixNet.getAttribute(mac1, '-mac') + '/counter',

    '-direction', 'increment',

    '-start', '22:22:22:22:22:22',

    '-step', '00:00:00:00:00:01')

ixNet.setMultiAttribute(ixNet.getAttribute(mac2, '-mac') + '/counter',

    '-direction', 'increment',

    '-start', '44:44:44:44:44:44',

    '-step', '00:00:00:00:00:01')

ixNet.setMultiAttribute(ixNet.getAttribute(mac3, '-mac') + '/singleValue',

    '-value', '66:66:66:66:66:66')

ixNet.commit()

print("Adding MSRP talker in topology 1")

talker1 = ixNet.add(mac1, 'msrpTalker')

ixNet.commit()

talker1 = ixNet.remapIds(talker1)[0]

print("Configuring 2 streams in talker")

ixNet.setAttribute(talker1, '-streamCount', '2')

ixNet.commit()

print("Adding gPTP clock in topology 1")

ptp1 = ixNet.add(mac1, 'ptp')

ixNet.commit()

ptp1 = ixNet.remapIds(ptp1)[0]

print("Setting clock role as master in AVB talker")

ixNet.setAttribute(ixNet.getAttribute(ptp1, '-role') + '/singleValue',

    '-value', 'master')

ixNet.commit()

print("Adding MSRP listener in topology 2")

listener1 = ixNet.add(mac2, 'msrpListener')

ixNet.commit()

listener1 = ixNet.remapIds(listener1)[0]

print("Adding gptp clock in topology 2")

ptp2 = ixNet.add(mac3, 'ptp')

ixNet.commit()

ptp2 = ixNet.remapIds(ptp2)[0]

ixNet.setAttribute(ixNet.getAttribute(ptp2, '-profile') + '/singleValue',

    '-value', 'ieee8021as')

ixNet.setAttribute(ixNet.getAttribute(ptp2, '-delayMechanism') + '/singleValue',

    '-value', 'peerdelay')

ixNet.commit()

################################################################################

# 2. Start AVB protocols and wait for 60 seconds

################################################################################

print("Starting protocols and waiting for 60 seconds for protocols to come up")

ixNet.execute('startAllProtocols')

time.sleep(60)

################################################################################

# 3. Retrieve protocol statistics.

################################################################################

print("Fetching all Protocol Summary Stats\n")

viewPage = '::ixNet::OBJ-/statistics/view:"Protocols Summary"/page'

statcap  = ixNet.getAttribute(viewPage, '-columnCaptions')

for statValList in ixNet.getAttribute(viewPage, '-rowValues') :

    for  statVal in statValList :

        print("***************************************************")

        index = 0

        for statIndv in statVal :

            print("%-30s:%s" % (statcap[index], statIndv))

            index = index + 1

        # end for

    # end for

# end for

print("***************************************************")

################################################################################

# 4. Retrieve protocol learned info

################################################################################

print("Fetching MSRP Talker Learned Info")

ixNet.execute('getTalkerDatabases', talker1)

time.sleep(5)

linfo = ixNet.getList(talker1, 'learnedInfo')

streamDb = linfo[0]

domainDb = linfo[1]

vlanDb   = linfo[2]

values = ixNet.getAttribute(streamDb, '-values')

column = ixNet.getAttribute(streamDb, '-columns')

print("***************************************************")

print("****  MSRP Talker stream database learned info ****")

for index in range(len(values)):

    rowValue = values[index]

    for col in range(len(column)):

        print("%-30s:%s" % (column[col], rowValue[col]))

    #end for

#end for

print("***************************************************")

values = ixNet.getAttribute(domainDb, '-values')

column = ixNet.getAttribute(domainDb, '-columns')

print("***************************************************")

print("****  MSRP Talker Domain database learned info ****")

for index in range(len(values)):

    rowValue = values[index]

    for col in range(len(column)):

        print("%-30s:%s" % (column[col], rowValue[col]))

    #end for

#end for

print("***************************************************")

values = ixNet.getAttribute(vlanDb, '-values')

column = ixNet.getAttribute(vlanDb, '-columns')

print("***************************************************")

print("*****  MSRP Talker VLAN database learned info *****")

for index in range(len(values)):

    rowValue = values[index]

    for col in range(len(column)):

        print("%-30s:%s" % (column[col], rowValue[col]))

    #end for

#end for

print("***************************************************")

print("Fetching MSRP Listener Learned Info for listener 1")

ixNet.execute('getListenerDatabases', listener1, '1')

time.sleep(5)

linfo = ixNet.getList(listener1, 'learnedInfo')

streamDb = linfo[0]

domainDb = linfo[1]

vlanDb   = linfo[2]

values = ixNet.getAttribute(streamDb, '-values')

column = ixNet.getAttribute(streamDb, '-columns')

print("***************************************************")

print("*** MSRP Listener stream database learned info ****")

for index in range(len(values)):

    rowValue = values[index]

    for col in range(len(column)):

        print("%-30s:%s" % (column[col], rowValue[col]))

    #end for

#end for

print("***************************************************")

values = ixNet.getAttribute(domainDb, '-values')

column = ixNet.getAttribute(domainDb, '-columns')

print("***************************************************")

print("*** MSRP Listener Domain database learned info ****")

for index in range(len(values)):

    rowValue = values[index]

    for col in range(len(column)):

        print("%-30s:%s" % (column[col], rowValue[col]))

    #end for

#end for

print("***************************************************")

values = ixNet.getAttribute(vlanDb, '-values')

column = ixNet.getAttribute(vlanDb, '-columns')

print("***************************************************")

print("**** MSRP Listener VLAN database learned info *****")

for index in range(len(values)):

    rowValue = values[index]

    for col in range(len(column)):

        print("%-30s:%s" % (column[col], rowValue[col]))

    #end for

#end for

print("***************************************************")

################################################################################

# 5. Disable streams and apply changes On The Fly (OTF). Enable again.

################################################################################

print("Deactivating the streams")

streams = ixNet.getList(mac1, 'streams')[0]

multiValue = ixNet.getAttribute(streams, '-active')

ixNet.setAttribute(multiValue + '/singleValue', '-value', 'false')

ixNet.commit()

globals = ixNet.getRoot() + '/globals'

topology = globals + '/topology'

print("Applying changes on the fly")

try :

    ixNet.execute('applyOnTheFly', topology)

except :

    print("error in applying on the fly change")

# end try/expect

time.sleep(5)

print("Activating the streams")

ixNet.setAttribute(multiValue + '/singleValue', '-value', 'true')

ixNet.commit()

print("Applying changes on the fly")

try :

    ixNet.execute('applyOnTheFly', topology)

except :

    print("error in applying on the fly change")

# end try/expect

time.sleep(5)

################################################################################

# 7. Configure L2-L3 traffic

################################################################################

print("Configuring L2-L3 Traffic Item")

trafficItem1 = ixNet.add(ixNet.getRoot() + '/traffic', 'trafficItem')

ixNet.setMultiAttribute(trafficItem1,

    '-name', 'avb_traffic',

    '-roundRobinPacketOrdering', 'false',

    '-trafficType', 'avb1722')

ixNet.commit()

trafficItem1 = ixNet.remapIds(trafficItem1)[0]

endpointSet1 = ixNet.add(trafficItem1, 'endpointSet')

mcastDestination = [['false', 'none', '22:22:22:22:22:22:00:01',

    '00:00:00:00:00:00:00:00', '1'], ['false', 'none', '22:22:22:22:22:22:00:02',

    '00:00:00:00:00:00:00:00', '1']]

mcastReceiver = [[listener1 + '/subscribedStreams', '0', '0', '0'],

    [listener1 + '/subscribedStreams', '0', '0', '1'],

    [listener1 + '/subscribedStreams', '0', '1', '0'],

    [listener1 + '/subscribedStreams', '0', '1', '1']]

ixNet.setMultiAttribute(endpointSet1,

    '-name',                  'EndpointSet-1',

    '-multicastDestinations', mcastDestination,

    '-scalableSources',       [],

    '-multicastReceivers',    mcastReceiver,

    '-scalableDestinations',  [],

    '-ngpfFilters',           [],

    '-trafficGroups',         [],

    '-sources',               [],

    '-destinations',          [])

ixNet.commit()

ixNet.setMultiAttribute(trafficItem1 + '/tracking',

    '-trackBy', ['trackingenabled0', 'avbStreamName0'])

ixNet.commit()

###############################################################################

# 9. Apply and start L2/L3 traffic

###############################################################################

print ('applying L2/L3 traffic')

ixNet.execute('apply', ixNet.getRoot() + '/traffic')

time.sleep(5)

print ('starting L2/L3 traffic')

ixNet.execute('start', ixNet.getRoot() + '/traffic')

###############################################################################

# 12. Retrieve L2/L3 traffic item statistics

###############################################################################

print ('Verifying all the L2-L3 traffic stats')

viewPage = '::ixNet::OBJ-/statistics/view:"Flow Statistics"/page'

statcap =  ixNet.getAttribute(viewPage, '-columnCaptions')

for statValList in ixNet.getAttribute(viewPage, '-rowValues') :

    for  statVal in statValList :

        print("***************************************************")

        index = 0

        for satIndv in statVal :

            print("%-30s:%s" % (statcap[index], satIndv))

            index = index + 1

        # end for

    # end for

# end for

print("***************************************************")

################################################################################

# 14. Stop L2/L3 traffic

#################################################################################

print ('Stopping L2/L3 traffic')

ixNet.execute('stop', ixNet.getRoot() + '/traffic')

time.sleep(5)

################################################################################

# 15. Stop all protocols

################################################################################

print ('Stopping protocols')

ixNet.execute('stopAllProtocols')

print ('!!! Test Script Ends !!!')


#tutorial link : https://www.youtube.com/watch?v=DIT8rwyPEns&ab_channel=JohnWatsonRooney
#tutorial github link: https://github.com/jhnwr/scrape-amazon-reviews
# url we want the reviews from  'https://www.amazon.nl/That-Will-Never-Work-Netflix/product-reviews/0316530204/ref=cm_cr_arp_d_paging_btm_next_2?ie=UTF8&reviewerType=all_reviews&pageNumber=1'

#first start splash from docker, than change the url at the bottom and change the attribtues in get_reviews.

import requests
import pandas as pd
from unidecode import unidecode
import json
from bs4 import BeautifulSoup

list_url=[]
players=[]
def file_reader(file_name):
	file=open(file_name, encoding="utf8")
	text=file.readlines()
	all_players=[]
	for line in text:
		player=str(line)
		players.append(player)
		print(player)
	for item in players:
		tijdelijk=str(item)
		tijdelijk=tijdelijk.replace('[','').replace(']','').replace("'", "")
		url='https://www.transfermarkt.com/schnellsuche/ergebnis/schnellsuche?query='+str(tijdelijk) #use search function of website
		list_url.append(url)

file_reader('SerieA_missing_value.txt')

players_worth=[]
for item in list_url:
	print(item)
	r=requests.get('http://localhost:8050/render.html', params={'url': item, 'wait': 1})
	soup= BeautifulSoup(r.text, 'html.parser')
	players_info= soup.find('td', class_='rechts hauptlink')
	name=str(item)
	name=name.replace("https://www.transfermarkt.com/schnellsuche/ergebnis/schnellsuche?query=", "")
	name=name.replace("\n", "")
	print(name)
	if players_info is not None  :
		print(players_info.text)
		waardengoed=str(players_info.text)
		if "m" in waardengoed:
			waardengoed=waardengoed.replace('.', "")
			waardengoed=waardengoed.replace('m', "0")
		elif "Th" in waardengoed:
			waardengoed=waardengoed.replace("Th.","")
		else:
			waardengoed="Geen waarde"
		players_worth.append(name+":"+waardengoed)
	else:
		players_worth.append(name+":"+"Fout, geen speler gevonden")
print(players_worth)

df = pd.DataFrame(players_worth)
writer = pd.ExcelWriter('SerieA_missing_value_players.xlsx', engine='xlsxwriter')
df.to_excel(writer, index=False)
df.to_excel(writer,startrow=len(df)+1, index=False, header=None)
writer.save()

Code creating dataset/Get current value of player by name from kladblock.py


"""
This is my story
"""
myStory = """My favourite animal is a {animal} and it loves to eat a lot of {food}. I have to import {food} from {city}."""

def tellStory():
    myFavs = dict()
    addFav('animal', myFavs)
    addFav('food', myFavs)
    addFav('city', myFavs)
    story = myStory.format(**myFavs)
    print(story)

def addFav(Kavar, myFavs):
    """
    This allows user input
    """
    prompt = 'Enter name of ' + Kavar+':'
    response = input(prompt).strip()
    myFavs[Kavar] = response

tellStory()
input('Press enter to exit')


import requests
import os
import json

class TgSender:
    def __init__(self, token, url):
        self.TOKEN = token
        self.URL = url

    def send_message(self, chat_id, text):
        requests.get(f'{self.URL}{self.TOKEN}/sendMessage?chat_id={chat_id}&text={text}')

    def send_photo(self, chat_id, path):
        files = {'photo': open(path, 'rb')}
        requests.post(f'{self.URL}{self.TOKEN}/sendPhoto?chat_id={chat_id}', files=files)

    def telegram_send_media_group(self, chat_id, path):
        url = "https://api.telegram.org/bot" + self.TOKEN + "/sendMediaGroup"
        channel_id = "@level_signals"

        files = dict.fromkeys(os.listdir(path))
        folder = path + '/'

        media_list = []

        for key in files:
            files[key] = open(folder + key, 'rb')

            media_data = dict.fromkeys(['type', 'media', 'caption'])
            media_data['type'] = 'photo'
            media_data['media'] = 'attach://' + key
            media_data['caption'] = key

            media_list.append(media_data)

        media = json.dumps(media_list)

        params = {
                'chat_id': chat_id,
                'media': media
                }

        r = requests.post(url, params= params, files= files)


"""Prediction functions
"""

import io
import logging
from time import sleep
from urllib.parse import urlparse

import boto3 as boto
import httpx
from typing import List

from sync.clients.sync import get_default_client
from sync.models import Platform, PredictionError, Response

logger = logging.getLogger(__name__)

def get_products() -> Response[List[str]]:
    """Get supported platforms

    :return: list of platform names
    :rtype: Response[list[str]]
    """
    response = get_default_client().get_products()
    return Response(**response)

def generate_prediction(
    platform: Platform, cluster_report: dict, eventlog_url: str, preference: str = None
) -> Response[dict]:
    """Create and return prediction

    :param platform: e.g. "aws-emr"
    :type platform: Platform
    :param cluster_report: cluster report
    :type cluster_report: dict
    :param eventlog_url: Apache Spark event log URL
    :type eventlog_url: str
    :param preference: prediction preference, defaults to None
    :type preference: str, optional
    :return: prediction object
    :rtype: Response[dict]
    """
    response = create_prediction(platform, cluster_report, eventlog_url)

    prediction_id = response.result
    if prediction_id:
        return wait_for_prediction(prediction_id, preference)

    return response

def wait_for_prediction(prediction_id: str, preference: str = None) -> Response[dict]:
    """Get a prediction, wait if it's not ready

    :param prediction_id: prediction ID
    :type prediction_id: str
    :param preference: prediction preference, defaults to None
    :type preference: str, optional
    :return: prediction object
    :rtype: Response[dict]
    """
    response = wait_for_final_prediction_status(prediction_id)
    result = response.result
    if result:
        if result == "SUCCESS":
            return get_prediction(prediction_id, preference)

        return Response(error=PredictionError(message="Prediction failed"))

    return response

def get_prediction(prediction_id: str, preference: str = None) -> Response[dict]:
    """Get a prediction, don't wait

    :param prediction_id: prediction ID
    :type prediction_id: str
    :param preference: prediction preference, defaults to None
    :type preference: str, optional
    :return: prediction object
    :rtype: Response[dict]
    """
    response = get_default_client().get_prediction(
        prediction_id, {"preference": preference} if preference else None
    )

    result = response.get("result")
    if result:
        return Response(result=result)

    return Response(**response)

def get_status(prediction_id: str) -> Response[str]:
    """Get prediction status

    :param prediction_id: prediction ID
    :type prediction_id: str
    :return: prediction status, e.g. "SUCCESS"
    :rtype: Response[str]
    """
    response = get_default_client().get_prediction_status(prediction_id)

    result = response.get("result")
    if result:
        return Response(result=result["status"])

    return Response(**response)

def get_predictions(
    product: str = None, project_id: str = None, preference: str = None
) -> Response[List[dict]]:
    """Get predictions

    :param product: platform to filter by, e.g. "aws-emr", defaults to None
    :type product: str, optional
    :param project_id: project to filter by, defaults to None
    :type project_id: str, optional
    :param preference: prediction preference, defaults to None
    :type preference: str, optional
    :return: list of prediction objects
    :rtype: Response[list[dict]]
    """
    params = {}
    if product:
        params["products"] = [product]
    if project_id:
        params["project_id"] = project_id
    if preference:
        params["preference"] = preference

    response = get_default_client().get_predictions(params)

    if response.get("result") is not None:
        return Response(result=response["result"])

    return Response(**response)

def wait_for_final_prediction_status(prediction_id: str) -> Response[str]:
    """Wait for and return terminal prediction status

    :param prediction_id: prediction ID
    :type prediction_id: str
    :return: prediction status, e.g. "SUCCESS"
    :rtype: Response[str]
    """
    response = get_default_client().get_prediction_status(prediction_id)
    while response:
        result = response.get("result")
        if result:
            if result["status"] in ("SUCCESS", "FAILURE"):
                return Response(result=result["status"])
        else:
            return Response(**response)

        logger.info("Waiting for prediction")
        sleep(10)

        response = get_default_client().get_prediction_status(prediction_id)

    return Response(error=PredictionError(message="Failed to get prediction status"))

def create_prediction_with_eventlog_bytes(
    platform: Platform,
    cluster_report: dict,
    eventlog_name: str,
    eventlog_bytes: bytes,
    project_id: str = None,
) -> Response[str]:
    """Creates a prediction giving event log bytes instead of a URL

    :param platform: platform, e.g. "aws-emr"
    :type platform: Platform
    :param cluster_report: cluster report
    :type cluster_report: dict
    :param eventlog_name: name of event log (extension is important)
    :type eventlog_name: str
    :param eventlog_bytes: encoded event log
    :type eventlog_bytes: bytes
    :param project_id: ID of project to which the prediction belongs, defaults to None
    :type project_id: str, optional
    :return: prediction ID
    :rtype: Response[str]
    """
    response = get_default_client().create_prediction(
        {
            "project_id": project_id,
            "product_code": platform,
            "configs": cluster_report,
        }
    )

    if response.get("error"):
        return Response(**response)

    upload_details = response["result"]["upload_details"]
    log_response = httpx.post(
        upload_details["url"],
        data={
            **upload_details["fields"],
            "key": upload_details["fields"]["key"].replace("${filename}", eventlog_name),
        },
        files={"file": io.BytesIO(eventlog_bytes)},
    )
    if not log_response.status_code == httpx.codes.NO_CONTENT:
        return Response(error=PredictionError(message="Failed to upload event log"))

    return Response(result=response["result"]["prediction_id"])

def create_prediction(
    platform: Platform, cluster_report: dict, eventlog_url: str, project_id: str = None
) -> Response[str]:
    """Create prediction

    :param platform: platform, e.g. "aws-emr"
    :type platform: Platform
    :param cluster_report: cluster report
    :type cluster_report: dict
    :param eventlog_url: event log URL
    :type eventlog_url: str
    :param project_id: ID of project to which the prediction belongs, defaults to None
    :type project_id: str, optional
    :return: prediction ID
    :rtype: Response[str]
    """
    scheme = urlparse(eventlog_url).scheme
    if scheme == "s3":
        response = generate_presigned_url(eventlog_url)
        if response.error:
            return response
        eventlog_http_url = response.result
    elif scheme in {"http", "https"}:
        eventlog_http_url = eventlog_url
    else:
        return Response(error=PredictionError(message="Unsupported event log URL scheme"))

    response = get_default_client().create_prediction(
        {
            "project_id": project_id,
            "product_code": platform,
            "eventlog_url": eventlog_http_url,
            "configs": cluster_report,
        }
    )

    if response.get("error"):
        return Response(**response)

    return Response(result=response["result"]["prediction_id"])

def generate_presigned_url(s3_url: str, expires_in_secs: int = 3600) -> Response[str]:
    """Generates presigned HTTP URL for S3 URL

    :param s3_url: URL of object in S3
    :type s3_url: str
    :param expires_in_secs: number of seconds after which presigned URL expires, defaults to 3600
    :type expires_in_secs: int, optional
    :return: presigned URL
    :rtype: Response[str]
    """
    parsed_s3_url = urlparse(s3_url)

    s3 = boto.client("s3")
    return Response(
        result=s3.generate_presigned_url(
            "get_object",
            Params={"Bucket": parsed_s3_url.netloc, "Key": parsed_s3_url.path.lstrip("/")},
            ExpiresIn=expires_in_secs,
        )
    )


from sklearn.datasets import fetch_lfw_people
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.svm import SVC
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt

face_data = fetch_lfw_people(min_faces_per_person=80)

X = face_data.data
Y = face_data.target
labels = face_data.target_names

print("Labels:", labels)

for i in range(5):
    print("Class", labels[i], ":", {(Y == i).sum()})

plt.show()

print("Fitting...")

x_train, x_test, y_train, y_test = train_test_split(X, Y)

svm = SVC(class_weight='balanced')

parameters = {'C': [0.1, 1, 10], 'gamma': [1e-07, 1e-08, 1e-06], 'kernel' : ['rbf', 'linear'] }

grid_search = GridSearchCV(svm, parameters, cv=5, n_jobs=-1)

grid_search.fit(x_train, y_train)

print("Best Settings for SVC:", grid_search.best_params_)

best_svm = grid_search.best_estimator_

print("Score:", best_svm.score(x_test, y_test))

pred = best_svm.predict(x_test)

print(classification_report(y_test, pred))


from PIL import Image

import ImageEnhance

myImage = Image.open("gogeta.png")
size = 700, 700
myImage.thumbnail(size)
myImage.show()

sharp = ImageEnhance.Sharpness(myImage)

sharp.enhance(4).show()
sharp.enhance(10).show()

color = ImageEnhance.Color(myImage)

color.enhance(0).show()
color.enhance(2).show()
color.enhance(4).show()

bright = ImageEnhance.Brightness(myImage)

bright.enhance(2).show()
bright.enhance(4).show()

contrast = ImageEnhance.Contrast(myImage)

contrast.enhance(2).show()
contrast.enhance(4).show()

#Check out the file image_processing_3.png for the output.


"""
Hashcode 2020 data structures and parsing helpers
"""

import logging
from typing import List, Set, TextIO, Dict

class Library:  # pylint: disable=too-few-public-methods
    """Library"""

    def __init__(self, id_: int,  # pylint: disable=too-many-arguments
                 n_books: int, signup_delay: int, books_per_day: int, books: Set[int]):
        self.id_ = id_
        self.n_books = n_books
        self.signup_delay = signup_delay  # days
        self.books_per_day = books_per_day  # books/day
        self.books = books  # books id set

class InputDataSet:  # pylint: disable=too-few-public-methods
    """Input Data"""

    def __init__(self, n_books,  # pylint: disable=too-many-arguments
                 n_libraries, n_days, book_scores, libraries):
        self.n_books = n_books
        self.n_libraries = n_libraries
        self.n_days = n_days
        self.book_scores: List[int] = book_scores  # book i score = book_scores[i]
        self.libraries: List[Library] = libraries  # List[Library]

    def __str__(self):
        return f"B: {self.n_books} L: {self.n_libraries} D: {self.n_days}"

class LibraryOrder:  # pylint: disable=too-few-public-methods
    """Library Order"""

    def __init__(self, id_: int, n_books: int, books: List[int]):
        self.id_ = id_
        self.n_books = n_books
        self.books = books  # book id list (ordered)

class OutputDataSet:  # pylint: disable=too-few-public-methods
    """Output Data"""

    def __init__(self, n_libraries, library_orders: List[LibraryOrder]):
        self.n_libraries = n_libraries
        self.library_orders = library_orders  # List[LibraryOrder]

    def __str__(self):
        return f"L: {self.n_libraries}"

def parse_input_file(input_file) -> InputDataSet:
    """
    Parse input file and return an initialized InputDataSet struct
    :param input_file: file for parsing
    :return: InputDataSet
    """
    # B, L, D
    (total_books, total_libraries, total_days) = map(int, input_file.readline().split(' '))
    book_scores = list(map(int, input_file.readline().split(' ')))
    libraries = []
    for id_ in range(total_libraries):
        (n_books, signup_delay, books_per_day) = map(int, input_file.readline().split(' '))
        books = set(map(int, input_file.readline().split(' ')))
        libraries.append(Library(id_, n_books, signup_delay, books_per_day, books))
    return InputDataSet(total_books, total_libraries, total_days, book_scores, libraries)

def parse_output_file(output_file, n_libraries, n_books) -> (bool, OutputDataSet):
    """
    Parse output file and return an initialized OutputDataSet struct
    :param output_file: file for parsing
    :param n_libraries: number of libraries from input file
    :param n_books: number of books from input file
    :return: OutputDataSet
    """
    n_orders = int(output_file.readline())  # L
    library_orders = []
    library_found: Dict[int, int] = {}  # library, line 1st definition
    books_found: Dict[int, int] = {}  # book, line 1st definition
    for line in range(n_orders):
        current_line = 2 * line + 2
        library_definition = output_file.readline()
        try:
            (id_, lib_n_books) = map(int, library_definition.split(' '))
            if id_ < 0 or id_ >= n_libraries:
                logging.warning(f"line {current_line}: library id {id_} is invalid should be >= 0 and < {n_libraries}")
                continue
            if lib_n_books == 0:
                logging.warning(f"line {current_line}: library books sent {lib_n_books} == 0")
            if id_ in library_found:
                logging.warning(f"line {current_line}: library {id_} previously defined line {library_found[id_]}")
                continue
            else:
                library_found[id_] = current_line
        except ValueError as _:
            logging.warning(
                f"line {current_line}: invalid content: '{library_definition}' should be <library_id> <books>")
            continue
        current_line = 2 * line + 3
        library_books = output_file.readline()
        try:
            books = list(map(int, library_books.split(' ')))
            for book in books:
                if book < 0 or book >= n_books:
                    logging.warning(f"line {current_line}: book id {book} is invalid should be >= 0 and < {n_books}")
                if book in books_found:
                    logging.debug(f"line {current_line}: book {book} previously defined line {books_found[book]}")
                else:
                    books_found[book] = current_line
        except ValueError as _:
            logging.warning(f"line {current_line}: invalid content: '{library_books}' should be <books>...")
            continue
        if len(books) != lib_n_books:
            logging.warning(f"line {current_line}: number of books ({len(books)}) does not match declaration at line {current_line - 1} ({lib_n_books})")
        library_orders.append(LibraryOrder(id_, lib_n_books, books))
    return OutputDataSet(n_orders, library_orders)

def write_output_file(output_data: OutputDataSet, output_file: TextIO):
    """
    Dump output data (solution) to output file
    :param output_data: solution
    :param output_file: file for dumping
    """
    output_file.write(f"{output_data.n_libraries}\n")
    for library_order in output_data.library_orders:
        output_file.write(f"{library_order.id_} {library_order.n_books}\n")
        output_file.write(" ".join(str(lib) for lib in library_order.books) + "\n")

def build_signup_schedule(input_data_set: InputDataSet, output_data_set: OutputDataSet):
    """
    Return signup schedule i.e (day signup complete, library id) list

    Note: returned list in arbitrary order
    :param input_data_set:
    :param output_data_set:
    :return: (day signup complete, library id) list
    """
    day = 0
    signup_schedule = []
    for library_order in output_data_set.library_orders:
        library = input_data_set.libraries[library_order.id_]
        day += library.signup_delay
        signup_schedule.append((day, library.id_))
    return signup_schedule


from db import db
import users

def get_rating(recipe_id):
    sql = "SELECT AVG(rating) FROM Ratings WHERE recipe_id=:recipe_id"
    result = db.session.execute(sql, {"recipe_id":recipe_id})
    rating = result.fetchone()[0]
    if rating == None:
        rating = -1
    rating = round(rating, 1)
    return rating

def rate(recipe_id, rating):
    username = users.get_user()
    if username is None:
        return False
    user_id = users.get_id()
    sql = "INSERT INTO Ratings(recipe_id, rating, user_id) VALUES (:recipe_id, :rating, :user_id)"
    db.session.execute(sql, {"recipe_id":recipe_id, "rating":rating, "user_id":user_id})
    db.session.commit()
    return True

def get_rating_status(recipe_id):
    username = users.get_user()
    if username is None:
        return True
    user_id = users.get_id()
    sql = "SELECT COUNT(*) FROM Ratings WHERE recipe_id=:recipe_id AND user_id=:user_id"
    result = db.session.execute(sql, {"recipe_id":recipe_id, "user_id":user_id})
    count = result.fetchone()[0]
    if count:
        return True
    return False


import pandas as pd
from datetime import datetime as dt

curr_search_id = ''

raw_search_weather_dict = {
'search_id': '',
    'city': '',
    'address': '',
    'latitude': 0,
    'longitude': 0,
    'timezone': '',
    'tzoffset': 0,
    'description': '',
    'user_id': 0
}

search_weather_schema = {
    'search_id': str,
    'city': str,
    'address': str,
    'latitude': float,
    'longitude': float,
    'timezone': str,
    'tzoffset': float,
    'description': str,
    'user_id': int
}

def search_weather_decode(key, value, user_id):
    try:

        src_id = str(dt.now().strftime('%Y-%m-%d_%H%M%S'))
        global curr_search_id

        if key == 'address' and len(value) > 0:
            raw_search_weather_dict['search_id'] = src_id + '_' + value.strip().upper().replace(' ', '_')
            raw_search_weather_dict['city'] = value
            curr_search_id = src_id + '_' + value.strip().upper().replace(' ', '_')
            raw_search_weather_dict['user_id'] = user_id

        if key == 'resolvedAddress' and len(value) > 0:
            raw_search_weather_dict['address'] = value

        if key == 'latitude' and len(value) > 0:
            raw_search_weather_dict['latitude'] = value

        if key == 'longitude' and len(value) > 0:
            raw_search_weather_dict['longitude'] = value

        if key == 'timezone' and len(value) > 0:
            raw_search_weather_dict['timezone'] = value

        if key == 'tzoffset' and len(value) > 0 :
            raw_search_weather_dict['tzoffset'] = value

        if key == 'description' and len(value) > 0:
            raw_search_weather_dict['description'] = value

    except Exception as e:
        print('Error in search_weather_decode()', e)

    return raw_search_weather_dict

daily_weather_schema = {
    'day_id': str,
    'search_id': str,
    'day_datetime': 'datetime64',
    'temp_max': float,
    'temp_min': float,
    'temp': float,
    'feels_like': float,
    'dew': float,
    'humidity': float,
    'precip': float,
    'precip_prob': float,
    'precip_cover': float,
    'precip_type': str,
    'snow': float,
    'snowd_depth': float,
    'wind_gust': float,
    'wind_speed': float,
    'wind_dir': float,
    'pressure': float,
    'cloud_cover': float,
    'visibility': float,
    'solar_radiation': float,
    'solar_energy': float,
    'uv_index': float,
    'severe_risk': float,
    'sun_rise': str,
    'sun_set': str,
    'conditions': str,
    'description': str,
}

def daily_weather_decode(day_list):
    try:
        filter_day_list = []
        key_list = list(day_list[0].keys())
        for day in day_list:
            raw_daily_weather_dict = {}
            if 'search_id' in key_list:
                raw_daily_weather_dict['day_id'] = str(day['datetime']) + '_' + day['search_id']
                raw_daily_weather_dict['search_id'] = day['search_id']

            if 'datetime' in key_list:
                raw_daily_weather_dict['day_datetime'] = day['datetime']

            if 'tempmax' in key_list and type(day['tempmax']) != str:
                raw_daily_weather_dict['temp_max'] = day['tempmax']
            else:
                raw_daily_weather_dict['temp_max'] = 0

            if 'tempmin' in key_list and type(day['tempmin']) != str:
                raw_daily_weather_dict['temp_min'] = day['tempmin']
            else:
                raw_daily_weather_dict['temp_min'] = 0

            if 'temp' in key_list and type(day['temp']) != str:
                raw_daily_weather_dict['temp'] = day['temp']
            else:
                raw_daily_weather_dict['temp'] = 0

            if 'feelslike' in key_list and type(day['feelslike']) != str:
                raw_daily_weather_dict['feels_like'] = day['feelslike']
            else:
                raw_daily_weather_dict['feels_like'] = 0

            if 'dew' in key_list and type(day['dew']) != str:
                raw_daily_weather_dict['dew'] = day['dew']
            else:
                raw_daily_weather_dict['dew'] = 0

            if 'humidity' in key_list and type(day['humidity']) != str:
                raw_daily_weather_dict['humidity'] = day['humidity']
            else:
                raw_daily_weather_dict['humidity'] = 0

            if 'precip' in key_list and type(day['precip']) != str:
                raw_daily_weather_dict['precip'] = day['precip']
            else:
                raw_daily_weather_dict['precip'] = 0

            if 'precipprob' in key_list and type(day['precipprob']) != str:
                raw_daily_weather_dict['precip_prob'] = day['precipprob']
            else:
                raw_daily_weather_dict['precip_prob'] = 0

            if 'precipcover' in key_list and type(day['precipcover']) != str:
                raw_daily_weather_dict['precip_cover'] = day['precipcover']
            else:
                raw_daily_weather_dict['precip_cover'] = 0

            if 'preciptype' in key_list :
                raw_daily_weather_dict['precip_type'] = day['preciptype']
            else:
                raw_daily_weather_dict['precip_type'] = '0'

            if 'snow' in key_list and type(day['snow']) != str:
                raw_daily_weather_dict['snow'] = day['snow']
            else:
                raw_daily_weather_dict['snow'] = 0

            if 'snowdepth' in key_list and type(day['snowdepth']) != str:
                raw_daily_weather_dict['snowd_depth'] = day['snowdepth']
            else:
                raw_daily_weather_dict['snowd_depth'] = 0

            if 'windgust' in key_list and type(day['windgust']) != str:
                raw_daily_weather_dict['wind_gust'] = day['windgust']
            else:
                raw_daily_weather_dict['wind_gust'] = 0

            if 'windspeed' in key_list and type(day['windspeed']) != str:
                raw_daily_weather_dict['wind_speed'] = day['windspeed']
            else:
                raw_daily_weather_dict['wind_speed'] = 0

            if 'winddir' in key_list and type(day['winddir']) != str:
                raw_daily_weather_dict['wind_dir'] = day['winddir']
            else:
                raw_daily_weather_dict['wind_dir'] = 0

            if 'pressure' in key_list and type(day['pressure']) != str:
                raw_daily_weather_dict['pressure'] = day['pressure']
            else:
                raw_daily_weather_dict['pressure'] = 0

            if 'cloudcover' in key_list and type(day['cloudcover']) != str:
                raw_daily_weather_dict['cloud_cover'] = day['cloudcover']
            else:
                raw_daily_weather_dict['cloud_cover'] = 0

            if 'visibility' in key_list and type(day['visibility']) != str:
                raw_daily_weather_dict['visibility'] = day['visibility']
            else:
                raw_daily_weather_dict['visibility'] = 0

            if 'solarradiation' in key_list and type(day['solarradiation']) != str:
                raw_daily_weather_dict['solar_radiation'] = day['solarradiation']
            else:
                raw_daily_weather_dict['solar_radiation'] = 0

            if 'solarenergy' in key_list and type(day['solarenergy']) != str:
                raw_daily_weather_dict['solar_energy'] = day['solarenergy']
            else:
                raw_daily_weather_dict['solar_energy'] = 0

            if 'uvindex' in key_list and type(day['uvindex']) != str:
                raw_daily_weather_dict['uv_index'] = day['uvindex']
            else:
                raw_daily_weather_dict['uv_index'] = 0

            if 'severerisk' in key_list and type(day['severerisk']) != str:
                raw_daily_weather_dict['severe_risk'] = day['severerisk']
            else:
                raw_daily_weather_dict['severe_risk'] = 0

            if 'sunrise' in key_list:
                raw_daily_weather_dict['sun_rise'] = day['sunrise']
            else:
                raw_daily_weather_dict['sun_rise'] = ''

            if 'sunset' in key_list:
                raw_daily_weather_dict['sun_set'] = day['sunset']
            else:
                raw_daily_weather_dict['sun_set'] = ''

            if 'conditions' in key_list:
                raw_daily_weather_dict['conditions'] = day['conditions']
            else:
                raw_daily_weather_dict['conditions'] = ''

            if 'description' in key_list:
                raw_daily_weather_dict['description'] = day['description']
            else:
                raw_daily_weather_dict['description'] = ''

            if 'hours' in key_list:
                raw_daily_weather_dict['hours'] = day['hours']

            filter_day_list.append(raw_daily_weather_dict)
        return filter_day_list

    except Exception as e:
        print('Error in daily_weather_decode()', str(e))

hourly_weather_schema = {'hour_id': str,
                         'search_id': str,
                         'day_id': str,
                         'hour_no': str,
                         'hour': str,
                         'temp': float,
                         'feels_like': float,
                         'humidity': float,
                         'dew': float,
                         'precip': float,
                         'precip_prob': float,
                         'snow': float,
                         'snow_depth': float,
                         'precip_type': str,
                         'wind_gust': float,
                         'wind_speed': float,
                         'wind_dir': float,
                         'pressure': float,
                         'visibility': float,
                         'cloud_cover': float,
                         'solar_radiation': float,
                         'solar_energy': float,
                         'uv_index': float,
                         'severe_risk': float,
                         'conditions': str,
                         'icon': str,
                         'stations': str,
                         'source': str}

final_hrs_list = []

def hourly_weather_decode(day):
    try:
        for hour in day['hours']:
            raw_hrs_weather = {}
            if 'datetime' in hour.keys():
                raw_hrs_weather['hour_id'] = hour['hour_id']
                raw_hrs_weather['search_id'] = hour['search_id']
                raw_hrs_weather['day_id'] = hour['day_id']
                raw_hrs_weather['hour_no'] = str(hour['datetime']).split(':')[0]
                raw_hrs_weather['hour'] = hour['datetime']

            if 'temp' in hour.keys():
                raw_hrs_weather['temp'] = hour['temp']

            if 'feelslike' in hour.keys() and type(hour['feelslike']) != str:
                raw_hrs_weather['feels_like'] = hour['feelslike']
            else:
                raw_hrs_weather['feels_like'] = 0

            if 'humidity' in hour.keys() and type(hour['humidity']) != str:
                raw_hrs_weather['humidity'] = hour['humidity']
            else:
                raw_hrs_weather['humidity'] = 0

            if 'dew' in hour.keys() and type(hour['dew']) != str:
                raw_hrs_weather['dew'] = hour['dew']
            else:
                raw_hrs_weather['dew'] = 0

            if 'precip' in hour.keys() and type(hour['precip']) != str:
                raw_hrs_weather['precip'] = hour['precip']
            else:
                raw_hrs_weather['precip'] = 0

            if 'precipprob' in hour.keys() and type(hour['precipprob']) != str:
                raw_hrs_weather['precip_prob'] = hour['precipprob']
            else:
                raw_hrs_weather['precip_prob'] = 0

            if 'snow' in hour.keys() and type(hour['snow']) != str:
                raw_hrs_weather['snow'] = hour['snow']
            else:
                raw_hrs_weather['snow'] = 0

            if 'snowdepth' in hour.keys() and type(hour['snowdepth']) != str:
                raw_hrs_weather['snow_depth'] = hour['snowdepth']
            else:
                raw_hrs_weather['snow_depth'] = 0

            if 'preciptype' in hour.keys() :
                raw_hrs_weather['precip_type'] = hour['preciptype']
            else:
                raw_hrs_weather['precip_type'] = '0'

            if 'windgust' in hour.keys() and type(hour['windgust']) != str:
                raw_hrs_weather['wind_gust'] = hour['windgust']
            else:
                raw_hrs_weather['wind_gust'] = 0

            if 'windspeed' in hour.keys() and type(hour['windspeed']) != str:
                raw_hrs_weather['wind_speed'] = hour['windspeed']
            else:
                raw_hrs_weather['wind_speed'] = 0

            if 'winddir' in hour.keys() and type(hour['winddir']) != str:
                raw_hrs_weather['wind_dir'] = hour['winddir']
            else:
                raw_hrs_weather['wind_dir'] = 0

            if 'pressure' in hour.keys() and type(hour['pressure']) != str:
                raw_hrs_weather['pressure'] = hour['pressure']
            else:
                raw_hrs_weather['pressure'] = 0

            if 'visibility' in hour.keys() and type(hour['visibility']) != str:
                raw_hrs_weather['visibility'] = hour['visibility']
            else:
                raw_hrs_weather['visibility'] = 0

            if 'cloudcover' in hour.keys() and type(hour['cloudcover']) != str:
                raw_hrs_weather['cloud_cover'] = hour['cloudcover']
            else:
                raw_hrs_weather['cloud_cover'] = 0

            if 'solarradiation' in hour.keys() and type(hour['solarradiation']) != str:
                raw_hrs_weather['solar_radiation'] = hour['solarradiation']
            else:
                raw_hrs_weather['solar_radiation'] = 0

            if 'solarenergy' in hour.keys() and type(hour['solarenergy']) != str:
                raw_hrs_weather['solar_energy'] = hour['solarenergy']
            else:
                raw_hrs_weather['solar_energy'] = 0

            if 'uvindex' in hour.keys() and type(hour['uvindex']) != str:
                raw_hrs_weather['uv_index'] = hour['uvindex']
            else:
                raw_hrs_weather['uv_index'] = 0

            if 'severerisk' in hour.keys() and type(hour['severerisk']) != str:
                raw_hrs_weather['severe_risk'] = hour['severerisk']
            else:
                raw_hrs_weather['severe_risk'] = 0

            if 'conditions' in hour.keys():
                raw_hrs_weather['conditions'] = hour['conditions']
            else:
                raw_hrs_weather['conditions'] = ''

            if 'icon' in hour.keys():
                raw_hrs_weather['icon'] = hour['icon']
            else:
                raw_hrs_weather['icon'] = ''

            if 'stations' in hour.keys():
                raw_hrs_weather['stations'] = hour['stations']
            else:
                raw_hrs_weather['stations'] = ''

            if 'source' in hour.keys():
                raw_hrs_weather['source'] = hour['source']
            else:
                raw_hrs_weather['source'] = ''

            final_hrs_list.append(raw_hrs_weather)

        return final_hrs_list
    except Exception as e:
        print('Error in hourly_weather_decode()', str(e))

curr_weather_schema = {'curr_weather_id': str,
                       'search_id': str,
                       'search_hour': str,
                       'temp': float,
                       'feels_like': float,
                       'humidity': float,
                       'dew': float,
                       'precip': float,
                       'precip_prob': float,
                       'snow': float,
                       'snow_depth': float,
                       'precip_type': str,
                       'wind_gust': float,
                       'wind_speed': float,
                       'wind_dir': float,
                       'pressure': float,
                       'visibility': float,
                       'cloud_cover': float,
                       'solar_radiation': float,
                       'solar_energy': float,
                       'uv_index': float,
                       'severe_risk': float,
                       'conditions': str,
                       'icon': str,
                       'stations': str,
                       'source': str,
                       'sun_rise': str,
                       'sun_set': str}

def current_weather_decode(curr_dict):
    try:
        global curr_search_id
        raw_curr_weather = {}
        raw_curr_weather['curr_weather_id'] = curr_dict['datetime'].replace(':', '-') + '_' + curr_search_id
        raw_curr_weather['search_id'] = curr_search_id
        raw_curr_weather['search_hour'] = curr_dict['datetime']

        if 'temp' in curr_dict.keys() and type(curr_dict['temp']) != str:
            raw_curr_weather['temp'] = curr_dict['temp']
        else:
            raw_curr_weather['temp'] = 0

        if 'feelslike' in curr_dict.keys() and type(curr_dict['feelslike']) != str:
            raw_curr_weather['feels_like'] = curr_dict['feelslike']
        else:
            raw_curr_weather['feels_like'] = 0

        if 'humidity' in curr_dict.keys() and type(curr_dict['humidity']) != str:
            raw_curr_weather['humidity'] = curr_dict['humidity']
        else:
            raw_curr_weather['humidity'] = 0

        if 'dew' in curr_dict.keys() and type(curr_dict['dew']) != str:
            raw_curr_weather['dew'] = curr_dict['dew']
        else:
            raw_curr_weather['dew'] = 0

        if 'precip' in curr_dict.keys() and type(curr_dict['precip']) != str:
            raw_curr_weather['precip'] = curr_dict['precip']
        else:
            raw_curr_weather['precip'] = 0

        if 'precipprob' in curr_dict.keys() and type(curr_dict['precipprob']) != str:
            raw_curr_weather['precip_prob'] = curr_dict['precipprob']
        else:
            raw_curr_weather['precip_prob'] = 0

        if 'snow' in curr_dict.keys() and type(curr_dict['snow']) != str:
            raw_curr_weather['snow'] = curr_dict['snow']
        else:
            raw_curr_weather['snow'] = 0

        if 'snowdepth' in curr_dict.keys() and type(curr_dict['snowdepth']) != str:
            raw_curr_weather['snow_depth'] = curr_dict['snowdepth']
        else:
            raw_curr_weather['snow_depth'] = 0

        if 'preciptype' in curr_dict.keys() :
            raw_curr_weather['precip_type'] = curr_dict['preciptype']
        else:
            raw_curr_weather['precip_type'] = '0'

        if 'windgust' in curr_dict.keys() and type(curr_dict['windgust']) != str :
            raw_curr_weather['wind_gust'] = curr_dict['windgust']
        else:
            raw_curr_weather['wind_gust'] = 0

        if 'windspeed' in curr_dict.keys() and type(curr_dict['windspeed']) != str:
            raw_curr_weather['wind_speed'] = curr_dict['windspeed']
        else:
            raw_curr_weather['wind_speed'] = 0

        if 'winddir' in curr_dict.keys() and type(curr_dict['winddir']) != str:
            raw_curr_weather['wind_dir'] = curr_dict['winddir']
        else:
            raw_curr_weather['wind_dir'] = 0

        if 'pressure' in curr_dict.keys() and type(curr_dict['pressure']) != str:
            raw_curr_weather['pressure'] = curr_dict['pressure']
        else:
            raw_curr_weather['pressure'] = 0

        if 'visibility' in curr_dict.keys() and type(curr_dict['visibility']) != str:
            raw_curr_weather['visibility'] = curr_dict['visibility']
        else:
            raw_curr_weather['visibility'] = 0

        if 'cloudcover' in curr_dict.keys() and type(curr_dict['cloudcover']) != str:
            raw_curr_weather['cloud_cover'] = curr_dict['cloudcover']
        else:
            raw_curr_weather['cloud_cover'] = 0

        if 'solarradiation' in curr_dict.keys() and type(curr_dict['solarradiation']) != str:
            raw_curr_weather['solar_radiation'] = curr_dict['solarradiation']
        else:
            raw_curr_weather['solar_radiation'] = 0

        if 'solarenergy' in curr_dict.keys() and type(curr_dict['solarenergy']) != str:
            raw_curr_weather['solar_energy'] = curr_dict['solarenergy']
        else:
            raw_curr_weather['solar_energy'] = 0

        if 'uvindex' in curr_dict.keys() and type(curr_dict['uvindex']) != str:
            raw_curr_weather['uv_index'] = curr_dict['uvindex']
        else:
            raw_curr_weather['uv_index'] = 0

        if 'severerisk' in curr_dict.keys() and type(curr_dict['severerisk']) != str:
            raw_curr_weather['severe_risk'] = curr_dict['severerisk']
        else:
            raw_curr_weather['severe_risk'] = 0

        if 'conditions' in curr_dict.keys():
            raw_curr_weather['conditions'] = curr_dict['conditions']
        else:
            raw_curr_weather['conditions'] = ''

        if 'icon' in curr_dict.keys():
            raw_curr_weather['icon'] = curr_dict['icon']
        else:
            raw_curr_weather['icon'] = ''

        if 'stations' in curr_dict.keys():
            raw_curr_weather['stations'] = curr_dict['stations']
        else:
            raw_curr_weather['stations'] = ''

        if 'source' in curr_dict.keys():
            raw_curr_weather['source'] = curr_dict['source']
        else:
            raw_curr_weather['source'] = ''

        if 'sunrise' in curr_dict.keys():
            raw_curr_weather['sun_rise'] = curr_dict['sunrise']
        else:
            raw_curr_weather['sun_rise'] = ''

        if 'sunset' in curr_dict.keys():
            raw_curr_weather['sun_set'] = curr_dict['sunset']
        else:
            raw_curr_weather['sun_set'] = ''

        return raw_curr_weather
    except Exception as e:
        print('Error in current_weather_decode()', str(e))

def decode_raw_json(raw_data, user_id):
    try:
        filtered_weather_data = {
            'user_id': "",
            'search_id': "",
            'src_city': "",
            'daily_weather': "",
            'hourly_weather': "",
            'current_weather': ""
        }
        src_weather_dict = {}
        # PART : 1 decoding main weather json
        # to create search_id
        for key, value in raw_data.items():
            if key != 'days':
                src_weather_dict = search_weather_decode(key, str(value), user_id)

        # setting search_id
        search_id = src_weather_dict['search_id']

        # converting return dict into dataframe and applying schema for type changed
        search_weather_df = pd.DataFrame([src_weather_dict]).astype(search_weather_schema)

        # PART : 2 Decoding daily weather json
        # decoding daily weather json and adding search_id
        for i in range(len(raw_data['days'])):
            raw_data['days'][i]['search_id'] = search_id

        filter_day_list = daily_weather_decode(raw_data['days'])

        # creating dataframe from raw day weather
        daily_weather_df = pd.DataFrame(filter_day_list, columns=list(daily_weather_schema.keys())).astype(
            daily_weather_schema)
        daily_weather_df.fillna(value=0, inplace=True)

        # PART : 3
        # created hours list to store hourly weather details
        for day in filter_day_list:
            for hour in day['hours']:
                hour['hour_id'] = hour['datetime'] + '_' + day['day_id']
                hour['search_id'] = search_id
                hour['day_id'] = day['day_id']
                hour['hour_no'] = str(hour['datetime'].split(':')[0])

        hours_list = []
        for day in filter_day_list:
            hours_list = hourly_weather_decode(day)

        # converting hours_list into dataframe
        hourly_weather_df = pd.DataFrame(hours_list, columns=list(hourly_weather_schema.keys())).astype(
            hourly_weather_schema)
        hourly_weather_df.fillna(value=0, inplace=True)
        hourly_weather_df.drop_duplicates(inplace=True)

        # setting current weather details
        curr_weather_df = current_weather_decode(raw_data['currentConditions'])
        curr_weather_df = pd.DataFrame([curr_weather_df]).astype(curr_weather_schema)
        curr_weather_df.fillna(value=0, inplace=True)

        filtered_weather_data['user_id'] = user_id
        filtered_weather_data['search_id'] = search_id
        filtered_weather_data['search_city'] = search_weather_df
        filtered_weather_data['daily_weather'] = daily_weather_df
        filtered_weather_data['hourly_weather'] = hourly_weather_df
        filtered_weather_data['current_weather'] = curr_weather_df

        return filtered_weather_data
    except Exception as e:
        print('Error in decode_raw_json()',e)


#
# @lc app=leetcode.cn id=1546 lang=python3
#
# [1546] 和为目标值且不重叠的非空子数组的最大数目
#
from prelude import *
# @lc code=start

class Solution:
    def maxNonOverlapping(self, nums: List[int], target: int) -> int:
        n = len(nums)
        count = 0

        i = 0
        while i < n:
            # 从 i 开始计算前缀和
            prefixes = {0}
            prefix = 0
            while i < n:
                prefix += nums[i]
                if prefix - target in prefixes:
                    count += 1
                    break
                else:
                    prefixes.add(prefix)
                    i += 1
            # 找到了一个子数组
            i += 1

        return count

# @lc code=end
if __name__ == '__main__':
    def test(nums, target, expected):
        calc = Solution().maxNonOverlapping(nums, target)
        if calc != expected:
            print(f'case failed: `{nums, target}`')
            print(f'  calc     = `{calc}`')
            print(f'  expected = `{expected}`')
            exit(1)
    test(nums=[1, 1, 1, 1, 1], target=2, expected=2)
    test(nums=[-1, 3, 5, 1, 4, 2, -9], target=6, expected=2)
    test(nums=[-2, 6, 6, 3, 5, 4, 1, 2, 8], target=10, expected=3)
    test(nums=[0, 0, 0], target=0, expected=3)

src/1546.和为目标值且不重叠的非空子数组的最大数目.py

1546.和为目标值且不重叠的非空子数组的最大数目.py


from openpyxl import load_workbook
from docx import Document

# Excel-Tabelle öffnen
workbook = load_workbook('deine_tabelle.xlsx')
sheet = workbook.active

# Beispielhafte Verarbeitung der Werte
wert1 = sheet['A1'].value
wert2 = sheet['B1'].value

# Word-Dokument erstellen
document = Document()

# Text mit Formatierung hinzufügen
paragraph = document.add_paragraph()
run = paragraph.add_run()
run.text = f"Der Wert 1 ist {wert1} und der Wert 2 ist {wert2}."
run.bold = True
run.italic = True

# Word-Dokument speichern
document.save('ergebnis.docx')


import marisco

responses = marisco.get([
    'https://python.org',
    'https://pypi.org',
    'https://github.com',
    'https://gitlab.com',
])

for response in responses:
    print(len(response))

urls = (
    f'https://xkcd.com/{i}/'
    for i in range(1, 100)
)

with marisco.Session(max_connections_per_host=20) as session:
    for response in session.get(urls):
        print(len(response))


import json
import os

def hello_pubsub(event, context):

     if event:
          print(event)

          pubsub_message = json.dumps(event['data']) + '\n'
          print(pubsub_message)
          path = '/data/pv01/'
          print("Using folder: " + path)
          log_file_name = path + 'log.txt'

          # Mais simples que o google, MAS não eh safe pra acesso concorrente!!!
          with open(log_file_name,'a') as log_file:
               log_file.write(pubsub_message)

     else:
          print("Erro sem data no event")


from app.models import Patient, Embryo
from django.contrib.auth.models import User
from rest_framework import serializers
from datetime import datetime

class UserSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
	    view_name='user-detail',
	    lookup_field='username'
    )

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')

#class PatientSerializer(serializers.ModelSerializer):
#    #user = serializers.HyperlinkedIdentityField(
#	   # view_name='user-detail',
#	   # lookup_field='id'
#    #)

#    class Meta:
#        model = Patient
#        fields = ('first_name','last_name','email','phone','user')

class PatientSerializer(serializers.ModelSerializer):
    #user = UserSerializer()# usting this will include the full user object data in the patient - not what we want
    #this code below will return the user id and it's essentially the same as the version above that uses a ModelSerializer
    #in both cases the user value is an id. removing the .lookup_field='username' yields the following error which I could not
    #get past: Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include
    #the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

    user = serializers.HyperlinkedIdentityField(
	    view_name='user-detail',
	    lookup_field='user'
    ).lookup_field='username'

    class Meta:
        model = Patient
        fields = ('first_name','last_name','email','phone','user')

#using the HyperlinkedModelSerializer requires the use of the url to reference the patient on POST/Create as so:http://localhost:8000/patient/1/
#instead of using the user id
#class EmbryoSerializer(serializers.HyperlinkedModelSerializer): #using this HyperlinkModelSelector returns the patient as url to locate the data
class EmbryoSerializer(serializers.ModelSerializer): #using the ModelSerializer returns the object's id instead of a hyperlink
    class Meta:
        model = Embryo
        fields = ('code_name','karyotype','patient')


import numpy as np
import sys
import os
from plyfile import PlyData, PlyElement

def read_ply(ply_path):
    plydata = PlyData.read(ply_path)
    points = np.asarray(plydata['vertex'].data.tolist())
    return points

def write_ply(points,output_file):

    with open(output_file, 'w') as ply:
        header = ['ply']
        header.append('format ascii 1.0')
        header.append('element vertex {}'.format(points.shape[0]))
        header.append('property float x')
        header.append('property float y')
        header.append('property float z')
        # header.append('property float intensity')
        header.append('end_header')

        for line in header:
            ply.write("%s\n" % line)

        for p in points:
            ply.write("{} {} {} {}\n".format(p[0], p[1], p[2]))

    return

def process_point_cloud(input_cluster_path, output_cluster_path):

    pcd = read_ply(input_cluster_path)
    write_ply(pcd, output_cluster_path)

    return

if __name__ == '__main__':

    # Path to input folder
    input_clusters_dir = sys.argv[1]

    # Path to output folder
    output_clusters_dir = sys.argv[2]

    # Iterate input folder
    for subdir, dirs, files in os.walk(input_clusters_dir):
        for file in files:
            if '.ply' in file:
                input_cluster_path = os.path.join(subdir,file)
                output_cluster_path = os.path.join(output_clusters_dir, file)

                process_point_cloud(input_cluster_path, output_cluster_path)


class Student:

  def getData(self):
   self.rollno=input("Enter the rollno: ")
   self.name=input("Enter the name: ")
   self.branch=input("Enter the branch: ")
   self.phy=int(input("Enter the physics: "))
   self.che=int(input("Enter the chemistry: "))
   self.math=int(input("Enter the maths: "))

  def showData(self):
   print("Roll Number is: ",self.rollno)
   print("Student Name is: ",self.name)
   print("Branch is: ",self.branch)
   print("Physics marks is: ",self.phy)
   print("Chemistry marks is: ",self.che)
   print("Maths marks is: ",self.math)

  def totalmarks(self):
   total=self.phy + self.che + self.math
   print("Total marks: ",total)

  def percentage(self):
   per=(self.phy + self.che + self.math)/3
   print("percentage: ",per)

s=Student()
s.getData()
print("---------------------Student Information-------------------")
s.showData()
print("------------------------------------------")
s.totalmarks()
s.percentage()


from dotenv import load_dotenv
from bs4 import BeautifulSoup
import pika
import json
import sys
import os
load_dotenv()
sys.path.append(os.path.abspath(os.getenv("system_path")))
from lib import rabbit_mq, request, mongo

try:
    connection = rabbit_mq.create_connection()
    channel = connection.channel()

    channel.queue_declare(queue='mongo_jobs_save', durable=True)
    channel.queue_declare(queue='shine_html_parse')

    count = 0

    def callback(ch, method, properties, body):
        global count
        count += 1
        print("count::", count)
        # print(" [x] Received %r" % body)
        soup = BeautifulSoup(body, 'html.parser')
        res = soup.find_all("li", class_="search_listingleft search_listingleft_100")
        print("\n\n\nJob count before data formation:",len(res))
        print("Creating job data...")
        j = 0
        for item in res:
            # print(j)
            job = {}
            job['title'] = item.find("li",class_="snp cls_jobtitle").getText().strip() if item.find("li",class_="snp cls_jobtitle") else ''
            job['company'] = item.find("li",class_="snp_cnm cls_cmpname cls_jobcompany").getText().strip() if item.find("li",class_="snp_cnm cls_cmpname cls_jobcompany") else ''
            job['experience'] = item.find("span", class_="snp_yoe cls_jobexperience").getText().strip() if item.find("span", class_="snp_yoe cls_jobexperience") else ''
            job['description'] = item.find("li", class_="srcresult").getText().strip() if item.find("li", class_="srcresult") else ''
            job['location'] = item.find("em", class_="snp_loc").getText().strip() if item.find("em", class_="snp_loc") else ''
            # job['skills'] = item.find(["div", "span", "mark"], class_="sk jsrp cls_jobskill").getText().strip() if item.find(["div", "mark"], class_="sk jsrp cls_jobskill") else ''
            job['job_link'] = "https://www.shine.com" + item.find("a", class_="cls_searchresult_a searchresult_link").get('href') if item.find("a", class_="cls_searchresult_a searchresult_link") else ''
            job['link_2'] = ''
            job['salary'] = ''
            job['source'] = 'shine'
            skillsData = item.find(["div", "sk jsrp cls_jobskill"]) if item.find(["div", "sk jsrp cls_jobskill"]) else ''
            if len(skillsData) > 0:
                spans = skillsData.find_all('span') if skillsData.find_all('span') else ''
                if len(spans) > -1:
                    skills = []
                    for span in spans:
                        skills.append(span.getText().strip())
                    skills.pop(0)
            job['skills'] = skills

            channel.basic_publish(exchange='', routing_key='mongo_jobs_save', body=json.dumps(job))

    channel.basic_consume(
        queue='shine_html_parse', on_message_callback=callback, auto_ack=True)
    # channel.basic_consume(callback, 'shine_html_parse',  no_ack=False)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
except Exception as e:
    error = {
        "status": "Shine......... Error occured while parsing html",
        "errorMsg": e
    }
    print("Error: ",error)


import requests
from bs4 import BeautifulSoup
import json
import re
from urllib.parse import urlparse, quote, quote_plus
import io
import time
import os
import threading
import argparse

headers = {
    "sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "Windows",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Mobile Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-User": "?1",
    "Sec-Fetch-Dest": "document",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-GB,en;q=0.9",
}

all_images = {}

threads_running = 0 # don't change this

def get_extension(url):
    path = urlparse(url).path.strip('/')
    path,filename = os.path.split(path)
    filename,ext = os.path.splitext(filename)
    if ext:
        return ext
    else:
        return '.jpg'

def threaded_download(savepath,url):
    global threads_running

    try:
        r = requests.get(url,headers=headers)
    except Exception as e:
        print("Request Error : "+url)
        threads_running-=1
        return

    if r.status_code==200:
        if 'image' in r.headers.get('content-type','').lower():
            with open(savepath,'wb',) as f:
                f.write(r.content)
            print('Downloaded '+os.path.basename(savepath))
        else:
            print("Not an Image : "+url)

    else:
        print(f"{r.status_code} Error : "+url+" ")

    threads_running-=1

def collect_images_from_google(query):
    chunk = 0
    step = 0
    prev_len = 0
    no_more = 0

    while True:
        r = requests.get(f'https://www.google.com/search?q={query}&tbm=isch&biw=1271&bih=697&async=_id:islrg_c,_fmt:json&asearch=ichunklite&ved=0ahUKEwjw_KTXkM_3AhVJxDgGHR-pDeoQtDIIPSgA&start={chunk}&ijn={step}',headers=headers)

        if r.status_code==200:
            json_text = r.content.decode('utf8').removeprefix(")]}'")
            json_data = json.loads(json_text)
            try:
                results = json_data['ichunklite']['results']
                for result in results:
                    original_image = result['viewer_metadata']['original_image']['url']
                    all_images[original_image] = result['image_docid']
            except:
                pass

            chunk +=100
            step +=1

        if no_more>5:
            break
        if prev_len==len(all_images):
            no_more+=1
        else:
            print(len(all_images))
            prev_len = len(all_images)
            no_more = 0

def download_images(query):
    global threads_running
    print("Downloading "+str(len(all_images))+" images")
    if not os.path.exists(os.path.join("Images",f"{query}")):
        os.makedirs(os.path.join("Images",f"{query}"))

    for image_link,doc_id in all_images.items():
        image_save_path = os.path.join("Images",f"{query}",doc_id+get_extension(image_link))
        if os.path.exists(image_save_path):
            if os.path.getsize(image_save_path)>1:
                continue
        threading.Thread(target=threaded_download,kwargs={'url':image_link,'savepath':image_save_path}).start()
        threads_running+=1
        while threads_running>9:
            time.sleep(1)

    while threads_running>0:
        time.sleep(1)

if __name__=="__main__":
    parser = argparse.ArgumentParser(description = "Download images from google")
    parser.add_argument("-k", "--keyword", help = "Example: Help argument", required = True, default = "")
    arguments = parser.parse_args()
    keyword= arguments.keyword

    collect_images_from_google(keyword)
    download_images(keyword)


import re
from models import Quote
import redis

# Підключення до Redis
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0)

def search_by_tags(tags):

    # Перевіряємо, чи результат не зберігався у кеші Redis
    tags_str = ','.join(tags)
    cached_result = redis_client.get(f'tags:{tags_str}')
    if cached_result:
        return cached_result.decode('utf-8')

    # Видаляємо можливі пробіли з тегів і виконуємо пошук у базі даних MongoDB
    cleaned_tags = [tag.strip() for tag in tags]
    quotes = Quote.objects(tags__in=cleaned_tags)
    result = '\n'.join([quote.quote for quote in quotes])

    # Зберігаємо результат у кеші Redis на 5 хвилин
    redis_client.setex(f'tags:{tags_str}', 300, result)
    return result

def search_quotes():
    # Функція для пошуку цитат за ім'ям автора, тегом або набором тегів
    while True:
        command = input("Введіть команду: ")

        if command.startswith('name:'):
            name = command.replace('name:', '').strip()
            result = search_by_author_name(name)
            print(result)
        elif command.startswith('tag:'):
            tag = command.replace('tag:', '').strip()
            result = search_by_tags([tag])
            print(result)
        elif command.startswith('tags:'):
            tags = command.replace('tags:', '').strip().split(',')
            result = search_by_tags(tags)
            print(result)
        elif command == 'exit':
            break
        else:
            print("Невірна команда. Спробуйте ще раз.")

def search_by_author_name(name):

    # Перевіряємо, чи результат не зберігався у кеші Redis
    cached_result = redis_client.get(f'author:{name}')
    if cached_result:
        return cached_result.decode('utf-8')
    print(f"Ім'я для пошуку: {name}")

    quotes = Quote.objects()

    # Фільтруємо цитати за ім'ям автора, використовуючи регулярний вираз у Python
    filtered_quotes = [quote for quote in quotes if re.search(f'.*{re.escape(name)}.*', quote.author.fullname, re.IGNORECASE)]

    result = '\n'.join([quote.quote for quote in filtered_quotes])
    print(f"Кількість знайдених цитат: {len(filtered_quotes)}")

    # Зберігаємо результат у кеші Redis на 5 хвилин
    redis_client.setex(f'author:{name}', 300, result)
    return result


#!/usr/bin/env python3

from pytube import Playlist, YouTube

# download directory
download_destination = '/Users/vijoy.vallachira/YouTubeDump'

# To download a single video
def download_video():
    youtube_video_source = input("Enter Youtube Video URL : ")

    # Downloading the highest resolution, progressive type video.
    print("Downloading %s" % youtube_video_source)
    YouTube(youtube_video_source).streams.get_highest_resolution().download(download_destination)

    ## YouTube(youtube_video_source).streams.filter(progressive=True, file_extension='mp4').order_by('resolution')[-1].download()

    return

# To download entire Playlist
def download_playlist():
    youtube_playlist = input("Enter Youtube Playlist URL : ")
    pl = Playlist(youtube_playlist)
    pl.populate_video_urls()
    print('Title of the playlist: %s' % pl.title())
    print('Number of videos in playlist: %s' % len(pl.video_urls))
    pl_video_list = pl.video_urls
    print("Downloading Playlist Videos \n")

    # printing the list using loop
    for x in range(len(pl_video_list)):
        print(str(x) + " >> " + pl_video_list[x])
        individual_video_stream = YouTube(pl_video_list[x]).streams.filter(mime_type="video/mp4").first()
        individual_video_stream.download(download_destination, "PhysicsSession"+str(x), "unAcademy-ClassXI-JEE")

    # print(*pl_video_list, sep="\n")
    # pl.download_all(download_destination)

    return

def display_video_streams():
    youtube_video_source = input("Enter Youtube URL : ")

    yt=YouTube(youtube_video_source)
    print(yt.streams)

    return

option = input("Download a Single [V]ideo OR [P]laylist OR  [Di]splay available Videos? [D, V or P] : ")
# download_destination = input("Download Directory : ")

if option == 'V':
    download_video()
elif option == 'P':
    download_playlist()
elif option == "D":
    display_video_streams()
else:
    print("Invalid Option...! Only 'D', 'V' or 'P' allowed.....")

print("Good Bye!")


# Fazer a média aritmética de 50 alunos utilizando o for
contador = 0
acumulador = 0
mediaAnualTurma = 0

for contador in range(5):
    mediaAnualAluno = float(input('Insira a nota do aluno: '))
    acumulador += mediaAnualAluno

mediaAnualTurma = acumulador/5
print('A media anual da turma é: ', mediaAnualTurma)


import sys

# PS
# 각 알파벳이 몇번째 자리에 쓰이는지 파악
n = int(sys.stdin.readline())
word_list = [sys.stdin.readline().rstrip() for i in range(n)]
alpha_list = {}
num_list = []

for word in word_list:
    for i in range(len(word)):
        if word[i] in alpha_list.keys():
            alpha_list[word[i]] += pow(10, len(word)-1-i)  # 자릿수를 더해준다.
        else:
            alpha_list[word[i]] = pow(10, len(word)-1-i)

for value in alpha_list.values():
    num_list.append(value)

num_list.sort(reverse=True)

answer, number = 0, 9
for num in num_list:
    answer += num*number
    number -= 1

print(answer)


# week03_02_chap03_04

def add_data(friend):
    katok.append(None)
    katok[len(katok) - 1] = friend

def insert_data(position, friend):
    if position < 0 or position > len(katok):
        print("데이터를 삽입할 범위를 벗어났습니다.")
        return

    katok.append(None)  # 빈칸 추가

    for i in range(len(katok) - 1, position, -1):
        katok[i] = katok[i - 1]
        katok[i - 1] = None

    katok[position] = friend  # 배열의 제일 뒤에 친구 추가

def delete_data(position):
    if position < 0 or position > len(katok):
        print("데이터를 삭제할 범위를 벗어났습니다.")
        return

    k_len = len(katok)
    katok[position] = None

    for i in range(position + 1, k_len):
        katok[i - 1] = katok[i]
        katok[i] = None

    del (katok[k_len - 1])

katok = []

if __name__ == "__main__":
    while True:
        menu = input("선택하세요(1: 추가, 2: 삽입, 3: 삭제, 4: 종료)--> ")

        if menu == "1":
            data = input("추가할 데이터--> ")
            add_data(data)
            print(katok)
        elif menu == "2":
            pos = int(input("삽입할 위치--> "))
            data = input("추가할 데이터--> ")
            insert_data(pos, data)
            print(katok)
        elif menu == "3":
            pos = int(input("삭제할 위치--> "))
            delete_data(pos)
            print(katok)
        elif menu == "4":
            print(katok)
            #exit(1)  # 즉시 종료
            break
        else:
            print("1~4 중 하나를 입력하세요.")
    print("프로그램을 종료합니다")


from ..base import to_attr
from ..autocorrelograms import AutoCorrelogramsWidget
from .base_sortingview import SortingviewPlotter

class AutoCorrelogramsPlotter(SortingviewPlotter):
    default_label = "SpikeInterface - Auto Correlograms"

    def do_plot(self, data_plot, **backend_kwargs):
        import sortingview.views as vv

        backend_kwargs = self.update_backend_kwargs(**backend_kwargs)
        dp = to_attr(data_plot)
        unit_ids = self.make_serializable(dp.unit_ids)

        ac_items = []
        for i in range(len(unit_ids)):
            for j in range(i, len(unit_ids)):
                if i == j:
                    ac_items.append(
                        vv.AutocorrelogramItem(
                            unit_id=unit_ids[i],
                            bin_edges_sec=(dp.bins/1000.).astype("float32"),
                            bin_counts=dp.correlograms[i, j].astype("int32")
                        )
                    )

        v_autocorrelograms = vv.Autocorrelograms(
            autocorrelograms=ac_items
        )

        self.handle_display_and_url(v_autocorrelograms, **backend_kwargs)
        return v_autocorrelograms

AutoCorrelogramsPlotter.register(AutoCorrelogramsWidget)


#!/bin/bash
import os.path
from pathlib import Path
from shutil import copyfile, move

with open("paths.temp") as f:
    ls = f.readlines()
ls = [Path(i.rstrip()) for i in ls]

for i in ls:
    if (i/"CONTCAR").is_file():
        if os.path.getsize(i/"CONTCAR") > 0:
            try:
                print(f"Move CONTCAR to POSCAR for {i}")
                move(i/"POSCAR", i/"OLD_POSCAR")
                copyfile(i/"CONTCAR", i/"POSCAR")
            except:
                print(f"Error for: {i}")

    if not (i/"POSCAR").is_file() and not (i/"CONTCAR").is_file() and (i / "OLD_POSCAR").is_file() and os.path.getsize(i/"OLD_POSCAR")>0:
        move(i/"OLD_POSCAR", i/"POSCAR")
        print(f"Restore OLD_POSCAR to POSCAR for {i}")

important script and software/script/cont2pos.py


A = list(input())
L = len(A)
A = list(map(int, A))

ans = L
for i in range(2**L):
    tmp = 0
    count = 0
    for j in range(L):
        if (i >> j) % 2 == 1:
            tmp += A[j]
        else:
            count += 1
    if (tmp % 3) == 0 and count < ans:
        ans = count
if ans == L:
    ans = -1
print(ans)