Skip to content

Instantly share code, notes, and snippets.

View astagi's full-sized avatar
💫

Andrea Stagi astagi

💫
View GitHub Profile
@astagi
astagi / remove_all_sessions.py
Created December 18, 2018 11:07
Remove all sessions from Django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'ares.settings'
import django
django.setup()
import datetime
from django.conf import settings
from django.contrib.auth import logout
require 'yard'
require 'fileutils'
YARD::Rake::YardocTask.new do |t|
dynamic_dir = File.join('lib', 'ansa', 'docs')
dynamic_file = File.join(dynamic_dir, 'dynamic_docs.rb')
FileUtils.mkdir_p dynamic_dir
f = File.new(dynamic_file, 'w')
f.write("module Ansa")
Ansa::LINKS.each do |category, url|
module Ansa
generate_get_news 'homepage', 'http://www.ansa.it/sito/ansait_rss.xml'
generate_get_news 'news', 'http://www.ansa.it/sito/notizie/cronaca/cronaca_rss.xml'
generate_get_news 'politics', 'http://www.ansa.it/sito/notizie/politica/politica_rss.xml'
generate_get_news 'world', 'http://www.ansa.it/sito/notizie/mondo/mondo_rss.xml'
generate_get_news 'economy', 'http://www.ansa.it/sito/notizie/economia/economia_rss.xml'
generate_get_news 'soccer', 'http://www.ansa.it/sito/notizie/sport/calcio/calcio_rss.xml'
generate_get_news 'sport', 'http://www.ansa.it/sito/notizie/sport/sport_rss.xml'
end
@astagi
astagi / client.rb
Last active September 27, 2018 12:48
Try to use macro
module Ansa
private
# @!macro [attach] generate_get_news
# @method get_$1_news
# @!scope class
# @note This method is autogenerated
# Get news for "$1" category.
# @raise [Ansa::AnsaError] when errors occour fetching news
# @return [Array<Ansa::News>] all the news of category "$1".
def self.generate_get_news(category, url)
@astagi
astagi / client.rb
Last active September 27, 2018 12:50
Dynamic generation
module Ansa
# ....
private
def self.generate_get_news(category, url)
define_singleton_method("get_#{category}_news") do
get_news_by_url(url)
end
end
public
@astagi
astagi / main.rb
Created September 27, 2018 12:01
Try ansa gem
require 'ansa'
puts "\n⚽️ SOCCER NEWS ⚽️\n\n"
begin
Ansa::get_soccer_news.each do |news|
puts "🥅 [#{news.date.strftime("%d/%m %H:%M:%S")}] #{news.title}"
puts news.description
puts "\n"
end
@astagi
astagi / _newmath.c
Last active September 14, 2018 21:58
#define Py_LIMITED_API
#include <Python.h>
#include "libnewmath.h"
PyObject *sum_wrapper(PyObject *obj, PyObject *args) {
const long a, b;
if (!PyArg_ParseTuple(args, "LL", &a, &b))
return NULL;
@astagi
astagi / newmath.go
Last active September 14, 2018 21:58
package main
import "C"
//export sum
func sum(a, b int) int {
return (a + b)
}
#include <Python.h>
// ...
int is_a_long(PyObject * p) {
return PyLong_Check(p);
}
#define Py_LIMITED_API
#include <Python.h>
int PyArg_ParseTuple_LL(
PyObject * args,
long long * a,
long long * b
) {
return PyArg_ParseTuple(args, "LL", a, b);
}