Skip to content

Instantly share code, notes, and snippets.

Vinicius Roggério da Rocha viniroger

Block or report user

Report or block viniroger

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@viniroger
viniroger / trycatch.R
Created Apr 22, 2019
Try catch read CSV file with error function and warning print
View trycatch.R
diretorio = '/home/user/model'
uid = 1
filename = sprintf('%s/saida/saida_%s/prevaz_%s.csv', diretorio, 'semanal', uid)
values = tryCatch(read.csv(filename,as.is = TRUE),
error = function(e) NA, warning = function(w) print('Without semanal file'))
@viniroger
viniroger / randompass.sh
Created Mar 26, 2019
Generate random password
View randompass.sh
openssl rand -base64 8
@viniroger
viniroger / fill_random.py
Created Mar 21, 2019
Fill a column from a pandas dataframe with random values
View fill_random.py
import random
random.seed(42)
df['column_name'] = df['column_name'].apply(lambda v: random.random() * 100)
@viniroger
viniroger / print_all_df.py
Created Feb 28, 2019
Pretty-print an entire Pandas Series / DataFrame
View print_all_df.py
def print_all_df(self, df):
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df)
@viniroger
viniroger / bash_execution.py
Created Feb 28, 2019
Execute bash script using python
View bash_execution.py
from subprocess import check_output
# List files using pattern and get most recent
bashCommand = ("ls -1t %s/saida/arquivos_clientes/cn*.txt | head -1" %path)
# Define file name as last string, striping by '/'
filename = str(check_output(bashCommand, shell=True).strip(), 'utf-8')
remoteFileName = filename.split('/')[-1]
@viniroger
viniroger / lambda_ex.py
Created Feb 21, 2019
Example of lambda function use with conditional statment - Python
View lambda_ex.py
# Example of Conditional statements using lambda funtion
dates = ['2019-02-20','2019-02-21','2019-02-22']
td = '2019-02-21'
i = 0
# Conditional statement
if dates[i] < td:
print('obs')
else:
print('prev')
# Conditional statement with lambda
View tricks.R
# Iniciar tabela vazia
tabela = data.frame()
# Cortar período
serie_model = subset(dados, data > ini_model)
# Último mês de uma sequência de 14 meses
ultimo_mes_prev = tail(seq(as.Date(fim_model), by = "month", length = 14),n=1)
@viniroger
viniroger / concat_columns.py
Created Aug 7, 2018
Juntar colunas de arquivos diferentes em um só arquivo
View concat_columns.py
# -*- coding: utf-8 -*-
# Script para juntar colunas de 2 arquivos com mesmo número de linhas em um só
import pandas as pd
import csv
# Abrir arquivos com colunas separadas por ";" e decimais por ","
df1 = pd.read_csv('dados.csv',delimiter=';',decimal=',')
df2 = pd.read_csv('outros_dados.csv',delimiter=';',decimal=',')
# Concatenar colunas
@viniroger
viniroger / ping_test.sh
Last active Jun 5, 2018
Test many IP addresses and save output on log file
View ping_test.sh
#!/bin/bash
# Script to test many IP addresses
ips=( '192.168.0.1' '192.168.0.2' '192.168.0.4' '192.168.0.12' '192.168.0.102' )
descr=( 'Host 1' 'Host 2' 'Host 3' 'Host 4' 'Host 5' )
filename='pings_'$(cat /etc/hostname)'.txt'
rm -f $filename; touch $filename
n=0
for ip in "${ips[@]}"; do
@viniroger
viniroger / write_week.R
Created May 18, 2018
Função para escrever número da semana entrando com data formato YYYY-mm-dd (conferir em http://whatweekisit.org/calendar-2018.html)
View write_week.R
date_ini = '2018-05-10'
get_week_numbers = function(date_ini){
# Week number of first day from next month (1st week)
week_ini = as.integer(format(as.Date(date_ini), '%W'))
# Week number of first day from next-next month (4th or 5th week) minus 1 (+2 pra completar 6 semanas)
date_end_temp = as.character(as.Date(date_ini) + 31)
date_end = paste(strsplit(date_end_temp, '-')[[1]][1], strsplit(date_end_temp, '-')[[1]][2], '01', sep = '-')
week_end = as.integer(format(as.Date(date_end), '%W')) - 1 + 2
# Return array with year and week numbers
year = format(as.Date(date_ini), '%Y')
You can’t perform that action at this time.