text:
<repo_name>faker690/ConsoleFileOperator<file_sep>/ConsoleFileOperator/Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleFileOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            FileUtil util = new FileUtil();
            util.StartService(true);
            //foreach(var item in Directory.GetFiles("C:\\Program Files")){
            //    string loc = "C:\\Program Files";
            //    string logpath = Path.Combine(loc,"log");
            //    Console.WriteLine(item.Split('\\').LastOrDefault());
            //    Console.WriteLine(logpath);
            //}
            Console.Read();
        }
    }
}
<file_sep>/ConsoleFileOperator/XmlUtil.cs
using System;
using System.IO;
using System.Xml;

namespace ConsoleFileOperator
{
    public class XmlUtil
    {
        public string GetNodeValueByName(string name)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Environment.CurrentDirectory);
            string xmlName = Path.Combine(dirInfo.Parent.Parent.FullName, "Config.xml");

            XmlReaderSettings setting = new XmlReaderSettings();
            setting.IgnoreComments = true;
            XmlReader reader = XmlReader.Create(xmlName, setting);

            XmlDocument xml = new XmlDocument();
            xml.Load(reader);
            reader.Close();

            XmlNode root = xml.SelectSingleNode("root");
            XmlNodeList list = root.ChildNodes;
            foreach (var item in list)
            {
                XmlElement ele = (XmlElement)item;
                if (ele.Name.Equals(name))
                {
                    if (!string.IsNullOrEmpty(ele.InnerText))
                    {
                        return ele.InnerText.Trim();
                    }
                }
            }
            return null;
        }
    }
}
<file_sep>/ConsoleFileOperator/FileUtil.cs
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleFileOperator
{
    public class FileUtil
    {
        public XmlUtil xmlUtil = new XmlUtil();
        /// <summary>
        /// find all the files in the directory
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string[] GetFileNames(string filePath)
        {
            if (Directory.Exists(filePath))
            {
                return Directory.GetFiles(filePath);
            }
            return null;
        }
        /// <summary>
        /// get directory ,if not exist ,create it.
        /// </summary>
        /// <param name="loc"></param>
        /// <returns></returns>
        public string GetDirectory(string loc)
        {
            if (Directory.Exists(loc))
            {
                return loc;
            }
            else
            {
                return Directory.CreateDirectory(loc).FullName;
            }
        }
        /// <summary>
        /// write into log
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="logPath"></param>
        public void RecordLog(string fileName, string logPath, string exception)
        {
            string name = string.Format("{0}{1}", DateTime.Now.Date.ToString("yyyyMMdd"), "Log.txt");
            string logFile = Path.Combine(GetDirectory(logPath), name);

            string content;
            if (!string.IsNullOrEmpty(exception))
            {
                content = string.Format("{0}: Error: {1} !Exception: {2}\n", DateTime.Now, fileName, exception);
            }
            else {
                content = string.Format("{0}: {1} is completed !\n", DateTime.Now, fileName);
            }
            StreamWriter file = new StreamWriter(logFile, true, Encoding.UTF8);
            file.Write(content);
            file.Close();

        }
        /// <summary>
        /// read and stored in DB , then move the file finished
        /// </summary>
        /// <param name="isStop"></param>
        public void StartService(bool isStop)
        {
            string filePath = xmlUtil.GetNodeValueByName(FilePathEnum.SourceDirectory.ToString());
            string destPath = xmlUtil.GetNodeValueByName(FilePathEnum.MoveDirectory.ToString());
            string logPath = xmlUtil.GetNodeValueByName(FilePathEnum.LogDirectory.ToString());
            string[] names;
            //var logic = new FileUploadLogic();

            while (isStop)
            {
                names = GetFileNames(filePath);
                if (names == null)
                {
                    continue;
                }
                foreach (var name in names)
                {
                    FileInfo file = new FileInfo(name);
                    if (!file.Extension.ToUpper().Equals(".CSV"))
                    {
                        continue;
                    }

                    string fileName = name.Split('\\').Last();

                    try
                    {
                        //logic.ReadStandardFile(name);
                        //logic.SaveStandardTransImport("System", name, logic.ReadStandardFile(name));
                        //log and move
                        RecordLog(fileName, logPath, string.Empty);
                        Directory.Move(name, Path.Combine(GetDirectory(destPath), fileName));
                    }
                    catch (Exception e)
                    {
                        RecordLog(fileName, logPath, e.Message);
                        break;
                    }
                }
            }
        }
    }

}
<file_sep>/README.md
# ConsoleFileOperator
C# build
Add a branch
Run it again
<file_sep>/ConsoleFileOperator/FilePathEnum.cs

namespace ConsoleFileOperator
{
    public enum FilePathEnum
    {
        SourceDirectory,

        LogDirectory,

        MoveDirectory
    }

}

branch_name: refs/heads/master

languages: C# Markdown

repo_language: C#


text:
<file_sep>var test_data = [-10, -5, -2, 4, 6, 20, 7, 3, 0, -2, -4,
                     -3, 0, 5, 3, 2, 1, 3, 8, 10];

ReactDOM.render(

Sample Data

    	<BarChart height={300} width={500} data={test_data} graph_id={'test_bar_chart'}/>

,
	document.getElementById('content')
);<file_sep># glass-taxes
A web app that makes government spending more transparent.
<file_sep>from logger import logger
import urllib
from HTMLParser import HTMLParser
from htmlentitydefs import name2codepoint

class FederalBudgetParser(HTMLParser):
	def __init__(self):
		HTMLParser.__init__(self)
		self.tag_stack = []
		self.links = []
		self.in_anchor = False

	def handle_starttag(self, tag, attrs):
		if (len(self.tag_stack)):
			self.tag_stack.append(tag)
		elif ('id','content') in attrs:
			self.tag_stack.append(tag)
			logger.info('in content div')
		else:
			return

		if (tag=='a' and self.in_content_table()):
			self.links.append({'link':dict(attrs)['href'], 'text':u''})
			self.in_anchor = True
			logger.info('recording link {}'.format(self.links[-1]['link']))

	def handle_endtag(self, tag):
		if (len(self.tag_stack)):
			if (tag != self.tag_stack[-1]):
				# the expected closing tag did not match the actual closing tag
				raise HTMLParser.HTMLParseError

			self.tag_stack.pop()
			if (tag=='a'):
				self.in_anchor = False

			if (len(self.tag_stack) == 0):
				logger.info('end content div')

	def handle_entityref(self, name):
		c = unichr(name2codepoint[name])
		self.handle_data(c)

	def handle_charref(self, name):
		if name.startswith('x'):
			c = unichr(int(name[1:], 16))
		else:
			c = unichr(int(name))
		self.handle_data(c)

	def handle_data(self, data):
		if (self.in_anchor):
			self.links[-1]['text'] += unicode(data)

	def in_content_table(self):
		return 'table' in self.tag_stack

def fetch_budget():
	parser = FederalBudgetParser()

	# feed the parser the data
	fd = urllib.urlopen("https://www.whitehouse.gov/omb/budget/Historicals","test_historical_data.html")
	logger.info('got response from www.whitehouse.gov');
	parser.feed(fd.read())
	fd.close()

	# download the docs
	logger.info('fetching files')
	for link in parser.links:
		filename = u'{name}.{extension}'.format(
			name=link['text'].strip(),
			extension=link['link'].split('.')[-1]
		)
		logger.info(u'fetching {}'.format(filename))
		urllib.urlretrieve('https://www.whitehouse.gov'+link['link'],u'data/raw_budget_files/{}'.format(filename))
		logger.info('finished fetching')
	logger.info('finished fetching files')

if __name__ == "__main__":
	fetch_budget()<file_sep>#!/bin/sh
# make sure the directory structure is setup
mkdir -p data/raw_budget_files
mkdir -p data/json_budget_files

# start the server
python -m SimpleHTTPServer 8000 &
open http://localhost:8000<file_sep>import os
import re
import xlrd
import json

from logger import logger

raw_data_path = './data/raw_budget_files'
json_data_path = './data/json_budget_files'

def get_budget_filenames(directory):
	filenames = os.walk(directory).next()[2]
	return filenames

def match_filename_pattern(filenames, pattern):
	for filename in filenames:
		if re.match(pattern, filename, flags=re.IGNORECASE):
			return filename

def parse_header(sheet):
	header_list = []

	# primary header
	last_primary_header = u''
	last_secondary_header = u''
	for i in range(sheet.ncols):
		current_primary_header = sheet.row(2)[i].value
		current_secondary_header = sheet.row(3)[i].value

		if (len(current_primary_header)):
			last_primary_header = current_primary_header
			last_secondary_header = u'' # reset the secondary header

		if (len(current_secondary_header)):
			last_secondary_header = current_secondary_header

		header = ' '.join([last_secondary_header, last_primary_header])
		header_list.append(header.strip())

	return header_list

def parse_data(sheet, header):
	data = []

	def convert_data(d):
		if (x.ctype in {2,3}):
			return x.value
		elif(x.ctype == 1):
			try:
				if ('estimate' in x.value):
					m = re.search(r'(2[0-9]{3})',x.value)
					return int(m.group(0))
				return int(x.value)
			except ValueError:
				pass
		return None

	for i in range(4,sheet.nrows):
		data_point = [convert_data(x.value) for x in sheet.row(i)]
		data.append(dict(zip(header,data_point)))

	return data

def save_as_json(data, filename):
	with open(filename, 'w') as file:
		json.dump(data,file)
	logger.info('successfully converted {}'.format(filename))

def load_and_save_as_json(filename):
	# load the file to memory
	book = xlrd.open_workbook(os.path.join(raw_data_path, filename))
	sheet = book.sheet_by_index(0)
	book.release_resources()

	# extract the data from the sheet
	header = parse_header(sheet)
	data = parse_data(sheet, header)

	# replace the current extension with json
	json_filename = filename.split('.')
	json_filename[-1] = 'json'
	json_filename = '.'.join(json_filename)

	# save the file to disk
	save_as_json(data,os.path.join(json_data_path,json_filename))

if __name__ == '__main__':
	filenames = get_budget_filenames(raw_data_path)
	summary_filename = match_filename_pattern(filenames, r'.*summary of receipts.*current dollars.*')
	fund_group_split_filename = match_filename_pattern(filenames, r'.*receipts.*by fund group.*')
	load_and_save_as_json(summary_filename)
	load_and_save_as_json(fund_group_split_filename)
	<file_sep>var test_data = [-10, -5, -2, 4, 6, 20, 7, 3, 0, -2, -4,
                     -3, 0, 5, 3, 2, 1, 3, 8, 10];

var accessor = function(d){return d;}
var get_test_data = function(cb){
	cb(test_data);
}

var formatting = {
	fill:function(d){
		if (accessor(d)>0) return 'rgb(0,153,0)';
		else if (accessor(d)<0) return 'rgb(204,0,0)';
        else return 'black';
	}
};

ReactDOM.render(

Sample Data

    	<BasicBarChart height={300}
    				   width={500}
    				   data={get_test_data}
    				   graph_id={'test_bar_chart'}
    				   accessData={accessor}
    				   formatting={formatting}/>

,
	document.getElementById('content')
);

languages: Markdown Shell JavaScript Python

repo_language: Markdown

repo_name: npmcdn-to-unpkg-bot/glass-taxes


text:
<repo_name>ljm7248/Python-telegrambot<file_sep>/index.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# python-telegram-bot, emoji 설치
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
from emoji import emojize
from bs4 import BeautifulSoup
import time
import requests
import datetime
import schedule
#cctv를 위해서
import threading
import numpy

g_bot = None
g_chat_id = None

updater = Updater(token='<KEY>')
dispatcher = updater.dispatcher
updater.start_polling()

run_thread = False
# 스케줄러 메세지 전송 여부
send_tag =  False

def handler(bot, update):
  text = update.message.text
  chat_id = update.message.chat_id
  global run_thread, send_tag

  if '검색' in text or '순위' in text:
    print('검색순위')
    bot.send_message(chat_id=chat_id, text=search_rank())

  elif '날씨' in text:
      bot.send_message(chat_id=chat_id, text=weather())

  elif '이모티콘' in text:
      print('이모티콘')
      bot.send_message(chat_id=chat_id, text=emojize('아잉:heart_eyes:', use_aliases=True))

  elif '사진' in text:
      bot.send_photo(chat_id=chat_id,photo=open('test.jpg','rb'))

  elif 'start' in text:
        if not run_thread:

            run_thread=True
            global g_bot, g_chat_id
            g_bot = bot
            g_chat_id = chat_id
            # # 주간보고
            # t = threading.Thread(target=hmx_Report_scheduler, args=(bot, chat_id, 100000))
            # t.daemon=True
            # t.start()
            # # 근태
            # t1 = threading.Thread(target=hmx_Break_scheduler, args=(bot, chat_id,100000))
            # t1.daemon=True
            # t1.start()

            # 스케줄러 동작시킬 Thread
            t = threading.Thread(target=hmx_thread, args=(bot, chat_id, 100000))
            t.daemon=True
            t.start()

            bot.send_message(chat_id, "스케줄러를 시작했습니다.")
        else:
            bot.send_message(chat_id, "스케줄러를 작동중인 상태입니다.")

  elif 'end' in text:

      run_thread=False
      bot.send_message(chat_id, "스케줄러를 종료합니다.")

  else:
      print('몰라')
      bot.send_message(chat_id=chat_id, text='몰라')

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

# 스케줄러(주간보고) 함수
def hmx_Report_scheduler():
    print('hmx_Report_scheduler')
    try:
        if run_thread:
            global g_bot, g_chat_id
            hmx_message_send(g_bot, g_chat_id,'수요일 15시까지 change request에 금주/차주 내용 작성 부탁 드립니다. ' + emojize(':smiley:', use_aliases=True))
            hmx_message_send(g_bot, g_chat_id,'그리고 15시에 회의 있습니다.')

    except Exception as e:
        print(e)

# 밥 주문 함수
def hmx_Food():
    print('hmx_Report_scheduler')
    try:
        if run_thread:
            global g_bot, g_chat_id
            hmx_message_send(g_bot, g_chat_id,'밥 주문하기')

    except Exception as e:
        print(e)

# 근태 함수
def hmx_Break_scheduler():
    print('hmx_Break_scheduler')
    try:
        if run_thread:
            global g_bot, g_chat_id
            hmx_message_send(g_bot, g_chat_id, "다음달 근태정보 hitops 등록 부탁드립니다.")

    except Exception as e:
        print(e)

def hmx_message_send(bot, chat_id, msg):
    bot.send_message(chat_id, msg)

# 교통비

# 스케줄러 설정
# 근태 매월 25일 13:15
# schedule.every().day.at("13:15").do(hmx_Break_scheduler)
# schedule.every(25).at("13:15").do(hmx_Break_scheduler)
# 주간보고 매주 13:15
schedule.every().wednesday.at("13:15").do(hmx_Report_scheduler)
schedule.every().days.at("09:15").do(hmx_Food)
# schedule.every(15).seconds.do(hmx_Report_scheduler)
# schedule.every(5).seconds.do(hmx_Break_scheduler)

def hmx_thread(bot, chat_id, tm):
    while run_thread:
        schedule.run_pending()
        time.sleep(1)

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

def weather():
    daum_url = requests.get('https://www.daum.net/').text
    daum_p = BeautifulSoup(daum_url, 'html.parser')

    daum_location = daum_p.select('span.txt_part')
    daum_weather = daum_p.select('em.screen_out')

    weather = "다음 날씨 \n"

    for temp in range(0,len(daum_location)):
        print(daum_location[temp].text+" 온도 : ", daum_weather[temp].text)
        weather+= daum_location[temp].text+" 온도 : "+ daum_weather[temp].text + "\n"
    return weather

def search_rank():
    json = requests.get('https://www.naver.com/srchrank?frm=main').json()

    # json 데이터에서 "data" 항목의 값을 추출test test
    data = json["data"]
    searchList =""

    # 해당 값은 리스트 형태로 제공되기에 리스트만큼 반복
    for temp in data:
        print("rank : ", temp["rank"],' keyword : ', temp["keyword"])
        searchList += "rank : "+ str(temp["rank"])+' keyword : '+temp["keyword"]+"\n"
    return searchList

echo_handler = MessageHandler(Filters.text, handler)
dispatcher.add_handler(echo_handler)

repo_language: Python


text:
<file_sep>#include "my_ls.h"

void  my_putchar(char c)
{
  write(1, &c, 1);
}

void  my_putstr(char *str)
{
  while (*str != '\0')
    {
      my_putchar(*str);
      str++;
    }
}

void  my_put_nbr(int n)
{
  int d;
  int no;

  no = n;
  d = 1;
  if (no == -2147483648)
    {
      my_putstr("-2147483648");
      return ;
    }
  else if (n < 0)
    {
      my_putchar('-');
      no = - n;
    }
  while ((no / d) >= 10)
    {
      d = d * 10;
    }
  while (d > 0)
    {
      my_putchar((no / d) % 10 + 48);
      d = d / 10;
    }
}

int  ignore_signs(char *str, int i, int sign)
{
  int  n;
  int  cp;

  n = 0;
  while (str[i] && str[i] >= '0' && str[i] <= '9')
    {
      cp = n;
      if (cp > (n * 10 + (str[i] - '0')))
        return (0);
      n = n * 10 + (str[i++] - '0');
    }
  if (sign % 2 == 1)
    n = n * -1;
  return (n);
}

int             my_getnbr(char *str)
{
  int  i;
  int  nbre;
  int  minus;

  i = 0;
  minus = 0;
  while (str[i])
    {
      if (str[i] == '-')
	{
	  minus++;
	}
      if (str[i] >= '0' && str[i] <= '9')
        {
          nbre = ignore_signs(str, i, minus);
          return (nbre);
        }
      i++;
    }
  return (0);
}
<file_sep>#include "my_ls.h"

char c;

char get_opt(int argc, char** argv)
{
  char optstring[]="aAdlr";

  while ((c=getopt(argc, argv, optstring)) != EOF)
  {
    if (argc == 2 && argv[1][0] == '-')
      gestion_erreur(argc, argv, c);
    else if (argc == 3)
      gestion_erreur_2(argc, argv, c);
    }
  return (c);
}

int  gestion_erreur(int argc, char** argv, char c)
{
  char* strg;
  int (*tab[5]) ();
  int i;

  i = 0;
  if (argc){}
  if (argv){}
  strg = "darlA";
  tab[0] = &ls_d;
  tab[1] = &ls_a;
  tab[2] = &ls_r;
  tab[3] = &ls_l;
  tab[4] = &ls_A;
  while ( strg[i] != '\0' )
    {
      if (c == strg[i])
        (*tab[i])();
          i++;
      }
  my_putstr("\n");
  return (0);
}

int  gestion_erreur_2(int argc, char** argv, char c)
{
  char* strg;
  int (*tab[5]) ();
  int i;

  i = 0;
  strg = "Adrla";
  tab[0] = &ls_A_2;
  tab[1] = &ls_d_2;
  tab[2] = &ls_r_2;
  tab[3] = &ls_l_2;
  tab[4] = &ls_a_2;
  if (argc){}
  while ( strg[i] != '\0' )
    {
     if (c == strg[i])
       (*tab[i])(argv);
     i++;
   }
  my_putstr("\n");
  return (0);
}

int main(int argc, char** argv)
{
  if (argc == 1)
    my_ls();
  else if (argc == 2 && argv[1][0] != '-')
      my_ls_arg(argv);
  else
    get_opt(argc, argv);
  return (0);
}
<file_sep>#include "my_ls.h"

int   ls_a_2(char** argv)
{
  int i;
  char** tab;

  rep = NULL;
  i = 0;
  fichierLu = NULL;
  tab = malloc(sizeof(char*) * 100);
  rep = opendir(argv[2]);
  if (rep == NULL)
  {
    my_putstr("No such file or directory !\n");
    return (0);
  }
  while ((fichierLu = readdir(rep)) != NULL)
  {
    tab[i] = fichierLu->d_name;
    i++;
  }
    if (closedir(rep) == -1)
      return(0);
    aff_tab(tab);
    free(tab);
    return (0);
}

int   ls_A_2(char** argv)
{
  int i;
  char** tab;

  rep = NULL;
  fichierLu = NULL;
  i = 0;
  tab = malloc(sizeof(char*) * 100);
  rep = opendir(argv[2]);
  if (rep == NULL)
  {
    my_putstr("No such file or directory !\n");
    return (0);
  }
  while ((fichierLu = readdir(rep)) != NULL)
  {
    if (my_strcmp(fichierLu->d_name, "..") != 0 && my_strcmp(fichierLu->d_name, ".") != 0)
    {
      tab[i] = fichierLu->d_name;
      i++;
    }
  }
  if (closedir(rep) == -1)
    return(0);
  aff_tab(tab);
  free(tab);
  return (0);
}

int   ls_d_2(char** argv)
{
  rep = NULL;

  rep = opendir(argv[2]);
  if (rep == NULL)
  {
    my_putstr("No such file or directory !\n");
    return (0);
  }
  else
  {
    my_putstr(argv[2]);
  }
  return (0);
}

int   ls_r_2(char** argv)
{
  int i;
  char** tab;

  rep = NULL;
  i = 0;
  tab = malloc(sizeof(char*) * 100);
  fichierLu = NULL;
  rep = opendir(argv[2]);
  if (rep == NULL)
  {
    my_putstr("No such file or directory !\n");
    return (0);
  }
  while ((fichierLu = readdir(rep)) != NULL)
  {
    if (fichierLu->d_name[0] != '.' && fichierLu->d_name != '\0')
      {
        tab[i] = fichierLu->d_name;
        i++;
      }
  }
  if (closedir(rep) == -1)
    return(0);
  aff_tab_2(tab);
  free(tab);
  return (0);
}

int   my_ls_arg(char** argv)
{
  int i;
  char** tab;

  rep = NULL;
  i = 0;
  fichierLu = NULL;
  tab = malloc(sizeof(char*) * 100);
  rep = opendir(argv[1]);
  if (rep == NULL)
  {
    my_putstr("No such file or directory !\n");
    return (0);
  }
  while ((fichierLu = readdir(rep)) != NULL)
  {
    if (fichierLu->d_name[0] != '.' && fichierLu->d_name != '\0')
      {
        tab[i] = fichierLu->d_name;
        i++;
      }
  }
  if (closedir(rep) == -1)
    return(0);
  aff_tab(tab);
  my_putstr("\n");
  free(tab);
  return (0);
}
<file_sep>#include "my_ls.h"

void aff_tab_2(char** tab)
{
  int i;
  int y;

  y = 0;
  while (tab[y] != '\0')
    {
      y++;
    }
  i = 0;
  tab = my_sort_tab_2(tab, y);
  while (tab[i] != '\0')
    {
      my_putstr(tab[i]);
      my_putstr("  ");
      i++;
    }
}

int  my_int(char* argv)
{
  int  i;

  i = 0;
  while (argv[i] != '\0')
    {
      if (argv[i] < 48 || argv[i] > 57)
        return (0);
      i++;
    }
  return (1);

}
<file_sep>CC	= gcc
NAME	= my_ls
SRC	=	fonction.c\
			fonction2.c\
			main.c\
			fonction_ls.c\
			ls_l.c\
			fonction_ls_2.c\
			fonction3.c

OBJ	= $(SRC:%.c=%.o)
RM	= rm -f
CFLAGS	+= -W -Wall -Werror -ansi -pedantic

$(NAME):	$(OBJ)
		$(CC) $(OBJ) -o $(NAME)

all:	$(NAME)

clean:
	$(RM) $(OBJ)

fclean:		clean
	$(RM) $(NAME)

re:	fclean all
<file_sep>#include "my_ls.h"

void ls_perm()
{
  my_putstr( (S_ISDIR(fileStat.st_mode))  ? "d" : "-");
  my_putstr( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
  my_putstr( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
  my_putstr( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
  my_putstr( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
  my_putstr( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
  my_putstr( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
  my_putstr( (fileStat.st_mode & S_IROTH) ? "r" : "-");
  my_putstr( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
  my_putstr( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
  my_putstr(" ");
}

void ls_affich()
{
  int a;
  char* tmp;

  tmp = ctime(&fileStat.st_mtime);
  for (a = 4; a < my_strlen(tmp) - 9; a++)
      my_putchar(tmp[a]);
  my_putstr("  ");
}

void ls_infos_l()
{
  pwd = getpwuid(fileStat.st_uid);
  grp = getgrgid(fileStat.st_gid);
  my_put_nbr(fileStat.st_nlink);
  my_putstr(" ");
  my_put_nbr(fileStat.st_size);
  my_putstr(" ");
  my_putstr(pwd->pw_name);
  my_putstr(" ");
  my_putstr(grp->gr_name);
  my_putstr(" ");
}
int   ls_l()
{
  if ((rep = opendir("./")) == NULL)
    return (EXIT_FAILURE);
  while ((fichierLu = readdir(rep)) != NULL)
  {
    if (fichierLu->d_name[0] != '.')
    {
      if(stat(fichierLu->d_name,&fileStat) < 0)
          return 1;
    ls_perm();
    ls_infos_l();
    ls_affich();
    my_putstr(fichierLu->d_name);
    my_putstr("\n");
    }
  }
  if (closedir(rep) == -1)
    return (0);
  return (0);
}

int   ls_l_2(char** argv)
{
  rep = opendir(argv[2]);
  if (rep == NULL)
    return 0;
  while ((fichierLu = readdir(rep)) != NULL)
  {
    if (fichierLu->d_name[0] != '.')
    {
      if(stat(fichierLu->d_name,&fileStat) < 0)
          return 1;
    ls_perm();
    ls_infos_l();
    ls_affich();
    my_putstr(fichierLu->d_name);
    my_putstr("\n");
    }
  }
  if (closedir(rep) == -1)
 	 return (0);
  return (0);
}
<file_sep>#ifndef __my_ls_h__
#define __my_ls_h__

#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <ctype.h>

struct dirent* readdir(DIR* repertoire) ;
struct stat fileStat;
struct passwd *pwd;
struct group  *grp;
struct tm *p;
DIR* rep;
struct dirent* fichierLu;

void    my_putchar(char c);
void    my_putstr(char *str);
void    my_put_nbr(int n);
void    aff_tab(char** tab);
void    aff_tab_2(char** tab);
void    ls_perm();
void    ls_affich();
void    ls_infos_l();

char**	my_sort_tab(char **tab, int size);
char**	my_sort_tab_2(char **tab, int size);

int     gestion_erreur(int argc, char** argv, char c);
int     gestion_erreur_2(int argc, char** argv, char c);
int     my_strlen(char *str);
int     my_strcmp(char *s1, char *s2);
int     my_int(char* argv);
int     my_getnbr(char *str);
int     ls_a();
int     my_ls();
int     ls_A();
int     ignore_signs(char *str, int i, int sign);
int     main(int argc, char** argv);
int     ls_d();
int     ls_r();
int     ls_l();
int     ls_d_2(char** argv);
int     ls_r_2(char** argv);
int     ls_A_2(char** argv);
int     ls_a_2(char** argv);
int     ls_l_2(char** argv);
int     my_ls_arg(char** argv);

char    get_opt(int argc, char** argv);
#endif
<file_sep>#include "my_ls.h"

int  my_strcmp(char *s1, char *s2)
{
  int  w;

  w = 0;
  while (s1[w] != '\0' || s2[w] != '\0')
    {
      if (s1[w] < s2[w])
        {
          return (-1);
        }
      if (s1[w] == s2[w])
        {
          w = w + 1;
        }
      if (s1[w] > s2[w])
        {
          return (1);
	}
    }
  return (0);
}

int  my_strlen(char *str)
{
  int  d;

  d = 0;

  while (*str != '\0')
    {
      d = d + 1;
      str = str + 1;
    }
  return (d);
}

char**	my_sort_tab(char **tab, int size)
{
  int	draa;
  int	droo;
  char*	swap;

  draa = 0;
  droo = 0;
  while (draa < size)
    {
      droo = 0;
      while (droo < size)
	     {
	        if (tab[draa][0] < tab[droo][0])
	         {
	            swap = tab[draa];
	            tab[draa] = tab[droo];
	            tab[droo] = swap;
	          }
	          droo = droo + 1;
	      }
      draa = draa + 1;
    }
  return (tab);
}

char**	my_sort_tab_2(char **tab, int size)
{
  int	draa;
  int	droo;
  char*	swap;

  draa = 0;
  droo = 0;
  while (draa < size)
    {
      droo = 0;
      while (droo < size)
	     {
	        if (tab[draa][0] > tab[droo][0])
	         {
	            swap = tab[draa];
	            tab[draa] = tab[droo];
	            tab[droo] = swap;
	          }
	          droo = droo + 1;
	      }
      draa = draa + 1;
    }
  return (tab);
}

void aff_tab(char** tab)
{
  int i;
  int y;

  y = 0;
  while (tab[y] != '\0')
    {
      y++;
    }
  i = 0;
  tab = my_sort_tab(tab, y);
  while (tab[i] != '\0')
    {
      my_putstr(tab[i]);
      my_putstr("  ");
      i++;
    }
}

languages: Makefile C

repo_language: Makefile


text:
<repo_name>radai-rosenblatt/beans<file_sep>/src/main/java/net/radai/beans/generator/cplusplus/CppClassData.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.generator.cplusplus;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

class CppClassData {
    private final String simpleClassName;
    private Set<String> headerIncludes = new HashSet<>();
    private Set<String> implementationIncludes = new HashSet<>();
    private Map<String, CppType> properties = new HashMap<>();
    private Map<String, String> fieldDeclarations = new HashMap<>();
    private Map<String, String> getterDeclarations = new HashMap<>();
    private Map<String, String> setterDeclarations = new HashMap<>();
    private Map<String, List<String>> getterImplementations = new HashMap<>();
    private Map<String, List<String>> setterImplementations = new HashMap<>();

    public CppClassData(String simpleClassName) {
        this.simpleClassName = simpleClassName;
    }

    public String getSimpleClassName() {
        return simpleClassName;
    }

    public void addHeaderInclude(String includeStatement) {
        if (includeStatement != null && !includeStatement.isEmpty()) {
            headerIncludes.add(includeStatement);
        }
    }

    public void addHeaderIncludes(Iterable<String> includeStatements) {
        for (String includeStatement : includeStatements) {
            addHeaderInclude(includeStatement);
        }
    }

    public void addImplementationInclude(String includeStatement) {
        if (includeStatement != null && !includeStatement.isEmpty()) {
            implementationIncludes.add(includeStatement);
        }
    }

    public void addInclude(String includeStatement) {
        addHeaderInclude(includeStatement);
        addImplementationInclude(includeStatement);
    }

    public void addProperty(String propertyName, CppType propertyType) {
        if (properties.put(propertyName, propertyType) != null) {
            throw new IllegalArgumentException("property " + propertyName + " already defined");
        }
    }

    public Map<String, CppType> getProperties() {
        return properties;
    }

    public CppType getProperty(String propertyName) {
        return properties.get(propertyName);
    }

    public void addFieldDeclaration(String propertyName, String fieldDeclaration) {
        fieldDeclarations.put(propertyName, fieldDeclaration);
    }

    public String getFieldDeclaration(String propertyName) {
        return fieldDeclarations.get(propertyName);
    }

    public void addGetterDeclaration(String propertyName, String getterDeclaration) {
        getterDeclarations.put(propertyName, getterDeclaration);
    }

    public void addSetterDeclaration(String propertyName, String setterDeclaration) {
        setterDeclarations.put(propertyName, setterDeclaration);
    }

    public void addGetterImplementation(String propertyName, List<String> getterImplementation) {
        getterImplementations.put(propertyName, getterImplementation);
    }

    public void addSetterImplementation(String propertyName, List<String> setterImplementation) {
        setterImplementations.put(propertyName, setterImplementation);
    }

    public boolean hasProperties() {
        return !properties.isEmpty();
    }

    public boolean hasHeaderIncludes() {
        return !headerIncludes.isEmpty();
    }

    public Set<String> getHeaderIncludes() {
        return headerIncludes;
    }

    public boolean hasImplementationIncludes() {
        return !implementationIncludes.isEmpty();
    }

    public Set<String> getImplementationIncludes() {
        return implementationIncludes;
    }

    public Map<String, String> getGetterDeclarations() {
        return getterDeclarations;
    }

    public Map<String, String> getSetterDeclarations() {
        return setterDeclarations;
    }

    public Map<String, List<String>> getGetterImplementations() {
        return getterImplementations;
    }

    public Map<String, List<String>> getSetterImplementations() {
        return setterImplementations;
    }
}
<file_sep>/src/main/java/net/radai/beans/model/BeanCollectionPropertyDescriptor.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.model;

import java.util.Set;

import net.radai.beans.analyzer.BeanAnalysisContext;

public class BeanCollectionPropertyDescriptor extends BeanPropertyDescriptor {
    private TypeDescriptor elementType;
    private BeanDescriptor elementTypeDescriptor;

    public TypeDescriptor getElementType() {
        return elementType;
    }

    public void setElementType(TypeDescriptor elementType) {
        this.elementType = elementType;
    }

    public BeanDescriptor getElementTypeDescriptor() {
        return elementTypeDescriptor;
    }

    public void setElementTypeDescriptor(BeanDescriptor elementTypeDescriptor) {
        this.elementTypeDescriptor = elementTypeDescriptor;
    }

    @Override
    public Set<String> getAllReferencedFqcns(Set<String> output) {
        elementType.getAllReferencedFqcns(output);
        return output;
    }

    @Override
    public void populateBeanReferences(BeanAnalysisContext context) {
        if (elementType != null && !context.isTerminal(elementType)) {
            if ((elementTypeDescriptor = context.getBeanDescriptor(elementType.getTypeFqcn())) == null) {
                throw new IllegalStateException("class "+elementType.getTypeFqcn()+" not found among bean classes");
            }
        }
    }

    @Override
    public BeanPropertyType getPropertyType() {
        return BeanPropertyType.COLLECTION;
    }

    @Override
    public String toString() {
        return "Collection<"+elementType+"> "+name;
    }
}
<file_sep>/src/main/java/net/radai/beans/analyzer/BeanAnalyzer.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.analyzer;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;

import net.radai.beans.Util;
import net.radai.beans.model.BeanCollectionPropertyDescriptor;
import net.radai.beans.model.BeanDescriptor;
import net.radai.beans.model.BeanMapPropertyDescriptor;
import net.radai.beans.model.BeanPackage;
import net.radai.beans.model.BeanPropertyDescriptor;
import net.radai.beans.model.BeanSimplePropertyDescriptor;
import net.radai.beans.model.TypeDescriptor;

public class BeanAnalyzer {
    private final static Logger logger = LoggerFactory.getLogger(BeanAnalyzer.class);

    private final static Pattern GETTER_NAME_PATTERN = Pattern.compile("get(\\p{Upper}.*)");
    private final static Pattern BOOLEAN_GETTER_NAME_PATTERN = Pattern.compile("is(\\p{Upper}.*)");
    private final static Pattern SETTER_NAME_PATTERN = Pattern.compile("set(\\p{Upper}.*)");

    private TypeResolver resolver = new TypeResolver();

    public BeanPackage analyze(Class... beanClasses) {
        BeanAnalysisContext context = new BeanAnalysisContext();
        for (Class toAnalyze : beanClasses) {
            analyze(toAnalyze, context);
        }
        return new BeanPackage(context.getBeanDescriptors());
    }

    public BeanDescriptor analyze(Class beanClass, BeanAnalysisContext analysisContext) {

        Set<String> fqcnsEncountered = new HashSet<>(); //keep track of our adventures

        //analyze the class itself

        BeanDescriptor result = analyzeSingleClass(beanClass, analysisContext, fqcnsEncountered);

        //analyze any other classes we encounter along the way

        Set<String> fqcnsToAnalyze = new HashSet<>();
        findRemainingClasses(fqcnsEncountered, analysisContext, fqcnsToAnalyze);

        while (!fqcnsToAnalyze.isEmpty()) {
            for (String fqcnToAnalyze : fqcnsToAnalyze) {
                try {
                    analyzeSingleClass(Class.forName(fqcnToAnalyze), analysisContext, fqcnsEncountered);
                } catch (ClassNotFoundException e) {
                    throw new IllegalStateException(e);
                }
            }
            findRemainingClasses(fqcnsEncountered, analysisContext, fqcnsToAnalyze);
        }

        //now that everything is analyzed link them together

        Map<String, BeanDescriptor> allBeans = analysisContext.getBeanDescriptors();
        for (BeanDescriptor bean : allBeans.values()) {
            bean.populateBeanReferences(analysisContext);
        }

        return result;
    }

    private void findRemainingClasses(Set<String> encountered, BeanAnalysisContext context, Set<String> output) {
        output.addAll(encountered);
        output.removeAll(context.getAllTerminalFqcns());
        output.removeAll(context.getBeanDescriptors().keySet());
    }

    private BeanDescriptor analyzeSingleClass(Class beanClass, BeanAnalysisContext analysisContext, Set<String> fqcnsEncountered) {

        //analyze the bean itself

        String beanSimpleClassName = beanClass.getSimpleName();
        String beanFqcn = beanClass.getName();
        BeanDescriptor descriptor = analysisContext.getBeanDescriptor(beanFqcn);
        if (descriptor != null) {
            return descriptor;
        }
        logger.debug("analyzing {}",beanFqcn);

        Type parentClass = beanClass.getGenericSuperclass();
        Map<String, ProspectiveBeanProperty> possibleProperties = findProspectiveProperties(beanClass, analysisContext.isScanFields());
        Map<String, ProspectiveBeanProperty> filteredProperties = filterProspectiveProperties(beanClass, possibleProperties);
        Map<String, BeanPropertyDescriptor> properties = parseProperties(beanClass, filteredProperties, analysisContext);

        descriptor = new BeanDescriptor(beanSimpleClassName, beanFqcn);
        if (parentClass != null && !analysisContext.isTerminal(parentClass)) {
            descriptor.setParentFqcn(((Class)parentClass).getName());
        }
        descriptor.setDirectProperties(properties);
        analysisContext.addBeanDescriptor(descriptor);

        descriptor.getAllReferencedFqcns(fqcnsEncountered);

        return descriptor;
    }

    private Map<String, ProspectiveBeanProperty> findProspectiveProperties (Class beanClass, boolean includeFields) {
        Map<String, ProspectiveBeanProperty> possibleProperties = new HashMap<>();

        for (Method directMethod : beanClass.getDeclaredMethods()) {
            int modifiers = directMethod.getModifiers();
            if (Modifier.isStatic(modifiers)) {
                continue;
            }
            String methodName = directMethod.getName();
            Class[] parameterTypes = directMethod.getParameterTypes();
            Type returnType = directMethod.getGenericReturnType();
            String propName;
            if (parameterTypes.length == 0 && !Void.TYPE.equals(returnType)) { //getter?
                if (GETTER_NAME_PATTERN.matcher(methodName).matches()) {
                    propName = Util.lowercaseFirstLetter(methodName.substring(3));
                } else if ((Boolean.class.equals(returnType) || Boolean.TYPE.equals(returnType)) && BOOLEAN_GETTER_NAME_PATTERN.matcher(methodName).matches()) {
                    propName = Util.lowercaseFirstLetter(methodName.substring(2));
                } else {
                    continue;
                }
                ProspectiveBeanProperty prospectiveBeanProperty = getProspectiveProperty(possibleProperties, propName);
                prospectiveBeanProperty.getter = directMethod;
            } else if (parameterTypes.length == 1 && Void.TYPE.equals(returnType)) { //setter?
                if (SETTER_NAME_PATTERN.matcher(methodName).matches()) {
                    propName = Util.lowercaseFirstLetter(methodName.substring(3));
                } else {
                    continue;
                }
                ProspectiveBeanProperty prospectiveBeanProperty = getProspectiveProperty(possibleProperties, propName);
                prospectiveBeanProperty.setter = directMethod;
            }
        }
        if (includeFields) {
            for (Field directField : beanClass.getDeclaredFields()) {
                int modifiers = directField.getModifiers();
                if (Modifier.isStatic(modifiers)) {
                    continue;
                }
                String fieldName = directField.getName();

                ProspectiveBeanProperty prospectiveBeanProperty = getProspectiveProperty(possibleProperties, fieldName);
                prospectiveBeanProperty.field = directField;
            }
        }

        return possibleProperties;
    }

    private ProspectiveBeanProperty getProspectiveProperty(Map<String, ProspectiveBeanProperty> possibleProperties, String propName) {
        ProspectiveBeanProperty prospectiveBeanProperty = possibleProperties.get(propName);
        if (prospectiveBeanProperty == null) {
            prospectiveBeanProperty = new ProspectiveBeanProperty(propName);
            possibleProperties.put(propName, prospectiveBeanProperty);
        }
        return prospectiveBeanProperty;
    }

    private Map<String, ProspectiveBeanProperty> filterProspectiveProperties(Class beanClass, Map<String, ProspectiveBeanProperty> prospectiveProperties) {
        Map<String, ProspectiveBeanProperty> filteredProps = new HashMap<>();
        for (Map.Entry<String, ProspectiveBeanProperty> entry : prospectiveProperties.entrySet()) {
            String propName = entry.getKey();
            ProspectiveBeanProperty prospectiveProp = entry.getValue();

            Type getterType = prospectiveProp.getter != null ? prospectiveProp.getter.getGenericReturnType() : null;
            Type setterType = prospectiveProp.setter != null ? prospectiveProp.setter.getGenericParameterTypes()[0] : null;
            Type fieldType = prospectiveProp.field != null ? prospectiveProp.field.getGenericType() : null;

            boolean match = true; //until proven otherwise
            Type t = getterType;
            if (t != null) {
                if (setterType != null) {
                    match = setterType.equals(t);
                }
            } else {
                t = setterType;
            }
            if (match && t != null) {
                if (fieldType != null) {
                    match = fieldType.equals(t);
                }
            }

            if (!match) {
                logger.info("discarding property {}.{} because {} types do not match", beanClass.getSimpleName(), propName, fieldType!=null ? "getter/setter/field" : "getter/setter");
                continue;
            }

            filteredProps.put(propName, prospectiveProp);
        }
        return filteredProps;
    }

    private static class ProspectiveBeanProperty {
        private final String name;
        private Field field;
        private Method getter;
        private Method setter;

        private ProspectiveBeanProperty(String name) {
            this.name = name;
        }

        Type getType() {
            //this method is used after non-props have been filtered out, so all 3 fields
            //will yield the same result, just return the 1st one that isnt null.
            if (field!=null) {
                return field.getGenericType();
            }
            if (getter!=null) {
                return getter.getGenericReturnType();
            }
            if (setter!=null) {
                return setter.getGenericParameterTypes()[0];
            }
            throw new IllegalStateException();
        }

        @Override
        public String toString() {
            return name+": "+getter+" "+setter+" "+field;
        }
    }

    private Map<String, BeanPropertyDescriptor> parseProperties(Class beanClass, Map<String, ProspectiveBeanProperty> prospectiveProperties, BeanAnalysisContext analysisContext) {
        Map<String, BeanPropertyDescriptor> properties = new HashMap<>();
        for (Map.Entry<String, ProspectiveBeanProperty> entry : prospectiveProperties.entrySet()) {
            String propName = entry.getKey();
            ProspectiveBeanProperty prospectiveProp = entry.getValue();

            BeanPropertyDescriptor propDesc = createPropertyDescriptor(prospectiveProp, propName);

            switch (propDesc.getPropertyType()) {
                case SIMPLE:
                    break;
                case COLLECTION:
                    BeanCollectionPropertyDescriptor colProp = (BeanCollectionPropertyDescriptor) propDesc;
                    TypeDescriptor elementType = colProp.getElementType();
                    if (Util.isACollection(elementType.getTypeFqcn()) || Util.isAMap(elementType.getTypeFqcn())) {
                        logger.info("discarding property {}.{} because element type {} is a collection/map", beanClass.getSimpleName(), propName, elementType);
                        continue;
                    }
                    break;
                case MAP:
                    BeanMapPropertyDescriptor mapProp = (BeanMapPropertyDescriptor) propDesc;
                    TypeDescriptor keyType = mapProp.getKeyType();
                    if (Util.isACollection(keyType.getTypeFqcn()) || Util.isAMap(keyType.getTypeFqcn())) {
                        logger.info("discarding property {}.{} because key type {} is a collection/map", beanClass.getSimpleName(), propName, keyType);
                        continue;
                    }
                    TypeDescriptor valueType = mapProp.getValueType();
                    if (Util.isACollection(valueType.getTypeFqcn()) || Util.isAMap(valueType.getTypeFqcn())) {
                        logger.info("discarding property {}.{} because value type {} is a collection/map", beanClass.getSimpleName(), propName, valueType);
                        continue;
                    }
                    break;
                default:
                    throw new IllegalStateException("unhandled "+propDesc.getPropertyType());
            }

            properties.put(propName, propDesc);
        }
        return properties;
    }

    private BeanPropertyDescriptor createPropertyDescriptor(ProspectiveBeanProperty prospectiveProp, String propName) {
        BeanPropertyDescriptor result;

        ResolvedType resolvedPropType = resolver.resolve(prospectiveProp.getType());
        Class rawClass = resolvedPropType.getErasedType();

        if (Util.isACollection(rawClass)) {
            BeanCollectionPropertyDescriptor collectionDesc = new BeanCollectionPropertyDescriptor();
            List<ResolvedType> typeParameters = resolvedPropType.getTypeParameters();
            collectionDesc.setElementType(createTypeDescriptor(typeParameters.get(0)));
            result = collectionDesc;
        } else if (Util.isAMap(rawClass)) {
            BeanMapPropertyDescriptor mapDesc = new BeanMapPropertyDescriptor();
            List<ResolvedType> typeParameters = resolvedPropType.getTypeParameters();
            mapDesc.setKeyType(createTypeDescriptor(typeParameters.get(0)));
            mapDesc.setValueType(createTypeDescriptor(typeParameters.get(1)));
            result = mapDesc;
        } else {
            BeanSimplePropertyDescriptor simpleDesc = new BeanSimplePropertyDescriptor();
            simpleDesc.setType(createTypeDescriptor(resolvedPropType));
            result = simpleDesc;
        }

        //TODO - annotations
        result.setName(propName);
        result.setField(prospectiveProp.field);
        result.setGetterMethod(prospectiveProp.getter);
        result.setSetterMethod(prospectiveProp.setter);
        return result;
    }

    private static TypeDescriptor createTypeDescriptor(ResolvedType resolvedType) {
        Class erasedType = resolvedType.getErasedType();
        TypeDescriptor typeDescriptor = new TypeDescriptor();
        typeDescriptor.setTypeFqcn(erasedType.getName());
        typeDescriptor.setTypeSimpleClassName(erasedType.getSimpleName());
        if (resolvedType.isArray()) {
            throw new UnsupportedOperationException("not implemented yet");
        }
        List<ResolvedType> typeParams = resolvedType.getTypeParameters();
        if (typeParams != null && !typeParams.isEmpty()) {
            for (ResolvedType typeParam : typeParams) {
                TypeDescriptor converted = createTypeDescriptor(typeParam);
                typeDescriptor.addTypeParameter(converted);
            }
        }
        return typeDescriptor;
    }
}
<file_sep>/src/main/java/net/radai/beans/generator/cplusplus/SimpleCodeComposer.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.generator.cplusplus;

import net.radai.beans.generator.NamingConvention;

import java.util.ArrayList;
import java.util.List;

/**
 * Created on 05/09/2014.
 */
public class SimpleCodeComposer implements CodeComposer {

    @Override
    public String composeFieldDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        CppType propertyCppType = cppClass.getProperty(propertyName);
        return propertyCppType.getDefinition()+" "+namingConvention.fieldName(propertyName)+";";
    }

    @Override
    public String composeGetterDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        CppType propertyCppType = cppClass.getProperty(propertyName);
        if (propertyCppType instanceof CppSimpleType && ((CppSimpleType)propertyCppType).isPrimitive()) {
            return propertyCppType.getDefinition() + " " + namingConvention.getterName(propertyName) + "() const";
        } else {
            return "const " + propertyCppType.getDefinition() + "& " + namingConvention.getterName(propertyName) + "() const";
        }
    }

    @Override
    public List<String> composeGetterImplementation(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        CppType propertyCppType = cppClass.getProperty(propertyName);
        List<String> result = new ArrayList<>();
        if (propertyCppType instanceof CppSimpleType && ((CppSimpleType)propertyCppType).isPrimitive()) {
            result.add(propertyCppType.getDefinition()+" "+namingConvention.className(cppClass.getSimpleClassName())+"::"+namingConvention.getterName(propertyName)+"() const {");
            result.add("    return "+namingConvention.fieldName(propertyName)+";");
            result.add("}");
        } else {
            result.add("const "+propertyCppType.getDefinition()+"& "+namingConvention.className(cppClass.getSimpleClassName())+"::"+namingConvention.getterName(propertyName)+"() const {");
            result.add("    return this->"+namingConvention.fieldName(propertyName)+";");
            result.add("}");
        }
        return result;
    }

    @Override
    public String composeSetterDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        CppType propertyCppType = cppClass.getProperty(propertyName);
        if (propertyCppType instanceof CppSimpleType && ((CppSimpleType)propertyCppType).isPrimitive()) {
            return "void " + namingConvention.setterName(propertyName) + "(" + propertyCppType.getDefinition() + " value_literal)";
        } else {
            return "void " + namingConvention.setterName(propertyName) + "(" + propertyCppType.getDefinition() + " const& value_ref)";
        }
    }

    @Override
    public List<String> composeSetterImplementation(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        CppType propertyCppType = cppClass.getProperty(propertyName);
        List<String> result = new ArrayList<>();
        if (propertyCppType instanceof CppSimpleType && ((CppSimpleType)propertyCppType).isPrimitive()) {
            result.add("void "+namingConvention.className(cppClass.getSimpleClassName())+"::"+namingConvention.setterName(propertyName)+"("+propertyCppType.getDefinition()+" value_literal) {");
            result.add("    this->"+namingConvention.fieldName(propertyName)+" = value_literal;");
            result.add("}");
        } else {
            result.add("void "+namingConvention.className(cppClass.getSimpleClassName())+"::"+namingConvention.setterName(propertyName)+"("+propertyCppType.getDefinition()+" const& value_ref) {");
            result.add("    this->"+namingConvention.fieldName(propertyName)+" = value_ref;");
            result.add("}");
        }
        return result;
    }

    @Override
    public String composeClearMethodDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        return null; //not supported
    }

    @Override
    public List<String> composeClearMethodImplementation(CppClassData cppClass, String propertyName, NamingConvention namingConvention) {
        return null; //not supported
    }
}
<file_sep>/src/test/java/net/radai/beans/generator/cplusplus/CPlusPlusGeneratorTest.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.generator.cplusplus;

import java.io.File;
import java.nio.file.Files;
import java.util.List;

import org.junit.Test;

import net.radai.beans.TestUtil;
import net.radai.beans.analyzer.BeanAnalyzer;
import net.radai.beans.model.BeanPackage;

public class CPlusPlusGeneratorTest {

    @Test
    public void testSimpleGeneration() throws Exception {
        BeanAnalyzer analyzer = new BeanAnalyzer();
        BeanPackage beanPackage = analyzer.analyze(SimpleBean.class);
        CPlusPlusGenerator generator = new CPlusPlusGenerator();
        File generationTargetDirectory = Files.createTempDirectory("testSimpleGeneration").toFile();
        generator.setTargetDirectory(generationTargetDirectory);
        generator.generate(beanPackage);
        TestUtil.delete(generationTargetDirectory);
    }

    static class SimpleBean {
        private int intProp;
        private String stringProp;
        private List<String> ListProp;

        public int getIntProp() {
            return intProp;
        }

        public void setIntProp(int intProp) {
            this.intProp = intProp;
        }

        public String getStringProp() {
            return stringProp;
        }

        public void setStringProp(String stringProp) {
            this.stringProp = stringProp;
        }

        public List<String> getListProp() {
            return ListProp;
        }

        public void setListProp(List<String> listProp) {
            ListProp = listProp;
        }
    }
}
<file_sep>/src/main/java/net/radai/beans/model/AnnotationDescriptor.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.model;

import java.util.HashSet;
import java.util.Set;

public class AnnotationDescriptor {
    private String fqcn;
    private String javaExpression;

    public AnnotationDescriptor(String fqcn, String javaExpression) {
        this.fqcn = fqcn;
        this.javaExpression = javaExpression;
    }

    public String getFqcn() {
        return fqcn;
    }

    public void setFqcn(String fqcn) {
        this.fqcn = fqcn;
    }

    public String getJavaExpression() {
        return javaExpression;
    }

    public void setJavaExpression(String javaExpression) {
        this.javaExpression = javaExpression;
    }

    public Set<String> getAllReferencedFqcns() {
        Set<String> output = new HashSet<>();
        return getAllReferencedFqcns(output);
    }

    public Set<String> getAllReferencedFqcns(Set<String> output) {
        output.add(fqcn);
        //TODO - some annotations can reference other classes as arguments
        return output;
    }

    @Override
    public String toString() {
        return javaExpression;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AnnotationDescriptor that = (AnnotationDescriptor) o;

        return fqcn.equals(that.fqcn);
    }

    @Override
    public int hashCode() {
        return fqcn.hashCode();
    }
}
<file_sep>/src/main/java/net/radai/beans/generator/cplusplus/CodeComposer.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.generator.cplusplus;

import net.radai.beans.generator.NamingConvention;

import java.util.List;

/**
 * Created on 05/09/2014.
 */
public interface CodeComposer {
    String composeFieldDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention);

    String composeGetterDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention);

    List<String> composeGetterImplementation(CppClassData cppClass, String propertyName, NamingConvention namingConvention);

    String composeSetterDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention);

    List<String> composeSetterImplementation(CppClassData cppClass, String propertyName, NamingConvention namingConvention);

    String composeClearMethodDeclaration(CppClassData cppClass, String propertyName, NamingConvention namingConvention);

    List<String> composeClearMethodImplementation(CppClassData cppClass, String propertyName, NamingConvention namingConvention);
}
<file_sep>/src/test/java/net/radai/beans/UtilTest.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;

public class UtilTest {
    @Test
    public void testSplitCamelCaseToWords() {
        String input = "someCamelCasedInput";
        List<String> split = Util.splitCamelCaseToWords(input);
        Assert.assertEquals(4, split.size());
        split = Util.splitCamelCaseToWords("AAA");
        Assert.assertEquals(1, split.size());
        Assert.assertEquals("AAA", split.get(0));
    }
}
<file_sep>/src/main/java/net/radai/beans/model/TypeDescriptor.java
/*
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.radai.beans.model;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TypeDescriptor {
    private int arrayCount;
    private String typeSimpleClassName;
    private String typeFqcn;
    private List<TypeDescriptor> typeParameters;
    private List<AnnotationDescriptor> annotations;

    public int getArrayCount() {
        return arrayCount;
    }

    public void setArrayCount(int arrayCount) {
        this.arrayCount = arrayCount;
    }

    public String getTypeSimpleClassName() {
        return typeSimpleClassName;
    }

    public void setTypeSimpleClassName(String typeSimpleClassName) {
        this.typeSimpleClassName = typeSimpleClassName;
    }

    public String getTypeFqcn() {
        return typeFqcn;
    }

    public void setTypeFqcn(String typeFqcn) {
        this.typeFqcn = typeFqcn;
    }

    public List<TypeDescriptor> getTypeParameters() {
        return typeParameters;
    }

    public void setTypeParameters(List<TypeDescriptor> typeParameters) {
        this.typeParameters = typeParameters;
    }

    public void addTypeParameter(TypeDescriptor typeParam) {
        if (typeParameters ==null) {
            typeParameters = new ArrayList<>();
        }
        typeParameters.add(typeParam);
    }

    public TypeDescriptor getTypeParameter(int index) {
        if (typeParameters == null || typeParameters.size() <= index) {
            throw new IllegalArgumentException("no type param "+index);
        }
        return typeParameters.get(index);
    }

    public List<AnnotationDescriptor> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(List<AnnotationDescriptor> annotations) {
        this.annotations = annotations;
    }

    public void addAnnotation(AnnotationDescriptor annotation) {
        if (annotations == null) {
            annotations = new ArrayList<>();
        }
        annotations.add(annotation);
    }

    public boolean hasAnnotation(Class<? extends Annotation> annotationClass) {
        String fqcn = annotationClass.getCanonicalName();
        if (annotations != null) {
            for (AnnotationDescriptor annotationDescriptor : annotations) {
                if (annotationDescriptor.getFqcn().equals(fqcn)) {
                    return true;
                }
            }
        }
        return false;
    }

    public Set<String> getAllReferencedFqcns() {
        Set<String> output = new HashSet<>();
        return getAllReferencedFqcns(output);
    }

    public Set<String> getAllReferencedFqcns(Set<String> output) {
        output.add(typeFqcn);
        if (typeParameters != null) {
            for (TypeDescriptor child : typeParameters) {
                child.getAllReferencedFqcns(output);
            }
        }
        if (annotations != null) {
            for (AnnotationDescriptor annotationDescriptor : annotations) {
                annotationDescriptor.getAllReferencedFqcns(output);
            }
        }
        return output;
    }

    public boolean hasGenerics() {
        return typeParameters != null && !typeParameters.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(getTypeSimpleClassName());
        if (typeParameters!= null && !typeParameters.isEmpty()) {
            result.append("<");
            for (TypeDescriptor typeParam : typeParameters) {
                result.append(typeParam.toString());
                result.append(",");
            }
            result.delete(result.length()-1, result.length());
            result.append(">");
        }
        return result.toString();
    }
}

repo_language: Java

repo_name: radai-rosenblatt/beans


text:
<file_sep># IM-assignments

Upskill ISA (Income Sharing Agreement) collaborated with Intelligent Machines offering a "Machine Learning Course" where only 39 shortlisted candidates are provided with the opportunity to take this course for free. The assignments of this course are completed by me uploaded in this repository.
<file_sep>## Comparison Between Five Time Complexities

### What is Time Complexity?
Time complexity of an algorithm signifies the total time required by the program to run till its completion.

Graph for the following time complexities are shown below:

Logarithmic = θ(log n)

Linear = θ(n)

Quadratic = θ(n²)

Cubic = θ(n³)

Polynomial = θ(n^k)

![Graph](https://user-images.githubusercontent.com/38761302/120105031-26bc2480-c179-11eb-8b57-0a1c83832d13.png)

In the above graph,
Axis - X = Number of input sizes,
Axis - Y = Computational cost.

The more computation cost increases, the higher it takes time to solve the problem.
The graph shows the lowest computation cost among those 5 is θ(logn). So, I can say among five time complexities θ(logn) will be the best solution. Because it is clearly visible that the increasing nature of x value does not increases that much computatinal cost for θ(log n).

branch_name: refs/heads/main


text:
<repo_name>selmanj/awair_exporter<file_sep>/README.md
# awair_exporter

Prometheus exporter for Awair air quality monitor products.

![Example Grafana dashboard](https://user-images.githubusercontent.com/798897/91205603-aaad2180-e6ba-11ea-8c0b-393b2dd20c99.png)

## Example metrics

```
# HELP awair_absolute_humidity_grams_per_cubic_meter Absolute humidity.
# TYPE awair_absolute_humidity_grams_per_cubic_meter gauge
awair_absolute_humidity_grams_per_cubic_meter 10.58
# HELP awair_awair_score Awair score.
# TYPE awair_awair_score gauge
awair_awair_score 83
# HELP awair_co2_est_parts_per_million (Estimated?) CO2; unclear how this metric differs from CO2.
# TYPE awair_co2_est_parts_per_million gauge
awair_co2_est_parts_per_million 627
# HELP awair_co2_parts_per_million CO2.
# TYPE awair_co2_parts_per_million gauge
awair_co2_parts_per_million 1143
# HELP awair_dew_point_celsius Dew point.
# TYPE awair_dew_point_celsius gauge
awair_dew_point_celsius 12.42
# HELP awair_pm10_est_micrograms_per_cubic_meter Likely estimated particulate matter (big particles).
# TYPE awair_pm10_est_micrograms_per_cubic_meter gauge
awair_pm10_est_micrograms_per_cubic_meter 5
# HELP awair_pm25_micrograms_per_cubic_meter Particulate matter (fine-dust).
# TYPE awair_pm25_micrograms_per_cubic_meter gauge
awair_pm25_micrograms_per_cubic_meter 4
# HELP awair_relative_humidity Relative humidity.
# TYPE awair_relative_humidity gauge
awair_relative_humidity 55.59
# HELP awair_scrape_duration_seconds Amount of time spent scraping metrics.
# TYPE awair_scrape_duration_seconds gauge
awair_scrape_duration_seconds 0.020197711
# HELP awair_scrape_errors How many errors occured during the scrape event.
# TYPE awair_scrape_errors gauge
awair_scrape_errors 0
# HELP awair_temp_celsius Temperature.
# TYPE awair_temp_celsius gauge
awair_temp_celsius 21.7
# HELP awair_voc_baseline Unknown, possibly unused?
# TYPE awair_voc_baseline gauge
awair_voc_baseline 2.354942386e+09
# HELP awair_voc_ethanol_raw Unknown, possibly ethanol ppb?
# TYPE awair_voc_ethanol_raw gauge
awair_voc_ethanol_raw 34
# HELP awair_voc_h2_raw Unknown, possibly dihydrogen ppb?
# TYPE awair_voc_h2_raw gauge
awair_voc_h2_raw 25
# HELP awair_voc_parts_per_billion VOC (Volatile organic compounds).
# TYPE awair_voc_parts_per_billion gauge
awair_voc_parts_per_billion 398
```

Not all the metrics are known (e.g. `awair_voc_baseline`) but are exposed regardless.

## Usage

The exporter operates similar to [blackbox_exporter](https://github.com/prometheus/blackbox_exporter) in that one process can scrape multiple instances. If your Awair air quality monitor is running on ip 192.168.86.147  and `awair_exporter` is running on port 8123, you would query it via the command:

```
$ curl -s http://localhost:8123/awair?host=192.168.86.147
```

To get prometheus to scrape metrics and use a custom name, you'd use relabeling similar to how `blackbox_exporter` works. Example:

```
  - job_name: 'awair'
    metrics_path: /awair
    static_configs:
    - targets: ['192.168.86.147']
      labels:
        instance: 'office'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_host
      - target_label: __address__
        replacement: localhost:8123
```

Here I've given a custom name for instance ('office'), but you can use the ip address instead by using a relabel rule of:
```
      - source_labels: [__param_host]
        target_label: instance
```<file_sep>/main.go
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"regexp"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
	hostRegexp = regexp.MustCompile("^[A-Za-z0-9.-]+$")

	// Our metrics
	awairScoreDesc = prometheus.NewDesc(
		"awair_awair_score", "Awair score.", []string{}, nil,
	)
	dewPointDesc = prometheus.NewDesc(
		"awair_dew_point_celsius", "Dew point.", []string{}, nil,
	)
	tempDesc = prometheus.NewDesc(
		"awair_temp_celsius", "Temperature.", []string{}, nil,
	)
	humidDesc = prometheus.NewDesc(
		"awair_relative_humidity", "Relative humidity.", []string{}, nil,
	)
	absHumidDesc = prometheus.NewDesc(
		"awair_absolute_humidity_grams_per_cubic_meter", "Absolute humidity.", []string{}, nil,
	)
	cO2Desc = prometheus.NewDesc(
		"awair_co2_parts_per_million", "CO2.", []string{}, nil,
	)
	cO2EstDesc = prometheus.NewDesc(
		"awair_co2_est_parts_per_million", "(Estimated?) CO2; unclear how this metric differs from CO2.", []string{}, nil,
	)
	vOCDesc = prometheus.NewDesc(
		"awair_voc_parts_per_billion", "VOC (Volatile organic compounds).", []string{}, nil,
	)
	vOCBaselineDesc = prometheus.NewDesc(
		"awair_voc_baseline", "Unknown, possibly unused?", []string{}, nil,
	)
	vOCH2RawDesc = prometheus.NewDesc(
		"awair_voc_h2_raw", "Unknown, possibly dihydrogen ppb?", []string{}, nil,
	)
	vOCEthanolRawDesc = prometheus.NewDesc(
		"awair_voc_ethanol_raw", "Unknown, possibly ethanol ppb?", []string{}, nil,
	)
	pM25Desc = prometheus.NewDesc(
		"awair_pm25_micrograms_per_cubic_meter", "Particulate matter (fine-dust).", []string{}, nil,
	)
	pM10EstDesc = prometheus.NewDesc(
		"awair_pm10_est_micrograms_per_cubic_meter", "Likely estimated particulate matter (big particles).", []string{}, nil,
	)

	// App-specific metrics
	collectTimeDesc = prometheus.NewDesc(
		"awair_scrape_duration_seconds", "Amount of time spent scraping metrics.", []string{}, nil,
	)
	errorsDesc = prometheus.NewDesc(
		"awair_scrape_errors", "How many errors occured during the scrape event.", []string{}, nil,
	)
)

// AwairCollector collects metrics from an Awair instance. Implements the Collector interface.
type AwairCollector struct {
	start time.Time
	url   *url.URL
}

// Describe is implemented by DescribeByCollect
func (ac AwairCollector) Describe(ch chan<- *prometheus.Desc) {
	prometheus.DescribeByCollect(ac, ch)
}

// Collect actually collects those sweet sweet metrics.
func (ac AwairCollector) Collect(ch chan<- prometheus.Metric) {
	errors := 0
	resp, err := ac.SampleApi()
	if err != nil {
		log.Printf("Error scraping metrics: %s\n", err.Error())
		errors++
	} else {
		ch <- prometheus.MustNewConstMetric(awairScoreDesc, prometheus.GaugeValue, float64(resp.Score))
		ch <- prometheus.MustNewConstMetric(dewPointDesc, prometheus.GaugeValue, float64(resp.DewPoint))
		ch <- prometheus.MustNewConstMetric(tempDesc, prometheus.GaugeValue, float64(resp.Temp))
		ch <- prometheus.MustNewConstMetric(humidDesc, prometheus.GaugeValue, float64(resp.Humid))
		ch <- prometheus.MustNewConstMetric(absHumidDesc, prometheus.GaugeValue, float64(resp.AbsHumid))
		ch <- prometheus.MustNewConstMetric(cO2Desc, prometheus.GaugeValue, float64(resp.CO2))
		ch <- prometheus.MustNewConstMetric(cO2EstDesc, prometheus.GaugeValue, float64(resp.CO2Est))
		ch <- prometheus.MustNewConstMetric(vOCDesc, prometheus.GaugeValue, float64(resp.VOC))
		ch <- prometheus.MustNewConstMetric(vOCBaselineDesc, prometheus.GaugeValue, float64(resp.VOCBaseline))
		ch <- prometheus.MustNewConstMetric(vOCH2RawDesc, prometheus.GaugeValue, float64(resp.VOCH2Raw))
		ch <- prometheus.MustNewConstMetric(vOCEthanolRawDesc, prometheus.GaugeValue, float64(resp.VOCEthanolRaw))
		ch <- prometheus.MustNewConstMetric(pM25Desc, prometheus.GaugeValue, float64(resp.PM25))
		ch <- prometheus.MustNewConstMetric(pM10EstDesc, prometheus.GaugeValue, float64(resp.PM10Est))
	}
	// App-specific metrics
	ch <- prometheus.MustNewConstMetric(collectTimeDesc, prometheus.GaugeValue, time.Since(ac.start).Seconds())
	ch <- prometheus.MustNewConstMetric(errorsDesc, prometheus.GaugeValue, float64(errors))

}

func (ac AwairCollector) SampleApi() (*AwairAirDataResponse, error) {
	resp, err := http.Get(ac.url.String())
	if err != nil {
		return nil, fmt.Errorf("Unable to poll url %s: %w", ac.url.String(), err)
	}
	defer resp.Body.Close()
	dec := json.NewDecoder(resp.Body)
	var awairResponse AwairAirDataResponse
	if err := dec.Decode(&awairResponse); err != nil {
		return nil, fmt.Errorf("Invalid response: %w", err)
	}

	return &awairResponse, nil
}

type AwairAirDataResponse struct {
	// ISO 8601, example: "2020-08-09T05:35:28.034Z"
	Timestamp string  `json:"timestamp"`
	Score     float64 `json:"score"`
	DewPoint  float64 `json:"dew_point"`
	Temp      float64 `json:"temp"`
	Humid     float64 `json:"humid"`
	AbsHumid  float64 `json:"abs_humid"`
	CO2       float64 `json:"co2"`
	CO2Est    float64 `json:"co2_est"`
	VOC       float64 `json:"voc"`
	// Example value: 2352254740, unsure why so high
	VOCBaseline   float64 `json:"voc_baseline"`
	VOCH2Raw      float64 `json:"voc_h2_raw"`
	VOCEthanolRaw float64 `json:"voc_ethanol_raw"`
	PM25          float64 `json:"pm25"`
	PM10Est       float64 `json:"pm10_est"`
}

func AwairHandler() http.Handler {
	return http.HandlerFunc(func(rsp http.ResponseWriter, req *http.Request) {
		// Parse host address
		h := req.URL.Query().Get("host")
		if h == "" {
			rsp.WriteHeader(http.StatusBadRequest)
			_, err := rsp.Write([]byte("host query parameter is required"))
			if err != nil {
				log.Printf("Error writing response: %s", err.Error())
			}
			return
		}
		if hostRegexp.FindString(h) == "" {
			rsp.WriteHeader(http.StatusBadRequest)
			_, err := rsp.Write([]byte(fmt.Sprintf("host query parameter does not match valid hostname: %s", hostRegexp.String())))
			if err != nil {
				log.Printf("Error writing response: %s", err.Error())
			}
			return
		}
		u := &url.URL{
			Scheme: "http",
			Host:   h,
			Path:   "air-data/latest",
		}

		// TODO probably pretty wasteful to create these dynamically
		reg := prometheus.NewRegistry()
		ac := &AwairCollector{url: u, start: time.Now()}
		reg.MustRegister(ac)

		promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(rsp, req)
	})
}

var addr = flag.String("listen-address", ":8123", "The address to listen on for HTTP requests.")

func main() {
	flag.Parse()
	http.Handle("/metrics", promhttp.Handler())
	http.Handle("/awair", AwairHandler())
	log.Printf("Listening for http connections on %s\n", *addr)
	log.Fatal(http.ListenAndServe(*addr, nil))
}
<file_sep>/go.mod
module github.com/selmanj/awair_exporter

go 1.13

require github.com/prometheus/client_golang v1.7.1

languages:
Go Module
Markdown
Go

repo_language: Go Module


text:
<file_sep>package com.zou.designPattern.create.factoryMethod;

import com.zou.designPattern.entity.Fruit;

public class Grape implements Fruit {
    private int price = 5;

    public Grape() {
    }

    public Grape(int price) {
        this.price = price;
    }

    @Override
    public int price() {
        return price;
    }

    @Override
    public void draw() {
        System.out.println("生产葡萄" + price);
    }

    public void setPrice(int price) {
        this.price = price;
    }
}
<file_sep>package com.zou.designPattern.create.abstractFactory;

import com.zou.designPattern.entity.Fruit;

public class AbstractFactoryTest {
    public static void main(String[] args) {
        AbstractFactory appleFactory = new AppleFactory();
        Fruit fruit = appleFactory.getFruit();
        fruit.draw();

        Bag bag = appleFactory.getBag();
        bag.pack();

    }
}
<file_sep>package com.zou.designPattern.structure.bridge.material;

/**
 * 材料属性
 */
public interface Material {
    void type();
}
<file_sep>package com.zou.designPattern.create.factoryMethod;

import com.zou.designPattern.entity.Fruit;

public class GrapeFactory implements FruitFactory {

    @Override
    public Fruit getFruit() {
        return new Grape();
    }
}
<file_sep>package com.zou.designPattern.create.abstractFactory;

public class AppleBag implements Bag {

    @Override
    public void pack() {
        System.out.println("使用苹果箱包装");
    }
}
<file_sep>package com.zou.designPattern.create.factoryMethod;

import com.zou.designPattern.entity.Fruit;

/**
 * 工厂方法
 */
public class FruitFactoryTest {
    public static void main(String[] args) {
        FruitFactory appleFactory = new AppleFactory();
        Fruit fruit = appleFactory.getFruit();
        fruit.draw();

        FruitFactory grapeFactory = new GrapeFactory();
        Fruit fruit1 = grapeFactory.getFruit();
        fruit1.draw();
    }
}
<file_sep># designPattern
将Java中的设计模式做一个专题，分为三类：创建型、行为型、结构型

前期是模式的简介和简单例子，后期分析设计模式在spring、springboot、mybatis中的使用。

##设计模式的六大原则

- 开闭原则：对扩展开放，对修改关闭。
- 里氏代换原则：任何几类出现的地方子类也可以出现。
- 依赖倒转原则：依赖于抽象，而不依赖于实现。
- 接口隔离原则：一个接口只为一个类服务，降低类的耦合度。
- 迪米特法则：一个类应当尽量少于其他实体类之间发生相互作用，使得系统模块之间相对独立。
- 合成复用原则：尽量使用合成/聚合的方式，而不是使用继承。

##一：创建型
对象的创建和对象的使用分开也就成为了必然趋势。因为对象的创建会消耗掉系统的很多资源，所以单独对对象的创建进行研究，从而能够高效地创建对象就是创建型模式要探讨的问题。
###1.简单工厂模式
由工厂管理对象的创建，实现较为简单。
优点在于对象少时管理方便；
缺点在于如果需要频繁增加对象，就需要频繁改动工厂的代码。不符合开闭原则和单一职责。

###2.工厂方法模式
在简单工厂的基础上，将一个生产多个对象的工厂拆分成每个对象分别由对应的工厂创建。
优点在于符合开闭原则，对工厂的扩展开放，对修改关闭。符合单一职责，一个类只负责一个对象。
缺点在于如果工厂生产很多品种的产品，需要很多工厂。

###3.抽象工厂模式
当一个类别的产品有多个系列时，采用工厂方法需要重新建立多套工厂体系生产不同系列的产品，再将不同体系的多个产品关联。往往会产生不同类别的多个系列产品关联在一起，过程中也不会报错直到拿到最终产品。
抽象工厂就是将一个类别多个系列的产品采用同一个工厂

###4.建造者模式
将一个复杂对象的构造与它的表示分离，使同样的构建过程可以创建不同的表示，这样的设计模式被称为建造者模式。

###5.单例模式
单例模式很简单了，懒汉式和饿汉式，一般写懒汉式，多线程下用双重判断。

##二：结构型
在解决了对象的创建问题之后，对象的组成以及对象之间的依赖关系就成了开发人员关注的焦点，因为如何设计对象的结构、继承和依赖关系会影响到后续程序的维护性、代码的健壮性、耦合性等。对象结构的设计很容易体现出设计人员水平的高低。
###1.适配器模式
适配器模式是两个已知的对象做适配，继承要适配的对象，将另一个对象作为构造参数传入进来。
类似于亡羊补牢。

###2.桥接模式
在软件系统中，某些类型由于自身的逻辑，它具有两个或多个维度的变化，利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化，而又不引入额外的复杂度，这就要使用Bridge模式。
有三个关键词：抽象化、实现化、脱耦。

- 抽象化： 抽象化就是忽略一些信息，从而把不同的实体当做相同的实体对待。即先用最重要的维度去创建实体，再关联另一个维度。
- 实现化： 抽象化给出的具体实现，就是实现化。
- 脱耦： 将强关联去掉或者换成弱关联，将两个角色之间的继承关系改为聚合关系，就是将它们之间的强关联改换成为弱关联。因此，桥梁模式中的所谓脱耦，就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系，从而使两者可以相对独立地变化。这就是桥梁模式的用意。

###3.装饰器模式
向一个现有的对象添加新的功能，同时又不改变其结构。创建了一个装饰类，用来包装原有的类，并在保持类方法签名完整性的前提下，提供了额外的功能。

###4.代理模式
在代理模式（Proxy Pattern）中，一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。
在代理模式中，我们创建具有现有对象的对象，以便向外界提供功能接口。

###5.组合模式

###6.外观模式
外观模式（Facade Pattern）隐藏系统的复杂性，并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式，它向现有的系统添加一个接口，来隐藏系统的复杂性。
这种模式涉及到一个单一的类，该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
简单说就是封装一个单一类去供外部调用，通常在构造方法中初始化要调用的对象，然后添加调用初始化后对象的方法即可，将这些方法暴露给外部使用。

##三：行为型
在对象的结构和对象的创建问题都解决了之后，就剩下对象的行为问题了，如果对象的行为设计的好，那么对象的行为就会更清晰，它们之间的协作效率就会提高。
###1.模板方法模式
在模板模式（Template Pattern）中，一个抽象类公开定义了执行它的方法的方式/模板。它的子类可以按需要重写方法实现，但调用将以抽象类中定义的方式进行。
主要解决：一些方法通用，却在每一个子类都重新写了这一方法。
何时使用：有一些通用的方法。
如何解决：将这些通用算法抽象出来。
关键代码：在抽象类实现，其他步骤在子类实现。
场景：
1. 买东西到结账，结账可以在线或者现金支付，可以把支付的方法用模板方法；

###2.策略模式
在策略模式（Strategy Pattern）中，一个类的行为或其算法可以在运行时更改。
在策略模式中，我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。
主要解决：在有多种算法相似的情况下，使用 if...else 所带来的复杂和难以维护。
何时使用：一个系统有许多许多类，而区分它们的只是他们直接的行为。
如何解决：将这些算法封装成一个一个的类，任意地替换。
关键代码：实现同一个接口。
优点： 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。
缺点： 1、策略类会增多。 2、所有策略类都需要对外暴露。

###3.职责链模式
责任链模式（Chain of Responsibility Pattern）为请求创建了一个接收者对象的链。这种模式给予请求的类型，对请求的发送者和接收者进行解耦。
在这种模式中，通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求，那么它会把相同的请求传给下一个接收者，依此类推。
意图：避免请求发送者与接收者耦合在一起，让多个对象都有可能接收请求，将这些对象连接成一条链，并且沿着这条链传递请求，直到有对象处理它为止。
主要解决：职责链上的处理者负责处理请求，客户只需要将请求发送到职责链上即可，无须关心请求的处理细节和请求的传递，所以职责链将请求的发送者和请求的处理者解耦了。
何时使用：在处理消息的时候以过滤很多道。
如何解决：拦截的类都实现统一接口。
优点： 1、降低耦合度。它将请求的发送者和接收者解耦。 2、简化了对象。使得对象不需要知道链的结构。 3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序，允许动态地新增或者删除责任。 4、增加新的请求处理类很方便。
缺点： 1、不能保证请求一定被接收。 2、系统性能将受到一定影响，而且在进行代码调试时不太方便，可能会造成循环调用。 3、可能不容易观察运行时的特征，有碍于除错。

###4.观察者模式

###5.命令模式

###6.访问者模式

<file_sep>package com.zou.designPattern.structure.bridge.material;

public class Paper implements Material {
    @Override
    public void type() {
        System.out.println("我是 paper");
    }
}
<file_sep>package com.zou.designPattern.create.abstractFactory;

import com.zou.designPattern.entity.Fruit;

public interface AbstractFactory {
    Fruit getFruit();

    Bag getBag();
}
<file_sep>package com.zou.designPattern.structure.adapter;

import com.zou.designPattern.create.abstractFactory.AppleBag;
import com.zou.designPattern.entity.Orange;

public class OrangeBagAdapterTest {
    public static void main(String[] args) {
        Orange orange = new Orange("o", 10);
        OrangeBag orangeBag = new OrangeBagAdapter(new AppleBag());
        orange.pack(orangeBag);
    }
}
<file_sep>package com.zou.designPattern.structure.bridge.material;

public class Plastic implements Material {
    @Override
    public void type() {
        System.out.println("我是 Plastic");
    }
}
<file_sep>package com.zou.designPattern.create.simple;

import com.zou.designPattern.entity.Fruit;

/**
 * 简单工厂
 */
public class SimpleFactoryTest {
    public static void main(String[] args) {
        Fruit apple = SimpleFactory.getApple();
        System.out.println(apple.price());
        apple.draw();
    }
}
<file_sep>package com.zou.designPattern.structure.decorator;

import com.zou.designPattern.create.abstractFactory.Bag;

public class CheckBagDecorator extends BagDecorator {
    public CheckBagDecorator(Bag bag) {
        super(bag);
    }

    @Override
    public void pack() {
        super.pack();
        cheak();
    }

    //扩展的方法
    private void cheak() {
        System.out.println("我是扩展的方法");
        System.out.println("打上防伪标签");
    }
}
<file_sep>package com.zou.designPattern.create.singleton;

import java.util.concurrent.atomic.AtomicInteger;

public class SessionCount {
    private static AtomicInteger count = new AtomicInteger(0);
    private static SessionCount instance = null;

    private SessionCount() {
    }

    public static SessionCount getInstance() {
        if (instance == null) {
            synchronized (SessionCount.class) {
                if (instance == null) {
                    instance = new SessionCount();
                }
            }
        }
        return instance;
    }

    public int plus() {
        return count.incrementAndGet();
    }

    public int decrease() {
        return count.decrementAndGet();
    }

    public void showMessage() {
        System.out.println("当前人数为" + count);
    }

}
<file_sep>package com.zou.designPattern.create.abstractFactory;

import com.zou.designPattern.entity.Apple;
import com.zou.designPattern.entity.Fruit;

public class AppleFactory implements AbstractFactory {
    @Override
    public Fruit getFruit() {
        return new Apple();
    }

    @Override
    public Bag getBag() {
        return new AppleBag();
    }
}
<file_sep>package com.zou.designPattern.structure.decorator;

import com.zou.designPattern.create.abstractFactory.AbstractFactory;
import com.zou.designPattern.create.abstractFactory.AppleFactory;
import com.zou.designPattern.create.abstractFactory.Bag;
import com.zou.designPattern.entity.Fruit;

public class DecoratorTest {
    public static void main(String[] args) {

        AbstractFactory appleFactory = new AppleFactory();
        Fruit fruit = appleFactory.getFruit();
        Bag bag = appleFactory.getBag();
        fruit.draw();
        bag.pack();

        System.out.println("---------");

        BagDecorator bagDecorator = new CheckBagDecorator(bag);
        bagDecorator.pack();
    }
}
<file_sep>package com.zou.designPattern.create.builder;

public interface Builder {
    void buildApple(int price);

    void buildOrange(int price);

    FruitMeal getFruitMeal();//返回创建的套餐
}
<file_sep>package com.zou.designPattern.create.builder;

public class FruitMealTest {
    public static void main(String[] args) {
        construct();
    }

    private static void construct() {
        Builder builder = new DisCountBuilder();
        builder.buildApple(11);
        builder.buildOrange(11);

        FruitMeal fruitMeal = builder.getFruitMeal();
        fruitMeal.showItems();//折后价
    }
}
<file_sep>package com.zou.designPattern.action.templatePattern;

public class Basketball extends Game {
    @Override
    void init() {
        System.out.println(this.getClass().getName() + " " + "init");
    }

    @Override
    void playing() {
        System.out.println(this.getClass().getName() + " " + "playing");

    }

    @Override
    void end() {
        System.out.println(this.getClass().getName() + " " + "end");
    }

}
<file_sep>package com.zou.designPattern.structure.bridge.bag;

/**
 * 采摘小袋
 * Material
 */
public class SmallBag extends AbstractionBag {

    public void pick() {
        System.out.println("采摘水果开始");
        this.material.type();
        System.out.println("采摘了一小包");
    }

}
<file_sep>package com.zou.designPattern.action.chain;

public class ChainTest {
    public static void main(String[] args) {
        AbstractLogger chainOfLoggers = getChainOfLoggers();
        chainOfLoggers.logMessage(AbstractLogger.INFO, "info");
        System.out.println("-----------");
        chainOfLoggers.logMessage(AbstractLogger.DEBUG, "DEBUG");
        System.out.println("-----------");

        chainOfLoggers.logMessage(AbstractLogger.ERROR, "ERROR");

    }

    private static AbstractLogger getChainOfLoggers() {
        AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
        AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);
        AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);
        errorLogger.setLogger(fileLogger);
        fileLogger.setLogger(consoleLogger);
        return errorLogger;
    }
}
<file_sep>package com.zou.designPattern.create.factoryMethod;

import com.zou.designPattern.entity.Fruit;

public interface FruitFactory {
    Fruit getFruit();
}
<file_sep>package com.zou.designPattern.create.builder;

import com.zou.designPattern.entity.Apple;
import com.zou.designPattern.entity.Orange;

//创建一个水果的套餐
public class FruitMeal {
    private Apple apple;
    private Orange orange;
    private int disCount;//折扣价
    private int totolPrice;//原价
    private int after;//折后价

    public Apple getApple() {
        return apple;
    }

    public void setApple(Apple apple) {
        this.apple = apple;
    }

    public Orange getOrange() {
        return orange;
    }

    public void setOrange(Orange orange) {
        this.orange = orange;
    }

    public int getDisCount() {
        return disCount;
    }

    public void setDisCount(int disCount) {
        this.disCount = disCount;
    }

    public int getTotolPrice() {
        return totolPrice;
    }

    public void setTotolPrice(int totolPrice) {
        this.totolPrice = totolPrice;
    }

    public void cost() {
        if (null != apple) {
            totolPrice += apple.price();
        }
        if (null != orange) {
            totolPrice += orange.price();
        }
        System.out.println("原价为：" + totolPrice);
    }

    public void init() {
        if (totolPrice > 0 && disCount > 0) {
            after = totolPrice - disCount;
        }
    }

    public void showItems() {
        System.out.println(after);
    }

}
<file_sep>package com.zou.designPattern.action.chain;

public abstract class AbstractLogger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;
    protected int level;
    protected AbstractLogger logger;//下一个元素

    public AbstractLogger getLogger() {
        return logger;
    }

    public void setLogger(AbstractLogger logger) {
        this.logger = logger;
    }

    public void logMessage(int level, String message) {
        if (this.level <= level) {
            write(message);
        }
        if (logger != null) {
            logger.logMessage(level, message);
        }
    }

    abstract void write(String message);
}
<file_sep>package com.zou.designPattern.action.templatePattern;

public abstract class Game {
    abstract void init();

    abstract void playing();

    abstract void end();

    //模板方法
    public final void play() {
        init();
        playing();
        end();
    }
}
<file_sep>package com.zou.designPattern.structure.bridge.bag;

import com.zou.designPattern.structure.bridge.material.Material;

/**
 * 抽象容器,容器的大小和材质两个维度不同，采用桥接模式，先忽略掉材质的属性，用容器的大小的维度创建实体，
 * 再将材质通过set方法传入进来。
 */
public abstract class AbstractionBag {
    protected Material material;

    public Material getMaterial() {
        return material;
    }

    public void setMaterial(Material material) {
        this.material = material;
    }

    //采摘
    public abstract void pick();
}
<file_sep>package com.zou.designPattern.create.factoryMethod;

import com.zou.designPattern.entity.Apple;
import com.zou.designPattern.entity.Fruit;

public class AppleFactory implements FruitFactory {
    @Override
    public Fruit getFruit() {
        return new Apple();
    }
}

languages: Java Markdown

repo_name: zou-weidong/designPattern


text:
<file_sep>---
title: "Clustering Wholesale Customers Using Kmeans : (Optimal K - Silhouette Method)"
output: html_notebook
---

#### Introduction

  This demonstration is about clustering using **Kmeans** and also determining the optimal number of clusters (k) using **Silhouette Method**.
  The code for this article is hosted here.

#### Dataset

  This data set is taken from UCI Machine Learning Repository. The data set refers to clients of a wholesale distributor. It includes the annual spending in monetary units (m.u.) on diverse product categories

Attribute Information:

1)	FRESH: annual spending (m.u.) on fresh products (Continuous);
2)	MILK: annual spending (m.u.) on milk products (Continuous);
3)	GROCERY: annual spending (m.u.)on grocery products (Continuous);
4)	FROZEN: annual spending (m.u.)on frozen products (Continuous)
5)	DETERGENTS_PAPER: annual spending (m.u.) on detergents and paper products (Continuous)
6)	DELICATESSEN: annual spending (m.u.)on and delicatessen products (Continuous);
7)	CHANNEL: customers Channel - Horeca (Hotel/Restaurant/Cafe) or Retail channel (Nominal)
8)	REGION: customers Region  Lisnon, Oporto or Other (Nominal)

#### Loading the libraries
`cluster` library is loaded for clustering algorithms and `factoextra` is loaded for visualization.
```{r}
# install.packages('corrplot')
library(corrplot)

# Clustering
library(cluster)
library(factoextra)
```

#### Loading Data set
```{r}
data <- read.csv('Wholesale customers data.csv', header = TRUE)
head(data,5)
```
#### EDA

* Structure of data
* Summary statistics
* Correlation Plot

```{r}
data <- na.omit(data)

str(data)

summary(data)

corrmatrix <- cor(data)
corrplot(corrmatrix, method = 'number')
```

#### Data Preparation

  All the attributes are of same scale except **"channel"** and **"region"**. We can ignore those attributes for clustering and normalization is not required.

```{r}
df <- data[-c(1,2)]
head(df,3)
```

### Clustering

#### Kmeans

  Kmeans algorithm (also referred as Lloyd's algorithm) is the most commonly used unsupervised machine learning algorithm used to partition the data into a set of k groups or clusters.

#### How Kmeans works?

1. Define the number of clusters (k).
2. Initialize k centroids by randomly.
3. **Assignment Step:** Assign each observation to the closest centroid (center-point) by calculting least squared euclidean distance between centroids and observations. (i.e. least squared euclidean distance between assigned center and observation should be minimum than other centers).
4. **Update Step:** Calculate the new means as centroids for new clusters.
5. Repeat both assignment and update step (i.e. steps 3 & 4) until convergence (minimum total sum of square) or maximum iteration is reached.

#### Determining optimal number of clusters (k)

  Before we do the actual clustering, we need to identity the Optimal number of clusters (k) for this data set of wholesale customers. The popular way of determining number of clusters are

1. Elbow Method
2. **Silhouette Method**
3. Gap Static Method

  Elbow and  Silhouette methods are **direct** methods and gap statistic method is the **statistics** method.

In this demonstration, we are going to see how silhouette method is used.

```{r}
silhouette_score <- function(k){
  km <- kmeans(df, centers = k, nstart=25)
  ss <- silhouette(km$cluster, dist(df))
  mean(ss[, 3])
}

k <- 2:10

avg_sil <- sapply(k, silhouette_score)
plot(k, type='b', avg_sil, xlab='Number of clusters', ylab='Average Silhouette Scores', frame=FALSE)

```
*Note:* The kmeans() function has an nstart option that attempts multiple initial configurations and reports on the best one. For example, adding nstart=25 will generate 25 initial configurations. This approach is often recommended.

The above method of calculating silhouette score using `silhouette()` and plotting the results states that **optimal number of clusters as 2**

The other method with visual aid is using `factoextra` package

```{r}
fviz_nbclust(df, kmeans, method='silhouette')
```

**The optimal number of clusters is 2.**

#### Actual Clustering
```{r}
km.final <- kmeans(df, 2)

## Total Within cluster sum of square
km.final$tot.withinss

## Cluster sizes
km.final$size

data$cluster <- km.final$cluster

head(data, 6)

```

```{r}
fviz_cluster(km.final, data=df)

clusplot(data, data$cluster, color=TRUE, shade = TRUE, label=2)
```
#### Conclusion

  We had seen kmeans clustering of wholesale customers and how average silhouette measure is used to identify optimal number of clusters. In the next article, we will see about various other methods for determining optimal number of clusters.

#### References
1. https://home.deib.polimi.it/matteucc/Clustering/tutorial_html/kmeans.html
2. https://www.rdocumentation.org/packages/cluster/versions/2.0.7-1/topics/silhouette

repo_language: RMarkdown