Skip to content

Instantly share code, notes, and snippets.

View lewoudar's full-sized avatar
🏠
Working from home

Kevin Tewouda lewoudar

🏠
Working from home
  • Paris
View GitHub Profile
@lewoudar
lewoudar / green_static_spider.py
Created November 8, 2020 17:22
Example of pyscalpel static spider using the gevent backend
from datetime import datetime
from pathlib import Path
from scalpel import Configuration
from scalpel.green import StaticSpider, StaticResponse, read_mp
def parse(spider: StaticSpider, response: StaticResponse) -> None:
for quote in response.xpath('//div[@class="quote"]'):
data = {
@lewoudar
lewoudar / green_selenium_spider.py
Created November 8, 2020 21:23
Example of pyscalpel selenium spider using the gevent backend
from datetime import datetime
from pathlib import Path
from scalpel import Configuration, datetime_decoder
from scalpel.green import SeleniumSpider, SeleniumResponse, read_mp
def parse(spider: SeleniumSpider, response: SeleniumResponse) -> None:
for block in response.driver.find_elements_by_xpath('//div[@class="opblock-tag-section"]'):
block.click()
@lewoudar
lewoudar / anyio_dual_server.py
Last active November 20, 2023 19:11
An example of a server supporting tcp and udp with anyio
import signal
import anyio
async def signal_handler(scope):
with anyio.open_signal_receiver(signal.SIGINT, signal.SIGTERM) as signals:
async for signum in signals:
if signum == signal.SIGINT:
print('Ctrl+C pressed!')
@lewoudar
lewoudar / click_wc.py
Last active November 29, 2020 23:26
Example of wc command implemented with click
import re
import click
@click.command()
@click.option('-m', 'chars', is_flag=True, help='number of characters')
@click.option('-l', 'lines', is_flag=True, help='number of lines')
@click.option('-c', 'byte', is_flag=True, help='number of bytes')
@click.option('-w', 'words', is_flag=True, help='number of words')
@click.argument('filename', type=click.Path(exists=True, dir_okay=False))
@lewoudar
lewoudar / click_test_pycat.py
Created November 30, 2020 18:02
An example of how to test a cli application with click
from click.testing import CliRunner
from scripts.pycat import cli
def test_pycat(tmp_path):
hello_file = tmp_path / 'hello.txt'
hello_file.write_text('hello world!')
runner = CliRunner()
result = runner.invoke(cli, [f'{hello_file.resolve()}'])
@lewoudar
lewoudar / click_less.py
Created November 30, 2020 19:14
Example of less command implemented with click
import click
@click.command()
@click.argument('file', type=click.File())
def cli(file):
"""Read file part by part"""
click.echo_via_pager(file.read())
@lewoudar
lewoudar / click_ptr.py
Created December 1, 2020 07:02
An implementation of a cli returning the ptr name of an ip address
import ipaddress
import click
class IPParamType(click.ParamType):
name = 'ip address'
def convert(self, value, param, ctx):
try:
return ipaddress.ip_address(value)
@lewoudar
lewoudar / click_img_downloader.py
Last active December 4, 2020 07:55
Implementation of a click CLI capable of downloading multiple images concurrently
from pathlib import Path
from typing import List
import anyio
import click
import httpx
from rich.console import Console
from rich.progress import Progress, TaskID
@lewoudar
lewoudar / disney_packet.py
Created March 11, 2021 20:52
Basic example usage of kifurushi
import enum
from kifurushi import Packet, ShortField, ByteField, IntEnumField
class Mood(enum.Enum):
happy = 1
cool = 2
angry = 4
@lewoudar
lewoudar / send_kifurushi_packet.py
Created March 11, 2021 21:07
An example showing how to manipulate a kifurushi packet
import socket
import enum
from kifurushi import Packet, ShortField, ByteField, IntEnumField
HOST = 'disney-stuff.com'
PORT = 14006
class Mood(enum.Enum):
happy = 1